/** * \brief An serial of structs and functions to store and parse attributes of operations * The member of variables of returned struct are guaranteed to be initialized * \author Ryan Han, Yuhan Ma, Nan Zhou * \copyright 2019 Kneron Inc. All right reserved. */ #ifndef PIANO_DYNASTY_INCLUDE_PARSER_ATTRIBUTEPARSER_H_ #define PIANO_DYNASTY_INCLUDE_PARSER_ATTRIBUTEPARSER_H_ #include #include "graph/node.h" #include "graph/node_list.h" #include "utils.h" namespace dynasty { namespace parser { using namespace dynasty::common; /** * @brief The basic attributes of an operation. Children structs could extend the base class to add special attributes if * necessary. * @note a operation will always have a single output and potential multiple inputs, * all parse function should initialize the basic attributes */ struct Attribute { std::vector in_bchws_; std::vector in_pixel_sizes_; BCHW out_bchw_; uint32_t out_pixel_size_; explicit Attribute(const Node *n); }; Attribute parse(const Node *node); struct ArgMaxAttribute : Attribute { int axis_ = 0; int keepdims_ = 1; int select_last_index_ = 0; explicit ArgMaxAttribute(const ArgMaxNode *n); explicit ArgMaxAttribute(const ArgMinNode *n); }; ArgMaxAttribute parse(const ArgMaxNode *n); using ArgMinAttribute = ArgMaxAttribute; ArgMinAttribute parse(const ArgMinNode *n); // This is to calculate the space for broadcast if needed struct ExpandAttribute : Attribute { uint32_t broadcast_first_size; // broadcast first matrix size explicit ExpandAttribute(const ExpandNode *n); }; ExpandAttribute parse(const ExpandNode *n); struct BatchNormalizationAttribute : Attribute { float epsilon_; // value to avoid 0 division float momentum_; // variable factor explicit BatchNormalizationAttribute(const BatchNormalizationNode *n); }; BatchNormalizationAttribute parse(const BatchNormalizationNode *n); struct BitShiftAttribute : Attribute { uint32_t broadcast_first_size; // broadcast first matrix size std::string direction_; // direction to shift bits explicit BitShiftAttribute(const BitShiftNode *n); }; BitShiftAttribute parse(const BitShiftNode *n); struct CastAttribute : Attribute { int to_; // data type to cast to explicit CastAttribute(const CastNode *n); }; CastAttribute parse(const CastNode *n); struct ClipAttribute : Attribute { float max_; // upper bound of interval float min_; // lower bound of interval explicit ClipAttribute(const ClipNode *n); }; ClipAttribute parse(const ClipNode *n); struct ConvAttribute : Attribute { std::string pad_type_; std::vector dilations_; std::vector kernel_; std::vector pads_; std::vector strides_; uint32_t group_; uint32_t stride_h_; uint32_t stride_w_; uint32_t out_top_pad_; uint32_t out_bottom_pad_; uint32_t out_left_pad_; uint32_t out_right_pad_; explicit ConvAttribute(const ConvNode *n); explicit ConvAttribute(const ConvIntegerNode *n); explicit ConvAttribute(const ConvTransposeNode *n); }; ConvAttribute parse(const ConvNode *n); using ConvTransposeAttribute = ConvAttribute; ConvTransposeAttribute parse(const ConvTransposeNode *n); using ConvIntegerAttribute = ConvAttribute; ConvIntegerAttribute parse(const ConvIntegerNode *n); struct KneronUpsampleZeroFillAttribute : Attribute { std::vector upsample_scale; explicit KneronUpsampleZeroFillAttribute(const KneronUpsampleZeroFillNode *n); }; KneronUpsampleZeroFillAttribute parse(const KneronUpsampleZeroFillNode *n); struct KneronChannelSplitAttribute : Attribute { std::string mode; explicit KneronChannelSplitAttribute(const KneronChannelSplitNode *n); }; KneronChannelSplitAttribute parse(const KneronChannelSplitNode *n); struct KneronChannelShuffleAttribute : Attribute { int group; std::string mode; explicit KneronChannelShuffleAttribute(const KneronChannelShuffleNode *n); }; KneronChannelShuffleAttribute parse(const KneronChannelShuffleNode *n); struct ConcatAttribute : Attribute { int axis_; // axis to concatenate on explicit ConcatAttribute(const ConcatNode *n); }; ConcatAttribute parse(const ConcatNode *n); struct CompressAttribute : Attribute { int axis_; // axis to concatenate on bool axis_set_; explicit CompressAttribute(const CompressNode *n); }; CompressAttribute parse(const CompressNode *n); struct CumSumAttribute : Attribute { int exclusive_; int reverse_; explicit CumSumAttribute(const CumSumNode *n); }; CumSumAttribute parse(const CumSumNode *n); struct CenterCropPadAttribute : Attribute { std::vector axes_; explicit CenterCropPadAttribute(const CenterCropPadNode *n); }; CenterCropPadAttribute parse(const CenterCropPadNode *n); struct DepthToSpaceAttribute : Attribute { uint32_t blocksize_; // size of blocks to be moved std::string mode_; // type of rearrangement explicit DepthToSpaceAttribute(const DepthToSpaceNode *n); explicit DepthToSpaceAttribute(const SpaceToDepthNode *n); }; DepthToSpaceAttribute parse(const DepthToSpaceNode *n); using SpaceToDepthAttribute = DepthToSpaceAttribute; //SpaceToDepthAttribute parse(const SpaceToDepthNode *n); struct DequantizeLinearAttribute : Attribute { int axis_; explicit DequantizeLinearAttribute(const DequantizeLinearNode *n); }; DequantizeLinearAttribute parse(const DequantizeLinearNode *n); struct GroupNormalizationAttribute : Attribute { // int axis_; float epsilon_; int num_groups_; explicit GroupNormalizationAttribute(const GroupNormalizationNode *n); }; GroupNormalizationAttribute parse(const GroupNormalizationNode *n); struct EyeLikeAttribute : Attribute { // int axis_; float dtype_; int k_; explicit EyeLikeAttribute(const EyeLikeNode *n); }; EyeLikeAttribute parse(const EyeLikeNode *n); struct EluAttribute : Attribute { float alpha_; // coefficient for function explicit EluAttribute(const LeakyReluNode *n); explicit EluAttribute(const EluNode *n); explicit EluAttribute(const ThresholdedReluNode *n); explicit EluAttribute(const CeluNode *n); }; EluAttribute parse(EluNode *n); using CeluAttribute = EluAttribute; CeluAttribute parse(const CeluNode *n); using LeakyReluAttribute = EluAttribute; LeakyReluAttribute parse(const LeakyReluNode *n); using ThresholdedReluAttribute = EluAttribute; ThresholdedReluAttribute parse(const ThresholdedReluNode *n); struct EinsumAttribute: Attribute{ string equation_; explicit EinsumAttribute(const EinsumNode* n) : Attribute(n), equation_(n->equation()) {} }; struct GatherAttribute : Attribute { int axis_; // axis to gather on explicit GatherAttribute(const GatherNode *n); explicit GatherAttribute(const GatherElementsNode *n); }; GatherAttribute parse(GatherNode *n); using GatherElementsAttribute = GatherAttribute; GatherElementsAttribute parse(const GatherElementsNode *n); struct GatherNDAttribute : Attribute { int batch_dims_; // axis to gather on explicit GatherNDAttribute(const GatherNDNode *n); }; GatherNDAttribute parse(GatherNDNode *n); struct GemmAttribute : Attribute { float alpha_; float beta_; int transA_; int transB_; explicit GemmAttribute(const GemmNode *n); }; GemmAttribute parse(const GemmNode *n); struct GridSampleAttribute : Attribute { int align_corners_; std::string mode_; std::string padding_mode_; explicit GridSampleAttribute(const GridSampleNode *n); }; GridSampleAttribute parse(const GridSampleNode *n); struct HardSigmoidAttribute : Attribute { float alpha_; // coefficient float beta_; // coefficient explicit HardSigmoidAttribute(const HardSigmoidNode *n); }; HardSigmoidAttribute parse(const HardSigmoidNode *n); struct InstanceNormalizationAttribute : Attribute { float epsilon_; // value to avoid 0 division explicit InstanceNormalizationAttribute(const InstanceNormalizationNode *n); }; InstanceNormalizationAttribute parse(const InstanceNormalizationNode *n); struct IsInfAttribute : Attribute { int detect_negative_; int detect_positive_; explicit IsInfAttribute(const IsInfNode *n); }; IsInfAttribute parse(const IsInfNode *n); struct NegativeLogLikelihoodLossAttribute : Attribute { int ignore_index_; string reduction_; explicit NegativeLogLikelihoodLossAttribute(const NegativeLogLikelihoodLossNode *n); }; NegativeLogLikelihoodLossAttribute parse(const NegativeLogLikelihoodLossNode *n); struct LpNormAttribute : Attribute { int axis_; // axis to apply normalization int p_; // order of normalization explicit LpNormAttribute(const GlobalLpPoolNode *n); explicit LpNormAttribute(const LpNormalizationNode *n); }; using GlobalLpPoolAttribute = LpNormAttribute; LpNormAttribute parse(const GlobalLpPoolNode *n); using LpNormalizationAttribute = LpNormAttribute; LpNormalizationAttribute parse(const LpNormalizationNode *n); struct KneronDownSampleAttribute : Attribute { uint32_t top_pad_; uint32_t bottom_pad_; uint32_t left_pad_; uint32_t right_pad_; uint32_t stride_h_; uint32_t stride_w_; Shape offsets_; explicit KneronDownSampleAttribute(const KneronDownSampleNode *n); }; KneronDownSampleAttribute parse(const KneronDownSampleNode *n); struct KneronRollAttribute : Attribute { Shape axes_; Shape shifts_; explicit KneronRollAttribute(const KneronRollNode *n); }; KneronRollAttribute parse(const KneronRollNode *n); struct KneronSpaceToDepthAttribute : Attribute { int blocksize_; string mode_; explicit KneronSpaceToDepthAttribute(const KneronSpaceToDepthNode *n); }; KneronSpaceToDepthAttribute parse(const KneronSpaceToDepthNode *n); struct LpPoolAttribute : Attribute { string auto_pad_; int ceil_mode_; std::vector dilations_; std::vector kernel_shape_; int p_; std::vector pads_; std::vector strides_; explicit LpPoolAttribute(const LpPoolNode *n); }; LpPoolAttribute parse(const LpPoolNode *n); struct LayerNormalizationAttribute : Attribute { int axis_; float epsilon_; int stash_type_; explicit LayerNormalizationAttribute(const LayerNormalizationNode *n); }; LayerNormalizationAttribute parse(const LayerNormalizationNode *n); struct MelWeightMatrixAttribute : Attribute { int output_datatype_ ; explicit MelWeightMatrixAttribute(const MelWeightMatrixNode *n); }; MelWeightMatrixAttribute parse(const MelWeightMatrixNode *n); struct MeanVarianceNormalizationAttribute : Attribute { std::vector axes_; explicit MeanVarianceNormalizationAttribute(const MeanVarianceNormalizationNode *n); }; MeanVarianceNormalizationAttribute parse(const MeanVarianceNormalizationNode *n); struct LRNAttribute : Attribute { float alpha_; // scaling parameter float beta_; // exponent float bias_; // bias int size_; // number of local channels explicit LRNAttribute(const LRNNode *n); }; LRNAttribute parse(const LRNNode *n); struct MaxRoiPoolAttribute : Attribute { uint32_t pooled_shape_h; // output height uint32_t pooled_shape_w; // output width float spatial_scale_; // coordinate scaling Shape roi_; explicit MaxRoiPoolAttribute(const MaxRoiPoolNode *n); }; MaxRoiPoolAttribute parse(const MaxRoiPoolNode *n); struct KneronMaxRoiPoolAttribute : Attribute { float spatial_scale_; // coordinate scaling Shape roi_; Shape roi_kernel_; explicit KneronMaxRoiPoolAttribute(const KneronMaxRoiPoolNode *n); }; KneronMaxRoiPoolAttribute parse(const KneronMaxRoiPoolNode *n); struct MatMulAttribute : Attribute { uint32_t broadcast_first_size; // broadcast first matrix size uint32_t broadcast_second_size; // broadcast first matrix size Shape first_dim; Shape second_dim; Shape broadcast_first_dim; Shape broadcast_second_dim; uint32_t submatrices; explicit MatMulAttribute(const MatMulNode *n); }; MatMulAttribute parse(const MatMulNode *n); struct MysteryAttribute : Attribute { std::string op_type_; int attr_num_; std::vector attr_name_; std::vector attr_type_; std::vector attr_size_; std::vector attr_value_; explicit MysteryAttribute(const MysteryNode *n); }; MysteryAttribute parse(const MysteryNode *n); struct MultinomialAttribute : Attribute { int sample_size_; // times to sample float seed_; // seed for generator explicit MultinomialAttribute(const MultinomialNode *n); }; MultinomialAttribute parse(const MultinomialNode *n); struct NonMaxSuppressionAttribute : Attribute { int center_point_box_; // format of box data explicit NonMaxSuppressionAttribute(const NonMaxSuppressionNode *n); }; NonMaxSuppressionAttribute parse(const NonMaxSuppressionNode *n); struct OneHotAttribute : Attribute { int axis_; // axis on which one-hot is added explicit OneHotAttribute(const HardmaxNode *n); explicit OneHotAttribute(const OneHotNode *n); }; OneHotAttribute parse(const OneHotNode *n); using HardmaxAttribute = OneHotAttribute; HardmaxAttribute parse(const HardmaxNode *n); struct PadAttribute : Attribute { Shape pads_; explicit PadAttribute(const PadNode *n); }; PadAttribute parse(const PadNode *n); struct PoolingAttribute : Attribute { uint32_t top_pad_; uint32_t bottom_pad_; uint32_t left_pad_; uint32_t right_pad_; uint32_t kernel_h_; uint32_t kernel_w_; uint32_t stride_h_; uint32_t stride_w_; std::vector pads_; std::vector kernels_; std::vector strides_; explicit PoolingAttribute(const MaxPoolNode *n); explicit PoolingAttribute(const MaxUnpoolNode *n); }; using MaxPoolAttribute = PoolingAttribute; MaxPoolAttribute parse(const MaxPoolNode *n); using MaxUnpoolAttribute = PoolingAttribute; MaxUnpoolAttribute parse(const MaxUnpoolNode *n); struct AveragePoolAttribute : Attribute { uint32_t top_pad_; uint32_t bottom_pad_; uint32_t left_pad_; uint32_t right_pad_; uint32_t kernel_h_; uint32_t kernel_w_; uint32_t stride_h_; uint32_t stride_w_; int32_t count_include_pad_; // flag to include padding explicit AveragePoolAttribute(const AveragePoolNode *n); }; AveragePoolAttribute parse(const AveragePoolNode *n); struct RandomUniformLikeAttribute : Attribute { float high_; // upper bound float low_; // lower bound float seed_; // seed for generator explicit RandomUniformLikeAttribute(const RandomUniformLikeNode *n); }; RandomUniformLikeAttribute parse(const RandomUniformLikeNode *n); struct ReduceAttribute : Attribute { Shape axes_; // axes to reduce on int keepdims_; // flag to keep dimensions int noop_with_empty_axes_; // added opset 18 explicit ReduceAttribute(const ReduceSumNode *n); explicit ReduceAttribute(const ReduceLogSumNode *n); explicit ReduceAttribute(const ReduceLogSumExpNode *n); explicit ReduceAttribute(const ReduceMeanNode *n); explicit ReduceAttribute(const ReduceMaxNode *n); explicit ReduceAttribute(const ReduceSumSquareNode *n); }; using ReduceSumAttribute =ReduceAttribute; ReduceSumAttribute parse(const ReduceSumNode *n); using ReduceLogSumAttribute =ReduceAttribute; ReduceLogSumAttribute parse(const ReduceLogSumNode *n); using ReduceLogSumExpAttribute = ReduceAttribute; ReduceLogSumExpAttribute parse(ReduceLogSumExpNode *n); using ReduceMeanAttribute = ReduceAttribute; ReduceMeanAttribute parse(const ReduceMeanNode *n); using ReduceMaxAttribute = ReduceAttribute; ReduceMaxAttribute parse(const ReduceMaxNode *n); using ReduceSumSquareAttribute = ReduceAttribute; ReduceSumSquareAttribute parse(const ReduceSumAttribute *n); struct ResizeAttribute : Attribute { std::string coordinate_transformation_mode_; float cubic_coeff_a_; int exclude_outside_; float extrapolation_value_; std::string mode_; std::string nearest_mode_; explicit ResizeAttribute(const ResizeNode *n); }; ResizeAttribute parse(const ResizeNode *n); struct RNNAttribute : Attribute { std::string direction_; // direction of RNN std::vector activation_alpha_; // scaling for activations std::vector activation_beta_; // scaling for activations std::vector activations_; // activation functions int linear_before_reset_ = 0; // flag to apply transformation before reset int input_forget_ = 0; // flag to couple input and forget gate int hidden_size_; // number of hidden neurons float clip_ = 0.0; // bounds explicit RNNAttribute(const GRUNode *n); explicit RNNAttribute(const LSTMNode *n); }; using GRUAttribute = RNNAttribute; RNNAttribute parse(const GRUNode *n); using LSTMAttribute = RNNAttribute; RNNAttribute parse(const LSTMNode *n); struct RoiAlignAttribute : Attribute { std::string mode_; // std::string coordinate_transformation_mode_; // int output_height_; // pooled output height int output_width_; // pooled output width int sampling_ratio_; // points in ROI to sample float spatial_scale_; // scale of feature map to image explicit RoiAlignAttribute(const RoiAlignNode *n); }; RoiAlignAttribute parse(const RoiAlignNode *n); struct SliceAttribute : Attribute { std::vector start_index_; std::vector end_index_; std::vector input_low_dim_size_; std::vector output_low_dim_size_; std::vector steps_; explicit SliceAttribute(const SliceNode *n); }; SliceAttribute parse(const SliceNode *n); struct SoftmaxAttribute : Attribute { int axis_; explicit SoftmaxAttribute(const SoftmaxNode *n); }; SoftmaxAttribute parse(const SoftmaxNode *n); struct LogSoftmaxAttribute : Attribute { int axis_; explicit LogSoftmaxAttribute(const LogSoftmaxNode *n); }; LogSoftmaxAttribute parse(const LogSoftmaxNode *n); struct TransposeAttribute : Attribute { Shape perm_; // permutation of axes explicit TransposeAttribute(const TransposeNode *n); }; TransposeAttribute parse(const TransposeNode *n); struct UpsampleAttribute : Attribute { string mode_; // permutation of axes explicit UpsampleAttribute(const UpsampleNode *n); }; UpsampleAttribute parse(UpsampleNode *n); struct LoopAttribute : Attribute { Graph body_; explicit LoopAttribute(const LoopNode *n); }; LoopAttribute parse(LoopNode *n); struct SeluAttribute : Attribute { float alpha_; // coefficient for function float gamma_; // coefficient for function explicit SeluAttribute(const SeluNode *n); }; SeluAttribute parse(const SeluNode *n); struct ShrinkAttribute : Attribute { float bias_; // coefficient for function float lambd_; // coefficient for function explicit ShrinkAttribute(const ShrinkNode *n); }; ShrinkAttribute parse(const ShrinkNode *n); struct ReverseSequenceAttribute : Attribute { uint32_t batch_axis_; uint32_t time_axis_; explicit ReverseSequenceAttribute(const ReverseSequenceNode *n); }; ReverseSequenceAttribute parse(const ReverseSequenceNode *n); #define CREATE_ATTRIBUTE(name) \ using name##Attribute = Attribute; \ name##Attribute inline parse(const name##Node *n) {return Attribute(n);} CREATE_ATTRIBUTE(Add) CREATE_ATTRIBUTE(Sub) CREATE_ATTRIBUTE(Mul) CREATE_ATTRIBUTE(Div) CREATE_ATTRIBUTE(Pow) CREATE_ATTRIBUTE(Det) CREATE_ATTRIBUTE(Dropout) CREATE_ATTRIBUTE(Exp) CREATE_ATTRIBUTE(Erf) CREATE_ATTRIBUTE(Equal) CREATE_ATTRIBUTE(Flatten) CREATE_ATTRIBUTE(Floor) CREATE_ATTRIBUTE(GlobalAveragePool) CREATE_ATTRIBUTE(GlobalMaxPool) CREATE_ATTRIBUTE(Greater) CREATE_ATTRIBUTE(GreaterOrEqual) CREATE_ATTRIBUTE(Max) CREATE_ATTRIBUTE(Mean) CREATE_ATTRIBUTE(Mish) CREATE_ATTRIBUTE(Mod) CREATE_ATTRIBUTE(MatMulInteger) CREATE_ATTRIBUTE(Neg) CREATE_ATTRIBUTE(NonZero) CREATE_ATTRIBUTE(Not) CREATE_ATTRIBUTE(PRelu) CREATE_ATTRIBUTE(Relu) CREATE_ATTRIBUTE(Reshape) CREATE_ATTRIBUTE(ScatterND) CREATE_ATTRIBUTE(Sigmoid) CREATE_ATTRIBUTE(Sqrt) CREATE_ATTRIBUTE(KneronScale) CREATE_ATTRIBUTE(KneronSwish) CREATE_ATTRIBUTE(KneronHardSwish) CREATE_ATTRIBUTE(KneronLog2) CREATE_ATTRIBUTE(KneronLogExp) CREATE_ATTRIBUTE(KneronPow2) CREATE_ATTRIBUTE(KneronSquare) CREATE_ATTRIBUTE(Squeeze) CREATE_ATTRIBUTE(IsNaN) CREATE_ATTRIBUTE(Less) CREATE_ATTRIBUTE(LessOrEqual) CREATE_ATTRIBUTE(Abs) CREATE_ATTRIBUTE(And) CREATE_ATTRIBUTE(Cos) CREATE_ATTRIBUTE(Cosh) CREATE_ATTRIBUTE(Acos) CREATE_ATTRIBUTE(Asin) CREATE_ATTRIBUTE(Atan) CREATE_ATTRIBUTE(Acosh) CREATE_ATTRIBUTE(Asinh) CREATE_ATTRIBUTE(Atanh) CREATE_ATTRIBUTE(BitwiseAnd) CREATE_ATTRIBUTE(BitwiseOr) CREATE_ATTRIBUTE(BitwiseXor) CREATE_ATTRIBUTE(BitwiseNot) CREATE_ATTRIBUTE(Ceil) CREATE_ATTRIBUTE(Softplus) CREATE_ATTRIBUTE(Softsign) CREATE_ATTRIBUTE(Log) CREATE_ATTRIBUTE(Tanh) CREATE_ATTRIBUTE(Tile) CREATE_ATTRIBUTE(Unsqueeze) CREATE_ATTRIBUTE(Min) CREATE_ATTRIBUTE(Or) CREATE_ATTRIBUTE(Identity) CREATE_ATTRIBUTE(Reciprocal) CREATE_ATTRIBUTE(KneronInvsqrt) } } #endif // PIANO_DYNASTY_INCLUDE_PARSER_ATTRIBUTEPARSER_H_