Buckets:
| import"../chunks/DsnmJJEf.js";import{i as I,h as Q,C as q,H as d,a as j,D as o,E as J,s as Z}from"../chunks/BLbO27j4.js";import{p as z,o as E,s as e,f as F,a as T,b as O,c as n,d as b,n as i,r as t}from"../chunks/jDjavuwI.js";const L='{"title":"QwenImage21Transformer2DModel","local":"qwenimage21transformer2dmodel","sections":[{"title":"QwenImage21Transformer2DModel","local":"diffusers.QwenImage21Transformer2DModel","sections":[],"depth":2},{"title":"Transformer2DModelOutput","local":"diffusers.models.modeling_outputs.Transformer2DModelOutput","sections":[],"depth":2}],"depth":1}';var C=b('<meta name="hf:doc:metadata"/>'),N=b(`<p></p> <!> <!> <p>The single-stream transformer used by Qwen-Image 2.1. Text and image latents share one sequence, and a single shared <code>modulation</code> projection feeds every block.</p> <p>Two behaviours distinguish 2.1 from earlier QwenImage transformers:</p> <ul><li><strong>Block-causal attention</strong> — attention follows <code>(q_idx >= kv_idx) or same_image_block</code>, so the joint sequence is | |
| causal while each image block stays internally bidirectional. <code>QwenImage21AttnProcessor</code> implements it as one | |
| attention call per prefix segment and is the default. <code>QwenImage21FlexAttnProcessor</code> implements it as a single <code>flex_attention</code> call driven by a <code>BlockMask</code>, which is faster once the model is compiled. Both produce the same | |
| results.</li> <li><code>causal_condition</code> — text and condition-image tokens are modulated from <code>t = 0</code> rather than the sampled timestep. | |
| Their activations are independent of the denoising step, so the keys and values of that prefix are cacheable | |
| across steps via the <code>kv_cache</code> argument.</li></ul> <p>Load it with:</p> <!> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>The single-stream Transformer used by Qwen-Image 2.1.</p> <p>Text and image latents share one sequence: condition-image tokens are substituted into the text stream at the | |
| positions the vision-language encoder reserved for them, and the target image’s tokens are appended. A single | |
| shared <code>modulation</code> projection feeds every block, so blocks hold no modulation parameters of their own.</p> <p>Two behaviours distinguish 2.1:</p> <ul><li><strong>Block-causal attention</strong> — attention follows <code>(q_idx >= kv_idx) or same_image_block</code>, so the sequence is causal | |
| while each image block stays internally bidirectional. The <code>flex</code> attention backend gives efficient single-pass | |
| attention; without it the model uses an exact multi-pass SDPA prefill that processes each block separately.</li> <li><code>causal_condition</code> — text and condition-image tokens are modulated from <code>t = 0</code> instead of the sampled timestep, | |
| which also makes their activations timestep-independent and so cacheable across denoising steps.</li></ul> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>Label every token of the joint sequence with the image block it belongs to.</p> <p>Block boundaries come from the token counts in <code>img_shapes</code>, not from runs of <code>True</code> in <code>image_pad_mask</code>: two | |
| condition images that happen to sit next to each other with no text between them form one run but must stay | |
| separate blocks, otherwise they would attend to each other bidirectionally.</p></div> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!></div></div> <!> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><!> <p>The output of <a href="/docs/diffusers/pr_14921/en/api/models/transformer2d#diffusers.Transformer2DModel">Transformer2DModel</a>.</p></div> <!> <p></p>`,1);function U(v,x){z(x,!1),E(()=>{new URLSearchParams(window.location.search).get("fw")}),I();var c=N();Q("1li55sc",_=>{var w=C();Z(w,"content",L),T(_,w)});var m=e(F(c),2);q(m,{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"});var l=e(m,2);d(l,{title:"QwenImage21Transformer2DModel",local:"qwenimage21transformer2dmodel",headingTag:"h1"});var f=e(l,10);j(f,{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwUXdlbkltYWdlMjFUcmFuc2Zvcm1lcjJETW9kZWwlMEElMEF0cmFuc2Zvcm1lciUyMCUzRCUyMFF3ZW5JbWFnZTIxVHJhbnNmb3JtZXIyRE1vZGVsLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJRd2VuJTJGUXdlbi1JbWFnZS0yLjElMjIlMkMlMjBzdWJmb2xkZXIlM0QlMjJ0cmFuc2Zvcm1lciUyMiUyQyUyMGR0eXBlJTNEdG9yY2guYmZsb2F0MTYlMEEp",highlighted:`<span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> QwenImage21Transformer2DModel | |
| transformer = QwenImage21Transformer2DModel.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image-2.1"</span>, subfolder=<span class="hljs-string">"transformer"</span>, dtype=torch.bfloat16 | |
| )`,lang:"python",wrap:!1});var u=e(f,2);d(u,{title:"QwenImage21Transformer2DModel",local:"diffusers.QwenImage21Transformer2DModel",headingTag:"h2"});var a=e(u,2),h=n(a);o(h,{name:"class diffusers.QwenImage21Transformer2DModel",anchor:"diffusers.QwenImage21Transformer2DModel",source:"https://github.com/huggingface/diffusers/blob/vr_14921/src/diffusers/models/transformers/transformer_qwenimage21.py#L713",parameters:[{name:"patch_size",val:": int = 1"},{name:"in_channels",val:": int = 64"},{name:"out_channels",val:": int | None = 64"},{name:"num_layers",val:": int = 32"},{name:"attention_head_dim",val:": int = 128"},{name:"num_attention_heads",val:": int = 32"},{name:"context_in_dim",val:": int = 4096"},{name:"mlp_ratio",val:": int = 3"},{name:"axes_dims_rope",val:": tuple = (16, 56, 56)"},{name:"eps",val:": float = 1e-06"},{name:"causal_condition",val:": bool = True"}],parametersDescription:[{anchor:"diffusers.QwenImage21Transformer2DModel.patch_size",description:`<strong>patch_size</strong> (<code>int</code>, defaults to <code>1</code>) — | |
| Side length of the latent patch folded into the channel dim. 2.1 consumes latents unpatched.`,name:"patch_size"},{anchor:"diffusers.QwenImage21Transformer2DModel.in_channels",description:`<strong>in_channels</strong> (<code>int</code>, defaults to <code>64</code>) — | |
| Latent channels of the input.`,name:"in_channels"},{anchor:"diffusers.QwenImage21Transformer2DModel.out_channels",description:`<strong>out_channels</strong> (<code>int</code>, <em>optional</em>, defaults to <code>64</code>) — | |
| Latent channels of the output. Falls back to <code>in_channels</code>.`,name:"out_channels"},{anchor:"diffusers.QwenImage21Transformer2DModel.num_layers",description:`<strong>num_layers</strong> (<code>int</code>, defaults to <code>32</code>) — | |
| Number of single-stream blocks.`,name:"num_layers"},{anchor:"diffusers.QwenImage21Transformer2DModel.attention_head_dim",description:`<strong>attention_head_dim</strong> (<code>int</code>, defaults to <code>128</code>) — | |
| Channels per attention head.`,name:"attention_head_dim"},{anchor:"diffusers.QwenImage21Transformer2DModel.num_attention_heads",description:`<strong>num_attention_heads</strong> (<code>int</code>, defaults to <code>32</code>) — | |
| Number of attention heads.`,name:"num_attention_heads"},{anchor:"diffusers.QwenImage21Transformer2DModel.context_in_dim",description:`<strong>context_in_dim</strong> (<code>int</code>, defaults to <code>4096</code>) — | |
| Channel dim of <code>encoder_hidden_states</code>.`,name:"context_in_dim"},{anchor:"diffusers.QwenImage21Transformer2DModel.mlp_ratio",description:`<strong>mlp_ratio</strong> (<code>int</code>, defaults to <code>3</code>) — | |
| Feed-forward expansion factor.`,name:"mlp_ratio"},{anchor:"diffusers.QwenImage21Transformer2DModel.axes_dims_rope",description:`<strong>axes_dims_rope</strong> (<code>tuple[int]</code>, defaults to <code>(16, 56, 56)</code>) — | |
| Rotary dims for the frame, height and width axes.`,name:"axes_dims_rope"},{anchor:"diffusers.QwenImage21Transformer2DModel.eps",description:`<strong>eps</strong> (<code>float</code>, defaults to <code>1e-6</code>) — | |
| Epsilon for the norm layers.`,name:"eps"},{anchor:"diffusers.QwenImage21Transformer2DModel.causal_condition",description:`<strong>causal_condition</strong> (<code>bool</code>, defaults to <code>True</code>) — | |
| Modulate text and condition-image tokens from <code>t = 0</code>. Required for KV caching.`,name:"causal_condition"}]});var s=e(h,10),k=n(s);o(k,{name:"build_token_metadata",anchor:"diffusers.QwenImage21Transformer2DModel.build_token_metadata",source:"https://github.com/huggingface/diffusers/blob/vr_14921/src/diffusers/models/transformers/transformer_qwenimage21.py#L807",parameters:[{name:"image_pad_mask",val:": Tensor"},{name:"img_shapes",val:": list"}],parametersDescription:[{anchor:"diffusers.QwenImage21Transformer2DModel.build_token_metadata.image_pad_mask",description:"<strong>image_pad_mask</strong> (<code>torch.Tensor</code>) — <code>(seq_len,)</code> bool, <code>True</code> at image-token positions.",name:"image_pad_mask"},{anchor:"diffusers.QwenImage21Transformer2DModel.build_token_metadata.img_shapes",description:`<strong>img_shapes</strong> (<code>list[tuple[int, int, int]]</code>) — Per-image <code>(frame, height, width)</code> in latent tokens, condition | |
| images first and the target image last.`,name:"img_shapes"}],returnDescription:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p><code>image_ids</code> <code>(seq_len,)</code> with <code>-1</code> at text positions and a unique id | |
| per image block, and <code>target_token_mask</code> <code>(seq_len,)</code> marking the target image’s tokens.</p> | |
| `,returnType:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p><code>tuple[torch.Tensor, torch.Tensor]</code></p> | |
| `}),i(4),t(s);var p=e(s,2),M=n(p);o(M,{name:"forward",anchor:"diffusers.QwenImage21Transformer2DModel.forward",source:"https://github.com/huggingface/diffusers/blob/vr_14921/src/diffusers/models/transformers/transformer_qwenimage21.py#L846",parameters:[{name:"hidden_states",val:": Tensor"},{name:"encoder_hidden_states",val:": Tensor"},{name:"timestep",val:": Tensor"},{name:"img_shapes",val:": list"},{name:"img_mask",val:": Tensor"},{name:"encoder_hidden_states_mask",val:": typing.Optional[torch.Tensor] = None"},{name:"attention_kwargs",val:": dict[str, typing.Any] | None = None"},{name:"kv_cache",val:": diffusers.models.transformers.transformer_qwenimage21.QwenImage21KVCache | None = None"},{name:"kv_cache_mode",val:": str | None = None"},{name:"return_dict",val:": bool = True"}],parametersDescription:[{anchor:"diffusers.QwenImage21Transformer2DModel.forward.hidden_states",description:`<strong>hidden_states</strong> (<code>torch.Tensor</code> of shape <code>(batch_size, image_sequence_length, in_channels)</code>) — | |
| Packed latents, condition images first and the target image last.`,name:"hidden_states"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.encoder_hidden_states",description:`<strong>encoder_hidden_states</strong> (<code>torch.Tensor</code> of shape <code>(batch_size, text_sequence_length, context_in_dim)</code>) — | |
| Text embeddings from the vision-language encoder.`,name:"encoder_hidden_states"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.timestep",description:`<strong>timestep</strong> (<code>torch.Tensor</code>) — | |
| Current denoising step, scaled to <code>[0, 1]</code>.`,name:"timestep"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.img_shapes",description:`<strong>img_shapes</strong> (<code>list[list[tuple[int, int, int]]]</code>) — | |
| Per-sample list of <code>(frame, height, width)</code> in latent tokens, condition images first and the target | |
| image last. All samples must share a layout.`,name:"img_shapes"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.img_mask",description:`<strong>img_mask</strong> (<code>torch.Tensor</code> of shape <code>(batch_size, vlm_sequence_length)</code>) — | |
| <code>True</code> at the vision-language encoder’s image slots, each standing for a <code>2x2</code> group of latent tokens.`,name:"img_mask"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.encoder_hidden_states_mask",description:`<strong>encoder_hidden_states_mask</strong> (<code>torch.Tensor</code>, <em>optional</em>) — | |
| <code>(batch_size, text_sequence_length)</code> bool marking valid text tokens. Padded positions are excluded from | |
| attention.`,name:"encoder_hidden_states_mask"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.kv_cache",description:`<strong>kv_cache</strong> (<code>QwenImage21KVCache</code>, <em>optional</em>) — | |
| Cache container. Pass together with <code>kv_cache_mode</code> to enable prefix KV caching.`,name:"kv_cache"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.kv_cache_mode",description:`<strong>kv_cache_mode</strong> (<code>str</code>, <em>optional</em>) — | |
| <code>"extract"</code> to prefill the cache (first denoising step), <code>"cached"</code> to decode from it (later steps). | |
| Requires <code>causal_condition=True</code>.`,name:"kv_cache_mode"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.attention_kwargs",description:`<strong>attention_kwargs</strong> (<code>dict</code>, <em>optional</em>) — | |
| Forwarded to the attention processors, and carries <code>scale</code> for the LoRA layers.`,name:"attention_kwargs"},{anchor:"diffusers.QwenImage21Transformer2DModel.forward.return_dict",description:`<strong>return_dict</strong> (<code>bool</code>, <em>optional</em>, defaults to <code>True</code>) — | |
| Whether or not to return a <a href="/docs/diffusers/pr_14921/en/api/models/hunyuan_video15_transformer_3d#diffusers.models.modeling_outputs.Transformer2DModelOutput">Transformer2DModelOutput</a> instead of a plain | |
| tuple.`,name:"return_dict"}],returnDescription:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p><a | |
| href="/docs/diffusers/pr_14921/en/api/models/hunyuan_video15_transformer_3d#diffusers.models.modeling_outputs.Transformer2DModelOutput" | |
| >Transformer2DModelOutput</a> if <code>return_dict</code> is True, otherwise a plain | |
| <code>tuple</code> whose first element is the denoised latents.</p> | |
| `,returnType:`<script context="module">export const metadata = 'undefined';<\/script> | |
| <p><a | |
| href="/docs/diffusers/pr_14921/en/api/models/hunyuan_video15_transformer_3d#diffusers.models.modeling_outputs.Transformer2DModelOutput" | |
| >Transformer2DModelOutput</a> or <code>tuple</code></p> | |
| `}),t(p),t(a);var g=e(a,2);d(g,{title:"Transformer2DModelOutput",local:"diffusers.models.modeling_outputs.Transformer2DModelOutput",headingTag:"h2"});var r=e(g,2),D=n(r);o(D,{name:"class diffusers.models.modeling_outputs.Transformer2DModelOutput",anchor:"diffusers.models.modeling_outputs.Transformer2DModelOutput",source:"https://github.com/huggingface/diffusers/blob/vr_14921/src/diffusers/models/modeling_outputs.py#L21",parameters:[{name:"sample",val:": torch.Tensor"}],parametersDescription:[{anchor:"diffusers.models.modeling_outputs.Transformer2DModelOutput.sample",description:`<strong>sample</strong> (<code>torch.Tensor</code> of shape <code>(batch_size, num_channels, height, width)</code> or <code>(batch size, num_vector_embeds - 1, num_latent_pixels)</code> if <a href="/docs/diffusers/pr_14921/en/api/models/transformer2d#diffusers.Transformer2DModel">Transformer2DModel</a> is discrete) — | |
| The hidden states output conditioned on the <code>encoder_hidden_states</code> input. If discrete, returns probability | |
| distributions for the unnoised latent pixels.`,name:"sample"}]}),i(2),t(r);var y=e(r,2);J(y,{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/api/models/qwenimage21_transformer2d.md"}),i(2),T(v,c),O()}export{U as component}; | |
Xet Storage Details
- Size:
- 16 kB
- Xet hash:
- 14cffd37b8d3f56ff3be6ff579e601869b9c61d132c1cbb3f29a4f4326da654b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.