Skip to content

Fix SVE blendv mask handling after bool fill - #183697

Closed
robert-hardwick wants to merge 3 commits into
gh/robert-hardwick/26/basefrom
gh/robert-hardwick/26/head
Closed

robert-hardwick wants to merge 3 commits into
gh/robert-hardwick/26/basefrom
gh/robert-hardwick/26/head

Conversation

@robert-hardwick

@robert-hardwick robert-hardwick commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

This fixes the remaining SVE SDPA rewriter failures after the VecMask bit-cast fix.

The failing path comes from compiled CPU vector code for boolean mask operations such as:

mask.masked_fill(torch.logical_not(mask), -float("inf")).to(torch.float32)

On SVE, blendv was reconstructing its predicate by checking whether each stored mask lane was exactly 0xffffffff. That is too strict: due to a GCC codegen bug being tracked separately, a logically true lane can be produced with a different nonzero bit pattern rather than the canonical all-bits-true value. When that happened, blendv misread the lane as false and selected the wrong value.

This changes SVE blendv predicate reconstruction from “equal to all-bits-true” to “not equal to zero”, matching the boolean-mask contract: zero is false, any nonzero value is true.

The PR also adds a targeted regression test for the boolean masked_fill(..., -inf).to(float32) pattern and removes the SVE256 xfails from the affected SDPA rewriter tests.

Fixes #177244

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @jerryzh168 @aditew01 @voznesenskym @penguinwu @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented May 14, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/183697

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 4162289 with merge base 9df9278 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/inductor module: cpu CPU specific problem (e.g., perf, algorithm) module: inductor labels May 14, 2026
@robert-hardwick robert-hardwick added the ciflow/trunk Trigger trunk jobs on your pull request label May 14, 2026
@robert-hardwick

Copy link
Copy Markdown
Collaborator Author

@pytorchbot label "topic: not user facing"

@aditew01

Copy link
Copy Markdown
Collaborator

@claude review the PR, check if the unit test coverage is enough for this and are the ci failures related

@claude

claude Bot commented May 14, 2026 •

Copy link
Copy Markdown

Claude finished @aditew01's task in 4m 46s —— View job


PR Review: Fix SVE blendv mask handling after bool fill

  • Read the code changes and understand the fix
  • Assess unit test coverage
  • Check if CI failures are related
  • Post review feedback

Summary

The fix is correct and well-targeted. It changes SVE blendv predicate reconstruction in vec_float.h from strict equality with ALL_S32_TRUE_MASK (0xFFFFFFFF) to a "not equal to zero" check, matching the standard boolean mask contract.

Code Review

The fix (aten/src/ATen/cpu/vec/sve/vec_float.h:82):

// Before:
svbool_t mask = svcmpeq_s32(ptrue, svreinterpret_s32_f32(mask_), ALL_S32_TRUE_MASK);
// After:
svbool_t mask = svcmpne_s32(ptrue, svreinterpret_s32_f32(mask_), ZERO_S32);

This is the right fix. The x86 _mm256_blendv_ps uses the sign bit to select lanes (so any negative float bit-pattern selects b), but the semantic contract for blendv in PyTorch's vec abstraction is "nonzero = true". The old SVE code was overly strict by requiring the exact 0xFFFFFFFF pattern. With a GCC codegen quirk producing a non-canonical nonzero value for logically-true lanes, the strict check failed.

Potential concern: Consistency with other SVE types

The same strict svcmpeq pattern still exists in:

  • vec_double.h:83-84 — uses svcmpeq_s64(..., ALL_S64_TRUE_MASK)
  • vec_bfloat16.h:62-63 — uses svcmpeq_s16(..., ALL_S16_TRUE_MASK)
  • vec_int.h:69 — uses svcmpeq_s##bit(ptrue, mask_, ALL_S##bit##_TRUE_MASK)

If the GCC codegen bug can produce non-canonical true values for these types too, they could exhibit the same failure. Is the float path the only one that gets exercised in this particular SDPA code path, or should these be updated for consistency?

Test Coverage

New test (test/inductor/test_cpu_repro.py:979-998):

The test directly exercises the failing pattern: mask.masked_fill(logical_not(mask), -inf).to(float32) on a lower-triangular bool mask. It validates:

  1. Correctness — actual == expected (all 1.0s, since bool True filled everywhere then cast to float32)
  2. Vectorization — at least one vectorized kernel was generated

The test is appropriate and targeted. One minor observation:

  • The test uses torch.ones((16, 16), ...) which is 256 bool elements — exactly matching SVE256's capacity for 8 floats × 32 rows. The test is reasonable for triggering vectorization, though it runs on all vectorization-capable platforms (not just SVE). On x86, the old code already worked correctly, so this test simply becomes a regression guard on all platforms.
  • The expected = torch.ones_like(mask, dtype=torch.float32) is correct: masked_fill on a bool tensor with -inf converts to True (nonzero), making all elements True, then .to(float32) gives 1.0 everywhere.

xfail removals (test/inductor/test_fused_attention.py): Appropriate — the fix resolves the issue tracked in #177244, so the SVE256 xfails are no longer needed.

CI Failure Analysis

The one new failure is:

test/distributed/_composable/test_replicate_training.py::TestReplicateCastAfterInit::test_to_float64_after_init

This is on linux-jammy-rocm-py3.10-mi355 (ROCm GPU) in a distributed training test — completely unrelated to SVE vectorization changes. This is likely a pre-existing flake or infrastructure issue on the ROCm runner.

The 4 "unstable" jobs are all OSDC (inductor) jobs already marked as unstable on trunk — not caused by this PR.

Verdict

LGTM. The fix is minimal, correct, and well-tested. The only suggestion is to consider whether vec_double.h, vec_bfloat16.h, and vec_int.h need the same svcmpeq → svcmpne treatment for robustness, even if the current failure only manifests on the float path.


Anndrey24 added a commit to Anndrey24/pytorch that referenced this pull request May 14, 2026
This commits temporarily skips tests fixed by pytorch#182034 and pytorch#183697
@aditew01
aditew01 requested a review from desertfire May 14, 2026 12:45
[ghstack-poisoned]
robert-hardwick added a commit that referenced this pull request May 14, 2026
@robert-hardwick

Copy link
Copy Markdown
Collaborator Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased gh/robert-hardwick/26/orig onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/183697)

@Skylion007

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Approvers from one of the following sets are needed:

  • Metamates (8Keep, aditvenk, AmesingFlank, andrewor14, andyanwang, ...)
  • Core Reviewers (mruberry, lezcano, Skylion007, ngimel, peterbell10, ...)
  • Core Maintainers (soumith, gchanan, ezyang, malfet, albanD, ...)
Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Aug 21, 2026
@github-actions github-actions Bot closed this Sep 20, 2026
@github-actions
github-actions Bot deleted the gh/robert-hardwick/26/head branch October 21, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request module: cpu CPU specific problem (e.g., perf, algorithm) module: inductor open source Stale topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants