conv_direct.cc 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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.

#include "lite/kernels/arm/conv_direct.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

22 23 24 25 26 27 28 29
#ifdef LITE_WITH_PROFILE
template <>
void DirectConv<PRECISION(kFloat), PRECISION(kFloat)>::
    SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
  ch->kernel_func_name = kernel_func_name_;
}
#endif

30
template <>
T
TianXiaogang 已提交
31 32
void DirectConv<PRECISION(kFloat), PRECISION(kFloat)>::Run() {
  auto& param = this->Param<param_t>();
33
  auto& ctx = this->ctx_->template As<ARMContext>();
T
TianXiaogang 已提交
34
  // extend workspace
35 36 37 38 39 40 41 42
  if (param.strides[0] == 2) {
    ctx.ExtendWorkspace(
        lite::arm::math::conv3x3s2_direct_workspace_size(param, &ctx));
  } else {
    ctx.ExtendWorkspace(
        lite::arm::math::conv3x3s1_direct_workspace_size(param, &ctx));
  }

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 68 69 70 71 72
  const auto* i_data = param.x->data<float>();
  const auto* w_data = weights_.data<float>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  auto* o_data = param.output->mutable_data<float>();

  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

  int iw = x_dims[3];  // nchw
  int ih = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int oh = o_dims[2];
  int ow = o_dims[3];
  int oc = o_dims[1];
  if (param.strides[0] == 1) {
    lite::arm::math::conv_3x3s1_direct_fp32(i_data,
                                            o_data,
                                            bs,
                                            oc,
                                            oh,
                                            ow,
                                            ic,
                                            ih,
                                            iw,
                                            w_data,
                                            b_data,
                                            param,
                                            &ctx);
73 74 75
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_3x3s1_direct_fp32";
#endif
76 77 78 79 80 81 82 83 84 85 86 87 88 89
  } else {
    lite::arm::math::conv_3x3s2_direct_fp32(i_data,
                                            o_data,
                                            bs,
                                            oc,
                                            oh,
                                            ow,
                                            ic,
                                            ih,
                                            iw,
                                            w_data,
                                            b_data,
                                            param,
                                            &ctx);
90 91 92
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_3x3s2_direct_fp32";
#endif
93 94 95
  }
}

96 97 98 99 100 101 102 103
#ifdef LITE_WITH_PROFILE
template <>
void DirectConv<PRECISION(kInt8), PRECISION(kFloat)>::
    SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
  ch->kernel_func_name = kernel_func_name_;
}
#endif

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
template <>
void DirectConv<PRECISION(kInt8), PRECISION(kFloat)>::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->template As<ARMContext>();
  const auto* i_data = param.x->data<int8_t>();
  const auto* w_data = weights_.data<int8_t>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  if (flag_trans_bias_) {
    b_data = bias_.data<float>();
  }
  auto* o_data = param.output->mutable_data<float>();

  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

  int iw = x_dims[3];  // nchw
  int ih = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int oh = o_dims[2];
  int ow = o_dims[3];
  int oc = o_dims[1];
  if (param.strides[0] == 1) {
    lite::arm::math::conv_3x3s1_direct_int8(i_data,
                                            o_data,
                                            bs,
                                            oc,
                                            oh,
                                            ow,
                                            ic,
                                            ih,
                                            iw,
                                            w_data,
                                            b_data,
                                            param,
                                            &ctx,
                                            w_scale_.data());
142 143 144
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_3x3s1_direct_int8";
#endif
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  } else {
    lite::arm::math::conv_3x3s2_direct_int8(i_data,
                                            o_data,
                                            bs,
                                            oc,
                                            oh,
                                            ow,
                                            ic,
                                            ih,
                                            iw,
                                            w_data,
                                            b_data,
                                            param,
                                            &ctx,
                                            w_scale_.data());
160 161 162
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_3x3s2_direct_int8";
#endif
163 164 165
  }
}

166 167 168 169 170 171 172 173
#ifdef LITE_WITH_PROFILE
template <>
void DirectConv<PRECISION(kInt8), PRECISION(kInt8)>::
    SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
  ch->kernel_func_name = kernel_func_name_;
}
#endif

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
template <>
void DirectConv<PRECISION(kInt8), PRECISION(kInt8)>::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->template As<ARMContext>();
  const auto* i_data = param.x->data<int8_t>();
  const auto* w_data = weights_.data<int8_t>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  if (flag_trans_bias_) {
    b_data = bias_.data<float>();
  }
  auto* o_data = param.output->mutable_data<int8_t>();

  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

  int iw = x_dims[3];  // nchw
  int ih = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int oh = o_dims[2];
  int ow = o_dims[3];
  int oc = o_dims[1];
  if (param.strides[0] == 1) {
    lite::arm::math::conv_3x3s1_direct_int8(i_data,
                                            o_data,
                                            bs,
                                            oc,
                                            oh,
                                            ow,
                                            ic,
                                            ih,
                                            iw,
                                            w_data,
                                            b_data,
                                            param,
                                            &ctx,
                                            w_scale_.data());
212 213 214
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_3x3s1_direct_int8";
#endif
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  } else {
    lite::arm::math::conv_3x3s2_direct_int8(i_data,
                                            o_data,
                                            bs,
                                            oc,
                                            oh,
                                            ow,
                                            ic,
                                            ih,
                                            iw,
                                            w_data,
                                            b_data,
                                            param,
                                            &ctx,
                                            w_scale_.data());
230 231 232
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_3x3s2_direct_int8";
#endif
233 234 235 236 237 238 239
  }
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle