vector-quantize-pytorch 1.28.3__tar.gz → 1.29.0__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.28.3 → vector_quantize_pytorch-1.29.0}/PKG-INFO +13 -1
  2. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/README.md +12 -0
  3. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/pyproject.toml +1 -1
  4. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/__init__.py +1 -0
  5. vector_quantize_pytorch-1.29.0/vector_quantize_pytorch/hierarchical_vq.py +170 -0
  6. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/utils.py +3 -1
  7. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/.gitignore +0 -0
  8. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/LICENSE +0 -0
  9. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/binary_mapper.py +0 -0
  10. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/finite_scalar_quantization.py +0 -0
  11. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/latent_quantization.py +0 -0
  12. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/lookup_free_quantization.py +0 -0
  13. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/random_projection_quantizer.py +0 -0
  14. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/residual_fsq.py +0 -0
  15. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/residual_lfq.py +0 -0
  16. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/residual_sim_vq.py +0 -0
  17. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/residual_vq.py +0 -0
  18. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/sim_vq.py +0 -0
  19. {vector_quantize_pytorch-1.28.3 → vector_quantize_pytorch-1.29.0}/vector_quantize_pytorch/vector_quantize_pytorch.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: vector-quantize-pytorch
3
- Version: 1.28.3
3
+ Version: 1.29.0
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
@@ -884,3 +884,15 @@ assert loss.item() >= 0
884
884
  url = {https://arxiv.org/abs/2307.13304},
885
885
  }
