Skip to main content

Simple pytorch implementation of log-signatures.

Project description

log_signatures_pytorch

Differentiable log-signature and signature kernels implemented in PyTorch with both CPU-friendly and GPU-parallel execution paths.

What you'll find

  • Batched signature and log-signature computation for tensors shaped (batch, length, dim) with optional streaming outputs at every step. For a single path, add a leading dimension via unsqueeze(0).
  • Hall-basis utilities (hall_basis, logsigdim, logsigkeys) for inspecting dimensions and basis labels.
  • Two log-signature backends: the default signature→log path, and an incremental sparse BCH implementation for depths up to 4 (falls back otherwise).
  • The implementation of signatures is structured after keras_sig, but only focuses on pytorch.
  • Dependencies are kept minimal.

Installation

Requires Python 3.13+ and PyTorch ≥ 2.9 (CPU or CUDA builds work). From the repository root:

uv venv
source .venv/bin/activate
uv sync                    # installs runtime deps + project in editable mode
# uv sync --group dev      # adds pytest/esig for running the full test suite

Verify PyTorch is available:

python - <<'PY'
import torch
print("torch version:", torch.__version__)
print("cuda available:", torch.cuda.is_available())
PY

Quick start

Signature and log-signature of a single path

import torch
from log_signatures_pytorch import signature, log_signature, logsigdim

path = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]]).unsqueeze(0)

sig = signature(path, depth=2)
print(sig.shape)           # torch.Size([1, 6]) = sum(width**k for k in 1..depth)

log_sig = log_signature(path, depth=2)
print(log_sig.shape)       # torch.Size([1, 3]) = logsigdim(2, 2)
print("logsigdim:", logsigdim(2, 2))  # 3

Batched computation and streaming outputs

batch_paths = torch.tensor([
    [[0.0, 0.0], [1.0, 1.0]],
    [[0.0, 0.0], [2.0, 2.0]],
])

sig = signature(batch_paths, depth=2)
print(sig.shape)                 # torch.Size([2, 6])

log_sig_stream = log_signature(batch_paths, depth=2, stream=True)
print(log_sig_stream.shape)      # torch.Size([2, 1, 3]) -> (batch, steps, logsigdim)

# Streaming for a single path returns one row per increment (batch=1)
sig_stream = signature(path, depth=2, stream=True)
print(sig_stream.shape)          # torch.Size([1, 2, 6])

Hall basis helpers

from log_signatures_pytorch import hall_basis, logsigkeys

basis = hall_basis(width=2, depth=2)
print(basis)          # [1, 2, (1, 2)]

keys = logsigkeys(width=2, depth=2)
print(keys)           # ['1', '2', '[1,2]'] (matches esig format)

Choosing computation mode

  • gpu_optimized: defaults to True when the input tensor is on CUDA. Set False to force the CPU scan path.
  • chunk_size: optional on CPU to trade a small amount of extra compute for lower peak memory when sequences are long.
  • method: log_signature(..., method="bch_sparse") uses the incremental BCH routine for depths supported by HallBCH (depth ≤ 4); otherwise it falls back to the default path.

Signature outputs exclude the empty word (dimension is sum(width**k for k=1..depth)); use logsigdim(width, depth) to size log-signature outputs.

GPU compile recommendations

  • For fixed shapes with many repeated calls, torch.compile with mode reduce-overhead gives the fastest runtime for _batch_signature_gpu (~0.08–0.12 ms in our sweeps) and for log_signature (similarly sized speedups). First-call compile time can be large—especially for log-signatures—so cache per shape if you need to reuse compiled artifacts.
  • For workloads with many varying shapes or when compile latency matters more than per-call speed, prefer none (no compile) or the lighter default mode. default is slower than reduce-overhead at runtime but compiles much faster; this tradeoff is more pronounced for log-signatures.
  • The benchmark helper benchmarks/benchmark_batch_signature_gpu.py supports --target signature|log_signature, --compile-modes none reduce-overhead default, --measure-compile-time, and per-shape compile caching. CSVs land under benchmarks/results/ by default.

Testing and verification

  • Run the suite: pytest tests -q
  • Quick smoke check: python main.py
  • Mathematical property checks are documented in tests/mathematical_verification_guide.md.

Documentation

Documentation is built using MkDocs with mkdocstrings and Material theme. To build and serve the documentation:

# Build static site
uv run mkdocs build

# Serve locally (with auto-reload on changes)
uv run mkdocs serve

The documentation will be available at http://127.0.0.1:8000 when serving locally.

References

License

MIT

Citation

If you use this software in your research or in your project, please cite it as follows:

BibTeX

@software{log_signatures_pytorch,
  author = {Aune, Erlend},
  title = {log-signatures-pytorch: Differentiable log-signature and signature kernels in PyTorch},
  version = {0.1.0},
  url = {https://github.com/hance-engine/log_signatures_pytorch},
  year = {2025},
  license = {MIT}
}

Plain text

Aune, Erlend. (2025). log-signatures-pytorch: Differentiable log-signature and signature kernels in PyTorch (Version 0.1.0). [Computer software]: https://github.com/hance-engine/log_signatures_pytorch

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

log_signatures_pytorch-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

log_signatures_pytorch-0.1.0-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file log_signatures_pytorch-0.1.0.tar.gz.

File metadata

  • Download URL: log_signatures_pytorch-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for log_signatures_pytorch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 db81bc4efe4be60bc90f2b0134c730b02e2b5076778cb29ff893d69b24853b59
MD5 59d9acd2058a18286cdcaa79e9457a99
BLAKE2b-256 17e3d3e8ef07eacc5c1162de639820ab2ee9e483c42b36fb4b0fe5f65377315c

See more details on using hashes here.

Provenance

The following attestation bundles were made for log_signatures_pytorch-0.1.0.tar.gz:

Publisher: publish.yml on Froskekongen/log_signatures_pytorch

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

File details

Details for the file log_signatures_pytorch-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for log_signatures_pytorch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f17384c698f87a60924326cb6bca582c4b76b0c566d01c54743991e1a1f532f
MD5 ed79faa43c61e30c34eeb1db66f5f7ce
BLAKE2b-256 eabd55e7af6591ca95c853f5664af615274ac0631d103b20f5c306413b829014

See more details on using hashes here.

Provenance

The following attestation bundles were made for log_signatures_pytorch-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Froskekongen/log_signatures_pytorch

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