1 //
2 // NeuralNetWorkOp.hpp
3 // MNN
4 //
5 // Created by MNN on 2019/06/27.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8
9 #ifndef NeuralNetWorkOp_HPP
10 #define NeuralNetWorkOp_HPP
11
12 namespace MNN {
13 namespace Express {
14 enum PaddingMode {CAFFE, VALID, SAME};
15 enum PoolingMode {MAXPOOL, AVEPOOL};
16 enum PadValueMode {CONSTANT, REFLECT, SYMMETRIC};
17 MNN_PUBLIC VARP _Input(INTS shape = {}, Dimensionformat data_format = NC4HW4, halide_type_t dtype = halide_type_of<float>()) ;
18 MNN_PUBLIC VARP _Clone(VARP source, bool deepCopy = false);
19
20 MNN_PUBLIC VARP _Scalar(const void* ptr, halide_type_t type);
21
22 template <typename T>
_Scalar(T value)23 VARP _Scalar(T value) {
24 return _Scalar(&value, halide_type_of<T>());
25 }
26
27
28 MNN_PUBLIC VARP _Const(float value, INTS shape = {}, Dimensionformat format = NHWC);
29 MNN_PUBLIC VARP _Const(const void* ptr, INTS shape = {}, Dimensionformat format = NHWC,
30 halide_type_t type = halide_type_of<float>());
31 MNN_PUBLIC VARP _TrainableParam(float value, INTS dims, Dimensionformat format);
32 MNN_PUBLIC VARP _TrainableParam(const void* ptr, INTS dims, Dimensionformat format,
33 halide_type_t type = halide_type_of<float>());
34 MNN_PUBLIC VARP _InnerProduct(std::vector<float>&& weight, std::vector<float>&& bias, VARP x, INTS outputShape);
35 MNN_PUBLIC VARP _Conv(VARP weight, VARP bias, VARP x, PaddingMode pad = VALID, INTS stride = {1, 1},
36 INTS dilate = {1, 1}, int group = 1, INTS pads = {0, 0});
37
38 MNN_PUBLIC VARP _Conv(float weight, float bias, VARP x, INTS channel, INTS kernelSize, PaddingMode pad = VALID,
39 INTS stride = {1, 1}, INTS dilate = {1, 1}, int group = 1);
40 MNN_PUBLIC VARP _Conv(std::vector<int8_t>&& weight, std::vector<float>&& bias, VARP x, INTS channel, INTS kernelSize,
41 PaddingMode pad = VALID, INTS stride = {1, 1}, INTS dilate = {1, 1}, int group = 1, INTS pads = {0, 0}, bool relu = false, bool relu6 = false, int nbits = 8);
42 MNN_PUBLIC VARP _Conv(std::vector<float>&& weight, std::vector<float>&& bias, VARP x, INTS channel, INTS kernelSize,
43 PaddingMode pad = VALID, INTS stride = {1, 1}, INTS dilate = {1, 1}, int group = 1, INTS pads = {0, 0}, bool relu = false, bool relu6 = false);
44 MNN_PUBLIC VARP _Deconv(VARP weight, VARP bias, VARP x, PaddingMode pad = VALID, INTS stride = {1, 1},
45 INTS dilate = {1, 1}, int group = 1, INTS pads = {0, 0});
46
47 MNN_PUBLIC VARP _Deconv(std::vector<float>&& weight, std::vector<float>&& bias, VARP x, INTS channel, INTS kernelSize,
48 PaddingMode pad, INTS stride = {1, 1}, INTS dilate = {1, 1}, int group = 1, INTS pads = {0, 0}, bool relu = false, bool relu6 = false);
49
50 MNN_PUBLIC VARP _MaxPool(VARP x, INTS kernel, INTS stride = {1, 1}, PaddingMode pad = VALID, INTS pads= {0, 0});
51 MNN_PUBLIC VARP _AvePool(VARP x, INTS kernel, INTS stride = {1, 1}, PaddingMode pad = VALID, INTS pads= {0, 0});
52 MNN_PUBLIC VARP _Reshape(VARP x, INTS shape, Dimensionformat original_format = NCHW);
53 MNN_PUBLIC VARP _Reshape(VARP x, VARP shape);
54 MNN_PUBLIC VARP _Scale(VARP x, int channels, std::vector<float>&& scales, std::vector<float>&& bias);
55
56 MNN_PUBLIC VARP _Relu(VARP x, float slope = 0.0f);
57 MNN_PUBLIC VARP _Relu6(VARP x, float minValue = 0.0f, float maxValue = 6.0f);
58 MNN_PUBLIC VARP _PRelu(VARP x, std::vector<float> &&slopes);
59 MNN_PUBLIC VARP _Softmax(VARP logits, int axis = -1);
60 MNN_PUBLIC VARP _Softplus(VARP features);
61 MNN_PUBLIC VARP _Softsign(VARP features);
62 MNN_PUBLIC std::vector<VARP> _Split(VARP value, INTS size_splits, int axis = 0);
63 MNN_PUBLIC VARP _Slice(VARP x, VARP starts, VARP sizes);
64 MNN_PUBLIC VARP _StridedSlice(VARP input, VARP begin, VARP end, VARP strided,
65 int32_t beginMask, int32_t endMask, int32_t ellipsisMask,
66 int32_t newAxisMask, int32_t shrinkAxisMask);
67 MNN_PUBLIC VARP _Concat(VARPS values, int axis);
68 MNN_PUBLIC VARP _Convert(VARP input, Dimensionformat format);
69 MNN_PUBLIC VARP _Transpose(VARP x, INTS perm);
70 MNN_PUBLIC VARP _Transpose(VARP x, VARP perm);
71 MNN_PUBLIC VARP _ChannelShuffle(VARP x, int group);
72 MNN_PUBLIC VARP _ChangeInputFormat(VARP input, Dimensionformat format);
73 MNN_PUBLIC VARP _Conv2DBackPropFilter(VARP input, VARP inputGrad, INTS kernelSize, PaddingMode pad = VALID, INTS stride = {1, 1}, INTS dilate = {1, 1}, int group = 1, INTS pads = {0, 0});
74 MNN_PUBLIC VARP _PoolGrad(VARP originInput, VARP originOutput, VARP inputGrad, INTS kernel, INTS stride, PoolingMode type, PaddingMode pad = VALID, INTS pads= {0, 0});
75 // FIXME: move the api to Array Ops
76 MNN_PUBLIC VARP _ReverseSequence(VARP x, VARP y, int batchDim, int seqDim);
77 // FIXME: move the api to Image Ops
78 MNN_PUBLIC VARP _Crop(VARP images, VARP size, int axis, INTS offset);
79 MNN_PUBLIC VARP _Resize(VARP images, float xScale, float yScale);
80 MNN_PUBLIC VARP _Pad(VARP x, VARP paddings, PadValueMode mode = CONSTANT);
81 MNN_PUBLIC VARP _ExpandDims(VARP input, int axis);
82 MNN_PUBLIC VARP _ExpandDims(VARP input, VARP axis);
83
84 MNN_PUBLIC VARP _Shape(VARP input, bool nchw = false);
85 MNN_PUBLIC VARP _Stack(VARPS values, int axis=0);
86 enum InterpolationMethod {BILINEAR, NEAREST};
87 MNN_PUBLIC VARP _CropAndResize(VARP image, VARP boxes, VARP box_ind, VARP crop_size,
88 InterpolationMethod method, float extrapolation_value = 0.0);
89 MNN_PUBLIC VARP _Fill(VARP dims, VARP value);
90 MNN_PUBLIC VARP _Tile(VARP input, VARP multiples);
91 MNN_PUBLIC VARP _Gather(VARP params, VARP indices);
92 MNN_PUBLIC VARP _GatherV2(VARP params, VARP indices, VARP axis = nullptr);
93 MNN_PUBLIC VARP _Squeeze(VARP input, INTS axis = {});
94 MNN_PUBLIC VARP _Unsqueeze(VARP input, INTS axis = {});
95 MNN_PUBLIC VARP _BatchToSpaceND(VARP input, VARP block_shape, VARP crops);
96 MNN_PUBLIC VARP _GatherND(VARP params, VARP indices);
97 MNN_PUBLIC VARP _Selu(VARP features, float scale, float alpha);
98 MNN_PUBLIC VARP _Size(VARP input);
99 MNN_PUBLIC VARP _Elu(VARP features, float alpha=1.0);
100 MNN_PUBLIC VARP _Threshold(VARP features, float alpha=1.0);
101 MNN_PUBLIC VARP _MatrixBandPart(VARP input, VARP num_lower, VARP num_upper);
102 MNN_PUBLIC std::vector<VARP> _Moments(VARP x, INTS axis, VARP shift, bool keepDims);
103 MNN_PUBLIC VARP _SetDiff1D(VARP x, VARP y);
104 MNN_PUBLIC VARP _SpaceToDepth(VARP input, int block_size);
105 MNN_PUBLIC VARP _SpaceToBatchND(VARP input, VARP block_shape, VARP paddings);
106 MNN_PUBLIC VARP _ZerosLike(VARP input);
107 MNN_PUBLIC std::vector<VARP> _Unstack(VARP value, int axis=0);
108 MNN_PUBLIC VARP _Rank(VARP input);
109 MNN_PUBLIC VARP _Range(VARP start, VARP limit, VARP delta);
110 MNN_PUBLIC VARP _DepthToSpace(VARP input, int block_size);
111 MNN_PUBLIC VARP _PriorBox(VARP feature, VARP image,
112 std::vector<float> min_size, std::vector<float> max_size, std::vector<float>aspect_ratio,
113 bool flip, bool clip, std::vector<float>variance,
114 unsigned int img_h, unsigned int img_w, float step_h, float step_w, float offset = 0.5);
115 MNN_PUBLIC VARP _Permute(VARP input, INTS dims);
116 MNN_PUBLIC VARP _DetectionOutput(VARP location, VARP confidence, VARP priorbox,
117 unsigned int num_classes, bool share_location, int background_label_id,
118 float nms_threshhold, int nms_topk, int code_type,
119 bool variance_encoded_in_target,
120 int keep_top_k, float confidence_threshold, float visualize_threshold);
121 MNN_PUBLIC std::vector<VARP> _DetectionPostProcess(VARP encode_boxes, VARP class_predictions, VARP anchors,
122 int num_classes, int max_detections,
123 int max_class_per_detection, int detections_per_class,
124 float nms_threshold, float iou_threshold,
125 bool use_regular_nms, std::vector<float> centersize_encoding);
126 MNN_PUBLIC VARP _Interp(VARPS xs, float widthScale, float heightScale, int outputWidth, int outputHeight, int resizeType, bool alignCorners);
127
128 MNN_PUBLIC VARP _ZeroGrad(VARP x);
129
130 // Int8 Inference
131 MNN_PUBLIC VARP _Conv(std::vector<int8_t>&& weight, std::vector<int>&& bias, std::vector<float>&& scale, VARP x, INTS channel, INTS kernelSize,
132 PaddingMode pad, INTS stride, INTS dilate, int group, INTS pads, bool relu, int nbits = 8);
133 MNN_PUBLIC VARP _Conv(std::vector<int8_t>&& weight, std::vector<int>&& bias, std::vector<float>&& scale,
134 VARP x, INTS channel, INTS kernelSize,
135 PaddingMode pad, INTS stride, INTS dilate, int group, INTS pads, bool relu,
136 int8_t inputZeroPoint, int8_t outputZeroPoint,
137 int8_t minValue, int8_t maxValue, bool accumulateToInt16);
138 MNN_PUBLIC VARP _Conv(std::vector<int8_t>&& weight, std::vector<float>&& bias, std::vector<float>&& weightScale,
139 VARP x, INTS channel, INTS kernelSize,
140 PaddingMode pad, INTS stride, INTS dilate, int group, INTS pads, bool relu,
141 float scaleIn, float scaleOut,
142 int8_t inputZeroPoint, int8_t outputZeroPoint,
143 int8_t minValue, int8_t maxValue, float weightClampValue, bool accumulateToInt16);
144 MNN_PUBLIC VARP _CosineSimilarity(VARP input0, VARP input1, VARP inputDim);
145
146 enum GridSamplePaddingMode {GRID_SAMPLE_PADDING_ZEROS, GRID_SAMPLE_PADDING_BORDER, GRID_SAMPLE_PADDING_REFLECTION};
147 MNN_PUBLIC VARP _GridSample(VARP input, VARP grid, InterpolationMethod mode=BILINEAR, GridSamplePaddingMode paddingMode=GRID_SAMPLE_PADDING_ZEROS, bool alignCorners=false);
148 MNN_PUBLIC VARP _FloatToInt8(VARP x, VARP scale, char minValue, char maxValue);
149 MNN_PUBLIC VARP _FloatToInt8(VARP x, VARP scale, int8_t minValue, int8_t maxValue, int8_t zeroPoint);
150 MNN_PUBLIC VARP _Int8ToFloat(VARP x, VARP scale);
151 MNN_PUBLIC VARP _Int8ToFloat(VARP x, VARP scale, int8_t zeroPoint);
152
153 MNN_PUBLIC VARP _Select(VARP select, VARP input0, VARP input1);
154 MNN_PUBLIC std::vector<VARP> _TopKV2(VARP input0, VARP input1);
155
156 } // namespace Express
157 } // namespace MNN
158
159 #endif /* NeuralNetWorkOp_HPP */
160