Skip to main content

Sesum

Sesum (Scalable einsum) combines a fast C/C++ backend (with AVX2, AVX-512, and ARM64 support) and a clean Python interface to efficiently compute contraction paths and execute einsum expressions.

Sesum combines features rarely found in a single einsum library:

  • Single library: Efficient path computation and execution in one package, requiring only NumPy.
  • Highly scalable: Handles large-scale expressions with thousands of tensors.
  • Large integer support: Supports int128, uint128, and unlimited-precision bigint.
  • Sparse backend: Enables sparse execution for general tensor shapes, with an internal sparse representation optimized for sparse contractions.
  • Progress and debug info: Optional progress bars and detailed debug output.
  • Flexible indexing: Supports repeating indices in both input and output (e.g. "ijji,kki,lkll->iilki").
  • Semiring support: Allows replacing standard + and × with max and + (max-plus semiring) for optimization-style tasks.

Installation

Sesum is available on PyPI:

pip install sesum

Usage Example

The example below demonstrates basic usage of Sesum.

import numpy as np
import sesum as sr

# define an einsum expression
format_string = "dgfh,bgdec,ec,chfa->ab"
arrays = [np.random.rand(*shape) for shape in [(2, 2, 2, 2), (2, 2, 2, 2, 2), (2, 2), (2, 2, 2, 2)]]

# compute the contraction path for the einsum expression
path, flops_log10, size_log2 = sr.compute_path(format_string, *arrays)

# execute the einsum expression
result = sr.sesum(format_string, *arrays, path=path)

Use help(sr.compute_path) to view all parameters and their descriptions for path computation, and help(sr.sesum) for einsum execution.

It is recommended to review all supported parameters at least once to fully understand and utilize the capabilities of Sesum. For example, a more realistic usage for finding efficient contraction paths might look as follows:

path, flops_log10, size_log2 = sr.compute_path(
    format_string, *arrays,
    seed=0,                      # random seed for reproducibility
    minimize="flops",            # objective to minimize: "flops", "size" or "b-flops"
    algorithm="kahypar",         # path algorithm: "greedy" or "kahypar"
    skops_alpha=0,               # >0 (e.g. 64) may speed up execution, but adds flops
    max_repeats=128,             # max number of optimization repeats
    max_time=0.0,                # time limit in seconds (0.0 = unlimited)
    progbar=True,                # show progress bar during optimization
    is_outer_optimal=False,      # consider outer products in optimal search
    threshold_optimal=18,        # max number of inputs for optimal search
    threads=0,                   # number of threads (0 = use all cores)
    is_linear=True               # return linear (vs SSA) contraction path
)

As with path computation, there are numerous options available to configure einsum execution. The example below demonstrates how to enforce the use of np.float32 as the computation data type.

result = sr.sesum(
    format_string, *arrays,
    path=path,                  # precomputed contraction path
    dtype=np.float32,           # force np.float32, supports custom types like sr.bigint
    debug=True,                 # print debug info during execution
    safe_convert=False,         # if True, avoid overflow in input type conversion
    backend="dense",            # "dense" for general use, "sparse" for sparse-aware execution on general tensor shapes
    semiring=sr.standard,       # semiring used: sr.standard or sr.max_plus
    output="dense",             # "dense" returns np.ndarray, "sparse" returns SesumSparseTensor for sr.standard
    postorder=False             # False follows path order, True uses postorder tree traversal
)

By default Sesum executes pairwise contractions in the provided or generated path order. Set postorder=True only when you explicitly want postorder tree traversal.

Sparse Tensors and Sparse Backend

Sesum provides a native sparse tensor type for the sparse backend. The public sparse constructor is sparse_coo_tensor, which follows the PyTorch-style COO layout with indices shaped as (ndim, nnz):

S = sr.sparse_coo_tensor(
    [[0, 1, 1], [2, 0, 0]],
    [5, 7, 3],
    size=(2, 3),
    dtype=np.int64,
)

Coordinates do not need to be sorted. Duplicate coordinates are supported and are coalesced by summing their values, so the example above stores two entries:

S.nnz      # 2
S.coords() # array([[0, 2], [1, 0]])
S.values() # array([5, 10])

coords() returns coordinates in Sesum's sorted/coalesced storage order, which may differ from the input order.

Sesum also accepts (nnz, ndim) coordinate tuples when the shape is unambiguous:

S = sr.sparse_coo_tensor(
    [(0, 2), (1, 0), (1, 0)],
    [5, 7, 3],
    size=(2, 3),
    dtype=np.int64,
)

To convert a dense NumPy array to Sesum's sparse format, use dense_to_sparse. This scans the dense input, so sparse_coo_tensor is usually better when coordinates are already known:

a = np.array([[0, 5, 0],
              [7, 0, 0]], dtype=np.float64)

S = sr.dense_to_sparse(a)

If duplicate values sum to zero, the entry is removed. For sr.int128, sr.uint128, and sr.bigint, values() returns a NumPy object array containing Python int values.

Sparse tensors can be used directly in contractions:

B = np.ones((3, 2), dtype=np.int64)

dense_result = sr.sesum("ij,jk->ik", S, B, backend="sparse")
sparse_result = sr.sesum("ij,jk->ik", S, B, backend="sparse", output="sparse")

type(sparse_result)  # sr.SesumSparseTensor
sparse_result.to_numpy()

output="sparse" may also be used with backend="dense" for the standard semiring. In that case Sesum computes the contraction densely and converts the dense result to a native sparse tensor before returning it.

Dense and sparse inputs may be mixed. Dtype inference follows dense Sesum: if sparse input dtypes differ from the inferred or requested execution dtype, Sesum converts them sparsely using SesumSparseTensor.astype(dtype) rather than densifying the tensor.

S32 = sr.dense_to_sparse(np.array([[1, 0]], dtype=np.int32))
T64 = sr.dense_to_sparse(np.array([[2], [3]], dtype=np.int64))

out = sr.sesum("ij,jk->ik", S32, T64, backend="sparse")
out.dtype  # np.int64

The sparse backend currently supports the standard semiring for native sparse inputs and sparse outputs. The dense backend supports both sr.standard and sr.max_plus.

References

The multiple-cost-functions strategy employed in the greedy contraction path algorithm is discussed in the paper, titled "Optimizing Tensor Contraction Paths: A Greedy Algorithm Approach With Improved Cost Functions".

@article{OrglerB24,
  author    = {Sheela Orgler and Mark Blacher},
  title     = {{Optimizing Tensor Contraction Paths: A Greedy Algorithm Approach With Improved Cost Functions}},
  year      = {2024},
  journal   = {{arXiv}}
}

The algorithm driving the hypergraph partitioning approach for finding efficient contraction paths is discussed in the paper titled "Improved cut strategy for tensor network contraction orders".

@inproceedings{StaudtBKLG24,
  author       = {Christoph Staudt and Mark Blacher and Julien Klaus and Farin Lippmann and Joachim Giesen},
  title        = {{Improved Cut Strategy for Tensor Network Contraction Orders}},
  booktitle    = {{SEA}},
  year         = {2024}
}

Sesum is designed to execute large einsum problems like those introduced in "Einsum Benchmark: Enabling the Development of Next-Generation Tensor Execution Engines".

@inproceedings{BlacherSKWMBELG24,
  author       = {Mark Blacher and Christoph Staudt and Julien Klaus and Maurice Wenig and Niklas Merk and Alexander Breuer and Max Engel and S{\"{o}}ren Laue and Joachim Giesen},
  title        = {{Einsum Benchmark: Enabling the Development of Next-Generation Tensor Execution Engines}},
  booktitle    = {{NeurIPS}},
  year         = {2024}
}

The sparse backend was utilized in the experiments of "Exploiting Dynamic Sparsity in Einsum".

@inproceedings{StaudtBHKBG25,
  author       = {Christoph Staudt and Mark Blacher and Tim Hoffmann and Kaspar Kasche and Olaf Beyersdorff and Joachim Giesen},
  title        = {{Exploiting Dynamic Sparsity in Einsum}},
  booktitle    = {{NeurIPS}},
  year         = {2025}
}

Source Code

The source code used to compile Sesum is bundled with the installed Python package and accessible from its installation directory.

Metadata

Release files for sesum 0.4.3

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

Built distributions (wheels)

Table of built distributions (wheels) for sesum 0.4.3
File Actions
sesum-0.4.3-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details for sesum-0.4.3-py3-none-win_arm64.whl
sesum-0.4.3-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for sesum-0.4.3-py3-none-win_amd64.whl
sesum-0.4.3-py3-none-manylinux_2_18_x86_64.whl Python 3 none Linux glibc 2.18+ x86-64 Details for sesum-0.4.3-py3-none-manylinux_2_18_x86_64.whl
sesum-0.4.3-py3-none-manylinux_2_18_aarch64.whl Python 3 none Linux glibc 2.18+ ARM64 Details for sesum-0.4.3-py3-none-manylinux_2_18_aarch64.whl
sesum-0.4.3-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for sesum-0.4.3-py3-none-macosx_11_0_arm64.whl
sesum-0.4.3-py3-none-macosx_10_15_x86_64.whl Python 3 none macOS 10.15+ x86-64 Details for sesum-0.4.3-py3-none-macosx_10_15_x86_64.whl

Total release size: 192.7 MB

Release files / sesum-0.4.3-py3-none-win_arm64.whl

Download URL sesum-0.4.3-py3-none-win_arm64.whl
Size 25.3 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
2907a2d514ddea011c5db1d4f5bbbf99717fe0d5d34954ff01e8f1a978c1fe39
BLAKE2b-256 checksum
How to use checksums
3453cae98b91b0ca362f932596a5ebefbd394658d5d334d2a8d4458cf317b7f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.7

Release files / sesum-0.4.3-py3-none-win_amd64.whl

Download URL sesum-0.4.3-py3-none-win_amd64.whl
Size 37.1 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
4a36cbd5c439af5c91dc192564fad434f452b60208140157a76b2e97e12f64b7
BLAKE2b-256 checksum
How to use checksums
3e2f92c332dc2b42ebb1bb04794d50604b2ef4465ef9bca67a546b2a690f7061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.7

Release files / sesum-0.4.3-py3-none-manylinux_2_18_x86_64.whl

Download URL sesum-0.4.3-py3-none-manylinux_2_18_x86_64.whl
Size 36.7 MB
Tags Linux glibc 2.18+ x86-64 Python 3
SHA-256 checksum
How to use checksums
4710422cc2b4d0fc1f83ee5270b547c881787378e68ec5066977b67f126c2d1b
BLAKE2b-256 checksum
How to use checksums
96cfe9b817d3551fe5e2bf23cfd748683bf097d926c06539ab8c227ba9041fa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.7

Release files / sesum-0.4.3-py3-none-manylinux_2_18_aarch64.whl

Download URL sesum-0.4.3-py3-none-manylinux_2_18_aarch64.whl
Size 25.5 MB
Tags Linux glibc 2.18+ ARM64 Python 3
SHA-256 checksum
How to use checksums
ef25be25040806dc1806d4a154f44b724ccad2662fc67e03c2b1549ad89eae37
BLAKE2b-256 checksum
How to use checksums
0193a84ff60707edff454f1b44d2ac870b2d8865fdea2587473e86327c1b7de8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.7

Release files / sesum-0.4.3-py3-none-macosx_11_0_arm64.whl

Download URL sesum-0.4.3-py3-none-macosx_11_0_arm64.whl
Size 26.7 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c0e9934055082da17a651dca805145ff32cfd701e9d9fe8ae14c50c63195a4e4
BLAKE2b-256 checksum
How to use checksums
58338b98ac42ab89a0ac7033f9f88220d812adab60ad52d2e75595b0cd1ec21f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.7

Release files / sesum-0.4.3-py3-none-macosx_10_15_x86_64.whl

Download URL sesum-0.4.3-py3-none-macosx_10_15_x86_64.whl
Size 41.5 MB
Tags Python 3 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
a6960c86eafe820b2cd456d31dcfedfe695414ded87efb62ccfc34881f788592
BLAKE2b-256 checksum
How to use checksums
341874bf3f2ea77f183a07b125c5c9d005009f9860b59f704aa7b9d5cfbec148
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.7

Security for sesum 0.4.3

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