886
886
  ```
887
+
888
+ ```bibtex
889
+ @misc{tian2024visualautoregressivemodelingscalable,
890
+ title = {Visual Autoregressive Modeling: Scalable Image Generation via Next-Scale Prediction},
891
+ author = {Keyu Tian and Yi Jiang and Zehuan Yuan and Bingyue Peng and Liwei Wang},
892
+ year = {2024},
893
+ eprint = {2404.02905},
894
+ archivePrefix = {arXiv},
895
+ primaryClass = {cs.CV},
896
+ url = {https://arxiv.org/abs/2404.02905},
897
+ }
898
+ ```
@@ -840,3 +840,15 @@ assert loss.item() >= 0
840
840
  url = {https://arxiv.org/abs/2307.13304},
841
841
  }
842
842
  ```
843
+
844
+ ```bibtex
845
+ @misc{tian2024visualautoregressivemodelingscalable,
846
+ title = {Visual Autoregressive Modeling: Scalable Image Generation via Next-Scale Prediction},
847
+ author = {Keyu Tian and Yi Jiang and Zehuan Yuan and Bingyue Peng and Liwei Wang},
848
+ year = {2024},
849
+ eprint = {2404.02905},
850
+ archivePrefix = {arXiv},
851
+ primaryClass = {cs.CV},
852
+ url = {https://arxiv.org/abs/2404.02905},
853
+ }
854
+ ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "vector-quantize-pytorch"
3
- version = "1.28.3"
3
+ version = "1.29.0"
4
4
  description = "Vector Quantization - Pytorch"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -11,5 +11,6 @@ from vector_quantize_pytorch.sim_vq import SimVQ
11
11
  from vector_quantize_pytorch.residual_sim_vq import ResidualSimVQ
12
12
 
13
13
  from vector_quantize_pytorch.binary_mapper import BinaryMapper
14
+ from vector_quantize_pytorch.hierarchical_vq import HierarchicalVQ
14
15
 
15
16
  from vector_quantize_pytorch.utils import Sequential
@@ -0,0 +1,170 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import List, Optional, Sequence
4
+
5
+ import torch
6
+ from torch import nn
7
+ from torch.nn import functional as F
8
+
9
+ from vector_quantize_pytorch.vector_quantize_pytorch import VectorQuantize
10
+
11
+
12
+ def exists(v):
13
+ return v is not None
14
+
15
+
16
+ class _Phi2D(nn.Module):
17
+ def __init__(self, dim: int, resi_ratio: float):
18
+ super().__init__()
19
+ self.resi_ratio = float(abs(resi_ratio))
20
+ self.conv = nn.Conv2d(dim, dim, 3, padding = 1)
21
+
22
+ def forward(self, x):
23
+ if self.resi_ratio <= 1e-8:
24
+ return x
25
+ return (1. - self.resi_ratio) * x + self.resi_ratio * self.conv(x)
26
+
27
+
28
+ class HierarchicalVQ(nn.Module):
29
+ def __init__(
30
+ self,
31
+ *,
32
+ dim: int,
33
+ codebook_size: int,
34
+ scales: Sequence[int],
35
+ decay: float = 0.99,
36
+ commitment_weight: float = 1.,
37
+ rotation_trick: bool = False,
38
+ kmeans_init: bool = True,
39
+ kmeans_iters: int = 10,
40
+ threshold_ema_dead_code: int = 2,
41
+ stochastic_sample_codes: bool = False,
42
+ sample_codebook_temp: float = 0.1,
43
+ orthogonal_reg_weight: float = 0.,
44
+ orthogonal_reg_max_codes: int = 128,
45
+ orthogonal_reg_active_codes_only: bool = False,
46
+ quant_resi: float = 0.5,
47
+ share_quant_resi: int = 1,
48
+ accept_image_fmap: bool = False
49
+ ):
50
+ super().__init__()
51
+ assert accept_image_fmap, 'HierarchicalVQ currently expects accept_image_fmap = True'
52
+
53
+ scales = [int(scale) for scale in scales]
54
+ assert len(scales) > 0
55
+ assert scales == sorted(scales)
56
+ assert all(scale > 0 for scale in scales)
57
+
58
+ self.dim = dim
59
+ self.scales = tuple(scales)
60
+ self.accept_image_fmap = True
61
+
62
+ self.vq = VectorQuantize(
63
+ dim = dim,
64
+ codebook_size = codebook_size,
65
+ decay = decay,
66
+ commitment_weight = commitment_weight,
67
+ rotation_trick = rotation_trick,
68
+ kmeans_init = kmeans_init,
69
+ kmeans_iters = kmeans_iters,
70
+ threshold_ema_dead_code = threshold_ema_dead_code,
71
+ stochastic_sample_codes = stochastic_sample_codes,
72
+ sample_codebook_temp = sample_codebook_temp,
73
+ orthogonal_reg_weight = orthogonal_reg_weight,
74
+ orthogonal_reg_max_codes = orthogonal_reg_max_codes,
75
+ orthogonal_reg_active_codes_only = orthogonal_reg_active_codes_only,
76
+ accept_image_fmap = True
77
+ )
78
+
79
+ if share_quant_resi == 1:
80
+ self.phi_shared = _Phi2D(dim, quant_resi)
81
+ self.phi_levels = None
82
+ else:
83
+ num_phi_levels = len(self.scales) if share_quant_resi <= 0 else min(len(self.scales), int(share_quant_resi))
84
+ self.phi_shared = None
85
+ self.phi_levels = nn.ModuleList([_Phi2D(dim, quant_resi) for _ in range(num_phi_levels)])
86
+
87
+ def _choose_phi(self, scale_index: int):
88
+ if exists(self.phi_shared):
89
+ return self.phi_shared
90
+
91
+ assert exists(self.phi_levels)
92
+
93
+ if len(self.phi_levels) == len(self.scales):
94
+ return self.phi_levels[scale_index]
95
+
96
+ if len(self.scales) == 1:
97
+ return self.phi_levels[0]
98
+
99
+ position = scale_index / float(len(self.scales) - 1)
100
+ phi_index = round(position * (len(self.phi_levels) - 1))
101
+ phi_index = max(0, min(len(self.phi_levels) - 1, phi_index))
102
+ return self.phi_levels[phi_index]
103
+
104
+ def _upsample_to_full(self, q, full_hw, scale_index: int):
105
+ if q.shape[-2:] != full_hw:
106
+ q = F.interpolate(q, size = full_hw, mode = 'bilinear', align_corners = False)
107
+
108
+ phi = self._choose_phi(scale_index)
109
+ if exists(phi):
110
+ q = phi(q)
111
+
112
+ return q
113
+
114
+ def forward(
115
+ self,
116
+ x,
117
+ indices = None,
118
+ sample_codebook_temp = None,
119
+ **kwargs
120
+ ):
121
+ assert indices is None, 'reconstruction-from-indices path not implemented in forward'
122
+ del kwargs
123
+
124
+ assert x.ndim == 4, 'expected image fmap of shape (batch, channels, height, width)'
125
+ batch, dim, height, width = x.shape
126
+ assert dim == self.dim
127
+
128
+ residual = x
129
+ reconstruction = torch.zeros_like(x)
130
+ all_indices = []
131
+ all_commit_losses = []
132
+
133
+ for scale_index, scale in enumerate(self.scales):
134
+ residual_down = F.adaptive_avg_pool2d(residual, output_size = (scale, scale))
135
+
136
+ vq_kwargs = {}
137
+ if exists(sample_codebook_temp):
138
+ vq_kwargs.update(sample_codebook_temp = sample_codebook_temp)
139
+
140
+ quantized, scale_indices, commit_loss = self.vq(residual_down, **vq_kwargs)
141
+ quantized = self._upsample_to_full(quantized, (height, width), scale_index)
142
+
143
+ reconstruction = reconstruction + quantized
144
+ residual = residual - quantized
145
+
146
+ all_indices.append(scale_indices)
147
+ all_commit_losses.append(commit_loss)
148
+
149
+ mean_commit_loss = torch.stack(all_commit_losses).mean()
150
+ return reconstruction, tuple(all_indices), mean_commit_loss
151
+
152
+ def get_output_from_indices(self, indices):
153
+ assert isinstance(indices, (tuple, list))
154
+ assert len(indices) == len(self.scales)
155
+
156
+ first = indices[0]
157
+ assert first.ndim == 3
158
+ batch, height, width = first.shape
159
+ full_hw = (self.scales[-1], self.scales[-1])
160
+ if (height, width) != full_hw:
161
+ full_hw = (self.scales[-1], self.scales[-1])
162
+
163
+ reconstructed = None
164
+
165
+ for scale_index, scale_indices in enumerate(indices):
166
+ q = self.vq.get_output_from_indices(scale_indices)
167
+ q = self._upsample_to_full(q, full_hw, scale_index)
168
+ reconstructed = q if reconstructed is None else reconstructed + q
169
+
170
+ return reconstructed
@@ -14,6 +14,7 @@ from vector_quantize_pytorch.residual_fsq import ResidualFSQ, GroupedResidualFSQ
14
14
  from vector_quantize_pytorch.latent_quantization import LatentQuantize
15
15
  from vector_quantize_pytorch.sim_vq import SimVQ
16
16
  from vector_quantize_pytorch.residual_sim_vq import ResidualSimVQ
17
+ from vector_quantize_pytorch.hierarchical_vq import HierarchicalVQ
17
18
 
18
19
  QUANTIZE_KLASSES = (
19
20
  VectorQuantize,
@@ -28,7 +29,8 @@ QUANTIZE_KLASSES = (
28
29
  GroupedResidualLFQ,
29
30
  ResidualFSQ,
30
31
  GroupedResidualFSQ,
31
- LatentQuantize
32
+ LatentQuantize,
33
+ HierarchicalVQ
32
34
  )
33
35
 
34
36
  # classes