<h2 dir="auto">Environment info</h2>

<ul dir="auto">
<li><code class="notranslate">transformers</code> version: master (4.4.0dev0)</li>
<li>Platform: Google colab</li>
<li>Python version: 3.7</li>
<li>PyTorch version (GPU?): None</li>
<li>Tensorflow version (GPU?): 2.4</li>
<li>Using GPU in script?: Yes</li>
<li>Using distributed or parallel set-up in script?: No</li>
</ul>
<h3 dir="auto">Who can help</h3>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jplu/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jplu">@jplu</a></p>

<h2 dir="auto">Information</h2>
<p dir="auto">Model I am using (Bert, XLNet ...): None</p>
<p dir="auto">The problem arises when using:</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> the official example scripts: (give details below)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> my own modified scripts: (give details below)</li>
</ul>
<p dir="auto">The tasks I am working on is:</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> an official GLUE/SQUaD task: (give the name)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> my own task or dataset: (give details below)</li>
</ul>
<h2 dir="auto">To reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<p dir="auto">This might be somewhat of a duplicate of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="787301182" data-permission-text="Title is private" data-url="https://github.com/huggingface/transformers/issues/9629" data-hovercard-type="issue" data-hovercard-url="/huggingface/transformers/issues/9629/hovercard" href="https://github.com/huggingface/transformers/issues/9629">#9629</a> but in a different use case</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dataset = tf.data.TextLineDataset(&quot;/content/train.txt&quot;)
tokenizer = transformers.DistilBertTokenizerFast.from_pretrained(&quot;/content/Tokenizer&quot;, do_lower_case=False)

def tokenize(sentence):
  sentence = sentence.numpy().decode('utf-8')
  a = tokenizer.encode_plus(sentence, padding=&quot;max_length&quot;, max_length=256, truncation=True, return_tensors=&quot;tf&quot;)
  return tf.constant(a.input_ids), tf.constant(a.attention_mask), tf.constant(a.input_ids)

def get_tokenized(sentence):
  a = tf.py_function(tokenize, inp=[sentence], Tout=[tf.int32, tf.int32, tf.int32])
  return {&quot;input_ids&quot;: a[0], &quot;attention_mask&quot;: a[1]}, a[2]

dataset = dataset.map(get_tokenized, num_parallel_calls=tf.data.AUTOTUNE)
# dataset = dataset.apply(tf.data.experimental.assert_cardinality(8000))
print(next(iter(dataset)))"><pre class="notranslate"><code class="notranslate">dataset = tf.data.TextLineDataset("/content/train.txt")
tokenizer = transformers.DistilBertTokenizerFast.from_pretrained("/content/Tokenizer", do_lower_case=False)

def tokenize(sentence):
  sentence = sentence.numpy().decode('utf-8')
  a = tokenizer.encode_plus(sentence, padding="max_length", max_length=256, truncation=True, return_tensors="tf")
  return tf.constant(a.input_ids), tf.constant(a.attention_mask), tf.constant(a.input_ids)

def get_tokenized(sentence):
  a = tf.py_function(tokenize, inp=[sentence], Tout=[tf.int32, tf.int32, tf.int32])
  return {"input_ids": a[0], "attention_mask": a[1]}, a[2]

dataset = dataset.map(get_tokenized, num_parallel_calls=tf.data.AUTOTUNE)
# dataset = dataset.apply(tf.data.experimental.assert_cardinality(8000))
print(next(iter(dataset)))
</code></pre></div>
<p dir="auto">Error</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="UnknownError: RuntimeError: Already borrowed
Traceback (most recent call last):

  File &quot;/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py&quot;, line 247, in __call__
    return func(device, token, args)

  File &quot;/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py&quot;, line 135, in __call__
    ret = self._func(*args)

  File &quot;/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py&quot;, line 620, in wrapper
    return func(*args, **kwargs)

  File &quot;&lt;ipython-input-34-2e27f300f71b&gt;&quot;, line 9, in tokenize
    a = tokenizer.encode_plus(sentence, padding=&quot;max_length&quot;, max_length=256, truncation=True, return_tensors=&quot;tf&quot;)

  File &quot;/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_base.py&quot;, line 2438, in encode_plus
    **kwargs,

  File &quot;/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py&quot;, line 472, in _encode_plus
    **kwargs,

  File &quot;/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py&quot;, line 379, in _batch_encode_plus
    pad_to_multiple_of=pad_to_multiple_of,

  File &quot;/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py&quot;, line 330, in set_truncation_and_padding
    self._tokenizer.enable_truncation(max_length, stride=stride, strategy=truncation_strategy.value)

RuntimeError: Already borrowed


	 [[{{node EagerPyFunc}}]]"><pre class="notranslate"><code class="notranslate">UnknownError: RuntimeError: Already borrowed
Traceback (most recent call last):

  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py", line 247, in __call__
    return func(device, token, args)

  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py", line 135, in __call__
    ret = self._func(*args)

  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py", line 620, in wrapper
    return func(*args, **kwargs)

  File "&lt;ipython-input-34-2e27f300f71b&gt;", line 9, in tokenize
    a = tokenizer.encode_plus(sentence, padding="max_length", max_length=256, truncation=True, return_tensors="tf")

  File "/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_base.py", line 2438, in encode_plus
    **kwargs,

  File "/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py", line 472, in _encode_plus
    **kwargs,

  File "/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py", line 379, in _batch_encode_plus
    pad_to_multiple_of=pad_to_multiple_of,

  File "/usr/local/lib/python3.7/dist-packages/transformers/tokenization_utils_fast.py", line 330, in set_truncation_and_padding
    self._tokenizer.enable_truncation(max_length, stride=stride, strategy=truncation_strategy.value)

RuntimeError: Already borrowed


	 [[{{node EagerPyFunc}}]]
</code></pre></div>
<p dir="auto">The important thing that I should probably mention here is that if I change my code to load the same using the tokenizers library, the code executes without any issues. I have also tried using the slow implementation and the error still persists. Any help regarding this would be great!</p>

<h2 dir="auto">Expected behavior</h2>
<p dir="auto">Tokenization should happen on the fly without errors as it does with the Tokenizer from the tokenizers library.</p>
<h3 dir="auto">Feature request</h3>
<p dir="auto">Our request a simpler and more convenient inference process for a speech recognition model based on MMS just like wav2vec 2.0 in Transformers.</p>
<h3 dir="auto">Motivation</h3>
<p dir="auto">We aim to encapsulate the various subroutines called by Facebook’s official model into a direct speech recognition model that is as easy to use as other transformer-based models like wav2vec 2.0. But we also know that the Hugging face team has been among the industry leaders in this area of work.</p>
<h3 dir="auto">Your contribution</h3>
<p dir="auto">We recognize that it may not be feasible for us to directly assist the Hugging Face technical team in this task. We believe that such an effort would be forward-looking given the popularity of MMS in current speech recognition research. The resulting model would be ideal for quickly transcribing our meeting notes.</p>