torch-einops-utils 0.1.0__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.0
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.0"
3
+ version = "0.1.2"
4
4
  description = "Personal utility functions"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -310,3 +310,31 @@ def test_shift():
310
310
  def test_reverse_cumsum():
311
311
  t = tensor([1, 2, 3])
312
312
  assert reverse_cumsum(t).tolist() == [6, 5, 3]
313
+
314
+ def test_pad_right_ndim_to_and_expand_as():
315
+ from torch_einops_utils.torch_einops_utils import pad_right_ndim_to_and_expand_as
316
+
317
+ target = torch.randn(2, 8, 64)
318
+ source = torch.randint(0, 8, (2, 4))
319
+ assert pad_right_ndim_to_and_expand_as(source, target).shape == (2, 4, 64)
320
+
321
+ dest = torch.zeros(2, 8, 64)
322
+ source = torch.arange(4).unsqueeze(0).expand(2, -1)
323
+
324
+ scattered = dest.scatter(1, pad_right_ndim_to_and_expand_as(source, dest), torch.ones(2, 4, 64))
325
+
326
+ assert (scattered[:, :4] == 1.).all()
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]
@@ -57,6 +57,11 @@ from torch_einops_utils.torch_einops_utils import (
57
57
  pack_with_inverse
58
58
  )
59
59
 
60
+ from torch_einops_utils.torch_einops_utils import (
61
+ pad_right_ndim_to_and_expand_as,
62
+ repeat_interleave_to_match
63
+ )
64
+
60
65
  from torch_einops_utils.nn import (
61
66
  Sequential,
62
67
  Lambda,
@@ -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
 
@@ -349,3 +352,25 @@ def pack_with_inverse(t, pattern):
349
352
  return out
350
353
 
351
354
  return packed, inverse
355
+
356
+ # gather and scatter
357
+
358
+ def pad_right_ndim_to_and_expand_as(source, target):
359
+ source_ndim = source.ndim
360
+ source = pad_right_ndim_to(source, target.ndim)
361
+ shape = [*source.shape[:source_ndim], *target.shape[source_ndim:]]
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)