Optimize tokenizer.decode() Performance for List[int]
Inputs
#36872
Labels
Feature request
Request for a new feature
List[int]
Inputs
#36872
Feature request
When calling
tokenizer.decode()
with aList[int]
astoken_ids
, the method appears to be significantly slower than necessary due to redundantto_py_obj
conversions.Motivation
Example:
The trace results for the above code show that most of the time is spent on repeated
to_py_obj
calls, rather than in the actual_decode
function:In this case, since the input is already a
List[int]
, passing it throughto_py_obj
seems redundant. By adding a conditional check to bypass this line forList[int]
inputs:transformers/src/transformers/tokenization_utils_base.py
Line 3868 in 6a26279
…the example code improves by nearly 10x in my environment (from ~7s to ~0.7s).
Your contribution
I wasn’t sure where the best place to apply this optimization would be—either within
decode()
or insideto_py_obj()
—so I haven’t opened a PR yet. I’d be happy to contribute a fix if there’s guidance on where such a change would be most appropriate.The text was updated successfully, but these errors were encountered: