// 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_gemmlike.h" #include #include "lite/backends/arm/math/gemm_prepacked_int8.h" #include "lite/backends/arm/math/packed_sgemm.h" namespace paddle { namespace lite { namespace kernels { namespace arm { template <> void GemmLikeConv::PrepareForRun() { ReInitWhenNeeded(); } template <> void GemmLikeConv::PrepareForRun() { ReInitWhenNeeded(); auto& param = this->Param(); /// update scale w_scale_ = param.weight_scale; if (w_scale_.size() != 1 && w_scale_.size() != param.filter->dims()[0]) { LOG(FATAL) << "weights scale size must equal to filter size"; return; } if (w_scale_.size() == 1) { for (int i = 0; i < param.filter->dims()[0] - 1; ++i) { w_scale_.push_back(w_scale_[0]); } } float input_scale = param.input_scale; for (auto& ws : w_scale_) { ws *= input_scale; } } template <> void GemmLikeConv::PrepareForRun() { ReInitWhenNeeded(); auto& param = this->Param(); /// update scale /// update scale w_scale_ = param.weight_scale; if (w_scale_.size() != 1 && w_scale_.size() != param.filter->dims()[0]) { LOG(FATAL) << "weights scale size must equal to filter size"; return; } if (w_scale_.size() == 1) { for (int i = 0; i < param.filter->dims()[0] - 1; ++i) { w_scale_.push_back(w_scale_[0]); } } float input_scale = param.input_scale; float output_scale = param.output_scale; for (auto& ws : w_scale_) { ws = ws * input_scale / output_scale; } //! update bias if (param.bias) { bias_.Resize(param.bias->dims()); auto ptr = bias_.mutable_data(); auto ptr_in = param.bias->data(); for (int i = 0; i < bias_.numel(); ++i) { ptr[i] = ptr_in[i] / param.output_scale; } flag_trans_bias_ = true; } } template <> void GemmLikeConv::Run() { auto& param = this->Param(); auto& ctx = this->ctx_->template As(); auto weights = param.filter->data(); if (flag_trans_weights_) { weights = weights_.data(); } const float* bias = param.bias ? param.bias->data() : nullptr; if (flag_trans_bias_) { bias = bias_.data(); } auto din = param.x->data(); auto dout = param.output->mutable_data(); 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 (flag_1x1gemm_) { lite::arm::math::conv1x1s1_gemm( din, dout, bs, oc, oh, ow, ic, ih, iw, weights, bias, param, &ctx); } else { lite::arm::math::conv_im2col_gemm( din, dout, bs, oc, oh, ow, ic, ih, iw, weights, bias, param, &ctx); } } template <> void GemmLikeConv::Run() { auto& param = this->Param(); auto& ctx = this->ctx_->template As(); auto weights = param.filter->data(); if (flag_trans_weights_) { weights = weights_.data(); } auto bias = param.bias ? param.bias->data() : nullptr; if (flag_trans_bias_) { bias = bias_.data(); } auto din = param.x->data(); auto dout = param.output->mutable_data(); 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 (flag_1x1gemm_) { lite::arm::math::conv1x1s1_gemm_int8(din, dout, bs, oc, oh, ow, ic, ih, iw, weights, bias, param, &ctx, w_scale_.data()); } else { lite::arm::math::conv_im2col_gemm_int8(din, dout, bs, oc, oh, ow, ic, ih, iw, weights, bias, param, &ctx, w_scale_.data()); } } template <> void GemmLikeConv::Run() { auto& param = this->Param(); auto& ctx = this->ctx_->template As(); auto weights = param.filter->data(); if (flag_trans_weights_) { weights = weights_.data(); } auto bias = param.bias ? param.bias->data() : nullptr; if (flag_trans_bias_) { bias = bias_.data(); } auto din = param.x->data(); auto dout = param.output->mutable_data(); 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 (flag_1x1gemm_) { lite::arm::math::conv1x1s1_gemm_int8(din, dout, bs, oc, oh, ow, ic, ih, iw, weights, bias, param, &ctx, w_scale_.data()); } else { lite::arm::math::conv_im2col_gemm_int8(din, dout, bs, oc, oh, ow, ic, ih, iw, weights, bias, param, &ctx, w_scale_.data()); } } } // namespace arm } // namespace kernels } // namespace lite } // namespace paddle