Write a Triton kernel transpose_kernel and wrapper transpose(x) that transposes a (M, N) tensor into a (N, M) contiguous output.
This kernel has no arithmetic - it's pure pointer math. The challenge is translating one tile's input position (m, n) to its output position (n, m).
Use a 2-D grid: one program per (BLOCK_M, BLOCK_N) tile. Each program loads a tile of shape (BLOCK_M, BLOCK_N) from the input and stores it transposed into the output, where it occupies a tile of shape (BLOCK_N, BLOCK_M).
Required kernel signature:
@triton.jit
def transpose_kernel(x_ptr, out_ptr, M, N, stride_xm, stride_xn,
stride_om, stride_on,
BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr):
Wrapper: def transpose(x: torch.Tensor) -> torch.Tensor. Use BLOCK_M = BLOCK_N = 32.
(BLOCK_M, BLOCK_N) block of pointers.(rows < M) & (cols < N).(BLOCK_N, BLOCK_M). The simplest route: load, call tl.trans(tile) to swap the two dims in registers, then store with swapped offsets.