Merged
fix(moe): 加固 MoeTokenUnpermuteWithEp Tiling/InferShape 输入校验 #15936
gylAdd on 16 days ago
fix(moe): 加固 MoeTokenUnpermuteWithEp Tiling/InferShape 输入校验 #15936
Merged
Total: 15 files changed+825-237
| @@ -18,32 +18,24 @@ | |||
| 18 | - 算子功能:根据sortedIndices存储的下标位置,获取permutedTokens中的输入数据与probs相乘,并进行合并累加。 | 18 | - 算子功能:根据sortedIndices存储的下标位置,获取permutedTokens中的输入数据与probs相乘,并进行合并累加。 |
| 19 | - 计算公式: | 19 | - 计算公式: |
| 20 | 20 | ||
| 21 | -$$ | 21 | +记 sortedIndices 的第 k 个元素为 `sortedIndices[k]`,`num_tokens = len(sortedIndices) / numTopk`,`rangeOptional = [start, end)`。仅当 `start <= sortedIndices[k] < end` 时该槽位取数,取数行为 `permutedTokens[sortedIndices[k] - start]`;否则该槽位贡献 0。`rangeOptional` 为空时等价于默认值 `{0, 0}`,所有槽位均不满足条件,输出全 0。 |
| 22 | -sortedIndices = sortedIndices[rangeOptional[0]<=i<rangeOptional[1]] | ||
| 23 | -$$ | ||
| 24 | 22 | ||
| 25 | -(1)probs非None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., numTopk - 1}$,$k \in {0, 1, 2, ..., num\_tokens * numTopk}$: | 23 | +(1)probs非None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., numTopk - 1}$,槽位下标$k = i * numTopk + j$: |
| 26 | 24 | ||
| 27 | $$ | 25 | $$ |
| 28 | -permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | 26 | +out_{i} = \sum_{j=0}^{numTopk - 1} \begin{cases} |
| 27 | +permutedTokens[sortedIndices[k] - start] * probs_{i,j}, & start \le sortedIndices[k] < end \\ | ||
| 28 | +0, & \text{otherwise} | ||
| 29 | +\end{cases} | ||
| 29 | $$ | 30 | $$ |
| 30 | 31 | ||
| 31 | -$$ | 32 | +(2)probs为None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., numTopk - 1}$,槽位下标$k = i * numTopk + j$: |
| 32 | -permutedTokens_{k} = permutedTokens_{k} * probs_{i,j} | ||
| 33 | -$$ | ||
| 34 | 33 | ||
| 35 | $$ | 34 | $$ |
| 36 | -out_{i} = \sum_{k=i*numTopk}^{(i+1)*numTopk - 1 } permutedTokens_{k} | 35 | +out_{i} = \sum_{j=0}^{numTopk - 1} \begin{cases} |
| 37 | -$$ | 36 | +permutedTokens[sortedIndices[k] - start], & start \le sortedIndices[k] < end \\ |
| 38 | - | 37 | +0, & \text{otherwise} |
| 39 | -(2)probs为None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., numTopk - 1}$: | 38 | +\end{cases} |
| 40 | - | ||
| 41 | -$$ | ||
| 42 | -permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | ||
| 43 | -$$ | ||
| 44 | - | ||
| 45 | -$$ | ||
| 46 | -out_{i} = \sum_{k=i*numTopk}^{(i+1)*numTopk - 1 } permutedTokens_{k} | ||
| 47 | $$ | 39 | $$ |
| 48 | 40 | ||
| 49 | ## 参数说明 | 41 | ## 参数说明 |
| @@ -69,21 +61,21 @@ $$ | |||
| 69 | <tr> | 61 | <tr> |
| 70 | <td>permutedTokens</td> | 62 | <td>permutedTokens</td> |
| 71 | <td>输入</td> | 63 | <td>输入</td> |
| 72 | - <td>表示经过扩展并排序过的tokens,对应公式中的`permutedTokens`。</td> | 64 | + <td>表示经过扩展并排序过的tokens,对应公式中的<code>permutedTokens</code>。</td> |
| 73 | <td>FLOAT16、BFLOAT16、FLOAT32</td> | 65 | <td>FLOAT16、BFLOAT16、FLOAT32</td> |
| 74 | <td>ND</td> | 66 | <td>ND</td> |
| 75 | </tr> | 67 | </tr> |
| 76 | <tr> | 68 | <tr> |
| 77 | <td>sortedIndices</td> | 69 | <td>sortedIndices</td> |
| 78 | <td>输入</td> | 70 | <td>输入</td> |
| 79 | - <td>表示需要计算的数据在permutedTokens中的位置,对应公式中的`sortedIndices`。</td> | 71 | + <td>表示需要计算的数据在permutedTokens中的位置,对应公式中的<code>sortedIndices</code>。</td> |
| 80 | <td>INT32</td> | 72 | <td>INT32</td> |
| 81 | <td>ND</td> | 73 | <td>ND</td> |
| 82 | </tr> | 74 | </tr> |
| 83 | <tr> | 75 | <tr> |
| 84 | <td>probsOptional</td> | 76 | <td>probsOptional</td> |
| 85 | <td>可选输入</td> | 77 | <td>可选输入</td> |
| 86 | - <td>表示输入tokens对应的专家概率,对应公式中的`probs`。</td> | 78 | + <td>表示输入tokens对应的专家概率,对应公式中的<code>probs</code>。</td> |
| 87 | <td>FLOAT16、BFLOAT16、FLOAT32</td> | 79 | <td>FLOAT16、BFLOAT16、FLOAT32</td> |
| 88 | <td>ND</td> | 80 | <td>ND</td> |
| 89 | </tr> | 81 | </tr> |
| @@ -97,8 +89,8 @@ $$ | |||
| 97 | <tr> | 89 | <tr> |
| 98 | <td>rangeOptional</td> | 90 | <td>rangeOptional</td> |
| 99 | <td>属性</td> | 91 | <td>属性</td> |
| 100 | - <td>ep切分的有效范围,size为2。</td> | 92 | + <td>ep切分的有效范围[start, end),size为2,元素类型为INT64。</td> |
| 101 | - <td>aclIntArray*</td> | 93 | + <td>INT64(aclIntArray*)</td> |
| 102 | <td>-</td> | 94 | <td>-</td> |
| 103 | </tr> | 95 | </tr> |
| 104 | <tr> | 96 | <tr> |
| @@ -112,13 +104,13 @@ $$ | |||
| 112 | <td>restoreShapeOptional</td> | 104 | <td>restoreShapeOptional</td> |
| 113 | <td>属性</td> | 105 | <td>属性</td> |
| 114 | <td>目前仅支持nullptr。</td> | 106 | <td>目前仅支持nullptr。</td> |
| 115 | - <td>aclIntArray*</td> | 107 | + <td>INT64(aclIntArray*)</td> |
| 116 | <td>-</td> | 108 | <td>-</td> |
| 117 | </tr> | 109 | </tr> |
| 118 | <tr> | 110 | <tr> |
| 119 | <td>out</td> | 111 | <td>out</td> |
| 120 | <td>输出</td> | 112 | <td>输出</td> |
| 121 | - <td>表示permutedTokens反重排的输出结果,对应公式中的`out`。</td> | 113 | + <td>表示permutedTokens反重排的输出结果,对应公式中的<code>out</code>。</td> |
| 122 | <td>FLOAT16、BFLOAT16、FLOAT32</td> | 114 | <td>FLOAT16、BFLOAT16、FLOAT32</td> |
| 123 | <td>ND</td> | 115 | <td>ND</td> |
| 124 | </tr> | 116 | </tr> |
| @@ -128,7 +120,9 @@ $$ | |||
| 128 | 120 | ||
| 129 | ## 约束说明 | 121 | ## 约束说明 |
| 130 | 122 | ||
| 131 | -- numTopk必须大于等于1;probsOptional非空时,numTopk必须小于等于512。 | 123 | +- numTopk必须在[1, 512]范围内。 |
| 124 | +- sortedIndices必须为1D,长度为num_tokens*numTopk,且num_tokens大于等于1,不支持空Tensor。 | ||
| 125 | +- rangeOptional非空时,必须满足0 <= rangeOptional[0] <= rangeOptional[1] <= rangeOptional[0] + permutedTokens的第0维长度。 | ||
| 132 | - 不支持Broadcast。 | 126 | - 不支持Broadcast。 |
| 133 | - 不支持paddedMode为`True`。 | 127 | - 不支持paddedMode为`True`。 |
| 134 | - 当rangeOptional为空时,使用默认值{0,0},输出为全0,不会回退调用其他算子。 | 128 | - 当rangeOptional为空时,使用默认值{0,0},输出为全0,不会回退调用其他算子。 |
| @@ -20,32 +20,24 @@ English | [简体中文](./README.md) | |||
| 20 | - Operator function: obtains the input data in permutedTokens based on the index positions stored in sortedIndices, multiplies the data by probs, and performs merged accumulation. | 20 | - Operator function: obtains the input data in permutedTokens based on the index positions stored in sortedIndices, multiplies the data by probs, and performs merged accumulation. |
| 21 | - Computation formula: | 21 | - Computation formula: |
| 22 | 22 | ||
| 23 | -$$ | 23 | +Let `sortedIndices[k]` be the k-th element of sortedIndices, `num_tokens = len(sortedIndices) / numTopk`, and `rangeOptional = [start, end)`. A slot is read only when `start <= sortedIndices[k] < end`, and the row is `permutedTokens[sortedIndices[k] - start]`; otherwise the slot contributes 0. An empty `rangeOptional` is equivalent to the default value `{0, 0}`, so no slot is valid and the output is all zeros. |
| 24 | -sortedIndices = sortedIndices[rangeOptional[0]<=i<rangeOptional[1]] | ||
| 25 | -$$ | ||
| 26 | 24 | ||
| 27 | -(1) When probs is not None, the computation formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., numTopk - 1}$, and $k \in {0, 1, 2, ..., num\_tokens * numTopk}$: | 25 | +(1) When probs is not None, the computation formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., numTopk - 1}$, and the slot index is $k = i * numTopk + j$: |
| 28 | 26 | ||
| 29 | $$ | 27 | $$ |
| 30 | -permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | 28 | +out_{i} = \sum_{j=0}^{numTopk - 1} \begin{cases} |
| 29 | +permutedTokens[sortedIndices[k] - start] * probs_{i,j}, & start \le sortedIndices[k] < end \\ | ||
| 30 | +0, & \text{otherwise} | ||
| 31 | +\end{cases} | ||
| 31 | $$ | 32 | $$ |
| 32 | 33 | ||
| 33 | -$$ | 34 | +(2) When probs is None, the computation formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., numTopk - 1}$, and the slot index is $k = i * numTopk + j$: |
| 34 | -permutedTokens_{k} = permutedTokens_{k} * probs_{i,j} | ||
| 35 | -$$ | ||
| 36 | 35 | ||
| 37 | $$ | 36 | $$ |
| 38 | -out_{i} = \sum_{k=i*numTopk}^{(i+1)*numTopk - 1 } permutedTokens_{k} | 37 | +out_{i} = \sum_{j=0}^{numTopk - 1} \begin{cases} |
| 39 | -$$ | 38 | +permutedTokens[sortedIndices[k] - start], & start \le sortedIndices[k] < end \\ |
| 40 | - | 39 | +0, & \text{otherwise} |
| 41 | -(2) When probs is None, the computation formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$ and $j \in {0, 1, 2, ..., numTopk - 1}$: | 40 | +\end{cases} |
| 42 | - | ||
| 43 | -$$ | ||
| 44 | -permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | ||
| 45 | -$$ | ||
| 46 | - | ||
| 47 | -$$ | ||
| 48 | -out_{i} = \sum_{k=i*numTopk}^{(i+1)*numTopk - 1 } permutedTokens_{k} | ||
| 49 | $$ | 41 | $$ |
| 50 | 42 | ||
| 51 | ## Parameter Description | 43 | ## Parameter Description |
| @@ -71,21 +63,21 @@ $$ | |||
| 71 | <tr> | 63 | <tr> |
| 72 | <td>permutedTokens</td> | 64 | <td>permutedTokens</td> |
| 73 | <td>Input</td> | 65 | <td>Input</td> |
| 74 | - <td>Tokens that have been expanded and sorted, corresponding to `permutedTokens` in the formula.</td> | 66 | + <td>Tokens that have been expanded and sorted, corresponding to <code>permutedTokens</code> in the formula.</td> |
| 75 | <td>FLOAT16, BFLOAT16, FLOAT32</td> | 67 | <td>FLOAT16, BFLOAT16, FLOAT32</td> |
| 76 | <td>ND</td> | 68 | <td>ND</td> |
| 77 | </tr> | 69 | </tr> |
| 78 | <tr> | 70 | <tr> |
| 79 | <td>sortedIndices</td> | 71 | <td>sortedIndices</td> |
| 80 | <td>Input</td> | 72 | <td>Input</td> |
| 81 | - <td>Indicates the positions of the data to be computed in permutedTokens, corresponding to `sortedIndices` in the formula.</td> | 73 | + <td>Indicates the positions of the data to be computed in permutedTokens, corresponding to <code>sortedIndices</code> in the formula.</td> |
| 82 | <td>INT32</td> | 74 | <td>INT32</td> |
| 83 | <td>ND</td> | 75 | <td>ND</td> |
| 84 | </tr> | 76 | </tr> |
| 85 | <tr> | 77 | <tr> |
| 86 | <td>probsOptional</td> | 78 | <td>probsOptional</td> |
| 87 | <td>Optional Input</td> | 79 | <td>Optional Input</td> |
| 88 | - <td>Expert probabilities corresponding to the input tokens, corresponding to `probs` in the formula.</td> | 80 | + <td>Expert probabilities corresponding to the input tokens, corresponding to <code>probs</code> in the formula.</td> |
| 89 | <td>FLOAT16, BFLOAT16, FLOAT32</td> | 81 | <td>FLOAT16, BFLOAT16, FLOAT32</td> |
| 90 | <td>ND</td> | 82 | <td>ND</td> |
| 91 | </tr> | 83 | </tr> |
| @@ -99,8 +91,8 @@ $$ | |||
| 99 | <tr> | 91 | <tr> |
| 100 | <td>rangeOptional</td> | 92 | <td>rangeOptional</td> |
| 101 | <td>Attribute</td> | 93 | <td>Attribute</td> |
| 102 | - <td>Valid range for EP partitioning; the size is 2.</td> | 94 | + <td>Valid range for EP split [start, end), with a size of 2. The element type is INT64.</td> |
| 103 | - <td>aclIntArray*</td> | 95 | + <td>INT64 (aclIntArray*)</td> |
| 104 | <td>-</td> | 96 | <td>-</td> |
| 105 | </tr> | 97 | </tr> |
| 106 | <tr> | 98 | <tr> |
| @@ -114,13 +106,13 @@ $$ | |||
| 114 | <td>restoreShapeOptional</td> | 106 | <td>restoreShapeOptional</td> |
| 115 | <td>Attribute</td> | 107 | <td>Attribute</td> |
| 116 | <td>Currently only nullptr is supported.</td> | 108 | <td>Currently only nullptr is supported.</td> |
| 117 | - <td>aclIntArray*</td> | 109 | + <td>INT64 (aclIntArray*)</td> |
| 118 | <td>-</td> | 110 | <td>-</td> |
| 119 | </tr> | 111 | </tr> |
| 120 | <tr> | 112 | <tr> |
| 121 | <td>out</td> | 113 | <td>out</td> |
| 122 | <td>Output</td> | 114 | <td>Output</td> |
| 123 | - <td>Output result of the unpermute operation on permutedTokens, corresponding to `out` in the formula.</td> | 115 | + <td>Output result of the unpermute operation on permutedTokens, corresponding to <code>out</code> in the formula.</td> |
| 124 | <td>FLOAT16, BFLOAT16, FLOAT32</td> | 116 | <td>FLOAT16, BFLOAT16, FLOAT32</td> |
| 125 | <td>ND</td> | 117 | <td>ND</td> |
| 126 | </tr> | 118 | </tr> |
| @@ -130,7 +122,9 @@ $$ | |||
| 130 | 122 | ||
| 131 | ## Constraints | 123 | ## Constraints |
| 132 | 124 | ||
| 133 | -- numTopk must be greater than or equal to 1. When probsOptional is not empty, numTopk must be less than or equal to 512. | 125 | +- numTopk must be in the range [1, 512]. |
| 126 | +- sortedIndices must be 1D with length num_tokens*numTopk, where num_tokens is greater than or equal to 1. Empty tensors are not supported. | ||
| 127 | +- When rangeOptional is not empty, 0 <= rangeOptional[0] <= rangeOptional[1] <= rangeOptional[0] + shape[0] of permutedTokens must hold. | ||
| 134 | - Broadcast is not supported. | 128 | - Broadcast is not supported. |
| 135 | - paddedMode set to `True` is not supported. | 129 | - paddedMode set to `True` is not supported. |
| 136 | - When rangeOptional is empty, the default value {0,0} is used, the output is all 0s, and no fallback call to other operators is performed. | 130 | - When rangeOptional is empty, the default value {0,0} is used, the output is all 0s, and no fallback call to other operators is performed. |
| @@ -27,32 +27,24 @@ | |||
| 27 | 27 | ||
| 28 | - **计算公式:** | 28 | - **计算公式:** |
| 29 | 29 | ||
| 30 | - $$ | 30 | + 记 sortedIndices 的第 k 个元素为 `sortedIndices[k]`,`num_tokens = len(sortedIndices) / numTopk`,`rangeOptional = [start, end)`(`start = rangeOptional[0]`,`end = rangeOptional[1]`,公式中的 `topK_num` 即参数 `numTopk`)。仅当 `start <= sortedIndices[k] < end` 时该槽位取数,取数行为 `permutedTokens[sortedIndices[k] - start]`;否则该槽位贡献 0。`rangeOptional` 为空时等价于默认值 `{0, 0}`,所有槽位均不满足条件,输出全 0。 |
| 31 | - sortedIndices = sortedIndices[rangeOptional[0]<=i<rangeOptional[1]] | ||
| 32 | - $$ | ||
| 33 | 31 | ||
| 34 | - (1)probs非None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., topK\_num - 1}$,$k \in {0, 1, 2, ..., num\_tokens * topK\_num}$: | 32 | + (1)probs非None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., topK\_num - 1}$,槽位下标$k = i * topK\_num + j$: |
| 35 | 33 | ||
| 36 | $$ | 34 | $$ |
| 37 | - permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | 35 | + out_{i} = \sum_{j=0}^{topK\_num - 1} \begin{cases} |
| 36 | + permutedTokens[sortedIndices[k] - start] * probs_{i,j}, & start \le sortedIndices[k] < end \\ | ||
| 37 | + 0, & \text{otherwise} | ||
| 38 | + \end{cases} | ||
| 38 | $$ | 39 | $$ |
| 39 | 40 | ||
| 40 | - $$ | 41 | + (2)probs为None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., topK\_num - 1}$,槽位下标$k = i * topK\_num + j$: |
| 41 | - permutedTokens_{k} = permutedTokens_{k} * probs_{i,j} | ||
| 42 | - $$ | ||
| 43 | 42 | ||
| 44 | $$ | 43 | $$ |
| 45 | - out_{i} = \sum_{k=i*topK\_num}^{(i+1)*topK\_num - 1 } permutedTokens_{k} | 44 | + out_{i} = \sum_{j=0}^{topK\_num - 1} \begin{cases} |
| 46 | - $$ | 45 | + permutedTokens[sortedIndices[k] - start], & start \le sortedIndices[k] < end \\ |
| 47 | - | 46 | + 0, & \text{otherwise} |
| 48 | - (2)probs为None计算公式如下,其中$i \in {0, 1, 2, ..., num\_tokens - 1}$,$j \in {0, 1, 2, ..., topK\_num - 1}$: | 47 | + \end{cases} |
| 49 | - | ||
| 50 | - $$ | ||
| 51 | - permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | ||
| 52 | - $$ | ||
| 53 | - | ||
| 54 | - $$ | ||
| 55 | - out_{i} = \sum_{k=i*topK\_num}^{(i+1)*topK\_num - 1 } permutedTokens_{k} | ||
| 56 | $$ | 48 | $$ |
| 57 | 49 | ||
| 58 | ## 函数原型 | 50 | ## 函数原型 |
| @@ -143,7 +135,7 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 143 | <td>numTopk</td> | 135 | <td>numTopk</td> |
| 144 | <td>输入</td> | 136 | <td>输入</td> |
| 145 | <td>被选中的专家个数。</td> | 137 | <td>被选中的专家个数。</td> |
| 146 | - <td>取值必须大于等于1;probsOptional非空时,取值必须小于等于512。</td> | 138 | + <td>取值必须在[1, 512]范围内。</td> |
| 147 | <td>INT64</td> | 139 | <td>INT64</td> |
| 148 | <td>-</td> | 140 | <td>-</td> |
| 149 | <td>-</td> | 141 | <td>-</td> |
| @@ -152,9 +144,9 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 152 | <tr> | 144 | <tr> |
| 153 | <td>rangeOptional</td> | 145 | <td>rangeOptional</td> |
| 154 | <td>可选输入</td> | 146 | <td>可选输入</td> |
| 155 | - <td>ep切分的有效范围,size为2。</td> | 147 | + <td>ep切分的有效范围[start, end),size为2,元素类型为INT64。</td> |
| 156 | - <td>允许传入空指针;传入空指针时使用默认值{0,0},输出为全0。</td> | 148 | + <td>允许传入空指针;传入空指针时使用默认值{0,0},输出为全0。非空时必须满足0 <= start <= end <= start + permutedTokens的第0维长度。</td> |
| 157 | - <td>-</td> | 149 | + <td>INT64</td> |
| 158 | <td>-</td> | 150 | <td>-</td> |
| 159 | <td>-</td> | 151 | <td>-</td> |
| 160 | <td>-</td> | 152 | <td>-</td> |
| @@ -174,7 +166,7 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 174 | <td>可选输入</td> | 166 | <td>可选输入</td> |
| 175 | <td>-</td> | 167 | <td>-</td> |
| 176 | <td>预留参数,当前仅支持传入空指针。</td> | 168 | <td>预留参数,当前仅支持传入空指针。</td> |
| 177 | - <td>-</td> | 169 | + <td>INT64</td> |
| 178 | <td>-</td> | 170 | <td>-</td> |
| 179 | <td>-</td> | 171 | <td>-</td> |
| 180 | <td>-</td> | 172 | <td>-</td> |
| @@ -291,7 +283,8 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 291 | - 确定性计算: | 283 | - 确定性计算: |
| 292 | - aclnnMoeTokenUnpermuteWithEp默认确定性实现。 | 284 | - aclnnMoeTokenUnpermuteWithEp默认确定性实现。 |
| 293 | 285 | ||
| 294 | -- numTopk必须大于等于1;probsOptional非空时,numTopk必须小于等于512。 | 286 | +- numTopk必须在[1, 512]范围内。 |
| 287 | +- sortedIndices必须为1D,长度为num_tokens*numTopk,且num_tokens大于等于1,不支持空Tensor。 | ||
| 295 | - 不支持Broadcast。 | 288 | - 不支持Broadcast。 |
| 296 | - aclTensor的shape不支持使用-1表示动态维度或使用-2表示动态Rank。 | 289 | - aclTensor的shape不支持使用-1表示动态维度或使用-2表示动态Rank。 |
| 297 | - 不支持paddedMode为`True`。 | 290 | - 不支持paddedMode为`True`。 |
| @@ -29,32 +29,24 @@ | |||
| 29 | 29 | ||
| 30 | - **Formula:** | 30 | - **Formula:** |
| 31 | 31 | ||
| 32 | - $$ | 32 | + Let `sortedIndices[k]` be the k-th element of sortedIndices, `num_tokens = len(sortedIndices) / numTopk`, and `rangeOptional = [start, end)` (`start = rangeOptional[0]`, `end = rangeOptional[1]`; `topK_num` in the formulas is the parameter `numTopk`). A slot is read only when `start <= sortedIndices[k] < end`, and the row is `permutedTokens[sortedIndices[k] - start]`; otherwise the slot contributes 0. An empty `rangeOptional` is equivalent to the default value `{0, 0}`, so no slot is valid and the output is all zeros. |
| 33 | - sortedIndices = sortedIndices[rangeOptional[0]<=i<rangeOptional[1]] | ||
| 34 | - $$ | ||
| 35 | 33 | ||
| 36 | - (1) When probs is not None, the formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., topK\_num - 1}$, and $k \in {0, 1, 2, ..., num\_tokens * topK\_num}$: | 34 | + (1) When probs is not None, the formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., topK\_num - 1}$, and the slot index is $k = i * topK\_num + j$: |
| 37 | 35 | ||
| 38 | $$ | 36 | $$ |
| 39 | - permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | 37 | + out_{i} = \sum_{j=0}^{topK\_num - 1} \begin{cases} |
| 38 | + permutedTokens[sortedIndices[k] - start] * probs_{i,j}, & start \le sortedIndices[k] < end \\ | ||
| 39 | + 0, & \text{otherwise} | ||
| 40 | + \end{cases} | ||
| 40 | $$ | 41 | $$ |
| 41 | 42 | ||
| 42 | - $$ | 43 | + (2) When probs is None, the formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., topK\_num - 1}$, and the slot index is $k = i * topK\_num + j$: |
| 43 | - permutedTokens_{k} = permutedTokens_{k} * probs_{i,j} | ||
| 44 | - $$ | ||
| 45 | 44 | ||
| 46 | $$ | 45 | $$ |
| 47 | - out_{i} = \sum_{k=i*topK\_num}^{(i+1)*topK\_num - 1 } permutedTokens_{k} | 46 | + out_{i} = \sum_{j=0}^{topK\_num - 1} \begin{cases} |
| 48 | - $$ | 47 | + permutedTokens[sortedIndices[k] - start], & start \le sortedIndices[k] < end \\ |
| 49 | - | 48 | + 0, & \text{otherwise} |
| 50 | - (2) When probs is None, the formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$ and $j \in {0, 1, 2, ..., topK\_num - 1}$: | 49 | + \end{cases} |
| 51 | - | ||
| 52 | - $$ | ||
| 53 | - permutedTokens = permutedTokens.indexSelect(0, sortedIndices) | ||
| 54 | - $$ | ||
| 55 | - | ||
| 56 | - $$ | ||
| 57 | - out_{i} = \sum_{k=i*topK\_num}^{(i+1)*topK\_num - 1 } permutedTokens_{k} | ||
| 58 | $$ | 50 | $$ |
| 59 | 51 | ||
| 60 | ## Function Prototype | 52 | ## Function Prototype |
| @@ -132,7 +124,7 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 132 | <td>probsOptional</td> | 124 | <td>probsOptional</td> |
| 133 | <td>Optional input</td> | 125 | <td>Optional input</td> |
| 134 | <td>Represents the expert probability corresponding to the input tokens.</td> | 126 | <td>Represents the expert probability corresponding to the input tokens.</td> |
| 135 | - <td>The shape is 2D. Empty tensors are not supported. num_tokens is the number of original tokens, and the length of the first dimension must be equal to numTopk.<br>When a non-empty and valid tensor is passed in, the input data in permutedTokens is multiplied by probsOptional.<br>When a null pointer is passed in, the input data in permutedTokens is not multiplied.</td> | 127 | + <td>The shape is 2D. Empty tensors are not supported. num_tokens is the number of original tokens, and the length of the second dimension (dim(1)) must be equal to numTopk.<br>When a non-empty and valid tensor is passed in, the input data in permutedTokens is multiplied by probsOptional.<br>When a null pointer is passed in, the input data in permutedTokens is not multiplied.</td> |
| 136 | <td>BFLOAT16, FLOAT16, FLOAT32</td> | 128 | <td>BFLOAT16, FLOAT16, FLOAT32</td> |
| 137 | <td>ND</td> | 129 | <td>ND</td> |
| 138 | <td>(num_tokens, numTopk)</td> | 130 | <td>(num_tokens, numTopk)</td> |
| @@ -142,7 +134,7 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 142 | <td>numTopk</td> | 134 | <td>numTopk</td> |
| 143 | <td>Input</td> | 135 | <td>Input</td> |
| 144 | <td>Number of selected experts.</td> | 136 | <td>Number of selected experts.</td> |
| 145 | - <td>The value must be greater than or equal to 1. When probsOptional is not None, the value must be less than or equal to 512.</td> | 137 | + <td>The value must be in the range [1, 512].</td> |
| 146 | <td>INT64</td> | 138 | <td>INT64</td> |
| 147 | <td>-</td> | 139 | <td>-</td> |
| 148 | <td>-</td> | 140 | <td>-</td> |
| @@ -151,9 +143,9 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 151 | <tr> | 143 | <tr> |
| 152 | <td>rangeOptional</td> | 144 | <td>rangeOptional</td> |
| 153 | <td>Optional input</td> | 145 | <td>Optional input</td> |
| 154 | - <td>Valid range for EP split, with a size of 2.</td> | 146 | + <td>Valid range for EP split [start, end), with a size of 2. The element type is INT64.</td> |
| 155 | - <td>A null pointer can be passed. If a null pointer is passed, the default value {0,0} is used and the output is all 0.</td> | 147 | + <td>A null pointer can be passed. If a null pointer is passed, the default value {0,0} is used and the output is all 0. When it is not null, 0 <= start <= end <= start + shape[0] of permutedTokens must hold.</td> |
| 156 | - <td>-</td> | 148 | + <td>INT64</td> |
| 157 | <td>-</td> | 149 | <td>-</td> |
| 158 | <td>-</td> | 150 | <td>-</td> |
| 159 | <td>-</td> | 151 | <td>-</td> |
| @@ -173,7 +165,7 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 173 | <td>Optional input</td> | 165 | <td>Optional input</td> |
| 174 | <td>-</td> | 166 | <td>-</td> |
| 175 | <td>Reserved parameter. Currently, only a null pointer can be passed.</td> | 167 | <td>Reserved parameter. Currently, only a null pointer can be passed.</td> |
| 176 | - <td>-</td> | 168 | + <td>INT64</td> |
| 177 | <td>-</td> | 169 | <td>-</td> |
| 178 | <td>-</td> | 170 | <td>-</td> |
| 179 | <td>-</td> | 171 | <td>-</td> |
| @@ -290,7 +282,8 @@ aclnnStatus aclnnMoeTokenUnpermuteWithEp( | |||
| 290 | - Deterministic computation: | 282 | - Deterministic computation: |
| 291 | - aclnnMoeTokenUnpermuteWithEp defaults to a deterministic implementation. | 283 | - aclnnMoeTokenUnpermuteWithEp defaults to a deterministic implementation. |
| 292 | 284 | ||
| 293 | -- numTopk must be greater than or equal to 1. If probsOptional is not null, numTopk must be less than or equal to 512. | 285 | +- numTopk must be in the range [1, 512]. |
| 286 | +- sortedIndices must be 1D with length num_tokens*numTopk, where num_tokens is greater than or equal to 1. Empty tensors are not supported. | ||
| 294 | - Broadcast is not supported. | 287 | - Broadcast is not supported. |
| 295 | - Using -1 in the shape of aclTensor to represent a dynamic dimension or -2 to represent a dynamic rank is not supported. | 288 | - Using -1 in the shape of aclTensor to represent a dynamic dimension or -2 to represent a dynamic rank is not supported. |
| 296 | - paddedMode being `True` is not supported. | 289 | - paddedMode being `True` is not supported. |
| @@ -185,6 +185,7 @@ int main() | |||
| 185 | aclDestroyTensor(sortedIndices); | 185 | aclDestroyTensor(sortedIndices); |
| 186 | aclDestroyTensor(probsOptional); | 186 | aclDestroyTensor(probsOptional); |
| 187 | aclDestroyTensor(out); | 187 | aclDestroyTensor(out); |
| 188 | + aclDestroyIntArray(range); | ||
| 188 | 189 | ||
| 189 | // 7. 释放device资源 | 190 | // 7. 释放device资源 |
| 190 | aclrtFree(permutedTokensAddr); | 191 | aclrtFree(permutedTokensAddr); |
| @@ -0,0 +1,11 @@ | |||
| 1 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +add_graph_plugin_sources() | ||
| @@ -0,0 +1,29 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file moe_token_unpermute_with_ep_graph_infer.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | +#include "register/op_impl_registry.h" | ||
| 16 | +#include "log/log.h" | ||
| 17 | +using namespace ge; | ||
| 18 | +namespace ops { | ||
| 19 | + | ||
| 20 | +static constexpr int64_t UNPERMUTE_WITH_EP_INPUT_TOKENS = 0; | ||
| 21 | + | ||
| 22 | +static ge::graphStatus InferDataTypeForMoeTokenUnpermuteWithEp(gert::InferDataTypeContext* context) | ||
| 23 | +{ | ||
| 24 | + context->SetOutputDataType(0, context->GetInputDataType(UNPERMUTE_WITH_EP_INPUT_TOKENS)); | ||
| 25 | + return GRAPH_SUCCESS; | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +IMPL_OP(MoeTokenUnpermuteWithEp).InferDataType(InferDataTypeForMoeTokenUnpermuteWithEp); | ||
| 29 | +} // namespace ops | ||
| @@ -23,4 +23,11 @@ endif() | |||
| 23 | 23 | ||
| 24 | if(NOT BUILD_OPS_RTY_KERNEL) | 24 | if(NOT BUILD_OPS_RTY_KERNEL) |
| 25 | add_modules_sources(OPTYPE moe_token_unpermute_with_ep ACLNNTYPE aclnn) | 25 | add_modules_sources(OPTYPE moe_token_unpermute_with_ep ACLNNTYPE aclnn) |
| 26 | + | ||
| 27 | + # InferDataType 按规范放在 op_graph/ 下,标准构建宏不扫描 op_graph/*_graph_infer*.cpp, | ||
| 28 | + # 此处显式并入 infer obj(共享的 ophost_xxx_infer_obj),确保注册进入 liboptiling 与 cust_proto。 | ||
| 29 | + if(TARGET ${OPHOST_NAME}_infer_obj) | ||
| 30 | + target_sources(${OPHOST_NAME}_infer_obj PRIVATE | ||
| 31 | + ${CMAKE_CURRENT_SOURCE_DIR}/../op_graph/moe_token_unpermute_with_ep_graph_infer.cpp) | ||
| 32 | + endif() | ||
| 26 | endif() | 33 | endif() |
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "register/op_impl_registry.h" | 15 | #include "register/op_impl_registry.h" |
| 16 | #include "log/log.h" | 16 | #include "log/log.h" |
| 17 | #include "platform/platform_info.h" | 17 | #include "platform/platform_info.h" |
| 18 | +#include <string> | ||
| 18 | using namespace ge; | 19 | using namespace ge; |
| 19 | namespace ops { | 20 | namespace ops { |
| 20 | 21 | ||
| @@ -23,36 +24,84 @@ static constexpr int64_t UNPERMUTE_WITH_EP_INPUT_IDX = 1; | |||
| 23 | static constexpr int64_t UNPERMUTE_WITH_EP_INPUT_PROBS = 2; | 24 | static constexpr int64_t UNPERMUTE_WITH_EP_INPUT_PROBS = 2; |
| 24 | static constexpr int64_t UNPERMUTE_WITH_EP_OUTPUT_TOKENS = 0; | 25 | static constexpr int64_t UNPERMUTE_WITH_EP_OUTPUT_TOKENS = 0; |
| 25 | static constexpr int64_t UNPERMUTE_WITH_EP_ARRT_TOPK = 0; | 26 | static constexpr int64_t UNPERMUTE_WITH_EP_ARRT_TOPK = 0; |
| 26 | -static constexpr int64_t UNPERMUTE_WITH_EP_ARRT_RANGE = 1; | 27 | +static constexpr int64_t UNKNOWN_DIM = -1; |
| 28 | +static constexpr int64_t MAX_NUM_TOPK = 512; | ||
| 27 | 29 | ||
| 28 | static ge::graphStatus InferShapeForMoeTokenUnpermuteWithEp(gert::InferShapeContext* context) | 30 | static ge::graphStatus InferShapeForMoeTokenUnpermuteWithEp(gert::InferShapeContext* context) |
| 29 | { | 31 | { |
| 32 | + const char* nodeName = context->GetNodeName(); | ||
| 30 | const gert::Shape* permuted_inputs_shape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_TOKENS); | 33 | const gert::Shape* permuted_inputs_shape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_TOKENS); |
| 31 | - if (permuted_inputs_shape->GetDimNum() != 2) { | 34 | + const gert::Shape* sortedIndicesShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_IDX); |
| 32 | - return GRAPH_FAILED; | ||
| 33 | - } | ||
| 34 | - if (permuted_inputs_shape->GetDim(0) == 0 || permuted_inputs_shape->GetDim(1) == 0) { | ||
| 35 | - return GRAPH_FAILED; | ||
| 36 | - } | ||
| 37 | const gert::Shape* probs_shape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_PROBS); | 35 | const gert::Shape* probs_shape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_PROBS); |
| 38 | const auto* attrs = context->GetAttrs(); | 36 | const auto* attrs = context->GetAttrs(); |
| 37 | + | ||
| 38 | + OP_CHECK_IF( | ||
| 39 | + permuted_inputs_shape == nullptr || sortedIndicesShape == nullptr || attrs == nullptr, | ||
| 40 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 41 | + nodeName, "context", "permuted_tokens/sorted_indices shape or attrs is nullptr during infer shape."), | ||
| 42 | + return GRAPH_FAILED); | ||
| 39 | const int64_t* topk = attrs->GetAttrPointer<int64_t>(UNPERMUTE_WITH_EP_ARRT_TOPK); | 43 | const int64_t* topk = attrs->GetAttrPointer<int64_t>(UNPERMUTE_WITH_EP_ARRT_TOPK); |
| 40 | - const auto* range = attrs->GetAttrPointer<gert::ContinuousVector>(UNPERMUTE_WITH_EP_ARRT_RANGE); | 44 | + OP_CHECK_IF(topk == nullptr, OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "num_topk is nullptr."), |
| 41 | - int64_t inputTopK = *topk; | 45 | + return GRAPH_FAILED); |
| 46 | + const int64_t inputTopK = *topk; | ||
| 47 | + OP_CHECK_IF(inputTopK < 1, | ||
| 48 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "num_topk must be no less than 1."), | ||
| 49 | + return GRAPH_FAILED); | ||
| 50 | + OP_CHECK_IF(inputTopK > MAX_NUM_TOPK, | ||
| 51 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 52 | + nodeName, "num_topk", "num_topk must be no greater than " + std::to_string(MAX_NUM_TOPK) + "."), | ||
| 53 | + return GRAPH_FAILED); | ||
| 54 | + | ||
| 55 | + OP_CHECK_IF(permuted_inputs_shape->GetDimNum() != 2, | ||
| 56 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(nodeName, "permuted_tokens", | ||
| 57 | + std::to_string(permuted_inputs_shape->GetDimNum()), | ||
| 58 | + "permuted_tokens's shape is not 2D."), | ||
| 59 | + return GRAPH_FAILED); | ||
| 60 | + OP_CHECK_IF(permuted_inputs_shape->GetDim(0) == 0 || permuted_inputs_shape->GetDim(1) == 0, | ||
| 61 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", | ||
| 62 | + "permuted_tokens does not support empty tensor."), | ||
| 63 | + return GRAPH_FAILED); | ||
| 64 | + | ||
| 65 | + OP_CHECK_IF(sortedIndicesShape->GetDimNum() != 1, | ||
| 66 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(nodeName, "sorted_indices", | ||
| 67 | + std::to_string(sortedIndicesShape->GetDimNum()), | ||
| 68 | + "sorted_indices's shape is not 1D."), | ||
| 69 | + return GRAPH_FAILED); | ||
| 70 | + const int64_t indicesDim0 = sortedIndicesShape->GetDim(0); | ||
| 71 | + OP_CHECK_IF(indicesDim0 == 0, | ||
| 72 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", | ||
| 73 | + "sorted_indices does not support empty tensor."), | ||
| 74 | + return GRAPH_FAILED); | ||
| 75 | + | ||
| 42 | int64_t tokens_num; | 76 | int64_t tokens_num; |
| 43 | if (probs_shape == nullptr) { | 77 | if (probs_shape == nullptr) { |
| 44 | - const gert::Shape* indices_shape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_IDX); | 78 | + if (indicesDim0 == UNKNOWN_DIM) { |
| 45 | - int64_t indicesDim0 = indices_shape->GetDim(0); | 79 | + tokens_num = UNKNOWN_DIM; |
| 46 | - if (range == nullptr || range->GetSize() == 0) { | ||
| 47 | - tokens_num = indicesDim0; | ||
| 48 | } else { | 80 | } else { |
| 49 | - tokens_num = (indicesDim0 == -1) ? -1 : indicesDim0 / inputTopK; | 81 | + OP_CHECK_IF(indicesDim0 % inputTopK != 0 || indicesDim0 / inputTopK < 1, |
| 82 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 83 | + nodeName, "sorted_indices", | ||
| 84 | + "sorted_indices length(" + std::to_string(indicesDim0) + | ||
| 85 | + ") must be a positive multiple of num_topk(" + std::to_string(inputTopK) + ")."), | ||
| 86 | + return GRAPH_FAILED); | ||
| 87 | + tokens_num = indicesDim0 / inputTopK; | ||
| 50 | } | 88 | } |
| 51 | } else { | 89 | } else { |
| 90 | + OP_CHECK_IF(probs_shape->GetDimNum() != 2, | ||
| 91 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON( | ||
| 92 | + nodeName, "probs", std::to_string(probs_shape->GetDimNum()), "probs's shape is not 2D."), | ||
| 93 | + return GRAPH_FAILED); | ||
| 94 | + OP_CHECK_IF(probs_shape->GetDim(0) == 0 || probs_shape->GetDim(1) == 0, | ||
| 95 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "probs", "probs does not support empty tensor."), | ||
| 96 | + return GRAPH_FAILED); | ||
| 52 | tokens_num = probs_shape->GetDim(0); | 97 | tokens_num = probs_shape->GetDim(0); |
| 53 | } | 98 | } |
| 54 | 99 | ||
| 55 | gert::Shape* out_shape = context->GetOutputShape(UNPERMUTE_WITH_EP_OUTPUT_TOKENS); | 100 | gert::Shape* out_shape = context->GetOutputShape(UNPERMUTE_WITH_EP_OUTPUT_TOKENS); |
| 101 | + OP_CHECK_IF(out_shape == nullptr, | ||
| 102 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "unpermuted_tokens", | ||
| 103 | + "output shape is nullptr during infer shape."), | ||
| 104 | + return GRAPH_FAILED); | ||
| 56 | const int8_t out_dim_num = 2; | 105 | const int8_t out_dim_num = 2; |
| 57 | out_shape->SetDimNum(out_dim_num); | 106 | out_shape->SetDimNum(out_dim_num); |
| 58 | out_shape->SetDim(0, tokens_num); | 107 | out_shape->SetDim(0, tokens_num); |
| @@ -61,13 +110,5 @@ static ge::graphStatus InferShapeForMoeTokenUnpermuteWithEp(gert::InferShapeCont | |||
| 61 | return GRAPH_SUCCESS; | 110 | return GRAPH_SUCCESS; |
| 62 | } | 111 | } |
| 63 | 112 | ||
| 64 | -static ge::graphStatus InferDataTypeForMoeTokenUnpermuteWithEp(gert::InferDataTypeContext* context) | 113 | +IMPL_OP_INFERSHAPE(MoeTokenUnpermuteWithEp).InferShape(InferShapeForMoeTokenUnpermuteWithEp); |
| 65 | -{ | ||
| 66 | - context->SetOutputDataType(0, context->GetInputDataType(UNPERMUTE_WITH_EP_INPUT_TOKENS)); | ||
| 67 | - return GRAPH_SUCCESS; | ||
| 68 | -} | ||
| 69 | - | ||
| 70 | -IMPL_OP_INFERSHAPE(MoeTokenUnpermuteWithEp) | ||
| 71 | - .InferShape(InferShapeForMoeTokenUnpermuteWithEp) | ||
| 72 | - .InferDataType(InferDataTypeForMoeTokenUnpermuteWithEp); | ||
| 73 | } // namespace ops | 114 | } // namespace ops |
| @@ -14,6 +14,7 @@ | |||
| 14 | */ | 14 | */ |
| 15 | #include "log/log.h" | 15 | #include "log/log.h" |
| 16 | #include <string> | 16 | #include <string> |
| 17 | +#include "graph/utils/type_utils.h" | ||
| 17 | #include "moe_token_unpermute_with_ep_tiling.h" | 18 | #include "moe_token_unpermute_with_ep_tiling.h" |
| 18 | 19 | ||
| 19 | namespace optiling { | 20 | namespace optiling { |
| @@ -24,7 +25,10 @@ const static int64_t UNPERMUTE_WITH_EP_INPUT_PROBS = 2; | |||
| 24 | const static int64_t UNPERMUTE_WITH_EP_OUTPUT_TOKENS = 0; | 25 | const static int64_t UNPERMUTE_WITH_EP_OUTPUT_TOKENS = 0; |
| 25 | const static int64_t UNPERMUTE_WITH_EP_ARRT_TOPK = 0; | 26 | const static int64_t UNPERMUTE_WITH_EP_ARRT_TOPK = 0; |
| 26 | const static int64_t UNPERMUTE_WITH_EP_ARRT_RANGE = 1; | 27 | const static int64_t UNPERMUTE_WITH_EP_ARRT_RANGE = 1; |
| 28 | +const static int64_t UNPERMUTE_WITH_EP_ARRT_PADDED_MODE = 2; | ||
| 29 | +const static int64_t UNPERMUTE_WITH_EP_ARRT_RESTORE_SHAPE = 3; | ||
| 27 | const static int64_t RANGE_SIZE = 2; | 30 | const static int64_t RANGE_SIZE = 2; |
| 31 | +const static int64_t MAX_NUM_TOPK = 512; | ||
| 28 | 32 | ||
| 29 | ge::graphStatus TilingMoeTokenUnpermuteWithEp(gert::TilingContext* context); | 33 | ge::graphStatus TilingMoeTokenUnpermuteWithEp(gert::TilingContext* context); |
| 30 | 34 | ||
| @@ -59,6 +63,16 @@ static inline int64_t GetLengthByType(const int32_t dtype) | |||
| 59 | } | 63 | } |
| 60 | } | 64 | } |
| 61 | 65 | ||
| 66 | +static inline bool IsSupportedFloatDtype(const ge::DataType dtype) | ||
| 67 | +{ | ||
| 68 | + return dtype == ge::DT_FLOAT || dtype == ge::DT_FLOAT16 || dtype == ge::DT_BF16; | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +static inline std::string DtypeStr(const ge::DataType dtype) | ||
| 72 | +{ | ||
| 73 | + return ge::TypeUtils::DataTypeToSerialString(dtype); | ||
| 74 | +} | ||
| 75 | + | ||
| 62 | static inline int64_t safeMod(const int64_t a, const int64_t b) | 76 | static inline int64_t safeMod(const int64_t a, const int64_t b) |
| 63 | { | 77 | { |
| 64 | return b == 0 ? 0 : a % b; | 78 | return b == 0 ? 0 : a % b; |
| @@ -93,26 +107,102 @@ static inline int64_t ComputeMaxHiddenSize(MoeTokenUnpermuteWithEpParam& param, | |||
| 93 | return AlignN(maxHiddenSize - ALIGN_512, ALIGN_512); | 107 | return AlignN(maxHiddenSize - ALIGN_512, ALIGN_512); |
| 94 | } | 108 | } |
| 95 | 109 | ||
| 110 | +static inline ge::graphStatus CheckDtypes(const gert::TilingContext* context, const bool haveProbs) | ||
| 111 | +{ | ||
| 112 | + auto nodeName = context->GetNodeName(); | ||
| 113 | + const auto* tokensDesc = context->GetInputDesc(UNPERMUTE_WITH_EP_INPUT_TOKENS); | ||
| 114 | + OP_CHECK_IF(tokensDesc == nullptr, | ||
| 115 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", "desc is nullptr."), | ||
| 116 | + return ge::GRAPH_FAILED); | ||
| 117 | + const ge::DataType tokensDtype = tokensDesc->GetDataType(); | ||
| 118 | + const auto* indicesDesc = context->GetInputDesc(UNPERMUTE_WITH_EP_INPUT_IDX); | ||
| 119 | + OP_CHECK_IF(indicesDesc == nullptr, | ||
| 120 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", "desc is nullptr."), | ||
| 121 | + return ge::GRAPH_FAILED); | ||
| 122 | + const ge::DataType indicesDtype = indicesDesc->GetDataType(); | ||
| 123 | + OP_CHECK_IF(!IsSupportedFloatDtype(tokensDtype), | ||
| 124 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(nodeName, "permuted_tokens", DtypeStr(tokensDtype).c_str(), | ||
| 125 | + "permuted_tokens only supports float32/float16/bfloat16."), | ||
| 126 | + return ge::GRAPH_FAILED); | ||
| 127 | + OP_CHECK_IF(indicesDtype != ge::DT_INT32, | ||
| 128 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(nodeName, "sorted_indices", DtypeStr(indicesDtype).c_str(), | ||
| 129 | + "sorted_indices only supports int32."), | ||
| 130 | + return ge::GRAPH_FAILED); | ||
| 131 | + if (haveProbs) { | ||
| 132 | + const auto* probsDesc = context->GetOptionalInputDesc(UNPERMUTE_WITH_EP_INPUT_PROBS); | ||
| 133 | + OP_CHECK_IF(probsDesc == nullptr, | ||
| 134 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "probs", "probs desc is nullptr."), | ||
| 135 | + return ge::GRAPH_FAILED); | ||
| 136 | + const ge::DataType probsDtype = probsDesc->GetDataType(); | ||
| 137 | + OP_CHECK_IF(!IsSupportedFloatDtype(probsDtype), | ||
| 138 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(nodeName, "probs", DtypeStr(probsDtype).c_str(), | ||
| 139 | + "probs only supports float32/float16/bfloat16."), | ||
| 140 | + return ge::GRAPH_FAILED); | ||
| 141 | + } | ||
| 142 | + return ge::GRAPH_SUCCESS; | ||
| 143 | +} | ||
| 144 | + | ||
| 96 | static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert::TilingContext* context, | 145 | static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert::TilingContext* context, |
| 97 | const bool isUnpermute) | 146 | const bool isUnpermute) |
| 98 | { | 147 | { |
| 99 | - const gert::StorageShape* tokensShape = context->GetInputShape(0); | 148 | + const gert::StorageShape* tokensShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_TOKENS); |
| 100 | - const gert::StorageShape* indicesShape = context->GetInputShape(1); | 149 | + const gert::StorageShape* indicesShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_IDX); |
| 101 | - const gert::StorageShape* probsShape = context->GetInputShape(2); | 150 | + const gert::StorageShape* probsShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_PROBS); |
| 102 | - auto dataTensor0 = context->GetInputTensor(0); | 151 | + auto dataTensor0 = context->GetInputTensor(UNPERMUTE_WITH_EP_INPUT_TOKENS); |
| 103 | - auto dataTensor1 = context->GetInputTensor(1); | 152 | + auto dataTensor1 = context->GetInputTensor(UNPERMUTE_WITH_EP_INPUT_IDX); |
| 104 | auto nodeName = context->GetNodeName(); | 153 | auto nodeName = context->GetNodeName(); |
| 105 | - const int64_t* tmpTopK = context->GetAttrs()->GetAttrPointer<int64_t>(0); | 154 | + auto attrs = context->GetAttrs(); |
| 106 | - int inputTopK = *tmpTopK; | 155 | + OP_CHECK_IF(attrs == nullptr, OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "attrs", "attrs is nullptr."), |
| 156 | + return ge::GRAPH_FAILED); | ||
| 157 | + const int64_t* tmpTopK = attrs->GetAttrPointer<int64_t>(UNPERMUTE_WITH_EP_ARRT_TOPK); | ||
| 158 | + OP_CHECK_IF(tmpTopK == nullptr, | ||
| 159 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "num_topk is nullptr."), | ||
| 160 | + return ge::GRAPH_FAILED); | ||
| 161 | + const int64_t inputTopK = *tmpTopK; | ||
| 107 | 162 | ||
| 108 | OP_CHECK_IF(inputTopK < 1, | 163 | OP_CHECK_IF(inputTopK < 1, |
| 109 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "input num TopK cannot less than 1."), | 164 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "input num TopK cannot less than 1."), |
| 110 | return ge::GRAPH_FAILED); | 165 | return ge::GRAPH_FAILED); |
| 166 | + OP_CHECK_IF( | ||
| 167 | + inputTopK > MAX_NUM_TOPK, | ||
| 168 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", | ||
| 169 | + "input num TopK(" + std::to_string(inputTopK) + | ||
| 170 | + ") can not larger than " + std::to_string(MAX_NUM_TOPK) + "."), | ||
| 171 | + return ge::GRAPH_FAILED); | ||
| 111 | OP_CHECK_IF(tokensShape == nullptr || indicesShape == nullptr || dataTensor0 == nullptr || dataTensor1 == nullptr, | 172 | OP_CHECK_IF(tokensShape == nullptr || indicesShape == nullptr || dataTensor0 == nullptr || dataTensor1 == nullptr, |
| 112 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(Ops::Base::GetOpInfo(nodeName), | 173 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(Ops::Base::GetOpInfo(nodeName), |
| 113 | "tokensShape, indicesShape, dataTensor0, dataTensor1", | 174 | "tokensShape, indicesShape, dataTensor0, dataTensor1", |
| 114 | "permutedTokens or sortedIndices is nullptr."), | 175 | "permutedTokens or sortedIndices is nullptr."), |
| 115 | return ge::GRAPH_FAILED); | 176 | return ge::GRAPH_FAILED); |
| 177 | + if (CheckDtypes(context, probsShape != nullptr) == ge::GRAPH_FAILED) { | ||
| 178 | + return ge::GRAPH_FAILED; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + const bool* paddedModePtr = attrs->GetAttrPointer<bool>(UNPERMUTE_WITH_EP_ARRT_PADDED_MODE); | ||
| 182 | + OP_CHECK_IF( | ||
| 183 | + paddedModePtr != nullptr && *paddedModePtr, | ||
| 184 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(nodeName, "padded_mode", "true", "padded_mode only supports false."), | ||
| 185 | + return ge::GRAPH_FAILED); | ||
| 186 | + // restore_shape is an unsupported (must be empty) optional list attribute. | ||
| 187 | + // The generated aclnn wrapper encodes a null restoreShapeOptional as a | ||
| 188 | + // single-element array, but the encoded value is not stable: it is [0] on | ||
| 189 | + // the plain path while it can be [1] when AutoContiguous nodes are inserted | ||
| 190 | + // for non-contiguous inputs. A single-element list cannot be a valid shape, | ||
| 191 | + // so only a list with two or more elements is treated as genuinely | ||
| 192 | + // provided; the read is additionally guarded by the real attribute count | ||
| 193 | + // because this last attribute may be absent when no default is materialized. | ||
| 194 | + bool restoreShapeProvided = false; | ||
| 195 | + const size_t attrNum = attrs->GetAttrNum(); | ||
| 196 | + if (attrNum > static_cast<size_t>(UNPERMUTE_WITH_EP_ARRT_RESTORE_SHAPE)) { | ||
| 197 | + const gert::ContinuousVector* restoreShapePtr = | ||
| 198 | + attrs->GetAttrPointer<gert::ContinuousVector>(UNPERMUTE_WITH_EP_ARRT_RESTORE_SHAPE); | ||
| 199 | + restoreShapeProvided = (restoreShapePtr != nullptr && restoreShapePtr->GetSize() > 1); | ||
| 200 | + } | ||
| 201 | + OP_CHECK_IF(restoreShapeProvided, | ||
| 202 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(nodeName, "restore_shape", "non-empty", | ||
| 203 | + "restore_shape is not supported and must be empty."), | ||
| 204 | + return ge::GRAPH_FAILED); | ||
| 205 | + | ||
| 116 | OP_CHECK_IF( | 206 | OP_CHECK_IF( |
| 117 | tokensShape->GetStorageShape().GetDimNum() != 2, | 207 | tokensShape->GetStorageShape().GetDimNum() != 2, |
| 118 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", "permutedTokens's shape is not 2D."), | 208 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", "permutedTokens's shape is not 2D."), |
| @@ -121,11 +211,17 @@ static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert: | |||
| 121 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", | 211 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", |
| 122 | "permutedTokens does not support empty tensor."), | 212 | "permutedTokens does not support empty tensor."), |
| 123 | return ge::GRAPH_FAILED); | 213 | return ge::GRAPH_FAILED); |
| 214 | + OP_CHECK_IF(indicesShape->GetStorageShape().GetDimNum() != 1, | ||
| 215 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(nodeName, "sorted_indices", | ||
| 216 | + std::to_string(indicesShape->GetStorageShape().GetDimNum()), | ||
| 217 | + "sortedIndices's shape is not 1D."), | ||
| 218 | + return ge::GRAPH_FAILED); | ||
| 124 | OP_CHECK_IF(indicesShape->GetStorageShape().GetDim(0) == 0, | 219 | OP_CHECK_IF(indicesShape->GetStorageShape().GetDim(0) == 0, |
| 125 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", | 220 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", |
| 126 | "sortedIndices does not support empty tensor."), | 221 | "sortedIndices does not support empty tensor."), |
| 127 | return ge::GRAPH_FAILED); | 222 | return ge::GRAPH_FAILED); |
| 128 | 223 | ||
| 224 | + const int64_t totalLength = indicesShape->GetStorageShape().GetDim(0); | ||
| 129 | if (probsShape != nullptr) { | 225 | if (probsShape != nullptr) { |
| 130 | int64_t probsDimNum = probsShape->GetStorageShape().GetDimNum(); | 226 | int64_t probsDimNum = probsShape->GetStorageShape().GetDimNum(); |
| 131 | if (isUnpermute) { | 227 | if (isUnpermute) { |
| @@ -137,7 +233,6 @@ static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert: | |||
| 137 | 233 | ||
| 138 | int64_t tokensNum = probsShape->GetStorageShape().GetDim(0); | 234 | int64_t tokensNum = probsShape->GetStorageShape().GetDim(0); |
| 139 | int64_t topK = probsShape->GetStorageShape().GetDim(1); | 235 | int64_t topK = probsShape->GetStorageShape().GetDim(1); |
| 140 | - int64_t totalLength = indicesShape->GetStorageShape().GetDim(0); | ||
| 141 | 236 | ||
| 142 | OP_CHECK_IF( | 237 | OP_CHECK_IF( |
| 143 | topK != inputTopK, | 238 | topK != inputTopK, |
| @@ -149,9 +244,6 @@ static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert: | |||
| 149 | nodeName, "permuted_tokens, probs", | 244 | nodeName, "permuted_tokens, probs", |
| 150 | "permutedTokens's dim(0) is not equal to probs's dim(0) * probs's dim(1)."), | 245 | "permutedTokens's dim(0) is not equal to probs's dim(0) * probs's dim(1)."), |
| 151 | return ge::GRAPH_FAILED); | 246 | return ge::GRAPH_FAILED); |
| 152 | - OP_CHECK_IF(topK > 512, | ||
| 153 | - OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "topK can not larger than 512."), | ||
| 154 | - return ge::GRAPH_FAILED); | ||
| 155 | } else { | 247 | } else { |
| 156 | OP_CHECK_IF(probsDimNum != 1, | 248 | OP_CHECK_IF(probsDimNum != 1, |
| 157 | OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON( | 249 | OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON( |
| @@ -159,38 +251,89 @@ static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert: | |||
| 159 | "permutedProbsGrad's shape(" + std::to_string(probsDimNum) + ") is not 1D."), | 251 | "permutedProbsGrad's shape(" + std::to_string(probsDimNum) + ") is not 1D."), |
| 160 | return ge::GRAPH_FAILED); | 252 | return ge::GRAPH_FAILED); |
| 161 | } | 253 | } |
| 254 | + } else if (isUnpermute) { | ||
| 255 | + OP_CHECK_IF(safeMod(totalLength, inputTopK) != 0 || safeDiv(totalLength, inputTopK) < 1, | ||
| 256 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", | ||
| 257 | + "sortedIndices length(" + std::to_string(totalLength) + | ||
| 258 | + ") must be a positive multiple of num_topk(" + | ||
| 259 | + std::to_string(inputTopK) + ")."), | ||
| 260 | + return ge::GRAPH_FAILED); | ||
| 261 | + } | ||
| 262 | + | ||
| 263 | + if (isUnpermute) { | ||
| 264 | + const gert::StorageShape* outShape = context->GetOutputShape(UNPERMUTE_WITH_EP_OUTPUT_TOKENS); | ||
| 265 | + if (outShape != nullptr) { | ||
| 266 | + const gert::Shape& outStorageShape = outShape->GetStorageShape(); | ||
| 267 | + const int64_t hiddenSize = tokensShape->GetStorageShape().GetDim(1); | ||
| 268 | + const int64_t expectTokens = | ||
| 269 | + (probsShape != nullptr) ? probsShape->GetStorageShape().GetDim(0) : safeDiv(totalLength, inputTopK); | ||
| 270 | + OP_CHECK_IF(outStorageShape.GetDimNum() != 2, | ||
| 271 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(nodeName, "unpermuted_tokens", | ||
| 272 | + std::to_string(outStorageShape.GetDimNum()), | ||
| 273 | + "unpermuted_tokens's shape is not 2D."), | ||
| 274 | + return ge::GRAPH_FAILED); | ||
| 275 | + OP_CHECK_IF(outStorageShape.GetDim(0) >= 0 && outStorageShape.GetDim(0) != expectTokens, | ||
| 276 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 277 | + nodeName, "unpermuted_tokens", | ||
| 278 | + "unpermuted_tokens dim(0)(" + std::to_string(outStorageShape.GetDim(0)) + | ||
| 279 | + ") is not equal to num_tokens(" + std::to_string(expectTokens) + ")."), | ||
| 280 | + return ge::GRAPH_FAILED); | ||
| 281 | + OP_CHECK_IF(outStorageShape.GetDim(1) >= 0 && outStorageShape.GetDim(1) != hiddenSize, | ||
| 282 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 283 | + nodeName, "unpermuted_tokens", | ||
| 284 | + "unpermuted_tokens dim(1)(" + std::to_string(outStorageShape.GetDim(1)) + | ||
| 285 | + ") is not equal to hidden_size(" + std::to_string(hiddenSize) + ")."), | ||
| 286 | + return ge::GRAPH_FAILED); | ||
| 287 | + } | ||
| 162 | } | 288 | } |
| 163 | 289 | ||
| 164 | return ge::GRAPH_SUCCESS; | 290 | return ge::GRAPH_SUCCESS; |
| 165 | } | 291 | } |
| 166 | 292 | ||
| 167 | -static inline void Init(const gert::TilingContext* context, const int64_t topK, MoeTokenUnpermuteWithEpParam& param, | 293 | +static inline ge::graphStatus Init(const gert::TilingContext* context, const int64_t topK, |
| 168 | - const bool isUnpermute) | 294 | + MoeTokenUnpermuteWithEpParam& param, const bool isUnpermute) |
| 169 | { | 295 | { |
| 170 | - auto ascendPlaform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo()); | 296 | + auto nodeName = context->GetNodeName(); |
| 297 | + auto platformInfo = context->GetPlatformInfo(); | ||
| 298 | + OP_CHECK_IF(platformInfo == nullptr, | ||
| 299 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "platform_info", "platform info is nullptr."), | ||
| 300 | + return ge::GRAPH_FAILED); | ||
| 301 | + auto ascendPlaform = platform_ascendc::PlatformAscendC(platformInfo); | ||
| 171 | param.core.maxCoreNum = static_cast<int64_t>(ascendPlaform.GetCoreNumAiv()); | 302 | param.core.maxCoreNum = static_cast<int64_t>(ascendPlaform.GetCoreNumAiv()); |
| 172 | - uint64_t maxCoreMemory; | 303 | + OP_CHECK_IF(param.core.maxCoreNum <= 0, |
| 304 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "max_core_num", "AIV core num must be positive."), | ||
| 305 | + return ge::GRAPH_FAILED); | ||
| 306 | + uint64_t maxCoreMemory = 0; | ||
| 173 | ascendPlaform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, maxCoreMemory); | 307 | ascendPlaform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, maxCoreMemory); |
| 174 | param.core.maxCoreMemory = static_cast<int64_t>(maxCoreMemory); | 308 | param.core.maxCoreMemory = static_cast<int64_t>(maxCoreMemory); |
| 309 | + OP_CHECK_IF(param.core.maxCoreMemory == 0, | ||
| 310 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "max_core_memory", "UB size must be positive."), | ||
| 311 | + return ge::GRAPH_FAILED); | ||
| 175 | 312 | ||
| 176 | const gert::StorageShape* tokensShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_TOKENS); | 313 | const gert::StorageShape* tokensShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_TOKENS); |
| 177 | const gert::StorageShape* sortedIndicesShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_IDX); | 314 | const gert::StorageShape* sortedIndicesShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_IDX); |
| 178 | const gert::StorageShape* probsShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_PROBS); | 315 | const gert::StorageShape* probsShape = context->GetInputShape(UNPERMUTE_WITH_EP_INPUT_PROBS); |
| 179 | 316 | ||
| 180 | - param.input.tokensDtypeSize = | 317 | + const auto* tokensDesc = context->GetInputDesc(UNPERMUTE_WITH_EP_INPUT_TOKENS); |
| 181 | - GetLengthByType(context->GetInputTensor(UNPERMUTE_WITH_EP_INPUT_TOKENS)->GetDataType()); | 318 | + OP_CHECK_IF(tokensDesc == nullptr, |
| 182 | - param.input.indicesDtypeSize = GetLengthByType(context->GetInputTensor(UNPERMUTE_WITH_EP_INPUT_IDX)->GetDataType()); | 319 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", "desc is nullptr."), |
| 320 | + return ge::GRAPH_FAILED); | ||
| 321 | + const auto* indicesDesc = context->GetInputDesc(UNPERMUTE_WITH_EP_INPUT_IDX); | ||
| 322 | + OP_CHECK_IF(indicesDesc == nullptr, | ||
| 323 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", "desc is nullptr."), | ||
| 324 | + return ge::GRAPH_FAILED); | ||
| 325 | + param.input.tokensDtypeSize = GetLengthByType(tokensDesc->GetDataType()); | ||
| 326 | + param.input.indicesDtypeSize = GetLengthByType(indicesDesc->GetDataType()); | ||
| 183 | param.input.isUnpermute = isUnpermute; | 327 | param.input.isUnpermute = isUnpermute; |
| 184 | 328 | ||
| 185 | auto attrPtr = context->GetAttrs(); | 329 | auto attrPtr = context->GetAttrs(); |
| 186 | const int64_t* tmpTopK = attrPtr->GetAttrPointer<int64_t>(UNPERMUTE_WITH_EP_ARRT_TOPK); | 330 | const int64_t* tmpTopK = attrPtr->GetAttrPointer<int64_t>(UNPERMUTE_WITH_EP_ARRT_TOPK); |
| 187 | - int inputTopK = *tmpTopK; | 331 | + const int64_t inputTopK = *tmpTopK; |
| 188 | auto rangePtr = attrPtr->GetAttrPointer<gert::ContinuousVector>(UNPERMUTE_WITH_EP_ARRT_RANGE); | 332 | auto rangePtr = attrPtr->GetAttrPointer<gert::ContinuousVector>(UNPERMUTE_WITH_EP_ARRT_RANGE); |
| 189 | - if (rangePtr != nullptr) { | 333 | + if (rangePtr != nullptr && rangePtr->GetSize() != 0) { |
| 190 | OP_CHECK_IF(rangePtr->GetSize() != RANGE_SIZE, | 334 | OP_CHECK_IF(rangePtr->GetSize() != RANGE_SIZE, |
| 191 | - OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "range", | 335 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "range", "the size of range only support 2"), |
| 192 | - "the size of range only support 2"), | 336 | + return ge::GRAPH_FAILED); |
| 193 | - return); | ||
| 194 | const int64_t* rangeList = reinterpret_cast<const int64_t*>(rangePtr->GetData()); | 337 | const int64_t* rangeList = reinterpret_cast<const int64_t*>(rangePtr->GetData()); |
| 195 | param.input.start = rangeList[0]; | 338 | param.input.start = rangeList[0]; |
| 196 | param.input.end = rangeList[1]; | 339 | param.input.end = rangeList[1]; |
| @@ -203,10 +346,28 @@ static inline void Init(const gert::TilingContext* context, const int64_t topK, | |||
| 203 | param.input.hiddenSize = tokensShape->GetStorageShape().GetDim(1); | 346 | param.input.hiddenSize = tokensShape->GetStorageShape().GetDim(1); |
| 204 | param.input.totalLength = sortedIndicesShape->GetStorageShape().GetDim( | 347 | param.input.totalLength = sortedIndicesShape->GetStorageShape().GetDim( |
| 205 | 0); // tokens可能存在numOutTokens,因此totalLength从sortedIndices获取。 | 348 | 0); // tokens可能存在numOutTokens,因此totalLength从sortedIndices获取。 |
| 349 | + | ||
| 350 | + OP_CHECK_IF(param.input.start < 0 || param.input.start > param.input.end, | ||
| 351 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "range", | ||
| 352 | + "range must be [start, end] with 0 <= start <= end, got [" + | ||
| 353 | + std::to_string(param.input.start) + ", " + | ||
| 354 | + std::to_string(param.input.end) + "]."), | ||
| 355 | + return ge::GRAPH_FAILED); | ||
| 356 | + OP_CHECK_IF( | ||
| 357 | + param.input.end - param.input.start > param.input.numOutTokens, | ||
| 358 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "range", | ||
| 359 | + "range span(" + std::to_string(param.input.end - param.input.start) + | ||
| 360 | + ") can not be larger than permuted_tokens dim(0)(" + | ||
| 361 | + std::to_string(param.input.numOutTokens) + ")."), | ||
| 362 | + return ge::GRAPH_FAILED); | ||
| 363 | + | ||
| 206 | if (isUnpermute) { | 364 | if (isUnpermute) { |
| 207 | if (probsShape != nullptr) { | 365 | if (probsShape != nullptr) { |
| 208 | - auto dataTensor2 = context->GetInputTensor(2); | 366 | + const auto* probsDesc = context->GetOptionalInputDesc(UNPERMUTE_WITH_EP_INPUT_PROBS); |
| 209 | - param.input.probsDtypeSize = GetLengthByType(dataTensor2->GetDataType()); | 367 | + OP_CHECK_IF(probsDesc == nullptr, |
| 368 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "probs", "probs desc is nullptr."), | ||
| 369 | + return ge::GRAPH_FAILED); | ||
| 370 | + param.input.probsDtypeSize = GetLengthByType(probsDesc->GetDataType()); | ||
| 210 | param.input.haveProbs = true; | 371 | param.input.haveProbs = true; |
| 211 | param.input.tokensNum = probsShape->GetStorageShape().GetDim(0); | 372 | param.input.tokensNum = probsShape->GetStorageShape().GetDim(0); |
| 212 | param.input.topK = probsShape->GetStorageShape().GetDim(1); | 373 | param.input.topK = probsShape->GetStorageShape().GetDim(1); |
| @@ -223,6 +384,7 @@ static inline void Init(const gert::TilingContext* context, const int64_t topK, | |||
| 223 | param.input.haveProbs = (probsShape != nullptr) ? true : false; | 384 | param.input.haveProbs = (probsShape != nullptr) ? true : false; |
| 224 | param.input.tokensNum = safeDiv(param.input.totalLength, param.input.topK); | 385 | param.input.tokensNum = safeDiv(param.input.totalLength, param.input.topK); |
| 225 | } | 386 | } |
| 387 | + return ge::GRAPH_SUCCESS; | ||
| 226 | } | 388 | } |
| 227 | 389 | ||
| 228 | static void SetCoreNum(MoeTokenUnpermuteWithEpParam& param) | 390 | static void SetCoreNum(MoeTokenUnpermuteWithEpParam& param) |
| @@ -234,9 +396,14 @@ static void SetCoreNum(MoeTokenUnpermuteWithEpParam& param) | |||
| 234 | } | 396 | } |
| 235 | } | 397 | } |
| 236 | 398 | ||
| 237 | -static inline void TilingHiddenSize(MoeTokenUnpermuteWithEpParam& param) | 399 | +static inline ge::graphStatus TilingHiddenSize(MoeTokenUnpermuteWithEpParam& param) |
| 238 | { | 400 | { |
| 239 | int64_t maxHiddenSize = ComputeMaxHiddenSize(param, MIN_BUFFER_NUM); | 401 | int64_t maxHiddenSize = ComputeMaxHiddenSize(param, MIN_BUFFER_NUM); |
| 402 | + OP_CHECK_IF(maxHiddenSize <= 0, | ||
| 403 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 404 | + "MoeTokenUnpermuteWithEp", "hidden_size", | ||
| 405 | + "UB is too small to hold any hidden_size block for the current dtype."), | ||
| 406 | + return ge::GRAPH_FAILED); | ||
| 240 | if (AlignN(param.input.hiddenSize, ALIGN_512) <= maxHiddenSize) { | 407 | if (AlignN(param.input.hiddenSize, ALIGN_512) <= maxHiddenSize) { |
| 241 | param.hiddenTiling.length = param.input.hiddenSize; | 408 | param.hiddenTiling.length = param.input.hiddenSize; |
| 242 | param.hiddenTiling.remain = 0; | 409 | param.hiddenTiling.remain = 0; |
| @@ -246,6 +413,7 @@ static inline void TilingHiddenSize(MoeTokenUnpermuteWithEpParam& param) | |||
| 246 | param.hiddenTiling.remain = safeMod(param.input.hiddenSize, maxHiddenSize); | 413 | param.hiddenTiling.remain = safeMod(param.input.hiddenSize, maxHiddenSize); |
| 247 | param.hiddenTiling.num = safeDiv(param.input.hiddenSize, maxHiddenSize); | 414 | param.hiddenTiling.num = safeDiv(param.input.hiddenSize, maxHiddenSize); |
| 248 | } | 415 | } |
| 416 | + return ge::GRAPH_SUCCESS; | ||
| 249 | } | 417 | } |
| 250 | 418 | ||
| 251 | static inline void SetBufferNum(MoeTokenUnpermuteWithEpParam& param) | 419 | static inline void SetBufferNum(MoeTokenUnpermuteWithEpParam& param) |
| @@ -266,7 +434,7 @@ static inline void ComputeRemainMemerySpace(MoeTokenUnpermuteWithEpParam& param) | |||
| 266 | param.core.remainMemorySpace -= ALIGN_256; | 434 | param.core.remainMemorySpace -= ALIGN_256; |
| 267 | } | 435 | } |
| 268 | 436 | ||
| 269 | -static inline void TilingToken(MoeTokenUnpermuteWithEpParam& param) | 437 | +static inline ge::graphStatus TilingToken(MoeTokenUnpermuteWithEpParam& param) |
| 270 | { | 438 | { |
| 271 | param.tokenPerCore.length = safeDiv(param.input.tokensNum, param.core.usedCoreNum); | 439 | param.tokenPerCore.length = safeDiv(param.input.tokensNum, param.core.usedCoreNum); |
| 272 | param.tokenPerCore.num = param.core.usedCoreNum; | 440 | param.tokenPerCore.num = param.core.usedCoreNum; |
| @@ -288,24 +456,41 @@ static inline void TilingToken(MoeTokenUnpermuteWithEpParam& param) | |||
| 288 | param.tokenTiling.num = 1; | 456 | param.tokenTiling.num = 1; |
| 289 | } else { | 457 | } else { |
| 290 | int64_t maxTokenSize = safeDiv(param.core.remainMemorySpace, (param.input.topK * unitTokenSpace)); | 458 | int64_t maxTokenSize = safeDiv(param.core.remainMemorySpace, (param.input.topK * unitTokenSpace)); |
| 459 | + OP_CHECK_IF( | ||
| 460 | + param.input.topK <= 0 || unitTokenSpace <= 0 || param.core.remainMemorySpace <= 0 || maxTokenSize <= 0, | ||
| 461 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 462 | + "MoeTokenUnpermuteWithEp", "num_topk", | ||
| 463 | + "num_topk(" + std::to_string(param.input.topK) + | ||
| 464 | + ") is too large for the available UB to hold sorted_indices/probs."), | ||
| 465 | + return ge::GRAPH_FAILED); | ||
| 291 | param.tokenTiling.length = maxTokenSize; | 466 | param.tokenTiling.length = maxTokenSize; |
| 292 | param.tokenTiling.remain = safeMod(param.tokenPerCore.length, maxTokenSize); | 467 | param.tokenTiling.remain = safeMod(param.tokenPerCore.length, maxTokenSize); |
| 293 | param.tokenTiling.num = safeDiv(param.tokenPerCore.length, maxTokenSize); | 468 | param.tokenTiling.num = safeDiv(param.tokenPerCore.length, maxTokenSize); |
| 294 | } | 469 | } |
| 470 | + OP_CHECK_IF(param.tokenTiling.length <= 0, | ||
| 471 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON("MoeTokenUnpermuteWithEp", "token_tiling", | ||
| 472 | + "token tiling length must be positive."), | ||
| 473 | + return ge::GRAPH_FAILED); | ||
| 474 | + return ge::GRAPH_SUCCESS; | ||
| 295 | } | 475 | } |
| 296 | /* | 476 | /* |
| 297 | tilingKey计算规则 | 477 | tilingKey计算规则 |
| 298 | 0表示probs为None, | 478 | 0表示probs为None, |
| 299 | 1 2 3 表示 DT_FLOAT DT_FLOAT16 DT_BF16 | 479 | 1 2 3 表示 DT_FLOAT DT_FLOAT16 DT_BF16 |
| 300 | */ | 480 | */ |
| 301 | -static inline void SetTilingKey(const gert::TilingContext* context, const bool isUnpermute, | 481 | +static inline ge::graphStatus SetTilingKey(const gert::TilingContext* context, const bool isUnpermute, |
| 302 | - MoeTokenUnpermuteWithEpParam& param) | 482 | + MoeTokenUnpermuteWithEpParam& param) |
| 303 | { | 483 | { |
| 304 | if (param.input.haveProbs) { | 484 | if (param.input.haveProbs) { |
| 305 | // 存在probs | 485 | // 存在probs |
| 306 | if (isUnpermute == true) { | 486 | if (isUnpermute == true) { |
| 307 | // 在unpermute的条件下 | 487 | // 在unpermute的条件下 |
| 308 | - int64_t dataTensor2Type = context->GetInputTensor(2)->GetDataType(); | 488 | + const auto* probsDesc = context->GetOptionalInputDesc(UNPERMUTE_WITH_EP_INPUT_PROBS); |
| 489 | + OP_CHECK_IF( | ||
| 490 | + probsDesc == nullptr, | ||
| 491 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "probs", "probs desc is nullptr."), | ||
| 492 | + return ge::GRAPH_FAILED); | ||
| 493 | + int64_t dataTensor2Type = probsDesc->GetDataType(); | ||
| 309 | if (dataTensor2Type == ge::DT_FLOAT) { | 494 | if (dataTensor2Type == ge::DT_FLOAT) { |
| 310 | param.core.tilingKey = TILINGKEY_FLOAT; | 495 | param.core.tilingKey = TILINGKEY_FLOAT; |
| 311 | } else if (dataTensor2Type == ge::DT_FLOAT16) { | 496 | } else if (dataTensor2Type == ge::DT_FLOAT16) { |
| @@ -313,8 +498,10 @@ static inline void SetTilingKey(const gert::TilingContext* context, const bool i | |||
| 313 | } else if (dataTensor2Type == ge::DT_BF16) { | 498 | } else if (dataTensor2Type == ge::DT_BF16) { |
| 314 | param.core.tilingKey = TILINGKEY_BF16; | 499 | param.core.tilingKey = TILINGKEY_BF16; |
| 315 | } else { | 500 | } else { |
| 316 | - OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "probs", | 501 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(context->GetNodeName(), "probs", |
| 317 | - "the data type of probs is incorrect"); | 502 | + DtypeStr(static_cast<ge::DataType>(dataTensor2Type)).c_str(), |
| 503 | + "the data type of probs is incorrect"); | ||
| 504 | + return ge::GRAPH_FAILED; | ||
| 318 | } | 505 | } |
| 319 | } else { | 506 | } else { |
| 320 | // 在permute的条件下 | 507 | // 在permute的条件下 |
| @@ -324,6 +511,7 @@ static inline void SetTilingKey(const gert::TilingContext* context, const bool i | |||
| 324 | // 不存在probs | 511 | // 不存在probs |
| 325 | param.core.tilingKey = 0; | 512 | param.core.tilingKey = 0; |
| 326 | } | 513 | } |
| 514 | + return ge::GRAPH_SUCCESS; | ||
| 327 | } | 515 | } |
| 328 | 516 | ||
| 329 | static inline void SetTilingData(gert::TilingContext* context, const MoeTokenUnpermuteWithEpParam& param) | 517 | static inline void SetTilingData(gert::TilingContext* context, const MoeTokenUnpermuteWithEpParam& param) |
| @@ -394,13 +582,21 @@ ge::graphStatus UnpermuteWithEpTilingCompute(gert::TilingContext* context, const | |||
| 394 | if (MoeTokenUnpermuteWithEpInputParamCheck(context, isUnpermute) == ge::GRAPH_FAILED) { | 582 | if (MoeTokenUnpermuteWithEpInputParamCheck(context, isUnpermute) == ge::GRAPH_FAILED) { |
| 395 | return ge::GRAPH_FAILED; | 583 | return ge::GRAPH_FAILED; |
| 396 | } | 584 | } |
| 397 | - Init(context, topK, param, isUnpermute); | 585 | + if (Init(context, topK, param, isUnpermute) == ge::GRAPH_FAILED) { |
| 586 | + return ge::GRAPH_FAILED; | ||
| 587 | + } | ||
| 398 | SetCoreNum(param); | 588 | SetCoreNum(param); |
| 399 | - TilingHiddenSize(param); | 589 | + if (TilingHiddenSize(param) == ge::GRAPH_FAILED) { |
| 590 | + return ge::GRAPH_FAILED; | ||
| 591 | + } | ||
| 400 | SetBufferNum(param); | 592 | SetBufferNum(param); |
| 401 | ComputeRemainMemerySpace(param); | 593 | ComputeRemainMemerySpace(param); |
| 402 | - TilingToken(param); | 594 | + if (TilingToken(param) == ge::GRAPH_FAILED) { |
| 403 | - SetTilingKey(context, isUnpermute, param); | 595 | + return ge::GRAPH_FAILED; |
| 596 | + } | ||
| 597 | + if (SetTilingKey(context, isUnpermute, param) == ge::GRAPH_FAILED) { | ||
| 598 | + return ge::GRAPH_FAILED; | ||
| 599 | + } | ||
| 404 | SetTilingData(context, param); | 600 | SetTilingData(context, param); |
| 405 | DebugPrint(context, param); | 601 | DebugPrint(context, param); |
| 406 | size_t* workSpaces = context->GetWorkspaceSizes(1); | 602 | size_t* workSpaces = context->GetWorkspaceSizes(1); |
| @@ -0,0 +1,314 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 4 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 5 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 8 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | +"""Independent Torch-composition golden/reference for MoeTokenUnpermuteWithEp. | ||
| 11 | + | ||
| 12 | +Semantics (public README/docs) for the unpermute direction without padded mode: | ||
| 13 | + | ||
| 14 | + out[i] = sum_j ( permuted_tokens[sorted_indices[i * num_topk + j] - range[0]] | ||
| 15 | + * (probs[i, j] if probs is not None else 1) | ||
| 16 | + if range[0] <= sorted_indices[i * num_topk + j] < range[1] | ||
| 17 | + else 0 ) | ||
| 18 | + | ||
| 19 | +`range`/`rangeOptional` empty (None or length 0) means the default {0, 0}, | ||
| 20 | +therefore every slot contributes 0 and the output is all zeros. | ||
| 21 | + | ||
| 22 | +The golden keeps the compute dtype supplied by the test framework (when the | ||
| 23 | +framework promotes inputs, e.g. fp16/bf16 -> fp32 and fp32 -> fp64, that promoted | ||
| 24 | +dtype is used), accumulates in torch (never NumPy), and returns the result in the | ||
| 25 | +input dtype. Both the kernel (GEIR) and the ACLNN two-phase API are registered here. | ||
| 26 | +""" | ||
| 27 | + | ||
| 28 | +from __future__ import annotations | ||
| 29 | + | ||
| 30 | +import torch | ||
| 31 | + | ||
| 32 | +__spec__ = { | ||
| 33 | + "moe_token_unpermute_with_ep": "MoeTokenUnpermuteWithEpNoProbsSpec", | ||
| 34 | + "aclnnMoeTokenUnpermuteWithEp": "MoeTokenUnpermuteWithEpAclnnSpec", | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +_TOLERANCE_L0 = { | ||
| 38 | + "float16": {"standard": "stat_rel_err", "level": "L0"}, | ||
| 39 | + "bfloat16": {"standard": "stat_rel_err", "level": "L0"}, | ||
| 40 | + "float32": {"standard": "stat_rel_err", "level": "L0"}, | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +_TOLERANCE_L1 = { | ||
| 44 | + "float16": {"standard": "stat_rel_err", "level": "L1"}, | ||
| 45 | + "bfloat16": {"standard": "stat_rel_err", "level": "L1"}, | ||
| 46 | + "float32": {"standard": "stat_rel_err", "level": "L1"}, | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | + | ||
| 50 | +def _to_torch(value): | ||
| 51 | + """numpy / torch / tf carrier -> torch tensor, preserving dtype.""" | ||
| 52 | + if value is None: | ||
| 53 | + return None | ||
| 54 | + if isinstance(value, torch.Tensor): | ||
| 55 | + return value | ||
| 56 | + from ttk.utilities.dtypes import numpy_to_torch_tensor | ||
| 57 | + | ||
| 58 | + return numpy_to_torch_tensor(value) | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +def _to_numpy(value): | ||
| 62 | + if value is None: | ||
| 63 | + return None | ||
| 64 | + if isinstance(value, torch.Tensor): | ||
| 65 | + from ttk.utilities.dtypes import torch_to_numpy_tensor | ||
| 66 | + | ||
| 67 | + return torch_to_numpy_tensor(value.detach().cpu()) | ||
| 68 | + return value | ||
| 69 | + | ||
| 70 | + | ||
| 71 | +def _normalize_range(range_value): | ||
| 72 | + """Return (start, end); empty/None maps to the documented default {0, 0}.""" | ||
| 73 | + if range_value is None or isinstance(range_value, (int, float)): | ||
| 74 | + return 0, 0 | ||
| 75 | + if isinstance(range_value, torch.Tensor): | ||
| 76 | + range_value = range_value.detach().cpu().reshape(-1).tolist() | ||
| 77 | + elif hasattr(range_value, "numpy"): | ||
| 78 | + range_value = range_value.numpy().reshape(-1).tolist() | ||
| 79 | + values = [int(v) for v in range_value] | ||
| 80 | + if len(values) == 0: | ||
| 81 | + return 0, 0 | ||
| 82 | + if len(values) != 2: | ||
| 83 | + raise ValueError(f"range must contain exactly 2 elements, got {len(values)}") | ||
| 84 | + return values[0], values[1] | ||
| 85 | + | ||
| 86 | + | ||
| 87 | +def _resolve_num_topk(num_topk, probs, indices): | ||
| 88 | + if probs is not None: | ||
| 89 | + if probs.dim() != 2: | ||
| 90 | + raise ValueError( | ||
| 91 | + f"probs must be 2D [tokens, num_topk], got rank {probs.dim()}" | ||
| 92 | + ) | ||
| 93 | + return int(probs.shape[1]) | ||
| 94 | + topk = int(num_topk) | ||
| 95 | + if topk < 1: | ||
| 96 | + raise ValueError(f"num_topk must be >= 1, got {topk}") | ||
| 97 | + return topk | ||
| 98 | + | ||
| 99 | + | ||
| 100 | +def _reference(permuted_tokens, sorted_indices, probs, num_topk, range_value): | ||
| 101 | + tokens = _to_torch(permuted_tokens) | ||
| 102 | + indices = _to_torch(sorted_indices) | ||
| 103 | + probs_t = _to_torch(probs) | ||
| 104 | + | ||
| 105 | + if tokens is None or indices is None: | ||
| 106 | + raise ValueError("permuted_tokens and sorted_indices are required") | ||
| 107 | + if tokens.dim() != 2: | ||
| 108 | + raise ValueError(f"permuted_tokens must be 2D, got rank {tokens.dim()}") | ||
| 109 | + if tokens.shape[0] == 0 or tokens.shape[1] == 0: | ||
| 110 | + raise ValueError("permuted_tokens must not be empty") | ||
| 111 | + if indices.dim() != 1: | ||
| 112 | + raise ValueError(f"sorted_indices must be 1D, got rank {indices.dim()}") | ||
| 113 | + if indices.numel() == 0: | ||
| 114 | + raise ValueError("sorted_indices must not be empty") | ||
| 115 | + | ||
| 116 | + indices = indices.reshape(-1).to(torch.int64) | ||
| 117 | + topk = _resolve_num_topk(num_topk, probs_t, indices) | ||
| 118 | + total_length = indices.numel() | ||
| 119 | + if total_length % topk != 0: | ||
| 120 | + raise ValueError( | ||
| 121 | + f"sorted_indices length {total_length} must be divisible by num_topk {topk}" | ||
| 122 | + ) | ||
| 123 | + token_count = total_length // topk | ||
| 124 | + if token_count < 1: | ||
| 125 | + raise ValueError("number of output tokens must be >= 1") | ||
| 126 | + if probs_t is not None: | ||
| 127 | + if probs_t.shape[0] != token_count: | ||
| 128 | + raise ValueError( | ||
| 129 | + f"probs dim0 {probs_t.shape[0]} must equal tokens {token_count}" | ||
| 130 | + ) | ||
| 131 | + | ||
| 132 | + start, end = _normalize_range(range_value) | ||
| 133 | + if start < 0 or start > end: | ||
| 134 | + raise ValueError(f"invalid range [{start}, {end})") | ||
| 135 | + rows = int(tokens.shape[0]) | ||
| 136 | + if end - start > rows: | ||
| 137 | + raise ValueError( | ||
| 138 | + f"permuted_tokens rows {rows} must be >= range span {end - start}" | ||
| 139 | + ) | ||
| 140 | + | ||
| 141 | + out_dtype = tokens.dtype | ||
| 142 | + # Accumulate the reference in float64 so that finite values are not turned | ||
| 143 | + # into inf by intermediate overflow (e.g. near-max bf16 additions), then | ||
| 144 | + # round once to the declared output dtype. | ||
| 145 | + compute_dtype = torch.float64 | ||
| 146 | + values = tokens.to(compute_dtype) | ||
| 147 | + | ||
| 148 | + valid = (indices >= start) & (indices < end) | ||
| 149 | + positions = torch.nonzero(valid, as_tuple=False).reshape(-1) | ||
| 150 | + output = torch.zeros( | ||
| 151 | + (token_count, int(tokens.shape[1])), dtype=compute_dtype, device=tokens.device | ||
| 152 | + ) | ||
| 153 | + if positions.numel() > 0: | ||
| 154 | + local = indices.index_select(0, positions) - start | ||
| 155 | + gathered = values.index_select(0, local) | ||
| 156 | + if probs_t is not None: | ||
| 157 | + weights = probs_t.to(compute_dtype).reshape(-1).index_select(0, positions) | ||
| 158 | + gathered = gathered * weights.unsqueeze(1) | ||
| 159 | + destination = torch.div(positions, topk, rounding_mode="floor") | ||
| 160 | + output.index_add_(0, destination, gathered) | ||
| 161 | + return output.to(out_dtype) | ||
| 162 | + | ||
| 163 | + | ||
| 164 | +def _resolve_call( | ||
| 165 | + permuted_tokens=None, | ||
| 166 | + sorted_indices=None, | ||
| 167 | + probs=None, | ||
| 168 | + num_topk=1, | ||
| 169 | + range_value=None, | ||
| 170 | + permutedTokens=None, | ||
| 171 | + sortedIndices=None, | ||
| 172 | + probsOptional=None, | ||
| 173 | + numTopk=None, | ||
| 174 | + rangeOptional=None, | ||
| 175 | + **kwargs, | ||
| 176 | +): | ||
| 177 | + del kwargs | ||
| 178 | + if permutedTokens is not None: | ||
| 179 | + permuted_tokens = permutedTokens | ||
| 180 | + if sortedIndices is not None: | ||
| 181 | + sorted_indices = sortedIndices | ||
| 182 | + if probsOptional is not None: | ||
| 183 | + probs = probsOptional | ||
| 184 | + if numTopk is not None: | ||
| 185 | + num_topk = numTopk | ||
| 186 | + if rangeOptional is not None: | ||
| 187 | + range_value = rangeOptional | ||
| 188 | + return _reference(permuted_tokens, sorted_indices, probs, num_topk, range_value) | ||
| 189 | + | ||
| 190 | + | ||
| 191 | +def moe_token_unpermute_with_ep_golden( | ||
| 192 | + permuted_tokens, | ||
| 193 | + sorted_indices, | ||
| 194 | + probs=None, | ||
| 195 | + num_topk=1, | ||
| 196 | + range=None, | ||
| 197 | + padded_mode=False, | ||
| 198 | + restore_shape=None, | ||
| 199 | + unpermuted_tokens=None, | ||
| 200 | + **kwargs, | ||
| 201 | +): | ||
| 202 | + del padded_mode, restore_shape, unpermuted_tokens | ||
| 203 | + kwargs.pop("numTopk", None) | ||
| 204 | + if kwargs.get("rangeOptional") is not None: | ||
| 205 | + range = kwargs["rangeOptional"] | ||
| 206 | + return [ | ||
| 207 | + _to_numpy( | ||
| 208 | + _resolve_call( | ||
| 209 | + permuted_tokens=permuted_tokens, | ||
| 210 | + sorted_indices=sorted_indices, | ||
| 211 | + probs=probs, | ||
| 212 | + num_topk=num_topk, | ||
| 213 | + range_value=range, | ||
| 214 | + ) | ||
| 215 | + ) | ||
| 216 | + ] | ||
| 217 | + | ||
| 218 | + | ||
| 219 | +def aclnn_moe_token_unpermute_with_ep_golden( | ||
| 220 | + permutedTokens, | ||
| 221 | + sortedIndices, | ||
| 222 | + probsOptional=None, | ||
| 223 | + numTopk=1, | ||
| 224 | + rangeOptional=None, | ||
| 225 | + paddedMode=False, | ||
| 226 | + restoreShapeOptional=None, | ||
| 227 | + out=None, | ||
| 228 | + **kwargs, | ||
| 229 | +): | ||
| 230 | + del paddedMode, restoreShapeOptional, out | ||
| 231 | + return [ | ||
| 232 | + _to_numpy( | ||
| 233 | + _resolve_call( | ||
| 234 | + permutedTokens=permutedTokens, | ||
| 235 | + sortedIndices=sortedIndices, | ||
| 236 | + probsOptional=probsOptional, | ||
| 237 | + numTopk=numTopk, | ||
| 238 | + rangeOptional=rangeOptional, | ||
| 239 | + **kwargs, | ||
| 240 | + ) | ||
| 241 | + ) | ||
| 242 | + ] | ||
| 243 | + | ||
| 244 | + | ||
| 245 | +class _TorchMoeTokenUnpermuteWithEp: | ||
| 246 | + """Third-party reference class shared by the kernel and ACLNN paths.""" | ||
| 247 | + | ||
| 248 | + def __call__( | ||
| 249 | + self, | ||
| 250 | + permuted_tokens=None, | ||
| 251 | + sorted_indices=None, | ||
| 252 | + probs=None, | ||
| 253 | + num_topk=1, | ||
| 254 | + range_value=None, | ||
| 255 | + permutedTokens=None, | ||
| 256 | + sortedIndices=None, | ||
| 257 | + probsOptional=None, | ||
| 258 | + numTopk=None, | ||
| 259 | + rangeOptional=None, | ||
| 260 | + **kwargs, | ||
| 261 | + ): | ||
| 262 | + return [ | ||
| 263 | + _resolve_call( | ||
| 264 | + permuted_tokens=permuted_tokens, | ||
| 265 | + sorted_indices=sorted_indices, | ||
| 266 | + probs=probs, | ||
| 267 | + num_topk=num_topk, | ||
| 268 | + range_value=range_value, | ||
| 269 | + permutedTokens=permutedTokens, | ||
| 270 | + sortedIndices=sortedIndices, | ||
| 271 | + probsOptional=probsOptional, | ||
| 272 | + numTopk=numTopk, | ||
| 273 | + rangeOptional=rangeOptional, | ||
| 274 | + **kwargs, | ||
| 275 | + ) | ||
| 276 | + ] | ||
| 277 | + | ||
| 278 | + | ||
| 279 | +class _TorchAclnnMoeTokenUnpermuteWithEp: | ||
| 280 | + def __call__( | ||
| 281 | + self, | ||
| 282 | + permutedTokens=None, | ||
| 283 | + sortedIndices=None, | ||
| 284 | + probsOptional=None, | ||
| 285 | + numTopk=1, | ||
| 286 | + rangeOptional=None, | ||
| 287 | + paddedMode=False, | ||
| 288 | + restoreShapeOptional=None, | ||
| 289 | + out=None, | ||
| 290 | + **kwargs, | ||
| 291 | + ): | ||
| 292 | + del paddedMode, restoreShapeOptional, out | ||
| 293 | + return [ | ||
| 294 | + _resolve_call( | ||
| 295 | + permutedTokens=permutedTokens, | ||
| 296 | + sortedIndices=sortedIndices, | ||
| 297 | + probsOptional=probsOptional, | ||
| 298 | + numTopk=numTopk, | ||
| 299 | + rangeOptional=rangeOptional, | ||
| 300 | + **kwargs, | ||
| 301 | + ) | ||
| 302 | + ] | ||
| 303 | + | ||
| 304 | + | ||
| 305 | +class MoeTokenUnpermuteWithEpNoProbsSpec: | ||
| 306 | + golden = staticmethod(moe_token_unpermute_with_ep_golden) | ||
| 307 | + third_party = {"torch": _TorchMoeTokenUnpermuteWithEp} | ||
| 308 | + tolerance = _TOLERANCE_L0 | ||
| 309 | + | ||
| 310 | + | ||
| 311 | +class MoeTokenUnpermuteWithEpAclnnSpec: | ||
| 312 | + golden = staticmethod(aclnn_moe_token_unpermute_with_ep_golden) | ||
| 313 | + third_party = {"torch": _TorchAclnnMoeTokenUnpermuteWithEp} | ||
| 314 | + tolerance = _TOLERANCE_L1 | ||
Amoe/moe_token_unpermute_with_ep/tests/st/arch35/test_aclnn_moe_token_unpermute_with_ep_arch35.csv+9-0
| @@ -0,0 +1,9 @@ | |||
| 1 | +testcase_name,api_name,tensor_view_shapes,tensor_dtypes,tensor_formats,output_tensor_indexes,output_inplace_indexes,attributes,input_data_ranges,precision_tolerances,remark | ||
| 2 | +aclnn_moe_token_unpermute_with_ep_arch35_000_fp16_topk1,aclnnMoeTokenUnpermuteWithEp,"((16,32),(16,),None,(16,32))","('float16','int32',None,'float16')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 1, 'paddedMode': False, 'rangeOptional': [0, 16], 'restoreShapeOptional': None}","((-2,2),(0,15),None)",,fp16/topk=1/无probs,range 覆盖全部 | ||
| 3 | +aclnn_moe_token_unpermute_with_ep_arch35_001_bf16_probs_topk2,aclnnMoeTokenUnpermuteWithEp,"((32,64),(32,),(16,2),(16,64))","('bfloat16','int32','bfloat16','bfloat16')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 2, 'paddedMode': False, 'rangeOptional': [0, 32], 'restoreShapeOptional': None}","((-2,2),(0,31),(0,1))",,bf16/topk=2/带probs | ||
| 4 | +aclnn_moe_token_unpermute_with_ep_arch35_002_fp32_topk4,aclnnMoeTokenUnpermuteWithEp,"((32,128),(32,),None,(8,128))","('float32','int32',None,'float32')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 4, 'paddedMode': False, 'rangeOptional': [0, 32], 'restoreShapeOptional': None}","((-2,2),(0,31),None)",,fp32/topk=4/无probs | ||
| 5 | +aclnn_moe_token_unpermute_with_ep_arch35_003_fp16_probs_topk8_h1,aclnnMoeTokenUnpermuteWithEp,"((64,1),(64,),(8,8),(8,1))","('float16','int32','float16','float16')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 8, 'paddedMode': False, 'rangeOptional': [0, 64], 'restoreShapeOptional': None}","((-2,2),(0,63),(0,1))",,fp16/topk=8/hidden=1/带probs | ||
| 6 | +aclnn_moe_token_unpermute_with_ep_arch35_004_bf16_topk512_bound,aclnnMoeTokenUnpermuteWithEp,"((512,16),(512,),None,(1,16))","('bfloat16','int32',None,'bfloat16')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 512, 'paddedMode': False, 'rangeOptional': [0, 512], 'restoreShapeOptional': None}","((-2,2),(0,511),None)",,bf16/topk=512 边界 | ||
| 7 | +aclnn_moe_token_unpermute_with_ep_arch35_005_fp32_probs_min,aclnnMoeTokenUnpermuteWithEp,"((1,4),(1,),(1,1),(1,4))","('float32','int32','float32','float32')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 1, 'paddedMode': False, 'rangeOptional': [0, 1], 'restoreShapeOptional': None}","((-2,2),(0,0),(0,1))",,fp32/最小规模(1 token)/带probs | ||
| 8 | +aclnn_moe_token_unpermute_with_ep_arch35_006_fp16_range_mask,aclnnMoeTokenUnpermuteWithEp,"((64,8),(32,),None,(16,8))","('float16','int32',None,'float16')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 2, 'paddedMode': False, 'rangeOptional': [0, 20], 'restoreShapeOptional': None}","((-2,2),(0,63),None)",,fp16/部分 range 掩码(indices 越界贡献0) | ||
| 9 | +aclnn_moe_token_unpermute_with_ep_arch35_007_bf16_probs_topk4_h256,aclnnMoeTokenUnpermuteWithEp,"((16,256),(16,),(4,4),(4,256))","('bfloat16','int32','bfloat16','bfloat16')","('ND','ND','ND','ND')","(3,)",(),"{'numTopk': 4, 'paddedMode': False, 'rangeOptional': [0, 16], 'restoreShapeOptional': None}","((-2,2),(0,15),(0,1))",,bf16/topk=4/hidden=256/带probs | ||
Amoe/moe_token_unpermute_with_ep/tests/st/arch35/test_geir_moe_token_unpermute_with_ep_arch35.csv+9-0
| @@ -0,0 +1,9 @@ | |||
| 1 | +testcase_name,network_name,op_name,op_type,input_shapes,input_dtypes,input_formats,output_shapes,output_dtypes,output_formats,input_ori_shapes,input_ori_formats,output_ori_shapes,output_ori_formats,attributes,input_data_ranges,precision_tolerances,absolute_precision,output_inplace_indexes,output_shape_unknown_indexes,dyn_input_shapes,is_enabled,remark,soc_series,priority,dump_file_prefix,manual_input_binaries,manual_golden_binaries | ||
| 2 | +geir_moe_token_unpermute_with_ep_arch35_000_fp16_topk1,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((16,32),(16,),None,)","('float16','int32','float16',)","('ND','ND','ND')","((16,32),)","('float16',)","('ND',)","((16,32),(16,),None,)","('ND','ND','ND')","((16,32),)","('ND',)","{'num_topk': 1, 'range': [0, 16], 'padded_mode': False}","((-2,2),(0,15),None)",,0,(),(),"((16,32),(16,),None)",True,fp16/topk=1/无probs,Ascend950,0,,(),() | ||
| 3 | +geir_moe_token_unpermute_with_ep_arch35_001_bf16_probs_topk2,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((32,64),(32,),(16,2),)","('bfloat16','int32','bfloat16',)","('ND','ND','ND')","((16,64),)","('bfloat16',)","('ND',)","((32,64),(32,),(16,2),)","('ND','ND','ND')","((16,64),)","('ND',)","{'num_topk': 2, 'range': [0, 32], 'padded_mode': False}","((-2,2),(0,31),(0,1))",,0,(),(),"((-1,64),(-1,),(-1,2))",True,bf16/topk=2/带probs/动态dim0,Ascend950,0,,(),() | ||
| 4 | +geir_moe_token_unpermute_with_ep_arch35_002_fp32_topk4,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((32,128),(32,),None,)","('float32','int32','float32',)","('ND','ND','ND')","((8,128),)","('float32',)","('ND',)","((32,128),(32,),None,)","('ND','ND','ND')","((8,128),)","('ND',)","{'num_topk': 4, 'range': [0, 32], 'padded_mode': False}","((-2,2),(0,31),None)",,0,(),(),"((32,128),(32,),None)",True,fp32/topk=4/无probs,Ascend950,0,,(),() | ||
| 5 | +geir_moe_token_unpermute_with_ep_arch35_003_fp16_probs_topk8_h1,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((64,1),(64,),(8,8),)","('float16','int32','float16',)","('ND','ND','ND')","((8,1),)","('float16',)","('ND',)","((64,1),(64,),(8,8),)","('ND','ND','ND')","((8,1),)","('ND',)","{'num_topk': 8, 'range': [0, 64], 'padded_mode': False}","((-2,2),(0,63),(0,1))",,0,(),(),"((-1,1),(-1,),(-1,8))",True,fp16/topk=8/hidden=1/带probs/动态,Ascend950,0,,(),() | ||
| 6 | +geir_moe_token_unpermute_with_ep_arch35_004_bf16_topk512_bound,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((512,16),(512,),None,)","('bfloat16','int32','bfloat16',)","('ND','ND','ND')","((1,16),)","('bfloat16',)","('ND',)","((512,16),(512,),None,)","('ND','ND','ND')","((1,16),)","('ND',)","{'num_topk': 512, 'range': [0, 512], 'padded_mode': False}","((-2,2),(0,511),None)",,0,(),(),"((512,16),(512,),None)",True,bf16/topk=512 边界,Ascend950,0,,(),() | ||
| 7 | +geir_moe_token_unpermute_with_ep_arch35_005_fp32_probs_min,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((1,4),(1,),(1,1),)","('float32','int32','float32',)","('ND','ND','ND')","((1,4),)","('float32',)","('ND',)","((1,4),(1,),(1,1),)","('ND','ND','ND')","((1,4),)","('ND',)","{'num_topk': 1, 'range': [0, 1], 'padded_mode': False}","((-2,2),(0,0),(0,1))",,0,(),(),"((-1,4),(-1,),(-1,1))",True,fp32/最小规模/带probs,Ascend950,0,,(),() | ||
| 8 | +geir_moe_token_unpermute_with_ep_arch35_006_fp16_range_mask,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((64,8),(32,),None,)","('float16','int32','float16',)","('ND','ND','ND')","((16,8),)","('float16',)","('ND',)","((64,8),(32,),None,)","('ND','ND','ND')","((16,8),)","('ND',)","{'num_topk': 2, 'range': [0, 20], 'padded_mode': False}","((-2,2),(0,63),None)",,0,(),(),"((64,8),(32,),None)",True,fp16/部分 range 掩码,Ascend950,0,,(),() | ||
| 9 | +geir_moe_token_unpermute_with_ep_arch35_007_bf16_probs_topk4_h256,UNKNOWN,moe_token_unpermute_with_ep,MoeTokenUnpermuteWithEp,"((16,256),(16,),(4,4),)","('bfloat16','int32','bfloat16',)","('ND','ND','ND')","((4,256),)","('bfloat16',)","('ND',)","((16,256),(16,),(4,4),)","('ND','ND','ND')","((4,256),)","('ND',)","{'num_topk': 4, 'range': [0, 16], 'padded_mode': False}","((-2,2),(0,15),(0,1))",,0,(),(),"((-1,256),(-1,),(-1,4))",True,bf16/topk=4/hidden=256/带probs/动态,Ascend950,0,,(),() | ||
| @@ -243,29 +243,28 @@ TEST_F(MoeTokenUnpermuteWithEp, test_infertype_bf16) | |||
| 243 | ->GetOpImpl("MoeTokenUnpermuteWithEp") | 243 | ->GetOpImpl("MoeTokenUnpermuteWithEp") |
| 244 | ->infer_datatype; | 244 | ->infer_datatype; |
| 245 | 245 | ||
| 246 | - if (data_type_func != nullptr) { | 246 | + ASSERT_NE(data_type_func, nullptr); |
| 247 | - ge::DataType input_ref = ge::DT_BF16; | 247 | + ge::DataType input_ref = ge::DT_BF16; |
| 248 | - ge::DataType input_indices_ref = ge::DT_INT32; | 248 | + ge::DataType input_indices_ref = ge::DT_INT32; |
| 249 | - ge::DataType output_ref = ge::DT_BF16; | 249 | + ge::DataType output_ref = ge::DT_BF16; |
| 250 | - auto context_holder = gert::InferDataTypeContextFaker() | 250 | + auto context_holder = gert::InferDataTypeContextFaker() |
| 251 | - .IrInputNum(3) | 251 | + .IrInputNum(3) |
| 252 | - .NodeIoNum(3, 1) | 252 | + .NodeIoNum(3, 1) |
| 253 | - .NodeInputTd(0, ge::DT_BF16, ge::FORMAT_ND, ge::FORMAT_ND) | 253 | + .NodeInputTd(0, ge::DT_BF16, ge::FORMAT_ND, ge::FORMAT_ND) |
| 254 | - .NodeInputTd(1, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND) | 254 | + .NodeInputTd(1, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND) |
| 255 | - .NodeInputTd(2, ge::DT_BF16, ge::FORMAT_ND, ge::FORMAT_ND) | 255 | + .NodeInputTd(2, ge::DT_BF16, ge::FORMAT_ND, ge::FORMAT_ND) |
| 256 | - .NodeOutputTd(0, ge::FORMAT_ND, ge::FORMAT_ND) | 256 | + .NodeOutputTd(0, ge::FORMAT_ND, ge::FORMAT_ND) |
| 257 | - .InputDataTypes({&input_ref, &input_indices_ref, &input_ref}) | 257 | + .InputDataTypes({&input_ref, &input_indices_ref, &input_ref}) |
| 258 | - .OutputDataTypes({&output_ref}) | 258 | + .OutputDataTypes({&output_ref}) |
| 259 | - .Build(); | 259 | + .Build(); |
| 260 | - auto context = context_holder.GetContext<gert::InferDataTypeContext>(); | 260 | + auto context = context_holder.GetContext<gert::InferDataTypeContext>(); |
| 261 | - EXPECT_EQ(data_type_func(context), ge::GRAPH_SUCCESS); | 261 | + EXPECT_EQ(data_type_func(context), ge::GRAPH_SUCCESS); |
| 262 | - ASSERT_NE(context, nullptr); | 262 | + ASSERT_NE(context, nullptr); |
| 263 | 263 | ||
| 264 | - EXPECT_EQ(context->GetInputDataType(0), input_ref); | 264 | + EXPECT_EQ(context->GetInputDataType(0), input_ref); |
| 265 | - EXPECT_EQ(context->GetInputDataType(1), input_indices_ref); | 265 | + EXPECT_EQ(context->GetInputDataType(1), input_indices_ref); |
| 266 | - EXPECT_EQ(context->GetInputDataType(2), input_ref); | 266 | + EXPECT_EQ(context->GetInputDataType(2), input_ref); |
| 267 | - EXPECT_EQ(context->GetOutputDataType(0), output_ref); | 267 | + EXPECT_EQ(context->GetOutputDataType(0), output_ref); |
| 268 | - } | ||
| 269 | } | 268 | } |
| 270 | 269 | ||
| 271 | TEST_F(MoeTokenUnpermuteWithEp, test_infertype_fp16) | 270 | TEST_F(MoeTokenUnpermuteWithEp, test_infertype_fp16) |
| @@ -278,29 +277,28 @@ TEST_F(MoeTokenUnpermuteWithEp, test_infertype_fp16) | |||
| 278 | ->GetOpImpl("MoeTokenUnpermuteWithEp") | 277 | ->GetOpImpl("MoeTokenUnpermuteWithEp") |
| 279 | ->infer_datatype; | 278 | ->infer_datatype; |
| 280 | 279 | ||
| 281 | - if (data_type_func != nullptr) { | 280 | + ASSERT_NE(data_type_func, nullptr); |
| 282 | - ge::DataType input_ref = ge::DT_FLOAT16; | 281 | + ge::DataType input_ref = ge::DT_FLOAT16; |
| 283 | - ge::DataType input_indices_ref = ge::DT_INT32; | 282 | + ge::DataType input_indices_ref = ge::DT_INT32; |
| 284 | - ge::DataType output_ref = ge::DT_FLOAT16; | 283 | + ge::DataType output_ref = ge::DT_FLOAT16; |
| 285 | - auto context_holder = gert::InferDataTypeContextFaker() | 284 | + auto context_holder = gert::InferDataTypeContextFaker() |
| 286 | - .IrInputNum(3) | 285 | + .IrInputNum(3) |
| 287 | - .NodeIoNum(3, 1) | 286 | + .NodeIoNum(3, 1) |
| 288 | - .NodeInputTd(0, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_ND) | 287 | + .NodeInputTd(0, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_ND) |
| 289 | - .NodeInputTd(1, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND) | 288 | + .NodeInputTd(1, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND) |
| 290 | - .NodeInputTd(2, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_ND) | 289 | + .NodeInputTd(2, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_ND) |
| 291 | - .NodeOutputTd(0, ge::FORMAT_ND, ge::FORMAT_ND) | 290 | + .NodeOutputTd(0, ge::FORMAT_ND, ge::FORMAT_ND) |
| 292 | - .InputDataTypes({&input_ref, &input_indices_ref, &input_ref}) | 291 | + .InputDataTypes({&input_ref, &input_indices_ref, &input_ref}) |
| 293 | - .OutputDataTypes({&output_ref}) | 292 | + .OutputDataTypes({&output_ref}) |
| 294 | - .Build(); | 293 | + .Build(); |
| 295 | - auto context = context_holder.GetContext<gert::InferDataTypeContext>(); | 294 | + auto context = context_holder.GetContext<gert::InferDataTypeContext>(); |
| 296 | - EXPECT_EQ(data_type_func(context), ge::GRAPH_SUCCESS); | 295 | + EXPECT_EQ(data_type_func(context), ge::GRAPH_SUCCESS); |
| 297 | - ASSERT_NE(context, nullptr); | 296 | + ASSERT_NE(context, nullptr); |
| 298 | 297 | ||
| 299 | - EXPECT_EQ(context->GetInputDataType(0), input_ref); | 298 | + EXPECT_EQ(context->GetInputDataType(0), input_ref); |
| 300 | - EXPECT_EQ(context->GetInputDataType(1), input_indices_ref); | 299 | + EXPECT_EQ(context->GetInputDataType(1), input_indices_ref); |
| 301 | - EXPECT_EQ(context->GetInputDataType(2), input_ref); | 300 | + EXPECT_EQ(context->GetInputDataType(2), input_ref); |
| 302 | - EXPECT_EQ(context->GetOutputDataType(0), output_ref); | 301 | + EXPECT_EQ(context->GetOutputDataType(0), output_ref); |
| 303 | - } | ||
| 304 | } | 302 | } |
| 305 | 303 | ||
| 306 | TEST_F(MoeTokenUnpermuteWithEp, test_infertype_fp32) | 304 | TEST_F(MoeTokenUnpermuteWithEp, test_infertype_fp32) |
| @@ -313,29 +311,28 @@ TEST_F(MoeTokenUnpermuteWithEp, test_infertype_fp32) | |||
| 313 | ->GetOpImpl("MoeTokenUnpermuteWithEp") | 311 | ->GetOpImpl("MoeTokenUnpermuteWithEp") |
| 314 | ->infer_datatype; | 312 | ->infer_datatype; |
| 315 | 313 | ||
| 316 | - if (data_type_func != nullptr) { | 314 | + ASSERT_NE(data_type_func, nullptr); |
| 317 | - ge::DataType input_ref = ge::DT_FLOAT; | 315 | + ge::DataType input_ref = ge::DT_FLOAT; |
| 318 | - ge::DataType input_indices_ref = ge::DT_INT32; | 316 | + ge::DataType input_indices_ref = ge::DT_INT32; |
| 319 | - ge::DataType output_ref = ge::DT_FLOAT; | 317 | + ge::DataType output_ref = ge::DT_FLOAT; |
| 320 | - auto context_holder = gert::InferDataTypeContextFaker() | 318 | + auto context_holder = gert::InferDataTypeContextFaker() |
| 321 | - .IrInputNum(3) | 319 | + .IrInputNum(3) |
| 322 | - .NodeIoNum(3, 1) | 320 | + .NodeIoNum(3, 1) |
| 323 | - .NodeInputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND) | 321 | + .NodeInputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND) |
| 324 | - .NodeInputTd(1, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND) | 322 | + .NodeInputTd(1, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND) |
| 325 | - .NodeInputTd(2, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND) | 323 | + .NodeInputTd(2, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND) |
| 326 | - .NodeOutputTd(0, ge::FORMAT_ND, ge::FORMAT_ND) | 324 | + .NodeOutputTd(0, ge::FORMAT_ND, ge::FORMAT_ND) |
| 327 | - .InputDataTypes({&input_ref, &input_indices_ref, &input_ref}) | 325 | + .InputDataTypes({&input_ref, &input_indices_ref, &input_ref}) |
| 328 | - .OutputDataTypes({&output_ref}) | 326 | + .OutputDataTypes({&output_ref}) |
| 329 | - .Build(); | 327 | + .Build(); |
| 330 | - auto context = context_holder.GetContext<gert::InferDataTypeContext>(); | 328 | + auto context = context_holder.GetContext<gert::InferDataTypeContext>(); |
| 331 | - EXPECT_EQ(data_type_func(context), ge::GRAPH_SUCCESS); | 329 | + EXPECT_EQ(data_type_func(context), ge::GRAPH_SUCCESS); |
| 332 | - ASSERT_NE(context, nullptr); | 330 | + ASSERT_NE(context, nullptr); |
| 333 | 331 | ||
| 334 | - EXPECT_EQ(context->GetInputDataType(0), input_ref); | 332 | + EXPECT_EQ(context->GetInputDataType(0), input_ref); |
| 335 | - EXPECT_EQ(context->GetInputDataType(1), input_indices_ref); | 333 | + EXPECT_EQ(context->GetInputDataType(1), input_indices_ref); |
| 336 | - EXPECT_EQ(context->GetInputDataType(2), input_ref); | 334 | + EXPECT_EQ(context->GetInputDataType(2), input_ref); |
| 337 | - EXPECT_EQ(context->GetOutputDataType(0), output_ref); | 335 | + EXPECT_EQ(context->GetOutputDataType(0), output_ref); |
| 338 | - } | ||
| 339 | } | 336 | } |
| 340 | 337 | ||
| 341 | TEST_F(MoeTokenUnpermuteWithEp, test_infershape_prob_none_empty_range) | 338 | TEST_F(MoeTokenUnpermuteWithEp, test_infershape_prob_none_empty_range) |
| @@ -359,7 +356,7 @@ TEST_F(MoeTokenUnpermuteWithEp, test_infershape_prob_none_empty_range) | |||
| 359 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 356 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| 360 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>(restore_shape)}, | 357 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>(restore_shape)}, |
| 361 | }); | 358 | }); |
| 362 | - ExecuteTestCase(infershapeContextPara, ge::GRAPH_SUCCESS, {{8, 2}}); | 359 | + ExecuteTestCase(infershapeContextPara, ge::GRAPH_SUCCESS, {{1, 2}}); |
| 363 | } | 360 | } |
| 364 | 361 | ||
| 365 | TEST_F(MoeTokenUnpermuteWithEp, test_infershape_prob_none_empty_range_topk_one) | 362 | TEST_F(MoeTokenUnpermuteWithEp, test_infershape_prob_none_empty_range_topk_one) |
| @@ -217,12 +217,12 @@ TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_prob_none_fp32) | |||
| 217 | {{{768, 20480}, {768, 20480}}, ge::DT_FLOAT, ge::FORMAT_ND}, | 217 | {{{768, 20480}, {768, 20480}}, ge::DT_FLOAT, ge::FORMAT_ND}, |
| 218 | }, | 218 | }, |
| 219 | {{"num_topk", Ops::Transformer::AnyValue::CreateFrom<int64_t>(8)}, | 219 | {{"num_topk", Ops::Transformer::AnyValue::CreateFrom<int64_t>(8)}, |
| 220 | - {"range", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({1, 49151})}, | 220 | + {"range", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({1, 6144})}, |
| 221 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 221 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| 222 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, | 222 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, |
| 223 | &compileInfo); | 223 | &compileInfo); |
| 224 | uint64_t expectTilingKey = 0; | 224 | uint64_t expectTilingKey = 0; |
| 225 | - string expectTilingData = "20480 1 8 1 49151 6143 20480 1 0 12 0 12 1 0 2 "; | 225 | + string expectTilingData = "20480 1 8 1 6144 6143 20480 1 0 12 0 12 1 0 2 "; |
| 226 | std::vector<size_t> expectWorkspaces = {0}; | 226 | std::vector<size_t> expectWorkspaces = {0}; |
| 227 | ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces); | 227 | ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces); |
| 228 | } | 228 | } |
| @@ -261,7 +261,7 @@ TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_small_tokens_num_core) | |||
| 261 | {{{8}, {8}}, ge::DT_INT32, ge::FORMAT_ND}, | 261 | {{{8}, {8}}, ge::DT_INT32, ge::FORMAT_ND}, |
| 262 | }, | 262 | }, |
| 263 | { | 263 | { |
| 264 | - {{{8, 128}, {8, 128}}, ge::DT_FLOAT16, ge::FORMAT_ND}, | 264 | + {{{1, 128}, {1, 128}}, ge::DT_FLOAT16, ge::FORMAT_ND}, |
| 265 | }, | 265 | }, |
| 266 | {{"num_topk", Ops::Transformer::AnyValue::CreateFrom<int64_t>(8)}, | 266 | {{"num_topk", Ops::Transformer::AnyValue::CreateFrom<int64_t>(8)}, |
| 267 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 267 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| @@ -328,7 +328,7 @@ TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_probs_invalid_dtype) | |||
| 328 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 328 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| 329 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, | 329 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, |
| 330 | &compileInfo); | 330 | &compileInfo); |
| 331 | - ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, 0); | 331 | + ExecuteTestCase(tilingContextPara, ge::GRAPH_FAILED); |
| 332 | } | 332 | } |
| 333 | 333 | ||
| 334 | TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_input_topk_invalid) | 334 | TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_input_topk_invalid) |
| @@ -368,7 +368,7 @@ TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_tokens_unknown_dtype) | |||
| 368 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 368 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| 369 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, | 369 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, |
| 370 | &compileInfo); | 370 | &compileInfo); |
| 371 | - ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, 0); | 371 | + ExecuteTestCase(tilingContextPara, ge::GRAPH_FAILED); |
| 372 | } | 372 | } |
| 373 | 373 | ||
| 374 | TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_indices_int64_dtype) | 374 | TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_indices_int64_dtype) |
| @@ -388,7 +388,7 @@ TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_indices_int64_dtype) | |||
| 388 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, | 388 | {"padded_mode", Ops::Transformer::AnyValue::CreateFrom<bool>(false)}, |
| 389 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, | 389 | {"restore_shape", Ops::Transformer::AnyValue::CreateFrom<std::vector<int64_t>>({})}}, |
| 390 | &compileInfo); | 390 | &compileInfo); |
| 391 | - ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, 0); | 391 | + ExecuteTestCase(tilingContextPara, ge::GRAPH_FAILED); |
| 392 | } | 392 | } |
| 393 | 393 | ||
| 394 | TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_tokens_not_2d) | 394 | TEST_F(MoeTokenUnpermuteWithEpTiling, test_tiling_tokens_not_2d) |