Yukyukuon / blog

博客的文章
1 stars 0 forks source link

Zhouyi AIPU #29

Open Yukyukuon opened 10 months ago

Yukyukuon commented 10 months ago

周易AIPU编程指南

Chapter 1:介绍

1.1 Zhouyi AIPU

周易AIPU是一款领域灵活、高能效的处理单元(power efficient processing unit)。

周易AIPU在架构(architecture)及其软件开发工具包(Software Development Kit)上有几个显着的特点:

1.2 Hardware architecture

AIPU可以通过不同单元提供 一般用途计算能力(general-purpose computing ability)人工智能计算能力(AI-specific processing ability)

AIPU processor

寄存器

根据访问方式将 AIPU 寄存器分为六种类型:

1.3 Software architecture

SDK 包括以下:

Chapter 2:内存操作

Memory hierarchy

内存结构

DMA operation

DMA由软件控制,在内部存储器和外部存储器空间之间执行数据移动。与 AIFF 操作类似,支持两种模式--MMR 配置模式(MMR configuration model)和描述符模式(descriptor model)。这两种模式不能同时工作,当软件需要更改模式时,需要确保 DMA 处于空闲状态(DMA is idle)。

有两种寄存器:

Register configuration mode

要在寄存器配置模式下工作,软件必须执行以下配置:

Chapter 3:AIPU toolchain

AIPU 工具链包括编译器(compiler),汇编器(assembler),链接器(linker)。利用这些工具,程序员可以在 AIPU 上开发自己的人工智能操作。
完成运算符的编码后(coding of an operator),需要对代码进行编译(compile),生成可在 AIPU 上运行的最终二进制文件(binary file)。编译过程与传统的编译过程类似,包括编译、汇编和链接。

编译过程

在编译阶段:

  1. In the compilation stage, the .c source file is compiled through AIPU C compiler aipucc to generate a .s assembly file(汇编文件).
  2. In the assembly stage, the .s file is turned into a .o object file by assembler aipuas.
  3. In the final stage, linker aipuld(链接器aipuld) links one or more .o files and the dependent(从属的) .a static library file and generates the final .out binary file.

    对于某些特殊函数(如 printf),还需要驱动程序的支持。
    最终编译好的 .out 二进制文件可以直接在 AIPU 模拟器上运行,也可以通过插件集成到 NN 编译器中。

AIPU C compiler

AIPU C,它基于 C 编程语言并进行了一些扩展,还提供了名为 aipucc 的 AIPU C 编译器。
与传统的 C 语言一样,AIPU C 支持标量日期类型和标量运算符。此外,AIPU C 还有以下主要扩展:

AIPU C 编译器支持所有周易系列平台。编译器提供 -mcpu 选项,用于指定程序运行的进程。这个编译器选项是你需要知道的最重要的选项。

AIPU C 编译器支持多级优化(mulit-level optimization)。Arm 中国建议在开发过程中使用 -O0,在需要更高性能的版本中使用 -O2
请注意以下 aipucc 编译命令中的选项:

AIPU assembler

如编译过程所示,AIPU 汇编程序 aipuas 将汇编语言代码转换为目标机器代码。如果您熟悉 AIPU 汇编语言,就可以直接用汇编语言为 AIPU 编程。
AIPU 汇编语言的语法与普通汇编语言的语法相同,只是有一些细微差别。AIPU 汇编程序支持指令、指令和用户定义的宏。
aipuas 的主要特点有:

示例

$aipuas input.S -mcpu=Z2_1104 -o output.o

请参阅《Arm 中国 AI 平台周易指南针汇编编程指南》。

AIPU linker

AIPU 链接器 aipuld 用于将目标文件链接到可执行文件。 AIPU 连接器的简单用法:

AIPU debugger

Arm 中国还提供了一个名为 aipudbg 的调试器,用于在 AIPU 模拟器或实际硬件上调试 AIPU 应用程序。aipudbg 允许您设置断点和单步,并检查和修改存储器和变量。
在硬件平台上调试则需要实际的额外外设,以便在客户端和实际硬件之间进行通信。
在多实例(多核)模式下,调试只支持硬件目标。应在调试器中启动应用程序前指定内核。
要调试程序,除了强烈建议使用"-O0 "外,还可在使用 aipucc 编译时添加 -g 选项,因为更高的优化级别和捆绑策略会扰乱调试信息。

指令 Description 描述
platform select (platform-name) Create a platform and select it as the current platform 创建一个平台并将其选为当前平台
platform connect (connect-url) Select the current platform by providing a connection URL 通过提供连接 URL 来选择平台
target create (target-file) Create a target using the argument as the main application 使用作为主程序的参数创建目标
settings set target.run-args (configuration-file) [core-id=Number] Set the run arguments with the configuration file and the core ID for multi-instance mode. The core ID is 0 by default. core-id is only used for hardware targets 使用配置文件和多实例模式的核心 ID 设置运行参数。core-id 仅用于硬件目标
breakpoint set (cmd-options) Set a breakpoint or a set of breakpoints in the executable using the options 使用选项在可执行文件中设置一个或一组断点
breakpoint delete (cmd-options) Delete the specified breakpoint(s). If no breakpoints are specified, delete them all 删除指定的断点。如果没有指定断点,则全部删除
watchpoint delete (cmd-options) Delete the specified watchpoint(s). If no watchpoints are specified, delete them all 删除指定的观察点。如果没有指定观察点,则全部删除
process launch Launch the AIPU application in the debugger 在调试器中启动 AIPU 应用程序
thread step-in Source level single step, stepping into calls 来源级单步骤,进入呼叫
thread step-over Source level single step, stepping over calls 源级单步,跨步调用
thread continue Commands for operating on one or more threads in the current process 对当前进程中的一个或多个线程进行操作的命令
thread backtrace Show thread call stacks 显示线程调用堆栈
memory read (cmd-options) (address-expression) Read from the memory of the current target process 读取当前目标进程的内存
memory write (cmd-options) (address) (value) [(value) [...]] Write to the memory of the current target process 写入当前目标进程的内存
register read (cmd-options) [(register-name) [...]] Dump the contents of one or more register values from the current frame. If no register is specified, dumps all general-purpose registers 转存当前帧中一个或多个寄存器的内容。如果没有指定寄存器,则转存所有通用寄存器
register write (register-name) (value) Modify a single register value 修改单个寄存器值

