Reject integral and bool dtypes in uniform decomposition by tekinertekin · Pull Request #196163 · pytorch/pytorch · GitHub
Skip to content

Reject integral and bool dtypes in uniform decomposition - #196163

Draft
tekinertekin wants to merge 1 commit into
pytorch:mainfrom
tekinertekin:rand-like-integral-dtype-195673
Draft

Reject integral and bool dtypes in uniform decomposition#196163
tekinertekin wants to merge 1 commit into
pytorch:mainfrom
tekinertekin:rand-like-integral-dtype-195673

Conversation

@tekinertekin

Copy link
Copy Markdown

Fixes #195673

Summary

Calling torch.rand_like or Tensor.uniform_ with integral/bool dtypes raises NotImplementedError in eager mode (via AT_DISPATCH_FLOATING_TYPES_AND2 in DistributionTemplates.h). However, under torch.compile / Inductor, the Python uniform() decomposition lacked this check.

Traced graphs were silently sampling [0, 1) floats and casting them to the target dtype:

  • Integral dtypes (int64, int32, int16, int8, uint8) truncated all values to 0.
  • bool dtypes converted non-zero floats to True.
  • torch.rand(4, dtype=torch.int64) under Inductor also returned zeros because replace_random bypasses the decomposition pass.

Upstream already notes this gap in torch/_refs/__init__.py:6639 (# TODO: fix inductor rand_like for integer, bool dtypes).

This PR adds non-floating/complex dtype checks to both uniform() decomposition and meta_rand_default(). Note that this is a behavioral change: code that previously returned zeros silently under Inductor will now raise NotImplementedError, matching eager mode. Other distributions (randn_like, normal_, exponential_) already raise in both modes, while randint_like continues to handle integers.

Behavior

Setup / Op Eager Mode Inductor (Before) Inductor (After)
rand_like(..., dtype=torch.int64) NotImplementedError [0, 0, 0, 0] NotImplementedError
rand_like(..., dtype=torch.bool) NotImplementedError [True, True, True, True] NotImplementedError
rand(4, dtype=torch.int64) NotImplementedError [0, 0, 0, 0] NotImplementedError

Across 15 tested op/dtype configurations (including float/complex types and invalid integer paths), eager and compiled outputs match 15/15.

Test Plan

python test/test_decomp.py -k test_uniform_integral_dtype
python test/test_decomp.py DecompOneOffTestsCPU

Without patch: test_uniform_integral_dtype fails 4/4 (NotImplementedError not raised).

With patch: test_uniform_integral_dtype passes 4/4.

Regression check: DecompOneOffTestsCPU: ran 22, OK (6 skipped, 1 expected failure).

Note: Tests were run against an installed PyTorch 2.14.0 wheel with these exact Python-side patches applied (test_meta.py was skipped locally due to internal API differences with main and relies on CI).

This PR description was generated with AI assistance.

torch.rand_like on an integer tensor raises in eager, because uniform_impl_
dispatches over floating types only (AT_DISPATCH_FLOATING_TYPES_AND2 in
DistributionTemplates.h). The Python uniform decomposition had no equivalent
check, so a traced graph sampled [0, 1) and converted to the input dtype
instead: every integral element truncates to 0 and every bool element converts
to True. Code asking for random values received a constant, with no error.

The check goes in the decomposition, next to the sampling it guards.
meta_rand_default repeats it because inductor's replace_random pass rewrites
aten.rand before the decomposition runs, so torch.rand(4, dtype=torch.int64)
would otherwise still return zeros under inductor. The sibling distributions
already reject these dtypes while tracing: randn_like, normal_ and
exponential_ all raise, and randint_like is integral by definition, so uniform
was the only gap.

Fixes pytorch#195673

Test Plan:

The new test fails on all four dtypes without the change ("NotImplementedError
not raised") and passes with it.

```
python test/test_decomp.py -k test_uniform_integral_dtype
python test/test_decomp.py DecompOneOffTestsCPU
```

This PR was authored with AI assistance.
@pytorch-bot

pytorch-bot Bot commented Sep 6, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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.

@pytorch-bot

pytorch-bot Bot commented Sep 6, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@pytorch-bot

pytorch-bot Bot commented Sep 6, 2026

Copy link
Copy Markdown

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.

torch.compile accepts rand_like on an integer tensor that eager rejects, and returns an all-zero tensor

2 participants