Linear Learning-Rate Scaling With Warm-up
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `LinearScalingConfig(base_lr, base_batch_size, batch_size, warmup_steps_per_scale)`. It scales the learning rate linearly with batch size, warms up from `base_lr` to the scaled rate, and applies plain SGD updates.
The class must provide:
- `scale_factor = batch_size / base_batch_size` and `scaled_lr = base_lr * scale_factor`, as properties. Both apply when scaling down too.
- `warmup_steps`, an `int`: `ceil(warmup_steps_per_scale * k)` if `k > 1`, else `0`, where `k` is `scale_factor`.
- `lr_at(step)`: `base_lr + (scaled_lr - base_lr) * step / W` for `step < W`, otherwise `scaled_lr`, where `W` is `warmup_steps`.
- `lr_schedule(n)`: a float array of shape `(n,)` holding the rates for steps `0..n-1`.
- `apply_update(params, grads, step)`: returns `params - lr_at(step) * grads`.
Example:
```python
cfg = LinearScalingConfig(0.2, 256, 384, 3)
cfg.scale_factor # 1.5
cfg.scaled_lr # 0.3
cfg.warmup_steps # 5 (ceil(4.5))
cfg.lr_schedule(7) # [0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.3]
```
```hint When there is no warm-up
List every input combination that makes `W` zero, and check what the ramp formula does there.
```
```hint Rounding before the ceiling
`ceil` turns a tiny floating-point error into a whole extra step. Think about the order in which you compute the product so that integer inputs give an exact value.
```
### Constraints & Assumptions
- Raise `ValueError` for `base_lr <= 0`, either batch size `<= 0`, negative `warmup_steps_per_scale`, or negative `step`.
- `apply_update` must not mutate `params` or `grads`.
- `params` and `grads` are array-likes of the same shape.
### What a Strong Answer Covers
- `scale_factor` and `scaled_lr` derived from the batch sizes, including the scale-down case
- The warm-up length rule, with the ceiling computed exactly and zero warm-up when `k <= 1`
- A ramp that starts at `base_lr`, reaches `scaled_lr` exactly at step `W`, and never divides by zero
- Input validation and an update that returns a new array
### Follow-up Questions
- Why does linear scaling need warm-up at all?
- When does the linear scaling rule break down, and what alternatives exist?
- How would you compose this with a cosine decay after warm-up?
Overview: Implement a learning-rate configuration that applies the linear scaling rule for larger batches, a linear warm-up whose length grows with the scale factor, and a plain SGD update. It tests schedule arithmetic, edge cases such as zero warm-up and floating-point ceilings, and why large-batch training needs warm-up.