triton.language.mul

triton.language.mul(x, y, sanitize_overflow: constexpr = True, _semantic=None)

Computes the element-wise product of x and y.

This is the function form of the * operator.

参数:
  • x (Block) -- the first input tensor

  • y (Block) -- the second input tensor

  • sanitize_overflow (bool) -- insert an integer-overflow check when overflow sanitization is enabled at compile time; set to False to emit plain wrapping arithmetic. Ignored for floating-point operands.

示例

import triton
import triton.language as tl
import torch


def torch_mul(x0, x1):
    res = x0 * x1
    return res


@triton.jit
def triton_mul(in_ptr0, in_ptr1, out_ptr0, XBLOCK: tl.constexpr, XBLOCK_SUB: tl.constexpr):
    offset = tl.program_id(0) * XBLOCK
    base1 = tl.arange(0, XBLOCK_SUB)
    loops1: tl.constexpr = (XBLOCK + XBLOCK_SUB - 1) // XBLOCK_SUB
    for loop1 in range(loops1):
        x0_prime = offset + (loop1 * XBLOCK_SUB) + base1
        x0 = offset + (loop1 * XBLOCK_SUB) + base1
        tmp0 = tl.load(in_ptr0 + (x0), None)
        tmp1 = tl.load(in_ptr1 + (x0), None)
        tmp2 = tmp0 * tmp1
        tl.store(out_ptr0 + (x0), tmp2, None)


def test_mul():
    param_list = ['float32', (2, 4096, 8), 2, 32768, 1024]
    dtype, shape, ncore, xblock, xblock_sub = param_list
    x0 = torch.randn(size=shape, dtype=eval('torch.' + dtype)).npu()
    x1 = torch.randn(size=shape, dtype=eval('torch.' + dtype)).npu()

    torch_res = torch_mul(x0, x1)
    triton_res = torch.empty_like(x0)
    triton_mul[ncore, 1, 1](x0, x1, triton_res, xblock, xblock_sub)

    torch.testing.assert_close(torch_res, triton_res, rtol=1e-04, atol=1e-04, equal_nan=True)


if __name__ == '__main__':
    test_mul()

数据类型支持

平台

uint8

int8

uint16

int16

uint32

int32

uint64

int64

fp16

fp32

fp64

bf16

fp8e(e4m3)

fp8e5(e5m2)

bool

Ascend A2/A3

×

×

×

Ascend 950

×