User Tasks
A Task defines custom logic for a single train or inference step.
Each Task may implement Stateful protocol, so you may store some mutable state here.
TrainTask
It is responsible for logging metrics, mapping batch inputs before they are fed into the model, and for computing the task loss function value.
Init: create_metrics(...), dump_hparams(...).
Lifecycle:
build_forward_inputs(...)(will be called once) ->compute_loss(...)(will be called multiple times if pipelining is enabled - once for each pipeline microbatch) ->update_metrics(...)(will be called once).
Exit: finalize(...).
State Management: state_dict(...), load_state_dict(...).
Events Registration: register_events(...) allows you to link specific custom methods to framework-wide Event Hooks.
InferenceTask
The InferenceTask defines the logic for a single inference step.
It is designed to handle the forward-only flow, processing the raw tensors synthesized by the model (e.g., logits, hidden states).
Lifecycle:
build_forward_inputs(...)(called once) ->process_outputs(...)(called once per pipeline microbatch).
Exit: finalize(...).
State Management: state_dict(...), load_state_dict(...).
Events Registration: register_events(...) allows hooking into the Event Bus alongside regular execution.
Task State
You may note that batch is only accessible in build_forward_inputs(...), but not in the later stages. Don't worry!
The state carries side-data from build_forward_inputs to the later stages of the same microbatch — labels, masks, token counts, or anything else the model forward does not return but the loss or metrics need.
It is declared by TState, the last type parameter of TrainTask / InferenceTask. TState is any PyTree, so you can use whatever shape fits:
- a dataclass — when you want attribute access and strict typing;
- a
TypedDict— when you prefer dict access but still want keys checked; - a plain
dict— for quick, untyped side-data; None— for tasks that carry nothing.
build_forward_inputs returns the state; compute_loss / process_outputs / update_metrics read it back, fully typed.
Tensors stored in the state are detached from the autograd graph automatically, so caching them across the pipeline never keeps the graph alive.
Example Implementation
A task's IO is typed by the same four PyTree roles the model pipeline uses (see Pipeline
Parallelism). TrainTask is generic over
[TBatch, TPipelineInput, TSharedInput, TPipelineOutput, TState]:
TPipelineInput— thePipelineInputfed to the first stage (built here).TSharedInput— theSharedInputbroadcast to every stage.TPipelineOutput— thePipelineOutputproduced by the last stage, read incompute_loss; for a single-head model this is that head's output, and for a model composed with several named heads it is each head's output keyed by head name.
build_forward_inputs returns a BuildForwardInputsResult with input / shared / state
fields — no dict keys.
d9d.loop.control.task
BaseTask
Bases: ABC, Stateful, Generic[TBatch, TPipelineInput, TSharedInput, TState]
Abstract base class representing a unit of work (Task) in the training/inference loop.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
TBatch
|
The raw microbatch type produced by the data stream. |
required | |
TPipelineInput
|
The |
required | |
TSharedInput
|
The |
required | |
TState
|
The per-microbatch side-data carried from input building to loss/output processing.
Tasks that carry nothing use |
required |
build_forward_inputs(ctx)
abstractmethod
Transforms one raw microbatch into arguments for the model.
Called once per microbatch in the step's pack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
BuildForwardInputsContext[TBatch]
|
Context object. |
required |
Returns:
| Type | Description |
|---|---|
BuildForwardInputsResult[TPipelineInput, TSharedInput, TState]
|
Result object, including the |
finalize(ctx)
Performs cleanup or final actions when the task execution finishes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
FinalizeContext
|
Context object. |
required |
load_state_dict(state_dict)
register_events(context)
Register task-specific event subscriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
RegisterTaskEventsContext
|
Context providing access to the distributed environment and the event bus. |
required |
BuildForwardInputsContext
dataclass
Bases: Generic[TBatch]
Context data to prepare inputs for the model forward pass of a single microbatch.
Attributes:
| Name | Type | Description |
|---|---|---|
batch |
TBatch
|
One raw microbatch of data produced by the data stream. |
BuildForwardInputsResult
dataclass
Bases: Generic[TPipelineInput, TSharedInput, TState]
The result of processing one raw microbatch into model inputs.
Attributes:
| Name | Type | Description |
|---|---|---|
input |
TPipelineInput
|
The |
shared |
TSharedInput
|
The |
state |
TState
|
Side-data (a PyTree, e.g. a TypedDict) carried to loss/output processing for this same microbatch — labels, masks, anything the model forward does not return but the loss needs. |
ComputeLossContext
dataclass
Bases: Generic[TPipelineOutput, TState]
Context data provided to calculate the loss during training.
Attributes:
| Name | Type | Description |
|---|---|---|
pipeline_results |
TPipelineOutput
|
The |
state |
TState
|
The side-data this microbatch's |
schedule |
JobSchedule
|
Component tracking the current step. |
ComputeLossResult
dataclass
CreateMetricsContext
dataclass
Context data provided to initialize metrics.
CreateMetricsResult
dataclass
FinalizeContext
dataclass
Context data provided when the task is being finalized.
InferenceTask
Bases: BaseTask[TBatch, TPipelineInput, TSharedInput, TState], ABC, Generic[TBatch, TPipelineInput, TSharedInput, TPipelineOutput, TState]
Abstract base class for defining inference-specific logic.
build_forward_inputs(ctx)
abstractmethod
Transforms one raw microbatch into arguments for the model.
Called once per microbatch in the step's pack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
BuildForwardInputsContext[TBatch]
|
Context object. |
required |
Returns:
| Type | Description |
|---|---|
BuildForwardInputsResult[TPipelineInput, TSharedInput, TState]
|
Result object, including the |
finalize(ctx)
Performs cleanup or final actions when the task execution finishes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
FinalizeContext
|
Context object. |
required |
load_state_dict(state_dict)
process_outputs(ctx)
abstractmethod
Processes the model outputs (e.g. saving to disk, decoding tokens).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ProcessOutputsContext[TPipelineOutput, TState]
|
Context containing the model outputs and pipeline state. |
required |
register_events(context)
Register task-specific event subscriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
RegisterTaskEventsContext
|
Context providing access to the distributed environment and the event bus. |
required |
InferenceTaskProvider
Bases: Protocol
Protocol for a callable that creates an InferenceTask instance.
__call__(ctx)
Creates and returns a new InferenceTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
InferenceTaskProviderContext
|
Context providing distributed environment information. |
required |
Returns:
| Type | Description |
|---|---|
InferenceTask
|
An instantiated InferenceTask. |
InferenceTaskProviderContext
dataclass
Context data provided to the factory creating an InferenceTask.
Attributes:
| Name | Type | Description |
|---|---|---|
dist_context |
DistributedContext
|
Information about the distributed environment. |
ProcessOutputsContext
dataclass
Bases: Generic[TPipelineOutput, TState]
Context data provided to process outputs during inference.
Attributes:
| Name | Type | Description |
|---|---|---|
pipeline_results |
TPipelineOutput
|
The |
state |
TState
|
The side-data this microbatch's |
RegisterTaskEventsContext
dataclass
Context for registering task-specific events.
Attributes:
| Name | Type | Description |
|---|---|---|
dist_context |
DistributedContext
|
The distributed execution context. |
event_bus |
EventBus
|
The event bus for subscribing to events. |
TrainTask
Bases: BaseTask[TBatch, TPipelineInput, TSharedInput, TState], ABC, Generic[TBatch, TPipelineInput, TSharedInput, TPipelineOutput, TState]
Abstract base class for defining training-specific logic.
build_forward_inputs(ctx)
abstractmethod
Transforms one raw microbatch into arguments for the model.
Called once per microbatch in the step's pack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
BuildForwardInputsContext[TBatch]
|
Context object. |
required |
Returns:
| Type | Description |
|---|---|
BuildForwardInputsResult[TPipelineInput, TSharedInput, TState]
|
Result object, including the |
compute_loss(ctx)
abstractmethod
Calculates the loss based on model outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ComputeLossContext[TPipelineOutput, TState]
|
Context object. |
required |
Returns:
| Type | Description |
|---|---|
ComputeLossResult
|
Result object. |
create_metrics(ctx)
Initializes metrics to be tracked during training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
CreateMetricsContext
|
Context object. |
required |
Returns:
| Type | Description |
|---|---|
CreateMetricsResult
|
Result object. |
dump_hparams()
Exports hyperparameters associated with this task for logging.
Returns:
| Type | Description |
|---|---|
ScalarTree
|
A dictionary of hyperparameter names and values. |
finalize(ctx)
Performs cleanup or final actions when the task execution finishes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
FinalizeContext
|
Context object. |
required |
load_state_dict(state_dict)
register_events(context)
Register task-specific event subscriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
RegisterTaskEventsContext
|
Context providing access to the distributed environment and the event bus. |
required |
state_dict()
update_metrics(ctx)
Updates the state of the metrics at the end of training step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
UpdateMetricsContext[TState]
|
Context object. |
required |
TrainTaskProvider
Bases: Protocol
Protocol that creates a TrainTask instance.
__call__(ctx)
Creates and returns a new TrainTask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
TrainTaskProviderContext
|
Context object. |
required |
Returns:
| Type | Description |
|---|---|
TrainTask
|
An instantiated TrainTask. |
TrainTaskProviderContext
dataclass
Context data provided to the factory creating a TrainTask.
Attributes:
| Name | Type | Description |
|---|---|---|
dist_context |
DistributedContext
|
Information about the distributed environment. |