asc_reduce_max
产品支持情况
- 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/compute/reg_reduce.h"。
根据mask对源操作数src进行归约最大值操作,得到归约结果。结果保存在dst的第0个元素,最大值在src中的索引原始位模式保存在dst的第1个元素,dst中的其他元素置0。如果存在多个最大值,则保留最小的索引。计算公式如下:
函数原型
C
// 通过函数返回值返回结果(占位符形式)
__simd_callee__ inline vector_<dtype> asc_reduce_max(vector_<dtype> src,
vector_bool mask)
// 通过引用参数输出结果(占位符形式)
__simd_callee__ inline void asc_reduce_max(vector_<dtype>& dst,
vector_<dtype> src,
vector_bool mask)
dtype支持数据类型
dtype取值为:int16_t、uint16_t、half、int32_t、uint32_t、float。
函数原型典型示例
C
// 示例:对int16_t矢量数据寄存器执行归约最大值操作
__simd_callee__ inline void asc_reduce_max(vector_int16_t& dst,
vector_int16_t src,
vector_bool mask)
参数说明
表1 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| dst | 输出 | 目的操作数(矢量数据寄存器)。 |
| src | 输入 | 源操作数(矢量数据寄存器)。 |
| mask | 输入 | 掩码寄存器,用于控制各元素是否参与归约。 |
矢量数据寄存器和掩码寄存器的详细说明请参见reg数据类型定义。
返回值说明
- 通过引用参数输出结果:归约结果写入
dst。 - 通过函数返回值返回结果:返回归约结果,类型为矢量数据寄存器,与
src的数据类型一致。
约束说明
- 通过引用参数输出结果的函数原型在非AIV上调用时直接返回。
- 通过函数返回值输出结果的函数原型在非AIV上调用时返回对应矢量类型的默认构造值。
mask需通过掩码设置接口预先赋值后再传入,未赋值的掩码寄存器内容不确定,会导致有效元素位置错误。- 未被
mask选中的元素被视为对应数据类型的最小值,浮点数类型的最小值为-inf。如果src中的所有元素均未被mask选中,则将该最小值写入dst的第0个元素,并将其余元素置0。 - 比较时遵循
。 - 如果输入数据中存在nan,则将nan写入
dst的第0个元素,并将第一个nan的索引写入dst的第1个元素。
关键特性
规约产生值+索引两个结果,索引值需要强制类型转换:
dst的索引按照dst的数据类型存储,比如dst为half类型时,索引按照half类型存储,因此读取索引需要使用 reinterpret_cast方法转换到整数类型。若数据类型是half,需要使用reinterpret_cast<uint16_t*>;若数据类型是float,需要使用reinterpret_cast<uint32_t*>。值+索引的分别提取方式请参见调用示例。
调用示例
将以下代码保存为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++
// 本例使用half类型演示归约结果"值+索引"的分别提取,
// 归约结果中dst的第0个元素为最大值,第1个元素为最大值索引的原始位模式。
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"
namespace {
constexpr uint32_t ELEMENT_COUNT = 64;
__simd_vf__ inline void reduce(__ubuf__ half* dst, __ubuf__ half* src)
{
vector_half dst_reg, src_reg;
uint32_t count = ELEMENT_COUNT;
vector_bool mask = asc_update_mask_b16(count);
asc_loadalign(src_reg, src);
asc_reduce_max(dst_reg, src_reg, mask);
asc_storealign(dst, dst_reg, mask);
}
__global__ __vector__ void asc_reduce_max_kernel(__gm__ half* dst, __gm__ half* src)
{
asc_init();
__ubuf__ half dst_local[ELEMENT_COUNT], src_local[ELEMENT_COUNT];
asc_copy_gm2ub_align(src_local, src, ELEMENT_COUNT * sizeof(half));
asc_sync_notify(PIPE_MTE2, PIPE_V, EVENT_ID0);
asc_sync_wait(PIPE_MTE2, PIPE_V, EVENT_ID0);
reduce(dst_local, src_local);
asc_sync_notify(PIPE_V, PIPE_MTE3, EVENT_ID0);
asc_sync_wait(PIPE_V, PIPE_MTE3, EVENT_ID0);
asc_copy_ub2gm_align(dst, dst_local, ELEMENT_COUNT * sizeof(half));
asc_sync();
}
} // namespace
int main()
{
std::vector<half> src(ELEMENT_COUNT), output(ELEMENT_COUNT, half(0));
for (uint32_t i = 0; i < ELEMENT_COUNT; ++i) {
src[i] = half(static_cast<float>(i) + 1.0f); // 1, 2, ..., 64
}
aclInit(nullptr);
aclrtSetDevice(0);
half* src_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&src_device), (ELEMENT_COUNT) * sizeof(half),
ACL_MEM_MALLOC_HUGE_FIRST);
half* dst_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&dst_device), (ELEMENT_COUNT) * sizeof(half),
ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(src_device, src.size() * sizeof(half), src.data(), src.size() * sizeof(half),
ACL_MEMCPY_HOST_TO_DEVICE);
asc_reduce_max_kernel<<<1, 0>>>(dst_device, src_device);
aclrtSynchronizeDevice();
aclrtMemcpy(output.data(), output.size() * sizeof(half), dst_device, output.size() * sizeof(half),
ACL_MEMCPY_DEVICE_TO_HOST);
half max_value = output[0]; // 提取最大值
uint16_t max_index = *reinterpret_cast<uint16_t*>(&output[1]); // 提取最大值索引:索引按half类型存储,需强制转换到整数类型
const bool passed = (static_cast<float>(max_value) == 64.0f) && (max_index == ELEMENT_COUNT - 1);
std::cout << (passed ? "[Success] asc_reduce_max passed." : "[Failed] asc_reduce_max failed.") << std::endl;
aclrtFree(dst_device);
aclrtFree(src_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}