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.meshgrid to torch frontend #11560

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions ivy/functional/frontends/torch/miscellaneous_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def roll(input, shifts, dims=None):
return ivy.roll(input, shifts, axis=dims)


@to_ivy_arrays_and_back
def meshgrid(*tensors, indexing=None):
return ivy.meshgrid(*tensors, indexing=indexing)
Copy link
Contributor

@Infrared1029 Infrared1029 Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch's meshgrid returns a tuple so cast the output to a tuple instead of a list, (this also fixes the type issues and output length issues)



@to_ivy_arrays_and_back
@with_unsupported_dtypes(
{"1.11.0 and below": ("uint8", "bfloat16", "float16"), "1.12.1": ()},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,51 @@ def test_torch_roll(
)


# meshgrid
@handle_frontend_test(
fn_tree="torch.meshgrid",
dtypes_and_tensors=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("float").filter(
lambda x: "bfloat16" not in x[0]
),
num_arrays=st.integers(min_value=1, max_value=5),
min_num_dims=1,
max_num_dims=1,
min_dim_size=2,
max_dim_size=5,
shared_dtype=True,
),
indexing=st.sampled_from(['ij', 'xy']),
)
def test_torch_meshgrid(
*,
dtypes_and_tensors,
indexing,
on_device,
fn_tree,
frontend,
test_flags,
):
dtypes, tensors = dtypes_and_tensors
if isinstance(dtypes, list):
Copy link
Contributor

@Infrared1029 Infrared1029 Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if else statement here is actually not needed as the output is always a list, also no need to cast the output to a numpy array, it is already one, no need to cast the type either, just keep it as kwargs = { f"tensor{i}": tensor for i, tensor in enumerate(tensors) }

kwargs = {
f"tensor{i}": np.array(tensor, dtype=dtypes[i])
for i, tensor in enumerate(tensors)
}
else:
args = {"tensor": np.array(tensors, dtype=dtypes)}
kwargs['indexing'] = indexing
test_flags.num_positional_args = len(tensors) + 1
Copy link
Contributor

@Infrared1029 Infrared1029 Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need for the +1 in line 174, len(tensor) should be enough

helpers.test_frontend_function(
input_dtypes=dtypes,
frontend=frontend,
test_flags=test_flags,
fn_tree=fn_tree,
on_device=on_device,
**kwargs,
)


# fliplr
@handle_frontend_test(
fn_tree="torch.fliplr",
Expand Down