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.post1.tar.gz (117.7 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.post1-cp39-abi3-manylinux_2_34_x86_64.whl (457.2 kB view details)

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

File details

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

File metadata

  • Download URL: sgemm_bi-0.1.1.post1.tar.gz
  • Upload date:
  • Size: 117.7 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.post1.tar.gz
Algorithm Hash digest
SHA256 b23e21b28093fd37e401b9d5f97c29342c60903ecb2de3ece2ed56e9ea09d638
MD5 662271061980b87a9c01a146fe97c490
BLAKE2b-256 bc4dcb86b5dd33148564afc3610bb8a3d527d94d065bc81daa11da7ed16fb3f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgemm_bi-0.1.1.post1.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.post1-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for sgemm_bi-0.1.1.post1-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 146d641984b59b085ca5461f32d2edd52cff78f88e9a82be2a6c77c94db88f8e
MD5 8cf38cc1cdd239136cd2a237dbb50e3e
BLAKE2b-256 7c569a15b7d8809fcbbd2390cf9d6bad26573ebfab193ca643ed9a37cbae75a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgemm_bi-0.1.1.post1-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