Repository navigation
[Fix][Relax][Frontend][Torch] Promote mixed-dtype branches in torch.where - #21130
Open
GoyalIshaan wants to merge 1 commit into
Open
GoyalIshaan wants to merge 1 commit into
GoyalIshaan wants to merge 1 commit into
Conversation
…here Fixes apache#20940. Promote mismatched TensorType branches in BaseFXGraphImporter._where using PyTorch's rank-aware torch.result_type on zero-allocation meta tensors (falling back to torch.promote_types when rank is unknown) before emitting relax.op.where, leaving condition unchanged. Adds LLVM/CPU tests in test_frontend_from_exported_program.py for mixed-dtype branch pairs in both orders, 0-D vs 1-D rank-aware cases, and mixed-dtype select_scatter, plus FX structural tests in test_frontend_from_fx.py.
This branch has not been deployed
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.
Fixes #20940.
BaseFXGraphImporter._wherepreviously passedxandystraight torelax.op.wherewithout dtype promotion. Non-boolmismatches (e.g.,int64/float32) failed inInferTypeWhere,bool/float32slipped past type inference and failed later intvm.compile, and ops decomposed intoaten.where.self(like mixed-dtypetorch.select_scatter) hit the same error.To fix this,
_wherenow promotes mismatchedTensorTypebranches before emittingrelax.op.where(leavingconditionunchanged and taking the fast path when dtypes already match). When both ranks are known (ndim >= 0), it computes the target dtype viatorch.result_typeon zero-allocation"meta"tensors (()for 0-D,(1,)for N-D) so 0-D vs N-D cases match PyTorch's rank-aware promotion rules (e.g., 1-Dfloat32+ 0-Dfloat64staysfloat32), falling back totorch.promote_typesif either rank is unknown, and casts any mismatched branch withrelax.op.astype. Becausefrom_exported_programandfrom_fxshare_where, both importers are covered without touching_binary_op.Testing
Added LLVM/CPU execution tests in
test_frontend_from_exported_program.py(6 dtype pairs in both branch orders includingbool/float32anduint8/int8, 0-D vs 1-D rank-aware cases, and mixed-dtypeselect_scatter) and structural IR tests intest_frontend_from_fx.py.pytest tests/python/relax/test_frontend_from_exported_program.py \ tests/python/relax/test_frontend_from_fx.py \ -k "test_where or test_binary_dtype_promotion" -v # 29 passed pre-commit run --files \ python/tvm/relax/frontend/torch/base_fx_graph_translator.py \ tests/python/relax/test_frontend_from_exported_program.py \ tests/python/relax/test_frontend_from_fx.py # All checks passed