---
title: "Resume a deterministic data loader in C#"
url: https://recursiveai.org/onemodel/cookbook/resume-deterministic-data-loader/
description: "Resume C# training batches at a global step with a seeded sampler, compare worker and prefetch settings, and keep dataset and collator ownership explicit."
version: "1.3.0"
---

# Resume a deterministic data loader in C#

**OneModel** is a free, open-source neural-network library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel.Training) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/resume-deterministic-data-loader/resume-deterministic-data-loader.zip)

Resume the same training batches from the next global step.

```shell
dotnet add package RecursiveAI.OneModel.Training
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneModel.Training.Data;
var data = new ListDataset<ImageExample>([
    new(new float[] { 1 }, 1, 1, 1)]);
var loader =
    new DataLoader<ImageExample, ImageBatch>(
    data, EpochSampler.Shuffled(1, 1, 42),
    new ImageBatchCollator());
using var stream = loader.Start(2, 3);
if (stream.TryNext(out var batch))
    using (batch)
        Console.WriteLine(batch.Images.Data<float>()[0]);
```

Prints:

```text
1
```

Unzip [the example](https://recursiveai.org/onemodel/cookbook/resume-deterministic-data-loader/resume-deterministic-data-loader.zip), then `dotnet run`.

## The full program

The snippet at the top resumes a one-sample image dataset at global step two. The full program uses eight integer samples, two per shuffled batch, and compares a resumed two-worker stream with an uninterrupted zero-worker stream. All samples and helper types are defined below.

```csharp
using RecursiveAI.OneModel.Training.Data;

var baseline = MakeLoader(0, 1);
var expected = new List<string>();
using (var stream = baseline.Start(0, 6))
    while (stream.TryNext(out var batch))
        expected.Add(string.Join(",", batch));
using var resumed = MakeLoader(2, 3).Start(2, 6);
int step = 2;
while (resumed.TryNext(out var batch))
{
    string values = string.Join(",", batch);
    if (values != expected[step]) throw new Exception("Batch mismatch");
    Console.WriteLine($"step {step}: {values} (matches)");
    step++;
}

static DataLoader<int, int[]> MakeLoader(int workers, int prefetch) => new(
    new Numbers(), EpochSampler.Shuffled(8, 2, seed: 42), new CopyBatch(),
    new DataLoaderOptions { Seed = 42, Workers = workers, Prefetch = prefetch });

sealed class Numbers : IDataset<int>
{
    public int Count => 8;
    public int GetSample(int index) => index;
}
sealed class CopyBatch : IBatchCollator<int, int[]>
{
    public int[] Collate(ReadOnlySpan<int> samples, ReadOnlySpan<int> indices,
        in BatchContext context) => samples.ToArray();
}
```

`dotnet run` prints:

```text
step 2: 3,6 (matches)
step 3: 1,0 (matches)
step 4: 6,3 (matches)
step 5: 7,0 (matches)
```

A sample-building exception is delivered when the consumer reaches that step and stops the stream.

## Variations

### Read one step directly

With `baseline` from the full program, read a single batch without starting a stream:

```csharp
// baseline is the zero-worker loader in the full program.
Console.WriteLine(string.Join(",", baseline.Load(2)));
```

```text
3,6
```

## Related recipes

- [Train a neural network](https://recursiveai.org/onemodel/cookbook/train-a-model/)
- [Run an ONNX model](https://recursiveai.org/onemodel/cookbook/run-onnx-model/)
- [Slice and reshape tensor views](https://recursiveai.org/onemodel/cookbook/slice-and-reshape-tensor-views/)

## The short answer

Use `DataLoader.Start(firstStep, endStep)` with the next global step to consume. Keep the same dataset, sampler arguments, seeds, transform and collator. OneModel derives each batch from those inputs and its step; it does not need to replay every earlier batch to reach the restart point.

## How do you resume a deterministic data loader in C#?

Record the next unconsumed step alongside your training checkpoint. If the update using step one completed, resume the input stream at step two. The end step is exclusive. In the full program, `Start(2, 6)` yields steps two through five, including the transition into the next epoch.

With eight samples and batches of two, this sampler has four steps per epoch. Global step four is therefore epoch one, step zero. Restarting at epoch one alone would skip the unfinished part of epoch zero if your checkpoint actually belonged to global step two. A single global counter avoids that ambiguity.

The [data-loading contract](https://recursiveai.org/onemodel/docs/data-loading/) defines a batch as a pure function of seed and global step, together with the dataset and pipeline arguments. The shuffled sampler has its own seed, while the loader seed supplies transform and collator random streams. Preserve both when you use both. Changing the dataset order, batch size or transformation changes the stream even if the numeric seed is unchanged.

This resumes input data, not the whole training state. Model parameters, optimizer state and schedules still need their own checkpoint. Saving the correct data step alongside that state is what keeps the restarted model aligned with the intended batch.

## Does changing the worker count change the batches?

No, provided your dataset, transform and collator satisfy the contract. Workers may build steps in a different order, but the consumer receives batches in global-step order. Prefetch controls how far ahead construction can proceed, not the contents of a batch.

The full program checks this directly: it collects six baseline batches using zero workers and prefetch one, then recreates the last four using two workers and prefetch three. It compares every sample, not merely the number of batches. The printed lines show matching contents on both sides of the epoch boundary.

`GetSample` must be thread-safe and return the same sample for a given index. A mutable shared random generator in a custom transform violates that requirement. Use the supplied per-sample `SampleRng` stream instead. In browser WebAssembly and WASI, the documented fallback uses zero workers even if you request more; the batch contract stays the same.

## How do you use a data loader without persistent workers?

Set `Workers = 0` to construct batches on the caller’s thread. For a single step, use `Load(globalStep)`, shown in the variation. This is also useful when debugging a dataset because sample construction stays on the calling thread.

Dispose a started `BatchSequence` to stop its workers. The example’s collator copies the supplied samples into a new managed array; it does not retain the loader’s reusable spans. Those integer arrays need no disposal. By contrast, `ImageBatchCollator` produces an `ImageBatch` holding pooled tensors: dispose each consumed image batch, as the snippet at the top does. Disposing the stream also releases untaken disposable batches; it does not replace the caller’s responsibility for batches already consumed.
