Skip to content

Revert "Say when xFormers is installed but its kernels cannot load" - #8237

Merged
danielhanchen merged 1 commit into
mainfrom
revert-8157-nv/p0-xformers
Aug 9, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
revert-8157-nv/p0-xformers

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Reverts #8157

@danielhanchen
danielhanchen requested a review from Datta0 as a code owner August 9, 2026 08:27
@danielhanchen
danielhanchen merged commit ee76b5f into main Aug 9, 2026
61 of 72 checks passed
@danielhanchen
danielhanchen deleted the revert-8157-nv/p0-xformers branch August 9, 2026 08:27

@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: a1eff9f455

ℹ️ 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/models/mistral.py
Comment on lines 140 to 141
causal_mask = causal_mask,
# The window the flash path already gets through window_size. SDPA needs it too, and
# not only in the branch above: training takes `elif self.training: pass`, so no 4D
# mask is synthesized, and with xformers off and flash absent the local window was the
# one thing nothing carried -- every sequence past config.sliding_window silently
# attended its whole causal history.
sliding_window = None if window_size == (-1, -1) else sw,
prefix_seg_info = _pg_seg,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Pass Mistral's sliding window into SDPA

When Mistral falls back to SDPA because FlashAttention/xFormers is unavailable or an attention_mask is supplied, this context now leaves sliding_window at its default None. The training path intentionally does not construct a 4D mask, and SDPA's is_causal mode permits the full causal history, so sequences longer than config.sliding_window silently train or infer with incorrect attention. Pass the normalized window through the context so the dispatcher can construct the local mask.

Useful? React with 👍 / 👎.

Comment on lines +75 to +77
_XFORMERS_FP32_UNSUPPORTED = (
torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 10
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Guard CUDA capability reads during import

On a CUDA host where is_available() succeeds but device properties are temporarily unreadable—for example, a device in exclusive-process mode or otherwise busy—get_device_capability() raises here while the module is being evaluated. Since the call is unguarded and runs even without xFormers, import unsloth fails instead of conservatively treating the capability as unknown; move this query behind an exception-safe helper.

Useful? React with 👍 / 👎.

Comment on lines +68 to +70
if HAS_XFORMERS and not HAS_FLASH_ATTENTION and torch.cuda.is_available():
if _xformers_disabled_for_capability(torch.cuda.get_device_capability()):
HAS_XFORMERS = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Probe the GPU assigned to the current rank

When torchrun exposes multiple GPUs and the application has not yet called torch.cuda.set_device(LOCAL_RANK) during import, this no-index capability query and the subsequent device="cuda" probe both target GPU 0. On a mixed host where GPU 0 is pre-sm_120 but another rank owns an sm_120 card, that rank skips the probe and later selects an xFormers kernel that may not exist for its card; the reverse arrangement can disable xFormers based on a GPU the rank never uses. Resolve and use the rank/current-device index for both the query and probe.

Useful? React with 👍 / 👎.

Comment thread unsloth/models/mistral.py
Comment on lines 111 to 113
sw_cfg = getattr(self.config, "sliding_window", None)
# A non-positive window means "no local attention", the same as absent -- which is how
# the mask builders above read it. Passing 0 through made window_size (0, 0) for flash
# and, once the dispatcher started honouring the window, an all-false SDPA mask: the
# lower bound q_pos - (0 - 1) sits above the causal upper bound, so nothing is visible.
if sw_cfg is None or sw_cfg == "null" or (isinstance(sw_cfg, int) and sw_cfg <= 0):
sw = kv_seq_len
else:
sw = sw_cfg
sw = kv_seq_len if (sw_cfg is None or sw_cfg == "null") else sw_cfg
window_size = (-1, -1) if (kv_seq_len <= sw) else (sw, sw)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Normalize a zero sliding window before FlashAttention

When a Mistral configuration uses sliding_window = 0 to mean that local attention is disabled—the later mask-building branch already treats nonpositive values this way—this calculation instead produces window_size = (0, 0) for every nonempty sequence. The FlashAttention path then restricts attention to a zero-width/self-only window rather than full causal history, producing incorrect outputs; normalize nonpositive values to an absent window before constructing the tuple.

Useful? React with 👍 / 👎.

Comment on lines +61 to +62
if capability[0] < 12:
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Probe xFormers on pre-sm_120 GPUs

On GPUs below sm_120, an xFormers package can import and register its operators while still lacking a runnable kernel for the installed torch/CUDA combination or the card's architecture, such as a source build compiled with a different TORCH_CUDA_ARCH_LIST. This unconditional early return treats that installation as usable without launching the kernel, so the first real attention forward fails instead of selecting SDPA; run the same small probe on every CUDA capability.

Useful? React with 👍 / 👎.

# one real forward to decide. Below sm_120 xformers always works; skip the probe.
if capability[0] < 12:
return False
return not probe()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep xFormers enabled after inconclusive probe failures

On an sm_120 host where the probe's tiny allocation or synchronization fails because the GPU is temporarily full, busy, or unavailable, probe() returns False and this code permanently disables xFormers for the process. Those failures do not establish that the kernel is incompatible, and the resulting SDPA fallback can substantially increase training memory use after the transient condition clears; classify resource/device-availability errors as inconclusive and leave xFormers enabled for the real forward.

Useful? React with 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant