Liger-Kernel
Liger-Kernel copied to clipboard
Skip norm weight/bias gradients when frozen (LoRA/PEFT optimization)
Summary
This PR optimizes norm backward passes for LoRA/PEFT training by skipping weight/bias gradient computation when parameters are frozen (requires_grad=False).
Closes https://github.com/linkedin/Liger-Kernel/issues/1067
Motivation
When using LoRA/PEFT, normalization weights are typically frozen but Liger was still computing their gradients. This wastes computation and memory, especially at large hidden sizes common in modern LLMs. See the linked issue for detailed motivation and benchmarks.
Changes
Kernel optimizations:
- Add
compute_dW/compute_dBflags to backward kernels (astl.constexprfor dead code elimination) - Skip gradient buffer allocation when not needed
- Check
ctx.needs_input_gradin all norm backward passes
Affected ops:
- RMSNorm
- FusedAddRMSNorm
- LayerNorm
- GroupNorm
- PolyNorm
Tests:
- Add frozen weight/bias test coverage for all norm ops
Benchmarks:
- Add
freeze_weightoption to RMSNorm benchmark - Add mixed benchmark (RMSNorm + LoRA Linear)
Bug fixes:
- Fix
dS_outNone check infused_add_rms_norm_backward
Benchmark Results
RTX 3090, bf16, M=2048
| Hidden Size | Backward Speedup | Full Speedup |
|---|---|---|
| H=1024 | 1.25× | 1.12× |
| H=4096 | 1.11× | 1.05× |
| H=16384 | 1.37× | 1.22× |
| H=32768 | 3.12× | 2.41× |
API Impact
- No public API changes
- Internal
*_backwardhelpers now acceptcompute_dW/compute_dBflags
Test Plan
- [x] All existing norm tests pass
- [x] New frozen weight/bias tests pass for all norm ops
- [x] Lint/format checks pass
# Run frozen weight tests
pytest test/transformers/test_rms_norm.py test/transformers/test_layer_norm.py \
test/transformers/test_group_norm.py test/transformers/test_poly_norm.py \
test/transformers/test_fused_add_rms_norm.py -v -k "frozen"
# Run benchmarks
python benchmark/scripts/benchmark_rms_norm.py --overwrite
python benchmark/scripts/benchmark_rms_norm_mixed.py --overwrite