Skip to main content
The ggml_opt_dataset_t type manages training data inside ggml. It stores all samples in two flat tensors — one for inputs and one for labels — and provides shuffling and batching operations that feed the optimizer.

Initializing a dataset

Free the dataset when you are done:

Accessing the underlying tensors

After calling ggml_opt_dataset_init, retrieve the raw tensors and populate them with your training data:
Both tensors are stored contiguously. Copy your training samples into them using memcpy or any ggml tensor-write helper:

Shard size

The ndata_shard parameter controls the granularity of dataset shuffling. Instead of shuffling individual datapoints, the optimizer shuffles shards — contiguous groups of ndata_shard datapoints that are always moved together.
  • ndata_shard = 1 — maximum randomness, each datapoint is shuffled independently. This is correct but has higher overhead when copying data to the device, because each transfer covers only a single sample.
  • ndata_shard > 1 — shards are shuffled as blocks. This reduces the number of individual memory operations at the cost of slightly less randomization.
For small models where data-loading overhead is significant (for example, when using a CUDA backend), a shard size of 8–16 can meaningfully reduce training time with negligible impact on convergence. The MNIST example uses ndata_shard = 10 for this reason.

Shuffling

Shuffles the first idata datapoints using the RNG from opt_ctx. Pass a negative value to shuffle all datapoints:
Shuffling only the training portion (and leaving the validation tail untouched) is the standard pattern used by ggml_opt_epoch.

Retrieving batches

Two functions copy a batch from the dataset into tensors that the optimizer can consume:
ggml_opt_dataset_get_batch writes into ggml tensors (suitable for passing directly to the optimizer). ggml_opt_dataset_get_batch_host writes into raw host-memory buffers, which is useful for inspection or pre-processing outside of ggml. The batch index ibatch is zero-based. The number of available batches is ndata / ndata_batch where ndata_batch is the second dimension of your data_batch tensor.

Custom training loops

For full control over the training loop — custom logging, mid-epoch checkpointing, or per-batch metric collection — use ggml_opt_epoch instead of ggml_opt_fit.
ggml_opt_epoch runs one full pass over the dataset: it trains on dataset[0 .. idata_split) and evaluates on dataset[idata_split .. ndata). Separate result objects accumulate metrics for each split.

Epoch callback signature

The callback is invoked after every batch evaluation. Use ibatch and ibatch_max to report progress, and t_start_us together with the current time to estimate throughput.

Built-in progress bar callback

Pass ggml_opt_epoch_callback_progress_bar as the callback to get a formatted progress bar printed to stderr:
You are responsible for calling ggml_opt_dataset_shuffle before each epoch when using ggml_opt_epoch directly. ggml_opt_fit handles shuffling automatically.