Skip to main content

cholqr

Fast, compile-friendly shifted CholeskyQR3 for batched tall-skinny matrices in PyTorch. It is a drop-in for torch.linalg.qr(Y, mode="reduced")[0] when you only need an orthonormal basis of range(Y), for example in randomized SVD and sketching, low-rank optimizers such as randomized Muon, or subspace iteration.

import torch
from cholqr import cholesky_qr

Y = torch.randn(40, 768, 210, device="cuda")       # batch of tall-skinny matrices
Q = cholesky_qr(Y)                                 # orthonormal columns, same span as Y
fast = torch.compile(cholesky_qr, fullgraph=True)  # single graph, no host synchronization

Why

On GPUs, batched Householder QR (cuSOLVER geqr2) processes the batch one matrix at a time.

On an NVIDIA A100, for the sketches of randomized low-rank Muon on nanoGPT (two weight banks, sketch width 210):

Householder QR cholqr full Newton–Schulz (reference)
optimizer time per step, both banks, one GPU 117.6 ms 15.2 ms 17.1 ms
  • torch.linalg.qr reaches about 1.6% of FP32 peak here.
  • On the QR step alone, cholqr is 3–14x faster.
  • End to end (1490 steps, A100-80GB), replacing the QR removes about 100 ms per step. Randomized Muon then trains as fast as full Muon at unchanged perplexity.

Library benchmark on an NVIDIA Tesla T4 (torch 2.14.1, CUDA 13.0; mean of 20 calls; benchmarks/bench_qr.py):

batch x m x r torch.linalg.qr cholqr (compiled) speedup robust=True max ‖QᵀQ − I‖_F
40 x 768 x 210 51.7 ms 10.8 ms 4.8x 45.1 ms 3.5e-6
10 x 3072 x 210 23.0 ms 9.7 ms 2.4x 40.4 ms 3.5e-6
16 x 4096 x 256 62.4 ms 25.5 ms 2.4x 104.2 ms 4.3e-6
4 x 768 x 64 1.55 ms 1.68 ms 0.9x 6.2 ms 1.5e-6
  • The gain comes from batches of moderately wide sketches. For small problems (e.g. 4 x 768 x 64), Householder QR is already fast, so do not expect a speedup there.
  • robust=True is slow on the T4 because the T4 has low FP64 throughput. Use it only for ill-conditioned inputs, preferably on A100/H100-class GPUs.
  • The A100 numbers in the table above come from the randomized-Muon training code (same algorithm).

Run python benchmarks/bench_qr.py --compile on your GPU to measure your own shapes.

How

Shifted CholeskyQR3 (Fukaya et al., 2020) with three implementation choices:

  1. Column scaling before every Gram product. The shift is scale invariant, and unnormalized inputs do not overflow FP32.
  2. Explicit r x r triangular inverse applied as one batched GEMM. This replaces a slow triangular solve over m rows.
  3. cholesky_ex with a per-matrix torch.where fallback. There is no host synchronization, and the routine compiles with zero graph breaks.

Accuracy and range of validity

input condition number default (FP32) robust=True (FP64 first pass)
up to 1e6 ‖QᵀQ − I‖_F ≈ 1e-5 ‖QᵀQ − I‖_F ≈ 1e-5
5e6 – 1e8 breaks down (orthogonality lost) ‖QᵀQ − I‖_F ≈ 1e-5, subspace error ≈ 2e-6

These numbers were tested on 3072 x 210 inputs with geometric and power-law spectra (tests/).

  • The default first-pass shift 1e-6 * tr(G) is a heuristic. The a-priori shift of Fukaya et al. is impractically large in FP32 for large m·r.
  • robust=True costs one FP64 Gram product. That is cheap on data-center GPUs (A100/H100) and slow on consumer GPUs.
  • Real optimizer states (27 nanoGPT momentum snapshots, sketch width 210, bf16 power iteration as in training):
    • κ(Y) ≤ 4.5×10⁴.
    • No fallback was triggered.
    • The final low-rank update differs from an FP64 Householder reference by at most 7.6×10⁻⁵ (relative).
    • Sketches computed in bf16 are typically well inside the default range, because bf16 rounding caps their effective condition number.
  • Exactly rank-deficient or zero inputs return finite outputs (not an orthonormal basis of a larger space).
  • Real dtypes only; complex inputs raise TypeError. Autograd works (gradients agree with Householder QR to ~1e-7 in tests). Runs on CUDA, CPU and Apple MPS.
  • Status: experimental (0.x). The API may change. Issues and benchmarks on other GPUs are welcome.
Q = cholesky_qr(Y)               # fast default, cond(Y) <~ 1e6
Q = cholesky_qr(Y, robust=True)  # ill-conditioned inputs, cond(Y) up to >= 1e8

Install

pip install cholqr        # or: pip install git+https://github.com/createcentury/cholqr

Citation

Please cite the algorithm:

T. Fukaya, R. Kannan, Y. Nakatsukasa, Y. Yamamoto, Y. Yanagisawa. Shifted Cholesky QR for computing the QR factorization of ill-conditioned matrices. SIAM J. Sci. Comput. 42(1):A477–A503, 2020.

Y. Yamamoto, Y. Nakatsukasa, Y. Yanagisawa, T. Fukaya. Roundoff error analysis of the CholeskyQR2 algorithm. Electron. Trans. Numer. Anal. 44:306–326, 2015.

License

MIT

Metadata

Release files for cholqr 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cholqr 0.1.0
File Size Uploaded Provenance Actions
cholqr-0.1.0.tar.gz 50.0 kB Without provenance Details for cholqr-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cholqr 0.1.0
File Interpreter ABI Platform Actions
cholqr-0.1.0-py3-none-any.whl Python 3 none any Details for cholqr-0.1.0-py3-none-any.whl

Total release size: 56.6 kB

Release files / cholqr-0.1.0.tar.gz

Download URL cholqr-0.1.0.tar.gz
Size 50.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7cfb1e3d899941b1be000b51c734d6b0cba65a61778dd1fa522fb5e862d83c59
BLAKE2b-256 checksum
How to use checksums
18776fa4c8b434dd82defd13ae6dafeb942eacfd2c33d3374f8e30aa2a76d115
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / cholqr-0.1.0-py3-none-any.whl

Download URL cholqr-0.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56ad8c6f82495593995ec029c7eec49c2fddb007d36ed3ba9b3e7431e92ec41b
BLAKE2b-256 checksum
How to use checksums
be0419331ebc1ec397ae8de4912445dd4a58db44be8fb32a1f0e5acf4c2fd499
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for cholqr 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page