codeislife99 commented on a change in pull request #7562:
URL: https://github.com/apache/tvm/pull/7562#discussion_r585911327
##########
File path: python/tvm/relay/op/transform.py
##########
@@ -1450,6 +1450,61 @@ def sparse_reshape(sparse_indices, prev_shape,
new_shape):
return TupleWrapper(_make.sparse_reshape(sparse_indices, prev_shape,
new_shape), 2)
+def segment_sum(data, indices, num_segments=None):
+ """
+ Computes the sum along segments of a tensor. This op is much better
understood with
+ visualization articulated in the following links and examples at the end
of this docstring.
+
+
https://www.tensorflow.org/api_docs/python/tf/raw_ops/UnsortedSegmentSum?hl=fr
+
https://caffe2.ai/docs/sparse-operations.html#null__unsorted-segment-reduction-ops
+
+ Parameters
+ ----------
+ data : relay.Expr
+ Input floating point data
+ indices : relay.Expr
+ A 1-D tensor containing the indices of the rows to calculate the
output sum upon.
+ This tensor doesn't need to be sorted
+ num_segments : Optional[int]
+ An integer describing the shape of the zeroth dimension. If
unspecified, its calculated
+ equivalent to the number of unique indices
+ Returns
+ -------
+ result: relay.Expr
+ Output tensor.
+ Examples
+ --------
+ .. code-block:: python
+ data = [[1, 2, 3, 4],
+ [4, -3, 2, -1],
+ [5, 6, 7, 8]]
+ indices = [0, 0, 1]
+ result = segment_sum(data, indices)
+ result = [[5, -1, 5, 3],[5, 6, 7, 8]]
+
+ data = [[1, 2, 3, 4],
+ [4, -3, 2, -1],
+ [5, 6, 7, 8]]
+ indices = [2, 0, 0]
+ num_segments = 3
+ result = segment_sum(data, indices, num_segments)
+ result = [[5, 6, 7, 8],[0, 0, 0, 0], [5, -1, 5, 3]]
+ """
+
+ if num_segments:
+ num_unique = const([num_segments])
+ else:
+ _, _, num_unique = unique(reshape(indices, -1))
+ data_offrow_shape = strided_slice(_make.shape_of(data, "int64"), [1],
[-1], slice_mode="size")
Review comment:
I changed it to `int32`, No it shouldn't match the input datatype.
##########
File path: python/tvm/relay/op/transform.py
##########
@@ -1450,6 +1450,61 @@ def sparse_reshape(sparse_indices, prev_shape,
new_shape):
return TupleWrapper(_make.sparse_reshape(sparse_indices, prev_shape,
new_shape), 2)
+def segment_sum(data, indices, num_segments=None):
+ """
+ Computes the sum along segments of a tensor. This op is much better
understood with
+ visualization articulated in the following links and examples at the end
of this docstring.
+
+
https://www.tensorflow.org/api_docs/python/tf/raw_ops/UnsortedSegmentSum?hl=fr
+
https://caffe2.ai/docs/sparse-operations.html#null__unsorted-segment-reduction-ops
+
+ Parameters
+ ----------
+ data : relay.Expr
+ Input floating point data
Review comment:
Changed it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]