TensorTonic
Problems
Tiled Transpose in Triton
Linear AlgebraGPU Programming
Medium

Implement a tiled transpose kernel in Triton. Given AAA of shape (M,N)(M, N)(M,N), write out\texttt{out}out of shape (N,M)(N, M)(N,M) such that:

out[j,i]=A[i,j]\texttt{out}[j, i] = A[i, j]out[j,i]=A[i,j]

This is a 2D tile pattern with no reduction: each program loads a BLOCK_M×BLOCK_N\texttt{BLOCK\_M} \times \texttt{BLOCK\_N}BLOCK_M×BLOCK_N tile of AAA and stores its transpose into out\texttt{out}out. The interesting part is that the load is row-contiguous in AAA but the store is row-contiguous in a transposed coordinate system, so the per-axis strides on the two pointer expressions are swapped.

Loading visualization...

Examples

Input: A = [[1, 2, 3], [4, 5, 6]]

Output: [[1, 4], [2, 5], [3, 6]]

Input: A = [[1, 2], [3, 4], [5, 6]]

Output: [[1, 3, 5], [2, 4, 6]]

Hint 1

Use a 2D launch grid sized (⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉)(\lceil M / \text{BLOCK\_M} \rceil, \lceil N / \text{BLOCK\_N} \rceil)(⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉). Each program owns a tile of AAA in the input coordinate system.

Hint 2

Build one set of row and column index vectors. Use them on the load with AAA's strides, and on the store with out\texttt{out}out's strides but with row and column roles swapped.

Hint 3

Both tile axes can be partial. Combine the row tail mask and the column tail mask with an AND for the 2D mask on the load and the store.

Requirements

  • Use one Triton program per input tile on a 2D grid
  • Swap the row and column strides between the load and the store to encode the transpose
  • Mask both the load and the store on the MMM and NNN tile tails
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤5121 \le M, N \le 5121≤M,N≤512.
  • Input and output are torch.float32 on cuda.
  • Tolerance: atol=1e-3. No accumulation, no precision loss.
Try Similar Problems
Autotuned Matrix Multiplication in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasyGrouped Program-ID Matmul in TritonHardL2 Vector Norm in TritonEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Tiled Transpose in Triton

Tiled Transpose in Triton

Linear AlgebraGPU Programming
Medium

Implement a tiled transpose kernel in Triton. Given AAA of shape (M,N)(M, N)(M,N), write out\texttt{out}out of shape (N,M)(N, M)(N,M) such that:

out[j,i]=A[i,j]\texttt{out}[j, i] = A[i, j]out[j,i]=A[i,j]

This is a 2D tile pattern with no reduction: each program loads a BLOCK_M×BLOCK_N\texttt{BLOCK\_M} \times \texttt{BLOCK\_N}BLOCK_M×BLOCK_N tile of AAA and stores its transpose into out\texttt{out}out. The interesting part is that the load is row-contiguous in AAA but the store is row-contiguous in a transposed coordinate system, so the per-axis strides on the two pointer expressions are swapped.

Loading visualization...

Examples

Input: A = [[1, 2, 3], [4, 5, 6]]

Output: [[1, 4], [2, 5], [3, 6]]

Input: A = [[1, 2], [3, 4], [5, 6]]

Output: [[1, 3, 5], [2, 4, 6]]

Hint 1

Use a 2D launch grid sized (⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉)(\lceil M / \text{BLOCK\_M} \rceil, \lceil N / \text{BLOCK\_N} \rceil)(⌈M/BLOCK_M⌉,⌈N/BLOCK_N⌉). Each program owns a tile of AAA in the input coordinate system.

Hint 2

Build one set of row and column index vectors. Use them on the load with AAA's strides, and on the store with out\texttt{out}out's strides but with row and column roles swapped.

Hint 3

Both tile axes can be partial. Combine the row tail mask and the column tail mask with an AND for the 2D mask on the load and the store.

Requirements

  • Use one Triton program per input tile on a 2D grid
  • Swap the row and column strides between the load and the store to encode the transpose
  • Mask both the load and the store on the MMM and NNN tile tails
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N≤5121 \le M, N \le 5121≤M,N≤512.
  • Input and output are torch.float32 on cuda.
  • Tolerance: atol=1e-3. No accumulation, no precision loss.
Try Similar Problems
Autotuned Matrix Multiplication in TritonMediumGEMV: Matrix Vector Product in TritonMediumFused Matmul + Bias + ReLU in TritonMediumTiled Matrix Multiplication in TritonMediumScaled Dot-Product Attention in TritonMediumBlock-Pointer Matmul in TritonMediumSplit-K Matmul in TritonHardVector Addition in TritonEasyFused Multiply-Add in TritonEasyGrouped Program-ID Matmul in TritonHardL2 Vector Norm in TritonEasyRMS Normalization as a CUDA KernelMedium

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1