已合并
AvgPool3D和AvgPool3DGrad两个算子增加infer Datatype #8395
duxinlei创建于 8月7日
AvgPool3D和AvgPool3DGrad两个算子增加infer Datatype #8395
已合并
duxinlei创建于 8月7日
8 个文件变更+914-0
@@ -0,0 +1,285 @@
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_geir_avg_pool3_d.cpp
13+ * \brief AvgPool3D GE IR test example
14+ */
15+ 
16+#include <ctime>
17+#include <cstdio>
18+#include <fstream>
19+#include <iostream>
20+#include <map>
21+#include <numeric>
22+#include <stdint.h>
23+#include <string>
24+#include <string.h>
25+#include <vector>
26+ 
27+#include "assert.h"
28+ 
29+#include "ge_api.h"
30+#include "ge_api_types.h"
31+#include "ge_error_codes.h"
32+#include "ge_ir_build.h"
33+#include "graph.h"
34+#include "graph/operator.h"
35+#include "graph/operator_reg.h"
36+#include "tensor.h"
37+#include "types.h"
38+#include "array_ops.h"
39+ 
40+#include "nn_other.h"
41+#include "../../op_graph/avg_pool3_d_proto.h"
42+ 
43+#define FAILED -1
44+#define SUCCESS 0
45+ 
46+using namespace ge;
47+using std::map;
48+using std::string;
49+using std::vector;
50+ 
51+string GetTime()
52+{
53+ time_t timep;
54+ time(&timep);
55+ char tmp[64];
56+ strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S,000", localtime(&timep));
57+ return tmp;
58+}
59+ 
60+uint32_t GetDataTypeSize(DataType dt)
61+{
62+ uint32_t oneByte = 1;
63+ uint32_t twoByte = 2;
64+ uint32_t fourByte = 4;
65+ uint32_t eightByte = 8;
66+ 
67+ if (dt == ge::DT_FLOAT) {
68+ return fourByte;
69+ } else if (dt == ge::DT_FLOAT16 || dt == ge::DT_BF16 || dt == ge::DT_INT16 || dt == ge::DT_UINT16) {
70+ return twoByte;
71+ } else if (dt == ge::DT_INT32 || dt == ge::DT_UINT32) {
72+ return fourByte;
73+ } else if (dt == ge::DT_INT64 || dt == ge::DT_UINT64 || dt == ge::DT_DOUBLE) {
74+ return eightByte;
75+ }
76+ return oneByte;
77+}
78+ 
79+template <typename T>
80+int32_t GenTensorData(const vector<int64_t>& shapes, Tensor& input_tensor, TensorDesc& input_tensor_desc,
81+ const vector<T>& values)
82+{
83+ input_tensor_desc.SetRealDimCnt(shapes.size());
84+ size_t size = 1;
85+ for (auto dim : shapes) {
86+ size *= dim;
87+ }
88+ if (size != values.size()) {
89+ printf("%s - ERROR - [XIR]: GenTensorData size mismatch, expected %zu, got %zu\n", GetTime().c_str(), size,
90+ values.size());
91+ return FAILED;
92+ }
93+ auto* data = new (std::nothrow) T[size];
94+ if (data == nullptr) {
95+ return FAILED;
96+ }
97+ for (size_t i = 0; i < size; ++i) {
98+ data[i] = values[i];
99+ }
100+ input_tensor = Tensor(input_tensor_desc, reinterpret_cast<uint8_t*>(data), size * sizeof(T));
101+ return SUCCESS;
102+}
103+ 
104+int32_t WriteDataToFile(const string& bin_file, uint64_t data_size, uint8_t* input_data)
105+{
106+ FILE* fp = fopen(bin_file.c_str(), "w");
107+ if (fp == nullptr) {
108+ printf("%s - ERROR - [XIR]: Failed to open file %s\n", GetTime().c_str(), bin_file.c_str());
109+ return FAILED;
110+ }
111+ fwrite(input_data, sizeof(uint8_t), data_size, fp);
112+ fclose(fp);
113+ return SUCCESS;
114+}
115+ 
116+#define ADD_INPUT(inputIndex, inputName, inputDtype, inputShape, inputValues) \
117+ vector<int64_t> placeholder##inputIndex##_shape = inputShape; \
118+ auto placeholder##inputIndex = op::Data("placeholder" + inputIndex).set_attr_index((inputIndex) - 1); \
119+ TensorDesc placeholder##inputIndex##_desc = TensorDesc(ge::Shape(placeholder##inputIndex##_shape), FORMAT_NDHWC, \
120+ inputDtype); \
121+ placeholder##inputIndex##_desc.SetPlacement(ge::kPlacementHost); \
122+ placeholder##inputIndex##_desc.SetFormat(FORMAT_NDHWC); \
123+ placeholder##inputIndex##_desc.SetOriginFormat(FORMAT_NDHWC); \
124+ Tensor tensor_placeholder##inputIndex; \
125+ ret = GenTensorData(placeholder##inputIndex##_shape, tensor_placeholder##inputIndex, \
126+ placeholder##inputIndex##_desc, inputValues); \
127+ if (ret != SUCCESS) { \
128+ printf("%s - ERROR - [XIR]: Generate input data failed\n", GetTime().c_str()); \
129+ return FAILED; \
130+ } \
131+ placeholder##inputIndex.update_input_desc_x(placeholder##inputIndex##_desc); \
132+ placeholder##inputIndex.update_output_desc_y(placeholder##inputIndex##_desc); \
133+ input.push_back(tensor_placeholder##inputIndex); \
134+ graph.AddOp(placeholder##inputIndex); \
135+ avg_pool3_d.set_input_##inputName(placeholder##inputIndex); \
136+ inputs.push_back(placeholder##inputIndex)
137+ 
138+#define ADD_INPUT_ATTR(attrName, attrValue) avg_pool3_d.set_attr_##attrName(attrValue)
139+ 
140+#define ADD_OUTPUT(outputIndex, outputName, outputDtype, outputShape) \
141+ TensorDesc outputName##outputIndex##_desc_ = TensorDesc(ge::Shape(outputShape), FORMAT_NDHWC, outputDtype); \
142+ outputName##outputIndex##_desc_.SetFormat(FORMAT_NDHWC); \
143+ outputName##outputIndex##_desc_.SetOriginFormat(FORMAT_NDHWC); \
144+ avg_pool3_d.update_output_desc_##outputName(outputName##outputIndex##_desc_)
145+ 
146+int CreateOppInGraph(std::vector<ge::Tensor>& input, std::vector<Operator>& inputs, std::vector<Operator>& outputs,
147+ Graph& graph)
148+{
149+ Status ret = SUCCESS;
150+ auto avg_pool3_d = op::AvgPool3D("avg_pool3_d");
151+ 
152+ // Input: 5D tensor [N, D, H, W, C] in NDHWC format (default, most widely supported)
153+ // x_shape: N=1, D=4, H=4, W=4, C=1
154+ std::vector<int64_t> x_shape = {1, 4, 4, 4, 1};
155+ // y_shape: N=1, D_out=2, H_out=2, W_out=2, C=1
156+ // D_out = (D - ksize_d) / stride_d + 1 = (4 - 2) / 2 + 1 = 2
157+ // H_out = (H - ksize_h) / stride_h + 1 = (4 - 2) / 2 + 1 = 2
158+ // W_out = (W - ksize_w) / stride_w + 1 = (4 - 2) / 2 + 1 = 2
159+ std::vector<int64_t> y_shape = {1, 2, 2, 2, 1};
160+ 
161+ // Generate input data with value 1.0 (1*4*4*4*1 = 64 elements)
162+ std::vector<float> x_data(64, 1.0f);
163+ 
164+ ADD_INPUT(1, x, DT_FLOAT, x_shape, x_data);
165+ 
166+ // Set attributes (ksize/strides length 5 corresponds to [N, D, H, W, C] for NDHWC)
167+ // N and C dimensions must have ksize=1, stride=1 (no pooling on N/C)
168+ ADD_INPUT_ATTR(ksize, std::vector<int64_t>({1, 2, 2, 2, 1}));
169+ ADD_INPUT_ATTR(strides, std::vector<int64_t>({1, 2, 2, 2, 1}));
170+ ADD_INPUT_ATTR(pads, std::vector<int64_t>({0, 0, 0}));
171+ ADD_INPUT_ATTR(ceil_mode, false);
172+ ADD_INPUT_ATTR(count_include_pad, true);
173+ ADD_INPUT_ATTR(divisor_override, static_cast<int64_t>(0));
174+ ADD_INPUT_ATTR(data_format, "NDHWC");
175+ 
176+ ADD_OUTPUT(1, y, DT_FLOAT, y_shape);
177+ 
178+ outputs.push_back(avg_pool3_d);
179+ 
180+ return SUCCESS;
181+}
182+ 
183+int main(int argc, char* argv[])
184+{
185+ (void)argc;
186+ (void)argv;
187+ 
188+ const char* graph_name = "tc_ge_irrun_test_avg_pool3_d";
189+ Graph graph(graph_name);
190+ std::vector<ge::Tensor> input;
191+ 
192+ printf("%s - INFO - [XIR]: Start to initialize ge using ge global options\n", GetTime().c_str());
193+ std::map<AscendString, AscendString> global_options = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "1"}};
194+ Status ret = ge::GEInitialize(global_options);
195+ if (ret != SUCCESS) {
196+ printf("%s - ERROR - [XIR]: Initialize ge using ge global options failed\n", GetTime().c_str());
197+ return FAILED;
198+ }
199+ printf("%s - INFO - [XIR]: Initialize ge using ge global options success\n", GetTime().c_str());
200+ 
201+ std::vector<Operator> inputs{};
202+ std::vector<Operator> outputs{};
203+ 
204+ ret = CreateOppInGraph(input, inputs, outputs, graph);
205+ if (ret != SUCCESS) {
206+ printf("%s - ERROR - [XIR]: Create graph failed\n", GetTime().c_str());
207+ return FAILED;
208+ }
209+ 
210+ if (!inputs.empty() && !outputs.empty()) {
211+ graph.SetInputs(inputs).SetOutputs(outputs);
212+ }
213+ 
214+ std::map<AscendString, AscendString> build_options = {};
215+ printf("%s - INFO - [XIR]: Start to create ir session using build options\n", GetTime().c_str());
216+ ge::Session* session = new Session(build_options);
217+ 
218+ if (session == nullptr) {
219+ printf("%s - ERROR - [XIR]: Create ir session using build options failed\n", GetTime().c_str());
220+ return FAILED;
221+ }
222+ printf("%s - INFO - [XIR]: Create ir session using build options success\n", GetTime().c_str());
223+ printf("%s - INFO - [XIR]: Start to add compute graph to ir session\n", GetTime().c_str());
224+ 
225+ std::map<AscendString, AscendString> graph_options = {};
226+ uint32_t graph_id = 0;
227+ ret = session->AddGraph(graph_id, graph, graph_options);
228+ 
229+ printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());
230+ printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());
231+ std::string file_path = "./dump";
232+ aclgrphDumpGraph(graph, file_path.c_str(), file_path.length());
233+ printf("%s - INFO - [XIR]: Start to run ir compute graph\n", GetTime().c_str());
234+ std::vector<ge::Tensor> output;
235+ ret = session->RunGraph(graph_id, input, output);
236+ if (ret != SUCCESS) {
237+ printf("%s - ERROR - [XIR]: Run graph failed\n", GetTime().c_str());
238+ delete session;
239+ GEFinalize();
240+ return FAILED;
241+ }
242+ printf("%s - INFO - [XIR]: Session run ir compute graph success\n", GetTime().c_str());
243+ 
244+ int input_num = input.size();
245+ for (int i = 0; i < input_num; i++) {
246+ std::cout << "input " << i << " dtype : " << input[i].GetTensorDesc().GetDataType() << std::endl;
247+ string input_file = "./tc_ge_irrun_test_0008_npu_input_" + std::to_string(i) + ".bin";
248+ uint8_t* input_data_i = input[i].GetData();
249+ int64_t input_shape = input[i].GetTensorDesc().GetShape().GetShapeSize();
250+ std::cout << "this is " << i << "th input, input shape size =" << input_shape << std::endl;
251+ uint32_t data_size = input_shape * GetDataTypeSize(input[i].GetTensorDesc().GetDataType());
252+ WriteDataToFile((const char*)input_file.c_str(), data_size, input_data_i);
253+ }
254+ 
255+ int output_num = output.size();
256+ for (int i = 0; i < output_num; i++) {
257+ std::cout << "output " << i << " dtype : " << output[i].GetTensorDesc().GetDataType() << std::endl;
258+ string output_file = "./tc_ge_irrun_test_0008_npu_output_" + std::to_string(i) + ".bin";
259+ uint8_t* output_data_i = output[i].GetData();
260+ int64_t output_shape = output[i].GetTensorDesc().GetShape().GetShapeSize();
261+ std::cout << "this is " << i << "th output, output shape size =" << output_shape << std::endl;
262+ uint32_t data_size = output_shape * GetDataTypeSize(output[i].GetTensorDesc().GetDataType());
263+ WriteDataToFile((const char*)output_file.c_str(), data_size, output_data_i);
264+ float* resultData = (float*)output_data_i;
265+ for (int64_t j = 0; j < output_shape; j++) {
266+ printf("result[%ld] is: %f\n", j, resultData[j]);
267+ }
268+ }
269+ 
270+ ge::AscendString error_msg = ge::GEGetErrorMsgV2();
271+ std::string error_str(error_msg.GetString());
272+ std::cout << "Error message: " << error_str << std::endl;
273+ ge::AscendString warning_msg = ge::GEGetWarningMsgV2();
274+ std::string warning_str(warning_msg.GetString());
275+ std::cout << "Warning message: " << warning_str << std::endl;
276+ printf("%s - INFO - [XIR]: Start to finalize ir graph session\n", GetTime().c_str());
277+ delete session;
278+ ret = ge::GEFinalize();
279+ if (ret != SUCCESS) {
280+ printf("%s - ERROR - [XIR]: Finalize ir graph session failed\n", GetTime().c_str());
281+ return FAILED;
282+ }
283+ printf("%s - INFO - [XIR]: Finalize ir graph session success\n", GetTime().c_str());
284+ return SUCCESS;
285+}
@@ -0,0 +1,62 @@
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 avg_pool3_d_graph_infer.cpp
13+ * \brief Data type inference implementation for AvgPool3D.
14+ */
15+ 
16+#include <cstddef>
17+ 
18+#include "log/log.h"
19+#include "register/op_impl_registry.h"
20+ 
21+namespace ops {
22+namespace {
23+constexpr size_t INPUT_X_INDEX = 0;
24+constexpr size_t OUTPUT_Y_INDEX = 0;
25+ 
26+bool IsSupportedDataType(ge::DataType dataType)
27+{
28+ switch (dataType) {
29+ case ge::DT_FLOAT:
30+ case ge::DT_FLOAT16:
31+ case ge::DT_BF16:
32+ return true;
33+ default:
34+ return false;
35+ }
36+}
37+} // namespace
38+ 
39+ge::graphStatus InferDataTypeAvgPool3D(gert::InferDataTypeContext* context)
40+{
41+ if (context == nullptr) {
42+ return ge::GRAPH_FAILED;
43+ }
44+ 
45+ OP_LOGD(context->GetNodeName(), "Begin InferDataTypeAvgPool3D.");
46+ 
47+ const ge::DataType inputDtype = context->GetInputDataType(INPUT_X_INDEX);
48+ 
49+ if (!IsSupportedDataType(inputDtype)) {
50+ OP_LOGE(context->GetNodeName(), "x must use a dtype supported by AvgPool3D.");
51+ return ge::GRAPH_FAILED;
52+ }
53+ 
54+ context->SetOutputDataType(OUTPUT_Y_INDEX, inputDtype);
55+ 
56+ OP_LOGD(context->GetNodeName(), "End InferDataTypeAvgPool3D.");
57+ return ge::GRAPH_SUCCESS;
58+}
59+ 
60+IMPL_OP(AvgPool3D).InferDataType(InferDataTypeAvgPool3D);
61+ 
62+} // namespace ops
@@ -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,77 @@
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 "op_infer_datatype_context_builder.h"
13+ 
14+namespace ops {
15+ge::graphStatus InferDataTypeAvgPool3D(gert::InferDataTypeContext* context);
16+}
17+ 
18+TEST(AvgPool3DGraphInfer, InferDataTypeFP16)
19+{
20+ gert::OpInferDataTypeContextBuilder builder;
21+ builder.OpType("AvgPool3D").OpName("AvgPool3D");
22+ builder.IONum(1, 1);
23+ builder.InputTensorDesc(0, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_ND);
24+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
25+ auto holder = builder.Build();
26+ auto* context = holder.GetContext();
27+ ASSERT_NE(context, nullptr);
28+ 
29+ ASSERT_EQ(ops::InferDataTypeAvgPool3D(context), ge::GRAPH_SUCCESS);
30+ EXPECT_EQ(context->GetOutputDataType(0), ge::DT_FLOAT16);
31+}
32+ 
33+TEST(AvgPool3DGraphInfer, InferDataTypeFP32)
34+{
35+ gert::OpInferDataTypeContextBuilder builder;
36+ builder.OpType("AvgPool3D").OpName("AvgPool3D");
37+ builder.IONum(1, 1);
38+ builder.InputTensorDesc(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND);
39+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
40+ auto holder = builder.Build();
41+ auto* context = holder.GetContext();
42+ ASSERT_NE(context, nullptr);
43+ 
44+ ASSERT_EQ(ops::InferDataTypeAvgPool3D(context), ge::GRAPH_SUCCESS);
45+ EXPECT_EQ(context->GetOutputDataType(0), ge::DT_FLOAT);
46+}
47+ 
48+TEST(AvgPool3DGraphInfer, InferDataTypeBF16)
49+{
50+ gert::OpInferDataTypeContextBuilder builder;
51+ builder.OpType("AvgPool3D").OpName("AvgPool3D");
52+ builder.IONum(1, 1);
53+ builder.InputTensorDesc(0, ge::DT_BF16, ge::FORMAT_ND, ge::FORMAT_ND);
54+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
55+ auto holder = builder.Build();
56+ auto* context = holder.GetContext();
57+ ASSERT_NE(context, nullptr);
58+ 
59+ ASSERT_EQ(ops::InferDataTypeAvgPool3D(context), ge::GRAPH_SUCCESS);
60+ EXPECT_EQ(context->GetOutputDataType(0), ge::DT_BF16);
61+}
62+ 
63+TEST(AvgPool3DGraphInfer, InferDataTypeUnsupportedDtype)
64+{
65+ gert::OpInferDataTypeContextBuilder builder;
66+ builder.OpType("AvgPool3D").OpName("AvgPool3D");
67+ builder.IONum(1, 1);
68+ builder.InputTensorDesc(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND);
69+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
70+ auto holder = builder.Build();
71+ auto* context = holder.GetContext();
72+ ASSERT_NE(context, nullptr);
73+ 
74+ EXPECT_EQ(ops::InferDataTypeAvgPool3D(context), ge::GRAPH_FAILED);
75+}
76+ 
77+TEST(AvgPool3DGraphInfer, RejectsNullContext) { EXPECT_EQ(ops::InferDataTypeAvgPool3D(nullptr), ge::GRAPH_FAILED); }
@@ -0,0 +1,322 @@
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_geir_avg_pool3_d_grad.cpp
13+ * \brief AvgPool3DGrad GE IR test example
14+ */
15+ 
16+#include <ctime>
17+#include <cstdio>
18+#include <fstream>
19+#include <iostream>
20+#include <map>
21+#include <numeric>
22+#include <stdint.h>
23+#include <string>
24+#include <string.h>
25+#include <vector>
26+ 
27+#include "assert.h"
28+ 
29+#include "ge_api.h"
30+#include "ge_api_types.h"
31+#include "ge_error_codes.h"
32+#include "ge_ir_build.h"
33+#include "graph.h"
34+#include "graph/operator.h"
35+#include "graph/operator_reg.h"
36+#include "tensor.h"
37+#include "types.h"
38+#include "array_ops.h"
39+ 
40+#include "nn_other.h"
41+#include "../../op_graph/avg_pool3_d_grad_proto.h"
42+ 
43+#define FAILED -1
44+#define SUCCESS 0
45+ 
46+using namespace ge;
47+using std::map;
48+using std::string;
49+using std::vector;
50+ 
51+string GetTime()
52+{
53+ time_t timep;
54+ time(&timep);
55+ char tmp[64];
56+ strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S,000", localtime(&timep));
57+ return tmp;
58+}
59+ 
60+uint32_t GetDataTypeSize(DataType dt)
61+{
62+ uint32_t oneByte = 1;
63+ uint32_t twoByte = 2;
64+ uint32_t fourByte = 4;
65+ uint32_t eightByte = 8;
66+ 
67+ if (dt == ge::DT_FLOAT) {
68+ return fourByte;
69+ } else if (dt == ge::DT_FLOAT16 || dt == ge::DT_BF16 || dt == ge::DT_INT16 || dt == ge::DT_UINT16) {
70+ return twoByte;
71+ } else if (dt == ge::DT_INT32 || dt == ge::DT_UINT32) {
72+ return fourByte;
73+ } else if (dt == ge::DT_INT64 || dt == ge::DT_UINT64 || dt == ge::DT_DOUBLE) {
74+ return eightByte;
75+ }
76+ return oneByte;
77+}
78+ 
79+template <typename T>
80+int32_t GenTensorData(const vector<int64_t>& shapes, Tensor& input_tensor, TensorDesc& input_tensor_desc,
81+ const vector<T>& values)
82+{
83+ input_tensor_desc.SetRealDimCnt(shapes.size());
84+ size_t size = 1;
85+ for (auto dim : shapes) {
86+ size *= dim;
87+ }
88+ if (size != values.size()) {
89+ printf("%s - ERROR - [XIR]: GenTensorData size mismatch, expected %zu, got %zu\n", GetTime().c_str(), size,
90+ values.size());
91+ return FAILED;
92+ }
93+ auto* data = new (std::nothrow) T[size];
94+ if (data == nullptr) {
95+ return FAILED;
96+ }
97+ for (size_t i = 0; i < size; ++i) {
98+ data[i] = values[i];
99+ }
100+ input_tensor = Tensor(input_tensor_desc, reinterpret_cast<uint8_t*>(data), size * sizeof(T));
101+ return SUCCESS;
102+}
103+ 
104+template <typename T>
105+int32_t GenOnesData(vector<int64_t> shapes, Tensor& input_tensor, TensorDesc& input_tensor_desc, DataType data_type,
106+ const vector<T>& values)
107+{
108+ input_tensor_desc.SetRealDimCnt(shapes.size());
109+ size_t size = 1;
110+ for (uint32_t i = 0; i < shapes.size(); i++) {
111+ size *= shapes[i];
112+ }
113+ auto* data = new (std::nothrow) T[size];
114+ if (data == nullptr) {
115+ return FAILED;
116+ }
117+ for (size_t i = 0; i < size; ++i) {
118+ data[i] = values[i];
119+ }
120+ input_tensor = Tensor(input_tensor_desc, reinterpret_cast<uint8_t*>(data), size * sizeof(T));
121+ return SUCCESS;
122+}
123+ 
124+int32_t WriteDataToFile(const string& bin_file, uint64_t data_size, uint8_t* input_data)
125+{
126+ FILE* fp = fopen(bin_file.c_str(), "w");
127+ if (fp == nullptr) {
128+ printf("%s - ERROR - [XIR]: Failed to open file %s\n", GetTime().c_str(), bin_file.c_str());
129+ return FAILED;
130+ }
131+ fwrite(input_data, sizeof(uint8_t), data_size, fp);
132+ fclose(fp);
133+ return SUCCESS;
134+}
135+ 
136+#define ADD_INPUT(inputIndex, inputName, inputDtype, inputShape, inputValues) \
137+ vector<int64_t> placeholder##inputIndex##_shape = inputShape; \
138+ auto placeholder##inputIndex = op::Data("placeholder" + inputIndex).set_attr_index((inputIndex) - 1); \
139+ TensorDesc placeholder##inputIndex##_desc = TensorDesc(ge::Shape(placeholder##inputIndex##_shape), FORMAT_ND, \
140+ inputDtype); \
141+ placeholder##inputIndex##_desc.SetPlacement(ge::kPlacementHost); \
142+ placeholder##inputIndex##_desc.SetFormat(FORMAT_ND); \
143+ Tensor tensor_placeholder##inputIndex; \
144+ ret = GenTensorData(placeholder##inputIndex##_shape, tensor_placeholder##inputIndex, \
145+ placeholder##inputIndex##_desc, inputValues); \
146+ if (ret != SUCCESS) { \
147+ printf("%s - ERROR - [XIR]: Generate input data failed\n", GetTime().c_str()); \
148+ return FAILED; \
149+ } \
150+ placeholder##inputIndex.update_input_desc_x(placeholder##inputIndex##_desc); \
151+ placeholder##inputIndex.update_output_desc_y(placeholder##inputIndex##_desc); \
152+ input.push_back(tensor_placeholder##inputIndex); \
153+ graph.AddOp(placeholder##inputIndex); \
154+ avg_pool3_d_grad.set_input_##inputName(placeholder##inputIndex); \
155+ inputs.push_back(placeholder##inputIndex)
156+ 
157+#define ADD_CONST_INPUT(intputIndex, intputName, intputDtype, inputShape, inputValues) \
158+ vector<int64_t> placeholder##intputIndex##_shape = inputShape; \
159+ auto placeholder##intputIndex = op::Const("placeholder" + intputIndex); \
160+ TensorDesc placeholder##intputIndex##_desc = TensorDesc(ge::Shape(placeholder##intputIndex##_shape), FORMAT_ND, \
161+ intputDtype); \
162+ placeholder##intputIndex##_desc.SetPlacement(ge::kPlacementHost); \
163+ placeholder##intputIndex##_desc.SetFormat(FORMAT_ND); \
164+ Tensor tensor_placeholder##intputIndex; \
165+ ret = GenOnesData(placeholder##intputIndex##_shape, tensor_placeholder##intputIndex, \
166+ placeholder##intputIndex##_desc, intputDtype, inputValues); \
167+ if (ret != SUCCESS) { \
168+ printf("%s - ERROR - [XIR]: Generate input data failed\n", GetTime().c_str()); \
169+ return FAILED; \
170+ } \
171+ placeholder##intputIndex.SetAttr("value", tensor_placeholder##intputIndex); \
172+ placeholder##intputIndex.update_output_desc_y(placeholder##intputIndex##_desc); \
173+ graph.AddOp(placeholder##intputIndex); \
174+ avg_pool3_d_grad.set_input_##intputName(placeholder##intputIndex); \
175+ avg_pool3_d_grad.update_input_desc_##intputName(placeholder##intputIndex##_desc); \
176+ inputs.push_back(placeholder##intputIndex)
177+ 
178+#define ADD_INPUT_ATTR(attrName, attrValue) avg_pool3_d_grad.set_attr_##attrName(attrValue)
179+ 
180+#define ADD_OUTPUT(outputIndex, outputName, outputDtype, outputShape) \
181+ TensorDesc outputName##outputIndex##_desc_ = TensorDesc(ge::Shape(outputShape), FORMAT_ND, outputDtype); \
182+ avg_pool3_d_grad.update_output_desc_##outputName(outputName##outputIndex##_desc_)
183+ 
184+int CreateOppInGraph(std::vector<ge::Tensor>& input, std::vector<Operator>& inputs, std::vector<Operator>& outputs,
185+ Graph& graph)
186+{
187+ Status ret = SUCCESS;
188+ auto avg_pool3_d_grad = op::AvgPool3DGrad("avg_pool3_d_grad");
189+ 
190+ // Input 0: orig_input_shape (1D tensor describing original input shape)
191+ std::vector<int64_t> orig_input_shape_shape = {5};
192+ std::vector<int32_t> orig_input_shape_data = {1, 1, 4, 4, 4};
193+ 
194+ // Input 1: grads (5D tensor matching forward output shape)
195+ std::vector<int64_t> grads_shape = {1, 1, 2, 2, 2};
196+ std::vector<float> grads_data(8, 1.0f);
197+ 
198+ // Output: gradient w.r.t. input (5D tensor matching forward input shape)
199+ std::vector<int64_t> y_shape = {1, 1, 4, 4, 4};
200+ 
201+ ADD_CONST_INPUT(1, orig_input_shape, DT_INT32, orig_input_shape_shape, orig_input_shape_data);
202+ ADD_INPUT(2, grads, DT_FLOAT, grads_shape, grads_data);
203+ 
204+ // Set attributes
205+ ADD_INPUT_ATTR(ksize, std::vector<int64_t>({1, 1, 2, 2, 2}));
206+ ADD_INPUT_ATTR(strides, std::vector<int64_t>({1, 1, 2, 2, 2}));
207+ ADD_INPUT_ATTR(pads, std::vector<int64_t>({0, 0, 0}));
208+ ADD_INPUT_ATTR(ceil_mode, false);
209+ ADD_INPUT_ATTR(count_include_pad, true);
210+ ADD_INPUT_ATTR(divisor_override, static_cast<int64_t>(0));
211+ ADD_INPUT_ATTR(data_format, "NCDHW");
212+ 
213+ ADD_OUTPUT(1, output, DT_FLOAT, y_shape);
214+ 
215+ outputs.push_back(avg_pool3_d_grad);
216+ 
217+ return SUCCESS;
218+}
219+ 
220+int main(int argc, char* argv[])
221+{
222+ (void)argc;
223+ (void)argv;
224+ 
225+ const char* graph_name = "tc_ge_irrun_test_avg_pool3_d_grad";
226+ Graph graph(graph_name);
227+ std::vector<ge::Tensor> input;
228+ 
229+ printf("%s - INFO - [XIR]: Start to initialize ge using ge global options\n", GetTime().c_str());
230+ std::map<AscendString, AscendString> global_options = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "1"}};
231+ Status ret = ge::GEInitialize(global_options);
232+ if (ret != SUCCESS) {
233+ printf("%s - ERROR - [XIR]: Initialize ge using ge global options failed\n", GetTime().c_str());
234+ return FAILED;
235+ }
236+ printf("%s - INFO - [XIR]: Initialize ge using ge global options success\n", GetTime().c_str());
237+ 
238+ std::vector<Operator> inputs{};
239+ std::vector<Operator> outputs{};
240+ 
241+ ret = CreateOppInGraph(input, inputs, outputs, graph);
242+ if (ret != SUCCESS) {
243+ printf("%s - ERROR - [XIR]: Create graph failed\n", GetTime().c_str());
244+ return FAILED;
245+ }
246+ 
247+ if (!inputs.empty() && !outputs.empty()) {
248+ graph.SetInputs(inputs).SetOutputs(outputs);
249+ }
250+ 
251+ std::map<AscendString, AscendString> build_options = {};
252+ printf("%s - INFO - [XIR]: Start to create ir session using build options\n", GetTime().c_str());
253+ ge::Session* session = new Session(build_options);
254+ 
255+ if (session == nullptr) {
256+ printf("%s - ERROR - [XIR]: Create ir session using build options failed\n", GetTime().c_str());
257+ return FAILED;
258+ }
259+ printf("%s - INFO - [XIR]: Create ir session using build options success\n", GetTime().c_str());
260+ printf("%s - INFO - [XIR]: Start to add compute graph to ir session\n", GetTime().c_str());
261+ 
262+ std::map<AscendString, AscendString> graph_options = {};
263+ uint32_t graph_id = 0;
264+ ret = session->AddGraph(graph_id, graph, graph_options);
265+ 
266+ printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());
267+ printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());
268+ std::string file_path = "./dump";
269+ aclgrphDumpGraph(graph, file_path.c_str(), file_path.length());
270+ printf("%s - INFO - [XIR]: Start to run ir compute graph\n", GetTime().c_str());
271+ std::vector<ge::Tensor> output;
272+ ret = session->RunGraph(graph_id, input, output);
273+ if (ret != SUCCESS) {
274+ printf("%s - ERROR - [XIR]: Run graph failed\n", GetTime().c_str());
275+ delete session;
276+ GEFinalize();
277+ return FAILED;
278+ }
279+ printf("%s - INFO - [XIR]: Session run ir compute graph success\n", GetTime().c_str());
280+ 
281+ int input_num = input.size();
282+ for (int i = 0; i < input_num; i++) {
283+ std::cout << "input " << i << " dtype : " << input[i].GetTensorDesc().GetDataType() << std::endl;
284+ string input_file = "./tc_ge_irrun_test_0008_npu_input_" + std::to_string(i) + ".bin";
285+ uint8_t* input_data_i = input[i].GetData();
286+ int64_t input_shape = input[i].GetTensorDesc().GetShape().GetShapeSize();
287+ std::cout << "this is " << i << "th input, input shape size =" << input_shape << std::endl;
288+ uint32_t data_size = input_shape * GetDataTypeSize(input[i].GetTensorDesc().GetDataType());
289+ WriteDataToFile((const char*)input_file.c_str(), data_size, input_data_i);
290+ }
291+ 
292+ int output_num = output.size();
293+ for (int i = 0; i < output_num; i++) {
294+ std::cout << "output " << i << " dtype : " << output[i].GetTensorDesc().GetDataType() << std::endl;
295+ string output_file = "./tc_ge_irrun_test_0008_npu_output_" + std::to_string(i) + ".bin";
296+ uint8_t* output_data_i = output[i].GetData();
297+ int64_t output_shape = output[i].GetTensorDesc().GetShape().GetShapeSize();
298+ std::cout << "this is " << i << "th output, output shape size =" << output_shape << std::endl;
299+ uint32_t data_size = output_shape * GetDataTypeSize(output[i].GetTensorDesc().GetDataType());
300+ WriteDataToFile((const char*)output_file.c_str(), data_size, output_data_i);
301+ float* resultData = (float*)output_data_i;
302+ for (int64_t j = 0; j < output_shape; j++) {
303+ printf("result[%ld] is: %f\n", j, resultData[j]);
304+ }
305+ }
306+ 
307+ ge::AscendString error_msg = ge::GEGetErrorMsgV2();
308+ std::string error_str(error_msg.GetString());
309+ std::cout << "Error message: " << error_str << std::endl;
310+ ge::AscendString warning_msg = ge::GEGetWarningMsgV2();
311+ std::string warning_str(warning_msg.GetString());
312+ std::cout << "Warning message: " << warning_str << std::endl;
313+ printf("%s - INFO - [XIR]: Start to finalize ir graph session\n", GetTime().c_str());
314+ delete session;
315+ ret = ge::GEFinalize();
316+ if (ret != SUCCESS) {
317+ printf("%s - ERROR - [XIR]: Finalize ir graph session failed\n", GetTime().c_str());
318+ return FAILED;
319+ }
320+ printf("%s - INFO - [XIR]: Finalize ir graph session success\n", GetTime().c_str());
321+ return SUCCESS;
322+}
@@ -0,0 +1,62 @@
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 avg_pool3_d_grad_graph_infer.cpp
13+ * \brief Data type inference implementation for AvgPool3DGrad.
14+ */
15+ 
16+#include <cstddef>
17+ 
18+#include "log/log.h"
19+#include "register/op_impl_registry.h"
20+ 
21+namespace ops {
22+namespace {
23+constexpr size_t GRADS_INDEX = 1;
24+constexpr size_t OUTPUT_INDEX = 0;
25+ 
26+bool IsSupportedDataType(ge::DataType dataType)
27+{
28+ switch (dataType) {
29+ case ge::DT_FLOAT:
30+ case ge::DT_FLOAT16:
31+ case ge::DT_BF16:
32+ return true;
33+ default:
34+ return false;
35+ }
36+}
37+} // namespace
38+ 
39+ge::graphStatus InferDataTypeAvgPool3DGrad(gert::InferDataTypeContext* context)
40+{
41+ if (context == nullptr) {
42+ return ge::GRAPH_FAILED;
43+ }
44+ 
45+ OP_LOGD(context->GetNodeName(), "Begin InferDataTypeAvgPool3DGrad.");
46+ 
47+ const ge::DataType gradsDtype = context->GetInputDataType(GRADS_INDEX);
48+ 
49+ if (!IsSupportedDataType(gradsDtype)) {
50+ OP_LOGE(context->GetNodeName(), "grads must use a dtype supported by AvgPool3DGrad.");
51+ return ge::GRAPH_FAILED;
52+ }
53+ 
54+ context->SetOutputDataType(OUTPUT_INDEX, gradsDtype);
55+ 
56+ OP_LOGD(context->GetNodeName(), "End InferDataTypeAvgPool3DGrad.");
57+ return ge::GRAPH_SUCCESS;
58+}
59+ 
60+IMPL_OP(AvgPool3DGrad).InferDataType(InferDataTypeAvgPool3DGrad);
61+ 
62+} // namespace ops
@@ -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,84 @@
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 "op_infer_datatype_context_builder.h"
13+ 
14+namespace ops {
15+ge::graphStatus InferDataTypeAvgPool3DGrad(gert::InferDataTypeContext* context);
16+}
17+ 
18+TEST(AvgPool3DGradGraphInfer, InferDataTypeFP16)
19+{
20+ gert::OpInferDataTypeContextBuilder builder;
21+ builder.OpType("AvgPool3DGrad").OpName("AvgPool3DGrad");
22+ builder.IONum(2, 1);
23+ builder.InputTensorDesc(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND);
24+ builder.InputTensorDesc(1, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_ND);
25+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
26+ auto holder = builder.Build();
27+ auto* context = holder.GetContext();
28+ ASSERT_NE(context, nullptr);
29+ 
30+ ASSERT_EQ(ops::InferDataTypeAvgPool3DGrad(context), ge::GRAPH_SUCCESS);
31+ EXPECT_EQ(context->GetOutputDataType(0), ge::DT_FLOAT16);
32+}
33+ 
34+TEST(AvgPool3DGradGraphInfer, InferDataTypeFP32)
35+{
36+ gert::OpInferDataTypeContextBuilder builder;
37+ builder.OpType("AvgPool3DGrad").OpName("AvgPool3DGrad");
38+ builder.IONum(2, 1);
39+ builder.InputTensorDesc(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND);
40+ builder.InputTensorDesc(1, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND);
41+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
42+ auto holder = builder.Build();
43+ auto* context = holder.GetContext();
44+ ASSERT_NE(context, nullptr);
45+ 
46+ ASSERT_EQ(ops::InferDataTypeAvgPool3DGrad(context), ge::GRAPH_SUCCESS);
47+ EXPECT_EQ(context->GetOutputDataType(0), ge::DT_FLOAT);
48+}
49+ 
50+TEST(AvgPool3DGradGraphInfer, InferDataTypeBF16)
51+{
52+ gert::OpInferDataTypeContextBuilder builder;
53+ builder.OpType("AvgPool3DGrad").OpName("AvgPool3DGrad");
54+ builder.IONum(2, 1);
55+ builder.InputTensorDesc(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND);
56+ builder.InputTensorDesc(1, ge::DT_BF16, ge::FORMAT_ND, ge::FORMAT_ND);
57+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
58+ auto holder = builder.Build();
59+ auto* context = holder.GetContext();
60+ ASSERT_NE(context, nullptr);
61+ 
62+ ASSERT_EQ(ops::InferDataTypeAvgPool3DGrad(context), ge::GRAPH_SUCCESS);
63+ EXPECT_EQ(context->GetOutputDataType(0), ge::DT_BF16);
64+}
65+ 
66+TEST(AvgPool3DGradGraphInfer, InferDataTypeUnsupportedDtype)
67+{
68+ gert::OpInferDataTypeContextBuilder builder;
69+ builder.OpType("AvgPool3DGrad").OpName("AvgPool3DGrad");
70+ builder.IONum(2, 1);
71+ builder.InputTensorDesc(0, ge::DT_INT32, ge::FORMAT_ND, ge::FORMAT_ND);
72+ builder.InputTensorDesc(1, ge::DT_INT64, ge::FORMAT_ND, ge::FORMAT_ND);
73+ builder.OutputTensorDesc(0, ge::FORMAT_ND, ge::FORMAT_ND);
74+ auto holder = builder.Build();
75+ auto* context = holder.GetContext();
76+ ASSERT_NE(context, nullptr);
77+ 
78+ EXPECT_EQ(ops::InferDataTypeAvgPool3DGrad(context), ge::GRAPH_FAILED);
79+}
80+ 
81+TEST(AvgPool3DGradGraphInfer, RejectsNullContext)
82+{
83+ EXPECT_EQ(ops::InferDataTypeAvgPool3DGrad(nullptr), ge::GRAPH_FAILED);
84+}