Skip to content

Commit

Permalink
Added abs_ instance methods and absolute and absolute_ method aliases…
Browse files Browse the repository at this point in the history
… and tested (#5963)

Co-authored-by: Intenzo21 <“[email protected]”>
  • Loading branch information
Intenzo21 and Intenzo21 authored Oct 23, 2022
1 parent 3d5e2f0 commit e1d1105
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ivy/functional/frontends/torch/Tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ def amax(self, dim=None, keepdim=False):
def abs(self, *, out=None):
return torch_frontend.abs(self.data, out=out)

def abs_(self):
self.data = self.abs()
return self.data

def contiguous(self, memory_format=torch.contiguous_format):
return self.data

def new_ones(self, size, * , dtype=None, device=None, requires_grad=False):
def new_ones(self, size, *, dtype=None, device=None, requires_grad=False):
return torch_frontend.ones(size, dtype=dtype, device=device,
requires_grad=requires_grad)

Expand Down Expand Up @@ -136,6 +140,9 @@ def __sub__(self, other, *, alpha=1):
def __truediv__(self, other, *, rounding_mode=None):
return torch_frontend.div(self, other, rounding_mode=rounding_mode)

# Method aliases
absolute, absolute_ = abs, abs_


# Tensor (alias)
tensor = Tensor
32 changes: 32 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 @@ -627,6 +627,38 @@ def test_torch_instance_abs(
)


# abs_
@handle_cmd_line_args
@given(
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float"),
)
)
def test_torch_instance_abs_(
dtype_and_x,
as_variable,
native_array,
):
input_dtype, x = dtype_and_x
helpers.test_frontend_method(
input_dtypes_init=input_dtype,
as_variable_flags_init=as_variable,
num_positional_args_init=1,
native_array_flags_init=native_array,
all_as_kwargs_np_init={
"data": x[0],
},
input_dtypes_method=input_dtype,
as_variable_flags_method=as_variable,
num_positional_args_method=0,
native_array_flags_method=native_array,
all_as_kwargs_np_method={},
frontend="torch",
class_name="tensor",
method_name="abs_",
)


# contiguous
@handle_cmd_line_args
@given(
Expand Down

0 comments on commit e1d1105

Please sign in to comment.