/* 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/fluid/operators/math/math_function.h" #ifdef PADDLE_WITH_MKLML #include "paddle/fluid/platform/dynload/mklml.h" #endif #ifdef PADDLE_USE_OPENBLAS #include #endif #include #include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/operators/math/math_function_impl.h" #include "paddle/fluid/platform/float16.h" namespace paddle { namespace operators { namespace math { using float16 = paddle::platform::float16; template struct SetConstant; template struct SetConstant; template struct SetConstant; template struct SetConstant; template struct SetConstant; template struct SetConstant; template struct SetConstant; #define DEFINE_CPU_TRANS(RANK) \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; \ template struct Transpose; DEFINE_CPU_TRANS(1); DEFINE_CPU_TRANS(2); DEFINE_CPU_TRANS(3); DEFINE_CPU_TRANS(4); DEFINE_CPU_TRANS(5); DEFINE_CPU_TRANS(6); struct TensorSetConstantCPU { TensorSetConstantCPU(framework::Tensor* tensor, float value) : tensor_(tensor), value_(value) {} template void apply() const { auto cpu = platform::CPUPlace(); auto* begin = tensor_->mutable_data(cpu); std::fill(begin, begin + tensor_->numel(), static_cast(value_)); } framework::Tensor* tensor_; float value_; }; template <> void set_constant_with_place( const platform::DeviceContext& context, framework::Tensor* tensor, float value) { framework::VisitDataType(framework::ToDataType(tensor->type()), TensorSetConstantCPU(tensor, value)); } template <> void set_constant_with_place( const platform::DeviceContext& context, framework::Tensor* tensor, float value) { framework::VisitDataType(framework::ToDataType(tensor->type()), TensorSetConstantCPU(tensor, value)); } struct TensorSetConstantWithPlace : public boost::static_visitor { TensorSetConstantWithPlace(const platform::DeviceContext& context, framework::Tensor* tensor, float value) : context_(context), tensor_(tensor), value_(value) {} template void operator()(Place place) const { set_constant_with_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) { TensorSetConstantWithPlace func(context, tensor, value); #ifdef PADDLE_WITH_CUDA tensor->place().apply_visitor(func); #else func(platform::CPUPlace()); #endif } template struct RowwiseAdd { void operator()(const platform::CPUDeviceContext& context, const framework::Tensor& input, const framework::Tensor& vector, framework::Tensor* output) { auto in_dims = input.dims(); auto size = input.numel() / in_dims[0]; PADDLE_ENFORCE_EQ(vector.numel(), size); PADDLE_ENFORCE_EQ(output->dims(), in_dims); auto in = framework::EigenMatrix::From(input); auto vec = framework::EigenVector::Flatten(vector); auto out = framework::EigenMatrix::From(*output); for (int64_t i = 0; i < in_dims[0]; ++i) { out.chip(i, 0) = in.chip(i, 0) + vec; } } }; template struct RowwiseAdd; template struct RowwiseAdd; template struct ColwiseSum; template struct ColwiseSum; template struct ColwiseSum; template struct ColwiseSum; template struct RowwiseSum; template struct RowwiseSum; template struct RowwiseMean; template struct RowwiseMean; } // namespace math } // namespace operators } // namespace paddle