docs/developer-guide/expression.md
expression is used in the reshape slice parameter to express the dynamic shape or subscript value based on the expression formula and input shape
Compared with directly converting the expression calculation process into multiple operators, the motivation for using expression
In the param file, Reshape layer can contain 6=expression
The pnnx tool can automatically convert pnnx.Expression to the expr parameter of ncnn Reshape
size(@0,1)pnnx.Expression and Tensor.reshape/Tensor.view two operators are fused into ncnn ReshapeExample pnnx.param where A and B are 3D tensors
pnnx.Expression expr 2 1 A B shape expr=[add(size(@1,0),2),mul(size(@0,1),2),-1]
Tensor.reshape reshape 2 1 A shape out
pnnx.py
shape = [(B.size(0) + 2), (A.size(1) * 2), -1]
out = A.reshape(*shape)
Converted to ncnn.param
Reshape reshape 2 1 A B out 6="-1,*(0h,2),+(1c,2)"
Use infix expression, format is op(arg0,arg1,...), multiple operations can be nested, multiple sizes are separated by commas, and numbers can be integers or decimals
Among them, the commonly used add sub mul div floor_div are abbreviated as + - * / //, and other arithmetic operations use names, such as sin ceil max, etc.
max(2,3)floor(sin(3.14))+(*(-2,1),10) means (-2 * 1) + 101,2,+(3,2) list can represent output shape with 3-rankThe input shape can be referenced at runtime, format is id(w|h|d|c), the maximum id is 9, which means that up to 10 inputs can be referenced
Assuming that the Reshape layer has two input blobs, A and B, then
0w,1h means A.w, B.h*(+(0c,1c),2) means (A.c + B.c) * 2#include "expression.h"
int count_expression_blobs(const std::string& expr);
int eval_list_expression(const std::string& expr, const std::vector<Mat>& blobs, std::vector<int>& outlist);
count_expression_blobsPass expression to get the number of inputs it references, such as 0w,1h returns 2
eval_list_expressionEvaluate the result list according to expression and input blob calculate. If the calculation result is a floating point number, it will be automatically truncated to an integer.
| type | operators |
|---|---|
| float to int | trunc ceil floor round |
| binary arithmetic | + - * / // max min pow fmod remainder atan2 logaddexp |
| unary arithmetic | abs neg sign square sqrt rsqrt reciprocal exp log log10 sin asin cos acos tan atan sinh asinh cosh acosh tanh atanh |
| integer bitwise | and or xor lshift rshift |