Bug: LatentCompositeMasked fails with 16-channel latents due to hardcoded [:3] channel slice in composite()
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
## Description
`LatentCompositeMasked` crashes with a `RuntimeError` when used with any 16-channel latent model (FLUX, Z-Image-Turbo, SD3, etc.). The shared `composite()` function in `comfy_extras/nodes_mask.py` was refactored to handle both image and latent compositing, but contains a hardcoded `[:3]` channel slice that extracts only 3 channels from the source tensor. When this is added to a 16-channel destination slice, PyTorch raises a dimension mismatch error.
## Error
```
RuntimeError: The size of tensor a (3) must match the size of tensor b (16) at non-singleton dimension 1
```
Stack trace points to `nodes_mask.py` line ~56:
```
destination[..., top:bottom, left:right] = source_portion + destination_portion
```
## Root Cause
In `composite()`, line ~43:
```python
source_rgb = source[:, :3, :visible_height, :visible_width]
```
This slices only 3 channels from the source, which is correct for image compositing (RGB/RGBA), but breaks for latents with more than 3 channels. The resulting `source_portion` has shape `[B, 3, H, W]` while `destination_portion` has shape `[B, 16, H, W]`, causing the crash.
## Fix
Change line ~43 from:
```python
source_rgb = source[:, :3, :visible_height, :visible_width]
```
to:
```python
source_rgb = source[:, :, :visible_height, :visible_width]
```
This allows the function to work correctly for latents of any channel count while remaining backward compatible with image compositing (since `ImageCompositeMasked` already normalises channel count before calling `composite()` via `node_helpers.image_alpha_fix`).
## Steps to Reproduce
1. Use any 16-channel model (e.g. Z-Image-Turbo, FLUX, SD3)
2. Add a `LatentCompositeMasked` node with both destination and source connected to latents from that model
3. Queue — crashes immediately after sampling completes
## Environment
- ComfyUI v0.21.0
- PyTorch 2.9.1+cu130
- Affects all 16-channel models (FLUX VAE / Z-Image / SD3)
- Introduced when `composite()` was refactored to be shared between `LatentCompositeMasked` and `ImageCompositeMasked`
Contributor guide
Assessment
This issue has not been assessed yet.