Merged
fix(example): preserve ND outputs for INTrainingReduceV2 on Ascend950 #14400
zsq_888Add on 9 days ago
fix(example): preserve ND outputs for INTrainingReduceV2 on Ascend950 #14400
Merged
Total: 1 files changed+32-1
| @@ -33,6 +33,9 @@ using namespace ge; | |||
| 33 | using std::string; | 33 | using std::string; |
| 34 | using std::vector; | 34 | using std::vector; |
| 35 | 35 | ||
| 36 | +// Both outputs are always FP32, including when the input is FP16. | ||
| 37 | +constexpr DataType OUTPUT_DTYPE = DT_FLOAT; | ||
| 38 | + | ||
| 36 | string GetTime() | 39 | string GetTime() |
| 37 | { | 40 | { |
| 38 | time_t timep; | 41 | time_t timep; |
| @@ -145,7 +148,7 @@ bool CheckOutputs(const ExampleOptions& options, const vector<Tensor>& outputs) | |||
| 145 | for (size_t outputIndex = 0; outputIndex < outputs.size(); ++outputIndex) { | 148 | for (size_t outputIndex = 0; outputIndex < outputs.size(); ++outputIndex) { |
| 146 | const auto desc = outputs[outputIndex].GetTensorDesc(); | 149 | const auto desc = outputs[outputIndex].GetTensorDesc(); |
| 147 | const auto& expected = outputIndex == 0 ? sums : squares; | 150 | const auto& expected = outputIndex == 0 ? sums : squares; |
| 148 | - if (desc.GetDataType() != DT_FLOAT || desc.GetFormat() != FORMAT_ND || | 151 | + if (desc.GetDataType() != OUTPUT_DTYPE || desc.GetFormat() != FORMAT_ND || |
| 149 | desc.GetShape().GetDims() != expectedShape || outputs[outputIndex].GetData() == nullptr || | 152 | desc.GetShape().GetDims() != expectedShape || outputs[outputIndex].GetData() == nullptr || |
| 150 | outputs[outputIndex].GetSize() != expected.size() * sizeof(float)) { | 153 | outputs[outputIndex].GetSize() != expected.size() * sizeof(float)) { |
| 151 | printf("Output %zu has an unexpected descriptor or buffer size\n", outputIndex); | 154 | printf("Output %zu has an unexpected descriptor or buffer size\n", outputIndex); |
| @@ -165,8 +168,23 @@ bool CheckOutputs(const ExampleOptions& options, const vector<Tensor>& outputs) | |||
| 165 | return true; | 168 | return true; |
| 166 | } | 169 | } |
| 167 | 170 | ||
| 171 | +ge::graphStatus InferExampleOutputFormats(ge::Operator& op) | ||
| 172 | +{ | ||
| 173 | + // Keep the declared ND outputs through GE's format propagation stage. | ||
| 174 | + for (uint32_t index = 0; index < 2; ++index) { | ||
| 175 | + auto desc = op.GetOutputDesc(index); | ||
| 176 | + desc.SetFormat(FORMAT_ND); | ||
| 177 | + desc.SetOriginFormat(FORMAT_ND); | ||
| 178 | + if (op.UpdateOutputDesc(index, desc) != ge::GRAPH_SUCCESS) { | ||
| 179 | + return ge::GRAPH_FAILED; | ||
| 180 | + } | ||
| 181 | + } | ||
| 182 | + return ge::GRAPH_SUCCESS; | ||
| 183 | +} | ||
| 184 | + | ||
| 168 | int32_t RunExample(const ExampleOptions& options) | 185 | int32_t RunExample(const ExampleOptions& options) |
| 169 | { | 186 | { |
| 187 | + const ge::InferFormatFuncRegister outputFormatRegistration("INTrainingReduceV2", InferExampleOutputFormats); | ||
| 170 | Graph graph("tc_ge_irrun_in_training_reduce_v2"); | 188 | Graph graph("tc_ge_irrun_in_training_reduce_v2"); |
| 171 | auto xData = op::Data("placeholder_x").set_attr_index(0); | 189 | auto xData = op::Data("placeholder_x").set_attr_index(0); |
| 172 | TensorDesc xDesc(ge::Shape(options.shape), options.format, options.dtype); | 190 | TensorDesc xDesc(ge::Shape(options.shape), options.format, options.dtype); |
| @@ -179,6 +197,19 @@ int32_t RunExample(const ExampleOptions& options) | |||
| 179 | } | 197 | } |
| 180 | auto reduceOp = op::INTrainingReduceV2("in_training_reduce_v2"); | 198 | auto reduceOp = op::INTrainingReduceV2("in_training_reduce_v2"); |
| 181 | reduceOp.set_input_x(xData); | 199 | reduceOp.set_input_x(xData); |
| 200 | + // Both outputs use ND storage, independently of the input's channel layout. | ||
| 201 | + const bool channelLast = options.format == FORMAT_NHWC || options.format == FORMAT_NDHWC; | ||
| 202 | + const size_t channelAxis = channelLast ? options.shape.size() - 1 : 1; | ||
| 203 | + vector<int64_t> outputShape(options.shape.size(), 1); | ||
| 204 | + outputShape[0] = options.shape[0]; | ||
| 205 | + outputShape[channelAxis] = options.shape[channelAxis]; | ||
| 206 | + TensorDesc outputDesc(ge::Shape(outputShape), FORMAT_ND, OUTPUT_DTYPE); | ||
| 207 | + outputDesc.SetOriginFormat(FORMAT_ND); | ||
| 208 | + if (reduceOp.update_output_desc_sum(outputDesc) != ge::GRAPH_SUCCESS || | ||
| 209 | + reduceOp.update_output_desc_square_sum(outputDesc) != ge::GRAPH_SUCCESS) { | ||
| 210 | + printf("Set output descriptors failed\n"); | ||
| 211 | + return FAILED; | ||
| 212 | + } | ||
| 182 | const vector<Operator> inputs = {xData}; | 213 | const vector<Operator> inputs = {xData}; |
| 183 | const vector<Operator> outputs = {reduceOp}; | 214 | const vector<Operator> outputs = {reduceOp}; |
| 184 | graph.SetInputs(inputs).SetOutputs(outputs); | 215 | graph.SetInputs(inputs).SetOutputs(outputs); |
🟡 Medium Priority
变更行 203 用
TensorDesc(ge::Shape(outputShape), FORMAT_ND, DT_FLOAT)把 sum/square_sum 两个输出的 dtype 硬编码为 DT_FLOAT,而同一函数第 187-188 行创建输入描述符时按options.dtype走,且 main/ParseOptions(56-62 行)明确支持fp16参数、GenFloatData 也真的按 DT_FLOAT16 填数据。改动前后行为差异:改动前输出 dtype 由 GE 的 infer-dtype 阶段推导;改动后被显式覆盖为 DT_FLOAT,与输入 dtype 解耦。
触发条件:以
./test_geir_in_training_reduce_v2 fp16运行。失败模式(取决于算子 fp16 情况下输出 dtype 的真实语义):expected.size() * sizeof(float)长度校验都会“通过”,但 156-158 行按 float32 解释 float16 的原始字节,必然与 expected 不等,用例稳定报 FAIL(错值被当成真实测试失败,掩盖真实原因);建议:输出 TensorDesc 的 dtype 改为按约定推导(例如
const DataType outDtype = DT_FLOAT;并注释“该算子输出恒为 fp32,与输入 dtype 无关”),或直接使用 GE 推导的 dtype;同时与 CheckOutputs 的 dtype 校验保持同一来源,避免两处硬编码各写一份。