|
| 1 | +# Copyright (c) 2021-2025, InterDigital Communications, Inc |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted (subject to the limitations in the disclaimer |
| 6 | +# below) provided that the following conditions are met: |
| 7 | + |
| 8 | +# * Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# * Neither the name of InterDigital Communications, Inc nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from this |
| 15 | +# software without specific prior written permission. |
| 16 | + |
| 17 | +# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
| 18 | +# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | +# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 20 | +# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 21 | +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 | +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 | +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 | +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +"""Pure functional helpers shared by checkerboard latent codecs. |
| 31 | +
|
| 32 | +These are extracted from :class:`CheckerboardLatentCodec` so that sibling |
| 33 | +codecs (e.g. :class:`MultiContextCheckerboardLatentCodec`) can reuse the |
| 34 | +exact same checkerboard split / merge / mask logic without duplicating it. |
| 35 | +A single source of truth here also means an anchor-parity boundary fix |
| 36 | +applies to every checkerboard codec at once. |
| 37 | +""" |
| 38 | + |
| 39 | +from __future__ import annotations |
| 40 | + |
| 41 | +import torch |
| 42 | + |
| 43 | +from torch import Tensor |
| 44 | + |
| 45 | +__all__ = [ |
| 46 | + "embed", |
| 47 | + "embed_step", |
| 48 | + "mask_all", |
| 49 | + "mask_all_but_step", |
| 50 | + "merge", |
| 51 | + "step_parity", |
| 52 | + "unembed", |
| 53 | + "write_step", |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +def step_parity(step: str, anchor_parity: str) -> str: |
| 58 | + """Resolve a ``step`` ('anchor' / 'non_anchor') to a parity string.""" |
| 59 | + if step == "anchor": |
| 60 | + return anchor_parity |
| 61 | + if step == "non_anchor": |
| 62 | + return "odd" if anchor_parity == "even" else "even" |
| 63 | + raise ValueError(f'Invalid "step" value "{step}"') |
| 64 | + |
| 65 | + |
| 66 | +def unembed(y: Tensor, *, anchor_parity: str) -> Tensor: |
| 67 | + """Separate single tensor into two even/odd checkerboard chunks. |
| 68 | +
|
| 69 | + .. code-block:: none |
| 70 | +
|
| 71 | + ■ □ ■ □ ■ ■ □ □ |
| 72 | + □ ■ □ ■ ---> ■ ■ □ □ |
| 73 | + ■ □ ■ □ ■ ■ □ □ |
| 74 | + """ |
| 75 | + n, c, h, w = y.shape |
| 76 | + y_packed = y.new_zeros((2, n, c, h, w // 2)) |
| 77 | + if anchor_parity == "even": |
| 78 | + y_packed[0, ..., 0::2, :] = y[..., 0::2, 0::2] |
| 79 | + y_packed[0, ..., 1::2, :] = y[..., 1::2, 1::2] |
| 80 | + y_packed[1, ..., 0::2, :] = y[..., 0::2, 1::2] |
| 81 | + y_packed[1, ..., 1::2, :] = y[..., 1::2, 0::2] |
| 82 | + else: |
| 83 | + y_packed[0, ..., 0::2, :] = y[..., 0::2, 1::2] |
| 84 | + y_packed[0, ..., 1::2, :] = y[..., 1::2, 0::2] |
| 85 | + y_packed[1, ..., 0::2, :] = y[..., 0::2, 0::2] |
| 86 | + y_packed[1, ..., 1::2, :] = y[..., 1::2, 1::2] |
| 87 | + return y_packed |
| 88 | + |
| 89 | + |
| 90 | +def embed(y_packed: Tensor, *, anchor_parity: str) -> Tensor: |
| 91 | + """Combine two even/odd checkerboard chunks into single tensor. |
| 92 | +
|
| 93 | + .. code-block:: none |
| 94 | +
|
| 95 | + ■ ■ □ □ ■ □ ■ □ |
| 96 | + ■ ■ □ □ ---> □ ■ □ ■ |
| 97 | + ■ ■ □ □ ■ □ ■ □ |
| 98 | + """ |
| 99 | + num_chunks, n, c, h, w_half = y_packed.shape |
| 100 | + assert num_chunks == 2 |
| 101 | + y = y_packed.new_zeros((n, c, h, w_half * 2)) |
| 102 | + if anchor_parity == "even": |
| 103 | + y[..., 0::2, 0::2] = y_packed[0, ..., 0::2, :] |
| 104 | + y[..., 1::2, 1::2] = y_packed[0, ..., 1::2, :] |
| 105 | + y[..., 0::2, 1::2] = y_packed[1, ..., 0::2, :] |
| 106 | + y[..., 1::2, 0::2] = y_packed[1, ..., 1::2, :] |
| 107 | + else: |
| 108 | + y[..., 0::2, 1::2] = y_packed[0, ..., 0::2, :] |
| 109 | + y[..., 1::2, 0::2] = y_packed[0, ..., 1::2, :] |
| 110 | + y[..., 0::2, 0::2] = y_packed[1, ..., 0::2, :] |
| 111 | + y[..., 1::2, 1::2] = y_packed[1, ..., 1::2, :] |
| 112 | + return y |
| 113 | + |
| 114 | + |
| 115 | +def embed_step( |
| 116 | + step_index: int, y_i: Tensor, width: int, *, anchor_parity: str |
| 117 | +) -> Tensor: |
| 118 | + """Embed a per-step half-width tensor back into a full-grid tensor.""" |
| 119 | + n, c, h, _ = y_i.shape |
| 120 | + y_packed = y_i.new_zeros((2, n, c, h, width // 2)) |
| 121 | + y_packed[step_index] = y_i |
| 122 | + return embed(y_packed, anchor_parity=anchor_parity) |
| 123 | + |
| 124 | + |
| 125 | +def write_step(dest: Tensor, src: Tensor, step: str, *, anchor_parity: str) -> None: |
| 126 | + """Copy ``src`` pixels at the current step's positions into ``dest`` in-place.""" |
| 127 | + parity = step_parity(step, anchor_parity) |
| 128 | + if parity == "even": |
| 129 | + dest[..., 0::2, 0::2] = src[..., 0::2, 0::2] |
| 130 | + dest[..., 1::2, 1::2] = src[..., 1::2, 1::2] |
| 131 | + else: |
| 132 | + dest[..., 0::2, 1::2] = src[..., 0::2, 1::2] |
| 133 | + dest[..., 1::2, 0::2] = src[..., 1::2, 0::2] |
| 134 | + |
| 135 | + |
| 136 | +def mask_all_but_step(y: Tensor, step: str, *, anchor_parity: str) -> Tensor: |
| 137 | + """Keep only pixels in the current step, and zero out the rest.""" |
| 138 | + y = y.clone() |
| 139 | + parity = step_parity(step, anchor_parity) |
| 140 | + if parity == "even": |
| 141 | + y[..., 0::2, 1::2] = 0 |
| 142 | + y[..., 1::2, 0::2] = 0 |
| 143 | + else: |
| 144 | + y[..., 0::2, 0::2] = 0 |
| 145 | + y[..., 1::2, 1::2] = 0 |
| 146 | + return y |
| 147 | + |
| 148 | + |
| 149 | +def mask_all(y: Tensor) -> Tensor: |
| 150 | + """Return a zero tensor with the same shape, dtype and device as ``y``.""" |
| 151 | + return torch.zeros_like(y) |
| 152 | + |
| 153 | + |
| 154 | +def merge(*args: Tensor) -> Tensor: |
| 155 | + """Concatenate tensors along the channel dimension.""" |
| 156 | + return torch.cat(args, dim=1) |
0 commit comments