triton.language.view#
- triton.language.view(input, *shape, _semantic=None)#
Returns a tensor with the same elements as input but a different shape. The order of the elements may not be preserved.
- 参数:
input (Block) -- The input tensor.
shape -- The desired shape.
shapecan be passed as a tuple or as individual parameters:# These are equivalent view(x, (32, 32)) view(x, 32, 32) This function can also be called as a member function on :py:class:`tensor`, as :code:`x.view(...)` instead of :code:`view(x, ...)`.
示例
import torch import triton import triton.language as tl @triton.jit def view_kernel(x_ptr, out_ptr, D1: tl.constexpr, D2: tl.constexpr, D3: tl.constexpr, R1: tl.constexpr, R2: tl.constexpr): # (D1, D2, D3) viewed as (R1, R2) d1 = tl.arange(0, D1)[:, None, None] d2 = tl.arange(0, D2)[None, :, None] d3 = tl.arange(0, D3)[None, None, :] x = tl.load(x_ptr + d1 * D2 * D3 + d2 * D3 + d3) y = tl.view(x, [R1, R2]) flat = tl.reshape(y, (D1 * D2 * D3, )) tl.store(out_ptr + tl.arange(0, D1 * D2 * D3), flat) def test_view(): D1, D2, D3 = 2, 3, 4 R1, R2 = 6, 4 x = torch.zeros([D1, D2, D3], dtype=torch.float32).npu() out = torch.empty((D1 * D2 * D3, ), dtype=torch.float32, device="npu") view_kernel[(1, )](x, out, D1, D2, D3, R1, R2) assert out.shape == (D1 * D2 * D3, ), f"Shape mismatch: expected {(D1*D2*D3,)} but got {out.shape}." print("Test passed.") if __name__ == "__main__": test_view()
数据类型支持
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
√
√
√
√
√
√
√
√
√
√
×
√
√
√
√