Skip to content

Add opt-in foreach path to GPA-AdamW step - #279

Open
shaneraphel wants to merge 1 commit into
facebookresearch:mainfrom
shaneraphel:gpa-foreach-cpu
Open

shaneraphel wants to merge 1 commit into
facebookresearch:mainfrom
shaneraphel:gpa-foreach-cpu

Conversation

@shaneraphel

@shaneraphel shaneraphel commented Sep 23, 2026 •

Copy link
Copy Markdown

Summary

  • GPAAdamW.step can now run the parameter update with multi-tensor foreach ops on a single device, instead of only the per-parameter Python loop.
  • The arithmetic is the same as the loop. Sparse grads, mixed devices, or mixed dtypes fall back to the loop.
  • Default behavior does not change: foreach defaults to off so existing runs are unaffected. It is opt-in because the win is shape-dependent.
  • The flag is not stored in the param group, so existing checkpoints keep their keys.

Evidence

Apple M4, PyTorch 2.11.0, CPU, 10 threads, 8 steps, median of 3 runs. foreach result is bit-identical to the loop (torch.equal on params, z-buffer, exp_avg, exp_avg_sq).

shape loop foreach
200 x 1024 2.587 ms 1.727 ms 1.50x faster
48 x 768 x 768 41.121 ms 63.859 ms 0.64x, slower

The 768 case is why this is opt-in rather than a new default.

Test plan

  • python -m unittest gpa.tests.gpa_adamw_test gpa.tests.gpa_equivalence_test (20 tests, new GPAAdamWForeachTest checks bit-identical params and state over 4 steps)
  • ruff check and ruff format on the two touched files
  • GPU run of the new test (make test-gpu needs CUDA hardware I do not have here)

Prepared with an AI assistant. The table was measured on this machine before the pull request was opened. The author of the commit is shanyu.

Same arithmetic as the per-parameter loop, grouped with multi-tensor foreach ops on one device. Default stays on the loop so existing runs do not change; set foreach=True where it is faster (many small parameters on CPU). Sparse grads or mixed dtypes fall back. The flag stays off the param group so checkpoints keep their keys.
@meta-cla

meta-cla Bot commented Sep 23, 2026

Copy link
Copy Markdown

Hi @shaneraphel!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 23, 2026
@meta-cla

meta-cla Bot commented Sep 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant