Skip to content
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

Merged
merged 7 commits into from
Sep 25, 2023
Merged

MASS objective + transform #16

merged 7 commits into from
Sep 25, 2023

Conversation

TimotheeMickus
Copy link
Collaborator

@TimotheeMickus TimotheeMickus commented Sep 21, 2023

closes #15

Essentially, adds a labels key to batches to distinguish between decoder-side inputs (tgt and targets') 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 of target 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.

@TimotheeMickus TimotheeMickus marked this pull request as ready for review September 22, 2023 14:25
@TimotheeMickus
Copy link
Collaborator Author

smoketested on unpc. requires a thorough review.

Copy link
Collaborator

@Waino Waino left a 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.

@@ -22,22 +22,22 @@ def test_transform_register(self):
"sentencepiece",
"bpe",
"onmt_tokenize",
"bart",
"ae_noise",
Copy link
Collaborator

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:
Copy link
Collaborator

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.

Copy link
Collaborator Author

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],
Copy link
Collaborator

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.

Copy link
Collaborator Author

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)

Copy link
Collaborator

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
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so random_ratio > 0

Mickus Timothee added 2 commits September 25, 2023 12:11
Copy link
Collaborator

@Waino Waino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TimotheeMickus TimotheeMickus merged commit 470d466 into main Sep 25, 2023
4 checks passed
@TimotheeMickus TimotheeMickus deleted the feats/mass branch September 25, 2023 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement MASS objective for denoising autoencoder
2 participants