Skip to content

fix: clone latent before TAESD preview decode to prevent corruption#13383

Open
octo-patch wants to merge 1 commit intoComfy-Org:masterfrom
octo-patch:fix/issue-13366-taesd-preview-corrupts-latent
Open

fix: clone latent before TAESD preview decode to prevent corruption#13383
octo-patch wants to merge 1 commit intoComfy-Org:masterfrom
octo-patch:fix/issue-13366-taesd-preview-corrupts-latent

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #13366

Problem

The TAESD preview decoders (TAESDPreviewerImpl and TAEHVPreviewerImpl) were passing tensor views (slices of x0) directly to the decoder's decode() call. Since these are views (not copies), any in-place operation within the decoder's pipeline could potentially write back through the view and corrupt the original x0 latent mid-sampling.

This was specifically observed when:

  • Using a model with the Wan21 latent format (e.g., QwenImage)
  • lighttaew2_1.safetensors (WanVAE) is present in models/vae_approx/
  • TAESD preview method is enabled

In this configuration, the full VAE decode pipeline (including model management, conv operations, and CUDA kernel scheduling) processes the non-contiguous tensor slice, which can result in x0 being modified in-place.

Solution

Clone the tensor slice before passing it to the decoder:

# Before
x_sample = self.taesd.decode(x0[:1, :, :1])[0][0]

# After
x_sample = self.taesd.decode(x0[:1, :, :1].clone())[0][0]

This ensures the original x0 latent is never modified by any operations within the preview decode path, including any potential in-place writes in CUDA kernels or PyTorch operations that may operate directly on the underlying memory of non-contiguous views.

Testing

  • The fix is minimal and targeted: only the input to the preview decoder is cloned
  • No change to preview output quality or logic
  • The clone creates an independent contiguous copy, protecting x0 from any potential modification during the preview decode phase

…ixes Comfy-Org#13366)

The TAESD preview decoders (TAESDPreviewerImpl and TAEHVPreviewerImpl) were
passing tensor views (slices) of x0 directly to the decoder. This allowed the
decoder's internal operations to potentially modify the original latent in-place,
corrupting the midsampling latent.

The fix clones the sliced tensor before passing it to the decoder, ensuring the
original x0 is never affected by any in-place operations within the preview decode
path.

This specifically addresses the case where lighttaew2_1.safetensors (WanVAE) is
used as the TAESD preview decoder for models using the Wan21 latent format (e.g.,
QwenImage), where the full VAE decode pipeline could write back to the input slice.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 02a3f3bf-5825-4e44-99b6-c51484cda844

📥 Commits

Reviewing files that changed from the base of the PR and between c2657d5 and ce6b8b7.

📒 Files selected for processing (1)
  • latent_preview.py

📝 Walkthrough

Walkthrough

The pull request modifies the latent preview decoding implementations in latent_preview.py. Specifically, in both TAESDPreviewerImpl.decode_latent_to_preview and TAEHVPreviewerImpl.decode_latent_to_preview methods, latent tensor slices are now cloned before being passed to the decoder. This prevents the decoder from modifying the original latent tensor through in-place operations. The changes are internal implementation details with no alterations to public or exported entity declarations.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the primary change: cloning latent tensors before TAESD preview decode to prevent corruption.
Description check ✅ Passed The description is thorough and directly related to the changeset, explaining the problem, solution, and testing approach for the latent tensor corruption fix.
Linked Issues check ✅ Passed The PR successfully addresses issue #13366 by implementing the required fix: cloning tensor slices before passing to TAESD decoder to prevent in-place modifications of the original latent.
Out of Scope Changes check ✅ Passed All changes are in-scope and directly address the linked issue; only the specific latent cloning logic is modified in the two previewer implementations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TAESD preview corrupts midsampling latent if lighttaew2_1 is present

1 participant