# How to use Seq2SeqTrainer (Seq2SeqDataCollator) in v4.2.1

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%3E%5C
**Category:** 🤗Transformers\
**Created:** [January 17, 2021, 9:27am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243 "2021-01-17T09:27:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusukemori](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/yusukemori/32/23302_2.png) [@yusukemori](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fyusukemori)%5C
**Post date:** [January 17, 2021, 9:27am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%2F1 "2021-01-17T09:27:04Z")

</div>

Hello,

I’d like to update my training script using `Seq2SeqTrainer` to match the newest version, v4.2.1.

My code worked with v3.5.1.  
However, when I update it, it doesn’t work with v4.2.1.

It is said that `ValueError` occurs.

```auto
  File "/ **** /seq2seq_trainer.py", line 193, in compute_loss
    loss, _ = self._compute_loss(model, inputs, labels)
  File "/ **** /seq2seq_trainer.py", line 180, in _compute_loss
    loss = self.loss_fn(logits.view(-1, logits.shape[-1]), labels.view(-1))
ValueError: Expected input batch_size (464) to match target batch_size (480).

```

I tried `print` debug,  
inserted:

```auto
    def _compute_loss(self, model, inputs, labels):
        if self.args.label_smoothing == 0:
            if self.data_args is not None and self.data_args.ignore_pad_token_for_loss:
                # force training to ignore pad token
                logits = model(**inputs, use_cache=False)[0]

                print(inputs["input_ids"].shape)
                print(logits.shape)
                print(labels.shape)

                loss = self.loss_fn(logits.view(-1, logits.shape[-1]), labels.view(-1))

```

and got:

```auto
torch.Size([8, 58])
torch.Size([8, 58, 50266])
torch.Size([8, 60])

```

(I added my own special token, so the embedding size becomes 50266)

Am I forgetting to do the necessary processing when updating the file to fit the new version?

In the `Seq2SeqDataCollator`, it seems that `shift_tokens_right`, which was imported from `transformers.models.bart.modeling_bart` is no longer needed.  
I update my own DataCollator on the basis of this new `Seq2SeqDataCollator`, and I think something I’m misunderstanding is related to here.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![yusukemori](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/yusukemori/32/23302_2.png) [@yusukemori](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fyusukemori)%5C
**Post date:** [January 17, 2021, 10:57am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%2F2 "2021-01-17T10:57:28Z")

</div>

I lowered the version from 4.2.1 to 4.1.1 and reverted to the version that has `shift_tokens_right` in `Seq2SeqDataCollator`.  
I revert my own `DataCollator` to the old version, then, apparently, the above problem no longer occurs.

Are there any tips to make my own `DataCollator` for `Seq2SeqTrainer` in v4.2.1?

Thank you.

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [January 19, 2021, 6:06am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%2F3 "2021-01-19T06:06:56Z")

</div>

Hi @yusukemori

We are in the process of re-writing the seq2seq fine-tuning scripts, and `Seq2SeqDataCollator` will probably be deprecated, so I would wait to adapt the code to the latest version, you could check out this PR, if you want to try the new script [New run\_seq2seq script by sgugger · Pull Request #9605 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/pull/9605)

---

<div class="post-metadata">

**Author:** ![yusukemori](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/yusukemori/32/23302_2.png) [@yusukemori](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fyusukemori)%5C
**Post date:** [January 19, 2021, 7:12am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%2F4 "2021-01-19T07:12:30Z")

</div>

Hi @valhalla

Thank you for the detailed answer.  
I now understand that you are in the process of re-writing the seq2seq fine-tuning scripts and adapting `Seq2SeqDataCollator` to the latest version is in a waiting state.

I think I’ll use the conventional script for now, but I’d love to check out the PR.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [January 19, 2021, 9:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%2F5 "2021-01-19T21:15:43Z")

</div>

The PR has been merged, so you should be able to use a similar workflow. Note that the processing that used to be done in `Seq2SeqDataCollator` is now done on the dataset directly.

---

<div class="post-metadata">

**Author:** ![yusukemori](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/yusukemori/32/23302_2.png) [@yusukemori](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fyusukemori)%5C
**Post date:** [January 20, 2021, 3:47am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-use-seq2seqtrainer-seq2seqdatacollator-in-v4-2-1%2F3243%2F6 "2021-01-20T03:47:06Z")

</div>

Hi @sgugger

Thank you for giving me the information!  
I’ll check the PR and closely look at the changes about where processing is done.
