S-exp GPU
All pages
Docs · The run fileMarkdown

Loaders and prepare

train-loader says where training records come from; prepare turns one batch of them into the model's inputs and the objective's targets. Shapes are static, so every field declares its shape.

(defvar train-loader
  (loader :sources [(files ["data/train.parquet"])]
          :fields [(field :tokens :from "input_ids" :dtype :i32 :shape [1025])]
          :batch-size 4
          :infinite true))

(defun prepare (batch)
  (let ((tokens (field batch :tokens)))
    (counter :tokens (numel tokens))
    (list :inputs (slice tokens 1 0 seq)
          :targets (slice tokens 1 1 (+ seq 1)))))

The forms

formkeywordsnotes
loader:sources :stages :fields :batch-size :shuffle :infinite:sources, :shuffle and :infinite describe the single main stage; needs :fields and at least one source or stage
stage:name :sources :shuffle :infinite:name defaults to main
filesa vector of paths, :encodingone URI per string, no globbing; the encoding is inferred from the name when absent: parquet, jsonl.gz, msgpack.zstf
manifesta URI, :split :subsets :weight :encodingan S3 data manifest; reads the whole split, or with :subsets ["context:512" ...] only those named subsets of it, below
fielda name, :from :dtype :shape:from defaults to the name, :dtype to :f32, :shape to a scalar
  • An unknown keyword anywhere here is E-CONTRACT-003, and the fix lists the accepted ones.
  • :shuffle N is a buffer of N rows, a positive integer (E-CONTRACT-003 otherwise). Without :shuffle the source order is kept.
  • :batch-size belongs to the loader, not the stage: every stage has the same batch size because shapes are static. It is a positive integer (E-CONTRACT-003 otherwise).
  • Loaders return only full batches. At the end of a finite loader, an incomplete batch is dropped: 105 rows at :batch-size 10 yield 10 batches and leave 5 rows unread by the model. Batches can span source files, so the tail belongs to the loader, not to each file. To evaluate every row, use a batch size that divides the loader's row count; :batch-size 1 always does on one device. With several devices, evaluation distributes full batches among them. A device may evaluate fewer batches than another, or none: 100 rows at :batch-size 50 yield two batches even with four devices.
  • A relative path in files resolves against the experiment file's directory. Absolute paths and s3:// URIs pass through untouched, and file://<path> is the path; any other scheme is refused when the run starts. S3 credentials are in environment.
  • :subsets names shard subsets of the split, the manifest's splits.<split>.curriculum. The source reads their union in the listed order, each shard once. A name the manifest lacks fails when the run starts, listing the names that exist; anything but a non-empty vector of strings is E-CONTRACT-003. Subsets only pick shards for one source; a curriculum moves between the loader's :stages, [(stage :name "easy" ...) (stage :name "hard" ...)].
  • A loader read by an evaluation pass without :batches must be finite. See evaluation passes. When a pass reaches its loader's end and batching omitted rows, a warning names the pass and the omitted-row count. A pass stopped by :batches before that end reports no tail warning.

Host memory for a shard is bounded by its row count, at most 131,072 rows: the manifest's records for a manifest source (a shard with more rows is rejected), the file's own rows for a local Parquet or gzipped JSONL file, and the full 131,072 for any other files source, on S3 or MessagePack. A file of a few very large rows can exceed the memory limit under that bound; list it in a manifest with its true records instead. A loader holds up to two decoded shards per source, at least four, or one per file when it has fewer files, and refuses to open when they, its decode buffers, batches and shuffle buffer could exceed half the host's memory; the error names the shard count and rows, and shards of fewer rows lower it.

A Parquet row group whose loaded columns hold more than 64 MiB is read in pieces, which can be slower from S3. Each of its pages may then hold at most 64 MiB divided by three times the number of loaded columns, about 7 MiB for three columns; a larger page, or a page header that cannot be read, is refused with an error naming the limit. Writing wide rows in smaller row groups, such as pyarrow's row_group_size=4096, keeps each under 64 MiB and avoids both.

