Mojo function
coalesce
coalesce(layout: Layout, keep_rank: Bool = False) -> Layout
Simplifies a layout by combining dimensions with contiguous strides.
This function reduces the rank of a layout by merging dimensions that have contiguous memory layouts, resulting in a simpler but equivalent layout.
Example:
from layout import Layout, IntTuple
from layout.layout import coalesce
# A layout with shape (2, (1, 4)) and stride (1, (4, 2)) can be coalesced
var layout = Layout(IntTuple(2, IntTuple(1, 4)), IntTuple(1, IntTuple(4, 2)))
var coalesced = coalesce(layout)
# Result: Layout with shape (8) and stride (1)
from layout import Layout, IntTuple
from layout.layout import coalesce
# A layout with shape (2, (1, 4)) and stride (1, (4, 2)) can be coalesced
var layout = Layout(IntTuple(2, IntTuple(1, 4)), IntTuple(1, IntTuple(4, 2)))
var coalesced = coalesce(layout)
# Result: Layout with shape (8) and stride (1)
.
Args:
- layout (
Layout
): The layout to coalesce. - keep_rank (
Bool
): If True, maintains the original rank of the layout. Default is False.
Returns:
A simplified layout with reduced rank where possible.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!