# TRL SFTTrainer 0.15 compute\_token\_accuracy error

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrl-sfttrainer-0-15-compute-token-accuracy-error%2F142011%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 20, 2025, 12:57pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrl-sfttrainer-0-15-compute-token-accuracy-error%2F142011 "2025-02-20T12:57:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbelanec](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/rbelanec/32/32117_2.png) [@rbelanec](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Frbelanec)%5C
**Post date:** [February 20, 2025, 12:57pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrl-sfttrainer-0-15-compute-token-accuracy-error%2F142011%2F1 "2025-02-20T12:57:54Z")

</div>

I have updated my version of TRL from 0.11 to 0.15. When training LLaMa3.1-8b-Instruct, I get an error:

```auto
Traceback (most recent call last):
  File "/home/jovyan/prompt-arithmetics/llama31_instruct_pt.py", line 328, in <module>
    trainer.train()
  File "/home/jovyan/my-conda-envs/tpv/lib/python3.12/site-packages/transformers/trainer.py", line 2241, in train
    return inner_training_loop(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/my-conda-envs/tpv/lib/python3.12/site-packages/transformers/trainer.py", line 2548, in _inner_training_loop
    tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/my-conda-envs/tpv/lib/python3.12/site-packages/transformers/trainer.py", line 3698, in training_step
    loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batch)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/my-conda-envs/tpv/lib/python3.12/site-packages/trl/trainer/sft_trainer.py", line 453, in compute_loss
    accuracy = compute_token_accuracy(shift_logits, shift_labels)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/my-conda-envs/tpv/lib/python3.12/site-packages/trl/trainer/utils.py", line 1664, in compute_token_accuracy
    correct_predictions = (predictions == labels) & mask
                           ^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The size of tensor a (355) must match the size of tensor b (255) at non-singleton dimension 1

```

I have traced that the compute\_loss method from the transformers Trainer class was overridden by the SFTTraininr in 0.15 version. But I have no idea why this is happening. The problem is probably that the label size differs from the size of the model outputs. I have set max\_seq\_lenght in SFTConfig to 512.

Here is how I initialize the tokenizer and model (nothing special really):

```auto
        model = AutoModelForCausalLM.from_pretrained(
            model_args.model_name_or_path,
            torch_dtype=torch.bfloat16,
        ).to("cuda")
        model.active_adapters = [
            "default"
        ] # fix because llama has some active adapters for some reason
        model = get_peft_model(model, peft_config=peft_config)

        tokenizer = AutoTokenizer.from_pretrained(
            data_args.data_tokenizer_name_or_path,
            trust_remote_code=True,
            padding_side="right",
        )
        tokenizer.add_special_tokens({"pad_token": "<|reserved_special_token_0|>"})
        model.config.pad_token_id = tokenizer.pad_token_id
        model.generation_config.pad_token_id = tokenizer.pad_token_id

```

Does anyone have an idea what could be causing the error?

Thank you!

---

<div class="post-metadata">

**Author:** ![rbelanec](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/rbelanec/32/32117_2.png) [@rbelanec](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Frbelanec)%5C
**Post date:** [March 18, 2025, 11:46am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrl-sfttrainer-0-15-compute-token-accuracy-error%2F142011%2F2 "2025-03-18T11:46:16Z")

</div>

## **Solution with explanation**

So, I have realized that this problem persists only when using prompt tuning with SFTTrainer and CausalLM models. This is because prompt tuning prepends trainable embeddings to the input embeddings, and due to the auto-regressive process of forward function the _prepended soft-prompt of length 100 was also outputted in the model outputs_.

I am not sure if this is the problem of the PEFT library implementation of prompt tuning for CausalLM or whether this is the desired behavior and needs to be fixed on the TRL SFTTrainer side. I managed to create a quick workaround by slicing the first n\_vritual\_tokens of the outputs if prompt tuning is used in the compute\_loss method:

```auto
def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=None):
  """
  Compute training loss and additionally compute token accuracies
  """
  (loss, outputs) = super().compute_loss(
      model, inputs, return_outputs=True, num_items_in_batch=num_items_in_batch
  )

  # Compute token accuracy if we have labels and if the model is not using Liger (no logits)
  if "labels" in inputs and not self.args.use_liger:
      if isinstance(model, PeftModel) and model.peft_type == PeftType.PROMPT_TUNING:
          num_virtual_tokens = model.peft_config["default"].num_virtual_tokens
          shift_logits = outputs.logits[..., :-(1+num_virtual_tokens), :].contiguous()
      else:
          shift_logits = outputs.logits[..., :-1, :].contiguous()
      
      shift_labels = inputs["labels"][..., 1:].contiguous()

```

For some reason, the token accuracy is still really low (compared to using LoRA). I may have to investigate even further, and I will probably open a PR to fix this.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [March 18, 2025, 11:46pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrl-sfttrainer-0-15-compute-token-accuracy-error%2F142011%2F3 "2025-03-18T23:46:44Z")

</div>

Closed on March 18, 2025, 11:46pm UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
