From d77b3e2eed3a8b256ebb5390a1389db890ffd3eb Mon Sep 17 00:00:00 2001 From: digital-garbage <133010426+digital-garbage@users.noreply.github.com> Date: Sat, 22 Aug 2026 18:53:06 +0300 Subject: [PATCH] docs: record why clone_latent's nested-tensor aliasing is safe _refine_latent rejects nested latents at the door and every other caller returns the clone unmodified, so the alias is unreachable. Noted so the next audit does not re-open it. Co-Authored-By: Claude Opus 5 --- conditioning.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/conditioning.py b/conditioning.py index fb75206e..298c345c 100644 --- a/conditioning.py +++ b/conditioning.py @@ -538,6 +538,19 @@ def refinement_state_path(refinement_key, mode, prefix="refine", extension="json def clone_latent(latent): + """Shallow-clone a LATENT dict, deep-copying its tensor values. + + A NestedTensor (LTXAV / H3 packed audio+video) is NOT a torch.Tensor, so it is carried over + BY REFERENCE and the "clone" aliases the caller's samples. That is safe here, and an audit + should not re-open it: `latent_samples` returns None for a nested value, so + `latent_is_plain_video_tensor` is False and `_refine_latent` rejects the latent outright + (`_raise_wrong_latent`) before reaching this function. Every other caller returns the clone + unmodified — the two writes in `_refine_latent` REPLACE the "samples" key rather than + mutating it in place, so nothing can reach through the alias. + + The sampler's own `_clone_latent` unbinds and clones each stream, because it works on + latents that ARE nested. + """ if not isinstance(latent, dict): return None