Concepts / InstructionDataset Class for Instruction Fine-Tuning

InstructionDataset Class for Instruction Fine-Tuning

Instruction masking replaces the instruction and input tokens with the -100 mask, and Exercise 7.2 asks you to evaluate whether that change has a positive effect on model performance.

  • Programming

Masking as a Small Experiment

By the time you reach the masking exercise at the end of the instruction fine-tuning chapter, you have already done most of the work. You have built an InstructionDataset class, written a custom_collate_fn function to batch instruction examples, and fine-tuned a model. The exercise asks for one more thing: change which tokens count during training, then judge whether that change helps. Think of it as a controlled experiment layered on top of a pipeline you already understand.

Because it is an experiment, it helps to treat the work as a sequence of decisions. Each step produces a result you can compare against the step before it. That comparison is what lets you tell whether masking made a difference, rather than guessing.

What Instruction Masking Replaces

Definition

Instruction masking replaces the instruction and input tokens of a training example with the -100 mask. The exercise then asks you to evaluate whether that change has a positive effect on model performance.

The key idea is positional. Within one tokenized training sequence, some positions hold the instruction and the input, and other positions hold the response. Masking changes only the first group. Those instruction and input positions are overwritten with -100, while the response positions keep their real token IDs. The diagram below shows one sequence laid out in order.

Passing Datasets Into Data Loaders

The chapter uses three pieces that play different roles. The InstructionDataset object holds the instruction examples. The custom_collate_fn function describes how individual examples are combined into a batch. The PyTorch data loader connects the two and handles shuffling and batching, so the training code can simply request the next batch.

Read the diagram from left to right. The dataset supplies raw examples, the collate function defines how they are grouped, and the data loader handles the rest. The training code never touches the individual examples directly. It only asks for the next batch. This separation is why the same batching logic can later serve evaluation as well as fine-tuning.

Where Loader Creation Fits

The source presents instruction fine-tuning as three stages: preparing the dataset, fine-tuning the LLM, and evaluating the LLM. Each stage contains numbered steps. Creating the data loaders is the closing move of the first stage. Before it, you have examples; after it, you have a stream of batches that the fine-tuning stage can consume.

The source explains that data loaders are created for the training, validation, and test sets as the last step of the dataset preparation stage. Each split gets its own loader. Everything in the fine-tuning stage depends on that handoff, which is why the masking experiment depends on the data flowing through these loaders.

Designing a Fair Masking Test

A fair test changes masking as its only variable and compares the result against the earlier fine-tuned run. Without that discipline, you cannot attribute any difference in performance to masking. The table below sets the two setups side by side.

AspectEarlier fine-tuned runMasked run
Instruction and input tokensKeep their real token IDsReplaced with the -100 mask
Response tokensKeep their real token IDsKeep their real token IDs
Dataset, collate function, and loadersUnchanged baselineUnchanged, so they are not the variable
What is comparedPerformance from the earlier runPerformance after masking, checked against the baseline

Only the masking setting should differ between the two runs.

Worked example

Planning the masking experiment

You want to know whether masking the instruction and input tokens improves model performance. Describe the steps you would follow so that the result can be trusted.

  1. 1
    Start from the earlier fine-tuned run

    The earlier run is the baseline. You already have its result, so the new run is compared against it rather than judged in isolation.

  2. 2
    Change only the masking

    Apply the -100 mask to the instruction and input tokens. Keep the InstructionDataset, the custom_collate_fn function, and the data loaders exactly as they were.

  3. 3
    Fine-tune under the same conditions

    Run fine-tuning with the masked setup so that any difference in outcome can be traced to the one variable you changed.

  4. 4
    Compare the results

    Evaluate the masked model and check whether its performance is better than the baseline. Only then can you judge whether masking had a positive effect.

Result

The masking exercise is judged by comparison, not by a single number. If the masked run is not measurably better than the earlier run, the experiment does not support masking as a benefit.

Mistakes That Undermine the Masking Test

  • Common mistake

    Changing several things at once

    What it looks like

    Masking the tokens and also rebuilding the data loaders with a different batch size, then attributing the result to masking alone.

    Why it fails

    A fair test changes masking as its only variable. Any other change makes the comparison meaningless.

    Do this instead

    Keep the dataset, collate function, and loaders fixed, and change only the masking.

  • Common mistake

    Judging the result without a baseline

    What it looks like

    Looking only at the masked model's performance and declaring success.

    Why it fails

    The experiment asks whether masking has a positive effect, which requires a comparison against the earlier fine-tuned run.

    Do this instead

    Always compare the masked result against the earlier fine-tuned run.

  • Common mistake

    Confusing masking with deleting data

    What it looks like

    Assuming the instruction and input text disappears from the dataset after masking.

    Why it fails

    Masking replaces those tokens with -100 to change what counts during training. It is a change to the training signal, not a removal of the examples.

    Do this instead

    Think of masking as changing which positions count, while the examples remain in the dataset.

  • Common mistake

    Treating data loaders as an afterthought when they are actually the handoff into fine-tuning

Checking Your Understanding

EASY

Without looking back, describe in your own words the full path a training example takes from the InstructionDataset to the training code, naming the role of custom_collate_fn and the data loader. Then state which stage of the three-stage process the loader creation belongs to, and what the next stage consumes.

Hints
  • Follow the three roles: the dataset holds examples, the collate function defines how they combine into a batch, and the data loader shuffles and batches.
  • Recall that loader creation is the last step of the first stage, and the second stage depends on it.
MEDIUM

Design a two-run comparison for instruction masking. List the single variable you would change, the baseline you would compare against, and the one result you would check before concluding that masking helped.

Hints
  • The baseline is the earlier fine-tuned run.
  • Only the masking setting should differ between the two runs.

Key Takeaways

  • Instruction masking replaces the instruction and input tokens with the -100 mask, while response tokens keep their real token IDs.
  • The InstructionDataset holds the examples, custom_collate_fn defines how they form a batch, and the PyTorch data loader shuffles and batches them for training.
  • Data loaders are created for the training, validation, and test sets as the last step of the dataset preparation stage, before fine-tuning begins.
  • A fair masking test changes masking as its only variable and compares the result against the earlier fine-tuned run.

Related concepts

More concepts starting with I