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.

binaryop-broadcasting.md 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ### broadcasting rule
  2. ncnn BinaryOp accepts blobs with different shape
  3. C = BinaryOp(A, B)
  4. shape notation convention is [w], [w,h], [w,h,c], [w,h,d,c]
  5. * binaryop with scalar and scalar-like
  6. |A|B|C|
  7. |---|---|---|
  8. |[2]|scalar / [1]|[2]|
  9. |[2,3]|scalar / [1] / [1,1]|[2,3]|
  10. |[2,3,4]|scalar / [1] / [1,1] / [1,1,1]|[2,3,4]|
  11. |[2,3,4,5]|scalar / [1] / [1,1] / [1,1,1] / [1,1,1,1]|[2,3,4,5]|
  12. * no broadcast
  13. |A|B|C|
  14. |---|---|---|
  15. |[2]|[2]|[2]|
  16. |[2,3]|[2,3]|[2,3]|
  17. |[2,3,4]|[2,3,4]|[2,3,4]|
  18. |[2,3,4,5]|[2,3,4,5]|[2,3,4,5]|
  19. * explicit broadcast B
  20. |A|B|C|
  21. |---|---|---|
  22. |[2,3]|[1,3]|[2,3]|
  23. |[2,3]|[2,1]|[2,3]|
  24. |[2,3,4]|[1,3,4]|[2,3,4]|
  25. |[2,3,4]|[2,1,4]|[2,3,4]|
  26. |[2,3,4]|[2,3,1]|[2,3,4]|
  27. |[2,3,4]|[1,1,4]|[2,3,4]|
  28. |[2,3,4]|[1,3,1]|[2,3,4]|
  29. |[2,3,4]|[2,1,1]|[2,3,4]|
  30. |[2,3,4,5]|[1,3,4,5]|[2,3,4,5]|
  31. |[2,3,4,5]|[2,1,4,5]|[2,3,4,5]|
  32. |[2,3,4,5]|[2,3,1,5]|[2,3,4,5]|
  33. |[2,3,4,5]|[2,3,4,1]|[2,3,4,5]|
  34. |[2,3,4,5]|[1,1,4,5]|[2,3,4,5]|
  35. |[2,3,4,5]|[1,3,1,5]|[2,3,4,5]|
  36. |[2,3,4,5]|[1,3,4,1]|[2,3,4,5]|
  37. |[2,3,4,5]|[2,1,1,5]|[2,3,4,5]|
  38. |[2,3,4,5]|[2,1,4,1]|[2,3,4,5]|
  39. |[2,3,4,5]|[2,3,1,1]|[2,3,4,5]|
  40. |[2,3,4,5]|[1,1,1,5]|[2,3,4,5]|
  41. |[2,3,4,5]|[1,1,4,1]|[2,3,4,5]|
  42. |[2,3,4,5]|[1,3,1,1]|[2,3,4,5]|
  43. |[2,3,4,5]|[2,1,1,1]|[2,3,4,5]|
  44. * implicit broadcast B for inner axis
  45. It broadcasts in the opposite direction of the numpy's implicit broadcasting behavior.
  46. pnnx will insert reshape operator at the appropriate position to convert it to explicit broadcast automatically.
  47. |A|B|C|
  48. |---|---|---|
  49. |[2,3]|[3]|[2,3]|
  50. |[2,3,4]|[4]|[2,3,4]|
  51. |[2,3,4]|[3,4]|[2,3,4]|
  52. |[2,3,4,5]|[5]|[2,3,4,5]|
  53. |[2,3,4,5]|[4,5]|[2,3,4,5]|
  54. |[2,3,4,5]|[3,4,5]|[2,3,4,5]|
  55. * implicit broadcast B with 1 dimension rank for outer axis
  56. This exists only for compatibility.
  57. When the size is the same, eg. [2,2] and [2], broadcast B for inner axis will be prioritized.
  58. |A|B|C|
  59. |---|---|---|
  60. |[2,3]|[2]|[2,3]|
  61. |[2,3,4]|[2]|[2,3,4]|
  62. |[2,3,4,5]|[2]|[2,3,4,5]|