triton.language.atomic_max

目录

triton.language.atomic_max#

triton.language.atomic_max(pointer, val, mask=None, sem=None, scope=None, _semantic=None)#

Performs an atomic max at the memory location specified by pointer.

Return the data stored at pointer before the atomic operation.

参数:
  • pointer (Block of dtype=triton.PointerDType) -- The memory locations to operate on

  • val (Block of dtype=pointer.dtype.element_ty) -- The values with which to perform the atomic operation

  • sem (str, optional) -- Specifies the memory semantics for the operation. Acceptable values are "acquire", "release", "acq_rel" (stands for "ACQUIRE_RELEASE"), and "relaxed". If not provided, the function defaults to using "acq_rel" semantics.

  • scope (str, optional) -- Defines the scope of threads that observe the synchronizing effect of the atomic operation. Acceptable values are "gpu" (default), "cta" (cooperative thread array, thread block), or "sys" (stands for "SYSTEM"). The default value is "gpu".

This function can also be called as a member function on tensor, as x.atomic_max(...) instead of atomic_max(x, ...).

示例

import triton
import triton.language as tl
import torch


@triton.jit
def triton_atomic_max(in_ptr0, out_ptr0, n_elements: tl.constexpr, BLOCK_SIZE: tl.constexpr):
    xoffset = tl.program_id(0) * BLOCK_SIZE
    xindex = xoffset + tl.arange(0, BLOCK_SIZE)[:]
    yindex = xoffset + tl.arange(0, BLOCK_SIZE)[:]
    xmask = xindex < n_elements
    x0 = xindex
    x1 = yindex
    tmp0 = tl.load(in_ptr0 + (x0), xmask)
    tmp1 = tl.atomic_max(out_ptr0 + (x1), tmp0, xmask)


def test_atomic_max():
    dtype = 'int32'
    shape = (3, 1)

    x0 = torch.randint(low=0, high=2000, size=shape, dtype=eval('torch.' + dtype)).npu()
    x1 = torch.randint(low=0, high=2000, size=shape, dtype=eval('torch.' + dtype)).npu()

    x1_ref = torch.maximum(x0, x1)

    n_elements = shape[0] * shape[1]
    triton_atomic_max[shape[0], 1, 1](x0, x1, n_elements, BLOCK_SIZE=shape[1])
    assert torch.equal(x1, x1_ref)


if __name__ == "__main__":
    test_atomic_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

×

×

×

×

√

√

√

√

×

√

×

×

×

×

×

特殊说明

  • sem: "acquire","release","relaxed" are not supported

  • scope: "cta","sys" are not supported