trace_op.h 8.9 KB
Newer Older
H
hong 已提交
1
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
L
Li Fuchen 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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 <algorithm>
H
hong 已提交
17
#include <vector>
L
Li Fuchen 已提交
18
#include "paddle/fluid/framework/eigen.h"
H
hong 已提交
19
#include "paddle/fluid/framework/op_registry.h"
L
Li Fuchen 已提交
20 21 22
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/for_range.h"

H
hong 已提交
23 24 25
namespace paddle {
namespace operators {

L
Li Fuchen 已提交
26
template <typename T>
27
struct DiagonalFunctor {
H
hong 已提交
28 29
  DiagonalFunctor(const T* input, const int64_t* diag_stride,
                  const int64_t* ret_strides, int64_t pos, int64_t dim_size,
30
                  T* diag)
L
Li Fuchen 已提交
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
      : input_(input),
        diag_stride_(diag_stride),
        ret_strides_(ret_strides),
        pos_(pos),
        dim_size_(dim_size),
        diag_(diag) {}

  HOSTDEVICE void operator()(size_t idx) const {
    int64_t position = pos_;
    int64_t num = idx;
    for (int64_t i = 0; i < dim_size_; i++) {
      position += num / diag_stride_[i] * ret_strides_[i];
      num = num % diag_stride_[i];
    }
    diag_[idx] = input_[position];
  }

  const T* input_;
  const int64_t* diag_stride_;
  const int64_t* ret_strides_;
  int64_t pos_;
  int64_t dim_size_;
  T* diag_;
};

template <typename T>
struct TraceGradFunctor {
H
hong 已提交
58 59 60
  TraceGradFunctor(const T* d_out, const int64_t* out_stride,
                   const int64_t* x_strides, int64_t pos, int64_t dim_size,
                   int64_t dim1, int64_t dim2, int64_t diag_size, T* d_x)
L
Li Fuchen 已提交
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
      : d_out_(d_out),
        out_stride_(out_stride),
        x_strides_(x_strides),
        pos_(pos),
        dim_size_(dim_size),
        dim1_(dim1),
        dim2_(dim2),
        diag_size_(diag_size),
        d_x_(d_x) {}

  HOSTDEVICE void operator()(size_t idx) const {
    int64_t num = idx - pos_;
    int64_t position = 0;
    if (num >= 0) {
      int64_t dim1 = 0;
      int64_t dim2 = 0;
      int64_t out_idx = 0;
      for (int64_t i = 0; i < dim_size_; i++) {
        if (i != dim1_ && i != dim2_) {
          position += num / x_strides_[i] * out_stride_[out_idx++];
        } else if (i == dim1_) {
          dim1 = num / x_strides_[i];
        } else {
          dim2 = num / x_strides_[i];
        }
        num = num % x_strides_[i];
      }
      if (dim1 == dim2 && dim1 < diag_size_) {
        d_x_[idx] = d_out_[position];
      }
    }
  }
  const T* d_out_;
  const int64_t* out_stride_;
  const int64_t* x_strides_;
  int64_t pos_;
  int64_t dim_size_;
  int64_t dim1_;
  int64_t dim2_;
  int64_t diag_size_;
  T* d_x_;
};

H
hong 已提交
104 105 106 107
template <typename DeviceContext, typename T>
framework::Tensor Diagonal(const framework::ExecutionContext& context,
                           const framework::Tensor* input, int64_t offset,
                           int64_t dim1, int64_t dim2) {
L
Li Fuchen 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  auto* input_data = input->data<T>();
  auto input_dims = input->dims();
  auto input_stride = framework::stride(input_dims);
  auto dim1_ = dim1 < 0 ? input_dims.size() + dim1 : dim1;
  auto dim2_ = dim2 < 0 ? input_dims.size() + dim2 : dim2;
  auto len1 = input_dims[std::min(dim1_, dim2_)];
  auto len2 = input_dims[std::max(dim1_, dim2_)];
  auto stride1 = input_stride[std::min(dim1_, dim2_)];
  auto stride2 = input_stride[std::max(dim1_, dim2_)];

  int offset_stride = 0;
  if (offset >= 0) {
    offset_stride = stride2;
    len2 -= offset;
  } else {
    offset_stride = stride1;
    len1 += offset;
  }
  int diag_size = len2 < len1 ? len2 : len1;

  if (diag_size > 0) {
    auto ret_strides = vectorize(input_stride);
    auto ret_dims = vectorize(input_dims);
    ret_strides.erase(ret_strides.begin() + std::max(dim1_, dim2_));
    ret_strides.erase(ret_strides.begin() + std::min(dim1_, dim2_));
    ret_dims.erase(ret_dims.begin() + std::max(dim1_, dim2_));
    ret_dims.erase(ret_dims.begin() + std::min(dim1_, dim2_));
    if (ret_strides.empty()) {
      ret_strides.push_back(1);
      ret_dims.push_back(1);
    }
    ret_strides.push_back(stride1 + stride2);
    ret_dims.push_back(diag_size);
H
hong 已提交
141
    framework::Tensor diag;
L
Li Fuchen 已提交
142 143 144 145 146 147
    framework::DDim diag_dims = framework::make_ddim(ret_dims);
    auto dig_stride = framework::stride(diag_dims);
    auto diag_data = diag.mutable_data<T>(diag_dims, context.GetPlace());

    int64_t pos = std::abs(offset) * offset_stride;
    int64_t dim_size = ret_strides.size();
148
#if defined(__NVCC__) || defined(__HIPCC__)
L
Li Fuchen 已提交
149 150 151 152 153 154 155 156 157
    thrust::device_vector<int64_t> diag_vec(vectorize(dig_stride));
    const int64_t* diag_arr = thrust::raw_pointer_cast(diag_vec.data());
    thrust::device_vector<int64_t> ret_vec(ret_strides);
    const int64_t* ret_arr = thrust::raw_pointer_cast(ret_vec.data());
#else
    auto* diag_arr = dig_stride.Get();
    const auto* ret_arr = ret_strides.data();
#endif

H
hong 已提交
158 159 160 161
    auto& dev_ctx = context.template device_context<DeviceContext>();
    platform::ForRange<DeviceContext> for_range(dev_ctx, diag.numel());
    DiagonalFunctor<T> functor(input_data, diag_arr, ret_arr, pos, dim_size,
                               diag_data);
L
Li Fuchen 已提交
162 163 164 165 166 167 168
    for_range(functor);
    return diag;
  } else {
    return {};
  }
}

H
hong 已提交
169 170 171 172 173 174
template <typename DeviceContext, typename T>
class TraceKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* input = context.Input<framework::Tensor>("Input");
    auto* out = context.Output<framework::Tensor>("Out");
L
Li Fuchen 已提交
175

H
hong 已提交
176 177 178
    const int64_t offset = context.Attr<int>("offset");
    const int64_t dim1 = context.Attr<int>("axis1");
    const int64_t dim2 = context.Attr<int>("axis2");
L
Li Fuchen 已提交
179

H
hong 已提交
180
    auto output_dims = out->dims();
L
Li Fuchen 已提交
181

H
hong 已提交
182
    T* out_data = out->mutable_data<T>(context.GetPlace());
L
Li Fuchen 已提交
183

H
hong 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
    const framework::Tensor diag =
        Diagonal<DeviceContext, T>(context, input, offset, dim1, dim2);
    if (diag.numel() > 0) {
      auto x = framework::EigenMatrix<T>::Reshape(diag, diag.dims().size() - 1);
      auto output = framework::EigenVector<T>::Flatten(*out);
      auto& place =
          *context.template device_context<DeviceContext>().eigen_device();
      auto reduce_dim = Eigen::array<int, 1>({1});
      output.device(place) = x.sum(reduce_dim);
      out->Resize(output_dims);
    } else {
      std::fill(out_data, out_data + out->numel(), static_cast<T>(0));
    }
L
Li Fuchen 已提交
197
  }
H
hong 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
};

template <typename DeviceContext, typename T>
class TraceGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const auto* d_out =
        context.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* d_x =
        context.Output<framework::Tensor>(framework::GradVarName("Input"));

    int64_t offset = context.Attr<int>("offset");
    int64_t dim1 = context.Attr<int>("axis1");
    int64_t dim2 = context.Attr<int>("axis2");

    auto input_dims = d_x->dims();
    auto input_stride = framework::stride(input_dims);
    auto output_dims = d_out->dims();
    auto output_stride = framework::stride(output_dims);

    auto* out_data = d_out->data<T>();
    T* x_data = d_x->mutable_data<T>(context.GetPlace());

    math::SetConstant<DeviceContext, T> set_zero;
    auto& dev_ctx = context.template device_context<DeviceContext>();
    set_zero(dev_ctx, d_x, static_cast<T>(0.0));

    auto dim1_ = dim1 < 0 ? input_dims.size() + dim1 : dim1;
    auto dim2_ = dim2 < 0 ? input_dims.size() + dim2 : dim2;
    auto len1 = input_dims[std::min(dim1_, dim2_)];
    auto len2 = input_dims[std::max(dim1_, dim2_)];
    auto stride1 = input_stride[std::min(dim1_, dim2_)];
    auto stride2 = input_stride[std::max(dim1_, dim2_)];

    int offset_stride = 0;
    if (offset >= 0) {
      offset_stride = stride2;
      len2 -= offset;
    } else {
      offset_stride = stride1;
      len1 += offset;
    }
    int64_t diag_size = len2 < len1 ? len2 : len1;
    int64_t pos = std::abs(offset) * offset_stride;
    if (diag_size > 0) {
243
#if defined(__NVCC__) || defined(__HIPCC__)
H
hong 已提交
244 245 246 247
      thrust::device_vector<int64_t> output_vec(vectorize(output_stride));
      const int64_t* output_arr = thrust::raw_pointer_cast(output_vec.data());
      thrust::device_vector<int64_t> input_vec(vectorize(input_stride));
      const int64_t* input_arr = thrust::raw_pointer_cast(input_vec.data());
L
Li Fuchen 已提交
248 249

#else
H
hong 已提交
250 251
      const auto* output_arr = output_stride.Get();
      const auto* input_arr = input_stride.Get();
L
Li Fuchen 已提交
252 253
#endif

H
hong 已提交
254 255 256 257 258 259
      platform::ForRange<DeviceContext> for_range(dev_ctx, d_x->numel());
      TraceGradFunctor<T> functor(out_data, output_arr, input_arr, pos,
                                  input_dims.size(), dim1_, dim2_, diag_size,
                                  x_data);
      for_range(functor);
    }
L
Li Fuchen 已提交
260
  }
H
hong 已提交
261
};
L
Li Fuchen 已提交
262

H
hong 已提交
263 264
}  // namespace operators
}  // namespace paddle