Expose bicubic mode for torch::nn::functional::grid_sample in LibTorch (#150817)

When bicubic interpolation was added to grid_sampler in #44780, `GridSampleFuncOptions` was not updated to allow a user to use bicubic mode in LibTorch, even though the function could handle it. This PR fixes the parity such that LibTorch's  `torch::nn::functional::grid_sample` behaves the same as PyTorch's `torch.nn.functional.grid_sample`.

Existing users can directly use `torch::grid_sampler` but must know what int to pass for the interpolation (2 for bicubic) and padding mode parameters, which is not ideal.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/150817
Approved by: https://github.com/Skylion007
This commit is contained in:
inventshah
2025-04-21 08:55:27 +00:00
committed by PyTorch MergeBot
parent 2a9afdae81
commit bf28d1cafc
2 changed files with 14 additions and 1 deletions

View File

@ -631,6 +631,17 @@ TEST_F(FunctionalTest, GridSample) {
ASSERT_TRUE(output.allclose(expected));
// bicubic, zeros, true
options = F::GridSampleFuncOptions()
.mode(torch::kBicubic)
.padding_mode(torch::kZeros)
.align_corners(true);
output = F::grid_sample(input, grid, options);
expected = torch::tensor(
{{{{0., 0., 1.}, {3., 4., 5.}, {7., 8., 0.}}}}, torch::kFloat);
ASSERT_TRUE(output.allclose(expected));
// bilinear, border, true
options = F::GridSampleFuncOptions()
.mode(torch::kBilinear)

View File

@ -16,7 +16,9 @@ namespace torch::nn::functional {
/// F::GridSampleFuncOptions().mode(torch::kBilinear).padding_mode(torch::kZeros).align_corners(true));
/// ```
struct TORCH_API GridSampleFuncOptions {
typedef std::variant<enumtype::kBilinear, enumtype::kNearest> mode_t;
typedef std::
variant<enumtype::kBilinear, enumtype::kNearest, enumtype::kBicubic>
mode_t;
typedef std::
variant<enumtype::kZeros, enumtype::kBorder, enumtype::kReflection>
padding_mode_t;