01-supervised-fine-tuning-response-loss-mask implemented the LOSS-masking mechanics of SFT, but it took for granted that a "prompt" and "response" were already separated. This question builds the actual data structure a real instruction-tuning dataset uses, and quantifies exactly how it differs from a raw pretraining example: not just "different content", but a structurally different relationship between the tokens and the loss.
Implement build_instruction_example (turning a prompt/response pair into one training example with its own loss mask), build_pretraining_example (the raw-pretraining equivalent, with no prompt/response distinction), and fraction_of_tokens_supervised, quantifying the difference directly.
build_instruction_example(prompt_ids, response_ids, eos_token_id) concatenates prompt_ids + response_ids + [eos_token_id], with a loss_mask that's False over the prompt and True over the response AND the trailing EOS token.build_pretraining_example(token_ids) returns the tokens with a loss_mask that's True everywhere.fraction_of_tokens_supervised(example) returns the mean of example["loss_mask"].1.0; an instruction example's is always strictly less than 1.0 whenever the prompt is nonempty.Open one at a time. Each gives away a little more than the last.
The EOS token belongs in the SUPERVISED region, not the prompt: the model needs to learn to predict "stop generating now" just as much as it needs to learn the actual response content.
build_instruction_example's loss_mask is exactly [False] * len(prompt_ids) + [True] * (len(response_ids) + 1): the +1 accounts for the trailing EOS token also being supervised.
No results yet
Run your code to check it against the examples, or submit to run every test.