File size: 9,223 Bytes
f3b08bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
"""Trout sampler: parallel confidence-ordered unmasking of the full 256-token canvas.

Per step:
  1. one batched forward pass (conditional + unconditional when CFG is on)
  2. guided logits  l = l_u + w * (l_c - l_u)
  3. sample a colour for every masked pixel
  4. keep the most confident ones so that the number still masked follows a cosine
     schedule; Gumbel noise anneals the choice (MaskGIT-style) and spatial
     non-maximum suppression stops two *uncertain* neighbouring pixels from being
     committed in the same step
  5. feed the full predicted distribution back as self-conditioning
Optional "polish" steps re-mask the least confident committed pixels and re-predict.
Inpainting: pass `init_tokens` and `keep_mask`; kept pixels are never changed.
"""

from __future__ import annotations

import math
from dataclasses import dataclass
from typing import Iterator, Optional

import torch
import torch.nn.functional as F


def filter_logits(logits: torch.Tensor, top_k: int = 0, top_p: float = 1.0, min_p: float = 0.0) -> torch.Tensor:
    if top_k and 0 < top_k < logits.shape[-1]:
        kth = torch.topk(logits, top_k, dim=-1).values[..., -1:]
        logits = logits.masked_fill(logits < kth, -math.inf)
    if min_p and min_p > 0:
        p = logits.softmax(-1)
        logits = logits.masked_fill(p < min_p * p.amax(-1, keepdim=True), -math.inf)
    if top_p is not None and top_p < 1.0:
        sorted_l, idx = torch.sort(logits, dim=-1, descending=True)
        sp = sorted_l.softmax(-1)
        remove = (sp.cumsum(-1) - sp) > top_p
        sorted_l = sorted_l.masked_fill(remove, -math.inf)
        logits = torch.full_like(logits, -math.inf).scatter(-1, idx, sorted_l)
    return logits


def cfg_weight(scale: float, schedule: str, frac: float) -> float:
    if schedule == "constant":
        return scale
    if schedule == "linear":
        return 1.0 + (scale - 1.0) * frac
    if schedule == "cosine":
        return 1.0 + (scale - 1.0) * (1 - math.cos(math.pi * frac)) / 2
    raise ValueError(f"unknown cfg schedule {schedule!r}")


def _gumbel(shape, device, generator):
    u = torch.rand(shape, device=device, generator=generator).clamp_(1e-10, 1 - 1e-10)
    return -torch.log(-torch.log(u))


def _nms_eligible(score: torch.Tensor, masked: torch.Tensor, certain: torch.Tensor, grid: int) -> torch.Tensor:
    B, N = score.shape
    cand = masked & ~certain
    s = torch.where(cand, score, torch.full_like(score, -math.inf)).view(B, 1, grid, grid)
    local_max = F.max_pool2d(s, 3, stride=1, padding=1).view(B, N)
    return certain | (cand & (score >= local_max))


@dataclass
class SampleStep:
    """State after one decoding step (tensors (B, N) on the sampling device)."""
    index: int
    total: int
    phase: str                  # "decode" or "polish"
    tokens: torch.Tensor        # current canvas; still-masked pixels hold the mask id
    preview: torch.Tensor       # the model's current best guess for every pixel
    confidence: torch.Tensor
    committed: torch.Tensor


@torch.no_grad()
def sample(model, text_states: torch.Tensor, text_pooled: torch.Tensor, steps: int = 16,
           return_history: bool = False, **kwargs):
    """Generate palette-token canvases. Returns tokens (B, N) [and the per-step history]."""
    x, history = None, []
    for st in sample_iter(model, text_states, text_pooled, steps=steps, **kwargs):
        x = st.tokens
        if return_history:
            history.append(x.clone())
    if x is None:
        x = _initial_canvas(model.cfg, text_pooled, kwargs.get("init_tokens"), kwargs.get("keep_mask"))
    return (x, history) if return_history else x


def _initial_canvas(cfg, text_pooled, init_tokens, keep_mask):
    B, N, M = text_pooled.shape[0], cfg.num_tokens, cfg.mask_id
    if init_tokens is not None and keep_mask is not None:
        init_tokens = init_tokens.to(text_pooled.device).long()
        return torch.where(keep_mask.to(text_pooled.device).bool(), init_tokens, torch.full_like(init_tokens, M))
    return torch.full((B, N), M, dtype=torch.long, device=text_pooled.device)


