You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "V-aerus (via GitHub)" <gi...@apache.org> on 2026/07/04 09:42:55 UTC
[PR] [Relax][PyTorch] Fix masked_select VM build [tvm]
V-aerus opened a new pull request, #19937:
URL: https://github.com/apache/tvm/pull/19937
This PR fixes the PyTorch ExportedProgram importer lowering for `torch.masked_select`.
Previously, `masked_select` lowered to:
- flatten data and mask
- `nonzero(mask_flat)`
- `squeeze(axis=[0])`
- `take(data_flat, indices)`
However, the result of `R.nonzero` only carried rank information. The following `R.squeeze` over the dynamic nonzero output could remain unhandled during build/VM execution.
This PR inserts a `match_cast` after `R.nonzero` using the exported output metadata, preserving the dynamic selected-length dimension before `squeeze`.
A numerical regression test is also added to cover:
PyTorch eager -> torch.export -> Relax import -> build -> VM run -> output comparison
Testing:
- `python -m pytest -q tests/python/relax/test_frontend_from_exported_program.py -k 'masked_select'`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][PyTorch] Fix masked_select VM build [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19937:
URL: https://github.com/apache/tvm/pull/19937#discussion_r3522945405
##########
python/tvm/relax/frontend/torch/base_fx_graph_translator.py:
##########
@@ -2619,6 +2619,16 @@ def _masked_select(self, node: fx.Node) -> relax.Var:
data_flat = self.block_builder.emit(relax.op.reshape(data, [-1]))
mask_flat = self.block_builder.emit(relax.op.reshape(mask, [-1]))
indices = self.block_builder.emit(relax.op.nonzero(mask_flat))
+ tensor_meta = node.meta.get("tensor_meta")
+ if tensor_meta is not None and len(tensor_meta.shape) == 1:
+ num_selected = tensor_meta.shape[0]
+ if not isinstance(num_selected, int):
+ num_selected = tirx.Var(str(num_selected), "int64")
+ else:
+ num_selected = tirx.Var(f"{node.name}_num_selected", "int64")
+ indices = self.block_builder.match_cast(
+ indices, relax.TensorType([1, num_selected], "int64")
+ )
Review Comment:

In TVM Relax, `match_cast` expects a `StructInfo` (such as `TensorStructInfo`) rather than a `Type` (such as `TensorType`). Additionally, `relax.TensorType`'s constructor takes `ndim` and `dtype` rather than a shape list, so passing `[1, num_selected]` as the first argument is incorrect. Please use `relax.TensorStructInfo` instead.
```suggestion
indices = self.block_builder.match_cast(
indices, relax.TensorStructInfo([1, num_selected], "int64")
)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][PyTorch] Fix masked_select VM build [tvm]
Posted by "V-aerus (via GitHub)" <gi...@apache.org>.
V-aerus commented on code in PR #19937:
URL: https://github.com/apache/tvm/pull/19937#discussion_r3523202149
##########
python/tvm/relax/frontend/torch/base_fx_graph_translator.py:
##########
@@ -2619,6 +2619,16 @@ def _masked_select(self, node: fx.Node) -> relax.Var:
data_flat = self.block_builder.emit(relax.op.reshape(data, [-1]))
mask_flat = self.block_builder.emit(relax.op.reshape(mask, [-1]))
indices = self.block_builder.emit(relax.op.nonzero(mask_flat))
+ tensor_meta = node.meta.get("tensor_meta")
+ if tensor_meta is not None and len(tensor_meta.shape) == 1:
+ num_selected = tensor_meta.shape[0]
+ if not isinstance(num_selected, int):
+ num_selected = tirx.Var(str(num_selected), "int64")
+ else:
+ num_selected = tirx.Var(f"{node.name}_num_selected", "int64")
+ indices = self.block_builder.match_cast(
+ indices, relax.TensorType([1, num_selected], "int64")
+ )
Review Comment:
Thanks for the review. I checked the current TVM Relax API and existing code patterns. `BlockBuilder.match_cast` is used with `relax.TensorType(shape, dtype)` in the current codebase, for example in `tests/python/relax/test_blockbuilder_core.py`, and the ONNX frontend has a similar `nonzero ->
match_cast` pattern using `relax.TensorType([1, num_nonzero], "int64")`.
Also, in this TVM version, `relax.TensorType` accepts a shape and dtype. I don't see `relax.TensorStructInfo` being used in the current tree. The current code passes the targeted masked_select tests, so I will keep this as `TensorType` unless maintainers prefer a different API.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Relax][PyTorch] Fix masked_select VM build [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19937:
URL: https://github.com/apache/tvm/pull/19937
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org