CANN/GE控制边示例指南
发布时间:2026/9/10 4:49:52 作者:尧图编辑部 阅读量:1,286

Sample Usage Guide【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge1. Functional DescriptionThis sample uses control edges for graph construction, aiming to help graph construction developers quickly understand the concept of control edges and use control edges for graph construction2. Directory Structurecpp/ ├── src/ | └── CMakeLists.txt // CMake build file | └── es_showcase.h // Header file | └── make_control_edge_graph.cpp // sample file ├── CMakeLists.txt // CMake build file ├── main.cpp // Program main entry ├── README.md // README file ├── run_sample.sh // Execution script ├── utils.h // Utility file3. Usage3.1. Prepare CANN PackageCorrectly installtoolkitandopspackages through installation guide Environment PreparationSet environment variables (assuming packages are installed in /usr/local/Ascend/)source /usr/local/Ascend/cann/set_env.sh3.2. Compilation and ExecutionSimply run the following command to complete cleanup, interface generation, graph construction and DUMP graph:bash run_sample.shCurrent run_sample.sh behavior: first automatically clean up old build, build sample and execute sample dump by default. When you see the following information, it means successful execution:[Success] sample executed successfully, pbtxt dump generated in current directory. This file starts with ge_onnx_ and can be opened in netron for displayOutput File DescriptionAfter successful execution, the following files will be generated in the current directory:ge_onnx_*.pbtxt- Graph structure protobuf text format, can be viewed with netronBuild Graph and ExecuteBesides basic graph construction and dump functionality, esb_sample also supports building graphs and executing actual computation.bash run_sample.sh -t sample_and_runThis command will:Automatically generate ES interfacesCompile sample programGenerate dump graph, run graph and output computation resultsAfter successful execution you will see:[Success] sample_and_run executed successfully, pbtxt and data output dump generated in current directoryYou can view computation results through data files3.3. Log PrintingIf you need log printing to assist debugging during executable program execution, you can set the following environment variables before bash run_sample.sh to print logs to screenexport ASCEND_SLOG_PRINT_TO_STDOUT1 #Print logs to screen export ASCEND_GLOBAL_LOG_LEVEL0 #Log level is debug level3.4. DUMP Graph During Graph Compilation ProcessDuring executable program execution, if you need to DUMP graph to assist debugging graph compilation process, you can set the following environment variable before bash run_sample.sh -t sample_and_run to DUMP graph to execution pathexport DUMP_GE_GRAPH24. Core Concept Introduction4.1. Graph Construction StepsCreate graph builder (used to provide context, workspace and construction-related methods needed for graph construction)Add start nodes (start nodes refer to nodes without input dependencies, usually including graph inputs (like Data nodes) and weight constants (like Const nodes))Add intermediate nodes (intermediate nodes are computation nodes with input dependencies, usually generated by user graph construction logic, and connected through existing nodes as inputs)Set graph output (explicitly specify graph output nodes as endpoints of computation results)4.2. Control EdgeConcept Description:Control edges are used to specify execution order of nodes in computation graphs, even if there are no data dependencies between these nodes. Control edges do not transmit data, only transmit control signals, ensuring source nodes execute before target nodes.Graph Construction API Features:ES API providesAddControlEdge()method, supports usage in C and CCan add control dependencies from multiple source nodes for one target node5. Control Dependency Relationship ExampleThis document demonstrates how to express control dependency relationships at C and C levels.5.1. Scenario DescriptionAssume we have the following computation requirements:Node1: tensor_a Const(1.0) Node2: tensor_b Const(2.0) Node3: tensor_c Add(tensor_a, tensor_b) Control dependency: tensor_c must execute after tensor_a and tensor_bAlthough theAddoperation already has data dependencies (depending on tensor_a and tensor_b), here we use it to demonstrate the expression of control dependencies.5.2. C API Example#include esb_funcs.h // 1. Create graph builder EsCGraphBuilderPtr builder EsCreateGraphBuilder(control_dep_example); // 2. Create nodes EsCTensorHolderPtr tensor_a EsCreateScalarFloat(builder, 1.0f); EsCTensorHolderPtr tensor_b EsCreateScalarFloat(builder, 2.0f); // 3. Create dependency target node (assuming EsAdd function exists) EsCTensorHolderPtr tensor_c EsAdd(tensor_a, tensor_b); // 4. Add control dependency: tensor_c depends on tensor_a and tensor_b EsCTensorHolderPtr src_tensors[] {tensor_a, tensor_b}; uint32_t ret EsAddControlEdge(tensor_c, src_tensors, 2); if (ret ! 0) { printf(Failed to add control dependency\n); // Error handling } // 5. Set output and build graph EsSetGraphOutput(tensor_c, 0); EsCGraphPtr graph EsBuildGraph(builder); // 6. Cleanup EsDestroyGraphBuilder(builder);5.3. C API Example# include es_graph_builder.h using namespace ge::es; void build_graph_with_control_dep() { // 1. Create graph builder EsGraphBuilder builder(control_dep_example); // 2. Create nodes auto tensor_a builder.CreateScalar(1.0f); auto tensor_b builder.CreateScalar(2.0f); // 3. Create dependency target node Add control dependency auto tensor_c Add(tensor_a, tensor_b) (void) tensor_c.AddControlEdge({tensor_a, tensor_b}); // 5. Set output and build builder.SetOutput(tensor_c, 0); auto graph_ptr builder.build_and_reset(); }【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考