@torch.no_grad()
def sample_iter(model, text_states: torch.Tensor, text_pooled: torch.Tensor, steps: int = 16,
                cfg_scale: float = 3.0, cfg_schedule: str = "constant",
                temperature: float = 1.0, temperature_end: Optional[float] = None,
                top_k: int = 0, top_p: float = 1.0, min_p: float = 0.0,
                choice_temperature: float = 2.0, spatial_nms: bool = True, nms_threshold: float = 0.9,
                commit_threshold: float = 0.0, polish_steps: int = 0, polish_frac: float = 0.08,
                init_tokens: Optional[torch.Tensor] = None, keep_mask: Optional[torch.Tensor] = None,
                self_cond: bool = True, generator: Optional[torch.Generator] = None):
    """Same decoding as `sample`, but yields a `SampleStep` after every step (for live previews)."""
    cfg = model.cfg
    device = text_pooled.device
    B, N, K, M, G = text_pooled.shape[0], cfg.num_tokens, cfg.vocab_size, cfg.mask_id, cfg.grid_size

    x = _initial_canvas(cfg, text_pooled, init_tokens, keep_mask)
    masked = x == M
    total = steps + polish_steps
    n_start = masked.sum(1)
    conf_map = torch.where(masked, torch.zeros(B, N, device=device), torch.full((B, N), math.inf, device=device))

    use_cfg = cfg_scale is not None and abs(cfg_scale - 1.0) > 1e-6
    if use_cfg:
        ts2 = torch.cat([text_states, text_states])
        tp2 = torch.cat([text_pooled, text_pooled])
        drop2 = torch.cat([torch.zeros(B, dtype=torch.bool, device=device),
                           torch.ones(B, dtype=torch.bool, device=device)])
    sc = torch.zeros(B, N, K, device=device) if (cfg.self_cond and self_cond) else None

    def step_state(index, phase, probs_full, committed):
        best_p, best = probs_full.max(-1)
        conf = torch.where(masked, best_p, conf_map)
        return SampleStep(index, total, phase, x, best, conf, committed)

    def predict(x_in, sc_in, frac):
        ratio = (x_in == M).float().mean(1)
        if use_cfg:
            sc2 = torch.cat([sc_in, sc_in]) if sc_in is not None else None
            out = model(torch.cat([x_in, x_in]), ts2, tp2, torch.cat([ratio, ratio]), sc2, drop2).float()
            lc, lu = out.chunk(2)
            w = cfg_weight(cfg_scale, cfg_schedule, frac)
            return lu + w * (lc - lu)
        return model(x_in, text_states, text_pooled, ratio, sc_in, None).float()

    for k in range(steps):
        if not masked.any():
            break
        frac = (k + 1) / steps
        logits = predict(x, sc, frac)
        probs_full = logits.softmax(-1)

        T = temperature if temperature_end is None else temperature + (temperature_end - temperature) * (k / max(1, steps - 1))
        if T <= 0:
            sampled = logits.argmax(-1)
            p_sel = probs_full.gather(-1, sampled.unsqueeze(-1)).squeeze(-1)
        else:
            probs = filter_logits(logits / T, top_k, top_p, min_p).softmax(-1)
            sampled = torch.multinomial(probs.view(-1, K), 1, generator=generator).view(B, N)
            p_sel = probs.gather(-1, sampled.unsqueeze(-1)).squeeze(-1)

        n_masked = masked.sum(1)
        remaining = torch.floor(n_start.float() * math.cos(math.pi / 2 * frac)).long()
        if k == steps - 1:
            remaining = torch.zeros_like(remaining)
        n_commit = torch.minimum((n_masked - remaining).clamp(min=1), n_masked)
        if commit_threshold > 0:
            n_commit = torch.maximum(n_commit, ((p_sel >= commit_threshold) & masked).sum(1))

        score = torch.log(p_sel.clamp(min=1e-12))
        if choice_temperature > 0:
            score = score + choice_temperature * (1.0 - frac) * _gumbel(score.shape, device, generator)
        priority = score
        if spatial_nms:
            elig = _nms_eligible(score, masked, p_sel >= nms_threshold, G)
            priority = score + elig.float() * 1e4
        priority = torch.where(masked, priority, torch.full_like(priority, -math.inf))
        ranks = priority.argsort(1, descending=True).argsort(1)
        commit = (ranks < n_commit.unsqueeze(1)) & masked

        x = torch.where(commit, sampled, x)
        conf_map = torch.where(commit, p_sel, conf_map)
        masked = masked & ~commit
        if sc is not None:
            sc = probs_full
        yield step_state(k, "decode", probs_full, commit)

    # ---- optional polish: re-mask the least confident generated pixels and re-predict ----
    n_re = max(1, int(round(polish_frac * N)))
    for j in range(polish_steps):
        low = torch.topk(-conf_map, n_re, dim=1).indices
        remask = torch.zeros(B, N, dtype=torch.bool, device=device).scatter_(1, low, True)
        remask &= torch.isfinite(conf_map)
        x_in = torch.where(remask, torch.full_like(x, M), x)
        logits = predict(x_in, sc, 1.0)
        probs_full = logits.softmax(-1)
        new = logits.argmax(-1)
        x = torch.where(remask, new, x)
        conf_map = torch.where(remask, probs_full.gather(-1, new.unsqueeze(-1)).squeeze(-1), conf_map)
        if sc is not None:
            sc = probs_full
        yield step_state(steps + j, "polish", probs_full, remask)