十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

CANN/GE控制边缘Python示例指南

CANN/GE控制边缘Python示例指南 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 Structurepython/ ├── src/ | └── make_control_edge_graph.py // sample file ├── CMakeLists.txt // Compilation script ├── README.md // README file ├── run_sample.sh // Execution script3. 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 ExecutionNote: Compared with C/C graph construction, Python graph construction requires additional LD_LIBRARY_PATH and PYTHONPATH (refer to sample configuration)bash run_sample.sh -t sample_and_run_pythonThis command will:Automatically generate ES interfacesCompile sample programGenerate dump graph and run the graphAfter successful execution you will see:[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 netron3.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 -t sample_and_run_python 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_python 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 providesadd_control_dependency()method, supports usage in PythonCan add control dependencies from multiple source nodes for one target node5. Control Dependency Relationship Example5.1. OverviewControl dependencies are used to specify execution order of nodes in computation graphs, even if there are no data dependencies between these nodes. This document demonstrates how to express control dependency relationships at the Python level.Python API ExampleMethod 1: Directly Call add_control_dependency()from ge.es import GraphBuilder # 1. Create graph builder builder GraphBuilder(control_dep_example) # 2. Create nodes tensor_a builder.create_scalar_float(1.0) tensor_b builder.create_scalar_float(2.0) # 3. Create dependency target node (assuming generated Add operation exists) from ge.es.all import Add tensor_c Add(tensor_a, tensor_b) # 4. Add control dependency: tensor_c depends on tensor_a and tensor_b builder.add_control_dependency( dst_tensortensor_c, src_tensors[tensor_a, tensor_b] ) # 5. Set output and build builder.set_graph_output(tensor_c, 0) graph builder.build_and_reset()Method 2: Use control_dependency_scope Context Manager (Recommended)from ge.es import GraphBuilder from ge.es.graph_builder import control_dependency_scope from ge.es.all import Add # 1. Create graph builder builder GraphBuilder(control_dep_scope_example) # 2. Create dependency source nodes tensor_a builder.create_scalar_float(1.0) tensor_b builder.create_scalar_float(2.0) # 3. Use scope: all nodes created within scope automatically depend on tensor_a and tensor_b with control_dependency_scope([tensor_a, tensor_b]): # Nodes created in this scope will automatically add control dependencies tensor_c builder.create_scalar_float(3.0) tensor_d Add(tensor_a, tensor_c) # Producer nodes of tensor_c and tensor_d both automatically depend on tensor_a and tensor_b # 4. Set output and build builder.set_graph_output(tensor_d, 0) graph builder.build_and_reset()Nested Scope Examplefrom ge.es import GraphBuilder from ge.es.graph_builder import control_dependency_scope from ge.es.all import Add def build_graph_with_nested_scopes(): Demonstrate nested control dependency scopes builder GraphBuilder(nested_scopes) # Global initialization node global_init builder.create_scalar_float(0.0) # First layer scope with control_dependency_scope([global_init]): # Module A initialization module_a_init builder.create_scalar_float(1.0) # Second layer nested scope with control_dependency_scope([module_a_init]): # Module A computation (depends on global_init and module_a_init) module_a_output Add(module_a_init, global_init) # Return to first layer scope # Module B initialization (only depends on global_init) module_b_init builder.create_scalar_float(2.0) # Final output (not in any scope, no additional control dependencies) final_output Add(module_a_output, module_b_init) builder.set_graph_output(final_output, 0) return builder.build_and_reset() # Usage graph build_graph_with_nested_scopes()【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表