IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Mojo function

coalesce

def coalesce[LayoutType: TensorLayout, //](layout: LayoutType) -> Layout[*?, *?]

Simplifies a layout by merging contiguous dimensions.

Iterates over the flattened (shape, stride) pairs and:

  1. Skips shape-1 dimensions.
  2. Merges a dimension into the previous one when prev_shape * prev_stride == current_stride (contiguous).
  3. Otherwise starts a new dimension.

The result is the simplest layout that maps coordinates to the same linear offsets as the original.

Example:

from layout.tile_layout import Layout, coalesce, row_major, Idx

# A row-major 2x4 layout has contiguous strides -> coalesces to 1D
var layout = row_major[2, 4]()
var coalesced = coalesce(layout)  # shape (8,), stride (1,)

Parameters:

  • LayoutType (TensorLayout): The type of the input layout.

Args:

  • layout (LayoutType): The layout to coalesce.

Returns:

Layout[*?, *?]: A new layout with contiguous dimensions merged.

Was this page helpful?