perf(optimizer): stream factored AdamW4bit state - #546
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
--adam-4bitAlgorithm
The packed 4-bit codebooks follow Memory Efficient Optimizers with 4-bit States. Matrix second moments follow the row/column estimator from Adafactor: each TP-local tensor is viewed as
[shape[0], -1], DP ranks sum partial squared-gradient row/column statistics, and the update reconstructsrow[i] * column[j] / mean(row).This is deliberately a factored AdamW approximation, not the prior rank-1 quantization design. It removes the per-element packed second moment for matrices. Vectors keep the 4-bit B128 second moment because row/column factorization is not applicable.
Adam4-only scope
AdamW4bitopts intostream_gradient_shards=Trueand BF16 gradient shards. The trainer gates the streamed path on that optimizer capability. The shared FP32-master base defaults remainFalseand FP32, so AdamW8bit and FP32 AdamW continue through the pre-existing full FP32main_gradaccumulation and step-time synchronization path.Memory impact
For a large matrix, persistent optimizer moments approach
0.5 byte/parameterfor packed momentum plus B128 scale metadata and4 * (rows + columns)factored variance bytes. The Adam4 DP gradient shard is BF16 instead of FP32 and the trainer does not retain a full-model FP32main_gradfor this mode.Peak savings still depend on DP/TP topology, activation memory, temporary collectives, parameter gathering, and state offload. This PR does not claim a fixed end-to-end GiB reduction without GPU profiling.
Compatibility
Validation performed in this checkout
ruff checkon all changed Python modules and the AdamW4bit test module: passedruff format --checkon all changed Python modules and the AdamW4bit test module: passedpython3 -m py_compileon changed Python modules and tests: passedgit diff --check: passedThe current local Python environment does not provide PyTorch, and this host does not provide a CUDA build/runtime, so the revised CPU/Gloo test suite and fused CUDA build were not executed locally. Those results are intentionally not claimed here; PR CI and a CUDA runner must validate them.
Follow-up validation required
Addresses #542