Sample Usage Guide【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge1. Function DescriptionThis sample uses BatchNorm operators optional inputs for graph construction, aiming to help graph construction developers quickly understand the definition of optional inputs and how to use this type of operator for graph construction.2. Directory Structurecpp/ ├── src/ | └── CMakeLists.txt // CMake build file | └── es_showcase.h // Header file | └── make_batchnorm_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 following the installation guide Environment PreparationSet environment variables (assuming the package is installed in /usr/local/Ascend/)source /usr/local/Ascend/cann/set_env.sh3.2 Build and Execute1.2.1 Generate ES Interface and Build Graph for DUMPSimply run the following command to clean up, generate interface, build graph and DUMP graph:bash run_sample.shThe current behavior of run_sample.sh is: automatically clean up old build, build sample and default execute sample dump. When you see the following message, it means successful execution:[Success] sample executed successfully, pbtxt dump has been generated in current directory. This file starts with ge_onnx_ and can be opened in netron for display1.2.2 Output File DescriptionAfter successful execution, the following file will be generated in current directory:ge_onnx_*.pbtxt - Protobuf text format of graph structure, can be viewed with netron1.2.3 Build Graph and ExecuteBesides basic graph building and dump functionality, esb_sample supports building graph and actually executing computation.bash run_sample.sh -t sample_and_runThis command will:Automatically generate ES interfaceCompile 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 have been generated in current directoryYou can check computation results through data file3.3 Log PrintingIf log printing is needed during executable program execution to assist debugging, set the following environment variables before bash run_sample.sh to print logs to screen:export ASCEND_SLOG_PRINT_TO_STDOUT1 #Print logs to screen export ASCEND_GLOBAL_LOG_LEVEL0 #Log level set to debug level1.4 Graph Compilation DUMPIf DUMP graph is needed during executable program execution to assist graph compilation debugging, set the following environment variables before bash run_sample.sh -t sample_and_run to DUMP graph to execution path:export DUMP_GE_GRAPH24. Core Concept Introduction4.1 Graph Construction StepsCreate graph builder (to provide context, workspace and build-related methods needed for graph construction)Add starting nodes (starting 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 using existing nodes as inputs)Set graph output (explicitly specify graph output nodes as endpoints of computation results)4.2 Concept DescriptionOptional input refers to certain inputs of an operator that are non-mandatory inputs.For example, BatchNorm operator prototype is shown below, ES graph construction generated API isBatchNorm(), supporting use at C layerREG_OP(BatchNorm) .INPUT(x, TensorType({DT_FLOAT16,DT_FLOAT})) .INPUT(scale, TensorType({DT_FLOAT})) .INPUT(offset, TensorType({DT_FLOAT})) .OPTIONAL_INPUT(mean, TensorType({DT_FLOAT})) .OPTIONAL_INPUT(variance, TensorType({DT_FLOAT})) .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT})) .OUTPUT(batch_mean, TensorType({DT_FLOAT})) .OUTPUT(batch_variance, TensorType({DT_FLOAT})) .OUTPUT(reserve_space_1, TensorType({DT_FLOAT})) .OUTPUT(reserve_space_2, TensorType({DT_FLOAT})) .OUTPUT(reserve_space_3, TensorType({DT_FLOAT})) .ATTR(epsilon, Float, 0.0001f) .ATTR(data_format, String, NHWC) .ATTR(is_training, Bool, true) .ATTR(exponential_avg_factor, Float, 1.0) .OP_END_FACTORY_REG(BatchNorm)Its corresponding function prototype is:Function name: BatchNormParameters: Total 9, sequentially x, scale, offset, mean (optional input), variance (optional input), epsilon, data_format, is_training, exponential_avg_factorReturn values: Outputs y, batch_mean, batch_variance, reserve_space_1, reserve_space_2, reserve_space_3In C API:EsBatchNormOutput EsBatchNorm(EsCTensorHolder *x, EsCTensorHolder *scale, EsCTensorHolder *offset, EsCTensorHolder *mean, EsCTensorHolder *variance, float epsilon, const char *data_format, bool is_training, float exponential_avg_factor); typedef struct { EsCTensorHolder *y; EsCTensorHolder *batch_mean; EsCTensorHolder *batch_variance; EsCTensorHolder *reserve_space_1; EsCTensorHolder *reserve_space_2; EsCTensorHolder *reserve_space_3; } EsBatchNormOutput;In C API:BatchNormOutput BatchNorm(const EsTensorLike x, const EsTensorLike scale, const EsTensorLike offset, const EsTensorLike meannullptr, const EsTensorLike variancenullptr, float epsilon0.000100, const char *data_formatNHWC, bool is_trainingtrue, float exponential_avg_factor1.000000); struct BatchNormOutput { EsTensorHolder y; EsTensorHolder batch_mean; EsTensorHolder batch_variance; EsTensorHolder reserve_space_1; EsTensorHolder reserve_space_2; EsTensorHolder reserve_space_3; };Note: Use TensorLike type to express input, to support cases where actual parameters can directly pass numeric values【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考