-
Notifications
You must be signed in to change notification settings - Fork 22
Description
Summary
This proposal introduces a unified Layout abstraction to replace the separate NDArrayShape and NDArrayStrides structures, which currently duplicate much of their functionality. By consolidating them under a single, parameterized Layout type, we reduce redundancy, improve maintainability, and align Numojo’s internal array metadata representation with the LayoutTensor concept used in Mojo’s GPU programming model.
To maintain backward compatibility and minimize disruption, the existing Shape and Strides types will remain as public-facing aliases of the new Layout specialization. Internally, however, the system will standardize on Layout for all shape/stride logic. This creates a unified conceptual and implementation bridge between Numojo and MAX, supporting a more consistent user experience across the ecosystem.
Motivation
NDArrayShape and NDArrayStrides currently define nearly identical APIs and internal structures. Maintaining parallel implementations leads to code duplication, a larger surface area for bugs, and inconsistencies over time. A generalized Layout type allows for:
- Unified logic for dimension-related operations.
- Better maintainability by eliminating duplicated methods.
- Greater consistency with Mojo GPU abstractions
Detailed Explanation
The new Layout type captures the shared data model of shapes and strides by parameterizing over a type field that indicates whether a given instance represents a shape (0) or a stride (1):
alias Shape = Layout[0]
alias Strides = Layout[1]
struct Layout[type: Bool](): # Bool tag distinguishes shape vs stride roles, we could also use an i8 or String instead
alias dtype: DType = DType.int
var data: UnsafePointer[Scalar[Self.dtype]]
"""Underlying buffer storing dimension values."""
var ndim: Int
"""Number of array dimensions. Must be > 0."""This abstraction preserves the semantics of shape and stride objects while eliminating duplicated internals. The public API for users remains stable through the aliases Shape and Strides, ensuring seamless migration.