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