trl icon indicating copy to clipboard operation
trl copied to clipboard

gkd cannot run on the dataset OpenThoughts3-1.2M.

Open Jim2016713 opened this issue 11 months ago • 2 comments

Reproduction

accelerate launch --config_file ./examples/accelerate_configs/deepspeed_zero3.yaml  --num_processes=8  $BASE_PATH/examples/scripts/gkd.py \
    --model_name_or_path $STUDENT_MODEL_PATH \
    --teacher_model_name_or_path $TEACHER_MODEL_PATH \
    --dataset_name $DATASET_PATH \
    --learning_rate 2e-5 \
    --per_device_train_batch_size 4 \
    --gradient_accumulation_steps 8 \
    --output_dir gkd-model \
    --num_train_epochs 1 \
    --gradient_checkpointing \
    --seq_kd false \
    --lmbda 1 \
    --beta 1 \

outputs:

[rank0]: Traceback (most recent call last):
[rank0]:   File "/dc-hl/zibing.wei/code/distill/trl/examples/scripts/gkd.py", line 156, in <module>
[rank0]:     trainer.train()
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/trainer.py", line 2325, in train
[rank0]:     return inner_training_loop(
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/trainer.py", line 2674, in _inner_training_loop
[rank0]:     tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
[rank0]:   File "/dc-hl/zibing.wei/code/distill/trl/trl/experimental/gkd/gkd_trainer.py", line 486, in training_step
[rank0]:     new_input_ids, new_attention_mask, new_labels = self.generate_on_policy_outputs(
[rank0]:   File "/dc-hl/zibing.wei/code/distill/trl/trl/experimental/gkd/gkd_trainer.py", line 445, in generate_on_policy_outputs
[rank0]:     generated_outputs = model.generate(
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/generation/utils.py", line 2564, in generate
[rank0]:     result = decoding_method(
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/generation/utils.py", line 2787, in _sample
[rank0]:     outputs = model_forward(**model_inputs, return_dict=True)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1881, in _call_impl
[rank0]:     return inner()
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1829, in inner
[rank0]:     result = forward_call(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/utils/generic.py", line 918, in wrapper
[rank0]:     output = func(self, *args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/models/qwen3/modeling_qwen3.py", line 480, in forward
[rank0]:     outputs: BaseModelOutputWithPast = self.model(
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/utils/generic.py", line 1064, in wrapper
[rank0]:     outputs = func(self, *args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/models/qwen3/modeling_qwen3.py", line 410, in forward
[rank0]:     hidden_states = decoder_layer(
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/modeling_layers.py", line 94, in __call__
[rank0]:     return super().__call__(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/utils/deprecation.py", line 172, in wrapped_func
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/models/qwen3/modeling_qwen3.py", line 260, in forward
[rank0]:     hidden_states, _ = self.self_attn(
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
[rank0]:     return forward_call(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/utils/deprecation.py", line 172, in wrapped_func
[rank0]:     return func(*args, **kwargs)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/models/qwen3/modeling_qwen3.py", line 205, in forward
[rank0]:     query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
[rank0]:   File "/opt/miniconda/envs/trl/lib/python3.10/site-packages/transformers/models/qwen3/modeling_qwen3.py", line 115, in apply_rotary_pos_emb
[rank0]:     q_embed = (q * cos) + (rotate_half(q) * sin)
[rank0]: RuntimeError: The size of tensor a (94) must match the size of tensor b (95) at non-singleton dimension 2
  0%|          | 0/1250 [00:02<?, ?it/s]                                                                                                                                                                                                                                                                                                                                    

System Info

GPU:A100-80G trl: 0.26.0.dev0 python:3.10 base image:nvcr.io/nvidia/pytorch:24.08-py3@sha256:60cc7c9cf24b423c28f621287779ebe5e52cd39cfd284df04983df52566c1e39

Checklist

  • [x] I have checked that my issue isn't already filed (see open issues)
  • [x] I have included my system information
  • [x] Any code provided is minimal, complete, and reproducible (more on MREs)
  • [x] Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
  • [x] Any traceback provided is complete

Jim2016713 avatar Dec 19 '25 10:12 Jim2016713

Please refer to https://github.com/huggingface/trl/issues/4588#issuecomment-3685146649

tienanh28122000 avatar Dec 23 '25 05:12 tienanh28122000

Hey @Jim2016713, sorry for the slow reply, we're catching up on the backlog.

Thanks for the clean repro; this is a real bug.

DataCollatorForChatML keeps the last max_length tokens of prompt+completion. With long completions (OpenThoughts3 traces), the whole prompt gets dropped → an all-padding prompts row is fed to model.generate. On transformers < 5.0 this hits config-merge bug transformers#42762 and crashes in rotary; on transformers ≥ 5.0 it doesn't crash but silently generates from an empty prompt.

I recommend that you update TRL.

The collator emitting an empty prompt is a latent data bug (crash on tf<5, garbage on-policy samples on tf≥5). We will raise a clear error when current_prompt_len == 0.

qgallouedec avatar Jul 06 '26 17:07 qgallouedec