triton.language.max#
- triton.language.max(input, axis=None, return_indices=False, return_indices_tie_break_left=True, keep_dims=False)#
Returns the maximum of all elements in the
inputtensor along the providedaxis- 参数:
input (Tensor) -- the input values
axis (int) -- the dimension along which the reduction should be done. If None, reduce all dimensions
keep_dims (bool) -- if true, keep the reduced dimensions with length 1
return_indices (bool) -- if true, return index corresponding to the maximum value
return_indices_tie_break_left (bool) -- if true, in case of a tie (i.e., multiple elements have the same maximum value), return the left-most index for values that aren't NaN
This function can also be called as a member function on
tensor, asx.max(...)instead ofmax(x, ...).示例
import torch import torch_npu import triton import triton.language as tl @triton.jit def triton_max_2d(in_ptr0, out_ptr0, dim: tl.constexpr, M: tl.constexpr, N: tl.constexpr, MNUMEL: tl.constexpr, NNUMEL: tl.constexpr): mblk_idx = tl.arange(0, MNUMEL) nblk_idx = tl.arange(0, NNUMEL) mmask = mblk_idx < M nmask = nblk_idx < N mask = (mmask[:, None]) & (nmask[None, :]) idx = mblk_idx[:, None] * N + nblk_idx[None, :] x = tl.load(in_ptr0 + idx, mask=mask, other=-float('inf')) tmp4 = tl.max(x, dim) if dim == 0: tl.store(out_ptr0 + tl.arange(0, N), tmp4, None) else: tl.store(out_ptr0 + tl.arange(0, M), tmp4, None) def test_max(): M, N, dim = 4, 8, 1 x = torch.randn(M, N, dtype=torch.float32).npu() out = torch.empty(M, dtype=torch.float32).npu() triton_max_2d[1, 1, 1](x, out, dim, M, N, M, N) ref = torch.max(x, dim=dim)[0] assert torch.allclose(out.cpu(), ref.cpu()), "max result mismatch" print("max result:", out) if __name__ == "__main__": test_max()
数据类型支持
uint8
int8
uint16
int16
uint32
int32
uint64
int64
fp16
fp32
fp64
bf16
fp8e(e4m3)
fp8e5(e5m2)
bool
Atlas A2 products/Atlas A3 products
√
√
×
√
×
√
×
√
√
√
×
√
×
×
√
Ascend 950PR&950DT products
√
√
√
√
√
√
√
√
√
√
×
√
×
×
√
特殊说明
return_indices: return_indices=True is not supported when axis=None.keep_dims=Truerequires more test coverage; currently verified for 3D tensor with dim=2.