lu_unpack_op.h 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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 "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/lu_op.h"
19 20
#include "paddle/fluid/platform/for_range.h"
#include "paddle/phi/kernels/funcs/tril_triu_compute.h"
21 22 23 24 25 26 27 28 29 30 31 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

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using LoDTensorArray = framework::LoDTensorArray;

template <typename DeviceContext, typename T>
class LU_UnpackKernel : public framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
    auto xin = ctx.Input<framework::Tensor>("X");
    auto P = ctx.Input<framework::Tensor>("Pivots");

    auto ltensor = ctx.Output<framework::Tensor>("L");
    auto utensor = ctx.Output<framework::Tensor>("U");
    auto ptensor = ctx.Output<framework::Tensor>("Pmat");

    auto unpack_ludata = ctx.Attr<bool>("unpack_ludata");
    auto unpack_pivots = ctx.Attr<bool>("unpack_pivots");

    const auto& dev_ctx = ctx.template device_context<DeviceContext>();

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

    if (unpack_ludata) {
      ltensor->mutable_data<T>(ctx.GetPlace());
      utensor->mutable_data<T>(ctx.GetPlace());

      framework::Tensor L, U;
      LU_Unpack<DeviceContext, T>(dev_ctx, xin, &L, &U);

      if (m >= n) {
        framework::TensorCopy(L, ctx.GetPlace(), ltensor);
        Tensor_narrow<DeviceContext, T>(ctx, &U, utensor, 0, k, 0, k);
      } else {
        framework::TensorCopy(U, ctx.GetPlace(), utensor);
        Tensor_narrow<DeviceContext, T>(ctx, &L, ltensor, 0, k, 0, k);
      }
    }

    if (unpack_pivots) {
      ptensor->mutable_data<T>(ctx.GetPlace());
      Unpack_Pivot<DeviceContext, T>(dev_ctx, *P, ptensor, m, k);
    }
  }
};

template <typename DeviceContext, typename T>
class LU_UnpackGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto dl = ctx.Input<framework::Tensor>(framework::GradVarName("L"));
    auto du = ctx.Input<framework::Tensor>(framework::GradVarName("U"));
    auto dx = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    dx->mutable_data<T>(ctx.GetPlace());

    const auto& dev_ctx = ctx.template device_context<DeviceContext>();

    framework::Tensor dl_tril, du_triu;
    const auto ldims = dl->dims();
    dl_tril.Resize(ldims);
    auto H = ldims[ldims.size() - 2];
    auto W = ldims[ldims.size() - 1];
    auto L_dataptr = dl_tril.mutable_data<T>(dev_ctx.GetPlace());
    platform::ForRange<DeviceContext> l_for_range(dev_ctx, dl->numel());
91 92
    phi::funcs::TrilTriuCompute<T> tril_computer(dl->data<T>(), -1, true, H, W,
                                                 L_dataptr);
93 94 95 96 97 98 99 100
    l_for_range(tril_computer);

    const auto udims = du->dims();
    du_triu.Resize(udims);
    H = udims[udims.size() - 2];
    W = udims[udims.size() - 1];
    auto U_dataptr = du_triu.mutable_data<T>(dev_ctx.GetPlace());
    platform::ForRange<DeviceContext> u_for_range(dev_ctx, du->numel());
101 102
    phi::funcs::TrilTriuCompute<T> triu_computer(du->data<T>(), 0, false, H, W,
                                                 U_dataptr);
103 104 105 106 107 108 109 110 111 112 113 114 115
    u_for_range(triu_computer);

    auto xdims = dx->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);

116
    phi::funcs::SetConstant<DeviceContext, T> setter;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    setter(dev_ctx, dx, 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<DeviceContext, T>(ctx, dx, &dl_tril, dx, axes,
                                                 &slice_starts, &slice_ends,
                                                 valuedims, xrank);

      Tensor_Add<DeviceContext, T>(dev_ctx, *dx, du_triu, dx);
    } 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<DeviceContext, T>(ctx, dx, &du_triu, dx, axes,
                                                 &slice_starts, &slice_ends,
                                                 valuedims, xrank);

      Tensor_Add<DeviceContext, T>(dev_ctx, *dx, dl_tril, dx);
    }
  }
};

}  // namespace operators
}  // namespace paddle