Inference Loop
About
The d9d.loop package also provides the execution engine for distributed inference. Like the Trainer, the Inference engine separates the definition of a job from its execution. It runs forward passes only.
Configuration and Construction
You build the Inference engine with the InferenceConfigurator. It combines the infrastructure configuration, the job configuration and the user logic into an Inference object that is ready to run.
The Configuration Lifecycle
InferenceConfigurator.configure() runs a setup sequence similar to training, but for forward-only execution:
-
Distributed Context Initialization:
- Builds the global DistributedContext.
-
Seeding:
- Sets distributed seeds, e.g. for reproducible sampling or validation splits.
-
Task Instantiation:
- Creates the
InferenceTask. It defines how inputs are built and what happens to the outputs, e.g. writing them to a JSONL file.
- Creates the
-
Event Bus Initialization:
- Creates the global
EventBus. The model provider and the task use it to register custom hooks. - Triggers the
EVENT_INFERENCE_CONFIG_STARTEDevent.
- Creates the global
-
Data Stream Construction:
- Calls the
DataProviderto build theMicrobatchPackStream. The stream yields one pack (the microbatches of one step) per iteration. - Triggers the
EVENT_INFERENCE_DATA_STREAM_READYevent.
- Calls the
-
Model Materialization:
- The
ModelStageFactorybuilds the model, as in training. - You can reuse the
ModelProviderfrom training. An inference-only provider can skipprepare_export_model_stage(). - The pipeline schedule is always
PipelineScheduleInferenceConfig.InferenceConfighas no pipelining settings. - Triggers the
EVENT_INFERENCE_MODEL_STAGES_READYevent.
- The
-
State Assembly:
- The components are packed into the
InferenceJobState. - The
Inferenceengine is created with this state and returned.
- The components are packed into the
The Inference Lifecycle
Inference.infer() runs the lifecycle below.
1. Initialization and Recovery
Before the loop starts:
- Mode Switching:
- Enters
torch.inference_mode(), which disables gradient tracking and saves memory. - Switches all model modules to
.eval()mode, which affects dropout, batch norm and similar layers.
- Enters
- State Loading:
- If the save directory holds a checkpoint of this job, the
StateCheckpointerloads it (see Checkpointing). - This restores the model state, the
JobScheduleand the data stream position, so an interrupted job resumes where it stopped.
- If the save directory holds a checkpoint of this job, the
- Context Entry:
- Enters the UI, garbage collector and profiler contexts.
- Ready Hook Trigger: Triggers the
EVENT_INFERENCE_READYevent.
2. The Step Loop
The loop runs until it reaches JobSchedule.total_steps, as in training. For every step:
- Triggers the
EVENT_INFERENCE_STEP_PREevent. -
Microbatch Execution:
- The
DevicePackStreamhands out a pack of \(N\) microbatches, already on the device, as in training. - Triggers the
EVENT_INFERENCE_FORWARD_PREevent. - The
InferenceTaskmaps each microbatch to model inputs. After the forward pass, it processes the outputs of each microbatch. - Unlike training, no backward pass runs.
- Triggers the
EVENT_INFERENCE_FORWARD_POSTevent.
- The
-
Maintenance:
- GC:
ManualGarbageCollectorruns periodically to keep peak memory in check. - Event-Based Logic: Triggers the
EVENT_INFERENCE_STEP_POSTevent. - Advance: The
JobScheduleincrements the step counter.
- GC:
-
Checkpointing:
- If configured, the engine saves the progress of the job. A restarted job then skips the data it already processed.
3. Finalization
- Task-specific: Calls
InferenceTask.finalize(). Use it e.g. to flush and close output files. - Event-specific: Triggers the
EVENT_INFERENCE_FINISHEDevent.
Usage
Build the engine with an InferenceConfigurator, then call infer():
API Reference
d9d.loop.run.InferenceConfigurator
Configurator that assembles the distributed inference environment.
It combines the device mesh parameters, the InferenceConfig and the user-defined providers
into an Inference object that is ready to run the inference loop.
__init__(mesh, parameters, task_provider, model_provider, data_provider)
Constructs the InferenceConfigurator object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
DeviceMeshParameters
|
Definition of the distributed device mesh topology. |
required |
parameters
|
InferenceConfig
|
The global configuration object for inference. |
required |
task_provider
|
InferenceTaskProvider
|
Factory for creating the inference task logic. |
required |
model_provider
|
ModelProvider
|
Factory for defining and creating model stages. |
required |
data_provider
|
DataProvider
|
Factory for building the microbatch pack stream. |
required |
configure()
Builds all inference components and returns a configured Inference object.
It creates the distributed context, sets seeds and builds the model, the data stream and the auxiliary components.
Returns:
| Type | Description |
|---|---|
Inference
|
A ready-to-use inference engine that holds the job state. |
d9d.loop.run.Inference
The main execution engine for running a distributed inference job.
This class manages the inference loop, lifecycle events, distributed synchronization,
and periodic side effects (profiling, checkpointing). The model runs in evaluation mode
inside torch.inference_mode.
__init__(state)
Constructs the Inference object from a pre-built job state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
InferenceJobState
|
The encapsulated state object containing all initialized components. |
required |
infer()
Executes the full inference workflow.
This method:
- Waits for all ranks.
- Loads the latest checkpoint if available.
- Iterates through the data stream.
- Runs the pipeline forward pass for every pack.
- Runs periodic garbage collection and profiling.
- Finalizes the task on completion.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the data stream ends before |