Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions nb/Qwen2_5_7B_VL_GRPO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1106,8 +1106,19 @@
"source": [
"from unsloth_zoo.utils import Version\n",
"\n",
"# Only apply chat template for TRL < 0.24.0, otherwise TRL handles it\n",
"if Version(\"trl\") < Version(\"0.24.0\"):\n",
"# Apply chat template whenever prompts are still structured messages.\n",
"# This keeps multimodal placeholder handling consistent across TRL versions.\n",
"if len(train_dataset) != 0 and isinstance(train_dataset[0][\"prompt\"], list):\n",
" train_dataset = train_dataset.map(\n",
" lambda example: {\n",
" \"prompt\": tokenizer.apply_chat_template(\n",
" example[\"prompt\"],\n",
" tokenize = False,\n",
" add_generation_prompt = True, # Must add assistant\n",
" )\n",
" }\n",
" )\n",
"elif Version(\"trl\") < Version(\"0.24.0\"):\n",
Comment on lines +1111 to +1121
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The if and elif blocks contain identical code for applying the chat template. This code duplication can be avoided by combining the conditions with an or operator, which would make the code more concise and easier to maintain.

Here's a suggested refactoring:

# Apply chat template whenever prompts are still structured messages,
# or for older TRL versions.
# This keeps multimodal placeholder handling consistent across TRL versions.
if (len(train_dataset) > 0 and isinstance(train_dataset[0].get("prompt"), list)) or \
   (Version("trl") < Version("0.24.0")):
    train_dataset = train_dataset.map(
        lambda example: {
            "prompt": tokenizer.apply_chat_template(
                example["prompt"],
                tokenize = False,
                add_generation_prompt = True, # Must add assistant
            )
        }
    )

This version also includes a safer way to access the prompt key using .get() to prevent potential KeyError exceptions, and uses len > 0 which is slightly more idiomatic.

" train_dataset = train_dataset.map(\n",
" lambda example: {\n",
" \"prompt\": tokenizer.apply_chat_template(\n",
Expand Down