Restructuring Vector Quantization with the Rotation Trick

## Abstract

Abstract Vector Quantized Variational AutoEncoders (VQ-VAEs) are designed to compress a continuous input to a discrete latent space and reconstruct it with minimal distortion.
They operate by maintaining a set of vectors—often referred to as the codebook—and quantizing each encoder output to the nearest vector in the codebook.
However, as vector quantization is non-differentiable, the gradient to the encoder flows around the vector quantization layer rather than through it in a straight-through approximation.
This approximation may be undesirable as all information from the vector quantization operation is lost.
In this work, we propose a way to propagate gradients through the vector quantization layer of VQ-VAEs.
We smoothly transform each encoder output into its corresponding codebook vector via a rotation and rescaling linear transformation that is treated as a constant during backpropagation.
As a result, the relative magnitude and angle between encoder output and codebook vector becomes encoded into the gradient as it propagates through the vector quantization layer and back to the encoder.
Across 11 different VQ-VAE training paradigms, we find this restructuring improves reconstruction metrics, codebook utilization, and quantization error. Our code is available at https://github.com/cfifty/rotation_trick .

## 1 Introduction

Vector quantization (Gray, ) is an approach to discretize a continuous vector space. It defines a finite set of vectors—referred to as the codebook—and maps any vector in the continuous vector space to the closest vector in the codebook.
However, deep learning paradigms that use vector quantization are often difficult to train because replacing a vector with its closest codebook counterpart is a non-differentiable operation (Huh et al., ).
This characteristic was not an issue at its creation during the Renaissance of Information Theory for applications like noisy channel communication (Cover, ); however in the era deep learning, it presents a challenge as gradients cannot directly flow through layers that use vector quantization during backpropagation.

In deep learning, vector quantization is largely used in the eponymous Vector Quantized-Variational AutoEncoder (VQ-VAE) (Van Den Oord et al., ). A VQ-VAE is an AutoEncoder with a vector quantization layer between the encoder’s output and decoder’s input, thereby quantizing the learned representation at the bottleneck. While VQ-VAEs are ubiquitous in state-of-the-art generative modeling (Rombach et al., ; Dhariwal et al., ; Brooks et al., ), their gradients cannot flow from the decoder to the encoder uninterrupted as they must pass through a non-differentiable vector quantization layer.

A solution to the non-differentiability problem is to approximate gradients via a “straight-through estimator” (STE) (Bengio et al., ). During backpropagation, the STE copies and pastes the gradients from the decoder’s input to the encoder’s output, thereby skipping the quantization operation altogether.
However, this approximation can lead to poor-performing models and codebook collapse: a phenomena where a large percentage of the codebook converge to zero norm and are unused by the model (Mentzer et al., ). Even if codebook collapse does not occur, the codebook is often under-utilized, thereby limiting the information capacity of the VQ-VAEs’s bottleneck (Dhariwal et al., ).

Figure: Figure 1: Illustration of the rotation trick. In the forward pass, encoder output $e$ is rotated and rescaled to $q_{1}$. Not shown is the rotation of the other encoder outputs to their corresponding codebook vectors. In the backward pass, the gradient at $q_{1}$ moves to $e$ so that the angle between $\nabla_{q_{1}}\mathcal{L}$ and $q_{1}$ is preserved. Now, points within the same codebook region receive different gradients depending on their relative angle and magnitude to the codebook vector. For example, points with high angular distance can be pushed into new codebook regions, thereby increasing codebook utilization.
Refer to caption: x1.png

Figure: Algorithm 1 VQ-VAE with the Rotation Trick

In this work, we propose an alternate way to propagate gradients through the vector quantization layer in VQ-VAEs.
For a given encoder output $e$ and nearest codebook vector $q$, we smoothly transform $e$ to $q$ via a rotation and rescaling linear transformation and then send this output—rather than the direct result of the codebook lookup—to the decoder.
As the input to the decoder, $\tilde{q}$, is now treated as a smooth linear transformation of $e$, gradients flow back from the decoder to the encoder unimpeded.
To avoid differentiating through the rotation and rescaling, we treat both as constants with respect to $e$ and $q$. We explain why this choice is necessary in Section A.2.
Following the convention of Kingma & Welling (), we call this restructuring “the rotation trick.” It is illustrated in Figure 3 and described in Algorithm 1.

The rotation trick does not change the output of the VQ-VAE in the forward pass.
However, during the backward pass, it transports the gradient $\nabla_{q}\mathcal{L}$ at $q$ to become the gradient $\nabla_{e}\mathcal{L}$ at $e$ so that the angle between $q$ and $\nabla_{q}\mathcal{L}$ after the vector quantization layer equals the angle between $e$ and $\nabla_{e}\mathcal{L}$ before the vector quantization layer. Preserving this angle encodes relative angular distances and magnitudes into the gradient and changes how points within the same codebook region are updated.

The STE applies the same update to all points within the same codebook region, maintaining their relative distances. However as we will show in Section 4.3, the rotation trick can push points within the same codebook region farther apart—or pull them closer together—depending on the direction of the gradient vector.
The former capability can correspond to increased codebook usage while the latter to lower quantization error. In the context of lossy compression, both capabilities are desirable for reducing the distortion and increasing the information capacity of the vector quantization layer.

When applied to several open-source VQ-VAE repositories, we find the rotation trick substantively improves reconstruction performance, increases codebook usage, and decreases the distance between encoder outputs and their corresponding codebook vectors. For instance, training the VQGAN from Rombach et al. () on ImageNet (Deng et al., ) with the rotation trick improves reconstruction FID from 5.0 to 1.6, reconstruction IS from 141.5 to 190.3, increases codebook usage from 2% to 9%, and decreases quantization error by over an order of magnitude.

## 2 Related Work

Many researchers have built upon the seminal work of Van Den Oord et al. () to improve VQ-VAE performance. While non-exhaustive, our review focuses on methods that address training instabilities caused by the vector quantization layer. We partition these efforts into two categories: (1) methods that sidestep the STE and (2) methods that improve codebook-model interactions.

Sidestepping the STE. Several prior works have sought to fix the problems caused by the STE by avoiding deterministic vector quantization. Baevski et al. () employ the Gumbel-Softmax trick (Jang et al., ) to fit a categorical distribution over codebook vectors that converges to a one-hot distribution towards the end of training, Gautam et al. () quantize using a a convex combination of codebook vectors, and Takida et al. () employ stochastic quantization. Unlike the above that cast vector quantization as a distribution over codebook vectors, Huh et al. () propose an alternating optimization where the encoder is optimized to output representations close to the codebook vectors while the decoder minimizes reconstruction loss from a fixed set of codebook vector inputs. While these approaches sidestep the training instabilities caused by the STE, they can introduce their own set of problems and complexities such as low codebook utilization at inference and the tuning of a temperature schedule (Zhang et al., ). As a result, many applications and research papers continue to employ VQ-VAEs that are trained using the STE (Rombach et al., ; Chang et al., ; Huang et al., ; Zhu et al., ; Dong et al., ).

Codebook-Model Improvements. Another way to attack codebook collapse or under-utilization is to change the codebook lookup. Rather than use Euclidean distance, Yu et al. () employ a cosine similarity measure, Goswami et al. () a hyperbolic metric, and Lee et al. () stochastically sample codes as a function of the distance between the encoder output and codebook vectors. Another perspective examines the learning of the codebook. Kolesnikov et al. () split high-usage codebook vectors, Dhariwal et al. (); Łańcucki et al. (); Zheng & Vedaldi () resurrect low-usage codebook vectors throughout training, Chen et al. () dynamically selects one of $m$ codebooks for each datapoint, and Mentzer et al. (); Zhao et al. (); Yu et al. (); Chiu et al. () fix the codebook vectors to an a priori geometry and train the model without learning the codebook at all. Other works propose loss penalties to encourage codebook utilization. Zhang et al. () add a KL-divergence penalty between codebook utilization and a uniform distribution while Yu et al. () add an entropy loss term to penalize low codebook utilization. While effective at targeting specific training difficulties, as each of these methods continue to use the STE, the training instability caused by this estimator persist. Most of our experiments in Section 5 implement a subset of these approaches, and we find that replacing the STE with the rotation trick further improves performance.

## 3 Straight Through Estimator (STE)

In this section, we review the Straight-Through Estimator (STE) and visualize its effect on the gradients. We then explore two STE alternatives that—at first glance—appear to correct the approximation made by the STE.

For notation, we define a sample space $\mathcal{X}$ over the input data with probability distribution $p$. For input $x\in\mathcal{X}$, we define the encoder as a deterministic mapping that parameterizes a posterior distribution $p_{\mathcal{E}}(e|x)$. The vector quantization layer, $\mathcal{Q}(\cdot)$, is a function that selects the codebook vector $q\in\mathcal{C}$ nearest to the encoder output $e$. Under Euclidean distance, it has the form:

