pool_compute.h 2.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2019 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.
#pragma once

#include <Eigen/Core>
17 18
#include "lite/backends/x86/math/math_function.h"
#include "lite/backends/x86/math/pooling.h"
Y
Yan Chunwei 已提交
19 20 21
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
#include "lite/core/types.h"
22
#include "lite/fluid/eigen.h"
Y
Yan Chunwei 已提交
23 24 25 26 27 28 29 30 31 32 33

namespace paddle {
namespace lite {
namespace kernels {
namespace x86 {

template <typename T>
class PoolCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
 public:
  using param_t = operators::PoolParam;
  void Run() override {
34
    auto& context = ctx_->As<X86Context>();
Y
Yan Chunwei 已提交
35 36 37 38 39 40 41 42 43
    auto& param = *param_.get_mutable<param_t>();
    if (param.global_pooling) {
      for (size_t i = 0; i < param.ksize.size(); ++i) {
        param.ksize[i] = static_cast<int>(param.x->dims()[i + 2]);
      }
    }
    switch (param.ksize.size()) {
      case 2: {
        if (param.pooling_type == "max") {
44 45 46
          paddle::lite::x86::math::Pool2dFunctor<
              lite::TargetType::kX86,
              paddle::lite::x86::math::MaxPool<T>,
Y
Yan Chunwei 已提交
47 48
              T>
              pool2d_forward;
49 50 51
          paddle::lite::x86::math::MaxPool<T> pool_process;
          pool2d_forward(context,
                         param.x,
Y
Yan Chunwei 已提交
52 53
                         param.ksize,
                         param.strides,
54
                         *param.paddings,
Y
Yan Chunwei 已提交
55 56 57
                         pool_process,
                         true,
                         false,
58
                         param.output);
Y
Yan Chunwei 已提交
59
        } else if (param.pooling_type == "avg") {
60 61 62
          paddle::lite::x86::math::Pool2dFunctor<
              lite::TargetType::kX86,
              paddle::lite::x86::math::AvgPool<T>,
Y
Yan Chunwei 已提交
63 64
              T>
              pool2d_forward;
65 66 67
          paddle::lite::x86::math::AvgPool<T> pool_process;
          pool2d_forward(context,
                         param.x,
Y
Yan Chunwei 已提交
68 69
                         param.ksize,
                         param.strides,
70
                         *param.paddings,
Y
Yan Chunwei 已提交
71 72 73
                         pool_process,
                         param.exclusive,
                         param.adaptive,
74
                         param.output);
Y
Yan Chunwei 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87
        }
      } break;
      case 3: {
      } break;
    }
  }
  virtual ~PoolCompute() = default;
};

}  // namespace x86
}  // namespace kernels
}  // namespace lite
}  // namespace paddle