Skip to content

Learning Rate Scheduler

About

The loop creates one learning rate scheduler per optimizer through an LRSchedulerProvider. You can configure a standard schedule with AutoLRSchedulerProvider, or implement the LRSchedulerProvider protocol yourself.

Auto Scheduler

The d9d.loop.auto package builds schedulers from a Pydantic configuration. It supports piecewise schedules (warmup, hold, decay). The name field selects the scheduler.

Custom Scheduler

For a custom scheduler, implement the LRSchedulerProvider protocol. It receives the optimizer and the total number of steps, and returns the scheduler.

Usage

AutoLRSchedulerConfig is a discriminated union, so validate it with a Pydantic TypeAdapter:

from pydantic import TypeAdapter

from d9d.loop.auto import AutoLRSchedulerConfig, AutoLRSchedulerProvider

cfg = """
{
    "name": "piecewise",
    "scheduler": {
        "initial_multiplier": 0.0,
        "phases": [
            {
                "mode": "steps",
                "steps": 100,
                "target_multiplier": 1.0,
                "curve": { "type": "linear" }
            },
            {
                "mode": "rest",
                "target_multiplier": 0.1,
                "curve": { "type": "cosine" }
            }
        ]
    }
}
"""

provider = AutoLRSchedulerProvider(TypeAdapter(AutoLRSchedulerConfig).validate_json(cfg))

Inside a larger Pydantic config, declare a field of type AutoLRSchedulerConfig instead.

API Reference

d9d.loop.auto.auto_lr_scheduler

Learning rate scheduler provider built from a configuration.

AutoLRSchedulerConfig = Annotated[PiecewiseConfig, Field(discriminator='name')] module-attribute

Discriminated union of the supported learning rate scheduler configurations.

AutoLRSchedulerProvider

Bases: LRSchedulerProvider

Provider that builds a learning rate scheduler from a configuration object.

__init__(config)

Constructs the AutoLRSchedulerProvider object.

PiecewiseConfig

Bases: BaseModel

Configuration for the piecewise learning rate scheduler.

Attributes:

Name Type Description
name Literal['piecewise']

Discriminator field. Always "piecewise".

scheduler PiecewiseSchedulerConfig

Detailed configuration for the piecewise schedule.

d9d.loop.control.lr_scheduler_provider

Interface for providing a learning rate scheduler.

InitializeLRSchedulerContext dataclass

Context data required to initialize an LR scheduler.

Attributes:

Name Type Description
dist_context DistributedContext

The distributed context.

total_steps int

The total number of training steps.

optimizer Optimizer

The optimizer instance that the scheduler will control.

LRSchedulerProvider

Bases: Protocol

Protocol for defining how learning rate schedulers are created.

__call__(context) abstractmethod

Initializes the LR scheduler for one model pipeline stage.

Parameters:

Name Type Description Default
context InitializeLRSchedulerContext

Context for this operation.

required

Returns:

Type Description
LRSchedulerProtocol

The instantiated LR scheduler adhering to the protocol.