CI fails for slow tests on multiple GPUs: RuntimeError: Expected all tensors to be on the same device, but got index is on cuda:1, different from other tensors on cuda:0
Author: albertvillanovaCreated Sep 28, 2026Updated Sep 28, 2026
CI fails for slow tests on multiple GPUs: https://github.com/huggingface/trl/actions/runs/36237528914/job/108391917644
RuntimeError: Expected all tensors to be on the same device, but got index is on cuda:1, different from other tensors on cuda:0 (when checking argument in method wrapper_CUDA__index_select)
FAILED tests/test_grpo_trainer.py::TestGRPOTrainerSlow::test_train_with_transformers_continuous_batching[trl-internal-testing/small-Qwen2ForCausalLM-2.5] - RuntimeError: Caught RuntimeError in replica 1 on device 1.
FAILED tests/test_grpo_trainer.py::TestGRPOTrainerSlow::test_train_with_transformers_continuous_batching[trl-internal-testing/small-Qwen3ForCausalLM] - RuntimeError: Caught RuntimeError in replica 1 on device 1.
= 2 failed, 32 passed, 44 skipped, 2681 deselected, 16 xfailed in 250.95s (0:04:10) =Traceback:
_ TestGRPOTrainerSlow.test_train_with_transformers_continuous_batching[trl-internal-testing/small-Qwen2ForCausalLM-2.5] _
self = <tests.test_grpo_trainer.TestGRPOTrainerSlow object at 0x7f3e2dc77500>
model_name = 'trl-internal-testing/small-Qwen2ForCausalLM-2.5'
@pytest.mark.parametrize(
"model_name",
[
"trl-internal-testing/small-Qwen2ForCausalLM-2.5",
"trl-internal-testing/small-Qwen3ForCausalLM",
],
)
@pytest.mark.skipif(
not is_ampere_or_newer() and torch_device != "xpu",
reason="transformers continuous batching switches attention to Flash Attention, which requires an Ampere or "
"newer GPU, or XPU (see https://github.com/huggingface/transformers/issues/47926)",
)
def test_train_with_transformers_continuous_batching(self, model_name):
"""Test that training works with transformers continuous batching (requires GPU)."""
if not Version(transformers.__version__) >= Version("5.8.0"):
pytest.skip("transformers continuous batching requires transformers>=5.8.0.")
training_args = GRPOConfig(
output_dir=self.tmp_dir,
learning_rate=0.1, # use higher lr because gradients are tiny and default lr can stall updates
per_device_train_batch_size=3, # reduce the batch size to reduce memory usage
num_generations=3, # reduce the number of generations to reduce memory usage
max_completion_length=8, # reduce the completion length to reduce memory usage
use_transformers_continuous_batching=True,
report_to="none",
logging_strategy="no",
)
model = AutoModelForCausalLM.from_pretrained(model_name, dtype="float32")
trainer = GRPOTrainer(
model=model,
reward_funcs="trl-internal-testing/tiny-Qwen2ForSequenceClassification-2.5",
args=training_args,
train_dataset=self.train_dataset,
)
previous_trainable_params = {n: param.clone() for n, param in model.named_parameters()}
> trainer.train()
tests/test_grpo_trainer.py:4591:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv/lib/python3.12/site-packages/transformers/trainer.py:1520: in train
return inner_training_loop(
.venv/lib/python3.12/site-packages/transformers/trainer.py:1602: in _inner_training_loop
self._run_epoch(
.venv/lib/python3.12/site-packages/transformers/trainer.py:1842: in _run_epoch
tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
trl/trainer/grpo_trainer.py:1452: in training_step
output = super().training_step(model, inputs, num_items_in_batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.12/site-packages/transformers/trainer.py:2014: in training_step
loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
trl/extras/profiling.py:211: in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
trl/trainer/grpo_trainer.py:2851: in compute_loss
return self._compute_loss(model, inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
trl/trainer/grpo_trainer.py:2939: in _compute_loss
per_token_logps, entropies, aux_loss = self._get_per_token_logps_and_entropies(
trl/extras/profiling.py:211: in wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
trl/trainer/grpo_trainer.py:1438: in _get_per_token_logps_and_entropies
outputs = model(**model_inputs, labels=labels, fused_lm_head=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1783: in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1794: in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.12/site-packages/torch/nn/parallel/data_parallel.py:196: in forward
outputs = self.parallel_apply(replicas, inputs, module_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.venv/lib/python3.12/site-packages/torch/nn/parallel/data_parallel.py:213: in parallel_apply
return parallel_apply(
.venv/lib/python3.12/site-packages/torch/nn/parallel/parallel_apply.py:144: in parallel_apply
output.reraise()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <torch._utils.ExceptionWrapper object at 0x7f3da0d87ce0>
def reraise(self):
r"""Reraises the wrapped exception in the current thread"""
# Format a message such as: "Caught ValueError in DataLoader worker
# process 2. Original Traceback:", followed by the traceback.
msg = f"Caught {self.exc_type.__name__} {self.where}.\nOriginal {self.exc_msg}" # pyrefly: ignore [missing-attribute]
if self.exc_type is KeyError:
# KeyError calls repr() on its argument (usually a dict key). This
# makes stack traces unreadable. It will not be changed in Python
# (https://bugs.python.org/issue2651), so we work around it.
msg = KeyErrorMessage(msg)
elif getattr(self.exc_type, "message", None):
# Some exceptions have first argument as non-str but explicitly
# have message field
# pyrefly: ignore [not-callable]
raise self.exc_type(
# pyrefly: ignore [unexpected-keyword]
message=msg
)
try:
exception = self.exc_type(msg) # pyrefly: ignore [not-callable]
except Exception:
# If the exception takes multiple arguments or otherwise can't
# be constructed, don't try to instantiate since we don't know how to
raise RuntimeError(msg) from None
> raise exception
E RuntimeError: Caught RuntimeError in replica 1 on device 1.
E Original Traceback (most recent call last):
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/parallel/parallel_apply.py", line 114, in _worker
E output = module(*input, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
E return self._call_impl(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
E return forward_call(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/trl/trainer/utils.py", line 1545, in _fused_forward
E outputs = getattr(self, backbone_attr)(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
E return self._call_impl(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
E return forward_call(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/transformers/utils/generic.py", line 1068, in wrapper
E output = func(self, *args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/transformers/utils/output_capturing.py", line 286, in wrapper
E outputs = func(self, *args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 356, in forward
E inputs_embeds = self.embed_tokens(input_ids)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
E return self._call_impl(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1889, in _call_impl
E return inner()
E ^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1837, in inner
E result = forward_call(*args, **kwargs)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/modules/sparse.py", line 189, in forward
E return F.embedding(
E ^^^^^^^^^^^^
E File "/__w/trl/trl/.venv/lib/python3.12/site-packages/torch/nn/functional.py", line 2621, in embedding
E return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
E ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E RuntimeError: Expected all tensors to be on the same device, but got index is on cuda:1, different from other tensors on cuda:0 (when checking argument in method wrapper_CUDA__index_select)
.venv/lib/python3.12/site-packages/torch/_utils.py:803: RuntimeErrorRelated to #6836 (same root cause: nn.DataParallel).
Source: huggingface/trl