已合并
fix: replace Chinese log messages with English in examples #4883
wangyuchen创建于 9月10日
fix: replace Chinese log messages with English in examples #4883
已合并
共 60 个文件变更+784-562
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeControlEdgeGraphByEsAndDump(); | 40 | es_showcase::MakeControlEdgeGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -22,12 +22,18 @@ def build_control_edge_graph(): | |||
| 22 | # 1. 创建图构建器 | 22 | # 1. 创建图构建器 |
| 23 | builder = GraphBuilder("control_dep_scope_example") | 23 | builder = GraphBuilder("control_dep_scope_example") |
| 24 | # 2. 创建依赖源节点 | 24 | # 2. 创建依赖源节点 |
| 25 | - input_tensor_holder0 = builder.create_input(index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 2]) | 25 | + input_tensor_holder0 = builder.create_input( |
| 26 | - input_tensor_holder1 = builder.create_input(index=1, name="input1", data_type=DataType.DT_FLOAT, shape=[2, 2]) | 26 | + index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 2] |
| 27 | + ) | ||
| 28 | + input_tensor_holder1 = builder.create_input( | ||
| 29 | + index=1, name="input1", data_type=DataType.DT_FLOAT, shape=[2, 2] | ||
| 30 | + ) | ||
| 27 | # 3. 使用 scope:在 scope 内创建的所有节点自动依赖 input_tensor_holder0 和 input_tensor_holder1 | 31 | # 3. 使用 scope:在 scope 内创建的所有节点自动依赖 input_tensor_holder0 和 input_tensor_holder1 |
| 28 | with control_dependency_scope([input_tensor_holder0, input_tensor_holder1]): | 32 | with control_dependency_scope([input_tensor_holder0, input_tensor_holder1]): |
| 29 | # 在此 scope 内创建的节点会自动添加控制依赖 | 33 | # 在此 scope 内创建的节点会自动添加控制依赖 |
| 30 | - input_tensor_holder2 = builder.create_input(index=2, name="input2", data_type=DataType.DT_FLOAT, shape=[2, 2]) | 34 | + input_tensor_holder2 = builder.create_input( |
| 35 | + index=2, name="input2", data_type=DataType.DT_FLOAT, shape=[2, 2] | ||
| 36 | + ) | ||
| 31 | input_tensor_holder3 = input_tensor_holder0 + input_tensor_holder2 | 37 | input_tensor_holder3 = input_tensor_holder0 + input_tensor_holder2 |
| 32 | # 4. 设置输出并构建 | 38 | # 4. 设置输出并构建 |
| 33 | builder.set_graph_output(input_tensor_holder3, 0) | 39 | builder.set_graph_output(input_tensor_holder3, 0) |
| @@ -46,34 +52,40 @@ def run_graph(graph) -> None: | |||
| 46 | ge_api = GeApi() | 52 | ge_api = GeApi() |
| 47 | ret = ge_api.ge_initialize(config) | 53 | ret = ge_api.ge_initialize(config) |
| 48 | if ret != 0: | 54 | if ret != 0: |
| 49 | - print(f"GE初始化失败,返回码: {ret}") | 55 | + print(f"GE initialization failed, return code: {ret}") |
| 50 | return ret | 56 | return ret |
| 51 | - print("GE环境初始化成功 (Device ID: 0)") | 57 | + print("GE environment initialized successfully (Device ID: 0)") |
| 52 | 58 | ||
| 53 | try: | 59 | try: |
| 54 | session = Session() | 60 | session = Session() |
| 55 | graph_id = 1 | 61 | graph_id = 1 |
| 56 | ret = session.add_graph(graph_id, graph) | 62 | ret = session.add_graph(graph_id, graph) |
| 57 | if ret != 0: | 63 | if ret != 0: |
| 58 | - print(f"添加图失败,返回码: {ret}") | 64 | + print(f"Failed to add graph, return code: {ret}") |
| 59 | return ret | 65 | return ret |
| 60 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 66 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 61 | 67 | ||
| 62 | # 4. 准备输入数据 | 68 | # 4. 准备输入数据 |
| 63 | - tensor1 = Tensor([2.0, 2.0, 2.0, 2.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2, 2]) | 69 | + tensor1 = Tensor( |
| 64 | - tensor2 = Tensor([1.0, 1.0, 1.0, 1.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2, 2]) | 70 | + [2.0, 2.0, 2.0, 2.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2, 2] |
| 65 | - tensor3 = Tensor([1.0, 1.0, 1.0, 1.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2, 2]) | 71 | + ) |
| 72 | + tensor2 = Tensor( | ||
| 73 | + [1.0, 1.0, 1.0, 1.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2, 2] | ||
| 74 | + ) | ||
| 75 | + tensor3 = Tensor( | ||
| 76 | + [1.0, 1.0, 1.0, 1.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2, 2] | ||
| 77 | + ) | ||
| 66 | # 创建Tensor对象 | 78 | # 创建Tensor对象 |
| 67 | inputs = [tensor1, tensor2, tensor3] | 79 | inputs = [tensor1, tensor2, tensor3] |
| 68 | # 5. 运行图 | 80 | # 5. 运行图 |
| 69 | ret = session.run_graph(graph_id, inputs) | 81 | ret = session.run_graph(graph_id, inputs) |
| 70 | - print("[Info] 图运行成功!") | 82 | + print("[Info] Graph executed successfully!") |
| 71 | for idx, tensor in enumerate(ret, start=1): | 83 | for idx, tensor in enumerate(ret, start=1): |
| 72 | - print(f"Tensor{idx}详情:{tensor}") | 84 | + print(f"Tensor{idx} details: {tensor}") |
| 73 | return 0 | 85 | return 0 |
| 74 | 86 | ||
| 75 | except Exception as e: | 87 | except Exception as e: |
| 76 | - print(f"[Error] 执行过程中出错: {e}") | 88 | + print(f"[Error] Error during execution: {e}") |
| 77 | import traceback | 89 | import traceback |
| 78 | 90 | ||
| 79 | traceback.print_exc() | 91 | traceback.print_exc() |
| @@ -81,9 +93,9 @@ def run_graph(graph) -> None: | |||
| 81 | 93 | ||
| 82 | finally: | 94 | finally: |
| 83 | # 6. 清理GE环境 | 95 | # 6. 清理GE环境 |
| 84 | - print("[Info] 清理GE环境...") | 96 | + print("[Info] Cleaning up GE environment...") |
| 85 | ge_api.ge_finalize() | 97 | ge_api.ge_finalize() |
| 86 | - print("[Success] GE环境已清理") | 98 | + print("[Success] GE environment cleaned up") |
| 87 | 99 | ||
| 88 | 100 | ||
| 89 | graph = build_control_edge_graph() | 101 | graph = build_control_edge_graph() |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeIfGraphByEsAndDump(); | 40 | es_showcase::MakeIfGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -23,8 +23,12 @@ def build_if_graph(): | |||
| 23 | # 1、创建图构建器 | 23 | # 1、创建图构建器 |
| 24 | builder = GraphBuilder("MakeIfGraph") | 24 | builder = GraphBuilder("MakeIfGraph") |
| 25 | # 2、MakeIfGraph实例创建输入节点 | 25 | # 2、MakeIfGraph实例创建输入节点 |
| 26 | - input_tensor_holder = builder.create_input(index=0, name="test_input", data_type=DataType.DT_FLOAT, shape=[2]) | 26 | + input_tensor_holder = builder.create_input( |
| 27 | - cond = builder.create_input(index=1, name="cond", data_type=DataType.DT_INT32, shape=[]) | 27 | + index=0, name="test_input", data_type=DataType.DT_FLOAT, shape=[2] |
| 28 | + ) | ||
| 29 | + cond = builder.create_input( | ||
| 30 | + index=1, name="cond", data_type=DataType.DT_INT32, shape=[] | ||
| 31 | + ) | ||
| 28 | # 初始化图构建器实例,用于构建子图then_branch | 32 | # 初始化图构建器实例,用于构建子图then_branch |
| 29 | then_branch_builder = GraphBuilder("then_branch") | 33 | then_branch_builder = GraphBuilder("then_branch") |
| 30 | then_branch_input_tensor = then_branch_builder.create_input( | 34 | then_branch_input_tensor = then_branch_builder.create_input( |
| @@ -63,9 +67,9 @@ def run_graph(graph) -> None: | |||
| 63 | ge_api = GeApi() | 67 | ge_api = GeApi() |
| 64 | ret = ge_api.ge_initialize(config) | 68 | ret = ge_api.ge_initialize(config) |
| 65 | if ret != 0: | 69 | if ret != 0: |
| 66 | - print(f"GE初始化失败,返回码: {ret}") | 70 | + print(f"GE initialization failed, return code: {ret}") |
| 67 | return ret | 71 | return ret |
| 68 | - print("GE环境初始化成功 (Device ID: 0)") | 72 | + print("GE environment initialized successfully (Device ID: 0)") |
| 69 | 73 | ||
| 70 | try: | 74 | try: |
| 71 | # 2. 创建Session | 75 | # 2. 创建Session |
| @@ -74,9 +78,9 @@ def run_graph(graph) -> None: | |||
| 74 | graph_id = 1 | 78 | graph_id = 1 |
| 75 | ret = session.add_graph(graph_id, graph) | 79 | ret = session.add_graph(graph_id, graph) |
| 76 | if ret != 0: | 80 | if ret != 0: |
| 77 | - print(f"添加图失败,返回码: {ret}") | 81 | + print(f"Failed to add graph, return code: {ret}") |
| 78 | return ret | 82 | return ret |
| 79 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 83 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 80 | 84 | ||
| 81 | # 4. 准备输入数据 | 85 | # 4. 准备输入数据 |
| 82 | tensor1 = Tensor([1.0, 2.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2]) | 86 | tensor1 = Tensor([1.0, 2.0], None, DataType.DT_FLOAT, Format.FORMAT_ND, [2]) |
| @@ -85,22 +89,22 @@ def run_graph(graph) -> None: | |||
| 85 | inputs = [tensor1, tensor_const] | 89 | inputs = [tensor1, tensor_const] |
| 86 | # 5. 运行图 | 90 | # 5. 运行图 |
| 87 | ret = session.run_graph(graph_id, inputs) | 91 | ret = session.run_graph(graph_id, inputs) |
| 88 | - print("[Info] 图运行成功!") | 92 | + print("[Info] Graph executed successfully!") |
| 89 | for idx, tensor in enumerate(ret, start=1): | 93 | for idx, tensor in enumerate(ret, start=1): |
| 90 | - print(f"Tensor{idx}详情:{tensor}") | 94 | + print(f"Tensor{idx} details: {tensor}") |
| 91 | return 0 | 95 | return 0 |
| 92 | 96 | ||
| 93 | except Exception as e: | 97 | except Exception as e: |
| 94 | - print(f"[Error] 执行过程中出错: {e}") | 98 | + print(f"[Error] Error during execution: {e}") |
| 95 | import traceback | 99 | import traceback |
| 96 | 100 | ||
| 97 | traceback.print_exc() | 101 | traceback.print_exc() |
| 98 | return -1 | 102 | return -1 |
| 99 | finally: | 103 | finally: |
| 100 | # 6. 清理GE环境 | 104 | # 6. 清理GE环境 |
| 101 | - print("[Info] 清理GE环境...") | 105 | + print("[Info] Cleaning up GE environment...") |
| 102 | ge_api.ge_finalize() | 106 | ge_api.ge_finalize() |
| 103 | - print("[Success] GE环境已清理") | 107 | + print("[Success] GE environment cleaned up") |
| 104 | 108 | ||
| 105 | 109 | ||
| 106 | graph = build_if_graph() | 110 | graph = build_if_graph() |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeConcatV2GraphByEsAndDump(); | 40 | es_showcase::MakeConcatV2GraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -23,9 +23,15 @@ def build_concat_graph(): | |||
| 23 | # 1、创建图构建器 | 23 | # 1、创建图构建器 |
| 24 | builder = GraphBuilder("MakeConcatV2Graph") | 24 | builder = GraphBuilder("MakeConcatV2Graph") |
| 25 | # 2、创建图输入节点 | 25 | # 2、创建图输入节点 |
| 26 | - tensor_holder1 = builder.create_input(index=0, name="tensor1", data_type=DataType.DT_FLOAT, shape=[8, 64, 128]) | 26 | + tensor_holder1 = builder.create_input( |
| 27 | - tensor_holder2 = builder.create_input(index=1, name="tensor2", data_type=DataType.DT_FLOAT, shape=[2, 64, 128]) | 27 | + index=0, name="tensor1", data_type=DataType.DT_FLOAT, shape=[8, 64, 128] |
| 28 | - tensor_holder3 = builder.create_input(index=2, name="tensor3", data_type=DataType.DT_FLOAT, shape=[6, 64, 128]) | 28 | + ) |
| 29 | + tensor_holder2 = builder.create_input( | ||
| 30 | + index=1, name="tensor2", data_type=DataType.DT_FLOAT, shape=[2, 64, 128] | ||
| 31 | + ) | ||
| 32 | + tensor_holder3 = builder.create_input( | ||
| 33 | + index=2, name="tensor3", data_type=DataType.DT_FLOAT, shape=[6, 64, 128] | ||
| 34 | + ) | ||
| 29 | tensor_holder_list = [tensor_holder1, tensor_holder2, tensor_holder3] | 35 | tensor_holder_list = [tensor_holder1, tensor_holder2, tensor_holder3] |
| 30 | concat_tensor_holder = ConcatV2(tensor_holder_list, 0, N=3) | 36 | concat_tensor_holder = ConcatV2(tensor_holder_list, 0, N=3) |
| 31 | # 3、设置图输出节点 | 37 | # 3、设置图输出节点 |
| @@ -46,9 +52,9 @@ def run_graph(graph) -> int: | |||
| 46 | ge_api = GeApi() | 52 | ge_api = GeApi() |
| 47 | ret = ge_api.ge_initialize(config) | 53 | ret = ge_api.ge_initialize(config) |
| 48 | if ret != 0: | 54 | if ret != 0: |
| 49 | - print(f"GE初始化失败,返回码: {ret}") | 55 | + print(f"GE initialization failed, return code: {ret}") |
| 50 | return ret | 56 | return ret |
| 51 | - print("GE环境初始化成功 (Device ID: 0)") | 57 | + print("GE environment initialized successfully (Device ID: 0)") |
| 52 | 58 | ||
| 53 | try: | 59 | try: |
| 54 | # 2. 创建Session | 60 | # 2. 创建Session |
| @@ -57,9 +63,9 @@ def run_graph(graph) -> int: | |||
| 57 | graph_id = 1 | 63 | graph_id = 1 |
| 58 | ret = session.add_graph(graph_id, graph) | 64 | ret = session.add_graph(graph_id, graph) |
| 59 | if ret != 0: | 65 | if ret != 0: |
| 60 | - print(f"添加图失败,返回码: {ret}") | 66 | + print(f"Failed to add graph, return code: {ret}") |
| 61 | return ret | 67 | return ret |
| 62 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 68 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 63 | 69 | ||
| 64 | # 4. 准备输入数据 | 70 | # 4. 准备输入数据 |
| 65 | tensor1_data = np.full(8 * 64 * 128, 1.0, dtype=np.float32) | 71 | tensor1_data = np.full(8 * 64 * 128, 1.0, dtype=np.float32) |
| @@ -89,22 +95,22 @@ def run_graph(graph) -> int: | |||
| 89 | input_tensor = [tensor1, tensor2, tensor3] | 95 | input_tensor = [tensor1, tensor2, tensor3] |
| 90 | # 5. 运行图 | 96 | # 5. 运行图 |
| 91 | ret = session.run_graph(graph_id, input_tensor) | 97 | ret = session.run_graph(graph_id, input_tensor) |
| 92 | - print("[Info] 图运行成功!") | 98 | + print("[Info] Graph executed successfully!") |
| 93 | for idx, tensor in enumerate(ret, start=1): | 99 | for idx, tensor in enumerate(ret, start=1): |
| 94 | - print(f"Tensor{idx}详情: {tensor}") | 100 | + print(f"Tensor{idx} details: {tensor}") |
| 95 | return 0 | 101 | return 0 |
| 96 | 102 | ||
| 97 | except Exception as e: | 103 | except Exception as e: |
| 98 | - print(f"[Error] 执行过程中出错: {e}") | 104 | + print(f"[Error] Error during execution: {e}") |
| 99 | import traceback | 105 | import traceback |
| 100 | 106 | ||
| 101 | traceback.print_exc() | 107 | traceback.print_exc() |
| 102 | return -1 | 108 | return -1 |
| 103 | finally: | 109 | finally: |
| 104 | # 6. 清理GE环境 | 110 | # 6. 清理GE环境 |
| 105 | - print("[Info] 清理GE环境...") | 111 | + print("[Info] Cleaning up GE environment...") |
| 106 | ge_api.ge_finalize() | 112 | ge_api.ge_finalize() |
| 107 | - print("[Success] GE环境已清理") | 113 | + print("[Success] GE environment cleaned up") |
| 108 | 114 | ||
| 109 | 115 | ||
| 110 | graph = build_concat_graph() | 116 | graph = build_concat_graph() |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeSplitGraphByEsAndDump(); | 40 | es_showcase::MakeSplitGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -23,7 +23,9 @@ def build_split_graph(): | |||
| 23 | # 1、创建图构建器 | 23 | # 1、创建图构建器 |
| 24 | builder = GraphBuilder("MakeSplitGraph") | 24 | builder = GraphBuilder("MakeSplitGraph") |
| 25 | # 2、创建图输入节点 | 25 | # 2、创建图输入节点 |
| 26 | - input_tensor_holder = builder.create_input(index=0, name="input", data_type=DataType.DT_FLOAT, shape=[8, 16, 64]) | 26 | + input_tensor_holder = builder.create_input( |
| 27 | + index=0, name="input", data_type=DataType.DT_FLOAT, shape=[8, 16, 64] | ||
| 28 | + ) | ||
| 27 | split_list = Split(1, input_tensor_holder, 4, num_split=4) | 29 | split_list = Split(1, input_tensor_holder, 4, num_split=4) |
| 28 | # 3、设置图输出节点 | 30 | # 3、设置图输出节点 |
| 29 | for i, tensor in enumerate(split_list): | 31 | for i, tensor in enumerate(split_list): |
| @@ -44,9 +46,9 @@ def run_graph(graph) -> int: | |||
| 44 | ge_api = GeApi() | 46 | ge_api = GeApi() |
| 45 | ret = ge_api.ge_initialize(config) | 47 | ret = ge_api.ge_initialize(config) |
| 46 | if ret != 0: | 48 | if ret != 0: |
| 47 | - print(f"GE初始化失败,返回码: {ret}") | 49 | + print(f"GE initialization failed, return code: {ret}") |
| 48 | return ret | 50 | return ret |
| 49 | - print("GE环境初始化成功 (Device ID: 0)") | 51 | + print("GE environment initialized successfully (Device ID: 0)") |
| 50 | 52 | ||
| 51 | try: | 53 | try: |
| 52 | # 2. 创建Session | 54 | # 2. 创建Session |
| @@ -55,9 +57,9 @@ def run_graph(graph) -> int: | |||
| 55 | graph_id = 1 | 57 | graph_id = 1 |
| 56 | ret = session.add_graph(graph_id, graph) | 58 | ret = session.add_graph(graph_id, graph) |
| 57 | if ret != 0: | 59 | if ret != 0: |
| 58 | - print(f"添加图失败,返回码: {ret}") | 60 | + print(f"Failed to add graph, return code: {ret}") |
| 59 | return ret | 61 | return ret |
| 60 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 62 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 61 | 63 | ||
| 62 | # 4. 准备输入数据 | 64 | # 4. 准备输入数据 |
| 63 | tensor_data = np.full(8 * 16 * 64, 1.0, dtype=np.float32) | 65 | tensor_data = np.full(8 * 16 * 64, 1.0, dtype=np.float32) |
| @@ -72,22 +74,22 @@ def run_graph(graph) -> int: | |||
| 72 | input_tensor = [tensor1] | 74 | input_tensor = [tensor1] |
| 73 | # 5. 运行图 | 75 | # 5. 运行图 |
| 74 | ret = session.run_graph(graph_id, input_tensor) | 76 | ret = session.run_graph(graph_id, input_tensor) |
| 75 | - print("[Info] 图运行成功!") | 77 | + print("[Info] Graph executed successfully!") |
| 76 | for idx, tensor in enumerate(ret, start=1): | 78 | for idx, tensor in enumerate(ret, start=1): |
| 77 | - print(f"Tensor{idx}详情:{tensor}") | 79 | + print(f"Tensor{idx} details: {tensor}") |
| 78 | return 0 | 80 | return 0 |
| 79 | 81 | ||
| 80 | except Exception as e: | 82 | except Exception as e: |
| 81 | - print(f"[Error] 执行过程中出错: {e}") | 83 | + print(f"[Error] Error during execution: {e}") |
| 82 | import traceback | 84 | import traceback |
| 83 | 85 | ||
| 84 | traceback.print_exc() | 86 | traceback.print_exc() |
| 85 | return -1 | 87 | return -1 |
| 86 | finally: | 88 | finally: |
| 87 | # 6. 清理GE环境 | 89 | # 6. 清理GE环境 |
| 88 | - print("[Info] 清理GE环境...") | 90 | + print("[Info] Cleaning up GE environment...") |
| 89 | ge_api.ge_finalize() | 91 | ge_api.ge_finalize() |
| 90 | - print("[Success] GE环境已清理") | 92 | + print("[Success] GE environment cleaned up") |
| 91 | 93 | ||
| 92 | 94 | ||
| 93 | graph = build_split_graph() | 95 | graph = build_split_graph() |
| @@ -51,7 +51,7 @@ int main(int argc, char **argv) { | |||
| 51 | {"ge.exec.rankId", rank_id}}; | 51 | {"ge.exec.rankId", rank_id}}; |
| 52 | auto ret = ge::GEInitialize(config); | 52 | auto ret = ge::GEInitialize(config); |
| 53 | if (ret != ge::SUCCESS) { | 53 | if (ret != ge::SUCCESS) { |
| 54 | - std::cerr << "GE 初始化失败\n"; | 54 | + std::cerr << "GE initialization failed\n"; |
| 55 | return -1; | 55 | return -1; |
| 56 | } | 56 | } |
| 57 | int result = -1; | 57 | int result = -1; |
| @@ -59,10 +59,10 @@ int main(int argc, char **argv) { | |||
| 59 | result = 0; | 59 | result = 0; |
| 60 | } | 60 | } |
| 61 | ge::GEFinalize(); | 61 | ge::GEFinalize(); |
| 62 | - std::cout << "执行结束" << std::endl; | 62 | + std::cout << "Execution completed" << std::endl; |
| 63 | return result; | 63 | return result; |
| 64 | } else { | 64 | } else { |
| 65 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 65 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 66 | return -1; | 66 | return -1; |
| 67 | } | 67 | } |
| 68 | } | 68 | } |
| @@ -10,6 +10,7 @@ | |||
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | 12 | ||
| 13 | +# ruff: noqa: F403, F405 | ||
| 13 | import os | 14 | import os |
| 14 | import sys | 15 | import sys |
| 15 | 16 | ||
| @@ -46,13 +47,19 @@ def build_ep_graph(): | |||
| 46 | # 3、创建输入节点 | 47 | # 3、创建输入节点 |
| 47 | inputs = [] | 48 | inputs = [] |
| 48 | for idx, (name, shape, dtype) in enumerate(input_configs): | 49 | for idx, (name, shape, dtype) in enumerate(input_configs): |
| 49 | - input_node = builder.create_input(index=idx, name=name, data_type=dtype, shape=shape) | 50 | + input_node = builder.create_input( |
| 51 | + index=idx, name=name, data_type=dtype, shape=shape | ||
| 52 | + ) | ||
| 50 | inputs.append(input_node) | 53 | inputs.append(input_node) |
| 51 | 54 | ||
| 52 | - hidden_states, gate_weight, finished, shared_output, expert_weight, group_list = inputs | 55 | + hidden_states, gate_weight, finished, shared_output, expert_weight, group_list = ( |
| 56 | + inputs | ||
| 57 | + ) | ||
| 53 | 58 | ||
| 54 | # 4、构建计算图 | 59 | # 4、构建计算图 |
| 55 | - quant_result = DynamicQuant(hidden_states, smooth_scales=None, group_index=None, dst_type=DataType.DT_INT8) | 60 | + quant_result = DynamicQuant( |
| 61 | + hidden_states, smooth_scales=None, group_index=None, dst_type=DataType.DT_INT8 | ||
| 62 | + ) | ||
| 56 | hidden_states_int8 = quant_result.y | 63 | hidden_states_int8 = quant_result.y |
| 57 | pertoken_scale = quant_result.scale | 64 | pertoken_scale = quant_result.scale |
| 58 | 65 | ||
| @@ -79,28 +86,38 @@ def build_ep_graph(): | |||
| 79 | N=4, | 86 | N=4, |
| 80 | ) | 87 | ) |
| 81 | 88 | ||
| 82 | - topk_all = HcomAllGather(topk_cat, rank_size=RANK_SIZE, group="hccl_world_group", fusion=0, fusion_id=-1) | 89 | + topk_all = HcomAllGather( |
| 90 | + topk_cat, rank_size=RANK_SIZE, group="hccl_world_group", fusion=0, fusion_id=-1 | ||
| 91 | + ) | ||
| 83 | 92 | ||
| 84 | size_splits = builder.create_vector_int64([TOP_K, TOP_K, TOP_K, 1]) | 93 | size_splits = builder.create_vector_int64([TOP_K, TOP_K, TOP_K, 1]) |
| 85 | split_results = SplitV(topk_all, size_splits, -1, 4, num_split=4) | 94 | split_results = SplitV(topk_all, size_splits, -1, 4, num_split=4) |
| 86 | global_topk_weights = split_results[0] | 95 | global_topk_weights = split_results[0] |
| 87 | - global_topk_ids_float = split_results[1] | ||
| 88 | global_row_idx_float = split_results[2] | 96 | global_row_idx_float = split_results[2] |
| 89 | global_pertoken_scale = split_results[3] | 97 | global_pertoken_scale = split_results[3] |
| 90 | 98 | ||
| 91 | - topk_ids_rounded = Round(global_topk_ids_float) | ||
| 92 | row_idx_rounded = Round(global_row_idx_float) | 99 | row_idx_rounded = Round(global_row_idx_float) |
| 93 | global_row_idx = Cast(row_idx_rounded, dst_type=DataType.DT_INT64) | 100 | global_row_idx = Cast(row_idx_rounded, dst_type=DataType.DT_INT64) |
| 94 | global_row_idx_flat = Reshape(global_row_idx, builder.create_vector_int64([-1])) | 101 | global_row_idx_flat = Reshape(global_row_idx, builder.create_vector_int64([-1])) |
| 95 | 102 | ||
| 96 | axis_const = builder.create_const_int32([0], []) | 103 | axis_const = builder.create_const_int32([0], []) |
| 97 | - dispatched_hidden_states = GatherV2(global_hidden_states, global_row_idx_flat, axis_const) | 104 | + dispatched_hidden_states = GatherV2( |
| 98 | - global_pertoken_scale_squeezed = Reshape(global_pertoken_scale, builder.create_vector_int64([-1])) | 105 | + global_hidden_states, global_row_idx_flat, axis_const |
| 99 | - dispatched_pertoken_scale = GatherV2(global_pertoken_scale_squeezed, global_row_idx_flat, axis_const) | 106 | + ) |
| 100 | - global_topk_weights_flat = Reshape(global_topk_weights, builder.create_vector_int64([-1])) | 107 | + global_pertoken_scale_squeezed = Reshape( |
| 108 | + global_pertoken_scale, builder.create_vector_int64([-1]) | ||
| 109 | + ) | ||
| 110 | + dispatched_pertoken_scale = GatherV2( | ||
| 111 | + global_pertoken_scale_squeezed, global_row_idx_flat, axis_const | ||
| 112 | + ) | ||
| 113 | + global_topk_weights_flat = Reshape( | ||
| 114 | + global_topk_weights, builder.create_vector_int64([-1]) | ||
| 115 | + ) | ||
| 101 | group_list_int64 = Cast(group_list, dst_type=DataType.DT_INT64) | 116 | group_list_int64 = Cast(group_list, dst_type=DataType.DT_INT64) |
| 102 | scale_data = np.ones([NUM_EXPERTS, HIDDEN_SIZE], dtype=np.float32) | 117 | scale_data = np.ones([NUM_EXPERTS, HIDDEN_SIZE], dtype=np.float32) |
| 103 | - weight_scale = builder.create_const_float(scale_data.flatten().tolist(), [NUM_EXPERTS, HIDDEN_SIZE]) | 118 | + weight_scale = builder.create_const_float( |
| 119 | + scale_data.flatten().tolist(), [NUM_EXPERTS, HIDDEN_SIZE] | ||
| 120 | + ) | ||
| 104 | 121 | ||
| 105 | moe_output = GroupedMatmulFinalizeRouting( | 122 | moe_output = GroupedMatmulFinalizeRouting( |
| 106 | dispatched_hidden_states, # x (int8) | 123 | dispatched_hidden_states, # x (int8) |
| @@ -153,17 +170,23 @@ def run_graph(graph, device_id="0") -> int: | |||
| 153 | if rank_id is not None and rank_table_file is not None: | 170 | if rank_id is not None and rank_table_file is not None: |
| 154 | config["ge.exec.rankTableFile"] = rank_table_file | 171 | config["ge.exec.rankTableFile"] = rank_table_file |
| 155 | config["ge.exec.rankId"] = rank_id | 172 | config["ge.exec.rankId"] = rank_id |
| 156 | - print(f"[Info] 多卡模式 - RANK_ID: {rank_id}, RANK_TABLE_FILE: {rank_table_file}") | 173 | + print( |
| 174 | + f"[Info] Multi-card mode - RANK_ID: {rank_id}, RANK_TABLE_FILE: {rank_table_file}" | ||
| 175 | + ) | ||
| 157 | else: | 176 | else: |
| 158 | - print("[Info] 单卡模式 - 未检测到RANK_ID和RANK_TABLE_FILE环境变量") | 177 | + print( |
| 178 | + "[Info] Single-card mode - RANK_ID and RANK_TABLE_FILE environment variables not detected" | ||
| 179 | + ) | ||
| 159 | 180 | ||
| 160 | ge_api = GeApi() | 181 | ge_api = GeApi() |
| 161 | ret = ge_api.ge_initialize(config) | 182 | ret = ge_api.ge_initialize(config) |
| 162 | if ret != 0: | 183 | if ret != 0: |
| 163 | - print(f"[Error] GE初始化失败,返回码: {ret}") | 184 | + print(f"[Error] GE initialization failed, return code: {ret}") |
| 164 | return ret | 185 | return ret |
| 165 | 186 | ||
| 166 | - print(f"[Info] GE环境初始化成功 (Device ID: {device_id}, RANK_ID: {rank_id if rank_id else 'N/A'})") | 187 | + print( |
| 188 | + f"[Info] GE environment initialized successfully (Device ID: {device_id}, RANK_ID: {rank_id if rank_id else 'N/A'})" | ||
| 189 | + ) | ||
| 167 | 190 | ||
| 168 | try: | 191 | try: |
| 169 | # 2. 创建Session | 192 | # 2. 创建Session |
| @@ -173,9 +196,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 173 | graph_id = 1 | 196 | graph_id = 1 |
| 174 | ret = session.add_graph(graph_id, graph) | 197 | ret = session.add_graph(graph_id, graph) |
| 175 | if ret != 0: | 198 | if ret != 0: |
| 176 | - print(f"[Error] 添加图失败,返回码: {ret}") | 199 | + print(f"[Error] Failed to add graph, return code: {ret}") |
| 177 | return ret | 200 | return ret |
| 178 | - print(f"[Info] 图已添加到Session (Graph ID: {graph_id})") | 201 | + print(f"[Info] Graph added to Session (Graph ID: {graph_id})") |
| 179 | 202 | ||
| 180 | # 4. 准备输入数据 | 203 | # 4. 准备输入数据 |
| 181 | # hidden_states | 204 | # hidden_states |
| @@ -200,7 +223,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 200 | 223 | ||
| 201 | # finished (BOOL 类型) | 224 | # finished (BOOL 类型) |
| 202 | finished_data = [False] * LOCAL_BATCH | 225 | finished_data = [False] * LOCAL_BATCH |
| 203 | - finished_tensor = Tensor(finished_data, None, DataType.DT_BOOL, Format.FORMAT_ND, [LOCAL_BATCH]) | 226 | + finished_tensor = Tensor( |
| 227 | + finished_data, None, DataType.DT_BOOL, Format.FORMAT_ND, [LOCAL_BATCH] | ||
| 228 | + ) | ||
| 204 | 229 | ||
| 205 | # shared_output | 230 | # shared_output |
| 206 | shared_output_data = np.full([LOCAL_BATCH, HIDDEN_SIZE], 0.5, dtype=np.float32) | 231 | shared_output_data = np.full([LOCAL_BATCH, HIDDEN_SIZE], 0.5, dtype=np.float32) |
| @@ -213,7 +238,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 213 | ) | 238 | ) |
| 214 | 239 | ||
| 215 | # expert_weight | 240 | # expert_weight |
| 216 | - expert_weight_data = np.ones([NUM_EXPERTS, HIDDEN_SIZE, HIDDEN_SIZE], dtype=np.int8) | 241 | + expert_weight_data = np.ones( |
| 242 | + [NUM_EXPERTS, HIDDEN_SIZE, HIDDEN_SIZE], dtype=np.int8 | ||
| 243 | + ) | ||
| 217 | expert_weight_tensor = Tensor( | 244 | expert_weight_tensor = Tensor( |
| 218 | expert_weight_data.flatten().tolist(), | 245 | expert_weight_data.flatten().tolist(), |
| 219 | None, | 246 | None, |
| @@ -241,17 +268,17 @@ def run_graph(graph, device_id="0") -> int: | |||
| 241 | group_list_tensor, | 268 | group_list_tensor, |
| 242 | ] | 269 | ] |
| 243 | 270 | ||
| 244 | - print(f"[Info] 输入数据已准备,共{len(inputs)}个输入tensor") | 271 | + print(f"[Info] Prepared {len(inputs)} input tensor(s)") |
| 245 | 272 | ||
| 246 | # 5. 运行图 | 273 | # 5. 运行图 |
| 247 | ret = session.run_graph(graph_id, inputs) | 274 | ret = session.run_graph(graph_id, inputs) |
| 248 | - print("[Info] 图运行成功!") | 275 | + print("[Info] Graph executed successfully!") |
| 249 | for idx, tensor in enumerate(ret, start=1): | 276 | for idx, tensor in enumerate(ret, start=1): |
| 250 | - print(f"Tensor{idx}详情:{tensor}") | 277 | + print(f"Tensor{idx} details: {tensor}") |
| 251 | return 0 | 278 | return 0 |
| 252 | 279 | ||
| 253 | except Exception as e: | 280 | except Exception as e: |
| 254 | - print(f"[Error] 执行过程中出错: {e}") | 281 | + print(f"[Error] Error during execution: {e}") |
| 255 | import traceback | 282 | import traceback |
| 256 | 283 | ||
| 257 | traceback.print_exc() | 284 | traceback.print_exc() |
| @@ -259,9 +286,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 259 | 286 | ||
| 260 | finally: | 287 | finally: |
| 261 | # 6. 清理GE环境 | 288 | # 6. 清理GE环境 |
| 262 | - print("[Info] 清理GE环境...") | 289 | + print("[Info] Cleaning up GE environment...") |
| 263 | ge_api.ge_finalize() | 290 | ge_api.ge_finalize() |
| 264 | - print("[Success] GE环境已清理") | 291 | + print("[Success] GE environment cleaned up") |
| 265 | 292 | ||
| 266 | 293 | ||
| 267 | if __name__ == "__main__": | 294 | if __name__ == "__main__": |
| @@ -51,7 +51,7 @@ int main(int argc, char **argv) { | |||
| 51 | {"ge.exec.rankId", rank_id}}; | 51 | {"ge.exec.rankId", rank_id}}; |
| 52 | auto ret = ge::GEInitialize(config); | 52 | auto ret = ge::GEInitialize(config); |
| 53 | if (ret != ge::SUCCESS) { | 53 | if (ret != ge::SUCCESS) { |
| 54 | - std::cerr << "GE 初始化失败\n"; | 54 | + std::cerr << "GE initialization failed\n"; |
| 55 | return -1; | 55 | return -1; |
| 56 | } | 56 | } |
| 57 | int result = -1; | 57 | int result = -1; |
| @@ -59,10 +59,10 @@ int main(int argc, char **argv) { | |||
| 59 | result = 0; | 59 | result = 0; |
| 60 | } | 60 | } |
| 61 | ge::GEFinalize(); | 61 | ge::GEFinalize(); |
| 62 | - std::cout << "执行结束" << std::endl; | 62 | + std::cout << "Execution completed" << std::endl; |
| 63 | return result; | 63 | return result; |
| 64 | } else { | 64 | } else { |
| 65 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 65 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 66 | return -1; | 66 | return -1; |
| 67 | } | 67 | } |
| 68 | } | 68 | } |
| @@ -10,6 +10,7 @@ | |||
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | 12 | ||
| 13 | +# ruff: noqa: F403, F405 | ||
| 13 | import os | 14 | import os |
| 14 | import sys | 15 | import sys |
| 15 | 16 | ||
| @@ -47,7 +48,9 @@ def build_pfa_hcom_graph(): | |||
| 47 | inputs_fp32 = [] | 48 | inputs_fp32 = [] |
| 48 | inputs_fp16 = [] | 49 | inputs_fp16 = [] |
| 49 | for idx, (name, shape) in enumerate(input_configs): | 50 | for idx, (name, shape) in enumerate(input_configs): |
| 50 | - input_fp32 = builder.create_input(index=idx, name=name, data_type=DataType.DT_FLOAT, shape=shape) | 51 | + input_fp32 = builder.create_input( |
| 52 | + index=idx, name=name, data_type=DataType.DT_FLOAT, shape=shape | ||
| 53 | + ) | ||
| 51 | inputs_fp32.append(input_fp32) | 54 | inputs_fp32.append(input_fp32) |
| 52 | inputs_fp16.append(Cast(input_fp32, dst_type=DataType.DT_FLOAT16)) | 55 | inputs_fp16.append(Cast(input_fp32, dst_type=DataType.DT_FLOAT16)) |
| 53 | 56 | ||
| @@ -134,17 +137,23 @@ def run_graph(graph, device_id="0") -> int: | |||
| 134 | if rank_id is not None and rank_table_file is not None: | 137 | if rank_id is not None and rank_table_file is not None: |
| 135 | config["ge.exec.rankTableFile"] = rank_table_file | 138 | config["ge.exec.rankTableFile"] = rank_table_file |
| 136 | config["ge.exec.rankId"] = rank_id | 139 | config["ge.exec.rankId"] = rank_id |
| 137 | - print(f"[Info] 多卡模式 - RANK_ID: {rank_id}, RANK_TABLE_FILE: {rank_table_file}") | 140 | + print( |
| 141 | + f"[Info] Multi-card mode - RANK_ID: {rank_id}, RANK_TABLE_FILE: {rank_table_file}" | ||
| 142 | + ) | ||
| 138 | else: | 143 | else: |
| 139 | - print("[Info] 单卡模式 - 未检测到RANK_ID和RANK_TABLE_FILE环境变量") | 144 | + print( |
| 145 | + "[Info] Single-card mode - RANK_ID and RANK_TABLE_FILE environment variables not detected" | ||
| 146 | + ) | ||
| 140 | 147 | ||
| 141 | ge_api = GeApi() | 148 | ge_api = GeApi() |
| 142 | ret = ge_api.ge_initialize(config) | 149 | ret = ge_api.ge_initialize(config) |
| 143 | if ret != 0: | 150 | if ret != 0: |
| 144 | - print(f"[Error] GE初始化失败,返回码: {ret}") | 151 | + print(f"[Error] GE initialization failed, return code: {ret}") |
| 145 | return ret | 152 | return ret |
| 146 | 153 | ||
| 147 | - print(f"[Info] GE环境初始化成功 (Device ID: {device_id}, RANK_ID: {rank_id if rank_id else 'N/A'})") | 154 | + print( |
| 155 | + f"[Info] GE environment initialized successfully (Device ID: {device_id}, RANK_ID: {rank_id if rank_id else 'N/A'})" | ||
| 156 | + ) | ||
| 148 | 157 | ||
| 149 | try: | 158 | try: |
| 150 | # 2. 创建Session | 159 | # 2. 创建Session |
| @@ -154,9 +163,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 154 | graph_id = 1 | 163 | graph_id = 1 |
| 155 | ret = session.add_graph(graph_id, graph) | 164 | ret = session.add_graph(graph_id, graph) |
| 156 | if ret != 0: | 165 | if ret != 0: |
| 157 | - print(f"[Error] 添加图失败,返回码: {ret}") | 166 | + print(f"[Error] Failed to add graph, return code: {ret}") |
| 158 | return ret | 167 | return ret |
| 159 | - print(f"[Info] 图已添加到Session (Graph ID: {graph_id})") | 168 | + print(f"[Info] Graph added to Session (Graph ID: {graph_id})") |
| 160 | 169 | ||
| 161 | # 4. 准备输入数据 | 170 | # 4. 准备输入数据 |
| 162 | input_data_configs = [ | 171 | input_data_configs = [ |
| @@ -184,17 +193,17 @@ def run_graph(graph, device_id="0") -> int: | |||
| 184 | ) | 193 | ) |
| 185 | inputs.append(tensor) | 194 | inputs.append(tensor) |
| 186 | 195 | ||
| 187 | - print(f"[Info] 输入数据已准备,共{len(inputs)}个输入tensor") | 196 | + print(f"[Info] Prepared {len(inputs)} input tensor(s)") |
| 188 | 197 | ||
| 189 | # 5. 运行图 | 198 | # 5. 运行图 |
| 190 | ret = session.run_graph(graph_id, inputs) | 199 | ret = session.run_graph(graph_id, inputs) |
| 191 | - print("[Info] 图运行成功!") | 200 | + print("[Info] Graph executed successfully!") |
| 192 | for idx, tensor in enumerate(ret, start=1): | 201 | for idx, tensor in enumerate(ret, start=1): |
| 193 | - print(f"Tensor{idx}详情:{tensor}") | 202 | + print(f"Tensor{idx} details: {tensor}") |
| 194 | return 0 | 203 | return 0 |
| 195 | 204 | ||
| 196 | except Exception as e: | 205 | except Exception as e: |
| 197 | - print(f"[Error] 执行过程中出错: {e}") | 206 | + print(f"[Error] Error during execution: {e}") |
| 198 | import traceback | 207 | import traceback |
| 199 | 208 | ||
| 200 | traceback.print_exc() | 209 | traceback.print_exc() |
| @@ -202,9 +211,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 202 | 211 | ||
| 203 | finally: | 212 | finally: |
| 204 | # 6. 清理GE环境 | 213 | # 6. 清理GE环境 |
| 205 | - print("[Info] 清理GE环境...") | 214 | + print("[Info] Cleaning up GE environment...") |
| 206 | ge_api.ge_finalize() | 215 | ge_api.ge_finalize() |
| 207 | - print("[Success] GE环境已清理") | 216 | + print("[Success] GE environment cleaned up") |
| 208 | 217 | ||
| 209 | 218 | ||
| 210 | if __name__ == "__main__": | 219 | if __name__ == "__main__": |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeMatMulGraphByEsAndDump(); | 40 | es_showcase::MakeMatMulGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -23,10 +23,14 @@ def build_matmul_graph(): | |||
| 23 | # 1、创建图构建器 | 23 | # 1、创建图构建器 |
| 24 | builder = GraphBuilder("MakeMatMulGraph") | 24 | builder = GraphBuilder("MakeMatMulGraph") |
| 25 | # 2、创建图输入节点 | 25 | # 2、创建图输入节点 |
| 26 | - input_tensor_holder = builder.create_input(index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 3]) | 26 | + input_tensor_holder = builder.create_input( |
| 27 | + index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 3] | ||
| 28 | + ) | ||
| 27 | weight = builder.create_const_float([1.0] * 6, shape=[2, 3]) | 29 | weight = builder.create_const_float([1.0] * 6, shape=[2, 3]) |
| 28 | # transpose_x1 和 transpose_x2 为 MatMul 的属性 | 30 | # transpose_x1 和 transpose_x2 为 MatMul 的属性 |
| 29 | - matmul_tensor_holder = MatMul(weight, input_tensor_holder, None, transpose_x1=True, transpose_x2=False) | 31 | + matmul_tensor_holder = MatMul( |
| 32 | + weight, input_tensor_holder, None, transpose_x1=True, transpose_x2=False | ||
| 33 | + ) | ||
| 30 | # 3、设置图输出节点 | 34 | # 3、设置图输出节点 |
| 31 | builder.set_graph_output(matmul_tensor_holder, 0) | 35 | builder.set_graph_output(matmul_tensor_holder, 0) |
| 32 | # 4、构建图 | 36 | # 4、构建图 |
| @@ -44,10 +48,10 @@ def run_graph(graph, device_id="0") -> int: | |||
| 44 | ge_api = GeApi() | 48 | ge_api = GeApi() |
| 45 | ret = ge_api.ge_initialize(config) | 49 | ret = ge_api.ge_initialize(config) |
| 46 | if ret != 0: | 50 | if ret != 0: |
| 47 | - print(f"[Error] GE初始化失败,返回码: {ret}") | 51 | + print(f"[Error] GE initialization failed, return code: {ret}") |
| 48 | return ret | 52 | return ret |
| 49 | 53 | ||
| 50 | - print(f"[Info] GE环境初始化成功 (Device ID: {device_id})") | 54 | + print(f"[Info] GE environment initialized successfully (Device ID: {device_id})") |
| 51 | 55 | ||
| 52 | try: | 56 | try: |
| 53 | # 2. 创建Session | 57 | # 2. 创建Session |
| @@ -57,9 +61,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 57 | graph_id = 1 | 61 | graph_id = 1 |
| 58 | ret = session.add_graph(graph_id, graph) | 62 | ret = session.add_graph(graph_id, graph) |
| 59 | if ret != 0: | 63 | if ret != 0: |
| 60 | - print(f"[Error] 添加图失败,返回码: {ret}") | 64 | + print(f"[Error] Failed to add graph, return code: {ret}") |
| 61 | return ret | 65 | return ret |
| 62 | - print(f"[Info] 图已添加到Session (Graph ID: {graph_id})") | 66 | + print(f"[Info] Graph added to Session (Graph ID: {graph_id})") |
| 63 | 67 | ||
| 64 | # 4. 准备输入数据 | 68 | # 4. 准备输入数据 |
| 65 | input_data = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) | 69 | input_data = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) |
| @@ -73,17 +77,17 @@ def run_graph(graph, device_id="0") -> int: | |||
| 73 | ) | 77 | ) |
| 74 | 78 | ||
| 75 | inputs = [input_tensor] | 79 | inputs = [input_tensor] |
| 76 | - print(f"[Info] 输入数据已准备,共{len(inputs)}个输入tensor") | 80 | + print(f"[Info] Prepared {len(inputs)} input tensor(s)") |
| 77 | 81 | ||
| 78 | # 5. 运行图 | 82 | # 5. 运行图 |
| 79 | ret = session.run_graph(graph_id, inputs) | 83 | ret = session.run_graph(graph_id, inputs) |
| 80 | - print("[Info] 图运行成功!") | 84 | + print("[Info] Graph executed successfully!") |
| 81 | for idx, tensor in enumerate(ret, start=1): | 85 | for idx, tensor in enumerate(ret, start=1): |
| 82 | - print(f"Tensor{idx}详情:{tensor}") | 86 | + print(f"Tensor{idx} details: {tensor}") |
| 83 | return 0 | 87 | return 0 |
| 84 | 88 | ||
| 85 | except Exception as e: | 89 | except Exception as e: |
| 86 | - print(f"[Error] 执行过程中出错: {e}") | 90 | + print(f"[Error] Error during execution: {e}") |
| 87 | import traceback | 91 | import traceback |
| 88 | 92 | ||
| 89 | traceback.print_exc() | 93 | traceback.print_exc() |
| @@ -91,9 +95,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 91 | 95 | ||
| 92 | finally: | 96 | finally: |
| 93 | # 6. 清理GE环境 | 97 | # 6. 清理GE环境 |
| 94 | - print("[Info] 清理GE环境...") | 98 | + print("[Info] Cleaning up GE environment...") |
| 95 | ge_api.ge_finalize() | 99 | ge_api.ge_finalize() |
| 96 | - print("[Success] GE环境已清理") | 100 | + print("[Success] GE environment cleaned up") |
| 97 | 101 | ||
| 98 | 102 | ||
| 99 | if __name__ == "__main__": | 103 | if __name__ == "__main__": |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeReluAddGraphByEsAndDump(); | 40 | es_showcase::MakeReluAddGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -23,7 +23,9 @@ def build_input_graph(): | |||
| 23 | # 1、创建图构建器 | 23 | # 1、创建图构建器 |
| 24 | builder = GraphBuilder("MakeMatMulGraph") | 24 | builder = GraphBuilder("MakeMatMulGraph") |
| 25 | # 2、创建图输入节点 | 25 | # 2、创建图输入节点 |
| 26 | - input_tensor_holder1 = builder.create_input(index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 3]) | 26 | + input_tensor_holder1 = builder.create_input( |
| 27 | + index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 3] | ||
| 28 | + ) | ||
| 27 | relu1 = Relu(input_tensor_holder1) | 29 | relu1 = Relu(input_tensor_holder1) |
| 28 | relu2 = Relu(relu1) | 30 | relu2 = Relu(relu1) |
| 29 | add_tensor_holder = Add(input_tensor_holder1, relu2) | 31 | add_tensor_holder = Add(input_tensor_holder1, relu2) |
| @@ -44,10 +46,10 @@ def run_graph(graph, device_id="0") -> int: | |||
| 44 | ge_api = GeApi() | 46 | ge_api = GeApi() |
| 45 | ret = ge_api.ge_initialize(config) | 47 | ret = ge_api.ge_initialize(config) |
| 46 | if ret != 0: | 48 | if ret != 0: |
| 47 | - print(f"[Error] GE初始化失败,返回码: {ret}") | 49 | + print(f"[Error] GE initialization failed, return code: {ret}") |
| 48 | return ret | 50 | return ret |
| 49 | 51 | ||
| 50 | - print(f"[Info] GE环境初始化成功 (Device ID: {device_id})") | 52 | + print(f"[Info] GE environment initialized successfully (Device ID: {device_id})") |
| 51 | 53 | ||
| 52 | try: | 54 | try: |
| 53 | # 2. 创建Session | 55 | # 2. 创建Session |
| @@ -57,9 +59,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 57 | graph_id = 1 | 59 | graph_id = 1 |
| 58 | ret = session.add_graph(graph_id, graph) | 60 | ret = session.add_graph(graph_id, graph) |
| 59 | if ret != 0: | 61 | if ret != 0: |
| 60 | - print(f"[Error] 添加图失败,返回码: {ret}") | 62 | + print(f"[Error] Failed to add graph, return code: {ret}") |
| 61 | return ret | 63 | return ret |
| 62 | - print(f"[Info] 图已添加到Session (Graph ID: {graph_id})") | 64 | + print(f"[Info] Graph added to Session (Graph ID: {graph_id})") |
| 63 | 65 | ||
| 64 | # 4. 准备输入数据 | 66 | # 4. 准备输入数据 |
| 65 | input_data = np.full([2, 3], 1.0, dtype=np.float32) | 67 | input_data = np.full([2, 3], 1.0, dtype=np.float32) |
| @@ -72,17 +74,17 @@ def run_graph(graph, device_id="0") -> int: | |||
| 72 | ) | 74 | ) |
| 73 | 75 | ||
| 74 | inputs = [input_tensor] | 76 | inputs = [input_tensor] |
| 75 | - print(f"[Info] 输入数据已准备,共{len(inputs)}个输入tensor") | 77 | + print(f"[Info] Prepared {len(inputs)} input tensor(s)") |
| 76 | 78 | ||
| 77 | # 5. 运行图 | 79 | # 5. 运行图 |
| 78 | ret = session.run_graph(graph_id, inputs) | 80 | ret = session.run_graph(graph_id, inputs) |
| 79 | - print("[Info] 图运行成功!") | 81 | + print("[Info] Graph executed successfully!") |
| 80 | for idx, tensor in enumerate(ret, start=1): | 82 | for idx, tensor in enumerate(ret, start=1): |
| 81 | - print(f"Tensor{idx}详情:{tensor}") | 83 | + print(f"Tensor{idx} details: {tensor}") |
| 82 | return 0 | 84 | return 0 |
| 83 | 85 | ||
| 84 | except Exception as e: | 86 | except Exception as e: |
| 85 | - print(f"[Error] 执行过程中出错: {e}") | 87 | + print(f"[Error] Error during execution: {e}") |
| 86 | import traceback | 88 | import traceback |
| 87 | 89 | ||
| 88 | traceback.print_exc() | 90 | traceback.print_exc() |
| @@ -90,9 +92,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 90 | 92 | ||
| 91 | finally: | 93 | finally: |
| 92 | # 6. 清理GE环境 | 94 | # 6. 清理GE环境 |
| 93 | - print("[Info] 清理GE环境...") | 95 | + print("[Info] Cleaning up GE environment...") |
| 94 | ge_api.ge_finalize() | 96 | ge_api.ge_finalize() |
| 95 | - print("[Success] GE环境已清理") | 97 | + print("[Success] GE environment cleaned up") |
| 96 | 98 | ||
| 97 | 99 | ||
| 98 | if __name__ == "__main__": | 100 | if __name__ == "__main__": |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeAddSubMulDivGraphByEsAndDump(); | 40 | es_showcase::MakeAddSubMulDivGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -22,11 +22,19 @@ def build_overload_graph(): | |||
| 22 | # 1、创建图构建器 | 22 | # 1、创建图构建器 |
| 23 | builder = GraphBuilder("MakeAddGraph") | 23 | builder = GraphBuilder("MakeAddGraph") |
| 24 | # 2、创建图输入节点 | 24 | # 2、创建图输入节点 |
| 25 | - input_tensor_holder1 = builder.create_input(index=0, name="input1", data_type=DataType.DT_FLOAT, shape=[2, 3]) | 25 | + input_tensor_holder1 = builder.create_input( |
| 26 | - input_tensor_holder2 = builder.create_input(index=1, name="input2", data_type=DataType.DT_INT64, shape=[2, 3]) | 26 | + index=0, name="input1", data_type=DataType.DT_FLOAT, shape=[2, 3] |
| 27 | - input_tensor_holder3 = builder.create_input(index=2, name="input3", data_type=DataType.DT_INT64, shape=[2, 3]) | 27 | + ) |
| 28 | + input_tensor_holder2 = builder.create_input( | ||
| 29 | + index=1, name="input2", data_type=DataType.DT_INT64, shape=[2, 3] | ||
| 30 | + ) | ||
| 31 | + input_tensor_holder3 = builder.create_input( | ||
| 32 | + index=2, name="input3", data_type=DataType.DT_INT64, shape=[2, 3] | ||
| 33 | + ) | ||
| 28 | # 操作符重载 | 34 | # 操作符重载 |
| 29 | - add_tensor_holder2 = input_tensor_holder1 + input_tensor_holder2 + input_tensor_holder3 | 35 | + add_tensor_holder2 = ( |
| 36 | + input_tensor_holder1 + input_tensor_holder2 + input_tensor_holder3 | ||
| 37 | + ) | ||
| 30 | # 3、设置图输出节点 | 38 | # 3、设置图输出节点 |
| 31 | builder.set_graph_output(add_tensor_holder2, 0) | 39 | builder.set_graph_output(add_tensor_holder2, 0) |
| 32 | # 4、构建图 | 40 | # 4、构建图 |
| @@ -45,9 +53,9 @@ def run_graph(graph) -> int: | |||
| 45 | ge_api = GeApi() | 53 | ge_api = GeApi() |
| 46 | ret = ge_api.ge_initialize(config) | 54 | ret = ge_api.ge_initialize(config) |
| 47 | if ret != 0: | 55 | if ret != 0: |
| 48 | - print(f"GE初始化失败,返回码: {ret}") | 56 | + print(f"GE initialization failed, return code: {ret}") |
| 49 | return ret | 57 | return ret |
| 50 | - print("GE环境初始化成功 (Device ID: 0)") | 58 | + print("GE environment initialized successfully (Device ID: 0)") |
| 51 | 59 | ||
| 52 | try: | 60 | try: |
| 53 | # 2. 创建Session | 61 | # 2. 创建Session |
| @@ -56,9 +64,9 @@ def run_graph(graph) -> int: | |||
| 56 | graph_id = 1 | 64 | graph_id = 1 |
| 57 | ret = session.add_graph(graph_id, graph) | 65 | ret = session.add_graph(graph_id, graph) |
| 58 | if ret != 0: | 66 | if ret != 0: |
| 59 | - print(f"添加图失败,返回码: {ret}") | 67 | + print(f"Failed to add graph, return code: {ret}") |
| 60 | return ret | 68 | return ret |
| 61 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 69 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 62 | 70 | ||
| 63 | # 4. 准备输入数据 | 71 | # 4. 准备输入数据 |
| 64 | tensor1 = Tensor( | 72 | tensor1 = Tensor( |
| @@ -85,22 +93,22 @@ def run_graph(graph) -> int: | |||
| 85 | input_tensor = [tensor1, tensor2, tensor3] | 93 | input_tensor = [tensor1, tensor2, tensor3] |
| 86 | # 5. 运行图 | 94 | # 5. 运行图 |
| 87 | ret = session.run_graph(graph_id, input_tensor) | 95 | ret = session.run_graph(graph_id, input_tensor) |
| 88 | - print("[Info] 图运行成功!") | 96 | + print("[Info] Graph executed successfully!") |
| 89 | for idx, tensor in enumerate(ret, start=1): | 97 | for idx, tensor in enumerate(ret, start=1): |
| 90 | - print(f"Tensor{idx}详情:{tensor}") | 98 | + print(f"Tensor{idx} details: {tensor}") |
| 91 | return 0 | 99 | return 0 |
| 92 | 100 | ||
| 93 | except Exception as e: | 101 | except Exception as e: |
| 94 | - print(f"[Error] 执行过程中出错: {e}") | 102 | + print(f"[Error] Error during execution: {e}") |
| 95 | import traceback | 103 | import traceback |
| 96 | 104 | ||
| 97 | traceback.print_exc() | 105 | traceback.print_exc() |
| 98 | return -1 | 106 | return -1 |
| 99 | finally: | 107 | finally: |
| 100 | # 6. 清理GE环境 | 108 | # 6. 清理GE环境 |
| 101 | - print("[Info] 清理GE环境...") | 109 | + print("[Info] Cleaning up GE environment...") |
| 102 | ge_api.ge_finalize() | 110 | ge_api.ge_finalize() |
| 103 | - print("[Success] GE环境已清理") | 111 | + print("[Success] GE environment cleaned up") |
| 104 | 112 | ||
| 105 | 113 | ||
| 106 | graph = build_overload_graph() | 114 | graph = build_overload_graph() |
| @@ -26,28 +26,28 @@ int main(int argc, char **argv) { | |||
| 26 | std::string command = argv[1]; | 26 | std::string command = argv[1]; |
| 27 | if (command == "run") { | 27 | if (command == "run") { |
| 28 | if (argc >= 3) { | 28 | if (argc >= 3) { |
| 29 | - std::cerr << "错误: run 命令不需要 mode 参数,当前固定为 copy 模式\n"; | 29 | + std::cerr << "Error: run command does not accept a mode argument, currently fixed to copy mode\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | - std::cout << "运行模式: copy" << std::endl; | 32 | + std::cout << "Run mode: copy" << std::endl; |
| 33 | constexpr int32_t kDeviceId = 0; | 33 | constexpr int32_t kDeviceId = 0; |
| 34 | const std::string device_id = std::to_string(kDeviceId); | 34 | const std::string device_id = std::to_string(kDeviceId); |
| 35 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", ge::AscendString(device_id.c_str())}, | 35 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", ge::AscendString(device_id.c_str())}, |
| 36 | {"ge.graphRunMode", "0"}}; | 36 | {"ge.graphRunMode", "0"}}; |
| 37 | auto ret = ge::GEInitialize(config); | 37 | auto ret = ge::GEInitialize(config); |
| 38 | if (ret != ge::SUCCESS) { | 38 | if (ret != ge::SUCCESS) { |
| 39 | - std::cerr << "GE 初始化失败\n"; | 39 | + std::cerr << "GE initialization failed\n"; |
| 40 | return -1; | 40 | return -1; |
| 41 | } | 41 | } |
| 42 | const aclError acl_init_ret = aclInit(nullptr); | 42 | const aclError acl_init_ret = aclInit(nullptr); |
| 43 | if (acl_init_ret != ACL_SUCCESS) { | 43 | if (acl_init_ret != ACL_SUCCESS) { |
| 44 | - std::cerr << "ACL 初始化失败, error=" << acl_init_ret << std::endl; | 44 | + std::cerr << "ACL initialization failed, error=" << acl_init_ret << std::endl; |
| 45 | (void)ge::GEFinalize(); | 45 | (void)ge::GEFinalize(); |
| 46 | return -1; | 46 | return -1; |
| 47 | } | 47 | } |
| 48 | const aclError set_device_ret = aclrtSetDevice(kDeviceId); | 48 | const aclError set_device_ret = aclrtSetDevice(kDeviceId); |
| 49 | if (set_device_ret != ACL_SUCCESS) { | 49 | if (set_device_ret != ACL_SUCCESS) { |
| 50 | - std::cerr << "aclrtSetDevice 失败, error=" << set_device_ret << std::endl; | 50 | + std::cerr << "aclrtSetDevice failed, error=" << set_device_ret << std::endl; |
| 51 | (void)aclFinalize(); | 51 | (void)aclFinalize(); |
| 52 | (void)ge::GEFinalize(); | 52 | (void)ge::GEFinalize(); |
| 53 | return -1; | 53 | return -1; |
| @@ -57,18 +57,18 @@ int main(int argc, char **argv) { | |||
| 57 | result = 0; | 57 | result = 0; |
| 58 | } | 58 | } |
| 59 | if (ge::GEFinalize() != ge::SUCCESS) { | 59 | if (ge::GEFinalize() != ge::SUCCESS) { |
| 60 | - std::cerr << "GE 反初始化失败\n"; | 60 | + std::cerr << "GE finalization failed\n"; |
| 61 | result = -1; | 61 | result = -1; |
| 62 | } | 62 | } |
| 63 | (void)aclrtResetDevice(kDeviceId); | 63 | (void)aclrtResetDevice(kDeviceId); |
| 64 | (void)aclFinalize(); | 64 | (void)aclFinalize(); |
| 65 | - std::cout << "执行结束" << std::endl; | 65 | + std::cout << "Execution completed" << std::endl; |
| 66 | return result; | 66 | return result; |
| 67 | } else if (command == "dump") { | 67 | } else if (command == "dump") { |
| 68 | es_showcase::MakeAddGraphByEsAndDump(); | 68 | es_showcase::MakeAddGraphByEsAndDump(); |
| 69 | return 0; | 69 | return 0; |
| 70 | } else { | 70 | } else { |
| 71 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 71 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 72 | return -1; | 72 | return -1; |
| 73 | } | 73 | } |
| 74 | } | 74 | } |
| @@ -45,7 +45,7 @@ while [[ $# -gt 0 ]]; do | |||
| 45 | usage | 45 | usage |
| 46 | ;; | 46 | ;; |
| 47 | *) | 47 | *) |
| 48 | - echo "未知选项: $1" >&2 | 48 | + echo "Unknown option: $1" >&2 |
| 49 | usage | 49 | usage |
| 50 | exit 1 | 50 | exit 1 |
| 51 | ;; | 51 | ;; |
| @@ -55,17 +55,17 @@ done | |||
| 55 | # 验证目标有效性 | 55 | # 验证目标有效性 |
| 56 | VALID_TARGETS=("sample" "sample_and_run") | 56 | VALID_TARGETS=("sample" "sample_and_run") |
| 57 | if [[ ! " ${VALID_TARGETS[@]} " =~ " ${TARGET} " ]]; then | 57 | if [[ ! " ${VALID_TARGETS[@]} " =~ " ${TARGET} " ]]; then |
| 58 | - echo "错误: 无效目标 '${TARGET}'。有效目标: ${VALID_TARGETS[*]}" >&2 | 58 | + echo "Error: invalid target '${TARGET}'. Valid targets: ${VALID_TARGETS[*]}" >&2 |
| 59 | exit 1 | 59 | exit 1 |
| 60 | fi | 60 | fi |
| 61 | 61 | ||
| 62 | -echo "[Info] 目标设置为: ${TARGET}" | 62 | +echo "[Info] Target set to: ${TARGET}" |
| 63 | -echo "[Info] 运行模式设置为: copy输入输出" | 63 | +echo "[Info] Run mode set to: copy input/output" |
| 64 | 64 | ||
| 65 | set +u | 65 | set +u |
| 66 | if [[ -z "${ASCEND_HOME_PATH}" ]]; then | 66 | if [[ -z "${ASCEND_HOME_PATH}" ]]; then |
| 67 | - echo -e "ERROR 环境变量ASCEND_HOME_PATH 未配置" >&2 | 67 | + echo -e "ERROR Environment variable ASCEND_HOME_PATH is not set" >&2 |
| 68 | - echo -e "ERROR 请先执行: source /usr/local/Ascend/cann/set_env.sh " >&2 | 68 | + echo -e "ERROR Please source the environment first: source /usr/local/Ascend/cann/set_env.sh " >&2 |
| 69 | exit 1 | 69 | exit 1 |
| 70 | fi | 70 | fi |
| 71 | 71 | ||
| @@ -81,79 +81,79 @@ case "${ARCH}" in | |||
| 81 | ASCEND_ARCH="aarch64-linux" | 81 | ASCEND_ARCH="aarch64-linux" |
| 82 | ;; | 82 | ;; |
| 83 | *) | 83 | *) |
| 84 | - echo "WARNING: 未识别的架构 ${ARCH},使用默认值 x86_64-linux" >&2 | 84 | + echo "WARNING: Unrecognized architecture ${ARCH}, using default x86_64-linux" >&2 |
| 85 | ASCEND_ARCH="x86_64-linux" | 85 | ASCEND_ARCH="x86_64-linux" |
| 86 | ;; | 86 | ;; |
| 87 | esac | 87 | esac |
| 88 | 88 | ||
| 89 | -echo "[Info] 检测到系统架构: ${ARCH}" | 89 | +echo "[Info] Detected architecture: ${ARCH}" |
| 90 | -echo "[Info] 使用 ASCEND 架构: ${ASCEND_ARCH}" | 90 | +echo "[Info] Using ASCEND architecture: ${ASCEND_ARCH}" |
| 91 | 91 | ||
| 92 | ASCEND_LIB_DIR="${ASCEND_HOME_PATH}/lib64" | 92 | ASCEND_LIB_DIR="${ASCEND_HOME_PATH}/lib64" |
| 93 | echo "[Info] ASCEND_LIB_DIR = ${ASCEND_LIB_DIR}" | 93 | echo "[Info] ASCEND_LIB_DIR = ${ASCEND_LIB_DIR}" |
| 94 | 94 | ||
| 95 | # 预先设置 LD_LIBRARY_PATH,保证 gen_esb 能加载 | 95 | # 预先设置 LD_LIBRARY_PATH,保证 gen_esb 能加载 |
| 96 | export LD_LIBRARY_PATH="${ASCEND_LIB_DIR}:${LD_LIBRARY_PATH:-}" | 96 | export LD_LIBRARY_PATH="${ASCEND_LIB_DIR}:${LD_LIBRARY_PATH:-}" |
| 97 | -echo "[Info] 预先设置 LD_LIBRARY_PATH=${LD_LIBRARY_PATH} 以支持 gen_esb 运行" | 97 | +echo "[Info] LD_LIBRARY_PATH preset to ${LD_LIBRARY_PATH} for gen_esb" |
| 98 | 98 | ||
| 99 | # ---------- 3. 生成 build 目录 ---------- | 99 | # ---------- 3. 生成 build 目录 ---------- |
| 100 | BUILD_DIR="build" | 100 | BUILD_DIR="build" |
| 101 | if [[ ! -d "${BUILD_DIR}" ]]; then | 101 | if [[ ! -d "${BUILD_DIR}" ]]; then |
| 102 | - echo "[Info] 创建构建目录 ${BUILD_DIR}" | 102 | + echo "[Info] Creating build directory ${BUILD_DIR}" |
| 103 | mkdir -p "${BUILD_DIR}" | 103 | mkdir -p "${BUILD_DIR}" |
| 104 | fi | 104 | fi |
| 105 | 105 | ||
| 106 | # ---------- 5. 设置 LD_LIBRARY_PATH ---------- | 106 | # ---------- 5. 设置 LD_LIBRARY_PATH ---------- |
| 107 | export LD_LIBRARY_PATH="${ASCEND_LIB_DIR}:${LD_LIBRARY_PATH:-}" | 107 | export LD_LIBRARY_PATH="${ASCEND_LIB_DIR}:${LD_LIBRARY_PATH:-}" |
| 108 | -echo "[Info] LD_LIBRARY_PATH 已设置为: ${LD_LIBRARY_PATH}" | 108 | +echo "[Info] LD_LIBRARY_PATH set to: ${LD_LIBRARY_PATH}" |
| 109 | # ---------- 6. 运行指定目标 ---------- | 109 | # ---------- 6. 运行指定目标 ---------- |
| 110 | case "${TARGET}" in | 110 | case "${TARGET}" in |
| 111 | sample) | 111 | sample) |
| 112 | - echo "[Info] 开始准备并编译目标: sample" | 112 | + echo "[Info] Preparing and building target: sample" |
| 113 | - echo "[Info] 清理旧的 ${BUILD_DIR}..." | 113 | + echo "[Info] Cleaning old ${BUILD_DIR}..." |
| 114 | [ -n "${BUILD_DIR}" ] && rm -rf "${BUILD_DIR}" || true | 114 | [ -n "${BUILD_DIR}" ] && rm -rf "${BUILD_DIR}" || true |
| 115 | mkdir -p "${BUILD_DIR}" | 115 | mkdir -p "${BUILD_DIR}" |
| 116 | cmake -S . -B "${BUILD_DIR}" -DCMAKE_BUILD_TYPE=Release | 116 | cmake -S . -B "${BUILD_DIR}" -DCMAKE_BUILD_TYPE=Release |
| 117 | cmake --build "${BUILD_DIR}" --target sample -j"$(nproc)" | 117 | cmake --build "${BUILD_DIR}" --target sample -j"$(nproc)" |
| 118 | 118 | ||
| 119 | - echo "[Info] 运行 ${BUILD_DIR}/sample dump" | 119 | + echo "[Info] Running ${BUILD_DIR}/sample dump" |
| 120 | if [[ -x "${BUILD_DIR}/sample" ]]; then | 120 | if [[ -x "${BUILD_DIR}/sample" ]]; then |
| 121 | "${BUILD_DIR}/sample" dump | 121 | "${BUILD_DIR}/sample" dump |
| 122 | - echo "[Success] sample 执行成功,pbtxt dump 已生成在当前目录。该文件以 ge_onnx_ 开头,可以在 netron 中打开显示" | 122 | + echo "[Success] sample execution succeeded, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be viewed in netron." |
| 123 | else | 123 | else |
| 124 | - echo "ERROR: 找不到或不可执行 ${BUILD_DIR}/sample" >&2 | 124 | + echo "ERROR: ${BUILD_DIR}/sample not found or not executable" >&2 |
| 125 | exit 1 | 125 | exit 1 |
| 126 | fi | 126 | fi |
| 127 | ;; | 127 | ;; |
| 128 | sample_and_run) | 128 | sample_and_run) |
| 129 | - echo "[Info] 开始准备并编译目标: sample_and_run" | 129 | + echo "[Info] Preparing and building target: sample_and_run" |
| 130 | bash "$0" -t sample | 130 | bash "$0" -t sample |
| 131 | - echo "[Info] 设置NPU设备下的环境变量 ${ASCEND_HOME_PATH}/set_env.sh" | 131 | + echo "[Info] Setting NPU device environment ${ASCEND_HOME_PATH}/set_env.sh" |
| 132 | - echo "[Info] 检查环境变量和文件" | 132 | + echo "[Info] Checking environment variables and files" |
| 133 | if [ -z "${ASCEND_HOME_PATH:-}" ]; then | 133 | if [ -z "${ASCEND_HOME_PATH:-}" ]; then |
| 134 | - echo "[Error] ASCEND_HOME_PATH 未设置" | 134 | + echo "[Error] ASCEND_HOME_PATH is not set" |
| 135 | exit 1 | 135 | exit 1 |
| 136 | fi | 136 | fi |
| 137 | 137 | ||
| 138 | if [ -z "${ASCEND_ARCH:-}" ]; then | 138 | if [ -z "${ASCEND_ARCH:-}" ]; then |
| 139 | - echo "[Error] ASCEND_ARCH 未设置" | 139 | + echo "[Error] ASCEND_ARCH is not set" |
| 140 | exit 1 | 140 | exit 1 |
| 141 | fi | 141 | fi |
| 142 | 142 | ||
| 143 | SETENV_FILE="${ASCEND_HOME_PATH}/set_env.sh" | 143 | SETENV_FILE="${ASCEND_HOME_PATH}/set_env.sh" |
| 144 | if [ ! -f "$SETENV_FILE" ]; then | 144 | if [ ! -f "$SETENV_FILE" ]; then |
| 145 | - echo "[Error] set_env.sh 不存在: $SETENV_FILE" | 145 | + echo "[Error] set_env.sh does not exist: $SETENV_FILE" |
| 146 | exit 1 | 146 | exit 1 |
| 147 | fi | 147 | fi |
| 148 | # 临时禁用错误退出进行 source | 148 | # 临时禁用错误退出进行 source |
| 149 | set +e | 149 | set +e |
| 150 | source "$SETENV_FILE" | 150 | source "$SETENV_FILE" |
| 151 | set -e | 151 | set -e |
| 152 | - echo "[Info] 运行 ${BUILD_DIR}/sample run" | 152 | + echo "[Info] Running ${BUILD_DIR}/sample run" |
| 153 | - "${BUILD_DIR}/sample" run && echo "[Success] sample_and_run 执行成功,pbtxt和data输出dump 已生成在当前目录" | 153 | + "${BUILD_DIR}/sample" run && echo "[Success] sample_and_run execution succeeded, pbtxt and data output dump generated in current directory" |
| 154 | ;; | 154 | ;; |
| 155 | *) | 155 | *) |
| 156 | - echo "错误: 未知目标 ${TARGET}" >&2 | 156 | + echo "Error: unknown target ${TARGET}" >&2 |
| 157 | exit 1 | 157 | exit 1 |
| 158 | ;; | 158 | ;; |
| 159 | esac | 159 | esac |
| @@ -46,14 +46,14 @@ bash run_sample.sh -t sample_and_run_python_custom_allocator | |||
| 46 | 46 | ||
| 47 | 执行成功后,终端会出现以下格式的打印(具体行数取决于 GE 对输出 Tensor 的分配/释放;地址与 `size` 以实际运行为准): | 47 | 执行成功后,终端会出现以下格式的打印(具体行数取决于 GE 对输出 Tensor 的分配/释放;地址与 `size` 以实际运行为准): |
| 48 | ``` | 48 | ``` |
| 49 | -[Info] SamplePoolAllocator 已注册到 stream | 49 | +[Info] SamplePoolAllocator registered to stream |
| 50 | -[SamplePool] new : addr=0x<设备内存地址> size=<字节数> B | 50 | +[SamplePool] new : addr=0x<addr> size=<N> B |
| 51 | -[Info] 异步执行 Graph 成功! | 51 | +[Info] Asynchronous execution of the graph succeeded. |
| 52 | -[SamplePool] cache : addr=0x<设备内存地址> size=<字节数> B | 52 | +[SamplePool] cache : addr=0x<addr> size=<N> B |
| 53 | -[Info] SamplePoolAllocator 已注销 | 53 | +[Info] SamplePoolAllocator unregistered |
| 54 | [SamplePool] destroy: freed <N> cached blocks | 54 | [SamplePool] destroy: freed <N> cached blocks |
| 55 | -[Info] 运行环境已清理 | 55 | +[Info] Runtime environment cleaned up |
| 56 | -[Success] sample 执行成功,pbtxt dump 已生成在当前目录。该文件以 ge_onnx_ 开头,可以在 netron 中打开显示 | 56 | +[Success] sample execution succeeded, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be viewed in netron. |
| 57 | ``` | 57 | ``` |
| 58 | #### 输出文件说明 | 58 | #### 输出文件说明 |
| 59 | 59 | ||
| @@ -62,11 +62,11 @@ After successful execution, terminal will show output in following format (speci | |||
| 62 | ```text | 62 | ```text |
| 63 | [Info] SamplePoolAllocator registered to stream | 63 | [Info] SamplePoolAllocator registered to stream |
| 64 | [SamplePool] new : addr=0x<device memory address> size=<byte count> B | 64 | [SamplePool] new : addr=0x<device memory address> size=<byte count> B |
| 65 | -[Info] Asynchronous Graph execution successful! | 65 | +[Info] Asynchronous execution of the graph succeeded. |
| 66 | [SamplePool] cache : addr=0x<device memory address> size=<byte count> B | 66 | [SamplePool] cache : addr=0x<device memory address> size=<byte count> B |
| 67 | [Info] SamplePoolAllocator unregistered | 67 | [Info] SamplePoolAllocator unregistered |
| 68 | [SamplePool] destroy: freed <N> cached blocks | 68 | [SamplePool] destroy: freed <N> cached blocks |
| 69 | -[Info] Running environment cleaned | 69 | +[Info] Runtime environment cleaned up |
| 70 | [Success] sample execution successful, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be opened in netron for display | 70 | [Success] sample execution successful, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be opened in netron for display |
| 71 | ``` | 71 | ``` |
| 72 | 72 | ||
| @@ -41,7 +41,7 @@ while [[ $# -gt 0 ]]; do | |||
| 41 | usage | 41 | usage |
| 42 | ;; | 42 | ;; |
| 43 | *) | 43 | *) |
| 44 | - echo "未知选项: $1" >&2 | 44 | + echo "Unknown option: $1" >&2 |
| 45 | usage | 45 | usage |
| 46 | exit 1 | 46 | exit 1 |
| 47 | ;; | 47 | ;; |
| @@ -51,17 +51,17 @@ done | |||
| 51 | # 验证目标有效性 | 51 | # 验证目标有效性 |
| 52 | VALID_TARGETS=("sample_and_run_python" "sample_and_run_python_custom_allocator") | 52 | VALID_TARGETS=("sample_and_run_python" "sample_and_run_python_custom_allocator") |
| 53 | if [[ ! " ${VALID_TARGETS[@]} " =~ " ${TARGET} " ]]; then | 53 | if [[ ! " ${VALID_TARGETS[@]} " =~ " ${TARGET} " ]]; then |
| 54 | - echo "错误: 无效目标 '${TARGET}'。有效目标: ${VALID_TARGETS[*]}" >&2 | 54 | + echo "Error: invalid target '${TARGET}'. Valid targets: ${VALID_TARGETS[*]}" >&2 |
| 55 | exit 1 | 55 | exit 1 |
| 56 | fi | 56 | fi |
| 57 | 57 | ||
| 58 | -echo "[Info] 目标设置为: ${TARGET}" | 58 | +echo "[Info] Target set to: ${TARGET}" |
| 59 | -echo "[Info] 测试用例设置为: add" | 59 | +echo "[Info] Test case set to: add" |
| 60 | 60 | ||
| 61 | set +u | 61 | set +u |
| 62 | if [[ -z "${ASCEND_HOME_PATH}" ]]; then | 62 | if [[ -z "${ASCEND_HOME_PATH}" ]]; then |
| 63 | - echo -e "ERROR 环境变量ASCEND_HOME_PATH 未配置" >&2 | 63 | + echo -e "ERROR Environment variable ASCEND_HOME_PATH is not set" >&2 |
| 64 | - echo -e "ERROR 请先执行: source /usr/local/Ascend/cann/set_env.sh " >&2 | 64 | + echo -e "ERROR Please source the environment first: source /usr/local/Ascend/cann/set_env.sh " >&2 |
| 65 | exit 1 | 65 | exit 1 |
| 66 | fi | 66 | fi |
| 67 | 67 | ||
| @@ -76,33 +76,33 @@ case "${ARCH}" in | |||
| 76 | ASCEND_ARCH="aarch64-linux" | 76 | ASCEND_ARCH="aarch64-linux" |
| 77 | ;; | 77 | ;; |
| 78 | *) | 78 | *) |
| 79 | - echo "WARNING: 未识别的架构 ${ARCH},使用默认值 x86_64-linux" >&2 | 79 | + echo "WARNING: Unrecognized architecture ${ARCH}, using default x86_64-linux" >&2 |
| 80 | ASCEND_ARCH="x86_64-linux" | 80 | ASCEND_ARCH="x86_64-linux" |
| 81 | ;; | 81 | ;; |
| 82 | esac | 82 | esac |
| 83 | 83 | ||
| 84 | -echo "[Info] 检测到系统架构: ${ARCH}" | 84 | +echo "[Info] Detected architecture: ${ARCH}" |
| 85 | -echo "[Info] 使用 ASCEND 架构: ${ASCEND_ARCH}" | 85 | +echo "[Info] Using ASCEND architecture: ${ASCEND_ARCH}" |
| 86 | 86 | ||
| 87 | ASCEND_LIB_DIR="${ASCEND_HOME_PATH}/lib64" | 87 | ASCEND_LIB_DIR="${ASCEND_HOME_PATH}/lib64" |
| 88 | echo "[Info] ASCEND_LIB_DIR = ${ASCEND_LIB_DIR}" | 88 | echo "[Info] ASCEND_LIB_DIR = ${ASCEND_LIB_DIR}" |
| 89 | 89 | ||
| 90 | export LD_LIBRARY_PATH="${ASCEND_LIB_DIR}:${LD_LIBRARY_PATH:-}" | 90 | export LD_LIBRARY_PATH="${ASCEND_LIB_DIR}:${LD_LIBRARY_PATH:-}" |
| 91 | -echo "[Info] LD_LIBRARY_PATH 已设置为: ${LD_LIBRARY_PATH}" | 91 | +echo "[Info] LD_LIBRARY_PATH set to: ${LD_LIBRARY_PATH}" |
| 92 | 92 | ||
| 93 | # ---------- 运行单个 Python 文件 ---------- | 93 | # ---------- 运行单个 Python 文件 ---------- |
| 94 | run_python_file() { | 94 | run_python_file() { |
| 95 | local py_file="$1" | 95 | local py_file="$1" |
| 96 | if [[ ! -f "${py_file}" ]]; then | 96 | if [[ ! -f "${py_file}" ]]; then |
| 97 | - echo "[Error] 未找到 Python 用例 ${py_file}" >&2 | 97 | + echo "[Error] Python test file not found: ${py_file}" >&2 |
| 98 | return 1 | 98 | return 1 |
| 99 | fi | 99 | fi |
| 100 | - echo "[Info] 运行:${py_file}" | 100 | + echo "[Info] Running: ${py_file}" |
| 101 | if python3 "${py_file}"; then | 101 | if python3 "${py_file}"; then |
| 102 | - echo "[Success] ${py_file} 执行成功" | 102 | + echo "[Success] ${py_file} execution succeeded" |
| 103 | return 0 | 103 | return 0 |
| 104 | else | 104 | else |
| 105 | - echo "[Error] ${py_file} 执行失败" >&2 | 105 | + echo "[Error] ${py_file} execution failed" >&2 |
| 106 | return 1 | 106 | return 1 |
| 107 | fi | 107 | fi |
| 108 | } | 108 | } |
| @@ -110,22 +110,22 @@ run_python_file() { | |||
| 110 | case "${TARGET}" in | 110 | case "${TARGET}" in |
| 111 | sample_and_run_python) | 111 | sample_and_run_python) |
| 112 | if run_python_file "src/make_add_graph.py"; then | 112 | if run_python_file "src/make_add_graph.py"; then |
| 113 | - echo "[Success] sample 执行成功,pbtxt dump 已生成在当前目录。该文件以 ge_onnx_ 开头,可以在 netron 中打开显示" | 113 | + echo "[Success] sample execution succeeded, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be viewed in netron." |
| 114 | else | 114 | else |
| 115 | - echo "[Error] sample 执行失败,请检查上述错误信息" >&2 | 115 | + echo "[Error] sample execution failed, check the error messages above" >&2 |
| 116 | exit 1 | 116 | exit 1 |
| 117 | fi | 117 | fi |
| 118 | ;; | 118 | ;; |
| 119 | sample_and_run_python_custom_allocator) | 119 | sample_and_run_python_custom_allocator) |
| 120 | if run_python_file "src/make_add_graph_custom_allocator.py"; then | 120 | if run_python_file "src/make_add_graph_custom_allocator.py"; then |
| 121 | - echo "[Success] sample 执行成功,pbtxt dump 已生成在当前目录。该文件以 ge_onnx_ 开头,可以在 netron 中打开显示" | 121 | + echo "[Success] sample execution succeeded, pbtxt dump generated in current directory. The file starts with ge_onnx_ and can be viewed in netron." |
| 122 | else | 122 | else |
| 123 | - echo "[Error] sample 执行失败,请检查上述错误信息" >&2 | 123 | + echo "[Error] sample execution failed, check the error messages above" >&2 |
| 124 | exit 1 | 124 | exit 1 |
| 125 | fi | 125 | fi |
| 126 | ;; | 126 | ;; |
| 127 | *) | 127 | *) |
| 128 | - echo "错误: 未知目标 ${TARGET}" >&2 | 128 | + echo "Error: unknown target ${TARGET}" >&2 |
| 129 | exit 1 | 129 | exit 1 |
| 130 | ;; | 130 | ;; |
| 131 | esac | 131 | esac |
| @@ -36,9 +36,15 @@ def check_ret(name: str, ret: int) -> None: | |||
| 36 | def build_overload_graph() -> Graph: | 36 | def build_overload_graph() -> Graph: |
| 37 | """使用操作符重载构建静态 shape 加法图(shape 固定为 [2, 3])。""" | 37 | """使用操作符重载构建静态 shape 加法图(shape 固定为 [2, 3])。""" |
| 38 | builder = GraphBuilder("MakeAddGraph") | 38 | builder = GraphBuilder("MakeAddGraph") |
| 39 | - h1 = builder.create_input(index=0, name="input1", data_type=DataType.DT_FLOAT, shape=[2, 3]) | 39 | + h1 = builder.create_input( |
| 40 | - h2 = builder.create_input(index=1, name="input2", data_type=DataType.DT_INT64, shape=[2, 3]) | 40 | + index=0, name="input1", data_type=DataType.DT_FLOAT, shape=[2, 3] |
| 41 | - h3 = builder.create_input(index=2, name="input3", data_type=DataType.DT_INT64, shape=[2, 3]) | 41 | + ) |
| 42 | + h2 = builder.create_input( | ||
| 43 | + index=1, name="input2", data_type=DataType.DT_INT64, shape=[2, 3] | ||
| 44 | + ) | ||
| 45 | + h3 = builder.create_input( | ||
| 46 | + index=2, name="input3", data_type=DataType.DT_INT64, shape=[2, 3] | ||
| 47 | + ) | ||
| 42 | builder.set_graph_output(h1 + h2 + h3, 0) | 48 | builder.set_graph_output(h1 + h2 + h3, 0) |
| 43 | return builder.build_and_reset() | 49 | return builder.build_and_reset() |
| 44 | 50 | ||
| @@ -82,7 +88,7 @@ def run_graph(graph: Graph, session_runner: Callable[[Graph, Session], int]) -> | |||
| 82 | } | 88 | } |
| 83 | ge_api = GeApi() | 89 | ge_api = GeApi() |
| 84 | ge_api.ge_initialize(config) | 90 | ge_api.ge_initialize(config) |
| 85 | - print(f"[Info] GE 环境初始化成功 (Device ID: {DEVICE_ID})") | 91 | + print(f"[Info] GE environment initialized successfully (Device ID: {DEVICE_ID})") |
| 86 | 92 | ||
| 87 | try: | 93 | try: |
| 88 | check_ret("acl.init", acl.init()) | 94 | check_ret("acl.init", acl.init()) |
| @@ -90,11 +96,11 @@ def run_graph(graph: Graph, session_runner: Callable[[Graph, Session], int]) -> | |||
| 90 | check_ret("acl.rt.set_device", acl.rt.set_device(DEVICE_ID)) | 96 | check_ret("acl.rt.set_device", acl.rt.set_device(DEVICE_ID)) |
| 91 | return session_runner(graph, session) | 97 | return session_runner(graph, session) |
| 92 | except Exception as e: | 98 | except Exception as e: |
| 93 | - print(f"[Error] 执行过程中出错: {e}") | 99 | + print(f"[Error] Error during execution: {e}") |
| 94 | traceback.print_exc() | 100 | traceback.print_exc() |
| 95 | return -1 | 101 | return -1 |
| 96 | finally: | 102 | finally: |
| 97 | acl.rt.reset_device(DEVICE_ID) | 103 | acl.rt.reset_device(DEVICE_ID) |
| 98 | acl.finalize() | 104 | acl.finalize() |
| 99 | ge_api.ge_finalize() | 105 | ge_api.ge_finalize() |
| 100 | - print("[Info] 运行环境已清理") | 106 | + print("[Info] Runtime environment cleaned up") |
| @@ -36,16 +36,18 @@ def run_with_default_allocator(ge_graph: Graph, session: Session) -> int: | |||
| 36 | # 3. 添加并异步执行 Graph | 36 | # 3. 添加并异步执行 Graph |
| 37 | session.add_graph(GRAPH_ID, ge_graph) | 37 | session.add_graph(GRAPH_ID, ge_graph) |
| 38 | graph_added = True | 38 | graph_added = True |
| 39 | - device_outputs = session.run_graph_with_stream_async(GRAPH_ID, stream, list(inputs)) | 39 | + device_outputs = session.run_graph_with_stream_async( |
| 40 | + GRAPH_ID, stream, list(inputs) | ||
| 41 | + ) | ||
| 40 | 42 | ||
| 41 | # 4. 等待 Stream 上的任务完成 | 43 | # 4. 等待 Stream 上的任务完成 |
| 42 | check_ret("acl.rt.synchronize_stream", acl.rt.synchronize_stream(stream)) | 44 | check_ret("acl.rt.synchronize_stream", acl.rt.synchronize_stream(stream)) |
| 43 | - print("[Info] 异步执行 Graph 成功!") | 45 | + print("[Info] Asynchronous execution of the graph succeeded.") |
| 44 | 46 | ||
| 45 | # 5. 将 Device 数据传回 Host 并打印 | 47 | # 5. 将 Device 数据传回 Host 并打印 |
| 46 | host_outputs = [out.to_host() for out in device_outputs] | 48 | host_outputs = [out.to_host() for out in device_outputs] |
| 47 | for idx, tensor in enumerate(host_outputs, start=1): | 49 | for idx, tensor in enumerate(host_outputs, start=1): |
| 48 | - print(f"Tensor{idx} 详情:{tensor}") | 50 | + print(f"Tensor{idx} details: {tensor}") |
| 49 | return 0 | 51 | return 0 |
| 50 | finally: | 52 | finally: |
| 51 | if stream is not None: | 53 | if stream is not None: |
| @@ -78,7 +78,7 @@ def run_with_sample_allocator(sample_graph: Graph, session: Session) -> int: | |||
| 78 | allocator = SamplePoolAllocator() | 78 | allocator = SamplePoolAllocator() |
| 79 | session.register_external_allocator(stream, allocator) | 79 | session.register_external_allocator(stream, allocator) |
| 80 | allocator_registered = True | 80 | allocator_registered = True |
| 81 | - print("[Info] SamplePoolAllocator 已注册到 stream") | 81 | + print("[Info] SamplePoolAllocator registered to stream") |
| 82 | 82 | ||
| 83 | # 3. 创建 Device 输入 | 83 | # 3. 创建 Device 输入 |
| 84 | inputs = create_input_tensors() | 84 | inputs = create_input_tensors() |
| @@ -86,21 +86,23 @@ def run_with_sample_allocator(sample_graph: Graph, session: Session) -> int: | |||
| 86 | # 4. 添加并异步执行 Graph | 86 | # 4. 添加并异步执行 Graph |
| 87 | session.add_graph(GRAPH_ID, sample_graph) | 87 | session.add_graph(GRAPH_ID, sample_graph) |
| 88 | graph_added = True | 88 | graph_added = True |
| 89 | - device_outputs = session.run_graph_with_stream_async(GRAPH_ID, stream, list(inputs)) | 89 | + device_outputs = session.run_graph_with_stream_async( |
| 90 | + GRAPH_ID, stream, list(inputs) | ||
| 91 | + ) | ||
| 90 | 92 | ||
| 91 | # 5. 等待 Stream 上的任务完成 | 93 | # 5. 等待 Stream 上的任务完成 |
| 92 | check_ret("acl.rt.synchronize_stream", acl.rt.synchronize_stream(stream)) | 94 | check_ret("acl.rt.synchronize_stream", acl.rt.synchronize_stream(stream)) |
| 93 | - print("[Info] 异步执行 Graph 成功!") | 95 | + print("[Info] Asynchronous execution of the graph succeeded.") |
| 94 | 96 | ||
| 95 | # 6. 将 Device 数据传回 Host 并打印 | 97 | # 6. 将 Device 数据传回 Host 并打印 |
| 96 | host_outputs = [out.to_host() for out in device_outputs] | 98 | host_outputs = [out.to_host() for out in device_outputs] |
| 97 | for idx, tensor in enumerate(host_outputs, start=1): | 99 | for idx, tensor in enumerate(host_outputs, start=1): |
| 98 | - print(f"Tensor{idx} 详情:{tensor}") | 100 | + print(f"Tensor{idx} details: {tensor}") |
| 99 | return 0 | 101 | return 0 |
| 100 | finally: | 102 | finally: |
| 101 | if allocator_registered: | 103 | if allocator_registered: |
| 102 | session.unregister_external_allocator(stream) | 104 | session.unregister_external_allocator(stream) |
| 103 | - print("[Info] SamplePoolAllocator 已注销") | 105 | + print("[Info] SamplePoolAllocator unregistered") |
| 104 | if stream is not None: | 106 | if stream is not None: |
| 105 | check_ret("acl.rt.destroy_stream", acl.rt.destroy_stream(stream)) | 107 | check_ret("acl.rt.destroy_stream", acl.rt.destroy_stream(stream)) |
| 106 | if graph_added and session is not None: | 108 | if graph_added and session is not None: |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeBatchNormGraphByEsAndDump(); | 40 | es_showcase::MakeBatchNormGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -30,8 +30,12 @@ def build_batch_norm_graph(): | |||
| 30 | format=Format.FORMAT_NCHW, | 30 | format=Format.FORMAT_NCHW, |
| 31 | shape=[1, 3, 1, 2], | 31 | shape=[1, 3, 1, 2], |
| 32 | ) | 32 | ) |
| 33 | - mean = builder.create_input(index=1, name="mean", data_type=DataType.DT_FLOAT, shape=[3]) | 33 | + mean = builder.create_input( |
| 34 | - variance = builder.create_input(index=2, name="variance", data_type=DataType.DT_FLOAT, shape=[3]) | 34 | + index=1, name="mean", data_type=DataType.DT_FLOAT, shape=[3] |
| 35 | + ) | ||
| 36 | + variance = builder.create_input( | ||
| 37 | + index=2, name="variance", data_type=DataType.DT_FLOAT, shape=[3] | ||
| 38 | + ) | ||
| 35 | scale = builder.create_const_float([1.0, 1.0, 1.0], shape=[3]) | 39 | scale = builder.create_const_float([1.0, 1.0, 1.0], shape=[3]) |
| 36 | offset = builder.create_const_float([0.0, 0.0, 0.0], shape=[3]) | 40 | offset = builder.create_const_float([0.0, 0.0, 0.0], shape=[3]) |
| 37 | batchNorm_tensor_holder = BatchNorm( | 41 | batchNorm_tensor_holder = BatchNorm( |
| @@ -61,10 +65,10 @@ def run_graph(graph, device_id="0") -> int: | |||
| 61 | ge_api = GeApi() | 65 | ge_api = GeApi() |
| 62 | ret = ge_api.ge_initialize(config) | 66 | ret = ge_api.ge_initialize(config) |
| 63 | if ret != 0: | 67 | if ret != 0: |
| 64 | - print(f"[Error] GE初始化失败,返回码: {ret}") | 68 | + print(f"[Error] GE initialization failed, return code: {ret}") |
| 65 | return ret | 69 | return ret |
| 66 | 70 | ||
| 67 | - print(f"[Info] GE环境初始化成功 (Device ID: {device_id})") | 71 | + print(f"[Info] GE environment initialized successfully (Device ID: {device_id})") |
| 68 | 72 | ||
| 69 | try: | 73 | try: |
| 70 | # 2. 创建Session | 74 | # 2. 创建Session |
| @@ -74,9 +78,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 74 | graph_id = 1 | 78 | graph_id = 1 |
| 75 | ret = session.add_graph(graph_id, graph) | 79 | ret = session.add_graph(graph_id, graph) |
| 76 | if ret != 0: | 80 | if ret != 0: |
| 77 | - print(f"[Error] 添加图失败,返回码: {ret}") | 81 | + print(f"[Error] Failed to add graph, return code: {ret}") |
| 78 | return ret | 82 | return ret |
| 79 | - print(f"[Info] 图已添加到Session (Graph ID: {graph_id})") | 83 | + print(f"[Info] Graph added to Session (Graph ID: {graph_id})") |
| 80 | 84 | ||
| 81 | # 4. 准备输入数据 | 85 | # 4. 准备输入数据 |
| 82 | input_data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=np.float32) | 86 | input_data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=np.float32) |
| @@ -90,21 +94,25 @@ def run_graph(graph, device_id="0") -> int: | |||
| 90 | Format.FORMAT_NCHW, | 94 | Format.FORMAT_NCHW, |
| 91 | [1, 3, 1, 2], | 95 | [1, 3, 1, 2], |
| 92 | ) | 96 | ) |
| 93 | - mean_tensor = Tensor(mean_data.tolist(), None, DataType.DT_FLOAT, Format.FORMAT_ND, [3]) | 97 | + mean_tensor = Tensor( |
| 94 | - variance_tensor = Tensor(variance_data.tolist(), None, DataType.DT_FLOAT, Format.FORMAT_ND, [3]) | 98 | + mean_data.tolist(), None, DataType.DT_FLOAT, Format.FORMAT_ND, [3] |
| 99 | + ) | ||
| 100 | + variance_tensor = Tensor( | ||
| 101 | + variance_data.tolist(), None, DataType.DT_FLOAT, Format.FORMAT_ND, [3] | ||
| 102 | + ) | ||
| 95 | 103 | ||
| 96 | inputs = [input_tensor, mean_tensor, variance_tensor] | 104 | inputs = [input_tensor, mean_tensor, variance_tensor] |
| 97 | - print(f"[Info] 输入数据已准备,共{len(inputs)}个输入tensor (input, mean, variance)") | 105 | + print(f"[Info] Prepared {len(inputs)} input tensor(s) (input, mean, variance)") |
| 98 | 106 | ||
| 99 | # 5. 运行图 | 107 | # 5. 运行图 |
| 100 | ret = session.run_graph(graph_id, inputs) | 108 | ret = session.run_graph(graph_id, inputs) |
| 101 | - print("[Info] 图运行成功!") | 109 | + print("[Info] Graph executed successfully!") |
| 102 | for idx, tensor in enumerate(ret, start=1): | 110 | for idx, tensor in enumerate(ret, start=1): |
| 103 | - print(f"Tensor{idx}详情:{tensor}") | 111 | + print(f"Tensor{idx} details: {tensor}") |
| 104 | return 0 | 112 | return 0 |
| 105 | 113 | ||
| 106 | except Exception as e: | 114 | except Exception as e: |
| 107 | - print(f"[Error] 执行过程中出错: {e}") | 115 | + print(f"[Error] Error during execution: {e}") |
| 108 | import traceback | 116 | import traceback |
| 109 | 117 | ||
| 110 | traceback.print_exc() | 118 | traceback.print_exc() |
| @@ -112,9 +120,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 112 | 120 | ||
| 113 | finally: | 121 | finally: |
| 114 | # 6. 清理GE环境 | 122 | # 6. 清理GE环境 |
| 115 | - print("[Info] 清理GE环境...") | 123 | + print("[Info] Cleaning up GE environment...") |
| 116 | ge_api.ge_finalize() | 124 | ge_api.ge_finalize() |
| 117 | - print("[Success] GE环境已清理") | 125 | + print("[Success] GE environment cleaned up") |
| 118 | 126 | ||
| 119 | 127 | ||
| 120 | if __name__ == "__main__": | 128 | if __name__ == "__main__": |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeSigmoidAddGraphByEsAndDump(); | 40 | es_showcase::MakeSigmoidAddGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -23,7 +23,9 @@ def build_sigmoid_add_graph(): | |||
| 23 | # 1、创建图构建器 | 23 | # 1、创建图构建器 |
| 24 | builder = GraphBuilder("MakeSigmoidAddGraph") | 24 | builder = GraphBuilder("MakeSigmoidAddGraph") |
| 25 | # 2、创建图输入节点 | 25 | # 2、创建图输入节点 |
| 26 | - input_tensor_holder1 = builder.create_input(index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 3]) | 26 | + input_tensor_holder1 = builder.create_input( |
| 27 | + index=0, name="input", data_type=DataType.DT_FLOAT, shape=[2, 3] | ||
| 28 | + ) | ||
| 27 | with attr_scope({"_stream_label": "label_0"}): | 29 | with attr_scope({"_stream_label": "label_0"}): |
| 28 | sigmoid0 = Sigmoid(input_tensor_holder1) | 30 | sigmoid0 = Sigmoid(input_tensor_holder1) |
| 29 | # 添加私有属性 | 31 | # 添加私有属性 |
| @@ -55,10 +57,10 @@ def run_graph(graph, device_id="0") -> int: | |||
| 55 | ge_api = GeApi() | 57 | ge_api = GeApi() |
| 56 | ret = ge_api.ge_initialize(config) | 58 | ret = ge_api.ge_initialize(config) |
| 57 | if ret != 0: | 59 | if ret != 0: |
| 58 | - print(f"[Error] GE初始化失败,返回码: {ret}") | 60 | + print(f"[Error] GE initialization failed, return code: {ret}") |
| 59 | return ret | 61 | return ret |
| 60 | 62 | ||
| 61 | - print(f"[Info] GE环境初始化成功 (Device ID: {device_id})") | 63 | + print(f"[Info] GE environment initialized successfully (Device ID: {device_id})") |
| 62 | 64 | ||
| 63 | try: | 65 | try: |
| 64 | # 2. 创建Session | 66 | # 2. 创建Session |
| @@ -68,9 +70,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 68 | graph_id = 1 | 70 | graph_id = 1 |
| 69 | ret = session.add_graph(graph_id, graph) | 71 | ret = session.add_graph(graph_id, graph) |
| 70 | if ret != 0: | 72 | if ret != 0: |
| 71 | - print(f"[Error] 添加图失败,返回码: {ret}") | 73 | + print(f"[Error] Failed to add graph, return code: {ret}") |
| 72 | return ret | 74 | return ret |
| 73 | - print(f"[Info] 图已添加到Session (Graph ID: {graph_id})") | 75 | + print(f"[Info] Graph added to Session (Graph ID: {graph_id})") |
| 74 | 76 | ||
| 75 | # 4. 准备输入数据 | 77 | # 4. 准备输入数据 |
| 76 | input_data = np.full([2, 3], 1.0, dtype=np.float32) | 78 | input_data = np.full([2, 3], 1.0, dtype=np.float32) |
| @@ -83,17 +85,17 @@ def run_graph(graph, device_id="0") -> int: | |||
| 83 | ) | 85 | ) |
| 84 | 86 | ||
| 85 | inputs = [input_tensor] | 87 | inputs = [input_tensor] |
| 86 | - print(f"[Info] 输入数据已准备,共{len(inputs)}个输入tensor") | 88 | + print(f"[Info] Prepared {len(inputs)} input tensor(s)") |
| 87 | 89 | ||
| 88 | # 5. 运行图 | 90 | # 5. 运行图 |
| 89 | ret = session.run_graph(graph_id, inputs) | 91 | ret = session.run_graph(graph_id, inputs) |
| 90 | - print("[Info] 图运行成功!") | 92 | + print("[Info] Graph executed successfully!") |
| 91 | for idx, tensor in enumerate(ret, start=1): | 93 | for idx, tensor in enumerate(ret, start=1): |
| 92 | - print(f"Tensor{idx}详情:{tensor}") | 94 | + print(f"Tensor{idx} details: {tensor}") |
| 93 | return 0 | 95 | return 0 |
| 94 | 96 | ||
| 95 | except Exception as e: | 97 | except Exception as e: |
| 96 | - print(f"[Error] 执行过程中出错: {e}") | 98 | + print(f"[Error] Error during execution: {e}") |
| 97 | import traceback | 99 | import traceback |
| 98 | 100 | ||
| 99 | traceback.print_exc() | 101 | traceback.print_exc() |
| @@ -101,9 +103,9 @@ def run_graph(graph, device_id="0") -> int: | |||
| 101 | 103 | ||
| 102 | finally: | 104 | finally: |
| 103 | # 6. 清理GE环境 | 105 | # 6. 清理GE环境 |
| 104 | - print("[Info] 清理GE环境...") | 106 | + print("[Info] Cleaning up GE environment...") |
| 105 | ge_api.ge_finalize() | 107 | ge_api.ge_finalize() |
| 106 | - print("[Success] GE环境已清理") | 108 | + print("[Success] GE environment cleaned up") |
| 107 | 109 | ||
| 108 | 110 | ||
| 109 | if __name__ == "__main__": | 111 | if __name__ == "__main__": |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeTransformerGraphByEsAndDump(); | 40 | es_showcase::MakeTransformerGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -9,6 +9,7 @@ | |||
| 9 | # See LICENSE in the root of the software repository for the full text of the License. | 9 | # See LICENSE in the root of the software repository for the full text of the License. |
| 10 | # ---------------------------------------------------------------------------- | 10 | # ---------------------------------------------------------------------------- |
| 11 | 11 | ||
| 12 | +# ruff: noqa: F403, F405 | ||
| 12 | import numpy as np | 13 | import numpy as np |
| 13 | from ge.es import GraphBuilder | 14 | from ge.es import GraphBuilder |
| 14 | from ge.es.all import * | 15 | from ge.es.all import * |
| @@ -34,7 +35,9 @@ def build_transformer_graph(): | |||
| 34 | transpose_x2=False, | 35 | transpose_x2=False, |
| 35 | ) | 36 | ) |
| 36 | sigmoid_result1 = Sigmoid(matmul_result1) | 37 | sigmoid_result1 = Sigmoid(matmul_result1) |
| 37 | - add_result1 = Reshape(sigmoid_result1, [-1, 256]) + Cast(Unsqueeze(input3, axes=[0]), dst_type=DataType.DT_FLOAT) | 38 | + add_result1 = Reshape(sigmoid_result1, [-1, 256]) + Cast( |
| 39 | + Unsqueeze(input3, axes=[0]), dst_type=DataType.DT_FLOAT | ||
| 40 | + ) | ||
| 38 | 41 | ||
| 39 | topkv2_result1 = TopKV2(add_result1, 2) | 42 | topkv2_result1 = TopKV2(add_result1, 2) |
| 40 | reducesum_result1 = ReduceSum(topkv2_result1.values, [-1]) | 43 | reducesum_result1 = ReduceSum(topkv2_result1.values, [-1]) |
| @@ -49,7 +52,9 @@ def build_transformer_graph(): | |||
| 49 | Cast(builder.create_scalar_float(1.0), dst_type=DataType.DT_FLOAT), | 52 | Cast(builder.create_scalar_float(1.0), dst_type=DataType.DT_FLOAT), |
| 50 | ), | 53 | ), |
| 51 | ) | 54 | ) |
| 52 | - identity_result1 = Identity(BroadcastTo(Unsqueeze(scatterelements_result1, axes=[-1]), [256, 256])) | 55 | + identity_result1 = Identity( |
| 56 | + BroadcastTo(Unsqueeze(scatterelements_result1, axes=[-1]), [256, 256]) | ||
| 57 | + ) | ||
| 53 | maskedfill_result1 = MaskedFill( | 58 | maskedfill_result1 = MaskedFill( |
| 54 | add_result1, | 59 | add_result1, |
| 55 | LogicalNot( | 60 | LogicalNot( |
| @@ -60,7 +65,9 @@ def build_transformer_graph(): | |||
| 60 | ), | 65 | ), |
| 61 | builder.create_scalar_float(0.0), | 66 | builder.create_scalar_float(0.0), |
| 62 | ) | 67 | ) |
| 63 | - cast_result3 = Cast(TopKV2(maskedfill_result1, 4, sorted=False).indices, dst_type=DataType.DT_INT64) | 68 | + cast_result3 = Cast( |
| 69 | + TopKV2(maskedfill_result1, 4, sorted=False).indices, dst_type=DataType.DT_INT64 | ||
| 70 | + ) | ||
| 64 | # 3、设置图输出节点 | 71 | # 3、设置图输出节点 |
| 65 | gatherelements_result1 = GatherElements(sigmoid_result1, cast_result3, dim=1) | 72 | gatherelements_result1 = GatherElements(sigmoid_result1, cast_result3, dim=1) |
| 66 | realdiv_result1 = RealDiv(gatherelements_result1, 1e-20) | 73 | realdiv_result1 = RealDiv(gatherelements_result1, 1e-20) |
| @@ -98,9 +105,9 @@ def run_graph(graph) -> int: | |||
| 98 | ge_api = GeApi() | 105 | ge_api = GeApi() |
| 99 | ret = ge_api.ge_initialize(config) | 106 | ret = ge_api.ge_initialize(config) |
| 100 | if ret != 0: | 107 | if ret != 0: |
| 101 | - print(f"GE初始化失败,返回码: {ret}") | 108 | + print(f"GE initialization failed, return code: {ret}") |
| 102 | return ret | 109 | return ret |
| 103 | - print("GE环境初始化成功 (Device ID: 0)") | 110 | + print("GE environment initialized successfully (Device ID: 0)") |
| 104 | 111 | ||
| 105 | try: | 112 | try: |
| 106 | # 2. 创建Session | 113 | # 2. 创建Session |
| @@ -110,9 +117,9 @@ def run_graph(graph) -> int: | |||
| 110 | graph_id = 1 | 117 | graph_id = 1 |
| 111 | ret = session.add_graph(graph_id, graph) | 118 | ret = session.add_graph(graph_id, graph) |
| 112 | if ret != 0: | 119 | if ret != 0: |
| 113 | - print(f"添加图失败,返回码: {ret}") | 120 | + print(f"Failed to add graph, return code: {ret}") |
| 114 | return ret | 121 | return ret |
| 115 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 122 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 116 | 123 | ||
| 117 | # 4. 准备输入数据 | 124 | # 4. 准备输入数据 |
| 118 | input1_data = np.random.randn(32, 8, 7168).astype(np.float32) | 125 | input1_data = np.random.randn(32, 8, 7168).astype(np.float32) |
| @@ -145,13 +152,13 @@ def run_graph(graph) -> int: | |||
| 145 | inputs = [tensor1, tensor2, tensor3] | 152 | inputs = [tensor1, tensor2, tensor3] |
| 146 | # 5. 运行图 | 153 | # 5. 运行图 |
| 147 | ret = session.run_graph(graph_id, inputs) | 154 | ret = session.run_graph(graph_id, inputs) |
| 148 | - print("[Info] 图运行成功!") | 155 | + print("[Info] Graph executed successfully!") |
| 149 | for idx, tensor in enumerate(ret, start=1): | 156 | for idx, tensor in enumerate(ret, start=1): |
| 150 | - print(f"Tensor{idx}详情:{tensor}") | 157 | + print(f"Tensor{idx} details: {tensor}") |
| 151 | return 0 | 158 | return 0 |
| 152 | 159 | ||
| 153 | except Exception as e: | 160 | except Exception as e: |
| 154 | - print(f"[Error] 执行过程中出错: {e}") | 161 | + print(f"[Error] Error during execution: {e}") |
| 155 | import traceback | 162 | import traceback |
| 156 | 163 | ||
| 157 | traceback.print_exc() | 164 | traceback.print_exc() |
| @@ -159,9 +166,9 @@ def run_graph(graph) -> int: | |||
| 159 | 166 | ||
| 160 | finally: | 167 | finally: |
| 161 | # 6. 清理GE环境 | 168 | # 6. 清理GE环境 |
| 162 | - print("[Info] 清理GE环境...") | 169 | + print("[Info] Cleaning up GE environment...") |
| 163 | ge_api.ge_finalize() | 170 | ge_api.ge_finalize() |
| 164 | - print("[Success] GE环境已清理") | 171 | + print("[Success] GE environment cleaned up") |
| 165 | 172 | ||
| 166 | 173 | ||
| 167 | graph = build_transformer_graph() | 174 | graph = build_transformer_graph() |
| @@ -26,7 +26,7 @@ int main(int argc, char **argv) { | |||
| 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; | 26 | std::map<ge::AscendString, ge::AscendString> config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "0"}}; |
| 27 | auto ret = ge::GEInitialize(config); | 27 | auto ret = ge::GEInitialize(config); |
| 28 | if (ret != ge::SUCCESS) { | 28 | if (ret != ge::SUCCESS) { |
| 29 | - std::cerr << "GE 初始化失败\n"; | 29 | + std::cerr << "GE initialization failed\n"; |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | int result = -1; | 32 | int result = -1; |
| @@ -34,13 +34,13 @@ int main(int argc, char **argv) { | |||
| 34 | result = 0; | 34 | result = 0; |
| 35 | } | 35 | } |
| 36 | ge::GEFinalize(); | 36 | ge::GEFinalize(); |
| 37 | - std::cout << "执行结束" << std::endl; | 37 | + std::cout << "Execution completed" << std::endl; |
| 38 | return result; | 38 | return result; |
| 39 | } else if (command == "dump") { | 39 | } else if (command == "dump") { |
| 40 | es_showcase::MakeTransformerNzGraphByEsAndDump(); | 40 | es_showcase::MakeTransformerNzGraphByEsAndDump(); |
| 41 | return 0; | 41 | return 0; |
| 42 | } else { | 42 | } else { |
| 43 | - std::cout << "错误: 未知命令 '" << command << "'" << std::endl; | 43 | + std::cout << "Error: unknown command '" << command << "'" << std::endl; |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| @@ -9,6 +9,7 @@ | |||
| 9 | # See LICENSE in the root of the software repository for the full text of the License. | 9 | # See LICENSE in the root of the software repository for the full text of the License. |
| 10 | # ---------------------------------------------------------------------------- | 10 | # ---------------------------------------------------------------------------- |
| 11 | 11 | ||
| 12 | +# ruff: noqa: F403, F405 | ||
| 12 | import numpy as np | 13 | import numpy as np |
| 13 | from ge.es import GraphBuilder | 14 | from ge.es import GraphBuilder |
| 14 | from ge.es.all import * | 15 | from ge.es.all import * |
| @@ -58,7 +59,9 @@ def build_transformer_nz_graph(): | |||
| 58 | transpose_x2=False, | 59 | transpose_x2=False, |
| 59 | ) | 60 | ) |
| 60 | sigmoid_result1 = Sigmoid(matmul_result1) | 61 | sigmoid_result1 = Sigmoid(matmul_result1) |
| 61 | - add_result1 = Reshape(sigmoid_result1, [-1, 256]) + Cast(input3, dst_type=DataType.DT_FLOAT) | 62 | + add_result1 = Reshape(sigmoid_result1, [-1, 256]) + Cast( |
| 63 | + input3, dst_type=DataType.DT_FLOAT | ||
| 64 | + ) | ||
| 62 | 65 | ||
| 63 | topkv2_result1 = TopKV2(add_result1, 2) | 66 | topkv2_result1 = TopKV2(add_result1, 2) |
| 64 | reducesum_result1 = ReduceSum(topkv2_result1.values, [-1]) | 67 | reducesum_result1 = ReduceSum(topkv2_result1.values, [-1]) |
| @@ -73,7 +76,9 @@ def build_transformer_nz_graph(): | |||
| 73 | Cast(builder.create_scalar_float(1.0), dst_type=DataType.DT_FLOAT), | 76 | Cast(builder.create_scalar_float(1.0), dst_type=DataType.DT_FLOAT), |
| 74 | ), | 77 | ), |
| 75 | ) | 78 | ) |
| 76 | - identity_result1 = Identity(BroadcastTo(Unsqueeze(scatterelements_result1, axes=[-1]), [256, 256])) | 79 | + identity_result1 = Identity( |
| 80 | + BroadcastTo(Unsqueeze(scatterelements_result1, axes=[-1]), [256, 256]) | ||
| 81 | + ) | ||
| 77 | maskedfill_result1 = MaskedFill( | 82 | maskedfill_result1 = MaskedFill( |
| 78 | add_result1, | 83 | add_result1, |
| 79 | LogicalNot( | 84 | LogicalNot( |
| @@ -84,11 +89,15 @@ def build_transformer_nz_graph(): | |||
| 84 | ), | 89 | ), |
| 85 | builder.create_scalar_float(0.0), | 90 | builder.create_scalar_float(0.0), |
| 86 | ) | 91 | ) |
| 87 | - cast_result3 = Cast(TopKV2(maskedfill_result1, 4, sorted=False).indices, dst_type=DataType.DT_INT64) | 92 | + cast_result3 = Cast( |
| 93 | + TopKV2(maskedfill_result1, 4, sorted=False).indices, dst_type=DataType.DT_INT64 | ||
| 94 | + ) | ||
| 88 | # 3、设置图输出节点 | 95 | # 3、设置图输出节点 |
| 89 | gatherelements_result1 = GatherElements(sigmoid_result1, cast_result3, dim=1) | 96 | gatherelements_result1 = GatherElements(sigmoid_result1, cast_result3, dim=1) |
| 90 | realdiv_result1 = RealDiv(gatherelements_result1, 1e-20) | 97 | realdiv_result1 = RealDiv(gatherelements_result1, 1e-20) |
| 91 | - output = Cast(realdiv_result1 * builder.create_scalar_float(2.5), dst_type=DataType.DT_FLOAT) | 98 | + output = Cast( |
| 99 | + realdiv_result1 * builder.create_scalar_float(2.5), dst_type=DataType.DT_FLOAT | ||
| 100 | + ) | ||
| 92 | # 4、构建图 | 101 | # 4、构建图 |
| 93 | return builder.build_and_reset([cast_result3, output]) | 102 | return builder.build_and_reset([cast_result3, output]) |
| 94 | 103 | ||
| @@ -115,9 +124,9 @@ def run_graph(graph) -> int: | |||
| 115 | ge_api = GeApi() | 124 | ge_api = GeApi() |
| 116 | ret = ge_api.ge_initialize(config) | 125 | ret = ge_api.ge_initialize(config) |
| 117 | if ret != 0: | 126 | if ret != 0: |
| 118 | - print(f"GE初始化失败,返回码: {ret}") | 127 | + print(f"GE initialization failed, return code: {ret}") |
| 119 | return ret | 128 | return ret |
| 120 | - print("GE环境初始化成功 (Device ID: 0)") | 129 | + print("GE environment initialized successfully (Device ID: 0)") |
| 121 | 130 | ||
| 122 | try: | 131 | try: |
| 123 | # 2. 创建Session | 132 | # 2. 创建Session |
| @@ -127,9 +136,9 @@ def run_graph(graph) -> int: | |||
| 127 | graph_id = 1 | 136 | graph_id = 1 |
| 128 | ret = session.add_graph(graph_id, graph) | 137 | ret = session.add_graph(graph_id, graph) |
| 129 | if ret != 0: | 138 | if ret != 0: |
| 130 | - print(f"添加图失败,返回码: {ret}") | 139 | + print(f"Failed to add graph, return code: {ret}") |
| 131 | return ret | 140 | return ret |
| 132 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 141 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 133 | 142 | ||
| 134 | # 4. 准备输入数据 | 143 | # 4. 准备输入数据 |
| 135 | input1_data = np.random.randn(256, 7168).astype(np.float32) | 144 | input1_data = np.random.randn(256, 7168).astype(np.float32) |
| @@ -144,13 +153,13 @@ def run_graph(graph) -> int: | |||
| 144 | inputs = [tensor1, tensor2, tensor3] | 153 | inputs = [tensor1, tensor2, tensor3] |
| 145 | # 5. 运行图 | 154 | # 5. 运行图 |
| 146 | ret = session.run_graph(graph_id, inputs) | 155 | ret = session.run_graph(graph_id, inputs) |
| 147 | - print("[Info] 图运行成功!") | 156 | + print("[Info] Graph executed successfully!") |
| 148 | for idx, tensor in enumerate(ret, start=1): | 157 | for idx, tensor in enumerate(ret, start=1): |
| 149 | - print(f"Tensor{idx}详情:{tensor}") | 158 | + print(f"Tensor{idx} details: {tensor}") |
| 150 | return 0 | 159 | return 0 |
| 151 | 160 | ||
| 152 | except Exception as e: | 161 | except Exception as e: |
| 153 | - print(f"[Error] 执行过程中出错: {e}") | 162 | + print(f"[Error] Error during execution: {e}") |
| 154 | import traceback | 163 | import traceback |
| 155 | 164 | ||
| 156 | traceback.print_exc() | 165 | traceback.print_exc() |
| @@ -158,9 +167,9 @@ def run_graph(graph) -> int: | |||
| 158 | 167 | ||
| 159 | finally: | 168 | finally: |
| 160 | # 6. 清理GE环境 | 169 | # 6. 清理GE环境 |
| 161 | - print("[Info] 清理GE环境...") | 170 | + print("[Info] Cleaning up GE environment...") |
| 162 | ge_api.ge_finalize() | 171 | ge_api.ge_finalize() |
| 163 | - print("[Success] GE环境已清理") | 172 | + print("[Success] GE environment cleaned up") |
| 164 | 173 | ||
| 165 | 174 | ||
| 166 | graph = build_transformer_nz_graph() | 175 | graph = build_transformer_nz_graph() |
| @@ -41,13 +41,15 @@ except ImportError: | |||
| 41 | def _require_es_apis() -> None: | 41 | def _require_es_apis() -> None: |
| 42 | if ConcatV2 is None: | 42 | if ConcatV2 is None: |
| 43 | raise RuntimeError( | 43 | raise RuntimeError( |
| 44 | - "未找到 ge.es.math.ConcatV2。请先 source CANN 环境;如仍缺失,请参考 README 的" | 44 | + "ge.es.math.ConcatV2 not found. Please source the CANN environment first; " |
| 45 | - "“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 45 | + "if still missing, refer to the README section 'Handling Missing ES APIs (Optional)' " |
| 46 | + "to generate and load es_all, then run again." | ||
| 46 | ) | 47 | ) |
| 47 | if Relu is None: | 48 | if Relu is None: |
| 48 | raise RuntimeError( | 49 | raise RuntimeError( |
| 49 | - "未找到 ge.es.nn.Relu。请先 source CANN 环境;如仍缺失,请参考 README 的" | 50 | + "ge.es.nn.Relu not found. Please source the CANN environment first; " |
| 50 | - "“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 51 | + "if still missing, refer to the README section 'Handling Missing ES APIs (Optional)' " |
| 52 | + "to generate and load es_all, then run again." | ||
| 51 | ) | 53 | ) |
| 52 | 54 | ||
| 53 | 55 | ||
| @@ -128,8 +130,8 @@ class PythonMoveReluBeforeConcatPass(FusionBasePass): | |||
| 128 | 130 | ||
| 129 | 131 | ||
| 130 | if __name__ == "__main__": | 132 | if __name__ == "__main__": |
| 131 | - print("PythonMoveReluBeforeConcatPass 已注册。") | 133 | + print("PythonMoveReluBeforeConcatPass registered.") |
| 132 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 134 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 133 | print( | 135 | print( |
| 134 | " export ASCEND_GE_PY_PASS_PATH=$PWD/python/src/python_move_relu_before_concat_pass.py" | 136 | " export ASCEND_GE_PY_PASS_PATH=$PWD/python/src/python_move_relu_before_concat_pass.py" |
| 135 | ) | 137 | ) |
| @@ -199,8 +199,8 @@ class PythonConvTransFormatPass(FusionBasePass): | |||
| 199 | 199 | ||
| 200 | 200 | ||
| 201 | if __name__ == "__main__": | 201 | if __name__ == "__main__": |
| 202 | - print("PythonConvTransFormatPass 已注册。") | 202 | + print("PythonConvTransFormatPass registered.") |
| 203 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 203 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 204 | print( | 204 | print( |
| 205 | " export ASCEND_GE_PY_PASS_PATH=$PWD/python/src/python_modify_conv_data_format_pass.py" | 205 | " export ASCEND_GE_PY_PASS_PATH=$PWD/python/src/python_modify_conv_data_format_pass.py" |
| 206 | ) | 206 | ) |
| @@ -50,13 +50,16 @@ def _require_es_apis() -> None: | |||
| 50 | missing = [name for name, obj in pairs if obj is None] | 50 | missing = [name for name, obj in pairs if obj is None] |
| 51 | if missing: | 51 | if missing: |
| 52 | raise RuntimeError( | 52 | raise RuntimeError( |
| 53 | - "未找到 ES API: " | 53 | + "ES API not found: " |
| 54 | + ", ".join(missing) | 54 | + ", ".join(missing) |
| 55 | - + "。请先 source CANN 环境;如仍缺失,请参考 README 的“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 55 | + + ". Please source the CANN environment first; if still missing, refer to the README " |
| 56 | + + "section 'Handling Missing ES APIs (Optional)' to generate and load es_all, then run again." | ||
| 56 | ) | 57 | ) |
| 57 | 58 | ||
| 58 | 59 | ||
| 59 | -@register_fusion_pass(name="PythonFuseMatMulAndAddPass", stage=PassStage.BEFORE_INFER_SHAPE) | 60 | +@register_fusion_pass( |
| 61 | + name="PythonFuseMatMulAndAddPass", stage=PassStage.BEFORE_INFER_SHAPE | ||
| 62 | +) | ||
| 60 | class PythonFuseMatMulAndAddPass(PatternFusionPass): | 63 | class PythonFuseMatMulAndAddPass(PatternFusionPass): |
| 61 | @pattern | 64 | @pattern |
| 62 | def matmul_add(self, inputs): | 65 | def matmul_add(self, inputs): |
| @@ -83,6 +86,6 @@ class PythonFuseMatMulAndAddPass(PatternFusionPass): | |||
| 83 | 86 | ||
| 84 | 87 | ||
| 85 | if __name__ == "__main__": | 88 | if __name__ == "__main__": |
| 86 | - print("PythonFuseMatMulAndAddPass 已注册。") | 89 | + print("PythonFuseMatMulAndAddPass registered.") |
| 87 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 90 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 88 | print(" export ASCEND_GE_PY_PASS_PATH=$(pwd)/src/python_fuse_matmul_add_pass.py") | 91 | print(" export ASCEND_GE_PY_PASS_PATH=$(pwd)/src/python_fuse_matmul_add_pass.py") |
| @@ -53,9 +53,10 @@ def _require_es_apis() -> None: | |||
| 53 | missing = [name for name, obj in pairs if obj is None] | 53 | missing = [name for name, obj in pairs if obj is None] |
| 54 | if missing: | 54 | if missing: |
| 55 | raise RuntimeError( | 55 | raise RuntimeError( |
| 56 | - "未找到 ES API: " | 56 | + "ES API not found: " |
| 57 | + ", ".join(missing) | 57 | + ", ".join(missing) |
| 58 | - + "。请先 source CANN 环境;如仍缺失,请参考 README 的“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 58 | + + ". Please source the CANN environment first; if still missing, refer to the README " |
| 59 | + + "section 'Handling Missing ES APIs (Optional)' to generate and load es_all, then run again." | ||
| 59 | ) | 60 | ) |
| 60 | 61 | ||
| 61 | 62 | ||
| @@ -82,7 +83,9 @@ class PythonFuseMatMulAndAddCaptureTensorPass(PatternFusionPass): | |||
| 82 | return [pat0, pat1] | 83 | return [pat0, pat1] |
| 83 | 84 | ||
| 84 | def meet_requirements(self, match_result): | 85 | def meet_requirements(self, match_result): |
| 85 | - print("Define MeetRequirements for FuseMatMulAndAddPass in capture tensor sample") | 86 | + print( |
| 87 | + "Define MeetRequirements for FuseMatMulAndAddPass in capture tensor sample" | ||
| 88 | + ) | ||
| 86 | add_io = match_result.get_captured_tensor(_K_ADD_CAPTURE_IDX) | 89 | add_io = match_result.get_captured_tensor(_K_ADD_CAPTURE_IDX) |
| 87 | add_node = add_io.node | 90 | add_node = add_io.node |
| 88 | add_input0_dt = add_node.get_input_desc(0).get_data_type() | 91 | add_input0_dt = add_node.get_input_desc(0).get_data_type() |
| @@ -127,6 +130,6 @@ class PythonFuseMatMulAndAddCaptureTensorPass(PatternFusionPass): | |||
| 127 | 130 | ||
| 128 | 131 | ||
| 129 | if __name__ == "__main__": | 132 | if __name__ == "__main__": |
| 130 | - print("PythonFuseMatMulAndAddCaptureTensorPass 已注册。") | 133 | + print("PythonFuseMatMulAndAddCaptureTensorPass registered.") |
| 131 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 134 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 132 | print(" export ASCEND_GE_PY_PASS_PATH=$(pwd)/src/python_fuse_matmul_add_pass.py") | 135 | print(" export ASCEND_GE_PY_PASS_PATH=$(pwd)/src/python_fuse_matmul_add_pass.py") |
| @@ -10,6 +10,7 @@ | |||
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | 12 | ||
| 13 | +# ruff: noqa: F403, F405 | ||
| 13 | import numpy as np | 14 | import numpy as np |
| 14 | from ge.es import GraphBuilder | 15 | from ge.es import GraphBuilder |
| 15 | from ge.es.all import * | 16 | from ge.es.all import * |
| @@ -32,7 +33,9 @@ def build_matmul_add_graph(): | |||
| 32 | Format.FORMAT_ND, | 33 | Format.FORMAT_ND, |
| 33 | [2, 2], | 34 | [2, 2], |
| 34 | ) | 35 | ) |
| 35 | - matmul_tensor_holder = MatMul(input1, input2, None, transpose_x1=False, transpose_x2=False) | 36 | + matmul_tensor_holder = MatMul( |
| 37 | + input1, input2, None, transpose_x1=False, transpose_x2=False | ||
| 38 | + ) | ||
| 36 | add_tensor_holder = Add(matmul_tensor_holder, Const(builder, value=input3)) | 39 | add_tensor_holder = Add(matmul_tensor_holder, Const(builder, value=input3)) |
| 37 | # 3、设置图输出节点 | 40 | # 3、设置图输出节点 |
| 38 | builder.set_graph_output(add_tensor_holder, 0) | 41 | builder.set_graph_output(add_tensor_holder, 0) |
| @@ -61,9 +64,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 61 | ge_api = GeApi() | 64 | ge_api = GeApi() |
| 62 | ret = ge_api.ge_initialize(config) | 65 | ret = ge_api.ge_initialize(config) |
| 63 | if ret != 0: | 66 | if ret != 0: |
| 64 | - print(f"GE初始化失败,返回码: {ret}") | 67 | + print(f"GE initialization failed, return code: {ret}") |
| 65 | return ret | 68 | return ret |
| 66 | - print("GE环境初始化成功 (Device ID: 0)") | 69 | + print("GE environment initialized successfully (Device ID: 0)") |
| 67 | 70 | ||
| 68 | try: | 71 | try: |
| 69 | # 2. 创建Session | 72 | # 2. 创建Session |
| @@ -73,9 +76,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 73 | graph_id = 1 | 76 | graph_id = 1 |
| 74 | ret = session.add_graph(graph_id, graph) | 77 | ret = session.add_graph(graph_id, graph) |
| 75 | if ret != 0: | 78 | if ret != 0: |
| 76 | - print(f"添加图失败,返回码: {ret}") | 79 | + print(f"Failed to add graph, return code: {ret}") |
| 77 | return ret | 80 | return ret |
| 78 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 81 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 79 | 82 | ||
| 80 | # 4. 准备输入数据 | 83 | # 4. 准备输入数据 |
| 81 | input1_data = np.random.randn(2, 3).astype(np.float32) | 84 | input1_data = np.random.randn(2, 3).astype(np.float32) |
| @@ -101,13 +104,13 @@ def run_matmul_add_graph(graph) -> int: | |||
| 101 | # 5. 运行图 | 104 | # 5. 运行图 |
| 102 | ret = session.run_graph(graph_id, inputs) | 105 | ret = session.run_graph(graph_id, inputs) |
| 103 | if not isinstance(ret, list): | 106 | if not isinstance(ret, list): |
| 104 | - print(f"运行图失败,返回码: {ret}") | 107 | + print(f"Failed to run graph, return code: {ret}") |
| 105 | return ret | 108 | return ret |
| 106 | - print("图运行成功!") | 109 | + print("Graph ran successfully!") |
| 107 | return 0 | 110 | return 0 |
| 108 | 111 | ||
| 109 | except Exception as e: | 112 | except Exception as e: |
| 110 | - print(f"[Error] 执行过程中出错: {e}") | 113 | + print(f"[Error] Error during execution: {e}") |
| 111 | import traceback | 114 | import traceback |
| 112 | 115 | ||
| 113 | traceback.print_exc() | 116 | traceback.print_exc() |
| @@ -115,9 +118,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 115 | 118 | ||
| 116 | finally: | 119 | finally: |
| 117 | # 6. 清理GE环境 | 120 | # 6. 清理GE环境 |
| 118 | - print("[Info] 清理GE环境...") | 121 | + print("[Info] Cleaning up GE environment...") |
| 119 | ge_api.ge_finalize() | 122 | ge_api.ge_finalize() |
| 120 | - print("[Success] GE环境已清理") | 123 | + print("[Success] GE environment cleaned up") |
| 121 | 124 | ||
| 122 | 125 | ||
| 123 | origin_graph = build_matmul_add_graph() | 126 | origin_graph = build_matmul_add_graph() |
| @@ -10,6 +10,7 @@ | |||
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | 12 | ||
| 13 | +# ruff: noqa: F403, F405 | ||
| 13 | import numpy as np | 14 | import numpy as np |
| 14 | from ge.es import GraphBuilder | 15 | from ge.es import GraphBuilder |
| 15 | from ge.es.all import * | 16 | from ge.es.all import * |
| @@ -67,9 +68,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 67 | ge_api = GeApi() | 68 | ge_api = GeApi() |
| 68 | ret = ge_api.ge_initialize(config) | 69 | ret = ge_api.ge_initialize(config) |
| 69 | if ret != 0: | 70 | if ret != 0: |
| 70 | - print(f"GE初始化失败,返回码: {ret}") | 71 | + print(f"GE initialization failed, return code: {ret}") |
| 71 | return ret | 72 | return ret |
| 72 | - print("GE环境初始化成功 (Device ID: 0)") | 73 | + print("GE environment initialized successfully (Device ID: 0)") |
| 73 | 74 | ||
| 74 | try: | 75 | try: |
| 75 | # 2. 创建Session | 76 | # 2. 创建Session |
| @@ -79,9 +80,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 79 | graph_id = 1 | 80 | graph_id = 1 |
| 80 | ret = session.add_graph(graph_id, graph) | 81 | ret = session.add_graph(graph_id, graph) |
| 81 | if ret != 0: | 82 | if ret != 0: |
| 82 | - print(f"添加图失败,返回码: {ret}") | 83 | + print(f"Failed to add graph, return code: {ret}") |
| 83 | return ret | 84 | return ret |
| 84 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 85 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 85 | 86 | ||
| 86 | # 4. 准备输入数据 | 87 | # 4. 准备输入数据 |
| 87 | input1_data = np.random.randn(3, 2).astype(np.float32) | 88 | input1_data = np.random.randn(3, 2).astype(np.float32) |
| @@ -107,13 +108,13 @@ def run_matmul_add_graph(graph) -> int: | |||
| 107 | # 5. 运行图 | 108 | # 5. 运行图 |
| 108 | ret = session.run_graph(graph_id, inputs) | 109 | ret = session.run_graph(graph_id, inputs) |
| 109 | if not isinstance(ret, list): | 110 | if not isinstance(ret, list): |
| 110 | - print(f"运行图失败,返回码: {ret}") | 111 | + print(f"Failed to run graph, return code: {ret}") |
| 111 | return ret | 112 | return ret |
| 112 | - print("图运行成功!") | 113 | + print("Graph ran successfully!") |
| 113 | return 0 | 114 | return 0 |
| 114 | 115 | ||
| 115 | except Exception as e: | 116 | except Exception as e: |
| 116 | - print(f"[Error] 执行过程中出错: {e}") | 117 | + print(f"[Error] Error during execution: {e}") |
| 117 | import traceback | 118 | import traceback |
| 118 | 119 | ||
| 119 | traceback.print_exc() | 120 | traceback.print_exc() |
| @@ -121,9 +122,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 121 | 122 | ||
| 122 | finally: | 123 | finally: |
| 123 | # 6. 清理GE环境 | 124 | # 6. 清理GE环境 |
| 124 | - print("[Info] 清理GE环境...") | 125 | + print("[Info] Cleaning up GE environment...") |
| 125 | ge_api.ge_finalize() | 126 | ge_api.ge_finalize() |
| 126 | - print("[Success] GE环境已清理") | 127 | + print("[Success] GE environment cleaned up") |
| 127 | 128 | ||
| 128 | 129 | ||
| 129 | origin_graph = build_matmul_add_graph() | 130 | origin_graph = build_matmul_add_graph() |
| @@ -10,6 +10,7 @@ | |||
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | 12 | ||
| 13 | +# ruff: noqa: F403, F405 | ||
| 13 | import numpy as np | 14 | import numpy as np |
| 14 | from ge.es import GraphBuilder | 15 | from ge.es import GraphBuilder |
| 15 | from ge.es.all import * | 16 | from ge.es.all import * |
| @@ -33,7 +34,9 @@ def build_matmul_add_graph(): | |||
| 33 | Format.FORMAT_ND, | 34 | Format.FORMAT_ND, |
| 34 | [2, 2], | 35 | [2, 2], |
| 35 | ) | 36 | ) |
| 36 | - matmul_tensor_holder = MatMul(input1, input2, None, transpose_x1=False, transpose_x2=False) | 37 | + matmul_tensor_holder = MatMul( |
| 38 | + input1, input2, None, transpose_x1=False, transpose_x2=False | ||
| 39 | + ) | ||
| 37 | add_tensor_holder = Add(matmul_tensor_holder, Const(builder, value=input3)) | 40 | add_tensor_holder = Add(matmul_tensor_holder, Const(builder, value=input3)) |
| 38 | # 3、设置图输出节点 | 41 | # 3、设置图输出节点 |
| 39 | builder.set_graph_output(add_tensor_holder, 0) | 42 | builder.set_graph_output(add_tensor_holder, 0) |
| @@ -62,9 +65,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 62 | ge_api = GeApi() | 65 | ge_api = GeApi() |
| 63 | ret = ge_api.ge_initialize(config) | 66 | ret = ge_api.ge_initialize(config) |
| 64 | if ret != 0: | 67 | if ret != 0: |
| 65 | - print(f"GE初始化失败,返回码: {ret}") | 68 | + print(f"GE initialization failed, return code: {ret}") |
| 66 | return ret | 69 | return ret |
| 67 | - print("GE环境初始化成功 (Device ID: 0)") | 70 | + print("GE environment initialized successfully (Device ID: 0)") |
| 68 | 71 | ||
| 69 | try: | 72 | try: |
| 70 | # 2. 创建Session | 73 | # 2. 创建Session |
| @@ -74,9 +77,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 74 | graph_id = 1 | 77 | graph_id = 1 |
| 75 | ret = session.add_graph(graph_id, graph) | 78 | ret = session.add_graph(graph_id, graph) |
| 76 | if ret != 0: | 79 | if ret != 0: |
| 77 | - print(f"添加图失败,返回码: {ret}") | 80 | + print(f"Failed to add graph, return code: {ret}") |
| 78 | return ret | 81 | return ret |
| 79 | - print(f"图已添加到Session (Graph ID: {graph_id})") | 82 | + print(f"Graph added to Session (Graph ID: {graph_id})") |
| 80 | 83 | ||
| 81 | # 4. 准备输入数据 | 84 | # 4. 准备输入数据 |
| 82 | input1_data = np.random.randn(2, 3).astype(np.float32) | 85 | input1_data = np.random.randn(2, 3).astype(np.float32) |
| @@ -102,13 +105,13 @@ def run_matmul_add_graph(graph) -> int: | |||
| 102 | # 5. 运行图 | 105 | # 5. 运行图 |
| 103 | ret = session.run_graph(graph_id, inputs) | 106 | ret = session.run_graph(graph_id, inputs) |
| 104 | if not isinstance(ret, list): | 107 | if not isinstance(ret, list): |
| 105 | - print(f"运行图失败,返回码: {ret}") | 108 | + print(f"Failed to run graph, return code: {ret}") |
| 106 | return ret | 109 | return ret |
| 107 | - print("图运行成功!") | 110 | + print("Graph ran successfully!") |
| 108 | return 0 | 111 | return 0 |
| 109 | 112 | ||
| 110 | except Exception as e: | 113 | except Exception as e: |
| 111 | - print(f"[Error] 执行过程中出错: {e}") | 114 | + print(f"[Error] Error during execution: {e}") |
| 112 | import traceback | 115 | import traceback |
| 113 | 116 | ||
| 114 | traceback.print_exc() | 117 | traceback.print_exc() |
| @@ -116,9 +119,9 @@ def run_matmul_add_graph(graph) -> int: | |||
| 116 | 119 | ||
| 117 | finally: | 120 | finally: |
| 118 | # 6. 清理GE环境 | 121 | # 6. 清理GE环境 |
| 119 | - print("[Info] 清理GE环境...") | 122 | + print("[Info] Cleaning up GE environment...") |
| 120 | ge_api.ge_finalize() | 123 | ge_api.ge_finalize() |
| 121 | - print("[Success] GE环境已清理") | 124 | + print("[Success] GE environment cleaned up") |
| 122 | 125 | ||
| 123 | 126 | ||
| 124 | origin_graph = build_matmul_add_graph() | 127 | origin_graph = build_matmul_add_graph() |
| @@ -52,16 +52,24 @@ def _require_es_apis() -> None: | |||
| 52 | missing = [name for name, obj in pairs if obj is None] | 52 | missing = [name for name, obj in pairs if obj is None] |
| 53 | if missing: | 53 | if missing: |
| 54 | raise RuntimeError( | 54 | raise RuntimeError( |
| 55 | - "未找到 ES API: " | 55 | + "ES API not found: " |
| 56 | + ", ".join(missing) | 56 | + ", ".join(missing) |
| 57 | - + "。请先 source CANN 环境;如仍缺失,请参考 README 的“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 57 | + + ". Please source the CANN environment first; if still missing, refer to the README " |
| 58 | + + "section 'Handling Missing ES APIs (Optional)' to generate and load es_all, then run again." | ||
| 58 | ) | 59 | ) |
| 59 | 60 | ||
| 60 | 61 | ||
| 61 | -@register_fusion_pass(name="PythonMatmulAddFusionPass", stage=PassStage.BEFORE_INFER_SHAPE) | 62 | +@register_fusion_pass( |
| 63 | + name="PythonMatmulAddFusionPass", stage=PassStage.BEFORE_INFER_SHAPE | ||
| 64 | +) | ||
| 62 | class PythonMatmulAddFusionPass(PatternFusionPass): | 65 | class PythonMatmulAddFusionPass(PatternFusionPass): |
| 63 | def __init__(self): | 66 | def __init__(self): |
| 64 | - super().__init__(PatternMatcherConfigBuilder().enable_const_value_match().enable_ir_attr_match().build()) | 67 | + super().__init__( |
| 68 | + PatternMatcherConfigBuilder() | ||
| 69 | + .enable_const_value_match() | ||
| 70 | + .enable_ir_attr_match() | ||
| 71 | + .build() | ||
| 72 | + ) | ||
| 65 | 73 | ||
| 66 | @pattern | 74 | @pattern |
| 67 | def matmul_add(self, inputs): | 75 | def matmul_add(self, inputs): |
| @@ -89,6 +97,6 @@ class PythonMatmulAddFusionPass(PatternFusionPass): | |||
| 89 | 97 | ||
| 90 | 98 | ||
| 91 | if __name__ == "__main__": | 99 | if __name__ == "__main__": |
| 92 | - print("PythonMatmulAddFusionPass 已注册。") | 100 | + print("PythonMatmulAddFusionPass registered.") |
| 93 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 101 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 94 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_fuse_matmul_add_pass.py") | 102 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_fuse_matmul_add_pass.py") |
| @@ -73,6 +73,6 @@ class PythonAddZeroPass(PatternFusionPass): | |||
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | if __name__ == "__main__": | 75 | if __name__ == "__main__": |
| 76 | - print("PythonAddZeroPass 已注册。") | 76 | + print("PythonAddZeroPass registered.") |
| 77 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 77 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 78 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_add_zero_pass.py") | 78 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_add_zero_pass.py") |
| @@ -21,12 +21,16 @@ from ge.passes import ( | |||
| 21 | ) | 21 | ) |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | -@register_fusion_pass(name="PythonAddZeroConstValueMatchPass", stage=PassStage.BEFORE_INFER_SHAPE) | 24 | +@register_fusion_pass( |
| 25 | + name="PythonAddZeroConstValueMatchPass", stage=PassStage.BEFORE_INFER_SHAPE | ||
| 26 | +) | ||
| 25 | class PythonAddZeroConstValueMatchPass(PatternFusionPass): | 27 | class PythonAddZeroConstValueMatchPass(PatternFusionPass): |
| 26 | """Recognize Add(x, 0.0f) with strict const-value-match and replace it with x.""" | 28 | """Recognize Add(x, 0.0f) with strict const-value-match and replace it with x.""" |
| 27 | 29 | ||
| 28 | def __init__(self): | 30 | def __init__(self): |
| 29 | - super().__init__(PatternMatcherConfigBuilder().enable_const_value_match().build()) | 31 | + super().__init__( |
| 32 | + PatternMatcherConfigBuilder().enable_const_value_match().build() | ||
| 33 | + ) | ||
| 30 | 34 | ||
| 31 | @pattern | 35 | @pattern |
| 32 | def add_zero(self, inputs): | 36 | def add_zero(self, inputs): |
| @@ -45,6 +49,8 @@ class PythonAddZeroConstValueMatchPass(PatternFusionPass): | |||
| 45 | 49 | ||
| 46 | 50 | ||
| 47 | if __name__ == "__main__": | 51 | if __name__ == "__main__": |
| 48 | - print("PythonAddZeroConstValueMatchPass 已注册。") | 52 | + print("PythonAddZeroConstValueMatchPass registered.") |
| 49 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 53 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 50 | - print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_add_zero_pass_const_value_match.py") | 54 | + print( |
| 55 | + " export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_add_zero_pass_const_value_match.py" | ||
| 56 | + ) | ||
| @@ -42,8 +42,8 @@ except ImportError: | |||
| 42 | def _require_es_custom_apis() -> None: | 42 | def _require_es_custom_apis() -> None: |
| 43 | if AddCustom is None: | 43 | if AddCustom is None: |
| 44 | raise RuntimeError( | 44 | raise RuntimeError( |
| 45 | - "未找到 ge.es.custom.AddCustom。请先参考 README 的使用方式生成 es_custom Python ES 包," | 45 | + "ge.es.custom.AddCustom not found. Please generate the es_custom Python ES package " |
| 46 | - "设置 PYTHONPATH 与 LD_LIBRARY_PATH 后重新执行。" | 46 | + "as described in the README, set PYTHONPATH and LD_LIBRARY_PATH, then run again." |
| 47 | ) | 47 | ) |
| 48 | 48 | ||
| 49 | 49 | ||
| @@ -51,11 +51,13 @@ def _require_es_apis() -> None: | |||
| 51 | _require_es_custom_apis() | 51 | _require_es_custom_apis() |
| 52 | if Identity is None: | 52 | if Identity is None: |
| 53 | raise RuntimeError( | 53 | raise RuntimeError( |
| 54 | - "未找到 ge.es.math.Identity。请先 source CANN 环境;如仍缺失,请参考 README 的“ES API 说明”后重新执行。" | 54 | + "ge.es.math.Identity not found. Please source the CANN environment first; " |
| 55 | + "if still missing, refer to the README section 'ES API Description' and run again." | ||
| 55 | ) | 56 | ) |
| 56 | if TensorMove is None: | 57 | if TensorMove is None: |
| 57 | raise RuntimeError( | 58 | raise RuntimeError( |
| 58 | - "未找到 ge.es.math.TensorMove。请先 source CANN 环境;如仍缺失,请参考 README 的“ES API 说明”后重新执行。" | 59 | + "ge.es.math.TensorMove not found. Please source the CANN environment first; " |
| 60 | + "if still missing, refer to the README section 'ES API Description' and run again." | ||
| 59 | ) | 61 | ) |
| 60 | 62 | ||
| 61 | 63 | ||
| @@ -83,7 +85,9 @@ def _is_tensor_value_equal_to_zero(tensor: Tensor): | |||
| 83 | return False | 85 | return False |
| 84 | 86 | ||
| 85 | 87 | ||
| 86 | -@register_fusion_pass(name="PythonAddCustomZeroPass", stage=PassStage.BEFORE_INFER_SHAPE) | 88 | +@register_fusion_pass( |
| 89 | + name="PythonAddCustomZeroPass", stage=PassStage.BEFORE_INFER_SHAPE | ||
| 90 | +) | ||
| 87 | class PythonAddCustomZeroPass(PatternFusionPass): | 91 | class PythonAddCustomZeroPass(PatternFusionPass): |
| 88 | """Recognize AddCustom(AddCustom(x, zero), y) and replace it with AddCustom(x, y).""" | 92 | """Recognize AddCustom(AddCustom(x, zero), y) and replace it with AddCustom(x, y).""" |
| 89 | 93 | ||
| @@ -132,7 +136,7 @@ class PythonAddCustomZeroPass(PatternFusionPass): | |||
| 132 | 136 | ||
| 133 | 137 | ||
| 134 | if __name__ == "__main__": | 138 | if __name__ == "__main__": |
| 135 | - print("PythonAddCustomZeroPass 已注册。") | 139 | + print("PythonAddCustomZeroPass registered.") |
| 136 | - print("运行前请先生成 es_custom,并设置 PYTHONPATH 与 LD_LIBRARY_PATH。") | 140 | + print("Generate es_custom and set PYTHONPATH and LD_LIBRARY_PATH before running.") |
| 137 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 141 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 138 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_addcustom_zero_pass.py") | 142 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_addcustom_zero_pass.py") |
| @@ -47,18 +47,21 @@ except ImportError: | |||
| 47 | def _require_es_apis() -> None: | 47 | def _require_es_apis() -> None: |
| 48 | if Split is None: | 48 | if Split is None: |
| 49 | raise RuntimeError( | 49 | raise RuntimeError( |
| 50 | - "未找到 ge.es.math.Split。请先 source CANN 环境;如仍缺失,请参考 README 的" | 50 | + "ge.es.math.Split not found. Please source the CANN environment first; " |
| 51 | - "“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 51 | + "if still missing, refer to the README section 'Handling Missing ES APIs (Optional)' " |
| 52 | + "to generate and load es_all, then run again." | ||
| 52 | ) | 53 | ) |
| 53 | if Conv2D is None: | 54 | if Conv2D is None: |
| 54 | raise RuntimeError( | 55 | raise RuntimeError( |
| 55 | - "未找到 ge.es.nn.Conv2D / ge.es.all.Conv2D。请先 source CANN 环境;如仍缺失," | 56 | + "ge.es.nn.Conv2D / ge.es.all.Conv2D not found. Please source the CANN environment first; " |
| 56 | - "请参考 README 的“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 57 | + "if still missing, refer to the README section 'Handling Missing ES APIs (Optional)' " |
| 58 | + "to generate and load es_all, then run again." | ||
| 57 | ) | 59 | ) |
| 58 | if Concat is None and ConcatV2 is None: | 60 | if Concat is None and ConcatV2 is None: |
| 59 | raise RuntimeError( | 61 | raise RuntimeError( |
| 60 | - "未找到 ge.es.math.Concat / ConcatV2。请先 source CANN 环境;如仍缺失,请参考 README 的" | 62 | + "ge.es.math.Concat / ConcatV2 not found. Please source the CANN environment first; " |
| 61 | - "“ES API 缺失时处理(可选)”生成并加载 es_all 后重新执行。" | 63 | + "if still missing, refer to the README section 'Handling Missing ES APIs (Optional)' " |
| 64 | + "to generate and load es_all, then run again." | ||
| 62 | ) | 65 | ) |
| 63 | 66 | ||
| 64 | 67 | ||
| @@ -133,6 +136,6 @@ class PythonDecomposeGroupedConvToSplitedPass(DecomposePass): | |||
| 133 | 136 | ||
| 134 | 137 | ||
| 135 | if __name__ == "__main__": | 138 | if __name__ == "__main__": |
| 136 | - print("PythonDecomposeGroupedConvToSplitedPass 已注册。") | 139 | + print("PythonDecomposeGroupedConvToSplitedPass registered.") |
| 137 | - print("请通过 ASCEND_GE_PY_PASS_PATH 指向本文件,例如:") | 140 | + print("Please add this file to ASCEND_GE_PY_PASS_PATH, for example:") |
| 138 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_decompose_pass.py") | 141 | print(" export ASCEND_GE_PY_PASS_PATH=$PWD/src/python_decompose_pass.py") |
| @@ -127,20 +127,20 @@ python3 ge_ms_autotune.py \ | |||
| 127 | → 执行命令 → 从 stdout 与该轮 plog 中收集 STEP → 校验后统计。控制台输出形如: | 127 | → 执行命令 → 从 stdout 与该轮 plog 中收集 STEP → 校验后统计。控制台输出形如: |
| 128 | 128 | ||
| 129 | ``` | 129 | ``` |
| 130 | -候选配置(7 个 × 3 轮):default, LoadBalance:2, LoadBalance:4, ... | 130 | +candidate configs (7 x 3 rounds, online mode): default, LoadBalance:2, LoadBalance:4, ... |
| 131 | 131 | ||
| 132 | -[000] 配置=default 第 1 轮:python3 sample_run.py --steps 12 | 132 | +[000] config=default round 1: python3 sample_run.py --steps 12 |
| 133 | - 退出码=0 STEP=13 有效=是 耗时=21.4s | 133 | + exit_code=0 STEP=13 valid=yes wall=21.4s |
| 134 | ... | 134 | ... |
| 135 | 135 | ||
| 136 | -寻优结果(按中位耗时升序): | 136 | +Tuning results (sorted by median cost): |
| 137 | -配置 有效轮次 步数 平均(ms) 中位(ms) P90(ms) CV 加速比 结论 | 137 | +config valid_runs steps mean(ms) median(ms) P90(ms) CV speedup verdict |
| 138 | -LoadBalance:4 3/3 36 12.104 12.088 12.301 0.014 1.243 提升 | 138 | +LoadBalance:4 3/3 36 12.104 12.088 12.301 0.014 1.243 improvement |
| 139 | -MainStream:4 3/3 36 13.552 13.489 13.702 0.011 1.114 提升 | 139 | +MainStream:4 3/3 36 13.552 13.489 13.702 0.011 1.114 improvement |
| 140 | -default 3/3 36 15.037 15.028 15.311 0.009 1.000 持平 | 140 | +default 3/3 36 15.037 15.028 15.311 0.009 1.000 neutral |
| 141 | 141 | ||
| 142 | -[结论] 推荐配置:LoadBalance:4,相对 default 加速比 1.243,中位耗时 12.088 ms。 | 142 | +[result] recommended config: LoadBalance:4, speedup over default: 1.243, median cost: 12.088 ms. |
| 143 | -[复现] GE_AUTO_MULTISTREAM_PARALLEL_MODE=LoadBalance:4 python3 sample_run.py --steps 12 | 143 | +[reproduce] GE_AUTO_MULTISTREAM_PARALLEL_MODE=LoadBalance:4 python3 sample_run.py --steps 12 |
| 144 | ``` | 144 | ``` |
| 145 | 145 | ||
| 146 | 一阶段扫完后若想细化流数,指定相邻取值再跑一次即可: | 146 | 一阶段扫完后若想细化流数,指定相邻取值再跑一次即可: |
| @@ -137,20 +137,20 @@ For every candidate the driver injects `GE_AUTO_MULTISTREAM_PARALLEL_MODE` and a | |||
| 137 | run's plog directory, then validates and aggregates them. The console output looks like: | 137 | run's plog directory, then validates and aggregates them. The console output looks like: |
| 138 | 138 | ||
| 139 | ``` | 139 | ``` |
| 140 | -候选配置(7 个 × 3 轮):default, LoadBalance:2, LoadBalance:4, ... | 140 | +candidate configs (7 x 3 rounds, online mode): default, LoadBalance:2, LoadBalance:4, ... |
| 141 | 141 | ||
| 142 | -[000] 配置=default 第 1 轮:python3 sample_run.py --steps 12 | 142 | +[000] config=default round 1: python3 sample_run.py --steps 12 |
| 143 | - 退出码=0 STEP=13 有效=是 耗时=21.4s | 143 | + exit_code=0 STEP=13 valid=yes wall=21.4s |
| 144 | ... | 144 | ... |
| 145 | 145 | ||
| 146 | -寻优结果(按中位耗时升序): | 146 | +Tuning results (sorted by median cost): |
| 147 | -配置 有效轮次 步数 平均(ms) 中位(ms) P90(ms) CV 加速比 结论 | 147 | +config valid_runs steps mean(ms) median(ms) P90(ms) CV speedup verdict |
| 148 | -LoadBalance:4 3/3 36 12.104 12.088 12.301 0.014 1.243 提升 | 148 | +LoadBalance:4 3/3 36 12.104 12.088 12.301 0.014 1.243 improvement |
| 149 | -MainStream:4 3/3 36 13.552 13.489 13.702 0.011 1.114 提升 | 149 | +MainStream:4 3/3 36 13.552 13.489 13.702 0.011 1.114 improvement |
| 150 | -default 3/3 36 15.037 15.028 15.311 0.009 1.000 持平 | 150 | +default 3/3 36 15.037 15.028 15.311 0.009 1.000 neutral |
| 151 | 151 | ||
| 152 | -[结论] 推荐配置:LoadBalance:4,相对 default 加速比 1.243,中位耗时 12.088 ms。 | 152 | +[result] recommended config: LoadBalance:4, speedup over default: 1.243, median cost: 12.088 ms. |
| 153 | -[复现] GE_AUTO_MULTISTREAM_PARALLEL_MODE=LoadBalance:4 python3 sample_run.py --steps 12 | 153 | +[reproduce] GE_AUTO_MULTISTREAM_PARALLEL_MODE=LoadBalance:4 python3 sample_run.py --steps 12 |
| 154 | ``` | 154 | ``` |
| 155 | 155 | ||
| 156 | To refine the stream count around the winner, run the neighbouring values again: | 156 | To refine the stream count around the winner, run the neighbouring values again: |
| @@ -139,16 +139,22 @@ def validate_config(config: str) -> None: | |||
| 139 | return | 139 | return |
| 140 | if config.count(":") != 1: | 140 | if config.count(":") != 1: |
| 141 | raise AutotuneError( | 141 | raise AutotuneError( |
| 142 | - "非法配置 {!r}:格式应为“策略名:流数”,例如 LoadBalance:2。".format(config) | 142 | + "Invalid config {!r}: format must be 'strategy:streams', e.g. LoadBalance:2.".format( |
| 143 | + config | ||
| 144 | + ) | ||
| 143 | ) | 145 | ) |
| 144 | strategy, streams = config.split(":", 1) | 146 | strategy, streams = config.split(":", 1) |
| 145 | if strategy not in STRATEGIES or strategy == "cv": | 147 | if strategy not in STRATEGIES or strategy == "cv": |
| 146 | raise AutotuneError( | 148 | raise AutotuneError( |
| 147 | - "未知策略 {!r},可选:{}。".format(strategy, "/".join(STRATEGIES)) | 149 | + "Unknown strategy {!r}, available: {}.".format( |
| 150 | + strategy, "/".join(STRATEGIES) | ||
| 151 | + ) | ||
| 148 | ) | 152 | ) |
| 149 | if not streams.isdigit() or not 1 <= int(streams) <= MAX_STREAMS: | 153 | if not streams.isdigit() or not 1 <= int(streams) <= MAX_STREAMS: |
| 150 | raise AutotuneError( | 154 | raise AutotuneError( |
| 151 | - "配置 {!r} 的流数必须是 [1,{}] 的十进制整数。".format(config, MAX_STREAMS) | 155 | + "Stream count in config {!r} must be a decimal integer in [1,{}].".format( |
| 156 | + config, MAX_STREAMS | ||
| 157 | + ) | ||
| 152 | ) | 158 | ) |
| 153 | 159 | ||
| 154 | 160 | ||
| @@ -93,17 +93,17 @@ def execute(args: argparse.Namespace) -> int: | |||
| 93 | session = Session() | 93 | session = Session() |
| 94 | session.add_graph(graph_id, build_graph(args.dim)) | 94 | session.add_graph(graph_id, build_graph(args.dim)) |
| 95 | inputs = create_inputs(args.dim) | 95 | inputs = create_inputs(args.dim) |
| 96 | - outputs = session.run_graph(graph_id, inputs) # STEP 0:预热 | 96 | + outputs = session.run_graph(graph_id, inputs) # STEP 0: warmup |
| 97 | for _ in range(args.steps): | 97 | for _ in range(args.steps): |
| 98 | outputs = session.run_graph(graph_id, inputs) | 98 | outputs = session.run_graph(graph_id, inputs) |
| 99 | print( | 99 | print( |
| 100 | - "[Info] 样例执行完成:device={}, steps={}, outputs={}".format( | 100 | + "[Info] sample execution completed: device={}, steps={}, outputs={}".format( |
| 101 | args.device, args.steps, [output.get_shape() for output in outputs] | 101 | args.device, args.steps, [output.get_shape() for output in outputs] |
| 102 | ) | 102 | ) |
| 103 | ) | 103 | ) |
| 104 | return 0 | 104 | return 0 |
| 105 | - except Exception as error: # noqa: BLE001 - 样例进程以退出码反馈失败即可 | 105 | + except Exception as error: # noqa: BLE001 - the sample reports failure via its exit code |
| 106 | - print("[Error] 样例执行失败:{}".format(error), file=sys.stderr) | 106 | + print("[Error] sample execution failed: {}".format(error), file=sys.stderr) |
| 107 | return 1 | 107 | return 1 |
| 108 | finally: | 108 | finally: |
| 109 | # Session 必须先于 GE 去初始化释放。 | 109 | # Session 必须先于 GE 去初始化释放。 |
| @@ -22,7 +22,7 @@ bool ParseSocVersion(int argc, char **argv, int start_index, std::string *soc_ve | |||
| 22 | for (int i = start_index; i < argc; ++i) { | 22 | for (int i = start_index; i < argc; ++i) { |
| 23 | if (std::strcmp(argv[i], "--soc-version") == 0) { | 23 | if (std::strcmp(argv[i], "--soc-version") == 0) { |
| 24 | if (i + 1 >= argc) { | 24 | if (i + 1 >= argc) { |
| 25 | - std::cerr << "[Error] --soc-version 参数为空\n"; | 25 | + std::cerr << "[Error] --soc-version argument is empty\n"; |
| 26 | return false; | 26 | return false; |
| 27 | } | 27 | } |
| 28 | *soc_version = argv[i + 1]; | 28 | *soc_version = argv[i + 1]; |
| @@ -36,7 +36,7 @@ bool ParseSocVersion(int argc, char **argv, int start_index, std::string *soc_ve | |||
| 36 | 36 | ||
| 37 | int main(int argc, char **argv) { | 37 | int main(int argc, char **argv) { |
| 38 | if (argc < 2) { | 38 | if (argc < 2) { |
| 39 | - std::cerr << "[Error] 用法: offline_compile_sample <命令> [--soc-version <版本>]\n" | 39 | + std::cerr << "[Error] Usage: offline_compile_sample <command> [--soc-version <version>]\n" |
| 40 | << " 命令: build-model | build-bundle | run-infer | run-bundle-infer\n"; | 40 | << " 命令: build-model | build-bundle | run-infer | run-bundle-infer\n"; |
| 41 | return -1; | 41 | return -1; |
| 42 | } | 42 | } |
| @@ -57,6 +57,6 @@ int main(int argc, char **argv) { | |||
| 57 | if (cmd == "run-bundle-infer") { | 57 | if (cmd == "run-bundle-infer") { |
| 58 | return RunBundleModelInfer(); | 58 | return RunBundleModelInfer(); |
| 59 | } | 59 | } |
| 60 | - std::cerr << "[Error] 未知命令: " << cmd << std::endl; | 60 | + std::cerr << "[Error] Unknown command: " << cmd << std::endl; |
| 61 | return -1; | 61 | return -1; |
| 62 | } | 62 | } |
| @@ -37,10 +37,10 @@ int BuildBundleModel(const std::string &soc_version) { | |||
| 37 | auto global_options = GlobalOptionsWithSoc(soc_version); | 37 | auto global_options = GlobalOptionsWithSoc(soc_version); |
| 38 | auto ret = ge::aclgrphBuildInitialize(global_options); | 38 | auto ret = ge::aclgrphBuildInitialize(global_options); |
| 39 | if (ret != ge::GRAPH_SUCCESS) { | 39 | if (ret != ge::GRAPH_SUCCESS) { |
| 40 | - std::cerr << "[Error] aclgrphBuildInitialize 失败" << static_cast<int>(ret) << std::endl; | 40 | + std::cerr << "[Error] aclgrphBuildInitialize failed" << static_cast<int>(ret) << std::endl; |
| 41 | return -1; | 41 | return -1; |
| 42 | } | 42 | } |
| 43 | - std::cout << "[Info] 系统初始化成功\n"; | 43 | + std::cout << "[Info] System initialized successfully\n"; |
| 44 | 44 | ||
| 45 | auto add_graph = MakeOfflineAddGraph(); | 45 | auto add_graph = MakeOfflineAddGraph(); |
| 46 | auto mul_graph = MakeOfflineMulGraph(); | 46 | auto mul_graph = MakeOfflineMulGraph(); |
| @@ -51,22 +51,23 @@ int BuildBundleModel(const std::string &soc_version) { | |||
| 51 | ge::ModelBufferData model; | 51 | ge::ModelBufferData model; |
| 52 | ret = ge::aclgrphBundleBuildModel(bundle, model); | 52 | ret = ge::aclgrphBundleBuildModel(bundle, model); |
| 53 | if (ret != ge::GRAPH_SUCCESS) { | 53 | if (ret != ge::GRAPH_SUCCESS) { |
| 54 | - std::cerr << "[Error] aclgrphBundleBuildModel 失败, graphStatus=" << static_cast<int>(ret) << std::endl; | 54 | + std::cerr << "[Error] aclgrphBundleBuildModel failed, graphStatus=" << static_cast<int>(ret) << std::endl; |
| 55 | ge::aclgrphBuildFinalize(); | 55 | ge::aclgrphBuildFinalize(); |
| 56 | return -1; | 56 | return -1; |
| 57 | } | 57 | } |
| 58 | - std::cout << "[Info] Bundle 构建成功,模型大小: " << model.length << " bytes\n"; | 58 | + std::cout << "[Info] Bundle built successfully, model size: " << model.length << " bytes\n"; |
| 59 | 59 | ||
| 60 | const char *const output_file = "bundle_sample"; | 60 | const char *const output_file = "bundle_sample"; |
| 61 | ret = ge::aclgrphBundleSaveModel(output_file, model); | 61 | ret = ge::aclgrphBundleSaveModel(output_file, model); |
| 62 | if (ret != ge::GRAPH_SUCCESS) { | 62 | if (ret != ge::GRAPH_SUCCESS) { |
| 63 | - std::cerr << "[Error] aclgrphBundleSaveModel 失败, graphStatus=" << static_cast<int>(ret) << std::endl; | 63 | + std::cerr << "[Error] aclgrphBundleSaveModel failed, graphStatus=" << static_cast<int>(ret) << std::endl; |
| 64 | ge::aclgrphBuildFinalize(); | 64 | ge::aclgrphBuildFinalize(); |
| 65 | return -1; | 65 | return -1; |
| 66 | } | 66 | } |
| 67 | - std::cout << "[Info] 模型保存成功," << output_file << ".om 已生成在当前目录。\n"; | 67 | + std::cout << "[Info] Model saved successfully, " << output_file |
| 68 | + << ".om has been generated in the current directory.\n"; | ||
| 68 | 69 | ||
| 69 | ge::aclgrphBuildFinalize(); | 70 | ge::aclgrphBuildFinalize(); |
| 70 | - std::cout << "[Info] 系统释放成功\n"; | 71 | + std::cout << "[Info] System resources released successfully\n"; |
| 71 | return 0; | 72 | return 0; |
| 72 | } | 73 | } |
| @@ -21,14 +21,14 @@ ge::Status LoadBundleModelIds(uint32_t bundle_id, std::vector<uint32_t> *model_i | |||
| 21 | size_t model_num = 0; | 21 | size_t model_num = 0; |
| 22 | aclError err = aclmdlBundleGetModelNum(bundle_id, &model_num); | 22 | aclError err = aclmdlBundleGetModelNum(bundle_id, &model_num); |
| 23 | if (err != ACL_SUCCESS) { | 23 | if (err != ACL_SUCCESS) { |
| 24 | - std::cerr << "[Error] aclmdlBundleGetModelNum 失败, aclError=" << err << std::endl; | 24 | + std::cerr << "[Error] aclmdlBundleGetModelNum failed, aclError=" << err << std::endl; |
| 25 | return ge::FAILED; | 25 | return ge::FAILED; |
| 26 | } | 26 | } |
| 27 | model_ids->resize(model_num); | 27 | model_ids->resize(model_num); |
| 28 | for (size_t i = 0; i < model_num; ++i) { | 28 | for (size_t i = 0; i < model_num; ++i) { |
| 29 | err = aclmdlBundleGetModelId(bundle_id, i, &(*model_ids)[i]); | 29 | err = aclmdlBundleGetModelId(bundle_id, i, &(*model_ids)[i]); |
| 30 | if (err != ACL_SUCCESS) { | 30 | if (err != ACL_SUCCESS) { |
| 31 | - std::cerr << "[Error] aclmdlBundleGetModelId 失败, aclError=" << err << std::endl; | 31 | + std::cerr << "[Error] aclmdlBundleGetModelId failed, aclError=" << err << std::endl; |
| 32 | return ge::FAILED; | 32 | return ge::FAILED; |
| 33 | } | 33 | } |
| 34 | } | 34 | } |
| @@ -37,15 +37,15 @@ ge::Status LoadBundleModelIds(uint32_t bundle_id, std::vector<uint32_t> *model_i | |||
| 37 | 37 | ||
| 38 | ge::Status RunBundleSubmodelInfer(const uint32_t model_id, const char *label, | 38 | ge::Status RunBundleSubmodelInfer(const uint32_t model_id, const char *label, |
| 39 | const std::vector<std::vector<float>> &host_inputs) { | 39 | const std::vector<std::vector<float>> &host_inputs) { |
| 40 | - std::cout << "[Info] 执行子模型推理,model_id: " << model_id << "\n"; | 40 | + std::cout << "[Info] Executing sub-model inference, model_id: " << model_id << "\n"; |
| 41 | aclmdlDesc *model_desc = aclmdlCreateDesc(); | 41 | aclmdlDesc *model_desc = aclmdlCreateDesc(); |
| 42 | if (model_desc == nullptr) { | 42 | if (model_desc == nullptr) { |
| 43 | - std::cerr << "[Error] aclmdlCreateDesc 失败\n"; | 43 | + std::cerr << "[Error] aclmdlCreateDesc failed\n"; |
| 44 | return ge::FAILED; | 44 | return ge::FAILED; |
| 45 | } | 45 | } |
| 46 | aclError e = aclmdlGetDesc(model_desc, model_id); | 46 | aclError e = aclmdlGetDesc(model_desc, model_id); |
| 47 | if (e != ACL_SUCCESS) { | 47 | if (e != ACL_SUCCESS) { |
| 48 | - std::cerr << "[Error] aclmdlGetDesc 失败, aclError=" << e << std::endl; | 48 | + std::cerr << "[Error] aclmdlGetDesc failed, aclError=" << e << std::endl; |
| 49 | TeardownBundleSubmodelSession(model_desc, nullptr, nullptr); | 49 | TeardownBundleSubmodelSession(model_desc, nullptr, nullptr); |
| 50 | return ge::FAILED; | 50 | return ge::FAILED; |
| 51 | } | 51 | } |
| @@ -65,11 +65,11 @@ ge::Status RunBundleSubmodelInfer(const uint32_t model_id, const char *label, | |||
| 65 | } | 65 | } |
| 66 | e = aclmdlExecute(model_id, input_io.dataset, output_io.dataset); | 66 | e = aclmdlExecute(model_id, input_io.dataset, output_io.dataset); |
| 67 | if (e != ACL_SUCCESS) { | 67 | if (e != ACL_SUCCESS) { |
| 68 | - std::cerr << "[Error] aclmdlExecute 失败, aclError=" << e << std::endl; | 68 | + std::cerr << "[Error] aclmdlExecute failed, aclError=" << e << std::endl; |
| 69 | TeardownBundleSubmodelSession(model_desc, &input_io, &output_io); | 69 | TeardownBundleSubmodelSession(model_desc, &input_io, &output_io); |
| 70 | return ge::FAILED; | 70 | return ge::FAILED; |
| 71 | } | 71 | } |
| 72 | - std::cout << "[Info] " << label << " 推理结果:\n"; | 72 | + std::cout << "[Info] " << label << " inference result:\n"; |
| 73 | if (PrintModelOutputs(model_desc, output_io) != ge::SUCCESS) { | 73 | if (PrintModelOutputs(model_desc, output_io) != ge::SUCCESS) { |
| 74 | TeardownBundleSubmodelSession(model_desc, &input_io, &output_io); | 74 | TeardownBundleSubmodelSession(model_desc, &input_io, &output_io); |
| 75 | return ge::FAILED; | 75 | return ge::FAILED; |
| @@ -80,15 +80,15 @@ ge::Status RunBundleSubmodelInfer(const uint32_t model_id, const char *label, | |||
| 80 | } // namespace | 80 | } // namespace |
| 81 | 81 | ||
| 82 | int RunBundleModelInfer() { | 82 | int RunBundleModelInfer() { |
| 83 | - std::cout << "[Info] 初始化运行环境及数据\n"; | 83 | + std::cout << "[Info] Initializing runtime environment and data\n"; |
| 84 | aclError err = aclInit(nullptr); | 84 | aclError err = aclInit(nullptr); |
| 85 | if (err != ACL_SUCCESS) { | 85 | if (err != ACL_SUCCESS) { |
| 86 | - std::cerr << "[Error] aclInit 失败, aclError=" << err << std::endl; | 86 | + std::cerr << "[Error] aclInit failed, aclError=" << err << std::endl; |
| 87 | return -1; | 87 | return -1; |
| 88 | } | 88 | } |
| 89 | err = aclrtSetDevice(kDeviceId); | 89 | err = aclrtSetDevice(kDeviceId); |
| 90 | if (err != ACL_SUCCESS) { | 90 | if (err != ACL_SUCCESS) { |
| 91 | - std::cerr << "[Error] aclrtSetDevice 失败, aclError=" << err << std::endl; | 91 | + std::cerr << "[Error] aclrtSetDevice failed, aclError=" << err << std::endl; |
| 92 | (void)aclFinalize(); | 92 | (void)aclFinalize(); |
| 93 | return -1; | 93 | return -1; |
| 94 | } | 94 | } |
| @@ -96,7 +96,7 @@ int RunBundleModelInfer() { | |||
| 96 | uint32_t bundle_id = 0; | 96 | uint32_t bundle_id = 0; |
| 97 | err = aclmdlBundleLoadFromFile("bundle_sample.om", &bundle_id); | 97 | err = aclmdlBundleLoadFromFile("bundle_sample.om", &bundle_id); |
| 98 | if (err != ACL_SUCCESS) { | 98 | if (err != ACL_SUCCESS) { |
| 99 | - std::cerr << "[Error] aclmdlBundleLoadFromFile 失败, aclError=" << err << std::endl; | 99 | + std::cerr << "[Error] aclmdlBundleLoadFromFile failed, aclError=" << err << std::endl; |
| 100 | (void)aclrtResetDevice(kDeviceId); | 100 | (void)aclrtResetDevice(kDeviceId); |
| 101 | (void)aclFinalize(); | 101 | (void)aclFinalize(); |
| 102 | return -1; | 102 | return -1; |
| @@ -107,7 +107,7 @@ int RunBundleModelInfer() { | |||
| 107 | TeardownAclBundleInfer(bundle_id); | 107 | TeardownAclBundleInfer(bundle_id); |
| 108 | return -1; | 108 | return -1; |
| 109 | } | 109 | } |
| 110 | - std::cout << "[Info] Bundle 模型加载成功,子模型个数: " << model_ids.size() << "\n"; | 110 | + std::cout << "[Info] Bundle model loaded successfully, number of sub-models: " << model_ids.size() << "\n"; |
| 111 | 111 | ||
| 112 | const auto inputs = SampleInputs(); | 112 | const auto inputs = SampleInputs(); |
| 113 | if (RunBundleSubmodelInfer(model_ids[0], "Add 子模型", inputs) != ge::SUCCESS) { | 113 | if (RunBundleSubmodelInfer(model_ids[0], "Add 子模型", inputs) != ge::SUCCESS) { |
| @@ -120,6 +120,6 @@ int RunBundleModelInfer() { | |||
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | TeardownAclBundleInfer(bundle_id); | 122 | TeardownAclBundleInfer(bundle_id); |
| 123 | - std::cout << "[Info] 所有资源释放成功\n"; | 123 | + std::cout << "[Info] All resources released successfully\n"; |
| 124 | return 0; | 124 | return 0; |
| 125 | } | 125 | } |
| @@ -19,20 +19,20 @@ ge::Status PrintOneInferOutputBuffer(aclmdlDesc *model_desc, size_t buffer_index | |||
| 19 | void *data = aclGetDataBufferAddr(data_buffer); | 19 | void *data = aclGetDataBufferAddr(data_buffer); |
| 20 | const size_t len = aclGetDataBufferSizeV2(data_buffer); | 20 | const size_t len = aclGetDataBufferSizeV2(data_buffer); |
| 21 | if (data == nullptr || len == 0U) { | 21 | if (data == nullptr || len == 0U) { |
| 22 | - std::cerr << "[Error] 输出 DataBuffer 无效, index=" << buffer_index << "\n"; | 22 | + std::cerr << "[Error] Output DataBuffer is invalid, index=" << buffer_index << "\n"; |
| 23 | return ge::FAILED; | 23 | return ge::FAILED; |
| 24 | } | 24 | } |
| 25 | aclmdlIODims dims{}; | 25 | aclmdlIODims dims{}; |
| 26 | aclError err = aclmdlGetOutputDims(model_desc, buffer_index, &dims); | 26 | aclError err = aclmdlGetOutputDims(model_desc, buffer_index, &dims); |
| 27 | if (err != ACL_SUCCESS) { | 27 | if (err != ACL_SUCCESS) { |
| 28 | - std::cerr << "[Error] aclmdlGetOutputDims 失败, aclError=" << err << "\n"; | 28 | + std::cerr << "[Error] aclmdlGetOutputDims failed, aclError=" << err << "\n"; |
| 29 | return ge::FAILED; | 29 | return ge::FAILED; |
| 30 | } | 30 | } |
| 31 | const size_t elem_count = len >= sizeof(float) ? len / sizeof(float) : 0U; | 31 | const size_t elem_count = len >= sizeof(float) ? len / sizeof(float) : 0U; |
| 32 | std::vector<float> host(elem_count); | 32 | std::vector<float> host(elem_count); |
| 33 | err = aclrtMemcpy(host.data(), len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); | 33 | err = aclrtMemcpy(host.data(), len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); |
| 34 | if (err != ACL_SUCCESS) { | 34 | if (err != ACL_SUCCESS) { |
| 35 | - std::cerr << "[Error] aclrtMemcpy(output D2H) 失败, aclError=" << err << "\n"; | 35 | + std::cerr << "[Error] aclrtMemcpy(output D2H) failed, aclError=" << err << "\n"; |
| 36 | return ge::FAILED; | 36 | return ge::FAILED; |
| 37 | } | 37 | } |
| 38 | const float *out_data = host.data(); | 38 | const float *out_data = host.data(); |
| @@ -57,7 +57,7 @@ ge::Status CreateIoDataset(aclmdlDesc *model_desc, bool is_input, IoBuffers *out | |||
| 57 | const size_t io_num = is_input ? aclmdlGetNumInputs(model_desc) : aclmdlGetNumOutputs(model_desc); | 57 | const size_t io_num = is_input ? aclmdlGetNumInputs(model_desc) : aclmdlGetNumOutputs(model_desc); |
| 58 | out->dataset = aclmdlCreateDataset(); | 58 | out->dataset = aclmdlCreateDataset(); |
| 59 | if (out->dataset == nullptr) { | 59 | if (out->dataset == nullptr) { |
| 60 | - std::cerr << "[Error] aclmdlCreateDataset 失败\n"; | 60 | + std::cerr << "[Error] aclmdlCreateDataset failed\n"; |
| 61 | return ge::FAILED; | 61 | return ge::FAILED; |
| 62 | } | 62 | } |
| 63 | for (size_t i = 0; i < io_num; ++i) { | 63 | for (size_t i = 0; i < io_num; ++i) { |
| @@ -66,14 +66,14 @@ ge::Status CreateIoDataset(aclmdlDesc *model_desc, bool is_input, IoBuffers *out | |||
| 66 | void *dev = nullptr; | 66 | void *dev = nullptr; |
| 67 | aclError err = aclrtMalloc(&dev, buf_size, ACL_MEM_MALLOC_NORMAL_ONLY); | 67 | aclError err = aclrtMalloc(&dev, buf_size, ACL_MEM_MALLOC_NORMAL_ONLY); |
| 68 | if (err != ACL_SUCCESS) { | 68 | if (err != ACL_SUCCESS) { |
| 69 | - std::cerr << "[Error] aclrtMalloc 失败, aclError=" << err << "\n"; | 69 | + std::cerr << "[Error] aclrtMalloc failed, aclError=" << err << "\n"; |
| 70 | ReleaseDataset(out->dataset); | 70 | ReleaseDataset(out->dataset); |
| 71 | *out = IoBuffers{}; | 71 | *out = IoBuffers{}; |
| 72 | return ge::FAILED; | 72 | return ge::FAILED; |
| 73 | } | 73 | } |
| 74 | aclDataBuffer *data_buffer = aclCreateDataBuffer(dev, buf_size); | 74 | aclDataBuffer *data_buffer = aclCreateDataBuffer(dev, buf_size); |
| 75 | if (data_buffer == nullptr) { | 75 | if (data_buffer == nullptr) { |
| 76 | - std::cerr << "[Error] aclCreateDataBuffer 失败\n"; | 76 | + std::cerr << "[Error] aclCreateDataBuffer failed\n"; |
| 77 | (void)aclrtFree(dev); | 77 | (void)aclrtFree(dev); |
| 78 | ReleaseDataset(out->dataset); | 78 | ReleaseDataset(out->dataset); |
| 79 | *out = IoBuffers{}; | 79 | *out = IoBuffers{}; |
| @@ -81,7 +81,7 @@ ge::Status CreateIoDataset(aclmdlDesc *model_desc, bool is_input, IoBuffers *out | |||
| 81 | } | 81 | } |
| 82 | err = aclmdlAddDatasetBuffer(out->dataset, data_buffer); | 82 | err = aclmdlAddDatasetBuffer(out->dataset, data_buffer); |
| 83 | if (err != ACL_SUCCESS) { | 83 | if (err != ACL_SUCCESS) { |
| 84 | - std::cerr << "[Error] aclmdlAddDatasetBuffer 失败, aclError=" << err << "\n"; | 84 | + std::cerr << "[Error] aclmdlAddDatasetBuffer failed, aclError=" << err << "\n"; |
| 85 | (void)aclDestroyDataBuffer(data_buffer); | 85 | (void)aclDestroyDataBuffer(data_buffer); |
| 86 | (void)aclrtFree(dev); | 86 | (void)aclrtFree(dev); |
| 87 | ReleaseDataset(out->dataset); | 87 | ReleaseDataset(out->dataset); |
| @@ -96,19 +96,19 @@ ge::Status CreateIoDataset(aclmdlDesc *model_desc, bool is_input, IoBuffers *out | |||
| 96 | 96 | ||
| 97 | ge::Status CopyFloatInputs(const std::vector<std::vector<float>> &host_inputs, const IoBuffers &inputs) { | 97 | ge::Status CopyFloatInputs(const std::vector<std::vector<float>> &host_inputs, const IoBuffers &inputs) { |
| 98 | if (host_inputs.size() > inputs.device_ptrs.size()) { | 98 | if (host_inputs.size() > inputs.device_ptrs.size()) { |
| 99 | - std::cerr << "[Error] CopyFloatInputs: host 输入个数超过 device buffer 数\n"; | 99 | + std::cerr << "[Error] CopyFloatInputs: number of host inputs exceeds device buffer count\n"; |
| 100 | return ge::FAILED; | 100 | return ge::FAILED; |
| 101 | } | 101 | } |
| 102 | for (size_t i = 0; i < host_inputs.size(); ++i) { | 102 | for (size_t i = 0; i < host_inputs.size(); ++i) { |
| 103 | const auto &data = host_inputs[i]; | 103 | const auto &data = host_inputs[i]; |
| 104 | const size_t bytes = data.size() * sizeof(float); | 104 | const size_t bytes = data.size() * sizeof(float); |
| 105 | if (bytes > inputs.sizes[i]) { | 105 | if (bytes > inputs.sizes[i]) { |
| 106 | - std::cerr << "[Error] CopyFloatInputs: 输入 " << i << " 超过 buffer 大小\n"; | 106 | + std::cerr << "[Error] CopyFloatInputs: input " << i << " exceeds buffer size\n"; |
| 107 | return ge::FAILED; | 107 | return ge::FAILED; |
| 108 | } | 108 | } |
| 109 | aclError err = aclrtMemcpy(inputs.device_ptrs[i], inputs.sizes[i], data.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE); | 109 | aclError err = aclrtMemcpy(inputs.device_ptrs[i], inputs.sizes[i], data.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE); |
| 110 | if (err != ACL_SUCCESS) { | 110 | if (err != ACL_SUCCESS) { |
| 111 | - std::cerr << "[Error] aclrtMemcpy(input) 失败, aclError=" << err << "\n"; | 111 | + std::cerr << "[Error] aclrtMemcpy(input) failed, aclError=" << err << "\n"; |
| 112 | return ge::FAILED; | 112 | return ge::FAILED; |
| 113 | } | 113 | } |
| 114 | } | 114 | } |
| @@ -117,14 +117,14 @@ ge::Status CopyFloatInputs(const std::vector<std::vector<float>> &host_inputs, c | |||
| 117 | 117 | ||
| 118 | ge::Status PrintInferOutputDataset(aclmdlDesc *model_desc, aclmdlDataset *output_dataset) { | 118 | ge::Status PrintInferOutputDataset(aclmdlDesc *model_desc, aclmdlDataset *output_dataset) { |
| 119 | if (output_dataset == nullptr) { | 119 | if (output_dataset == nullptr) { |
| 120 | - std::cerr << "[Error] output dataset 为空\n"; | 120 | + std::cerr << "[Error] output dataset is empty\n"; |
| 121 | return ge::FAILED; | 121 | return ge::FAILED; |
| 122 | } | 122 | } |
| 123 | const size_t num_buffers = aclmdlGetDatasetNumBuffers(output_dataset); | 123 | const size_t num_buffers = aclmdlGetDatasetNumBuffers(output_dataset); |
| 124 | for (size_t i = 0; i < num_buffers; ++i) { | 124 | for (size_t i = 0; i < num_buffers; ++i) { |
| 125 | aclDataBuffer *data_buffer = aclmdlGetDatasetBuffer(output_dataset, i); | 125 | aclDataBuffer *data_buffer = aclmdlGetDatasetBuffer(output_dataset, i); |
| 126 | if (data_buffer == nullptr) { | 126 | if (data_buffer == nullptr) { |
| 127 | - std::cerr << "[Error] aclmdlGetDatasetBuffer 失败, index=" << i << "\n"; | 127 | + std::cerr << "[Error] aclmdlGetDatasetBuffer failed, index=" << i << "\n"; |
| 128 | return ge::FAILED; | 128 | return ge::FAILED; |
| 129 | } | 129 | } |
| 130 | const ge::Status st = PrintOneInferOutputBuffer(model_desc, i, data_buffer); | 130 | const ge::Status st = PrintOneInferOutputBuffer(model_desc, i, data_buffer); |
| @@ -28,10 +28,10 @@ int BuildAddModel(const std::string &soc_version) { | |||
| 28 | auto global_options = GlobalOptionsWithSoc(soc_version); | 28 | auto global_options = GlobalOptionsWithSoc(soc_version); |
| 29 | auto ret = ge::aclgrphBuildInitialize(global_options); | 29 | auto ret = ge::aclgrphBuildInitialize(global_options); |
| 30 | if (ret != ge::GRAPH_SUCCESS) { | 30 | if (ret != ge::GRAPH_SUCCESS) { |
| 31 | - std::cerr << "[Error] aclgrphBuildInitialize 失败, graphStatus=" << static_cast<int>(ret) << std::endl; | 31 | + std::cerr << "[Error] aclgrphBuildInitialize failed, graphStatus=" << static_cast<int>(ret) << std::endl; |
| 32 | return -1; | 32 | return -1; |
| 33 | } | 33 | } |
| 34 | - std::cout << "[Info] 系统初始化成功\n"; | 34 | + std::cout << "[Info] System initialized successfully\n"; |
| 35 | 35 | ||
| 36 | auto graph = MakeOfflineAddGraph(); | 36 | auto graph = MakeOfflineAddGraph(); |
| 37 | ge::ModelBufferData model; | 37 | ge::ModelBufferData model; |
| @@ -39,22 +39,23 @@ int BuildAddModel(const std::string &soc_version) { | |||
| 39 | build_options.emplace(ge::AscendString("input_format"), ge::AscendString("ND")); | 39 | build_options.emplace(ge::AscendString("input_format"), ge::AscendString("ND")); |
| 40 | ret = ge::aclgrphBuildModel(*graph, build_options, model); | 40 | ret = ge::aclgrphBuildModel(*graph, build_options, model); |
| 41 | if (ret != ge::GRAPH_SUCCESS) { | 41 | if (ret != ge::GRAPH_SUCCESS) { |
| 42 | - std::cerr << "[Error] aclgrphBuildModel 失败, graphStatus=" << static_cast<int>(ret) << std::endl; | 42 | + std::cerr << "[Error] aclgrphBuildModel failed, graphStatus=" << static_cast<int>(ret) << std::endl; |
| 43 | ge::aclgrphBuildFinalize(); | 43 | ge::aclgrphBuildFinalize(); |
| 44 | return -1; | 44 | return -1; |
| 45 | } | 45 | } |
| 46 | - std::cout << "[Info] 模型构建成功,模型大小: " << model.length << " bytes\n"; | 46 | + std::cout << "[Info] Model built successfully, model size: " << model.length << " bytes\n"; |
| 47 | 47 | ||
| 48 | const char *const output_file = "add_sample"; | 48 | const char *const output_file = "add_sample"; |
| 49 | ret = ge::aclgrphSaveModel(output_file, model); | 49 | ret = ge::aclgrphSaveModel(output_file, model); |
| 50 | if (ret != ge::GRAPH_SUCCESS) { | 50 | if (ret != ge::GRAPH_SUCCESS) { |
| 51 | - std::cerr << "[Error] aclgrphSaveModel 失败, graphStatus=" << static_cast<int>(ret) << std::endl; | 51 | + std::cerr << "[Error] aclgrphSaveModel failed, graphStatus=" << static_cast<int>(ret) << std::endl; |
| 52 | ge::aclgrphBuildFinalize(); | 52 | ge::aclgrphBuildFinalize(); |
| 53 | return -1; | 53 | return -1; |
| 54 | } | 54 | } |
| 55 | - std::cout << "[Info] 模型保存成功," << output_file << ".om 已生成在当前目录。\n"; | 55 | + std::cout << "[Info] Model saved successfully, " << output_file |
| 56 | + << ".om has been generated in the current directory.\n"; | ||
| 56 | 57 | ||
| 57 | ge::aclgrphBuildFinalize(); | 58 | ge::aclgrphBuildFinalize(); |
| 58 | - std::cout << "[Info] 系统释放成功\n"; | 59 | + std::cout << "[Info] System resources released successfully\n"; |
| 59 | return 0; | 60 | return 0; |
| 60 | } | 61 | } |
| @@ -24,59 +24,59 @@ int ExecuteSingleLoadedModel(aclmdlDesc *model_desc, uint32_t model_id) { | |||
| 24 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); | 24 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); |
| 25 | return -1; | 25 | return -1; |
| 26 | } | 26 | } |
| 27 | - std::cout << "[Info] 执行模型推理\n"; | 27 | + std::cout << "[Info] Executing model inference\n"; |
| 28 | if (CopyFloatInputs(SampleInputs(), input_io) != ge::SUCCESS) { | 28 | if (CopyFloatInputs(SampleInputs(), input_io) != ge::SUCCESS) { |
| 29 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); | 29 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); |
| 30 | return -1; | 30 | return -1; |
| 31 | } | 31 | } |
| 32 | const aclError err = aclmdlExecute(model_id, input_io.dataset, output_io.dataset); | 32 | const aclError err = aclmdlExecute(model_id, input_io.dataset, output_io.dataset); |
| 33 | if (err != ACL_SUCCESS) { | 33 | if (err != ACL_SUCCESS) { |
| 34 | - std::cerr << "[Error] aclmdlExecute 失败, aclError=" << err << std::endl; | 34 | + std::cerr << "[Error] aclmdlExecute failed, aclError=" << err << std::endl; |
| 35 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); | 35 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); |
| 36 | return -1; | 36 | return -1; |
| 37 | } | 37 | } |
| 38 | - std::cout << "[Info] 模型推理结果:\n"; | 38 | + std::cout << "[Info] Model inference result:\n"; |
| 39 | if (PrintModelOutputs(model_desc, output_io) != ge::SUCCESS) { | 39 | if (PrintModelOutputs(model_desc, output_io) != ge::SUCCESS) { |
| 40 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); | 40 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); |
| 41 | return -1; | 41 | return -1; |
| 42 | } | 42 | } |
| 43 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); | 43 | TeardownAclSingleModelInfer(&input_io, &output_io, model_desc, model_id); |
| 44 | - std::cout << "[Info] 所有资源释放成功\n"; | 44 | + std::cout << "[Info] All resources released successfully\n"; |
| 45 | return 0; | 45 | return 0; |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | } // namespace | 48 | } // namespace |
| 49 | 49 | ||
| 50 | int RunSingleModelInfer() { | 50 | int RunSingleModelInfer() { |
| 51 | - std::cout << "[Info] 初始化运行环境及数据\n"; | 51 | + std::cout << "[Info] Initializing runtime environment and data\n"; |
| 52 | aclError err = aclInit(nullptr); | 52 | aclError err = aclInit(nullptr); |
| 53 | if (err != ACL_SUCCESS) { | 53 | if (err != ACL_SUCCESS) { |
| 54 | - std::cerr << "[Error] aclInit 失败, aclError=" << err << std::endl; | 54 | + std::cerr << "[Error] aclInit failed, aclError=" << err << std::endl; |
| 55 | return -1; | 55 | return -1; |
| 56 | } | 56 | } |
| 57 | err = aclrtSetDevice(kDeviceId); | 57 | err = aclrtSetDevice(kDeviceId); |
| 58 | if (err != ACL_SUCCESS) { | 58 | if (err != ACL_SUCCESS) { |
| 59 | - std::cerr << "[Error] aclrtSetDevice 失败, aclError=" << err << std::endl; | 59 | + std::cerr << "[Error] aclrtSetDevice failed, aclError=" << err << std::endl; |
| 60 | (void)aclFinalize(); | 60 | (void)aclFinalize(); |
| 61 | return -1; | 61 | return -1; |
| 62 | } | 62 | } |
| 63 | uint32_t model_id = 0; | 63 | uint32_t model_id = 0; |
| 64 | err = aclmdlLoadFromFile("add_sample.om", &model_id); | 64 | err = aclmdlLoadFromFile("add_sample.om", &model_id); |
| 65 | if (err != ACL_SUCCESS) { | 65 | if (err != ACL_SUCCESS) { |
| 66 | - std::cerr << "[Error] aclmdlLoadFromFile 失败, aclError=" << err << std::endl; | 66 | + std::cerr << "[Error] aclmdlLoadFromFile failed, aclError=" << err << std::endl; |
| 67 | (void)aclrtResetDevice(kDeviceId); | 67 | (void)aclrtResetDevice(kDeviceId); |
| 68 | (void)aclFinalize(); | 68 | (void)aclFinalize(); |
| 69 | return -1; | 69 | return -1; |
| 70 | } | 70 | } |
| 71 | aclmdlDesc *model_desc = aclmdlCreateDesc(); | 71 | aclmdlDesc *model_desc = aclmdlCreateDesc(); |
| 72 | if (model_desc == nullptr) { | 72 | if (model_desc == nullptr) { |
| 73 | - std::cerr << "[Error] aclmdlCreateDesc 失败\n"; | 73 | + std::cerr << "[Error] aclmdlCreateDesc failed\n"; |
| 74 | TeardownAclSingleModelInfer(nullptr, nullptr, nullptr, model_id); | 74 | TeardownAclSingleModelInfer(nullptr, nullptr, nullptr, model_id); |
| 75 | return -1; | 75 | return -1; |
| 76 | } | 76 | } |
| 77 | err = aclmdlGetDesc(model_desc, model_id); | 77 | err = aclmdlGetDesc(model_desc, model_id); |
| 78 | if (err != ACL_SUCCESS) { | 78 | if (err != ACL_SUCCESS) { |
| 79 | - std::cerr << "[Error] aclmdlGetDesc 失败, aclError=" << err << std::endl; | 79 | + std::cerr << "[Error] aclmdlGetDesc failed, aclError=" << err << std::endl; |
| 80 | TeardownAclSingleModelInfer(nullptr, nullptr, model_desc, model_id); | 80 | TeardownAclSingleModelInfer(nullptr, nullptr, model_desc, model_id); |
| 81 | return -1; | 81 | return -1; |
| 82 | } | 82 | } |
| @@ -22,7 +22,9 @@ from ge.offline_compile import ( | |||
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | def parse_bundle_args(): | 24 | def parse_bundle_args(): |
| 25 | - parser = argparse.ArgumentParser(description="Build an offline bundle_model OM model from GE Python Graphs.") | 25 | + parser = argparse.ArgumentParser( |
| 26 | + description="Build an offline bundle_model OM model from GE Python Graphs." | ||
| 27 | + ) | ||
| 26 | parser.add_argument( | 28 | parser.add_argument( |
| 27 | "--soc-version", | 29 | "--soc-version", |
| 28 | type=str, | 30 | type=str, |
| @@ -38,7 +40,7 @@ def main(): | |||
| 38 | if args.soc_version: | 40 | if args.soc_version: |
| 39 | global_options["ge.socVersion"] = args.soc_version | 41 | global_options["ge.socVersion"] = args.soc_version |
| 40 | build_initialize(global_options) | 42 | build_initialize(global_options) |
| 41 | - print("[Info] 系统初始化成功") | 43 | + print("[Info] System initialized successfully") |
| 42 | 44 | ||
| 43 | graph_with_options = [ | 45 | graph_with_options = [ |
| 44 | GraphWithOptions(build_add_graph(), {"input_format": "ND"}), | 46 | GraphWithOptions(build_add_graph(), {"input_format": "ND"}), |
| @@ -46,14 +48,16 @@ def main(): | |||
| 46 | ] | 48 | ] |
| 47 | try: | 49 | try: |
| 48 | model = bundle_build_model(graph_with_options) | 50 | model = bundle_build_model(graph_with_options) |
| 49 | - print(f"[Info] 模型构建成功,模型大小: {model.length} bytes") | 51 | + print(f"[Info] Model built successfully, model size: {model.length} bytes") |
| 50 | 52 | ||
| 51 | file_name = "bundle_sample" | 53 | file_name = "bundle_sample" |
| 52 | bundle_save_model(file_name, model) | 54 | bundle_save_model(file_name, model) |
| 53 | - print(f"[Info] 模型保存成功,{file_name}.om 模型文件已生成在当前目录。") | 55 | + print( |
| 56 | + f"[Info] Model saved successfully, {file_name}.om model file has been generated in the current directory." | ||
| 57 | + ) | ||
| 54 | finally: | 58 | finally: |
| 55 | build_finalize() | 59 | build_finalize() |
| 56 | - print("[Info] 系统释放成功") | 60 | + print("[Info] System resources released successfully") |
| 57 | 61 | ||
| 58 | 62 | ||
| 59 | if __name__ == "__main__": | 63 | if __name__ == "__main__": |
| @@ -30,7 +30,7 @@ class AclBundleModelRunner: | |||
| 30 | 30 | ||
| 31 | @staticmethod | 31 | @staticmethod |
| 32 | def run_model(model_id: int, inputs): | 32 | def run_model(model_id: int, inputs): |
| 33 | - print(f"[Info] 执行子模型推理,model_id: {model_id}") | 33 | + print(f"[Info] Executing sub-model inference, model_id: {model_id}") |
| 34 | model_desc = acl.mdl.create_desc() | 34 | model_desc = acl.mdl.create_desc() |
| 35 | input_dataset = None | 35 | input_dataset = None |
| 36 | output_dataset = None | 36 | output_dataset = None |
| @@ -52,7 +52,7 @@ class AclBundleModelRunner: | |||
| 52 | check_ret("acl.mdl.destroy_desc", acl.mdl.destroy_desc(model_desc)) | 52 | check_ret("acl.mdl.destroy_desc", acl.mdl.destroy_desc(model_desc)) |
| 53 | 53 | ||
| 54 | def init(self) -> None: | 54 | def init(self) -> None: |
| 55 | - print("[Info] 初始化运行环境及数据") | 55 | + print("[Info] Initializing runtime environment and data") |
| 56 | # 初始化 acl | 56 | # 初始化 acl |
| 57 | check_ret("acl.init", acl.init()) | 57 | check_ret("acl.init", acl.init()) |
| 58 | # 指定运行的 Device | 58 | # 指定运行的 Device |
| @@ -68,10 +68,12 @@ class AclBundleModelRunner: | |||
| 68 | model_id, ret = acl.mdl.bundle_get_model_id(self.bundle_id, index) | 68 | model_id, ret = acl.mdl.bundle_get_model_id(self.bundle_id, index) |
| 69 | check_ret("acl.mdl.bundle_get_model_id", ret) | 69 | check_ret("acl.mdl.bundle_get_model_id", ret) |
| 70 | self.model_ids.append(model_id) | 70 | self.model_ids.append(model_id) |
| 71 | - print(f"[Info] Bundle 模型加载成功,子模型个数: {len(self.model_ids)}") | 71 | + print( |
| 72 | + f"[Info] Bundle model loaded successfully, number of sub-models: {len(self.model_ids)}" | ||
| 73 | + ) | ||
| 72 | 74 | ||
| 73 | def release(self) -> None: | 75 | def release(self) -> None: |
| 74 | - print("[Info] 释放所有资源") | 76 | + print("[Info] Releasing all resources") |
| 75 | if self.bundle_id is not None: | 77 | if self.bundle_id is not None: |
| 76 | check_ret("acl.mdl.bundle_unload", acl.mdl.bundle_unload(self.bundle_id)) | 78 | check_ret("acl.mdl.bundle_unload", acl.mdl.bundle_unload(self.bundle_id)) |
| 77 | acl.rt.reset_device(self.device_id) | 79 | acl.rt.reset_device(self.device_id) |
| @@ -83,12 +85,12 @@ def main(): | |||
| 83 | try: | 85 | try: |
| 84 | runner.init() | 86 | runner.init() |
| 85 | add_outputs = runner.run_model(runner.model_ids[0], create_sample_inputs()) | 87 | add_outputs = runner.run_model(runner.model_ids[0], create_sample_inputs()) |
| 86 | - print("[Info] Add 子模型推理结果:") | 88 | + print("[Info] Add sub-model inference result:") |
| 87 | for i, output in enumerate(add_outputs, start=1): | 89 | for i, output in enumerate(add_outputs, start=1): |
| 88 | print(f"Output[{i}]: \n{output}") | 90 | print(f"Output[{i}]: \n{output}") |
| 89 | 91 | ||
| 90 | mul_outputs = runner.run_model(runner.model_ids[1], create_sample_inputs()) | 92 | mul_outputs = runner.run_model(runner.model_ids[1], create_sample_inputs()) |
| 91 | - print("[Info] Mul 子模型推理结果:") | 93 | + print("[Info] Mul sub-model inference result:") |
| 92 | for i, output in enumerate(mul_outputs, start=1): | 94 | for i, output in enumerate(mul_outputs, start=1): |
| 93 | print(f"Output[{i}]: \n{output}") | 95 | print(f"Output[{i}]: \n{output}") |
| 94 | finally: | 96 | finally: |
| @@ -16,7 +16,9 @@ from ge.offline_compile import build_finalize, build_initialize, build_model, sa | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | def parse_args(): | 18 | def parse_args(): |
| 19 | - parser = argparse.ArgumentParser(description="Build an offline OM model from a GE Python Graph.") | 19 | + parser = argparse.ArgumentParser( |
| 20 | + description="Build an offline OM model from a GE Python Graph." | ||
| 21 | + ) | ||
| 20 | parser.add_argument( | 22 | parser.add_argument( |
| 21 | "--soc-version", | 23 | "--soc-version", |
| 22 | type=str, | 24 | type=str, |
| @@ -32,20 +34,22 @@ def main(): | |||
| 32 | if args.soc_version: | 34 | if args.soc_version: |
| 33 | global_options["ge.socVersion"] = args.soc_version | 35 | global_options["ge.socVersion"] = args.soc_version |
| 34 | build_initialize(global_options) | 36 | build_initialize(global_options) |
| 35 | - print("[Info] 系统初始化成功") | 37 | + print("[Info] System initialized successfully") |
| 36 | 38 | ||
| 37 | build_options = {"input_format": "ND"} | 39 | build_options = {"input_format": "ND"} |
| 38 | graph = build_add_graph() | 40 | graph = build_add_graph() |
| 39 | try: | 41 | try: |
| 40 | model = build_model(graph, build_options) | 42 | model = build_model(graph, build_options) |
| 41 | - print(f"[Info] 模型构建成功,模型大小: {model.length} bytes") | 43 | + print(f"[Info] Model built successfully, model size: {model.length} bytes") |
| 42 | 44 | ||
| 43 | file_name = "add_sample" | 45 | file_name = "add_sample" |
| 44 | save_model(file_name, model) | 46 | save_model(file_name, model) |
| 45 | - print(f"[Info] 模型保存成功,{file_name}.om 模型文件已生成在当前目录。") | 47 | + print( |
| 48 | + f"[Info] Model saved successfully, {file_name}.om model file has been generated in the current directory." | ||
| 49 | + ) | ||
| 46 | finally: | 50 | finally: |
| 47 | build_finalize() | 51 | build_finalize() |
| 48 | - print("[Info] 系统释放成功") | 52 | + print("[Info] System resources released successfully") |
| 49 | 53 | ||
| 50 | 54 | ||
| 51 | if __name__ == "__main__": | 55 | if __name__ == "__main__": |
| @@ -33,7 +33,7 @@ class AclModelRunner: | |||
| 33 | self.output_data = [] | 33 | self.output_data = [] |
| 34 | 34 | ||
| 35 | def init(self) -> None: | 35 | def init(self) -> None: |
| 36 | - print("[Info] 初始化运行环境及数据") | 36 | + print("[Info] Initializing runtime environment and data") |
| 37 | # 初始化 acl | 37 | # 初始化 acl |
| 38 | check_ret("acl.init", acl.init()) | 38 | check_ret("acl.init", acl.init()) |
| 39 | # 指定运行的 Device | 39 | # 指定运行的 Device |
| @@ -45,11 +45,15 @@ class AclModelRunner: | |||
| 45 | self.model_desc = acl.mdl.create_desc() | 45 | self.model_desc = acl.mdl.create_desc() |
| 46 | check_ret("acl.mdl.get_desc", acl.mdl.get_desc(self.model_desc, self.model_id)) | 46 | check_ret("acl.mdl.get_desc", acl.mdl.get_desc(self.model_desc, self.model_id)) |
| 47 | 47 | ||
| 48 | - self.input_dataset, self.input_data = prepare_acl_mdl_dataset(self.model_desc, "input") | 48 | + self.input_dataset, self.input_data = prepare_acl_mdl_dataset( |
| 49 | - self.output_dataset, self.output_data = prepare_acl_mdl_dataset(self.model_desc, "output") | 49 | + self.model_desc, "input" |
| 50 | + ) | ||
| 51 | + self.output_dataset, self.output_data = prepare_acl_mdl_dataset( | ||
| 52 | + self.model_desc, "output" | ||
| 53 | + ) | ||
| 50 | 54 | ||
| 51 | def forward(self, inputs): | 55 | def forward(self, inputs): |
| 52 | - print("[Info] 执行模型推理") | 56 | + print("[Info] Executing model inference") |
| 53 | copy_inputs_to_acl_dataset(self.input_data, inputs) | 57 | copy_inputs_to_acl_dataset(self.input_data, inputs) |
| 54 | check_ret( | 58 | check_ret( |
| 55 | "acl.mdl.execute", | 59 | "acl.mdl.execute", |
| @@ -58,7 +62,7 @@ class AclModelRunner: | |||
| 58 | return collect_acl_model_outputs(self.model_desc, self.output_data) | 62 | return collect_acl_model_outputs(self.model_desc, self.output_data) |
| 59 | 63 | ||
| 60 | def release(self) -> None: | 64 | def release(self) -> None: |
| 61 | - print("[Info] 释放所有资源") | 65 | + print("[Info] Releasing all resources") |
| 62 | # 释放模型推理的输入、输出资源 | 66 | # 释放模型推理的输入、输出资源 |
| 63 | release_acl_mdl_dataset(self.input_dataset) | 67 | release_acl_mdl_dataset(self.input_dataset) |
| 64 | release_acl_mdl_dataset(self.output_dataset) | 68 | release_acl_mdl_dataset(self.output_dataset) |
| @@ -77,7 +81,7 @@ def main(): | |||
| 77 | try: | 81 | try: |
| 78 | runner.init() | 82 | runner.init() |
| 79 | outputs = runner.forward(create_sample_inputs()) | 83 | outputs = runner.forward(create_sample_inputs()) |
| 80 | - print("[Info] 模型推理结果:") | 84 | + print("[Info] Model inference result:") |
| 81 | for i, output in enumerate(outputs, start=1): | 85 | for i, output in enumerate(outputs, start=1): |
| 82 | print(f"Output[{i}]: \n{output}") | 86 | print(f"Output[{i}]: \n{output}") |
| 83 | finally: | 87 | finally: |