Cast torch.multinomial output to int to match torch's int64 contract (fixes #2337)#2710
Open
LeSingh1 wants to merge 1 commit into
Open
Cast torch.multinomial output to int to match torch's int64 contract (fixes #2337)#2710LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
`torch.multinomial(input, num_samples, ...)` is documented to return a LongTensor (int64) of sampled indices. The converter dispatched to MIL's `random_categorical`, which preserves the float dtype of its `probs` input. That meant any downstream op that expected integer indices (e.g. `index_select`, `gather`, or simple Python-side `argmax`-style math on the result) silently received fp16/fp32 — and in some cases the conversion of the consumer would fail later for non-obvious reasons. Add an explicit `mb.cast(..., dtype="int32")` after the `random_categorical` call so the output dtype matches torch. Adds `TestMultinomial::test_multinomial_returns_int` which builds the program with `convert_to="milinternal"` and asserts the output dtype is int. Runs without BlobWriter; existing TestMultinomial tests still pass. Fixes apple#2337
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.multinomial(input, num_samples, ...)is documented to return aLongTensor(int64) of sampled indices. The current converter dispatches to MIL'srandom_categorical, which preserves the float dtype of itsprobsinput. So a model that ends intorch.multinomialproduces an fp16/fp32 output from the converted Core ML model, even though the user wrote code that expects integers.Repro on current
main:That mismatch silently breaks anyone downstream of multinomial (e.g.
gather/index_selectover a vocab table after sampling). Tracked in #2337.Fix
Add an explicit
mb.cast(..., dtype="int32")afterrandom_categoricalso the converted output dtype matches torch:int32is the integer dtype Core ML programs use for indices throughout the converter (matching the existingrandint,argmax, etc. paths).Tests
Adds
TestMultinomial::test_multinomial_returns_int:torch.multinomial(x, 3, replacement=True)model withconvert_to="milinternal"(runs without BlobWriter, as the structural assertion is the goal).random_categoricalandcast.types.is_int(out.dtype).Passes locally:
The existing
TestMultinomialcases (test_multinomial,test_multinomial_probs_instead_of_logits,test_multinomial_not_supported) are unaffected by the cast — they either don't assert on dtype, or compare counts where the int cast is a no-op semantically.Issue
Fixes #2337