Wav2Vec2: ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length

opus111: <p>I am trying to fine tune facebook/wav2vec2-large-xlsr-53 models but am running into the above error.</p>
<p>I have read the various fixes and related bugs e.g.</p>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/64047261/how-to-make-a-trainer-pad-inputs-in-a-batch-with-huggingface-transformers">
  <header class="source">

      <a href="https://stackoverflow.com/questions/64047261/how-to-make-a-trainer-pad-inputs-in-a-batch-with-huggingface-transformers" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/4657751/fran%c3%a7ois-mentec" target="_blank" rel="noopener nofollow ugc">
    <img alt="Fran&amp;#231;ois MENTEC" src="https://lh5.googleusercontent.com/-b4TGPjGIZ8A/AAAAAAAAAAI/AAAAAAAAAEM/BCx7qxLnWYs/photo.jpg?sz=256" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/64047261/how-to-make-a-trainer-pad-inputs-in-a-batch-with-huggingface-transformers" target="_blank" rel="noopener nofollow ugc">How to make a Trainer pad inputs in a batch with huggingface-transformers?</a>
</h4>

<div class="tags">
  <strong>python, pytorch, huggingface-transformers</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/4657751/fran%c3%a7ois-mentec" target="_blank" rel="noopener nofollow ugc">
    Fran&amp;#231;ois MENTEC
  </a>
  on <a href="https://stackoverflow.com/questions/64047261/how-to-make-a-trainer-pad-inputs-in-a-batch-with-huggingface-transformers" target="_blank" rel="noopener nofollow ugc">01:13PM - 24 Sep 20 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<aside class="onebox githubissue" data-onebox-src="https://github.com/huggingface/transformers/issues/15183">
  <header class="source">

      <a href="https://github.com/huggingface/transformers/issues/15183" target="_blank" rel="noopener nofollow ugc">github.com/huggingface/transformers</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/huggingface/transformers/issues/15183" target="_blank" rel="noopener nofollow ugc">ValueError When Padding Long Sequences To Small Max Length (Pytorch and Tensorflow)</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-01-17" data-time="16:11:09" data-timezone="UTC">04:11PM - 17 Jan 22 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2022-01-17" data-time="17:41:10" data-timezone="UTC">05:41PM - 17 Jan 22 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/darien-schettler" target="_blank" rel="noopener nofollow ugc">
          <img alt="darien-schettler" src="https://avatars.githubusercontent.com/u/50381286?v=4" class="onebox-avatar-inline" width="20" height="20">
          darien-schettler
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## Environment info
&lt;!-- You can run the command `transformers-cli env` and cop<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">y-and-paste its output below.
     Don't forget to fill out the missing fields in that output! --&gt;

- `transformers` version: 4.15.0
- Platform: Linux-5.4.144+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.7.12
- PyTorch version (GPU?): 1.10.0+cu111 (False)
- Tensorflow version (GPU?): 2.7.0 (False)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: NO
- Using distributed or parallel set-up in script?: NO

### Who can help
- @Rocketknight1
- @SaulLu
- @sgugger

## Information

I am following along with the HuggingFace Transformers course. At the [end of Chapter 2](https://huggingface.co/course/chapter2/6?fw=tf), there is information concerning how to use **`AutoTokenizer`** to perform certain tasks (tokenized single/multiple sequences, pad, truncate, convert to other libraries). 

The problem arises when using:
* [the official example colab](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter2/section6_tf.ipynb): 
  * This code executes flawlessly because at no point do we ever have the following conditions:
    * **`return_tensors="pt" or return_tensors="tf"`**
    * **`padding="max_length"`**
    * **`max_length=&lt;a number smaller than the number of tokens in the longest sequence&gt;`**
* [my self contained example](https://colab.research.google.com/drive/1_Zt5I-pBAIIaiJRL9-JTg4BuOWR8a8LY?usp=sharing):
  * I simply coded a simple self-contained example to show the error and when it appears
  * I am 99% sure this has to do with the handling of ragged tensors. 
    * As when you pass a **`max_length`** value smaller than the longest sequence, the default behaviour is to leave this sequence alone (no truncation). 
    * This can result in tensors of different lengths. 
    * This causes problems as these tensors cannot be batched together.

**Similar errors can also occur when performing truncation and passing other combinations of arguments.**

&lt;br&gt;

The tasks I am working on is:
* I am replicating the entire HuggingFace course for Kaggle consumption (I'm doing both Pytorch and Tensorflow versions)

## To reproduce

Steps to reproduce the behavior:

1. Open the [**self-contained example**](https://colab.research.google.com/drive/1_Zt5I-pBAIIaiJRL9-JTg4BuOWR8a8LY?usp=sharing)
2. Run the code cells

## Code Snippet

**Code That Works**
```python
tokenizer(["I've been waiting for a HuggingFace course my whole life.", "So have I!"], 
           padding='max_length', 
           max_length=8)
```

**Code That Breaks**
```python
tokenizer(["I've been waiting for a HuggingFace course my whole life.", "So have I!"], 
           return_tensors="tf",
           padding='max_length', 
           max_length=8)
```

**Two main errors:**

```python
    ValueError: Can't convert non-rectangular Python sequence to Tensor.
```

```python
    ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length.
```

**Example of how to handle this situation**
```python
tokenizer(["I've been waiting for a HuggingFace course my whole life.", "So have I!"], 
           return_tensors="tf",
           truncation=True,
           padding='max_length', 
           max_length=8)
```

## Expected behavior

I'm not 100% sure what the desired functionality is in this case. However, I believe that throwing an error is probably not the correct action. Also, the error message does not make 100% sense. I would think that delivering a warning and coercing the argument **`truncation`** to be **`True`** would be an acceptable option. If this is of interest, I can create a pull request for this feature.

**Thanks in advance!**</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I suspect this is actually a bug in the Wav2Vec2 processor.  None of the suggested fixes seem to do anything e.g padding=True in various places.</p>
<p>Has anyone encountered this with these models, and.do you have any insight or suggestions</p>
<p>Thank you<br>
Peter</p>

lnxdx: <p>I’m facing the same problem. Did you find the solution?</p>

Roshan_Kumar_Sinha: <p>I’m also facing the same problem. Did any of you find the solution?<br>
please help!!</p>