math_function.cc 11.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/math_function.h"
D
dzhwinter 已提交
16 17 18 19 20 21 22 23 24

#ifdef PADDLE_WITH_MKLML
#include "paddle/fluid/platform/dynload/mklml.h"
#endif

#ifdef PADDLE_USE_OPENBLAS
#include <cblas.h>
#endif

25
#include <memory>
26
#include <utility>
Y
Yu Yang 已提交
27
#include <vector>
Y
Yi Wang 已提交
28 29
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/operators/math/math_function_impl.h"
30
#include "paddle/fluid/platform/bfloat16.h"
31
#include "paddle/fluid/platform/float16.h"
32
#include "paddle/pten/backends/cpu/cpu_context.h"
C
Chen Weihang 已提交
33
#include "paddle/pten/kernels/funcs/eigen/common.h"
34
#include "unsupported/Eigen/CXX11/Tensor"
Q
qijun 已提交
35 36 37 38 39

namespace paddle {
namespace operators {
namespace math {

40 41
using float16 = paddle::platform::float16;

K
Kexin Zhao 已提交
42
template struct SetConstant<platform::CPUDeviceContext, platform::float16>;
43
template struct SetConstant<platform::CPUDeviceContext, platform::bfloat16>;
Q
QI JUN 已提交
44 45
template struct SetConstant<platform::CPUDeviceContext, float>;
template struct SetConstant<platform::CPUDeviceContext, double>;
46
template struct SetConstant<platform::CPUDeviceContext, int16_t>;
Q
QI JUN 已提交
47 48 49
template struct SetConstant<platform::CPUDeviceContext, int>;
template struct SetConstant<platform::CPUDeviceContext, int64_t>;
template struct SetConstant<platform::CPUDeviceContext, bool>;
F
fengjiayi 已提交
50
template struct SetConstant<platform::CPUDeviceContext, uint8_t>;
51 52 53 54
template struct SetConstant<platform::CPUDeviceContext,
                            platform::complex<float>>;
template struct SetConstant<platform::CPUDeviceContext,
                            platform::complex<double>>;
55

56 57 58 59 60 61 62 63 64 65 66 67
template struct SetConstant<pten::CPUContext, platform::float16>;
template struct SetConstant<pten::CPUContext, platform::bfloat16>;
template struct SetConstant<pten::CPUContext, float>;
template struct SetConstant<pten::CPUContext, double>;
template struct SetConstant<pten::CPUContext, int16_t>;
template struct SetConstant<pten::CPUContext, int>;
template struct SetConstant<pten::CPUContext, int64_t>;
template struct SetConstant<pten::CPUContext, bool>;
template struct SetConstant<pten::CPUContext, uint8_t>;
template struct SetConstant<pten::CPUContext, platform::complex<float>>;
template struct SetConstant<pten::CPUContext, platform::complex<double>>;

68 69
#ifdef PADDLE_WITH_XPU
template struct SetConstant<platform::XPUDeviceContext, platform::float16>;
70
template struct SetConstant<platform::XPUDeviceContext, platform::bfloat16>;
71 72
template struct SetConstant<platform::XPUDeviceContext, float>;
template struct SetConstant<platform::XPUDeviceContext, double>;
73
template struct SetConstant<platform::XPUDeviceContext, uint8_t>;
74
template struct SetConstant<platform::XPUDeviceContext, int16_t>;
75 76 77
template struct SetConstant<platform::XPUDeviceContext, int>;
template struct SetConstant<platform::XPUDeviceContext, int64_t>;
template struct SetConstant<platform::XPUDeviceContext, bool>;
78 79 80 81
template struct SetConstant<platform::XPUDeviceContext,
                            platform::complex<float>>;
template struct SetConstant<platform::XPUDeviceContext,
                            platform::complex<double>>;
82 83
#endif

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define DEFINE_CPU_TRANS(RANK)                                              \
  template struct Transpose<platform::CPUDeviceContext, platform::float16,  \
                            RANK>;                                          \
  template struct Transpose<platform::CPUDeviceContext, platform::bfloat16, \
                            RANK>;                                          \
  template struct Transpose<platform::CPUDeviceContext, float, RANK>;       \
  template struct Transpose<platform::CPUDeviceContext, double, RANK>;      \
  template struct Transpose<platform::CPUDeviceContext, int, RANK>;         \
  template struct Transpose<platform::CPUDeviceContext, int64_t, RANK>;     \
  template struct Transpose<platform::CPUDeviceContext, bool, RANK>;        \
  template struct Transpose<platform::CPUDeviceContext, int16_t, RANK>;     \
  template struct Transpose<platform::CPUDeviceContext, uint8_t, RANK>;     \
  template struct Transpose<platform::CPUDeviceContext, int8_t, RANK>;      \
  template struct Transpose<platform::CPUDeviceContext,                     \
                            platform::complex<float>, RANK>;                \
  template struct Transpose<platform::CPUDeviceContext,                     \
                            platform::complex<double>, RANK>;
101 102 103 104 105 106 107

DEFINE_CPU_TRANS(1);
DEFINE_CPU_TRANS(2);
DEFINE_CPU_TRANS(3);
DEFINE_CPU_TRANS(4);
DEFINE_CPU_TRANS(5);
DEFINE_CPU_TRANS(6);
Q
qijun 已提交
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
template <typename T>
struct TransposeNormal<platform::CPUDeviceContext, T> {
  void operator()(const platform::CPUDeviceContext& context,
                  const framework::Tensor& in, framework::Tensor* out,
                  const std::vector<int>& axis) {
    const int rank = axis.size();
    auto in_stride = framework::stride(in.dims());
    auto out_stride = framework::stride(out->dims());
    const T* in_ptr = in.data<T>();
    T* out_ptr = out->data<T>();

    auto transpose_helper = [&](int64_t beg, int64_t end) {
      for (int64_t out_idx = beg; out_idx < end; ++out_idx) {
        int64_t in_idx = 0;
        int64_t tmp_idx = out_idx;
        // calculate the input index
        for (int i = 0; i < rank; ++i) {
          const int64_t coordinate = tmp_idx / out_stride[i];
          tmp_idx -= coordinate * out_stride[i];
          in_idx += coordinate * in_stride[axis[i]];
        }
        out_ptr[out_idx] = in_ptr[in_idx];
      }
    };
W
wawltor 已提交
133
    transpose_helper(0, out->numel());
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }
};

// define transpose normal
#define DEFINE_CPU_TRANS_NORMAL(TYPE) \
  template struct TransposeNormal<platform::CPUDeviceContext, TYPE>

DEFINE_CPU_TRANS_NORMAL(platform::float16);
DEFINE_CPU_TRANS_NORMAL(platform::bfloat16);
DEFINE_CPU_TRANS_NORMAL(float);
DEFINE_CPU_TRANS_NORMAL(double);
DEFINE_CPU_TRANS_NORMAL(int);
DEFINE_CPU_TRANS_NORMAL(int64_t);
DEFINE_CPU_TRANS_NORMAL(bool);
DEFINE_CPU_TRANS_NORMAL(int16_t);
DEFINE_CPU_TRANS_NORMAL(uint8_t);
DEFINE_CPU_TRANS_NORMAL(int8_t);
151 152
DEFINE_CPU_TRANS_NORMAL(platform::complex<float>);
DEFINE_CPU_TRANS_NORMAL(platform::complex<double>);
153

154 155
struct TensorSetConstantCPU {
  TensorSetConstantCPU(framework::Tensor* tensor, float value)
156 157
      : tensor_(tensor), value_(value) {}
  template <typename T>
D
dzhwinter 已提交
158
  void apply() const {
159 160 161 162 163 164 165 166
    auto cpu = platform::CPUPlace();
    auto* begin = tensor_->mutable_data<T>(cpu);
    std::fill(begin, begin + tensor_->numel(), static_cast<T>(value_));
  }
  framework::Tensor* tensor_;
  float value_;
};

167 168 169 170 171 172 173
template <>
void set_constant_with_place<platform::XPUPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
  PADDLE_THROW(platform::errors::Unimplemented("XPUPlace is not supported"));
}

174 175 176 177 178 179 180
template <>
void set_constant_with_place<platform::NPUPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
  PADDLE_THROW(platform::errors::Unimplemented("NPUPlace is not supported"));
}

181 182 183 184 185 186 187 188
template <>
void set_constant_with_place<platform::NPUPinnedPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
  PADDLE_THROW(
      platform::errors::Unimplemented("NPUPinnedPlace is not supported"));
}

J
jianghaicheng 已提交
189 190 191 192 193 194 195
template <>
void set_constant_with_place<platform::IPUPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
  PADDLE_THROW(platform::errors::Unimplemented("IPUPlace is not supported"));
}

196 197 198 199
template <>
void set_constant_with_place<platform::CPUPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
Y
Yu Yang 已提交
200
  framework::VisitDataType(tensor->type(), TensorSetConstantCPU(tensor, value));
201 202
}

F
fwenguang 已提交
203 204 205 206 207 208 209
template <>
void set_constant_with_place<platform::MLUPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
  PADDLE_THROW(platform::errors::Unimplemented("MLUPlace is not supported"));
}

C
chengduoZH 已提交
210 211 212 213
template <>
void set_constant_with_place<platform::CUDAPinnedPlace>(
    const platform::DeviceContext& context, framework::Tensor* tensor,
    float value) {
Y
Yu Yang 已提交
214
  framework::VisitDataType(tensor->type(), TensorSetConstantCPU(tensor, value));
C
chengduoZH 已提交
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
struct TensorSetConstantWithPlace : public boost::static_visitor<void> {
  TensorSetConstantWithPlace(const platform::DeviceContext& context,
                             framework::Tensor* tensor, float value)
      : context_(context), tensor_(tensor), value_(value) {}

  template <typename Place>
  void operator()(Place place) const {
    set_constant_with_place<Place>(context_, tensor_, value_);
  }

  const platform::DeviceContext& context_;
  framework::Tensor* tensor_;
  float value_;
};

void set_constant(const platform::DeviceContext& context,
                  framework::Tensor* tensor, float value) {
Y
Fix CI  
Yu Yang 已提交
234
  TensorSetConstantWithPlace func(context, tensor, value);
235
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
236 237
  // tensor->place().apply_visitor(func);
  paddle::platform::VisitPlace(tensor->place(), func);
238 239 240 241 242
#else
  func(platform::CPUPlace());
#endif
}

Q
qingqing01 已提交
243 244 245 246 247 248
template <typename T>
struct RowwiseAdd<platform::CPUDeviceContext, T> {
  void operator()(const platform::CPUDeviceContext& context,
                  const framework::Tensor& input,
                  const framework::Tensor& vector, framework::Tensor* output) {
    auto in_dims = input.dims();
249
    auto out_dims = output->dims();
Q
qingqing01 已提交
250
    auto size = input.numel() / in_dims[0];
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    PADDLE_ENFORCE_EQ(
        vector.numel(), size,
        platform::errors::InvalidArgument(
            "The input vector size"
            " should be equal to the size of each row of input tensor."
            " Expected vector size=%d, but received %d",
            size, vector.numel()));
    const char* in_dims_cstr = in_dims.to_str().c_str();
    const char* out_dims_cstr = out_dims.to_str().c_str();
    PADDLE_ENFORCE_EQ(out_dims, in_dims,
                      platform::errors::InvalidArgument(
                          "The output tensor shape should be same as the input"
                          " tensor shape. Expected output tensor shape: %s,"
                          " but received %s",
                          in_dims_cstr, out_dims_cstr));
Q
qingqing01 已提交
266

Q
qingqing01 已提交
267 268 269 270 271 272
    auto in = framework::EigenMatrix<T>::From(input);
    auto vec = framework::EigenVector<T>::Flatten(vector);
    auto out = framework::EigenMatrix<T>::From(*output);

    for (int64_t i = 0; i < in_dims[0]; ++i) {
      out.chip(i, 0) = in.chip(i, 0) + vec;
Q
qingqing01 已提交
273 274 275 276
    }
  }
};

Q
QI JUN 已提交
277 278
template struct RowwiseAdd<platform::CPUDeviceContext, float>;
template struct RowwiseAdd<platform::CPUDeviceContext, double>;
Q
qingqing01 已提交
279

Q
QI JUN 已提交
280 281
template struct ColwiseSum<platform::CPUDeviceContext, float>;
template struct ColwiseSum<platform::CPUDeviceContext, double>;
Y
yangyaming 已提交
282 283
template struct ColwiseSum<platform::CPUDeviceContext, int>;
template struct ColwiseSum<platform::CPUDeviceContext, int64_t>;
284

C
chengduoZH 已提交
285 286 287 288 289 290
template struct RowwiseSum<platform::CPUDeviceContext, float>;
template struct RowwiseSum<platform::CPUDeviceContext, double>;

template struct RowwiseMean<platform::CPUDeviceContext, float>;
template struct RowwiseMean<platform::CPUDeviceContext, double>;

291 292 293 294 295 296 297 298 299 300 301 302 303
template <typename T>
struct ElementwiseAddTo<platform::CPUDeviceContext, T> {
  void operator()(platform::CPUDeviceContext* ctx, const framework::Tensor& src,
                  framework::Tensor* dst) {
    auto in = framework::EigenVector<T>::Flatten(src);
    auto out = framework::EigenVector<T>::Flatten(*dst);
    auto& place = *(ctx->eigen_device());
    out.device(place) = out + in;
  }
};

template struct ElementwiseAddTo<platform::CPUDeviceContext, platform::float16>;

Q
qijun 已提交
304 305 306
}  // namespace math
}  // namespace operators
}  // namespace paddle