batch_norm_grad_kernel.cc 25.4 KB
Newer Older
H
hong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/batch_norm_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"

#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/gpu/batch_norm_utils.h"

namespace phi {

template <typename T>
using EigenArrayMap =
    Eigen::Map<Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>>;
template <typename T>
using ConstEigenArrayMap =
    Eigen::Map<const Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic>>;
template <typename T>
using EigenVectorArrayMap = Eigen::Map<Eigen::Array<T, Eigen::Dynamic, 1>>;
template <typename T>
using ConstEigenVectorArrayMap =
    Eigen::Map<const Eigen::Array<T, Eigen::Dynamic, 1>>;

template <typename T, typename Context>
void BatchNormGradRawKernel(const Context& ctx,
H
hong 已提交
40

H
hong 已提交
41 42 43
                            const DenseTensor& x,
                            const DenseTensor& scale,
                            const DenseTensor& bias,
H
hong 已提交
44 45
                            paddle::optional<const DenseTensor&> mean,
                            paddle::optional<const DenseTensor&> variance,
H
hong 已提交
46 47 48
                            const DenseTensor& saved_mean,
                            const DenseTensor& saved_variance,
                            paddle::optional<const DenseTensor&> reserve_space,
H
hong 已提交
49
                            const DenseTensor& y_grad,
H
hong 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                            float momentum,
                            float epsilon,
                            const std::string& data_layout_str,
                            bool is_test,
                            bool use_global_stats,
                            bool trainable_statistics,
                            bool fuse_with_relu,
                            bool is_inplace,
                            DenseTensor* x_grad,
                            DenseTensor* scale_grad,
                            DenseTensor* bias_grad) {
  const auto* d_y = &y_grad;

  DataLayout data_layout =
      paddle::framework::StringToDataLayout(data_layout_str);

  auto* d_x = x_grad;
  auto* d_scale = scale_grad;
  auto* d_bias = bias_grad;

  use_global_stats = is_test || use_global_stats;

  // batch_norm with inplace as false will take X as grad input, which
  // is same as cuDNN batch_norm backward calculation, batch_norm
  // with inplace as true only take Y as input and X should be calculate
  // by inverse operation of batch_norm on Y

  if (is_inplace) {
    if (d_x) {
      PADDLE_ENFORCE_EQ(d_x,
                        d_y,
                        phi::errors::InvalidArgument(
                            "X@GRAD and Y@GRAD inplaced in non-inplace mode"));
    }
  } else {
    if (d_x) {
      PADDLE_ENFORCE_NE(d_x,
                        d_y,
                        phi::errors::InvalidArgument(
                            "X@GRAD and Y@GRAD inplaced in non-inplace mode"));
    }
  }

  // Get the size for each dimension.
  // NCHW [batch_size, in_channels, in_height, in_width]
  const auto& x_dims = x.dims();
  PADDLE_ENFORCE_GE(
      x_dims.size(),
      2,
      phi::errors::InvalidArgument(
          "The size of input X's dimensions should be larger than 1."
          "But received: the size of input X's dimensions is [%d]",
          x_dims.size()));
  PADDLE_ENFORCE_LE(
      x_dims.size(),
      5,
      phi::errors::InvalidArgument(
          "The size of input X's dimensions should be less than 6."
          "But received: the size of input X's dimensions is [%d]",
          x_dims.size()));
  const int N = x_dims[0];
  const int C = (data_layout == DataLayout::kNCHW ? x_dims[1]
                                                  : x_dims[x_dims.size() - 1]);
  const int sample_size = x.numel() / N / C;

  // input dimension is 2 and the format is NCHW. The input can be regarded as
  // NHWC format
  if (x_dims.size() == 2 && data_layout == DataLayout::kNCHW) {
    data_layout = DataLayout::kNHWC;
  }

  // init output
  if (d_x) {
    ctx.template Alloc<T>(d_x);
  }

H
hong 已提交
126 127
  const T* mean_data = nullptr;
  const T* inv_var_data = nullptr;
H
hong 已提交
128 129 130 131 132 133 134 135 136 137 138 139
  DenseTensor inv_var_tensor;
  if (use_global_stats) {
    const auto* running_mean = mean.get_ptr();
    const auto* running_variance = variance.get_ptr();
    mean_data = running_mean->data<T>();
    inv_var_tensor.Resize({C});
    T* running_inv_var_data = ctx.template Alloc<T>(&inv_var_tensor);
    EigenVectorArrayMap<T> inv_var_tmp(running_inv_var_data, C);
    ConstEigenVectorArrayMap<T> var_arr(running_variance->data<T>(), C);

    inv_var_tmp = (var_arr + epsilon).sqrt().inverse();
    inv_var_data = running_inv_var_data;
H
hong 已提交
140 141 142
  } else {
    mean_data = saved_mean.data<T>();
    inv_var_data = saved_variance.data<T>();
H
hong 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  }

  ConstEigenVectorArrayMap<T> scale_arr(scale.data<T>(), C);
  ConstEigenVectorArrayMap<T> bias_arr(bias.data<T>(), C);
  ConstEigenVectorArrayMap<T> mean_arr(mean_data, C);
  ConstEigenVectorArrayMap<T> inv_var_arr(inv_var_data, C);

  T* d_bias_data = nullptr;
  T* d_scale_data = nullptr;
  if (d_scale && d_bias) {
    d_bias_data = ctx.template Alloc<T>(d_bias);
    d_scale_data = ctx.template Alloc<T>(d_scale);
  }

  // d_bias = np.sum(d_y, axis=0)
  // d_scale = np.sum((X - mean) / inv_std * dy, axis=0)
  // d_x = (1. / N) * scale * inv_var * (N * d_y - np.sum(d_y, axis=0)
  //   - (X - mean) * inv_var * inv_var * np.sum(d_y * (X - mean), axis=0))
  EigenVectorArrayMap<T> d_bias_arr(d_bias_data, C);
  EigenVectorArrayMap<T> d_scale_arr(d_scale_data, C);

  if (d_scale && d_bias) {
    d_bias_arr.setZero();
    d_scale_arr.setZero();
  }

  if (d_x && (N * sample_size) == 1 && !use_global_stats) {
    paddle::framework::TensorCopy(*d_y, ctx.GetPlace(), d_x);
    return;
  }

  int scale_coefff = use_global_stats ? 1 : N * sample_size;
  const auto scale_inv_var_nhw = scale_arr * inv_var_arr / scale_coefff;

  DenseTensor dy_sum;
  dy_sum.Resize({C});
  auto dy_sum_data = ctx.template Alloc<T>(&dy_sum);
  EigenVectorArrayMap<T> dy_sum_arr(dy_sum_data, C);

  DenseTensor dy_mul_x_sub_mean_mul_invstd_sum;
  dy_mul_x_sub_mean_mul_invstd_sum.Resize({C});
  auto dy_mul_x_sub_mean_mul_invstd_sum_data =
      ctx.template Alloc<T>(&dy_mul_x_sub_mean_mul_invstd_sum);
  EigenVectorArrayMap<T> dy_mul_x_sub_mean_mul_invstd_sum_arr(
      dy_mul_x_sub_mean_mul_invstd_sum_data, C);

  dy_sum_arr.setZero();
  dy_mul_x_sub_mean_mul_invstd_sum_arr.setZero();

  // inplace calculation
  // Y:  ((x - est_mean) * (inv_var) * scale + bias
  //   formula transform ====>
  //   (x * inv_var * scale) + (bias - est_mean * inv_var * scale)
  // X: (y - bias) / scale / (inv_var) + est_mean
  //   formula transform ====>
  //    (y - bias) / (scale * inv_var) + est_mean
  switch (data_layout) {
    case DataLayout::kNCHW: {
      if (is_inplace) {
        auto px = x;
        EigenArrayMap<T> x_data(ctx.template Alloc<T>(&px), sample_size, N * C);
        ConstEigenArrayMap<T> y_data(x.data<T>(), sample_size, N * C);
        for (int nc = 0; nc < N * C; ++nc) {
          x_data.col(nc) = (y_data.col(nc) - bias_arr(nc % C)) /
                               scale_inv_var_nhw(nc % C) / scale_coefff +
                           mean_arr(nc % C);
        }
      }
      ConstEigenArrayMap<T> x_arr(x.data<T>(), sample_size, N * C);
      ConstEigenArrayMap<T> d_y_arr(d_y->data<T>(), sample_size, N * C);

      for (int nc = 0; nc < N * C; ++nc) {
        int c = nc % C;
        dy_sum_arr(c) += d_y_arr.col(nc).sum();
        dy_mul_x_sub_mean_mul_invstd_sum_arr(c) +=
            ((x_arr.col(nc) - mean_arr(c)) * inv_var_arr(c) * d_y_arr.col(nc))
                .sum();
      }

      if (d_scale && d_bias) {
        d_bias_arr = dy_sum_arr;
        d_scale_arr = dy_mul_x_sub_mean_mul_invstd_sum_arr;
      }

      if (d_x) {
        EigenArrayMap<T> d_x_arr(
            ctx.template Alloc<T>(d_x), sample_size, N * C);
        if (!use_global_stats) {
          for (int nc = 0; nc < N * C; ++nc) {
            int c = nc % C;
            d_x_arr.col(nc) =
                scale_inv_var_nhw(c) *
                (d_y_arr.col(nc) * N * sample_size - dy_sum_arr(c) -
                 (x_arr.col(nc) - mean_arr[c]) *
                     dy_mul_x_sub_mean_mul_invstd_sum_arr(c) * inv_var_arr(c));
          }
        } else {
          for (int nc = 0; nc < N * C; ++nc) {
            int c = nc % C;
            d_x_arr.col(nc) = scale_inv_var_nhw(c) * d_y_arr.col(nc);
          }
        }
      }
      break;
    }
    case DataLayout::kNHWC: {
      if (is_inplace) {
        auto px = x;
        EigenArrayMap<T> x_data(ctx.template Alloc<T>(&px), C, N * sample_size);
        ConstEigenArrayMap<T> y_data(x.data<T>(), C, N * sample_size);
        for (int nhw = 0; nhw < N * sample_size; nhw++) {
          x_data.col(nhw) =
              (y_data.col(nhw) - bias_arr) / scale_inv_var_nhw / scale_coefff +
              mean_arr;
        }
      }
      ConstEigenArrayMap<T> x_arr(x.data<T>(), C, N * sample_size);
      ConstEigenArrayMap<T> d_y_arr(d_y->data<T>(), C, N * sample_size);

      for (int nhw = 0; nhw < N * sample_size; ++nhw) {
        dy_sum_arr += d_y_arr.col(nhw);
        dy_mul_x_sub_mean_mul_invstd_sum_arr +=
            (x_arr.col(nhw) - mean_arr) * inv_var_arr * d_y_arr.col(nhw);
      }

      if (d_scale && d_bias) {
        d_bias_arr = dy_sum_arr;
        d_scale_arr = dy_mul_x_sub_mean_mul_invstd_sum_arr;
      }

      if (d_x) {
        EigenArrayMap<T> d_x_arr(
            ctx.template Alloc<T>(d_x), C, N * sample_size);
        if (!use_global_stats) {
          for (int nhw = 0; nhw < N * sample_size; ++nhw) {
            d_x_arr.col(nhw) =
                scale_inv_var_nhw *
                (d_y_arr.col(nhw) * N * sample_size - dy_sum_arr -
                 (x_arr.col(nhw) - mean_arr) *
                     dy_mul_x_sub_mean_mul_invstd_sum_arr * inv_var_arr);
          }
        } else {
          for (int nhw = 0; nhw < N * sample_size; ++nhw) {
            d_x_arr.col(nhw) = scale_inv_var_nhw * d_y_arr.col(nhw);
          }
        }
      }
      break;
    }
    default:
      PADDLE_THROW(phi::errors::InvalidArgument("Unknown storage order: %s",
                                                data_layout_str));
  }
}

template <typename T, typename Context>
void BatchNormGradKernel(const Context& dev_ctx,
                         const DenseTensor& x,
                         const DenseTensor& scale,
                         const DenseTensor& bias,
H
hong 已提交
303 304
                         paddle::optional<const DenseTensor&> mean,
                         paddle::optional<const DenseTensor&> variance,
H
hong 已提交
305 306 307
                         const DenseTensor& saved_mean,
                         const DenseTensor& saved_variance,
                         paddle::optional<const DenseTensor&> reserve_space,
H
hong 已提交
308
                         const DenseTensor& y_grad,
H
hong 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322
                         float momentum,
                         float epsilon,
                         const std::string& data_layout,
                         bool is_test,
                         bool use_global_stats,
                         bool trainable_statistics,
                         bool fuse_with_relu,
                         DenseTensor* x_grad,
                         DenseTensor* scale_grad,
                         DenseTensor* bias_grad) {
  BatchNormGradRawKernel<T, Context>(dev_ctx,
                                     x,
                                     scale,
                                     bias,
H
hong 已提交
323 324
                                     mean,
                                     variance,
H
hong 已提交
325 326 327
                                     saved_mean,
                                     saved_variance,
                                     reserve_space,
H
hong 已提交
328
                                     y_grad,
H
hong 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
                                     momentum,
                                     epsilon,
                                     data_layout,
                                     is_test,
                                     use_global_stats,
                                     trainable_statistics,
                                     fuse_with_relu,
                                     false,
                                     x_grad,
                                     scale_grad,
                                     bias_grad);
}

template <typename T, typename Context>
void BatchNormDoubleGradKernel(const Context& ctx,
                               const DenseTensor& x_grad_grad,
                               const DenseTensor& scale_grad_grad,
                               const DenseTensor& bias_grad_grad,
                               const DenseTensor& y_grad,
                               const DenseTensor& x,
                               const DenseTensor& scale,
                               const DenseTensor& saved_mean,
                               const DenseTensor& saved_variance,
                               paddle::optional<const DenseTensor&> mean,
                               paddle::optional<const DenseTensor&> variance,
                               float momentum,
                               float epsilon,
                               const std::string& data_layout_str,
                               bool is_test,
                               bool use_global_stats,
                               bool trainable_statistics,
                               bool fuse_with_relu,
                               DenseTensor* x_grad,
                               DenseTensor* scale_grad,
                               DenseTensor* y_grad_grad) {
  const auto* X = &x;
  const auto* Scale = &scale;
  const auto* dY = &y_grad;
  const auto* Saved_mean = &saved_mean;
  const auto* Saved_variance = &saved_variance;

  PADDLE_ENFORCE_EQ(is_test,
                    false,
                    phi::errors::InvalidArgument(
                        "`is_test = True` CANNOT be used in train program. If "
                        "you want to use global status in pre_train model, "
                        "please set `use_global_stats = True`"));

  const auto data_layout =
      paddle::framework::StringToDataLayout(data_layout_str);

  const auto* ddX = &x_grad_grad;
  const auto* ddScale = &scale_grad_grad;
  const auto* ddBias = &bias_grad_grad;

  auto* dX = x_grad;
  auto* dScale = scale_grad;
  auto* ddY = y_grad_grad;
  ctx.template Alloc<T>(dX);
  ctx.template Alloc<T>(ddY);

  const auto& x_dims = X->dims();
  const int C = (data_layout == DataLayout::kNCHW ? x_dims[1]
                                                  : x_dims[x_dims.size() - 1]);
  const int sample_size = X->numel() / C;
  phi::funcs::SetConstant<Context, T> set_constant;

  const T* mean_data = Saved_mean->data<T>();
  const T* inv_var_data = Saved_variance->data<T>();

  DenseTensor inv_var_tensor;
  if (use_global_stats) {
    const auto* running_mean = mean.get_ptr();
    const auto* running_variance = variance.get_ptr();
    mean_data = running_mean->data<T>();
    inv_var_tensor.Resize({C});

    T* running_inv_var_data = ctx.template Alloc<T>(&inv_var_tensor);
    EigenVectorArrayMap<T> inv_var_tmp(running_inv_var_data, C);
    ConstEigenVectorArrayMap<T> var_arr(running_variance->data<T>(), C);

    inv_var_tmp = (var_arr + epsilon).sqrt().inverse();
    inv_var_data = running_inv_var_data;
  }

  // transpose NCHW -> NHWC for easy calculate
  DenseTensor transformed_x(X->type());
  DenseTensor transformed_dy(dY->type());
  DenseTensor transformed_ddx(ddX->type());

  DenseTensor transformed_dx(dX->type());
  DenseTensor transformed_ddy(ddY->type());
  if (data_layout == DataLayout::kNCHW && x_dims.size() > 2) {
    VLOG(3) << "Transform batchnorm output from NCHW to NHWC";
    // Input Tensor
    ResizeToChannelLast<Context, T>(ctx, X, &transformed_x);
    TransToChannelLast<Context, T>(ctx, X, &transformed_x);
    ResizeToChannelLast<Context, T>(ctx, dY, &transformed_dy);
    TransToChannelLast<Context, T>(ctx, dY, &transformed_dy);
    ResizeToChannelLast<Context, T>(ctx, ddX, &transformed_ddx);
    TransToChannelLast<Context, T>(ctx, ddX, &transformed_ddx);
    // Output Tensor
    ResizeToChannelLast<Context, T>(ctx, dX, &transformed_dx);
    ResizeToChannelLast<Context, T>(ctx, ddY, &transformed_ddy);
  } else {
    transformed_x.ShareDataWith(*X);
    transformed_dy.ShareDataWith(*dY);
    transformed_ddx.ShareDataWith(*ddX);

    transformed_dx.ShareDataWith(*dX);
    transformed_ddy.ShareDataWith(*ddY);
  }

  ConstEigenArrayMap<T> x_arr(transformed_x.data<T>(), C, sample_size);
  ConstEigenVectorArrayMap<T> mean_arr(mean_data, C);
  ConstEigenVectorArrayMap<T> inv_var_arr(inv_var_data, C);

  Tensor mean_tile;
  mean_tile.Resize({C, sample_size});
  EigenArrayMap<T> mean_tile_data(
      ctx.template Alloc<T>(&mean_tile), C, sample_size);

  DenseTensor inv_var_tile;
  inv_var_tile.Resize({C, sample_size});
  EigenArrayMap<T> inv_var_tile_data(
      ctx.template Alloc<T>(&inv_var_tile), C, sample_size);

  mean_tile_data = mean_arr.replicate(1, sample_size);
  inv_var_tile_data = inv_var_arr.replicate(1, sample_size);

  DenseTensor Scale_data;
  if (!Scale) {
    Scale_data.Resize({C});
    ctx.template Alloc<T>(&Scale_data);
    set_constant(ctx, &Scale_data, static_cast<T>(1));
  }
  ConstEigenVectorArrayMap<T> scale_arr(
      Scale ? Scale->data<T>() : Scale_data.data<T>(), C);

  Tensor scale_tile;
  scale_tile.Resize({C, sample_size});
  EigenArrayMap<T> scale_tile_data(
      ctx.template Alloc<T>(&scale_tile), C, sample_size);
  scale_tile_data = scale_arr.replicate(1, sample_size);

  ConstEigenArrayMap<T> dy_arr(transformed_dy.data<T>(), C, sample_size);
  ConstEigenArrayMap<T> ddx_arr(transformed_ddx.data<T>(), C, sample_size);

  DenseTensor x_sub_mean_mul_invstd;
  x_sub_mean_mul_invstd.Resize({C, sample_size});

  EigenArrayMap<T> x_sub_mean_mul_invstd_arr(
      ctx.template Alloc<T>(&x_sub_mean_mul_invstd), C, sample_size);
  x_sub_mean_mul_invstd_arr = (x_arr - mean_tile_data) * inv_var_tile_data;

  if (dX) {
    ctx.template Alloc<T>(dX);
    EigenArrayMap<T> dx_arr(
        ctx.template Alloc<T>(&transformed_dx), C, sample_size);
    dx_arr.setZero();
    if (use_global_stats) {
      // math: dx = (ddscale * dy) * inv_var
      if (ddScale) {
        ConstEigenVectorArrayMap<T> ddscale_arr(ddScale->data<T>(), C);
        Tensor ddscale_tile;
        ddscale_tile.Resize({C, sample_size});
        EigenArrayMap<T> ddscale_tile_data(
            ctx.template Alloc<T>(&ddscale_tile), C, sample_size);
        ddscale_tile_data = ddscale_arr.replicate(1, sample_size);

        dx_arr = dy_arr * ddscale_tile_data * inv_var_tile_data;
      }
    } else {
      // math: dx = scale * ((x - mean) * inv_var / NxHxW * (np.mean(ddx,
      // axis=(n,h,w)) *
      //          np.sum(dy, axis=(n,h,w)) -
      //          np.sum(dy * ddx, axis=(n,h,w)) + 3 * np.mean(dy * (x -
      //          mean),
      //          axis=(n,h,w)) * inv_var.pow(2) *
      //          np.sum(ddx * (x - mean), axis=(n,h,w))) + inv_var.pow(3) /
      //          NxHxW *
      //          np.sum(ddx * (x - mean)) *
      //          (np.mean(dy, axis=(n,h,w)) - dy) + inv_var.pow(3) / NxHxW *
      //          np.sum(dy,
      //          axis=(n,h,w)) * (x - mean) *
      //          (np.mean(ddx, axis=(n,h,w)) - ddx)) + ddr * (dy * inv_var -
      //          inv_var
      //          *
      //          np.mean(dy, axis=(n,h,w)) -
      //          inv_var.pow(3) * (x - mean) * np.mean(dy * (x - mean),
      //          axis=(n,h,w)))

      if (ddX) {
        dx_arr +=
            (x_sub_mean_mul_invstd_arr * inv_var_tile_data * inv_var_tile_data /
             sample_size)
                .colwise() *
            (ddx_arr.rowwise().sum() * dy_arr.rowwise().sum() / sample_size -
             (dy_arr * ddx_arr).rowwise().sum() +
             3. * (dy_arr * x_sub_mean_mul_invstd_arr).rowwise().sum() *
                 (ddx_arr * x_sub_mean_mul_invstd_arr).rowwise().sum() /
                 sample_size);

        dx_arr += (inv_var_tile_data * inv_var_tile_data).colwise() *
                  (ddx_arr * x_sub_mean_mul_invstd_arr).rowwise().sum() /
                  sample_size * (dy_arr.rowwise().sum() / sample_size - dy_arr);

        dx_arr += (inv_var_tile_data * inv_var_tile_data).colwise() *
                  (dy_arr * x_sub_mean_mul_invstd_arr).rowwise().sum() /
                  sample_size *
                  (ddx_arr.rowwise().sum() / sample_size - ddx_arr);

        dx_arr = scale_tile_data * dx_arr;
      }
      if (ddScale) {
        ConstEigenVectorArrayMap<T> ddscale_arr(ddScale->data<T>(), C);
        Tensor ddscale_tile;
        ddscale_tile.Resize({C, sample_size});
        EigenArrayMap<T> ddscale_tile_data(
            ctx.template Alloc<T>(&ddscale_tile), C, sample_size);
        ddscale_tile_data = ddscale_arr.replicate(1, sample_size);

        dx_arr +=
            (dy_arr * inv_var_tile_data -
             (dy_arr.rowwise().sum().replicate(1, sample_size) / sample_size) *
                 inv_var_tile_data -
             x_sub_mean_mul_invstd_arr * inv_var_tile_data *
                 (dy_arr * x_sub_mean_mul_invstd_arr)
                     .rowwise()
                     .sum()
                     .replicate(1, sample_size) /
                 sample_size) *
            ddscale_tile_data;
      }
    }
    if (data_layout == DataLayout::kNCHW) {
      VLOG(3) << "Transform batchnorm output from NHWC to NCHW";
      TransToChannelFirst<Context, T>(ctx, &transformed_dx, dX);
    }
  }
  if (dScale) {
    EigenVectorArrayMap<T> dscale_arr(ctx.template Alloc<T>(dScale), C);
    dscale_arr.setZero();
    if (use_global_stats) {
      // math: dscale = np.sum(ddx * dy, axis=(n,h,w)) * inv_var
      if (ddX) {
        dscale_arr = (ddx_arr * dy_arr * inv_var_tile_data).rowwise().sum();
      }
    } else {
      // math: dscale = inv_var * (dy - np.mean(dy, axis=(n,h,w) - (x-mean) *
      //            inv_var.pow(2) * np.mean(dy * (x-mean), axis=(n,h,w)))) *
      //            ddx
      if (ddX) {
        Tensor first_grad;
        first_grad.Resize({C, sample_size});
        EigenArrayMap<T> first_grad_arr(
            ctx.template Alloc<T>(&first_grad), C, sample_size);
        first_grad_arr.setZero();

        first_grad_arr +=
            inv_var_tile_data *
            (dy_arr -
             dy_arr.rowwise().sum().replicate(1, sample_size) / sample_size -
             x_sub_mean_mul_invstd_arr *
                 (dy_arr * x_sub_mean_mul_invstd_arr)
                     .rowwise()
                     .sum()
                     .replicate(1, sample_size) /
                 sample_size);
        dscale_arr = (first_grad_arr * ddx_arr).rowwise().sum();
      }
    }
  }

  if (ddY) {
    ctx.template Alloc<T>(ddY);
    EigenArrayMap<T> ddy_arr(
        ctx.template Alloc<T>(&transformed_ddy), C, sample_size);
    ddy_arr.setZero();
    if (use_global_stats) {
      // math: ddy = r * ddx * inv_var + ddbias +
      //           ddscale * (x - mean) * inv_var
      if (ddX) {
        ddy_arr = scale_tile_data * ddx_arr * inv_var_tile_data;
      }
    } else {
      // math: ddy = (x - mean) * inv_var * ddscale + ddbias +
      //           scale * inv_var * (ddx - (x - mean) * inv_var.pow(2) *
      //           np.mean(ddx * (x - mean), axis=(n,h,w)))
      if (ddX) {
        ddy_arr +=
            scale_tile_data * inv_var_tile_data *
            (ddx_arr -
             ddx_arr.rowwise().sum().replicate(1, sample_size) / sample_size -
             x_sub_mean_mul_invstd_arr *
                 (ddx_arr * x_sub_mean_mul_invstd_arr)
                     .rowwise()
                     .sum()
                     .replicate(1, sample_size) /
                 sample_size);
      }
    }
    if (ddScale) {
      ConstEigenVectorArrayMap<T> ddscale_arr(ddScale->data<T>(), C);
      Tensor ddscale_tile;
      ddscale_tile.Resize({C, sample_size});
      EigenArrayMap<T> ddscale_tile_data(
          ctx.template Alloc<T>(&ddscale_tile), C, sample_size);
      ddscale_tile_data = ddscale_arr.replicate(1, sample_size);

      ddy_arr += x_sub_mean_mul_invstd_arr * ddscale_tile_data;
    }

    if (ddBias) {
      ConstEigenVectorArrayMap<T> ddbias_arr(ddBias->data<T>(), C);
      Tensor ddbias_tile;
      ddbias_tile.Resize({C, sample_size});
      EigenArrayMap<T> ddbias_tile_data(
          ctx.template Alloc<T>(&ddbias_tile), C, sample_size);
      ddbias_tile_data = ddbias_arr.replicate(1, sample_size);

      ddy_arr += ddbias_tile_data;
    }

    if (data_layout == DataLayout::kNCHW) {
      VLOG(3) << "Transform batchnorm output from NHWC to NCHW";
      TransToChannelFirst<Context, T>(ctx, &transformed_ddy, ddY);
    }
  }
}

}  // namespace phi

PD_REGISTER_KERNEL(
    batch_norm_grad, CPU, ALL_LAYOUT, phi::BatchNormGradKernel, float, double) {
}

PD_REGISTER_KERNEL(batch_norm_grad_raw,
                   CPU,
                   ALL_LAYOUT,
                   phi::BatchNormGradRawKernel,
                   float,
                   double) {}

PD_REGISTER_KERNEL(batch_norm_grad_grad,
                   CPU,
                   ALL_LAYOUT,
                   phi::BatchNormDoubleGradKernel,
                   float,
                   double) {}