Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions particlegan/_minimal/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ def _minimal_critic_hook(module: nn.Module, args: Tuple[Any, ...], kwargs: Dict[
if rec is not None and not rec._in_penalty and module.training and torch.is_grad_enabled() and target.requires_grad and no_ig and (x_in.ndim >= 3 or x_in[0].numel() > 8):
skey = getattr(module, "_t2_pending_source_key", None) or _tensor_key(x_in)
if skey is not None:
unsc = target.float() if target.dtype == torch.bfloat16 else target
entry: List[Any] = [int(rec.observed_steps), skey, _cond_signature(args, kwargs), x_in.detach(), unsc, True]
unsc.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False)); rec._live_captures.append(entry); del rec._live_captures[:-6]
alive = [True]; rec._live_captures.append([int(rec.observed_steps), skey, _cond_signature(args, kwargs), x_in.detach(), target.float() if target.dtype == torch.bfloat16 else target, alive])
target.register_hook(lambda _g, _a=alive: _a.clear()); [e.__setitem__(slice(3, 5), [None, None]) or e[5].clear() for e in rec._live_captures[:-6]]; del rec._live_captures[:-6]
if rec is not None and not rec._in_penalty and torch.is_grad_enabled() and rec._thermal_scale < 1.0 and isinstance(output, torch.Tensor) and output.requires_grad:
scaled = output * rec._thermal_scale + output.detach() * (1.0 - rec._thermal_scale)
if x_in is not None: scaled._minimal_input, scaled._minimal_record, scaled._minimal_conditioned, scaled._minimal_cond_tensors = x_in, rec, is_cond, cond_t
Expand Down Expand Up @@ -119,7 +118,8 @@ def observe_cond_fiber(self, r_cond: float) -> None:
self.cond_ema = max(0.18, rc) if not self.cond_initialized else (0.99 * self.cond_ema + 0.01 * rc)
self.cond_initialized, self._thermal_scale, self._step_scale = True, 1.0, float(1.0 - 0.85 * _smoothstep01((0.050 - max(0.0, self.cond_ema)) / 0.024))

def record_step(self, optimizer: Any) -> None: super().record_step(optimizer); self._live_captures.clear(); self._g_probe_cache = self._last_real_pair = None
def clear_transient_captures(self) -> None: [e.__setitem__(slice(3, 5), [None, None]) or e[5].clear() for e in self._live_captures]; self._live_captures.clear()
def record_step(self, optimizer: Any) -> None: super().record_step(optimizer); self.clear_transient_captures(); self._g_probe_cache = self._last_real_pair = None

def state_dict(self) -> Dict[str, Any]: return {**super().state_dict(), "minimal_state": {"schema": 1, **{k: bool(getattr(self, k)) for k in _BOOL_KEYS}, **{k: float(getattr(self, "_" + k if k in ("thermal_scale", "step_scale") else k)) for k in _BOUNDS}, "cond_bank": deepcopy(self.cond_bank)}}

Expand All @@ -131,7 +131,7 @@ def load_state_dict(self, state: Dict[str, Any]) -> None:
cb = _validate_cond_bank(pkt["cond_bank"]); super().load_state_dict(sc)
for k in _BOOL_KEYS: setattr(self, k, pkt[k])
for k, v in vals.items(): setattr(self, "_" + k if k in ("thermal_scale", "step_scale") else k, v)
self.cond_bank, self._g_probe_cache = cb, None; self._live_captures.clear()
self.cond_bank, self._g_probe_cache = cb, None; self.clear_transient_captures()

def _collect_conv_layouts(modules: Iterable[Optional[nn.Module]]) -> Dict[int, Tuple[bool, int]]: return {id(c.weight): (isinstance(c, nn.ConvTranspose2d), int(c.groups)) for r in modules if isinstance(r, nn.Module) for c in r.modules() if isinstance(c, (nn.Conv2d, nn.ConvTranspose2d))}

Expand Down
26 changes: 19 additions & 7 deletions particlegan/_nash_v3/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import torch
from torch import nn

from .._t2_critic.critic import _cond_signature, _tensor_identity_key
from .._t2_critic.critic import (
_clear_live_capture_entries,
_cond_signature,
_tensor_identity_key,
_trim_live_captures,
)
from ..grad_regularizers import CriticStepRecord

FROZEN_THERMAL_SCALE = 1e-4
Expand Down Expand Up @@ -163,11 +168,11 @@ def _nash_v3_critic_forward_hook(
rec._live_captures = captures
c_sig = _cond_signature(args, kwargs)
step_tag = int(getattr(rec, "observed_steps", 0))
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, True]
unscaled.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False))
alive = [True]
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, alive]
target.register_hook(lambda _g, _a=alive: _a.clear())
captures.append(entry)
if len(captures) > _MAX_LIVE_CAPTURES:
del captures[: len(captures) - _MAX_LIVE_CAPTURES]
_trim_live_captures(captures, _MAX_LIVE_CAPTURES)

