/* 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 "common/log.h" #include "common/type_define.h" #include "framework/lod_tensor.h" #include "framework/scope.h" #include "framework/tensor.h" #include "framework/variable.h" namespace paddle_mobile { namespace operators { using framework::Attribute; using framework::AttributeMap; using framework::LoDTensor; using framework::Scope; using framework::Tensor; using std::string; using std::vector; class OpParam { protected: template static T *InputFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Input", inputs, scope); } template static T *InputXFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("X", inputs, scope); } template static T *InputYFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Y", inputs, scope); } template static T *InputZFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Z", inputs, scope); } template static T *InputBiasFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Bias", inputs, scope); } template static T *InputVarianceFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Variance", inputs, scope); } template static T *InputMeanFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Mean", inputs, scope); } template static T *InputScaleFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Scale", inputs, scope); } template static T *InputImageFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Image", inputs, scope); } template static T *InputPriorBoxFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("PriorBox", inputs, scope); } template static T *InputPriorBoxVarFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("PriorBoxVar", inputs, scope); } // LoDTensor but now use Tensor template static T *InputTargetBoxFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("TargetBox", inputs, scope); } template static T *InputBBoxesFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("BBoxes", inputs, scope); } template static T *InputScoresFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Scores", inputs, scope); } template static T *InputShapeFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Shape", inputs, scope); } template static vector InputMultiFrom(const VariableNameMap &inputs, const Scope &scope) { return GetMultiVarValue("X", inputs, scope); } template static T *OutputFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("Output", outputs, scope); } template static T *OutFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("Out", outputs, scope); } template static T *OutputYFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("Y", outputs, scope); } template static T *OutputBoxesFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("Boxes", outputs, scope); } template static T *OutputBoxFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("OutputBox", outputs, scope); } template static T *OutputVariancesFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("Variances", outputs, scope); } template static T *MidOutFrom(const VariableNameMap &outputs, const Scope &scope) { return GetVarValue("MidOut", outputs, scope); } template static T *FilterFrom(const VariableNameMap &inputs, const Scope &scope) { return GetVarValue("Filter", inputs, scope); } template static const T GetAttr(const string &key, const AttributeMap &map) { return ((Attribute)map.at(key)).Get(); } template static T *GetVarValue(const string &key, const VariableNameMap &var_map, const Scope &scope) { PADDLE_MOBILE_ENFORCE(var_map.count(key) > 0, "%s is not contained in var_map", key.c_str()) auto var_vec = var_map.at(key); if (!var_vec.empty()) { auto var = scope.FindVar(var_vec[0]); return var->GetMutable(); } else { return nullptr; } } template static vector GetMultiVarValue(const string &key, const VariableNameMap &var_map, const Scope &scope) { auto var_vecs = var_map.at(key); assert(var_vecs.size() > 1); vector var_res; for (auto &var_vec : var_vecs) { auto var = scope.FindVar(var_vec); var_res.push_back(var->GetMutable()); } return var_res; } }; #ifdef CONV_OP class ConvParam : OpParam { public: ConvParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { filter_ = FilterFrom(inputs, scope); input_ = InputFrom(inputs, scope); output_ = OutputFrom(outputs, scope); strides_ = GetAttr>("strides", attrs); paddings_ = GetAttr>("paddings", attrs); dilations_ = GetAttr>("dilations", attrs); groups = GetAttr("groups", attrs); } const Tensor *Input() const { return input_; } const Tensor *Filter() const { return filter_; } Tensor *Output() const { return output_; } const vector &Strides() const { return strides_; } const vector &Paddings() const { return paddings_; } const vector &Dilations() const { return dilations_; } const int &Groups() const { return groups; } private: Tensor *input_; Tensor *output_; Tensor *filter_; vector strides_; vector paddings_; vector dilations_; int groups; }; Print &operator<<(Print &printer, const ConvParam &conv_param); #endif #ifdef ELEMENTWISEADD_OP class ElementwiseAddParam : OpParam { public: ElementwiseAddParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); input_y_ = InputYFrom(inputs, scope); out_ = OutFrom(outputs, scope); axis_ = GetAttr("axis", attrs); } const Tensor *InputX() const { return input_x_; } const Tensor *InputY() const { return input_y_; } Tensor *Out() const { return out_; } const int &Axis() const { return axis_; } private: Tensor *input_x_; Tensor *input_y_; Tensor *out_; int axis_; }; #endif #ifdef MUL_OP class MulParam : OpParam { public: MulParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); input_y_ = InputYFrom(inputs, scope); out_ = OutFrom(outputs, scope); x_num_col_dims_ = GetAttr("x_num_col_dims", attrs); y_num_col_dims_ = GetAttr("y_num_col_dims", attrs); } const Tensor *InputX() const { return input_x_; } const Tensor *InputY() const { return input_y_; } Tensor *Out() const { return out_; } const int &XNumColDims() const { return x_num_col_dims_; } const int &YNumColDims() const { return y_num_col_dims_; } private: Tensor *input_x_; Tensor *input_y_; Tensor *out_; int x_num_col_dims_; int y_num_col_dims_; }; #endif #ifdef CONCAT_OP class ConcatParam : public OpParam { public: ConcatParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { inputs_ = InputMultiFrom(inputs, scope); out_ = OutFrom(outputs, scope); axis_ = GetAttr("axis", attrs); } vector Inputs() const { return inputs_; } Tensor *Out() const { return out_; } const int &Axis() const { return axis_; } private: vector inputs_; Tensor *out_; int axis_; }; #endif #ifdef LRN_OP class LrnParam : public OpParam { public: LrnParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); mid_out_ = MidOutFrom(outputs, scope); n_ = GetAttr("n", attrs); alpha_ = GetAttr("alpha", attrs); beta_ = GetAttr("beta", attrs); k_ = GetAttr("k", attrs); data_format_ = GetAttr("data_format", attrs); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } Tensor *MidOut() const { return mid_out_; } const int &N() const { return n_; } const float &Alpha() const { return alpha_; } const float &Beta() const { return beta_; } const float &K() const { return k_; } const string &DataFormat() const { return data_format_; } private: Tensor *input_x_; Tensor *out_; Tensor *mid_out_; int n_; float alpha_; float beta_; float k_; string data_format_; }; #endif #ifdef BATCHNORM_OP class BatchNormParam : OpParam { public: BatchNormParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); output_y_ = OutputYFrom(outputs, scope); input_bias_ = InputBiasFrom(inputs, scope); input_mean_ = InputMeanFrom(inputs, scope); input_scale_ = InputScaleFrom(inputs, scope); input_variance_ = InputVarianceFrom(inputs, scope); epsilon_ = GetAttr("epsilon", attrs); momentum_ = GetAttr("momentum", attrs); is_test_ = GetAttr("is_test", attrs); } const Tensor *InputX() const { return input_x_; } Tensor *OutputY() const { return output_y_; } const Tensor *InputBias() const { return input_bias_; } const Tensor *InputMean() const { return input_mean_; } const Tensor *InputScale() const { return input_scale_; } const Tensor *InputVariance() const { return input_variance_; } const float &Epsilon() const { return epsilon_; } const float &Momentum() const { return momentum_; } const bool &IsTest() const { return is_test_; } const string &DataFormat() const { return data_format_; } private: Tensor *input_x_; Tensor *output_y_; Tensor *input_bias_; Tensor *input_mean_; Tensor *input_scale_; Tensor *input_variance_; float epsilon_; float momentum_; bool is_test_; string data_format_; }; #endif #ifdef POOL_OP class PoolParam : public OpParam { public: PoolParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_ = InputXFrom(inputs, scope); output_ = OutFrom(outputs, scope); pooling_type_ = GetAttr("pooling_type", attrs); ksize_ = GetAttr>("ksize", attrs); strides_ = GetAttr>("strides", attrs); paddings_ = GetAttr>("paddings", attrs); ceil_mode_ = GetAttr("ceil_mode", attrs); gloabal_pooling_ = GetAttr("global_pooling", attrs); } const Tensor *Input() const { return input_; } Tensor *Output() const { return output_; } const string &PoolingType() const { return pooling_type_; } const vector &Ksize() const { return ksize_; } const vector &Strides() const { return strides_; } const vector &Paddings() const { return paddings_; } bool isCeilMode() const { return ceil_mode_; } bool isGlobalPooling() const { return gloabal_pooling_; } private: Tensor *input_; Tensor *output_; string pooling_type_; vector ksize_; vector strides_; vector paddings_; bool ceil_mode_; bool gloabal_pooling_ = false; }; #endif #ifdef PRIORBOX_OP class PriorBoxParam : public OpParam { public: PriorBoxParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_ = InputFrom(inputs, scope); input_image_ = InputImageFrom(inputs, scope); output_boxes_ = OutputBoxesFrom(outputs, scope); output_variances_ = OutputVariancesFrom(outputs, scope); min_sizes_ = GetAttr>("min_sizes", attrs); max_sizes_ = GetAttr>("max_sizes", attrs); aspect_ratios_ = GetAttr>("aspect_ratios", attrs); variances_ = GetAttr>("variances", attrs); flip_ = GetAttr("flip", attrs); clip_ = GetAttr("clip", attrs); step_w_ = GetAttr("step_w", attrs); step_h_ = GetAttr("step_h", attrs); offset_ = GetAttr("offset", attrs); } const Tensor *Input() const { return input_; } const Tensor *InputImage() const { return input_image_; } Tensor *OutputBoxes() const { return output_boxes_; } Tensor *OutputVariances() const { return output_variances_; } const vector &MinSizes() const { return min_sizes_; } const vector &MaxSizes() const { return max_sizes_; } const vector &AspectRatios() const { return aspect_ratios_; } const vector &Variances() const { return variances_; } const bool &Flip() const { return flip_; } const bool &Clip() const { return clip_; } const float &StepW() const { return step_w_; } const float &StepH() const { return step_h_; } const float &Offset() const { return offset_; } private: Tensor *input_; Tensor *input_image_; Tensor *output_boxes_; Tensor *output_variances_; vector min_sizes_; vector max_sizes_; vector aspect_ratios_; vector variances_; bool flip_; bool clip_; float step_w_; float step_h_; float offset_; }; #endif #ifdef BOXCODER_OP class BoxCoderParam : public OpParam { public: BoxCoderParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_priorbox_ = InputPriorBoxFrom(inputs, scope); input_priorboxvar_ = InputPriorBoxVarFrom(inputs, scope); input_targetbox_ = InputTargetBoxFrom(inputs, scope); output_box_ = OutputBoxFrom(outputs, scope); code_type_ = GetAttr("code_type", attrs); } const Tensor *InputPriorBox() const { return input_priorbox_; } const Tensor *InputPriorBoxVar() const { return input_priorboxvar_; } const Tensor *InputTargetBox() const { return input_targetbox_; } Tensor *OutputBox() const { return output_box_; } const std::string &CodeType() const { return code_type_; } private: Tensor *input_priorbox_; Tensor *input_priorboxvar_; Tensor *input_targetbox_; Tensor *output_box_; std::string code_type_; }; #endif #ifdef SOFTMAX_OP class SoftmaxParam : public OpParam { public: SoftmaxParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } private: Tensor *input_x_; Tensor *out_; }; #endif #ifdef SIGMOID_OP class SigmoidParam : public OpParam { public: SigmoidParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } private: Tensor *input_x_; Tensor *out_; }; #endif #ifdef MULTICLASSNMS_OP class MultiClassNMSParam : public OpParam { public: MultiClassNMSParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) { input_bboxes_ = InputBBoxesFrom(inputs, scope); input_scores_ = InputScoresFrom(inputs, scope); out_ = OutFrom(outputs, scope); background_label_ = GetAttr("background_label", attrs); nms_top_k_ = GetAttr("nms_top_k", attrs); keep_top_k_ = GetAttr("keep_top_k", attrs); nms_threshold_ = GetAttr("nms_threshold", attrs); nms_eta_ = GetAttr("nms_eta", attrs); score_threshold_ = GetAttr("score_threshold", attrs); } const Tensor *InputBBoxes() const { return input_bboxes_; } const Tensor *InputScores() const { return input_scores_; } Tensor *Out() const { return out_; } const int &BackGroundLabel() const { return background_label_; } const int &NMSTopK() const { return nms_top_k_; } const int &KeepTopK() const { return keep_top_k_; } const float &NMSThreshold() const { return nms_threshold_; } const float &NMSEta() const { return nms_eta_; } const float &ScoreThreshold() const { return score_threshold_; } private: Tensor *input_bboxes_; Tensor *input_scores_; Tensor *out_; int background_label_; int nms_top_k_; int keep_top_k_; float nms_threshold_; float nms_eta_; float score_threshold_; }; #endif class FeedParam : public OpParam { public: FeedParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); auto var = scope.Var("batch_size"); batch_size = var->GetValue(); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } const int BatchSize() const { return batch_size; } private: Tensor *input_x_; Tensor *out_; int batch_size; }; class FetchParam : public OpParam { public: FetchParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const framework::AttributeMap &attrs, const framework::Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } private: Tensor *input_x_; Tensor *out_; }; #ifdef TRANSPOSE_OP class TransposeParam : public OpParam { public: TransposeParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); axis_ = GetAttr>("axis", attrs); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } const vector &Axis() const { return axis_; } private: Tensor *input_x_; Tensor *out_; vector axis_; }; #endif #ifdef RESHAPE_OP class ReshapeParam : public OpParam { public: ReshapeParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) { input_x_ = InputXFrom(inputs, scope); input_shape_ = InputShapeFrom(inputs, scope); out_ = OutFrom(outputs, scope); shape_ = GetAttr>("shape", attrs); inplace_ = GetAttr("inplace", attrs); } const Tensor *InputX() const { return input_x_; } const Tensor *InputShape() const { return input_shape_; } Tensor *Out() const { return out_; } const vector &Shape() const { return shape_; } const bool &Inplace() const { return inplace_; } private: Tensor *input_x_; Tensor *input_shape_; Tensor *out_; vector shape_; bool inplace_; }; #endif #ifdef RELU_OP /* * @b op 层实例化好这个 param 传递给 kernel 层使用 * */ class ReluParam : public OpParam { public: ReluParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) { input_x_ = InputXFrom(inputs, scope); out_ = OutFrom(outputs, scope); } const Tensor *InputX() const { return input_x_; } Tensor *Out() const { return out_; } private: Tensor *input_x_; Tensor *out_; }; #endif #ifdef FUSION_FC_OP class FusionFcParam : public OpParam { public: FusionFcParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) { input_x_ = InputXFrom(inputs, scope); input_y_ = InputYFrom(inputs, scope); input_z_ = InputZFrom(inputs, scope); out_ = OutFrom(outputs, scope); x_num_col_dims_ = GetAttr("x_num_col_dims", attrs); y_num_col_dims_ = GetAttr("y_num_col_dims", attrs); axis_ = GetAttr("axis", attrs); } const Tensor *InputX() const { return input_x_; } const Tensor *InputY() const { return input_y_; } const Tensor *InputZ() const { return input_z_; } Tensor *Out() const { return out_; } const int &XNumColDims() const { return x_num_col_dims_; } const int &YNumColDims() const { return y_num_col_dims_; } const int &Axis() const { return axis_; } private: Tensor *input_x_; Tensor *input_y_; Tensor *input_z_; Tensor *out_; int x_num_col_dims_; int y_num_col_dims_; int axis_; }; #endif #ifdef FUSION_CONVADD_OP class FusionConvAddParam : public OpParam { public: FusionConvAddParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) { bias_ = InputYFrom(inputs, scope); axis_ = GetAttr("axis", attrs); filter_ = FilterFrom(inputs, scope); input_ = InputFrom(inputs, scope); output_ = OutFrom(outputs, scope); strides_ = GetAttr>("strides", attrs); paddings_ = GetAttr>("paddings", attrs); dilations_ = GetAttr>("dilations", attrs); groups = GetAttr("groups", attrs); } Tensor *Bias() const { return bias_; } const int &Axis() const { return axis_; } const Tensor *Input() const { return input_; } const Tensor *Filter() const { return filter_; } Tensor *Output() const { return output_; } const vector &Strides() const { return strides_; } const vector &Paddings() const { return paddings_; } const vector &Dilations() const { return dilations_; } const int &Groups() const { return groups; } protected: Tensor *bias_; int axis_; Tensor *input_; Tensor *output_; Tensor *filter_; vector strides_; vector paddings_; vector dilations_; int groups; }; Print &operator<<(Print &printer, const FusionConvAddParam &conv_param); #endif #ifdef FUSION_CONVADD_RELU_OP class FusionConvAddReluParam : public FusionConvAddParam { public: FusionConvAddReluParam(const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs, const Scope &scope) : FusionConvAddParam(inputs, outputs, attrs, scope) {} }; #endif } // namespace operators } // namespace paddle_mobile