$$ $\displaystyle\mathcal{Q}(q=i|e)$ $\displaystyle=\begin{cases}1\text{ if }i=\operatorname*{arg\,min}_{1\leq j\leq |\mathcal{C}|}{\lVert e-q_{j}\rVert}_{2}\\ 0\text{ otherwise}\end{cases}$ $$

The decoder is similarly defined as a deterministic mapping that parameterizes the conditional distribution over reconstructions $p_{\mathcal{D}}(\tilde{x}|q)$. As in the VAE (Kingma & Welling, ), the loss function follows from the ELBO with the KL-divergence term zeroing out as $p_{\mathcal{E}}(e|x)$ is deterministic and the utilization over codebook vectors is assumed to be uniform. Van Den Oord et al. () additionally add a “codebook loss” term ${\lVert sg(e)-q\rVert}_{2}^{2}$ to learn the codebook vectors and a “commitment loss” term $\beta{\lVert e-sg(q)\rVert}_{2}^{2}$ to pull the encoder’s output towards the codebook vectors. $sg$ stands for stop-gradient and $\beta$ is a hyperparameter, typically set to a value in $[0.25,2]$. For predicted reconstruction $\tilde{x}$, the optimization objective becomes:

$$ $\displaystyle\mathcal{L}(\tilde{x})$ $\displaystyle={\lVert x-\tilde{x}\rVert}_{2}^{2}+{\lVert sg(e)-q\rVert}_{2}^{2 }+\beta{\lVert e-sg(q)\rVert}_{2}^{2}$ $$

In the subsequent analysis, we focus only on the ${\lVert x-\tilde{x}\rVert}_{2}^{2}$ term as the other two are not functions of the decoder. During backpropagation, the model must differentiate through the vector quantization function $\mathcal{Q}(\cdot)$. We can break down the backward pass into three terms:

$$ $\displaystyle\frac{\partial\mathcal{L}}{\partial x}$ $\displaystyle=\frac{\partial\mathcal{L}}{\partial q}\frac{\partial q}{\partial e }\frac{\partial e}{\partial x}$ $$

where $\frac{\partial\mathcal{L}}{\partial q}$ represents backpropagation through the decoder, $\frac{\partial q}{\partial e}$ represents backpropagation through the vector quantization layer, and $\frac{\partial e}{\partial x}$ represents backpropagation through the encoder. As vector quantization is not a smooth transformation, $\frac{\partial q}{\partial e}$ cannot be computed and gradients cannot flow through this term to update the encoder in backpropagation.

Figure: Figure 2: Visualization of how the straight-through estimator (STE) transforms the gradient field for $16$ codebook vectors for (top) $f(x,y)=x^{2}+y^{2}$ and (bottom) $f(x,y)=\log(|\frac{1}{2}x+\tanh(y)|)$. The STE takes the gradient at the codebook vector $(q_{x},q_{y})$ and “copies-and-pastes” it to all other locations within the same codebook region, forming a “checker-board” pattern in the gradient field.
Refer to caption: x2.png

To solve the issue of non-differentiability, the STE copies the gradients from $q$ to $e$, bypassing vector quantization entirely. Simply, the STE sets $\frac{\partial q}{\partial e}$ to the identity matrix $I$ in the backward pass:

$$ $\displaystyle\frac{\partial\mathcal{L}}{\partial x}$ $\displaystyle=\frac{\partial\mathcal{L}}{\partial q}I\frac{\partial e}{ \partial x}$ $$

The first two terms $\frac{\partial\mathcal{L}}{\partial q}\frac{\partial q}{\partial e}$ combine to $\frac{\partial\mathcal{L}}{\partial e}$ which, somewhat misleadingly, does not actually depend on $e$. As a consequence, the location of $e$ within the Voronoi partition generated by codebook vector $q$—be it close to $q$ or at the boundary of the region—has no impact on the gradient update to the encoder.

An example of this effect is visualized in Figure 2 for two example functions. In the STE approximation, the “exact” gradient at the encoder output is replaced by the gradient at the corresponding codebook vector for each Voronoi partition, irrespective of where in that region the encoder output $e$ lies. As a result, the exact gradient field becomes “partitioned” into $16$ different regions—all with the same gradient update to the encoder—for the $16$ vectors in the codebook.

Returning to our question, is there a better way to propagate gradients through the vector quantization layer? At first glance, one may be tempted to estimate the curvature at $q$ and use this information to transform $\frac{\partial q}{\partial e}$ as $q$ moves to $e$. This is accomplished by taking a second order expansion around $q$ to approximate the value of the loss at $e$:

$$ $\displaystyle\mathcal{L}_{e}$ $\displaystyle\approx\mathcal{L}_{q}+(\nabla_{q}\mathcal{L})^{T}(e-q)+\frac{1}{ 2}(e-q)^{T}(\nabla_{q}^{2}\mathcal{L})(e-q)$ $$

Then we can compute the gradient at the point $e$ instead of $q$ up to second order approximation with:

$$ $\displaystyle\frac{\partial\mathcal{L}}{\partial e}$ $\displaystyle\approx\frac{\partial}{\partial e}\left[\mathcal{L}_{q}+(\nabla_{ q}\mathcal{L})^{T}(e-q)+\frac{1}{2}(e-q)^{T}(\nabla_{q}^{2}\mathcal{L})(e-q)\right]$ $\displaystyle=\nabla_{q}\mathcal{L}+(\nabla_{q}^{2}\mathcal{L})(e-q)$ $$

While computing Hessians with respect to model parameters are typically prohibitive in modern deep learning architectures, computing them with respect to only the codebook is feasible. Moreover as we must only compute $(\nabla_{q}^{2}\mathcal{L})(e-q)$, one may take advantage of efficient Hessian-Vector products implementations in deep learning frameworks (Dagréou et al., ) and avoid computing the full Hessian matrix.

Extending this idea a step further, we can compute the exact gradient $\frac{\partial\mathcal{L}}{\partial e}$ at $e$ by making two passes through the network. Let $\mathcal{L}_{q}$ be the loss with the vector quantization layer and $\mathcal{L}_{e}$ be the loss without vector quantization, i.e. $q=e$ rather than $q=\mathcal{Q}(e)$.
Then one may form the total loss $\mathcal{L}=\mathcal{L}_{q}+\lambda\mathcal{L}_{e}$, where $\lambda$ is a small constant like $10^{-6}$, to scale down the effect of $\mathcal{L}_{e}$ on the decoder’s parameters and use a gradient scaling multiplier of $\lambda^{-1}$ to reweigh the effect of $\mathcal{L}_{e}$ on the encoder’s parameters to $1$. As $\frac{\partial q}{\partial e}$ is non-differentiable, gradients from $\mathcal{L}_{q}$ will not flow to the encoder.

While seeming to correct the encoder’s gradients, replacing the STE with either approach will likely result in worse performance. This is because computing the exact gradient with respect to $e$ is actually the AutoEncoder (Hinton & Zemel, ) gradient, the model that VAEs (Kingma & Welling, ) and VQ-VAEs (Van Den Oord et al., ) were designed to replace given the AutoEncoder’s propensity to overfit and difficultly generalizing. Accordingly using either Hessian approximation or exact gradients via a double forward pass will cause the encoder to be trained like an AutoEncoder and the decoder to be trained like a VQ-VAE. This mis-match in optimization objectives is likely another contributing factor to the poor performance we observe for both methods in Table 1.

## 4 The Rotation Trick

As discussed in Section 3, updating the encoder’s parameters by approximating, or exactly, computing the gradient at the encoder’s output is undesirable.
Similarly, the STE appears to lose information: the location of $e$ within the quantized region—be it close to $q$ or far away at the boundary—has no impact on the gradient update to the encoder.
Capturing this information, i.e. using the location of $e$ in relation to $q$ to transform the gradients through $\frac{\partial q}{\partial e}$, could be beneficial to the encoder’s gradient updates and an improvement over the STE.

Viewed geometrically, we ask how to move the gradient $\nabla_{q}\mathcal{L}$ from $q$ to $e$, and what characteristics of $\nabla_{q}\mathcal{L}$ and $q$ should be preserved during this movement. The STE offers one possible answer: move the gradient from $q$ to $e$ so that its direction and magnitude are preserved. However, this paper supplies a different answer: move the gradient so that the angle between $\nabla_{q}\mathcal{L}$ and $q$ is preserved as $\nabla_{q}\mathcal{L}$ moves to $e$. We term this approach “the rotation trick”, and in Section 4.3 we show that preserving the angle between $q$ and $\nabla_{q}\mathcal{L}$ conveys desirable properties to how points move within the same quantized region.

### 4.1 The Rotation Trick Preserves Angles

In this section, we formally define the rotation trick. For encoder output $e$, let $q=\mathcal{Q}(e)$ represent the corresponding codebook vector. $\mathcal{Q}(\cdot)$ is non-differentiable so gradients cannot flow through this layer during the backward pass.
The STE solves this problem—maintaining the direction and magnitude of the gradient $\nabla_{q}\mathcal{L}$—as $\nabla_{q}\mathcal{L}$ moves from $q$ to $e$ with some clever hacking of the backpropagation function in deep learning frameworks:

