Closed
fix: harden。Relu6Grad and SGD validation and test references #13276
raoliang_sacAdd on 21 days agoClosed at 18 days ago
fix: harden。Relu6Grad and SGD validation and test references #13276
Closed
raoliang_sacAdd on 21 days agoClosed at 18 days ago
Total: 21 files changed+432-120
@@ -2,14 +2,14 @@
2 2 
3## 产品支持情况3## 产品支持情况
4 4 
5-|产品 | 是否支持 |5+| 产品 | 是否支持 |
6-|:-------------------------|:----------:|6+| :--- | :---: |
7-| <term>Ascend 950PR&950DT系列产品</term> | √ |7+| <term>Ascend 950PR&950DT系列产品</term> | √ |
8-| <term>Atlas A3系列产品</term> | √ |8+| <term>Atlas A3系列产品</term> | √ |
9-| <term>Atlas A2系列产品</term> | √ |9+| <term>Atlas A2系列产品</term> | √ |
10-| <term>Atlas 200I/500 A2推理产品</term> | × |10+| <term>Atlas 200I/500 A2推理产品</term> | × |
11-| <term>Atlas推理系列产品</term> | × |11+| <term>Atlas推理系列产品</term> | × |
12-| <term>Atlas训练系列产品</term> | × |12+| <term>Atlas训练系列产品</term> | × |
13 13 
14## 功能说明14## 功能说明
15 15 
@@ -26,49 +26,17 @@
26 $$26 $$
27 27 
28 其中:28 其中:
29+ 
29 - gradients:对应Relu6操作的反向传播梯度。30 - gradients:对应Relu6操作的反向传播梯度。
30 - features:作为输入传递给对应Relu6操作的特征,或其输出,使用任一者产生相同结果。31 - features:作为输入传递给对应Relu6操作的特征,或其输出,使用任一者产生相同结果。
31 32 
32## 参数说明33## 参数说明
33 34 
34-<table style="undefined;table-layout: fixed; width: 820px"><colgroup>35+| 参数名 | 输入/输出/属性 | 描述 | 数据类型 | 数据格式 |
35- <col style="width: 100px">36+| --- | --- | --- | --- | --- |
36- <col style="width: 150px">37+| gradients | 输入 | 传递给对应Relu6操作的反向传播梯度,公式中的gradients。 | FLOAT16、FLOAT、BFLOAT16 | ND |
37- <col style="width: 190px">38+| features | 输入 | 作为输入传递给对应Relu6操作的特征,或其输出,使用任一者产生相同结果,公式中的features。与gradients的shape关系见约束说明。 | FLOAT16、FLOAT、BFLOAT16 | ND |
38- <col style="width: 260px">39+| backprops | 输出 | 公式中的输出张量,shape由输入shape按各产品的广播约束确定,见约束说明。 | FLOAT16、FLOAT、BFLOAT16 | ND |
39- <col style="width: 120px">
40- </colgroup>
41- <thead>
42- <tr>
43- <th>参数名</th>
44- <th>输入/输出/属性</th>
45- <th>描述</th>
46- <th>数据类型</th>
47- <th>数据格式</th>
48- </tr></thead>
49- <tbody>
50- <tr>
51- <td>gradients</td>
52- <td>输入</td>
53- <td>传递给对应Relu6操作的反向传播梯度,公式中的gradients。</td>
54- <td>FLOAT16、FLOAT、BFLOAT16</td>
55- <td>ND</td>
56- </tr>
57- <tr>
58- <td>features</td>
59- <td>输入</td>
60- <td>作为输入传递给对应Relu6操作的特征,或其输出,使用任一者产生相同结果,公式中的features。与gradients的shape关系见约束说明。</td>
61- <td>FLOAT16、FLOAT、BFLOAT16</td>
62- <td>ND</td>
63- </tr>
64- <tr>
65- <td>backprops</td>
66- <td>输出</td>
67- <td>公式中的输出张量,shape由输入shape按各产品的广播约束确定,见约束说明。</td>
68- <td>FLOAT16、FLOAT、BFLOAT16</td>
69- <td>ND</td>
70- </tr>
71- </tbody></table>
72 40 
73## 约束说明41## 约束说明
74 42 
@@ -124,12 +92,12 @@ In0/In1 ─CopyInBrc─ Cast(->fp32) ─+
124把fp16/bf16整体提升到fp32做cmp/and/sel,末端用`CAST_MODE_RINT`92把fp16/bf16整体提升到fp32做cmp/and/sel,末端用`CAST_MODE_RINT`
125(round-to-nearest-even)回退到原dtype。原因:93(round-to-nearest-even)回退到原dtype。原因:
126 94 
127-1. 与DSL `relu6_grad.py`在fp16vcmpsel不可用时fallback到fp32的行为一致;95+1. 比较和选择操作统一使用fp32中间精度;
1282. 上下界0.0和6.0在fp16/bf16中均能精确表达,但cmp走fp32路径更稳健,962. 上下界0.0和6.0在fp16/bf16中均能精确表达,但cmp走fp32路径更稳健,
129 且dy是NaN/Inf时fp32中间不会被半精度范围截断。97 且dy是NaN/Inf时fp32中间不会被半精度范围截断。
130 98 
131## 调用说明99## 调用说明
132 100 
133-| 调用方式 | 样例代码 | 说明 |101+| 调用方式 | 样例代码 | 说明 |
134-| ---------------- | --------------------------- |-----------------------------------------------|102+| --- | --- | --- |
135-| GE图模式 | [test_geir_relu6_grad.cpp](examples/arch35/test_geir_relu6_grad.cpp) | 通过[算子IR](op_graph/relu6_grad_proto.h)构图方式调用Relu6Grad算子。 |103+| GE图模式 | [test_geir_relu6_grad.cpp](examples/arch35/test_geir_relu6_grad.cpp) | 通过[算子IR](op_graph/relu6_grad_proto.h)构图方式调用Relu6Grad算子。 |
@@ -18,8 +18,15 @@ ge::graphStatus InferDataType4Relu6Grad(gert::InferDataTypeContext* context)
18 return ge::GRAPH_FAILED;18 return ge::GRAPH_FAILED;
19 }19 }
20 const auto dtype = context->GetInputDataType(0);20 const auto dtype = context->GetInputDataType(0);
21+ OP_CHECK_IF(dtype != ge::DT_FLOAT16 && dtype != ge::DT_FLOAT && dtype != ge::DT_BF16,
22+ OP_LOGE_FOR_INVALID_DTYPE(context->GetNodeName(), "gradients", Ops::Base::ToString(dtype).c_str(),
23+ "FLOAT16, FLOAT, BFLOAT16"),
24+ return ge::GRAPH_FAILED);
21 OP_CHECK_IF(dtype != context->GetInputDataType(1),25 OP_CHECK_IF(dtype != context->GetInputDataType(1),
22- OP_LOGE(context->GetNodeName(), "gradients and features must have the same dtype."),26+ OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
27+ context->GetNodeName(), "gradients, features",
28+ (Ops::Base::ToString(dtype) + ", " + Ops::Base::ToString(context->GetInputDataType(1))).c_str(),
29+ "gradients and features must have the same dtype"),
23 return ge::GRAPH_FAILED);30 return ge::GRAPH_FAILED);
24 return context->SetOutputDataType(0, dtype);31 return context->SetOutputDataType(0, dtype);
25}32}
@@ -12,6 +12,7 @@
12 * \file relu6_grad_infershape.cpp12 * \file relu6_grad_infershape.cpp
13 * \brief13 * \brief
14 */14 */
15+#include <algorithm>
15#include "op_host/infershape_broadcast_util.h"16#include "op_host/infershape_broadcast_util.h"
16#include "register/op_impl_registry.h"17#include "register/op_impl_registry.h"
17#include "log/log.h"18#include "log/log.h"
@@ -21,7 +22,33 @@ namespace ops {
21 22 
22static ge::graphStatus InferShape4Relu6Grad(gert::InferShapeContext* context)23static ge::graphStatus InferShape4Relu6Grad(gert::InferShapeContext* context)
23{24{
25+ if (context == nullptr) {
26+ return ge::GRAPH_FAILED;
27+ }
24 OP_LOGD(context->GetNodeName(), "Begin to do InferShape4Relu6Grad in ops-nn");28 OP_LOGD(context->GetNodeName(), "Begin to do InferShape4Relu6Grad in ops-nn");
29+ constexpr size_t MAX_INPUT_DIM_NUM = 8;
30+ const char* inputNames[] = {"gradients", "features"};
31+ for (size_t input = 0; input < 2; ++input) {
32+ const auto* shape = context->GetInputShape(input);
33+ OP_CHECK_NULL_WITH_CONTEXT(context, shape);
34+ OP_CHECK_IF(shape->GetDimNum() > MAX_INPUT_DIM_NUM,
35+ OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), inputNames[input],
36+ std::to_string(shape->GetDimNum()).c_str(), "0 ~ 8 dims"),
37+ return ge::GRAPH_FAILED);
38+ }
39+ const auto* gradients = context->GetInputShape(0);
40+ const auto* features = context->GetInputShape(1);
41+ const size_t commonRank = std::min(gradients->GetDimNum(), features->GetDimNum());
42+ for (size_t offset = 1; offset <= commonRank; ++offset) {
43+ const int64_t gradDim = gradients->GetDim(gradients->GetDimNum() - offset);
44+ const int64_t featDim = features->GetDim(features->GetDimNum() - offset);
45+ OP_CHECK_IF(gradDim >= 0 && featDim >= 0 && gradDim != featDim && gradDim != 1 && featDim != 1,
46+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
47+ context->GetNodeName(), "gradients, features",
48+ Ops::Base::ToString(std::vector<const gert::Shape*>{gradients, features}).c_str(),
49+ "aligned dimensions must be equal or one of them must be 1"),
50+ return ge::GRAPH_FAILED);
51+ }
25 return Ops::Base::InferShape4Broadcast(context);52 return Ops::Base::InferShape4Broadcast(context);
26}53}
27 54 
@@ -60,9 +60,8 @@ struct Relu6Grad {
60};60};
61 61 
62// Promoted path for half / bfloat16_t: cast inputs to PromoteT (float) for62// Promoted path for half / bfloat16_t: cast inputs to PromoteT (float) for
63-// cmp/and/sel to (a) match the DSL fallback in relu6_grad.py that lifts to63+// cmp/and/sel to keep boundary comparison against 0 and 6 stable across
64-// fp32 when fp16 vcmpsel is unavailable, (b) keep boundary comparison64+// half/bf16. Result is cast back to T with
65-// against 0 and 6 stable across half/bf16. Result is cast back to T with
66// round-to-nearest-even.65// round-to-nearest-even.
67template <typename T, typename PromoteT>66template <typename T, typename PromoteT>
68struct Relu6GradFloatCast {67struct Relu6GradFloatCast {
@@ -0,0 +1,7 @@
1+testcase_name,network_name,op_name,input_shapes,input_dtypes,input_formats,output_shapes,output_dtypes,output_formats,input_ori_shapes,input_ori_formats,output_ori_shapes,output_ori_formats,attributes,input_data_ranges,precision_tolerances,absolute_precision,output_inplace_indexes,output_shape_unknown_indexes,is_enabled,remark,soc_series,priority,dump_file_prefix,manual_input_binaries,manual_golden_binaries
2+Relu6Grad_st_fp32_tail,UNKNOWN,Relu6Grad,"((33,), (33,))","('float32', 'float32')","('ND', 'ND')","((33,),)","('float32',)","('ND',)","((33,), (33,))","('ND', 'ND')","((33,),)","('ND',)",{},"((-2, 2), (-1, 7))",,1e-08,(),(),True,equal shapes and unaligned tail,,0,,(),()
3+Relu6Grad_st_fp16_zero_broadcast,UNKNOWN,Relu6Grad,"((1, 3), (2, 1))","('float16', 'float16')","('ND', 'ND')","((2, 3),)","('float16',)","('ND',)","((1, 3), (2, 1))","('ND', 'ND')","((2, 3),)","('ND',)",{},"((-2, 2), (0, 0))",,1e-08,(),(),True,bidirectional broadcast and lower endpoint,,0,,(),()
4+Relu6Grad_st_bf16_six,UNKNOWN,Relu6Grad,"((2, 3), (2, 3))","('bfloat16', 'bfloat16')","('ND', 'ND')","((2, 3),)","('bfloat16',)","('ND',)","((2, 3), (2, 3))","('ND', 'ND')","((2, 3),)","('ND',)",{},"((-2, 2), (6, 6))",,1e-08,(),(),True,upper endpoint,,0,,(),()
5+Relu6Grad_st_bf16_rank8,UNKNOWN,Relu6Grad,"((1, 1, 1, 1, 1, 1, 2, 1), (1, 1, 1, 1, 1, 1, 1, 3))","('bfloat16', 'bfloat16')","('ND', 'ND')","((1, 1, 1, 1, 1, 1, 2, 3),)","('bfloat16',)","('ND',)","((1, 1, 1, 1, 1, 1, 2, 1), (1, 1, 1, 1, 1, 1, 1, 3))","('ND', 'ND')","((1, 1, 1, 1, 1, 1, 2, 3),)","('ND',)",{},"((2, 2), (4, 4))",,1e-08,(),(),True,maximum rank and broadcast with nonzero output,,0,,(),()
6+Relu6Grad_st_fp16_feature_one,UNKNOWN,Relu6Grad,"((17,), (1,))","('float16', 'float16')","('ND', 'ND')","((17,),)","('float16',)","('ND',)","((17,), (1,))","('ND', 'ND')","((17,),)","('ND',)",{},"((-2, 2), (4, 4))",,1e-08,(),(),True,single-element feature broadcast,,0,,(),()
7+Relu6Grad_st_fp32_inf_outside,UNKNOWN,Relu6Grad,"((3,), (3,))","('float32', 'float32')","('ND', 'ND')","((3,),)","('float32',)","('ND',)","((3,), (3,))","('ND', 'ND')","((3,),)","('ND',)",{},"((float('inf'), float('inf')), (-1, -1))",,1e-08,(),(),True,Inf gradient outside interval selects zero,,0,,(),()
@@ -45,3 +45,10 @@ TEST(Relu6GradGraphInfer, RejectsMismatchedInputsWithoutLegacyVerifier)
45 CheckDtype(ge::DT_FLOAT16, ge::DT_FLOAT, ge::GRAPH_FAILED);45 CheckDtype(ge::DT_FLOAT16, ge::DT_FLOAT, ge::GRAPH_FAILED);
46 CheckDtype(ge::DT_BF16, ge::DT_FLOAT16, ge::GRAPH_FAILED);46 CheckDtype(ge::DT_BF16, ge::DT_FLOAT16, ge::GRAPH_FAILED);
47}47}
48+ 
49+TEST(Relu6GradGraphInfer, RejectsUnsupportedMatchingDataTypes)
50+{
51+ CheckDtype(ge::DT_INT32, ge::DT_INT32, ge::GRAPH_FAILED);
52+ CheckDtype(ge::DT_BOOL, ge::DT_BOOL, ge::GRAPH_FAILED);
53+ CheckDtype(ge::DT_DOUBLE, ge::DT_DOUBLE, ge::GRAPH_FAILED);
54+}
@@ -27,6 +27,23 @@ protected:
27 static void TearDownTestCase() { std::cout << "relu6grad TearDown" << std::endl; }27 static void TearDownTestCase() { std::cout << "relu6grad TearDown" << std::endl; }
28};28};
29 29 
30+TEST_F(relu6grad, relu6grad_infer_shape_rank9_rejected_for_each_input)
31+{
32+ auto inferShapeFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("Relu6Grad")->infer_shape;
33+ gert::StorageShape rank9 = {{1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1}};
34+ gert::StorageShape scalar = {{1}, {1}};
35+ for (size_t input = 0; input < 2; ++input) {
36+ gert::StorageShape output = {{}, {}};
37+ auto holder = gert::InferShapeContextFaker()
38+ .NodeIoNum(2, 1)
39+ .IrInstanceNum({1, 1})
40+ .InputShapes({input == 0 ? &rank9 : &scalar, input == 1 ? &rank9 : &scalar})
41+ .OutputShapes({&output})
42+ .Build();
43+ EXPECT_EQ(inferShapeFunc(holder.GetContext<gert::InferShapeContext>()), ge::GRAPH_FAILED);
44+ }
45+}
46+ 
30TEST_F(relu6grad, relu6grad_infer_shape_same_fp32)47TEST_F(relu6grad, relu6grad_infer_shape_same_fp32)
31{48{
32 auto inferShapeFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("Relu6Grad")->infer_shape;49 auto inferShapeFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("Relu6Grad")->infer_shape;
@@ -3,9 +3,9 @@
3## 产品支持情况3## 产品支持情况
4 4 
5| 产品 | 是否支持 |5| 产品 | 是否支持 |
6-| :----------------------------------------- | :------: |6+| :--- | :---: |
7| <term>Ascend 950PR&950DT系列产品</term> | √ |7| <term>Ascend 950PR&950DT系列产品</term> | √ |
8-| <term>Atlas A3系列产品</term>| √ |8+| <term>Atlas A3系列产品</term> | √ |
9| <term>Atlas A2系列产品</term> | √ |9| <term>Atlas A2系列产品</term> | √ |
10| <term>Atlas 200I/500 A2推理产品</term> | √ |10| <term>Atlas 200I/500 A2推理产品</term> | √ |
11| <term>Atlas推理系列产品</term> | √ |11| <term>Atlas推理系列产品</term> | √ |
@@ -61,7 +61,7 @@
61| accum | 输入 | 动量累积量,必须为可写Tensor;shape、数据类型和数据格式与`parameters`一致。 | FLOAT、FLOAT16、BFLOAT16 | NC1HWC0、NDC1HWC0、ND、FRACTAL_Z、FRACTAL_Z_3D |61| accum | 输入 | 动量累积量,必须为可写Tensor;shape、数据类型和数据格式与`parameters`一致。 | FLOAT、FLOAT16、BFLOAT16 | NC1HWC0、NDC1HWC0、ND、FRACTAL_Z、FRACTAL_Z_3D |
62| momentum | 输入 | 动量因子,元素个数必须为1,数据类型与`parameters`一致。 | FLOAT、FLOAT16、BFLOAT16 | ND |62| momentum | 输入 | 动量因子,元素个数必须为1,数据类型与`parameters`一致。 | FLOAT、FLOAT16、BFLOAT16 | ND |
63| stat | 输入 | 逐元素首步标记,取值1表示不施加阻尼;必须为可写Tensor,shape、数据类型和数据格式与`parameters`一致。 | FLOAT、FLOAT16、BFLOAT16 | NC1HWC0、NDC1HWC0、ND、FRACTAL_Z、FRACTAL_Z_3D |63| stat | 输入 | 逐元素首步标记,取值1表示不施加阻尼;必须为可写Tensor,shape、数据类型和数据格式与`parameters`一致。 | FLOAT、FLOAT16、BFLOAT16 | NC1HWC0、NDC1HWC0、ND、FRACTAL_Z、FRACTAL_Z_3D |
64-| parameters | 输出 | 更新后的权重,与输入`parameters`共享内存,shape、数据类型和数据格式保持一致。 | FLOAT、FLOAT16、BFLOAT16 | NC1HWC0、NDC1HWC0、ND、FRACTAL_Z、FRACTAL_Z_3D |64+| parameters | 输出 | 更新后的权重,shape、数据类型和数据格式与输入`parameters`保持一致。 | FLOAT、FLOAT16、BFLOAT16 | NC1HWC0、NDC1HWC0、ND、FRACTAL_Z、FRACTAL_Z_3D |
65| dampening | 可选属性 | 动量阻尼系数,默认值为0.0;当`nesterov=true`时必须为0。 | FLOAT | - |65| dampening | 可选属性 | 动量阻尼系数,默认值为0.0;当`nesterov=true`时必须为0。 | FLOAT | - |
66| weight_decay | 可选属性 | 权重衰减系数,默认值为0.0,取值必须大于或等于0。 | FLOAT | - |66| weight_decay | 可选属性 | 权重衰减系数,默认值为0.0,取值必须大于或等于0。 | FLOAT | - |
67| nesterov | 可选属性 | 是否启用Nesterov动量,默认值为false。 | BOOL | - |67| nesterov | 可选属性 | 是否启用Nesterov动量,默认值为false。 | BOOL | - |
@@ -69,11 +69,12 @@
69## 约束说明69## 约束说明
70 70 
71- 图上仅声明一个`parameters`输出,但`parameters`、`accum`和`stat`均可能被原地更新,71- 图上仅声明一个`parameters`输出,但`parameters`、`accum`和`stat`均可能被原地更新,
72- 调用方必须将这三个输入作为可写Tensor。72+ 调用方必须将这三个输入作为可写Tensor。权重更新值应从`parameters`输出读取。
73- `momentum=0`仅阻止`accum`和`stat`回写,不跳过权重更新中的动量乘法;当`accum`含Inf时,`0×Inf`产生的NaN会传播到权重结果。73- `momentum=0`仅阻止`accum`和`stat`回写,不跳过权重更新中的动量乘法;当`accum`含Inf时,`0×Inf`产生的NaN会传播到权重结果。
74- <term>Ascend 950PR&950DT系列产品</term>:74- <term>Ascend 950PR&950DT系列产品</term>:
75 - 数据类型:各输入、输出支持FLOAT、FLOAT16、BFLOAT16。75 - 数据类型:各输入、输出支持FLOAT、FLOAT16、BFLOAT16。
76 - 数据格式:静态shape和动态shape的各输入、输出均为ND。76 - 数据格式:静态shape和动态shape的各输入、输出均为ND。
77+ - GE图输入按ND连续布局提供数据;转置、切片等非连续视图需由调用方或框架先转换为连续Tensor。
77 - 动态shape能力:支持动态shape和动态rank。78 - 动态shape能力:支持动态shape和动态rank。
78 - 输入shape约束:`parameters`的rank为1~8,不支持空Tensor。79 - 输入shape约束:`parameters`的rank为1~8,不支持空Tensor。
79 - 中间计算精度:使用FLOAT进行中间计算,FLOAT16、BFLOAT16结果按就近偶数舍入转换回输入数据类型。80 - 中间计算精度:使用FLOAT进行中间计算,FLOAT16、BFLOAT16结果按就近偶数舍入转换回输入数据类型。
@@ -54,7 +54,7 @@ using std::string;
54using std::vector;54using std::vector;
55#define ADD_INPUT(intputIndex, intputName, intputDtype, inputShape, inputValue) \55#define ADD_INPUT(intputIndex, intputName, intputDtype, inputShape, inputValue) \
56 vector<int64_t> placeholder##intputIndex##_shape = inputShape; \56 vector<int64_t> placeholder##intputIndex##_shape = inputShape; \
57- auto placeholder##intputIndex = op::Data("placeholder" + std::to_string(intputIndex)) \57+ auto placeholder##intputIndex = op::Data(("placeholder" + std::to_string(intputIndex)).c_str()) \
58 .set_attr_index(intputIndex - 1); \58 .set_attr_index(intputIndex - 1); \
59 TensorDesc placeholder##intputIndex##_desc = TensorDesc(ge::Shape(placeholder##intputIndex##_shape), FORMAT_ND, \59 TensorDesc placeholder##intputIndex##_desc = TensorDesc(ge::Shape(placeholder##intputIndex##_shape), FORMAT_ND, \
60 intputDtype); \60 intputDtype); \
@@ -78,7 +78,7 @@ using std::vector;
78 78 
79#define ADD_CONST_INPUT(intputIndex, intputName, intputDtype, inputShape) \79#define ADD_CONST_INPUT(intputIndex, intputName, intputDtype, inputShape) \
80 vector<int64_t> placeholder##intputIndex##_shape = inputShape; \80 vector<int64_t> placeholder##intputIndex##_shape = inputShape; \
81- auto placeholder##intputIndex = op::Const("placeholder" + std::to_string(intputIndex)); \81+ auto placeholder##intputIndex = op::Const(("placeholder" + std::to_string(intputIndex)).c_str()); \
82 TensorDesc placeholder##intputIndex##_desc = TensorDesc(ge::Shape(placeholder##intputIndex##_shape), FORMAT_ND, \82 TensorDesc placeholder##intputIndex##_desc = TensorDesc(ge::Shape(placeholder##intputIndex##_shape), FORMAT_ND, \
83 intputDtype); \83 intputDtype); \
84 placeholder##intputIndex##_desc.SetPlacement(ge::kPlacementHost); \84 placeholder##intputIndex##_desc.SetPlacement(ge::kPlacementHost); \
@@ -328,7 +328,13 @@ int main(int argc, char* argv[])
328 printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());328 printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());
329 printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());329 printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());
330 std::string file_path = "./dump";330 std::string file_path = "./dump";
331- aclgrphDumpGraph(graph, file_path.c_str(), file_path.length());331+ ret = aclgrphDumpGraph(graph, file_path.c_str(), file_path.length());
332+ if (ret != SUCCESS) {
333+ printf("%s - ERROR - [XIR]: Dump graph failed\n", GetTime().c_str());
334+ delete session;
335+ GEFinalize();
336+ return FAILED;
337+ }
332 printf("%s - INFO - [XIR]: Start to run ir compute graph\n", GetTime().c_str());338 printf("%s - INFO - [XIR]: Start to run ir compute graph\n", GetTime().c_str());
333 std::vector<ge::Tensor> output;339 std::vector<ge::Tensor> output;
334 ret = session->RunGraph(graph_id, input, output);340 ret = session->RunGraph(graph_id, input, output);
@@ -22,13 +22,29 @@ namespace {
22constexpr size_t PARAMETERS_INDEX = 0;22constexpr size_t PARAMETERS_INDEX = 0;
23} // namespace23} // namespace
24 24 
25-static ge::graphStatus InferDataTypeForSgd(gert::InferDataTypeContext* context)25+ge::graphStatus InferDataTypeForSgd(gert::InferDataTypeContext* context)
26{26{
27+ if (context == nullptr) {
28+ return ge::GRAPH_FAILED;
29+ }
27 OP_LOGD(context, "InferDataTypeForSgd begin.");30 OP_LOGD(context, "InferDataTypeForSgd begin.");
28- // accum / stat 不是图输出(靠覆写输入 GM 原地回写),故此处只有一路。31+ const auto dtype = context->GetInputDataType(PARAMETERS_INDEX);
29- context->SetOutputDataType(PARAMETERS_INDEX, context->GetInputDataType(PARAMETERS_INDEX));32+ OP_CHECK_IF(dtype != ge::DT_FLOAT16 && dtype != ge::DT_FLOAT && dtype != ge::DT_BF16,
33+ OP_LOGE_FOR_INVALID_DTYPE(context->GetNodeName(), "parameters", Ops::Base::ToString(dtype).c_str(),
34+ "FLOAT16, FLOAT, BFLOAT16"),
35+ return ge::GRAPH_FAILED);
36+ const char* inputNames[] = {"parameters", "gradient", "learning_rate", "accum", "momentum", "stat"};
37+ for (size_t input = 1; input < 6; ++input) {
38+ const auto inputDtype = context->GetInputDataType(input);
39+ OP_CHECK_IF(inputDtype != dtype,
40+ OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
41+ context->GetNodeName(), (std::string("parameters, ") + inputNames[input]).c_str(),
42+ (Ops::Base::ToString(dtype) + ", " + Ops::Base::ToString(inputDtype)).c_str(),
43+ "all inputs must have the same dtype as parameters"),
44+ return ge::GRAPH_FAILED);
45+ }
30 OP_LOGD(context, "InferDataTypeForSgd end.");46 OP_LOGD(context, "InferDataTypeForSgd end.");
31- return ge::GRAPH_SUCCESS;47+ return context->SetOutputDataType(PARAMETERS_INDEX, dtype);
32}48}
33 49 
34IMPL_OP(SGD).InferDataType(InferDataTypeForSgd);50IMPL_OP(SGD).InferDataType(InferDataTypeForSgd);
@@ -13,6 +13,7 @@
13 * \brief13 * \brief
14 */14 */
15#include "sgd_tiling.h"15#include "sgd_tiling.h"
16+#include <limits>
16#include <graph/utils/type_utils.h>17#include <graph/utils/type_utils.h>
17#include "error_util.h"18#include "error_util.h"
18#include "register/op_def_registry.h"19#include "register/op_def_registry.h"
@@ -59,7 +60,7 @@ ge::graphStatus SgdRegbaseTiling::GetAttr()
59 nesterov_ = (nesterovAttr != nullptr) ? *nesterovAttr : false;60 nesterov_ = (nesterovAttr != nullptr) ? *nesterovAttr : false;
60 61 
61 // 对齐 ascend910b 的两条属性语义校验(canndev nn_training_ops.cc:1932-1955)。62 // 对齐 ascend910b 的两条属性语义校验(canndev nn_training_ops.cc:1932-1955)。
62- // 走结构化上报宏(R6),非裸 OP_LOGE。63+ // 属性非法时使用结构化错误上报。
63 OP_CHECK_IF(nesterov_ && dampening_ != 0.0f,64 OP_CHECK_IF(nesterov_ && dampening_ != 0.0f,
64 OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(tilingContext_->GetNodeName(), "dampening",65 OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(tilingContext_->GetNodeName(), "dampening",
65 std::to_string(dampening_).c_str(),66 std::to_string(dampening_).c_str(),
@@ -71,7 +72,7 @@ ge::graphStatus SgdRegbaseTiling::GetAttr()
71 "attr weight_decay must be more than or equal to 0"),72 "attr weight_decay must be more than or equal to 0"),
72 return ge::GRAPH_FAILED);73 return ge::GRAPH_FAILED);
73 74 
74- // 属性"是否为 0"固化为编译期分支(spec numerical_stability.skip_zero_branches):75+ // 属性"是否为 0"固化为编译期分支:
75 // 条件不成立时【真正跳过】该子图,而不是乘 0 —— 0 * inf = NaN 会污染结果。76 // 条件不成立时【真正跳过】该子图,而不是乘 0 —— 0 * inf = NaN 会污染结果。
76 useNesterovKey_ = nesterov_ ? 1U : 0U;77 useNesterovKey_ = nesterov_ ? 1U : 0U;
77 hasWeightDecayKey_ = (weightDecay_ != 0.0f) ? 1U : 0U;78 hasWeightDecayKey_ = (weightDecay_ != 0.0f) ? 1U : 0U;
@@ -91,13 +92,22 @@ ge::graphStatus SgdRegbaseTiling::CheckRank(const gert::Shape& input0Shape)
91 92 
92ge::graphStatus SgdRegbaseTiling::CheckNotEmpty(const gert::Shape& input0Shape)93ge::graphStatus SgdRegbaseTiling::CheckNotEmpty(const gert::Shape& input0Shape)
93{94{
94- // 空 Tensor(任意一轴或多轴为 0)按 spec error_codes 归 null_input,拒绝为非法。95+ int64_t elementCount = 1;
95- // 本算子无"空进空出"语义 —— accum / stat 的原地回写在 numel == 0 下无定义。96+ for (size_t axis = 0; axis < input0Shape.GetDimNum(); ++axis) {
96- OP_CHECK_IF(input0Shape.GetShapeSize() == 0,97+ const int64_t dim = input0Shape.GetDim(axis);
97- OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(tilingContext_->GetNodeName(), "parameters",98+ OP_CHECK_IF(
98- std::to_string(input0Shape.GetShapeSize()).c_str(),99+ dim == 0,
99- "empty tensor (any axis being 0) is not supported by SGD"),100+ OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(tilingContext_->GetNodeName(), "parameters", "0",
100- return ge::GRAPH_FAILED);101+ "empty tensor (any axis being 0) is not supported by SGD"),
102+ return ge::GRAPH_FAILED);
103+ // Tiling receives concrete runtime dimensions, unlike InferShape.
104+ OP_CHECK_IF(dim < 0 || elementCount > std::numeric_limits<int64_t>::max() / dim,
105+ OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON(
106+ tilingContext_->GetNodeName(), "parameters", Ops::Base::ToString(input0Shape).c_str(),
107+ "runtime dimensions must be positive and their product must fit in int64"),
108+ return ge::GRAPH_FAILED);
109+ elementCount *= dim;
110+ }
101 return ge::GRAPH_SUCCESS;111 return ge::GRAPH_SUCCESS;
102}112}
103 113 
@@ -120,7 +130,6 @@ ge::graphStatus SgdRegbaseTiling::CheckSameShape(int32_t inputIdx, const gert::S
120 auto inputShape = tilingContext_->GetInputShape(inputIdx);130 auto inputShape = tilingContext_->GetInputShape(inputIdx);
121 OP_CHECK_NULL_WITH_CONTEXT(tilingContext_, inputShape);131 OP_CHECK_NULL_WITH_CONTEXT(tilingContext_, inputShape);
122 // 严格相等,不做广播 —— "可广播但不相等"(如 [2,3] vs [1,3])同样判非法,132 // 严格相等,不做广播 —— "可广播但不相等"(如 [2,3] vs [1,3])同样判非法,
123- // 对齐 spec 的 shape_mismatch 口径。
124 if (inputShape->GetStorageShape() != input0Shape) {133 if (inputShape->GetStorageShape() != input0Shape) {
125 return ge::GRAPH_FAILED;134 return ge::GRAPH_FAILED;
126 }135 }
@@ -149,7 +158,7 @@ ge::graphStatus SgdRegbaseTiling::CheckShapeAndType()
149 158 
150 // 检查全部输入的shape与dtype;不能仅依赖parameters的rank校验。159 // 检查全部输入的shape与dtype;不能仅依赖parameters的rank校验。
151 // 内层 CheckRank 已用 OP_LOGE_FOR_INVALID_SHAPEDIM 结构化上报,此处只作调用链留痕,160 // 内层 CheckRank 已用 OP_LOGE_FOR_INVALID_SHAPEDIM 结构化上报,此处只作调用链留痕,
152- // 不重复打 ERROR(R6 要求的是"校验必须走结构化宏",不是"每层都再报一次")。161+ // 不重复上报同一错误。
153 OP_CHECK_IF(CheckRank(inputStorageShape) != ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_, "rank check failed"),162 OP_CHECK_IF(CheckRank(inputStorageShape) != ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_, "rank check failed"),
154 return ge::GRAPH_FAILED);163 return ge::GRAPH_FAILED);
155 OP_CHECK_IF(CheckNotEmpty(inputStorageShape) != ge::GRAPH_SUCCESS,164 OP_CHECK_IF(CheckNotEmpty(inputStorageShape) != ge::GRAPH_SUCCESS,
@@ -288,7 +297,6 @@ ge::graphStatus SgdRegbaseTiling::RunTiling()
288 }297 }
289 // GetAttr / CheckShapeAndType 内部对非法输入已用 OP_LOGE_FOR_INVALID_* 结构化上报,298 // GetAttr / CheckShapeAndType 内部对非法输入已用 OP_LOGE_FOR_INVALID_* 结构化上报,
290 // 此处只作调用链留痕、不重复打 ERROR —— 与上面 CheckRank / CheckNotEmpty 的处理一致。299 // 此处只作调用链留痕、不重复打 ERROR —— 与上面 CheckRank / CheckNotEmpty 的处理一致。
291- // R6 要求的是"校验必须走结构化宏",不是"每层都再报一次"。
292 OP_CHECK_IF(GetAttr() != ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_, "Get attr failed."), return ge::GRAPH_FAILED);300 OP_CHECK_IF(GetAttr() != ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_, "Get attr failed."), return ge::GRAPH_FAILED);
293 OP_CHECK_IF(CheckShapeAndType() != ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_, "Shape and dtype check failed."),301 OP_CHECK_IF(CheckShapeAndType() != ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_, "Shape and dtype check failed."),
294 return ge::GRAPH_FAILED);302 return ge::GRAPH_FAILED);
@@ -10,7 +10,7 @@
10 10 
11/*!11/*!
12 * \file sgd_def.cpp12 * \file sgd_def.cpp
13- * \brief SGD OpDef —— 仅适配 Ascend950(arch35 / DAV_3510 / regbase)13+ * \brief SGD OpDef(arch35 / regbase)
14 *14 *
15 * 支持面对齐 910B/910C 基线(canndev aic-ascend910b-ops-info.ini 的 [SGD] 段),15 * 支持面对齐 910B/910C 基线(canndev aic-ascend910b-ops-info.ini 的 [SGD] 段),
16 * 唯一收窄项是 format:ascend910b 大张量支持 NC1HWC0 / NDC1HWC0 / ND / FRACTAL_Z /16 * 唯一收窄项是 format:ascend910b 大张量支持 NC1HWC0 / NDC1HWC0 / ND / FRACTAL_Z /
@@ -19,9 +19,7 @@
19 * apply_centered_rms_prop)的 def.cpp 一律只声明 ge::FORMAT_ND,无一例声明私有19 * apply_centered_rms_prop)的 def.cpp 一律只声明 ge::FORMAT_ND,无一例声明私有
20 * format。当前支持范围及产品差异见算子 README。20 * format。当前支持范围及产品差异见算子 README。
21 *21 *
22- * R2(ascend950 不碰 ascend910b)天然满足:ops-nn 仓内不存在 SGD 算子(optim/fused_sgd 是语义22+ * 本文件通过 AddConfig 注册 ascend950 与 ascend350 配置,不修改其他产品的配置。
23- * 不同的另一个算子,且无 arch35 目录),本算子为净新增,只 AddConfig("ascend950"),
24- * 不触碰任何 ascend910b 配置;apply_momentum 另有 ascend350 等配置,不代表本算子的注册范围。
25 *23 *
26 * accum / stat 【不声明为 Output】—— 与 ascend910b 形态一致(canndev proto24 * accum / stat 【不声明为 Output】—— 与 ascend910b 形态一致(canndev proto
27 * nn_training_ops.h:1431 亦只有一个 OUTPUT),两者靠覆写输入 GM 原地回写。25 * nn_training_ops.h:1431 亦只有一个 OUTPUT),两者靠覆写输入 GM 原地回写。
@@ -39,10 +39,67 @@ constexpr int64_t UNKNOWN_RANK_DIM = -2;
39 39 
40// UNKNOWN_RANK 在 GE 下表现为 dims == {-2},须透传而非按 rank 拒绝。40// UNKNOWN_RANK 在 GE 下表现为 dims == {-2},须透传而非按 rank 拒绝。
41bool IsUnknownRank(const gert::Shape* shape) { return shape->GetDimNum() == 1 && shape->GetDim(0) == UNKNOWN_RANK_DIM; }41bool IsUnknownRank(const gert::Shape* shape) { return shape->GetDimNum() == 1 && shape->GetDim(0) == UNKNOWN_RANK_DIM; }
42+ 
43+ge::graphStatus CheckInputShapes(gert::InferShapeContext* context, const gert::Shape* paramShape)
44+{
45+ const char* inputNames[] = {"parameters", "gradient", "learning_rate", "accum", "momentum", "stat"};
46+ for (size_t input = 0; input < 6; ++input) {
47+ const auto* shape = context->GetInputShape(input);
48+ OP_CHECK_NULL_WITH_CONTEXT(context, shape);
49+ if (IsUnknownRank(shape)) {
50+ continue;
51+ }
52+ const bool isScalarInput = input == 2 || input == 4;
53+ const size_t rank = shape->GetDimNum();
54+ if (!isScalarInput) {
55+ OP_CHECK_IF(rank < MIN_DIM_NUM || rank > MAX_DIM_NUM,
56+ OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), inputNames[input],
57+ std::to_string(rank).c_str(), "1 ~ 8 dims"),
58+ return ge::GRAPH_FAILED);
59+ }
60+ for (size_t axis = 0; axis < rank; ++axis) {
61+ const int64_t dim = shape->GetDim(axis);
62+ OP_CHECK_IF(dim == 0 && !isScalarInput,
63+ OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(context->GetNodeName(), inputNames[input], "0",
64+ "empty tensors are not supported"),
65+ return ge::GRAPH_FAILED);
66+ OP_CHECK_IF(isScalarInput && (dim == 0 || dim > 1),
67+ OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON(context->GetNodeName(), inputNames[input],
68+ Ops::Base::ToString(*shape).c_str(),
69+ "must contain exactly one element"),
70+ return ge::GRAPH_FAILED);
71+ }
72+ // Unknown dimensions and unknown rank are resolved at runtime. Only
73+ // reject shape relationships that are already known to be invalid.
74+ if (isScalarInput || IsUnknownRank(paramShape)) {
75+ continue;
76+ }
77+ OP_CHECK_IF(rank != paramShape->GetDimNum(),
78+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
79+ context->GetNodeName(), (std::string("parameters, ") + inputNames[input]).c_str(),
80+ Ops::Base::ToString(std::vector<const gert::Shape*>{paramShape, shape}).c_str(),
81+ "must have the same shape as parameters"),
82+ return ge::GRAPH_FAILED);
83+ for (size_t axis = 0; axis < rank; ++axis) {
84+ const int64_t dim = shape->GetDim(axis);
85+ const int64_t paramDim = paramShape->GetDim(axis);
86+ OP_CHECK_IF(dim >= 0 && paramDim >= 0 && dim != paramDim,
87+ OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
88+ context->GetNodeName(), (std::string("parameters, ") + inputNames[input]).c_str(),
89+ Ops::Base::ToString(std::vector<const gert::Shape*>{paramShape, shape}).c_str(),
90+ "must have the same shape as parameters"),
91+ return ge::GRAPH_FAILED);
92+ }
93+ }
94+ return ge::GRAPH_SUCCESS;
95+}
42} // namespace96} // namespace
43 97 
44static ge::graphStatus InferShapeForSgd(gert::InferShapeContext* context)98static ge::graphStatus InferShapeForSgd(gert::InferShapeContext* context)
45{99{
100+ if (context == nullptr) {
101+ return ge::GRAPH_FAILED;
102+ }
46 OP_LOGD(context, "InferShapeForSgd begin.");103 OP_LOGD(context, "InferShapeForSgd begin.");
47 104 
48 const auto* attrs = context->GetAttrs();105 const auto* attrs = context->GetAttrs();
@@ -65,19 +122,11 @@ static ge::graphStatus InferShapeForSgd(gert::InferShapeContext* context)
65 "attr weight_decay must be more than or equal to 0"),122 "attr weight_decay must be more than or equal to 0"),
66 return ge::GRAPH_FAILED);123 return ge::GRAPH_FAILED);
67 124 
68- // 这里守的是「框架没把 shape 递过来」(返回 nullptr),属于内部失败,不是 parameters 取值非法,
69- // 所以用裸 OP_LOGE 而非 OP_LOGE_FOR_INVALID_*:后者会走 REPORT_PREDEFINED_ERR_MSG 报 EZ0009/EZ0010,
70- // 对用户提示「你的 shape 不合法」,而此刻并不知道用户给了什么。真正的 shape 取值校验在下面的 dimNum 分支。
71 const gert::Shape* paramShape = context->GetInputShape(PARAMETERS_INDEX);125 const gert::Shape* paramShape = context->GetInputShape(PARAMETERS_INDEX);
72 OP_CHECK_IF(paramShape == nullptr, OP_LOGE(context, "Get input shape of parameters returns nullptr."),126 OP_CHECK_IF(paramShape == nullptr, OP_LOGE(context, "Get input shape of parameters returns nullptr."),
73 return ge::GRAPH_FAILED);127 return ge::GRAPH_FAILED);
74- if (!IsUnknownRank(paramShape)) {128+ OP_CHECK_IF(CheckInputShapes(context, paramShape) != ge::GRAPH_SUCCESS,
75- const size_t dimNum = paramShape->GetDimNum();129+ OP_LOGD(context, "Input shape validation failed."), return ge::GRAPH_FAILED);
76- OP_CHECK_IF(dimNum < MIN_DIM_NUM || dimNum > MAX_DIM_NUM,
77- OP_LOGE_FOR_INVALID_SHAPEDIM(context->GetNodeName(), "parameters", std::to_string(dimNum).c_str(),
78- "1 ~ 8 dims"),
79- return ge::GRAPH_FAILED);
80- }
81 130 
82 ge::graphStatus ret = Ops::Base::InferShape4Elewise(context);131 ge::graphStatus ret = Ops::Base::InferShape4Elewise(context);
83 OP_CHECK_IF(ret == ge::GRAPH_FAILED, OP_LOGE(context, "InferShapeForSgd failed."), return ge::GRAPH_FAILED);132 OP_CHECK_IF(ret == ge::GRAPH_FAILED, OP_LOGE(context, "InferShapeForSgd failed."), return ge::GRAPH_FAILED);
@@ -85,8 +134,6 @@ static ge::graphStatus InferShapeForSgd(gert::InferShapeContext* context)
85 return ret;134 return ret;
86}135}
87 136 
88-// 双挂仍然必须:漏 InferDataType 会导致 GE 侧输出 dtype 推导缺失/推错,是拷模板时的高频漏项137+// InferDataType is registered separately in op_graph/sgd_graph_infer.cpp.
89-// (step3 GATE 明列)。但 InferDataType 只在图场景使用,按交付件划分落在
90-// op_graph/sgd_graph_infer.cpp;此处只挂图与单算子共用的 InferShape。
91IMPL_OP_INFERSHAPE(SGD).InferShape(InferShapeForSgd);138IMPL_OP_INFERSHAPE(SGD).InferShape(InferShapeForSgd);
92} // namespace ops139} // namespace ops
@@ -102,8 +102,7 @@ struct SgdDag {
102 using OpStatF = Bind<Vec::Cast<T, U, 0>, OpCopyInStat>;102 using OpStatF = Bind<Vec::Cast<T, U, 0>, OpCopyInStat>;
103 103 
104 // ── Step 1:权重衰减 grad = gradient + parameters * wd ──────────────────104 // ── Step 1:权重衰减 grad = gradient + parameters * wd ──────────────────
105- // wd == 0 时【真正跳过】而不是乘 0(spec numerical_stability.skip_zero_branches;105+ // wd == 0时跳过权重衰减乘法,避免0 * inf产生NaN并污染结果。
106- // 且 0 * inf = NaN 会污染结果)。
107 using VarWeightDecay = Placeholder::Var<T, SGD_VAR_IDX_WEIGHT_DECAY>;106 using VarWeightDecay = Placeholder::Var<T, SGD_VAR_IDX_WEIGHT_DECAY>;
108 using OpParamMulWd = Bind<Vec::Muls<T>, OpParamF, VarWeightDecay>;107 using OpParamMulWd = Bind<Vec::Muls<T>, OpParamF, VarWeightDecay>;
109 using OpGradWithWd = Bind<Vec::Add<T>, OpGradInF, OpParamMulWd>;108 using OpGradWithWd = Bind<Vec::Add<T>, OpGradInF, OpParamMulWd>;
@@ -11,13 +11,13 @@
11 11 
12"""SGD multi-path TestSpec golden (kernel + GEIR).12"""SGD multi-path TestSpec golden (kernel + GEIR).
13 13 
14-The CPU true-value path is a composition of Torch tensor operators. Its14+The CPU true-value path is a composition of Torch tensor operators following
15-arithmetic is kept identical to the legacy ``sgd_golden`` entry:15+the operator's formula:
16 16 
17 grad = gradient + parameters * weight_decay (when weight_decay != 0)17 grad = gradient + parameters * weight_decay (when weight_decay != 0)
18 accum_t = accum * momentum + grad (unconditional)18 accum_t = accum * momentum + grad (unconditional)
19 accum_t -= grad * (1 - stat) * dampening (when dampening != 0)19 accum_t -= grad * (1 - stat) * dampening (when dampening != 0)
20- p_out = p - (grad*lr + accum_t*m*lr) (Nesterov)20+ p_out = p - (grad + accum_t*m)*lr (Nesterov)
21 p - accum_t*lr (otherwise)21 p - accum_t*lr (otherwise)
22 22 
23``momentum == 0`` only masks the accum/stat writeback; it never skips the23``momentum == 0`` only masks the accum/stat writeback; it never skips the
@@ -25,11 +25,10 @@ arithmetic is kept identical to the legacy ``sgd_golden`` entry:
25NaN into parameters according to IEEE 754, while accum/stat retain their input25NaN into parameters according to IEEE 754, while accum/stat retain their input
26bit patterns (including NaN payloads and negative zero).26bit patterns (including NaN payloads and negative zero).
27 27 
28-The third-party implementation deliberately differs only in the Nesterov28+The Nesterov update direction is summed before multiplication by learning rate.
29-parenthesization: arch35 ``sgd_dag.h`` computes29+Distributing that multiplication changes IEEE 754 results for infinite learning
30-``p - (grad + accum_t * momentum) * learning_rate`` to satisfy its TensorScalar30+rates. Both references retain this grouping and cast writeback values to the
31-slot constraint. Cross-check must reproduce that actual device operation31+requested output dtype.
32-order, then cast all three writeback values to the NPU input dtype.
33 32 
34The CPU oracle evaluates intermediate arithmetic in float64 to avoid sharing33The CPU oracle evaluates intermediate arithmetic in float64 to avoid sharing
35float32 overflow with the device. The independent third-party composition keeps34float32 overflow with the device. The independent third-party composition keeps
@@ -46,12 +45,12 @@ except ImportError: # pragma: no cover - TTK normally provides ml_dtypes.
46 _NP_BFLOAT16 = None45 _NP_BFLOAT16 = None
47 46 
48 47 
49-__spec__ = {"sgd": "SgdKernelSpec"}48+__spec__ = {"sgd": "SgdKernelSpec", "SGD": "SgdKernelSpec"}
50 49 
51# Compatibility entry for existing kernel CSVs and callers. Both entries call50# Compatibility entry for existing kernel CSVs and callers. Both entries call
52# the same _compute function; this mapping must remain until all old loaders have51# the same _compute function; this mapping must remain until all old loaders have
53# migrated to TestSpec.52# migrated to TestSpec.
54-__golden__ = {"kernel": {"sgd": "sgd_golden"}}53+__golden__ = {"kernel": {"sgd": "sgd_golden", "SGD": "sgd_golden"}}
55 54 
56 55 
57_TOL = {56_TOL = {
@@ -128,8 +127,8 @@ def _compute(
128 """CPU reference shared by TestSpec and the legacy entry.127 """CPU reference shared by TestSpec and the legacy entry.
129 128 
130 Inputs and outputs are Torch tensors. This is the high-precision CPU129 Inputs and outputs are Torch tensors. This is the high-precision CPU
131- true-value path, so it keeps the legacy expanded Nesterov formula; the130+ true-value path. It preserves the formula's grouping for nonfinite values;
132- independent ``_SgdCompose`` below follows the grouped arch35 DAG order.131+ the independent ``_SgdCompose`` below uses float32 intermediates.
133 132 
134 Attribute defaults are ``None`` so that ``_attr`` can resolve each one133 Attribute defaults are ``None`` so that ``_attr`` can resolve each one
135 from the ``attributes`` container when the caller did not pass a named134 from the ``attributes`` container when the caller did not pass a named
@@ -168,13 +167,9 @@ def _compute(
168 if dampening_value != 0.0:167 if dampening_value != 0.0:
169 accum_t = accum_t - grad * ((1.0 - s) * dampening_value)168 accum_t = accum_t - grad * ((1.0 - s) * dampening_value)
170 169 
171- # Preserve the already-validated legacy true-value ordering. This is170+ # Sum the update direction before multiplying by a possibly infinite rate.
172- # mathematically equivalent to the grouped DAG form, but intentionally
173- # independent from it for cross-checking.
174 if use_nesterov:171 if use_nesterov:
175- p_new = p - (172+ p_new = p - (grad + accum_t * momentum_value) * learning_rate_value
176- grad * learning_rate_value + accum_t * momentum_value * learning_rate_value
177- )
178 else:173 else:
179 p_new = p - accum_t * learning_rate_value174 p_new = p - accum_t * learning_rate_value
180 175 
@@ -384,8 +379,7 @@ class _SgdCompose:
384 dampening_term = torch.mul(grad, stat_active_damped)379 dampening_term = torch.mul(grad, stat_active_damped)
385 accum_t = torch.sub(accum_t, dampening_term)380 accum_t = torch.sub(accum_t, dampening_term)
386 381 
387- # Step 4: reproduce the real grouped Nesterov DAG, not the expanded382+ # Step 4: sum the Nesterov update direction before scaling it.
388- # legacy true-value expression.
389 if self.nesterov:383 if self.nesterov:
390 accum_t_momentum = torch.mul(accum_t, momentum_value)384 accum_t_momentum = torch.mul(accum_t, momentum_value)
391 nesterov_sum = torch.add(grad, accum_t_momentum)385 nesterov_sum = torch.add(grad, accum_t_momentum)
@@ -406,7 +400,7 @@ class _SgdCompose:
406 400 
407 401 
408class SgdKernelSpec:402class SgdKernelSpec:
409- """Kernel and GEIR share the snake_case ``sgd`` registration."""403+ """Kernel and GEIR accept both snake_case and graph operator names."""
410 404 
411 def golden(405 def golden(
412 parameters,406 parameters,
@@ -0,0 +1,38 @@
1+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3+# CANN Open Software License Agreement Version 2.0 (the "License").
4+# Please refer to the License for details. You may not use this file except in compliance with the License.
5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7+# See LICENSE in the root of the software repository for the full text of the License.
8+ 
9+import math
10+import unittest
11+ 
12+import torch
13+ 
14+from golden import _compute
15+ 
16+ 
17+class SgdGoldenTest(unittest.TestCase):
18+ def test_nesterov_infinite_learning_rate_preserves_update_sign(self):
19+ # The finite update direction is positive. Multiplying its two terms
20+ # by infinity separately would instead introduce inf + (-inf) = NaN.
21+ for dtype in (torch.float16, torch.bfloat16, torch.float32):
22+ for rate, expected in ((math.inf, -math.inf), (-math.inf, math.inf)):
23+ with self.subTest(dtype=dtype, learning_rate=rate):
24+ values = (-1.0, 10.0, rate, 1.0, -0.01, -10.0)
25+ inputs = [torch.tensor([value], dtype=dtype) for value in values]
26+ outputs = _compute(*inputs, nesterov=True)
27+ self.assertEqual(outputs[0].item(), expected)
28+ self.assertTrue(torch.isfinite(outputs[1]).all())
29+ self.assertEqual(outputs[2].item(), 0.0)
30+ 
31+ def test_nesterov_finite_update(self):
32+ inputs = [torch.tensor([value]) for value in (1.0, 2.0, 0.25, 4.0, 0.5, 1.0)]
33+ outputs = _compute(*inputs, nesterov=True)
34+ self.assertEqual([value.item() for value in outputs], [0.0, 4.0, 0.0])
35+ 
36+ 
37+if __name__ == "__main__":
38+ unittest.main()
@@ -0,0 +1,7 @@
1+testcase_name,network_name,op_name,input_shapes,input_dtypes,input_formats,output_shapes,output_dtypes,output_formats,input_ori_shapes,input_ori_formats,output_ori_shapes,output_ori_formats,attributes,input_data_ranges,precision_tolerances,absolute_precision,output_inplace_indexes,output_shape_unknown_indexes,is_enabled,remark,soc_series,priority,dump_file_prefix,manual_input_binaries,manual_golden_binaries
2+SGD_st_fp32_defaults_zero_momentum,UNKNOWN,SGD,"((33,), (33,), (1,), (33,), (1,), (33,))","('float32', 'float32', 'float32', 'float32', 'float32', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((33,),)","('float32',)","('ND',)","((33,), (33,), (1,), (33,), (1,), (33,))","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((33,),)","('ND',)",{},"((-2, 2), (-1, 1), (0.125, 0.125), (0.5, 0.5), (0, 0), (1, 1))",,1e-08,"(0,)",(),True,default attributes and zero momentum,,0,,(),()
3+SGD_st_fp16_nesterov,UNKNOWN,SGD,"((17,), (17,), (1,), (17,), (1,), (17,))","('float16', 'float16', 'float16', 'float16', 'float16', 'float16')","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((17,),)","('float16',)","('ND',)","((17,), (17,), (1,), (17,), (1,), (17,))","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((17,),)","('ND',)",{'nesterov': True},"((-2, 2), (-1, 1), (0.125, 0.125), (0.5, 0.5), (0.5, 0.5), (1, 1))",,1e-08,"(0,)",(),True,Nesterov update and unaligned tail,,0,,(),()
4+SGD_st_bf16_dampening_decay,UNKNOWN,SGD,"((2, 3), (2, 3), (1, 1), (2, 3), (1,), (2, 3))","('bfloat16', 'bfloat16', 'bfloat16', 'bfloat16', 'bfloat16', 'bfloat16')","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((2, 3),)","('bfloat16',)","('ND',)","((2, 3), (2, 3), (1, 1), (2, 3), (1,), (2, 3))","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((2, 3),)","('ND',)","{'dampening': 0.125, 'weight_decay': 0.01}","((-2, 2), (-1, 1), (0.125, 0.125), (0.5, 0.5), (0.5, 0.5), (0, 0))",,1e-08,"(0,)",(),True,dampening with weight decay and one-element rank2 rate,,0,,(),()
5+SGD_st_fp32_rank8_first_step,UNKNOWN,SGD,"((1, 2, 1, 2, 1, 2, 1, 2), (1, 2, 1, 2, 1, 2, 1, 2), (1,), (1, 2, 1, 2, 1, 2, 1, 2), (1,), (1, 2, 1, 2, 1, 2, 1, 2))","('float32', 'float32', 'float32', 'float32', 'float32', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((1, 2, 1, 2, 1, 2, 1, 2),)","('float32',)","('ND',)","((1, 2, 1, 2, 1, 2, 1, 2), (1, 2, 1, 2, 1, 2, 1, 2), (1,), (1, 2, 1, 2, 1, 2, 1, 2), (1,), (1, 2, 1, 2, 1, 2, 1, 2))","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((1, 2, 1, 2, 1, 2, 1, 2),)","('ND',)","{'dampening': 0.25, 'weight_decay': 0.125}","((-2, 2), (-1, 1), (0.125, 0.125), (0.5, 0.5), (0.5, 0.5), (1, 1))",,1e-08,"(0,)",(),True,maximum rank and first-step flag,,0,,(),()
6+SGD_st_fp32_tiling_tail,UNKNOWN,SGD,"((3713,), (3713,), (1,), (3713,), (1,), (3713,))","('float32', 'float32', 'float32', 'float32', 'float32', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((3713,),)","('float32',)","('ND',)","((3713,), (3713,), (1,), (3713,), (1,), (3713,))","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((3713,),)","('ND',)","{'dampening': 0.125, 'weight_decay': 0.01}","((-2, 2), (-1, 1), (0.125, 0.125), (0.5, 0.5), (0.5, 0.5), (0, 0))",,1e-08,"(0,)",(),True,small tile boundary regression,,0,,(),()
7+SGD_st_fp16_nesterov_inf,UNKNOWN,SGD,"((3,), (3,), (1,), (3,), (1,), (3,))","('float16', 'float16', 'float16', 'float16', 'float16', 'float16')","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((3,),)","('float16',)","('ND',)","((3,), (3,), (1,), (3,), (1,), (3,))","('ND', 'ND', 'ND', 'ND', 'ND', 'ND')","((3,),)","('ND',)",{'nesterov': True},"((1, 1), (1, 1), (float('inf'), float('inf')), (-4, -4), (0.5, 0.5), (1, 1))",,1e-08,"(0,)",(),True,Nesterov direction sum before Inf learning-rate multiplication,,0,,(),()
@@ -0,0 +1,11 @@
1+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3+# CANN Open Software License Agreement Version 2.0 (the "License").
4+# Please refer to the License for details. You may not use this file except in compliance with the License.
5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7+# See LICENSE in the root of the software repository for the full text of the License.
8+ 
9+if(UT_TEST_ALL OR OP_GRAPH_UT)
10+ add_modules_ut_sources(HOSTNAME ${OP_GRAPH_MODULE_NAME} MODE PRIVATE DIR ${CMAKE_CURRENT_SOURCE_DIR})
11+endif()
@@ -0,0 +1,63 @@
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 <gtest/gtest.h>
12+#include <array>
13+#include "base/context_builder/op_infer_datatype_context_builder.h"
14+ 
15+namespace ops {
16+ge::graphStatus InferDataTypeForSgd(gert::InferDataTypeContext* context);
17+} // namespace ops
18+ 
19+namespace {
20+void CheckSgdDtype(const std::array<ge::DataType, 6>& types, ge::graphStatus expected)
21+{
22+ gert::OpInferDataTypeContextBuilder builder;
23+ builder.OpType("SGD").OpName("sgd_dtype").IONum(6, 1);
24+ for (size_t input = 0; input < types.size(); ++input) {
25+ builder.InputTensorDesc(input, types[input], ge::FORMAT_ND, ge::FORMAT_ND);
26+ }
27+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
28+ auto holder = builder.Build();
29+ ASSERT_NE(holder.GetContext(), nullptr);
30+ ASSERT_EQ(ops::InferDataTypeForSgd(holder.GetContext()), expected);
31+ if (expected == ge::GRAPH_SUCCESS) {
32+ EXPECT_EQ(holder.GetContext()->GetOutputDataType(0), types[0]);
33+ }
34+}
35+} // namespace
36+ 
37+TEST(SgdGraphInfer, PreservesSupportedTypes)
38+{
39+ for (auto dtype : {ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_BF16}) {
40+ std::array<ge::DataType, 6> types;
41+ types.fill(dtype);
42+ CheckSgdDtype(types, ge::GRAPH_SUCCESS);
43+ }
44+}
45+ 
46+TEST(SgdGraphInfer, RejectsEachMismatchedInput)
47+{
48+ for (size_t input = 1; input < 6; ++input) {
49+ std::array<ge::DataType, 6> types;
50+ types.fill(ge::DT_FLOAT);
51+ types[input] = ge::DT_FLOAT16;
52+ CheckSgdDtype(types, ge::GRAPH_FAILED);
53+ }
54+}
55+ 
56+TEST(SgdGraphInfer, RejectsUnsupportedMatchingTypes)
57+{
58+ for (auto dtype : {ge::DT_INT32, ge::DT_BOOL, ge::DT_DOUBLE}) {
59+ std::array<ge::DataType, 6> types;
60+ types.fill(dtype);
61+ CheckSgdDtype(types, ge::GRAPH_FAILED);
62+ }
63+}
@@ -301,8 +301,7 @@ TEST_F(TestSgdTiling, sgd_tiling_reject_empty_tensor)
301}301}
302 302 
303// ── DFX:空 tensor「全空间」枚举 ────────────────────────────────────────────303// ── DFX:空 tensor「全空间」枚举 ────────────────────────────────────────────
304-// changwei-op-dev step5-verify §5.4 要求:空 tensor = **任意一轴或多轴为 0**,304+// 空Tensor包括任意一轴或多轴为0的情形,逐形态检查拒绝行为。
305-// 必须逐形态真跑分类,不能只验一个维度,结论要写成「仅支持哪个轴为空」。
306// 本算子的契约是【所有形态一律拒绝】(无空进空出语义),故下面每一条都断言 GRAPH_FAILED。305// 本算子的契约是【所有形态一律拒绝】(无空进空出语义),故下面每一条都断言 GRAPH_FAILED。
307// 若将来放开某一轴,本组用例会立刻在该形态上变红,逼迫同步更新契约与文档。306// 若将来放开某一轴,本组用例会立刻在该形态上变红,逼迫同步更新契约与文档。
308//307//
@@ -421,7 +420,7 @@ TEST_F(TestSgdTiling, sgd_tiling_accept_scalar_shape_any_single_element)
421 420 
422TEST_F(TestSgdTiling, sgd_tiling_reject_dtype_mismatch)421TEST_F(TestSgdTiling, sgd_tiling_reject_dtype_mismatch)
423{422{
424- // 9 个张量位必须同 dtype(parameters 为 fp32,其余为 fp16)423+ // 全部输入必须同dtype(parameters为fp32,其余为fp16)。
425 gert::StorageShape param = {{2, 4}, {2, 4}};424 gert::StorageShape param = {{2, 4}, {2, 4}};
426 gert::StorageShape one = {{1}, {1}};425 gert::StorageShape one = {{1}, {1}};
427 ASSERT_EQ(426 ASSERT_EQ(
@@ -437,3 +436,26 @@ TEST_F(TestSgdTiling, sgd_tiling_reject_unsupported_dtype)
437 RunSgdTiling(param, param, one, param, one, param, ge::DT_INT32, ge::DT_INT32, 0.0f, 0.0f, false, nullptr),436 RunSgdTiling(param, param, one, param, one, param, ge::DT_INT32, ge::DT_INT32, 0.0f, 0.0f, false, nullptr),
438 ge::GRAPH_FAILED);437 ge::GRAPH_FAILED);
439}438}
439+ 
440+TEST_F(TestSgdTiling, sgd_tiling_reject_shape_size_overflow)
441+{
442+ // Metadata-only shapes: no tensor storage is allocated for these sizes.
443+ uint64_t key = 0;
444+ EXPECT_EQ(RunSgdTilingNormal({4294967297LL, 4294967297LL}, ge::DT_FLOAT, 0.0f, 0.0f, false, &key),
445+ ge::GRAPH_FAILED);
446+}
447+ 
448+TEST_F(TestSgdTiling, sgd_tiling_reject_overflowing_scalar_shape)
449+{
450+ gert::StorageShape param = {{2, 4}, {2, 4}};
451+ const int64_t maxDim = std::numeric_limits<int64_t>::max();
452+ // Unchecked signed multiplication can wrap this element count to one.
453+ gert::StorageShape invalidScalar = {{maxDim, maxDim}, {maxDim, maxDim}};
454+ gert::StorageShape one = {{1}, {1}};
455+ EXPECT_EQ(RunSgdTiling(param, param, invalidScalar, param, one, param, ge::DT_FLOAT, ge::DT_FLOAT, 0.0f, 0.0f,
456+ false, nullptr),
457+ ge::GRAPH_FAILED);
458+ EXPECT_EQ(RunSgdTiling(param, param, one, param, invalidScalar, param, ge::DT_FLOAT, ge::DT_FLOAT, 0.0f, 0.0f,
459+ false, nullptr),
460+ ge::GRAPH_FAILED);
461+}
@@ -24,6 +24,7 @@
24 */24 */
25 25 
26#include <gtest/gtest.h> // NOLINT26#include <gtest/gtest.h> // NOLINT
27+#include <array>
27#include <iostream>28#include <iostream>
28#include <limits>29#include <limits>
29#include <vector>30#include <vector>
@@ -165,3 +166,72 @@ TEST_F(SGD, sgd_infershape_nonnegative_weight_decay_boundaries)
165 EXPECT_EQ(RunSgdInferShape(bigShape, scalarShape, 0.0f, weightDecay, false), ge::GRAPH_SUCCESS);166 EXPECT_EQ(RunSgdInferShape(bigShape, scalarShape, 0.0f, weightDecay, false), ge::GRAPH_SUCCESS);
166 }167 }
167}168}
169+ 
170+namespace {
171+using SgdShapes = std::array<gert::StorageShape, SGD_INPUT_NUM>;
172+ 
173+SgdShapes NormalSgdShapes()
174+{
175+ gert::StorageShape tensor = {{2, 3}, {2, 3}};
176+ gert::StorageShape scalar = {{1}, {1}};
177+ return {tensor, tensor, scalar, tensor, scalar, tensor};
178+}
179+ 
180+ge::graphStatus RunSgdShapes(SgdShapes& shapes)
181+{
182+ auto inferShapeFunc = gert::OpImplRegistry::GetInstance().GetOpImpl("SGD")->infer_shape;
183+ gert::StorageShape output = {{}, {}};
184+ auto holder = gert::InferShapeContextFaker()
185+ .NodeIoNum(SGD_INPUT_NUM, SGD_OUTPUT_NUM)
186+ .IrInstanceNum({1, 1, 1, 1, 1, 1})
187+ .InputShapes({&shapes[0], &shapes[1], &shapes[2], &shapes[3], &shapes[4], &shapes[5]})
188+ .OutputShapes({&output})
189+ .NodeAttrs({{"dampening", Ops::NN::AnyValue::CreateFrom<float>(0.0f)},
190+ {"weight_decay", Ops::NN::AnyValue::CreateFrom<float>(0.0f)},
191+ {"nesterov", Ops::NN::AnyValue::CreateFrom<bool>(false)}})
192+ .Build();
193+ return inferShapeFunc(holder.GetContext<gert::InferShapeContext>());
194+}
195+} // namespace
196+ 
197+TEST_F(SGD, sgd_infershape_rejects_each_empty_axis)
198+{
199+ for (gert::StorageShape empty : {gert::StorageShape{{0, 3}, {0, 3}}, gert::StorageShape{{3, 0}, {3, 0}}}) {
200+ auto shapes = NormalSgdShapes();
201+ for (size_t input : {0U, 1U, 3U, 5U}) {
202+ shapes[input] = empty;
203+ }
204+ EXPECT_EQ(RunSgdShapes(shapes), ge::GRAPH_FAILED);
205+ }
206+}
207+ 
208+TEST_F(SGD, sgd_infershape_rejects_non_scalar_learning_rate_and_momentum)
209+{
210+ for (size_t input : {2U, 4U}) {
211+ auto shapes = NormalSgdShapes();
212+ shapes[input] = {{1, 2}, {1, 2}};
213+ EXPECT_EQ(RunSgdShapes(shapes), ge::GRAPH_FAILED);
214+ }
215+}
216+ 
217+TEST_F(SGD, sgd_infershape_rejects_shape_and_rank_mismatch)
218+{
219+ for (size_t input : {1U, 3U, 5U}) {
220+ for (gert::StorageShape mismatch : {gert::StorageShape{{3, 2}, {3, 2}}, gert::StorageShape{{6}, {6}}}) {
221+ auto shapes = NormalSgdShapes();
222+ shapes[input] = mismatch;
223+ EXPECT_EQ(RunSgdShapes(shapes), ge::GRAPH_FAILED);
224+ }
225+ }
226+}
227+ 
228+TEST_F(SGD, sgd_infershape_preserves_partial_and_unknown_rank_inputs)
229+{
230+ auto shapes = NormalSgdShapes();
231+ shapes[0] = {{2, -1}, {2, -1}};
232+ shapes[2] = {{-1, 1}, {-1, 1}};
233+ shapes[4] = {{-2}, {-2}};
234+ EXPECT_EQ(RunSgdShapes(shapes), ge::GRAPH_SUCCESS);
235+ shapes[0] = {{-2}, {-2}};
236+ EXPECT_EQ(RunSgdShapes(shapes), ge::GRAPH_SUCCESS);
237+}