Piecewise Scheduler
About
The d9d.lr_scheduler.piecewise module provides a flexible, builder-based system for constructing piecewise learning rate schedules.
Instead of writing custom LRScheduler subclasses, manual functions for LambdaLR for every variation of piecewise schedule (i.e. "Warmup + Hold + Decay"), you can construct any such a schedule declaratively by chaining phases together.
Usage Examples
Python API
Here is how to create a standard "Linear Warmup + Hold + Cosine Decay" schedule:
Pydantic API
Available Curves
The following curve classes are available to interpolate values between phases:
| Curve Class | Curve Config | Description |
|---|---|---|
CurveLinear |
"linear" |
Standard straight-line interpolation. |
CurveCosine |
"cosine" |
Half-period cosine interpolation (Cosine Annealing). |
CurvePoly(power) |
"poly" |
Polynomial interpolation. power=1 is linear, power=2 is quadratic, etc. |
CurveExponential |
"exponential" |
Exponential (log-linear) interpolation. |
API Reference
d9d.lr_scheduler.piecewise
Implements flexible piecewise learning rate schedules via a builder pattern.
CurveBase
Bases: ABC
Abstract base class for interpolation curves used in scheduling.
compute(start, end, step_p)
abstractmethod
Calculates the interpolated value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float
|
The value at the beginning of the phase. |
required |
end
|
float
|
The value at the end of the phase. |
required |
step_p
|
float
|
Progress fraction through the phase (0.0 to 1.0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The interpolated value. |
CurveCosine
CurveExponential
CurveLinear
CurvePoly
PiecewiseSchedulerConfig
piecewise_schedule(initial_multiplier, total_steps=None)
Entry point for creating a piecewise learning rate schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_multiplier
|
float
|
The initial learning rate multiplier. |
required |
total_steps
|
int | None
|
Total training steps. Required for percentage-based scheduling. |
None
|
Returns:
| Type | Description |
|---|---|
PiecewiseScheduleBuilder
|
A builder instance to configure phases. |
piecewise_scheduler_from_config(config, optimizer, total_steps)
Constructs a PyTorch scheduler from the provided configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
PiecewiseSchedulerConfig
|
The scheduler configuration. |
required |
optimizer
|
Optimizer
|
The optimizer to wrap. |
required |
total_steps
|
int | None
|
The total number of training steps. Required if using percentage-based phases. |
required |
Returns:
| Type | Description |
|---|---|
LRSchedulerProtocol
|
A configured learning rate scheduler. |