Merged
add inplace_partial_rotary_mul_grad #6895
add inplace_partial_rotary_mul_grad #6895
Merged
AlfengYuanAdd on Jun 13
Total: 36 files changed+6155-0
@@ -0,0 +1,20 @@
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+message(STATUS "=== Debug: start ops.posembedding.inplace_partial_rotary_mul_grad.CMakeLists.txt ")
12+file(GLOB CURRENT_DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*)
13+if(NOT ENABLE_TEST AND NOT BENCHMARK)
14+ list(REMOVE_ITEM CURRENT_DIRS tests)
15+endif()
16+foreach(SUB_DIR ${CURRENT_DIRS})
17+ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt")
18+ add_subdirectory(${SUB_DIR})
19+ endif()
20+endforeach()
@@ -0,0 +1,186 @@
1+# InplacePartialRotaryMulGrad
chenjiao

交付件不全: ①新增算子需同步刷新op_list.md ②新增aclnn需同步刷新op_api_list.md ③新增torch需同步刷新torch_api_list.md

likedislike
2+ 
3+## 产品支持情况
4+ 
5+| 产品 | 是否支持 |
6+| :----------------------------------------------------------- |:-------:|
7+| <term>Ascend 950PR/Ascend 950DT</term> | √ |
8+| <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term> | × |
9+| <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term> | × |
10+| <term>Atlas 200I/500 A2 推理产品</term> | × |
11+| <term>Atlas 推理系列产品</term> | × |
12+| <term>Atlas 训练系列产品</term> | × |
13+ 
14+## 功能说明
15+ 
16+- **算子功能**:执行局部旋转位置编码InplacePartialRotaryMul的反向计算。该算子对输入dy的D维度上切片[start, end)区域执行旋转位置编码梯度计算,dx'的结果inplace写回dy的[start, end)区间。dy的[start, end)之外的数据保持不变。
17+ 
18+- **计算公式**:
19+ 
20+ 取旋转位置编码的正向计算中,broadcast的轴列表为`dims`,在D维度上的切片范围为`[start, end)`,令参与计算的切片数据为:
21+ $$
22+ dy' = dy[..., start:end]
23+ $$
24+ $$
25+ cos' = cos[..., start:end]
26+ $$
27+ $$
28+ sin' = sin[..., start:end]
29+ $$
30+ 则梯度计算公式可表达如下:
31+ 
32+ (1)half模式(rotary_mode等于0):
33+ $$
34+ dy1', dy2' = chunk(dy', chunks=2, dim=-1)
35+ $$
36+ 
37+ $$
38+ cos1', cos2' = chunk(cos', chunks=2, dim=-1)
39+ $$
40+ 
41+ $$
42+ sin1', sin2' = chunk(sin', chunks=2, dim=-1)
43+ $$
44+ 
45+ $$
46+ dx' = cat((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1)
47+ $$
48+ 
49+ 
50+ 
51+ (2)interleave模式(rotary_mode等于1):
52+ $$
53+ dy1', dy2' = dy'[..., :: 2], dy'[..., 1 :: 2]
54+ $$
55+ 
56+ $$
57+ cos1', cos2' = cos'[..., :: 2], cos'[..., 1 :: 2]
58+ $$
59+ 
60+ $$
61+ sin1', sin2' = sin'[..., :: 2], sin'[..., 1 :: 2]
62+ $$
63+ 
64+ $$
65+ dx' = stack((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1).reshape(dy'.shape)
66+ $$
67+ 
68+ 
69+ 
70+ (3)quarter模式(rotary_mode等于2):
71+ $$
72+ dy1', dy2', dy3', dy4' = chunk(dy', chunks=4, dim=-1)
73+ $$
74+ 
75+ $$
76+ cos1', cos2', cos3', cos4' = chunk(cos', chunks=4, dim=-1)
77+ $$
78+ 
79+ $$
80+ sin1', sin2', sin3', sin4' = chunk(sin', chunks=4, dim=-1)
81+ $$
82+ 
83+ $$
84+ dx' = cat((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1', cos3' * dy3' + sin4' * dy4', cos4' * dy4' - sin3' * dy3'), dim=-1)
85+ $$
86+ 
87+ 
88+ 
89+ (4)interleave-half模式(rotary_mode等于3):
90+ $$
91+ dy1', dy2' = chunk(dy', chunks=2, dim=-1)
92+ $$
93+ 
94+ $$
95+ cos1', cos2' = chunk(cos', chunks=2, dim=-1)
96+ $$
97+ 
98+ $$
99+ sin1', sin2' = chunk(sin', chunks=2, dim=-1)
100+ $$
101+ 
102+ $$
103+ dx' = stack((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1).reshape(dy'.shape)
104+ $$
105+ 
106+ 
107+ 
108+## 参数说明
109+ 
110+<table style="table-layout: auto; width: 100%">
111+<colgroup>
112+ <col style="width: 10%">
113+ <col style="width: 10%">
114+ <col style="width: 40%">
115+ <col style="width: 20%">
116+ <col style="width: 10%">
117+</colgroup>
chenjiao

参数好像不是def.cpp里的,约束说明里好像也不是算子层的,好像是aclnn API层的

likedislike
118+<thead>
119+ <tr>
120+ <th>参数名</th>
121+ <th>输入/输出/属性</th>
122+ <th>描述</th>
123+ <th>数据类型</th>
124+ <th>数据格式</th>
125+ </tr>
126+</thead>
127+<tbody>
128+ <tr>
129+ <td>dyRef</td>
130+ <td>输入</td>
131+ <td>公式中的dy,表示正向计算输出y的导数,inplace更新为正向输入x的导数。Inplace模式,dyRef同时作为输出写入结果。</td>
132+ <td>BFLOAT16、FLOAT16、FLOAT32</td>
133+ <td>ND</td>
134+ </tr>
135+ <tr>
136+ <td>cos</td>
137+ <td>输入</td>
138+ <td>公式中的cos,正向计算输入,需与sin数据类型一致。</td>
139+ <td>BFLOAT16、FLOAT16、FLOAT32</td>
140+ <td>ND</td>
141+ </tr>
142+ <tr>
143+ <td>sin</td>
144+ <td>输入</td>
145+ <td>公式中的sin,正向计算输入,需与cos数据类型一致。</td>
146+ <td>BFLOAT16、FLOAT16、FLOAT32</td>
147+ <td>ND</td>
148+ </tr>
149+ <tr>
150+ <td>rotaryMode</td>
151+ <td>属性</td>
152+ <td>旋转模式,0=half,1=interleave,2=quarter,3=interleave-half。</td>
153+ <td>INT64</td>
154+ <td>-</td>
155+ </tr>
156+ <tr>
157+ <td>partialSlice</td>
158+ <td>属性</td>
159+ <td>D维度上的切片范围[start, end),默认{0, 0}表示不做有效计算。start须在[0, D]内,end须在[start, D]内。</td>
160+ <td>IntArray</td>
161+ <td>-</td>
162+ </tr>
163+</tbody>
164+</table>
165+ 
166+## 约束说明
167+ 
168+- 该算子仅支持Ascend 950 AI Processor。
169+- 该算子仅支持连续Tensor,不支持非连续Tensor。
170+- 该算子当前版本仅支持 interleave 模式(`rotary_mode=1`)。其他模式暂不支持。
171+- Inplace执行:输入dyRef和输出共享同一个Tensor,计算结果直接写回输入dyRef。
172+- 输入dyRef当前只支持BSND排布,输入cos/sin的shape必须与dyRef满足B/S/N维度的广播关系(如BSND、111D、1SND、B1ND、BS1D、11ND、B11D、1S1D等)。各参数的shape约束可以描述如下:
173+ - 输入张量dyRef的最后一维大小D必须小于等于1024。
174+ - 输入张量cos、sin的最后一维大小必须等于切片长度(end - start)。
175+ - 输入张量cos和sin的shape必须完全相同,cos和sin的B、S、N维度需要与dyRef满足[broadcast关系](../../docs/zh/context/broadcast关系.md),且广播后的B、S、N必须等于dyRef的B、S、N。
176+ - half、interleave和interleave-half模式下,切片长度(end - start)必须能被2整除。
177+ - quarter模式下,切片长度(end - start)必须能被4整除。
178+ - 当start等于end时,算子不执行有效计算,直接返回。
179+ - 输入张量cos和sin的数据类型必须相同。
180+ 
181+ 
182+## 调用说明
183+ 
184+| 调用方式 | 调用样例 | 说明 |
185+|----------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
186+| aclnn调用 | [test_aclnn_inplace_partial_rotary_mul_grad](./examples/arch35/test_aclnn_inplace_partial_rotary_mul_grad.cpp) | 通过[aclnnInplacePartialRotaryMulGrad](./docs/aclnnInplacePartialRotaryMulGrad.md)接口方式调用InplacePartialRotaryMulGrad算子。 |
@@ -0,0 +1,505 @@
1+# aclnnInplacePartialRotaryMulGrad
2+ 
3+[📄 查看源码](https://gitcode.com/cann/ops-transformer/tree/master/posembedding/inplace_partial_rotary_mul_grad)
4+ 
5+## 产品支持情况
6+ 
7+| 产品 | 是否支持 |
8+| :----------------------------------------------------------- |:-------:|
9+| <term>Ascend 950PR/Ascend 950DT</term> | √ |
10+| <term>Atlas A3 训练系列产品/Atlas A3 推理系列产品</term> | × |
11+| <term>Atlas A2 训练系列产品/Atlas A2 推理系列产品</term> | × |
12+| <term>Atlas 200I/500 A2 推理产品</term> | × |
13+| <term>Atlas 推理系列产品</term> | × |
14+| <term>Atlas 训练系列产品</term> | × |
15+ 
16+## 功能说明
17+ 
18+- 接口功能:执行局部旋转位置编码InplacePartialRotaryMul的反向计算。该算子对输入dy的D维度上切片[start, end)区域执行旋转位置编码梯度计算,计算结果inplace写回dy。
19+- 计算公式:
20+ 
21+ 取旋转位置编码的正向计算中,broadcast的轴列表为`dims`,在D维度上的切片范围为`[start, end)`,令参与计算的切片数据为:
22+ $$
23+ dy' = dy[..., start:end]
24+ $$
25+ $$
26+ cos' = cos[..., start:end]
27+ $$
28+ $$
29+ sin' = sin[..., start:end]
30+ $$
31+ 则梯度计算公式可表达如下:
wangyongguang

找陈娇检视下

likedislike
AlfengYuan
AlfengYuan
Jul 2 Comments:
likedislike
32+ 
33+ (1)half模式(rotary_mode等于0):
34+ 
35+ $$
36+ dy1', dy2' = chunk(dy', chunks=2, dim=-1)
37+ $$
38+ 
39+ $$
40+ cos1', cos2' = chunk(cos', chunks=2, dim=-1)
41+ $$
42+ 
43+ $$
44+ sin1', sin2' = chunk(sin', chunks=2, dim=-1)
45+ $$
46+ 
47+ $$
48+ dx' = cat((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1)
49+ $$
50+ 
51+ dx'的结果inplace写回dy的[start, end)区间。
52+ 
53+ (2)interleave模式(rotary_mode等于1):
54+ 
55+ $$
56+ dy1', dy2' = dy'[..., :: 2], dy'[..., 1 :: 2]
57+ $$
58+ 
59+ $$
60+ cos1', cos2' = cos'[..., :: 2], cos'[..., 1 :: 2]
61+ $$
62+ 
63+ $$
64+ sin1', sin2' = sin'[..., :: 2], sin'[..., 1 :: 2]
65+ $$
66+ 
67+ $$
68+ dx' = stack((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1'), dim=-1).reshape(dy'.shape)
69+ $$
70+ 
71+ dx'的结果inplace写回dy的[start, end)区间。
72+ 
73+ (3)quarter模式(rotary_mode等于2):
74+ 
75+ $$
76+ dy1', dy2', dy3', dy4' = chunk(dy', chunks=4, dim=-1)
77+ $$
78+ 
79+ $$
80+ cos1', cos2', cos3', cos4' = chunk(cos', chunks=4, dim=-1)
81+ $$
82+ 
83+ $$
84+ sin1', sin2', sin3', sin4' = chunk(sin', chunks=4, dim=-1)
85+ $$
86+ 
87+ $$
88+ dx' = cat((cos1' * dy1' + sin2' * dy2', cos2' * dy2' - sin1' * dy1', cos3' * dy3' + sin4' * dy4', cos4' * dy4' - sin3' * dy3'), dim=-1)
89+ $$
90+ 
91+ dx'的结果inplace写回dy的[start, end)区间。
92+ 
93+ (4)interleave-half模式(rotary_mode等于3):
94+ 
95+ $$
96+ dy1', dy2' = chunk(dy', chunks=2, dim=-1)
97+ $$
98+ 
99+ $$
100+ cos1', cos2' = chunk(cos', chunks=2, dim=-1)
@@ -0,0 +1,173 @@
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+#include "acl/acl.h"
12+#include "aclnnop/aclnn_inplace_partial_rotary_mul_grad.h"
13+#include <iostream>
14+#include <vector>
15+ 
16+#define CHECK_RET(cond, return_expr) \
17+ do { \
18+ if (!(cond)) { \
19+ return_expr; \
20+ } \
21+ } while (0)
22+ 
23+#define LOG_PRINT(message, ...) \
24+ do { \
25+ printf(message, ##__VA_ARGS__); \
26+ } while (0)
27+ 
28+int64_t GetShapeSize(const std::vector<int64_t> &shape)
29+{
30+ int64_t shape_size = 1;
31+ for (auto i : shape) {
32+ shape_size *= i;
33+ }
34+ return shape_size;
35+}
36+ 
37+int Init(int32_t deviceId, aclrtStream *stream)
38+{
39+ auto ret = aclInit(nullptr);
40+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
41+ ret = aclrtSetDevice(deviceId);
42+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
43+ ret = aclrtCreateStream(stream);
44+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
45+ return 0;
46+}
47+ 
48+template <typename T>
49+int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
50+ aclDataType dataType, aclTensor **tensor)
51+{
52+ auto size = GetShapeSize(shape) * sizeof(T);
53+ auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
54+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
55+ 
56+ ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
57+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
58+ 
59+ std::vector<int64_t> strides(shape.size(), 1);
60+ for (int64_t i = shape.size() - 2; i >= 0; i--) {
61+ strides[i] = shape[i + 1] * strides[i + 1];
62+ }
63+ 
64+ *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
65+ shape.data(), shape.size(), *deviceAddr);
66+ return 0;
67+}
68+ 
69+int main()
70+{
71+ int32_t deviceId = 0;
72+ aclrtStream stream;
73+ auto ret = Init(deviceId, &stream);
74+ CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
75+ 
76+ std::vector<int64_t> dyShape = {1, 1, 1, 128};
77+ std::vector<int64_t> cosShape = {1, 1, 1, 128};
78+ std::vector<int64_t> sinShape = {1, 1, 1, 128};
79+ int64_t rotaryMode = 1;
80+ std::vector<int64_t> partialSliceData = {0, 128};
81+ 
82+ void *dyRefDeviceAddr = nullptr;
83+ void *cosDeviceAddr = nullptr;
84+ void *sinDeviceAddr = nullptr;
85+ aclTensor *dyRef = nullptr;
86+ aclTensor *cos = nullptr;
87+ aclTensor *sin = nullptr;
88+ 
89+ std::vector<float> dyHostData = {
90+ 74, 54, 84, 125, 23, 78, 37, 72, 27, 98, 34, 107, 29, 23, 54, 60, 70, 49, 119, 54, 29, 54,
91+ 41, 99, 27, 62, 5, 46, 108, 39, 24, 123, 33, 82, 6, 40, 88, 24, 6, 116, 38, 119, 110, 5,
92+ 30, 79, 87, 18, 29, 100, 90, 24, 21, 93, 63, 68, 34, 112, 119, 48, 74, 43, 85, 64, 14, 49,
93+ 128, 59, 18, 37, 123, 76, 14, 63, 10, 39, 107, 124, 79, 16, 17, 76, 80, 47, 90, 41, 58, 82,
94+ 75, 80, 69, 37, 74, 36, 54, 26, 32, 54, 13, 100, 105, 15, 13, 69, 122, 26, 94, 59, 29, 14,
95+ 60, 8, 24, 17, 45, 33, 107, 122, 63, 111, 75, 128, 68, 31, 105, 6, 82, 99};
96+ std::vector<float> cosHostData = {
97+ 41, 37, 17, 25, 49, 25, 22, 24, 110, 120, 107, 3, 82, 66, 75, 86, 85, 115, 110, 56, 52, 39,
98+ 86, 23, 36, 71, 20, 73, 113, 25, 114, 56, 125, 80, 95, 82, 31, 63, 99, 62, 23, 55, 30, 99,
99+ 42, 121, 15, 24, 97, 87, 81, 67, 43, 21, 13, 9, 33, 29, 117, 10, 114, 61, 98, 15, 78, 108,
100+ 48, 97, 1, 3, 78, 109, 57, 46, 47, 56, 50, 66, 81, 77, 17, 128, 68, 121, 47, 91, 114, 125,
101+ 51, 108, 31, 15, 47, 78, 109, 115, 113, 26, 53, 97, 1, 111, 103, 58, 106, 68, 11, 104, 22, 79,
102+ 61, 127, 86, 39, 33, 123, 102, 39, 64, 41, 119, 120, 61, 29, 94, 68, 36, 12};
103+ std::vector<float> sinHostData = {
104+ 46, 56, 56, 101, 66, 10, 96, 16, 86, 57, 102, 66, 12, 105, 76, 58, 90, 6, 79, 128, 126, 82,
105+ 41, 3, 45, 7, 66, 4, 46, 22, 31, 26, 37, 63, 97, 84, 91, 90, 47, 77, 90, 34, 41, 83,
106+ 91, 108, 120, 13, 90, 32, 85, 37, 119, 31, 51, 82, 122, 125, 7, 116, 121, 108, 38, 56, 100, 20,
107+ 97, 119, 10, 4, 53, 13, 46, 82, 103, 119, 124, 80, 23, 67, 78, 56, 119, 122, 40, 58, 128, 27,
108+ 30, 52, 71, 42, 123, 69, 4, 5, 116, 97, 38, 107, 8, 4, 65, 120, 40, 22, 60, 44, 48, 66,
109+ 68, 125, 4, 93, 112, 112, 113, 90, 94, 23, 104, 39, 85, 84, 64, 128, 96, 119};
110+ 
111+ // 创建dyRef aclTensor(inplace操作,dyRef同时作为输入和输出)
112+ ret = CreateAclTensor(dyHostData, dyShape, &dyRefDeviceAddr, aclDataType::ACL_FLOAT, &dyRef);
113+ CHECK_RET(ret == ACL_SUCCESS, return ret);
114+ // 创建cos aclTensor
115+ ret = CreateAclTensor(cosHostData, cosShape, &cosDeviceAddr, aclDataType::ACL_FLOAT, &cos);
116+ CHECK_RET(ret == ACL_SUCCESS, return ret);
117+ // 创建sin aclTensor
118+ ret = CreateAclTensor(sinHostData, sinShape, &sinDeviceAddr, aclDataType::ACL_FLOAT, &sin);
119+ CHECK_RET(ret == ACL_SUCCESS, return ret);
120+ // 创建partialSlice aclIntArray
121+ aclIntArray *partialSlice = aclCreateIntArray(partialSliceData.data(), partialSliceData.size());
122+ 
123+ // 调用aclnnInplacePartialRotaryMulGrad第一段接口
124+ uint64_t workspaceSize = 0;
125+ aclOpExecutor *executor;
126+ ret = aclnnInplacePartialRotaryMulGradGetWorkspaceSize(dyRef, cos, sin, rotaryMode, partialSlice, &workspaceSize,
127+ &executor);
128+ CHECK_RET(ret == ACL_SUCCESS,
129+ LOG_PRINT("aclnnInplacePartialRotaryMulGradGetWorkspaceSize failed. ERROR: %d\n", ret);
130+ return ret);
131+ 
132+ void *workspaceAddr = nullptr;
133+ if (workspaceSize > 0) {
134+ ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
135+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
136+ }
137+ 
138+ // 调用aclnnInplacePartialRotaryMulGrad第二段接口
139+ ret = aclnnInplacePartialRotaryMulGrad(workspaceAddr, workspaceSize, executor, stream);
140+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplacePartialRotaryMulGrad failed. ERROR: %d\n", ret); return ret);
141+ 
142+ ret = aclrtSynchronizeStream(stream);
143+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
144+ 
145+ // 获取结果(inplace,直接从dyRefDeviceAddr读取即可)
146+ auto size = GetShapeSize(dyShape);
147+ std::vector<float> resultData(size, 0);
148+ ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), dyRefDeviceAddr,
149+ size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
150+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
151+ 
152+ for (int64_t i = 0; i < size; i++) {
153+ LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
154+ }
155+ 
156+ // 释放资源
157+ aclDestroyIntArray(partialSlice);
158+ aclDestroyTensor(dyRef);
159+ aclDestroyTensor(cos);
160+ aclDestroyTensor(sin);
161+ 
162+ aclrtFree(dyRefDeviceAddr);
163+ aclrtFree(cosDeviceAddr);
164+ aclrtFree(sinDeviceAddr);
165+ if (workspaceSize > 0) {
166+ aclrtFree(workspaceAddr);
167+ }
168+ aclrtDestroyStream(stream);
169+ aclrtResetDevice(deviceId);
170+ aclFinalize();
171+ 
172+ return 0;
173+}
@@ -0,0 +1,259 @@
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 test_aclnn_inplace_partial_rotary_mul_grad_bab.cpp
13+ * \brief BAB template test for InplacePartialRotaryMulGrad (multi-B/N, partial slice, interleave mode).
14+ *
15+ * BAB template constraints:
16+ * - Layout: BSND (dy shape [B, S, N, D])
17+ * - cosb_==1 (cos/sin broadcast on B dimension)
18+ * - rotary_mode=1 (interleave) only
19+ *
20+ * This test covers:
21+ * - B=2, S=2, N=2, D=8 (multi-batch, multi-head)
22+ * - cos/sin shape [1, 2, 1, 4] (broadcast on B and N)
23+ * - partialSliceData = {2, 6} (rotary applied only to D indices [2, 6))
24+ * - Elements outside the slice pass through unchanged
25+ *
26+ * Interleave gradient formula (within slice [start, end)):
27+ * dx[start+2k] = cos[k*2] * dy[start+2k] + sin[k*2+1] * dy[start+2k+1]
28+ * dx[start+2k+1] = cos[k*2+1] * dy[start+2k+1] - sin[k*2] * dy[start+2k]
29+ */
30+ 
31+#include "acl/acl.h"
32+#include "aclnnop/aclnn_inplace_partial_rotary_mul_grad.h"
33+#include <cmath>
34+#include <cstdint>
35+#include <iostream>
36+#include <vector>
37+ 
38+#define CHECK_RET(cond, return_expr) \
39+ do { \
40+ if (!(cond)) { \
41+ return_expr; \
42+ } \
43+ } while (0)
44+ 
45+#define LOG_PRINT(message, ...) \
46+ do { \
47+ printf(message, ##__VA_ARGS__); \
48+ } while (0)
49+ 
50+int64_t GetShapeSize(const std::vector<int64_t> &shape)
51+{
52+ int64_t shape_size = 1;
53+ for (auto i : shape) {
54+ shape_size *= i;
55+ }
56+ return shape_size;
57+}
58+ 
59+int Init(int32_t deviceId, aclrtStream *stream)
60+{
61+ auto ret = aclInit(nullptr);
62+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
63+ ret = aclrtSetDevice(deviceId);
64+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
65+ ret = aclrtCreateStream(stream);
66+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
67+ return 0;
68+}
69+ 
70+template <typename T>
71+int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
72+ aclDataType dataType, aclTensor **tensor)
73+{
74+ auto size = GetShapeSize(shape) * sizeof(T);
75+ auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
76+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
77+ 
78+ ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
79+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
80+ 
81+ std::vector<int64_t> strides(shape.size(), 1);
82+ for (int64_t i = shape.size() - 2; i >= 0; i--) {
83+ strides[i] = shape[i + 1] * strides[i + 1];
84+ }
85+ 
86+ *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
87+ shape.data(), shape.size(), *deviceAddr);
88+ return 0;
89+}
90+ 
91+// Reference: interleave gradient computation with partial slice
92+void InterleaveGradRef(const std::vector<float> &dy, const std::vector<float> &cos, const std::vector<float> &sin,
93+ std::vector<float> &dx, int64_t b, int64_t s, int64_t n, int64_t d, int64_t sliceStart,
94+ int64_t sliceEnd)
95+{
96+ int64_t cosD = sliceEnd - sliceStart;
97+ for (int64_t bi = 0; bi < b; bi++) {
98+ for (int64_t si = 0; si < s; si++) {
99+ for (int64_t ni = 0; ni < n; ni++) {
100+ int64_t groupOffset = ((bi * s + si) * n + ni) * d;
101+ // Copy dy to dx first (pass-through for elements outside the slice)
102+ for (int64_t di = 0; di < d; di++) {
103+ dx[groupOffset + di] = dy[groupOffset + di];
104+ }
105+ // Apply interleave rotary grad on the slice
106+ int64_t cosSOffset = si * cosD;
107+ for (int64_t k = 0; k < cosD / 2; k++) {
108+ int64_t idx0 = groupOffset + sliceStart + 2 * k;
109+ int64_t idx1 = groupOffset + sliceStart + 2 * k + 1;
110+ int64_t cIdx0 = cosSOffset + 2 * k;
111+ int64_t cIdx1 = cosSOffset + 2 * k + 1;
112+ dx[idx0] = cos[cIdx0] * dy[idx0] + sin[cIdx1] * dy[idx1];
113+ dx[idx1] = cos[cIdx1] * dy[idx1] - sin[cIdx0] * dy[idx0];
114+ }
115+ }
116+ }
117+ }
118+}
119+ 
120+int main()
121+{
122+ int32_t deviceId = 0;
123+ aclrtStream stream;
124+ auto ret = Init(deviceId, &stream);
125+ CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
126+ 
127+ // BSND layout: dy=[B, S, N, D], cos=[1, S, 1, D_cos], cosb_=1
128+ int64_t B = 2;
129+ int64_t S = 2;
130+ int64_t N = 2;
131+ int64_t D = 8;
132+ 
133+ std::vector<int64_t> dyShape = {B, S, N, D};
134+ std::vector<int64_t> cosShape = {1, S, 1, 4}; // cosb_=1: B dimension broadcast; D_cos=4
135+ std::vector<int64_t> sinShape = {1, S, 1, 4};
136+ int64_t rotaryMode = 1; // interleave
137+ std::vector<int64_t> partialSliceData = {2, 6};
138+ 
139+ // Test data
140+ // dy: 8 groups (B*S*N = 2*2*2 = 8) of 8 elements each
141+ // Each group: [1, 2, 3, 4, 5, 6, 7, 8]
142+ // cos: S=2 groups, each [0.5, 0.6, 0.7, 0.8]
143+ // sin: S=2 groups, each [0.1, 0.2, 0.3, 0.4]
144+ //
145+ // partialSliceData = {2, 6} => rotary applied to D indices [2, 6), i.e. positions 2,3,4,5
146+ // Elements at D indices 0,1,6,7 pass through unchanged.
147+ //
148+ // Interleave grad within slice [2, 6):
149+ // Pair (2,3): cos[0,1]=[0.5,0.6], sin[0,1]=[0.1,0.2]
150+ // dx[2] = 0.5*3 + 0.2*4 = 1.5 + 0.8 = 2.3
151+ // dx[3] = 0.6*4 - 0.1*3 = 2.4 - 0.3 = 2.1
152+ // Pair (4,5): cos[2,3]=[0.7,0.8], sin[2,3]=[0.3,0.4]
153+ // dx[4] = 0.7*5 + 0.4*6 = 3.5 + 2.4 = 5.9
154+ // dx[5] = 0.8*6 - 0.3*5 = 4.8 - 1.5 = 3.3
155+ //
156+ // Per-group expected dx: [1, 2, 2.3, 2.1, 5.9, 3.3, 7, 8]
157+ 
158+ std::vector<float> dyHostData;
159+ std::vector<float> expectedDx;
160+ for (int i = 0; i < B * S * N; i++) {
161+ dyHostData.insert(dyHostData.end(), {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f});
162+ expectedDx.insert(expectedDx.end(), {1.0f, 2.0f, 2.3f, 2.1f, 5.9f, 3.3f, 7.0f, 8.0f});
163+ }
164+ 
165+ // cos/sin: same values for both S positions (broadcast on B and N)
166+ std::vector<float> cosHostData = {0.5f, 0.6f, 0.7f, 0.8f, 0.5f, 0.6f, 0.7f, 0.8f};
167+ std::vector<float> sinHostData = {0.1f, 0.2f, 0.3f, 0.4f, 0.1f, 0.2f, 0.3f, 0.4f};
168+ 
169+ std::vector<float> referenceDx(expectedDx.size(), 0.0f);
170+ InterleaveGradRef(dyHostData, cosHostData, sinHostData, referenceDx, B, S, N, D, partialSliceData[0],
171+ partialSliceData[1]);
172+ 
173+ void *dyRefDeviceAddr = nullptr;
174+ void *cosDeviceAddr = nullptr;
175+ void *sinDeviceAddr = nullptr;
176+ aclTensor *dyRef = nullptr;
177+ aclTensor *cos = nullptr;
178+ aclTensor *sin = nullptr;
179+ 
180+ ret = CreateAclTensor(dyHostData, dyShape, &dyRefDeviceAddr, aclDataType::ACL_FLOAT, &dyRef);
181+ CHECK_RET(ret == ACL_SUCCESS, return ret);
182+ ret = CreateAclTensor(cosHostData, cosShape, &cosDeviceAddr, aclDataType::ACL_FLOAT, &cos);
183+ CHECK_RET(ret == ACL_SUCCESS, return ret);
184+ ret = CreateAclTensor(sinHostData, sinShape, &sinDeviceAddr, aclDataType::ACL_FLOAT, &sin);
185+ CHECK_RET(ret == ACL_SUCCESS, return ret);
186+ 
187+ aclIntArray *partialSlice = aclCreateIntArray(partialSliceData.data(), partialSliceData.size());
188+ 
189+ 
190+ // First stage: get workspace size
191+ uint64_t workspaceSize = 0;
192+ aclOpExecutor *executor;
193+ ret = aclnnInplacePartialRotaryMulGradGetWorkspaceSize(dyRef, cos, sin, rotaryMode, partialSlice, &workspaceSize,
194+ &executor);
195+ CHECK_RET(ret == ACL_SUCCESS,
196+ LOG_PRINT("aclnnInplacePartialRotaryMulGradGetWorkspaceSize failed. ERROR: %d\n", ret);
197+ return ret);
198+ 
199+ void *workspaceAddr = nullptr;
200+ if (workspaceSize > 0) {
201+ ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
202+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
203+ }
204+ 
205+ // Second stage: execute
206+ ret = aclnnInplacePartialRotaryMulGrad(workspaceAddr, workspaceSize, executor, stream);
207+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplacePartialRotaryMulGrad failed. ERROR: %d\n", ret); return ret);
208+ 
209+ ret = aclrtSynchronizeStream(stream);
210+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
211+ 
212+ // Read back result (inplace: dyRef was overwritten with dx)
213+ auto size = GetShapeSize(dyShape);
214+ std::vector<float> resultData(size, 0);
215+ ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), dyRefDeviceAddr,
216+ size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
217+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
218+ 
219+ // Verify results
220+ const float epsilon = 1e-6f;
221+ bool allPassed = true;
222+ for (int64_t i = 0; i < size; i++) {
223+ bool ok = std::fabs(resultData[i] - referenceDx[i]) < epsilon;
224+ if (!ok) {
225+ LOG_PRINT("MISMATCH at [%ld]: got %.6f, expected %.6f (reference %.6f)\n", i, resultData[i], expectedDx[i],
226+ referenceDx[i]);
227+ allPassed = false;
228+ }
229+ }
230+ 
231+ if (allPassed) {
232+ LOG_PRINT("BAB test PASSED: all %ld elements match reference.\n", size);
233+ LOG_PRINT("Result: ");
234+ for (int64_t i = 0; i < size; i++) {
235+ LOG_PRINT("%.6f ", resultData[i]);
236+ }
237+ LOG_PRINT("\n");
238+ } else {
239+ LOG_PRINT("BAB test FAILED.\n");
240+ }
241+ 
242+ // Cleanup
243+ aclDestroyIntArray(partialSlice);
244+ aclDestroyTensor(dyRef);
245+ aclDestroyTensor(cos);
246+ aclDestroyTensor(sin);
247+ 
248+ aclrtFree(dyRefDeviceAddr);
249+ aclrtFree(cosDeviceAddr);
250+ aclrtFree(sinDeviceAddr);
251+ if (workspaceSize > 0) {
252+ aclrtFree(workspaceAddr);
253+ }
254+ aclrtDestroyStream(stream);
255+ aclrtResetDevice(deviceId);
256+ aclFinalize();
257+ 
258+ return allPassed ? 0 : 1;
259+}
@@ -0,0 +1,12 @@
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+message(STATUS "=== Debug: start ops.posembedding.inplace_partial_rotary_mul_grad.graph_plugin.CMakeLists.txt ")
12+add_graph_plugin_sources()
@@ -0,0 +1,72 @@
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 inplace_partial_rotary_mul_grad_proto.h
13+ * \brief
14+ */
15+#ifndef OPS_OP_PROTO_INC_INPLACE_PARTIAL_ROTARY_MUL_GRAD_OPS_H_
16+#define OPS_OP_PROTO_INC_INPLACE_PARTIAL_ROTARY_MUL_GRAD_OPS_H_
17+ 
18+#include "graph/operator_reg.h"
19+ 
20+namespace ge {
21+/**
22+ * @brief Backwards calculation of InplacePartialRotaryMul.
23+ * @par Inputs:
24+ * @li dy: A 4D tensor which represents the gradient of output "y" in InplacePartialRotaryMul, updated
25+ * in-place to be the grad of input "x" in InplacePartialRotaryMul. Format supports ND, and data type must be float16,
26+ * float32 or bfloat16.
27+ * @li cos: A 4D tensor which is input "cos" in InplacePartialRotaryMul, format supports ND, data type must be
28+ * float16, float32 or bfloat16 and must be the same as "sin", and shape must be the same as "sin". The D of "cos"
29+ * must be equal to the slice length ("partial_slice"'s end - start).
wangyongguang

注释让陈萍萍检视下

likedislike
AlfengYuan
AlfengYuan
Jul 2 Comments:
likedislike
30+ * @li sin: A 4D tensor which is input "sin" in InplacePartialRotaryMul, format supports ND, data type must be
31+ * float16, float32 or bfloat16 and must be the same as "cos", and shape must be the same as "cos". The D of "sin"
32+ * must be equal to the slice length ("partial_slice"'s end - start).
33+ * @par Outputs:
34+ * @li dy: A 4D Tensor, in-place updated from input "dy", which is the grad of input "x" in
35+ * InplacePartialRotaryMul. Format supports ND, data type and shape must be the same as input "dy".
36+ * @par Attributes:
37+ * @li rotary_mode: An optional attribute of type int, specifying the mode of rotary position embedding, must be
38+ * 0-"half", 1-"interleave", 2-"quarter" or 3-"interleave-half". Defaults to 0.
39+ * @li partial_slice: An optional attribute of type list int, specifying the slice range of the partial rotary position
40+ * embedding. The value is a list of two integers. Defaults to {0, 0}.
41+ * @attention Constraints:
42+ * @li This operator is only supported on Ascend 950 AI Processor.
43+ * @li Currently only interleave mode (rotary_mode=1) is supported. Other modes (half=0, quarter=2,
44+ * interleave-half=3) are not implemented yet.
45+ * Let (B, S, N, D) represents the shape of the input "dy". Under this
46+ * representation, the shape constraints of each parameter can be described as follows:
47+ * @li The D of "dy" must be equal and less or equal to 1024. The D of "cos", "sin"
48+ * must be equal to the slice length ("partial_slice"'s end - start).
49+ * @li B, S, N of "cos", "sin" must broadcast to match the B, S, N of "dy". The D of these
50+ * tensors equals ("partial_slice"'s end - start). Specifically, B, S, N must meet one of the following four conditions:
51+ * - B, S, N are 1, means the shape is (1, 1, 1, end-start).
52+ * - B, S, N are the same as that of "dy", means the shape is (B, S, N, end-start).
53+ * - One of S and N is 1, the remaining one dimension and B are the same as that of "dy", means the shape is (B, 1, N,
54+ * end-start) or (B, S, 1, end-start).
55+ * - Two of B, S and N are 1, the remaining one dimension is the same as that of "dy", means the shape is (1, 1, N,
56+ * end-start), (1, S, 1, end-start) or (B, 1, 1, end-start).
57+ * @li Let the value of "partial_slice" be [start, end]. "start" must be in range [0, D], "end" must be in range
58+ * ["start", D]. In half, interleave and interleave-half mode, ("end" - "start") must be a multiple of 2. In quarter
59+ * mode, ("end" - "start") must be a multiple of 4. If "start" equals "end", the operator performs no
60+ * effective computation and returns directly.
61+ */
62+REG_OP(InplacePartialRotaryMulGrad)
63+ .INPUT(dy, TensorType({DT_FLOAT16, DT_FLOAT, DT_BFLOAT16}))
64+ .INPUT(cos, TensorType({DT_FLOAT16, DT_FLOAT, DT_BFLOAT16}))
65+ .INPUT(sin, TensorType({DT_FLOAT16, DT_FLOAT, DT_BFLOAT16}))
66+ .OUTPUT(dy, TensorType({DT_FLOAT16, DT_FLOAT, DT_BFLOAT16}))
67+ .ATTR(rotary_mode, Int, 0)
68+ .ATTR(partial_slice, ListInt, {0, 0})
69+ .OP_END_FACTORY_REG(InplacePartialRotaryMulGrad)
70+} // namespace ge
71+ 
72+#endif
@@ -0,0 +1,27 @@
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+if (BUILD_OPEN_PROJECT)
12+ target_sources(op_host_aclnnInner PRIVATE
13+ inplace_partial_rotary_mul_grad_def.cpp
14+ )
15+ add_modules_sources(OPTYPE inplace_partial_rotary_mul_grad ACLNNTYPE aclnn)
16+endif()
17+ 
18+if (BUILD_OPS_RTY_KERNEL)
19+ add_ops_compile_options(
20+ OP_NAME InplacePartialRotaryMulGrad
21+ OPTIONS --cce-auto-sync=on
22+ -Wno-deprecated-declarations
23+ -Werror
24+ )
25+else()
26+ add_modules_sources(OPTYPE inplace_partial_rotary_mul_grad ACLNNTYPE aclnn)
27+endif()
@@ -0,0 +1,464 @@
1+{
2+ "op_type": "InplacePartialRotaryMulGrad",
3+ "op_list": [
4+ {
5+ "bin_filename": "InplacePartialRotaryMulGrad_fp16_fp16_a431b172c64a720f415bf9412006d624",
6+ "inputs": [
7+ {
8+ "name": "dy",
9+ "index": 0,
10+ "dtype": "float16",
11+ "format": "ND",
12+ "paramType": "required",
13+ "shape": [-2]
14+ },
15+ {
16+ "name": "cos",
17+ "index": 1,
18+ "dtype": "float16",
19+ "format": "ND",
20+ "paramType": "required",
21+ "shape": [-2]
22+ },
23+ {
24+ "name": "sin",
25+ "index": 2,
26+ "dtype": "float16",
27+ "format": "ND",
28+ "paramType": "required",
29+ "shape": [-2]
30+ }
31+ ],
32+ "outputs": [
33+ {
34+ "name": "dy",
35+ "index": 0,
36+ "dtype": "float16",
37+ "format": "ND",
38+ "paramType": "required",
39+ "shape": [-2]
40+ }
41+ ],
42+ "attrs": [
43+ {
44+ "name": "rotary_mode",
45+ "dtype": "int",
46+ "value": 0
47+ },
48+ {
49+ "name": "partial_slice",
50+ "dtype": "listInt",
51+ "value": [0, 0]
52+ }
53+ ]
54+ },
55+ {
56+ "bin_filename": "InplacePartialRotaryMulGrad_fp16_fp32_3b2100d750ff36dea5d548600f496409",
57+ "inputs": [
58+ {
59+ "name": "dy",
60+ "index": 0,
61+ "dtype": "float16",
62+ "format": "ND",
63+ "paramType": "required",
64+ "shape": [-2]
65+ },
66+ {
67+ "name": "cos",
68+ "index": 1,
69+ "dtype": "float32",
70+ "format": "ND",
71+ "paramType": "required",
72+ "shape": [-2]
73+ },
74+ {
75+ "name": "sin",
76+ "index": 2,
77+ "dtype": "float32",
78+ "format": "ND",
79+ "paramType": "required",
80+ "shape": [-2]
81+ }
82+ ],
83+ "outputs": [
84+ {
85+ "name": "dy",
86+ "index": 0,
87+ "dtype": "float16",
88+ "format": "ND",
89+ "paramType": "required",
90+ "shape": [-2]
91+ }
92+ ],
93+ "attrs": [
94+ {
95+ "name": "rotary_mode",
96+ "dtype": "int",
97+ "value": 0
98+ },
99+ {
100+ "name": "partial_slice",
101+ "dtype": "listInt",
102+ "value": [0, 0]
103+ }
104+ ]
105+ },
106+ {
107+ "bin_filename": "InplacePartialRotaryMulGrad_fp16_bf16_fab80e552d2fb01dc1cfbd79769973ac",
108+ "inputs": [
109+ {
110+ "name": "dy",
111+ "index": 0,
112+ "dtype": "float16",
113+ "format": "ND",
114+ "paramType": "required",
115+ "shape": [-2]
116+ },
117+ {
118+ "name": "cos",
119+ "index": 1,
120+ "dtype": "bfloat16",
121+ "format": "ND",
122+ "paramType": "required",
123+ "shape": [-2]
124+ },
125+ {
126+ "name": "sin",
127+ "index": 2,
128+ "dtype": "bfloat16",
129+ "format": "ND",
130+ "paramType": "required",
131+ "shape": [-2]
132+ }
133+ ],
134+ "outputs": [
135+ {
136+ "name": "dy",
137+ "index": 0,
138+ "dtype": "float16",
139+ "format": "ND",
140+ "paramType": "required",
141+ "shape": [-2]
142+ }
143+ ],
144+ "attrs": [
145+ {
146+ "name": "rotary_mode",
147+ "dtype": "int",
148+ "value": 0
149+ },
150+ {
151+ "name": "partial_slice",
152+ "dtype": "listInt",
153+ "value": [0, 0]
154+ }
155+ ]
156+ },
157+ {
158+ "bin_filename": "InplacePartialRotaryMulGrad_fp32_fp16_83e634653b7413b9b65f3cc68b2d9389",
159+ "inputs": [
160+ {
161+ "name": "dy",
162+ "index": 0,
163+ "dtype": "float32",
164+ "format": "ND",
165+ "paramType": "required",
166+ "shape": [-2]
167+ },
168+ {
169+ "name": "cos",
170+ "index": 1,
171+ "dtype": "float16",
172+ "format": "ND",
173+ "paramType": "required",
174+ "shape": [-2]
175+ },
176+ {
177+ "name": "sin",
178+ "index": 2,
179+ "dtype": "float16",
180+ "format": "ND",
181+ "paramType": "required",
182+ "shape": [-2]
183+ }
184+ ],
185+ "outputs": [
186+ {
187+ "name": "dy",
188+ "index": 0,
189+ "dtype": "float32",
190+ "format": "ND",
191+ "paramType": "required",
192+ "shape": [-2]
193+ }
194+ ],
195+ "attrs": [
196+ {
197+ "name": "rotary_mode",
198+ "dtype": "int",
199+ "value": 0
200+ },
201+ {
202+ "name": "partial_slice",
203+ "dtype": "listInt",
204+ "value": [0, 0]
205+ }
206+ ]
207+ },
208+ {
209+ "bin_filename": "InplacePartialRotaryMulGrad_fp32_fp32_20928f043fcd5087a58be042e9cdb613",
210+ "inputs": [
211+ {
212+ "name": "dy",
213+ "index": 0,
214+ "dtype": "float32",
215+ "format": "ND",
216+ "paramType": "required",
217+ "shape": [-2]
218+ },
219+ {
220+ "name": "cos",
221+ "index": 1,
222+ "dtype": "float32",
223+ "format": "ND",
224+ "paramType": "required",
225+ "shape": [-2]
226+ },
227+ {
228+ "name": "sin",
229+ "index": 2,
230+ "dtype": "float32",
231+ "format": "ND",
232+ "paramType": "required",
233+ "shape": [-2]
234+ }
235+ ],
236+ "outputs": [
237+ {
238+ "name": "dy",
239+ "index": 0,
240+ "dtype": "float32",
241+ "format": "ND",
242+ "paramType": "required",
243+ "shape": [-2]
244+ }
245+ ],
246+ "attrs": [
247+ {
248+ "name": "rotary_mode",
249+ "dtype": "int",
250+ "value": 0
251+ },
252+ {
253+ "name": "partial_slice",
254+ "dtype": "listInt",
255+ "value": [0, 0]
256+ }
257+ ]
258+ },
259+ {
260+ "bin_filename": "InplacePartialRotaryMulGrad_fp32_bf16_555844a937e1649031deb13103c243a5",
261+ "inputs": [
262+ {
263+ "name": "dy",
264+ "index": 0,
265+ "dtype": "float32",
266+ "format": "ND",
267+ "paramType": "required",
268+ "shape": [-2]
269+ },
270+ {
271+ "name": "cos",
272+ "index": 1,
273+ "dtype": "bfloat16",
274+ "format": "ND",
275+ "paramType": "required",
276+ "shape": [-2]
277+ },
278+ {
279+ "name": "sin",
280+ "index": 2,
281+ "dtype": "bfloat16",
282+ "format": "ND",
283+ "paramType": "required",
284+ "shape": [-2]
285+ }
286+ ],
287+ "outputs": [
288+ {
289+ "name": "dy",
290+ "index": 0,
291+ "dtype": "float32",
292+ "format": "ND",
293+ "paramType": "required",
294+ "shape": [-2]
295+ }
296+ ],
297+ "attrs": [
298+ {
299+ "name": "rotary_mode",
300+ "dtype": "int",
301+ "value": 0
302+ },
303+ {
304+ "name": "partial_slice",
305+ "dtype": "listInt",
306+ "value": [0, 0]
307+ }
308+ ]
309+ },
310+ {
311+ "bin_filename": "InplacePartialRotaryMulGrad_bf16_fp16_f94e2557489c1b28e583ea799033cf32",
312+ "inputs": [
313+ {
314+ "name": "dy",
315+ "index": 0,
316+ "dtype": "bfloat16",
317+ "format": "ND",
318+ "paramType": "required",
319+ "shape": [-2]
320+ },
321+ {
322+ "name": "cos",
323+ "index": 1,
324+ "dtype": "float16",
325+ "format": "ND",
326+ "paramType": "required",
327+ "shape": [-2]
328+ },
329+ {
330+ "name": "sin",
331+ "index": 2,
332+ "dtype": "float16",
333+ "format": "ND",
334+ "paramType": "required",
335+ "shape": [-2]
336+ }
337+ ],
338+ "outputs": [
339+ {
340+ "name": "dy",
341+ "index": 0,
342+ "dtype": "bfloat16",
343+ "format": "ND",
344+ "paramType": "required",
345+ "shape": [-2]
346+ }
347+ ],
348+ "attrs": [
349+ {
350+ "name": "rotary_mode",
351+ "dtype": "int",
352+ "value": 0
353+ },
354+ {
355+ "name": "partial_slice",
356+ "dtype": "listInt",
357+ "value": [0, 0]
358+ }
359+ ]
360+ },
361+ {
362+ "bin_filename": "InplacePartialRotaryMulGrad_bf16_fp32_a6669211629fefbf913c9e5e3ec000d2",
363+ "inputs": [
364+ {
365+ "name": "dy",
366+ "index": 0,
367+ "dtype": "bfloat16",
368+ "format": "ND",
369+ "paramType": "required",
370+ "shape": [-2]
371+ },
372+ {
373+ "name": "cos",
374+ "index": 1,
375+ "dtype": "float32",
376+ "format": "ND",
377+ "paramType": "required",
378+ "shape": [-2]
379+ },
380+ {
381+ "name": "sin",
382+ "index": 2,
383+ "dtype": "float32",
384+ "format": "ND",
385+ "paramType": "required",
386+ "shape": [-2]
387+ }
388+ ],
389+ "outputs": [
390+ {
391+ "name": "dy",
392+ "index": 0,
393+ "dtype": "bfloat16",
394+ "format": "ND",
395+ "paramType": "required",
396+ "shape": [-2]
397+ }
398+ ],
399+ "attrs": [
400+ {
401+ "name": "rotary_mode",
402+ "dtype": "int",
403+ "value": 0
404+ },
405+ {
406+ "name": "partial_slice",
407+ "dtype": "listInt",
408+ "value": [0, 0]
409+ }
410+ ]
411+ },
412+ {
413+ "bin_filename": "InplacePartialRotaryMulGrad_bf16_bf16_9866ceef5fca86f186c438d3799f565f",
414+ "inputs": [
415+ {
416+ "name": "dy",
417+ "index": 0,
418+ "dtype": "bfloat16",
419+ "format": "ND",
420+ "paramType": "required",
421+ "shape": [-2]
422+ },
423+ {
424+ "name": "cos",
425+ "index": 1,
426+ "dtype": "bfloat16",
427+ "format": "ND",
428+ "paramType": "required",
429+ "shape": [-2]
430+ },
431+ {
432+ "name": "sin",
433+ "index": 2,
434+ "dtype": "bfloat16",
435+ "format": "ND",
436+ "paramType": "required",
437+ "shape": [-2]
438+ }
439+ ],
440+ "outputs": [
441+ {
442+ "name": "dy",
443+ "index": 0,
444+ "dtype": "bfloat16",
445+ "format": "ND",
446+ "paramType": "required",
447+ "shape": [-2]
448+ }
449+ ],
450+ "attrs": [
451+ {
452+ "name": "rotary_mode",
453+ "dtype": "int",
454+ "value": 0
455+ },
456+ {
457+ "name": "partial_slice",
458+ "dtype": "listInt",
459+ "value": [0, 0]
460+ }
461+ ]
462+ }
463+ ]
464+}
@@ -0,0 +1,23 @@
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+; 该文件主要影响 opc 工具 编译二进制kernel时, --simplified_key_mode 选项中填写的值,格式如下所示:
12+; [某算子]
13+; default=xx
14+; ascendxx=xx
15+; 其中,default为默认mode,ascnedxx为可选mode,如果不同芯片有差异化要求时,需要配置;
16+; 1)如果没有配置:非ascendC算子继续按空处理,即opc编译命令中不添加 --simplified_key_mode 选项,AscendC算子按照 simplified_key_mode=0 处理
17+; 2)如果仅有default配置:各个版本按default配置
18+; 3)如果仅有某些平台的配置,没有default配置:对应平台的按照配置的值传递,非对应平台的:非AscendC算子继续按空处理,AscendC算子按照 simplified_key_mode=0 处理
19+; 4)如果default配置和平台配置都有:对应平台的使用平台的配置,非对应的平台的以default值配置。
20+; 5)对于自定义simplified key的情况,需要在binary_simplified_key_mode.ini 文件中显式配置为None,不传入 --simplified_key_mode 选项,由opc工具和FE框架自行判断使用何种模式
21+; 6)是否是AscendC算子,由 ops/built-in/tbe/op_info_cfg/parser/ascendc_config.json 中配置的算子名字和对于的平台决定
22+[InplacePartialRotaryMulGrad]
23+default=0
@@ -0,0 +1,49 @@
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 inplace_partial_rotary_mul_grad_def.cpp
13+ * \brief
14+ */
15+#include "register/op_def_registry.h"
16+ 
17+namespace ops {
18+ 
19+static const std::vector<ge::DataType> DyDtype = {{ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT,
20+ ge::DT_FLOAT, ge::DT_FLOAT, ge::DT_BF16, ge::DT_BF16, ge::DT_BF16}};
21+ 
22+static const std::vector<ge::DataType> cosDtype = {{ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_BF16, ge::DT_FLOAT16,
23+ ge::DT_FLOAT, ge::DT_BF16, ge::DT_FLOAT16, ge::DT_FLOAT,
24+ ge::DT_BF16}};
25+static const std::vector<ge::Format> formatList = {{ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND,
26+ ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND,
27+ ge::FORMAT_ND}};
28+class InplacePartialRotaryMulGrad : public OpDef {
29+public:
30+ explicit InplacePartialRotaryMulGrad(const char *name) : OpDef(name)
31+ {
32+ this->Input("dy").ParamType(REQUIRED).DataType(DyDtype).Format(formatList);
33+ this->Input("cos").ParamType(REQUIRED).DataType(cosDtype).Format(formatList);
34+ this->Input("sin").ParamType(REQUIRED).DataType(cosDtype).Format(formatList);
35+ this->Output("dy").ParamType(REQUIRED).DataType(DyDtype).Format(formatList);
36+ this->Attr("rotary_mode").AttrType(OPTIONAL).Int(0);
37+ this->Attr("partial_slice").AttrType(OPTIONAL).ListInt({0, 0});
38+ 
39+ OpAICoreConfig config950;
40+ config950.DynamicCompileStaticFlag(true)
41+ .DynamicRankSupportFlag(true)
42+ .DynamicShapeSupportFlag(true)
43+ .ExtendCfgInfo("opFile.value", "inplace_partial_rotary_mul_grad_apt");
44+ this->AICore().AddConfig("ascend950", config950);
45+ }
46+};
47+ 
48+OP_ADD(InplacePartialRotaryMulGrad);
49+} // namespace ops
@@ -0,0 +1,51 @@
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 inplace_partial_rotary_mul_grad_infershape.cpp
13+ * \brief
14+ */
15+ 
16+#include "register/op_impl_registry.h"
17+#include "log/log.h"
18+#include "platform/platform_info.h"
19+ 
20+using namespace ge;
21+namespace ops {
22+static constexpr size_t INPUT_DY_INDEX = 0;
23+static constexpr size_t INPUT_COS_INDEX = 1;
24+static constexpr size_t INPUT_SIN_INDEX = 2;
25+static constexpr size_t OUTPUT_DY_INDEX = 0;
26+ 
27+ 
28+static ge::graphStatus InferShapeForInplacePartialRotaryMulGrad(gert::InferShapeContext *context)
29+{
30+ OP_LOGD(context->GetNodeName(), "Begin to do InferShapeForInplacePartialRotaryMulGrad.");
31+ const gert::Shape *dyShape = context->GetInputShape(INPUT_DY_INDEX);
32+ OP_CHECK_NULL_WITH_CONTEXT(context, dyShape);
33+ gert::Shape *outDyShape = context->GetOutputShape(OUTPUT_DY_INDEX);
34+ OP_CHECK_NULL_WITH_CONTEXT(context, outDyShape);
35+ 
36+ *outDyShape = *dyShape;
37+ 
38+ OP_LOGD(context->GetNodeName(), "End to do InferShapeForInplacePartialRotaryMulGrad.");
39+ return ge::GRAPH_SUCCESS;
40+}
41+ 
42+static graphStatus InferDataTypeForInplacePartialRotaryMulGrad(gert::InferDataTypeContext *context)
43+{
44+ context->SetOutputDataType(OUTPUT_DY_INDEX, context->GetInputDataType(INPUT_DY_INDEX));
45+ return GRAPH_SUCCESS;
46+}
47+ 
48+IMPL_OP_INFERSHAPE(InplacePartialRotaryMulGrad)
49+ .InferShape(InferShapeForInplacePartialRotaryMulGrad)
50+ .InferDataType(InferDataTypeForInplacePartialRotaryMulGrad);
51+} // namespace ops
@@ -0,0 +1,177 @@
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 inplace_partial_rotary_mul_grad_regbase_tiling.h
13+ * \brief
14+ */
15+#ifndef OPS_BUILD_IN_OP_TILING_RUNTIME_INPLACE_PARTIAL_ROTARY_MUL_GRAD_H
16+#define OPS_BUILD_IN_OP_TILING_RUNTIME_INPLACE_PARTIAL_ROTARY_MUL_GRAD_H
17+ 
18+#include "register/tilingdata_base.h"
19+#include "register/op_def_registry.h"
20+#include "op_host/tiling_templates_registry.h"
21+#include "tiling/tiling_api.h"
22+#include "op_host/tiling_base.h"
23+#include "op_host/tiling_util.h"
24+#include "platform/platform_info.h"
25+#include "util/math_util.h"
26+ 
27+namespace optiling {
28+ 
29+BEGIN_TILING_DATA_DEF(InplacePartialRotaryMulGradRegbaseTilingData)
30+TILING_DATA_FIELD_DEF(int64_t, b);
31+TILING_DATA_FIELD_DEF(int64_t, s);
32+TILING_DATA_FIELD_DEF(int64_t, d);
33+TILING_DATA_FIELD_DEF(int64_t, n);
34+TILING_DATA_FIELD_DEF(int64_t, blockNumB);
35+TILING_DATA_FIELD_DEF(int64_t, blockFactorB);
36+TILING_DATA_FIELD_DEF(int64_t, blockNumS);
37+TILING_DATA_FIELD_DEF(int64_t, blockFactorS);
38+TILING_DATA_FIELD_DEF(int64_t, ubFactorS);
39+TILING_DATA_FIELD_DEF(int64_t, ubFactorB);
40+TILING_DATA_FIELD_DEF(int64_t, ubLoopNumN);
41+TILING_DATA_FIELD_DEF(int64_t, ubFactorN);
42+TILING_DATA_FIELD_DEF(int64_t, ubTailFactorN);
43+TILING_DATA_FIELD_DEF(int64_t, usedCoreNum);
44+TILING_DATA_FIELD_DEF(int64_t, rotaryMode);
45+TILING_DATA_FIELD_DEF(int64_t, sliceStart);
46+TILING_DATA_FIELD_DEF(int64_t, sliceEnd);
47+TILING_DATA_FIELD_DEF(int64_t, sliceLength);
48+TILING_DATA_FIELD_DEF(int64_t, dSplitCoef);
49+END_TILING_DATA_DEF;
50+REGISTER_TILING_DATA_CLASS(InplacePartialRotaryMulGrad, InplacePartialRotaryMulGradRegbaseTilingData)
51+ 
52+BEGIN_TILING_DATA_DEF(InplacePartialRotaryMulGradRegbaseTilingDataAb)
53+TILING_DATA_FIELD_DEF(int64_t, b);
54+TILING_DATA_FIELD_DEF(int64_t, s);
55+TILING_DATA_FIELD_DEF(int64_t, d);
56+TILING_DATA_FIELD_DEF(int64_t, n);
57+TILING_DATA_FIELD_DEF(int64_t, dAlign);
58+TILING_DATA_FIELD_DEF(int64_t, dSplitCoef);
59+TILING_DATA_FIELD_DEF(int64_t, blockNumBS);
60+TILING_DATA_FIELD_DEF(int64_t, blockFactorBS);
61+TILING_DATA_FIELD_DEF(int64_t, blockTailBS);
62+TILING_DATA_FIELD_DEF(int64_t, blockNumN);
63+TILING_DATA_FIELD_DEF(int64_t, blockFactorN);
64+TILING_DATA_FIELD_DEF(int64_t, blockTailN);
65+TILING_DATA_FIELD_DEF(int64_t, ubFactorBS);
66+TILING_DATA_FIELD_DEF(int64_t, ubFactorN);
67+TILING_DATA_FIELD_DEF(int64_t, usedCoreNum);
68+TILING_DATA_FIELD_DEF(int64_t, rotaryMode);
69+TILING_DATA_FIELD_DEF(int64_t, sliceStart);
70+TILING_DATA_FIELD_DEF(int64_t, sliceEnd);
71+TILING_DATA_FIELD_DEF(int64_t, sliceLength);
72+END_TILING_DATA_DEF;
73+REGISTER_TILING_DATA_CLASS(InplacePartialRotaryMulGrad_204, InplacePartialRotaryMulGradRegbaseTilingDataAb)
74+ 
75+constexpr uint64_t TILING_KEY_EMPTY = 403;
76+constexpr static int64_t WORKSPACE_COUNT = 1;
77+constexpr static size_t RESERVED_WORKSPACE = static_cast<size_t>(16 * 1024 * 1024);
78+struct InplacePartialRotaryMulGradCompileInfo {};
79+ 
80+enum class InplacePartialRotaryMulGradLayout : int64_t {
81+ NO_BROADCAST = 1,
82+ BROADCAST_BSN = 2,
83+ BSND = 3,
84+ SBND = 4,
85+ BNSD = 5
86+};
87+ 
88+enum class InplacePartialRotaryMulGradMode : int64_t {
89+ HALF = 0,
90+ INTERLEAVE = 1,
91+ QUARTER = 2,
92+ INTERLEAVE_HALF = 3
93+};
94+ 
95+class InplacePartialRotaryMulGradRegbaseTiling : public Ops::Transformer::OpTiling::TilingBaseClass {
96+public:
97+ explicit InplacePartialRotaryMulGradRegbaseTiling(gert::TilingContext *context) : TilingBaseClass(context)
98+ {
99+ }
100+ 
101+ void Reset(gert::TilingContext *context) override
102+ {
103+ TilingBaseClass::Reset(context);
104+ }
105+ 
106+protected:
107+ ge::graphStatus GetPlatformInfo() override;
108+ ge::graphStatus GetShapeAttrsInfo() override;
109+ ge::graphStatus GetWorkspaceSize() override
110+ {
111+ return ge::GRAPH_SUCCESS;
112+ }
113+ 
114+ ge::graphStatus DoLibApiTiling() override
115+ {
116+ return ge::GRAPH_SUCCESS;
117+ }
118+ 
119+ ge::graphStatus DoOpTiling() override
120+ {
121+ return ge::GRAPH_SUCCESS;
122+ }
123+ 
124+ uint64_t GetTilingKey() const override;
125+ 
126+ ge::graphStatus PostTiling() override
127+ {
128+ return ge::GRAPH_SUCCESS;
129+ }
130+ 
131+ bool IsCapable() override
132+ {
133+ return true;
134+ }
135+ const static int64_t MAX_COPY_BLOCK_COUNT = 4095;
136+ int64_t b_{0};
137+ int64_t s_{0};
138+ int64_t n_{0};
139+ int64_t d_{0};
140+ int64_t cosb_{0};
141+ int64_t usedCoreNum_{0};
142+ ge::DataType dtype_;
143+ ge::DataType cosDtype_;
144+ InplacePartialRotaryMulGradLayout layout_;
145+ InplacePartialRotaryMulGradMode rotaryMode_;
146+ 
147+ gert::Shape dyShape_;
148+ gert::Shape cosShape_;
149+ uint64_t tilingKey_{0};
150+ int64_t blockSize_;
151+ int64_t dSplitCoef_;
152+ int64_t sliceStart_{0};
153+ int64_t sliceEnd_{0};
154+ int64_t sliceLength_{0};
155+ bool is1snd_ = false;
156+ 
157+private:
158+ bool IsInplacePartialRotaryMulGradMode(const int32_t mode) const;
159+ ge::graphStatus CheckNullptr();
160+ ge::graphStatus CheckShape();
161+ ge::graphStatus CheckDtype();
162+ ge::graphStatus CheckDtypeDyGroup();
163+ ge::graphStatus CheckDtypeCosGroup();
164+ ge::graphStatus CheckAttr();
165+ ge::graphStatus CheckParam();
166+ ge::graphStatus CheckShapeLimit();
167+ ge::graphStatus CheckShapeDim() const;
168+ ge::graphStatus JudgeLayoutByShape(const gert::Shape &xShape, const gert::Shape &cosShape);
169+ ge::graphStatus CheckRotaryModeShapeRelation(const int64_t sliceLen);
170+ ge::graphStatus CheckInPutShapeAllPositive(const int64_t idx) const;
171+ ge::graphStatus CheckOutPutShapeAllPositive(const int64_t idx) const;
172+ 
173+ ge::graphStatus CheckShapeAllPositive() const;
174+};
175+ 
176+} // namespace optiling
177+#endif // OPS_BUILD_IN_OP_TILING_RUNTIME_INPLACE_PARTIAL_ROTARY_MUL_GRAD_H
@@ -0,0 +1,209 @@
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 inplace_partial_rotary_mul_grad_regbase_tiling_a_and_b.cpp
13+ * \brief A (NO_BROADCAST) and B (BROADCAST_BSN) template tiling for InplacePartialRotaryMulGrad
14+ */
15+#include "inplace_partial_rotary_mul_grad_regbase_tiling.h"
16+namespace optiling {
17+constexpr uint64_t TILING_KEY_A = 205;
18+constexpr uint64_t TILING_KEY_B = 206;
19+constexpr uint64_t INPLACE_PARTIAL_ROTARY_MUL_GRAD_A_AND_B_TILING_PRIORITY = 40000;
20+constexpr int64_t UB_FACTOR = 4;
21+constexpr int64_t UB_COS_SIN_FACTOR = 2;
22+ 
23+class InplacePartialRotaryMulGradRegbaseTilingAAndB : public InplacePartialRotaryMulGradRegbaseTiling {
24+public:
25+ explicit InplacePartialRotaryMulGradRegbaseTilingAAndB(gert::TilingContext *context)
26+ : InplacePartialRotaryMulGradRegbaseTiling(context)
27+ {
28+ }
29+ 
30+protected:
31+ ge::graphStatus DoOpTiling() override;
32+ ge::graphStatus PostTiling() override;
33+ 
34+ bool IsCapable() override
35+ {
36+ if (Ops::Transformer::OpTiling::IsRegbaseSocVersion(context_) &&
37+ (layout_ == InplacePartialRotaryMulGradLayout::NO_BROADCAST ||
38+ layout_ == InplacePartialRotaryMulGradLayout::BROADCAST_BSN)) {
39+ return true;
40+ }
41+ return false;
42+ }
43+ 
44+private:
45+ ge::graphStatus MergeDim();
46+ ge::graphStatus SplitCore();
47+ ge::graphStatus ComputeUbFactor();
48+ 
49+ int64_t blockNumB_ = 0;
50+ int64_t blockFactorB_ = 0;
51+ int64_t ubFactorB_ = 0;
52+ InplacePartialRotaryMulGradRegbaseTilingData tilingDataAAndB_;
53+};
54+ 
55+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAAndB::DoOpTiling()
56+{
57+ if (sliceLength_ == 0) {
58+ usedCoreNum_ = 1;
59+ return ge::GRAPH_SUCCESS;
60+ }
61+ OP_CHECK_IF(MergeDim() != ge::GRAPH_SUCCESS, OP_LOGE(context_->GetNodeName(), "MergeDim failed."),
62+ return ge::GRAPH_FAILED);
63+ OP_CHECK_IF(SplitCore() != ge::GRAPH_SUCCESS, OP_LOGE(context_->GetNodeName(), "SplitCore failed."),
64+ return ge::GRAPH_FAILED);
65+ OP_CHECK_IF(ComputeUbFactor() != ge::GRAPH_SUCCESS, OP_LOGE(context_->GetNodeName(), "ComputeUbFactor failed."),
66+ return ge::GRAPH_FAILED);
67+ return ge::GRAPH_SUCCESS;
68+}
69+ 
70+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAAndB::MergeDim()
71+{
72+ // Flatten B×N×S into single B dimension
73+ b_ = b_ * n_ * s_;
74+ n_ = 1;
75+ s_ = 1;
76+ if (layout_ == InplacePartialRotaryMulGradLayout::NO_BROADCAST) {
77+ cosb_ = b_;
78+ } else {
79+ cosb_ = 1;
80+ }
81+ return ge::GRAPH_SUCCESS;
82+}
83+ 
84+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAAndB::SplitCore()
85+{
86+ int64_t coreNum = static_cast<int64_t>(aicoreParams_.numBlocks);
87+ blockFactorB_ = Ops::Base::CeilDiv(b_, coreNum);
88+ blockNumB_ = Ops::Base::CeilDiv(b_, blockFactorB_);
89+ usedCoreNum_ = blockNumB_;
90+ return ge::GRAPH_SUCCESS;
91+}
92+ 
93+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAAndB::ComputeUbFactor()
94+{
95+ int64_t typeSize = ge::GetSizeByDataType(dtype_);
96+ int64_t cosTypeSize = ge::GetSizeByDataType(cosDtype_);
97+ 
98+ int64_t dSizeDy = Ops::Base::CeilAlign(sliceLength_ * typeSize / dSplitCoef_, blockSize_) * dSplitCoef_;
99+ int64_t dSizeCos = Ops::Base::CeilAlign(sliceLength_ * cosTypeSize / dSplitCoef_, blockSize_) * dSplitCoef_;
100+ int64_t effectiveCosOverhead = std::max(int64_t(1), Ops::Base::CeilDiv(dSizeCos, dSizeDy));
101+ int64_t baseBlockInUb =
102+ Ops::Base::FloorAlign(static_cast<int64_t>(aicoreParams_.ubSize / UB_FACTOR), blockSize_) / dSizeDy;
103+ if (baseBlockInUb < effectiveCosOverhead + 1) {
104+ OP_LOGE(context_->GetNodeName(), "ubSize can't load slice, sliceLength=%ld, dSizeDy=%ld, dSizeCos=%ld.",
105+ sliceLength_, dSizeDy, dSizeCos);
106+ return ge::GRAPH_FAILED;
107+ }
108+ 
109+ if (layout_ == InplacePartialRotaryMulGradLayout::NO_BROADCAST) {
110+ // A: cos varies with B → UB = 4*ubFactorB*dSizeDy + 4*ubFactorB*dSizeCos
111+ // = 4*ubFactorB*(dSizeDy + dSizeCos) = 4*ubFactorB*dSizeDy*(1 + overhead)
112+ // ubFactorB <= baseBlockInUb / (1 + effectiveCosOverhead)
113+ ubFactorB_ = std::min(blockFactorB_, Ops::Base::FloorDiv(baseBlockInUb, 1 + effectiveCosOverhead));
114+ } else {
115+ // B: cos broadcast → UB = 4*ubFactorB*dSizeDy + 2*dSizeCos
116+ // dy+dx: 2 × double-buffer = 4*ubFactorB*dSizeDy
117+ // cos+sin: 2 × single-buffer = 2*dSizeCos
118+ int64_t availableForDy = static_cast<int64_t>(aicoreParams_.ubSize) - UB_COS_SIN_FACTOR * dSizeCos;
119+ if (availableForDy <= 0) {
120+ OP_LOGE(context_->GetNodeName(), "ubSize can't fit cos/sin, dSizeCos=%ld.", dSizeCos);
121+ return ge::GRAPH_FAILED;
122+ }
123+ int64_t numOfDAvailable = Ops::Base::FloorDiv(availableForDy, UB_FACTOR * dSizeDy);
124+ OP_CHECK_IF(numOfDAvailable < 1,
125+ OP_LOGE(context_->GetNodeName(),
126+ "ubSize can't load slice, sliceLength=%ld, dSizeDy=%ld, dSizeCos=%ld.", sliceLength_,
127+ dSizeDy, dSizeCos),
128+ return ge::GRAPH_FAILED);
129+ ubFactorB_ = std::min(blockFactorB_, numOfDAvailable);
130+ }
131+ ubFactorB_ = std::min(ubFactorB_, MAX_COPY_BLOCK_COUNT / dSplitCoef_);
132+ if (ubFactorB_ <= 0) {
133+ ubFactorB_ = 1;
134+ }
135+ 
136+ // Verify UB capacity
137+ int64_t dyBytes = UB_FACTOR * ubFactorB_ * dSizeDy;
138+ int64_t cosBytes;
139+ if (layout_ == InplacePartialRotaryMulGradLayout::NO_BROADCAST) {
140+ cosBytes = UB_FACTOR * ubFactorB_ * dSizeCos;
141+ } else {
142+ cosBytes = UB_COS_SIN_FACTOR * dSizeCos;
143+ }
144+ if (dyBytes + cosBytes > static_cast<int64_t>(aicoreParams_.ubSize)) {
145+ OP_LOGE(context_->GetNodeName(), "UB overflow: need %ld > %ld.", dyBytes + cosBytes, aicoreParams_.ubSize);
146+ return ge::GRAPH_FAILED;
147+ }
148+ 
149+ return ge::GRAPH_SUCCESS;
150+}
151+ 
152+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAAndB::PostTiling()
153+{
154+ tilingDataAAndB_.set_b(b_);
155+ tilingDataAAndB_.set_s(s_);
156+ tilingDataAAndB_.set_d(d_);
157+ tilingDataAAndB_.set_n(n_);
158+ tilingDataAAndB_.set_blockNumB(blockNumB_);
159+ tilingDataAAndB_.set_blockFactorB(blockFactorB_);
160+ tilingDataAAndB_.set_blockNumS(0);
161+ tilingDataAAndB_.set_blockFactorS(0);
162+ tilingDataAAndB_.set_ubFactorS(0);
163+ tilingDataAAndB_.set_ubFactorB(ubFactorB_);
164+ tilingDataAAndB_.set_ubLoopNumN(0);
165+ tilingDataAAndB_.set_ubFactorN(0);
166+ tilingDataAAndB_.set_ubTailFactorN(0);
167+ tilingDataAAndB_.set_usedCoreNum(usedCoreNum_);
168+ tilingDataAAndB_.set_rotaryMode(static_cast<int64_t>(rotaryMode_));
169+ tilingDataAAndB_.set_sliceStart(sliceStart_);
170+ tilingDataAAndB_.set_sliceEnd(sliceEnd_);
171+ tilingDataAAndB_.set_sliceLength(sliceLength_);
172+ tilingDataAAndB_.set_dSplitCoef(dSplitCoef_);
173+ 
174+ size_t *userWorkspaceSize = context_->GetWorkspaceSizes(WORKSPACE_COUNT);
175+ OP_CHECK_NULL_WITH_CONTEXT(context_, userWorkspaceSize);
176+ userWorkspaceSize[0] = RESERVED_WORKSPACE;
177+ 
178+ auto rawTilingDataPtr = context_->GetRawTilingData();
179+ OP_CHECK_NULL_WITH_CONTEXT(context_, rawTilingDataPtr);
180+ if (tilingDataAAndB_.GetDataSize() > rawTilingDataPtr->GetCapacity()) {
181+ return ge::GRAPH_FAILED;
182+ }
183+ 
184+ tilingDataAAndB_.SaveToBuffer(rawTilingDataPtr->GetData(), rawTilingDataPtr->GetCapacity());
185+ rawTilingDataPtr->SetDataSize(tilingDataAAndB_.GetDataSize());
186+ 
187+ if (layout_ == InplacePartialRotaryMulGradLayout::NO_BROADCAST) {
188+ tilingKey_ = ((sliceLength_ == 0) ? TILING_KEY_EMPTY : static_cast<uint64_t>(TILING_KEY_A));
189+ } else {
190+ tilingKey_ = ((sliceLength_ == 0) ? TILING_KEY_EMPTY : static_cast<uint64_t>(TILING_KEY_B));
191+ }
192+ 
193+ context_->SetTilingKey(tilingKey_);
194+ context_->SetBlockDim(usedCoreNum_);
195+ 
196+ OP_LOGI(context_->GetNodeName(),
197+ "InplacePartialRotaryMulGradA&B tiling: B=%ld S=%ld D=%ld N=%ld cosb=%ld layout=%ld "
198+ "blockNumB=%ld blockFactorB=%ld blockNumS=0 blockFactorS=0 "
199+ "ubFactorS=0 ubFactorB=%ld ubLoopNumN=0 ubFactorN=0 ubTailFactorN=0 "
200+ "usedCoreNum=%ld rotaryMode=%ld dSplitCoef=%ld "
201+ "sliceStart=%ld sliceEnd=%ld sliceLength=%ld tilingKey=%lu",
202+ b_, s_, d_, n_, cosb_, static_cast<int64_t>(layout_), blockNumB_, blockFactorB_, ubFactorB_, usedCoreNum_,
203+ static_cast<int64_t>(rotaryMode_), dSplitCoef_, sliceStart_, sliceEnd_, sliceLength_, tilingKey_);
204+ return ge::GRAPH_SUCCESS;
205+}
206+ 
207+REGISTER_OPS_TILING_TEMPLATE(InplacePartialRotaryMulGrad, InplacePartialRotaryMulGradRegbaseTilingAAndB,
208+ INPLACE_PARTIAL_ROTARY_MUL_GRAD_A_AND_B_TILING_PRIORITY);
209+} // namespace optiling
@@ -0,0 +1,189 @@
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 inplace_partial_rotary_mul_grad_regbase_tiling_ab.cpp
13+ * \brief AB template tiling for InplacePartialRotaryMulGrad (SBND layout, BS+N blocking)
14+ */
15+#include "inplace_partial_rotary_mul_grad_regbase_tiling.h"
16+namespace optiling {
17+constexpr uint64_t TILING_KEY_AB = 204;
18+constexpr uint64_t INPLACE_PARTIAL_ROTARY_MUL_GRAD_AB_TILING_PRIORITY = 25000;
19+constexpr int64_t CONST_TWO = 2;
20+constexpr int64_t DB_FLAG = 2;
21+ 
22+class InplacePartialRotaryMulGradRegbaseTilingAb : public InplacePartialRotaryMulGradRegbaseTiling {
23+public:
24+ explicit InplacePartialRotaryMulGradRegbaseTilingAb(gert::TilingContext *context)
25+ : InplacePartialRotaryMulGradRegbaseTiling(context)
26+ {
27+ }
28+ 
29+protected:
30+ ge::graphStatus DoOpTiling() override;
31+ ge::graphStatus PostTiling() override;
32+ 
33+ bool IsCapable() override
34+ {
35+ if (Ops::Transformer::OpTiling::IsRegbaseSocVersion(context_) &&
36+ (layout_ == InplacePartialRotaryMulGradLayout::SBND)) {
37+ return true;
38+ }
39+ return false;
40+ }
41+ 
42+private:
43+ int64_t blockNumBS_ = 0;
44+ int64_t blockFactorBS_ = 0;
45+ int64_t blockTailBS_ = 0;
46+ int64_t blockNumN_ = 0;
47+ int64_t blockFactorN_ = 0;
48+ int64_t blockTailN_ = 0;
49+ int64_t ubFactorBS_ = 0;
50+ int64_t ubFactorN_ = 0;
51+ int64_t dAlign_ = 0;
52+ int64_t bn_ = 0;
53+ InplacePartialRotaryMulGradRegbaseTilingDataAb tilingDataAb_;
54+};
55+ 
56+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAb::DoOpTiling()
57+{
58+ if (sliceLength_ == 0) {
59+ usedCoreNum_ = 1;
60+ return ge::GRAPH_SUCCESS;
61+ }
62+ int64_t bs = b_ * s_;
63+ bn_ = n_;
64+ if (cosb_ == 1) {
65+ bs = s_;
66+ bn_ = b_ * n_;
67+ }
68+ int64_t typeSize = ge::GetSizeByDataType(dtype_);
69+ int64_t cosTypeSize = ge::GetSizeByDataType(cosDtype_);
70+ 
71+ // Compute per-type aligned row sizes (in elements), matching kernel's dAlignLenDy/dAlignLenCos
72+ int64_t dAlignDy = Ops::Base::CeilAlign(sliceLength_ / dSplitCoef_, blockSize_ / typeSize) * dSplitCoef_;
73+ int64_t dAlignCos = Ops::Base::CeilAlign(sliceLength_ / dSplitCoef_, blockSize_ / cosTypeSize) * dSplitCoef_;
74+ int64_t dSizeDy = dAlignDy * typeSize; // bytes per dy/dx row
75+ int64_t dSizeCos = dAlignCos * cosTypeSize; // bytes per cos/sin row
76+ dAlign_ = dAlignDy; // store dy alignment in tiling data for logging
77+ 
78+ int64_t coreNum = static_cast<int64_t>(aicoreParams_.numBlocks);
79+ blockFactorBS_ = Ops::Base::CeilDiv(bs, coreNum);
80+ blockNumBS_ = Ops::Base::CeilDiv(bs, blockFactorBS_);
81+ blockTailBS_ = bs - (blockNumBS_ - 1) * blockFactorBS_;
82+ 
83+ if (bs <= coreNum / CONST_TWO) {
84+ if (blockNumBS_ == 0) {
85+ OP_LOGE(context_->GetNodeName(), "blockNumBS_ == 0");
86+ return ge::GRAPH_FAILED;
87+ }
88+ blockNumN_ = coreNum / blockNumBS_;
89+ blockFactorN_ = Ops::Base::CeilDiv(bn_, blockNumN_);
90+ blockNumN_ = Ops::Base::CeilDiv(bn_, blockFactorN_);
91+ blockTailN_ = bn_ - (blockNumN_ - 1) * blockFactorN_;
92+ } else {
93+ blockNumN_ = 1;
94+ blockFactorN_ = bn_;
95+ blockTailN_ = bn_;
96+ }
97+ usedCoreNum_ = blockNumBS_ * blockNumN_;
98+ 
99+ // UB split: derivable rows from half UB with double-buffer headroom
100+ // UB total = 4 * ubFactorBS * (ubFactorN * dSizeDy + dSizeCos) <= ubSize
101+ // => ubFactorBS * (ubFactorN + CeilDiv(dSizeCos, dSizeDy)) <= ubSize/(4*dSizeDy)
102+ int64_t effectiveCosOverhead = std::max(int64_t(1), Ops::Base::CeilDiv(dSizeCos, dSizeDy));
103+ int64_t baseBlockInUb =
104+ Ops::Base::FloorAlign(static_cast<int64_t>(aicoreParams_.ubSize / CONST_TWO / DB_FLAG), blockSize_) / dSizeDy;
105+ if (baseBlockInUb < effectiveCosOverhead + 1) {
106+ OP_LOGE(context_->GetNodeName(),
107+ "ubSize can't load slice, sliceLength=%ld, dSizeDy=%ld, dSizeCos=%ld, baseBlockInUb=%ld.", sliceLength_,
108+ dSizeDy, dSizeCos, baseBlockInUb);
109+ return ge::GRAPH_FAILED;
110+ }
111+ 
112+ ubFactorN_ = std::min(blockFactorN_, baseBlockInUb - effectiveCosOverhead);
113+ ubFactorN_ = std::min(ubFactorN_, MAX_COPY_BLOCK_COUNT / dSplitCoef_);
114+ if (ubFactorN_ <= 0) {
115+ ubFactorN_ = 1;
116+ }
117+ 
118+ ubFactorBS_ = std::min(Ops::Base::FloorDiv(baseBlockInUb, ubFactorN_ + effectiveCosOverhead), blockFactorBS_);
119+ ubFactorBS_ = (ubFactorBS_ == 0) ? 1 : ubFactorBS_;
120+ 
121+ // Verify UB capacity (4 = CONST_TWO * DB_FLAG: 2cos+2sin or 2dy+2dx, double-buffered)
122+ int64_t dyBytes = CONST_TWO * DB_FLAG * ubFactorBS_ * ubFactorN_ * dSizeDy;
123+ int64_t cosBytes = CONST_TWO * DB_FLAG * ubFactorBS_ * dSizeCos;
124+ if (dyBytes + cosBytes > static_cast<int64_t>(aicoreParams_.ubSize)) {
125+ OP_LOGE(context_->GetNodeName(), "UB overflow: need %ld > %ld.", dyBytes + cosBytes, aicoreParams_.ubSize);
126+ return ge::GRAPH_FAILED;
127+ }
128+ 
129+ return ge::GRAPH_SUCCESS;
130+}
131+ 
132+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAb::PostTiling()
133+{
134+ tilingDataAb_.set_b(b_);
135+ tilingDataAb_.set_s(s_);
136+ tilingDataAb_.set_d(d_);
137+ tilingDataAb_.set_n(bn_);
138+ tilingDataAb_.set_dAlign(dAlign_);
139+ tilingDataAb_.set_dSplitCoef(dSplitCoef_);
140+ tilingDataAb_.set_blockNumBS(blockNumBS_);
141+ tilingDataAb_.set_blockFactorBS(blockFactorBS_);
142+ tilingDataAb_.set_blockTailBS(blockTailBS_);
143+ tilingDataAb_.set_blockNumN(blockNumN_);
144+ tilingDataAb_.set_blockFactorN(blockFactorN_);
145+ tilingDataAb_.set_blockTailN(blockTailN_);
146+ tilingDataAb_.set_ubFactorBS(ubFactorBS_);
147+ tilingDataAb_.set_ubFactorN(ubFactorN_);
148+ tilingDataAb_.set_usedCoreNum(usedCoreNum_);
149+ tilingDataAb_.set_rotaryMode(static_cast<int64_t>(rotaryMode_));
150+ tilingDataAb_.set_sliceStart(sliceStart_);
151+ tilingDataAb_.set_sliceEnd(sliceEnd_);
152+ tilingDataAb_.set_sliceLength(sliceLength_);
153+ 
154+ // set userWorkspace
155+ size_t *userWorkspaceSize = context_->GetWorkspaceSizes(WORKSPACE_COUNT);
156+ OP_CHECK_NULL_WITH_CONTEXT(context_, userWorkspaceSize);
157+ userWorkspaceSize[0] = RESERVED_WORKSPACE;
158+ 
159+ auto rawTilingDataPtr = context_->GetRawTilingData();
160+ OP_CHECK_NULL_WITH_CONTEXT(context_, rawTilingDataPtr);
161+ if (tilingDataAb_.GetDataSize() > rawTilingDataPtr->GetCapacity()) {
162+ return ge::GRAPH_FAILED;
163+ }
164+ 
165+ tilingDataAb_.SaveToBuffer(rawTilingDataPtr->GetData(), rawTilingDataPtr->GetCapacity());
166+ rawTilingDataPtr->SetDataSize(tilingDataAb_.GetDataSize());
167+ 
168+ tilingKey_ = ((sliceLength_ == 0) ? TILING_KEY_EMPTY : static_cast<uint64_t>(TILING_KEY_AB));
169+ context_->SetTilingKey(tilingKey_);
170+ context_->SetBlockDim(usedCoreNum_);
171+ 
172+ OP_LOGI(context_->GetNodeName(),
173+ "InplacePartialRotaryMulGradAB tiling: B=%ld S=%ld D=%ld N=%ld "
174+ "dAlign=%ld dSplitCoef=%ld "
175+ "blockNumBS=%ld blockFactorBS=%ld blockTailBS=%ld "
176+ "blockNumN=%ld blockFactorN=%ld blockTailN=%ld "
177+ "ubFactorBS=%ld ubFactorN=%ld "
178+ "usedCoreNum=%ld rotaryMode=%ld "
179+ "sliceStart=%ld sliceEnd=%ld sliceLength=%ld "
180+ "tilingKey=%lu",
181+ b_, s_, d_, bn_, dAlign_, dSplitCoef_, blockNumBS_, blockFactorBS_, blockTailBS_, blockNumN_, blockFactorN_,
182+ blockTailN_, ubFactorBS_, ubFactorN_, usedCoreNum_, static_cast<int64_t>(rotaryMode_), sliceStart_,
183+ sliceEnd_, sliceLength_, tilingKey_);
184+ return ge::GRAPH_SUCCESS;
185+}
186+ 
187+REGISTER_OPS_TILING_TEMPLATE(InplacePartialRotaryMulGrad, InplacePartialRotaryMulGradRegbaseTilingAb,
188+ INPLACE_PARTIAL_ROTARY_MUL_GRAD_AB_TILING_PRIORITY);
189+} // namespace optiling
@@ -0,0 +1,283 @@
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 inplace_partial_rotary_mul_grad_regbase_tiling_aba_and_ba.cpp
13+ * \brief ABA (cosb_!=1) and BA (cosb_==1) template tiling for InplacePartialRotaryMulGrad (BNSD layout)
14+ */
15+#include "inplace_partial_rotary_mul_grad_regbase_tiling.h"
16+namespace optiling {
17+constexpr uint64_t TILING_KEY_ABA = 201;
18+constexpr uint64_t TILING_KEY_BA = 202;
19+constexpr uint64_t INPLACE_PARTIAL_ROTARY_MUL_GRAD_ABA_AND_BA_TILING_PRIORITY = 10000;
20+constexpr int64_t UB_FACTOR = 4;
21+ 
22+class InplacePartialRotaryMulGradRegbaseTilingAbaAndBa : public InplacePartialRotaryMulGradRegbaseTiling {
23+public:
24+ explicit InplacePartialRotaryMulGradRegbaseTilingAbaAndBa(gert::TilingContext *context)
25+ : InplacePartialRotaryMulGradRegbaseTiling(context)
26+ {
27+ }
28+ 
29+protected:
30+ ge::graphStatus DoOpTiling() override;
31+ ge::graphStatus PostTiling() override;
32+ 
33+ bool IsCapable() override
34+ {
35+ if (Ops::Transformer::OpTiling::IsRegbaseSocVersion(context_) &&
36+ (layout_ == InplacePartialRotaryMulGradLayout::BNSD)) {
37+ return true;
38+ }
39+ return false;
40+ }
41+ 
42+private:
43+ ge::graphStatus SplitCore();
44+ ge::graphStatus ComputeUbFactor();
45+ 
46+ int64_t blockNumB_ = 0;
47+ int64_t blockFactorB_ = 0;
48+ int64_t blockNumS_ = 0;
49+ int64_t blockFactorS_ = 0;
50+ int64_t ubFactorS_ = 0;
51+ int64_t ubFactorB_ = 0;
52+ int64_t ubLoopNumN_ = 0;
53+ int64_t ubFactorN_ = 0;
54+ int64_t ubTailFactorN_ = 0;
55+ InplacePartialRotaryMulGradRegbaseTilingData tilingDataAbaAndBa_;
56+};
57+ 
58+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAbaAndBa::DoOpTiling()
59+{
60+ if (sliceLength_ == 0) {
61+ usedCoreNum_ = 1;
62+ return ge::GRAPH_SUCCESS;
63+ }
64+ OP_CHECK_IF(SplitCore() != ge::GRAPH_SUCCESS, OP_LOGE(context_->GetNodeName(), "SplitCore failed."),
65+ return ge::GRAPH_FAILED);
66+ OP_CHECK_IF(ComputeUbFactor() != ge::GRAPH_SUCCESS, OP_LOGE(context_->GetNodeName(), "ComputeUbFactor failed."),
67+ return ge::GRAPH_FAILED);
68+ return ge::GRAPH_SUCCESS;
69+}
70+ 
71+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAbaAndBa::SplitCore()
72+{
73+ int64_t coreNum = static_cast<int64_t>(aicoreParams_.numBlocks);
74+ 
75+ // B divisible by core count → split on B only
76+ if (b_ % coreNum == 0) {
77+ blockNumB_ = coreNum;
78+ blockFactorB_ = b_ / coreNum;
79+ blockNumS_ = 1;
80+ blockFactorS_ = s_;
81+ usedCoreNum_ = blockNumB_ * blockNumS_;
82+ return ge::GRAPH_SUCCESS;
83+ }
84+ 
85+ // S divisible by core count → split on S only
86+ if (s_ % coreNum == 0) {
87+ blockNumS_ = coreNum;
88+ blockFactorS_ = s_ / coreNum;
89+ blockNumB_ = 1;
90+ blockFactorB_ = b_;
91+ usedCoreNum_ = blockNumB_ * blockNumS_;
92+ return ge::GRAPH_SUCCESS;
93+ }
94+ 
95+ // Try B-priority split
96+ auto blockFactorB1 = Ops::Base::CeilDiv(b_, coreNum);
97+ auto blockNumB1 = Ops::Base::CeilDiv(b_, blockFactorB1);
98+ auto blockNumS1 = std::min(coreNum / blockNumB1, s_);
99+ auto blockFactorS1 = Ops::Base::CeilDiv(s_, blockNumS1);
100+ blockNumS1 = Ops::Base::CeilDiv(s_, blockFactorS1);
101+ auto usedCoreNum1 = blockNumB1 * blockNumS1;
102+ 
103+ // Try S-priority split
104+ auto blockFactorS2 = Ops::Base::CeilDiv(s_, coreNum);
105+ auto blockNumS2 = Ops::Base::CeilDiv(s_, blockFactorS2);
106+ auto blockNumB2 = std::min(coreNum / blockNumS2, b_);
107+ auto blockFactorB2 = Ops::Base::CeilDiv(b_, blockNumB2);
108+ blockNumB2 = Ops::Base::CeilDiv(b_, blockFactorB2);
109+ auto usedCoreNum2 = blockNumB2 * blockNumS2;
110+ 
111+ if (usedCoreNum1 >= usedCoreNum2) {
112+ blockNumB_ = blockNumB1;
113+ blockFactorB_ = blockFactorB1;
114+ blockNumS_ = blockNumS1;
115+ blockFactorS_ = blockFactorS1;
116+ } else {
117+ blockNumB_ = blockNumB2;
118+ blockFactorB_ = blockFactorB2;
119+ blockNumS_ = blockNumS2;
120+ blockFactorS_ = blockFactorS2;
121+ }
122+ usedCoreNum_ = blockNumB_ * blockNumS_;
123+ return ge::GRAPH_SUCCESS;
124+}
125+ 
126+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAbaAndBa::ComputeUbFactor()
127+{
128+ int64_t typeSize = ge::GetSizeByDataType(dtype_);
129+ int64_t cosTypeSize = ge::GetSizeByDataType(cosDtype_);
130+ 
131+ // Per-type aligned row sizes in bytes
132+ int64_t dSizeDy = Ops::Base::CeilAlign(sliceLength_ * typeSize / dSplitCoef_, blockSize_) * dSplitCoef_;
133+ int64_t dSizeCos = Ops::Base::CeilAlign(sliceLength_ * cosTypeSize / dSplitCoef_, blockSize_) * dSplitCoef_;
134+ 
135+ // Kernel UB layout:
136+ // dy/dx: UB_FACTOR * ubFactorB * ubFactorS * ubFactorN * dSizeDy
137+ // cos/sin (Broadcast): UB_FACTOR * ubFactorS * dSizeCos
138+ // cos/sin (!Broadcast): UB_FACTOR * ubFactorB * ubFactorS * dSizeCos
139+ //
140+ // Use effectiveCosOverhead like BAB/AB to handle mixed precision uniformly:
141+ // Overhead = max(1, CeilDiv(dSizeCos, dSizeDy)) "dy-row equivalents" per cos row
142+ int64_t effectiveCosOverhead = std::max(int64_t(1), Ops::Base::CeilDiv(dSizeCos, dSizeDy));
143+ 
144+ // baseBlockInUb: effective dy-rows in UB/4
145+ int64_t baseBlockInUb =
146+ Ops::Base::FloorAlign(static_cast<int64_t>(aicoreParams_.ubSize / UB_FACTOR), blockSize_) / dSizeDy;
147+ if (baseBlockInUb < effectiveCosOverhead + 1) {
148+ OP_LOGE(context_->GetNodeName(), "ubSize can't load slice, sliceLength=%ld, dSizeDy=%ld, dSizeCos=%ld.",
149+ sliceLength_, dSizeDy, dSizeCos);
150+ return ge::GRAPH_FAILED;
151+ }
152+ 
153+ if (cosb_ == 1) {
154+ // BA: cos broadcasts on B → UB = 4*ubFactorB*ubFactorS*ubFactorN*dSizeDy + 4*ubFactorS*dSizeCos
155+ // = 4*ubFactorS*dSizeDy * (ubFactorB*ubFactorN + effectiveCosOverhead)
156+ // Constraint: ubFactorS * (ubFactorB * ubFactorN + effectiveCosOverhead) <= baseBlockInUb
157+ 
158+ // Step 1: ubFactorN = min(n_, baseBlockInUb - effectiveCosOverhead)
159+ // (reserve overhead for cos row, assume ubFactorB>=1, ubFactorS>=1)
160+ ubFactorN_ = std::min(n_, baseBlockInUb - effectiveCosOverhead);
161+ ubFactorN_ = std::min(ubFactorN_, MAX_COPY_BLOCK_COUNT / dSplitCoef_);
162+ if (ubFactorN_ <= 0) {
163+ ubFactorN_ = 1;
164+ }
165+ 
166+ // Step 2: ubFactorB * ubFactorS <= baseBlockInUb / (ubFactorN + effectiveCosOverhead/ubFactorB)
167+ // Approximate: remaining = baseBlockInUb / (ubFactorN + effectiveCosOverhead/ubFactorB)
168+ // For conservative estimate, use effectiveCosOverhead directly:
169+ int64_t afterN = Ops::Base::FloorDiv(baseBlockInUb, ubFactorN_ + effectiveCosOverhead);
170+ ubFactorB_ = std::min(blockFactorB_, afterN);
171+ if (ubFactorB_ <= 0) {
172+ ubFactorB_ = 1;
173+ }
174+ 
175+ // Step 3: ubFactorS from remaining budget
176+ ubFactorS_ =
177+ std::min(blockFactorS_, Ops::Base::FloorDiv(baseBlockInUb, ubFactorB_ * ubFactorN_ + effectiveCosOverhead));
178+ } else {
179+ // ABA: cos varies with B → UB = 4*ubFactorB*ubFactorS*(ubFactorN*dSizeDy + dSizeCos)
180+ // = 4*ubFactorB*ubFactorS*dSizeDy * (ubFactorN + CeilDiv(dSizeCos, dSizeDy))
181+ // => ubFactorB * ubFactorS * (ubFactorN + effectiveCosOverhead) <= baseBlockInUb
182+ 
183+ ubFactorN_ = std::min(n_, baseBlockInUb - effectiveCosOverhead);
184+ ubFactorN_ = std::min(ubFactorN_, MAX_COPY_BLOCK_COUNT / dSplitCoef_);
185+ if (ubFactorN_ <= 0) {
186+ ubFactorN_ = 1;
187+ }
188+ 
189+ int64_t afterN = Ops::Base::FloorDiv(baseBlockInUb, ubFactorN_ + effectiveCosOverhead);
190+ ubFactorB_ = std::min(blockFactorB_, afterN);
191+ if (ubFactorB_ <= 0) {
192+ ubFactorB_ = 1;
193+ }
194+ 
195+ ubFactorS_ = std::min(blockFactorS_,
196+ Ops::Base::FloorDiv(baseBlockInUb, ubFactorB_ * (ubFactorN_ + effectiveCosOverhead)));
197+ }
198+ 
199+ if (ubFactorS_ == 0) {
200+ ubFactorS_ = 1;
201+ }
202+ if (ubFactorB_ == 0) {
203+ ubFactorB_ = 1;
204+ }
205+ if (ubFactorN_ == 0) {
206+ ubFactorN_ = 1;
207+ }
208+ 
209+ // Final UB verification
210+ int64_t dyBytes = UB_FACTOR * ubFactorB_ * ubFactorS_ * ubFactorN_ * dSizeDy;
211+ int64_t cosBytes;
212+ if (cosb_ == 1) {
213+ cosBytes = UB_FACTOR * ubFactorS_ * dSizeCos;
214+ } else {
215+ cosBytes = UB_FACTOR * ubFactorB_ * ubFactorS_ * dSizeCos;
216+ }
217+ if (dyBytes + cosBytes > static_cast<int64_t>(aicoreParams_.ubSize)) {
218+ OP_LOGE(context_->GetNodeName(), "UB overflow: need %ld > %ld.", dyBytes + cosBytes, aicoreParams_.ubSize);
219+ return ge::GRAPH_FAILED;
220+ }
221+ 
222+ return ge::GRAPH_SUCCESS;
223+}
224+ 
225+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingAbaAndBa::PostTiling()
226+{
227+ tilingDataAbaAndBa_.set_b(b_);
228+ tilingDataAbaAndBa_.set_s(s_);
229+ tilingDataAbaAndBa_.set_d(d_);
230+ tilingDataAbaAndBa_.set_n(n_);
231+ tilingDataAbaAndBa_.set_blockNumB(blockNumB_);
232+ tilingDataAbaAndBa_.set_blockFactorB(blockFactorB_);
233+ tilingDataAbaAndBa_.set_blockNumS(blockNumS_);
234+ tilingDataAbaAndBa_.set_blockFactorS(blockFactorS_);
235+ tilingDataAbaAndBa_.set_ubFactorS(ubFactorS_);
236+ tilingDataAbaAndBa_.set_ubFactorB(ubFactorB_);
237+ tilingDataAbaAndBa_.set_ubLoopNumN(ubLoopNumN_);
238+ tilingDataAbaAndBa_.set_ubFactorN(ubFactorN_);
239+ tilingDataAbaAndBa_.set_ubTailFactorN(ubTailFactorN_);
240+ tilingDataAbaAndBa_.set_usedCoreNum(usedCoreNum_);
241+ tilingDataAbaAndBa_.set_rotaryMode(static_cast<int64_t>(rotaryMode_));
242+ tilingDataAbaAndBa_.set_sliceStart(sliceStart_);
243+ tilingDataAbaAndBa_.set_sliceEnd(sliceEnd_);
244+ tilingDataAbaAndBa_.set_sliceLength(sliceLength_);
245+ tilingDataAbaAndBa_.set_dSplitCoef(dSplitCoef_);
246+ 
247+ size_t *userWorkspaceSize = context_->GetWorkspaceSizes(WORKSPACE_COUNT);
248+ OP_CHECK_NULL_WITH_CONTEXT(context_, userWorkspaceSize);
249+ userWorkspaceSize[0] = RESERVED_WORKSPACE;
250+ 
251+ auto rawTilingDataPtr = context_->GetRawTilingData();
252+ OP_CHECK_NULL_WITH_CONTEXT(context_, rawTilingDataPtr);
253+ if (tilingDataAbaAndBa_.GetDataSize() > rawTilingDataPtr->GetCapacity()) {
254+ return ge::GRAPH_FAILED;
255+ }
256+ 
257+ tilingDataAbaAndBa_.SaveToBuffer(rawTilingDataPtr->GetData(), rawTilingDataPtr->GetCapacity());
258+ rawTilingDataPtr->SetDataSize(tilingDataAbaAndBa_.GetDataSize());
259+ 
260+ if (cosb_ == 1) {
261+ tilingKey_ = ((sliceLength_ == 0) ? TILING_KEY_EMPTY : static_cast<uint64_t>(TILING_KEY_BA));
262+ } else {
263+ tilingKey_ = ((sliceLength_ == 0) ? TILING_KEY_EMPTY : static_cast<uint64_t>(TILING_KEY_ABA));
264+ }
265+ 
266+ context_->SetTilingKey(tilingKey_);
267+ context_->SetBlockDim(usedCoreNum_);
268+ 
269+ OP_LOGI(context_->GetNodeName(),
270+ "InplacePartialRotaryMulGradABA&BA tiling: B=%ld S=%ld D=%ld N=%ld cosb=%ld "
271+ "blockNumB=%ld blockFactorB=%ld blockNumS=%ld blockFactorS=%ld "
272+ "ubFactorS=%ld ubFactorB=%ld ubLoopNumN=%ld ubFactorN=%ld ubTailFactorN=%ld "
273+ "usedCoreNum=%ld rotaryMode=%ld dSplitCoef=%ld "
274+ "sliceStart=%ld sliceEnd=%ld sliceLength=%ld tilingKey=%lu",
275+ b_, s_, d_, n_, cosb_, blockNumB_, blockFactorB_, blockNumS_, blockFactorS_, ubFactorS_, ubFactorB_,
276+ ubLoopNumN_, ubFactorN_, ubTailFactorN_, usedCoreNum_, static_cast<int64_t>(rotaryMode_), dSplitCoef_,
277+ sliceStart_, sliceEnd_, sliceLength_, tilingKey_);
278+ return ge::GRAPH_SUCCESS;
279+}
280+ 
281+REGISTER_OPS_TILING_TEMPLATE(InplacePartialRotaryMulGrad, InplacePartialRotaryMulGradRegbaseTilingAbaAndBa,
282+ INPLACE_PARTIAL_ROTARY_MUL_GRAD_ABA_AND_BA_TILING_PRIORITY);
283+} // namespace optiling
@@ -0,0 +1,216 @@
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 inplace_partial_rotary_mul_grad_regbase_tiling_bab.cpp
13+ * \brief BAB template tiling for InplacePartialRotaryMulGrad (BSND layout, cosb_==1)
14+ */
15+#include "inplace_partial_rotary_mul_grad_regbase_tiling.h"
16+namespace optiling {
17+constexpr uint64_t TILING_KEY_BAB = 203;
18+constexpr uint64_t INPLACE_PARTIAL_ROTARY_MUL_GRAD_BAB_TILING_PRIORITY = 20000;
19+constexpr uint32_t MIN_UB_LOAD_D_NUM = 4;
20+ 
21+class InplacePartialRotaryMulGradRegbaseTilingBab : public InplacePartialRotaryMulGradRegbaseTiling {
22+public:
23+ explicit InplacePartialRotaryMulGradRegbaseTilingBab(gert::TilingContext *context)
24+ : InplacePartialRotaryMulGradRegbaseTiling(context)
25+ {
26+ }
27+ 
28+protected:
29+ ge::graphStatus DoOpTiling() override;
30+ ge::graphStatus PostTiling() override;
31+ 
32+ bool IsCapable() override
33+ {
34+ if (Ops::Transformer::OpTiling::IsRegbaseSocVersion(context_) &&
35+ (layout_ == InplacePartialRotaryMulGradLayout::BSND) && (cosb_ == 1)) {
36+ return true;
37+ }
38+ return false;
39+ }
40+ 
41+private:
42+ int64_t coreNum_ = 0;
43+ int64_t blockNumB_ = 0;
44+ int64_t blockFactorB_ = 0;
45+ int64_t blockNumS_ = 0;
46+ int64_t blockFactorS_ = 0;
47+ int64_t ubFactorS_ = 1;
48+ int64_t ubFactorB_ = 1;
49+ int64_t ubLoopNumN_ = 0;
50+ int64_t ubFactorN_ = 1;
51+ int64_t ubTailFactorN_ = 0;
52+ int64_t ubSize_ = 0;
53+ InplacePartialRotaryMulGradRegbaseTilingData tilingDataBab_;
54+ 
55+ void SplitCore();
56+ ge::graphStatus SplitUb();
57+};
58+ 
59+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingBab::DoOpTiling()
60+{
61+ if (sliceLength_ == 0) {
62+ usedCoreNum_ = 1;
63+ return ge::GRAPH_SUCCESS;
64+ }
65+ ubSize_ = aicoreParams_.ubSize;
66+ coreNum_ = static_cast<int64_t>(aicoreParams_.numBlocks);
67+ 
68+ ge::graphStatus status = SplitUb();
69+ if (status != ge::GRAPH_SUCCESS) {
70+ OP_LOGE(context_->GetNodeName(), "SplitUb Failed.");
71+ return ge::GRAPH_FAILED;
72+ }
73+ SplitCore();
74+ if (blockNumB_ * blockNumS_ > coreNum_) {
75+ OP_LOGE(context_->GetNodeName(), "split coreNum [%ld] greater than coreNum[%ld]", blockNumB_ * blockNumS_,
76+ coreNum_);
77+ return ge::GRAPH_FAILED;
78+ }
79+ return ge::GRAPH_SUCCESS;
80+}
81+ 
82+void InplacePartialRotaryMulGradRegbaseTilingBab::SplitCore()
83+{
84+ auto blockFactorB1 = Ops::Base::CeilDiv(b_, coreNum_);
85+ auto blockNumB1 = Ops::Base::CeilDiv(b_, blockFactorB1);
86+ auto blockNumS1 = std::min(coreNum_ / blockNumB1, s_);
87+ auto blockFactorS1 = Ops::Base::CeilDiv(s_, blockNumS1);
88+ blockNumS1 = Ops::Base::CeilDiv(s_, blockFactorS1);
89+ auto usedCoreNum1 = blockNumB1 * blockNumS1;
90+ 
91+ auto blockFactorS2 = Ops::Base::CeilDiv(s_, coreNum_);
92+ auto blockNumS2 = Ops::Base::CeilDiv(s_, blockFactorS2);
93+ auto blockNumB2 = std::min(coreNum_ / blockNumS2, b_);
94+ auto blockFactorB2 = Ops::Base::CeilDiv(b_, blockNumB2);
95+ blockNumB2 = Ops::Base::CeilDiv(b_, blockFactorB2);
96+ auto usedCoreNum2 = blockNumB2 * blockNumS2;
97+ 
98+ auto ubFactorS1 = std::min(ubFactorS_, blockFactorS1);
99+ auto ubFactorS2 = std::min(ubFactorS_, blockFactorS2);
100+ if (usedCoreNum1 * ubFactorS1 >= usedCoreNum2 * ubFactorS2) {
101+ blockNumB_ = blockNumB1;
102+ blockFactorB_ = blockFactorB1;
103+ blockNumS_ = blockNumS1;
104+ blockFactorS_ = blockFactorS1;
105+ usedCoreNum_ = usedCoreNum1;
106+ ubFactorS_ = ubFactorS1;
107+ } else {
108+ blockNumB_ = blockNumB2;
109+ blockFactorB_ = blockFactorB2;
110+ blockNumS_ = blockNumS2;
111+ blockFactorS_ = blockFactorS2;
112+ usedCoreNum_ = usedCoreNum2;
113+ ubFactorS_ = ubFactorS2;
114+ }
115+}
116+ 
117+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingBab::SplitUb()
118+{
119+ int64_t typeSize = ge::GetSizeByDataType(dtype_);
120+ int64_t cosTypeSize = ge::GetSizeByDataType(cosDtype_);
121+ 
122+ // Per-type aligned row sizes (in elements), matching kernel's dAlignLenDy/dAlignLenCos
123+ int64_t dAlignDy = Ops::Base::CeilAlign(sliceLength_ / dSplitCoef_, blockSize_ / typeSize) * dSplitCoef_;
124+ int64_t dAlignCos = Ops::Base::CeilAlign(sliceLength_ / dSplitCoef_, blockSize_ / cosTypeSize) * dSplitCoef_;
125+ int64_t dSizeDy = dAlignDy * typeSize; // bytes per dy/dx row
126+ int64_t dSizeCos = dAlignCos * cosTypeSize; // bytes per cos/sin row
127+ 
128+ // Unified overhead: how many "dy-row equivalents" one cos/sin row occupies
129+ // UB = 4 * ubFactorS * (ubFactorN * dSizeDy + dSizeCos) <= ubSize
130+ // => ubFactorS * (ubFactorN + CeilDiv(dSizeCos, dSizeDy)) <= ubSize / (4 * dSizeDy)
131+ int64_t effectiveCosOverhead = std::max(int64_t(1), Ops::Base::CeilDiv(dSizeCos, dSizeDy));
132+ int64_t baseBlockInUb =
133+ Ops::Base::FloorAlign(static_cast<int64_t>(ubSize_ / MIN_UB_LOAD_D_NUM), blockSize_) / dSizeDy;
134+ if (baseBlockInUb < effectiveCosOverhead + 1) {
135+ OP_LOGE(context_->GetNodeName(),
136+ "ubSize can't load slice, sliceLength=%ld, dSizeDy=%ld, dSizeCos=%ld, baseBlockInUb=%ld.", sliceLength_,
137+ dSizeDy, dSizeCos, baseBlockInUb);
138+ return ge::GRAPH_FAILED;
139+ }
140+ 
141+ // N split
142+ int64_t ubLoopNum = Ops::Base::CeilDiv(n_, (baseBlockInUb - effectiveCosOverhead));
143+ ubFactorN_ = std::min(Ops::Base::CeilDiv(n_, ubLoopNum), MAX_COPY_BLOCK_COUNT / dSplitCoef_);
144+ ubLoopNumN_ = Ops::Base::CeilDiv(n_, ubFactorN_);
145+ ubTailFactorN_ = (n_ % ubFactorN_ == 0) ? ubFactorN_ : n_ % ubFactorN_;
146+ 
147+ // S split
148+ int64_t ubFactorS = Ops::Base::FloorDiv(baseBlockInUb, ubFactorN_ + effectiveCosOverhead);
149+ ubFactorS_ = (ubFactorS == 0) ? 1 : ubFactorS;
150+ 
151+ // Verify UB capacity
152+ int64_t dyBytes = MIN_UB_LOAD_D_NUM * ubFactorS_ * ubFactorN_ * dSizeDy;
153+ int64_t cosBytes = MIN_UB_LOAD_D_NUM * ubFactorS_ * dSizeCos;
154+ OP_CHECK_IF(dyBytes + cosBytes > static_cast<int64_t>(ubSize_),
155+ OP_LOGE(context_->GetNodeName(), "UB overflow: need %ld > %ld.", dyBytes + cosBytes, ubSize_),
156+ return ge::GRAPH_FAILED);
157+ 
158+ return ge::GRAPH_SUCCESS;
159+}
160+ 
161+ge::graphStatus InplacePartialRotaryMulGradRegbaseTilingBab::PostTiling()
162+{
163+ tilingDataBab_.set_b(b_);
164+ tilingDataBab_.set_s(s_);
165+ tilingDataBab_.set_d(d_);
166+ tilingDataBab_.set_n(n_);
167+ tilingDataBab_.set_ubFactorS(ubFactorS_);
168+ tilingDataBab_.set_ubFactorB(ubFactorB_);
169+ tilingDataBab_.set_ubLoopNumN(ubLoopNumN_);
170+ tilingDataBab_.set_ubFactorN(ubFactorN_);
171+ tilingDataBab_.set_ubTailFactorN(ubTailFactorN_);
172+ tilingDataBab_.set_blockNumB(blockNumB_);
173+ tilingDataBab_.set_blockFactorB(blockFactorB_);
174+ tilingDataBab_.set_blockNumS(blockNumS_);
175+ tilingDataBab_.set_blockFactorS(blockFactorS_);
176+ tilingDataBab_.set_usedCoreNum(usedCoreNum_);
177+ tilingDataBab_.set_rotaryMode(static_cast<int64_t>(rotaryMode_));
178+ tilingDataBab_.set_sliceStart(sliceStart_);
179+ tilingDataBab_.set_sliceEnd(sliceEnd_);
180+ tilingDataBab_.set_sliceLength(sliceLength_);
181+ tilingDataBab_.set_dSplitCoef(dSplitCoef_);
182+ 
183+ // 设置 userWorkspace
184+ size_t *userWorkspaceSize = context_->GetWorkspaceSizes(WORKSPACE_COUNT);
185+ OP_CHECK_NULL_WITH_CONTEXT(context_, userWorkspaceSize);
186+ userWorkspaceSize[0] = RESERVED_WORKSPACE;
187+ 
188+ auto rawTilingDataPtr = context_->GetRawTilingData();
189+ OP_CHECK_NULL_WITH_CONTEXT(context_, rawTilingDataPtr);
190+ if (tilingDataBab_.GetDataSize() > rawTilingDataPtr->GetCapacity()) {
191+ return ge::GRAPH_FAILED;
192+ }
193+ 
194+ tilingDataBab_.SaveToBuffer(rawTilingDataPtr->GetData(), rawTilingDataPtr->GetCapacity());
195+ rawTilingDataPtr->SetDataSize(tilingDataBab_.GetDataSize());
196+ 
197+ tilingKey_ = ((sliceLength_ == 0) ? TILING_KEY_EMPTY : static_cast<uint64_t>(TILING_KEY_BAB));
198+ context_->SetTilingKey(tilingKey_);
199+ context_->SetBlockDim(usedCoreNum_);
200+ 
201+ OP_LOGI(context_->GetNodeName(),
202+ "InplacePartialRotaryMulGradBAB tiling: B=%ld S=%ld D=%ld N=%ld "
203+ "blockNumB=%ld blockFactorB=%ld blockNumS=%ld blockFactorS=%ld "
204+ "ubFactorS=%ld ubFactorB=%ld ubLoopNumN=%ld ubFactorN=%ld ubTailFactorN=%ld "
205+ "usedCoreNum=%ld rotaryMode=%ld dSplitCoef=%ld "
206+ "sliceStart=%ld sliceEnd=%ld sliceLength=%ld "
207+ "tilingKey=%lu",
208+ b_, s_, d_, n_, blockNumB_, blockFactorB_, blockNumS_, blockFactorS_, ubFactorS_, ubFactorB_, ubLoopNumN_,
209+ ubFactorN_, ubTailFactorN_, usedCoreNum_, static_cast<int64_t>(rotaryMode_), dSplitCoef_, sliceStart_,
210+ sliceEnd_, sliceLength_, tilingKey_);
211+ return ge::GRAPH_SUCCESS;
212+}
213+ 
214+REGISTER_OPS_TILING_TEMPLATE(InplacePartialRotaryMulGrad, InplacePartialRotaryMulGradRegbaseTilingBab,
215+ INPLACE_PARTIAL_ROTARY_MUL_GRAD_BAB_TILING_PRIORITY);
216+} // namespace optiling
@@ -0,0 +1,34 @@
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 inplace_partial_rotary_mul_grad_tiling.cpp
13+ * \brief
14+ */
15+#include "inplace_partial_rotary_mul_grad_regbase_tiling.h"
16+#include "register/op_def_registry.h"
17+#include "tiling/tiling_api.h"
18+#include "op_host/tiling_templates_registry.h"
19+ 
20+namespace optiling {
21+ge::graphStatus Tiling4InplacePartialRotaryMulGrad(gert::TilingContext *context)
22+{
23+ return Ops::Transformer::OpTiling::TilingRegistry::GetInstance().DoTilingImpl(context);
24+}
25+ 
26+ge::graphStatus TilingPrepareForInplacePartialRotaryMulGrad(gert::TilingParseContext *context)
27+{
28+ return ge::GRAPH_SUCCESS;
29+}
30+ 
31+IMPL_OP_OPTILING(InplacePartialRotaryMulGrad)
32+ .Tiling(Tiling4InplacePartialRotaryMulGrad)
33+ .TilingParse<InplacePartialRotaryMulGradCompileInfo>(TilingPrepareForInplacePartialRotaryMulGrad);
34+} // namespace optiling