Broadcast Relationship
Broadcast Concept
Broadcasting describes how operators handle tensors (or arrays) of different shapes during computation. In most cases, tensors (or arrays) of different shapes are allowed to automatically expand their shapes during element-wise operations so that their dimensions become compatible with each other. Typically, the smaller tensor (or array) is "broadcast" to match the larger tensor (or array).
Currently, many CANN operator API parameters support broadcast in their shapes, which can improve computational efficiency and reduce memory usage (especially in large-scale data scenarios). For a more detailed introduction to broadcasting techniques, refer to the NumPy official website.
Broadcast Rules
When performing broadcast computation, the following rules generally apply:
Rule 1: If the arrays have different numbers of dimensions, all arrays are aligned to the array with the longest shape, and the missing dimensions are padded with 1 on the left until the numbers of dimensions are the same.
NOTE:
- Example 1: The number of dimensions is the number of dimensions in the shape of a tensor (or array). For example, x.shape=(1,1,2,4) has a dimension count of 4.
- Example 2: For example, to compute a+b, where a.shape=(2, 2, 3) and b.shape=(2, 3), array b is broadcast to b.shape=(1, 2, 3).
Rule 2: If the arrays have the same number of dimensions and a dimension of one array is 1, the array with the dimension of 1 is stretched to match the corresponding dimension shape of the other array.
NOTE: In this use case, broadcasting along one dimension is sufficient. For example, to compute a+b, where a.shape=(1, 3) and b.shape=(3, 1), the two arrays are broadcast to a.shape=(3, 3) and b.shape=(3, 3).
Rule 3: If the arrays have different numbers of dimensions and neither array has a dimension equal to 1, an error is reported.
Based on the above rules, the broadcast process generally first expands dimensions according to Rule 1, and then stretches shapes according to Rule 2. A specific example is as follows:
Assume a.shape=(2,2,3), with values like:
[[[1 2 3],[4 5 6]],
[[1 2 3],[4 5 6]]]
Assume b.shape=(2,3), with values like:
[[1 2 3],
[-1 -2 -3]]
Extend the dimensions according to Rule 1: b.shape=(1,2,3), with the following values:
[[[1 2 3],
[-1 -2 -3]]]
Stretch the shape according to Rule 2: b.shape=(2,2,3), with the following values:
[[[1 2 3],[-1 -2 -3]],
[[1 2 3],[-1 -2 -3]]]
Compute a+b. The actual result is as follows:
[[[2 4 6],[3 3 3]],
[[2 4 6],[3 3 3]]]Constraints
When the data types of inputs a and b that satisfy the broadcast relationship, or the deduced data types, are COMPLEX64, COMPLEX128, DOUBLE, INT16, UINT16, or UINT64, the following condition must be met in addition to the preceding broadcast rules. Otherwise, the broadcast fails, causing an error during operator execution.
Condition: The number of dimensions after merging consecutive axes to be broadcast and consecutive axes not to be broadcast must be less than 6.
Meaning: Mark each axis of the shapes participating in the broadcast as "needs broadcasting" or "does not need broadcasting" (broadcast rules as described above). Adjacent axes with the same mark are merged into one segment (consecutive axes that need broadcasting form one segment, and consecutive axes that do not need broadcasting form one segment). The total number of segments after merging must be fewer than 6 (that is, no more than 5 segments). Otherwise, the broadcast fails.
Example (indices start from 0):
- When a.shape=(5, 1, 5, 1, 5, 1), b.shape=(5, 5, 5, 5, 5, 5): axes 0, 2, and 4 do not need broadcasting, and axes 1, 3, and 5 need broadcasting. No adjacent axes with the same state can be merged, so the dimensions have 6 segments in total, and the broadcast reports an error.
- When a.shape=(5, 1, 5, 5, 1, 1), b.shape=(5, 5, 5, 5, 5, 5): axis 0 does not need broadcasting, axis 1 needs broadcasting, axes 2 and 3 do not need broadcasting (merged into one segment), and axes 4 and 5 need broadcasting (merged into one segment). The dimensions have 4 segments after merging, and the broadcast succeeds.