Field dtypes are :i32, :i64, :f32, :f64 and :bool; there is no :bf16 field, so load a supported dtype and cast in prepare. An :f64 field keeps double precision from JSON, MessagePack or Parquet onto the device, and needs a device with F64, CPU or CUDA, not Metal. Loading as :f32 and casting afterwards cannot recover the discarded bits.

By default, check and explain read no records, so they do not verify that a file's values match the declared shapes and dtypes; the loader does when it opens the source. Use check --data to inspect metadata and project the first record of every training and evaluation file before launching a run. This samples the data; later records can still fail.

A shaped field expects one nested list per axis: [2 3] reads a list of lists, not a flat list of six values. To read a flat list, declare [6] and reshape it in prepare. At each axis short lists are padded and long lists are truncated; check --data uses those same rules.

Several training loaders

train-loader can be a vector of loaders. Every microbatch then reads one batch of each, each in its own batch size and from its own sources, and prepare receives them as one batch:

(defvar train-loader
  [(loader :sources [(files ["data/stories.parquet"])]
           :fields [(field :tokens :from "input_ids" :dtype :i32 :shape [257])]
           :batch-size 16
           :infinite true)
   (loader :sources [(files ["data/mazes.parquet"])]
           :fields [(field :maze :from "cells" :dtype :i32 :shape [900])]
           :batch-size 2
           :shuffle 1024
           :infinite true)])

(defun prepare (batch)
  (let ((tokens (field batch :tokens)) (maze (field batch :maze)))
    ...))
  • (field batch :tokens) is [16 257] and (field batch :maze) is [2 900]. Field names are shared by the whole batch, so two loaders declaring one name is E-CONTRACT-023; give one another name and keep its column with :from, as in (field :maze-cells :from "cells" ...).
  • Each loader keeps its own position, shuffle and epochs. A finite loader that runs out ends the run, as a single one does. A checkpoint holds every loader's position and a resume continues each from its own; resuming with another number of training loaders, or in another order, is refused.
  • records counts every loader's rows: the example reads 18 records a microbatch, which explain prints as microbatches of 16 + 2. Declare a counter for each task to see them apart.
  • Under several GPUs each rank reads its own share of every loader.
  • A curriculum moves every loader that has more than one stage; those loaders declare the same stage names in the same order (E-CONTRACT-023 otherwise). A loader with one stage stays in it.
  • An evaluation pass reads one loader. Give each task its own pass with its own :prepare, since the training prepare reads fields a pass's loader does not have; see evaluation passes.

prepare

prepare receives the batch and returns a plist with :inputs and :targets (E-CONTRACT-004 otherwise).

  • (field batch :name) reads one field, shaped [batch-size] ++ field-shape with the batch size of the loader that declares it. A name no loader declares is E-CONTRACT-005, and the fix lists what the batch holds.
  • :inputs and :targets are each a tensor or a list of tensors; a list of inputs is spread over the model's parameters.
  • Any other key of the returned plist is passed to objective and evaluate as a third metadata argument, when they are written with three parameters.
  • eval-prepare, when defined, replaces prepare on every evaluation pass; otherwise prepare serves both.

prepare is traced into the training graph, so a metric in it behaves as one in objective.

Counters

(counter :name scalar) declares a progress counter. It belongs in prepare, where records are counted (E-CONTRACT-010 elsewhere). The runtime sums it over microbatches and keeps it for the whole run and the current stage:

  • ctx offers it as :name and :stage-name to schedules and the curriculum; see ctx.
  • every step emits counter/<name> and rate/<name> (per second); see metrics.
  • the status file and the summary carry the total.

A counter is not a metric: (counter :tokens (numel tokens)) gives a counter/tokens curve and tokens per second without reporting either. records is always counted.

Related: the run file, tensor operations.