Chapter 4:AIPU NN compiler

工作流程:
神经网络编译器是将神经网络模型编译成 AIPU 可执行文件的串行工具。 神经网络编译器的运行主要包括三个步骤:

这三个步骤有以下相应的三个模块:

Model parse module

模型解析模块用于将预训练模型转换为 IR。该版本支持以下模型格式:

对于 Caffe 模型,还包括一些非官方层,如 shuffle 和 detectionoutput。 在模型解析模块中,应包含一个预训练模型,以及关于该模型的一些关键信息:

要解析一个模型,模块将执行以下操作:

  1. Reading the model file (for example, the frozen pb file) and build a raw graph.
  2. Converting the raw graph nodes to unified nodes.
  3. Optimization in graph level, for example, fusing Convolution+BatchNorm.
  4. Inference (推断)of shape and adding some addition nodes such as post-process nodes if necessary.

Quantization module

为了降低内存存储和计算成本,量化模块会将浮点模型转换为量化模型。目前只支持 8 位对称量化方法(8-bit symmetric quantization method)。
量化模块的工作如下:

  1. Receive the float Internal Representation (IR) and build the float graph.
  2. Calibrate each operation to get the output range and the constant data range (like weight and bias) of each operation.(校准每个操作,以获得每个操作的输出范围和常量数据范围(如权重和偏置)。)
  3. Quantize each operation.

Generation module

生成模块是神经网络编译器的后台。生成模块是构建神经网络模型的最后一步。
在生成模块中,流程与其他编译器类似:

  1. 将 IR 解析为有向无环图(DAG)。在此阶段,将对 IR 进行解析,并构建一个 DAG 对象来表示 IR。
  2. 合法化/降低 DAG 以满足硬件规格。在此阶段,您将操作 DAG 以满足硬件要求,例如将一个节点拆分为两个节点,将一个子图转换为另一个子图。
  3. 生成二进制文件。在此阶段,您将生成所有节点的所有参数,并构建二进制文件。

Quantization

The following figure shows the 8-bit quantization general pipeline of an operation 量化操作

可以先离线量化所有权重,然后在推理过程中,在每次操作(如卷积、元素相加等)之前,将浮点输入量化为 8 位值。之后,还要重新量化,以确保操作结果再次回到 8 位。
单个值的量化相当于将 float32 范围内的值映射到 int8 范围内。

下图显示了该pipeline: Pipeline

量化程序

NN 编译器中的量化模块执行以下步骤:

  1. 接收浮点内部表示(IR)并构建浮点图。
  2. 校准各层的特征图和常量数据(如有,如权重和偏置),以获得数据范围。
  3. 使用相应的量化算法对各层进行量化。
  4. 在浮点和量化后的特征图张量之间进行相似性统计,并转储所有张量(可选)。
  5. 使用度量插件对浮动模型和量化模型进行度量。

Using the NN compiler

安装

NN编译器打包为Pythonwheel包,即WHL文件。

pip install AIPUBuilder-xxx-xxx.whl

该软件包将依赖于TensorFlow、NumPy、NetworkX和其他第三方软件包。确保满足以下依赖关系:

用法:

用于设置全局选项的环境变量:

aipubuild

aipubuild 是软件包的入口点。可以使用 -h 或 --help 获取帮助信息。该工具使用配置文件中的参数。运行时需要指定所有相关参数。

$aipubuild test.cfg

配置文件是一个标准的 ini 配置文件。它由四个部分组成:

Common section

共用部分只包含一个模式键,用于告诉 aipubuild 在哪种模式下运行。默认情况下,该键处于运行模式,只有两种模式可用:

Parser section

该部分用于模型解析模块:

Optimizer section

该部分用于量化模块:

GBuilder section

本节介绍一些控制 GBuilder 模块行为的选项

下面是一个配置文件示例:

[Common]
mode=run
[Parser]
model_name=resnet_50
detection_postprocess=
model_domain=image_classification
output=resnet_v1_50/predictions/Reshape
input_model=./resnet_50/frozen.pb
input=Placeholder
input_shape=[1,224,224,3]
[Optimizer]
model_name=resnet_50
calibration_data=./Dataset/ImageNet/preprocess/resnet/calibration_100.npy
data=./Dataset/ImageNet/preprocess/resnet/dataset_1000.npy
label=./Dataset/ImageNet/preprocess/resnet/label_1000.npy
calibration_batch_size=20
metric_batch_size=50
dataset=NumpyDataset
metric=TopKMetric
[GBuilder]
simulator=./aipu_simulator
target=Z2_1104
inputs=./resnet_50/input.bin
outputs=./resnet_50/output_resnet_50.bin
profile=True
prof_unit=TPC