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 module

int_tuple

Hierarchical integer tuple data structures for high-performance tensor operations.

This module provides a flexible, memory-efficient implementation of nested integer tuples optimized for tensor shape, stride, and index operations in high-performance computing. The core data structures support both flat and hierarchical representations with efficient memory sharing and zero-copy views.

Key components:

  • IntArray: Low-level register-passable array with direct memory management
  • IntTuple: Hierarchical nested tuple with efficient memory layout and operations
  • Utility functions for tensor shape manipulation, coordinate transformations, and layout operations

Performance features:

  • Register-passable data structures for optimal compiler optimizations
  • Zero-copy views for efficient memory sharing
  • Specialized memory layout for nested structures
  • Optimized algorithms for common tensor operations

Common operations:

  • Shape manipulation: flatten, to_nest, apply, product, sum
  • Coordinate transformations: idx2crd, crd2idx
  • Layout operations: compact_order, prefix_product
  • Structural comparisons: congruent, compatible, weakly_congruent

Example usage:

from layout import IntTuple
from layout.int_tuple import flatten, compact_order, size

# Create nested tuples
var shape = IntTuple(2, IntTuple(3, 4), 5)  # Represents shape (2, (3, 4), 5)

# Flatten a nested tuple
var flat = flatten(shape)  # Results in (2, 3, 4, 5)

# Create compact strides for a given shape and order
var order = IntTuple(1, IntTuple(2, 3), 4)
var strides = compact_order(shape, order)  # Results in (1, (2, 6), 24)

# Calculate total size (product of all elements)
var total_size = size(shape)  # Results in 120

comptime values​

IntList​

comptime IntList = List[Int]

A type alias for a List of integers with ownership.

This alias defines a List that contains Int values and has ownership of its data. It's used throughout the module for storing and manipulating collections of integers, particularly for operations like permutations and indices.

UNKNOWN_VALUE​

comptime UNKNOWN_VALUE = -1

Special value indicating an unknown or unspecified dimension.

This constant is used throughout the IntTuple system to represent dimensions that are not known at compile time or have not been specified.

Structs​

  • ​IntArray: A memory-efficient, register-passable array of integers.
  • ​IntTuple: A hierarchical, nested tuple of integers with efficient memory management.

Functions​

Was this page helpful?