Training Batch
Overview
Training batches are essential components in the process of training large language models (LLMs). They consist of collections of formatted and padded input sequences that are used during model training. The creation of these batches is crucial, especially when fine-tuning models with specific instructions. This involves using custom collate functions to ensure that the batches meet the specific requirements of instruction fine-tuning, including the generation of padding and target token IDs.
Assembling Training Batches
The process of assembling training batches is a critical step in the instruction fine-tuning of an LLM. As illustrated in Figure 7.5, this step is part of a three-stage process where the focus is on constructing the training batches effectively. This involves defining a method that ensures the model receives the correctly formatted training data during the fine-tuning process.
Figure 7.5 The three-stage process for instruction fine-tuning an LLM. Next, we look at step 2 of stage 1: assembling the training batches.
In previous implementations, training batches were typically created automatically by the PyTorch DataLoader class, which uses a default collate function. This function is responsible for combining lists of individual data samples into batches that can be processed efficiently by the model during training.
Custom Collate Function
To enhance the training process, a more sophisticated approach involves developing a custom collate function. This function is passed to the data loader and is responsible for padding the training examples in each batch to the same length. However, it allows different batches to have different lengths, which minimizes unnecessary padding. This is achieved by extending sequences only to match the longest one in each batch, rather than the entire dataset.
Figure 7.8 The padding of training examples in batches using token ID 50256 to ensure uniform length within each batch. Each batch may have different lengths, as shown by the first and second.
The custom collate function is a key component in optimizing the training process, as it ensures that the model receives input sequences that are both efficiently padded and formatted according to the specific requirements of the task at hand.