当前位置: 首页 > news >正文

CANN/asc-devkit随机数生成API

PhiloxRandom

【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit

产品支持情况

产品

是否支持

Ascend 950PR/Ascend 950DT

Atlas A3 训练系列产品 / Atlas A3 推理系列产品

x

Atlas A2 训练系列产品 / Atlas A2 推理系列产品

x

功能说明

基于Philox随机数生成算法,给定随机数种子,生成若干的随机数。

Philox随机数生成的核心算法是一个基于记数的伪随机数生成算法,输入为一个128bit的记数器C,两个32bit的key(k0和k1),输出为4个32bit的整数。

函数原型

  • 连续模式

    template <uint16_t Rounds = 7, typename T> __aicore__ inline void PhiloxRandom(const LocalTensor<T>& dstLocal, const PhiloxKey& philoxKey, const PhiloxCounter& philoxCounter, uint16_t count)
  • stride模式

    template <uint16_t Rounds = 7, typename T> __aicore__ inline void PhiloxRandom(const LocalTensor<T>& dstLocal, const PhiloxKey& philoxKey, const PhiloxCounter& philoxCounter, const PhiloxRandomParams& params)

参数说明

表 1模板参数说明

参数名

描述

Rounds

Philox算法内部实现迭代次数,支持取值7或10。

T

目的操作数数据类型,支持的数据类型为:uint32_t、int32_t、float。

其中uint32_t/int32_t为数据类型范围内的均匀分布,float为0-1范围内的均匀分布。

表 2参数说明

参数名

输入/输出

描述

dstLocal

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

LocalTensor的起始地址需要32字节对齐。

philoxKey

输入

随机数种子。两个32bit的key,定义如下:

using PhiloxKey = uint32_t[2];

philoxCounter

输入

随机数种子。一个128bit的记数器C(由4个32bit组成),定义如下:

using PhiloxCounter = uint32_t[4];

count

输入

生成目的操作数的元素个数。

params

输入

stride模式计算所需的参数信息。PhiloxRandomParams类型,定义如下:

