Skip to content

Conversation

DenisVieriu97
Copy link
Collaborator

Upstream grid_sampler_2d (cpu implementation) and the tests implemented by @versi379

Summary of changes:

  • Add grid_sampler_2d implementation
  • Add tests for grid_sampler_2d tests

cc @cccclai , @shoumikhin

Copy link

pytorch-bot bot commented Apr 29, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/10561

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit b4a4e04 with merge base b4e1145 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 29, 2025
@DenisVieriu97 DenisVieriu97 force-pushed the dev/denis/upstream_giovanni_grid_sampler_2d_support branch from 8f26df6 to 682b539 Compare April 29, 2025 21:58
@DenisVieriu97 DenisVieriu97 force-pushed the dev/denis/upstream_giovanni_grid_sampler_2d_support branch from 682b539 to 4450646 Compare May 1, 2025 04:17
@tarun292
Copy link
Contributor

tarun292 commented May 2, 2025

@cccclai @manuelcandales bumping up for a review.

@cccclai
Copy link
Contributor

cccclai commented May 5, 2025

It's portable op implementation, can @manuelcandales or @swolchok take a look?

Copy link
Contributor

@swolchok swolchok left a comment

Choose a reason for hiding this comment

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

I'm not familiar with this operator and its mathematical function, but here are some general C++ comments

Comment on lines +17 to +19
namespace torch {
namespace executor {
namespace native {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we have C++17, new code should use nested namespaces (namespace torch::executor::native {)

Comment on lines +21 to +23
using Tensor = exec_aten::Tensor;
using ScalarType = executorch::aten::ScalarType;
using SizesType = executorch::aten::SizesType;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: sort

using SizesType = executorch::aten::SizesType;

// Transform normalized coordinates to pixel space
inline float unnormalize_coord(float coord, int64_t size, bool align_corners) {
Copy link
Contributor

Choose a reason for hiding this comment

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

for internal functions like this one: remove inline, put in anonymous namespace

}

// Compute source index and interpolation weight
inline std::pair<int64_t, float>
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto inline

Comment on lines +55 to +56
int64_t abs_coord = std::abs(coord);
abs_coord = abs_coord % double_size;
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit: I see a couple places in this PR where you could be terser by performing more than one operation per statement. for example, here would be better as int64_t abs_coord = std::abs(coord) % double_size;


// Create a 1x1x4x4 input tensor
const std::vector<int32_t> input_sizes = {1, 1, 4, 4};
std::vector<float> input_data = {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you could use std::iota to make this a lot shorter
https://en.cppreference.com/w/cpp/algorithm/iota

align_corners,
out);

// Check non-zero
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment just repeats the code, delete (and similar comments throughout that repeat the code)


// Create a 1x1x4x4 input tensor
const std::vector<int32_t> input_sizes = {1, 1, 4, 4};
std::vector<float> input_data = {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto iota

@@ -1330,4 +1337,4 @@ def portable_source_list():

def portable_header_list():
"""All the header file names from //executorch/kernels/portable/cpu/"""
return ["selective_build.h", "scalar_utils.h", "math_constants.h", "vec_ops.h"]
return ["selective_build.h", "scalar_utils.h", "math_constants.h", "vec_ops.h"]
Copy link
Contributor

Choose a reason for hiding this comment

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

not 100% sure, but it looks like github is saying you removed the end-of-file newline? if so, please put it back

Comment on lines +240 to +241
(out.size(0) == N && out.size(1) == C && out.size(2) == out_H &&
out.size(3) == out_W),
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we be attempting to resize output to the desired size so that we can handle dynamic shapes smoothly? @manuelcandales

Copy link
Contributor

@manuelcandales manuelcandales May 7, 2025

Choose a reason for hiding this comment

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

Yes.
@DenisVieriu97 please follow this pattern

@@ -335,4 +336,4 @@ def define_common_targets():
_common_op_test("op_view_as_real_copy_test", ["aten", "portable"])
_common_op_test("op_view_copy_test", ["aten", "portable"])
_common_op_test("op_where_test", ["aten", "portable"])
_common_op_test("op_zeros_test", ["aten", "portable"])
_common_op_test("op_zeros_test", ["aten", "portable"])
Copy link
Contributor

Choose a reason for hiding this comment

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

newline?

const int64_t out_W = grid.size(2);

// Check for 4D input and grid
ET_KERNEL_CHECK(ctx, (input.dim() == 4), InvalidArgument, out);
Copy link
Contributor

Choose a reason for hiding this comment

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

@DenisVieriu97 do you mind refactoring all of the InvalidArgument checks into a helper function dedicated to check the arguments, which returns True only if all checks pass, and logs when a check doesn't pass. Here is an example
and then you would be calling
ET_KERNEL_CHECK(ctx, check_grid_sampler_2d_args(...) == 4), InvalidArgument, out);


// Process grid sampling with specified input, grid, and modes
template <typename T>
void grid_sampler_2d_impl(
Copy link
Contributor

Choose a reason for hiding this comment

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

This implementation requires default dim order. Add a check in your check_args function, to check that the dim order is the default.

Copy link

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. topic: not user facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants