You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

expression.md 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ### expression
  2. expression is used in the reshape slice parameter to express the dynamic shape or subscript value based on the expression formula and input shape
  3. Compared with directly converting the expression calculation process into multiple operators, the motivation for using expression
  4. * No additional shape concat and other operators will be generated due to dynamic calculation, which greatly reduces the number of layers of the ncnn model and makes it easier to view the model structure and modify expression
  5. * Shape or subscript evaluations are usually single-digit operations, which are more suitable for direct completion on the CPU without layout conversion and kernel call overhead
  6. In the param file, `Reshape` layer can contain 6=expression
  7. The pnnx tool can automatically convert `pnnx.Expression` to the expr parameter of ncnn `Reshape`
  8. * Convert to 0w, 0h, 0d or 0c according to the input shape rank and `size(@0,1)`
  9. * Automatically remove the batch dimension according to the input batch index
  10. * Convert `pnnx.Expression` and `Tensor.reshape`/`Tensor.view` two operators are fused into ncnn `Reshape`
  11. * Automatically summarize the number of references, exclude duplicate references and sort the indexes of references
  12. * Convert the customary shape representation order, such as CHW to WHC
  13. Example pnnx.param where A and B are 3D tensors
  14. ```
  15. pnnx.Expression expr 2 1 A B shape expr=[add(size(@1,0),2),mul(size(@0,1),2),-1]
  16. Tensor.reshape reshape 2 1 A shape out
  17. ```
  18. pnnx.py
  19. ```python
  20. shape = [(B.size(0) + 2), (A.size(1) * 2), -1]
  21. out = A.reshape(*shape)
  22. ```
  23. Converted to ncnn.param
  24. ```
  25. Reshape reshape 2 1 A B out 6="-1,*(0h,2),+(1c,2)"
  26. ```
  27. ### syntax
  28. 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
  29. 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.
  30. * `max(2,3)`
  31. * `floor(sin(3.14))`
  32. * `+(*(-2,1),10)` means (-2 * 1) + 10
  33. * `1,2,+(3,2)` list can represent output shape with 3-rank
  34. The 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
  35. Assuming that the Reshape layer has two input blobs, A and B, then
  36. * `0w,1h` means A.w, B.h
  37. * `*(+(0c,1c),2)` means (A.c + B.c) * 2
  38. ### helper api
  39. ```cpp
  40. #include "expression.h"
  41. int count_expression_blobs(const std::string& expr);
  42. int eval_list_expression(const std::string& expr, const std::vector<Mat>& blobs, std::vector<int>& outlist);
  43. ```
  44. * `count_expression_blobs`
  45. Pass expression to get the number of inputs it references, such as `0w,1h` returns 2
  46. * `eval_list_expression`
  47. Evaluate 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.
  48. ### supported operator
  49. |type|operators|
  50. |---|---|
  51. |float to int|`trunc` `ceil` `floor` `round`|
  52. |binary arithmetic|`+` `-` `*` `/` `//` `max` `min` `pow` `fmod` `remainder` `atan2` `logaddexp`|
  53. |unary arithmetic|`abs` `neg` `sign` `square` `sqrt` `rsqrt` `reciprocal` `exp` `log` `log10` `sin` `asin` `cos` `acos` `tan` `atan` `sinh` `asinh` `cosh` `acosh` `tanh` `atanh`|
  54. |integer bitwise|`and` `or` `xor` `lshift` `rshift`|