conv2d.cc 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2018 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/inference/anakin/convert/conv2d.h"
#include <algorithm>
#include <memory>
#include <vector>
19
#include "paddle/fluid/inference/anakin/convert/helper.h"
20

21
using anakin::PTuple;
22 23 24 25 26

namespace paddle {
namespace inference {
namespace anakin {

27 28
template <typename TargetT, ::anakin::Precision PrecisionT>
void Conv2dOpConverter<TargetT, PrecisionT>::operator()(
29 30
    const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc,
    const framework::Scope &scope, bool test_mode) {
31 32 33 34 35 36 37 38
  framework::OpDesc op_desc(op, nullptr);
  PADDLE_ENFORCE_EQ(op_desc.Input("Input").size(), 1UL);
  PADDLE_ENFORCE_EQ(op_desc.Input("Filter").size(), 1UL);
  PADDLE_ENFORCE_EQ(op_desc.Output("Output").size(), 1UL);

  auto input_name = op_desc.Input("Input").front();
  auto output_name = op_desc.Output("Output").front();
  auto op_name = op_desc.Type() + ":" + op_desc.Output("Output").front();
39
  this->engine_->AddOp(op_name, "Convolution", {input_name}, {output_name});
40 41 42

  auto *filter_v = scope.FindVar(op_desc.Input("Filter").front());
  PADDLE_ENFORCE_NOT_NULL(filter_v);
43 44
  auto weight_tensor = tensor_from_var(*filter_v, platform::CPUPlace());
  auto weight_shape = framework::vectorize2int(weight_tensor->dims());
45 46 47 48 49

  PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL);

  const int filter_h = weight_tensor->dims()[2];
  const int filter_w = weight_tensor->dims()[3];
50

51
  auto filter_num = weight_tensor->dims()[0];
52 53 54
  this->engine_->template AddOpAttr<int>(op_name, "filter_num", filter_num);
  this->engine_->template AddOpAttr<PTuple<int>>(op_name, "kernel_size",
                                                 {filter_h, filter_w});
55
  auto strides = boost::get<std::vector<int>>(op_desc.GetAttr("strides"));
56
  this->engine_->template AddOpAttr<PTuple<int>>(op_name, "strides", strides);
57
  auto paddings = boost::get<std::vector<int>>(op_desc.GetAttr("paddings"));
58
  this->engine_->template AddOpAttr<PTuple<int>>(op_name, "padding", paddings);
59
  auto dilations = boost::get<std::vector<int>>(op_desc.GetAttr("dilations"));
60 61
  this->engine_->template AddOpAttr<PTuple<int>>(op_name, "dilation_rate",
                                                 dilations);
62
  const int groups = boost::get<int>(op_desc.GetAttr("groups"));
63 64 65
  this->engine_->AddOpAttr(op_name, "group", groups);
  this->engine_->AddOpAttr(op_name, "axis", 1);
  this->engine_->AddOpAttr(op_name, "bias_term", false);
66

67 68 69 70 71 72 73
  ::anakin::saber::Shape anakin_shape(weight_shape);
  bool enable_int8 = boost::get<bool>(op_desc.HasAttr("enable_int8"));

  if (enable_int8) {
    const float int8_range = 127.;
    float in_scale = boost::get<float>(op_desc.GetAttr("input_scale"));
    float weight_scale = boost::get<float>(op_desc.GetAttr("weight_scale"));
74 75 76
    PBlock<TargetT> *weight1 =
        new PBlock<TargetT>(anakin_shape, ::anakin::AK_INT8);
    this->engine_->RegistBlock(weight1);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    float *weight_data = weight_tensor->data<float>();
    std::vector<char> weight_int8;
    int weight_num = weight_tensor->numel();
    for (int i = 0; i < weight_tensor->numel(); i++) {
      bool is_valid_int8 =
          ((weight_data[i] >= -128) && (weight_data[i] <= 127));
      PADDLE_ENFORCE(is_valid_int8,
                     "We are in anakin subgraph int8 mode, the weight of conv "
                     "should be in range [-128, 127]");
      weight_int8.push_back(static_cast<char>(weight_data[i]));
    }
    memcpy(static_cast<void *>(weight1->h_tensor().mutable_data()),
           static_cast<void *>(weight_int8.data()), sizeof(char) * weight_num);
    weight1->d_tensor().set_shape(anakin_shape);
    weight1->d_tensor().copy_from(weight1->h_tensor());
    this->engine_->AddOpAttr(op_name, "weight_1", *weight1);
    this->engine_->Graph()->SetOpPrec(op_name, ::anakin::AK_INT8);
    this->engine_->Graph()->SetWeightsScale(op_name,
                                            {weight_scale / int8_range}, false);
    this->engine_->AddTensorScale(input_name, in_scale / int8_range);
  } else {
98 99
    auto *weight1 = pblock_from_tensor<TargetT, PrecisionT>(
        *weight_tensor, weight_shape, this->engine_);
100 101
    this->engine_->AddOpAttr(op_name, "weight_1", *weight1);
  }
102 103 104 105 106 107
}

}  // namespace anakin
}  // namespace inference
}  // namespace paddle

108
#ifdef PADDLE_WITH_CUDA
109 110 111 112 113 114 115 116
using conv2d_nv_fp32 =
    ::paddle::inference::anakin::Conv2dOpConverter<::anakin::saber::NV,
                                                   ::anakin::Precision::FP32>;
using conv2d_nv_int8 =
    ::paddle::inference::anakin::Conv2dOpConverter<::anakin::saber::NV,
                                                   ::anakin::Precision::INT8>;
REGISTER_CUDA_ANAKIN_OP_CONVERTER(conv2d, conv2d_nv_fp32);
REGISTER_CUDA_INT8_ANAKIN_OP_CONVERTER(conv2d, conv2d_nv_int8);
117
#endif
118 119 120 121 122 123 124 125 126

using conv2d_cpu_fp32 =
    ::paddle::inference::anakin::Conv2dOpConverter<::anakin::saber::X86,
                                                   ::anakin::Precision::FP32>;
using conv2d_cpu_int8 =
    ::paddle::inference::anakin::Conv2dOpConverter<::anakin::saber::X86,
                                                   ::anakin::Precision::INT8>;
REGISTER_CPU_ANAKIN_OP_CONVERTER(conv2d, conv2d_cpu_fp32);
REGISTER_CPU_INT8_ANAKIN_OP_CONVERTER(conv2d, conv2d_cpu_int8);