noamaanMulla-03/transformers-issues huggingface dataset:
Blenderbot output logits dimensions mismatch
Author: naderabdelghany
Pull request: no

- `transformers` version: 4.9.0.dev0
- Platform: Linux-5.4.104+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.7.10
- PyTorch version (GPU?): 1.9.0+cu102 (True)
- Tensorflow version (GPU?): 2.5.0 (True)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

Models: Blenderbot — @patrickvonplaten, @patil-suraj

Documentation: @sgugger

## Information

The model I am using is `facebook/blenderbot-400M-distill`.

The problem arises when using:
* [x] my own modified scripts

## To reproduce

Steps to reproduce the behaviour:

Run the following code
```python
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
import torch

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = BlenderbotForConditionalGeneration.from_pretrained('facebook/blenderbot-400M-distill').to(DEVICE)
tokenizer = BlenderbotTokenizer.from_pretrained('facebook/blenderbot-400M-distill')

input_ids = tokenizer.encode("Hello there! My name is Nader", return_tensors="pt").to(DEVICE)
decoder_input_ids = tokenizer.encode("
", return_tensors="pt").to(DEVICE)
outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)

logits = outputs.logits

print(input_ids.shape)
print(logits.shape)
```
## Current output

```
torch.Size([1, 9])
torch.Size([1, 3, 8008])
```

## Expected behaviour

```
torch.Size([1, 9])
torch.Size([1, 9, 8008])
```

According to [the documentation of the `forward()` method,](https://huggingface.co/transformers/model_doc/blenderbot.html?highlight=forward#transformers.BlenderbotForConditionalGeneration.forward) it should return a `Seq2SeqLMOutput` object with a member `logits` having the following properties 

> logits (`torch.FloatTensor` of shape (`batch_size`, `sequence_length`, `config.vocab_size`))

and considering that the `input_ids` in this case has dimensions of `(batch_size, sequence_length)` which maps to `(1, 9)` in this case shown in code above, why doesn't the model output logits have dimensions of `(1, 9, 8008)`? Please tell me what does this `3` signify if I'm missing something here. Thanks.

<!-- A clear and concise description of what you would expect to happen. -->


Comments:
Seems like a documentation thing. After a little bit of digging, I found out that the output logits have the dimensions of `(batch_size, decoder_sequence_length, config.vocab_size)` not `(batch_size, sequence_length, config.vocab_size)`.

@naderabdalghani thanks for investigating! Also feel free to open a PR to clarify the docs if you'd like :-)
`Trainer.train(resume_from_checkpoint=False)` is causing an exception
Author: PhilipMay
Pull request: no

Since `resume_from_checkpoint` can be `str` and `bool` it should be possible to pass `False` to it.
But when `resume_from_checkpoint` is `False` it causes an exception here:

https://github.com/huggingface/transformers/blob/3d4b3bc3fd77e0e48e2364464ea90379f13bcf37/src/transformers/trainer.py#L1049-L1050

```text
E       TypeError: expected str, bytes or os.PathLike object, not bool
```

The most simple solution would be to do this at the beginning of the `train` function:

```python
resume_from_checkpoint = None if not resume_from_checkpoint else resume_from_checkpoint
```

If wanted I can provide a PR.

Comments:
That seems like the right fix indeed. Please go ahead with a PR, thanks! :-)
Wav2vec2 Dataset
Author: gouthamanwavicle
Pull request: no


