Repository: microsoft/onnxruntime
File: onnxruntime/contrib_ops/cuda/grid_sample_impl.cu (CUDA)

Code before:
        grid_y_imgSpace < y_min || grid_y_imgSpace > y_max) { // out of bound
      if (padding_mode == 1) {  // border
        grid_x_imgSpace = max(0.0f, min(grid_x_imgSpace, W_in - 1.0f));
        grid_y_imgSpace = max(0.0f, min(grid_y_imgSpace, H_in - 1.0f));
      } else if (padding_mode == 2) {  // reflection
        grid_x_imgSpace = GsReflect(grid_x_imgSpace, x_min, x_max);
        grid_y_imgSpace = GsReflect(grid_y_imgSpace, y_min, y_max);
      }
    }

    if (mode == 0) {  // bilinear
      int x1 = floor(grid_x_imgSpace);
      int y1 = floor(grid_y_imgSpace);
      int x2 = x1 + 1;
      int y2 = y1 + 1;
      T w_lt = 0.0f;
      T w_rt = 0.0f;
      T w_lb = 0.0f;
      T w_rb = 0.0f;

      T w_r = grid_x_imgSpace - x1;
      T w_l = 1.0f - w_r;
      T w_b = grid_y_imgSpace - y1;
      T w_t = 1.0f - w_b;

      w_lt = w_t * w_l;
      w_rt = w_t * w_r;
      w_lb = w_b * w_l;
      w_rb = w_b * w_r;

      T lt_v = PixelAtGrid(input_data, BIdx, cIdx, y1, x1, padding_mode, N, C, H_in, W_in, border);
      T rt_v = PixelAtGrid(input_data, BIdx, cIdx, y1, x2, padding_mode, N, C, H_in, W_in, border);
      T lb_v = PixelAtGrid(input_data, BIdx, cIdx, y2, x1, padding_mode, N, C, H_in, W_in, border);
      T rb_v = PixelAtGrid(input_data, BIdx, cIdx, y2, x2, padding_mode, N, C, H_in, W_in, border);
      T interpoV = w_lt * lt_v + w_rt * rt_v + w_lb * lb_v + w_rb * rb_v;
      output_data[outIdx] = interpoV;
      return;
    }
    if (mode == 1) {  // nearest
      int x_n = grid_x_imgSpace;
      int y_n = grid_y_imgSpace;
      output_data[outIdx] = PixelAtGrid(input_data, BIdx, cIdx, y_n, x_n, padding_mode, N, C, H_in, W_in, border);
      return;
    }
    if (mode == 2) {  // bicubic
      int64_t x0 = static_cast<int64_t>(std::floor(grid_x_imgSpace)) - 1;  // top-left corner of the bbox
      int64_t y0 = static_cast<int64_t>(std::floor(grid_y_imgSpace)) - 1;
      T p[4][4] = {};  // [H][W]
      for (int64_t h = 0; h < 4; h++) {
        for (int64_t w = 0; w < 4; w++) {
          p[h][w] = PixelAtGrid(input_data, BIdx, cIdx, h + y0, w + x0, padding_mode, N, C, H_in, W_in, border);

Review comment (bug):
Just curious: If there are tests that triggered this bug fixing (mentioned in the PR), how come the CPU version of the op not need similar fixing (it also has similar clamping) ?

Code after:
    T grid_Y = grid_data[grid_idx * 2 + 1];
    int outIdx = idx;

    T grid_x_imgSpace = GsDenormalize(grid_X, W_in, align_corners == 1);
    T grid_y_imgSpace = GsDenormalize(grid_Y, H_in, align_corners == 1);
    if (mode == 1) {  //nearest
      grid_x_imgSpace = nearbyint(grid_x_imgSpace);
      grid_y_imgSpace = nearbyint(grid_y_imgSpace);
    }
    float x_min = -0.5f;
    float x_max = W_in - 0.5f;
    float y_min = -0.5f;
    float y_max = H_in - 0.5f;

    if (align_corners) {
      x_min = 0.0f;
      x_max = W_in - 1.0;
      y_min = 0.0f;
      y_max = H_in - 1.0f;
    }
    float border[] = {x_min, y_min, x_max, y_max};  // l-t-r-b
    if (grid_x_imgSpace < x_min || grid_x_imgSpace > x_max ||
        grid_y_imgSpace < y_min || grid_y_imgSpace > y_max) { // out of bound
      if (padding_mode == 1) {  // border
        // Clamping must not be done here, see #10607
        // grid_x_imgSpace = max(0.0f, min(grid_x_imgSpace, W_in - 1.0f));
        // grid_y_imgSpace = max(0.0f, min(grid_y_imgSpace, H_in - 1.0f));
      } else if (padding_mode == 2) {  // reflection
        grid_x_imgSpace = GsReflect(grid_x_imgSpace, x_min, x_max);
        grid_y_imgSpace = GsReflect(grid_y_imgSpace, y_min, y_max);
      }
    }

    if (mode == 0) {  // bilinear
      int x1 = floor(grid_x_imgSpace);
      int y1 = floor(grid_y_imgSpace);
      int x2 = x1 + 1;
      int y2 = y1 + 1;
      T w_lt = 0.0f;
      T w_rt = 0.0f;
      T w_lb = 0.0f;
      T w_rb = 0.0f;

      T w_r = grid_x_imgSpace - x1;
      T w_l = 1.0f - w_r;
      T w_b = grid_y_imgSpace - y1;
      T w_t = 1.0f - w_b;

      w_lt = w_t * w_l;
      w_rt = w_t * w_r;
      w_lb = w_b * w_l;