-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MASS objective + transform #16
Conversation
smoketested on unpc. requires a thorough review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this should work. Some changes would improve clarity.
onmt/tests/test_transform.py
Outdated
@@ -22,22 +22,22 @@ def test_transform_register(self): | |||
"sentencepiece", | |||
"bpe", | |||
"onmt_tokenize", | |||
"bart", | |||
"ae_noise", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The transform is named ae_noise
but the file is named denoising
. Would it be better to use only one of the names in both locations?
@@ -461,7 +461,7 @@ def _gradient_accumulation_over_lang_pairs( | |||
seen_comm_batches.add(comm_batch) | |||
if self.norm_method == "tokens": | |||
num_tokens = ( | |||
batch.tgt[1:, :, 0].ne(self.train_loss_md[f'trainloss{metadata.tgt_lang}'].padding_idx).sum() | |||
batch.labels[1:, :, 0].ne(self.train_loss_md[f'trainloss{metadata.tgt_lang}'].padding_idx).sum() | |||
) | |||
normalization += num_tokens.item() | |||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later on line 484 tgt is used: tgt_outer = batch.tgt
.
If I read this correctly, the idea is that tgt_outer
is used only as the input sequence to the decoder. The whole batch is then given to the loss function self.train_loss_md
which will take care of using the label sequence instead.
This is not easy to figure out, because the loss function is wrapped in several layers of onmt scaffolding. A comment would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Full disclosure: I did minimal edits. I don't know why the code doesn't use batch.tgt
directly line 490. I try not to fix what's not broken.
@@ -328,6 +330,7 @@ def _make_shard_state(self, batch, output, range_, attns=None): | |||
shard_state = { | |||
"output": output, | |||
"target": batch.tgt[range_start:range_end, :, 0], | |||
"labels": batch.labels[range_start:range_end, :, 0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may not actually be necessary to have separate "target" and "labels" here. As this is the loss, it is only concerned with comparing the output to the desired labels. The decoder input ("target") shouldn't be needed, and as far as I can tell is not used.
This way is clearer, though. Also, slicing a tensor and then not touching the slice should not incur much of a cost, as it will be just a view, so removing the "target" might not make any difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm strongly in favor of the currently implemented approach here (I'd rather anything that exits the batchers to have roughly the same attributes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, consistent naming and structure is desirable, and is a good enough motivation for keeping target here.
On the topic of consistent naming: I noticed that there are some places (at least in the _stats
method) where the name target
is used, but it now actually refers to labels
. _stats
takes gtruth
, which was changed in this PR.
def __init__(self, opts): | ||
super().__init__(opts) | ||
self.denoising_objective = opts.denoising_objective |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should validate that mask-random
is not used with the MASS objective.
In BART, it is possible to occasionally substitute a random token instead of the mask token. This slightly alleviates the tendency to copy all unmasked tokens verbatim.
In MASS, substituting a random token on the source side would also affect the target sequence: the random token would be complemented into a mask token, and would not contribute to the loss. This does not make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so random_ratio > 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
closes #15
Essentially, adds a
labels
key to batches to distinguish between decoder-side inputs (tgt
andtargets'
) and decoder-side labels (currently only in use for MASS, but can be helpful for other decoder-side denoising schemes).labels
are initialized as a copy oftarget
unless otherwise specified by a transform.With this change, the MASS noising transform devolves into a slightly more complex formatting in a BART noising transform.