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

upcast

upcast[LayoutType: TensorLayout, factor: Int, //](layout: LayoutType) -> Layout[*?, *?]

Fuses consecutive elements in a layout to create a coarser layout.

This is useful for converting between different data type granularities. For example, if a layout describes byte-level offsets and you want to treat every 2 bytes as one bf16 element, use upcast[2](layout).

For each dimension with shape s and stride d:

  • new_stride = shape_div(d, factor)
  • new_shape = shape_div(s, shape_div(factor, d))

where shape_div(a, b) returns a // b if a is divisible by b, otherwise signum(a * b) (i.e., 1 for positive values).

Example:

from layout.tile_layout import row_major, upcast

# 4x8 row-major, strides (8, 1)
var layout = row_major[4, 8]()
# Upcast by 2: treat pairs as single elements
var coarser = upcast[factor=2](layout)
# Result: shape (4, 4), strides (4, 1)

Parameters:

  • LayoutType (TensorLayout): The type of the input layout.
  • factor (Int): The number of consecutive elements to fuse into one.

Args:

  • layout (LayoutType): The layout to upcast.

Returns:

Layout[*?, *?]: A new layout with adjusted shape and stride for the coarser granularity.