asc_storeunalign_post_postupdate
产品支持情况
- Ascend 950PR/Ascend 950DT:支持
- Atlas A3 训练系列产品/Atlas A3 推理系列产品:不支持
- Atlas A2 训练系列产品/Atlas A2 推理系列产品:不支持
- Atlas 200I/500 A2 推理产品:不支持
- Atlas 推理系列产品AI Core:不支持
- Atlas 推理系列产品Vector Core:不支持
- Atlas 训练系列产品:不支持
功能说明
头文件路径为:"c_api/reg_compute/store/storeunalign.h"。
将非对齐寄存器中暂存的尾块写入Unified Buffer(UB),用于搬出asc_storeunalign_postupdate立即数偏移搬出模式产生的非对齐寄存器。启用Post Update模式在搬出后自动累加目的地址指针,便于硬件循环内连续多次调用时无需手动维护目的地址。搬出结束后,目的地址向后偏移立即数offset个元素。
本接口在Vector Function(__simd_vf__ 标记的函数)内使用。本接口仅在AIV上生效。
函数原型
C
// 占位符形式
__simd_callee__ inline void asc_storeunalign_post_postupdate(__ubuf__ <dtype>*& dst,
vector_store_unalign unalign_reg,
int32_t offset)
dtype支持数据类型
dtype支持的数据类型为int4b_t、int8_t、uint8_t、fp4x2_e2m1_t、fp4x2_e1m2_t、hifloat8_t、fp8_e8m0_t、fp8_e5m2_t、fp8_e4m3fn_t、int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float、int64_t。
函数原型典型示例
C
__simd_callee__ inline void asc_storeunalign_post_postupdate(__ubuf__ uint8_t*& dst,
vector_store_unalign unalign_reg,
int32_t offset)
参数说明
表1 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| dst | 输入/输出 | 目的操作数的UB地址,类型为__ubuf__ <dtype>*&。通常由前序asc_storeunalign_postupdate更新到搬出区间结束地址。 |
| unalign_reg | 输入 | 非对齐寄存器,类型为vector_store_unalign,长度为32字节。须与前序接口使用同一个寄存器。 |
| offset | 输入 | 偏移量,类型为int32_t,单位为元素。指令搬出后,dst向后偏移offset个元素,若dst已指向结束地址时设置为0。 |
非对齐寄存器的详细说明请参见reg数据类型定义。
返回值说明
无
约束说明
通用约束
- 本接口仅在AIV上生效,非AIV调用直接返回。
- 调用本接口前,须调用asc_storeunalign_postupdate的立即数偏移搬出模式,并复用同一个
unalign_reg。 dst无需32字节对齐,但须按sizeof(dtype)字节对齐。- UB容量上限为256KB,用户可用容量随编译选项与编程场景变化(默认预留6KB SIMD VF栈+2KB Ascend C预留,可用248KB;SIMD+SIMT混编时再划分32KB~128KB作Data Cache,可用容量进一步减少)。目的操作数地址更新后不可超过实际可用容量,否则会报错。
- 如果本指令与其他指令存在UB地址重叠,需要插入同步指令asc_mem_bar,保证多个指令串行化,防止出现异常数据。
指令约束
- 调用本接口之前,需要调用asc_storeunalign_postupdate立即数偏移搬出模式,且本接口与前序接口的非对齐寄存器要保持一致。
调用示例
将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。
以Ascend 950PR/Ascend 950DT产品(对应NPU架构为dav-3510)为例,编译运行命令如下:
Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C++
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"
namespace {
constexpr uint32_t ELEMENT_COUNT = 256;
constexpr uint32_t START_OFFSET = 8;
constexpr uint32_t OUTPUT_BYTES = START_OFFSET + ELEMENT_COUNT;
__simd_vf__ inline void post_postupdate_vf(__ubuf__ uint8_t* output, __ubuf__ uint8_t* input)
{
vector_uint8_t src;
vector_store_unalign unalign_reg;
__ubuf__ uint8_t* dst = output + START_OFFSET;
asc_loadalign(src, input);
asc_storeunalign_postupdate(dst, unalign_reg, src, ELEMENT_COUNT);
asc_storeunalign_post_postupdate(dst, unalign_reg, 0);
}
__global__ __vector__ void post_postupdate_kernel(__gm__ uint8_t* output, __gm__ uint8_t* input)
{
asc_init();
__ubuf__ uint8_t output_local[288];
__ubuf__ uint8_t input_local[ELEMENT_COUNT];
asc_copy_gm2ub_align(input_local, input, ELEMENT_COUNT);
asc_sync_notify(PIPE_MTE2, PIPE_V, EVENT_ID0);
asc_sync_wait(PIPE_MTE2, PIPE_V, EVENT_ID0);
post_postupdate_vf(output_local, input_local);
asc_sync_notify(PIPE_V, PIPE_MTE3, EVENT_ID0);
asc_sync_wait(PIPE_V, PIPE_MTE3, EVENT_ID0);
asc_copy_ub2gm_align(output, output_local, OUTPUT_BYTES);
asc_sync();
}
} // namespace
int main()
{
std::vector<uint8_t> input(ELEMENT_COUNT);
std::vector<uint8_t> output(OUTPUT_BYTES, 0);
for (uint32_t i = 0; i < ELEMENT_COUNT; ++i) input[i] = static_cast<uint8_t>(i);
aclInit(nullptr);
aclrtSetDevice(0);
uint8_t* input_device = nullptr;
uint8_t* output_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&input_device), ELEMENT_COUNT, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&output_device), OUTPUT_BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(input_device, ELEMENT_COUNT, input.data(), ELEMENT_COUNT, ACL_MEMCPY_HOST_TO_DEVICE);
post_postupdate_kernel<<<1, 0>>>(output_device, input_device);
aclrtSynchronizeDevice();
aclrtMemcpy(output.data(), OUTPUT_BYTES, output_device, OUTPUT_BYTES, ACL_MEMCPY_DEVICE_TO_HOST);
bool passed = true;
for (uint32_t i = 0; i < ELEMENT_COUNT; ++i) passed = passed && output[START_OFFSET + i] == input[i];
std::cout << (passed ? "[Success] asc_storeunalign_post_postupdate completed."
: "[Failed] asc_storeunalign_post_postupdate output mismatch.") << std::endl;
aclrtFree(input_device);
aclrtFree(output_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}