data_collator.py - line 326, in mask tokens - xlnet finetuning error
Author: GenTxt
Pull request: no
Labels: name: wontfix

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: version: 3.2.0
- Platform: Linux-4.15.0-118-generic-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.6.9
- PyTorch version (GPU?): 1.5.1+cu101 (True)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

### Who can help
<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @
 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.
  
 
 --> TransfoXL/XLNet: @TevenLeScao 

## Information

Model I am using (XLNet):

The problem arises when using:
* [ x] the official example scripts: (give details below)
* [ ] my own modified scripts: (give details below)

Testing simple example in 'language-modeling/examples/README'  using recommended wiki-2-raw dataset and xlnet-base cased model

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x ] my own task or dataset: (give details below)

Same error occurs using simple one sentence per line text file (10 megs)

## To reproduce

Steps to reproduce the behavior:

1. Run all steps in 'language-modeling/examples/README' using xlnet-base-cased (cached or local)
2. Model loads with warnings and process begins before quickly exiting with the following error:

File "/home/pixelhead/Desktop/xlnet/transformers-master/transformers/data/data_collator.py", line 326, in mask_tokens
    "This collator requires that sequence lengths be even to create a leakage-free perm_mask. Please see relevant comments in source code for details."
ValueError: This collator requires that sequence lengths be even to create a leakage-free perm_mask. Please see relevant comments in source code for details.
Epoch:   0%|                                              | 0/3 [00:00<?, ?it/s]
Iteration:   0%|

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->

Expect 'run_language_modeling.py' to work for xlnet as per 'language-modeling/examples/README'
Have tested addition of '--line_by_line' and 'block_size=128, 256, 512'  etc. Same error.

Could be missing something here 'Please see relevant comments in source code for details.' but not clear.

Cheers,

Comments:
@sgugger might be interested in this issue as well.

I have the same issue. 
Does anybody know any "workarounds" to bypass this issue? 

@GenTxt did you find any workaround for this error ?

No, unfortuantely. Was hoping others more familiar with the problem would
offer solutions.

On Wed, Oct 7, 2020 at 8:46 AM Mihai Dobri <notifications@github.com> wrote:

> @GenTxt <https://github.com/GenTxt> did you find any workaround for this
> error ?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/huggingface/transformers/issues/7341#issuecomment-704910726>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AFMAWPJSWOH7MWHGL52UXATSJRPJHANCNFSM4RXDWVGA>
> .
>


@LysandreJik  or @sgugger I am wondering if you could please let us know if is a workaround for this issue ? or if a code fix is planned in the near future?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Solved by using TPU instructions on GPU:

**Note:** On TPU , you should use the flag `--pad_to_max_length` in conjunction with the `--line_by_line` flag to make
sure all your batches have the same length.

Works now.

Encountered similar issue with fine-tuning Bert. 

Solved by using:

 --max_seq_length=512 with --line_by_line

How to solve the proble

ile "/anaconda3/envs/pytorch-gpu/lib/python3.6/site-packages/transformers/data/data_collator.py", line 615, in mask_tokens
    "This collator requires that sequence lengths be even to create a leakage-free perm_mask. Please see relevant comments in source code for details."
ValueError: This collator requires that sequence lengths be even to create a leakage-free perm_mask. Please see relevant comments in source code for details.
  0%|                                                                                      

I ended up adding <pad> if token length is not even. Is this ok?

I need to hook in to this issue as I can't find a solution for this simple problem:

I have a dataset that I "stream", meaning that during `__getitem__`, I read the line from file/memory, encode and return it. 

As I am pre-training an `XLNetLMHeadModel`, I need the `DataCollatorForPermutationLanguageModeling `which throws the error 

                "This collator requires that sequence lengths be even to create a leakage-free perm_mask. Please see"
                " relevant comments in source code for details."

as in in its function [torch_mask_tokens](https://huggingface.co/docs/transformers/v4.33.0/en/main_classes/data_collator#transformers.DataCollatorForLanguageModeling.torch_mask_tokens) no padding is happening. 

Why is that? Can we somehow combine the `DataCollatorWithPadding ` with the `DataCollatorForPermutationLanguageModeling` in the [trainer](https://huggingface.co/docs/transformers/main_classes/trainer) class? Or is there any other clever solution for such a "stream"-like dataset?

Addendum: I think, a quick solution is to pad to `tokenizer.model_max_length` in the [_torch_collate_batch](https://github.com/huggingface/transformers/blob/v4.33.0/src/transformers/data/data_collator.py#L426) function.

It's not that _no padding_ is happening, but only to the max sequence length of the current batch which might happend to be uneven. As the `model_max_length `is usually an even number, this solves the problem.