vector-quantize-pytorch 1.14.2__tar.gz → 1.14.5__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 (19) hide show
  1. {vector_quantize_pytorch-1.14.2/vector_quantize_pytorch.egg-info → vector_quantize_pytorch-1.14.5}/PKG-INFO +1 -1
  2. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/setup.py +1 -1
  3. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/finite_scalar_quantization.py +18 -2
  4. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/vector_quantize_pytorch.py +12 -8
  5. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5/vector_quantize_pytorch.egg-info}/PKG-INFO +1 -1
  6. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/LICENSE +0 -0
  7. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/README.md +0 -0
  8. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/setup.cfg +0 -0
  9. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/__init__.py +0 -0
  10. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/latent_quantization.py +0 -0
  11. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/lookup_free_quantization.py +0 -0
  12. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/random_projection_quantizer.py +0 -0
  13. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/residual_fsq.py +0 -0
  14. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/residual_lfq.py +0 -0
  15. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch/residual_vq.py +0 -0
  16. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch.egg-info/SOURCES.txt +0 -0
  17. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch.egg-info/dependency_links.txt +0 -0
  18. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch.egg-info/requires.txt +0 -0
  19. {vector_quantize_pytorch-1.14.2 → vector_quantize_pytorch-1.14.5}/vector_quantize_pytorch.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vector_quantize_pytorch
3
- Version: 1.14.2
3
+ Version: 1.14.5
4
4
  Summary: Vector Quantization - Pytorch
5
5
  Home-page: https://github.com/lucidrains/vector-quantizer-pytorch
6
6
  Author: Phil Wang
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
3
3
  setup(
4
4
  name = 'vector_quantize_pytorch',
5
5
  packages = find_packages(),
6
- version = '1.14.2',
6
+ version = '1.14.5',
7
7
  license='MIT',
8
8
  description = 'Vector Quantization - Pytorch',
9
9
  long_description_content_type = 'text/markdown',
@@ -3,7 +3,7 @@ Finite Scalar Quantization: VQ-VAE Made Simple - https://arxiv.org/abs/2309.1550
3
3
  Code adapted from Jax version in Appendix A.1
4
4
  """
5
5
 
6
- from typing import List, Optional
6
+ from typing import List, Tuple, Optional
7
7
 
8
8
  import torch
9
9
  import torch.nn as nn
@@ -46,7 +46,8 @@ class FSQ(Module):
46
46
  dim: Optional[int] = None,
47
47
  num_codebooks = 1,
48
48
  keep_num_codebooks_dim: Optional[bool] = None,
49
- scale: Optional[float] = None
49
+ scale: Optional[float] = None,
50
+ allowed_dtypes: Tuple[torch.dtype, ...] = (torch.float32, torch.float64)
50
51
  ):
51
52
  super().__init__()
52
53
  _levels = torch.tensor(levels, dtype=int32)
@@ -80,6 +81,8 @@ class FSQ(Module):
80
81
  implicit_codebook = self.indices_to_codes(torch.arange(self.codebook_size), project_out = False)
81
82
  self.register_buffer("implicit_codebook", implicit_codebook, persistent = False)
82
83
 
84
+ self.allowed_dtypes = allowed_dtypes
85
+
83
86
  def bound(self, z: Tensor, eps: float = 1e-3) -> Tensor:
84
87
  """Bound `z`, an array of shape (..., d)."""
85
88
  half_l = (self._levels - 1) * (1 + eps) / 2
@@ -141,8 +144,14 @@ class FSQ(Module):
141
144
  c - number of codebook dim
142
145
  """
143
146
 
147
+ orig_dtype = z.dtype
144
148
  is_img_or_video = z.ndim >= 4
145
149
 
150
+ # make sure allowed dtype
151
+
152
+ if z.dtype not in self.allowed_dtypes:
153
+ z = z.float()
154
+
146
155
  # standardize image or video into (batch, seq, dimension)
147
156
 
148
157
  if is_img_or_video:
@@ -173,4 +182,11 @@ class FSQ(Module):
173
182
  if not self.keep_num_codebooks_dim:
174
183
  indices = rearrange(indices, '... 1 -> ...')
175
184
 
185
+ # cast back to original dtype
186
+
187
+ if out.dtype != orig_dtype:
188
+ out = out.type(orig_dtype)
189
+
190
+ # return quantized output and indices
191
+
176
192
  return out, indices
@@ -826,17 +826,21 @@ class VectorQuantize(nn.Module):
826
826
  is_multiheaded = codebook.ndim > 2
827
827
 
828
828
  if not is_multiheaded:
829
- return codebook[indices]
829
+ codes = codebook[indices]
830
+ else:
831
+ indices, ps = pack_one(indices, 'b * h')
832
+ indices = rearrange(indices, 'b n h -> b h n')
833
+
834
+ indices = repeat(indices, 'b h n -> b h n d', d = codebook.shape[-1])
835
+ codebook = repeat(codebook, 'h n d -> b h n d', b = indices.shape[0])
830
836
 
831
- indices, ps = pack_one(indices, 'b * h')
832
- indices = rearrange(indices, 'b n h -> b h n')
837
+ codes = codebook.gather(2, indices)
838
+ codes = rearrange(codes, 'b h n d -> b n (h d)')
839
+ codes = unpack_one(codes, ps, 'b * d')
833
840
 
834
- indices = repeat(indices, 'b h n -> b h n d', d = codebook.shape[-1])
835
- codebook = repeat(codebook, 'h n d -> b h n d', b = indices.shape[0])
841
+ if not self.channel_last:
842
+ codes = rearrange(codes, 'b ... d -> b d ...')
836
843
 
837
- codes = codebook.gather(2, indices)
838
- codes = rearrange(codes, 'b h n d -> b n (h d)')
839
- codes = unpack_one(codes, ps, 'b * d')
840
844
  return codes
841
845
 
842
846
  def get_output_from_indices(self, indices):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vector_quantize_pytorch
3
- Version: 1.14.2
3
+ Version: 1.14.5
4
4
  Summary: Vector Quantization - Pytorch
5
5
  Home-page: https://github.com/lucidrains/vector-quantizer-pytorch
6
6
  Author: Phil Wang