-
Notifications
You must be signed in to change notification settings - Fork 90
Description
Hey, thanks or onnxscript! It's really great, I didn't expect this to be so easy and fun!
I'm trying to write a simple percentile function, like e.g. np.percentile(X, 95).
One of the steps (see below) is to have a sorted 1D tensor, for which I use the op.TopK function, since it seems to be the only available 'sorting' in ONNX. Please direct me if I'm wrong about it.
Unfortunately, TopK used as sorting requires the K to be passed as a length of the input tensor X.
I've followed the https://onnxscript.ai/tutorial/index.html#semantics-script-constants, since I know the size of X at 'compile time', (but not necessarily at 'write time of percentile()'. This is my implementation, which gives the following error.
from onnx import helper
X= np.array([5,7,2,8,1,2,3,4,8,9,4,7]).reshape(3,4)
M = helper.make_tensor("M", TensorProto.INT64, (1,), [len(X)])
@script()
def percentile(X: FLOAT[...], p: FLOAT[1])->FLOAT[1]:
Mc = op.Constant(value=M)
X_R = op.Reshape(X, [-1])
X_SORTED, _ = op.TopK(X_R, Mc, axis=0, largest=0)
IX = p/100.0 * op.Cast(Mc, to=TensorProto.DOUBLE)
IX_INT = op.Cast(IX, to=TensorProto.INT64)
return X_SORTED[IX_INT]
print(percentile(X, 90.0))
onnx.checker.check_model(percentile.to_model_proto())Non-zero status code returned while running TopK node. Name:'' Status Message: k tensor should be a 1D tensor of size 1
I tried using op.Size to get the length of X at runtime, tried different reshapes etc, but still it seems the tensor M (and constant Mc) I construct is not a "1D with 1 element).
Any hints?
Thanks in advance!