struct PhiloxRandomParams { uint32_t stride; // 两行元素之间的间隔 uint32_t row; // 表示生成的行数 uint32_t column; // 表示生成的每一行的元素个数 }
  • row * column大于0,不大于LocalTensor的大小。
  • column % 4 == 0,stride % 4 == 0,stride >= column。

图 1PhiloxRandom示意图
![](https://raw.gitcode.com/cann/asc-devkit/raw/f35dfef9af78af31a0418d6061f7969c5ffc6990/docs/api/context/figures/PhiloxRandom示意图.png "PhiloxRandom示意图"?utm_source=gitcode_repo_files)

上图是一个生成随机数的示意图。

  • 连续模式下使用philoxCounter={0, 0, 0, 0},count=32来生成32个随机数。
  • stride模式下可按列分两次生成,调用两次接口。第一次调用参数为philoxCounter={0, 0, 0, 0},stride=8,row=4,column=4;第二次调用参数为philoxCounter={1, 0, 0, 0}(每次记数器C自增会生成128bit的随机数),stride=8,row=4,column=4。

返回值说明

约束说明

调用示例

  • 接口使用样例

    // philoxKey={0,0}, philoxCounter={0,0,0,0}, params={1024, 8, 1024} LocalTensor<uint32_t> dstLocal = outQueue.AllocTensor<uint32_t>(); PhiloxRandom<7>(dstLocal, {0, 0}, {0, 0, 0, 0},{1024, 8, 1024});
  • 完整样例

    #include "kernel_operator.h" template <uint16_t Rounds, typename srcType> class KernelPhiloxStride { public: __aicore__ inline KernelPhiloxStride() {} __aicore__ inline void Init(GM_ADDR dstGm, uint32_t paramStride, uint32_t paramRow, uint32_t paramColumn) { stride = paramStride; row = paramRow; column = paramColumn; count = row * column; const int alginSize = AscendC::GetDataBlockSizeInBytes() / sizeof(srcType); dstSize = (count + alginSize - 1) / alginSize * alginSize; dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ srcType *>(dstGm), dstSize); pipe.InitBuffer(outQueue, 1, dstSize * sizeof(srcType)); } __aicore__ inline void Process(uint32_t seed0, uint32_t seed1, uint32_t seed2, uint32_t seed3, uint32_t seed4, uint32_t seed5) { Compute(seed0, seed1, seed2, seed3, seed4, seed5); CopyOut(); } private: __aicore__ inline void Compute(uint32_t seed0, uint32_t seed1, uint32_t seed2, uint32_t seed3, uint32_t seed4, uint32_t seed5) { AscendC::LocalTensor<srcType> dstLocal = outQueue.AllocTensor<srcType>(); AscendC::PhiloxRandom<Rounds>(dstLocal, { seed0, seed1 }, { seed2, seed3, seed4, seed5 }, { stride, row, column }); outQueue.EnQue<srcType>(dstLocal); } __aicore__ inline void CopyOut() { AscendC::LocalTensor<srcType> dstLocal = outQueue.DeQue<srcType>(); AscendC::DataCopy(dstGlobal, dstLocal, dstSize); outQueue.FreeTensor(dstLocal); } private: AscendC::GlobalTensor<srcType> dstGlobal; AscendC::TPipe pipe; AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueue; uint32_t count; uint32_t stride; uint32_t row; uint32_t column; uint32_t dstSize; }; extern "C" __global__ __aicore__ void philox_kernel_stride(GM_ADDR dstGm) { KernelPhiloxStride<7, uint32_t> op; op.Init(dstGm, 1024, 8, 1024); op.Process(0, 0, 0, 0, 0, 0); }

【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit

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

http://www.jsqmd.com/news/793472/

相关文章:

  • 百度网盘直链解析:告别限速,实现免费高速下载的终极方案
  • 互联网音频播放器技术演进与Xilinx可编程逻辑应用
  • 鸿蒙一气总论(十)
  • CANN算子库幂运算API文档
  • AnsiWeather Unicode符号和ANSI色彩完全指南:终端天气显示的终极解决方案
  • 前端面试vue
  • CTFd与MCP协议集成:AI智能体赋能CTF赛事自动化运维
  • C# Winform窗体程序自重启:从Application.Restart到进程管理的进阶实践
  • Vibe-Coding:开源AI编码助手部署与深度集成指南
  • 如何永久保存微信聊天记录?5步实现数据自主管理
  • AI辅助生殖:多模态数据融合与深度学习在胚胎评估中的应用
  • Chapter用户权限系统详解:5种角色权限配置与最佳实践
  • CommentCoreLibrary数据格式完全指南:AcFun、Bilibili、CommonDanmaku格式解析
  • CANN/asc-devkit半精度转无符号整数函数
  • 08-方法
  • AI-Trader团队评分系统:评估AI代理协作表现的科学方法
  • ReportPortal故障排除:常见部署问题和解决方案大全
  • 5分钟快速上手slua-unreal:从零开始构建你的第一个Lua Actor
  • 鸿蒙一气总论(八)
  • CANN/Ascend C矩阵乘法策略API
  • Lustre状态管理完全教程:Erlang与Elm灵感的完美结合
  • AI知识库构建实战:从RAG原理到企业级应用部署
  • mitojs高级配置与Hook机制:如何实现高度定制化监控
  • 聊天插件SDK开发指南:从架构设计到实战部署
  • AI代码助手安全规则实战:从SQL注入防护到隐私合规
  • mckays-app-template支付系统详解:Stripe集成与订阅管理实战指南
  • CANN/asc-devkit Query API文档
  • CANN/ge获取输入格式API
  • Mentalist安全使用规范:合法渗透测试中的字典生成最佳实践
  • Boomerang性能监控最佳实践:20个提升网站速度的关键策略