news 2026/9/10 19:22:24

CANN/GE控制算子示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CANN/GE控制算子示例

Sample Usage Guide

【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge

1. Functional Description

This sample uses control operators for graph construction, aiming to help graph construction developers quickly get started with control operator definition and usage

2. Directory Structure

cpp/ ├── src/ | └── CMakeLists.txt // CMake build file | └── es_showcase.h // Header file | └── make_if_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 file

3. Usage

3.1. Prepare CANN Package

  • Correctly installtoolkitandopspackages through installation guide Environment Preparation
  • Set environment variables (assuming packages are installed in /usr/local/Ascend/)
source /usr/local/Ascend/cann/set_env.sh

3.2. Compilation and Execution

Simply run the following command to complete cleanup, interface generation, graph construction and DUMP graph

bash run_sample.sh

Current 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 display
Output File Description

After successful execution, the following files will be generated in the current directory:

  • ge_onnx_*.pbtxt- Graph structure protobuf text format, can be viewed with netron
Build Graph and Execute

Besides basic graph construction and dump functionality, esb_sample also supports building graphs and executing actual computation.

bash run_sample.sh -t sample_and_run

This command will:

  1. Automatically generate ES interfaces
  2. Compile sample program
  3. Generate dump graph, run graph and output computation results

After successful execution you will see:

[Success] sample_and_run executed successfully, pbtxt and data output dump generated in current directory

You can view computation results through data files

3.3. Log Printing

If 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 screen

export ASCEND_SLOG_PRINT_TO_STDOUT=1 #Print logs to screen export ASCEND_GLOBAL_LOG_LEVEL=0 #Log level is debug level

3.4. DUMP Graph During Graph Compilation Process

During 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 path

export DUMP_GE_GRAPH=2

4. Core Concept Introduction

4.1. Graph Construction Steps

  • Create 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 Operator (Control Op)

Concept Description:Control operators are a special class of operators used to implement control flow logic, such as conditional branches (If), etc.

Graph Construction API Features:

  • Control operators usually need to construct one or more subgraphs first

For example, the prototype of the If operator is shown below, the ES graph construction generated API isIf(), supports usage in C and C++

REG_OP(If) .INPUT(cond, TensorType::ALL()) .DYNAMIC_INPUT(input, TensorType::ALL()) .DYNAMIC_OUTPUT(output, TensorType::ALL()) .GRAPH(then_branch) .GRAPH(else_branch) .OP_END_FACTORY_REG(If)

Its corresponding function prototype is:

  • Function name: If (C++) or EsIf (C)
  • Parameters: 3 in total, namely cond, input, then_branch, else_branch
  • Return value: output

In C API:

EsIfOutput EsIf(EsCTensorHolder *cond, EsCTensorHolder **input, int64_t input_num, int64_t output_num, EsCGraph *then_branch, EsCGraph *else_branch); typedef struct { EsCTensorHolder **output; int64_t output_num; } EsIfOutput;

C++ API:

std::vector<EsTensorHolder> If(const EsTensorHolder &cond, const std::vector<EsTensorHolder> &input, int64_t output_num, std::unique_ptr<ge::Graph> then_branch, std::unique_ptr<ge::Graph> else_branch)

Note:

  1. Use TensorLike type to express input, to support the case where actual parameters can directly pass values
  2. Use default parameters to express optional attributes in IR prototype

【免费下载链接】geGE(Graph Engine)是面向昇腾的图编译器和执行器,提供了计算图优化、多流并行、内存复用和模型下沉等技术手段,加速模型执行效率,减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力,并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/10 19:22:02

Backbone.js:云平台前端开发中的务实技术选型与实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/10 19:21:38

GLITTER OPERA S小尾巴深度解析:解码耳放的声学空间重构

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/10 19:20:14

CANN/GE dataflow.TimeBatch功能

&#xfeff;# dataflow.TimeBatch 【免费下载链接】ge GE&#xff08;Graph Engine&#xff09;是面向昇腾的图编译器和执行器&#xff0c;提供了计算图优化、多流并行、内存复用和模型下沉等技术手段&#xff0c;加速模型执行效率&#xff0c;减少模型内存占用。 GE 提供对 P…

作者头像 李华
网站建设 2026/9/10 19:16:10

2026年大专生必备:数据分析技能与就业指南

1. 为什么2026年的大专生必须掌握数据分析&#xff1f;大数据技术专业的学生在2026年毕业时将面临一个完全不同的就业市场。根据LinkedIn发布的《2025年最紧缺技能报告》&#xff0c;数据分析能力已经连续五年位居企业最需求技能前三名。作为即将毕业的大专生&#xff0c;掌握数…

作者头像 李华
网站建设 2026/9/10 19:15:50

MoE、推理模型、多模态分不清?一文理清大模型选型三大维度

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华