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

add torch.tensor.masked_select #28780

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion ivy/functional/frontends/torch/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,11 @@ def masked_fill_(self, mask, value):
self.ivy_array = self.masked_fill(mask, value).ivy_array
return self

def masked_select(self, mask):
x, mask = torch_frontend.broadcast_tensors(self, mask)
out = x.reshape(-1).index_select(0, mask.reshape(-1).nonzero().flatten())
return torch_frontend.tensor(out)
Daniel4078 marked this conversation as resolved.
Show resolved Hide resolved

@with_unsupported_dtypes({"2.2 and below": ("float16", "bfloat16")}, "torch")
def index_add_(self, dim, index, source, *, alpha=1):
self.ivy_array = torch_frontend.index_add(
Expand Down Expand Up @@ -1891,7 +1896,9 @@ def addcdiv_(self, tensor1, tensor2, *, value=1):
).ivy_array
return self

@with_unsupported_dtypes({"2.2 and below": ("bfloat16", "float16")}, "torch")
@with_supported_dtypes(
{"2.2 and below": ("float32", "float64", "complex32", "complex64")}, "torch"
)
Daniel4078 marked this conversation as resolved.
Show resolved Hide resolved
def cholesky(self, upper=False):
return torch_frontend.cholesky(self, upper=upper)

Expand Down
35 changes: 35 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 @@ -9312,6 +9312,41 @@ def test_torch_masked_fill(
)


# masked_select
@handle_frontend_method(
class_tree=CLASS_TREE,
init_tree="torch.tensor",
method_name="masked_select",
x_mask_val=_masked_fill_helper(),
)
def test_torch_masked_select(
x_mask_val,
frontend_method_data,
init_flags,
method_flags,
frontend,
on_device,
backend_fw,
):
dtype, x, mask, _ = x_mask_val
helpers.test_frontend_method(
init_input_dtypes=[dtype],
backend_to_test=backend_fw,
init_all_as_kwargs_np={
"data": x,
},
method_input_dtypes=["bool", dtype],
method_all_as_kwargs_np={
"mask": mask,
},
frontend_method_data=frontend_method_data,
init_flags=init_flags,
method_flags=method_flags,
frontend=frontend,
on_device=on_device,
)


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