triton.language.randint#
- triton.language.randint(seed, offset, n_rounds: constexpr = constexpr[10])#
Given a
seedscalar and anoffsetblock, returns a single block of randomint32.If you need multiple streams of random numbers, using randint4x is likely to be faster than calling randint 4 times.
- Parameters:
seed – The seed for generating random numbers.
offset – The offsets to generate random numbers for.
Example
import triton import triton.language as tl import torch import math @triton.jit def kernel_randint(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.randint(5, 10 + global_offset, n_rounds) # Generate a random number for each index tl.store(x_ptr + global_offset, rand_vals) # Store the random number def test_randint(): shape = (1, 3) y_cali = torch.zeros(shape, dtype=eval('torch.int32')).npu() numel = y_cali.numel() ncore = 1 if numel < 32 else 32 xblock = math.ceil(numel / ncore) kernel_randint[ncore, 1, 1](y_cali, 10, numel, xblock) if __name__ == "__main__": test_randint()
DataType Support
平台
uint8
int8
uint16
int16
uint32
int32
uint64
int64
fp16
fp32
fp64
bf16
fp8e(e4m3)
fp8e5(e5m2)
bool
Ascend A2/A3
√
√
√
√
√
√
√
√
×
×
×
×
×
×
√
Ascend 950
√
√
√
√
√
√
√
√
×
×
×
×
×
×
√