Merged
添加SIMD-CPP Tensor编程指南 #3187
wuzhaolinAdd on Jun 13
添加SIMD-CPP Tensor编程指南 #3187
Merged
Merged fromwuzhaolin/asc-devkittensor_designintocann/asc-devkitmaster
Total: 24 files changed+2032-15
| @@ -74,7 +74,7 @@ address、value、返回值的数据类型相同,支持的数据类型为int32 | |||
| 74 | ```cpp | 74 | ```cpp |
| 75 | extern "C" __global__ __aicore__ void atomic_max_reduction_kernel(__gm__ int32_t* global_max, __gm__ int32_t* input, int32_t dataSize) | 75 | extern "C" __global__ __aicore__ void atomic_max_reduction_kernel(__gm__ int32_t* global_max, __gm__ int32_t* input, int32_t dataSize) |
| 76 | { | 76 | { |
| 77 | - // 静态Tensor编程方式下,需手动调用InitSocState初始化全局状态寄存器。 | 77 | + // 当不使用TPipe-TQue框架编程时,需手动调用InitSocState初始化全局状态寄存器。 |
| 78 | AscendC::InitSocState(); | 78 | AscendC::InitSocState(); |
| 79 | 79 | ||
| 80 | // 使用LocalMemAllocator在UB上分配内存。 | 80 | // 使用LocalMemAllocator在UB上分配内存。 |
| @@ -74,7 +74,7 @@ address、value、返回值的数据类型相同,支持的数据类型为int32 | |||
| 74 | ```cpp | 74 | ```cpp |
| 75 | extern "C" __global__ __aicore__ void atomic_min_reduction_kernel(__gm__ int32_t* global_min, __gm__ int32_t* input, int32_t dataSize) | 75 | extern "C" __global__ __aicore__ void atomic_min_reduction_kernel(__gm__ int32_t* global_min, __gm__ int32_t* input, int32_t dataSize) |
| 76 | { | 76 | { |
| 77 | - // 静态Tensor编程方式下,需手动调用InitSocState初始化全局状态寄存器。 | 77 | + // 当不使用TPipe-TQUE框架编程时,需手动调用InitSocState初始化全局状态寄存器。 |
| 78 | AscendC::InitSocState(); | 78 | AscendC::InitSocState(); |
| 79 | 79 | ||
| 80 | // 使用LocalMemAllocator在UB上分配内存。 | 80 | // 使用LocalMemAllocator在UB上分配内存。 |
| @@ -7,7 +7,7 @@ | |||
| 7 | </th> | 7 | </th> |
| 8 | <th class="cellrowborder" align="center" valign="top" width="23.1%" id="mcps1.1.4.1.2"><p id="p783113012187"><a name="p783113012187"></a><a name="p783113012187"></a>是否支持(Pipe框架)</p> | 8 | <th class="cellrowborder" align="center" valign="top" width="23.1%" id="mcps1.1.4.1.2"><p id="p783113012187"><a name="p783113012187"></a><a name="p783113012187"></a>是否支持(Pipe框架)</p> |
| 9 | </th> | 9 | </th> |
| 10 | -<th class="cellrowborder" align="center" valign="top" width="23.26%" id="mcps1.1.4.1.3"><p id="p510433120182"><a name="p510433120182"></a><a name="p510433120182"></a>是否支持(静态Tensor编程)</p> | 10 | +<th class="cellrowborder" align="center" valign="top" width="23.26%" id="mcps1.1.4.1.3"><p id="p510433120182"><a name="p510433120182"></a><a name="p510433120182"></a>是否支持(C++ Tensor编程)</p> |
| 11 | </th> | 11 | </th> |
| 12 | </tr> | 12 | </tr> |
| 13 | </thead> | 13 | </thead> |
| @@ -95,7 +95,7 @@ LocalTensor构造函数。 | |||
| 95 | </thead> | 95 | </thead> |
| 96 | <tbody><tr id="zh-cn_topic_0000001429830437_row1835857145817"><td class="cellrowborder" valign="top" width="16.28%" headers="mcps1.2.3.1.1 "><p id="zh-cn_topic_0000001429830437_p5835457165816"><a name="zh-cn_topic_0000001429830437_p5835457165816"></a><a name="zh-cn_topic_0000001429830437_p5835457165816"></a>T</p> | 96 | <tbody><tr id="zh-cn_topic_0000001429830437_row1835857145817"><td class="cellrowborder" valign="top" width="16.28%" headers="mcps1.2.3.1.1 "><p id="zh-cn_topic_0000001429830437_p5835457165816"><a name="zh-cn_topic_0000001429830437_p5835457165816"></a><a name="zh-cn_topic_0000001429830437_p5835457165816"></a>T</p> |
| 97 | </td> | 97 | </td> |
| 98 | -<td class="cellrowborder" valign="top" width="83.72%" headers="mcps1.2.3.1.2 "><a name="ul171781281310"></a><a name="ul171781281310"></a><ul id="ul171781281310"><li>适用于Pipe编程框架的原型,支持基础数据类型以及<a href="../../辅助数据结构/TensorTrait/TensorTrait.md">TensorTrait</a>类型。</li><li>适用于静态Tensor编程的原型,支持的数据类型如下:<pre class="screen" id="screen148501140172610"><a name="screen148501140172610"></a><a name="screen148501140172610"></a>// 仅支持基础数据类型 | 98 | +<td class="cellrowborder" valign="top" width="83.72%" headers="mcps1.2.3.1.2 "><a name="ul171781281310"></a><a name="ul171781281310"></a><ul id="ul171781281310"><li>适用于Pipe编程框架的原型,支持基础数据类型以及<a href="../../辅助数据结构/TensorTrait/TensorTrait.md">TensorTrait</a>类型。</li><li>适用于C++ Tensor编程的原型,支持的数据类型如下:<pre class="screen" id="screen148501140172610"><a name="screen148501140172610"></a><a name="screen148501140172610"></a>// 仅支持基础数据类型 |
| 99 | __aicore__ inline LocalTensor<T>(TPosition pos, uint32_t addr, uint32_t tileSize) | 99 | __aicore__ inline LocalTensor<T>(TPosition pos, uint32_t addr, uint32_t tileSize) |
| 100 | // 仅支持TensorTrait类型 | 100 | // 仅支持TensorTrait类型 |
| 101 | __aicore__ inline LocalTensor<T>(uint32_t addr)</pre> | 101 | __aicore__ inline LocalTensor<T>(uint32_t addr)</pre> |
| @@ -288,7 +288,7 @@ inputLocal.Print(); | |||
| 288 | // 0008: 9 10 11 12 13 14 15 | 288 | // 0008: 9 10 11 12 13 14 15 |
| 289 | #endif | 289 | #endif |
| 290 | 290 | ||
| 291 | -// 示例20在静态Tensor编程场景使用,根据传入的逻辑位置VECIN、起始地址128、元素个数32、数据类型float,构造出Tensor对象 | 291 | +// 示例20在C++ Tensor编程场景使用,根据传入的逻辑位置VECIN、起始地址128、元素个数32、数据类型float,构造出Tensor对象 |
| 292 | uint32_t addr = 128; | 292 | uint32_t addr = 128; |
| 293 | uint32_t tileSize = 32; | 293 | uint32_t tileSize = 32; |
| 294 | AscendC::LocalTensor<float> tensor1 = AscendC::LocalTensor<float>(AscendC::TPosition::VECIN, addr, tileSize); | 294 | AscendC::LocalTensor<float> tensor1 = AscendC::LocalTensor<float>(AscendC::TPosition::VECIN, addr, tileSize); |
| @@ -88,7 +88,7 @@ | |||
| 88 | </thead> | 88 | </thead> |
| 89 | <tbody><tr id="row18835145716587"><td class="cellrowborder" valign="top" width="17.43%" headers="mcps1.2.3.1.1 "><p id="p1383515717581"><a name="p1383515717581"></a><a name="p1383515717581"></a>pos</p> | 89 | <tbody><tr id="row18835145716587"><td class="cellrowborder" valign="top" width="17.43%" headers="mcps1.2.3.1.1 "><p id="p1383515717581"><a name="p1383515717581"></a><a name="p1383515717581"></a>pos</p> |
| 90 | </td> | 90 | </td> |
| 91 | -<td class="cellrowborder" valign="top" width="82.57%" headers="mcps1.2.3.1.2 "><p id="p18689719202918"><a name="p18689719202918"></a><a name="p18689719202918"></a><a href="../../../数据结构/辅助数据结构//TPosition.md">TPosition</a>位置,需要符合<a href="LocalMemAllocator简介.md">LocalMemAllocator</a>中指定的Hardware物理位置(静态Tensor编程场景下,此参数可以省略)。</p> | 91 | +<td class="cellrowborder" valign="top" width="82.57%" headers="mcps1.2.3.1.2 "><p id="p18689719202918"><a name="p18689719202918"></a><a name="p18689719202918"></a><a href="../../../数据结构/辅助数据结构//TPosition.md">TPosition</a>位置,需要符合<a href="LocalMemAllocator简介.md">LocalMemAllocator</a>中指定的Hardware物理位置(C++ Tensor编程场景下,此参数可以省略)。</p> |
| 92 | </td> | 92 | </td> |
| 93 | </tr> | 93 | </tr> |
| 94 | <tr id="row199212475441"><td class="cellrowborder" valign="top" width="17.43%" headers="mcps1.2.3.1.1 "><p id="p352033512453"><a name="p352033512453"></a><a name="p352033512453"></a>DataType</p> | 94 | <tr id="row199212475441"><td class="cellrowborder" valign="top" width="17.43%" headers="mcps1.2.3.1.1 "><p id="p352033512453"><a name="p352033512453"></a><a name="p352033512453"></a>DataType</p> |
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:e5588e0d69607d6b3ac7fbf20022321b6b15a8ee5b4bb8f7c60f637f5a124204 | ||
| 3 | +size 28308 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:ec34c7e28e636b86b5470b43978a00ae820d69b39a0d6e5baa5d1da5c369d8aa | ||
| 3 | +size 38683 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:0b9758741c3619eb8b20d636531339408269acaf36e09b1f40be622709db6523 | ||
| 3 | +size 63254 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:c79551fe97adcaae705a7a4530fb0773facd6b3f8329e3fd5e9a9a342839a1b3 | ||
| 3 | +size 15034 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:eb353b1acac4863d697e97b8bda3c5bbae1bbc8309405be53df75b8fa8d89373 | ||
| 3 | +size 17116 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:ae406b744b68d75868e77587e22f46c5282b6133c19b5f906b4a4fc5c1ec5bd0 | ||
| 3 | +size 19937 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:1f27ce1254ef30ddcfd9b958d908a3472fc6d98b2b446fb24ef5fd5b41525478 | ||
| 3 | +size 17358 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:9bda7f26567c66df9facef79cbb236163c34bcc9ab3ca6efcdbf39c014bc3f1b | ||
| 3 | +size 45464 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:2dce417a7e42562d0e1f6e6f2ad05770f6b7b3b51ffe811b3840ede6a28b313a | ||
| 3 | +size 47849 | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:cadda4891fa1e6fb92e3116b75ca826dd5b266f835e97c205c2a822fc897496b | ||
| 3 | +size 43445 | ||
| @@ -27,9 +27,12 @@ | |||
| 27 | - [Memory矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于指针的C语言编程/Memory矢量计算编程.md) | 27 | - [Memory矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于指针的C语言编程/Memory矢量计算编程.md) |
| 28 | - [Reg矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于指针的C语言编程/Reg矢量计算编程.md) | 28 | - [Reg矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于指针的C语言编程/Reg矢量计算编程.md) |
| 29 | - [Cube矩阵计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于指针的C语言编程/Cube矩阵计算编程.md) | 29 | - [Cube矩阵计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于指针的C语言编程/Cube矩阵计算编程.md) |
| 30 | - - [基于Tensor的C++编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/静态Tensor编程.md) | 30 | + - [基于Tensor的C++编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/CPPTensor编程概述.md) |
| 31 | - - [静态Tensor编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/静态Tensor编程.md) | 31 | + - [C++ Tensor编程概述](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/CPPTensor编程概述.md) |
| 32 | + - [Memory矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/Memory矢量计算编程.md) | ||
| 32 | - [Reg矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/Reg矢量计算编程.md) | 33 | - [Reg矢量计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/Reg矢量计算编程.md) |
| 34 | + - [Cube矩阵计算编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/Cube矩阵计算编程.md) | ||
| 35 | + - [静态Tensor编程](编程指南/编程模型/AI-Core-SIMD编程/基于Tensor的CPP编程/静态Tensor编程.md) | ||
| 33 | - [基于TPipe和TQue编程](编程指南/编程模型/AI-Core-SIMD编程/基于TPipe-TQue框架编程/TPipe-TQue框架编程原理.md) | 36 | - [基于TPipe和TQue编程](编程指南/编程模型/AI-Core-SIMD编程/基于TPipe-TQue框架编程/TPipe-TQue框架编程原理.md) |
| 34 | - [TPipe-TQue框架编程原理](编程指南/编程模型/AI-Core-SIMD编程/基于TPipe-TQue框架编程/TPipe-TQue框架编程原理.md) | 37 | - [TPipe-TQue框架编程原理](编程指南/编程模型/AI-Core-SIMD编程/基于TPipe-TQue框架编程/TPipe-TQue框架编程原理.md) |
| 35 | - [TPipe-TQue框架编程范式](编程指南/编程模型/AI-Core-SIMD编程/基于TPipe-TQue框架编程/TPipe-TQue框架编程范式.md) | 38 | - [TPipe-TQue框架编程范式](编程指南/编程模型/AI-Core-SIMD编程/基于TPipe-TQue框架编程/TPipe-TQue框架编程范式.md) |
| @@ -18,7 +18,7 @@ $$ | |||
| 18 | 18 | ||
| 19 | ## 算子实现 | 19 | ## 算子实现 |
| 20 | 20 | ||
| 21 | -本样例基于静态Tensor编程模式实现Matmul和LeakyRelu融合计算,展示Cube单元(矩阵计算单元)和Vector单元(向量计算单元)协同计算的编程模式。Cube核完成矩阵乘计算,Vector核完成LeakyRelu激活计算。 | 21 | +本样例基于C++ Tensor编程模式实现Matmul和LeakyRelu融合计算,展示Cube单元(矩阵计算单元)和Vector单元(向量计算单元)协同计算的编程模式。Cube核完成矩阵乘计算,Vector核完成LeakyRelu激活计算。 |
| 22 | 22 | ||
| 23 | **数据流路径**: | 23 | **数据流路径**: |
| 24 | 24 | ||
| @@ -1,6 +1,6 @@ | |||
| 1 | # 中级性能优化 | 1 | # 中级性能优化 |
| 2 | 2 | ||
| 3 | -本节基于静态Tensor编程范式,介绍Matmul基础API算子的性能调优方法,通过4个渐进式优化步骤(L1/L0双缓冲机制、细粒度流水同步、多核并行切分、DataCopyIn优先搬运B矩阵),展示从基础实现到高性能优化的完整调优路径。 | 3 | +本节基于C++ Tensor编程范式,介绍Matmul基础API算子的性能调优方法,通过4个渐进式优化步骤(L1/L0双缓冲机制、细粒度流水同步、多核并行切分、DataCopyIn优先搬运B矩阵),展示从基础实现到高性能优化的完整调优路径。 |
| 4 | 4 | ||
| 5 | ## 优化路径: | 5 | ## 优化路径: |
| 6 | 6 | ||
| @@ -1,6 +1,6 @@ | |||
| 1 | # 高级极限打磨 | 1 | # 高级极限打磨 |
| 2 | 2 | ||
| 3 | -本节基于静态Tensor编程范式,在上文中级性能优化的基础上,进一步介绍Matmul基础API算子的高级极限打磨方法,通过5个高级优化步骤(大包搬运、LoadData3D替代LoadData2D、常量Tiling、UnitFlag优化、L2Cache优化),深入挖掘硬件性能极限。 | 3 | +本节基于C++ Tensor编程范式,在上文中级性能优化的基础上,进一步介绍Matmul基础API算子的高级极限打磨方法,通过5个高级优化步骤(大包搬运、LoadData3D替代LoadData2D、常量Tiling、UnitFlag优化、L2Cache优化),深入挖掘硬件性能极限。 |
| 4 | 4 | ||
| 5 | ## 优化路径: | 5 | ## 优化路径: |
| 6 | 6 | ||
| @@ -0,0 +1,509 @@ | |||
| 1 | +# Cube矩阵计算编程<a name="ZH-CN_TOPIC_0000002600000003"></a> | ||
| 2 | + | ||
| 3 | +本文介绍如何在Ascend C中使用C++ Tensor编程API编写基于L1 Buffer、L0A Buffer等片上存储的**矩阵计算代码**。 | ||
| 4 | + | ||
| 5 | +AI Core包含Vector计算单元(部署于AIV核)和Cube计算单元(部署于AIC核)。Cube计算单元专用于执行矩阵运算,直接访问的专用缓存如下:L0A Buffer存储左矩阵,L0B Buffer存储右矩阵,L0C Buffer存储初始累加值及矩阵计算结果。 | ||
| 6 | + | ||
| 7 | +## 矩阵编程的基本步骤 | ||
| 8 | + | ||
| 9 | +Cube矩阵计算的基本步骤为:数据搬入 → 数据加载 → 计算 → 数据搬出。当我们描述一个常见的Cube矩阵计算时,需要执行以下四个步骤: | ||
| 10 | + | ||
| 11 | +1. 通过DMA将数据从GM搬入到`Local Memory(L1 Buffer)` | ||
| 12 | +2. 通过数据加载将L1 Buffer搬入到`L0A Buffer & L0B Buffer` | ||
| 13 | +3. 进行矩阵计算,计算结果存储在`L0C Buffer`中 | ||
| 14 | +4. 通过DMA将数据从`L0C Buffer`搬出到GM | ||
| 15 | + | ||
| 16 | +## 矩阵计算内存管理 | ||
| 17 | + | ||
| 18 | +### 矩阵计算内存申请 | ||
| 19 | + | ||
| 20 | +矩阵计算编程主要通过L0A/L0B/L0C Buffer进行矩阵计算。开发者需将输入数据从Global Memory(以下简称GM)搬入L1 Buffer,再从L1 Buffer搬入L0A/L0B Buffer,最终通过Cube计算指令完成计算。L1 Buffer与L0A/L0B/L0C Buffer均通过LocalTensor对象进行管理,开发者可通过LocalMemAllocator分配器申请矩阵计算所需的内存空间。 | ||
| 21 | + | ||
| 22 | +```cpp | ||
| 23 | +__global__ __cube__ void matmul_custom(...) | ||
| 24 | +{ | ||
| 25 | + constexpr uint32_t m_size = 128; | ||
| 26 | + constexpr uint32_t n_size = 128; | ||
| 27 | + constexpr uint32_t k_size = 256; | ||
| 28 | + | ||
| 29 | + AscendC::LocalMemAllocator<AscendC::Hardware::L1> l1Allocator; | ||
| 30 | + AscendC::LocalMemAllocator<AscendC::Hardware::L0A> l0aAllocator; | ||
| 31 | + AscendC::LocalMemAllocator<AscendC::Hardware::L0B> l0bAllocator; | ||
| 32 | + AscendC::LocalMemAllocator<AscendC::Hardware::L0C> l0cAllocator; | ||
| 33 | + | ||
| 34 | + // Allocate L1 Buffer for matrix A and B | ||
| 35 | + LocalTensor<half> l1a_buffer = l1Allocator.Alloc<half, m_size * k_size>(); | ||
| 36 | + LocalTensor<half> l1b_buffer = l1Allocator.Alloc<half, k_size * n_size>(); | ||
| 37 | + // Allocate L0A/L0B/L0C Buffer for matrix computation | ||
| 38 | + LocalTensor<half> l0a_buffer = l0aAllocator.Alloc<half, m_size * k_size>(); | ||
| 39 | + LocalTensor<half> l0b_buffer = l0bAllocator.Alloc<half, k_size * n_size>(); | ||
| 40 | + LocalTensor<float> l0c_buffer = l0cAllocator.Alloc<float, m_size * n_size>(); | ||
| 41 | + | ||
| 42 | + // ... Use Buffer for matrix computation | ||
| 43 | +} | ||
| 44 | +``` | ||
| 45 | + | ||
| 46 | +### 矩阵计算内存布局 | ||
| 47 | + | ||
| 48 | +矩阵计算单元采用分块计算逻辑,其中16×16是矩阵计算单元分形的基本粒度(对于half数据类型;int8类型为16×32或32×16;fp32类型分形为16×8),硬件可以并行处理多个矩阵计算单元。在传统的线性存储布局(如行主序或列主序)中,数据按行或列连续存放。当需要读取一个16×16的矩形数据块时,若直接从内存中获取,则需访问不连续的多个内存地址以逐行“收集”数据,这种非连续访问会导致内存效率显著下降。 | ||
| 49 | +为解决上述问题,引入了矩阵分形存储格式。该格式通过软件层对数据进行重排,使每个16×16计算块在物理内存中连续存放。硬件仅需发起一次连续读取,即可将整个数据块加载至片上寄存器,避免了多地址寻址开销,从而大幅提升矩阵计算单元的数据吞吐率。对于half数据类型,16×16是矩阵计算单元的基本分形粒度,当前支持Cube计算的产品型号包括[NPU架构版本2201](../../../语言扩展层/SIMD-BuiltIn关键字.md)和[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md)。 | ||
| 50 | +昇腾AI处理器的矩阵计算单元为追求极高的数据吞吐率与计算密度,采用了专有的硬件架构设计。与通用CPU常用的连续线性存储不同,该单元要求输入输出数据必须符合特定的分形存储格式。在使用Ascend C的Mmad类接口进行算子开发时,深入理解这些数据格式对于实现正确的数据搬运、分块及性能优化至关重要。 | ||
| 51 | +为便于理解与记忆,文档采用“大Y小x”的直观命名法: | ||
| 52 | + - 大Y(Z/N):表示分形矩阵之间的排列顺序(Z为行主序,N为列主序)。 | ||
| 53 | + - 小x(z/n):表示分形矩阵内部元素的排列顺序(z为行主序,n为列主序)。 | ||
| 54 | + | ||
| 55 | +### 关键分形格式详解 | ||
| 56 | + | ||
| 57 | +矩阵分形存储格式是对多维Tensor在内存中数据排布格式的描述。 | ||
| 58 | +为了适配矩阵计算单元的物理读取路径,不同的产品形态有不同的格式要求: | ||
| 59 | +针对[NPU架构版本2201](../../../语言扩展层/SIMD-BuiltIn关键字.md),矩阵乘法C = A × B要求:左矩阵A使用Zz格式,右矩阵B使用Zn格式,结果矩阵C使用Nz格式,如下图所示: | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +针对[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md),矩阵乘法C = A × B要求:左矩阵A使用Nz格式,右矩阵B使用Zn格式,结果矩阵C使用Nz格式,如下图所示: | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + | ||
| 67 | +#### ND格式(N-Dimension) | ||
| 68 | + | ||
| 69 | +ND格式是通用的N维张量格式,数据在内存中连续线性存放,通常存在于GM中。在进行矩阵运算前,通常需要将其转换为Nz格式以适配Cube计算单元。 | ||
| 70 | + | ||
| 71 | +#### Nz格式 | ||
| 72 | + | ||
| 73 | +Nz格式主要用于L1 Buffer和L0C Buffer中存放数据。针对[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md),L0A Buffer也采用Nz分形格式。该格式采用大N(外部列主序)+ 小z(内部行主序)的方式排布。 | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + | ||
| 77 | +Nz是Ascend架构的中间格式,介于线性数据(ND)和计算专用数据(Zz/Zn)之间,便于格式转换。大N排布(列主序Block)的设计通常是为了配合多核并行或后续算子(如Vector向量计算)在处理通道(Channel)维度数据时的便利性。 | ||
| 78 | + | ||
| 79 | +> 📌 Nz格式在不同Buffer中的应用场景: | ||
| 80 | +> - **L0C Buffer**: 存储矩阵乘法结果。分形形状为16×16(对于half数据类型,256元素),适合Cube计算单元高效运算。 | ||
| 81 | +> - **L1 Buffer**: 存储中间数据。分形形状为16×(32B/sizeof(DataType)),大小512字节,便于转换为Zz和Zn格式。 | ||
| 82 | +> - **L0A Buffer(仅[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md))**: 存储左矩阵。 | ||
| 83 | + | ||
| 84 | +#### Zz格式 | ||
| 85 | + | ||
| 86 | +针对[NPU架构版本2201](../../../语言扩展层/SIMD-BuiltIn关键字.md),L0A Buffer使用此格式([NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md)使用Nz格式)。该格式采用大Z(外部行主序)+ 小z(内部行主序)。 | ||
| 87 | + | ||
| 88 | + | ||
| 89 | + | ||
| 90 | +在矩阵乘法中,A矩阵逐行读取数据。Zz格式的行主序特性使同一行元素物理地址连续,与计算访问模式匹配,便于Cube单元左侧通道通过DMA连续读取,避免跨步访问,提升数据搬运效率。 | ||
| 91 | + | ||
| 92 | +#### Zn格式 | ||
| 93 | + | ||
| 94 | +Zn格式主要用于L0B Buffer中存放数据,如果离线处理好的右矩阵数据直接为Zn格式,则在GM和L1 Buffer上存储该格式。该格式采用大Z(外部行主序) + 小n(内部列主序)。 | ||
| 95 | + | ||
⚠️ [P1] [PG-5-约束前置] Nd2NzParams 8个参数完全无注释说明 代码示例 (此意见由AI提出,评审规则PG-5。如有异议,请回复此评论,让我变得更好:)) ![]() ![]() | |||
| 96 | + | ||
| 97 | + | ||
| 98 | +矩阵乘法需读取B矩阵列向量。普通行主序存储导致列数据在内存中地址不连续。Zn格式在分形内部按列存储,使列数据在L0B中紧凑排列,便于Cube单元右侧通道线性读取完整列向量,避免跨步访问。 | ||
| 99 | + | ||
| 100 | +### 格式转换 | ||
| @@ -0,0 +1,307 @@ | |||
| 1 | +# Memory矢量计算编程<a name="ZH-CN_TOPIC_0000002600000004"></a> | ||
| 2 | + | ||
| 3 | +本文介绍如何在Ascend C中使用C++ Tensor编程API编写基于Unified Buffer(以下简称UB)的矢量计算代码,该能力是Ascend C最基础、最通用的矢量编程方式。这种基于UB内存的编程方式被称为Memory矢量计算编程。[NPU架构版本2201](../../../语言扩展层/SIMD-BuiltIn关键字.md)产品支持完整的Memory矢量计算UB直接编程能力;而[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md)虽也支持UB操作,但推荐使用Reg矢量计算编程以获得更高性能。 | ||
| 4 | + | ||
| 5 | +## Memory矢量编程的基本步骤 | ||
| 6 | + | ||
| 7 | +Memory矢量计算的数据流为:数据搬入 → 计算 → 数据搬出。当我们描述一个常见的Vector矢量计算时,需要执行以下三个步骤: | ||
| 8 | + | ||
| 9 | +1. 通过DMA将数据从Global Memory搬入到Local Memory | ||
| 10 | +2. 进行矢量计算 | ||
| 11 | +3. 通过DMA将数据从Local Memory搬出到Global Memory | ||
| 12 | + | ||
| 13 | +## Memory矢量计算内存管理 | ||
| 14 | + | ||
| 15 | +Memory矢量编程的核心是基于Local Memory(UB)完成矢量计算,要求开发者先将输入数据从Global Memory(以下简称GM)搬入UB,再通过Vector计算指令执行运算。UB的管理依赖LocalTensor对象实现,开发者可通过LocalMemAllocator分配器申请UB空间,为后续矢量计算提供存储载体。 | ||
| 16 | + | ||
| 17 | +### 基础Tensor(不带Layout)定义 | ||
| 18 | + | ||
| 19 | +通过`LocalMemAllocator`内存分配器创建,用户可指定物理存储位置和内存大小。 | ||
| 20 | + | ||
| 21 | +```cpp | ||
| 22 | +__global__ __vector__ void add_custom(...) | ||
| 23 | +{ | ||
| 24 | + constexpr uint32_t ub_size = 256; | ||
| 25 | + AscendC::LocalMemAllocator<AscendC::Hardware::UB> ubAllocator; | ||
| 26 | + AscendC::LocalTensor<half> static_buffer = ubAllocator.Alloc<half, ub_size>(); | ||
| 27 | +} | ||
| 28 | +``` | ||
| 29 | + | ||
| 30 | +## Memory数据搬运 | ||
| 31 | + | ||
| 32 | +GM与UB之间的数据搬运提供了多样化的接口,支持丰富的数据搬运场景。为实现高效的Vector计算,系统支持将数据从GM搬运至UB,供Vector计算单元进行向量计算。计算完成后,再将结果从UB搬回GM,完成数据闭环。 | ||
| 33 | + | ||
| 34 | +开发者通过灵活配置DataCopyParams参数,精确控制数据块数量、地址长度及间隔等关键属性,实现连续和非连续数据搬运(高维切分数据搬运),充分释放硬件的数据搬运与计算协同能力,提升向量算子的执行效率。 | ||
| 35 | + | ||
| 36 | +| 数据通路 | 模式 | 接口 | 适用场景 | | ||
| 37 | +|---|---|---|---| | ||
| 38 | +| GM→UB | 连续搬运 | `DataCopy(dst, src, count)` | 一维连续数据搬运 | | ||
| 39 | +| GM→UB | 高维切分 | `DataCopy(dst, src, dataCopyParams)` | 多块非连续数据,可配置块数和间隔 | | ||
| 40 | +| GM→UB | 非对齐搬运 | `DataCopyPad(dst, src, dataCopyExtParams, padParams)` | 地址非32B对齐,支持自定义填充 | | ||
| 41 | +| UB→GM | 连续搬运 | `DataCopy(dst, src, count)` | 一维连续数据搬运 | | ||
| 42 | +| UB→GM | 高维切分 | `DataCopy(dst, src, dataCopyParams)` | 多块非连续数据,可配置块数和间隔 | | ||
| 43 | +| UB→GM | 非对齐搬运 | `DataCopyPad(dst, src, dataCopyExtParams)` | 地址非32B对齐 | | ||
| 44 | + | ||
| 45 | +根据上述矢量搬运能力的描述,开发者可以根据不同的场景选择合适的接口进行处理。 | ||
| 46 | + | ||
| 47 | +**连续搬运**: 如果开发者需要将一块连续的`Global Memory`搬运到UB时,只需指定搬运数据元素个数即可。例如srcGlobal数据(shape为256,数据类型为half),需搬入到UB中,通过`DataCopy`直接调用: | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + | ||
| 51 | +```cpp | ||
| 52 | +AscendC::DataCopy(dstLocal, srcGlobal, 256); | ||
| 53 | +``` | ||
| 54 | + | ||
| 55 | +**高维切分搬运**: 当需要将多块非连续的GM数据搬入单个UB时,可通过高维切分模式配置搬运数据块的数量、单个数据块长度及块间间隔。以srcGlobal数据(shape为256,数据类型为half)为例,如下场景的数据搬运可直接通过C++ Tensor接口实现: | ||
| 56 | + | ||
| 57 | + | ||
| 58 | + | ||
| 59 | +```cpp | ||
| 60 | +// A total of 6 dataBlocks are transferred, split into two rounds of 3 dataBlocks each. | ||
| 61 | +uint16_t blockCount = 2; | ||
| 62 | +// Length of single data block (in 32B units): value is 3 | ||
| 63 | +uint16_t blockLen = 3; | ||
| 64 | +// Gap from previous block to next block in source data (in 32B units): value is 1 | ||
| 65 | +uint16_t srcGap = 1; | ||
srcGap=1但注释说source blocks are consecutive,连续块间隔应该是0。同样dstGap=2但注释说interval of one block,应该是1。这两个值是不是改错了? ![]() ![]() | |||
| 66 | +// Gap from previous block to next block in destination data (in 32B units): interval of one datablock between destination blocks, value is 2 | ||
| 67 | +uint16_t dstGap = 2; | ||
| 68 | + | ||
| 69 | +DataCopyParams copyParams {blockCount, blockLen, srcGap, dstGap}; | ||
| 70 | +AscendC::DataCopy(dstLocal, srcGlobal, copyParams); | ||
| 71 | +``` | ||
| 72 | + | ||
| 73 | +**非对齐搬运**: 高维切分数据搬运提供了用户进行非连续多个数据块搬入的能力,但该接口要求GM中搬运的长度和间隔都必须保证是32B对齐的,因此对于一些非对齐场景无法支持。例如需要进行如下图所示的搬运,则无法通过`DataCopy`高维切分接口完成隔行数据的搬运,此时需要突破32B的约束限制。Ascend C根据芯片能力,提供了`DataCopyPad`接口来支持该搬运形式,该接口可以指定以字节为单位的blockLen和stride信息,完成搬运操作。 | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + | ||
| 77 | +在上图所示场景中,需将两个数据块从GM搬运至UB:每个数据块的blockLen为54B,源操作数中相邻数据块的间隔为1B,目的操作数中相邻数据块的间隔为32B。为满足32B对齐要求,需在blockLen左侧填充2个half类型元素、右侧填充3个half类型元素;此时总长度为blockLen + leftPadding × 2B + rightPadding × 2B = 54B + 2 × 2B + 3 × 2B = 64B,符合32B对齐约束。 | ||
| 78 | + | ||
| 79 | +```cpp | ||
| 80 | +// Number of consecutive data blocks: transfer two consecutive data blocks | ||
[V-14] P2 | CR-11 | Memory矢量计算编程.md:80
连续搬运代码示例使用 此评论由AI生成,如有异议,请回复帮助我更快成长:) ![]() ![]() | |||
| 81 | +uint16_t blockCount = 2; | ||
| 82 | +// Length of single data block (in bytes): value is 54 | ||
| 83 | +uint32_t blockLen = 54; | ||
| 84 | +// Stride from previous block to next block in source data: source data stride unit is 1B | ||
| 85 | +uint32_t srcStride = 1; | ||
| 86 | +// Stride from previous block to next block in destination data: UB operation constraint address must be 32B aligned, value is 32 / 32 | ||
| 87 | +uint32_t dstStride = 32 / 32; | ||
| 88 | +// Whether to fill with user-defined data: true means use custom pad value, false means fill with random values by default | ||
| 89 | +bool isPad = true; | ||
| 90 | +// Left padding of transfer result (element count): value is 4 / sizeof(half) = 2 | ||
| 91 | +uint8_t leftPadding = 2; | ||
| 92 | +// Right padding of transfer result (element count): value is 6 / sizeof(half) = 3 | ||
| 93 | +uint8_t rightPadding = 3; | ||
| 94 | + | ||
| 95 | +AscendC::DataCopyExtParams copyParams {blockCount, blockLen, srcStride, dstStride, 0}; | ||
[V-15] P2 | CR-11 | Memory矢量计算编程.md:95
非对齐搬运示例也使用了 此评论由AI生成,如有异议,请回复帮助我更快成长:) ![]() ![]() | |||
| 96 | +AscendC::DataCopyPadExtParams<half> padParams {isPad, leftPadding, rightPadding, 0}; | ||
| 97 | + | ||
| 98 | +AscendC::DataCopyPad(dstLocal, srcGlobal, copyParams, padParams); | ||
| 99 | +``` | ||
| 100 | + | ||
| 101 | +> 📌 该接口主要用于提升用户数据搬运的易用性,由软件层实现,底层基于高维切分接口封装。 | ||
| 102 | + | ||
| 103 | +如上完成了GM到UB的搬入,UB搬出到GM采用同样的接口处理。 | ||
| 104 | + | ||
| 105 | +更多搬运接口请参考API手册。 | ||
| 106 | + | ||
| 107 | +## Memory数据计算 | ||
| 108 | + | ||
| 109 | +Memory矢量计算提供连续计算和高维切分两种计算模式,并可通过掩码实现数据选择性参与计算。 | ||
| 110 | +以Add接口为例,两种计算模式的调用方式示意图如下。其中连续计算简单直观,适合一维Tensor连续数据处理;高维切分计算灵活可控,支持迭代执行和地址间隔配置。开发者可根据实际业务需求选择合适的计算方式。 | ||
| 111 | + | ||
| 112 | + | ||
| 113 | + | ||
| 114 | +### 高维切分计算 | ||
| 115 | + | ||
| 116 | +高维切分能力主要是支持对计算接口设置`repeatTime`、`stride`和`mask`参数等,能精确控制指令的迭代执行、操作数的地址间隔和迭代内的掩码。 | ||
| 117 | + | ||
| 118 | +#### 迭代控制 | ||
| 119 | + | ||
| 120 | +Vector计算单元每个迭代会从UB中取出8个DataBlock(每个DataBlock内部地址连续,长度为32Byte)进行计算,并将结果写入对应的8个DataBlock中。 | ||
| 121 | + | ||
| 122 | + | ||
| 123 | + | ||
| 124 | +矢量计算API支持通过配置repeatTime参数控制指令的迭代执行次数。若将repeatTime设为2,矢量计算单元会执行2次迭代计算,可处理的数据量为2 × 8(单次迭代8个DataBlock) × 32Byte = 512Byte;若数据类型为half(2B / 元素),则对应计算256个元素。下图为2次迭代Exp计算的示意图。 | ||
| 125 | +注意:受硬件限制,repeatTime的取值范围为[1, 255],该约束适用于所有支持Memory矢量计算的产品。 | ||
| 126 | + | ||
| 127 | + | ||
| 128 | + | ||
| 129 | +#### 地址间隔配置 | ||
| 130 | + | ||
| 131 | +矢量计算单元还支持带地址间隔的向量计算能力,可通过dataBlockStride和repeatStride两个参数精准配置: | ||
| 132 | +- dataBlockStride:单次迭代内不同DataBlock间的地址步长,取值不得超过UB的空间大小限制; | ||
| 133 | +- repeatStride:相邻迭代间相同DataBlock的地址步长(下文详细说明)。 | ||
| 134 | + | ||
| 135 | +连续计算,`dataBlockStride`设置为1,对同一迭代内的8个DataBlock数据连续进行处理。 | ||
| 136 | +非连续计算,`dataBlockStride`值大于1(如取2),同一迭代内不同DataBlock之间在读取数据时出现一个DataBlock的间隔,如下图所示。 | ||
| 137 | + | ||
| 138 | + | ||
| 139 | + | ||
| 140 | +> 📌 若dst_block_stride == 0,等同于dst_block_stride = 1;若src_block_stride = 0,则源操作数将始终复用第一个dataBlock。 | ||
| 141 | + | ||
| 142 | +下图给出了单次迭代内源操作数与目的操作数在UB空间的读写示例。示例中源操作数的`dataBlockStride`配置为2,表示单次迭代内不同DataBlock间地址步长为2个DataBlock;目的操作数的`dataBlockStride`配置为1,表示单次迭代内地址连续。 | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + | ||
| 146 | +其中**repeatStride**是指相邻迭代间相同DataBlock的地址步长,可通过设置`repeatStride`来灵活控制不同的场景。 | ||
| 147 | + | ||
| 148 | +| 场景 | repeatStride取值 | 数据读取特点 | 适用场景 | | ||
| 149 | +|------|------------------|-------------|--------| | ||
| 150 | +| 连续计算 | 8 | 每轮迭代读取连续8个dataBlock,完成所有输入数据的计算 |  | | ||
| 151 | +| 非连续计算 | >8 (如10) | 相邻迭代间存在dataBlock间隔,地址不连续 |  | | ||
| 152 | +| 反复计算 | 0 | 对首个连续8个dataBlock反复读取和计算 |  | | ||
| 153 | +| 部分重复计算 | (0, 8) | 相邻迭代间部分数据重复读取,一般场景不涉及 |  | | ||
| 154 | + | ||
| 155 | +当 `repeatTime`>1 时,通过多次迭代完成计算。repeatStride表示相邻迭代间相同位置DataBlock的起始地址间隔(以DataBlock为单位)。例如 `repeatStride` = 9 时,第一次迭代的第1个DataBlock与第二次迭代的第1个DataBlock间隔9个DataBlock。 | ||
| 156 | + | ||
| 157 | + | ||
| 158 | + | ||
| 159 | +通过C++ Tensor接口配置 `repeatStride` 和 `dataBlockStride` 实现上述多次迭代功能。 | ||
| 160 | + | ||
| 161 | +```cpp | ||
| 162 | +// 上述示例repeat间隔为9,则通过如下配置可完成对应的操作 | ||
| 163 | +uint16_t mask = 128; | ||
| 164 | +uint8_t repeatTime = 2; | ||
| 165 | +uint8_t dstBlockStride = 1; | ||
| 166 | +uint8_t src0BlockStride = 1; | ||
| 167 | +uint8_t src1BlockStride = 1; | ||
| 168 | +uint8_t dstRepeatStride = 9; | ||
| 169 | +uint8_t src0RepeatStride = 9; | ||
| 170 | +uint8_t src1RepeatStride = 9; | ||
| 171 | + | ||
| 172 | +AscendC::Add(dstLocal, src0Local, src1Local, mask, repeatTime, { dstBlockStride, src0BlockStride, src1BlockStride, dstRepeatStride, src0RepeatStride, src1RepeatStride }); | ||
| 173 | +``` | ||
| 174 | + | ||
| 175 | +#### 掩码设置 | ||
| 176 | + | ||
| 177 | +针对同一个迭代中的数据,可以通过mask参数进行掩码操作来控制实际参与计算的个数。下图为进行Abs计算时通过mask逐比特模式按位控制哪些元素参与计算的示意图,1表示参与计算,0表示不参与计算。 | ||
| 178 | + | ||
| 179 | + | ||
| 180 | + | ||
| 181 | +每一位掩码对应数据中的一个元素的位置,通过有效位和无效位标记,实现对数据操作的精细化开关控制。 | ||
| 182 | +掩码由固定位宽的数值表示:有效位(通常为1):对应元素正常参与计算。无效位(通常为0):对应元素被屏蔽,不执行操作。 | ||
| 183 | + | ||
| 184 | +掩码设置提供了两种模式:**Counter模式**和**Normal模式**。C++ Tensor矢量计算接口通过模板参数`isSetMask`控制掩码由开发者手动设置还是接口内部自动设置。以如下`Adds`接口为例,默认`isSetMask = true`表示由接口内部设置Counter或Normal模式;若开发者需自行控制两种模式的切换,则将`isSetMask`设为`false`。后续示例均以开发者自行控制切换为例。 | ||
| 185 | +```cpp | ||
| 186 | +template <typename T = BinaryDefaultType, bool isSetMask = true, const BinaryConfig& config = DEFAULT_BINARY_CONFIG, typename U, typename S, typename V> | ||
| 187 | +__aicore__ inline void Adds(const U& dst, const S& src0, const V& src1, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams) | ||
| 188 | +``` | ||
| 189 | + | ||
| 190 | +Counter模式用于计算连续N个元素。该模式下,Mask寄存器仅低64bit有效,表示参与计算的元素数量;repeatTime参数被忽略,系统根据元素数量自动计算迭代次数。 | ||
| 191 | +例如需计算200个half类型元素时,此时使用Counter模式,可仅针对200个有效元素执行计算,忽略剩余56个元素的无效空间,避免多余计算。 | ||
| 192 | + | ||
| 193 | + | ||
| 194 | + | ||
| 195 | +```cpp | ||
| 196 | +half scalarValue = 1.0; | ||
| 197 | + | ||
| 198 | +// 1. Direct transfer processing (temporarily omitted) | ||
| 199 | +// 2. Use high-dimensional segmentation API for computation | ||
| 200 | +// 2.1 Set mask counter mode | ||
| 201 | +AscendC::SetMaskCount(); | ||
| 202 | +// 2.2 Set Mask value to 200, indicating this computation processes 200 elements | ||
| 203 | +AscendC::SetVectorMask(0, static_cast<uint64_t>(200)); | ||
| 204 | + | ||
| 205 | +AscendC::Adds<half, false>(dstLocal, src0Local, scalarValue, AscendC::MASK_PLACEHOLDER, 1, { 1, 1, 8, 8 }); | ||
| 206 | + | ||
| 207 | +// 3. Default needs to reset to Normal mode, if subsequent still requires Counter mode then no need to reset to Normal mode | ||
| 208 | +AscendC::SetMaskNormal(); | ||
| 209 | +``` | ||
| 210 | + | ||
| 211 | +Normal计算模式下,MASK寄存器作为每个Repeat的掩码使用,通过SetVectorMask设置mask值,控制单次迭代内参与计算的元素个数。该模式仅高维切分支持,区分为单次迭代内连续模式和单次迭代内逐比特模式。 | ||
| 212 | +> 📌 Normal为默认模式。若未发生模式切换,则无需重复配置该模式。 | ||
| 213 | + | ||
| 214 | +- 单次迭代内连续模式:表示前n个连续元素参与计算。取值范围和操作数的数据类型有关,数据类型不同,每次迭代内能够处理的元素个数最大值不同。 | ||
| 215 | + | ||
| 216 | +> **mask范围推导**:Vector计算单元单次迭代固定处理32B × 8 block = 256B数据,因此mask最大值 = 256B / sizeof(DataType)。当操作数为16位(sizeof=2B)时,mask∈[0, 128](256/2=128);当操作数为32位(sizeof=4B)时,mask∈[0, 64](256/4=64);当操作数为64位(sizeof=8B)时,mask∈[0, 32](256/8=32)。此推导规则可推广至其他数据类型(如bfloat16=16bit→mask上限128,fp32=32bit→mask上限64)。 | ||
| 217 | + | ||
| 218 | +连续模式下,mask指定单次迭代内参与计算的前n个连续元素,超范围按对应数据类型mask最大值计算,mask为0视为NOP(空操作),负值产生未定义行为。例如对shape为256、数据类型为half的src数据(需2次迭代、每次128个元素),设置mask = 100时,第1次迭代计算0, 99、第2次迭代计算128, 227范围的元素,其余元素不参与计算。 | ||
| 219 | + | ||
| 220 | + | ||
| 221 | + | ||
| 222 | +```cpp | ||
| 223 | +AscendC::LocalTensor<half> dstLocal; | ||
| 224 | +AscendC::LocalTensor<half> src0Local; | ||
| 225 | +half scalarValue = 1.0; | ||
| 226 | + | ||
| 227 | +// 1. Data transfer (temporarily omitted) | ||
| 228 | +// 2. Use high-dimensional segmentation API for computation | ||
| 229 | +// 2.1 Set mask Normal mode | ||
| 230 | +AscendC::SetMaskNormal(); | ||
| 231 | +// 2.2 Set Mask value to 100, indicating single iteration computes first 100 elements | ||
| 232 | +AscendC::SetVectorMask(0, static_cast<uint64_t>(100)); | ||
| 233 | +AscendC::Adds<half, false>(dstLocal, src0Local, scalarValue, AscendC::MASK_PLACEHOLDER, 2, { 1, 1, 8, 8 }); | ||
| 234 | +``` | ||
| 235 | + | ||
| 236 | +- 单次迭代内逐比特模式:可以按位控制哪些元素参与计算,bit位的值为1表示参与计算,0表示不参与。分为maskHigh(高位mask)和maskLow(低位mask)。参数取值范围和操作数的数据类型有关,数据类型不同,每次迭代内能够处理的元素个数最大值不同。当操作数为16位时,maskLow、maskHigh∈[0, 2^64-1],并且不同时为0;当操作数为32位时,maskHigh为0,maskLow∈(0, 2^64-1];当操作数为64位时,maskHigh为0,maskLow∈(0, 2^32-1]。 | ||
| 237 | + | ||
| 238 | + 该模式下,可以指定maskHigh和maskLow的值,来处理各种掩码操作。如需进行交错计算,则mask设置成01010101...或者10101010...模式即可。以src数据(shape为256,数据类型为half)为例,通过设置逐bit的掩码,可在单迭代中选取特定bit位的数值参与计算: | ||
| 239 | + | ||
| 240 | +  | ||
| 241 | + | ||
| 242 | + ```cpp | ||
| 243 | + AscendC::LocalTensor<half> dstLocal; | ||
| 244 | + AscendC::LocalTensor<half> src0Local; | ||
| 245 | + AscendC::LocalTensor<half> src1Local; | ||
| 246 | + | ||
| 247 | + // 1. Data transfer (temporarily omitted) | ||
| 248 | + // 2. Use high-dimensional segmentation API for computation | ||
| 249 | + // 2.1 Set mask Normal mode | ||
| 250 | + AscendC::SetMaskNormal(); | ||
| 251 | + // 2.2 Set Mask to bit-wise mode (0101...), implement interleaved computation | ||
| 252 | + AscendC::SetVectorMask(0x000000000000AAAA, 0x0000000000000000); | ||
| 253 | + AscendC::Add<half, false>(dstLocal, src0Local, src1Local, AscendC::MASK_PLACEHOLDER, 2, { 1, 1, 1, 8, 8, 8 }); | ||
| 254 | + ``` | ||
| 255 | + | ||
| 256 | +### 连续计算 | ||
| 257 | + | ||
| 258 | +连续计算是C++ Tensor编程提供的简化计算接口,该能力由软件层面实现。开发者使用该接口无需配置mask掩码,连续计算针对源操作数的连续n个数据进行计算并连续写入目的操作数,解决一维数据的连续计算问题。 | ||
| 259 | + | ||
| 260 | +以Add接口为例,当开发者需要计算4096长度的向量加法,C++ Tensor编程通过如下形式调用: | ||
| 261 | + | ||
| 262 | +```cpp | ||
| 263 | +AscendC::Add(dstLocal, src0Local, src1Local, 4096); | ||
| 264 | +``` | ||
| 265 | + | ||
| 266 | +## Memory矢量计算同步控制 | ||
| 267 | + | ||
| 268 | +AI Core内部执行单元(如MTE2搬运单元、Vector计算单元等)采用异步并行方式运行,当不同单元读写同一存储资源时,易产生数据依赖问题。因此,Memory矢量计算需通过流水同步接口协调执行顺序,确保计算流程正确。Memory矢量计算流程较Cube矩阵计算更为简洁,主要包含三步:数据搬入(Global Memory → UB)、计算(UB)、数据搬出(UB → Global Memory)。三个步骤分别对应PIPE_MTE2、PIPE_V、PIPE_MTE3流水线,需通过核内同步接口协调执行顺序,确保各步骤按序完成。 | ||
| 269 | + | ||
| 270 | +当开发者进行一个矢量加法计算时([NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md)产品),可通过`Mutex::Lock`&`Mutex::Unlock`的方式完成对这三个步骤的同步。[NPU架构版本2201](../../../语言扩展层/SIMD-BuiltIn关键字.md)产品可使用`SetFlag`&`WaitFlag`实现相同同步效果,详见[同步机制](../基于Tensor的CPP编程/CPPTensor编程概述.md#同步机制)。 | ||
| 271 | + | ||
| 272 | +```cpp | ||
| 273 | +__global__ __vector__ void add_kernel(__gm__ float* x, __gm__ float* y, __gm__ float* z) | ||
| 274 | +{ | ||
| 275 | + uint8_t mutex_id = 1; | ||
| 276 | + AscendC::LocalMemAllocator<AscendC::Hardware::UB> ubAllocator; | ||
| 277 | + AscendC::LocalTensor<float> xLocal = ubAllocator.Alloc<float, 48>(); | ||
| 278 | + AscendC::LocalTensor<float> yLocal = ubAllocator.Alloc<float, 48>(); | ||
| 279 | + AscendC::LocalTensor<float> zLocal = ubAllocator.Alloc<float, 48>(); | ||
| 280 | + | ||
| 281 | + AscendC::GlobalTensor<float> xGlobal, yGlobal, zGlobal; | ||
| 282 | + xGlobal.SetGlobalBuffer((__gm__ float*)x); | ||
| 283 | + yGlobal.SetGlobalBuffer((__gm__ float*)y); | ||
| 284 | + zGlobal.SetGlobalBuffer((__gm__ float*)z); | ||
| 285 | + | ||
| 286 | + // ... | ||
| 287 | + // 1. Step 1: Data transfer in, execution pipeline is PIPE_MTE2 | ||
| 288 | + AscendC::Mutex::Lock<PIPE_MTE2>(mutex_id); | ||
| 289 | + AscendC::DataCopy(xLocal, xGlobal, 48); | ||
| 290 | + AscendC::DataCopy(yLocal, yGlobal, 48); | ||
| 291 | + AscendC::Mutex::Unlock<PIPE_MTE2>(mutex_id); | ||
| 292 | + | ||
| 293 | + // 2. Step 2: Compute, execution pipeline is PIPE_V | ||
| 294 | + AscendC::Mutex::Lock<PIPE_V>(mutex_id); | ||
| 295 | + AscendC::Add(zLocal, xLocal, yLocal, 48); | ||
| 296 | + AscendC::Mutex::Unlock<PIPE_V>(mutex_id); | ||
| 297 | + | ||
| 298 | + // 3. Step 3: Data transfer out, execution pipeline is PIPE_MTE3 | ||
| 299 | + AscendC::Mutex::Lock<PIPE_MTE3>(mutex_id); | ||
| 300 | + AscendC::DataCopy(zGlobal, zLocal, 48); | ||
| 301 | + AscendC::Mutex::Unlock<PIPE_MTE3>(mutex_id); | ||
| 302 | +} | ||
| 303 | +``` | ||
| 304 | + | ||
| 305 | +## Memory矢量计算示例 | ||
| 306 | + | ||
| 307 | +结合上文介绍的Memory矢量数据搬运与计算能力,开发者可基于C++ Tensor编程接口实现完整的Memory矢量计算算子。具体开发流程与代码示例可参考[cpp_api_add样例](https://gitcode.com/cann/asc-devkit/blob/master/examples/01_simd_cpp_api/00_introduction/01_add/add/README.md),该样例完整展示了矢量加法算子的开发、编译与验证流程。 | ||
| @@ -14,7 +14,7 @@ AI Core采用分级存储架构,不同计算单元的编程视角有所差异 | |||
| 14 | 14 | ||
| 15 | **Vector矢量计算**:传统架构采用「Global Memory → UB」两级层级;[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md)引入寄存器后构建「Global Memory → Unified Buffer → Register」三级层级。Global Memory存放输入输出数据,Unified Buffer作为矢量计算数据中间缓存,Register位于Vector计算单元最内层,直接与矢量计算执行单元交互。 | 15 | **Vector矢量计算**:传统架构采用「Global Memory → UB」两级层级;[NPU架构版本3510](../../../语言扩展层/SIMD-BuiltIn关键字.md)引入寄存器后构建「Global Memory → Unified Buffer → Register」三级层级。Global Memory存放输入输出数据,Unified Buffer作为矢量计算数据中间缓存,Register位于Vector计算单元最内层,直接与矢量计算执行单元交互。 |
| 16 | 16 | ||
| 17 | -<img src="../../../../figures/AI_Core存储层次结构_c.png" alt="AI Core存储层次结构" width="800" /> | 17 | +<img src="../../../../figures/AI_Core存储层次结构.png" alt="AI Core存储层次结构" width="800" /> |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | ### 外部存储(Global Memory) | 20 | ### 外部存储(Global Memory) |
| @@ -42,10 +42,10 @@ | |||
| 42 | constexpr uint32_t xAddr = 0; | 42 | constexpr uint32_t xAddr = 0; |
| 43 | constexpr uint32_t yAddr = xAddr + DATA_COUNT * sizeof(half); | 43 | constexpr uint32_t yAddr = xAddr + DATA_COUNT * sizeof(half); |
| 44 | 44 | ||
| 45 | - // 核函数入口:使用__vector__显式声明Vector核函数,搭配静态Tensor方式编程 | 45 | + // 核函数入口:使用__vector__显式声明Vector核函数,搭配C++ Tensor方式编程 |
| 46 | __vector__ __global__ void ln_custom(__gm__ uint8_t* src, __gm__ uint8_t* dst) | 46 | __vector__ __global__ void ln_custom(__gm__ uint8_t* src, __gm__ uint8_t* dst) |
| 47 | { | 47 | { |
| 48 | - // 静态Tensor需要手动调用InitSocState初始化全局状态寄存器 | 48 | + // C++ Tensor需要手动调用InitSocState初始化全局状态寄存器 |
| 49 | AscendC::InitSocState(); | 49 | AscendC::InitSocState(); |
| 50 | 50 | ||
| 51 | AscendC::GlobalTensor<half> srcGlobal; | 51 | AscendC::GlobalTensor<half> srcGlobal; |
| @@ -53,7 +53,7 @@ | |||
| 53 | srcGlobal.SetGlobalBuffer((__gm__ half*)src, DATA_COUNT); | 53 | srcGlobal.SetGlobalBuffer((__gm__ half*)src, DATA_COUNT); |
| 54 | dstGlobal.SetGlobalBuffer((__gm__ half*)dst, DATA_COUNT); | 54 | dstGlobal.SetGlobalBuffer((__gm__ half*)dst, DATA_COUNT); |
| 55 | 55 | ||
| 56 | - // 直接构造指定地址和存储位置的LocalTensor(静态Tensor方式) | 56 | + // 直接构造指定地址和存储位置的LocalTensor(C++ Tensor方式) |
| 57 | AscendC::LocalTensor<half> srcLocal(AscendC::TPosition::VECCALC, xAddr, DATA_COUNT); | 57 | AscendC::LocalTensor<half> srcLocal(AscendC::TPosition::VECCALC, xAddr, DATA_COUNT); |
| 58 | AscendC::LocalTensor<half> dstLocal(AscendC::TPosition::VECCALC, yAddr, DATA_COUNT); | 58 | AscendC::LocalTensor<half> dstLocal(AscendC::TPosition::VECCALC, yAddr, DATA_COUNT); |
| 59 | 59 | ||


🚨 [P0] [PG-1-ISA校验] 分形粒度"16×16"需明确仅适用于元素大小≤2B的数据类型
文中描述"16×16是矩阵计算单元(fractal)的基本粒度",但16×16仅对half/int8等≤2B元素成立。对于fp32等4B元素,分形形状可能不同(L0A/L0B的单fractal占用更大空间)。示例代码中同时使用了half和float类型,必须明确说明分形粒度与数据类型的对应关系,否则开发者可能对float场景使用错误的16×16分形。
(此意见由AI提出,评审规则PG-1。如有异议,请回复此评论,让我变得更好:))