/* 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. */ #pragma once #include #include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/math/fc.h" namespace paddle { namespace operators { enum { kFCMKLDNNFP32 = 1, kFCMKLDNNINT8 = 2 }; using Tensor = framework::Tensor; inline void FCOutputSize(const framework::DDim& in_dims, const framework::DDim& w_dims, std::vector& out_dims, // NOLINT int in_num_col_dims, bool padding_weights) { auto in_mat_dims = phi::flatten_to_2d(in_dims, in_num_col_dims); auto w_dims0 = padding_weights ? w_dims[0] - 4 : w_dims[0]; auto w_dims1 = padding_weights ? w_dims[1] - 4 : w_dims[1]; PADDLE_ENFORCE_EQ( in_mat_dims[1], w_dims0, platform::errors::InvalidArgument( "The input's second dimension and weight's first dimension is " "expected to be the same. But recieved input's second dimension is " "%d, input's shape is %s; weight's first dimension is %d, weight's " "shape is %s.", in_mat_dims[1], in_mat_dims, w_dims0, phi::make_ddim({w_dims0, w_dims1}))); out_dims.reserve(static_cast(in_num_col_dims + 1)); for (int i = 0; i < in_num_col_dims; ++i) { out_dims.push_back(in_dims[i]); } out_dims.push_back(w_dims1); } template class FCOpKernel : public framework::OpKernel { public: void Compute(const paddle::framework::ExecutionContext& ctx) const override { auto* input = ctx.Input("Input"); auto* w = ctx.Input("W"); auto* bias = ctx.Input("Bias"); auto* output = ctx.Output("Out"); int in_num_col_dims = ctx.Attr("in_num_col_dims"); bool with_relu = (ctx.Attr("activation_type") == "relu") ? true : false; auto w_dims = w->dims(); bool padding_weights = ctx.Attr("padding_weights"); std::vector output_dims; FCOutputSize(input->dims(), w_dims, output_dims, in_num_col_dims, padding_weights); output->Resize(phi::make_ddim(output_dims)); output->set_lod(input->lod()); auto out_dims = output->dims(); auto w_dims0 = padding_weights ? w_dims[0] - 4 : w_dims[0]; auto w_dims1 = padding_weights ? w_dims[1] - 4 : w_dims[1]; int M = phi::product(out_dims) / w_dims1; const T* input_data = input->data(); const T* w_data = w->data(); T* output_data = output->mutable_data(ctx.GetPlace()); auto& dev_ctx = ctx.template device_context(); math::FCFunctor fc; fc(dev_ctx, M, w_dims1, w_dims0, input_data, w_data, output_data, bias ? bias->data() : NULL, with_relu, padding_weights); } }; } // namespace operators } // namespace paddle