Skip to content

Skip fully-masked-out slices in masked softmax OpInfo samples - #196300

Closed
bodapatisaikrishna wants to merge 1 commit into
pytorch:mainfrom
bodapatisaikrishna:fix/masked-softmax-opinfo-fully-masked-196289
Closed

bodapatisaikrishna wants to merge 1 commit into
pytorch:mainfrom
bodapatisaikrishna:fix/masked-softmax-opinfo-fully-masked-196289

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown
Contributor

Summary

test/functorch/test_ops.py::TestOperators::test_vmapvjpvjp_masked_softmax_*
(and ..._masked_softmin_*) can fail with a nan vs 0 mismatch,
depending on the RNG stream of the device under test.

The failing sample is the 0-d case from sample_inputs_masked_softmax: a
scalar input with dim=0 and a mask drawn by _generate_masked_op_mask
via make_tensor(shape, dtype=torch.bool). For a 0-d input that mask is a
single random bool; when it is False the input is fully masked out,
which torch/masked/_ops.py explicitly documents as undefined:

If all elements of input along the given dim are ignored
(fully masked-out), the corresponding element of the output tensor will
have undefined value

The eager path yields nan and the vmap path yields 0, so the
gradient check compares values the op makes no promise about. CPU has
carried an xfail("masked.softmax", device_type="cpu") for this;
#193412 renamed the test class, which reshuffled the RNG draw and made
CUDA / ROCm / XPU start hitting the fully-masked mask too.

Changes

  • torch/testing/_internal/opinfo/definitions/_masked.py:
    sample_inputs_masked_softmax now skips a mask that leaves any slice
    along the softmax dim fully masked out (covers masked.softmax,
    masked.softmin, masked.log_softmax).
  • test/functorch/test_ops.py: remove the now-unnecessary
    xfail("masked.softmax", device_type="cpu") in test_vmapvjpvjp.

The change only drops the degenerate samples — with a fixed seed, the
generator went from 24 samples (2 with a fully-masked slice) to 22 (0),
and all non-degenerate samples are unchanged.

Testing

Verified on CPU (torch==2.14.0 runtime with the patched
_masked.py / test_ops.py overlaid):

# before: xfail (was masking a real nan-vs-0 failure)
$ python -m pytest test/functorch/test_ops.py \
    -k "test_vmapvjpvjp_masked_softmax and cpu and float32"
# with the xfail removed and the sample-generator fix NOT applied -> FAILS:
#   Greatest absolute difference: nan at index (0,)
#   Caused by sample input at index 23: SampleInput(input=Tensor[size=()],
#     args=(0), kwargs={'mask': Tensor[size=()] bool})

# after (fix applied, xfail removed):
$ python -m pytest test/functorch/test_ops.py \
    -k "masked_softmax or masked_softmin or masked_log_softmax"
41 passed, 1 skipped

Issue

Fixes #196289

torch.masked.{softmax,softmin,log_softmax} leave a reduction slice whose
elements are all masked out undefined (see torch/masked/_ops.py).
sample_inputs_masked_softmax draws its masks at random via make_tensor, so
for the 0-d input case a scalar False mask -- and other fully-masked
masks -- can occur. The eager result is then nan while the vmap result is
0, so test_vmapvjpvjp for masked.softmax fails a nan-vs-0 comparison
depending on the device's RNG stream (CPU carried an xfail for this;
a test-class rename shifted the draw and started hitting it on other
devices too).

Drop mask samples that leave any slice along the softmax dim fully masked
out, and remove the now-unnecessary CPU xfail on
test_vmapvjpvjp_masked_softmax.

Fixes pytorch#196289
@pytorch-bot

pytorch-bot Bot commented Sep 8, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@bodapatisaikrishna

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@zou3519 zou3519 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorry, this is not worth fixing, I don't think we support the _masked ops anymore

@zou3519 zou3519 closed this Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

masked.softmax/masked.softmin OpInfo samples can be fully masked-out, making test_vmapvjpvjp compare undefined values

4 participants