pathsig 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pathsig-0.1.1/LICENSE +21 -0
- pathsig-0.1.1/MANIFEST.in +7 -0
- pathsig-0.1.1/PKG-INFO +64 -0
- pathsig-0.1.1/README.md +53 -0
- pathsig-0.1.1/pyproject.toml +24 -0
- pathsig-0.1.1/setup.cfg +4 -0
- pathsig-0.1.1/setup.py +41 -0
- pathsig-0.1.1/src/pathsig/Signature.py +45 -0
- pathsig-0.1.1/src/pathsig/__init__.py +6 -0
- pathsig-0.1.1/src/pathsig/_autograd.py +54 -0
- pathsig-0.1.1/src/pathsig/pybindings.cpp +33 -0
- pathsig-0.1.1/src/pathsig/signature_backward/sig_backprop.cu +677 -0
- pathsig-0.1.1/src/pathsig/signature_backward/sig_backprop.cuh +87 -0
- pathsig-0.1.1/src/pathsig/signature_backward/sig_backprop_launch.cu +214 -0
- pathsig-0.1.1/src/pathsig/signature_backward/sig_backprop_launch.cuh +47 -0
- pathsig-0.1.1/src/pathsig/signature_forward/compute_sig.cu +487 -0
- pathsig-0.1.1/src/pathsig/signature_forward/compute_sig.cuh +98 -0
- pathsig-0.1.1/src/pathsig/signature_forward/compute_sig_launch.cu +203 -0
- pathsig-0.1.1/src/pathsig/signature_forward/compute_sig_launch.cuh +39 -0
- pathsig-0.1.1/src/pathsig/utils/SigDecomposition.cpp +259 -0
- pathsig-0.1.1/src/pathsig/utils/SigDecomposition.h +159 -0
- pathsig-0.1.1/src/pathsig/utils/extended_precision.cuh +72 -0
- pathsig-0.1.1/src/pathsig/utils/sig_setup.cu +208 -0
- pathsig-0.1.1/src/pathsig/utils/sig_setup.cuh +53 -0
- pathsig-0.1.1/src/pathsig/utils/word_mappings.cuh +54 -0
- pathsig-0.1.1/src/pathsig.egg-info/PKG-INFO +64 -0
- pathsig-0.1.1/src/pathsig.egg-info/SOURCES.txt +29 -0
- pathsig-0.1.1/src/pathsig.egg-info/dependency_links.txt +1 -0
- pathsig-0.1.1/src/pathsig.egg-info/not-zip-safe +1 -0
- pathsig-0.1.1/src/pathsig.egg-info/top_level.txt +1 -0
- pathsig-0.1.1/tests/test_forward_backward.py +270 -0
pathsig-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tobias Nygaard
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pathsig-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pathsig
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: GPU-accelerated library for scalable and efficient differentiable signature computations.
|
|
5
|
+
Author-email: Tobias Nygaard <tobiasknygard@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/tobiasny12/pathsig
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# pathsig
|
|
13
|
+
A high-performance, GPU-accelerated library for differentiable signature computations, with PyTorch integration. Built on a decomposition approach that enables efficient, scalable signature computations with minimal memory usage.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
```bash
|
|
17
|
+
pip install pathsig
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Requirements:**
|
|
21
|
+
- PyTorch >= 2.0
|
|
22
|
+
- CUDA-capable GPU (compute capability >= 7.0)
|
|
23
|
+
- Python >= 3.8
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
`pathsig` provides both a functional API and a PyTorch module for computing path signatures. The library allows for straightforward usage of the signature as part of machine learning models, acting as a differentiable feature extractor for time series or path data.
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import torch
|
|
31
|
+
import torch.nn as nn
|
|
32
|
+
import torch.optim as optim
|
|
33
|
+
import pathsig
|
|
34
|
+
|
|
35
|
+
# Compute signature of a path
|
|
36
|
+
path = torch.randn(1, 100, 3, device='cuda') # (batch_size, sequence_length, path_dim)
|
|
37
|
+
sig = pathsig.signature(path, truncation_level=4)
|
|
38
|
+
print(sig.shape) # (1, 120)
|
|
39
|
+
|
|
40
|
+
# Use as a PyTorch module
|
|
41
|
+
signature_layer = pathsig.Signature(truncation_level=4).to('cuda')
|
|
42
|
+
sig = signature_layer(path)
|
|
43
|
+
|
|
44
|
+
# As part of a neural network
|
|
45
|
+
class SignatureNet(nn.Module):
|
|
46
|
+
def __init__(self, input_channels, sig_level, num_classes):
|
|
47
|
+
super().__init__()
|
|
48
|
+
self.signature = pathsig.Signature(truncation_level=sig_level)
|
|
49
|
+
sig_dim = pathsig.sig_size(input_channels, sig_level)
|
|
50
|
+
self.classifier = nn.Linear(sig_dim, num_classes)
|
|
51
|
+
|
|
52
|
+
def forward(self, x):
|
|
53
|
+
sig = self.signature(x)
|
|
54
|
+
return self.classifier(sig)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## Limitations
|
|
59
|
+
- GPU-only (no CPU support currently).
|
|
60
|
+
- Maximum truncation level: 12.
|
|
61
|
+
- Maximum path dimension: 1000.
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
MIT License, see [LICENSE](LICENSE) file for details.
|
pathsig-0.1.1/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# pathsig
|
|
2
|
+
A high-performance, GPU-accelerated library for differentiable signature computations, with PyTorch integration. Built on a decomposition approach that enables efficient, scalable signature computations with minimal memory usage.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```bash
|
|
6
|
+
pip install pathsig
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**Requirements:**
|
|
10
|
+
- PyTorch >= 2.0
|
|
11
|
+
- CUDA-capable GPU (compute capability >= 7.0)
|
|
12
|
+
- Python >= 3.8
|
|
13
|
+
|
|
14
|
+
## Quick Start
|
|
15
|
+
`pathsig` provides both a functional API and a PyTorch module for computing path signatures. The library allows for straightforward usage of the signature as part of machine learning models, acting as a differentiable feature extractor for time series or path data.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
import torch
|
|
20
|
+
import torch.nn as nn
|
|
21
|
+
import torch.optim as optim
|
|
22
|
+
import pathsig
|
|
23
|
+
|
|
24
|
+
# Compute signature of a path
|
|
25
|
+
path = torch.randn(1, 100, 3, device='cuda') # (batch_size, sequence_length, path_dim)
|
|
26
|
+
sig = pathsig.signature(path, truncation_level=4)
|
|
27
|
+
print(sig.shape) # (1, 120)
|
|
28
|
+
|
|
29
|
+
# Use as a PyTorch module
|
|
30
|
+
signature_layer = pathsig.Signature(truncation_level=4).to('cuda')
|
|
31
|
+
sig = signature_layer(path)
|
|
32
|
+
|
|
33
|
+
# As part of a neural network
|
|
34
|
+
class SignatureNet(nn.Module):
|
|
35
|
+
def __init__(self, input_channels, sig_level, num_classes):
|
|
36
|
+
super().__init__()
|
|
37
|
+
self.signature = pathsig.Signature(truncation_level=sig_level)
|
|
38
|
+
sig_dim = pathsig.sig_size(input_channels, sig_level)
|
|
39
|
+
self.classifier = nn.Linear(sig_dim, num_classes)
|
|
40
|
+
|
|
41
|
+
def forward(self, x):
|
|
42
|
+
sig = self.signature(x)
|
|
43
|
+
return self.classifier(sig)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
## Limitations
|
|
48
|
+
- GPU-only (no CPU support currently).
|
|
49
|
+
- Maximum truncation level: 12.
|
|
50
|
+
- Maximum path dimension: 1000.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
MIT License, see [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# pyproject.toml
|
|
2
|
+
[build-system]
|
|
3
|
+
requires = ["setuptools", "wheel", "torch"]
|
|
4
|
+
build-backend = "setuptools.build_meta"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
[project]
|
|
8
|
+
name = "pathsig"
|
|
9
|
+
version = "0.1.1"
|
|
10
|
+
description = "GPU-accelerated library for scalable and efficient differentiable signature computations."
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
requires-python = ">=3.8"
|
|
13
|
+
authors = [{name = "Tobias Nygaard", email = "tobiasknygard@gmail.com"}]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://github.com/tobiasny12/pathsig"
|
|
17
|
+
|
|
18
|
+
[tool.setuptools]
|
|
19
|
+
package-dir = {"" = "src"}
|
|
20
|
+
packages = ["pathsig"]
|
|
21
|
+
include-package-data = false
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.exclude-package-data]
|
|
24
|
+
pathsig = ["signature_forward/*", "signature_backward/*", "utils/*", "*.cu", "*.cuh", "*.cpp", "*.h", "*.hpp"]
|
pathsig-0.1.1/setup.cfg
ADDED
pathsig-0.1.1/setup.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# setup.py for CUDA Torch extension with correct ABI passing to NVCC host compiler
|
|
2
|
+
import os
|
|
3
|
+
import torch
|
|
4
|
+
from setuptools import setup, find_packages
|
|
5
|
+
from torch.utils.cpp_extension import BuildExtension, CUDAExtension
|
|
6
|
+
|
|
7
|
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
8
|
+
abi_flag = '-D_GLIBCXX_USE_CXX11_ABI=' + ('1' if torch._C._GLIBCXX_USE_CXX11_ABI else '0')
|
|
9
|
+
|
|
10
|
+
setup(
|
|
11
|
+
name='pathsig',
|
|
12
|
+
version='0.1.1',
|
|
13
|
+
package_dir={'': 'src'},
|
|
14
|
+
packages=['pathsig'],
|
|
15
|
+
ext_modules=[
|
|
16
|
+
CUDAExtension(
|
|
17
|
+
name='pathsig._impl',
|
|
18
|
+
sources=[
|
|
19
|
+
'src/pathsig/pybindings.cpp',
|
|
20
|
+
'src/pathsig/utils/SigDecomposition.cpp',
|
|
21
|
+
'src/pathsig/signature_backward/sig_backprop.cu',
|
|
22
|
+
'src/pathsig/signature_backward/sig_backprop_launch.cu',
|
|
23
|
+
'src/pathsig/signature_forward/compute_sig.cu',
|
|
24
|
+
'src/pathsig/signature_forward/compute_sig_launch.cu',
|
|
25
|
+
'src/pathsig/utils/sig_setup.cu',
|
|
26
|
+
],
|
|
27
|
+
include_dirs=[
|
|
28
|
+
os.path.join(current_dir, 'src/pathsig'),
|
|
29
|
+
os.path.join(current_dir, 'src/pathsig/utils'),
|
|
30
|
+
os.path.join(current_dir, 'src/pathsig/signature_backward'),
|
|
31
|
+
os.path.join(current_dir, 'src/pathsig/signature_forward'),
|
|
32
|
+
],
|
|
33
|
+
extra_compile_args={
|
|
34
|
+
'cxx': ['-O2', abi_flag, '-fvisibility=hidden'],
|
|
35
|
+
'nvcc': ['-O2', abi_flag, '-diag-suppress=20281'],
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
],
|
|
39
|
+
cmdclass={'build_ext': BuildExtension},
|
|
40
|
+
zip_safe=False,
|
|
41
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
from ._impl import SigDecomposition
|
|
4
|
+
from ._autograd import SignatureFunction
|
|
5
|
+
|
|
6
|
+
class Signature(nn.Module):
|
|
7
|
+
def __init__(self, truncation_level: int, extended_precision: bool = False):
|
|
8
|
+
"""
|
|
9
|
+
Args:
|
|
10
|
+
truncation_level: Maximum degree of signature terms
|
|
11
|
+
extended_precision: If True and dtype is double, use higher precision during computation
|
|
12
|
+
"""
|
|
13
|
+
super(Signature, self).__init__()
|
|
14
|
+
self.truncation_level = truncation_level
|
|
15
|
+
self.extended_precision = extended_precision
|
|
16
|
+
self.sig_decomp = None
|
|
17
|
+
self._cached_path_dim = None
|
|
18
|
+
|
|
19
|
+
def forward(self, path: torch.Tensor) -> torch.Tensor:
|
|
20
|
+
"""
|
|
21
|
+
Computes the signatures of the input paths.
|
|
22
|
+
Args:
|
|
23
|
+
path: Input tensor of shape (batch_size, sequence_length, path_dim)
|
|
24
|
+
Returns:
|
|
25
|
+
Tensor containing signatures of shape (batch_size, signature_size)
|
|
26
|
+
"""
|
|
27
|
+
batch_size, sequence_length, path_dim = path.shape
|
|
28
|
+
if (self.sig_decomp is None or
|
|
29
|
+
self._cached_path_dim != path_dim):
|
|
30
|
+
self.sig_decomp = SigDecomposition(path_dim, self.truncation_level)
|
|
31
|
+
self._cached_path_dim = path_dim
|
|
32
|
+
return SignatureFunction.apply(path, self.truncation_level, self.extended_precision, self.sig_decomp)
|
|
33
|
+
|
|
34
|
+
def signature(path: torch.Tensor, truncation_level: int, extended_precision: bool = False, sig_decomp=None) -> torch.Tensor:
|
|
35
|
+
"""
|
|
36
|
+
Computes the signature/signatures of a path/paths up to a given truncation level.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
path: Input tensor of shape (batch_size, num_time_steps, path_dim)
|
|
40
|
+
truncation_level: Maximum degree of signature terms
|
|
41
|
+
extended_precision: If True and path is of dtype FP64, use higher precision in signature computations
|
|
42
|
+
Returns:
|
|
43
|
+
Tensor containing the truncated signature of the input path
|
|
44
|
+
"""
|
|
45
|
+
return SignatureFunction.apply(path, truncation_level, extended_precision, sig_decomp)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from ._impl import compute_sig, compute_sig_gradients, SigDecomposition
|
|
3
|
+
from torch.autograd.function import once_differentiable
|
|
4
|
+
from typing import Optional, Tuple, Union
|
|
5
|
+
|
|
6
|
+
class SignatureFunction(torch.autograd.Function):
|
|
7
|
+
"""
|
|
8
|
+
Custom autograd function for signature computation and differentiation.
|
|
9
|
+
"""
|
|
10
|
+
@staticmethod
|
|
11
|
+
def forward(path: torch.Tensor, truncation_level: int,
|
|
12
|
+
extended_precision: bool = False, sig_decomp: Optional[SigDecomposition] = None) -> torch.Tensor:
|
|
13
|
+
"""
|
|
14
|
+
Args:
|
|
15
|
+
path: Input tensor of shape (batch_size, num_time_steps, path_dim)
|
|
16
|
+
truncation_level: Maximum degree of signature terms
|
|
17
|
+
extended_precision: If True and path is of dtype FP64, use higher precision in signature computation
|
|
18
|
+
Returns:
|
|
19
|
+
Signatures torch tensor of shape (batch_size, signature_size)
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
# Compute signatures
|
|
23
|
+
sig = compute_sig(path, truncation_level, extended_precision, sig_decomp)
|
|
24
|
+
return sig
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def setup_context(ctx, inputs, output):
|
|
28
|
+
"""
|
|
29
|
+
Args:
|
|
30
|
+
ctx: Context object
|
|
31
|
+
inputs: Tuple of inputs to forward
|
|
32
|
+
output: Output of forward
|
|
33
|
+
"""
|
|
34
|
+
path, truncation_level, extended_precision, sig_decomp = inputs
|
|
35
|
+
sig = output
|
|
36
|
+
if path.requires_grad:
|
|
37
|
+
ctx.save_for_backward(path, sig)
|
|
38
|
+
ctx.truncation_level = truncation_level
|
|
39
|
+
ctx.sig_decomp = sig_decomp
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def backward(ctx, incoming_gradients: torch.Tensor) -> Tuple[torch.Tensor, None, None, None]:
|
|
43
|
+
"""
|
|
44
|
+
Args:
|
|
45
|
+
ctx: Context object with saved tensors
|
|
46
|
+
incoming_gradients: Gradient of loss w.r.t. signature
|
|
47
|
+
Returns:
|
|
48
|
+
gradient of signature w.r.t. input path, and None for other inputs
|
|
49
|
+
"""
|
|
50
|
+
path, sig = ctx.saved_tensors
|
|
51
|
+
path_grad = compute_sig_gradients(
|
|
52
|
+
path, sig, incoming_gradients, ctx.truncation_level, ctx.sig_decomp
|
|
53
|
+
)
|
|
54
|
+
return path_grad, None, None, None
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// pybindings.cpp
|
|
2
|
+
#include <torch/extension.h>
|
|
3
|
+
#include "compute_sig_launch.cuh"
|
|
4
|
+
#include "sig_backprop_launch.cuh"
|
|
5
|
+
#include "SigDecomposition.h"
|
|
6
|
+
|
|
7
|
+
namespace py = pybind11;
|
|
8
|
+
|
|
9
|
+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
|
10
|
+
py::class_<pathsig::SigDecomposition>(m, "SigDecomposition")
|
|
11
|
+
.def(py::init<int,int>(),
|
|
12
|
+
py::arg("path_dim"), py::arg("trunc_level"));
|
|
13
|
+
|
|
14
|
+
m.def("compute_sig", &pathsig::computeSignature,
|
|
15
|
+
py::arg("path"),
|
|
16
|
+
py::arg("truncation_level"),
|
|
17
|
+
py::arg("extended_precision") = false,
|
|
18
|
+
py::arg("sig_decomp") = py::none(), // None maps to nullptr
|
|
19
|
+
py::call_guard<py::gil_scoped_release>());
|
|
20
|
+
|
|
21
|
+
m.def("compute_sig_gradients", &pathsig::computeSigGradients,
|
|
22
|
+
py::arg("path"),
|
|
23
|
+
py::arg("signature"),
|
|
24
|
+
py::arg("incoming_grads"),
|
|
25
|
+
py::arg("truncation_level"),
|
|
26
|
+
py::arg("sig_decomp") = py::none(),
|
|
27
|
+
py::call_guard<py::gil_scoped_release>());
|
|
28
|
+
|
|
29
|
+
m.def("sig_size",
|
|
30
|
+
&pathsig::computeSigSize,
|
|
31
|
+
py::arg("path_dim"), py::arg("truncation_level"),
|
|
32
|
+
"Total number of signature terms excluding the level 0 identity term.");
|
|
33
|
+
}
|