Pure-Python GPU layout algebra for NVIDIA and AMD tensor core access patterns — no GPU required
Project description
A pure-Python implementation of the NVIDIA CuTe layout algebra. No GPU required.
CuTe layouts describe how logical coordinates map to memory offsets on GPUs. This library lets you construct, compose, and visualize those layouts using plain Python — useful for understanding tensor core access patterns, debugging swizzled shared memory, and prototyping tiled GPU kernels without compiling any CUDA. The code in src/layouts.py is intended to be readable and helpful to learn and understand layout algebra. The visualization layer is also designed to be pedagogical: for example, hierarchical layout views can explicitly show nested row/column coordinates and the resulting offset for each displayed cell.
Installation
pip install tensor-layouts
For visualization support:
pip install tensor-layouts[viz]
Quick Start
from tensor_layouts import Layout, compose, complement, logical_divide
# A 4x8 column-major layout: offset(i,j) = i + j*4
layout = Layout((4, 8), (1, 4))
print(layout) # (4, 8) : (1, 4)
print(layout(2, 3)) # 14
# Compose two layouts
a = Layout((4, 2), (1, 4))
b = Layout((2, 4), (4, 1))
print(compose(a, b))
# Tile a layout into 2x4 blocks
tiler = Layout((2, 4))
print(logical_divide(layout, tiler))
Core Concepts
A Layout is a function from logical coordinates to memory offsets, defined by
(shape, stride):
| Layout | Description |
|---|---|
Layout((4, 8), (8, 1)) |
4x8 row-major |
Layout((4, 8), (1, 4)) |
4x8 column-major |
Layout(((2,4), 8), ((1,16), 2)) |
Hierarchical (tiled) |
The algebra provides four key operations:
compose(A, B)— Function composition: apply B's indexing to A's codomaincomplement(L)— The "missing half" of a layout's codomainlogical_divide(L, T)— Factor a layout into tiles of shape Tlogical_product(A, B)— Replicate A's pattern across B's domain
Plus Swizzle(B, M, S) for XOR-based bank conflict avoidance patterns.
MMA Atoms
The library includes tensor core atom definitions for NVIDIA and AMD architectures.
NVIDIA Atoms
from tensor_layouts.atoms_nv import *
atom = SM90_64x64x16_F16F16F16_SS
print(atom.name) # SM90_64x64x16_F16F16F16_SS
print(atom.shape_mnk) # (64, 64, 16)
print(atom.c_layout) # Thread-value layout for C accumulator
Supported architectures: SM70 (Volta), SM75 (Turing), SM80 (Ampere), SM89 (Ada Lovelace), SM90 (Hopper GMMA), SM100 (Blackwell UMMA), SM120 (Blackwell B200).
AMD Atoms
from tensor_layouts.atoms_amd import *
atom = CDNA3_32x32x16_F32F8F8_MFMA
print(atom.name) # CDNA3_32x32x16_F32F8F8_MFMA
print(atom.shape_mnk) # (32, 32, 16)
print(atom.c_layout) # Thread-value layout for C accumulator
Supported architectures: CDNA1 (gfx908 / MI100), CDNA2 (gfx90a / MI200), CDNA3 (gfx942 / MI300), CDNA3+ (gfx950).
Visualization
With pip install tensor-layouts[viz]:
from tensor_layouts import Layout, Swizzle
from tensor_layouts.viz import draw_layout, draw_swizzle
draw_layout(Layout((8, 8), (8, 1)), title="Row-Major 8x8", colorize=True)
draw_swizzle(Layout((8, 8), (8, 1)), Swizzle(3, 0, 3), colorize=True)
See examples/viz.ipynb for a full
gallery of layout, swizzle, MMA atom, and tiled MMA visualizations.
Documentation
- Layout Algebra API — construction, querying, compose, complement, divide, product
- Visualization API — draw_layout, draw_swizzle, draw_mma_layout, and more
- Layout Examples — runnable script covering the full algebra (run from source checkout with
PYTHONPATH=src python3 examples/layouts.py) - Visualization Examples — runnable script generating all visualization types (run from source checkout with
PYTHONPATH=src python3 examples/viz.py) - Visualization Notebook — Jupyter gallery
Testing
pip install -e ".[test]"
pytest tests/
Oracle tests cross-validate against vendor reference implementations and are skipped automatically if the corresponding tool is unavailable:
# NVIDIA (cross-validation against pycute)
pip install -e ".[test,oracle-nv]"
pytest tests/oracle_nv.py
# AMD (cross-validation against amd_matrix_instruction_calculator)
pip install -e ".[test,oracle-amd]"
pytest tests/oracle_amd.py
References
- CuTe Documentation
- MMA Atom Documentation
- NVIDIA CUTLASS
- AMD Matrix Instruction Calculator
- AMD Matrix Cores Lab Notes
License
MIT License. See LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensor_layouts-0.1.1.tar.gz.
File metadata
- Download URL: tensor_layouts-0.1.1.tar.gz
- Upload date:
- Size: 77.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c437493b9a123d08bc67790c0f3df423336c1310de7f6b2965706e119ba5c63f
|
|
| MD5 |
8c53403f1bb9a206f3d6a3fc758a772f
|
|
| BLAKE2b-256 |
7620d2b23f0e18283ab74f25620b9ef6f436918819c90b1b3504db93babdbae5
|
Provenance
The following attestation bundles were made for tensor_layouts-0.1.1.tar.gz:
Publisher:
publish.yml on facebookresearch/tensor-layouts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tensor_layouts-0.1.1.tar.gz -
Subject digest:
c437493b9a123d08bc67790c0f3df423336c1310de7f6b2965706e119ba5c63f - Sigstore transparency entry: 1118470474
- Sigstore integration time:
-
Permalink:
facebookresearch/tensor-layouts@b658f20497544af9fef06fddf119ae3cb357227d -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/facebookresearch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b658f20497544af9fef06fddf119ae3cb357227d -
Trigger Event:
release
-
Statement type:
File details
Details for the file tensor_layouts-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tensor_layouts-0.1.1-py3-none-any.whl
- Upload date:
- Size: 81.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45a9aeae5571a1743a01628a38dabfe7a9c3b60d6041ac38c4851b764229cc73
|
|
| MD5 |
46eaa0bc018e230e874c93e42d78a1c6
|
|
| BLAKE2b-256 |
b008724e0d031f181405c83efedaa747730f656f4a8ef5f61242ec47f0cb849b
|
Provenance
The following attestation bundles were made for tensor_layouts-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on facebookresearch/tensor-layouts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tensor_layouts-0.1.1-py3-none-any.whl -
Subject digest:
45a9aeae5571a1743a01628a38dabfe7a9c3b60d6041ac38c4851b764229cc73 - Sigstore transparency entry: 1118470475
- Sigstore integration time:
-
Permalink:
facebookresearch/tensor-layouts@b658f20497544af9fef06fddf119ae3cb357227d -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/facebookresearch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b658f20497544af9fef06fddf119ae3cb357227d -
Trigger Event:
release
-
Statement type: