vector-quantize-pytorch 1.15.2__tar.gz → 1.15.4__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.
Files changed (26) hide show
  1. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/PKG-INFO +11 -2
  2. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/README.md +9 -0
  3. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/pyproject.toml +2 -2
  4. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/tests/test_readme.py +25 -0
  5. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/vector_quantize_pytorch.py +29 -5
  6. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/.github/workflows/build.yml +0 -0
  7. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/.github/workflows/python-publish.yml +0 -0
  8. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/.github/workflows/test.yml +0 -0
  9. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/.gitignore +0 -0
  10. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/LICENSE +0 -0
  11. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/examples/autoencoder.py +0 -0
  12. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/examples/autoencoder_fsq.py +0 -0
  13. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/examples/autoencoder_lfq.py +0 -0
  14. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/images/fsq.png +0 -0
  15. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/images/lfq.png +0 -0
  16. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/images/vq.png +0 -0
  17. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/ruff.toml +0 -0
  18. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/tests/test_latent_quantization.py +0 -0
  19. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/__init__.py +0 -0
  20. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/finite_scalar_quantization.py +0 -0
  21. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/latent_quantization.py +0 -0
  22. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/lookup_free_quantization.py +0 -0
  23. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/random_projection_quantizer.py +0 -0
  24. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/residual_fsq.py +0 -0
  25. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/residual_lfq.py +0 -0
  26. {vector_quantize_pytorch-1.15.2 → vector_quantize_pytorch-1.15.4}/vector_quantize_pytorch/residual_vq.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: vector-quantize-pytorch
3
- Version: 1.15.2
3
+ Version: 1.15.4
4
4
  Summary: Vector Quantization - Pytorch
5
5
  Project-URL: Homepage, https://pypi.org/project/vector-quantize-pytorch/
6
6
  Project-URL: Repository, https://github.com/lucidrains/vector-quantizer-pytorch
@@ -35,7 +35,7 @@ Classifier: Programming Language :: Python :: 3.6
35
35
  Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
36
36
  Requires-Python: >=3.9
37
37
  Requires-Dist: einops>=0.8.0
38
- Requires-Dist: einx>=0.2.2
38
+ Requires-Dist: einx>=0.3.0
39
39
  Requires-Dist: torch>=2.0
40
40
  Provides-Extra: examples
41
41
  Requires-Dist: torchvision; extra == 'examples'
@@ -723,3 +723,12 @@ assert loss.item() >= 0
723
723
  primaryClass = {cs.LG}
724
724
  }
725
725
  ```
726
+
727
+ ```bibtex
728
+ @inproceedings{Irie2023SelfOrganisingND,
729
+ title = {Self-Organising Neural Discrete Representation Learning \`a la Kohonen},
730
+ author = {Kazuki Irie and R'obert Csord'as and J{\"u}rgen Schmidhuber},
731
+ year = {2023},
732
+ url = {https://api.semanticscholar.org/CorpusID:256901024}
733
+ }
734
+ ```
@@ -679,3 +679,12 @@ assert loss.item() >= 0
679
679
  primaryClass = {cs.LG}
680
680
  }
681
681
  ```
682
+
683
+ ```bibtex
684
+ @inproceedings{Irie2023SelfOrganisingND,
685
+ title = {Self-Organising Neural Discrete Representation Learning \`a la Kohonen},
686
+ author = {Kazuki Irie and R'obert Csord'as and J{\"u}rgen Schmidhuber},
687
+ year = {2023},
688
+ url = {https://api.semanticscholar.org/CorpusID:256901024}
689
+ }
690
+ ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "vector-quantize-pytorch"
3
- version = "1.15.2"
3
+ version = "1.15.4"
4
4
  description = "Vector Quantization - Pytorch"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -25,7 +25,7 @@ classifiers=[
25
25
  dependencies = [
26
26
  "torch>=2.0",
27
27
  "einops>=0.8.0",
28
- "einx>=0.2.2",
28
+ "einx>=0.3.0",
29
29
  ]
30
30
 
31
31
  [project.urls]
@@ -37,6 +37,31 @@ def test_vq_eval():
37
37
  quantized, indices, commit_loss = vq(x)
38
38
  assert torch.allclose(quantized, vq.get_output_from_indices(indices))
39
39
 
40
+ def test_vq_mask():
41
+ from vector_quantize_pytorch import VectorQuantize
42
+
43
+ vq = VectorQuantize(
44
+ dim = 256,
45
+ codebook_size = 512, # codebook size
46
+ decay = 1., # the exponential moving average decay, lower means the dictionary will change faster
47
+ commitment_weight = 1. # the weight on the commitment loss
48
+ )
49
+
50
+ x = torch.randn(1, 1024, 256)
51
+ lens = torch.full((1,), 512)
52
+
53
+ vq.train()
54
+
55
+ quantized, indices, commit_loss = vq(x[:, :512])
56
+ mask_quantized, mask_indices, mask_commit_loss = vq(x, lens = lens)
57
+
58
+ assert torch.allclose(commit_loss, mask_commit_loss)
59
+ assert torch.allclose(quantized, mask_quantized[:, :512])
60
+ assert torch.allclose(indices, mask_indices[:, :512])
61
+
62
+ assert torch.allclose(mask_quantized[:, 512:], x[:, 512:])
63
+ assert (mask_indices[:, 512:] == -1).all()
64
+
40
65
  def test_residual_vq():
41
66
  from vector_quantize_pytorch import ResidualVQ
42
67
 
@@ -3,12 +3,13 @@ from collections import namedtuple
3
3
 
4
4
  import torch
5
5
  from torch.nn import Module
6
- from torch import nn, einsum
6
+ from torch import nn, einsum, Tensor
7
7
  import torch.nn.functional as F
8
8
  import torch.distributed as distributed
9
9
  from torch.optim import Optimizer
10
10
  from torch.cuda.amp import autocast
11
11
 
12
+ import einx
12
13
  from einops import rearrange, repeat, reduce, pack, unpack
13
14
 
14
15
  from typing import Callable
@@ -63,6 +64,10 @@ def pack_one(t, pattern):
63
64
  def unpack_one(t, ps, pattern):
64
65
  return unpack(t, ps, pattern)[0]
65
66
 
67
+ def lens_to_mask(lens, max_length):
68
+ seq = torch.arange(max_length, device = lens.device)
69
+ return seq < lens[:, None]
70
+
66
71
  def uniform_init(*shape):
67
72
  t = torch.empty(shape)
68
73
  nn.init.kaiming_uniform_(t)
@@ -312,7 +317,7 @@ class EuclideanCodebook(Module):
312
317
  self.all_reduce_fn = distributed.all_reduce if use_ddp else noop
313
318
 
314
319
  self.register_buffer('initted', torch.Tensor([not kmeans_init]))
315
- self.register_buffer('cluster_size', torch.zeros(num_codebooks, codebook_size))
320
+ self.register_buffer('cluster_size', torch.ones(num_codebooks, codebook_size))
316
321
  self.register_buffer('embed_avg', embed.clone())
317
322
 
318
323
  self.learnable_codebook = learnable_codebook
@@ -582,7 +587,7 @@ class CosineSimCodebook(Module):
582
587
  self.all_reduce_fn = distributed.all_reduce if use_ddp else noop
583
588
 
584
589
  self.register_buffer('initted', torch.Tensor([not kmeans_init]))
585
- self.register_buffer('cluster_size', torch.zeros(num_codebooks, codebook_size))
590
+ self.register_buffer('cluster_size', torch.ones(num_codebooks, codebook_size))
586
591
  self.register_buffer('embed_avg', embed.clone())
587
592
 
588
593
  self.learnable_codebook = learnable_codebook
@@ -897,12 +902,22 @@ class VectorQuantize(Module):
897
902
  x,
898
903
  indices = None,
899
904
  mask = None,
905
+ lens = None,
900
906
  sample_codebook_temp = None,
901
907
  freeze_codebook = False,
902
908
  return_loss_breakdown = False,
903
909
  ):
904
910
  orig_input = x
905
911
 
912
+ # handle masking, either passed in as `mask` or `lens`
913
+
914
+ assert not (exists(mask) and exists(lens))
915
+
916
+ if exists(lens):
917
+ mask = lens_to_mask(lens, x.shape[1])
918
+
919
+ # handle one token given
920
+
906
921
  only_one = x.ndim == 2
907
922
 
908
923
  if only_one:
@@ -917,6 +932,7 @@ class VectorQuantize(Module):
917
932
  # rearrange inputs
918
933
 
919
934
  if self.accept_image_fmap:
935
+ assert not exists(mask)
920
936
  height, width = x.shape[-2:]
921
937
  x = rearrange(x, 'b c h w -> b (h w) c')
922
938
 
@@ -1117,12 +1133,20 @@ class VectorQuantize(Module):
1117
1133
  # if masking, only return quantized for where mask has True
1118
1134
 
1119
1135
  if exists(mask):
1120
- quantize = torch.where(
1121
- rearrange(mask, '... -> ... 1'),
1136
+ quantize = einx.where(
1137
+ 'b n, b n d, b n d -> b n d',
1138
+ mask,
1122
1139
  quantize,
1123
1140
  orig_input
1124
1141
  )
1125
1142
 
1143
+ embed_ind = einx.where(
1144
+ 'b n, b n ..., -> b n ...',
1145
+ mask,
1146
+ embed_ind,
1147
+ -1
1148
+ )
1149
+
1126
1150
  if not return_loss_breakdown:
1127
1151
  return quantize, embed_ind, loss
1128
1152