I have been trying the code described in the huggingface blog https://huggingface.co/blog/fine-tune-wav2vec2-english.
While in the blog, when seeing random text from the dataset looks as shown below:
![image](https://user-images.githubusercontent.com/82436706/123511281-0ca13380-d69e-11eb-88b4-6e585f4f789e.png)
For me, trying the same code shows as below(same sentence)
![image](https://user-images.githubusercontent.com/82436706/123511305-25a9e480-d69e-11eb-892d-4692a13dbc7a.png)
Could you help to know what is this issue

Comments:
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.
Tokens Jumbling
Author: PhenomenalOnee
Pull request: no

I have trained a "bert-base-uncased" model (with lesser layers) on my dataset with my tokenizer for sentence similarity task. The final sentence embedding is formed using mean pooling strategy. Now during inference if my tokens for sentence1 is [t1,t2,t3,t4,t5] and for senetence2, I randomly shuffle these tokens example [t3,t1,t2,t5,t4], the score is really high, but sentence2 does't make any sense.
I tested the pretrained bert-base-uncased model and found out same problem, as shown below
Enter text1:a man is riding a horse
Enter text2:a riding man is a horse
Tokens1: [101, 1037, 2158, 2003, 5559, 1037, 3586, 102]
Tokens2: [101, 1037, 5559, 2158, 2003, 1037, 3586, 102]
Similarity: 0.703365683555603
Enter text1:A boy can throw a stone up to a maximum height
Enter text2:A stone up to a boy can maximum throw a height
Tokens1: [101, 1037, 2879, 2064, 5466, 1037, 2962, 2039, 2000, 1037, 4555, 4578, 102]
Tokens2: [101, 1037, 2962, 2039, 2000, 1037, 2879, 2064, 4555, 5466, 1037, 4578, 102]
Similarity: 0.9277969598770142
![aa](https://user-images.githubusercontent.com/47495143/123504257-b3250e80-d675-11eb-90b6-99bf8743e6ad.jpeg)


Comments:
Which model are you using exactly? `bert-base-uncased` is not a `sentence-transformers` model

i used "bert-base-uncased"  architecture from here: https://www.sbert.net/docs/training/overview.html to train for sentence embedding tak. I modified the number of layers and hidden size. 
Also as shown in above image, i also tested "bert-base-nli-cls-token" (from https://huggingface.co/sentence-transformers/bert-base-nli-cls-token) and also tested (https://huggingface.co/sentence-transformers/bert-base-nli-mean-tokens). But all shows same issue


Pinging @nreimers 

Hi @PhenomenalOnee 
the two models you linked are deprecated, I recommend to use  the paraphrase v2 models. 

But they will show the same behavior. That is how it is. The models don't check if a sentence makes sense or is grammatically correct. They try to infer the semantics of the sentence and they try to be robust for spelling mistakes, grammatical errors, word shuffleing etc.

If this is undesired for your task, you must create training data that teaches the network that these examples should not be close in the vector space.



This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.
How is Bert fine-tuned on STS-B task?
Author: PosoSAgapo
Pull request: no

Hi, I am new to NLP and trying to reproduce fine-tune results of Bert. However, the STST-B task troubles me, from what I understand, the STST-B task is a regression task, but Bert treats it as a classification task. I do not quite know the transformation between scores and label in detail, is anybody willing to give me a hint?

Comments:
Please ask those questions on the [forums](https://discuss.huggingface.co/). We keep the issues for bugs and feature requests only.

> Please ask those questions on the [forums](https://discuss.huggingface.co/). We keep the issues for bugs and feature requests only.

Thank you for your reply, I will post it on the forums
Update CANINE test
Author: NielsRogge
Pull request: yes

# What does this PR do?

Tiny fix in the tests of CANINE, namely replace nielsr by the Google namespace. 

@LysandreJik 

Comments:
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

Unstale
Longformer global attention mask, 2 or 1? 
Author: kakeith
Pull request: no

This example in the LongFormer documentation (https://huggingface.co/transformers/model_doc/longformer.html )
```
attention_mask[:, [1, 4, 21,]] = 2  # Set global attention based on the task. For example,
...                                     # classification: the 
 token
...                                     # QA: question tokens
...                                     # LM: potentially on the beginning of sentences and paragraphs
```
seems to mislead the user to put a 2 as the global attention mask instead of a 1 this is described in the actual documentation. I'm guessing this is a mistake because (a) the example is directly copied from the AllenAI (https://github.com/allenai/longformer#how-to-use), and (b) the source code actually adds a +1 to the attention mask in the function `_merge_to_attention_mask` (https://huggingface.co/transformers/_modules/transformers/modeling_longformer.html#LongformerModel.forward). 

Could the documentation please be fixed or clarified? Thanks! 

Also what is the 1, 4, 21 in that example? 

### Who can help
 Longformer/Reformer: @patrickvonplaten

## Information

Model I am using (Bert, XLNet ...): LongFormer 


Comments:
That is 100% correct - thanks for notifying us :-) PR is linked.

Also what is the 1, 4, 21 in that example? Can someone answer this?

Just random indices that will be attended and will attend globally

So if we are passing a sentence during inferencing `I love Hugging face` and I give value `attention_mask[:, [0,-1]] = 2`  then ` 
 and 
` will be attended globally?? And then I can fetch `
` embedding to get embedding of the sentence?
subclassing a torch.utils.data.Dataset object for a T5 model
Author: TheLongSentance
Pull request: no

# 🚀 Feature request

In the example on the HuggingFace website "[Fine-tuning with custom datasets](https://huggingface.co/transformers/master/custom_datasets.html)" it says that for a custom dataset:

"_Now, let’s turn our labels and encodings into a Dataset object. In PyTorch, this is done by subclassing a torch.utils.data.Dataset object and implementing __len__ and __getitem__. In TensorFlow, we pass our input encodings and labels to the from_tensor_slices constructor method. We put the data in this format so that the data can be easily batched such that each key in the batch encoding corresponds to a named parameter of the forward() method of the model we will train._"

So the example provided is for distilbert classification but for a text to text model like T5 I got the error message when calling the trainer of "RuntimeError: Could not infer dtype of tokenizers.Encoding" 

So could you provide more documentation or links to guides to what needs to change for T5 and maybe for other models if needed?

## Motivation

Here is the code I had to change going from the distilbert example you provide to T5. I assume it works becomes the DataCollatorForSeq2Seq() takes care of expanding the labels/output encoding into the features needed by T5? (but I know very little, I am guessing and I can't find any documentation that suggests this kind of change is needed).

![Solution](https://user-images.githubusercontent.com/35327158/127994038-61c4a5e8-3ece-4b35-b164-0f38e8c6981d.png)


## Your contribution

<!-- Is there any way that you could help, e.g. by submitting a PR?
     Make sure to read the CONTRIBUTING.MD readme:
     https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md -->


Comments:
This tutorial is out of date and will be rewritten soon. You should have a look at the [maintained examples](https://github.com/huggingface/transformers/tree/master/examples) or the [example notebooks](https://huggingface.co/transformers/notebooks.html) instead.

Thanks Sylvain, will do.
Fix from_pretrained with corrupted state_dict
Author: sgugger
Pull request: yes

# What does this PR do?

As we discovered in #12843, when a state dictionary contains keys for the body of the model that are not prefixed *and* keys for the head, the body is loaded but the head is ignored with no warning.

This PR fixes that by keeping track of the expected key that do not contain the prefix and erroring out if we load only the body of the model and there are some keys to load in that list of expected keys that do not contain the prefix. I chose the error since those kinds of state dictionaries should not exist, since `from_pretrained` or `torch.save(model.state_dict())` do not generate those.

Comments:
The test caught something weird with `sshleifer/tiny-distilbert-base-uncased-finetuned-sst-2-english` (another plus for this PR in my opinion!)

This model is used in the benchmark tests and in the zero shot pipeline but that model is beyond salvation: its weights have the names of BERT (in the keys) when it's a DistilBERT architecture, the number of labels of the config don't match the weights, the embedding size of the weights does not match the vocab size of the tokenzier or the embedding size in the config... 
Loading it for now just results in a random model (silently) since none of the weights can't be loaded.

To fix this, I created a new tiny random model following the same kind of config as `sshleifer/tiny-distilbert-base-uncased-finetuned-sst-2-english` (but not messed up) and stored it in `sgugger/tiny-distilbert-classification`.

I'll address @patrickvonplaten 's remarks regarding a more general refactor of the method to clean the code later on, merging this PR in the meantime.
Print defaults when using --help for scripts
Author: sgugger
Pull request: yes

# What does this PR do?

This PR uses the solution suggested in #12924 to automatically print the defaults of each argument when using `--help` for the script for instance, using `--help` on any of the examples would yield:
```
  --push_to_hub [PUSH_TO_HUB]
                        Whether or not to upload the trained model to the
                        model hub after training.
```
before, and after this PR it will yield
```
  --push_to_hub [PUSH_TO_HUB]
                        Whether or not to upload the trained model to the
                        model hub after training. (default: False)
```

Fixes #12924 

Comments:
This is indeed a great addition!
pylint error when using `transformers.AutoModelForSequenceClassification.from_pretrained(path)`
Author: PhilipMay
Pull request: no

I am using transformers 4.9.1 from PyPI.
When using pylint on `transformers.AutoModelForSequenceClassification.from_pretrained(path)` I am getting this error:
`my_scipt.py:277:11: E1120: No value for argument 'pretrained_model_name_or_path' in unbound method call (no-value-for-parameter)`

It I change it to
`transformers.AutoModelForSequenceClassification.from_pretrained(pretrained_model_name_or_path=path)`

I am getting

`script.py:277:11: E1120: No value for argument 'cls' in unbound method call (no-value-for-parameter)`

Could you maybe fix this?


Comments:
This may be linked to the missing `classmethod` decorators that were fixed in #12927, could you try on a source install?

> This may be linked to the missing `classmethod` decorators that were fixed in #12927, could you try on a source install?

I made a source install and then I am getting no pylint error anymore.
So this is fixed in the main branch. Closing this.

Thanks!
how to user class_weight in transformers.trainer
Author: wangdong1992
Pull request: no

In tensorflow，I can use class_weight for unbalanced data. Now, I want to train the model  through  transformers.trainer，how to use class_weight in transformers.trainer, This is an introduction to it
1.https://huggingface.co/transformers/custom_datasets.html#fine-tuning-with-trainer
2.https://huggingface.co/transformers/main_classes/trainer.html#transformers.TFTrainer
Please take a look at this question. Thank you

Comments:
For training related questions, refer to our [forum](https://discuss.huggingface.co). We like to keep Github issues for bugs/feature requests.

[This post](https://discuss.huggingface.co/t/how-can-i-use-class-weights-when-training/1067) for example will probably answer your question.

Therefore closing this issue.
pad_to_multiple_of added to DataCollatorForWholeWordMask
Author: Aktsvigun
Pull request: yes

There is a small bug in `DataCollatorForWholeWordMask`: it has an argument `pad_to_multiple_of`, however, when doing `_collate_batch` inside `__call__` method, this argument is not provided. This commit adds the usage of the argument.

Comments:
Have checked it, works fine, both `batch_input` and `batch_mask` are padded to a multiple of `pad_to_multiple_of` value.

Ok, will do that some of these days!
[Flax] Correct flax docs
Author: patrickvonplaten
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Lots of Flax models had incorrect or missing documentation. In this PR I went through the docs of all Flax models and corrected the docstrings so that they:

- run correctly
- show the correct use case (some MBart examples didn't make sense)
- link to the correct output types


@LysandreJik @sgugger - it would be great if we could re-enable the examples tests to make sure they all run correctly. Happy to help on it this week!

## Before submitting
- [x] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?




Comments:
Size of tensors not matching even though using tweets (all same length)
Author: batmanscode
Pull request: no

# What I'm doing

Trying to estimate the emotion of tweets using https://huggingface.co/cardiffnlp/twitter-roberta-base-emotion

# The error

`RuntimeError: The expanded size of the tensor (601) must match the existing size (514) at non-singleton dimension 1.  Target sizes: [1, 601].  Tensor sizes: [1, 514]`

Does anyone what the problem might be? I tried `truncating=True` as well

# Code and data to reproduce

### View only
https://deepnote.com/project/Code-to-reproduce-error-RuntimeError-The-expanded-size-of-the-tensor-601-must-match-the-existing-size-514-at-non-singleton-dimension-1-ZfBSsqbKQ7-XWrir593tKQ/%2Fnotebook.ipynb

### Interactive (can run and/or make changes)
https://deepnote.com/project/Interactive-Code-to-reproduce-error-RuntimeError-The-expanded-size-of-the-tensor-601-must-match-the-existing-size-514-at-non-singleton-dimension-1-Duplicate-qJTy9jxRTPWhXhwytQjU4Q/%2Fnotebook.ipynb

### Environment
"Deepnote projects run in containers on Debian Buster with Python 3.7"

Comments:
Can you try changing this line:
```
encoded_input = tokenizer(text, return_tensors='pt')
```
to
```
encoded_input = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=512)
```
?

It will pad/truncate all sequences to 512 tokens. Feel free to adapt to the maximum size within the batch or to a smaller max length (512 is the maximum sequence length for that model)

That worked! Thank you @LysandreJik, much appreciated 😊

Btw how did you get that the max sequence length for that model is 512? I thought it was 514 based on the error message.

I checked some tweets that were close to the character limit and their `num_tokens` (in encoded_input) was ~60. This is far less than the error message. Do you know why/how there could be such a difference?


This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.