torch-einops-utils 0.1.1__tar.gz → 0.1.2__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.
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/PKG-INFO +1 -1
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/pyproject.toml +1 -1
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/tests/test_utils.py +13 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/torch_einops_utils/__init__.py +2 -1
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/torch_einops_utils/torch_einops_utils.py +17 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/.gitignore +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/LICENSE +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/README.md +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/tests/test_device.py +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/tests/test_nn.py +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/tests/test_save_load.py +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/torch_einops_utils/device.py +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/torch_einops_utils/nn.py +0 -0
- {torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/torch_einops_utils/save_load.py +0 -0
|
@@ -325,3 +325,16 @@ def test_pad_right_ndim_to_and_expand_as():
|
|
|
325
325
|
|
|
326
326
|
assert (scattered[:, :4] == 1.).all()
|
|
327
327
|
assert (scattered[:, 4:] == 0.).all()
|
|
328
|
+
|
|
329
|
+
def test_repeat_interleave_to_match():
|
|
330
|
+
from torch_einops_utils.torch_einops_utils import repeat_interleave_to_match
|
|
331
|
+
time_lens = torch.tensor([2, 3])
|
|
332
|
+
|
|
333
|
+
out = repeat_interleave_to_match(time_lens, torch.randn(4, 512))
|
|
334
|
+
assert out.tolist() == [2, 2, 3, 3]
|
|
335
|
+
|
|
336
|
+
out2 = repeat_interleave_to_match(time_lens, torch.randn(6, 128))
|
|
337
|
+
assert out2.tolist() == [2, 2, 2, 3, 3, 3]
|
|
338
|
+
|
|
339
|
+
out3 = repeat_interleave_to_match(time_lens, 6)
|
|
340
|
+
assert out3.tolist() == [2, 2, 2, 3, 3, 3]
|
|
@@ -58,7 +58,8 @@ from torch_einops_utils.torch_einops_utils import (
|
|
|
58
58
|
)
|
|
59
59
|
|
|
60
60
|
from torch_einops_utils.torch_einops_utils import (
|
|
61
|
-
pad_right_ndim_to_and_expand_as
|
|
61
|
+
pad_right_ndim_to_and_expand_as,
|
|
62
|
+
repeat_interleave_to_match
|
|
62
63
|
)
|
|
63
64
|
|
|
64
65
|
from torch_einops_utils.nn import (
|
{torch_einops_utils-0.1.1 → torch_einops_utils-0.1.2}/torch_einops_utils/torch_einops_utils.py
RENAMED
|
@@ -17,6 +17,9 @@ def exists(v):
|
|
|
17
17
|
def default(v, d):
|
|
18
18
|
return v if exists(v) else d
|
|
19
19
|
|
|
20
|
+
def divisible_by(num, den):
|
|
21
|
+
return (num % den) == 0
|
|
22
|
+
|
|
20
23
|
def identity(t, *args, **kwargs):
|
|
21
24
|
return t
|
|
22
25
|
|
|
@@ -357,3 +360,17 @@ def pad_right_ndim_to_and_expand_as(source, target):
|
|
|
357
360
|
source = pad_right_ndim_to(source, target.ndim)
|
|
358
361
|
shape = [*source.shape[:source_ndim], *target.shape[source_ndim:]]
|
|
359
362
|
return source.expand(shape)
|
|
363
|
+
|
|
364
|
+
# repeat
|
|
365
|
+
|
|
366
|
+
def repeat_interleave_to_match(t, target, dim = 0, target_dim = None):
|
|
367
|
+
target_dim = default(target_dim, dim)
|
|
368
|
+
|
|
369
|
+
len_t = t.shape[dim]
|
|
370
|
+
len_target = target if isinstance(target, int) else target.shape[target_dim]
|
|
371
|
+
|
|
372
|
+
if len_t == len_target:
|
|
373
|
+
return t
|
|
374
|
+
|
|
375
|
+
assert divisible_by(len_target, len_t)
|
|
376
|
+
return t.repeat_interleave(len_target // len_t, dim = dim)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|