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) -> CoalesceLayout[LayoutType]

Simplifies a layout by merging modes that are contiguous in memory.

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

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

The result is the simplest layout that maps coordinates to the same linear offsets as the original. A generalized column-major layout (where strides increase left to right) coalesces to 1 dimension.

Example:

from layout.layout import Layout, coalesce, col_major, Idx

# A column-major 2x4 layout has contiguous strides -> coalesces to 1D
var layout = col_major[2, 4]()  # shape (2, 4), stride (1, 2)
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:

CoalesceLayout[LayoutType]: A new layout with contiguous dimensions merged.

Was this page helpful?