算子 ONNX TVM
NMS none none
topk TopK topk(data[, k, axis, ret_type, is_ascend, dtype])
Scatter Scatter (deprecated) scatter(data, indices, updates, axis)
RoiAlign RoiAlign none
Resize Resize **resize**(data, size[, layout, method, …])
NonZero NonZero none
Loop Loop none

NMS

Not found

TopK

Retrieve the top-K largest or smallest elements along a specified axis.
Given an input tensor of shape [a1, a_2, …, a_n, r] and integer argument k, return two outputs:
-Value tensor of shape [a_1, a_2, …, a
{axis-1}, k, a{axis+1}, … a_n] which contains the values of the top k elements along the specified axis
-Index tensor of shape [a_1, a_2, …, a
{axis-1}, k, a_{axis+1}, … a_n] which contains the indices of the top k elements (original indices from the input tensor).
If “largest” is 1 (the default value) then the k largest elements are returned.
If “sorted” is 1 (the default value) then the resulting k elements will be sorted.
If “sorted” is 0, order of returned ‘Values’ and ‘Indices’ are undefined.
Given two equivalent values, this operator uses the indices along the axis as a tiebreaker. That is, the element with the lower index will appear first.

Version

This version of the operator has been available since version 11 of the default ONNX operator set.
Other versions of this operator: 1, 10

Attributes

axis : int (default is -1)
Dimension on which to do the sort. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input).
largest : int (default is 1)
Whether to return the top-K largest or smallest elements.
sorted : int (default is 1)
Whether to return the elements in sorted order.

Inputs

X : TTensor of shape [a_1, a_2, …, a_n, r]
K : tensor(int64)A 1-D tensor containing a single positive value corresponding to the number of top elements to retrieve

Outputs

Values : TTensor of shape [a1, a_2, …, a{axis-1}, k, a{axis+1}, … a_n] containing top K values from the input tensor
Indices : ITensor of shape [a_1, a_2, …, a
{axis-1}, k, a_{axis+1}, … a_n] containing the corresponding input tensor indices for the top K values.

Type Constraints

T : tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(float16), tensor(float), tensor(double)Constrain input and output types to numeric tensors.
I : tensor(int64)Constrain index tensor to int64

Examples

top_k

  1. axis = 1
  2. largest = 1
  3. k = 3
  4. node = onnx.helper.make_node(
  5. 'TopK',
  6. inputs=['x', 'k'],
  7. outputs=['values', 'indices'],
  8. axis=axis
  9. )
  10. X = np.array([
  11. [0, 1, 2, 3],
  12. [4, 5, 6, 7],
  13. [8, 9, 10, 11],
  14. ], dtype=np.float32)
  15. K = np.array([k], dtype=np.int64)
  16. values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
  17. #print(values_ref)
  18. #[[ 3. 2. 1.]
  19. # [ 7. 6. 5.]
  20. # [11. 10. 9.]]
  21. #print(indices_ref)
  22. #[[3 2 1]
  23. # [3 2 1]
  24. # [3 2 1]]
  25. expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
  26. name='test_top_k')

top_k_negative_axis

  1. axis = -1
  2. largest = 1
  3. k = 3
  4. node = onnx.helper.make_node(
  5. 'TopK',
  6. inputs=['x', 'k'],
  7. outputs=['values', 'indices'],
  8. axis=axis
  9. )
  10. X = np.array([
  11. [0, 1, 2, 3],
  12. [4, 5, 6, 7],
  13. [8, 9, 10, 11],
  14. ], dtype=np.float32)
  15. K = np.array([k], dtype=np.int64)
  16. values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
  17. # print(values_ref)
  18. #[[ 3. 2. 1.]
  19. # [ 7. 6. 5.]
  20. # [11. 10. 9.]]
  21. # print(indices_ref)
  22. #[[3 2 1]
  23. # [3 2 1]
  24. # [3 2 1]]
  25. expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
  26. name='test_top_k_negative_axis')

