Fix torch.randint generating values equal to the upper bound (#2568)#2714
Merged
Conversation
torch.randint is documented as returning integers in [low, high), but the lowering used mb.random_uniform whose upper bound is inclusive — and cast-to-int truncates toward zero, so a sample exactly at high stays at high after casting. The reporter saw torch.randint(0, 100, ...) produce values up to 100. Clamp the cast result to high - 1 with mb.minimum so the converted graph matches torch's exclusive upper bound. Adds a regression test that asserts the lowered MIL graph contains the clamp op.
TobyRoseman
reviewed
May 23, 2026
| model = TestModel().eval() | ||
| x = torch.zeros((1000, 100), dtype=torch.int64) | ||
| torch_model = export_torch_model_to_frontend(model, (x,), frontend) | ||
| inputs = ( |
Collaborator
There was a problem hiding this comment.
Is this statement necessary? If so, why?
| ) | ||
| mlmodel = ct.convert(torch_model, inputs=inputs) | ||
|
|
||
| # Structural assertion: the lowering must wire a `minimum` clamp into |
Collaborator
There was a problem hiding this comment.
I don't understand what the rest of this test is doing. Why not just get the predictions from the converted model and make sure the max element is 100?
Contributor
Author
|
Done — replaced the MIL op-type walk with a prediction check.
Verified locally: passes on this branch, and fails on |
Collaborator
|
CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2553884155 If this passes, I will merge. |
TobyRoseman
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
torch.randint(low, high, ...)draws integers in[low, high)— the upper bound is exclusive. The lowering usedmb.random_uniform(whose bounds are inclusive) and cast the result to int, so the converted model could emit exactlyhigh(issue #2568).Clamp the cast result to
high - 1with aminimumop so the converted graph matches torch's exclusive upper bound:Fixes #2568.
Test plan
New
TestRandint::test_upper_bound_is_exclusive(TorchScript + TorchExport frontends; ExecuTorch is skipped becauseaten.randint.lowis not Aten Canonical) asserts the lowered graph wires aminimumclamp into the randint path, so the converted graph cannot produce values>= high.Verified locally on macOS that the clamp is present in the lowered graph (
convert_to="milinternal"):The full test converts to a serialized
mlprogram, so it runs end-to-end in CI.