Skip to content

Commit

Permalink
feat: add segment_sum function to Ivy TensorFlow frontend (#27459)
Browse files Browse the repository at this point in the history
Co-authored-by: manik <[email protected]>
Co-authored-by: ivy-branch <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2023
1 parent 55e0176 commit 6ddb9c5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ivy/functional/frontends/tensorflow/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,25 @@ def scalar_mul(scalar, x, name="scalar_mul"):
return ivy.multiply(x, scalar).astype(x.dtype)


@with_unsupported_dtypes(
{"2.15.0 and below": ("float16", "bool", "int16", "int8")},
"tensorflow",
)
@to_ivy_arrays_and_back
def segment_sum(data, segment_ids, name="segment_sum"):
data = ivy.array(data)
segment_ids = ivy.array(segment_ids)
ivy.utils.assertions.check_equal(
list(segment_ids.shape), [list(data.shape)[0]], as_array=False
)
sum_array = ivy.zeros(
tuple([int(segment_ids[-1] + 1)] + (list(data.shape))[1:]), dtype=data.dtype
)
for i in range((segment_ids).shape[0]):
sum_array[segment_ids[i]] = sum_array[segment_ids[i]] + data[i]
return sum_array


@to_ivy_arrays_and_back
def sigmoid(x, name=None):
return ivy.sigmoid(x)
Expand Down
33 changes: 33 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_tensorflow/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,39 @@ def test_tensorflow_scalar_mul(
)


@handle_frontend_test(
fn_tree="tensorflow.math.segment_sum",
data=helpers.array_values(dtype=helpers.get_dtypes("valid"), shape=(5, 6)),
segment_ids=helpers.array_values(
dtype=helpers.get_dtypes("signed_integer", prune_function=True),
shape=(5,),
min_value=0,
max_value=4,
),
test_with_out=st.just(False),
)
def test_tensorflow_segment_sum(
*,
data,
segment_ids,
frontend,
test_flags,
fn_tree,
backend_fw,
on_device,
):
helpers.test_frontend_function(
input_dtypes=[str(data.dtype), "int32", "int64"],
frontend=frontend,
backend_to_test=backend_fw,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
data=data,
segment_ids=np.sort(segment_ids),
)


# sigmoid
@handle_frontend_test(
fn_tree="tensorflow.math.sigmoid",
Expand Down

0 comments on commit 6ddb9c5

Please sign in to comment.