lu_unpack_grad_kernel_impl.h 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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

#include "paddle/phi/kernels/impl/lu_kernel_impl.h"

namespace phi {

template <typename T, typename Context>
void LUUnpackGradKernel(const Context& dev_ctx,
G
Galaxy1458 已提交
23 24 25 26 27
                        const DenseTensor& x UNUSED,
                        const DenseTensor& pivots UNUSED,
                        const DenseTensor& l UNUSED,
                        const DenseTensor& u UNUSED,
                        const DenseTensor& pmat UNUSED,
28 29
                        const DenseTensor& l_grad,
                        const DenseTensor& u_grad,
G
Galaxy1458 已提交
30 31
                        bool unpack_ludata UNUSED,
                        bool unpack_pivots UNUSED,
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 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
                        DenseTensor* x_grad) {
  dev_ctx.template Alloc<T>(x_grad);

  DenseTensor dl_tril, du_triu;
  const auto ldims = l_grad.dims();
  dl_tril.Resize(ldims);
  auto H = ldims[ldims.size() - 2];
  auto W = ldims[ldims.size() - 1];
  dev_ctx.template Alloc<T>(&dl_tril);
  auto L_dataptr = dl_tril.data<T>();
  phi::funcs::ForRange<Context> l_for_range(dev_ctx, l_grad.numel());
  phi::funcs::TrilTriuCompute<T> tril_computer(
      l_grad.data<T>(), -1, true, H, W, L_dataptr);
  l_for_range(tril_computer);

  const auto udims = u_grad.dims();
  du_triu.Resize(udims);
  H = udims[udims.size() - 2];
  W = udims[udims.size() - 1];
  dev_ctx.template Alloc<T>(&du_triu);
  auto U_dataptr = du_triu.data<T>();
  phi::funcs::ForRange<Context> u_for_range(dev_ctx, u_grad.numel());
  phi::funcs::TrilTriuCompute<T> triu_computer(
      u_grad.data<T>(), 0, false, H, W, U_dataptr);
  u_for_range(triu_computer);

  auto xdims = x_grad->dims();
  int xrank = xdims.size();
  int64_t m = xdims[xrank - 2];
  int64_t n = xdims[xrank - 1];
  int64_t k = std::min(m, n);

  std::vector<int64_t> axes = {xrank - 2, xrank - 1};
  std::vector<int64_t> slice_starts(2, 0);
  std::vector<int64_t> slice_ends(2, 0);
  auto valuedims = vectorize(xdims);

  phi::funcs::SetConstant<Context, T> setter;
  setter(dev_ctx, x_grad, static_cast<T>(0));
  if (m <= n) {
    slice_starts[0] = 0;
    slice_starts[1] = 0;
    slice_ends[0] = k;
    slice_ends[1] = k;
    valuedims[xrank - 2] = k;
    valuedims[xrank - 1] = k;
    SetValueCompute_dispatch<Context, T>(dev_ctx,
                                         x_grad,
                                         &dl_tril,
                                         x_grad,
                                         axes,
                                         &slice_starts,
                                         &slice_ends,
                                         valuedims,
                                         xrank);

    Tensor_Add<Context, T>(dev_ctx, *x_grad, du_triu, x_grad);
  } else {
    slice_starts[0] = 0;
    slice_starts[1] = 0;
    slice_ends[0] = k;
    slice_ends[1] = k;
    valuedims[xrank - 2] = k;
    valuedims[xrank - 1] = k;
    SetValueCompute_dispatch<Context, T>(dev_ctx,
                                         x_grad,
                                         &du_triu,
                                         x_grad,
                                         axes,
                                         &slice_starts,
                                         &slice_ends,
                                         valuedims,
                                         xrank);

    Tensor_Add<Context, T>(dev_ctx, *x_grad, dl_tril, x_grad);
  }
}

}  // namespace phi