[inductor] Sample float uniform in torch._refs.geometric for integer dtypes (#198846) by syerva-g · Pull Request #200010 · pytorch/pytorch · GitHub
Skip to content

[inductor] Sample float uniform in torch._refs.geometric for integer dtypes (#198846) - #200010

Open
syerva-g wants to merge 2 commits into
pytorch:mainfrom
syerva-g:fix-198846-inductor-int-geometric
Open

syerva-g wants to merge 2 commits into
pytorch:mainfrom
syerva-g:fix-198846-inductor-int-geometric

Conversation

@syerva-g

@syerva-g syerva-g commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #198846

Summary

In torch._refs.geometric, torch.rand_like(self) is called without specifying a floating-point dtype. Because geometric uses ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT, an integer tensor self (int64, int32, int16, int8, uint8) preserves its integer dtype.

When compiled with TorchInductor, _rand_like decomposes torch.rand_like(self) to aten.rand(shape, dtype=self.dtype), which replace_random lowers to inductor_prims.random(size, seed, 'rand').to(self.dtype). Casting uniform [0, 1) floating-point numbers to an integer dtype truncates every sample to 0, causing torch.floor(torch.log1p(-0) / math.log1p(-p)) + 1 to silently return all 1s.

Fix

Sample torch.rand_like(self, dtype=rand_dtype) using torch.get_default_dtype() when self.dtype is an integer dtype, after which @elementwise_type_promotion_wrapper casts the result back to self.dtype.

Test Plan

  • pytest test/inductor/test_torchinductor.py -k test_geometric_integer_dtypes

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 7, 2026
@syerva-g

syerva-g commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: inductor"

cc @eellison @jansel @malfet — could you please take a look and approve the CI run when you get a chance? Thanks!

Comment thread torch/_refs/__init__.py Outdated
@syerva-g
syerva-g force-pushed the fix-198846-inductor-int-geometric branch from c4df698 to ee32bbc Compare October 14, 2026 18:28
@syerva-g

Copy link
Copy Markdown
Contributor Author

…dtypes (pytorch#198846)

In `torch._refs.geometric`, `torch.rand_like(self)` is called without specifying
a floating-point `dtype`. Because `geometric` uses `ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT`,
an integer tensor `self` (`int64`, `int32`, `int16`, `int8`, `uint8`) preserves its
integer dtype.

When compiled with TorchInductor, `_rand_like` decomposes `torch.rand_like(self)` to
`aten.rand(shape, dtype=self.dtype)`, which `replace_random` lowers to
`inductor_prims.random(size, seed, 'rand').to(self.dtype)`. Casting uniform `[0, 1)`
floating-point numbers to an integer dtype truncates every sample to `0`, causing
`torch.floor(torch.log1p(-0) / math.log1p(-p)) + 1` to silently return all `1`s.

Fix this by sampling `torch.rand_like(self, dtype=rand_dtype)` using
`torch.get_default_dtype()` when `self.dtype` is an integer dtype, after which
`@elementwise_type_promotion_wrapper` casts the result back to `self.dtype`.

Fixes pytorch#198846
…eta xfail

- Use a fixed torch.float32 for the uniform sample on integer dtypes (review feedback): matches the eager CUDA kernel (DiscreteDistributionType<scalar_t>::type is float for non-double dtypes) and inductor_prims.random (always float32), and is independent of torch.set_default_dtype().
- Remove the unittest.expectedFailure for TestCommon.test_python_ref_meta on integer dtypes in common_methods_invocations.py: the test now passes since _refs.geometric no longer calls rand_like on an integer tensor, which CI reported as 'Unexpected success'.
@syerva-g
syerva-g force-pushed the fix-198846-inductor-int-geometric branch 2 times, most recently from ee32bbc to dd32177 Compare October 21, 2026 13:26

This branch was successfully deployed

1 active (outdated) deployment
bedrock — 25be384b Deployed Oct 7, 2026 by pytorchbot via analyze #3150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/inductor missing actionable issue A PR without a linked actionable issue or maintainer sponsor module: inductor open source release notes: inductor 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.

[Inductor][CPU/CUDA] torch.compile makes integer geometric_ return all ones

3 participants