$$ $\displaystyle\tilde{q}$ $\displaystyle=e-\underbrace{(q-e)}_{\text{constant}}$ $$

Figure: Figure 3: Illustration of how the gradient at $q$ moves to $e$ via the STE (middle) and rotation trick (right). The STE “copies-and-pastes” the gradient to preserve its direction while the rotation trick moves the gradient so the angle between $q$ and $\nabla_{q}\mathcal{L}$ is preserved.
Refer to caption: x3.png

which is a parameterization of vector quantization that sets the gradient at the encoder output to the gradient at the decoder’s input. The rotation trick offers a different parameterization: casting the forward pass as a rotation and rescaling that aligns $e$ with $q$:

$$ $\displaystyle\tilde{q}$ $\displaystyle=\underbrace{\left[\frac{{\lVert q\rVert}}{{\lVert e\rVert}}R \right]}_{\text{constant}}e$ $$

$R$ is the rotation(^1^11A rotation is defined as a linear transformation so that $RR^{T}=I$, $R^{-1}=R^{T}$, and det($R$) = 1.) transformation that aligns $e$ with $q$ and $\frac{{\lVert q\rVert}}{{\lVert e\rVert}}$ rescales $e$ to have the same magnitude as $q$. Note that both $R$ and $\frac{{\lVert q\rVert}}{{\lVert e\rVert}}$ are functions of $e$. To avoid differentiating through this dependency, we treat them as fixed constants—or detached from the computational graph in deep learning frameworks—when differentiating. This choice is explained in Section A.2.

While the rotation trick does not change the output of the forward pass, the backward pass changes. Rather than set $\frac{\partial q}{\partial e}=I$ as in the STE, the rotation trick sets $\frac{\partial q}{\partial e}$ to be a rotation and rescaling transformation:

$$ $\displaystyle\frac{\partial\tilde{q}}{\partial e}$ $\displaystyle=\frac{{\lVert q\rVert}}{{{\lVert e\rVert}}}R$ $$

As a result, $\frac{\partial q}{\partial e}$ changes based on the position of $e$ in the codebook partition of $q$, and notably, the angle between $\nabla_{q}\mathcal{L}$ and $q$ is preserved as $\nabla_{q}\mathcal{L}$ moves to $e$. This effect is visualized in Figure 3. While the STE translates the gradient from $q$ to $e$, the rotation trick rotates it so that the angle between $\nabla_{q}\mathcal{L}$ and $q$ is preserved. In a sense, the rotation trick and the STE are sibilings. They choose different characteristics of the gradient as desiderata and then preserve those characteristics as the gradient flows around the non-differentiable vector quantization operation to the encoder.

### 4.2 Efficient Rotation Computation

The rotation transformation $R$ that rotates $e$ to $q$ can be efficiently computed with Householder matrix reflections. We define $\hat{e}=\frac{e}{{\lVert e\rVert}}$, $\hat{q}=\frac{q}{{\lVert q\rVert}}$, $\lambda=\frac{{\lVert q\rVert}}{{\lVert e\rVert}}$, and $r=\frac{\hat{e}+\hat{q}}{{\lVert\hat{e}+\hat{q}\rVert}}$. Then the rotation and rescaling that aligns $e$ to $q$ is simply:

$$ $\displaystyle\tilde{q}$ $\displaystyle=\lambda Re$ $\displaystyle=\lambda(I-2rr^{T}+2\hat{q}\hat{e}^{T})e$ $\displaystyle=\lambda[e-2rr^{T}e+2\hat{q}\hat{e}^{T}e]$ $$

Due to space constraints, we leave the derivation of this formula to Section A.4. Parameterizing the rotation in this fashion avoids computing outer products and therefore consumes minimal GPU VRAM. Further, we did not detect a difference in wall-clock time between VQ-VAEs trained with the STE and VQ-VAEs trained with the rotation trick for our experiments in Section 5.

### 4.3 Voronoi Partition Analysis

Figure: Figure 4: Depiction of how points within the same codebook region change after a gradient update (red arrow) at the codebook vector (orange circle). The STE applies the same update to each point in the same region. The rotation trick modifies the update based on the location of each point with respect to the codebook vector.
Refer to caption: x4.png

Figure: Figure 5: With the STE, the distances among points within the same region do not change. However with the rotation trick, the distances among points do change. When $\tilde{\theta}<\pi/2$, points with large angular distance are pushed away (blue: increasing distance). When $\tilde{\theta}>\pi/2$, points are pulled towards the codebook vector (green: decreasing distance).
Refer to caption: x5.png

In the context of lossy compression, vector quantization works well when the distortion, or equivalently quantization error ${\lVert e-q\rVert}_{2}^{2}$, is low and the information capacity—equivalently codebook utilization—is high (Cover, ).
Later in Section 5, we will see that VQ-VAEs trained with the rotation trick have this desiderata—often reducing quantization error by an order of magnitude and substantially increasing codebook usage—when compared to VQ-VAEs trained with the STE. However, the underlying reason why this occurs is less clear.

In this section, we analyze the effect of the rotation trick by looking at how encoder outputs that are mapped to the same Voronoi region are updated. While the STE applies the same update to all points within the same partition, the rotation trick changes the update based on the location of points within the Voronoi region. It can push points within the same region farther apart or pull them closer together depending on the direction of the gradient vector. The former capability can correspond to increased codebook usage while the latter to lower quantization error.

Let $\theta$ be the angle between $e$ and $q$ and $\tilde{\theta}$ be the angle between $q$ and $\nabla_{q}\mathcal{L}$. Intuitively, when $\theta$ is large, $e$ maps to $q$ with high distortion. In this case, we’d like $e$ to commit one way or another. Either (1) make $e$ commit to its current codebook vector by pulling $e$ closer to $q$ or (2) push $e$ to a different codebook vector entirely. This behavior is preferable to maintaining states of high distortion where the distance between $e$ and $q$ remains constant. We will show the rotation trick enables both (1) and (2); however, the STE perpetuates the latter as visualized in Figure 5.

When $\nabla_{q}\mathcal{L}$ and $q$ point in the same direction, i.e. $-\pi/2<\tilde{\theta}<\pi/2$, encoder outputs with large angular distance to $q$ are pushed farther away than they would otherwise be moved by the STE update.
Figure 5 illustrates this effect. The points with large angular distance (blue regions) move further away from $q$ than the points with low angular distance (ivory regions).

The top right partitions of Figure 4 present an example of this effect. The two clusters of points at the boundary—with relatively large angle to the codebook vector—are pushed away while the cluster of points with small angle to the codebook vector move with it. The ability to push points at the boundary out of a quantized region and into another is desirable for increasing codebook utilization. Specifically, codebook utilization improves when points are pushed into the Voronoi regions of previously unused codebook vectors. This capability is not shared by the STE, which moves all points in the same region by the same amount.

When $\nabla_{q}\mathcal{L}$ and $q$ point in opposite directions, i.e. $\pi/2<\tilde{\theta}<3\pi/2$, the distance among points within the same Voronoi region decreases as they are pulled towards the location of the updated codebook vector. This effect is visualized in Figure 5 (green regions) and the bottom partitions of Figure 4 show an example. Unlike the STE update—that maintains the distances among points—the rotation trick pulls points with high angular distances closer towards the post-update codebook vector. This capability is desirable for reducing the quantization error and enabling the encoder to lock on (Van Den Oord et al., ) to a target codebook vector.

Taken together, both capabilities can form a push-pull effect that achieves two desiderata of vector quantization: increasing information capacity and reducing distortion. Encoder outputs that have large angular distance to the chosen codebook vector are “pushed” to other, possibly unused, codebook regions by outwards-pointing gradients, thereby increasing codebook utilization. Concurrent with this effect, center-pointing gradients will “pull” points loosely clustered around the codebook vector closer together, locking on to the chosen codebook vector and reducing quantization error.

A third and final difference with the STE occurs when $\nabla_{q}\mathcal{L}$ is exactly orthogonal to $q$, i.e. $\tilde{\theta}=\pi/2$. For this event, each point within the Voronoi region generated by $q$ is rotated by the same amount in the direction of $\nabla_{q}\mathcal{L}$. The top left Voroni regions of Figure 4 visualize this case. While it is unlikely that the gradient is exactly orthogonal to the codebook vector, the gradient’s orthogonal component to $q$
enables points to move in orthogonal directions via a rotation and expansion (if $\tilde{\theta}<\pi/2$) or a rotation and contraction (if $\tilde{\theta}>\pi/2$).

## 5 Experiments

In Section 4.3, we showed the rotation trick enables behavior that would increase codebook utilization and reduce quantization error by changing how points within the same Voronoi region are updated. However, the extent to which these changes will affect applications is unclear. In this section, we evaluate the effect of the rotation trick across many different VQ-VAE paradigms.

We begin with image reconstruction: training a VQ-VAE with the reconstruction objective of Van Den Oord et al. () and later extend our evaluation to the more complex VQGANs (Esser et al., ), the VQGANs designed for latent diffusion (Rombach et al., ), and then the ViT-VQGAN (Yu et al., ). Finally, we evaluate VQ-VAE reconstructions on videos using a TimeSformer (Bertasius et al., ) encoder and decoder.
In total, our empirical analysis spans 11 different VQ-VAE configurations.
For all experiments, aside from handling $\bm{\frac{\partial q}{\partial e}}$ differently, the models, hyperparameters, and training settings are identical.

**Table 1: Comparison of VQ-VAEs trained on ImageNet following Van Den Oord et al. (). We use the Vector Quantization layer from [https://github.com/lucidrains/vector-quantize-pytorch](https://github.com/lucidrains/vector-quantize-pytorch).**
| Approach | Training Metrics | Validation Metrics |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- |
| Codebook Usage ($\uparrow$) | Rec. Loss ($\downarrow$) | Quantization Error ($\downarrow$) | Rec. Loss ($\downarrow$) | r-FID ($\downarrow$) | r-IS ($\uparrow$) |  |
| Codebook Lookup: Euclidean & Latent Shape: $32\times 32\times 32$ & Codebook Size: $1024$ |  |  |  |  |  |  |
| VQ-VAE | 100% | 0.107 | 5.9e-3 | 0.115 | 106.1 | 11.7 |
| VQ-VAE w/ Rotation Trick | 97% | 0.116 | 5.1e-4 | 0.122 | 85.7 | 17.0 |
| Codebook Lookup: Cosine & Latent Shape: $32\times 32\times 32$ & Codebook Size: $1024$ |  |  |  |  |  |  |
| VQ-VAE | 75% | 0.107 | 2.9e-3 | 0.114 | 84.3 | 17.7 |
| VQ-VAE w/ Rotation Trick | 91% | 0.105 | 2.7e-3 | 0.111 | 82.9 | 18.1 |
| Codebook Lookup: Euclidean & Latent Shape: $64\times 64\times 3$ & Codebook Size: $8192$ |  |  |  |  |  |  |
| VQ-VAE | 100% | 0.028 | 1.0e-3 | 0.030 | 19.0 | 97.3 |
| VQ-VAE w/ Hessian Approx. | 39% | 0.082 | 6.9e-5 | 0.112 | 35.6 | 65.1 |
| VQ-VAE w/ Exact Gradients | 84% | 0.050 | 2.0e-3 | 0.053 | 25.4 | 80.4 |
| VQ-VAE w/ Rotation Trick | 99% | 0.028 | 1.4e-4 | 0.030 | 16.5 | 106.3 |
| Codebook Lookup: Cosine & Latent Shape: $64\times 64\times 3$ & Codebook Size: $8192$ |  |  |  |  |  |  |
| VQ-VAE | 31% | 0.034 | 1.2e-4 | 0.038 | 26.0 | 77.8 |
| VQ-VAE w/ Hessian Approx. | 37% | 0.035 | 3.8e-5 | 0.037 | 29.0 | 71.5 |
| VQ-VAE w/ Exact Gradients | 38% | 0.035 | 3.6e-5 | 0.037 | 28.2 | 75.0 |
| VQ-VAE w/ Rotation Trick | 38% | 0.033 | 9.6e-5 | 0.035 | 24.2 | 83.9 |

### 5.1 VQ-VAE Evaluation

We begin with a straightforward evaluation: training a VQ-VAE to reconstruct examples from ImageNet (Deng et al., ). Following Van Den Oord et al. (), our training objective is a linear combination of the reconstruction, codebook, and commitment loss:

$$ $\displaystyle\mathcal{L}={\lVert x-\tilde{x}\rVert}_{2}^{2}+{\lVert sg(e)-q \rVert}_{2}^{2}+\beta{\lVert e-sg(q)\rVert}_{2}^{2}$ $$

where $\beta$ is a hyperparameter scaling constant. Following convention, we drop the codebook loss term from the objective and instead use an exponential moving average to update the codebook vectors.

Evaluation Settings. For $256\times 256\times 3$ input images, we evaluate two different settings: (1) compressing to a latent space of dimension $32\times 32\times 32$ with a codebook size of $1024$ following Yu et al. () and (2) compressing to $64\times 64\times 3$ with a codebook size of $8192$ following Rombach et al. (). In both settings, we compare with a Euclidean and cosine similarity codebook lookup.

Evaluation Metrics. We log both training and validation set reconstruction metrics. Of note, we compute reconstruction FID (Heusel et al., ) and reconstruction IS (Salimans et al., ) on reconstructions from the full ImageNet validation set as a measure of reconstruction quality. We also compute codebook usage, or the percentage of codebook vectors that are used in each batch of data, as a measure of the information capacity of the vector quantization layer and quantization error ${\lVert e-q\rVert}_{2}^{2}$ as a measure of distortion.

Baselines. Our comparison spans the STE estimator (VQ-VAE), the Hessian approximation described in Section 3 (VQ-VAE w/ Hessian Approx), the exact gradient backward pass described in Section 3 (VQ-VAE w/ Exact Gradients), and the rotation trick (VQ-VAE w/ Rotation Trick). All methods share the same architecture, hyperparameters, and training settings, and these settings are summarized in Table 6 of the Appendix. There is no functional difference among methods in the forward pass; the only differences relates to how gradients are propagated through $\frac{\partial q}{\partial e}$ during backpropagation.

Results. Table 1 displays our findings. We find that using the rotation trick reduces the quantization error—sometimes by an order of magnitude—and improves low codebook utilization. Both results are expected given the Voronoi partition analysis in Section 4.3: points at the boundary of quantized regions are likely pushed to under-utilized codebook vectors while points loosely grouped around the codebook vector are condensed towards it. These two features appear to have a meaningful effect on reconstruction metrics: training a VQ-VAE with the rotation trick substantially improves r-FID and r-IS.

We also see that the Hessian Approximation or using Exact Gradients results in poor reconstruction performance. While the gradients to the encoder are, in a sense, “more accurate”, training the encoder like an AutoEncoder (Hinton & Zemel, ) likely introduces overfitting and poor generalization. Moreover, the mismatch in training objectives between the encoder and decoder is likely an aggravating factor and partly responsible for both models’ poor performance.

**Table 2: Results for VQGAN designed for autoregressive generation as implemented in [https://github.com/CompVis/taming-transformers](https://github.com/CompVis/taming-transformers). Experiments on ImageNet and the combined dataset FFHQ (Karras et al., ) and CelebA-HQ (Karras, ) use a latent bottleneck of dimension $16\times 16\times 256$ with $1024$ codebook vectors.**
| Approach | Dataset | Codebook Usage | Quantization Error ($\downarrow$) | Valid Loss ($\downarrow$) | r-FID ($\downarrow$) | r-IS ($\uparrow$) |
| --- | --- | --- | --- | --- | --- | --- |
| VQGAN (reported) | ImageNet | — | — | — | 7.9 | 114.4 |
| VQGAN (our run) | ImageNet | 95% | 0.134 | 0.594 | 7.3 | 118.2 |
| VQGAN w/ Rotation Trick | ImageNet | 98% | 0.002 | 0.422 | 4.6 | 146.5 |
| VQGAN | FFHQ & CelebA-HQ | 27% | 0.233 | 0.565 | 4.7 | 5.0 |
| VQGAN w/ Rotation Trick | FFHQ & CelebA-HQ | 99% | 0.002 | 0.313 | 3.7 | 5.2 |

**Table 3: Results for VQGAN designed for latent diffusion as implemented in [https://github.com/CompVis/latent-diffusion](https://github.com/CompVis/latent-diffusion). Both settings train on ImageNet.**
| Approach | Latent Shape | Codebook Size | Codebook Usage | Quantization Error ($\downarrow$) | Valid Loss ($\downarrow$) | r-FID ($\downarrow$) | r-IS ($\uparrow$) |
| --- | --- | --- | --- | --- | --- | --- | --- |
| VQGAN | $64\times 64\times 3$ | $8192$ | 15% | 2.5e-3 | 0.183 | 0.53 | 220.6 |
| VQGAN w/ Rotation Trick | $64\times 64\times 3$ | $8192$ | 10% | 2.9e-4 | 0.173 | 0.38 | 222.4 |
| VQGAN | $32\times 32\times 4$ | $16384$ | 2% | 1.2e-2 | 0.385 | 5.0 | 141.5 |
| VQGAN w/ Rotation Trick | $32\times 32\times 4$ | $16384$ | 9% | 7.2e-4 | 0.287 | 1.6 | 190.3 |

### 5.2 VQGAN Evaluation

Moving to the next level of complexity, we evaluate the effect of the rotation trick on VQGANs (Esser et al., ). The VQGAN training objective is:

$$ $\displaystyle\mathcal{L}_{\text{VQGAN}}$ $\displaystyle=\mathcal{L}_{\text{Per}}+{\lVert sg(e)-q\rVert}_{2}^{2}+\beta{ \lVert e-sg(q)\rVert}_{2}^{2}+\lambda\mathcal{L}_{\text{Adv}}$ $$

where $\mathcal{L}_{\text{Per}}$ is the perceptual loss from Johnson et al. () and replaces the $L_{2}$ loss used to train VQ-VAEs. $L_{Adv}$ is a patch-based adversarial loss similar to the adversarial loss in Conditional GAN (Isola et al., ). $\beta$ is a constant that weights the commitment loss while $\lambda$ is an adaptive weight based on the ratio of $\nabla\mathcal{L}_{\text{Per}}$ to $\nabla\mathcal{L}_{\text{Adv}}$ with respect to the last layer of the decoder.

Experimental Settings.
We evaluate VQGANs under two settings: (1) the paradigm amenable to autoregressive modeling with Transformers as described in Esser et al. () and (2) the paradigm suitable to latent diffusion models as described in Rombach et al. (). The first setting follows the convolutional neural network and default hyperparameters described in Esser et al. () while the second follows those from Rombach et al. (). A full description of both training settings is provided in Table 7 of the Appendix.

Results. Our results are listed in Table 2 for the first setting and Table 3 for the second. Similar to our findings in Section 5.1, we find that training a VQ-VAE with the rotation trick substantially decreases quantization error and often improves codebook usage. Moreover, reconstruction performance as measured on the validation set by the total loss, r-FID, and r-IS are significantly improved across both modeling paradigms.

**Table 4: Results for ViT-VQGAN (Yu et al., ) trained on ImageNet. The latent shape is $8\times 8\times 32$ with $8192$ codebook vectors. r-FID and r-IS are reported on the validation set.**
| Approach | Codebook Usage ($\uparrow$) | Train Loss ($\downarrow$) | Quantization Error ($\downarrow$) | Valid Loss ($\downarrow$) | r-FID ($\downarrow$) | r-IS ($\uparrow$) |
| --- | --- | --- | --- | --- | --- | --- |
| ViT-VQGAN [reported] | — | — | — | — | 22.8 | 72.9 |
| ViT-VQGAN [ours] | 0.3% | 0.124 | 6.7e-3 | 0.127 | 29.2 | 43.0 |
| ViT-VQGAN w/ Rotation Trick | 2.2% | 0.113 | 8.3e-3 | 0.113 | 11.2 | 93.1 |

**Table 5: Results for TimeSformer-VQGAN trained on BAIR and UCF-101 with 1024 codebook vectors. $\dagger$: model suffers from codebook collapse and diverges. r-FVD is computed on the validation set.**
| Approach | Dataset | Codebook Usage | Train Loss ($\downarrow$) | Quantization Error ($\downarrow$) | Valid Loss ($\downarrow$) | r-FVD ($\downarrow$) |
| --- | --- | --- | --- | --- | --- | --- |
| TimeSformer^† | BAIR | 0.4% | 0.221 | 0.03 | 0.28 | 1661.1 |
| TimeSformer w/ Rotation Trick | BAIR | 43% | 0.074 | 3.0e-3 | 0.074 | 21.4 |
| TimeSformer^† | UCF-101 | 0.1% | 0.190 | 0.006 | 0.169 | 2878.1 |
| TimeSformer w/ Rotation Trick | UCF-101 | 30% | 0.111 | 0.020 | 0.109 | 229.1 |

### 5.3 ViT-VQGAN Evaluation

Improving upon the VQGAN model, Yu et al. () propose using a ViT (Dosovitskiy, ) rather than CNN to parameterize the encoder and decoder. The ViT-VQGAN uses factorized codes and $L_{2}$ normalization on the output and input to the vector quantization layer to improve performance and training stability. Additionally, the authors change the training objective, adding a logit-laplace loss and restoring the $L_{2}$ reconstruction error to $\mathcal{L}_{\text{VQGAN}}$.

Experimental Settings. We follow the open source implementation of [https://github.com/thuanz123/enhancing-transformers](https://github.com/thuanz123/enhancing-transformers) and use the default model and hyperparameter settings for the small ViT-VQGAN. A complete description of the training settings can be found in Table 8 of the Appendix.

Results. Table 4 summarizes our findings. Similar to our previous results for VQ-VAEs in Section 5.1 and VQGANS in Section 5.2, codebook utilization and reconstruction metrics are significantly improved; however in this case, the quantization error is roughly the same.

### 5.4 Video Evaluation

Expanding our analysis beyond the image modality, we evaluate the effect of the rotation trick on video reconstructions from the BAIR Robot dataset (Ebert et al., ) and from the UCF101 action recognition dataset (Soomro, ). We follow the quantization paradigm used by ViT-VQGAN, but replace the ViT with a TimeSformer (Bertasius et al., ) video model. Due to compute limitations, both encoder and decoder follow a relatively small TimeSformer model: 8 layers, 256 hidden dimensions, 4 attention heads, and 768 MLP hidden dimensions. A complete description of the architecture, training settings, and hyperparameters are provided in Section A.5.4.

Results. Table 5 shows our results. For both datasets, training a TimeSformer-VQGAN model with the STE results in codebook collapse. We explored several different hyperparameter settings; however in all cases, codebook utilization drops to almost 0% within the first several epochs. On the other hand, models trained with the rotation trick do not exhibit any training instability and produce high quality reconstructions as indicated by r-FVD (Unterthiner et al., ). Several non-cherry picked video reconstructions are displayed in Section A.5.4.

## 6 Conclusion

In this work, we explore different ways to propagate gradients through the vector quantization layer of VQ-VAEs and find that preserving the angle—rather than the direction—between the codebook vector and gradient induces desirable effects for how points within the same codebook region are updated. Specifically, the gradient to the encoder changes as it propagates through the vector quantization layer to push points with high distortion to new codebook regions or to pull them closer to the chosen codebook vector.
The former capability corresponds to increased codebook usage and higher information capacity and the latter to decreased quantization error and lower distortion.

These effects cause a substantial improvement in model performance. Across 11 different settings, we find that training VQ-VAEs with the rotation trick improves their reconstructions. For example, training one of the VQGANs used in latent diffusion with the rotation trick improves r-FID from 5.0 to 1.6 and r-IS from 141.5 to 190.3, reduces quantization error by two orders of magnitude, and increases codebook usage by 4.5x.

## Appendix A Appendix

### A.1 Comparison within Generative Modeling Applications

Absent from our work is an analysis on the effect of VQ-VAEs trained with the rotation trick on down-stream generative modeling applications. We see this comparison as outside the scope of this work and do not claim that improving reconstruction metrics, codebook usage, or quantization error in “Stage 1” VQ-VAE training will lead to improvements in “Stage 2” generative modeling applications.

While poor reconstruction performance will clearly lead to poor generative modeling, recent work (Yu et al., ) suggests that—at least for autoregressive modeling of codebook sequences with MaskGit (Chang et al., )—the connection between VQ-VAE reconstruction performance and downstream generative modeling performance is non-linear. Specifically, increasing the size of the codebook past a certain amount will improve VQ-VAE reconstruction performance but make downstream likelihood-based geneative modeling of codebook vectors more difficult.

We believe this nuance may extend beyond MaskGit, and that the desiderata for likelihood-based generative models will likely be different than that for score-based generative models like diffusion. It is even possible that different preferences appear within the same class. For example, left-to-right autoregressive modeling of codebook elements with Transformers (Vaswani, ) may exhibit different preferences for Stage 1 VQ-VAE models than those of MaskGit.

These topics deserve a deep, and rich, analysis that we would find difficult to include within this work as our focus is on propagating gradients through vector quantization layers. As a result, we entrust the exploration of these questions to future work.

### A.2 Treating R 𝑅 R italic_R and ‖ q ‖ ‖ e ‖ norm 𝑞 norm 𝑒 \frac{||q||}{||e||} divide start_ARG | | italic_q | | end_ARG start_ARG | | italic_e | | end_ARG as Constants

In the rotation trick, we treat $R$ and $\frac{||q||}{||e||}$ as constants and detached from the computational graph during the forward pass of the rotation trick. In this section, we explain why this is the case.

The rotation trick computes the input to the decoder $\tilde{q}$ after performing a non-differentiable codebook lookup on $e$ to find $q$. It is defined as:

$$ $\displaystyle\tilde{q}$ $\displaystyle=\frac{||q||}{||e||}Re$ $$

As shown in Section 4, $R$ is a function of both $e$ and $q$. However, using the quantization function $\mathcal{Q}(e)=q$, we can rewrite both $\frac{||q||}{||e||}$ and $R$ as a single function of $e$:

$$ $\displaystyle f(e)$ $\displaystyle=\frac{{\lVert\mathcal{Q}(e)\rVert}}{{\lVert e\rVert}}\left[I-2 \left[\frac{e+\mathcal{Q}(e)}{{\lVert e+\mathcal{Q}(e)\rVert}}\right]\left[ \frac{e+\mathcal{Q}(e)}{{\lVert e+\mathcal{Q}(e)\rVert}}\right]^{T}+2\mathcal{ Q}(e)e^{T}\right]$ $\displaystyle=\frac{{\lVert q\rVert}}{{\lVert e\rVert}}R$ $$

The rotation trick then becomes

$$ $\displaystyle\tilde{q}$ $\displaystyle=f(e)e$ $$

and differentiating $\tilde{q}$ with respect to $e$ gives us:

$$ $\displaystyle\frac{\partial\tilde{q}}{\partial e}$ $\displaystyle=f^{\prime}(e)e+f(e)$ $$

However, $f^{\prime}(e)$ cannot be computed as it would require differentiating through $\mathcal{Q}(e)$, which is a non-differentiable codebook lookup. We therefore drop this term and use only $f(e)$ as our approximation of the gradient through the vector quantization layer: $\frac{\partial\tilde{q}}{\partial e}=f(e)$. This approximation conveys more information about the vector quantization operation than the STE, which sets $\frac{\partial\tilde{q}}{\partial e}=I$.

### A.3 Proof the Rotation Trick Preserves Angles

For encoder output $e$ and corresponding codebook vector $q$, we provide a formal proof that the rotation trick preserves the angle between $\nabla_{q}\mathcal{L}$ and $q$ as $\nabla_{q}\mathcal{L}$ moves to $e$. Unlike the notation in the main text, which assumes $q\in\mathbb{R}^{d\times 1}$, we use batch notation in the following proof to illustrate how the rotation trick works when training neural networks. Specifically, $q\in\mathbb{R}^{b\times d}$ and $R\in\mathbb{R}^{b\times d\times d}$ where $b$ is the number of examples in a batch and $d$ is the dimension of the codebook vector.

###### Remark 1 .

The angle between $q$ and $\nabla_{q}\mathcal{L}$ is preserved as $\nabla_{q}\mathcal{L}$ moves to $e$.

###### Proof.

With loss of generality, suppose ${\lVert e\rVert}={\lVert q\rVert}=1$. Then we have

$$ $\displaystyle q$ $\displaystyle=eR^{T}$ $\displaystyle\frac{\partial q}{\partial e}$ $\displaystyle=R$ $$

The gradient at $e$ will then equal:

$$ $\displaystyle\nabla_{e}\mathcal{L}$ $\displaystyle=\nabla_{q}\mathcal{L}\left[\frac{\partial q}{\partial e}\right]$ $\displaystyle=\nabla_{q}\mathcal{L}\left[R\right]$ $$

Let $\theta$ be the angle between $q$ and $\nabla_{q}\mathcal{L}$ and $\phi$ be the angle between $e$ and $\nabla_{q}\mathcal{L}$. Via the Euclidean inner product, we have:

$$ $\displaystyle{\lVert\nabla_{q}\mathcal{L}\rVert}\cos\theta$ $\displaystyle=q\left[\nabla_{q}\mathcal{L}\right]^{T}$ $\displaystyle=eR^{T}\left[\nabla_{q}\mathcal{L}\right]^{T}$ $\displaystyle=e\left[\nabla_{q}\mathcal{L}R\right]^{T}$ $\displaystyle=e\left[\nabla_{e}\mathcal{L}\right]^{T}$ $\displaystyle={\lVert\nabla_{q}\mathcal{L}\rVert}\cos\phi$ $$

so $\theta=\phi$ and the angle between $q$ and $\nabla_{q}\mathcal{L}$ is preserved as $\nabla_{q}\mathcal{L}$ moves to $e$.
∎

###### Remark 1 .

###### Proof.

### A.4 HouseHolder Reflection Transformation

For any given $e$ and $q$, the rotation that aligns $e$ with $q$ in the plane spanned by both vectors can be efficiently computed with Householder matrix reflections.

###### Definition 1 (Householder Reflection Matrix) .

For a vector $a\in\mathbb{R}^{d}$, $I-2aa^{T}\in\mathbb{R}^{d\times d}$ is reflection matrix across the subspace (hyperplane) orthogonal to $a$.

###### Remark 2 .

Let $a,b\in\mathbb{R}^{d}$ that define hyperplanes $a^{\perp}$ and $b^{\perp}$ respectively. Then a reflection across $a^{\perp}$ followed by a reflection across $b^{\perp}$ is a rotation of $2\theta$ in the plane spanned by $a,b$ where $\theta$ is the angle between $a,b$.

###### Remark 3 .

Let $a,b\in\mathbb{R}^{d}$ with ${\lVert a\rVert}={\lVert b\rVert}=1$. Define $c=\frac{a+b}{{\lVert a+b\rVert}}$ as the vector half-way between $a$ and $b$ so that $\angle(a,b)=\theta$ and $\angle(a,c)=\angle(b,c)=\frac{\theta}{2}$. $(I-cc^{T})$ is the Householder reflection across $a^{\perp}$ and $(I-bb^{T})$ is the Householder reflection across $b^{\perp}$. Therefore, $(I-cc^{T})(I-bb^{T})$ corresponds to a rotation of $2(\frac{\theta}{2})=\theta$ in $\text{span}(a,b)$ so that $(I-cc^{T})(I-bb^{T})a=b$.

Returning to vector quantization with $q=[\frac{{\lVert q\rVert}}{{\lVert e\rVert}}R]e$, we can write $R$ as the product of two Householder reflection matrices. Without loss of generality, assume $e$ and $q$ are unit norm. Setting $r=\frac{e+q}{{\lVert e+q\rVert}}$ and simplifying yields:

$$ $R=I-2rr^{T}+2qe^{T}$ $$

###### Definition 1 (Householder Reflection Matrix) .

###### Remark 2 .

###### Remark 3 .

### A.5 Training Settings

We detail the training settings used in our experimental analysis in Section 5. While a text description can be helpful for understanding the experimental settings, our released code should be referenced to fully reproduce the results presented in this work.

#### A.5.1 VQ-VAE Evaluation.

Table 6 summarizes the hyperparameters used for the experiments in Section 5.1. For the encoder and decoder architectures, we use the Convolutional Neural Network described by Esser et al. (). The hyperparameters for the cosine similarity codebook lookup follow from Yu et al. () and the hyperparameters for the Euclidean distance codebook lookup follow from the default values set in the Vector Quantization library from [https://github.com/lucidrains/vector-quantize-pytorch](https://github.com/lucidrains/vector-quantize-pytorch). All models replace the codebook loss with the exponential moving average described in Van Den Oord et al. () with decay = 0.8. The notation for both encoder and decoder architectures is adapted from Esser et al. ().

**Table 6: Hyperparameters for the experiments in Table 1. ($1024$, $32$) indicates a model trained with a codebook size of $1024$ and codebook dimension of $32$. Similarly, $(8192$, $3$) indicates a model trained with codebook size of $8192$ and codebook dimension of $3$.**
|  | Cosine Similarity Lookup | Euclidean Lookup |  |  |
| --- | --- | --- | --- | --- |
|  | ($1024$, $32$) | ($8192$, $3$) | ($1024$, $32$) | ($8192$, $3$) |
| Input size | $256\times 256\times 3$ | $256\times 256\times 3$ | $256\times 256\times 3$ | $256\times 256\times 3$ |
| Latent size | $16\times 16\times 32$ | $64\times 64\times 3$ | $16\times 16\times 32$ | $64\times 64\times 3$ |
| $\beta$ (commitment loss coefficient) | 1.0 | 1.0 | 1.0 | 1.0 |
| encoder/decoder channels | 128 | 128 | 128 | 128 |
| encoder/decoder channel mult. | [1, 1, 2, 2, 4] | [1, 2, 4] | [1, 1, 2, 2, 4] | [1, 2, 4] |
| [Effective] Batch size | 256 | 256 | 256 | 256 |
| Learning rate | $1\times 10^{-4}$ | $1\times 10^{-4}$ | $5\times 10^{-5}$ | $5\times 10^{-5}$ |
| Weight Decay | $1\times 10^{-4}$ | $1\times 10^{-4}$ | 0 0 | 0 0 |
| Codebook size | 1024 | 8192 | 1024 | 8192 |
| Codebook dimension | 32 | 3 | 32 | 3 |
| Training epochs | 25 | 20 | 25 | 20 |

#### A.5.2 VQGAN Evaluation

Table 7 summarizes the hyperparameters for the VQGAN experiments in Section 5.2. Non-cherry picked reconstructions for the models trained in Table 2 and Table 3 are depicted in Figure 6. As indicated by the increased r-FID score, the reconstructions out by the VQGAN trained with the rotation trick appear to better reproduce the original image, especially fine details.

Figure: Figure 6: Non-cherry picked reconstructions for VQGAN results in Table 2 and Table 3. ROT is an abbreviation for the rotation trick.
Refer to caption: x6.png

**Table 7: Hyperparameters for the experiments in Table 2 and Table 3. We implement the rotation trick in the open source [https://github.com/CompVis/taming-transformers](https://github.com/CompVis/taming-transformers) for the experiments in Table 2 and implement the rotation trick in [https://github.com/CompVis/latent-diffusion](https://github.com/CompVis/latent-diffusion) for Table 3. In both settings, we use the default hyperparameters. $\dagger$: 18 epochs for ImageNet and 50 epochs for FFHQ & CelebA-HQ.**
|  | Table 2 VQGAN | Table 3 VQGAN | Table 3 VQGAN |
| --- | --- | --- | --- |
| Input size | $256\times 256\times 3$ | $256\times 256\times 3$ | $256\times 256\times 3$ |
| Latent size | $16\times 16\times 256$ | $64\times 64\times 3$ | $32\times 32\times 4$ |
| Codebook weight | $1.0$ | $1.0$ | $1.0$ |
| Discriminator weight | $0.8$ | $0.75$ | $0.6$ |
| encoder/decoder channels | $128$ | $128$ | $128$ |
| encoder/decoder channel mult. | $[1,1,2,2,4]$ | $[1,2,4]$ | $[1,2,2,4]$ |
| [Effective] Batch size | 48 | 16 | 16 |
| [Effective] Learning rate | $4.5\times 10^{-6}$ | $4.5\times 10^{-6}$ | $4.5\times 10^{-6}$ |
| Codebook size | $1024$ | $8192$ | $16384$ |
| Codebook dimensions | $256$ | $3$ | $4$ |
| Training Epochs | 18/50^† | $4$ | $4$ |

#### A.5.3 ViT-VQGAN Evaluation

Our experiments in Section 5.3 use the ViT-VQGAN implemented in the open source repository [https://github.com/thuanz123/enhancing-transformers](https://github.com/thuanz123/enhancing-transformers). The default hyperparameters follow those specified by Yu et al. (), and our experiments use the default architecture settings specified by the ViT small model configuration file.

We depict several reconstructions in Figure 7 and see that the ViT-VQGAN trained with the rotation trick is able to better replicate small details that the ViT-VQGAN trained with the STE misses. This is expected as the rotation trick drops r-FID from 29.2 to 11.2 as shown in Table 4.

Figure: Table 8: Hyperparameters for the experiments in Table 4.
Refer to caption: x7.png

#### A.5.4 TimeSformer Video Evaluation

We use the Hugging Face implementation of the TimeSformer from [https://huggingface.co/docs/transformers/en/model_doc/timesformer](https://huggingface.co/docs/transformers/en/model_doc/timesformer) and the ViT-VQGAN vector quantization layer from [https://github.com/thuanz123/enhancing-transformers](https://github.com/thuanz123/enhancing-transformers). We loosely follow the hyperparameters listed in Yu et al. () and implement a small TimeSformer encoder and decoder due to GPU VRAM constraints. We reuse the dataloading functions of both BAIR Robot Pushing and UCF101 dataloaders from Yan et al. () at [https://github.com/wilson1yan/VideoGPT](https://github.com/wilson1yan/VideoGPT). A complete description of the settings we use for the experiments in Section 5.4 are listed in Table 9.

We also visualize the reconstructions for the TimeSformer-VQGAN trained with the rotation trick and the STE. Figure 8 shows the reconstructions for BAIR Robot Pushing, and Figure 9 shows the reconstructions for UCF101. For both datasets, the model trained with the STE undergoes codebook collapse early into training. Specifically, it learns to only use $\frac{4}{1024}$ of the available codebook vectors for BAIR Robot Pushing and $\frac{2}{2048}$ for UCF101 in a batch of 2 input examples. Small manual tweaks to the architecture and training hyperparameters did not fix this issue.

In contrast, VQ-VAEs trained with the rotation trick do not manifest this training instability. Instead, codebook usage is relatively high—at 43% for BAIR Robot Pushing and 30% for UCF101—and the reconstructions accurately match the input, even though both encoder and decoder are very small video models.

Figure: Figure 8: BAIR Robot Pushing reconstruction examples. While the model trains on 16 video frames at a time, we only visualize 4 at a time in this figure. The model trained with the STE undergoes codebook collapse, using 4 out of the 1024 codebook vectors for reconstruction and therefore crippling the information capacity of the vector quantization layer. On the other hand, the VQ-VAE trained with the rotation trick instead uses an average of 441 of the 1024 codebook vectors in each batch of 2 example videos.
Refer to caption: x8.png

Figure: Figure 9: UCF-101 reconstruction examples. While the model trains on 16 video frames at a time, we only visualize 4 at a time in this figure. The model trained with the STE undergoes codebook collapse, using approximately 2 out of the 2048 codebook vectors for reconstruction and therefore crippling the information capacity of the vector quantization layer. The VQ-VAE trained with the rotation trick instead uses an average of 615 of the 2048 codebook vectors in each batch of 2 example videos.
Refer to caption: x9.png

**Table 9: Hyperparameters for the experiments in Table 5. A TimeSformer (Bertasius et al., ) is used for the Encoder and Decoder architecture as implemented at [https://huggingface.co/docs/transformers/en/model_doc/timesformer](https://huggingface.co/docs/transformers/en/model_doc/timesformer). The vector quantization layer between Encoder and Decoder follow from Yu et al. () as implemented in [https://github.com/thuanz123/enhancing-transformers](https://github.com/thuanz123/enhancing-transformers).**
|  | TimeSformer-VQGAN Settings |  |
| --- | --- | --- |
|  | BAIR Robot Pushing | UCF101 Action Recognition |
| Input size | $16\times 64\times 64\times 3$ | $16\times 128\times 128\times 3$ |
| Patch size | $2$ | $4$ |
| Encoder / Decoder Hidden Dim | $256$ | $256$ |
| Encoder / Decoder MLP Dim | $768$ | $768$ |
| Encoder / Decoder Hidden Depth | $8$ | $8$ |
| Encoder / Decoder Hidden Num Heads | $4$ | $4$ |
| Codebook Dimension | $32$ | $32$ |
| Codebook Size | $1024$ | $2048$ |
| Codebook Loss Coefficient | 1.0 | 1.0 |
| Log Laplace loss Coefficient | 0.0 | 0.0 |
| Log Gaussian Coefficient | 1.0 | 1.0 |
| Perceptual loss Coefficient | 0.1 | 0.1 |
| Adversarial loss Coefficient | 0.1 | 0.1 |
| [Effective] Batch size | 24 | 20 |
| Learning rate | $1\times 10^{-4}$ | $4.5\times 10^{-6}$ |
| Weight Decay | $1\times 10^{-4}$ | $1\times 10^{-4}$ |
| Training epochs | 30 | 3 |

## Peer Reviews

confidence: 4
contribution: 3
initial_rating: 8
presentation: 4
questions: Please see the weaknesses above.
soundness: 4
strengths: - The method is principled, and I think it is a good and simple idea to improve some of the issues of STE
- The paper motivates the issues with STE well, as well as the rotation trick itself. The explanations with figures are helpful to understand the intuition of the method. 
- Experimental results are fairly extensive, encompassing many different scenarios where VQ-based models are used, each showing the superiority of the rotation trick over STE.
summary: This paper proposes an improvement to the STE method for training VQ-VAE. While STE copies the gradient from the quantizer output to input, this paper proposes the rotation trick, which uses a rotated and scaled gradient according to the value of the quantizer input. This enables the gradient to depend on the value of the quantizer input, whereas in STE it is the same for all inputs that get mapped to the same codebook vector. Experimental results demonstrate superior reconstruction performance and codebook usage across a variety of tasks using VQ-VAE methods.
weaknesses: - I have some trouble understanding why the Hessian idea is not good. The explanations provided in the paper are that it is similar to a vanilla auto encoder training, with no quantization. However, this seems only applicable to the "exact gradient" idea and not the Hessian idea. It seems the Hessian idea sets the gradient at e to the gradient at q (what STE does), plus the additional curvature term. Whereas the rotation trick multiples the gradient at q by the scaled rotation matrix. So it seems that both capture the location of e.
- Moreover, I wonder if the analysis in section 4.3 could be extended to the Hessian idea. It seems the Hessian idea also gets the benefits that Section 4.3 argues in favor of the rotation trick. There seems to be some disconnect between this and the experimental results where it was shown the Hessian idea does not perform well. If a stronger explanation is provided, that would be helpful to understand. 
- While the experimental settings are fairly extensive, they primarily demonstrate how the rotation trick is superior to STE. I think some other baseline methods that improve upon STE are also necessary for comparison. Namely, those mentioned in Section 2, such as Huh et al 2023, Gautam et al, 2023, and Takida et al, 2022. This would provide comparison between the rotation trick and other methods that go beyond STE.

---

confidence: 4
contribution: 3
initial_rating: 8
presentation: 3
questions: 1. What if we just do one Householder reflection, i.e. $R = I - 2rr^T$? It still preserves the angle ($\tilde{\theta} \in [0, \pi]$), but the "push" effect in Section 4.3 becomes "pull", and vice versa. However, as you pointed out, both push and pull are beneficial, so the result should still be good. The mirror symmetry makes me think this will be as effective as $R = I - 2rr^T + 2qe^T$, and it would be great if you could verify this hypothesis experimentally.
1. The rotation/reflection transformation depends on the location of a center, which you implicitly choose to be the origin. Do you need to shift the $q$ and $e$ such that their center/mean is the origin, or does the rotation trick work even if all $q$ and $e$ fall on one side of the origin (e.g. imagine Figure 4, but on a shifted coordinates system $x' = x + 10, y' = y + 10$, such that the coordinate of all points are positive)?
soundness: 4
strengths: 1. The rotation trick is novel, intuitive, and mathematically principled
1. Applying two Householder reflectors is computationally efficient, and incurs no additional training cost
1. Strong empirical success across various VQ(-VAE|GAN) training paradigms
summary: The authors address the training instability of the straight-through estimator by proposing the rotation trick for vector quantization (VQ), i.e. rotating the gradient $\nabla_q\mathcal{L}$ at the codebook vector $q$ to become the gradient $\nabla_e\mathcal{L}$ at the encoder output $e$ such that the angle between $q$ and $\nabla_q\mathcal{L}$ after the VQ equals the angle between $e$ and $\nabla_e\mathcal{L}$. The rotation trick significantly improves the image reconstruction quality across 11 VQ(-VAE|GAN) training paradigms.
weaknesses: 1. The method, i.e. how to achieve the rotation, is not explained clearly in the title or main text. It's probably a good idea to move Appendix A.4 into the main text. In fact, consider renaming this paper to "Restructuring Vector Quantization with the *Reflection* Trick", because the rotation $R$ is really two Householder reflectors chained together (maybe we can get away with one reflection? see below).
1. Appendix A.4 appears to be incorrect: in Remark 3, I think the final reflection/rotation matrix should be $(I-2cc^T)(I-2aa^T)$, or equivalently $(I-2bb^T)(I-2cc^T)$, but not $(I-cc^T)(I-bb^T)$. To make this part easier to understand, perhaps you should re-use the $e, q, r$ notation instead of introducing new symbols $a, b, c$, and include a detailed derivation of why $R = (I-2cc^T)(I-2aa^T) = (I-2bb^T)(I-2cc^T) = I - 2rr^T + 2qe^T$.
1. The effect of the rotation trick is only verified on a high level by codebook usage and quantization error. In particular, Figure 4 is only a diagram ("how we think it works"), not generated from an actual numerical simulation ("how it really works"). Moreover, Figure 4 only shows one gradient update step, and it's unclear how the rotation trick affect the codebook in the long run.
1. The notations are a little confusing. For example, Section 4.3 allows $\tilde{\theta}$ to be negative or exceed $\pi$, but the angle between two vectors is typically defined within $[0, \pi]$. The top panel in Figure 5 also defines $\tilde{\theta}$ in a nonstandard way (when $q$ and $\nabla_q\mathcal{L}$ points at opposite directions, $\tilde{\theta} = 0$ according to the diagram, but normally we say $\tilde{\theta} = \pi$ for this case). In addition, renaming $\tilde{\theta}$ to $\phi$ might avoid some misread.
1. There is an extra "a" on line 113.

---

confidence: 3
contribution: 3
initial_rating: 6
presentation: 3
questions: - Suggestion: Figure 2 beautifully illustrates the difference between the original and the STE gradient. To make it even nicer, I think the authors should consider adding the "rotation trick" gradient field as well!
 - Recently, the STE was connected to the deeper theory of numerical integration in [1]. Could the authors comment on whether their method might also have such an interpretation?

## References
[1] Liu, L., Dong, C., Liu, X., Yu, B., & Gao, J. (2024). Bridging discrete and back-propagation: Straight-through and beyond. Advances in Neural Information Processing Systems, 36.
soundness: 3
strengths: The paper's core idea to set the Jacobian to the quantity suggested by the authors is reasonably well-motivated. It can be considered a natural dual to STE, as it uses the multiplicative/polar structure over Euclidean space as opposed to the additive structure used by STE.

The core idea in the paper is neat and simple, and the experimental verification appears reasonably extensive and rigorous.

The paper is well-written, with very nice illustrative figures.
summary: The authors propose an alternative to using straight-through estimation for the gradients of a vector-quantized variational autoencoder (VQ-VAE) to stabilize training and improve codebook usage.

VQ-VAEs are a popular model for data compression and generation, consisting of an encoder and a decoder network. However, instead of passing the encoder output directly to the decoder, the output is first quantized to some codebook vector; these vectors are also learnt during training. However, vector quantization (VQ) is a non-differentiable operation; thus, practitioners usually ignore the quantization operation in the backward pass to enable gradient back-propagation; this is called straight-through estimation (STE). More formally, given some encoder output $e$ and its vector quantized version $q$, STE "sets" the Jacobian $\frac{\partial q}{\partial e} = I$, where $I$ is the identity matrix instead of the all-zero matrix (almost everywhere) as would be dictated by the standard rules of calculus.

In this paper, the authors identify some important shortcomings of using STE in practice, such as training instability and codebook collapse. Based on their observations, they instead suggest "setting" the Jacobian $\frac{\partial q}{\partial e} = \frac{\lVert q \rVert}{\lVert e \rVert} \cdot R$, where $R$ is an orthogonal matrix depending on $e$ such that $q = \frac{\partial q}{\partial e} \times e$.

The authors argue and demonstrate empirically across several experiments that this pick significantly improves codebook utilization, reconstruction loss, and reconstruction quality.
weaknesses: While I do not think the paper suffers from any major weakness, there are two aspects in which it could be strengthened:
1. The authors do not discuss the limitations of their proposed method. While they present good arguments for when their proposed rotation trick is preferable to STE, have they considered the scenarios in which it might be undesirable?
2. The theoretical motivation for the rotation trick is unclear, other than the fact that it is the natural "multiplicative counterpart" to STE. While I really like the "post hoc" argument that the rotation trick results in a non-constant Jacobian, which might help improve codebook usage, I do not see a good mathematical reason for ensuring that the angles between the vectors and their gradients are preserved, which is better than magnitude and direction. For example, one could argue that an advantage of the identity Jacobian is that it does not shrink or expand space since its determinant is one. This could be solved by combining the STE and the rotation trick by setting (in the authors' notation) $\tilde{q} = R e + (q - R e)$ for an appropriate rotation matrix $R$ and using an appropriate stop-gradient; this would now preserve both magnitude and angle as I understand. In fact, we can even create an entire two-parameter family of gradient approximations by setting (in the authors' notation) $\tilde{q} = \lambda^\alpha R_{\alpha, \beta} e + \beta (q - R_{\alpha, \beta} e)$. Have the authors considered such a solution? If not, could the authors run some small-scale experiments at least to check its performance?

Minor:
 - Fig 2: Increase the tick and axis label font size to match the font size of the caption, as the text is illegible at 100% zoom right now.

---

confidence: 4
contribution: 3
initial_rating: 8
presentation: 4
questions: * Why is the rescaling factor $||q|| / ||e||$ used for the gradient approximation? How does it affect training of the encoder and the codebook?
* Minor comment: In Definition 1 (Householder Reflection Matrix), isn't it required that $||a|| = 1$?
* Minor comment: there is a typo: "a a convex combination'" in p3 L6.
soundness: 4
strengths: This paper is well-written and well-motivated. Its review of VQ-VAE, STE, and related work is concise and easy to follow, providing a clear motivation for the study.
The algorithm for the rotation trick is clearly formulated.
The explanation of the effects of the rotation trick, supported by figures, is illustrative.
The effectiveness of the proposed method is investigated through experiments in 11 different settings, which can be considered sufficient.
summary: This paper proposes a method, reffered to as the "rotation trick", to propagate approximated gradients through the vector quantization layer of VQ-VAEs.
The proposed method addresses the issue of a "straight-through estimator (STE)," which loses information about the positions of encoder outputs relative to codebook vectors.
The capability of the method to increase information capacity and reduce distortion in vector quantization is illustrated with examples and explanation of the "push-pull effect".
The authors conduct experiments across 11 VQ-VAE training settings to demonstrate that the proposed method improves reconstruction performance compared to baseline methods.
weaknesses: Overall, this paper is well-motivated and well-structured, but I would like to raise a point that requires clarification.

The algorithm is designed to preserves the angle between a vector and a gradient, which leads to the push-pull effect described in Section 4.3. However, it appears that using only a rotation, $\frac{\partial \tilde{q}}{\partial e} = R$, satisfies this requirement, and rescaling with $\frac{||q||}{||e||}$ is not necessary for the gradient approximation. (I understand that rescaling is necessary for transforming $e$ into $q$.)
Additionally, it seems that rescaling of gradients may hinder the propagation of gradients when $||q||$ is close to zero.
Providing an explanation for why the rescaling factor $\frac{||q||}{||e||}$ is used for the gradient approximation would clarify the motivation behind this algorithm's design.

Meta-review: All reviewers appreciate the paper’s contribution and strongly recommend accepting the paper. The AC concurs, after reading the paper quickly.

Decision: Accept (Oral)