Skip to main content

Sketched matrix decompositions for PyTorch

Project description

Skerch logo, light mode Skerch logo, dark mode

skerch: Sketched matrix decompositions for PyTorch

PyPI Docs CI Tests
PyPI - Downloads Documentation Status GitHub Actions Workflow Status Coverage Status

skerch is a Python package to compute different decompositions (SVD, Hermitian Eigendecomposition, diagonal, subdiagonal, triangular, block-triangular) of linear operators via sketched methods.

  • Built on top of PyTorch, with natural support for CPU and CUDA interoperability, and very few dependencies otherwise
  • Works on matrices and matrix-free operators of potentially very large dimensionality
  • Support for sketched measurements in a fully distributed fashion via HDF5 databases

References:

See the documentation for more details, including examples for other decompositions and use cases.

Installation and basic usage

Install via:

pip install skerch

The sketched SVD of a linear operator op of shape (h, w) can be then computed simply via:

from skerch.decompositions import ssvd

q, u, s, vt, pt = ssvd(
    op,
    op_device=DEVICE,
    op_dtype=DTYPE,
    outer_dim=NUM_OUTER,
    inner_dim=NUM_INNER,
)

Where the number of outer and inner measurements for the sketch is specified, and q @ u @ diag(s) @ vt @ pt is a PyTorch matrix that approximates op, where q, p are thin orthonormal matrices of shape (h, NUM_OUTER) and (NUM_OUTER, w) respectively, and u, vt are small orthogonal matrices of shape (NUM_OUTER, NUM_OUTER).

The op object must simply satify the following criteria:

  • It must have a op.shape = (height, width) attribute
  • It must implement the w = op @ v right-matmul operator, receiving and returning PyTorch vectors/matrices
  • It must implement the w = v @ op left-matmul operator, receiving and returning PyTorch vectors/matrices

skerch provides a convenience PyTorch wrapper for the cases where op interacts with NumPy arrays instead (e.g. SciPy linear operators like the ones used in CurvLinOps).

To get a good suggestion of the number of measurements required for a given shape and budget, simply run:

python -m skerch prio_hpars --shape=100,200 --budget=12345

The library also implements cheap a-posteriori methods to estimate the error of the obtained sketched approximation:

from skerch.a_posteriori import a_posteriori_error
from skerch.linops import CompositeLinOp, DiagonalLinOp

# (q, u, s, vt, pt) previously computed via ssvd
sketched_op = CompositeLinOp(
    (
        ("Q", q),
        ("U", u),
        ("S", DiagonalLinOp(s)),
        ("Vt", vt),
        ("Pt", pt),
    )
)

(f1, f2, frob_err) = a_posteriori_error(
    op, sketched_op, NUM_A_POSTERIORI, dtype=DTYPE, device=DEVICE
)[0]
print("Estimated Frob(op):", f1**0.5)
print("Estimated Frob(sketched_op):", f2**0.5)
print("Estimated Frobenius Error:", frob_err**0.5)

For a given NUM_A_POSTERIORI measurements (30 is generally OK), the probability of frob_err**0.5 being wrong by a certain amount can be queried as follows:

python -m skerch post_bounds --apost_n=30 --apost_err=0.5

See Getting Started, Examples, and API docs for more details.

Developers

Contributions are most welcome under this repo's LICENSE. Feel free to open an issue with bug reports, feature requests, etc.

The documentation contains a For Developers section with useful guidelines to interact with this repo.

Researchers

If this library is useful for your work, please consider citing it:

@manual{fernandez2024skerch,
  title={{S}kerch: Sketched matrix decompositions for {PyTorch}},
  author={Andres Fernandez},
  year={2024},
  url={https://github.com/andres-fr/skerch},
}

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

skerch-0.11.0.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

skerch-0.11.0-py3-none-any.whl (89.1 kB view details)

Uploaded Python 3

File details

Details for the file skerch-0.11.0.tar.gz.

File metadata

  • Download URL: skerch-0.11.0.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for skerch-0.11.0.tar.gz
Algorithm Hash digest
SHA256 702355fd615f86f5e53a5559dfdb8a2f10783b03198d23a5d96edfad20cba44e
MD5 4469fd0d320c098becb5e8de9aff73eb
BLAKE2b-256 86e89b02cb36d23d8e5465349e90fbaf4cfb600ba11fb2d632c6c2c5b79948c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for skerch-0.11.0.tar.gz:

Publisher: ci.yaml on andres-fr/skerch

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

File details

Details for the file skerch-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: skerch-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 89.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for skerch-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b714ff3629b91ad9060c874f5cf391651420821b4a8fc6771cead658879d8f7d
MD5 3a78f35d44a3b81435141447875a64db
BLAKE2b-256 0913bb6e7cb73a81ff4c132f1be9dac823a5c934f3cabed25c422b6405648f1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for skerch-0.11.0-py3-none-any.whl:

Publisher: ci.yaml on andres-fr/skerch

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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page