|
| 1 | +import pytest |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import torch |
| 5 | + |
| 6 | +from functions import vq, vq_st |
| 7 | + |
| 8 | +def test_vq_shape(): |
| 9 | + inputs = torch.rand((2, 3, 5, 7), dtype=torch.float32, requires_grad=True) |
| 10 | + codebook = torch.rand((11, 7), dtype=torch.float32, requires_grad=True) |
| 11 | + indices = vq(inputs, codebook) |
| 12 | + |
| 13 | + assert indices.size() == (2, 3, 5) |
| 14 | + assert not indices.requires_grad |
| 15 | + assert indices.dtype == torch.int64 |
| 16 | + |
| 17 | +def test_vq(): |
| 18 | + inputs = torch.rand((2, 3, 5, 7), dtype=torch.float32, requires_grad=True) |
| 19 | + codebook = torch.rand((11, 7), dtype=torch.float32, requires_grad=True) |
| 20 | + indices = vq(inputs, codebook) |
| 21 | + |
| 22 | + differences = inputs.unsqueeze(3) - codebook |
| 23 | + distances = torch.norm(differences, p=2, dim=4) |
| 24 | + |
| 25 | + _, indices_torch = torch.min(distances, dim=3) |
| 26 | + |
| 27 | + assert np.allclose(indices.numpy(), indices_torch.numpy()) |
| 28 | + |
| 29 | +def test_vq_st_shape(): |
| 30 | + inputs = torch.rand((2, 3, 5, 7), dtype=torch.float32, requires_grad=True) |
| 31 | + codebook = torch.rand((11, 7), dtype=torch.float32, requires_grad=True) |
| 32 | + codes = vq_st(inputs, codebook) |
| 33 | + |
| 34 | + assert codes.size() == (2, 3, 5, 7) |
| 35 | + assert codes.requires_grad |
| 36 | + assert codes.dtype == torch.float32 |
| 37 | + |
| 38 | +def test_vq_st_gradient1(): |
| 39 | + inputs = torch.rand((2, 3, 5, 7), dtype=torch.float32, requires_grad=True) |
| 40 | + codebook = torch.rand((11, 7), dtype=torch.float32, requires_grad=True) |
| 41 | + codes = vq_st(inputs, codebook) |
| 42 | + |
| 43 | + grad_output = torch.rand((2, 3, 5, 7)) |
| 44 | + grad_inputs, = torch.autograd.grad(codes, inputs, |
| 45 | + grad_outputs=[grad_output]) |
| 46 | + |
| 47 | + # Straight-through estimator |
| 48 | + assert grad_inputs.size() == (2, 3, 5, 7) |
| 49 | + assert np.allclose(grad_output.numpy(), grad_inputs.numpy()) |
| 50 | + |
| 51 | +def test_vq_st_gradient2(): |
| 52 | + inputs = torch.rand((2, 3, 5, 7), dtype=torch.float32, requires_grad=True) |
| 53 | + codebook = torch.rand((11, 7), dtype=torch.float32, requires_grad=True) |
| 54 | + codes = vq_st(inputs, codebook) |
| 55 | + |
| 56 | + indices = vq(inputs, codebook) |
| 57 | + codes_torch = torch.embedding(codebook, indices, padding_idx=-1, |
| 58 | + scale_grad_by_freq=False, sparse=False) |
| 59 | + |
| 60 | + grad_output = torch.rand((2, 3, 5, 7), dtype=torch.float32) |
| 61 | + grad_codebook, = torch.autograd.grad(codes, codebook, |
| 62 | + grad_outputs=[grad_output]) |
| 63 | + grad_codebook_torch, = torch.autograd.grad(codes_torch, codebook, |
| 64 | + grad_outputs=[grad_output]) |
| 65 | + |
| 66 | + # Gradient is the same as torch.embedding function |
| 67 | + assert grad_codebook.size() == (11, 7) |
| 68 | + assert np.allclose(grad_codebook.numpy(), grad_codebook_torch.numpy()) |
0 commit comments