TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
dataloader package provides a DataLoader that draws random batches from a Dataset, returning input/label pairs as *variable.Variable tensors. Two dataset implementations are included: TokenDataset for pre-training on raw token sequences and AlpacaDataset for supervised fine-tuning (SFT) on instruction–response pairs.
Dataset interface
Any type that satisfies theDataset interface can be used as a data source with DataLoader:
| Method | Description |
|---|---|
Len() int | Returns the total number of samples in the dataset |
GetItem(i int) ([]int, []int) | Returns the input token IDs and corresponding label IDs for sample i |
TokenDataset and AlpacaDataset satisfy this interface and can be swapped into a DataLoader without any other changes.
DataLoader struct
DataLoader manages iteration state and assembles individual samples into batched tensors. Unexported fields (indices, idx) track the current shuffle permutation and position within the epoch.
Number of samples to include in each batch.
The underlying dataset implementation. Accepts any type that satisfies the
Dataset interface.When
true, the sample index order is randomly permuted at the start of each epoch via Reset(). Defaults to false.Batch()
(x, y) pair of batched tensors, each of shape (BatchSize, ContextLen). Internally, Batch() calls Reset() automatically (which optionally shuffles indices) whenever the dataset is exhausted, enabling seamless multi-epoch training loops.
For each sample in the batch, GetItem is called to obtain the raw token slices, which are converted to float64 tensors and stacked along dimension 0 using tensor.Stack.
Usage:
Reset() is called immediately and iteration continues from the beginning of the (re-shuffled) dataset.
TokenDataset
TokenDataset implements Dataset for pre-training on a flat token corpus. It produces next-token prediction pairs by sliding a window of length ContextLen across the token sequence.
The full corpus as a flat slice of integer token IDs, typically produced by a BPE tokenizer.
The length of each input and label sequence. Determines the window size for sliding-window sampling.
| Method | Behaviour |
|---|---|
Len() int | Returns len(Tokens) - ContextLen — the number of valid starting positions |
GetItem(i int) ([]int, []int) | x = Tokens[i : i+ContextLen], y = Tokens[i+1 : i+ContextLen+1] |
y is shifted one position to the right relative to x, implementing standard autoregressive next-token prediction.
MustLoadTokens()
[]int token file from path and returns the decoded slice. Panics if the file cannot be opened or decoded. Use the non-panicking companion when you need explicit error handling:
encoding/gob. The typical workflow is to tokenise a corpus once, save the result with gob.Encoder, and load it at training time with MustLoadTokens.
AlpacaDataset
AlpacaDataset implements Dataset for supervised fine-tuning (SFT) on instruction–response data. Construct it with NewAlpacaDataset; the struct fields are unexported after construction.
Slice of
Alpaca structs containing instruction–response pairs.Any type that satisfies
Tokenizer (i.e. exposes Encode(inputText string) []int). This is typically the BPE tokenizer used during pre-training.Maximum sequence length. Samples shorter than
contextLen are padded; samples longer are truncated.- Prompt is formatted as
AlpacaFormat(instruction). - Response is appended with the
<|endoftext|>sentinel. - Both are encoded via
tokenizer.Encode. - Input IDs =
promptIDs + responseIDs(shifted: last token dropped). - Labels =
-100for every prompt token (ignored in cross-entropy) +responseIDs(shifted: first token dropped). - Sequences shorter than
contextLenare right-padded with0IDs and-100labels; longer sequences are truncated.
| Method | Behaviour |
|---|---|
Len() int | Returns the number of encoded samples |
GetItem(i int) ([]int, []int) | Returns (IDs, Labels) for sample i |
Alpaca struct
The instruction text shown to the model as the prompt.
The expected model response. The
<|endoftext|> sentinel is appended automatically by NewAlpacaDataset.Helper functions
MustLoadAlpaca / LoadAlpaca
MustLoadAlpaca reads a JSON file at path, unmarshals it as a []Alpaca slice, and panics on any error. Use LoadAlpaca when you need to handle errors explicitly.
AlpacaFormat
-100 labels so they do not contribute to the loss.