349 lines
13 KiB
C++
349 lines
13 KiB
C++
/* Copyright (c) 2016 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. */
|
|
|
|
#include "paddle/phi/kernels/funcs/math_function.h"
|
|
#include "paddle/phi/core/utils/visit_place.h"
|
|
|
|
#ifdef PADDLE_WITH_MKLML
|
|
#include "paddle/phi/backends/dynload/mklml.h"
|
|
#endif
|
|
|
|
#ifdef PADDLE_USE_OPENBLAS
|
|
#include <cblas.h>
|
|
#elif PADDLE_USE_ACCELERATE
|
|
#include <Accelerate/Accelerate.h>
|
|
#endif
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "paddle/phi/backends/context_pool.h"
|
|
|
|
#ifdef PADDLE_WITH_XPU
|
|
#include "paddle/phi/backends/xpu/xpu_context.h"
|
|
#endif
|
|
|
|
#include "paddle/phi/backends/cpu/cpu_context.h"
|
|
#include "paddle/phi/common/data_type.h"
|
|
#include "paddle/phi/kernels/funcs/eigen/common.h"
|
|
#include "paddle/phi/kernels/funcs/math_function_impl.h"
|
|
#ifdef PADDLE_WITH_CUSTOM_DEVICE
|
|
#include "paddle/phi/api/lib/kernel_dispatch.h"
|
|
#include "paddle/phi/core/kernel_factory.h"
|
|
#endif
|
|
|
|
namespace phi::funcs {
|
|
|
|
template struct SetConstant<CPUContext, phi::float8_e4m3fn>;
|
|
template struct SetConstant<CPUContext, phi::float8_e5m2>;
|
|
template struct SetConstant<CPUContext, phi::float16>;
|
|
template struct SetConstant<CPUContext, phi::bfloat16>;
|
|
template struct SetConstant<CPUContext, float>;
|
|
template struct SetConstant<CPUContext, double>;
|
|
template struct SetConstant<CPUContext, int16_t>;
|
|
template struct SetConstant<CPUContext, int>;
|
|
template struct SetConstant<CPUContext, int64_t>;
|
|
template struct SetConstant<CPUContext, bool>;
|
|
template struct SetConstant<CPUContext, uint8_t>;
|
|
template struct SetConstant<CPUContext, uint16_t>;
|
|
template struct SetConstant<CPUContext, uint32_t>;
|
|
template struct SetConstant<CPUContext, uint64_t>;
|
|
template struct SetConstant<CPUContext, int8_t>;
|
|
template struct SetConstant<CPUContext, phi::complex64>;
|
|
template struct SetConstant<CPUContext, phi::complex128>;
|
|
|
|
#ifdef PADDLE_WITH_XPU
|
|
template struct SetConstant<XPUContext, phi::float16>;
|
|
template struct SetConstant<XPUContext, phi::bfloat16>;
|
|
template struct SetConstant<XPUContext, float>;
|
|
template struct SetConstant<XPUContext, double>;
|
|
template struct SetConstant<XPUContext, uint8_t>;
|
|
template struct SetConstant<XPUContext, int8_t>;
|
|
template struct SetConstant<XPUContext, int16_t>;
|
|
template struct SetConstant<XPUContext, int>;
|
|
template struct SetConstant<XPUContext, int64_t>;
|
|
template struct SetConstant<XPUContext, bool>;
|
|
template struct SetConstant<XPUContext, phi::complex64>;
|
|
template struct SetConstant<XPUContext, phi::complex128>;
|
|
#endif // PADDLE_WITH_XPU
|
|
|
|
#define DEFINE_CPU_TRANS(RANK) \
|
|
template struct PADDLE_API Transpose<CPUContext, phi::float16, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, phi::bfloat16, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, phi::float8_e4m3fn, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, phi::float8_e5m2, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, float, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, double, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, int, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, int64_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, bool, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, int16_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, uint8_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, uint16_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, uint32_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, uint64_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, int8_t, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, phi::complex64, RANK>; \
|
|
template struct PADDLE_API Transpose<CPUContext, phi::complex128, RANK>;
|
|
|
|
DEFINE_CPU_TRANS(1);
|
|
DEFINE_CPU_TRANS(2);
|
|
DEFINE_CPU_TRANS(3);
|
|
DEFINE_CPU_TRANS(4);
|
|
DEFINE_CPU_TRANS(5);
|
|
DEFINE_CPU_TRANS(6);
|
|
|
|
#ifdef PADDLE_WITH_XPU
|
|
#define DEFINE_XPU_TRANS(RANK) \
|
|
template struct PADDLE_API Transpose<XPUContext, bool, RANK>; \
|
|
template struct PADDLE_API Transpose<XPUContext, float, RANK>; \
|
|
template struct PADDLE_API Transpose<XPUContext, int, RANK>; \
|
|
template struct PADDLE_API Transpose<XPUContext, int64_t, RANK>; \
|
|
template struct PADDLE_API Transpose<XPUContext, phi::complex64, RANK>;
|
|
|
|
DEFINE_XPU_TRANS(1);
|
|
DEFINE_XPU_TRANS(2);
|
|
DEFINE_XPU_TRANS(3);
|
|
DEFINE_XPU_TRANS(4);
|
|
DEFINE_XPU_TRANS(5);
|
|
DEFINE_XPU_TRANS(6);
|
|
#endif // PADDLE_WITH_XPU
|
|
|
|
template <typename DeviceContext, typename T>
|
|
void TransposeNormal<DeviceContext, T>::operator()(
|
|
const DeviceContext& dev_ctx UNUSED,
|
|
const DenseTensor& in,
|
|
DenseTensor* out,
|
|
const std::vector<int>& axis) {
|
|
const int rank = static_cast<const int>(axis.size());
|
|
auto in_stride = common::stride(in.dims());
|
|
auto out_stride = common::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];
|
|
}
|
|
};
|
|
transpose_helper(0, out->numel());
|
|
}
|
|
|
|
// define transpose normal
|
|
#define DEFINE_CPU_TRANS_NORMAL(TYPE) \
|
|
template struct TransposeNormal<CPUContext, TYPE>
|
|
DEFINE_CPU_TRANS_NORMAL(phi::float8_e4m3fn);
|
|
DEFINE_CPU_TRANS_NORMAL(phi::float8_e5m2);
|
|
DEFINE_CPU_TRANS_NORMAL(phi::float16);
|
|
DEFINE_CPU_TRANS_NORMAL(phi::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(uint16_t);
|
|
DEFINE_CPU_TRANS_NORMAL(uint32_t);
|
|
DEFINE_CPU_TRANS_NORMAL(uint64_t);
|
|
DEFINE_CPU_TRANS_NORMAL(int8_t);
|
|
DEFINE_CPU_TRANS_NORMAL(phi::complex64);
|
|
DEFINE_CPU_TRANS_NORMAL(phi::complex128);
|
|
|
|
#ifdef PADDLE_WITH_XPU
|
|
#define DEFINE_XPU_TRANS_NORMAL(TYPE) \
|
|
template struct TransposeNormal<XPUContext, TYPE>
|
|
DEFINE_XPU_TRANS_NORMAL(bool);
|
|
DEFINE_XPU_TRANS_NORMAL(float);
|
|
DEFINE_XPU_TRANS_NORMAL(int);
|
|
DEFINE_XPU_TRANS_NORMAL(int64_t);
|
|
DEFINE_XPU_TRANS_NORMAL(phi::complex64);
|
|
#endif // PADDLE_WITH_XPU
|
|
|
|
struct TensorSetConstantCPU {
|
|
TensorSetConstantCPU(DenseTensor* tensor, float value)
|
|
: tensor_(tensor), value_(value) {}
|
|
template <typename T>
|
|
void apply() const {
|
|
auto cpu = CPUPlace();
|
|
auto* begin = tensor_->mutable_data<T>(cpu);
|
|
std::fill(begin, begin + tensor_->numel(), static_cast<T>(value_));
|
|
}
|
|
DenseTensor* tensor_;
|
|
float value_;
|
|
};
|
|
|
|
template <>
|
|
void set_constant_with_place<XPUPlace>(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value) {
|
|
#ifdef PADDLE_WITH_XPU
|
|
phi::VisitDataType(
|
|
tensor->dtype(),
|
|
TensorSetConstantXPU<float>(tensor, value, tensor->place()));
|
|
#else
|
|
PADDLE_THROW(common::errors::PreconditionNotMet("Not compiled with XPU!"));
|
|
#endif
|
|
}
|
|
|
|
template <>
|
|
void set_constant_with_place<phi::IPUPlace>(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value) {
|
|
PADDLE_THROW(common::errors::Unimplemented("IPUPlace is not supported"));
|
|
}
|
|
|
|
template <>
|
|
void set_constant_with_place<CustomPlace>(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value) {
|
|
#ifdef PADDLE_WITH_CUSTOM_DEVICE
|
|
auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError(
|
|
"full",
|
|
{paddle::experimental::ParseBackend(tensor->place()),
|
|
DataLayout::ALL_LAYOUT,
|
|
paddle::experimental::ParseDataType(tensor->dtype())});
|
|
const auto& kernel = kernel_result.kernel;
|
|
using kernel_signature = void (*)(const DeviceContext&,
|
|
const phi::IntArray&,
|
|
const phi::Scalar&,
|
|
DataType,
|
|
DenseTensor*);
|
|
auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>();
|
|
(*kernel_fn)(dev_ctx,
|
|
phi::IntArray(vectorize(tensor->dims())),
|
|
phi::Scalar(value),
|
|
tensor->dtype(),
|
|
tensor);
|
|
#else
|
|
PADDLE_THROW(common::errors::Unimplemented("CustomPlace is not supported"));
|
|
#endif
|
|
}
|
|
|
|
template <>
|
|
void set_constant_with_place<CPUPlace>(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value) {
|
|
phi::VisitDataType(tensor->dtype(), TensorSetConstantCPU(tensor, value));
|
|
}
|
|
|
|
template <>
|
|
void set_constant_with_place<phi::GPUPinnedPlace>(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value) {
|
|
phi::VisitDataType(tensor->dtype(), TensorSetConstantCPU(tensor, value));
|
|
}
|
|
|
|
struct TensorSetConstantWithPlace {
|
|
using argument_type = phi::Place;
|
|
using result_type = void;
|
|
TensorSetConstantWithPlace(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value)
|
|
: dev_ctx_(dev_ctx), tensor_(tensor), value_(value) {}
|
|
|
|
template <typename Place>
|
|
void operator()(Place place UNUSED) const {
|
|
set_constant_with_place<Place>(dev_ctx_, tensor_, value_);
|
|
}
|
|
|
|
const DeviceContext& dev_ctx_;
|
|
DenseTensor* tensor_;
|
|
float value_;
|
|
};
|
|
|
|
void set_constant(const DeviceContext& dev_ctx,
|
|
DenseTensor* tensor,
|
|
float value) {
|
|
TensorSetConstantWithPlace func(dev_ctx, tensor, value);
|
|
#ifdef PADDLE_WITH_CUSTOM_DEVICE
|
|
if (dev_ctx.GetPlace().GetType() == AllocationType::CUSTOM) {
|
|
func(CustomPlace());
|
|
return;
|
|
}
|
|
#endif
|
|
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
|
|
// tensor->place().apply_visitor(func);
|
|
phi::VisitPlace(tensor->place(), func);
|
|
#elif defined(PADDLE_WITH_XPU)
|
|
if (dev_ctx.GetPlace().GetType() == AllocationType::XPU) {
|
|
func(XPUPlace());
|
|
return;
|
|
} else {
|
|
func(CPUPlace());
|
|
}
|
|
#else
|
|
func(CPUPlace());
|
|
#endif
|
|
}
|
|
|
|
template struct ColwiseSum<CPUContext, float>;
|
|
template struct ColwiseSum<CPUContext, double>;
|
|
template struct ColwiseSum<CPUContext, int>;
|
|
template struct ColwiseSum<CPUContext, int64_t>;
|
|
|
|
template struct RowwiseMean<CPUContext, float>;
|
|
template struct RowwiseMean<CPUContext, double>;
|
|
|
|
template <typename T>
|
|
struct RowwiseAdd<CPUContext, T> {
|
|
void operator()(const CPUContext& dev_ctx UNUSED,
|
|
const DenseTensor& input,
|
|
const DenseTensor& vector,
|
|
DenseTensor* output) {
|
|
auto in_dims = input.dims();
|
|
const auto& out_dims = output->dims();
|
|
auto size = input.numel() / in_dims[0];
|
|
PADDLE_ENFORCE_EQ(
|
|
vector.numel(),
|
|
size,
|
|
common::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()));
|
|
PADDLE_ENFORCE_EQ(out_dims,
|
|
in_dims,
|
|
common::errors::InvalidArgument(
|
|
"The output tensor shape should be same as the input"
|
|
" tensor shape. Expected output tensor shape: %s,"
|
|
" but received %s",
|
|
in_dims.to_str().c_str(),
|
|
out_dims.to_str().c_str()));
|
|
|
|
auto in = EigenMatrix<T>::From(input);
|
|
auto vec = EigenVector<T>::Flatten(vector);
|
|
auto out = EigenMatrix<T>::From(*output);
|
|
|
|
for (int64_t i = 0; i < in_dims[0]; ++i) {
|
|
out.chip(i, 0) = in.chip(i, 0) + vec;
|
|
}
|
|
}
|
|
};
|
|
|
|
template struct RowwiseAdd<CPUContext, float>;
|
|
template struct RowwiseAdd<CPUContext, double>;
|
|
|
|
} // namespace phi::funcs
|