Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torch_frontend): added fill_diagonal_ method #27166

Merged
merged 8 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ivy/functional/backends/tensorflow/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ def scatter_flat(
if ivy.exists(size) and ivy.exists(target):
ivy.utils.assertions.check_equal(len(target.shape), 1, as_array=False)
ivy.utils.assertions.check_equal(target.shape[0], size, as_array=False)
if target_given:
updates = ivy.astype(updates, target.dtype)
if not target_given:
target = tf.zeros([size], dtype=updates.dtype)
res = tf.tensor_scatter_nd_update(target, tf.expand_dims(indices, -1), updates)
Expand Down
5 changes: 5 additions & 0 deletions ivy/functional/frontends/torch/tensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# global
from typing import Iterable
import math
from ivy.functional.frontends.torch.func_wrapper import to_ivy_arrays_and_back

# local
import ivy
Expand Down Expand Up @@ -2336,6 +2337,10 @@ def minimum(self, other, *, out=None):
def rad2deg(self, *, out=None):
return torch_frontend.rad2deg(self, out=out)

def fill_diagonal_(self, fill_value, wrap=False):
self._ivy_array = ivy.fill_diagonal(self._ivy_array, fill_value, wrap=wrap)
return self

@with_supported_dtypes(
{"2.2 and below": "valid"},
"torch",
Expand Down
43 changes: 43 additions & 0 deletions ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6907,6 +6907,49 @@ def test_torch_fill_(
)


# fill_diagonal_
@handle_frontend_method(
class_tree=CLASS_TREE,
init_tree="torch.tensor",
method_name="fill_diagonal_",
dtype_x_axis=helpers.dtype_values_axis(
available_dtypes=helpers.get_dtypes("float"),
min_num_dims=2,
min_dim_size=2,
max_num_dims=2,
),
val=helpers.floats(min_value=-10, max_value=10),
wrap=st.booleans(),
)
def test_torch_fill_diagonal_(
dtype_x_axis,
wrap,
val,
frontend,
frontend_method_data,
init_flags,
method_flags,
on_device,
backend_fw,
):
input_dtype, x, axis = dtype_x_axis
helpers.test_frontend_method(
init_input_dtypes=input_dtype,
backend_to_test=backend_fw,
init_all_as_kwargs_np={"data": x[0]},
method_input_dtypes=input_dtype,
method_all_as_kwargs_np={
"fill_value": val,
"wrap": wrap,
},
frontend_method_data=frontend_method_data,
init_flags=init_flags,
method_flags=method_flags,
frontend=frontend,
on_device=on_device,
)


# fix
@handle_frontend_method(
class_tree=CLASS_TREE,
Expand Down
Loading