Instruction tuning (supervised fine-tuning, SFT) uses the same next-token loss as pretraining, but only on the tokens the assistant wrote. For a conversation with several assistant replies, the RLHF book describes two common choices:
A conversation is given as a list of turns (role, n_tokens) with roles "system", "user" or "assistant"; the token sequence is the turns laid end to end. Implement the masks.
assistant_loss_mask(turns, mode)
mode is "final" or "assistant"; any other value raises ValueError.apply_label_mask(token_ids, mask)
mask is 1 and -100 where it is 0 (the value the cross-entropy loss ignores).unroll_assistant_turns(token_ids, turns)
(input_ids, labels) of lists: input_ids is the conversation up to and including that assistant turn, and labels is -100 everywhere except on that turn's tokens, where it is the token id.