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)                                 # shifted CholeskyQR3 (default, safe)
Q = cholesky_qr(Y, method="cqr2")                  # CholeskyQR2: ~1/3 cheaper, only for cond(Y) <~ 4e3
Q = cholesky_qr(Y, robust=True)                    # FP64 first pass: cond(Y) up to >= 1e8
Q, R = cholesky_qr(Y, return_r=True)               # also R (upper triangular), Y = Q R
fast = torch.compile(cholesky_qr, fullgraph=True)  # single graph, no host synchronization (no breakdown check)

Breakdown detection and fallback (0.5). CholeskyQR breaks down when the input is too ill-conditioned for the chosen method. Before 0.5 the result then silently lost orthogonality. In eager mode, cholqr now detects this and recomputes only the affected matrices: first with an FP64 first pass, then with Householder QR. The check (a failed factorization, or a last-pass Cholesky diagonal below 0.195: a pass on X leaves orthogonality ~ c·u/Lmin² with c ≈ 18–33 measured, so 0.195 targets 1e-4 with a 2x margin on c) reuses the small r x r Cholesky factors the passes already compute, so it adds no Gram product and no extra pass over the data. It does cost one host synchronization per call. Choose the behaviour with on_breakdown: "fallback" (default), "warn", "raise" or "ignore" (the 0.4 behaviour, no synchronization). Inside torch.compile the check is skipped. This detector is empirical (calibrated and validated, not proved; under TF32 it misses most degraded runs). For a guarantee use verify=True (Certified orthogonality).

method passes reliable range in FP32 (tested) use when
"scqr3" (default) 3 cond(Y) ≲ 1e6 general use
"scqr3", robust=True 3 (first in FP64) cond(Y) up to ≥ 1e8 ill-conditioned inputs, data-center GPUs
"cqr2" 2 cond(Y) ≲ 4e3 guaranteed (= u^{-1/2}); with the chunked Gram of v0.4 it survives 1e4 and breaks between 1e4 and 3e4 (CPU) known well-conditioned inputs, e.g. sketches without power iteration
"auto" — any recommended when shapes vary: Householder (torch.linalg.qr) for a single matrix with r ≤ 64, otherwise "scqr3"
"rcqr" sketch + 1 cond(Y) up to ≥ 1e8 (orth ≈ 7e-6) single large matrices; slow when batched (its preconditioner is a batched Householder QR)

Beyond its range, a method breaks down abruptly. Since 0.5 this is detected and repaired in eager mode (see above), but every repaired call pays for a second factorization. Pick the method for the conditioning you expect. Under torch.compile there is no check, so use robust=True there if the conditioning is unknown.

Example (randomized low-rank Muon on nanoGPT): the measured cond(Y) is at most 70 without power iteration (h=0), so "cqr2" is safe there. With one power iteration (h=1) it reaches 4.5×10⁴, which needs "scqr3".

Documentation

page contents
How it works why CholeskyQR, the shifted CholeskyQR3 algorithm, compile-friendly design
Accuracy and range of validity where each method breaks down, breakdown detection and fallback, TF32
Certified orthogonality verify=True: an FP64 a-posteriori certificate, assumptions, cost
Devices: proved vs measured device sweep (Apple, NVIDIA), python -m cholqr to check your device
Compatibility Python / PyTorch versions, execution modes, method="auto"
PowerSGD drop-in cholqr.contrib.powersgd for DDP gradient compression

Install

pip install cholqr        # or: pip install git+https://github.com/createcentury/cholqr
uv add cholqr             # in a uv project (or: uv pip install cholqr)

cholqr depends only on PyTorch (>= 2.1). If you need a specific CUDA or ROCm build of PyTorch, install that first.

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.5.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.5.0
File Size Uploaded Provenance Actions
cholqr-0.5.0.tar.gz 35.8 kB Without provenance Details for cholqr-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 57.1 kB

Release files / cholqr-0.5.0.tar.gz

Download URL cholqr-0.5.0.tar.gz
Size 35.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f8933ba909399824282552917114f0a99057ccafd73ba6cf196fb9ebc2dd350a
BLAKE2b-256 checksum
How to use checksums
bbc181454389dbc49163c514bbb79a1cff84ab9a22a23e578a1a3f8276046050
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

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

Download URL cholqr-0.5.0-py3-none-any.whl
Size 21.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36cb2f2db33c9ede1519bae35beb854b1cbb50de52922a6cdcf1a0b7f9042252
BLAKE2b-256 checksum
How to use checksums
e8f6b0dfdad0faecc56417e84daa2e2487852ba83ee610c35cc1d78bfa55c69e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for cholqr 0.5.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