Broadcasting describes how an operator treats tensors (or arrays) with different shapes during arithmetic operations. In most cases, the smaller tensor (or array) can be "broadcast" across the larger tensor (or array) so that they have compatible shapes.
The shape parameters of many CANN operator APIs support broadcasting, which can improve the computing efficiency and reduce the memory usage (especially when the data size is large). For more details about the broadcasting technique, see .
Generally, you need to understand the following broadcasting rules during computation:
Rule 1: If two arrays have different dimensions, it pads ones on the left side of the shape of the array that has fewer dimensions.
[object Object]
Rule 2: If the number of dimensions of two arrays is the same and a dimension of an array is 1, the array with the dimension 1 is stretched to match the dimension shape of the other array.
[object Object]
Rule 3: If any dimension of two arrays is not equal and neither is equal to one, an error is reported.
Based on the preceding rules, the broadcasting process first expands dimensions according to Rule 1 and then stretches the shape according to Rule 2. The following is an example:
If the data types of the two inputs a and b meet the broadcast relationship and their deduced data type is COMPLEX64, COMPLEX128, DOUBLE, INT16, UINT16, or UINT64, the following condition must be met in addition to the preceding broadcast rules. Otherwise, the broadcasting fails and an error is reported during operator execution. Condition: After combination of the contiguous axes that need to be broadcast and the contiguous axes that do not need to be broadcast, the number of dimensions must be fewer than 6. Example:
- If a.shape=(5, 1, 5, 1, 5, 1) and b.shape=(5, 5, 5, 5, 5, 5) do not have axes to be combined, the final shape is 6D. In this case, an error is reported.
- If a.shape=(5, 1, 5, 5, 1, 1) and b.shape=(5, 5, 5, 5, 5, 5) have the second and third dimensions not to be broadcast but the fourth and fifth dimensions to be broadcast contiguously, the final shape is 4D. In this case, the broadcasting is successful.