Skip to main content

Pure-Python GPU layout algebra for NVIDIA and AMD tensor core access patterns — no GPU required

Project description

tensor-layouts

CI PyPI License: MIT

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 codomain
  • complement(L) — The "missing half" of a layout's codomain
  • logical_divide(L, T) — Factor a layout into tiles of shape T
  • logical_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)

Row-Major 8x8 layout

Swizzle(3, 0, 3) applied to row-major 8x8

See examples/viz.ipynb for a full gallery of layout, swizzle, MMA atom, and tiled MMA visualizations.

Documentation

  • Example scripts assume tensor-layouts is installed. From a repo checkout, run pip install -e . first, or pip install -e ".[viz]" for visualization examples.
  • 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 (python3 examples/layouts.py)
  • Visualization Examples — runnable script generating all visualization types (python3 examples/viz.py)
  • Visualization Notebook — Jupyter gallery

Testing

pip install -e ".[test]"
pytest tests/

For local linting, install the dev extras and run Ruff on the Python sources:

pip install -e ".[dev]"
ruff check src/ tests/ examples/

The default Ruff configuration excludes *.ipynb; notebooks are treated as worked material rather than part of the Python lint surface.

Oracle tests cross-validate against vendor reference implementations and are skipped automatically if the corresponding tool is unavailable:

# NVIDIA pycute oracle
pip install -e ".[test,oracle-nv]"
pytest tests/oracle_nv.py

# Direct CuTe C++ oracle
# Requires a C++ compiler plus CUTLASS/CUDA headers in the active environment.
pytest tests/oracle_cute_cpp.py

# AMD (cross-validation against amd_matrix_instruction_calculator)
pip install -e ".[test,oracle-amd]"
pytest tests/oracle_amd.py

References

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

tensor_layouts-0.2.1.tar.gz (105.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensor_layouts-0.2.1-py3-none-any.whl (111.3 kB view details)

Uploaded Python 3

File details

Details for the file tensor_layouts-0.2.1.tar.gz.

File metadata

  • Download URL: tensor_layouts-0.2.1.tar.gz
  • Upload date:
  • Size: 105.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tensor_layouts-0.2.1.tar.gz
Algorithm Hash digest
SHA256 eaf362d95cc0c89f38c6f692854f901138b8f33589460c0f13535b027dcec424
MD5 fbe538e33ba768a4f21150da5bd73f60
BLAKE2b-256 403364ef880183063ce7eeeae5fb52b4c8ad342eb45aafc2a095b161f6a3a368

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensor_layouts-0.2.1.tar.gz:

Publisher: publish.yml on facebookresearch/tensor-layouts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tensor_layouts-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: tensor_layouts-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 111.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tensor_layouts-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e22bd44592c88b3a286e9e0fd359a9c284d37920998ca25b3012f699b8002bc
MD5 dbfdc6a2e1e5973afe6df713aaf1e676
BLAKE2b-256 18bf6a8e28fa709bcd261e98ed79be9e3a4dea04e6967905e565de1d1d3c4f86

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensor_layouts-0.2.1-py3-none-any.whl:

Publisher: publish.yml on facebookresearch/tensor-layouts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page