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 callingggml_opt_dataset_init, retrieve the raw tensors and populate them with your training data:
memcpy or any ggml tensor-write helper:
Shard size
Thendata_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.
Shuffling
idata datapoints using the RNG from opt_ctx. Pass a negative value to shuffle all datapoints:
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 — useggml_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
ibatch and ibatch_max to report progress, and t_start_us together with the current time to estimate throughput.
Built-in progress bar callback
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.