| 16 | |
| 17 | @si_module |
| 18 | class LatentQuantizer(nn.Module): |
| 19 | class Config: |
| 20 | compressor_config: Optional[FSQ.Config] = None |
| 21 | |
| 22 | dim: Optional[int] = None |
| 23 | ff_dim: Optional[int] = None |
| 24 | input_dim: int = None |
| 25 | |
| 26 | from_pretrained: Optional[Tuple[str, str]] = None |
| 27 | |
| 28 | def __init__(self, c: Config): |
| 29 | super().__init__() |
| 30 | |
| 31 | if exists(c.from_pretrained): |
| 32 | checkpoint = load_ckpt(*c.from_pretrained) |
| 33 | else: |
| 34 | assert exists(c.compressor_config), f'hmm {c}' |
| 35 | |
| 36 | self.compressor = c.compressor_config() |
| 37 | self.ffnn = FFNN(c.dim, c.ff_dim) |
| 38 | self.input = nn.Linear(c.input_dim, c.dim) if exists(c.input_dim) else nn.Identity() |
| 39 | |
| 40 | if exists(c.from_pretrained): |
| 41 | self.load_state_dict(checkpoint) |
| 42 | |
| 43 | @T.no_grad() |
| 44 | def forward(self, x, return_latent=False, known_latent=None): |
| 45 | """ |
| 46 | x: (B, S, D) |
| 47 | """ |
| 48 | if exists(known_latent): |
| 49 | return self.compressor.indices_to_codes(known_latent) |
| 50 | |
| 51 | x = self.input(x) |
| 52 | x = self.ffnn(x) |
| 53 | x, tokens = self.compressor(x) |
| 54 | |
| 55 | if return_latent: |
| 56 | return x, tokens |
| 57 | return x |
| 58 | |
| 59 | |
| 60 | @si_module |
nothing calls this directly
no outgoing calls
no test coverage detected