|
| 1 | +from typing import Tuple |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.nn.functional as F |
| 5 | +from einops import rearrange |
| 6 | +from torch import Tensor, einsum, nn |
| 7 | + |
| 8 | +""" Utils """ |
| 9 | + |
| 10 | + |
| 11 | +def to_bits(indices: Tensor, num_bits: int) -> Tensor: |
| 12 | + bitmask = 2 ** torch.arange(num_bits - 1, -1, -1) |
| 13 | + return indices.unsqueeze(-1).bitwise_and(bitmask).ne(0).long() |
| 14 | + |
| 15 | + |
| 16 | +def to_decimal(bits: Tensor) -> Tensor: |
| 17 | + num_bits = bits.shape[-1] |
| 18 | + bitmask = 2 ** torch.arange(num_bits - 1, -1, -1) |
| 19 | + return torch.sum(bitmask * bits, dim=-1) |
| 20 | + |
| 21 | + |
| 22 | +""" Bincodes """ |
| 23 | + |
| 24 | + |
| 25 | +class Bitcodes(nn.Module): |
| 26 | + def __init__(self, features: int, num_bits: int, temperature: int): |
| 27 | + super().__init__() |
| 28 | + self.temperature = temperature |
| 29 | + self.codebook = nn.Parameter(torch.randn(2 * num_bits, features)) |
| 30 | + |
| 31 | + def from_bits(self, bits: Tensor) -> Tensor: |
| 32 | + attn = F.one_hot(bits.long(), num_classes=2).float() |
| 33 | + attn = rearrange(attn, "b m p q -> b m (p q)") |
| 34 | + out = einsum("b m n, n d -> b m d", attn, self.codebook) |
| 35 | + return out |
| 36 | + |
| 37 | + def forward(self, x: Tensor) -> Tuple[Tensor, Tensor]: |
| 38 | + sim = einsum("b m d, n d -> b m n", x, self.codebook) |
| 39 | + pairs = rearrange(sim, "b m (p q) -> b m p q", q=2) |
| 40 | + |
| 41 | + if self.training: |
| 42 | + attn = F.gumbel_softmax(pairs, tau=self.temperature, dim=-1, hard=True) |
| 43 | + else: |
| 44 | + attn = F.one_hot(pairs.argmax(dim=-1), num_classes=2).float() |
| 45 | + |
| 46 | + attn = rearrange(attn, "b m p q -> b m (p q)") |
| 47 | + out = einsum("b m n, n d -> b m d", attn, self.codebook) |
| 48 | + bits = pairs.argmax(dim=-1) |
| 49 | + return out, bits |
0 commit comments