torchoef 0.1.0__py3-none-any.whl

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/__init__.py ADDED
@@ -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))