Repository: microsoft/onnxruntime
File: onnxruntime/test/providers/cpu/tensor/grid_sample_test.cc (C++)

Code before:
  if (version >= 20) {
    excluded_providers.insert(kCudaExecutionProvider);
  }
  return excluded_providers;
}

// DO NOT edit following tests. They are generated by:
// onnxruntime/test/providers/cpu/tensor/grid_sample_test_gen.py
TEST(GridsampleTest, test_grid_sample_16_4D_nearest_zeros_align_corners) {
  OpTester test("GridSample", 16);
  std::string mode = "nearest";
  std::string padding_mode = "zeros";
  int64_t align_corners = 1;
  std::initializer_list<int64_t> X_shape{2, 2, 3, 2};
  std::initializer_list<float> X_data{-1.125840f, -1.152360f, -0.250579f, -0.433879f, 0.848710f, 0.692009f, -0.316013f, -2.115219f, 0.468096f, -0.157712f, 1.443660f, 0.266049f, 0.166455f, 0.874382f, -0.143474f, -0.111609f, 0.931827f, 1.259009f, 2.004981f, 0.053737f, 0.618057f, -0.412802f, -0.841065f, -2.316042f};
  std::initializer_list<int64_t> Grid_shape{2, 3, 2, 2};
  std::initializer_list<float> Grid_data{0.063110f, -0.615220f, 0.203022f, -1.120434f, -0.867079f, -0.618636f, 0.757125f, 0.703586f, -0.532194f, -0.043299f, 0.767473f, 1.192960f, 0.476259f, 0.162111f, 0.804584f, -0.706563f, 0.223613f, -0.930367f, -0.831703f, -0.619900f, 0.542968f, 0.482592f, -0.710823f, 0.362529f};
  std::initializer_list<int64_t> Y_shape{2, 2, 3, 2};
  std::initializer_list<float> Y_data{-1.152360f, -1.152360f, -1.125840f, 0.692009f, -0.250579f, 0.692009f, -2.115219f, -2.115219f, -0.316013f, 0.266049f, 0.468096f, 0.266049f, -0.111609f, 0.874382f, 0.874382f, 0.166455f, -0.111609f, -0.143474f, -0.412802f, 0.053737f, 0.053737f, 2.004981f, -0.412802f, 0.618057f};
  test.AddInput<float>("X", X_shape, X_data);
  test.AddInput<float>("Grid", Grid_shape, Grid_data);
  test.AddAttribute("mode", mode);
  test.AddAttribute("padding_mode", padding_mode);
  test.AddAttribute("align_corners", align_corners);
  test.AddOutput<float>("Y", Y_shape, Y_data);
  test.Run(OpTester::ExpectResult::kExpectSuccess, "", GetExcludedExecutionProviders(16));
}

TEST(GridsampleTest, test_grid_sample_16_4D_nearest_zeros_no_align_corners) {
  OpTester test("GridSample", 16);
  std::string mode = "nearest";
  std::string padding_mode = "zeros";
  int64_t align_corners = 0;
  std::initializer_list<int64_t> X_shape{2, 2, 3, 2};
  std::initializer_list<float> X_data{-0.569248f, 0.919971f, 1.110816f, 1.289874f, -1.478174f, 2.567233f, -0.473120f, 0.335551f, -0.003304f, -0.534441f, 1.168688f, 0.394503f, 1.941462f, 0.791498f, -0.020252f, -0.437170f, -1.535287f, -0.412679f, 0.966303f, 1.624783f, -0.365619f, -1.302440f, 0.099403f, 0.441822f};
  std::initializer_list<int64_t> Grid_shape{2, 3, 2, 2};
  std::initializer_list<float> Grid_data{-1.143118f, -0.021569f, -0.903671f, -0.925628f, -0.066120f, 0.180174f, -0.491436f, 0.712053f, -0.730247f, 1.088844f, 0.822360f, -1.011940f, -0.298661f, 0.054147f, 0.175081f, 0.284609f, 0.470914f, 0.071880f, -0.585515f, 0.567827f, -1.151099f, -0.711248f, -0.300396f, -0.584536f};
  std::initializer_list<int64_t> Y_shape{2, 2, 3, 2};
  std::initializer_list<float> Y_data{0.000000f, -0.569248f, 1.110816f, -1.478174f, 0.000000f, 0.000000f, 0.000000f, -0.473120f, -0.003304f, 1.168688f, 0.000000f, 0.000000f, -0.020252f, -0.437170f, -0.437170f, -1.535287f, 0.000000f, 1.941462f, -0.365619f, -1.302440f, -1.302440f, 0.099403f, 0.000000f, 0.966303f};
  test.AddInput<float>("X", X_shape, X_data);
  test.AddInput<float>("Grid", Grid_shape, Grid_data);
  test.AddAttribute("mode", mode);
  test.AddAttribute("padding_mode", padding_mode);
  test.AddAttribute("align_corners", align_corners);
  test.AddOutput<float>("Y", Y_shape, Y_data);
  test.Run(OpTester::ExpectResult::kExpectSuccess, "", GetExcludedExecutionProviders(16));
}

