Skip to main content

Mojo function

blocked_product

blocked_product(layout_a: Layout, layout_b: Layout) -> Layout

Creates a blocked layout by combining two layouts.

This function creates a hierarchical blocked layout by combining a base layout with a block layout. The result is a layout where each element of the base layout is replaced by a block defined by the second layout.

This is particularly useful for creating tiled layouts for efficient cache utilization in tensor operations like matrix multiplication.

Example:

from layout import Layout
from layout.layout import blocked_product

# Create a 2x3 matrix layout
var matrix = Layout.row_major(2, 3)
# Define 2x2 blocks
var block = Layout.row_major(2, 2)
# Create a blocked layout with 2x2 blocks
var blocked = blocked_product(block, matrix)
from layout import Layout
from layout.layout import blocked_product

# Create a 2x3 matrix layout
var matrix = Layout.row_major(2, 3)
# Define 2x2 blocks
var block = Layout.row_major(2, 2)
# Create a blocked layout with 2x2 blocks
var blocked = blocked_product(block, matrix)

Output:

(((2, 2), (2, 3)):((2, 12), (1, 4)))
0 1 2 3 4 5
+----+----+----+----+----+----+
0 | 0 | 1 | 4 | 5 | 8 | 9 |
+----+----+----+----+----+----+
1 | 2 | 3 | 6 | 7 | 10 | 11 |
+----+----+----+----+----+----+
2 | 12 | 13 | 16 | 17 | 20 | 21 |
+----+----+----+----+----+----+
3 | 14 | 15 | 18 | 19 | 22 | 23 |
+----+----+----+----+----+----+
(((2, 2), (2, 3)):((2, 12), (1, 4)))
0 1 2 3 4 5
+----+----+----+----+----+----+
0 | 0 | 1 | 4 | 5 | 8 | 9 |
+----+----+----+----+----+----+
1 | 2 | 3 | 6 | 7 | 10 | 11 |
+----+----+----+----+----+----+
2 | 12 | 13 | 16 | 17 | 20 | 21 |
+----+----+----+----+----+----+
3 | 14 | 15 | 18 | 19 | 22 | 23 |
+----+----+----+----+----+----+

.

Args:

  • layout_a (Layout): The base layout to be blocked.
  • layout_b (Layout): The block layout defining the structure within each block.

Returns:

A new layout representing the blocked structure

Was this page helpful?