triton.language.rand
1. OP 概述
简介:给定 1 个 seed 标量和 1 个 offset 块,返回 1 个 在 U(0,1) 中的 float32 类型的随机块。 原型:
triton.language.rand(
seed,
offset,
n_rounds: constexpr = 10
)
2. OP 规格
2.1 参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
seed |
int或 tensor |
用于生成随机数的种子 |
offset |
int或 tensor |
用于生成随机数的偏移量 |
n_rounds |
constexpr,默认值为10 |
Philox 算法的迭代轮数 |
返回值:
1 个 float32 类型的随机块,其shape与offset的shape相同,其值在 [0.0, 1.0) 区间内均匀分布
2.2 支持规格
2.2.1 DataType 支持
输入seed的type:
| int8 | int16 | int32 | uint8 | uint16 | uint32 | uint64 | int64 | fp16 | fp32 | fp64 | bf16 | bool | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Ascend A2/A3 | √ | √ | √ | √ | √ | √ | √ | √ | × | × | × | × | √ |
2.2.2 Shape 支持
无特殊要求
2.3 特殊限制说明
相对社区能力暂不支持
2.4 使用方法
以下示例实现了对rand的调用:
@triton.jit
def kernel_rand(x_ptr, n_rounds: tl.constexpr, N: tl.constexpr, XBLOCK: tl.constexpr):
block_offset = tl.program_id(0) * XBLOCK
block_size = XBLOCK if block_offset + XBLOCK <= N else N - block_offset
for inner_idx in range(block_size):
global_offset = block_offset + inner_idx
rand_vals = tl.rand(5, 10 + global_offset, n_rounds) # 对每个索引生成一个随机数
tl.store(x_ptr + global_offset, rand_vals) # 存储随机数
y_calf = torch.zeros(shape, dtype=eval('torch.float32')).npu()
numel = y_calf.numel()
ncore = 1 if numel < 32 else 32
xblock = math.ceil(numel / ncore)
kernel_rand[ncore, 1, 1](y_calf, 10, numel, xblock)