# Low-D secant probe capture for critic inversion-deficit trust band (skipped on images for speed).
if (
Expand Down Expand Up @@ -363,7 +368,8 @@ def _init_nash_v3_fields(self) -> None:
self._in_probe: bool = False

def clear_transient_captures(self) -> None:
self._live_captures = []
if hasattr(self, "_live_captures") and self._live_captures:
_clear_live_capture_entries(self._live_captures)
self._last_probe_actual_input = None

def observe_transport(self, res: float, data_scale: float, ema_decay: float = 0.92) -> None:
Expand Down Expand Up @@ -465,8 +471,8 @@ def _restore_nash_v3_packet(self, packet: Dict[str, Any]) -> None:
self._last_d_logits = None
self._d_probe_list = None
self._g_probe_cache = None
self.clear_transient_captures()
self._live_captures = []
self._last_probe_actual_input = None

def restore_device_state(self, device: torch.device) -> None:
if isinstance(self.proto_sum, torch.Tensor) and self.proto_sum.device != device:
Expand All @@ -482,6 +488,12 @@ def __init__(self) -> None:
super().__init__()
self._init_nash_v3_fields()

def record_step(self, optimizer_or_lr: Any) -> None:
super().record_step(optimizer_or_lr)
self._last_d_logits = None
self._d_probe_list = None
self.clear_transient_captures()

def state_dict(self) -> Dict[str, Any]:
return {**super().state_dict(), "t4_nash_v3": self._nash_v3_packet()}

Expand Down
29 changes: 24 additions & 5 deletions particlegan/_t2_critic/critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@

_MAX_LIVE_CAPTURES = 6


def _clear_live_capture_entries(entries: List[Any]) -> None:
"""Release tensor references and backward-alive flags from live-capture entries."""
for entry in entries:
if isinstance(entry, list) and len(entry) >= 5:
entry[3] = None
entry[4] = None
if len(entry) >= 6 and isinstance(entry[5], list):
entry[5].clear()
entries.clear()


def _trim_live_captures(captures: List[Any], max_entries: int = _MAX_LIVE_CAPTURES) -> None:
"""Evict oldest live-capture entries while explicitly dropping their tensor references."""
if len(captures) > max_entries:
evict_n = len(captures) - max_entries
_clear_live_capture_entries(captures[:evict_n])
del captures[:evict_n]

_T2_STATE_KEYS = frozenset({
"schema",
"initialized",
Expand Down Expand Up @@ -146,11 +165,11 @@ def _t2_critic_forward_hook(
rec._live_captures = captures
c_sig = _cond_signature(args, kwargs)
step_tag = int(getattr(rec, "observed_steps", 0))
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, True]
unscaled.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False))
alive = [True]
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, alive]
raw_target.register_hook(lambda _g, _a=alive: _a.clear())
captures.append(entry)
if len(captures) > _MAX_LIVE_CAPTURES:
del captures[: len(captures) - _MAX_LIVE_CAPTURES]
_trim_live_captures(captures)

if module.training and rec is not None and not getattr(rec, "_in_penalty", False) and torch.is_grad_enabled():
s = float(getattr(rec, "_thermal_scale", 1.0))
Expand Down Expand Up @@ -257,7 +276,7 @@ def _init_t2_fields(self) -> None:

def clear_transient_captures(self) -> None:
if hasattr(self, "_live_captures") and self._live_captures:
self._live_captures.clear()
_clear_live_capture_entries(self._live_captures)
self._last_probe_actual_input = None

def __deepcopy__(self, memo: Dict[int, Any]) -> Any:
Expand Down
14 changes: 8 additions & 6 deletions particlegan/_unified/critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
_validate_t1c_packet,
)
from .._t2_critic.critic import (
_MAX_LIVE_CAPTURES,
_clear_live_capture_entries,
_cond_signature,
_tensor_identity_key,
_trim_live_captures,
)
from ..grad_regularizers import CriticStepRecord
from ..k3p import K3PCriticAdam
Expand Down Expand Up @@ -83,7 +84,7 @@ def rgdt_cond_ema(self, val: float) -> None:

def clear_transient_captures(self) -> None:
if hasattr(self, "_live_captures") and self._live_captures:
self._live_captures.clear()
_clear_live_capture_entries(self._live_captures)
self._last_probe_actual_input = None

