Skip to content

In GRPO special tokens are removed if max_prompt_length is set, else not #2980

Closed

Description

@fanconic

I have a question about GRPO and its implementation. This mainly concerns the special tokens.

In the GRPOTrainer, in the _generate_and_score_implementation in lines 1050 and following

 def _generate_and_score_completions(
        self, inputs: list[dict[str, Union[torch.Tensor, Any]]]
    ) -> dict[str, Union[torch.Tensor, Any]]:
        device = self.accelerator.device
        mode = "train" if self.model.training else "eval"

        prompts = [x["prompt"] for x in inputs]
        prompts_text = [maybe_apply_chat_template(example, self.processing_class)["prompt"] for example in inputs]
        prompt_inputs = self.processing_class(
            text=prompts_text, return_tensors="pt", padding=True, padding_side="left", add_special_tokens=False
        )
        prompt_inputs = super()._prepare_inputs(prompt_inputs)
        prompt_ids, prompt_mask = prompt_inputs["input_ids"], prompt_inputs["attention_mask"]

        if self.max_prompt_length is not None:
            prompt_ids = prompt_ids[:, -self.max_prompt_length :]
            prompt_mask = prompt_mask[:, -self.max_prompt_length :]
            prompts_text = self.processing_class.batch_decode(
                prompt_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
            )

From my understanding, the prompts_text have special tokens after the chat template is applied. However if self.max_prompt_length is not None and entering the if-statement, then we truncate the prompts to the left and return override the prompts_text, this time WITHOUT the special tokens.

I therefore believe that we should pass skip_special_tokens=False.

Am I missing something?

Activity

  1. danielhanchen commented on Jul 17, 2025

    @danielhanchen
    Member

    @fanconic You're correct - TRL had a fix in huggingface/trl@7dbf477

    I'll add a fix asap

  2. fanconic commented on Jul 17, 2025

    @fanconic
    Author

    God, it took me an eternity and quite some sanity to figure out what was going wrong. Thank you for the quick fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions