math_function.cc 5.4 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "lite/backends/x86/math/math_function.h"
Y
Yan Chunwei 已提交
16 17

#ifdef PADDLE_WITH_MKLML
18
#include "lite/backends/x86/mklml.h"
Y
Yan Chunwei 已提交
19 20 21 22 23 24 25
#endif

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

#include <vector>
26
#include "lite/backends/x86/math/math_function_impl.h"
Y
Yan Chunwei 已提交
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
#include "lite/fluid/data_type.h"
#include "lite/fluid/float16.h"

namespace paddle {
namespace lite {
namespace x86 {
namespace math {

template struct SetConstant<lite::TargetType::kX86, lite::fluid::float16>;
template struct SetConstant<lite::TargetType::kX86, float>;
template struct SetConstant<lite::TargetType::kX86, double>;
template struct SetConstant<lite::TargetType::kX86, int>;
template struct SetConstant<lite::TargetType::kX86, int64_t>;
template struct SetConstant<lite::TargetType::kX86, bool>;
template struct SetConstant<lite::TargetType::kX86, uint8_t>;

#define DEFINE_CPU_TRANS(RANK)                                      \
  template struct Transpose<lite::TargetType::kX86,                 \
                            lite::fluid::float16,                   \
                            RANK>;                                  \
  template struct Transpose<lite::TargetType::kX86, float, RANK>;   \
  template struct Transpose<lite::TargetType::kX86, double, RANK>;  \
  template struct Transpose<lite::TargetType::kX86, int, RANK>;     \
  template struct Transpose<lite::TargetType::kX86, int64_t, RANK>; \
  template struct Transpose<lite::TargetType::kX86, bool, RANK>;    \
  template struct Transpose<lite::TargetType::kX86, int16_t, RANK>; \
  template struct Transpose<lite::TargetType::kX86, uint8_t, RANK>; \
  template struct Transpose<lite::TargetType::kX86, int8_t, 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);

struct TensorSetConstantCPU {
  TensorSetConstantCPU(lite::Tensor* tensor, float value)
      : tensor_(tensor), value_(value) {}
  template <typename T>
  void apply() const {
H
huzhiqiang 已提交
68
    auto* begin = tensor_->template mutable_data<T>(lite::TargetType::kX86);
Y
Yan Chunwei 已提交
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    std::fill(begin, begin + tensor_->numel(), static_cast<T>(value_));
  }
  lite::Tensor* tensor_;
  float value_;
};

template <>
void set_constant_with_place<lite::TargetType::kX86>(
    const lite::Context<lite::TargetType::kX86>& context,
    lite::Tensor* tensor,
    float value) {
  // lite::VisitDataType(tensor->type(), TensorSetConstantCPU(tensor, value));
  TensorSetConstantCPU(tensor, value).apply<float>();
}

// template <>
// void set_constant_with_place<platform::CUDAPinnedPlace>(
//    const platform::DeviceContext& context, framework::Tensor* tensor,
//    float value) {
//  framework::VisitDataType(tensor->type(), TensorSetConstantCPU(tensor,
//  value));
//}

template <lite::TargetType Target>
struct TensorSetConstantWithTarget /*: public boost::static_visitor<void>*/ {
  TensorSetConstantWithTarget(const lite::Context<Target>& context,
                              lite::Tensor* tensor,
                              float value)
      : context_(context), tensor_(tensor), value_(value) {}

  void operator()() const {
    set_constant_with_place<Target>(context_, tensor_, value_);
  }

  const lite::Context<Target>& context_;
  lite::Tensor* tensor_;
  float value_;
};

template <lite::TargetType Target>
void set_constant(const lite::Context<Target>& context,
                  lite::Tensor* tensor,
                  float value) {
  TensorSetConstantWithTarget<Target> func(context, tensor, value);
  func();
}

template <typename T>
struct RowwiseAdd<lite::TargetType::kX86, T> {
  void operator()(const lite::Context<lite::TargetType::kX86>& context,
                  const lite::Tensor& input,
                  const lite::Tensor& vector,
                  lite::Tensor* output) {
122
    const auto& in_dims = input.dims();
Y
Yan Chunwei 已提交
123 124 125 126
    auto size = input.numel() / in_dims[0];
    PADDLE_ENFORCE_EQ(vector.numel(), size);
    PADDLE_ENFORCE_EQ(output->dims(), in_dims);

127 128
    const T* input_data = input.data<T>();
    const T* vector_data = vector.data<T>();
H
huzhiqiang 已提交
129
    T* output_data = output->template mutable_data<T>();
Y
Yan Chunwei 已提交
130
    for (int64_t i = 0; i < in_dims[0]; ++i) {
131
      for (int64_t j = 0; j < size; ++j) {
132
        output_data[i * size + j] = input_data[i * size + j] + vector_data[j];
133
      }
Y
Yan Chunwei 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }
  }
};

template struct RowwiseAdd<lite::TargetType::kX86, float>;
template struct RowwiseAdd<lite::TargetType::kX86, double>;

template struct ColwiseSum<lite::TargetType::kX86, float>;
template struct ColwiseSum<lite::TargetType::kX86, double>;
template struct ColwiseSum<lite::TargetType::kX86, int>;
template struct ColwiseSum<lite::TargetType::kX86, int64_t>;

template struct RowwiseSum<lite::TargetType::kX86, float>;
template struct RowwiseSum<lite::TargetType::kX86, double>;

template struct RowwiseMean<lite::TargetType::kX86, float>;
template struct RowwiseMean<lite::TargetType::kX86, double>;

}  // namespace math
}  // namespace x86
}  // namespace lite
}  // namespace paddle