LayoutTensor Version

Overview

Implement a kernel that broadcast adds LayoutTensor vector a and LayoutTensor vector b and stores it in LayoutTensor out.

Note: You have more threads than positions.

Key concepts

In this puzzle, you’ll learn about:

  • Using LayoutTensor for broadcast operations
  • Working with different tensor shapes
  • Handling 2D indexing with LayoutTensor

The key insight is that LayoutTensor allows natural broadcasting through different tensor shapes: \((n,1)\) and \((1,n)\) to \((n,n)\), while still requiring bounds checking.

  • Tensor shapes: Input vectors have shapes \((n,1)\) and \((1,n)\)
  • Broadcasting: Output combines both dimensions to \((n,n)\)
  • Guard condition: Still need bounds checking for output size
  • Thread bounds: More threads \((3 \times 3)\) than tensor elements \((2 \times 2)\)

Code to complete

alias SIZE = 2
alias BLOCKS_PER_GRID = 1
alias THREADS_PER_BLOCK = (3, 3)
alias dtype = DType.float32
alias out_layout = Layout.row_major(SIZE, SIZE)
alias a_layout = Layout.row_major(SIZE, 1)
alias b_layout = Layout.row_major(1, SIZE)


fn broadcast_add[
    out_layout: Layout,
    a_layout: Layout,
    b_layout: Layout,
](
    out: LayoutTensor[mut=True, dtype, out_layout],
    a: LayoutTensor[mut=True, dtype, a_layout],
    b: LayoutTensor[mut=True, dtype, b_layout],
    size: Int,
):
    local_i = thread_idx.x
    local_j = thread_idx.y
    # FILL ME IN (roughly 2 lines)


View full file: problems/p05/p05_layout_tensor.mojo

Tips
  1. Get 2D indices: local_i = thread_idx.x, local_j = thread_idx.y
  2. Add guard: if local_i < size and local_j < size
  3. Inside guard: out[local_i, local_j] = a[local_i, 0] + b[0, local_j]

Running the code

To test your solution, run the following command in your terminal:

magic run p05_layout_tensor

Your output will look like this if the puzzle isn’t solved yet:

out: HostBuffer([0.0, 0.0, 0.0, 0.0])
expected: HostBuffer([0.0, 1.0, 1.0, 2.0])

Solution

fn broadcast_add[
    out_layout: Layout,
    a_layout: Layout,
    b_layout: Layout,
](
    out: LayoutTensor[mut=True, dtype, out_layout],
    a: LayoutTensor[mut=True, dtype, a_layout],
    b: LayoutTensor[mut=True, dtype, b_layout],
    size: Int,
):
    local_i = thread_idx.x
    local_j = thread_idx.y
    if local_i < size and local_j < size:
        out[local_i, local_j] = a[local_i, 0] + b[0, local_j]


This solution:

  • Gets 2D thread indices with local_i = thread_idx.x, local_j = thread_idx.y
  • Guards against out-of-bounds with if local_i < size and local_j < size
  • Uses LayoutTensor broadcasting: a[local_i, 0] + b[0, local_j]