Skip to main content

Deterministic, batch-invariant CUDA GEMM for PyTorch: bit-identical training matmuls (forward / dW / dX) in f32, bf16, f16, with an opt-in tensor-core tier. Requires Linux x86_64 + NVIDIA Ampere or newer.

Project description

sgemm-bi for PyTorch

Deterministic, batch-invariant CUDA GEMM for PyTorch training: bit-identical matmuls (forward / dW / dX) in float32, bfloat16, and float16, with an opt-in tensor-core tier that is faster than cuBLAS PEDANTIC on transformer-class shapes.

Built on the sgemm-bi Rust engine. No libtorch linkage — tensors cross as raw device pointers, so one wheel works with any PyTorch build. Kernels compile once at engine creation via NVRTC; the runtime needs only the NVIDIA driver.

Requirements: Linux x86_64, NVIDIA Ampere or newer (sm_80+), PyTorch with CUDA. (No macOS/Windows — there is no CUDA there; the package refuses to import with a clear message.)

Install

pip install maturin
cd python && maturin build --release
pip install target/wheels/sgemm_bi-*.whl

Use

import torch, sgemm_bi

# Drop-in layer (weight stored [in, out] — GEMM-natural; convert
# existing layers with Linear.from_torch):
layer = sgemm_bi.Linear(768, 3072, device="cuda", dtype=torch.bfloat16,
                        tensor_cores=True)
y = layer(x)
y.sum().backward()   # deterministic dW (f32-accumulated) and dX

# Functional form:
y = sgemm_bi.deterministic_linear(x, weight, bias, tensor_cores=True)

# Low-level engine (raw pointers, explicit stream):
eng = sgemm_bi.Engine(0)
eng.forward(y.data_ptr(), x.data_ptr(), w.data_ptr(), None,
            m, k, n, "bfloat16",
            torch.cuda.current_stream().cuda_stream, True)

Determinism contracts

tier guarantee
f32 / typed scalar bit-identical across runs; bf16/f16 ≡ "upcast → f32 kernel → RNE downcast"; full shape coverage
tensor cores (tensor_cores=True) own deterministic contract (mma.sync, f32 accumulate); bit-identical across runs; forward strictly batch-invariant across all M; falls back to the scalar tier when an output dim is < 64 (shape-only dispatch — still deterministic; two bit-identical tile families, 128×128 and 64×64, cover everything above)

Bias gradient is a plain f32 column sum done in torch (deterministic run-to-run for a fixed shape; not part of the engine's batch-invariance contract).

Examples

examples/train_deterministic.py trains twice from one seed and asserts bit-identical weights, then checks batch invariance. Typed stubs ship in the wheel (py.typed) — IDEs autocomplete and document the native Engine class.

Tests

pip install pytest && pytest tests/ -v   # needs a CUDA GPU

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

sgemm_bi-0.1.1.post2.tar.gz (117.9 kB view details)

Uploaded Source

Built Distribution

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

sgemm_bi-0.1.1.post2-cp39-abi3-manylinux_2_34_x86_64.whl (457.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ x86-64

File details

Details for the file sgemm_bi-0.1.1.post2.tar.gz.

File metadata

  • Download URL: sgemm_bi-0.1.1.post2.tar.gz
  • Upload date:
  • Size: 117.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sgemm_bi-0.1.1.post2.tar.gz
Algorithm Hash digest
SHA256 2f8c2810e029214473434884a1883ef582a54798520a3b3aa7da0b57b61c1d4e
MD5 a46405108227a388653f0523b4300a5c
BLAKE2b-256 c384d81496c4edbf61e04de6a422147b40af03369893796829e1dcfe81d2ead9

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgemm_bi-0.1.1.post2.tar.gz:

Publisher: release.yml on silvermpx/sgemm-bi

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

File details

Details for the file sgemm_bi-0.1.1.post2-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for sgemm_bi-0.1.1.post2-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c585761fe517af8b93664753fcd49fdad77f6028bb1faa4b0d95080048083429
MD5 11d2710b832d14695e8657a610520d5f
BLAKE2b-256 51e2bb2a9065eaa5203d74b5eeb2b1194c4933880b91ede1fc4c58944bde47f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgemm_bi-0.1.1.post2-cp39-abi3-manylinux_2_34_x86_64.whl:

Publisher: release.yml on silvermpx/sgemm-bi

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