solve_op.h 24.0 KB
Newer Older
W
Weilong Wu 已提交
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
/* Copyright (c) 2021 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
#include "Eigen/Core"
#include "Eigen/LU"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/eigen/eigen_function.h"
#include "paddle/fluid/operators/math/matrix_solve.h"
#include "paddle/fluid/operators/reduce_ops/reduce_sum_op.h"
#include "paddle/fluid/operators/squeeze_op.h"
26 27
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/math_function.h"
W
Weilong Wu 已提交
28
#if defined(__NVCC__) || defined(__HIPCC__)
29
#include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h"
W
Weilong Wu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
#endif

#define MAX_RANK_SUPPORTED 6

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using framework::To32BitIndex;

constexpr int kMULMKLDNNINT8 = 1;

template <typename DeviceContext, typename T>
43 44 45 46
void ReduceSumForSolve(const Tensor* input,
                       Tensor* output,
                       const std::vector<int>& reduce_dims,
                       bool keep_dim,
47
                       const paddle::framework::ExecutionContext& ctx) {
W
Weilong Wu 已提交
48 49
#if defined(__NVCC__) || defined(__HIPCC__)
  auto stream = ctx.cuda_device_context().stream();
50
  TensorReduceImpl<T, T, kps::AddFunctor, kps::IdentityFunctor<T>>(
51 52 53 54 55 56
      ctx.cuda_device_context(),
      *input,
      output,
      kps::IdentityFunctor<T>(),
      reduce_dims,
      stream);
W
Weilong Wu 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
#else
  ReduceKernelFunctor<DeviceContext, T, ops::SumFunctor>(
      input, output, reduce_dims, keep_dim, false, ctx)
      .template apply<T>();
#endif
}

// check the input other is vector_case or not
static inline bool is_vector_rhs(const Tensor& input, const Tensor& other) {
  auto x_dim = input.dims();
  auto y_dim = other.dims();
  auto x_dim_size = x_dim.size();
  auto y_dim_size = y_dim.size();
70 71
  std::vector<int64_t> x_dims_vec = phi::vectorize(x_dim);
  std::vector<int64_t> y_dims_vec = phi::vectorize(y_dim);
W
Weilong Wu 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  std::vector<int64_t>::const_iterator f = x_dims_vec.begin();
  std::vector<int64_t>::const_iterator l = x_dims_vec.end() - 1;
  std::vector<int64_t> x_dims_vec_cut(f, l);  // input.shape[:-1]

  std::vector<int64_t> expected_batched_rhs_shape(x_dims_vec_cut);
  bool vector_case =
      y_dim_size == 1 || (x_dim_size - 1 == y_dim_size &&
                          y_dims_vec == (expected_batched_rhs_shape));

  return vector_case;
}

// unsqueeze operation helper
static framework::DDim GetOutputShapeUnsqueeze(
    const std::vector<int> unsqz_dims, const framework::DDim& in_dims) {
  int output_size = in_dims.size() + static_cast<int>(unsqz_dims.size());
  int cur_output_size = in_dims.size();
  std::vector<int64_t> output_shape(output_size, 0);

  // Validity Check: rank range.
93 94
  PADDLE_ENFORCE_LE(output_size,
                    6,
W
Weilong Wu 已提交
95 96 97 98 99 100 101
                    platform::errors::InvalidArgument(
                        "The output "
                        "tensor's rank should be less than 6."));

  for (int axis : unsqz_dims) {
    int cur = axis < 0 ? axis + cur_output_size + 1 : axis;
    // Vaildity Check: the axis bound
102
    PADDLE_ENFORCE_GE(
103 104
        cur,
        0,
105 106
        platform::errors::InvalidArgument("The insert dimension value should "
                                          "not be less than 0"));
107 108
    PADDLE_ENFORCE_LE(cur,
                      cur_output_size,
W
Weilong Wu 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                      platform::errors::InvalidArgument(
                          "The insert dimension value shoule not be larger "
                          "than the dimension size of input tensor"));
    // Move old axis, and insert new axis
    for (int i = cur_output_size; i >= cur; --i) {
      if (output_shape[i] == 1) {
        // Move axis
        output_shape[i + 1] = 1;
        output_shape[i] = 0;
      }
    }
    output_shape[cur] = 1;
    // Add the output size.
    cur_output_size++;
  }

  // Make output shape
  for (int in_idx = 0, out_idx = 0; out_idx < output_size; ++out_idx) {
    if (output_shape[out_idx] == 0) {
      output_shape[out_idx] = in_dims[in_idx++];
    }
  }

132
  return phi::make_ddim(output_shape);
W
Weilong Wu 已提交
133 134 135 136
}

// operation like squeeze(-1)
static void to_squeeze(const framework::ExecutionContext& context,
137 138
                       const framework::Tensor& in,
                       framework::Tensor* out) {
W
Weilong Wu 已提交
139 140 141 142 143
  auto x_dims = in.dims();
  std::vector<int> sqz_dims = {-1};
  auto out_dims = GetOutputShape(sqz_dims, x_dims, true);
  out->mutable_data(context.GetPlace(), in.type());
  framework::TensorCopy(
144 145 146 147
      in,
      context.GetPlace(),
      context.template device_context<platform::DeviceContext>(),
      out);
W
Weilong Wu 已提交
148 149 150 151 152
  out->Resize(out_dims);
}

// vector_case, need to operate like unsqueeze(-1)
static void to_unsqueeze(const framework::ExecutionContext& context,
153 154
                         const framework::Tensor& in,
                         framework::Tensor* out) {
W
Weilong Wu 已提交
155 156 157 158 159
  auto x_dims = in.dims();
  std::vector<int> unsqz_dims = {-1};
  framework::DDim out_dims = out->dims();
  out_dims = GetOutputShapeUnsqueeze(unsqz_dims, x_dims);
  framework::TensorCopy(
160 161 162 163
      in,
      context.GetPlace(),
      context.template device_context<platform::DeviceContext>(),
      out);
W
Weilong Wu 已提交
164 165 166
  out->Resize(out_dims);
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
// Prepared for the broadcast operation
static std::vector<int64_t> get_broadcast_batch_portion(
    std::vector<int64_t> x, std::vector<int64_t> y) {
  size_t size_x = x.size();
  size_t size_y = y.size();
  size_t size = std::max(size_x, size_y);
  std::vector<int64_t> batchPortion(size);

  ptrdiff_t i = (ptrdiff_t)size - 1;
  for (; i >= 0; --i) {
    ptrdiff_t offset = size - i - 1;
    ptrdiff_t dim_x = size_x - offset - 1;
    ptrdiff_t dim_y = size_y - offset - 1;
    int64_t x_size = (dim_x >= 0) ? x[dim_x] : 1;
    int64_t y_size = (dim_y >= 0) ? y[dim_y] : 1;
W
Weilong Wu 已提交
182 183

    PADDLE_ENFORCE_EQ(
184 185
        (x_size == y_size || x_size == 1 || y_size == 1),
        true,
W
Weilong Wu 已提交
186
        platform::errors::PreconditionNotMet(
187
            "The size of tensor x (%d) must match the size of tensor y "
W
Weilong Wu 已提交
188
            "(%d) at non-singleton dimension %d.",
189 190 191
            x_size,
            y_size,
            i));
W
Weilong Wu 已提交
192

193
    batchPortion[i] = x_size != 1 ? x_size : y_size;
W
Weilong Wu 已提交
194
  }
195
  return batchPortion;
W
Weilong Wu 已提交
196 197
}

198
// broadcast the batch dimensions of tensor x and tensor y.
W
Weilong Wu 已提交
199
static inline std::tuple<std::vector<int64_t>, std::vector<int64_t>>
200
get_broadcast_dims(const Tensor& x, const Tensor& y) {
201 202
  std::vector<int64_t> x_dims_vec = phi::vectorize(x.dims());
  std::vector<int64_t> y_dims_vec = phi::vectorize(y.dims());
W
Weilong Wu 已提交
203

204 205 206
  std::vector<int64_t>::const_iterator f1 = x_dims_vec.begin();
  std::vector<int64_t>::const_iterator l1 = x_dims_vec.end() - 2;
  std::vector<int64_t> x_dims_vec_cut(f1, l1);
W
Weilong Wu 已提交
207

208 209 210
  std::vector<int64_t>::const_iterator f2 = y_dims_vec.begin();
  std::vector<int64_t>::const_iterator l2 = y_dims_vec.end() - 2;
  std::vector<int64_t> y_dims_vec_cut(f2, l2);
W
Weilong Wu 已提交
211 212

  std::vector<int64_t> expand_batch_portion =
213
      get_broadcast_batch_portion(x_dims_vec_cut, y_dims_vec_cut);
W
Weilong Wu 已提交
214

215 216 217 218
  std::vector<int64_t> x_expand_size({expand_batch_portion});
  x_expand_size.insert(x_expand_size.end(),
                       {x_dims_vec[static_cast<int>(x_dims_vec.size()) - 2],
                        x_dims_vec[static_cast<int>(x_dims_vec.size()) - 1]});
W
Weilong Wu 已提交
219

220 221 222 223
  std::vector<int64_t> y_expand_size({expand_batch_portion});
  y_expand_size.insert(y_expand_size.end(),
                       {y_dims_vec[static_cast<int>(y_dims_vec.size()) - 2],
                        y_dims_vec[static_cast<int>(y_dims_vec.size()) - 1]});
W
Weilong Wu 已提交
224

225
  return std::make_tuple(x_expand_size, y_expand_size);
W
Weilong Wu 已提交
226 227 228
}

template <int Rank, typename T, typename DeviceContext>
229 230 231
void expand_impl(const DeviceContext& context,
                 const Tensor& in,
                 Tensor* out,
232
                 const std::vector<int64_t>& expand_shape) {
233
  auto vec_in_dims = phi::vectorize<int>(in.dims());
W
Weilong Wu 已提交
234 235 236
  auto diff = expand_shape.size() - vec_in_dims.size();
  vec_in_dims.insert(vec_in_dims.begin(), diff, 1);
  std::vector<int> repeat_times(vec_in_dims.size());
237

W
Weilong Wu 已提交
238 239
  for (size_t i = 0; i < vec_in_dims.size(); ++i) {
    PADDLE_ENFORCE_NE(
240 241
        expand_shape[i],
        0,
W
Weilong Wu 已提交
242 243 244
        platform::errors::InvalidArgument("The expanded size cannot be zero."));
    if (i < diff) {
      PADDLE_ENFORCE_GT(
245 246
          expand_shape[i],
          0,
W
Weilong Wu 已提交
247 248 249 250 251 252 253 254
          platform::errors::InvalidArgument(
              "The expanded size (%d) for non-existing dimensions must be "
              "positive for expand operation.",
              expand_shape[i]));
      repeat_times[i] = expand_shape[i];
    } else if (expand_shape[i] > 0) {
      if (vec_in_dims[i] != 1) {
        PADDLE_ENFORCE_EQ(
255 256
            vec_in_dims[i],
            expand_shape[i],
W
Weilong Wu 已提交
257 258 259
            platform::errors::InvalidArgument(
                "The value (%d) of the non-singleton dimension does not match"
                " the corresponding value (%d) in shape for expand operation.",
260 261
                vec_in_dims[i],
                expand_shape[i]));
W
Weilong Wu 已提交
262 263 264 265 266 267
        repeat_times[i] = 1;
      } else {
        repeat_times[i] = expand_shape[i];
      }
    } else {
      PADDLE_ENFORCE_EQ(
268 269
          expand_shape[i],
          -1,
W
Weilong Wu 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282
          platform::errors::InvalidArgument(
              "When the value in shape is negative for expand_v2 op, "
              "only -1 is supported, but the value received is %d.",
              expand_shape[i]));
      repeat_times[i] = 1;
    }
  }

  Eigen::DSizes<Eigen::DenseIndex, Rank> bcast_dims;
  for (size_t i = 0; i < repeat_times.size(); ++i) {
    bcast_dims[i] = repeat_times[i];
  }

283
  framework::DDim new_in_dims = phi::make_ddim(vec_in_dims);
W
Weilong Wu 已提交
284 285 286 287 288
  framework::DDim out_dims(new_in_dims);
  for (size_t i = 0; i < repeat_times.size(); ++i) {
    out_dims[i] *= repeat_times[i];
  }

289 290 291 292 293
  out->Resize(out_dims);
  out->mutable_data<T>(context.GetPlace());
  auto x = EigenTensor<T, Rank>::From(in, new_in_dims);
  auto y = EigenTensor<T, Rank>::From(*out, out_dims);
  auto& place = *context.eigen_device();
W
Weilong Wu 已提交
294 295 296 297 298 299
  // use 32-bit index to speed up
  bool use_32bit_index = y.size() < Eigen::NumTraits<int>::highest();
  if (use_32bit_index) {
    EigenBroadcast<std::decay_t<decltype(place)>, T, Rank>::Eval(
        place, To32BitIndex(y), To32BitIndex(x), bcast_dims);
  } else {
300 301
    EigenBroadcast<std::decay_t<decltype(place)>, T, Rank>::Eval(
        place, y, x, bcast_dims);
W
Weilong Wu 已提交
302 303 304
  }
}

305
template <typename T, typename DeviceContext>
306 307 308
void TensorExpand(const DeviceContext& context,
                  const Tensor& in,
                  Tensor* out,
309 310
                  const std::vector<int64_t>& expand_shape) {
  // necessary check before expand operation
311 312
  PADDLE_ENFORCE_GE(expand_shape.size(),
                    in.dims().size(),
313 314 315
                    platform::errors::InvalidArgument(
                        "The size of 'expand_shape' (%d) should >= the input "
                        "Tensor's rank (%d).",
316 317 318 319
                        expand_shape.size(),
                        in.dims().size()));
  PADDLE_ENFORCE_LE(expand_shape.size(),
                    MAX_RANK_SUPPORTED,
320 321
                    platform::errors::InvalidArgument(
                        "The size of 'expand_shape' (%d) should be <= %d",
322 323
                        expand_shape.size(),
                        MAX_RANK_SUPPORTED));
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  switch (expand_shape.size()) {
    case 1:
      expand_impl<1, T, DeviceContext>(context, in, out, expand_shape);
      break;
    case 2:
      expand_impl<2, T, DeviceContext>(context, in, out, expand_shape);
      break;
    case 3:
      expand_impl<3, T, DeviceContext>(context, in, out, expand_shape);
      break;
    case 4:
      expand_impl<4, T, DeviceContext>(context, in, out, expand_shape);
      break;
    case 5:
      expand_impl<5, T, DeviceContext>(context, in, out, expand_shape);
      break;
    case 6:
      expand_impl<6, T, DeviceContext>(context, in, out, expand_shape);
      break;
  }
}

W
Weilong Wu 已提交
346 347
template <typename DeviceContext, typename T>
static void linalg_solve(const framework::ExecutionContext& context,
348 349
                         const framework::Tensor* x,
                         const framework::Tensor* y,
W
Weilong Wu 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362
                         framework::Tensor* out) {
  out->mutable_data<T>(context.GetPlace());

  auto& dev_ctx = context.template device_context<DeviceContext>();
  math::MatrixSolveFunctor<DeviceContext, T> mat_solve;

  // input y can be vector or matrix
  // but need to be unsqueezed if y is a vector
  bool is_vector = false;
  is_vector = is_vector_rhs(*x, *y);

  Tensor tmp_y;
  if (is_vector) {
363
    tmp_y.mutable_data(context.GetPlace(), y->dtype());
W
Weilong Wu 已提交
364 365 366
    to_unsqueeze(context, *y, &tmp_y);
  } else {
    tmp_y.Resize(y->dims());
367
    tmp_y.mutable_data(context.GetPlace(), y->dtype());
W
Weilong Wu 已提交
368
    framework::TensorCopy(
369 370 371 372
        *y,
        context.GetPlace(),
        context.template device_context<platform::DeviceContext>(),
        &tmp_y);
W
Weilong Wu 已提交
373 374 375 376
  }

  Tensor tmp_x;
  tmp_x.Resize(x->dims());
377
  tmp_x.mutable_data(context.GetPlace(), x->dtype());
W
Weilong Wu 已提交
378
  framework::TensorCopy(
379 380 381 382
      *x,
      context.GetPlace(),
      context.template device_context<platform::DeviceContext>(),
      &tmp_x);
W
Weilong Wu 已提交
383 384 385 386

  std::vector<int64_t> x_broadcast_dims;
  std::vector<int64_t> y_broadcast_dims;
  std::tie(x_broadcast_dims, y_broadcast_dims) =
387
      get_broadcast_dims(tmp_x, tmp_y);
W
Weilong Wu 已提交
388 389

  Tensor tmp_x_bc;
390
  TensorExpand<T, DeviceContext>(dev_ctx, tmp_x, &tmp_x_bc, x_broadcast_dims);
W
Weilong Wu 已提交
391

392 393
  Tensor tmp_y_bc;
  TensorExpand<T, DeviceContext>(dev_ctx, tmp_y, &tmp_y_bc, y_broadcast_dims);
W
Weilong Wu 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

  auto x_dim = x->dims();
  auto y_dim = y->dims();
  auto x_dim_size = x_dim.size();
  auto y_dim_size = y_dim.size();

  if (is_vector) {                 // vector case
    out->Resize(tmp_y_bc.dims());  // out.unsqueeze(-1)
    mat_solve(dev_ctx, tmp_x_bc, tmp_y_bc, out);

    Tensor out_tmp;
    out_tmp.Resize(out->dims());
    out_tmp = *out;
    to_squeeze(context, out_tmp, out);  // out.squeeze(-1)
  } else {
    PADDLE_ENFORCE_EQ(
410 411
        x_dim[x_dim_size - 1],
        y_dim[y_dim_size - 2],
W
Weilong Wu 已提交
412 413 414 415 416 417
        platform::errors::InvalidArgument(
            "Matrix X1 with dimension greater than 2 and any matrix Y1,"
            "the matrix X1's width must be equal with matrix Y1's "
            "height. But received X's shape = [%s], X1's shape = [%s], X1's "
            "width = %s; Y's shape = [%s], Y1's shape = [%s], Y1's height = "
            "%s.",
418 419 420 421 422
            x_dim,
            x_dim,
            x_dim[x_dim_size - 1],
            y_dim,
            y_dim,
W
Weilong Wu 已提交
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
            y_dim[y_dim_size - 2]));
    mat_solve(dev_ctx, tmp_x_bc, tmp_y_bc, out);
  }
}

// for TransposeNormal
static std::vector<int> getNewAxis(const int b_rank) {
  std::vector<int> axis_1 = {0};
  std::vector<int> axis_2 = {1, 0};
  std::vector<int> axis_3 = {0, 2, 1};
  std::vector<int> axis_4 = {0, 1, 3, 2};
  std::vector<int> axis_5 = {0, 1, 2, 4, 3};
  std::vector<int> axis_6 = {0, 1, 2, 3, 5, 4};
  std::vector<int> axis_7 = {0, 1, 2, 3, 4, 6, 5};
  std::vector<int> axis_8 = {0, 1, 2, 3, 4, 5, 7, 6};
  std::vector<int> axis_9 = {0, 1, 2, 3, 4, 5, 6, 8, 7};
  switch (b_rank) {
    case 1:
      return axis_1;
      break;
    case 2:
      return axis_2;
      break;
    case 3:
      return axis_3;
      break;
    case 4:
      return axis_4;
      break;
    case 5:
      return axis_5;
      break;
    case 6:
      return axis_6;
      break;
    case 7:
      return axis_7;
      break;
    case 8:
      return axis_8;
      break;
    default:
      return axis_9;
  }
}

// for Resize
static std::vector<int64_t> getNewDimsVec(const DDim& b_dims) {
471
  std::vector<int64_t> b_dims_vec = phi::vectorize(b_dims);
W
Weilong Wu 已提交
472 473 474 475 476 477 478 479 480
  int size = b_dims_vec.size();
  if (size >= 2) {
    // swap the last 2 elements in b_dims_vec
    int64_t temp = b_dims_vec[size - 1];
    b_dims_vec[size - 1] = b_dims_vec[size - 2];
    b_dims_vec[size - 2] = temp;
    return b_dims_vec;
  }
  PADDLE_ENFORCE_NE(
481 482
      b_dims_vec.empty(),
      true,
W
Weilong Wu 已提交
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
      platform::errors::PreconditionNotMet(
          "The size of tensor b must not be %d after getting new dims", 0));
  // if b_dims_vec.size() == 1, just retun original vec
  return b_dims_vec;
}

template <typename DeviceContext, typename T>
class SolveKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const auto* x = context.Input<framework::Tensor>("X");
    const auto* y = context.Input<framework::Tensor>("Y");
    Tensor* out = context.Output<framework::Tensor>("Out");
    linalg_solve<DeviceContext, T>(context, x, y, out);
  }
};

template <typename DeviceContext, typename T>
class SolveGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<framework::Tensor>("X");
    auto* y = ctx.Input<framework::Tensor>("Y");
    auto* dout = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));

    // reuse the linalg.solve forward output
    auto* out = ctx.Input<framework::Tensor>("Out");

    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));

    bool is_vector = false;
    is_vector = is_vector_rhs(*input, *y);

    Tensor tmp_y;
    if (is_vector) {
519
      tmp_y.mutable_data(ctx.GetPlace(), y->dtype());
W
Weilong Wu 已提交
520 521 522
      to_unsqueeze(ctx, *y, &tmp_y);
    } else {
      tmp_y.Resize(y->dims());
523
      tmp_y.mutable_data(ctx.GetPlace(), y->dtype());
W
Weilong Wu 已提交
524
      framework::TensorCopy(
525 526 527 528
          *y,
          ctx.GetPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &tmp_y);
W
Weilong Wu 已提交
529 530 531 532
    }

    Tensor tmp_x;
    tmp_x.Resize(input->dims());
533
    tmp_x.mutable_data(ctx.GetPlace(), input->dtype());
W
Weilong Wu 已提交
534
    framework::TensorCopy(
535 536 537 538
        *input,
        ctx.GetPlace(),
        ctx.template device_context<platform::DeviceContext>(),
        &tmp_x);
W
Weilong Wu 已提交
539 540 541 542

    std::vector<int64_t> x_broadcast_dims;
    std::vector<int64_t> y_broadcast_dims;
    std::tie(x_broadcast_dims, y_broadcast_dims) =
543
        get_broadcast_dims(tmp_x, tmp_y);
W
Weilong Wu 已提交
544 545 546

    // tmp_dx
    Tensor tmp_dx;
547
    tmp_dx.Resize(phi::make_ddim(x_broadcast_dims));
W
Weilong Wu 已提交
548 549 550 551
    tmp_dx.mutable_data<T>(ctx.GetPlace());

    // tmp_dy
    Tensor tmp_dy;
552
    tmp_dy.Resize(phi::make_ddim(y_broadcast_dims));
W
Weilong Wu 已提交
553 554
    tmp_dy.mutable_data<T>(ctx.GetPlace());

555
    Tensor tmp_input(input->dtype());
W
Weilong Wu 已提交
556
    const auto& new_dims_vec = getNewDimsVec(input->dims());
557
    tmp_input.Resize(phi::make_ddim(new_dims_vec));
W
Weilong Wu 已提交
558
    tmp_input.mutable_data<T>(ctx.GetPlace());
559
    phi::funcs::TransposeNormal<DeviceContext, T> trans;
W
Weilong Wu 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572
    std::vector<int> new_axis = getNewAxis(input->dims().size());
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    trans(dev_ctx, *input, &tmp_input, new_axis);

    if (dy) {
      dy->mutable_data<T>(ctx.GetPlace());
      // reuse linalg_solve forward logics to get tmp_dy
      linalg_solve<DeviceContext, T>(ctx, &tmp_input, dout, &tmp_dy);
    }

    if (dx) {
      dx->mutable_data<T>(ctx.GetPlace());
      // to get dx
573
      auto blas = phi::funcs::GetBlas<DeviceContext, T>(ctx);
W
Weilong Wu 已提交
574
      if (input->dims().size() == 2 && y->dims().size() == 2) {
575
        auto mat_dim_a1 =
576
            phi::funcs::CreateMatrixDescriptor(tmp_dy.dims(), 0, false);
577
        auto mat_dim_b1 =
578
            phi::funcs::CreateMatrixDescriptor(out->dims(), 0, true);
W
Weilong Wu 已提交
579 580 581
        blas.MatMul(tmp_dy, mat_dim_a1, *out, mat_dim_b1, T(-1), &tmp_dx, T(0));
      } else if (is_vector_rhs(*input, *y)) {
        Tensor tmp_dy_;
582
        tmp_dy_.mutable_data(ctx.GetPlace(), y->dtype());
W
Weilong Wu 已提交
583 584 585
        to_unsqueeze(ctx, tmp_dy, &tmp_dy_);

        Tensor tmp_out_;
586
        tmp_out_.mutable_data(ctx.GetPlace(), out->dtype());
W
Weilong Wu 已提交
587 588 589
        to_unsqueeze(ctx, *out, &tmp_out_);

        auto mat_dim_a1 =
590
            phi::funcs::CreateMatrixDescriptor(tmp_dy_.dims(), 0, false);
W
Weilong Wu 已提交
591
        auto mat_dim_b1 =
592
            phi::funcs::CreateMatrixDescriptor(tmp_out_.dims(), 0, true);
593 594
        blas.MatMul(
            tmp_dy_, mat_dim_a1, tmp_out_, mat_dim_b1, T(-1), &tmp_dx, T(0));
W
Weilong Wu 已提交
595
      } else {
596
        auto mat_dim_a1 =
597
            phi::funcs::CreateMatrixDescriptor(tmp_dy.dims(), 0, false);
598
        auto mat_dim_b1 =
599
            phi::funcs::CreateMatrixDescriptor(out->dims(), 0, true);
W
Weilong Wu 已提交
600 601 602 603 604 605 606
        blas.MatMul(tmp_dy, mat_dim_a1, *out, mat_dim_b1, T(-1), &tmp_dx, T(0));
      }
    }

    if (y->dims() != tmp_dy.dims()) {
      Tensor dy_help;
      dy_help.Resize(tmp_dy.dims());
607
      dy_help.mutable_data(ctx.GetPlace(), tmp_dy.dtype());
W
Weilong Wu 已提交
608
      framework::TensorCopy(
609 610 611 612
          tmp_dy,
          ctx.GetPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &dy_help);
W
Weilong Wu 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

      // get dims
      std::vector<std::int64_t> x_dims = vectorize(input->dims());
      std::vector<std::int64_t> y_dims = vectorize(y->dims());
      std::vector<std::int64_t> dout_dims = vectorize(dout->dims());

      if (is_vector_rhs(*input, *y)) {
        dout_dims.push_back(1);
      }

      int y_ndim = y_dims.size();
      int ndim = dout_dims.size();

      const std::vector<std::int64_t> dy_help_dims = vectorize(dy_help.dims());
      std::vector<std::int64_t> dy_broadcast_dims(ndim);

      std::fill(dy_broadcast_dims.data(),
630 631 632 633
                dy_broadcast_dims.data() + ndim - y_ndim,
                1);
      std::copy(y_dims.data(),
                y_dims.data() + y_ndim,
W
Weilong Wu 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
                dy_broadcast_dims.data() + ndim - y_ndim);

      std::vector<int> dy_reduce_dims;
      for (int idx = 0; idx <= ndim - 3; idx++) {
        if (dy_help_dims[idx] != 1 && dy_broadcast_dims[idx] == 1) {
          dy_reduce_dims.push_back(idx);
        }
      }
      // reduce sum to get grad by ReduceSum
      if (dy) {
        if (dy_reduce_dims.empty()) {
          *dy = std::move(dy_help);
        } else {
          bool keep_dim = true;
          if (dy_help.dims().size() != dy->dims().size()) {
            keep_dim = false;
          }
651 652
          ReduceSumForSolve<DeviceContext, T>(
              &dy_help, dy, dy_reduce_dims, keep_dim, ctx);
W
Weilong Wu 已提交
653 654 655 656 657
        }
        dy->Resize(y->dims());
      }
    } else {
      framework::TensorCopy(
658 659 660 661
          tmp_dy,
          ctx.GetPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          dy);
W
Weilong Wu 已提交
662 663 664 665 666
    }

    if (input->dims() != tmp_dx.dims()) {
      Tensor dx_help;
      dx_help.Resize(tmp_dx.dims());
667
      dx_help.mutable_data(ctx.GetPlace(), tmp_dx.dtype());
W
Weilong Wu 已提交
668
      framework::TensorCopy(
669 670 671 672
          tmp_dx,
          ctx.GetPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          &dx_help);
W
Weilong Wu 已提交
673 674 675 676 677 678 679 680 681 682 683 684

      // get dims
      std::vector<std::int64_t> x_dims = vectorize(input->dims());
      std::vector<std::int64_t> y_dims = vectorize(y->dims());

      int x_ndim = x_dims.size();
      int ndim = x_broadcast_dims.size();

      const std::vector<std::int64_t> dx_help_dims = vectorize(dx_help.dims());
      std::vector<std::int64_t> dx_broadcast_dims(ndim);

      std::fill(dx_broadcast_dims.data(),
685 686 687 688
                dx_broadcast_dims.data() + ndim - x_ndim,
                1);
      std::copy(x_dims.data(),
                x_dims.data() + x_ndim,
W
Weilong Wu 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
                dx_broadcast_dims.data() + ndim - x_ndim);

      std::vector<int> dx_reduce_dims;
      for (int idx = 0; idx <= ndim - 3; idx++) {
        if (dx_help_dims[idx] != 1 && dx_broadcast_dims[idx] == 1) {
          dx_reduce_dims.push_back(idx);
        }
      }
      // reduce sum to get grad by ReduceSum
      if (dx) {
        dx->mutable_data<T>(ctx.GetPlace());
        if (dx_reduce_dims.empty()) {
          *dx = std::move(dx_help);
        } else {
          bool keep_dim = true;
          if (dx_help.dims().size() != dx->dims().size()) {
            keep_dim = false;
          }
707 708
          ReduceSumForSolve<DeviceContext, T>(
              &dx_help, dx, dx_reduce_dims, keep_dim, ctx);
W
Weilong Wu 已提交
709 710 711 712 713
        }
        dx->Resize(input->dims());
      }
    } else {
      framework::TensorCopy(
714 715 716 717
          tmp_dx,
          ctx.GetPlace(),
          ctx.template device_context<platform::DeviceContext>(),
          dx);
W
Weilong Wu 已提交
718 719 720 721 722
    }
  }
};
}  // namespace operators
}  // namespace paddle