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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""Inference-only PLPQ/HLQ quantizers used by PSI2.

The RGB, flow, and depth tokenizers released with PSI-0.5 all use the same
pyramidal local patch quantizer architecture with different channel counts and
numbers of residual scalar-quantizer codebooks. This file keeps only the pieces
needed for encode/decode at inference time:

- Haar patchwise wavelet projection.
- Local residual convolution blocks.
- Pyramidal finite scalar quantization (PFSQ).
- The PLPQ wrapper with ``quantize()``, ``decode()``, and
  ``decode_coarse_tokens()``.

The module names mirror the training implementation so existing checkpoints load
without key surgery.
"""

from __future__ import annotations

import math
from contextlib import nullcontext
from functools import wraps
from types import SimpleNamespace
from typing import Any, Dict, Iterable, List, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import pack, rearrange, unpack
from torch import Tensor, int32


def _autocast_disabled():
    """Return an autocast-disabled context/decorator without deprecation noise."""

    return torch.amp.autocast("cuda", enabled=False)


def _exists(value: Any) -> bool:
    return value is not None


def _default(*values: Any) -> Any:
    for value in values:
        if _exists(value):
            return value
    return None


def _maybe(fn):
    @wraps(fn)
    def inner(x, *args, **kwargs):
        if not _exists(x):
            return x
        return fn(x, *args, **kwargs)

    return inner


def _pack_one(tensor: torch.Tensor, pattern: str):
    return pack([tensor], pattern)


def _unpack_one(tensor: torch.Tensor, packed_shape, pattern: str):
    return unpack(tensor, packed_shape, pattern)[0]


def _round_ste(z: Tensor) -> Tensor:
    """Round with a straight-through estimator."""
    rounded = z.round()
    return z + (rounded - z).detach()


class LayerNorm(nn.Module):
    """LayerNorm with optional bias, matching the training implementation."""

    def __init__(self, ndim: int, bias: bool):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(ndim))
        self.bias = nn.Parameter(torch.zeros(ndim)) if bias else None

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        return F.layer_norm(input, self.weight.shape, self.weight, self.bias, 1e-5)


class PatchResidualConvBlock(nn.Module):
    """Local residual MLP implemented as two convolutions over patch grids."""

    def __init__(
        self,
        in_dim: int,
        out_dim: int,
        hidden_dim: int,
        kernel_size: int,
        stride: int,
        padding: int,
        dorpout: float = 0.1,
    ) -> None:
        super().__init__()
        self.nonlinearity = nn.SiLU()
        self.ln1 = LayerNorm(in_dim, bias=True)
        self.dropout = nn.Dropout(dorpout)
        self.conv1 = nn.Conv2d(in_dim, hidden_dim, kernel_size=kernel_size, stride=stride, padding=padding)
        self.conv2 = nn.Conv2d(hidden_dim, out_dim, kernel_size=kernel_size, stride=stride, padding=padding)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        b, c, h, w = x.shape
        z = self.ln1(x.permute(0, 2, 3, 1).reshape(b * h * w, c))
        z = z.reshape(b, h, w, c).permute(0, 3, 1, 2).contiguous()
        z = self.nonlinearity(self.conv1(z))
        z = self.dropout(z)
        z = self.nonlinearity(self.conv2(z))
        return z + x


class Upsample(nn.Module):
    def __init__(self, in_channels: int, out_channels: int):
        super().__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.conv(F.interpolate(x, scale_factor=2.0, mode="nearest"))


class Downsample(nn.Module):
    def __init__(self, in_channels: int, out_channels: int):
        super().__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=0)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.conv(F.pad(x, (0, 1, 0, 1), mode="constant", value=0))


class WaveletTransform(nn.Module):
    """Patchwise Haar transform used by released PLPQ/HLQ checkpoints."""

    def __init__(self, patch_size: int, inverse: bool = False):
        super().__init__()
        self.patch_size = int(patch_size)
        self.inverse = bool(inverse)
        self.haar = torch.tensor([0.7071067811865476, 0.7071067811865476])
        self.arange = torch.arange(len(self.haar))
        self.steps = int(math.log2(self.patch_size))

    def num_transformed_channels(self, in_channels: int = 3) -> int:
        return int(in_channels) * (4 ** self.steps)

    def forward(self, x: torch.Tensor, patchwise: bool = True, reshape: bool = False) -> torch.Tensor:
        if self.inverse:
            return self.invert(x, patchwise=patchwise, from_reshaped=reshape)
        return self.transform(x, patchwise=patchwise, reshape=reshape)

    def transform(self, x: torch.Tensor, patchwise: bool = True, reshape: bool = False) -> torch.Tensor:
        patch = self.patch_size
        if patchwise:
            b, c, h, w = x.shape
            init_b = b
            x = x.reshape(b, c, h // patch, patch, w // patch, patch).moveaxis(4, 3)
            x = x.moveaxis(1, 3).reshape(-1, c, patch, patch)

        for _ in range(self.steps):
            x = self.dwt(x)

        if patchwise:
            x = x.reshape(init_b, h // patch, w // patch, -1).moveaxis(3, 1)
        if reshape:
            b, cp2, hdp, wdp = x.shape
            c, h, w = cp2 // (patch**2), hdp * patch, wdp * patch
            x = x.reshape(b, patch, patch, c, hdp, wdp)
            x = x.moveaxis(3, 1).moveaxis(3, 4).reshape(b, c, h, w).contiguous()
        return x

    def invert(self, x: torch.Tensor, patchwise: bool = True, from_reshaped: bool = False) -> torch.Tensor:
        patch = self.patch_size
        if from_reshaped:
            b, c, h, w = x.shape
            cp2, hdp, wdp = c * patch**2, h // patch, w // patch
            x = x.reshape(b, c, patch, hdp, patch, wdp)
            x = x.moveaxis(4, 3).moveaxis(1, 3).reshape(b, cp2, hdp, wdp)
        if patchwise:
            init_b, lh, lw = x.shape[0], x.shape[2], x.shape[3]
            x = x.moveaxis(1, 3).reshape(-1, x.shape[1], 1, 1)

        for _ in range(self.steps):
            x = self.idwt(x)

        if patchwise:
            x = x.reshape(init_b, lh, lw, *x.shape[1:]).moveaxis(3, 1)
            x = x.moveaxis(3, 4).reshape(*x.shape[:2], lh * patch, lw * patch)
        return x

    def dwt(self, x: torch.Tensor) -> torch.Tensor:
        dtype = x.dtype
        h = self.haar
        n = h.shape[0]
        groups = x.shape[1]
        hl = h.flip(0).reshape(1, 1, -1).repeat(groups, 1, 1)
        hh = (h * ((-1) ** self.arange)).reshape(1, 1, -1).repeat(groups, 1, 1)
        hl = hl.to(device=x.device, dtype=dtype)
        hh = hh.to(device=x.device, dtype=dtype)

        x = F.pad(x, pad=(n - 2, n - 1, n - 2, n - 1), mode="reflect").to(dtype)
        xl = F.conv2d(x, hl.unsqueeze(2), groups=groups, stride=(1, 2))
        xh = F.conv2d(x, hh.unsqueeze(2), groups=groups, stride=(1, 2))
        xll = F.conv2d(xl, hl.unsqueeze(3), groups=groups, stride=(2, 1))
        xlh = F.conv2d(xl, hh.unsqueeze(3), groups=groups, stride=(2, 1))
        xhl = F.conv2d(xh, hl.unsqueeze(3), groups=groups, stride=(2, 1))
        xhh = F.conv2d(xh, hh.unsqueeze(3), groups=groups, stride=(2, 1))
        return 0.5 * torch.cat([xll, xlh, xhl, xhh], dim=1)

    def idwt(self, x: torch.Tensor) -> torch.Tensor:
        dtype = x.dtype
        h = self.haar
        n = h.shape[0]
        groups = x.shape[1] // 4
        hl = h.flip([0]).reshape(1, 1, -1).repeat([groups, 1, 1])
        hh = (h * ((-1) ** self.arange)).reshape(1, 1, -1).repeat(groups, 1, 1)
        hl = hl.to(device=x.device, dtype=dtype)
        hh = hh.to(device=x.device, dtype=dtype)

        xll, xlh, xhl, xhh = torch.chunk(x.to(dtype), 4, dim=1)
        yl = F.conv_transpose2d(xll, hl.unsqueeze(3), groups=groups, stride=(2, 1), padding=(n - 2, 0))
        yl += F.conv_transpose2d(xlh, hh.unsqueeze(3), groups=groups, stride=(2, 1), padding=(n - 2, 0))
        yh = F.conv_transpose2d(xhl, hl.unsqueeze(3), groups=groups, stride=(2, 1), padding=(n - 2, 0))
        yh += F.conv_transpose2d(xhh, hh.unsqueeze(3), groups=groups, stride=(2, 1), padding=(n - 2, 0))
        y = F.conv_transpose2d(yl, hl.unsqueeze(2), groups=groups, stride=(1, 2), padding=(0, n - 2))
        y += F.conv_transpose2d(yh, hh.unsqueeze(2), groups=groups, stride=(1, 2), padding=(0, n - 2))
        return 2.0 * y


class PFSQ(nn.Module):
    """Pyramidal finite scalar quantizer used inside PLPQ."""

    def __init__(
        self,
        levels: List[int],
        dim: int | None = None,
        num_codebooks: int = 1,
        keep_num_codebooks_dim: bool | None = None,
        scale: float | None = None,
        allowed_dtypes: Tuple[torch.dtype, ...] = (torch.float32, torch.float64),
        channel_first: bool = False,
        projection_has_bias: bool = True,
        return_indices: bool = True,
        force_quantization_f32: bool = True,
    ):
        super().__init__()
        self.register_buffer("_levels", torch.tensor(levels, dtype=int32), persistent=False)
        self.register_buffer("_basis", torch.cumprod(torch.tensor([1] + levels[:-1]), dim=0, dtype=int32), persistent=False)
        self.scale = scale
        self.codebook_dim = len(levels)
        self.num_codebooks = int(num_codebooks)
        self.effective_codebook_dim = self.codebook_dim * self.num_codebooks
        self.keep_num_codebooks_dim = _default(keep_num_codebooks_dim, self.num_codebooks > 1)
        if self.num_codebooks > 1 and not self.keep_num_codebooks_dim:
            raise ValueError("PFSQ with multiple codebooks must keep the codebook dimension.")
        self.dim = _default(dim, self.effective_codebook_dim)
        self.channel_first = bool(channel_first)
        has_projections = self.dim != self.effective_codebook_dim
        self.project_in = nn.Linear(self.dim, self.effective_codebook_dim, bias=projection_has_bias) if has_projections else nn.Identity()
        self.project_out = nn.Linear(self.effective_codebook_dim, self.dim, bias=projection_has_bias) if has_projections else nn.Identity()
        self.has_projections = has_projections
        self.return_indices = bool(return_indices)
        if self.return_indices:
            self.codebook_size = self._levels.prod().item()
            self.register_buffer(
                "implicit_codebook",
                self._indices_to_codes(torch.arange(self.codebook_size)),
                persistent=False,
            )
        self.allowed_dtypes = allowed_dtypes
        self.force_quantization_f32 = bool(force_quantization_f32)

    def bound(self, z: torch.Tensor, eps: float = 1e-3) -> torch.Tensor:
        half_l = (self._levels - 1) * (1 + eps) / 2
        offset = torch.where(self._levels % 2 == 0, 0.5, 0.0)
        shift = (offset / half_l).atanh()
        return (z + shift).tanh() * half_l - offset

    def quantize(self, z: torch.Tensor) -> torch.Tensor:
        half_width = self._levels // 2
        return _round_ste(self.bound(z)) / half_width

    def _scale_and_shift(self, zhat_normalized: torch.Tensor) -> torch.Tensor:
        half_width = self._levels // 2
        return (zhat_normalized * half_width) + half_width

    def _scale_and_shift_inverse(self, zhat: torch.Tensor) -> torch.Tensor:
        half_width = self._levels // 2
        return (zhat - half_width) / half_width

    def indices_to_level_indices(self, indices: torch.Tensor) -> torch.Tensor:
        indices = rearrange(indices, "... -> ... 1")
        return (indices // self._basis) % self._levels

    def _indices_to_codes(self, indices: torch.Tensor) -> torch.Tensor:
        return self._scale_and_shift_inverse(self.indices_to_level_indices(indices))

    def codes_to_indices(self, zhat: torch.Tensor) -> torch.Tensor:
        if zhat.shape[-1] != self.codebook_dim:
            raise ValueError(f"Expected last dim {self.codebook_dim}, got {zhat.shape[-1]}.")
        return (self._scale_and_shift(zhat) * self._basis).sum(dim=-1).to(int32)

    def indices_to_codes(self, indices: torch.Tensor, return_first: bool = False) -> torch.Tensor:
        if not _exists(indices):
            raise ValueError("indices must not be None.")
        n_codes = indices.shape[-1]
        is_img_or_video = indices.ndim >= (3 + int(self.keep_num_codebooks_dim))
        codes = self._indices_to_codes(indices)
        if self.keep_num_codebooks_dim:
            codes = rearrange(codes, "... c d -> ... (c d)")
        if n_codes == 1:
            return codes
        codes = self.project_out(codes)
        if is_img_or_video or self.channel_first:
            codes = rearrange(codes, "b ... d -> b d ...")
        return codes

    @_autocast_disabled()
    def forward(self, z: torch.Tensor):
        is_img_or_video = z.ndim >= 4
        need_move_channel_last = is_img_or_video or self.channel_first
        if need_move_channel_last:
            z = rearrange(z, "b d ... -> b ... d")
            z, packed_shape = _pack_one(z, "b * d")

        if z.shape[-1] != self.dim:
            raise ValueError(f"Expected dimension {self.dim}, found {z.shape[-1]}.")

        z = self.project_in(z)
        z = rearrange(z, "b n (c d) -> b n c d", c=self.num_codebooks)
        quantization_context = _autocast_disabled if self.force_quantization_f32 else nullcontext
        with quantization_context():
            orig_dtype = z.dtype
            if self.force_quantization_f32 and orig_dtype not in self.allowed_dtypes:
                z = z.float()
            codes = self.quantize(z)
            indices = self.codes_to_indices(codes) if self.return_indices else None
            first_codes = codes[:, :, 0, :].type(orig_dtype)
            codes = rearrange(codes, "b n c d -> b n (c d)").type(orig_dtype)

        out = self.project_out(codes)
        if need_move_channel_last:
            out = _unpack_one(out, packed_shape, "b * d")
            out = rearrange(out, "b ... d -> b d ...")
            indices = _maybe(_unpack_one)(indices, packed_shape, "b * c")
        if not self.keep_num_codebooks_dim and self.return_indices:
            indices = _maybe(rearrange)(indices, "... 1 -> ...")
        return out, first_codes, indices


class PLPQ(nn.Module):
    """Pyramidal Local Patch Quantizer inference wrapper."""

    def __init__(self, config: SimpleNamespace):
        super().__init__()
        self.config = config

        if getattr(config, "use_wavelets", False):
            wavelets = WaveletTransform(patch_size=config.patch_size)
            wavelet_channels = wavelets.num_transformed_channels(config.num_in_channels)
            in_proj = nn.Sequential(
                wavelets,
                nn.Conv2d(wavelet_channels, config.encoder_blocks[0][1], kernel_size=1, stride=1),
            )
            out_proj = nn.Sequential(
                nn.Conv2d(config.decoder_blocks[-1][2], wavelet_channels, kernel_size=3, stride=1, padding=1),
                WaveletTransform(patch_size=config.patch_size, inverse=True),
            )
        else:
            in_proj = nn.Conv2d(
                config.num_in_channels,
                config.encoder_blocks[0][1],
                kernel_size=config.patch_size,
                stride=config.patch_size,
            )
            out_proj = nn.Conv2d(config.decoder_blocks[-1][2], config.num_out_channels, kernel_size=3, stride=1, padding=1)

        self.encoder = nn.Sequential(
            in_proj,
            nn.SiLU(),
            *[
                PatchResidualConvBlock(*params[1:]) if params[0] == "ResBlock" else Downsample(*params[1:])
                for params in config.encoder_blocks
            ],
        )
        self.quantizer = PFSQ(
            levels=config.levels,
            num_codebooks=config.num_quantizers,
            dim=config.encoder_blocks[-1][2],
        )
        self.coarse_decoder = nn.Conv2d(len(config.levels), config.num_out_channels, kernel_size=1, stride=1)
        self.decoder = nn.Sequential(
            *[
                PatchResidualConvBlock(*params[1:]) if params[0] == "ResBlock" else Upsample(*params[1:])
                for params in config.decoder_blocks
            ],
            out_proj,
        )

    @torch.no_grad()
    def quantize(self, x: torch.Tensor, flatten: bool = True) -> torch.Tensor:
        z = self.encoder(x).permute(0, 2, 3, 1).contiguous()
        b, h, w, _c = z.shape
        z = z.view(b, h * w, -1)
        _quantized, _coarse_quantized, all_codes = self.quantizer(z)
        if not flatten:
            all_codes = all_codes.view(b, h, w, -1)
        return all_codes

    @torch.no_grad()
    def decode(self, indices: torch.Tensor, shape: Tuple[int, int] | None = None) -> torch.Tensor:
        n_codes = indices.shape[-1]
        emb = self.quantizer.indices_to_codes(indices).squeeze(-1)
        if len(emb.shape) == 4:
            emb = emb.permute(0, 1, 2, 3).contiguous()
        else:
            if shape is not None:
                b = emb.size(0)
                h = shape[0] // self.config.patch_size
                w = shape[1] // self.config.patch_size
            else:
                b = emb.size(0)
                h = w = int(math.sqrt(emb.size(1)))
            emb = emb.permute(0, 2, 1).reshape(b, -1, h, w).contiguous()
        if n_codes == 1:
            return self.coarse_decoder(emb)
        return self.decoder(emb)

    @torch.no_grad()
    def decode_coarse_tokens(self, indices: torch.Tensor) -> torch.Tensor:
        emb = self.quantizer.indices_to_codes(indices).squeeze(-1)
        emb = emb.transpose(1, 2).unsqueeze(-1).contiguous()
        return self.coarse_decoder(emb)


def quantizer_config_from_dict(config_dict: Dict[str, Any]) -> SimpleNamespace:
    """Return a config namespace compatible with released PLPQ checkpoints."""
    return SimpleNamespace(**dict(config_dict))


def quantizer_from_checkpoint_dict(ckpt: Dict[str, Any]) -> PLPQ:
    """Instantiate a PLPQ quantizer from a loaded checkpoint dictionary."""
    cfg = quantizer_config_from_dict(ckpt["cfg"])
    return PLPQ(cfg)