huggingface / huggingface/smol2operator
Getting shape mismatch error during the training of Phase 1
- Dominant language
- Python
- Stars
- 137
- Forks
- 18
- PR merge metrics
- No merged PRs in 30d
Description
Got below error while trying to train the model in Phase 1.
```python
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[11], line 2
1 dataset = get_dataset(dataset_mixture_phase_1)
----> 2 training_and_save(model, dataset, training_args, data_collator, processor)
Cell In[8], line 11, in training_and_save(model, dataset, training_args, data_collator, processor)
2 trainer = SFTTrainer(
3 model=model,
4 args=training_args,
(...) 8 processing_class=processor,
9 )
10 logger.info("*** Training ***")
---> 11 train_result = trainer.train()
12 metrics = train_result.metrics
13 metrics["train_samples"] = len(dataset["train"])
File /mnt/e/Work/smol2operator/.venv/lib/python3.12/site-packages/transformers/trainer.py:2328, in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
2326 hf_hub_utils.enable_progress_bars()
2327 else:
-> 2328 return inner_training_loop(
2329 args=args,
2330 resume_from_checkpoint=resume_from_checkpoint,
2331 trial=trial,
2332 ignore_keys_for_eval=ignore_keys_for_eval,
2333 )
File /mnt/e/Work/smol2operator/.venv/lib/python3.12/site-packages/transformers/trainer.py:2672, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
2665 context = (
2666 functools.partial(self.accelerator.no_sync, model=model)
2667 if i != len(batch_samples) - 1
2668 and self.accelerator.distributed_type != DistributedType.DEEPSPEED
2669 else contextlib.nullcontext
2670 )
2671 with context():
-> 2672 tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
2674 if (
2675 args.logging_nan_inf_filter
2676 and not is_torch_xla_available()
2677 and (torch.isnan(tr_loss_step) or torch.isinf(tr_loss_step))
2678 ):
2679 # if loss is nan or inf simply add the average of previous logged losses
2680 tr_loss = tr_loss + tr_loss / (1 + self.state.global_step - self._globalstep_last_logged)
File /mnt/e/Work/smol2operator/.venv/lib/python3.12/site-packages/trl/trainer/sft_trainer.py:904, in SFTTrainer.training_step(self, *args, **kwargs)
902 def training_step(self, *args, **kwargs):
903 with self.maybe_activation_offload_context:
--> 904 return super().training_step(*args, **kwargs)
File /mnt/e/Work/smol2operator/.venv/lib/python3.12/site-packages/transformers/trainer.py:4009, in Trainer.training_step(self, model, inputs, num_items_in_batch)
4006 return loss_mb.reduce_mean().detach().to(self.args.device)
4008 with self.compute_loss_context_manager():
-> 4009 loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batch)
4011 del inputs
4012 if (
4013 self.args.torch_empty_cache_steps is not None
4014 and self.state.global_step % self.args.torch_empty_cache_steps == 0
4015 ):
File /mnt/e/Work/smol2operator/.venv/lib/python3.12/site-packages/trl/trainer/sft_trainer.py:886, in SFTTrainer.compute_loss(self, model, inputs, return_outputs, num_items_in_batch)
883 mask = shift_labels != -100
885 # Calculate accuracy only on non-padding tokens
--> 886 correct_predictions = (predictions == shift_labels) & mask
887 total_tokens = mask.sum()
888 correct_tokens = correct_predictions.sum()
RuntimeError: The size of tensor a (2) must match the size of tensor b (4) at non-singleton dimension 0
```
I was able to mitigate it by overloading the compute_loss function. However, is this expected?
```python
from trl import SFTTrainer
class PatchedSFTTrainer(SFTTrainer):
def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=None):
outputs = model(**inputs)
loss = outputs.loss
if loss.ndim > 0:
loss = loss.mean()
if return_outputs:
return loss, outputs
return loss
def training_step(self, model, inputs, num_items_in_batch=None):
model.train()
inputs = self._prepare_inputs(inputs)
with self.compute_loss_context_manager():
loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batch)
if self.args.gradient_accumulation_steps > 1:
loss = loss / self.args.gradient_accumulation_steps
self.accelerator.backward(loss)
return loss.detach()
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.