mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Feature Request] `XBuffer` with static stride

Open YichengDWu opened this issue 1 year ago • 2 comments

Review Mojo's priorities

What is your request?

The current NDBuffer specifies a static size, why doesn't it allow for a statically defined stride as well? This would enable us to easily create a column-major array or a padded array. We could have XBuffer[rank, shape, stride, type].

For example, a column-major matrix is just:

shape: (3,5)
stride: (1,3)

Padding a column to a row-major matrix is simply

shape: (3,5) 
stride: (6,1)

And a lazy array filled with a single element could be defined as:

shape: (3,5)
stride: (0, 0)

What is your motivation for this change?

  1. Naturally create a lazily filled array, saving memory.
  2. Allow for the creation of column-major tensors and other things like "batched transposed tensor".
  3. In CUDA programming, it is often necessary to pad a tensor (without changing the logical shape) to avoid bank conflicts.

Any other details?

NVIDIA's CuTe provides an excellent abstraction for data layout, where they make extensive use of compile-time constants. Mojo seems inherently suited to implement a similar product.

YichengDWu avatar Jun 24 '23 16:06 YichengDWu

Seems quite reasonable, and I think it's something NDBuffer might just get. FYI @abduld

Mogball avatar Jun 26 '23 05:06 Mogball

yes, this is planned. We do not have an explicit timeline thought atm

abduld avatar Jun 26 '23 13:06 abduld