Skip to content

Model family API

See Add a model family for configuration and export guidance.

ModelFamilyABC

Bases: ABC

Abstract model-family contract for sampling and model construction.

Implementations must provide hyperparameter sampling and model construction. The base class also exposes optional helper hooks for validation, model loading, export-model materialization, custom object registration, FLOP counting, and export-capability reporting. By default, :attr:name returns the implementing class name.

sample_hparams(trial, ctx, config) abstractmethod

Sample normalized hyperparameters for one trial.

Parameters:

Name Type Description Default
trial Any

Trial-like sampling object.

required
ctx ModelBuildContext

Normalized build-time context.

required
config Any

Model-family configuration subtree.

required

Returns:

Type Description
dict[str, Any]

Normalized hyperparameter dictionary produced by the model-family implementation.

build_model(hparams, ctx, config) abstractmethod

Construct an uncompiled model for one hyperparameter sample.

Parameters:

Name Type Description Default
hparams dict[str, Any]

Normalized hyperparameter dictionary.

required
ctx ModelBuildContext

Normalized build-time context.

required
config Any

Model-family configuration subtree.

required

Returns:

Type Description
Model

Model instance constructed for the sampled hyperparameters and build context.

validate_config(model_config)

Validate model-family configuration.

Parameters:

Name Type Description Default
model_config Any

Model-family configuration subtree.

required

Returns:

Type Description
None

The default implementation intentionally performs no validation and accepts any config object.

validate_hparams(hparams, ctx, config)

Validate one sampled hyperparameter dictionary.

Parameters:

Name Type Description Default
hparams dict[str, Any]

Normalized hyperparameter dictionary.

required
ctx ModelBuildContext

Normalized build-time context.

required
config Any

Model-family configuration subtree.

required

Returns:

Type Description
None

The default implementation intentionally performs no hyperparameter checks.

decode_trial_hparams(raw_params, ctx, config)

Decode persisted trial parameters into build-time hyperparameters.

Parameters:

Name Type Description Default
raw_params dict[str, Any]

Raw parameter mapping persisted by the NAS runner.

required
ctx ModelBuildContext

Normalized build-time context.

required
config Any

Model-family configuration subtree.

required

Returns:

Type Description
dict[str, Any]

Model-family hyperparameters accepted by :meth:build_model.

Notes

The default implementation returns raw_params unchanged so families only need to override this hook when their persisted trial shape differs from their build-time hyperparameter shape.

materialize_export_model(hparams, ctx, config, *, model_variant, checkpoint_path=None)

Materialize the model variant used for export-oriented workflows.

Parameters:

Name Type Description Default
hparams dict[str, Any]

Normalized model-family hyperparameters.

required
ctx ModelBuildContext

Normalized build-time context.

required
config Any

Model-family configuration subtree.

required
model_variant str

Requested export variant name.

required
checkpoint_path str | Path | None

Checkpoint path required by variants that load persisted models.

None

Returns:

Type Description
Model

Model instance ready for export preparation. The default implementation builds a fresh model for "untrained" and routes variants whose normalized names start with "trained" through :meth:load_model.

Raises:

Type Description
FileNotFoundError

If a trained variant points at a checkpoint path that does not exist on disk.

ValueError

If model_variant is unsupported or if a trained variant is requested without checkpoint_path.

custom_objects()

Return custom objects required for model loading.

Returns:

Type Description
dict[str, Any]

Mapping of custom object names to runtime symbols. The default implementation returns an empty dictionary.

ModelBuildContext dataclass

Normalized build-time context supplied to a model family.

This context is the model-family-facing handoff assembled from dataset and task outputs. Unlike :class:DatasetBundle, it carries only the build-time contract needed to construct a model, not the raw split payloads.

Parameters:

Name Type Description Default
input_shape tuple[int, ...] | None

Shape of one logical input example, excluding the batch dimension.

required
input_dtype str | None

Input dtype label expected by the model family.

required
target_spec TargetSpec

Concrete target/output contract produced by the task.

required
dataset_metadata dict[str, Any]

Dataset-owned metadata relevant to model construction.

dict()
task_metadata dict[str, Any]

Task-owned metadata relevant to model construction.

dict()

Attributes:

Name Type Description
input_shape tuple[int, ...] | None

Shape of one logical input example, excluding the batch dimension.

input_dtype str | None

Input dtype label expected by the model family.

target_spec TargetSpec

Concrete target/output contract produced by the task.

dataset_metadata dict[str, Any]

Dataset-owned metadata relevant to model construction.

task_metadata dict[str, Any]

Task-owned metadata relevant to model construction.