Implement mask_instruction_loss(batch, pad_token_id, ignore_index=-100), which builds next-token inputs and targets for instruction fine-tuning so that only response tokens contribute to the loss.
batch is a list of dicts with 'tokens' (list of int ids: instruction followed by response) and 'instruction_length' (number of leading instruction tokens).
L be the longest tokens list in the batch; right-pad every sequence with pad_token_id to length L.inputs = padded[:-1] and targets = padded[1:].targets, set position i to ignore_index whenever i + 1 < instruction_length.targets, set every occurrence of pad_token_id to ignore_index except the first one (it acts as the end-of-text target).Return {'inputs': [...], 'targets': [...]}, each a list of lists (one per batch item). An empty batch returns empty lists.