Repository navigation
[Relax][FRONTEND][Pytorch] Add support for searchsorted - #20760
Netra-Srinivasan-McW wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Missing searchsorted rank and batch-shape validation allows incompatible inputs to reach CPU code that reads beyond the sequence buffer.
5 open issues
What changed in this PR
Adds PyTorch searchsorted support to TVM Relax, addressing the unsupported operator reported in #20739 for models such as pplx-embed-v2-late-9b.
Changes:
- Introduces the Relax operator, attributes, type inference, and Python/script APIs.
- Adds FX and exported-program conversion, CPU legalization, and GPU dispatch.
- Adds structural and CPU numerical tests covering search options and dynamic batch shapes.
| File | Description |
|---|---|
tests/python/relax/test_frontend_from_fx.py |
Adds structural FX import coverage. |
tests/python/relax/test_frontend_from_exported_program.py |
Adds structural and numerical tests, including dynamic shapes. |
src/relax/op/tensor/search.h |
Declares the C++ operator constructor. |
src/relax/op/tensor/search.cc |
Registers the operator, attributes, and type inference. |
python/tvm/relax/transform/legalize_ops/search.py |
Adds CPU legalization through TOPI. |
python/tvm/relax/script/ir_builder/op.py |
Exposes R.searchsorted in Relax scripts. |
python/tvm/relax/op/search.py |
Adds the public Python wrapper and documentation. |
python/tvm/relax/op/__init__.py |
Exports the new operator. |
python/tvm/relax/frontend/torch/fx_translator.py |
Registers FX conversion. |
python/tvm/relax/frontend/torch/exported_program_translator.py |
Registers tensor and scalar overloads. |
python/tvm/relax/frontend/torch/base_fx_graph_translator.py |
Translates operands, scalar constants, and search options. |
python/tvm/relax/backend/dispatch_sort_scan.py |
Selects CPU or GPU TOPI implementations. |
include/tvm/relax/op/search.h |
Defines output-dtype and insertion-side attributes. |
Review effort: Max
bc90aa8 to
73ce2c4
Compare
|
Hi @mshr-h, @yzhliu, @tqchen, @tlopex, I have updated the PR to address all the review changes:
Could you please take a look into the changes made when you get a chance? Thanks! |
There was a problem hiding this comment.
🟡 Changes recommended
Missing GPU target forwarding breaks CUDA compilation of relax.bucketize, while scalar-promotion and NaN-handling errors produce results inconsistent with PyTorch.
4 open issues
5 resolved since last review
Review effort: Max
|
Hi @mshr-h, @yzhliu, @tqchen, @tlopex, I have updated the PR to address all the review changes:
Could you take a look into the changes when you get a chance? Thanks! |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved correctness and safety issues include out-of-bounds CPU reads for symbolic batch shapes, CUDA compilation failures, and incorrect PyTorch searchsorted semantics.
9 open issues
Use GPU searchsorted lowering for CUDA targets · New Validate symbolic outer dimensions before kernel access · New Guard LLVM-dependent tests when LLVM is unavailable · New Reject unsupported non-None out arguments · New Promote boolean boundaries for integer scalar queries · New Materialize primitive scalar queries as tensors · New Apply PyTorch dtype promotion to tensor queries · New Defer inference when inputs need tensor normalization · New Preserve output shape and infer device from both operands · New
4 resolved since last review
Review effort: Max
| values = call.args[1] | ||
| right = call.attrs.right | ||
| out_dtype = "int32" if call.attrs.out_int32 else "int64" | ||
| return bb.call_te(topi.searchsorted, sorted_sequence, values, right, out_dtype) |
| static_cast<int>(values_shape->values.size()) == sorted_sequence_info->ndim) { | ||
| sym::Analyzer analyzer; | ||
| for (int i = 0; i + 1 < sorted_sequence_info->ndim; ++i) { | ||
| if (analyzer->CanProve(sequence_shape->values[i] != values_shape->values[i])) { |
|
|
||
|
|
||
| @pytest.mark.parametrize("right", [False, True]) | ||
| def test_searchsorted_nan_numerical(right): |
|
|
||
| def _searchsorted(self, node: fx.Node) -> relax.Var: | ||
| args = self.retrieve_args(node) | ||
| kwargs = self._retrieve_args(node.kwargs) |
| import torch | ||
|
|
||
| dtype = self._convert_data_type(torch.get_default_dtype()) | ||
| sorted_sequence = self.block_builder.emit(relax.op.astype(sorted_sequence, dtype)) |
|
|
||
| dtype = self._convert_data_type(torch.get_default_dtype()) | ||
| sorted_sequence = self.block_builder.emit(relax.op.astype(sorted_sequence, dtype)) | ||
| values = relax.const(values, dtype) |
| return self.block_builder.emit( | ||
| relax.op.searchsorted(sorted_sequence, values, out_int32=out_int32, right=right) | ||
| ) |
|
|
||
| Type InferTypeSearchSorted(const CallObj* call_node) { | ||
| const Call call = ffi::GetRef<Call>(call_node); | ||
| ffi::Array<TensorType> input_ty = GetInputTensorType(call); |
| const auto* data_shape = values_info->shape.as<ShapeExprObj>(); | ||
| if (data_shape) { | ||
| return TensorType(ShapeExpr(data_shape->values), out_dtype, values_info->vdevice); | ||
| } | ||
| return TensorType(out_dtype, values_info->ndim, values_info->vdevice); |
692db1d to
4207133
Compare
|
Hi @mshr-h, @yzhliu, @tqchen, @tlopex, I have updated the PR to address all the review comments:
Could you take a look into the changes when you get a chance? Thanks! |



This PR adds support for the
searchsortedoperator in TVM Relax, which is used in transformer models such as Perplexity AI's pplx-embed-v2-late-9b.The following requirements have been covered:
Tests have been added to validate the operator's behavior and compilation across the supported targets, covering both static and dynamic shapes.
Closes #20739