torchoef 0.1.0__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.
torchoef-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Haloroute
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.
@@ -0,0 +1,100 @@
1
+ Metadata-Version: 2.4
2
+ Name: torchoef
3
+ Version: 0.1.0
4
+ Summary: Differentiable parallel One-Euro filter for PyTorch using fixed-point iteration and associative scans.
5
+ Author: Haloroute
6
+ Author-email: Haloroute <topaz.and.numpy@gmail.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Requires-Dist: torch>=2.9.0
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+
13
+ # torchoef
14
+
15
+ Differentiable, parallel approximate [One-Euro filter](https://gery.casiez.net/1euro/) for PyTorch using fixed-point iteration and first-order parallel associative scans.
16
+
17
+ ## Overview
18
+
19
+ The standard One-Euro filter is sequential across time because its adaptive cutoff frequency depends on the smoothed velocity of the previously filtered signal. `torchoef` approximates it in parallel over the time dimension by iterating fixed-point updates, where each iteration solves two first-order linear recurrences (velocity smoothing and signal smoothing) via parallel associative scan.
20
+
21
+ - **Variable-length & irregular sampling**: Supports arbitrary ascending timestamps (`timeline`) and frame validity masks (`mask`).
22
+ - **Fixed-parameter & learnable APIs**: Exposes `OneEuroFilter` (takes explicit `f_min`, `beta`, `f_d` tensors) and `OneEuroFilterModule` (per-channel learnable `f_min`, `beta`, and `f_d` constrained via `softplus`).
23
+ - **Memory-efficient gradients**: Optional `skip_grad=True` computes gradients only through the final fixed-point iteration.
24
+ - **Scan backends**:
25
+ - `torch` (default): Pure PyTorch parallel scan (CPU & CUDA).
26
+ - `triton`: Triton GPU scan kernel (requires CUDA and Triton).
27
+ - `cuda`: Custom CUDA warp-scan kernel JIT-compiled via `load_inline` (requires CUDA toolkit).
28
+
29
+ ## Installation
30
+
31
+ Requires Python >= 3.10 and PyTorch >= 2.9.0.
32
+
33
+ ```bash
34
+ pip install torchoef
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ### Fixed-Parameter `OneEuroFilter`
40
+
41
+ ```python
42
+ import torch
43
+ from torchoef import OneEuroFilter
44
+
45
+ N, C, T = 2, 3, 128
46
+ signal = torch.randn(N, C, T)
47
+ mask = torch.ones(N, T, dtype=torch.bool)
48
+ timeline = torch.linspace(0.0, 1.0, T).unsqueeze(0).expand(N, T)
49
+
50
+ f_min = torch.full((C,), 1.0) # > 0
51
+ beta = torch.full((C,), 0.1) # >= 0
52
+ f_d = torch.full((C,), 1.0) # > 0
53
+
54
+ oef = OneEuroFilter(
55
+ n_loops=4,
56
+ skip_grad=False,
57
+ backend="torch", # "torch" | "triton" | "cuda"
58
+ )
59
+ filtered = oef(signal, mask, timeline, f_min, beta, f_d)
60
+ ```
61
+
62
+ ### Learnable `OneEuroFilterModule`
63
+
64
+ ```python
65
+ import torch
66
+ from torchoef import OneEuroFilterModule
67
+
68
+ N, C, T = 2, 3, 128
69
+ signal = torch.randn(N, C, T)
70
+ mask = torch.ones(N, T, dtype=torch.bool)
71
+ timeline = torch.linspace(0.0, 1.0, T).unsqueeze(0).expand(N, T)
72
+
73
+ oef = OneEuroFilterModule(
74
+ n_channels=C,
75
+ n_loops=4,
76
+ skip_grad=False,
77
+ backend="torch",
78
+ )
79
+ filtered = oef(signal, mask, timeline)
80
+ ```
81
+
82
+ ## API Reference
83
+
84
+ | Symbol | Key Arguments |
85
+ | --- | --- |
86
+ | `OneEuroFilter` | `__init__(eps=1e-6, n_loops=4, skip_grad=False, backend="torch")`; `forward(signal (N, C, T), mask (N, T), timeline (N, T), f_min (C,), beta (C,), f_d (C,)) -> (N, C, T)` |
87
+ | `OneEuroFilterModule` | `__init__(n_channels, eps=1e-6, n_loops=4, skip_grad=False, backend="torch")`; `forward(signal (N, C, T), mask (N, T), timeline (N, T)) -> (N, C, T)` |
88
+
89
+ - `mask`: `1` / `True` marks valid frames, `0` / `False` marks masked/padding frames.
90
+ - `timeline`: Frame timestamps in ascending order per sequence.
91
+ - `n_loops`: Number of fixed-point iterations (`>= 1`).
92
+
93
+ ## Acknowledgements
94
+
95
+ - Parallel associative scan implementations (`torchoef/accelerated_scan`) are adapted from [proger/accelerated-scan: Accelerated First Order Parallel Associative Scan](https://github.com/proger/accelerated-scan).
96
+ - Based on the [1€ Filter](https://gery.casiez.net/1euro/) (Casiez, Roussel, and Vogel, CHI 2012).
97
+
98
+ ## License
99
+
100
+ MIT License.
@@ -0,0 +1,88 @@
1
+ # torchoef
2
+
3
+ Differentiable, parallel approximate [One-Euro filter](https://gery.casiez.net/1euro/) for PyTorch using fixed-point iteration and first-order parallel associative scans.
4
+
5
+ ## Overview
6
+
7
+ The standard One-Euro filter is sequential across time because its adaptive cutoff frequency depends on the smoothed velocity of the previously filtered signal. `torchoef` approximates it in parallel over the time dimension by iterating fixed-point updates, where each iteration solves two first-order linear recurrences (velocity smoothing and signal smoothing) via parallel associative scan.
8
+
9
+ - **Variable-length & irregular sampling**: Supports arbitrary ascending timestamps (`timeline`) and frame validity masks (`mask`).
10
+ - **Fixed-parameter & learnable APIs**: Exposes `OneEuroFilter` (takes explicit `f_min`, `beta`, `f_d` tensors) and `OneEuroFilterModule` (per-channel learnable `f_min`, `beta`, and `f_d` constrained via `softplus`).
11
+ - **Memory-efficient gradients**: Optional `skip_grad=True` computes gradients only through the final fixed-point iteration.
12
+ - **Scan backends**:
13
+ - `torch` (default): Pure PyTorch parallel scan (CPU & CUDA).
14
+ - `triton`: Triton GPU scan kernel (requires CUDA and Triton).
15
+ - `cuda`: Custom CUDA warp-scan kernel JIT-compiled via `load_inline` (requires CUDA toolkit).
16
+
17
+ ## Installation
18
+
19
+ Requires Python >= 3.10 and PyTorch >= 2.9.0.
20
+
21
+ ```bash
22
+ pip install torchoef
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Fixed-Parameter `OneEuroFilter`
28
+
29
+ ```python
30
+ import torch
31
+ from torchoef import OneEuroFilter
32
+
33
+ N, C, T = 2, 3, 128
34
+ signal = torch.randn(N, C, T)
35
+ mask = torch.ones(N, T, dtype=torch.bool)
36
+ timeline = torch.linspace(0.0, 1.0, T).unsqueeze(0).expand(N, T)
37
+
38
+ f_min = torch.full((C,), 1.0) # > 0
39
+ beta = torch.full((C,), 0.1) # >= 0
40
+ f_d = torch.full((C,), 1.0) # > 0
41
+
42
+ oef = OneEuroFilter(
43
+ n_loops=4,
44
+ skip_grad=False,
45
+ backend="torch", # "torch" | "triton" | "cuda"
46
+ )
47
+ filtered = oef(signal, mask, timeline, f_min, beta, f_d)
48
+ ```
49
+
50
+ ### Learnable `OneEuroFilterModule`
51
+
52
+ ```python
53
+ import torch
54
+ from torchoef import OneEuroFilterModule
55
+
56
+ N, C, T = 2, 3, 128
57
+ signal = torch.randn(N, C, T)
58
+ mask = torch.ones(N, T, dtype=torch.bool)
59
+ timeline = torch.linspace(0.0, 1.0, T).unsqueeze(0).expand(N, T)
60
+
61
+ oef = OneEuroFilterModule(
62
+ n_channels=C,
63
+ n_loops=4,
64
+ skip_grad=False,
65
+ backend="torch",
66
+ )
67
+ filtered = oef(signal, mask, timeline)
68
+ ```
69
+
70
+ ## API Reference
71
+
72
+ | Symbol | Key Arguments |
73
+ | --- | --- |
74
+ | `OneEuroFilter` | `__init__(eps=1e-6, n_loops=4, skip_grad=False, backend="torch")`; `forward(signal (N, C, T), mask (N, T), timeline (N, T), f_min (C,), beta (C,), f_d (C,)) -> (N, C, T)` |
75
+ | `OneEuroFilterModule` | `__init__(n_channels, eps=1e-6, n_loops=4, skip_grad=False, backend="torch")`; `forward(signal (N, C, T), mask (N, T), timeline (N, T)) -> (N, C, T)` |
76
+
77
+ - `mask`: `1` / `True` marks valid frames, `0` / `False` marks masked/padding frames.
78
+ - `timeline`: Frame timestamps in ascending order per sequence.
79
+ - `n_loops`: Number of fixed-point iterations (`>= 1`).
80
+
81
+ ## Acknowledgements
82
+
83
+ - Parallel associative scan implementations (`torchoef/accelerated_scan`) are adapted from [proger/accelerated-scan: Accelerated First Order Parallel Associative Scan](https://github.com/proger/accelerated-scan).
84
+ - Based on the [1€ Filter](https://gery.casiez.net/1euro/) (Casiez, Roussel, and Vogel, CHI 2012).
85
+
86
+ ## License
87
+
88
+ MIT License.
@@ -0,0 +1,20 @@
1
+ [project]
2
+ name = "torchoef"
3
+ version = "0.1.0"
4
+ license = "MIT"
5
+ license-files = ["LICENSE"]
6
+ description = "Differentiable parallel One-Euro filter for PyTorch using fixed-point iteration and associative scans."
7
+ readme = "README.md"
8
+ requires-python = ">=3.10"
9
+ dependencies = ["torch>=2.9.0"]
10
+
11
+ [[project.authors]]
12
+ name = "Haloroute"
13
+ email = "topaz.and.numpy@gmail.com"
14
+
15
+ [build-system]
16
+ requires = ["uv_build>=0.13.9,<0.14.0"]
17
+ build-backend = "uv_build"
18
+
19
+ [tool.uv.build-backend]
20
+ module-root = ""
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "torchoef"
3
+ version = "0.1.0"
4
+ license = "MIT"
5
+ license-files = ["LICENSE"]
6
+ description = "Differentiable parallel One-Euro filter for PyTorch using fixed-point iteration and associative scans."
7
+ readme = "README.md"
8
+ authors = [
9
+ { name = "Haloroute", email = "topaz.and.numpy@gmail.com" }
10
+ ]
11
+ requires-python = ">=3.10"
12
+ dependencies = [
13
+ "torch>=2.9.0",
14
+ ]
15
+
16
+ [build-system]
17
+ requires = ["uv_build>=0.13.9,<0.14.0"]
18
+ build-backend = "uv_build"
19
+
20
+ [tool.uv.build-backend]
21
+ module-root = ""
@@ -0,0 +1,3 @@
1
+ from .oef import OneEuroFilter, OneEuroFilterModule
2
+
3
+ __all__ = ["OneEuroFilter", "OneEuroFilterModule"]
@@ -0,0 +1 @@
1
+ __version__ = '0.3.1'
@@ -0,0 +1,123 @@
1
+ import math
2
+ from typing import Callable
3
+
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+
8
+ def split(x: torch.Tensor) -> torch.Tensor:
9
+ """
10
+ Take a sequence of inputs that represents a tree level,
11
+ and return all left children and all right children.
12
+
13
+ Arguments:
14
+ x (torch.Tensor): shape (B, C, T)
15
+
16
+ Returns:
17
+ (torch.Tensor, torch.Tensor): shapes (B, C, T//2), (B, C, T//2)
18
+
19
+ >>> split(torch.tensor([1,2,3,4,5,6,7,8])[None, None, :])
20
+ (tensor([[[1, 3, 5, 7]]]), tensor([[[2, 4, 6, 8]]])
21
+ """
22
+ B, C, T = x.size()
23
+ x = x.view(B, C, T//2, 2)
24
+ return x[: , :, :, 0], x[:, :, :, 1]
25
+
26
+
27
+ def merge(lefts: torch.Tensor, rights: torch.Tensor) -> torch.Tensor:
28
+ """
29
+ Take sequences of all left children and sequences of all right children and merge them
30
+ into a single tree level.
31
+
32
+ Arguments:
33
+ lefts (torch.Tensor): shape (B, C, T//2)
34
+ rights (torch.Tensor): shape (B, C, T//2)
35
+
36
+ Returns:
37
+ (torch.Tensor): shape (B, C, T)
38
+
39
+ >>> lefts = torch.tensor([1,3,5,7])[None, None, :]
40
+ >>> rights = torch.tensor([2,4,6,8])[None, None, :]
41
+ >>> merge(lefts, rights)
42
+ tensor([[[1, 2, 3, 4, 5, 6, 7, 8]]])
43
+ """
44
+ B, C, half = lefts.size()
45
+ x = torch.stack([lefts, rights], dim=-1) # (bsz, dim, half, 2)
46
+ return x.view(B, C, half*2)
47
+
48
+
49
+ def scan(
50
+ gates: torch.Tensor,
51
+ tokens: torch.Tensor,
52
+ mul: Callable = torch.mul,
53
+ add: Callable = torch.add,
54
+ zeros_like: Callable = torch.zeros_like,
55
+ ones_like: Callable = torch.ones_like,
56
+ reverse: bool = False
57
+ ) -> torch.Tensor:
58
+ """Solve a first-order recurrence relation using a reference torch implementation:
59
+
60
+ .. math::
61
+ x_t = a_t x_{t-1} + b_t
62
+
63
+ where :math:`a_t` ("gates") and :math:`b_t` ("tokens") are sequences of vectors.
64
+
65
+ Arguments:
66
+ gates (torch.Tensor): shape (B, C, T), must be contiguous.
67
+ tokens (torch.Tensor): shape (B, C, T), must be contiguous.
68
+ mul (callable): multiplication function, defaults to torch.mul
69
+ add (callable): addition function, defaults to torch.add
70
+ zeros_like (callable): function to create a tensor of zeros like the input, defaults to torch.zeros_like
71
+ ones_like (callable): function to create a tensor of ones like the input, defaults to torch.ones_like
72
+ reverse (bool): whether to solve the recurrence in reverse order, defaults to False
73
+
74
+ Returns:
75
+ (torch.Tensor): shape (B, C, T)
76
+ """
77
+ B, C, T = tokens.size()
78
+ if T == 1: # base case: single time step, return the token directly
79
+ return tokens.clone()
80
+ padded_T = 1 << (T - 1).bit_length()
81
+ if padded_T != T:
82
+ pad_len = padded_T - T
83
+ gates = F.pad(gates, (0, pad_len), value=1).contiguous()
84
+ tokens = F.pad(tokens, (0, pad_len), value=0).contiguous()
85
+
86
+ level = int(math.log2(padded_T))
87
+ _, x = scan1(gates, tokens, mul, add, zeros_like, ones_like, level=level, reverse=reverse)
88
+ out = add(mul(x, gates), tokens)
89
+ return out[:, :, :T]
90
+
91
+
92
+ def scan1(
93
+ gates: torch.Tensor,
94
+ tokens: torch.Tensor,
95
+ mul: Callable,
96
+ add: Callable,
97
+ zeros_like: Callable,
98
+ ones_like: Callable,
99
+ level: int,
100
+ reverse: bool = False
101
+ ):
102
+ if reverse:
103
+ right_gates, left_gates = split(gates)
104
+ right_x, left_x = split(tokens)
105
+ else:
106
+ left_gates, right_gates = split(gates)
107
+ left_x, right_x = split(tokens)
108
+
109
+ # up: sum together
110
+ gates = mul(left_gates, right_gates)
111
+ tokens = add(mul(right_gates, left_x), right_x)
112
+
113
+ if level == 1:
114
+ root_gates, root_x = ones_like(tokens), zeros_like(tokens)
115
+ else:
116
+ root_gates, root_x = scan1(gates, tokens, mul, add, zeros_like, ones_like, level=level-1, reverse=reverse)
117
+
118
+ if reverse:
119
+ # down: right is root, left is left (+) right
120
+ return merge(mul(root_gates, left_gates), root_gates), merge(add(mul(root_x, left_gates), left_x), root_x)
121
+ else:
122
+ # down: left is root, right is left (+) right
123
+ return merge(root_gates, mul(root_gates, left_gates)), merge(root_x, add(mul(root_x, left_gates), left_x))