Dataset Formats and Factories#
A dataset flavor connects persistent files to Energon’s common runtime pipeline. The flavor is responsible for recognizing a path, loading format-specific metadata, and producing records. The base indexed factory then supplies worker partitioning, filtering, shuffling, restore keys, and mapping into user samples.
Core factory contracts#
BaseCoreDatasetFactory is the common leaf contract. Implementations provide:
build(worker_rotation_offset, part_filter), returning aSavableDataset;as_file_store(), when the format supports key-based access;__len__,__sample_type__,paths, andtags.
BaseIndexedDatasetFactory is the usual base for a finite, indexable format. Its build implementation:
merges the caller’s part filter with the factory filter;
applies a prepared filter index, if configured;
assigns virtual shards to workers;
asks the flavor to build an
IndexedSampleReader;wraps the reader in
DatasetSampler;maps each
SampleRecordthrough the configured sample loader.
Subclasses normally implement _build_reader, load_sample, and as_file_store rather than recreating
this pipeline.
Two narrower bases cover common storage layouts:
BaseSingleFileDatasetFactoryrepresents a single data file as one virtual shard.BaseManifestDatasetFactoryrecognizes a prepared.nv-metadataset and loads its dataset metadata.BaseManifestShardListDatasetFactoryadditionally loads a manifest shard list for the selected split.
Use the narrowest base that already models the format. Reimplementing worker assignment or filtering in a leaf factory creates a second compatibility surface.
Readers and records#
An IndexedSampleReader has a deliberately small interface:
len(reader)returns its ordinal sample count;reader[index]accepts an integer and returnsSampleRecord | None;close()releases owned resources.
SampleRecord carries the format payload plus Energon metadata. In particular:
__key__identifies the sample within its source;__shard__identifies its shard;__restore_key__is the structural key used for sample restoration;__sources__records provenance.
A reader may return None for a filtered or invalid ordinal only where its caller supports that behavior.
Do not use string keys on IndexedSampleReader; string lookup belongs to FileStore and part readers.
Key and part access#
FileStore retrieves (data, SourceInfo) for a string key and reports the underlying path. Part-aware
readers extend that model to enumerate or retrieve named parts. These interfaces support auxiliary media,
mounts, and crude-dataset access.
When adding file-store support:
use the same canonical sample keys as indexed iteration;
populate
SourceInfoso failures and output samples retain provenance;keep file handles worker-local and implement
closewhere the reader owns resources;define missing-key and missing-part behavior consistently with existing stores.
Path detection#
DatasetFactoryProvider separates path detection from factory construction. Providers are registered with
an integer priority:
Priority |
Meaning |
|---|---|
|
Explicit, first-choice formats. |
|
Single-file formats. |
|
Prepared manifest formats. |
Lower numbers are tried first. Providers with the same priority retain registration order. A detector should be cheap and specific: a broad detector at a high precedence can silently claim paths belonging to another format.
Importing a flavor module must register its provider before detection is used. If a new format is public, also expose its factory through the intended API module.
Factories must implement the BaseCoreDatasetFactory.get_original_shards(path) class method.
Providers expose the same method to the resolver. It returns ShardInfo entries in source order,
with names, primary file paths, and original sample counts before splits, subsets, or filters.
It must work quickly without walking the dataset. Manifest factories inherit this from BaseManifestDatasetFactory; direct-file formats supply their own implementation.
Adding a dataset flavor#
Use this checklist:
Persistent contract: define files, metadata, versioning, keys, and sample ordering.
Preparation: implement or extend the preparator that writes indexes and metadata. Define when a cached index is stale and must be rebuilt.
Reader: implement integer ordinal access, length, resource cleanup, and
SampleRecordprovenance.Factory: subclass the appropriate indexed base and implement only the format-specific hooks.
Filtering: verify
part_filter, exclusions, and preparedFilterViewtranslation.Key access: implement
FileStoreor part readers if mounts or auxiliary lookup require it.Detection: register a provider at the least surprising priority and test competing path shapes.
Configuration: ensure
config()contains stable leaf identity fields used by diagnostics and checkpoint migration.Exports and docs: expose the supported API and document preparation and recipe syntax.
Tests: cover preparation, direct reading, worker splitting, filtering, save/restore, detection, and stale or corrupt metadata.
Relevant tests include test_dataset_factory_resolver.py, the format-specific dataset tests, and
test_filter_index.py. A format that changes existing sample order also needs the iteration-order tests
listed in Compatibility and Review Matrix.
Configuration is descriptive#
A factory’s config() result is a recursively serializable description used for logging and checkpoint
identity matching. Stable fields such as _path, split_part, subset, and filter are significant.
Configuration should not contain open readers or runtime progress. Runtime progress belongs to
SavableDataset state. If a field is required to reconstruct a factory but should not distinguish two
logical dataset leaves, document that choice and add a checkpoint-migration test.