Keep xFormers attention masks on the GPU running each layer by oobabooga · Pull Request #8516 · unslothai/unsloth · GitHub
Skip to content

Keep xFormers attention masks on the GPU running each layer - #8516

Merged
oobabooga merged 8 commits into
unslothai:mainfrom
oobabooga:fix/xformers-mask-device-cache
Aug 19, 2026
Merged

Keep xFormers attention masks on the GPU running each layer#8516
oobabooga merged 8 commits into
unslothai:mainfrom
oobabooga:fix/xformers-mask-device-cache

Conversation

@oobabooga

@oobabooga oobabooga commented Aug 12, 2026

Copy link
Copy Markdown
Member

Multi-GPU CPT and other padding-free or packed training runs could fail when attention moved to a model shard on another GPU:

Attention bias and Query/Key/Value should be on the same device
query.device: cuda:1
attn_bias: cuda:0

xFormers creates BlockDiagonalCausalMask metadata on CUDA's current device when no device is supplied. Under model parallelism, that can remain cuda:0 while later layers run elsewhere. The process-wide cache also omitted the target device from its key, so packed and inherited block masks could reach the kernel on the wrong device.

Fix

  • Scope reusable xFormers masks by device and move generated or inherited masks to the query device before dispatch.
  • Skip reconstruction when the mask is already on the correct device.
  • Preserve compatibility with older xFormers versions and clear the global mask cache with the other packed-attention caches.
  • Add isolated regressions plus a real two-GPU xFormers kernel test.

Testing

  • Attention-mask suite: 16 passed, including the real CUDA 0 to CUDA 1 regression.
  • Packing and attention-dispatch suites: 57 passed.
  • TRL padding-free compatibility suite: 122 passed, 26 skipped.
  • Raw-text suite: 9 passed.
  • Ruff, Python compilation, and diff checks passed.

Runtime validation

On a two-GPU CUDA host, packed and inherited masks created on cuda:0 were correctly moved for query, key, and value tensors on cuda:1. xFormers validation passed and memory-efficient attention completed on cuda:1 with finite output.

@oobabooga

Copy link
Copy Markdown
Member Author

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 10e2e72779

ℹ️ 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".

Comment thread unsloth/utils/packing.py Outdated
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a5797a06b9

ℹ️ 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".

Comment thread tests/utils/test_attention_masks.py Outdated
@oobabooga

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: fc3e858693

ℹ️ 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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a14236fa1f

ℹ️ 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".

Comment thread unsloth/utils/packing.py Outdated
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 1562d2e503

ℹ️ 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

Copy link
Copy Markdown
Member

Confirmed the cache key in unsloth/utils/packing.py still omits the device, so a mask built on cuda:0 can reach a layer running on cuda:1. Core (HF=latest + TRL=latest) is red here but green on main, could you take a look at that one?

@oobabooga

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: 136cfe1792

ℹ️ 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".

@oobabooga

Copy link
Copy Markdown
Member Author

@oobabooga
oobabooga merged commit 1840f7b into unslothai:main Aug 19, 2026
36 of 41 checks passed
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.

2 participants