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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: torch-einops-utils
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Personal utility functions
5
5
  Project-URL: Homepage, https://pypi.org/project/torch-einops-utils/
6
6
  Project-URL: Repository, https://github.com/lucidrains/torch-einops-utils
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "torch-einops-utils"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "Personal utility functions"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -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 (
@@ -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)