"IndexError: -1 is out of bounds for dimension 0 with size 0" when using T5 with sentiment_tuning.py
I'm trying to use the sentiment_tuning.py with T5 (specifically, `lvwerra/t5-imdb", but I'm getting the following error:
❯ python sentiment_tuning.py --model_name "lvwerra/t5-imdb" --use_seq2seq true
0it [00:00, ?it/s]You're using a T5TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.
/home/ubuntu/tmp/trl/.venv/lib/python3.11/site-packages/transformers/pipelines/text_classification.py:104: UserWarning: `return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.
warnings.warn(
0it [00:09, ?it/s]
Traceback (most recent call last):
File "/home/ubuntu/tmp/trl/examples/scripts/sentiment_tuning.py", line 203, in <module>
stats = ppo_trainer.step(query_tensors, response_tensors, rewards)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ubuntu/.pyenv/versions/3.11.1/lib/python3.11/contextlib.py", line 81, in inner
return func(*args, **kwds)
^^^^^^^^^^^^^^^^^^^
File "/home/ubuntu/tmp/trl/trl/trainer/ppo_trainer.py", line 691, in step
rewards, non_score_reward = self.compute_rewards(scores, all_logprobs, ref_logprobs, masks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ubuntu/tmp/trl/trl/trainer/ppo_trainer.py", line 1043, in compute_rewards
last_non_masked_index = mask.nonzero()[-1]
~~~~~~~~~~~~~~^^^^
IndexError: index -1 is out of bounds for dimension 0 with size 0
I'm using trl 0.5.1.dev0. I also tested this with 0.4.7's t5_sentiment.py, and I get the same error.
Similar issue here with encoder-decoder model and 0.6.0.
Is it possible that you generate an empty or just 1 token long sequence?
I'm not sure. Bear in mind that this isn't my code. I'm just running the example in the repository with the t5-imdb model.
@younesbelkada could you maybe run that example script again and see if you can reproduce?
Hmm I can't repro, running on main branch python examples/scripts/sentiment_tuning.py --model_name "lvwerra/t5-imdb" --use_seq2seq true leads to normal training, at least I can train it for few steps. Perhaps you can install TRL from source ?
pip install -U git+https://github.com/huggingface/trl.git
@younesbelkada, I need to manually install peft if I install it through git. Running the script gives me the same output as before.
What Python version are you using? That might be it. I'm using 3.11.1.
This started happening with another model of mine. I don't really understand the surrounding code, but mask.nonzero()[-1] implicitly assumes that there will always be at least one non-zero item in the mask. Why is that the case?
Here's a full reproduction of the problem:
~/tmp/trl main ubuntu@gpu-vm-1 11:49:06
❯ python3.10 -m venv venv310
~/tmp/trl main ubuntu@gpu-vm-1 11:49:12
❯ source venv310/bin/activate.fish
~/tmp/trl main ubuntu@gpu-vm-1 venv310 11:49:15
❯ python -V
Python 3.10.11
~/tmp/trl main ubuntu@gpu-vm-1 venv310 11:49:16
❯ pip install -U git+https://github.com/huggingface/trl.git peft
Collecting git+https://github.com/huggingface/trl.git
Cloning https://github.com/huggingface/trl.git to /tmp/pip-req-build-apfw4_0p
Running command git clone --filter=blob:none --quiet https://github.com/huggingface/trl.git /tmp/pip-req-build-apfw4_0p
Resolved https://github.com/huggingface/trl.git to commit fbeb146eea11552726129debbee03a6562c95c63
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Collecting peft
Using cached peft-0.5.0-py3-none-any.whl (85 kB)
Collecting torch>=1.4.0
Using cached torch-2.1.0-cp310-cp310-manylinux1_x86_64.whl (670.2 MB)
Collecting transformers>=4.18.0
Using cached transformers-4.34.0-py3-none-any.whl (7.7 MB)
Collecting tyro>=0.5.7
Using cached tyro-0.5.9-py3-none-any.whl (93 kB)
Collecting numpy>=1.18.2
Using cached numpy-1.26.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB)
Collecting datasets
Using cached datasets-2.14.5-py3-none-any.whl (519 kB)
Collecting accelerate
Using cached accelerate-0.23.0-py3-none-any.whl (258 kB)
Collecting pyyaml
Using cached PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (705 kB)
Collecting safetensors
Using cached safetensors-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)
Collecting psutil
Using cached psutil-5.9.5-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (282 kB)
Collecting tqdm
Using cached tqdm-4.66.1-py3-none-any.whl (78 kB)
Collecting packaging>=20.0
Using cached packaging-23.2-py3-none-any.whl (53 kB)
Collecting networkx
Using cached networkx-3.1-py3-none-any.whl (2.1 MB)
Collecting nvidia-nccl-cu12==2.18.1
Using cached nvidia_nccl_cu12-2.18.1-py3-none-manylinux1_x86_64.whl (209.8 MB)
Collecting jinja2
Using cached Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting nvidia-cusolver-cu12==11.4.5.107
Using cached nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)
Collecting nvidia-cublas-cu12==12.1.3.1
Using cached nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)
Collecting nvidia-curand-cu12==10.3.2.106
Using cached nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)
Collecting filelock
Using cached filelock-3.12.4-py3-none-any.whl (11 kB)
Collecting nvidia-cufft-cu12==11.0.2.54
Using cached nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)
Collecting nvidia-nvtx-cu12==12.1.105
Using cached nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)
Collecting nvidia-cuda-runtime-cu12==12.1.105
Using cached nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)
Collecting nvidia-cudnn-cu12==8.9.2.26
Using cached nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB)
Collecting nvidia-cuda-cupti-cu12==12.1.105
Using cached nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)
Collecting typing-extensions
Using cached typing_extensions-4.8.0-py3-none-any.whl (31 kB)
Collecting fsspec
Using cached fsspec-2023.9.2-py3-none-any.whl (173 kB)
Collecting triton==2.1.0
Using cached triton-2.1.0-0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (89.2 MB)
Collecting nvidia-cuda-nvrtc-cu12==12.1.105
Using cached nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)
Collecting nvidia-cusparse-cu12==12.1.0.106
Using cached nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)
Collecting sympy
Using cached sympy-1.12-py3-none-any.whl (5.7 MB)
Collecting nvidia-nvjitlink-cu12
Using cached nvidia_nvjitlink_cu12-12.2.140-py3-none-manylinux1_x86_64.whl (20.2 MB)
Collecting regex!=2019.12.17
Using cached regex-2023.10.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (773 kB)
Collecting requests
Using cached requests-2.31.0-py3-none-any.whl (62 kB)
Collecting huggingface-hub<1.0,>=0.16.4
Using cached huggingface_hub-0.17.3-py3-none-any.whl (295 kB)
Collecting tokenizers<0.15,>=0.14
Using cached tokenizers-0.14.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.8 MB)
Collecting shtab>=1.5.6
Using cached shtab-1.6.4-py3-none-any.whl (13 kB)
Collecting docstring-parser>=0.14.1
Using cached docstring_parser-0.15-py3-none-any.whl (36 kB)
Collecting rich>=11.1.0
Using cached rich-13.6.0-py3-none-any.whl (239 kB)
Collecting xxhash
Downloading xxhash-3.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.1/194.1 kB 11.0 MB/s eta 0:00:00
Collecting pyarrow>=8.0.0
Using cached pyarrow-13.0.0-cp310-cp310-manylinux_2_28_x86_64.whl (40.0 MB)
Collecting dill<0.3.8,>=0.3.0
Using cached dill-0.3.7-py3-none-any.whl (115 kB)
Collecting aiohttp
Using cached aiohttp-3.8.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB)
Collecting fsspec[http]<2023.9.0,>=2023.1.0
Using cached fsspec-2023.6.0-py3-none-any.whl (163 kB)
Collecting multiprocess
Using cached multiprocess-0.70.15-py310-none-any.whl (134 kB)
Collecting pandas
Using cached pandas-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.3 MB)
Collecting attrs>=17.3.0
Using cached attrs-23.1.0-py3-none-any.whl (61 kB)
Collecting aiosignal>=1.1.2
Using cached aiosignal-1.3.1-py3-none-any.whl (7.6 kB)
Collecting async-timeout<5.0,>=4.0.0a3
Using cached async_timeout-4.0.3-py3-none-any.whl (5.7 kB)
Collecting charset-normalizer<4.0,>=2.0
Using cached charset_normalizer-3.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (138 kB)
Collecting frozenlist>=1.1.1
Using cached frozenlist-1.4.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (225 kB)
Collecting yarl<2.0,>=1.0
Using cached yarl-1.9.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (268 kB)
Collecting multidict<7.0,>=4.5
Using cached multidict-6.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114 kB)
Collecting certifi>=2017.4.17
Using cached certifi-2023.7.22-py3-none-any.whl (158 kB)
Collecting urllib3<3,>=1.21.1
Using cached urllib3-2.0.6-py3-none-any.whl (123 kB)
Collecting idna<4,>=2.5
Using cached idna-3.4-py3-none-any.whl (61 kB)
Collecting markdown-it-py>=2.2.0
Using cached markdown_it_py-3.0.0-py3-none-any.whl (87 kB)
Collecting pygments<3.0.0,>=2.13.0
Using cached Pygments-2.16.1-py3-none-any.whl (1.2 MB)
Collecting huggingface-hub<1.0,>=0.16.4
Using cached huggingface_hub-0.16.4-py3-none-any.whl (268 kB)
Collecting MarkupSafe>=2.0
Using cached MarkupSafe-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting pytz>=2020.1
Using cached pytz-2023.3.post1-py2.py3-none-any.whl (502 kB)
Collecting python-dateutil>=2.8.2
Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting tzdata>=2022.1
Using cached tzdata-2023.3-py2.py3-none-any.whl (341 kB)
Collecting mpmath>=0.19
Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)
Collecting mdurl~=0.1
Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Collecting six>=1.5
Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Building wheels for collected packages: trl
Building wheel for trl (pyproject.toml) ... done
Created wheel for trl: filename=trl-0.7.2.dev0-py3-none-any.whl size=123227 sha256=4658fd11c70a4322bb73f6972cdbb770ad5955783c997bac6f27d1371535ed16
Stored in directory: /tmp/pip-ephem-wheel-cache-djvz2hja/wheels/22/0e/42/319b77b2648bb6140ef2b08b0478ede9ca3cc7879fcd022d36
Successfully built trl
Installing collected packages: safetensors, pytz, mpmath, xxhash, urllib3, tzdata, typing-extensions, tqdm, sympy, six, shtab, regex, pyyaml, pygments, psutil, packaging, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, numpy, networkx, multidict, mdurl, MarkupSafe, idna, fsspec, frozenlist, filelock, docstring-parser, dill, charset-normalizer, certifi, attrs, async-timeout, yarl, triton, requests, python-dateutil, pyarrow, nvidia-cusparse-cu12, nvidia-cudnn-cu12, multiprocess, markdown-it-py, jinja2, aiosignal, rich, pandas, nvidia-cusolver-cu12, huggingface-hub, aiohttp, tyro, torch, tokenizers, transformers, datasets, accelerate, trl, peft
Successfully installed MarkupSafe-2.1.3 accelerate-0.23.0 aiohttp-3.8.5 aiosignal-1.3.1 async-timeout-4.0.3 attrs-23.1.0 certifi-2023.7.22 charset-normalizer-3.3.0 datasets-2.14.5 dill-0.3.7 docstring-parser-0.15 filelock-3.12.4 frozenlist-1.4.0 fsspec-2023.6.0 huggingface-hub-0.16.4 idna-3.4 jinja2-3.1.2 markdown-it-py-3.0.0 mdurl-0.1.2 mpmath-1.3.0 multidict-6.0.4 multiprocess-0.70.15 networkx-3.1 numpy-1.26.0 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.18.1 nvidia-nvjitlink-cu12-12.2.140 nvidia-nvtx-cu12-12.1.105 packaging-23.2 pandas-2.1.1 peft-0.5.0 psutil-5.9.5 pyarrow-13.0.0 pygments-2.16.1 python-dateutil-2.8.2 pytz-2023.3.post1 pyyaml-6.0.1 regex-2023.10.3 requests-2.31.0 rich-13.6.0 safetensors-0.3.3 shtab-1.6.4 six-1.16.0 sympy-1.12 tokenizers-0.14.0 torch-2.1.0 tqdm-4.66.1 transformers-4.34.0 triton-2.1.0 trl-0.7.2.dev0 typing-extensions-4.8.0 tyro-0.5.9 tzdata-2023.3 urllib3-2.0.6 xxhash-3.4.1 yarl-1.9.2
[notice] A new release of pip is available: 23.0.1 -> 23.2.1
[notice] To update, run: pip install --upgrade pip
~/tmp/trl main 1m 38s ubuntu@gpu-vm-1 venv310 11:51:03
❯ python examples/scripts/sentiment_tuning.py --ppo-config.model_name "lvwerra/t5-imdb" --use_seq2seq
/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/tyro/_resolver.py:305: UserWarning: <class 'int'> does not match any type in Union: [<class 'float'>, <class 'NoneType'>]
warnings.warn(
/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/tyro/_resolver.py:305: UserWarning: <class 'NoneType'> does not match any type in Union: [<class 'str'>, <enum 'PeftType'>]
warnings.warn(
/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/tyro/_resolver.py:305: UserWarning: <class 'NoneType'> does not match any type in Union: [<class 'str'>, <enum 'TaskType'>]
warnings.warn(
0it [00:00, ?it/s]You're using a T5TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.
/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/transformers/pipelines/text_classification.py:105: UserWarning: `return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.
warnings.warn(
2it [00:20, 10.08s/it]/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/trl/trainer/ppo_trainer.py:1174: UserWarning: The average ratio of batch (50.64) exceeds threshold 10.00. Skipping batch.
warnings.warn(
/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/trl/trainer/ppo_trainer.py:1174: UserWarning: The average ratio of batch (172.75) exceeds threshold 10.00. Skipping batch.
warnings.warn(
/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/trl/trainer/ppo_trainer.py:1174: UserWarning: The average ratio of batch (326.51) exceeds threshold 10.00. Skipping batch.
warnings.warn(
5it [00:52, 10.67s/it]/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/trl/trainer/ppo_trainer.py:1174: UserWarning: The average ratio of batch (10.65) exceeds threshold 10.00. Skipping batch.
warnings.warn(
6it [01:12, 12.02s/it]
Traceback (most recent call last):
File "/home/ubuntu/tmp/trl/examples/scripts/sentiment_tuning.py", line 197, in <module>
ppo_trainer.log_stats(stats, batch, rewards)
File "/home/ubuntu/.pyenv/versions/3.10.11/lib/python3.10/contextlib.py", line 79, in inner
return func(*args, **kwds)
File "/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/trl/trainer/ppo_trainer.py", line 712, in step
rewards, non_score_reward = self.compute_rewards(scores, all_logprobs, ref_logprobs, masks)
File "/home/ubuntu/tmp/trl/venv310/lib/python3.10/site-packages/trl/trainer/ppo_trainer.py", line 1074, in compute_rewards
last_non_masked_index = mask.nonzero()[-1]
IndexError: index -1 is out of bounds for dimension 0 with size 0
~/tmp/trl main 1m 31s ubuntu@gpu-vm-1 venv310 11:52:45
❯ git log -1 --oneline
98845b9 (HEAD -> main) Fix DeepSpeed ZeRO-{1,2} for DPOTrainer (#825)
I should note that the error only happens in the 6th iteration, so if you only ran it for a couple of steps, you wouldn't see it.
I have similar issue here. did you resolve this?
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.
I have similar issue here.
I also have the same issue, did you resolve this ?
I encountered the same issue myself, and I've identified the root cause. In my case, the problem was the EOS token being the first token in the response.
If one of the response tensors consists solely of special tokens, mask.nonzero() returns an empty tensor, leading to the IndexError.
To prevent this, one solution is to avoid generating the EOS token as the first token. This can be achieved by setting the min_length config to a value greater than 2.
However, this approach seems to contradict what's stated here, which is rather confusing.
Seems like its still a hard problem unsolved
To make sure what happens, I made a simple experiment with multiple base SFT model, when using Bart-large(also enc-dec model), trainer.generate() output only eos_token, but when I directly using trainer.model.generate(), the output content is as usual. So I guess maybe there's something wrong with trainer's batch_generation? I'm not sure whether it works cause my PPO didn't get convergence orz...