top_k_smallest

  1. axis = 1
  2. largest = 0
  3. sorted = 1
  4. k = 3
  5. node = onnx.helper.make_node(
  6. 'TopK',
  7. inputs=['x', 'k'],
  8. outputs=['values', 'indices'],
  9. axis=axis,
  10. largest=largest,
  11. sorted=sorted
  12. )
  13. X = np.array([
  14. [0, 1, 2, 3],
  15. [4, 5, 6, 7],
  16. [11, 10, 9, 8],
  17. ], dtype=np.float32)
  18. K = np.array([k], dtype=np.int64)
  19. values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
  20. #print(values_ref)
  21. #[[ 0. 1. 2.]
  22. # [ 4. 5. 6.]
  23. # [ 8. 9. 10.]]
  24. #print(indices_ref)
  25. #[[0 1 2]
  26. # [0 1 2]
  27. # [3 2 1]]
  28. expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
  29. name='test_top_k_smallest')

Scatter

This operator is deprecated. Please use ScatterElements, which provides the same functionality.
Scatter takes three inputs data, updates, and indices of the same rank r >= 1 and an optional attribute axis that identifies an axis of data (by default, the outer-most axis, that is axis 0). The output of the operation is produced by creating a copy of the input data, and then updating its value to values specified by updates at specific index positions specified by indices. Its output shape is the same as the shape of data.
For each entry in updates, the target index in data is obtained by combining the corresponding entry in indices with the index of the entry itself: the index-value for dimension = axis is obtained from the value of the corresponding entry in indices and the index-value for dimension != axis is obtained from the index of the entry itself.
For instance, in a 2-D tensor case, the update corresponding to the [i][j] entry is performed as below:

  1. output[indices[i][j]][j] = updates[i][j] if axis = 0,
  2. output[i][indices[i][j]] = updates[i][j] if axis = 1,

This operator is the inverse of GatherElements. It is similar to Torch’s Scatter operation.
Example 1:

  1. data = [
  2. [0.0, 0.0, 0.0],
  3. [0.0, 0.0, 0.0],
  4. [0.0, 0.0, 0.0],
  5. ]
  6. indices = [
  7. [1, 0, 2],
  8. [0, 2, 1],
  9. ]
  10. updates = [
  11. [1.0, 1.1, 1.2],
  12. [2.0, 2.1, 2.2],
  13. ]
  14. output = [
  15. [2.0, 1.1, 0.0]
  16. [1.0, 0.0, 2.2]
  17. [0.0, 2.1, 1.2]
  18. ]

Example 2:

  1. data = [[1.0, 2.0, 3.0, 4.0, 5.0]]
  2. indices = [[1, 3]]
  3. updates = [[1.1, 2.1]]
  4. axis = 1
  5. output = [[1.0, 1.1, 3.0, 2.1, 5.0]]

Version

This version of the operator has been deprecated since version 13 of the default ONNX operator set.
Other versions of this operator: 9, 11

Examples

scatter_with_axis

  1. axis = 1
  2. node = onnx.helper.make_node(
  3. 'Scatter',
  4. inputs=['data', 'indices', 'updates'],
  5. outputs=['y'],
  6. axis=axis,
  7. )
  8. data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
  9. indices = np.array([[1, 3]], dtype=np.int64)
  10. updates = np.array([[1.1, 2.1]], dtype=np.float32)
  11. y = scatter(data, indices, updates, axis=axis)
  12. # print(y) produces
  13. # [[1.0, 1.1, 3.0, 2.1, 5.0]]
  14. expect(node, inputs=[data, indices, updates], outputs=[y],
  15. name='test_scatter_with_axis', opset_imports=[helper.make_opsetid("", 10)])

scatter_without_axis

  1. node = onnx.helper.make_node(
  2. 'Scatter',
  3. inputs=['data', 'indices', 'updates'],
  4. outputs=['y'],
  5. )
  6. data = np.zeros((3, 3), dtype=np.float32)
  7. indices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)
  8. updates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)
  9. y = scatter(data, indices, updates)
  10. # print(y) produces
  11. # [[2.0, 1.1, 0.0],
  12. # [1.0, 0.0, 2.2],
  13. # [0.0, 2.1, 1.2]]
  14. expect(node, inputs=[data, indices, updates], outputs=[y],
  15. name='test_scatter_without_axis', opset_imports=[helper.make_opsetid("", 10)])

RoiAlign

Resize

NonZero

Loop