> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/automodel/_mcp/server.

# nemo_automodel.components.models.qwen_image_edit.adapter

Flow-matching adapter for cached Qwen-Image-Edit-2511 conditioning.

## Module Contents

### Classes

| Name                                                                                                     | Description                                                            |
| -------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| [`QwenImageEditAdapter`](#nemo_automodel-components-models-qwen_image_edit-adapter-QwenImageEditAdapter) | Adapt cached image-edit batches to the upstream Diffusers transformer. |

### API

```python
class nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter()
```

**Bases:** [ModelAdapter](/nemo-automodel/nemo_automodel/components/flow_matching/adapters/base#nemo_automodel-components-flow_matching-adapters-base-ModelAdapter)

Adapt cached image-edit batches to the upstream Diffusers transformer.

Qwen-Image-Edit concatenates the packed noisy target tokens followed by
the packed context-image tokens. The transformer predicts every image
token, but flow matching supervises only the leading target-token span.

```python
nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter._pack_latents(
    latents: torch.Tensor
) -> torch.Tensor
```

staticmethod

Pack 2-by-2 latent patches in the upstream Diffusers layout.

**Parameters:**

Tensor of shape \[batch, channels, latent\_height,
latent\_width]. Both spatial dimensions must be even.

**Returns:** `torch.Tensor`

Tensor of shape \[batch, packed\_tokens, 4 \* channels], where

```python
nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter._unpack_latents(
    packed_latents: torch.Tensor,
    height: int,
    width: int
) -> torch.Tensor
```

staticmethod

Restore packed predictions to a channels-first latent grid.

**Parameters:**

Tensor of shape \[batch, packed\_tokens,
4 \* channels] in the upstream Diffusers 2-by-2 patch layout.

Output latent height. It must be even.

Output latent width. It must be even.

**Returns:** `torch.Tensor`

Tensor of shape \[batch, channels, height, width]. The returned

```python
nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter._validate_context_latents(
    context_latents: typing.Any,
    batch_size: int,
    channels: int
) -> list[torch.Tensor]
```

staticmethod

Validate the ordered cached context-latent sequence.

**Parameters:**

Ordered list of tensors, each with shape \[batch,
channels, context\_height, context\_width].

Required leading size for every context tensor.

Required latent channel count for every context tensor.

**Returns:** `list[torch.Tensor]`

The validated list of context tensors in its original order. The

```python
nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter.compute_loss(
    model_pred: torch.Tensor,
    target: torch.Tensor
) -> torch.Tensor
```

Compute unreduced flow-matching velocity MSE in float32.

**Parameters:**

Predicted target velocity tensor of shape \[batch,
channels, target\_height, target\_width].

Reference `noise - clean_latent` velocity tensor with the
same shape as `model_pred`.

**Returns:** `torch.Tensor`

Per-element float32 squared-error tensor with the same shape as

```python
nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter.forward(
    model: torch.nn.Module,
    inputs: dict[str, typing.Any]
) -> torch.Tensor
```

Run the upstream transformer and return only target predictions.

**Parameters:**

Upstream Diffusers Qwen image transformer. Its packed output
has shape \[batch, total\_image\_tokens, 4 \* channels].

Mapping returned by `prepare_inputs`. Tensor-bearing fields
have the layouts documented by that method.

**Returns:** `torch.Tensor`

Target velocity prediction tensor of shape \[batch, channels,

```python
nemo_automodel.components.models.qwen_image_edit.adapter.QwenImageEditAdapter.prepare_inputs(
    context: nemo_automodel.components.flow_matching.adapters.base.FlowMatchingContext
) -> dict[str, typing.Any]
```

Build Qwen edit conditioning from a cached flow-matching batch.

**Parameters:**

Flow context whose `noisy_latents` tensor has shape
\[batch, channels, target\_height, target\_width]. Its batch must
contain `context_latents` as an ordered list of tensors with
shape \[batch, channels, context\_height, context\_width],
`text_embeddings` with shape \[batch, text\_tokens, hidden],
and `text_attention_mask` with shape \[batch, text\_tokens].

**Returns:** `dict[str, Any]`

Mapping containing `hidden_states` with shape \[batch, total\_image\_tokens,