TEST(GridsampleTest, test_grid_sample_16_4D_nearest_border_align_corners) {
  OpTester test("GridSample", 16);
  std::string mode = "nearest";

Review comment (suggestion):
Suggest to use ConfigEps like the following:
```
  std::vector<std::unique_ptr<IExecutionProvider>> execution_providers;
#ifdef USE_CUDA
  if constexpr (opset_version < 20) {
      execution_providers.emplace_back(DefaultCudaExecutionProvider());
  }
#endif
  execution_providers.emplace_back(DefaultCpuExecutionProvider());
  test.ConfigEps(std::move(execution_providers)).RunWithConfig();
 ```
 
Need change the test to cover both NCHW and NHWC: 
if ENABLE_CUDA_NHWC_OPS is defined, test NHWC like the following: transpose X, and Y, change operator domain to internal NHWC domain, then run the test again.

Code after:

  std::vector<std::unique_ptr<IExecutionProvider>> execution_providers;

  execution_providers.emplace_back(DefaultCpuExecutionProvider());
#ifdef USE_CUDA
  if (opset_version < 20) {
    execution_providers.emplace_back(DefaultCudaExecutionProvider());
#ifdef ENABLE_CUDA_NHWC_OPS
    execution_providers.push_back(DefaultCudaNHWCExecutionProvider());
#endif
  }

#endif
  return execution_providers;
}

template <typename T>
void RunTests(T& test, std::vector<std::unique_ptr<IExecutionProvider>>&& execution_providers) {
  for (size_t idx = 0; idx < execution_providers.size(); ++idx) {
    test.ConfigEp(std::move(execution_providers[idx])).RunWithConfig();
  }
  execution_providers.clear();
}

// DO NOT edit following tests. They are generated by:
// onnxruntime/test/providers/cpu/tensor/grid_sample_test_gen.py
TEST(GridsampleTest, test_grid_sample_16_4D_nearest_zeros_align_corners) {
  OpTester test("GridSample", 16);
  std::string mode = "nearest";
  std::string padding_mode = "zeros";
  int64_t align_corners = 1;
  std::initializer_list<int64_t> X_shape{2, 2, 3, 2};
  std::initializer_list<float> X_data{-1.125840f, -1.152360f, -0.250579f, -0.433879f, 0.848710f, 0.692009f, -0.316013f, -2.115219f, 0.468096f, -0.157712f, 1.443660f, 0.266049f, 0.166455f, 0.874382f, -0.143474f, -0.111609f, 0.931827f, 1.259009f, 2.004981f, 0.053737f, 0.618057f, -0.412802f, -0.841065f, -2.316042f};
  std::initializer_list<int64_t> Grid_shape{2, 3, 2, 2};
  std::initializer_list<float> Grid_data{0.063110f, -0.615220f, 0.203022f, -1.120434f, -0.867079f, -0.618636f, 0.757125f, 0.703586f, -0.532194f, -0.043299f, 0.767473f, 1.192960f, 0.476259f, 0.162111f, 0.804584f, -0.706563f, 0.223613f, -0.930367f, -0.831703f, -0.619900f, 0.542968f, 0.482592f, -0.710823f, 0.362529f};
  std::initializer_list<int64_t> Y_shape{2, 2, 3, 2};
  std::initializer_list<float> Y_data{-1.152360f, -1.152360f, -1.125840f, 0.692009f, -0.250579f, 0.692009f, -2.115219f, -2.115219f, -0.316013f, 0.266049f, 0.468096f, 0.266049f, -0.111609f, 0.874382f, 0.874382f, 0.166455f, -0.111609f, -0.143474f, -0.412802f, 0.053737f, 0.053737f, 2.004981f, -0.412802f, 0.618057f};
  test.AddInput<float>("X", X_shape, X_data);
  test.AddInput<float>("Grid", Grid_shape, Grid_data);
  test.AddAttribute("mode", mode);
  test.AddAttribute("padding_mode", padding_mode);
  test.AddAttribute("align_corners", align_corners);
  test.AddOutput<float>("Y", Y_shape, Y_data);
  RunTests(test, GetExecutionProviders(16));
}

TEST(GridsampleTest, test_grid_sample_16_4D_nearest_zeros_no_align_corners) {
  OpTester test("GridSample", 16);
  std::string mode = "nearest";
  std::string padding_mode = "zeros";
  int64_t align_corners = 0;