Skip to content

generate_full/adapter/adapter_v2: decode only the generated tokens (IndexError for non-Alpaca prompt styles) - #2416

Open
Zephyr4772 wants to merge 1 commit into
Lightning-AI:mainfrom
Zephyr4772:fix-generate-response-split
Open

Zephyr4772 wants to merge 1 commit into
Lightning-AI:mainfrom
Zephyr4772:fix-generate-response-split

Conversation

@Zephyr4772

Copy link
Copy Markdown

Problem

generate_full, generate_adapter and generate_adapter_v2 print the response with:

output = tokenizer.decode(y)
output = output.split("### Response:")[1].strip()

### Response: is only part of the Alpaca prompt style, which causes two problems.

1. IndexError for any other prompt style. The adapter tutorial runs litgpt generate_adapter <base checkpoint> .... A base checkpoint has no prompt_style.yaml, so the model's own prompt style is used, and that style often has no such marker:

litgpt finetune_adapter checkpoints/EleutherAI/pythia-14m --data JSON --data.json_path data.json --out_dir out/adapter
litgpt generate_adapter checkpoints/EleutherAI/pythia-14m --adapter_path out/adapter/final/lit_model.pth.adapter --prompt "What is 3+3?"
...
  File ".../litgpt/generate/adapter.py", line 142, in main
    output = output.split("### Response:")[1].strip()
IndexError: list index out of range

The same happens with generate_full for chat-style prompt styles.

2. Empty output when the model repeats the marker. split(...)[1] is the text between the prompt's marker and the next one. If the model generates ### Response: itself, which is common early in finetuning, only the empty string between the two markers is printed. With generate_full on a model I finetuned with Alpaca-style data, main printed an empty line, while the model had actually generated ### Response:\n110.

Fix

Decode only the generated tokens: tokenizer.decode(y[prompt_length:]).strip(). y contains the prompt followed by the new tokens, and the scripts already compute tokens_generated = y.size(0) - prompt_length. This works for every prompt style.

Tests

  • tests/generate/test_adapter.py::test_main (v1 and v2): the mocks now use 1-D token tensors as Tokenizer.encode returns, and a decoded response without the marker. The test checks that only the generated tokens are decoded.
  • New tests/generate/test_full.py::test_main does the same for generate_full, which had no test.
main this PR
test_adapter.py::test_main[v1], [v2], test_full.py::test_main 3 failed (IndexError) 3 passed

End-to-end on Windows 11, RTX 4060, pythia-14m finetuned for a few steps (litgpt 0.5.13, whose generate/*.py files are identical to main):

command main this PR
generate_adapter <base checkpoint> --adapter_path ... IndexError prints the generated text
generate_full <finetuned dir> --finetuned_path .../lit_model.pth prints an empty line prints ### Response: / 110 (what the model generated)

ruff check and ruff format --check pass on the changed files.

AI assistance (Claude Code) was used to find and write this change. I ran the commands above myself.

These scripts split the decoded text on Alpaca's "### Response:" marker.
With any other prompt style (e.g. a base checkpoint without
prompt_style.yaml) this raised IndexError, and when the model repeated the
marker the printed response was empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant