dot_grad_kernel_impl.h 54.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17 18
#include "glog/logging.h"

19
#include "paddle/phi/common/complex.h"
20 21
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/complex_kernel.h"
22
#include "paddle/phi/kernels/full_kernel.h"
23
#include "paddle/phi/kernels/funcs/complex_functors.h"
24
#include "paddle/phi/kernels/funcs/eigen/common.h"
25
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"
26

27
namespace phi {
28 29 30 31 32 33 34 35 36 37 38 39

template <typename DeviceContext, typename T, typename Enabel = void>
struct DotGradFunction {
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* tensor_x,
                  const DenseTensor* tensor_y,
                  const DenseTensor* tensor_dout,
                  DenseTensor* tensor_dx,
                  DenseTensor* tensor_dy);
};

template <typename DeviceContext, typename T>
40
struct DotGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
41 42 43 44 45 46
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* tensor_x,
                  const DenseTensor* tensor_y,
                  const DenseTensor* tensor_dout,
                  DenseTensor* tensor_dx,
                  DenseTensor* tensor_dy) {
47
    VLOG(1) << "enable route";
48 49 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
#if defined(__NVCC__) || defined(__HIPCC__)
    if (1 == tensor_dout->dims().size()) {
      auto dout = EigenVector<T>::Flatten(*tensor_dout);

      if (tensor_dx) {
        auto y = EigenVector<T>::Flatten(*tensor_y);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 1> size(tensor_dx->numel());

        ConjKernel<T, DeviceContext>(ctx, *tensor_y, tensor_dx);

        auto dx = EigenVector<T>::Flatten(*tensor_dx);
        dx.device(dev) = dx * dout.broadcast(size);
      }

      if (tensor_dy) {
        auto x = EigenVector<T>::Flatten(*tensor_x);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 1> size(tensor_dy->numel());

        ConjKernel<T, DeviceContext>(ctx, *tensor_x, tensor_dy);

        auto dy = EigenVector<T>::Flatten(*tensor_dy);
        dy.device(dev) = dy * dout.broadcast(size);
      }
    } else {
      auto dout = EigenMatrix<T>::From(*tensor_dout);

      if (tensor_dx) {
77
        ctx.template Alloc<T>(tensor_dx);
78 79 80 81 82 83 84 85 86 87 88
        auto y = EigenMatrix<T>::From(*tensor_y);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 2> size(1, tensor_dx->dims()[1]);

        ConjKernel<T, DeviceContext>(ctx, *tensor_y, tensor_dx);

        auto dx = EigenMatrix<T>::From(*tensor_dx);
        dx.device(dev) = dx * dout.broadcast(size);
      }

      if (tensor_dy) {
89
        ctx.template Alloc<T>(tensor_dy);
90 91 92 93 94 95 96 97 98 99 100 101 102 103
        auto x = EigenMatrix<T>::From(*tensor_x);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 2> size(1, tensor_dy->dims()[1]);

        ConjKernel<T, DeviceContext>(ctx, *tensor_x, tensor_dy);

        auto dy = EigenMatrix<T>::From(*tensor_dy);
        dy.device(dev) = dy * dout.broadcast(size);
      }
    }
#else
    const auto* data_dout = tensor_dout->data<T>();

    if (tensor_dx) {
104
      auto* data_dx = ctx.template Alloc<T>(tensor_dx);
105 106
      const auto* data_y = tensor_y->data<T>();
      const DDim& dim = tensor_x->dims();
107
      size_t N = static_cast<size_t>(phi::product(dim));
108

109 110
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
111 112 113 114 115 116 117 118 119

      int s = -1;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) ++s;
        data_dx[i] = T(data_y[i].real, -data_y[i].imag) * data_dout[s];
      }
    }

    if (tensor_dy) {
120
      auto* data_dy = ctx.template Alloc<T>(tensor_dy);
121 122
      const auto* data_x = tensor_x->data<T>();
      const DDim& dim = tensor_y->dims();
123
      size_t N = static_cast<size_t>(phi::product(dim));
124

125 126
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
127 128 129 130 131 132 133 134 135 136 137 138

      int s = -1;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) ++s;
        data_dy[i] = T(data_x[i].real, -data_x[i].imag) * data_dout[s];
      }
    }
#endif
  }
};

template <typename DeviceContext, typename T>
139
struct DotGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
140 141 142 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
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* tensor_x,
                  const DenseTensor* tensor_y,
                  const DenseTensor* tensor_dout,
                  DenseTensor* tensor_dx,
                  DenseTensor* tensor_dy) {
#if defined(__NVCC__) || defined(__HIPCC__)
    if (1 == tensor_dout->dims().size()) {
      auto dout = EigenVector<T>::Flatten(*tensor_dout);
      if (tensor_dx) {
        auto y = EigenVector<T>::Flatten(*tensor_y);
        auto dx = EigenVector<T>::Flatten(*tensor_dx);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 1> size(tensor_dx->numel());
        dx.device(dev) = y * dout.broadcast(size);
      }

      if (tensor_dy) {
        auto x = EigenVector<T>::Flatten(*tensor_x);
        auto dy = EigenVector<T>::Flatten(*tensor_dy);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 1> size(tensor_dy->numel());
        dy.device(dev) = x * dout.broadcast(size);
      }
    } else {
      auto dout = EigenMatrix<T>::From(*tensor_dout);

      if (tensor_dx) {
168
        ctx.template Alloc<T>(tensor_dx);
169 170 171 172 173 174 175 176
        auto y = EigenMatrix<T>::From(*tensor_y);
        auto dx = EigenMatrix<T>::From(*tensor_dx);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 2> size(1, tensor_dx->dims()[1]);
        dx.device(dev) = y * dout.broadcast(size);
      }

      if (tensor_dy) {
177
        ctx.template Alloc<T>(tensor_dy);
178 179 180 181 182 183 184 185 186 187 188 189
        auto x = EigenMatrix<T>::From(*tensor_x);
        auto dy = EigenMatrix<T>::From(*tensor_dy);
        auto& dev = *ctx.eigen_device();
        Eigen::DSizes<int, 2> size(1, tensor_dy->dims()[1]);
        dy.device(dev) = x * dout.broadcast(size);
      }
    }
#else
    auto const *x = tensor_x->data<T>(), *y = tensor_y->data<T>(),
               *dz = tensor_dout->data<T>();
    auto&& d = tensor_x->dims();
    auto const N = tensor_x->numel();
190 191
    auto const _B = d.size() == 0 ? 1 : d[d.size() - 1];
    auto const B = _B != 0 ? _B : 1;
192 193

    if (tensor_dx) {
194
      auto* dx = ctx.template Alloc<T>(tensor_dx);
195 196 197 198 199 200 201
      for (auto j = 0; j < N / B; ++j) {
        auto const ss = dz[j];
        for (auto i = 0; i < B; ++i) *dx++ = *y++ * ss;
      }
    }

    if (tensor_dy) {
202
      auto* dy = ctx.template Alloc<T>(tensor_dy);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
      for (auto j = 0; j < N / B; ++j) {
        auto const ss = dz[j];
        for (auto i = 0; i < B; i++) *dy++ = *x++ * ss;
      }
    }
#endif
  }
};

template <typename DeviceContext, typename T, typename Enabel = void>
struct DotDoubleGradFunction {
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* tensor_x,
                  const DenseTensor* tensor_y,
                  const DenseTensor* tensor_dout,
218 219
                  const paddle::optional<DenseTensor>* tensor_ddx_opt,
                  const paddle::optional<DenseTensor>* tensor_ddy_opt,
220 221 222 223 224 225
                  DenseTensor* tensor_dx,
                  DenseTensor* tensor_dy,
                  DenseTensor* tensor_ddout);
};

template <typename DeviceContext, typename T>
226
struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
227 228 229 230
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* tensor_x,
                  const DenseTensor* tensor_y,
                  const DenseTensor* tensor_dout,
231 232
                  const paddle::optional<DenseTensor>* tensor_ddx_opt,
                  const paddle::optional<DenseTensor>* tensor_ddy_opt,
233 234 235
                  DenseTensor* tensor_dx,
                  DenseTensor* tensor_dy,
                  DenseTensor* tensor_ddout) {
236 237
    const DenseTensor* tensor_ddx = tensor_ddx_opt->get_ptr();
    const DenseTensor* tensor_ddy = tensor_ddy_opt->get_ptr();
238 239 240 241 242 243 244
#if defined(__NVCC__) || defined(__HIPCC__)
    if (1 == tensor_dout->dims().size()) {
      DenseTensor tensor_dout_help;
      auto& dev = *ctx.eigen_device();
      if (tensor_dx || tensor_dy) {
        tensor_dout_help = Conj<T, DeviceContext>(ctx, *tensor_dout);
      }
245 246
      if (tensor_dx && tensor_ddy) {
        ctx.template Alloc<T>(tensor_dx);
247 248 249 250 251
        auto ddy = EigenVector<T>::Flatten(*tensor_ddy);
        Eigen::DSizes<int, 1> size(tensor_ddy->numel());
        auto dx = EigenVector<T>::Flatten(*tensor_dx);
        auto dout = EigenVector<T>::Flatten(tensor_dout_help);
        dx.device(dev) = ddy * dout.broadcast(size);
252 253 254
      } else if (tensor_dx && !tensor_ddy) {
        FullLikeKernel<T, DeviceContext>(
            ctx, *tensor_x, Scalar(T(0.0, 0.0)), tensor_x->dtype(), tensor_dx);
255 256
      }

257 258
      if (tensor_dy && tensor_ddx) {
        ctx.template Alloc<T>(tensor_dy);
259 260 261 262 263
        auto ddx = EigenVector<T>::Flatten(*tensor_ddx);
        Eigen::DSizes<int, 1> size(tensor_ddx->numel());
        auto dy = EigenVector<T>::Flatten(*tensor_dy);
        auto dout = EigenVector<T>::Flatten(tensor_dout_help);
        dy.device(dev) = ddx * dout.broadcast(size);
264 265 266
      } else if (tensor_dy && !tensor_ddx) {
        FullLikeKernel<T, DeviceContext>(
            ctx, *tensor_y, Scalar(T(0.0, 0.0)), tensor_y->dtype(), tensor_dy);
267 268
      }

269 270
      if (tensor_ddout && tensor_ddx && tensor_ddy) {
        ctx.template Alloc<T>(tensor_ddout);
271 272 273 274 275 276 277 278 279
        DenseTensor tensor_x_help = Conj<T, DeviceContext>(ctx, *tensor_x);
        DenseTensor tensor_y_help = Conj<T, DeviceContext>(ctx, *tensor_y);

        auto x = EigenVector<T>::Flatten(tensor_x_help);
        auto y = EigenVector<T>::Flatten(tensor_y_help);
        auto ddx = EigenVector<T>::Flatten(*tensor_ddx);
        auto ddy = EigenVector<T>::Flatten(*tensor_ddy);
        auto ddout = EigenVector<T>::Flatten(*tensor_ddout);
        ddout.device(dev) = (x * ddy + y * ddx).sum();
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      } else if (tensor_ddout && tensor_ddx && !tensor_ddy) {
        ctx.template Alloc<T>(tensor_ddout);
        DenseTensor tensor_y_help = Conj<T, DeviceContext>(ctx, *tensor_y);

        auto y = EigenVector<T>::Flatten(tensor_y_help);
        auto ddx = EigenVector<T>::Flatten(*tensor_ddx);
        auto ddout = EigenVector<T>::Flatten(*tensor_ddout);
        ddout.device(dev) = (y * ddx).sum();
      } else if (tensor_ddout && !tensor_ddx && tensor_ddy) {
        ctx.template Alloc<T>(tensor_ddout);
        DenseTensor tensor_x_help = Conj<T, DeviceContext>(ctx, *tensor_x);

        auto x = EigenVector<T>::Flatten(tensor_x_help);
        auto ddy = EigenVector<T>::Flatten(*tensor_ddy);
        auto ddout = EigenVector<T>::Flatten(*tensor_ddout);
        ddout.device(dev) = (x * ddy).sum();
296 297 298 299 300
      }
    }
#else
    const auto* data_dout = tensor_dout->data<T>();

301
    if (tensor_dx && tensor_ddy) {
302
      auto* data_dx = ctx.template Alloc<T>(tensor_dx);
303 304 305 306
      const auto* data_ddy = tensor_ddy->data<T>();
      const DDim& dim = tensor_dx->dims();
      size_t N = static_cast<size_t>(product(dim));

307 308
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
309 310 311 312 313 314

      int s = -1;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) ++s;
        data_dx[i] = T(data_dout[s].real, -data_dout[s].imag) * data_ddy[i];
      }
315 316 317
    } else if (tensor_dx && !tensor_ddy) {
      FullLikeKernel<T, DeviceContext>(
          ctx, *tensor_x, Scalar(T(0.0, 0.0)), tensor_x->dtype(), tensor_dx);
318 319
    }

320
    if (tensor_dy && tensor_ddx) {
321
      auto* data_dy = ctx.template Alloc<T>(tensor_dy);
322 323 324 325
      const auto* data_ddx = tensor_ddx->data<T>();
      const DDim& dim = tensor_dy->dims();
      size_t N = static_cast<size_t>(product(dim));

326 327
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
328 329 330 331 332 333

      int s = -1;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) ++s;
        data_dy[i] = T(data_dout[s].real, -data_dout[s].imag) * data_ddx[i];
      }
334 335 336
    } else if (tensor_dy && !tensor_ddx) {
      FullLikeKernel<T, DeviceContext>(
          ctx, *tensor_y, Scalar(T(0.0, 0.0)), tensor_y->dtype(), tensor_dy);
337 338
    }

339
    if (tensor_ddout && tensor_ddx && tensor_ddy) {
340
      auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
341 342 343 344 345 346 347
      auto* data_x = tensor_x->data<T>();
      auto* data_y = tensor_y->data<T>();
      auto* data_ddx = tensor_ddx->data<T>();
      auto* data_ddy = tensor_ddy->data<T>();

      const DDim& dim = tensor_dy->dims();
      size_t N = static_cast<size_t>(product(dim));
348 349
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
      int s = -1;
      bool new_s = false;

      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) {
          ++s;
          new_s = true;
        }
        if (new_s) {
          data_ddout[s] = T(data_x[i].real, -data_x[i].imag) * data_ddy[i] +
                          T(data_y[i].real, -data_y[i].imag) * data_ddx[i];
        } else {
          data_ddout[s] += T(data_x[i].real, -data_x[i].imag) * data_ddy[i] +
                           T(data_y[i].real, -data_y[i].imag) * data_ddx[i];
        }
        new_s = false;
      }
367 368 369 370 371 372 373
    } else if (tensor_ddout && tensor_ddx && !tensor_ddy) {
      auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
      auto* data_y = tensor_y->data<T>();
      auto* data_ddx = tensor_ddx->data<T>();

      const DDim& dim = tensor_dy->dims();
      size_t N = static_cast<size_t>(product(dim));
374 375
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
      int s = -1;
      bool new_s = false;

      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) {
          ++s;
          new_s = true;
        }
        if (new_s) {
          data_ddout[s] = T(data_y[i].real, -data_y[i].imag) * data_ddx[i];
        } else {
          data_ddout[s] += T(data_y[i].real, -data_y[i].imag) * data_ddx[i];
        }
        new_s = false;
      }
    } else if (tensor_ddout && !tensor_ddx && tensor_ddy) {
      auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
      auto* data_x = tensor_x->data<T>();
      auto* data_ddy = tensor_ddy->data<T>();

      const DDim& dim = tensor_dx->dims();
      size_t N = static_cast<size_t>(product(dim));
398 399
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
      int s = -1;
      bool new_s = false;

      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) {
          ++s;
          new_s = true;
        }
        if (new_s) {
          data_ddout[s] = T(data_x[i].real, -data_x[i].imag) * data_ddy[i];
        } else {
          data_ddout[s] += T(data_x[i].real, -data_x[i].imag) * data_ddy[i];
        }
        new_s = false;
      }
415 416 417 418 419 420
    }
#endif
  }
};

template <typename DeviceContext, typename T>
421
struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
422 423 424 425
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* tensor_x,
                  const DenseTensor* tensor_y,
                  const DenseTensor* tensor_dout,
426 427
                  const paddle::optional<DenseTensor>* tensor_ddx_opt,
                  const paddle::optional<DenseTensor>* tensor_ddy_opt,
428 429 430
                  DenseTensor* tensor_dx,
                  DenseTensor* tensor_dy,
                  DenseTensor* tensor_ddout) {
431 432
    const DenseTensor* tensor_ddx = tensor_ddx_opt->get_ptr();
    const DenseTensor* tensor_ddy = tensor_ddy_opt->get_ptr();
433 434 435
#if defined(__NVCC__) || defined(__HIPCC__)
    if (1 == tensor_dout->dims().size()) {
      auto& dev = *ctx.eigen_device();
436 437
      auto x = EigenVector<T>::Flatten(*tensor_x);
      auto y = EigenVector<T>::Flatten(*tensor_y);
438
      auto dout = EigenVector<T>::Flatten(*tensor_dout);
439
      if (tensor_dx && tensor_ddy) {
440
        ctx.template Alloc<T>(tensor_dx);
441 442 443 444
        auto ddy = EigenVector<T>::Flatten(*tensor_ddy);
        Eigen::DSizes<int, 1> size(tensor_ddy->numel());
        auto dx = EigenVector<T>::Flatten(*tensor_dx);
        dx.device(dev) = ddy * dout.broadcast(size);
445 446 447
      } else if (tensor_dx && !tensor_ddy) {
        FullLikeKernel<T, DeviceContext>(
            ctx, *tensor_x, Scalar(0.0), tensor_x->dtype(), tensor_dx);
448 449
      }

450
      if (tensor_dy && tensor_ddx) {
451
        ctx.template Alloc<T>(tensor_dy);
452 453 454 455
        auto ddx = EigenVector<T>::Flatten(*tensor_ddx);
        Eigen::DSizes<int, 1> size(tensor_ddx->numel());
        auto dy = EigenVector<T>::Flatten(*tensor_dy);
        dy.device(dev) = ddx * dout.broadcast(size);
456 457 458
      } else if (tensor_dy && !tensor_ddx) {
        FullLikeKernel<T, DeviceContext>(
            ctx, *tensor_y, Scalar(0.0), tensor_y->dtype(), tensor_dy);
459 460
      }

461
      if (tensor_ddout && tensor_ddx && tensor_ddy) {
462
        ctx.template Alloc<T>(tensor_ddout);
463 464 465 466
        auto ddx = EigenVector<T>::Flatten(*tensor_ddx);
        auto ddy = EigenVector<T>::Flatten(*tensor_ddy);
        auto ddout = EigenVector<T>::Flatten(*tensor_ddout);
        ddout.device(dev) = (x * ddy + y * ddx).sum();
467 468 469 470 471 472 473 474 475 476
      } else if (tensor_ddout && tensor_ddx && !tensor_ddy) {
        ctx.template Alloc<T>(tensor_ddout);
        auto ddx = EigenVector<T>::Flatten(*tensor_ddx);
        auto ddout = EigenVector<T>::Flatten(*tensor_ddout);
        ddout.device(dev) = (y * ddx).sum();
      } else if (tensor_ddout && !tensor_ddx && tensor_ddy) {
        ctx.template Alloc<T>(tensor_ddout);
        auto ddy = EigenVector<T>::Flatten(*tensor_ddy);
        auto ddout = EigenVector<T>::Flatten(*tensor_ddout);
        ddout.device(dev) = (x * ddy).sum();
477 478 479
      }
    }
#else
480 481 482 483 484 485
    const T* data_x = tensor_x->data<T>();
    const T* data_y = tensor_y->data<T>();
    const T* data_dout = tensor_dout->data<T>();
    const T* data_ddx = tensor_ddx ? tensor_ddx->data<T>() : nullptr;
    const T* data_ddy = tensor_ddy ? tensor_ddy->data<T>() : nullptr;
    if (tensor_dx && tensor_ddy) {
486
      auto* data_dx = ctx.template Alloc<T>(tensor_dx);
487 488
      const DDim& dim = tensor_dx->dims();
      size_t N = static_cast<size_t>(product(dim));
489 490
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
491 492 493 494 495
      int s = -1;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) ++s;
        data_dx[i] = data_dout[s] * data_ddy[i];
      }
496 497 498
    } else if (tensor_dx && !tensor_ddy) {
      FullLikeKernel<T, DeviceContext>(
          ctx, *tensor_x, Scalar(0.0), tensor_x->dtype(), tensor_dx);
499 500
    }

501
    if (tensor_dy && tensor_ddx) {
502
      auto* data_dy = ctx.template Alloc<T>(tensor_dy);
503 504
      const DDim& dim = tensor_dy->dims();
      size_t N = static_cast<size_t>(product(dim));
505 506
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
507 508 509 510 511
      int s = -1;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) ++s;
        data_dy[i] = data_dout[s] * data_ddx[i];
      }
512 513 514
    } else if (tensor_dy) {
      FullLikeKernel<T, DeviceContext>(
          ctx, *tensor_y, Scalar(0.0), tensor_y->dtype(), tensor_dy);
515 516
    }

517
    if (tensor_ddout && tensor_ddx && tensor_ddy) {
518
      auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
519 520
      const DDim& dim = tensor_dy->dims();
      size_t N = static_cast<size_t>(product(dim));
521 522
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
523 524 525 526 527 528 529 530 531 532 533 534 535 536
      int s = -1;
      bool new_s = false;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) {
          ++s;
          new_s = true;
        }
        if (new_s) {
          data_ddout[s] = data_x[i] * data_ddy[i] + data_y[i] * data_ddx[i];
        } else {
          data_ddout[s] += data_x[i] * data_ddy[i] + data_y[i] * data_ddx[i];
        }
        new_s = false;
      }
537 538 539 540
    } else if (tensor_ddout && tensor_ddx && !tensor_ddy) {
      auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
      const DDim& dim = tensor_dy->dims();
      size_t N = static_cast<size_t>(product(dim));
541 542
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
      int s = -1;
      bool new_s = false;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) {
          ++s;
          new_s = true;
        }
        if (new_s) {
          data_ddout[s] = data_y[i] * data_ddx[i];
        } else {
          data_ddout[s] += data_y[i] * data_ddx[i];
        }
        new_s = false;
      }
    } else if (tensor_ddout && !tensor_ddx && tensor_ddy) {
      auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
      const DDim& dim = tensor_dx->dims();
      size_t N = static_cast<size_t>(product(dim));
561 562
      auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
      auto step = _step != 0 ? _step : 1;
563 564 565 566 567 568 569 570 571 572 573 574 575 576
      int s = -1;
      bool new_s = false;
      for (size_t i = 0; i < N; ++i) {
        if (0 == i % step) {
          ++s;
          new_s = true;
        }
        if (new_s) {
          data_ddout[s] = data_x[i] * data_ddy[i];
        } else {
          data_ddout[s] += data_x[i] * data_ddy[i];
        }
        new_s = false;
      }
577 578 579 580 581 582 583 584 585 586 587
    }
#endif
  }
};

template <typename DeviceContext, typename T, typename Enabel = void>
struct DotTripleGradFunction {
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* in_tensor_x,
                  const DenseTensor* in_tensor_y,
                  const DenseTensor* in_tensor_dout,
588 589 590 591 592
                  const paddle::optional<DenseTensor>* in_tensor_ddx_opt,
                  const paddle::optional<DenseTensor>* in_tensor_ddy_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_dx_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_dy_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_ddout_opt,
593 594 595 596 597 598 599 600 601 602
                  DenseTensor* out_tensor_d_x,
                  DenseTensor* out_tensor_d_y,
                  DenseTensor* out_tensor_d_dout,
                  DenseTensor* out_tensor_d_ddx,
                  DenseTensor* out_tensor_d_ddy);
};

// TODO(wuweilong): enable this function when the unittests framewark for multi
// grad is ok (dtype: complex64 or complex128).
template <typename DeviceContext, typename T>
603
struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
604 605 606 607
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* in_tensor_x,
                  const DenseTensor* in_tensor_y,
                  const DenseTensor* in_tensor_dout,
608 609 610 611 612
                  const paddle::optional<DenseTensor>* in_tensor_ddx_opt,
                  const paddle::optional<DenseTensor>* in_tensor_ddy_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_dx_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_dy_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_ddout_opt,
613 614 615 616 617
                  DenseTensor* out_tensor_d_x,
                  DenseTensor* out_tensor_d_y,
                  DenseTensor* out_tensor_d_dout,
                  DenseTensor* out_tensor_d_ddx,
                  DenseTensor* out_tensor_d_ddy) {
618 619 620 621 622
    const DenseTensor* in_tensor_ddx = in_tensor_ddx_opt->get_ptr();
    const DenseTensor* in_tensor_ddy = in_tensor_ddy_opt->get_ptr();
    const DenseTensor* in_tensor_d_dx = in_tensor_d_dx_opt->get_ptr();
    const DenseTensor* in_tensor_d_dy = in_tensor_d_dy_opt->get_ptr();
    const DenseTensor* in_tensor_d_ddout = in_tensor_d_ddout_opt->get_ptr();
623
#if defined(__NVCC__) || defined(__HIPCC__)
624
    if (1 == in_tensor_dout->dims().size()) {
625
      auto& dev = *ctx.eigen_device();
626 627 628 629 630 631 632 633
      DenseTensor in_tensor_x_help = Conj<T, DeviceContext>(ctx, *in_tensor_x);
      DenseTensor in_tensor_y_help = Conj<T, DeviceContext>(ctx, *in_tensor_y);
      DenseTensor in_tensor_dout_help =
          Conj<T, DeviceContext>(ctx, *in_tensor_dout);
      DenseTensor in_tensor_ddx_help;
      DenseTensor in_tensor_ddy_help;
      if (in_tensor_ddx) {
        in_tensor_ddx_help = Conj<T, DeviceContext>(ctx, *in_tensor_ddx);
634
      }
635 636
      if (in_tensor_ddy) {
        in_tensor_ddy_help = Conj<T, DeviceContext>(ctx, *in_tensor_ddy);
637 638
      }

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
      bool d_dout_flag = false;
      bool d_ddx_flag = false;
      bool d_ddy_flag = false;

      if (in_tensor_ddx) {
        if (out_tensor_d_y && in_tensor_d_ddout) {
          ctx.template Alloc<T>(out_tensor_d_y);
          auto ddx = EigenVector<T>::Flatten(in_tensor_ddx_help);
          Eigen::DSizes<int, 1> size(in_tensor_ddx->numel());
          auto d_y = EigenVector<T>::Flatten(*out_tensor_d_y);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          d_y.device(dev) = ddx * d_ddout.broadcast(size);
        }
        if (out_tensor_d_dout && in_tensor_d_dy) {
          ctx.template Alloc<T>(out_tensor_d_dout);
          auto ddx = EigenVector<T>::Flatten(in_tensor_ddx_help);
          auto d_dy = EigenVector<T>::Flatten(*in_tensor_d_dy);
          auto d_dout = EigenVector<T>::Flatten(*out_tensor_d_dout);
          d_dout.device(dev) = (ddx * d_dy).sum();
          d_dout_flag = true;
        }
660 661
      }

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
      if (in_tensor_ddy) {
        if (out_tensor_d_x && in_tensor_d_ddout) {
          ctx.template Alloc<T>(out_tensor_d_x);
          auto ddy = EigenVector<T>::Flatten(in_tensor_ddy_help);
          Eigen::DSizes<int, 1> size(in_tensor_ddy->numel());
          auto d_x = EigenVector<T>::Flatten(*out_tensor_d_x);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          d_x.device(dev) = ddy * d_ddout.broadcast(size);
        }
        if (out_tensor_d_dout && in_tensor_d_dx) {
          ctx.template Alloc<T>(out_tensor_d_dout);
          auto ddy = EigenVector<T>::Flatten(in_tensor_ddy_help);
          auto d_dx = EigenVector<T>::Flatten(*in_tensor_d_dx);
          auto d_dout = EigenVector<T>::Flatten(*out_tensor_d_dout);
          if (d_dout_flag) {
            d_dout.device(dev) += (ddy * d_dx).sum();
          } else {
            d_dout.device(dev) = (ddy * d_dx).sum();
          }
        }
682 683
      }

684 685 686 687 688 689 690 691 692 693
      if (in_tensor_d_dx) {
        if (out_tensor_d_ddy) {
          ctx.template Alloc<T>(out_tensor_d_ddy);
          auto dout = EigenVector<T>::Flatten(in_tensor_dout_help);
          auto d_dx = EigenVector<T>::Flatten(*in_tensor_d_dx);
          auto d_ddy = EigenVector<T>::Flatten(*out_tensor_d_ddy);
          Eigen::DSizes<int, 1> size(in_tensor_x->numel());
          d_ddy.device(dev) = (dout.broadcast(size) * d_dx);
          d_ddy_flag = true;
        }
694 695
      }

696 697 698 699 700 701 702 703 704 705 706
      if (in_tensor_d_dy) {
        if (out_tensor_d_ddx) {
          ctx.template Alloc<T>(out_tensor_d_ddx);
          auto dout = EigenVector<T>::Flatten(in_tensor_dout_help);
          auto d_dy = EigenVector<T>::Flatten(*in_tensor_d_dy);
          auto d_ddx = EigenVector<T>::Flatten(*out_tensor_d_ddx);
          Eigen::DSizes<int, 1> size(in_tensor_y->numel());
          d_ddx.device(dev) = (dout.broadcast(size) * d_dy);
          d_ddx_flag = true;
        }
      }
707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
      if (in_tensor_d_ddout) {
        if (out_tensor_d_ddx) {
          ctx.template Alloc<T>(out_tensor_d_ddx);
          auto y = EigenVector<T>::Flatten(in_tensor_y_help);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          Eigen::DSizes<int, 1> size(in_tensor_y->numel());
          auto d_ddx = EigenVector<T>::Flatten(*out_tensor_d_ddx);
          if (d_ddx_flag) {
            d_ddx.device(dev) += (y * d_ddout.broadcast(size));
          } else {
            d_ddx.device(dev) = (y * d_ddout.broadcast(size));
          }
        }
        if (out_tensor_d_ddy) {
          ctx.template Alloc<T>(out_tensor_d_ddy);
          auto x = EigenVector<T>::Flatten(in_tensor_x_help);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          Eigen::DSizes<int, 1> size(in_tensor_x->numel());
          auto d_ddy = EigenVector<T>::Flatten(*out_tensor_d_ddy);
          if (d_ddy_flag) {
            d_ddy.device(dev) += (x * d_ddout.broadcast(size));
          } else {
            d_ddy.device(dev) = (x * d_ddout.broadcast(size));
          }
        }
      }
      if (out_tensor_d_x && !out_tensor_d_x->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_x,
                                         Scalar(T(0.0, 0.0)),
                                         in_tensor_x->dtype(),
                                         out_tensor_d_x);
      }
      if (out_tensor_d_y && !out_tensor_d_y->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_y,
                                         Scalar(T(0.0, 0.0)),
                                         in_tensor_y->dtype(),
                                         out_tensor_d_y);
      }
      if (out_tensor_d_dout && !out_tensor_d_dout->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_dout,
                                         Scalar(T(0.0, 0.0)),
                                         in_tensor_dout->dtype(),
                                         out_tensor_d_dout);
      }
      if (out_tensor_d_ddx && !out_tensor_d_ddx->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_x,
                                         Scalar(T(0.0, 0.0)),
                                         in_tensor_x->dtype(),
                                         out_tensor_d_ddx);
      }
      if (out_tensor_d_ddy && !out_tensor_d_ddy->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_y,
                                         Scalar(T(0.0, 0.0)),
                                         in_tensor_y->dtype(),
                                         out_tensor_d_ddy);
768 769 770
      }
    }
#else
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    const T* data_x = in_tensor_x->data<T>();
    const T* data_y = in_tensor_y->data<T>();
    const T* data_dout = in_tensor_dout->data<T>();
    const T* data_ddx = in_tensor_ddx ? in_tensor_ddx->data<T>() : nullptr;
    const T* data_ddy = in_tensor_ddy ? in_tensor_ddy->data<T>() : nullptr;
    const T* data_d_dx = in_tensor_d_dx ? in_tensor_d_dx->data<T>() : nullptr;
    const T* data_d_dy = in_tensor_d_dy ? in_tensor_d_dy->data<T>() : nullptr;
    const T* data_d_ddout =
        in_tensor_d_ddout ? in_tensor_d_ddout->data<T>() : nullptr;

    bool d_dout_flag = false;
    bool d_ddx_flag = false;
    bool d_ddy_flag = false;

    if (data_ddx) {
      if (out_tensor_d_y && data_d_ddout) {
        auto* data_d_y = ctx.template Alloc<T>(out_tensor_d_y);
        const DDim& dim = out_tensor_d_y->dims();
        size_t N = static_cast<size_t>(product(dim));
790 791
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
792 793 794 795 796 797 798
        int s = -1;

        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_y[i] =
              T(data_ddx[i].real, -data_ddx[i].imag) * data_d_ddout[s];
        }
799 800
      }

801 802 803 804
      if (out_tensor_d_dout && data_d_dy) {
        auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
        const DDim& dim = in_tensor_x->dims();
        size_t N = static_cast<size_t>(product(dim));
805 806
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
        int s = -1;
        bool new_s = false;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) {
            ++s;
            new_s = true;
          }
          if (new_s) {
            data_d_dout[s] =
                T(data_ddx[i].real, -data_ddx[i].imag) * data_d_dy[i];
          } else {
            data_d_dout[s] +=
                T(data_ddx[i].real, -data_ddx[i].imag) * data_d_dy[i];
          }
          new_s = false;
        }
        d_dout_flag = true;
824 825 826
      }
    }

827 828 829 830 831
    if (data_ddy) {
      if (out_tensor_d_x && data_d_ddout) {
        auto* data_d_x = ctx.template Alloc<T>(out_tensor_d_x);
        const DDim& dim = out_tensor_d_x->dims();
        size_t N = static_cast<size_t>(product(dim));
832 833
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
834 835 836 837 838 839
        int s = -1;

        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_x[i] =
              T(data_ddy[i].real, -data_ddy[i].imag) * data_d_ddout[s];
840
        }
841 842 843 844 845
      }
      if (out_tensor_d_dout && data_d_dx) {
        auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
        const DDim& dim = in_tensor_x->dims();
        size_t N = static_cast<size_t>(product(dim));
846 847
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
848 849 850 851 852 853 854 855 856 857
        int s = -1;
        bool new_s = false;
        if (d_dout_flag) {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) {
              ++s;
            }
            data_d_dout[s] +=
                T(data_ddy[i].real, -data_ddy[i].imag) * data_d_dx[i];
          }
858
        } else {
859 860 861 862 863 864 865 866 867 868 869 870 871 872
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) {
              ++s;
              new_s = true;
            }
            if (new_s) {
              data_d_dout[s] =
                  T(data_ddy[i].real, -data_ddy[i].imag) * data_d_dx[i];
            } else {
              data_d_dout[s] +=
                  T(data_ddy[i].real, -data_ddy[i].imag) * data_d_dx[i];
            }
            new_s = false;
          }
873 874 875 876
        }
      }
    }

877 878 879 880 881
    if (data_d_dx) {
      if (out_tensor_d_ddy) {
        auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
        const DDim& dim = out_tensor_d_ddy->dims();
        size_t N = static_cast<size_t>(product(dim));
882 883
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
884 885 886 887 888 889 890
        int s = -1;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_ddy[i] =
              T(data_dout[s].real, -data_dout[s].imag) * data_d_dx[i];
        }
        d_ddy_flag = true;
891 892 893
      }
    }

894 895 896 897 898
    if (data_d_dy) {
      if (out_tensor_d_ddx) {
        auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
        const DDim& dim = out_tensor_d_ddx->dims();
        size_t N = static_cast<size_t>(product(dim));
899 900
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
901 902 903 904 905 906 907 908 909
        int s = -1;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_ddx[i] =
              T(data_dout[s].real, -data_dout[s].imag) * data_d_dy[i];
        }
      }
      d_ddx_flag = true;
    }
910

911 912 913 914 915
    if (data_d_ddout) {
      if (out_tensor_d_ddx) {
        auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
        const DDim& dim = out_tensor_d_ddx->dims();
        size_t N = static_cast<size_t>(product(dim));
916 917
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
        int s = -1;
        if (d_ddx_flag) {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddx[i] +=
                T(data_y[i].real, -data_y[i].imag) * data_d_ddout[s];
          }
        } else {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddx[i] =
                T(data_y[i].real, -data_y[i].imag) * data_d_ddout[s];
          }
        }
      }
      if (out_tensor_d_ddy) {
        auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
        const DDim& dim = out_tensor_d_ddy->dims();
        size_t N = static_cast<size_t>(product(dim));
937 938
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
939 940 941 942 943 944 945 946 947 948 949 950 951 952
        int s = -1;
        if (d_ddy_flag) {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddy[i] +=
                T(data_x[i].real, -data_x[i].imag) * data_d_ddout[s];
          }
        } else {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddy[i] =
                T(data_x[i].real, -data_x[i].imag) * data_d_ddout[s];
          }
        }
953 954
      }
    }
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991

    if (out_tensor_d_x && !out_tensor_d_x->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_x,
                                       Scalar(T(0.0, 0.0)),
                                       in_tensor_x->dtype(),
                                       out_tensor_d_x);
    }
    if (out_tensor_d_y && !out_tensor_d_y->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_y,
                                       Scalar(T(0.0, 0.0)),
                                       in_tensor_y->dtype(),
                                       out_tensor_d_y);
    }
    if (out_tensor_d_dout && !out_tensor_d_dout->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_dout,
                                       Scalar(T(0.0, 0.0)),
                                       in_tensor_dout->dtype(),
                                       out_tensor_d_dout);
    }
    if (out_tensor_d_ddx && !out_tensor_d_ddx->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_x,
                                       Scalar(T(0.0, 0.0)),
                                       in_tensor_x->dtype(),
                                       out_tensor_d_ddx);
    }
    if (out_tensor_d_ddy && !out_tensor_d_ddy->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_y,
                                       Scalar(T(0.0, 0.0)),
                                       in_tensor_y->dtype(),
                                       out_tensor_d_ddy);
    }

992 993 994 995 996
#endif
  }
};

template <typename DeviceContext, typename T>
997
struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
998 999 1000 1001
  void operator()(const DeviceContext& ctx,
                  const DenseTensor* in_tensor_x,
                  const DenseTensor* in_tensor_y,
                  const DenseTensor* in_tensor_dout,
1002 1003 1004 1005 1006
                  const paddle::optional<DenseTensor>* in_tensor_ddx_opt,
                  const paddle::optional<DenseTensor>* in_tensor_ddy_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_dx_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_dy_opt,
                  const paddle::optional<DenseTensor>* in_tensor_d_ddout_opt,
1007 1008 1009 1010 1011
                  DenseTensor* out_tensor_d_x,
                  DenseTensor* out_tensor_d_y,
                  DenseTensor* out_tensor_d_dout,
                  DenseTensor* out_tensor_d_ddx,
                  DenseTensor* out_tensor_d_ddy) {
1012 1013 1014 1015 1016
    const DenseTensor* in_tensor_ddx = in_tensor_ddx_opt->get_ptr();
    const DenseTensor* in_tensor_ddy = in_tensor_ddy_opt->get_ptr();
    const DenseTensor* in_tensor_d_dx = in_tensor_d_dx_opt->get_ptr();
    const DenseTensor* in_tensor_d_dy = in_tensor_d_dy_opt->get_ptr();
    const DenseTensor* in_tensor_d_ddout = in_tensor_d_ddout_opt->get_ptr();
1017
#if defined(__NVCC__) || defined(__HIPCC__)
1018
    if (1 == in_tensor_dout->dims().size()) {
1019
      auto& dev = *ctx.eigen_device();
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
      bool d_dout_flag = false;
      bool d_ddx_flag = false;
      bool d_ddy_flag = false;

      if (in_tensor_ddx) {
        if (out_tensor_d_y && in_tensor_d_ddout) {
          ctx.template Alloc<T>(out_tensor_d_y);
          auto ddx = EigenVector<T>::Flatten(*in_tensor_ddx);
          Eigen::DSizes<int, 1> size(in_tensor_ddx->numel());
          auto d_y = EigenVector<T>::Flatten(*out_tensor_d_y);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          d_y.device(dev) = ddx * d_ddout.broadcast(size);
        }
        if (out_tensor_d_dout && in_tensor_d_dy) {
          ctx.template Alloc<T>(out_tensor_d_dout);
          auto ddx = EigenVector<T>::Flatten(*in_tensor_ddx);
          auto d_dy = EigenVector<T>::Flatten(*in_tensor_d_dy);
          auto d_dout = EigenVector<T>::Flatten(*out_tensor_d_dout);
          d_dout.device(dev) = (ddx * d_dy).sum();
          d_dout_flag = true;
        }
1041 1042
      }

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
      if (in_tensor_ddy) {
        if (out_tensor_d_x && in_tensor_d_ddout) {
          ctx.template Alloc<T>(out_tensor_d_x);
          auto ddy = EigenVector<T>::Flatten(*in_tensor_ddy);
          Eigen::DSizes<int, 1> size(in_tensor_ddy->numel());
          auto d_x = EigenVector<T>::Flatten(*out_tensor_d_x);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          d_x.device(dev) = ddy * d_ddout.broadcast(size);
        }
        if (out_tensor_d_dout && in_tensor_d_dx) {
          ctx.template Alloc<T>(out_tensor_d_dout);
          auto ddy = EigenVector<T>::Flatten(*in_tensor_ddy);
          auto d_dx = EigenVector<T>::Flatten(*in_tensor_d_dx);
          auto d_dout = EigenVector<T>::Flatten(*out_tensor_d_dout);
          if (d_dout_flag) {
            d_dout.device(dev) += (ddy * d_dx).sum();
          } else {
            d_dout.device(dev) = (ddy * d_dx).sum();
          }
        }
1063 1064
      }

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
      if (in_tensor_d_dx) {
        if (out_tensor_d_ddy) {
          ctx.template Alloc<T>(out_tensor_d_ddy);
          auto dout = EigenVector<T>::Flatten(*in_tensor_dout);
          auto d_dx = EigenVector<T>::Flatten(*in_tensor_d_dx);
          auto d_ddy = EigenVector<T>::Flatten(*out_tensor_d_ddy);
          Eigen::DSizes<int, 1> size(in_tensor_x->numel());
          d_ddy.device(dev) = (dout.broadcast(size) * d_dx);
          d_ddy_flag = true;
        }
1075 1076
      }

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
      if (in_tensor_d_dy) {
        if (out_tensor_d_ddx) {
          ctx.template Alloc<T>(out_tensor_d_ddx);
          auto dout = EigenVector<T>::Flatten(*in_tensor_dout);
          auto d_dy = EigenVector<T>::Flatten(*in_tensor_d_dy);
          auto d_ddx = EigenVector<T>::Flatten(*out_tensor_d_ddx);
          Eigen::DSizes<int, 1> size(in_tensor_y->numel());
          d_ddx.device(dev) = (dout.broadcast(size) * d_dy);
          d_ddx_flag = true;
        }
1087 1088
      }

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
      if (in_tensor_d_ddout) {
        if (out_tensor_d_ddx) {
          ctx.template Alloc<T>(out_tensor_d_ddx);
          auto y = EigenVector<T>::Flatten(*in_tensor_y);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          Eigen::DSizes<int, 1> size(in_tensor_y->numel());
          auto d_ddx = EigenVector<T>::Flatten(*out_tensor_d_ddx);
          if (d_ddx_flag) {
            d_ddx.device(dev) += (y * d_ddout.broadcast(size));
          } else {
            d_ddx.device(dev) = (y * d_ddout.broadcast(size));
          }
        }
        if (out_tensor_d_ddy) {
          ctx.template Alloc<T>(out_tensor_d_ddy);
          auto x = EigenVector<T>::Flatten(*in_tensor_x);
          auto d_ddout = EigenVector<T>::Flatten(*in_tensor_d_ddout);
          Eigen::DSizes<int, 1> size(in_tensor_x->numel());
          auto d_ddy = EigenVector<T>::Flatten(*out_tensor_d_ddy);
          if (d_ddy_flag) {
            d_ddy.device(dev) += (x * d_ddout.broadcast(size));
          } else {
            d_ddy.device(dev) = (x * d_ddout.broadcast(size));
          }
        }
      }
      if (out_tensor_d_x && !out_tensor_d_x->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_x,
                                         Scalar(0.0),
                                         in_tensor_x->dtype(),
                                         out_tensor_d_x);
      }
      if (out_tensor_d_y && !out_tensor_d_y->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_y,
                                         Scalar(0.0),
                                         in_tensor_y->dtype(),
                                         out_tensor_d_y);
      }
      if (out_tensor_d_dout && !out_tensor_d_dout->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_dout,
                                         Scalar(0.0),
                                         in_tensor_dout->dtype(),
                                         out_tensor_d_dout);
      }
      if (out_tensor_d_ddx && !out_tensor_d_ddx->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_x,
                                         Scalar(0.0),
                                         in_tensor_x->dtype(),
                                         out_tensor_d_ddx);
      }
      if (out_tensor_d_ddy && !out_tensor_d_ddy->IsInitialized()) {
        FullLikeKernel<T, DeviceContext>(ctx,
                                         *in_tensor_y,
                                         Scalar(0.0),
                                         in_tensor_y->dtype(),
                                         out_tensor_d_ddy);
1149 1150 1151
      }
    }
#else
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    const T* data_x = in_tensor_x->data<T>();
    const T* data_y = in_tensor_y->data<T>();
    const T* data_dout = in_tensor_dout->data<T>();
    const T* data_ddx = in_tensor_ddx ? in_tensor_ddx->data<T>() : nullptr;
    const T* data_ddy = in_tensor_ddy ? in_tensor_ddy->data<T>() : nullptr;
    const T* data_d_dx = in_tensor_d_dx ? in_tensor_d_dx->data<T>() : nullptr;
    const T* data_d_dy = in_tensor_d_dy ? in_tensor_d_dy->data<T>() : nullptr;
    const T* data_d_ddout =
        in_tensor_d_ddout ? in_tensor_d_ddout->data<T>() : nullptr;

    bool d_dout_flag = false;
    bool d_ddx_flag = false;
    bool d_ddy_flag = false;

    if (data_ddx) {
      if (out_tensor_d_y && data_d_ddout) {
        auto* data_d_y = ctx.template Alloc<T>(out_tensor_d_y);
        const DDim& dim = out_tensor_d_y->dims();
        size_t N = static_cast<size_t>(product(dim));
1171 1172
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1173 1174 1175 1176 1177
        int s = -1;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_y[i] = data_ddx[i] * data_d_ddout[s];
        }
1178
      }
1179 1180 1181 1182
      if (out_tensor_d_dout && data_d_dy) {
        auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
        const DDim& dim = in_tensor_x->dims();
        size_t N = static_cast<size_t>(product(dim));
1183 1184
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
        int s = -1;
        bool new_s = false;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) {
            ++s;
            new_s = true;
          }
          if (new_s) {
            data_d_dout[s] = data_ddx[i] * data_d_dy[i];
          } else {
            data_d_dout[s] += data_ddx[i] * data_d_dy[i];
          }
          new_s = false;
        }
        d_dout_flag = true;
1200 1201 1202
      }
    }

1203 1204 1205 1206 1207
    if (data_ddy) {
      if (out_tensor_d_x && data_d_ddout) {
        auto* data_d_x = ctx.template Alloc<T>(out_tensor_d_x);
        const DDim& dim = out_tensor_d_x->dims();
        size_t N = static_cast<size_t>(product(dim));
1208 1209
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1210 1211 1212 1213
        int s = -1;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_x[i] = data_ddy[i] * data_d_ddout[s];
1214
        }
1215 1216 1217 1218 1219
      }
      if (out_tensor_d_dout && data_d_dx) {
        auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
        const DDim& dim = in_tensor_x->dims();
        size_t N = static_cast<size_t>(product(dim));
1220 1221
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1222 1223 1224 1225 1226 1227 1228 1229 1230
        int s = -1;
        bool new_s = false;
        if (d_dout_flag) {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) {
              ++s;
            }
            data_d_dout[s] += data_ddy[i] * data_d_dx[i];
          }
1231
        } else {
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) {
              ++s;
              new_s = true;
            }
            if (new_s) {
              data_d_dout[s] = data_ddy[i] * data_d_dx[i];
            } else {
              data_d_dout[s] += data_ddy[i] * data_d_dx[i];
            }
            new_s = false;
          }
1244 1245 1246 1247
        }
      }
    }

1248 1249 1250 1251 1252
    if (data_d_dx) {
      if (out_tensor_d_ddy) {
        auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
        const DDim& dim = out_tensor_d_ddy->dims();
        size_t N = static_cast<size_t>(product(dim));
1253 1254
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1255 1256 1257 1258 1259 1260
        int s = -1;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_ddy[i] = data_dout[s] * data_d_dx[i];
        }
        d_ddy_flag = true;
1261 1262 1263
      }
    }

1264 1265 1266 1267 1268
    if (data_d_dy) {
      if (out_tensor_d_ddx) {
        auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
        const DDim& dim = out_tensor_d_ddx->dims();
        size_t N = static_cast<size_t>(product(dim));
1269 1270
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1271 1272 1273 1274 1275 1276 1277 1278
        int s = -1;
        for (size_t i = 0; i < N; ++i) {
          if (0 == i % step) ++s;
          data_d_ddx[i] = data_dout[s] * data_d_dy[i];
        }
      }
      d_ddx_flag = true;
    }
1279

1280 1281 1282 1283 1284
    if (data_d_ddout) {
      if (out_tensor_d_ddx) {
        auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
        const DDim& dim = out_tensor_d_ddx->dims();
        size_t N = static_cast<size_t>(product(dim));
1285 1286
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
        int s = -1;
        if (d_ddx_flag) {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddx[i] += data_y[i] * data_d_ddout[s];
          }
        } else {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddx[i] = data_y[i] * data_d_ddout[s];
          }
        }
1299
      }
1300 1301 1302 1303
      if (out_tensor_d_ddy) {
        auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
        const DDim& dim = out_tensor_d_ddy->dims();
        size_t N = static_cast<size_t>(product(dim));
1304 1305
        auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
        auto step = _step != 0 ? _step : 1;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
        int s = -1;
        if (d_ddy_flag) {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddy[i] += data_x[i] * data_d_ddout[s];
          }
        } else {
          for (size_t i = 0; i < N; ++i) {
            if (0 == i % step) ++s;
            data_d_ddy[i] = data_x[i] * data_d_ddout[s];
          }
        }
      }
    }

    if (out_tensor_d_x && !out_tensor_d_x->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(
          ctx, *in_tensor_x, Scalar(0.0), in_tensor_x->dtype(), out_tensor_d_x);
1324
    }
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
    if (out_tensor_d_y && !out_tensor_d_y->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(
          ctx, *in_tensor_y, Scalar(0.0), in_tensor_y->dtype(), out_tensor_d_y);
    }
    if (out_tensor_d_dout && !out_tensor_d_dout->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_dout,
                                       Scalar(0.0),
                                       in_tensor_dout->dtype(),
                                       out_tensor_d_dout);
    }
    if (out_tensor_d_ddx && !out_tensor_d_ddx->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_x,
                                       Scalar(0.0),
                                       in_tensor_x->dtype(),
                                       out_tensor_d_ddx);
    }
    if (out_tensor_d_ddy && !out_tensor_d_ddy->IsInitialized()) {
      FullLikeKernel<T, DeviceContext>(ctx,
                                       *in_tensor_y,
                                       Scalar(0.0),
                                       in_tensor_y->dtype(),
                                       out_tensor_d_ddy);
    }

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
#endif
  }
};

template <typename T, typename Context>
void DotGradKernel(const Context& dev_ctx,
                   const DenseTensor& x,
                   const DenseTensor& y,
                   const DenseTensor& dout,
                   DenseTensor* dx,
                   DenseTensor* dy) {
  if (dx) {
1363
    dev_ctx.template Alloc<T>(dx);
1364 1365
  }
  if (dy) {
1366
    dev_ctx.template Alloc<T>(dy);
1367 1368 1369 1370 1371 1372 1373 1374 1375
  }
  DotGradFunction<Context, T>()(dev_ctx, &x, &y, &dout, dx, dy);
}

template <typename T, typename Context>
void DotDoubleGradKernel(const Context& dev_ctx,
                         const DenseTensor& x,
                         const DenseTensor& y,
                         const DenseTensor& dout,
1376 1377
                         const paddle::optional<DenseTensor>& ddx,
                         const paddle::optional<DenseTensor>& ddy,
1378 1379 1380 1381
                         DenseTensor* dx,
                         DenseTensor* dy,
                         DenseTensor* ddout) {
  DotDoubleGradFunction<Context, T>()(
1382
      dev_ctx, &x, &y, &dout, ddx.get_ptr(), ddy.get_ptr(), dx, dy, ddout);
1383 1384 1385 1386 1387 1388 1389
}

template <typename T, typename Context>
void DotTripleGradKernel(const Context& dev_ctx,
                         const DenseTensor& x,
                         const DenseTensor& y,
                         const DenseTensor& dout,
1390 1391 1392 1393 1394
                         const paddle::optional<DenseTensor>& ddx,
                         const paddle::optional<DenseTensor>& ddy,
                         const paddle::optional<DenseTensor>& d_dx,
                         const paddle::optional<DenseTensor>& d_dy,
                         const paddle::optional<DenseTensor>& d_ddout,
1395 1396 1397 1398 1399 1400 1401 1402
                         DenseTensor* d_x,
                         DenseTensor* d_y,
                         DenseTensor* d_ddx,
                         DenseTensor* d_ddy,
                         DenseTensor* d_dout) {
  DotTripleGradFunction<Context, T>()(dev_ctx,
                                      &x,
                                      &y,
1403 1404 1405 1406 1407 1408
                                      &dout,
                                      ddx.get_ptr(),
                                      ddy.get_ptr(),
                                      d_dx.get_ptr(),
                                      d_dy.get_ptr(),
                                      d_ddout.get_ptr(),
1409 1410 1411 1412 1413 1414 1415
                                      d_x,
                                      d_y,
                                      d_dout,
                                      d_ddx,
                                      d_ddy);
}

1416
}  // namespace phi