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
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
"""Separate distribution estimation from generator backpropagation (Sec. 3.3).

The distribution batch B supplies all cross-video interactions. Only the smaller
configured differentiation batch b needs feature gradients and rollout replay.
This module caches features and returns their cotangents (dL/d feature); the
trainer later replays the encoders, VAE, and generator to obtain dL/d parameters.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Mapping, Sequence

import torch
import torch.distributed as dist

from losses.multi_encoder_mmd import (
    MultiEncoderMMD,
)


def _detach(value: Any) -> Any:
    if isinstance(value, torch.Tensor):
        return value.detach()
    if isinstance(value, Mapping):
        return {key: _detach(item) for key, item in value.items()}
    if isinstance(value, tuple):
        return tuple(_detach(item) for item in value)
    if isinstance(value, list):
        return [_detach(item) for item in value]
    return value


@dataclass(frozen=True)
class FeatureGradientReplayResult:
    """Detached loss, per-microbatch feature gradients, and replay bookkeeping.

    ``local_cotangents`` are the derivatives of the global distribution loss.
    ``fsdp_cotangents`` additionally compensate for FSDP's gradient averaging.
    Field names stay compatible with existing training logs and checkpoints.
    """

    loss: torch.Tensor
    optimized_loss: torch.Tensor
    metadata: Mapping[str, Any]
    local_cotangents: tuple[Mapping[str, torch.Tensor], ...]
    fsdp_cotangents: tuple[Mapping[str, torch.Tensor], ...]
    replay_batch_indices: tuple[int, ...]
    replay_sample_ids: tuple[tuple[str, ...], ...]
    local_samples: int
    global_samples: int
    gradient_local_samples: int
    gradient_global_samples: int
    current_teacher_local_samples: int
    current_teacher_global_samples: int
    world_size: int


class FeatureGradientReplay:
    """Cache B generated feature sets and differentiate the selected b samples.

    B and b denote global sample counts; constructor counts are per rank. Each
    encoder keeps its own [tokens, channels] layout while sharing sample order.
    This implementation selects a per-rank suffix. All B samples still define
    the distribution loss, including interactions with unselected samples.
    """

    def __init__(
        self,
        loss_module: MultiEncoderMMD,
        *,
        target_local_samples: int,
        gradient_local_samples: int,
        loss_scale: float = 1.0,
        require_sample_id_alignment: bool = True,
        full_student_partial_gradient: bool = True,
    ) -> None:
        if not isinstance(loss_module, MultiEncoderMMD):
            raise TypeError(
                "loss_module must be MultiRepresentationVideoNystromMMD")
        if not 0 < int(gradient_local_samples) < int(target_local_samples):
            raise ValueError(
                "dual GradCache requires a strict positive gradient suffix")
        if not full_student_partial_gradient:
            raise ValueError(
                "dual GradCache requires full_student_partial_gradient=true")
        self.loss_module = loss_module
        self.target_local_samples = int(target_local_samples)
        self.gradient_local_samples = int(gradient_local_samples)
        self.loss_scale = float(loss_scale)
        self.require_sample_id_alignment = bool(require_sample_id_alignment)
        self.full_student_partial_gradient = True
        self.partial_gradient_enabled = True
        self.current_teacher_attraction_enabled = True
        self._feature_batches: list[dict[str, torch.Tensor]] = []
        self._teacher_batches: list[dict[str, torch.Tensor]] = []
        self._batch_sizes: list[int] = []
        self._gradient_batch_indices: list[int] = []
        self._gradient_batch_sizes: list[int] = []
        self._gradient_sample_ids: list[tuple[str, ...]] = []
        self._selection_started = False
        self._device: torch.device | None = None
        self._result: FeatureGradientReplayResult | None = None

    @staticmethod
    def _world_size() -> int:
        return dist.get_world_size() if dist.is_initialized() else 1

    @staticmethod
    def _rank() -> int:
        return dist.get_rank() if dist.is_initialized() else 0

    @property
    def local_samples(self) -> int:
        return sum(self._batch_sizes)

    @property
    def selected_local_samples(self) -> int:
        return sum(self._gradient_batch_sizes)

    @property
    def finalized(self) -> bool:
        return self._result is not None

    def _validate_mapping(
        self,
        value: Mapping[str, torch.Tensor],
        label: str,
        *,
        expected_batch: int | None = None,
    ) -> tuple[int, torch.device]:
        if not isinstance(value, Mapping):
            raise TypeError(f"{label} must be a branch tensor mapping")
        if set(value) != set(self.loss_module.branch_names):
            raise ValueError(
                f"{label} keys must be {self.loss_module.branch_names}")
        batch_size = None
        device = None
        for name in self.loss_module.branch_names:
            tensor = value[name]
            if (
                not isinstance(tensor, torch.Tensor)
                or tensor.ndim != 3
                or not tensor.is_floating_point()
                or min(tensor.shape) <= 0
            ):
                raise ValueError(f"{label}[{name!r}] must be floating [B,N,D]")
            if tuple(tensor.shape[1:]) != self.loss_module.sample_shapes[name]:
                raise ValueError(
                    f"{label}[{name!r}] geometry {tuple(tensor.shape[1:])} "
                    f"!= {self.loss_module.sample_shapes[name]}")
            if not bool(torch.isfinite(tensor).all().item()):
                raise FloatingPointError(f"{label}[{name!r}] is non-finite")
            if batch_size is None:
                batch_size = int(tensor.shape[0])
                device = tensor.device
            elif int(tensor.shape[0]) != batch_size or tensor.device != device:
                raise ValueError(
                    f"all {label} branches must share batch size and device")
        if expected_batch is not None and batch_size != expected_batch:
            raise ValueError(
                f"{label} batch size {batch_size} != {expected_batch}")
        return int(batch_size), device

    def add(
        self,
        generated_features: Mapping[str, torch.Tensor],
        current_teacher_features: Mapping[str, torch.Tensor] | None = None,
        *,
        gradient_selected: bool = True,
        sample_ids: Sequence[str] | None = None,
        current_teacher_sample_ids: Sequence[str] | None = None,
    ) -> int:
        """Store one rollout's features without retaining its generator graph."""
        if self.finalized:
            raise RuntimeError("cannot add after finalize")
        batch_size, device = self._validate_mapping(
            generated_features, "generated_features")
        if self._device is None:
            self._device = device
        elif device != self._device:
            raise ValueError("all microbatches must share a device")
        if self.local_samples + batch_size > self.target_local_samples:
            raise ValueError("dual GradCache exceeds target_local_samples")
        if gradient_selected:
            self._selection_started = True
            if self.selected_local_samples + batch_size > self.gradient_local_samples:
                raise ValueError("dual GradCache exceeds gradient suffix")
        elif self._selection_started:
            raise ValueError("gradient-selected microbatches must form a suffix")

        normalized_ids = None
        if sample_ids is not None:
            normalized_ids = tuple(str(value) for value in sample_ids)
            if len(normalized_ids) != batch_size or any(not value for value in normalized_ids):
                raise ValueError("sample_ids must contain one non-empty ID per row")
        if gradient_selected and self.require_sample_id_alignment:
            if normalized_ids is None:
                raise ValueError("selected dual samples require sample_ids")
            teacher_ids = (
                tuple(str(value) for value in current_teacher_sample_ids)
                if current_teacher_sample_ids is not None else None
            )
            if teacher_ids != normalized_ids:
                raise ValueError("student and teacher sample IDs/order differ")

        if gradient_selected:
            if current_teacher_features is None:
                raise ValueError("selected dual samples require teacher features")
            self._validate_mapping(
                current_teacher_features,
                "current_teacher_features",
                expected_batch=batch_size,
            )
        elif current_teacher_features is not None:
            raise ValueError("prefix microbatches must not carry teacher features")

        # Collection stage (Algorithm 1): keep only feature values and sample
        # order. Replay state is stored by the trainer, outside this cache.
        staged = {
            name: generated_features[name].detach().float().clone()
            for name in self.loss_module.branch_names
        }
        self._feature_batches.append(staged)
        index = len(self._feature_batches) - 1
        if gradient_selected:
            self._gradient_batch_indices.append(index)
            self._gradient_batch_sizes.append(batch_size)
            self._gradient_sample_ids.append(normalized_ids)
            self._teacher_batches.append({
                name: current_teacher_features[name].detach().float().clone()
                for name in self.loss_module.branch_names
            })
        self._batch_sizes.append(batch_size)
        return index

    def _gather(self, value: torch.Tensor) -> list[torch.Tensor]:
        if self._world_size() == 1:
            return [value.detach()]
        gathered = [torch.empty_like(value) for _ in range(self._world_size())]
        dist.all_gather(gathered, value.detach().contiguous())
        return gathered

    def _validate_ready(self) -> None:
        return None

    def finalize(self) -> FeatureGradientReplayResult:
        """Evaluate the global objective and cache dL/d feature for replay."""
        if self._result is not None:
            return self._result
        self._validate_ready()
        world_size = self._world_size()
        global_distribution_features = {}
        global_differentiation_features = {}
        global_reference_features = {}
        local_leaves = {}
        # Map each rank's selected suffix into the concatenated B-sample batch.
        # These indices preserve full-batch interactions while only b rows have
        # an autograd path. This function does not randomly resample the subset.
        suffix_start = self.target_local_samples - self.gradient_local_samples
        gradient_indices = torch.cat([
            torch.arange(
                rank * self.target_local_samples + suffix_start,
                (rank + 1) * self.target_local_samples,
                device=self._device,
                dtype=torch.long,
            )
            for rank in range(world_size)
        ])
        for name in self.loss_module.branch_names:
            local_distribution_features = torch.cat([
                batch[name] for batch in self._feature_batches
            ], dim=0)
            local_differentiation_features = torch.cat([
                self._feature_batches[index][name]
                for index in self._gradient_batch_indices
            ], dim=0)
            local_reference_features = torch.cat([
                batch[name] for batch in self._teacher_batches
            ], dim=0)
            distribution_parts = self._gather(local_distribution_features)
            differentiation_parts = self._gather(local_differentiation_features)
            reference_parts = self._gather(local_reference_features)
            # Only this rank's selected features are differentiable leaves.
            # Remote features contribute values; their owning rank gets their
            # derivatives. No generator or encoder graph is retained here.
            leaf = local_differentiation_features.detach().requires_grad_(True)
            differentiation_parts[self._rank()] = leaf
            local_leaves[name] = leaf
            global_distribution_features[name] = torch.cat(distribution_parts, dim=0).detach()
            global_differentiation_features[name] = torch.cat(differentiation_parts, dim=0)
            global_reference_features[name] = torch.cat(reference_parts, dim=0).detach()

        # Resolve all cross-video interactions once. The loss combines the
        # persistent Nystrom summary with current reference features (Eq. 9),
        # then aggregates the encoder objectives (Eqs. 11 and 13).
        raw_loss, metadata = self.loss_module(
            gradient_students=global_differentiation_features,
            student_reference=global_distribution_features,
            current_teacher=global_reference_features,
            gradient_student_indices=gradient_indices,
        )
        optimized = raw_loss * self.loss_scale
        gradients = torch.autograd.grad(
            optimized,
            tuple(local_leaves[name] for name in self.loss_module.branch_names),
            retain_graph=False,
            create_graph=False,
        )
        # Restore the collection order so each feature gradient is replayed
        # through the exact sample and encoder that produced its feature value.
        split_by_branch = {
            name: tuple(part.contiguous() for part in gradient.detach().split(
                self._gradient_batch_sizes, dim=0))
            for name, gradient in zip(self.loss_module.branch_names, gradients)
        }
        local_parts = tuple({
            name: split_by_branch[name][index]
            for name in self.loss_module.branch_names
        } for index in range(len(self._gradient_batch_sizes)))
        # FSDP averages parameter gradients across ranks, so cancel that factor
        # here. This world_size multiplier is separate from the B/b selective
        # differentiation correction (Eq. 12), applied later by the trainer.
        fsdp_parts = tuple({
            name: (value * float(world_size)).contiguous()
            for name, value in part.items()
        } for part in local_parts)
        global_samples = self.target_local_samples * world_size
        gradient_global = self.gradient_local_samples * world_size
        kss_denominator = global_samples * global_samples
        # Keep established metadata names for checkpoint/log compatibility.
        detached_metadata = dict(_detach(metadata))
        detached_metadata.update({
            "raw_loss": raw_loss.detach(),
            "optimized_loss": optimized.detach(),
            "loss_scale": raw_loss.new_tensor(self.loss_scale),
            "gradcache_local_samples": raw_loss.new_tensor(
                self.target_local_samples, dtype=torch.long),
            "gradcache_global_samples": raw_loss.new_tensor(
                global_samples, dtype=torch.long),
            "gradcache_forward_local_samples": raw_loss.new_tensor(
                self.target_local_samples, dtype=torch.long),
            "gradcache_forward_global_samples": raw_loss.new_tensor(
                global_samples, dtype=torch.long),
            "gradcache_gradient_local_samples": raw_loss.new_tensor(
                self.gradient_local_samples, dtype=torch.long),
            "gradcache_gradient_global_samples": raw_loss.new_tensor(
                gradient_global, dtype=torch.long),
            "gradcache_current_teacher_local_samples": raw_loss.new_tensor(
                self.gradient_local_samples, dtype=torch.long),
            "gradcache_current_teacher_global_samples": raw_loss.new_tensor(
                gradient_global, dtype=torch.long),
            "gradcache_kss_denominator": raw_loss.new_tensor(
                kss_denominator, dtype=torch.long),
            "gradcache_partial_gradient_enabled": raw_loss.new_ones(
                (), dtype=torch.long),
            "gradcache_full_student_partial_gradient": raw_loss.new_ones(
                (), dtype=torch.long),
            "gradcache_world_size": raw_loss.new_tensor(
                world_size, dtype=torch.long),
            "gradcache_fsdp_cotangent_scale": raw_loss.new_tensor(
                float(world_size)),
            "gradcache_branch_names": self.loss_module.branch_names,
        })
        self._result = FeatureGradientReplayResult(
            loss=raw_loss.detach(),
            optimized_loss=optimized.detach(),
            metadata=detached_metadata,
            local_cotangents=local_parts,
            fsdp_cotangents=fsdp_parts,
            replay_batch_indices=tuple(self._gradient_batch_indices),
            replay_sample_ids=tuple(self._gradient_sample_ids),
            local_samples=self.target_local_samples,
            global_samples=global_samples,
            gradient_local_samples=self.gradient_local_samples,
            gradient_global_samples=gradient_global,
            current_teacher_local_samples=self.gradient_local_samples,
            current_teacher_global_samples=gradient_global,
            world_size=world_size,
        )
        return self._result

    def _clear(self) -> None:
        self._feature_batches.clear()
        self._teacher_batches.clear()
        self._batch_sizes.clear()
        self._gradient_batch_indices.clear()
        self._gradient_batch_sizes.clear()
        self._gradient_sample_ids.clear()
        self._selection_started = False
        self._device = None
        self._result = None

    def commit(self) -> None:
        """Release cached values after the corresponding optimizer update."""
        if self._result is None:
            raise RuntimeError("cannot commit before finalize")
        self._clear()

    def discard(self) -> None:
        """Release an abandoned batch without marking an optimizer update."""
        self._clear()


# Preserve old imports and serialized class references during the naming cleanup.
MultiRepresentationFreshVideoGradCache = FeatureGradientReplay
MultiRepresentationFreshVideoGradCacheResult = FeatureGradientReplayResult

__all__ = [
    "FeatureGradientReplay",
    "FeatureGradientReplayResult",
    "MultiRepresentationFreshVideoGradCache",
    "MultiRepresentationFreshVideoGradCacheResult",
]