Files
ncnn/docs/developer-guide/binaryop-broadcasting.md
T
nihui 9022b7162a implement all explicit binaryop broadcast types (#4809)
* simplify binaryop

* less gpu test

* update binaryop broadcast doc

* do not test atan2 zero
2023-06-19 19:32:32 +08:00

1.6 KiB

broadcasting rule

ncnn BinaryOp accepts blobs with different shape

C = BinaryOp(A, B)

shape notation convention is [w], [w,h], [w,h,c], [w,h,d,c]

  • binaryop with scalar and scalar-like
A B C
[2] scalar / [1] [2]
[2,3] scalar / [1] / [1,1] [2,3]
[2,3,4] scalar / [1] / [1,1] / [1,1,1] [2,3,4]
[2,3,4,5] scalar / [1] / [1,1] / [1,1,1] / [1,1,1,1] [2,3,4,5]
  • no broadcast
A B C
[2] [2] [2]
[2,3] [2,3] [2,3]
[2,3,4] [2,3,4] [2,3,4]
[2,3,4,5] [2,3,4,5] [2,3,4,5]
  • explicit broadcast B
A B C
[2,3] [1,3] [2,3]
[2,3] [2,1] [2,3]
[2,3,4] [1,3,4] [2,3,4]
[2,3,4] [2,1,4] [2,3,4]
[2,3,4] [2,3,1] [2,3,4]
[2,3,4] [1,1,4] [2,3,4]
[2,3,4] [1,3,1] [2,3,4]
[2,3,4] [2,1,1] [2,3,4]
[2,3,4,5] [1,3,4,5] [2,3,4,5]
[2,3,4,5] [2,1,4,5] [2,3,4,5]
[2,3,4,5] [2,3,1,5] [2,3,4,5]
[2,3,4,5] [2,3,4,1] [2,3,4,5]
[2,3,4,5] [1,1,4,5] [2,3,4,5]
[2,3,4,5] [1,3,1,5] [2,3,4,5]
[2,3,4,5] [1,3,4,1] [2,3,4,5]
[2,3,4,5] [2,1,1,5] [2,3,4,5]
[2,3,4,5] [2,1,4,1] [2,3,4,5]
[2,3,4,5] [2,3,1,1] [2,3,4,5]
[2,3,4,5] [1,1,1,5] [2,3,4,5]
[2,3,4,5] [1,1,4,1] [2,3,4,5]
[2,3,4,5] [1,3,1,1] [2,3,4,5]
[2,3,4,5] [2,1,1,1] [2,3,4,5]
  • implicit broadcast B for inner axis

It broadcasts in the opposite direction of the numpy's implicit broadcasting behavior.

pnnx will insert reshape operator at the appropriate position to convert it to explicit broadcast automatically.

A B C
[2,3] [3] [2,3]
[2,3,4] [4] [2,3,4]
[2,3,4] [3,4] [2,3,4]
[2,3,4,5] [5] [2,3,4,5]
[2,3,4,5] [4,5] [2,3,4,5]
[2,3,4,5] [3,4,5] [2,3,4,5]