def _unified_packet(self) -> Dict[str, Any]:
Expand All @@ -98,6 +99,7 @@ def _restore_unified_packet(self, packet: Dict[str, Any]) -> None:
self._restore_t1c_packet(t1c_sub)
self._step_scale = float(packet["step_scale"])
self._t1c_disc_gate = 0.0
self.clear_transient_captures()
self._live_captures = []
self._last_probe_actual_input = None

Expand Down Expand Up @@ -241,11 +243,11 @@ def _unified_critic_forward_hook(
rec._live_captures = captures
c_sig = _cond_signature(args, kwargs)
step_tag = int(getattr(rec, "observed_steps", 0))
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, True]
unscaled.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False))
alive = [True]
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, alive]
raw_target.register_hook(lambda _g, _a=alive: _a.clear())
captures.append(entry)
if len(captures) > _MAX_LIVE_CAPTURES:
del captures[: len(captures) - _MAX_LIVE_CAPTURES]
_trim_live_captures(captures)

# 2. Apply thermal cooling to outgoing critic logits (outside penalty probes):
modified = False
Expand Down
14 changes: 8 additions & 6 deletions particlegan/_unified_v11/critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
_validate_t1c_packet,
)
from .._t2_critic.critic import (
_MAX_LIVE_CAPTURES,
_clear_live_capture_entries,
_cond_signature,
_tensor_identity_key,
_trim_live_captures,
)
from ..grad_regularizers import CriticStepRecord
from ..k3p import K3PCriticAdam
Expand Down Expand Up @@ -158,7 +159,7 @@ def observe_margin_and_scale(

def clear_transient_captures(self) -> None:
if hasattr(self, "_live_captures") and self._live_captures:
self._live_captures.clear()
_clear_live_capture_entries(self._live_captures)
self._last_probe_actual_input = None

def clear_step_probes(self) -> None:
Expand Down Expand Up @@ -188,6 +189,7 @@ def _restore_unified_v11_packet(self, packet: Dict[str, Any]) -> None:
self.cond_fiber_bank.load_packet(packet["cond_fiber_bank"])
self._t1c_disc_gate = 0.0
self._simplicial_step = False
self.clear_transient_captures()
self._live_captures = []
self._d_probe_list = []
self._g_probe_cache = None
Expand Down Expand Up @@ -347,11 +349,11 @@ def _unified_v11_critic_forward_hook(
rec._live_captures = captures
c_sig = _cond_signature(args, kwargs)
step_tag = int(getattr(rec, "observed_steps", 0))
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, True]
unscaled.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False))
alive = [True]
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, alive]
raw_target.register_hook(lambda _g, _a=alive: _a.clear())
captures.append(entry)
if len(captures) > _MAX_LIVE_CAPTURES:
del captures[: len(captures) - _MAX_LIVE_CAPTURES]
_trim_live_captures(captures)

if (
getattr(rec, "_enable_secant_probe", True)
Expand Down
14 changes: 8 additions & 6 deletions particlegan/_unified_v4/critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
_validate_t1c_packet,
)
from .._t2_critic.critic import (
_MAX_LIVE_CAPTURES,
_clear_live_capture_entries,
_cond_signature,
_tensor_identity_key,
_trim_live_captures,
)
from ..grad_regularizers import CriticStepRecord
from ..k3p import K3PCriticAdam
Expand Down Expand Up @@ -308,7 +309,7 @@ def observe_cond_gen_stratum(

def clear_transient_captures(self) -> None:
if hasattr(self, "_live_captures") and self._live_captures:
self._live_captures.clear()
_clear_live_capture_entries(self._live_captures)
self._last_probe_actual_input = None

def clear_step_probes(self) -> None:
Expand Down Expand Up @@ -372,6 +373,7 @@ def _restore_unified_v4_packet(self, packet: Dict[str, Any]) -> None:
}
self._t1c_disc_gate = 0.0
self._simplicial_step = False
self.clear_transient_captures()
self._live_captures = []
self._d_probe_list = []
self._g_probe_cache = None
Expand Down Expand Up @@ -546,11 +548,11 @@ def _unified_v4_critic_forward_hook(
rec._live_captures = captures
c_sig = _cond_signature(args, kwargs)
step_tag = int(getattr(rec, "observed_steps", 0))
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, True]
unscaled.register_hook(lambda _g, _e=entry: _e.__setitem__(5, False))
alive = [True]
entry: List[Any] = [step_tag, src_key, c_sig, x_act, unscaled, alive]
raw_target.register_hook(lambda _g, _a=alive: _a.clear())
captures.append(entry)
if len(captures) > _MAX_LIVE_CAPTURES:
del captures[: len(captures) - _MAX_LIVE_CAPTURES]
_trim_live_captures(captures)

if (
getattr(rec, "_enable_secant_probe", True)
Expand Down
Loading
Loading