Repository navigation
[torchlib] Implement aten::searchsorted - #3172
Qiming Bao (14H034160212) wants to merge 3 commits into
Conversation
|
You missed NaN values here: |
Review feedback: for a NaN value both Less and LessOrEqual are false, so the count was 0. PyTorch's kernel compares with !(boundary >= value) and puts NaN past the last boundary on both sides. Count every boundary as below a NaN value for floating-point inputs.
|
Thanks dfedoryshchev, good catch. Fixed in 1d5d654 as you suggested: for floating-point inputs a NaN value now counts every boundary as below it ( |
aten_searchsortedis aNotImplementedErrorstub, sotorch.onnx.export(..., dynamo=True)fails on models that calltorch.searchsorted, for example top-p sampling (searchsorted(cumsum(softmax(logits)), p)) or quantile binning.This PR implements
aten::searchsorted.Tensorandaten::searchsorted.Scalar. The insertion index is the number of boundaries below each value:boundaries < valuefor the left side,boundaries <= valueforright=True/side="right". It comparesvalues[..., K, 1]againstsorted_sequence[..., 1, M]and sums over the last axis, so the same code handles a 1-D sequence (broadcast to every value) and batched N-D sequences. Also handled:sorter(applied withGatherElementson the last axis)out_int32Tests:
searchsortedtoops_test_data.py. All 768 non-empty OpInfo subtests pass. Size-0 samples are skipped with the samefixmeas other reductions (ORT reduces size-0 inputs to the wrong shape).torch.onnx.export(dynamo=True)+ ONNX Runtime, compared with eager: 13/13 cases match. They cover left/right/side,out_int32, batched N-D,sorter, 0-d and scalar values, int64 inputs, a top-p sampling pattern,bucketize, and dynamic batch/sequence/value sizes. On the released onnxscript, every case that reachessearchsortedfails to convert.ruff check/ruff format(0.15.8) clean.