Skip to content

Run each FlashAdamW update on its parameter's CUDA device - #14773

Merged
danielhanchen merged 6 commits into
mainfrom
fix-flash-adamw-device-guard
Oct 28, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
fix-flash-adamw-device-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

optim = "adamw_flash" delegates every parameter update to FlashOptim, whose Triton
kernels launch on the current CUDA device. With a model split across GPUs through
device_map (for example the documented "balanced"), parameters on a GPU other than
the current one are then updated from the wrong device. Without peer access between
the GPUs this fails with "Pointer argument cannot be accessed from Triton".

Reach: multi GPU device_map training with optim = "adamw_flash". Single GPU training
and DDP / FSDP (one device per process) are unaffected.

Fix: UnslothFlashAdamW.step_param runs each update for a CUDA parameter inside
torch.cuda.device(p.device). FlashOptim's step() calls step_param for every parameter
and gradient release calls it directly, so both paths are covered. The context only
switches the current device and restores the caller's afterwards; CPU and other
devices go through the unchanged path.

Tests:

  • tests/utils/test_flash_adamw_optim.py: a CPU test with the stubbed optimizer records
    the current device at each update and checks that every CUDA parameter is updated
    with its own device current, the CPU parameter enters no device context, and the
    caller's device is restored. It fails on main and passes with this change.
  • tests/utils/test_flash_adamw_device.py: on machines with two GPUs and flashoptim,
    updates of parameters on cuda:0 and cuda:1 from one optimizer must match, bit for
    bit, the same updates run with each device made current explicitly (fp32 and bf16
    unquantized, bf16 and fp16 quantized, through step() and step_param()). Skips
    without two GPUs.

Not covered here: the two GPU test was not run on this host (one GPU per job); the device mismatch it reproduces was shown on two B200s (parameter on cuda:1 with cuda:0 current: "Pointer argument cannot be accessed from Triton" before, bit-identical updates after).

FlashOptim launches its Triton kernels on the current CUDA device. With a model split across
GPUs by device_map, parameters on another GPU then fail with 'Pointer argument cannot be
accessed from Triton' when peer access is unavailable. Enter torch.cuda.device(p.device)
around each CUDA parameter update; CPU and other devices are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T12:18:19.165090Z d1211cf Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 532d10ffea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
@danielhanchen danielhanchen self-assigned this Oct 27, 2026
# Conflicts:
#	tests/utils/test_flash_adamw_optim.py
compress_state_dict=False dequantizes the moments in state_dict() and
requantizes them on load, both through Triton kernels that launch on the
current device. Route step_param, _state_dict_for_param and
_load_state_for_param through one device guard.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: d1211cf2f6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

[skip ci]
@danielhanchen
danielhanchen merged commit 9d719c0 into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the fix-flash-adamw-device-guard branch October 28, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant