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.

For floating-point sr.max_plus, the additive identity is -inf. Integer max-plus uses the dtype's lowest value as an absorbing identity; for unsigned integers, zero is therefore reserved as the identity. Before execution, Sesum checks a path-independent bound for every possible intermediate sum and rejects expressions that could overflow or collide with the reserved identity. Use a wider integer or floating-point dtype for such expressions. sr.bigint has no representable negative-infinity identity and is not supported with 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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sesum-0.4.2-py3-none-win_arm64.whl (25.3 MB view details)

Uploaded Python 3Windows ARM64

sesum-0.4.2-py3-none-win_amd64.whl (37.1 MB view details)

Uploaded Python 3Windows x86-64

sesum-0.4.2-py3-none-manylinux_2_18_x86_64.whl (36.7 MB view details)

Uploaded Python 3manylinux: glibc 2.18+ x86-64

sesum-0.4.2-py3-none-manylinux_2_18_aarch64.whl (25.5 MB view details)

Uploaded Python 3manylinux: glibc 2.18+ ARM64

sesum-0.4.2-py3-none-macosx_11_0_arm64.whl (26.7 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

sesum-0.4.2-py3-none-macosx_10_15_x86_64.whl (41.5 MB view details)

Uploaded Python 3macOS 10.15+ x86-64

File details

Details for the file sesum-0.4.2-py3-none-win_arm64.whl.

File metadata

  • Download URL: sesum-0.4.2-py3-none-win_arm64.whl
  • Upload date:
  • Size: 25.3 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.7

File hashes

Hashes for sesum-0.4.2-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 779782ca368900439eb99f537ddf30201f5c699551e65c1132d005d61e834069
MD5 3298d88505ea7659fbd40fdd560ad266
BLAKE2b-256 948af9f92c547ffb573db2ca2d7f24826fab9420f2f6c5d033bddffeb1a47fd6

See more details on using hashes here.

File details

Details for the file sesum-0.4.2-py3-none-win_amd64.whl.

File metadata

  • Download URL: sesum-0.4.2-py3-none-win_amd64.whl
  • Upload date:
  • Size: 37.1 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.7

File hashes

Hashes for sesum-0.4.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 e99fcf6377358e7ae98bc54693831d6b8a8f4edc6a9c372988567cfe5a4528a4
MD5 4e2fe4179bc6e244a094b5e398b8673a
BLAKE2b-256 5412f876a472c46f1268f8e3c2251d8d88f52c21dd3088e77f074d4e82046871

See more details on using hashes here.

File details

Details for the file sesum-0.4.2-py3-none-manylinux_2_18_x86_64.whl.

File metadata

File hashes

Hashes for sesum-0.4.2-py3-none-manylinux_2_18_x86_64.whl
Algorithm Hash digest
SHA256 9a7e46daef881a6d99d7ac984af5ecc67508f8ba59684cfb18c5bafafc80aa55
MD5 cabb389146fd1d7009f733e2cc7489a8
BLAKE2b-256 e91552cbc15c11df69f2e78fb5278c7047103189a8e5979e6305c5dc42917c5e

See more details on using hashes here.

File details

Details for the file sesum-0.4.2-py3-none-manylinux_2_18_aarch64.whl.

File metadata

File hashes

Hashes for sesum-0.4.2-py3-none-manylinux_2_18_aarch64.whl
Algorithm Hash digest
SHA256 cc85b6110c7d97c69cc026f3c6689f5ac132e48577db9e34576eff55815d3fda
MD5 8698d9b8a38cd5143f52f661b0b89fde
BLAKE2b-256 c534dd39f632d9ed09d4e311f821ad7a58f50038bdfd2e186b806bc18c72a13e

See more details on using hashes here.

File details

Details for the file sesum-0.4.2-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sesum-0.4.2-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 521e7e592e67d55de6c71be95fdf8e6f06e04d05c532940993f14295c7eb7879
MD5 2c238ff8de7bf8b3ec3ba64ec96d2e58
BLAKE2b-256 2967a7e81ba984a97a41859d5c52f22c9b6bfc9f36065b83272e693fb04049b2

See more details on using hashes here.

File details

Details for the file sesum-0.4.2-py3-none-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for sesum-0.4.2-py3-none-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 9e9e32d0c96512fca33249d5f67dda64710d9bd01896c957de1c8e18beda3d8d
MD5 5c7a6e5dd0eca5849f435f4645765fab
BLAKE2b-256 b0836238bdea4646b780e75ea5c2f5a0fc6214d4c56609dcc3e88294b7aa944b

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.3

6 files

This release

0.4.2 This release

6 files

0.4.1

1 file

0.4.0

1 file

0.3.9

1 file

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