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.

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: NVIDIA Ampere or newer (sm_80+), PyTorch with CUDA.

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

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

File details

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

File metadata

  • Download URL: sgemm_bi-0.1.1.tar.gz
  • Upload date:
  • Size: 116.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.tar.gz
Algorithm Hash digest
SHA256 163c69359b2fea84d77f9ce8f928172a8597713871b2eb8caac115cbe401024a
MD5 8fce50ac95f2628aff629315810b1808
BLAKE2b-256 23dbd749bc8f242d27f07b121fd2caefa920c93b70a8f7fadc486d50c42e5e68

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for sgemm_bi-0.1.1-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e6619e3bf47e266de5be7a44b081ce1dc841d30065a091a4cad9b26c5877a70b
MD5 4d192628df825bad36a5b72889379820
BLAKE2b-256 31f7e9e1322fd3e01f8757b3e60bcf38a7f4b85ee679525d7cad4c9547b9f9f0

See more details on using hashes here.

Provenance

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