Skip to content
16 changes: 16 additions & 0 deletions docs/source/en/optimization/neuron.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,19 @@ Launch one process per NeuronCore. Set `--nproc_per_node` to the number of Neuro
```bash
torchrun --nproc_per_node=8 tensor_parallel_neuron.py
```


## Serving with vLLM-Omni
Comment on lines +118 to +120

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Serving with vLLM-Omni
## Serve pipelines with vLLM-Omni


[vllm-omni-neuron](https://github.com/aws-neuron/vllm-omni-neuron) serves Diffusers pipelines on Neuron through
vLLM-Omni. Diffusers runs the pipeline, and the plugin handles device placement and compilation.

Neuron compiles static shapes, so declare the resolutions to serve up front. The server compiles them at startup and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Neuron compiles static shapes, so declare the resolutions to serve up front. The server compiles them at startup and
Neuron [compiles static shapes](#compiled-mode), so declare the resolutions to serve up front. The server compiles them at startup and

rejects any other resolution. The number of inference steps isn't restricted.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rejects any other resolution. The number of inference steps isn't restricted.
rejects any other resolution. If you don't set it, the server compiles only 256x256. The number of inference steps isn't restricted.


```bash
export VLLM_OMNI_NEURON_DIFFUSERS_RESOLUTIONS=256x256,512x512
vllm serve <model-id> --omni --diffusion-load-format diffusers --dtype bfloat16

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to use a real value here :)

Suggested change
vllm serve <model-id> --omni --diffusion-load-format diffusers --dtype bfloat16
vllm serve black-forest-labs/FLUX.2-klein-4B --omni --diffusion-load-format diffusers --dtype bfloat16

```

See the vllm-omni-neuron repository for installation and supported pipelines.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may also be nice to add what a sample request looks like so users know its good

Suggested change
See the vllm-omni-neuron repository for installation and supported pipelines.
The server is ready once lal declared resolutions have compiled. For installation steps and supported pipelines, see the [vllm-omni-neuron](https://github.com/aws-neuron/vllm-omni-neuron) repository.

11 changes: 10 additions & 1 deletion src/diffusers/pipelines/flux2/pipeline_flux2_klein.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ def _unpack_latents_with_ids(

return torch.stack(x_list, dim=0)

@staticmethod
def _prepare_timestep(timestep: torch.Tensor, latents: torch.Tensor) -> torch.Tensor:
"""Broadcast the timestep to the batch and cast it to the latents dtype.

Kept as a tensor-only method so backends can compile it as its own region (e.g. libtorch-neuronx-lite, where
running this cast eagerly breaks the denoising loop).
"""
return timestep.expand(latents.shape[0]).to(latents.dtype)

def encode_prompt(
self,
prompt: str | list[str],
Expand Down Expand Up @@ -838,7 +847,7 @@ def __call__(

self._current_timestep = t
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
timestep = t.expand(latents.shape[0]).to(latents.dtype)
timestep = self._prepare_timestep(t, latents)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't do one-liner methods like this and prefer inlining them as much as possible for better readability.


latent_model_input = latents.to(self.transformer.dtype)
latent_image_ids = latent_ids
Expand Down
12 changes: 11 additions & 1 deletion src/diffusers/pipelines/flux2/pipeline_flux2_klein_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ def _get_raw_image_size(image: PipelineImageInput) -> tuple[int, int]:

raise ValueError(f"Unsupported image type: {type(image)}")

@staticmethod
# Copied from diffusers.pipelines.flux2.pipeline_flux2_klein.Flux2KleinPipeline._prepare_timestep
def _prepare_timestep(timestep: torch.Tensor, latents: torch.Tensor) -> torch.Tensor:
"""Broadcast the timestep to the batch and cast it to the latents dtype.

Kept as a tensor-only method so backends can compile it as its own region (e.g. libtorch-neuronx-lite, where
running this cast eagerly breaks the denoising loop).
"""
return timestep.expand(latents.shape[0]).to(latents.dtype)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.


# Copied from diffusers.pipelines.flux2.pipeline_flux2_klein.Flux2KleinPipeline.encode_prompt
def encode_prompt(
self,
Expand Down Expand Up @@ -1173,7 +1183,7 @@ def __call__(

self._current_timestep = t
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
timestep = t.expand(latents.shape[0]).to(latents.dtype)
timestep = self._prepare_timestep(t, latents)

latent_model_input = torch.cat([latents, condition_image_latents], dim=1)
img_ids = combined_image_ids
Expand Down
12 changes: 11 additions & 1 deletion src/diffusers/pipelines/flux2/pipeline_flux2_klein_kv.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,16 @@ def _unpack_latents_with_ids(x: torch.Tensor, x_ids: torch.Tensor) -> list[torch

return torch.stack(x_list, dim=0)

@staticmethod
# Copied from diffusers.pipelines.flux2.pipeline_flux2_klein.Flux2KleinPipeline._prepare_timestep
def _prepare_timestep(timestep: torch.Tensor, latents: torch.Tensor) -> torch.Tensor:
"""Broadcast the timestep to the batch and cast it to the latents dtype.

Kept as a tensor-only method so backends can compile it as its own region (e.g. libtorch-neuronx-lite, where
running this cast eagerly breaks the denoising loop).
"""
return timestep.expand(latents.shape[0]).to(latents.dtype)

def _set_kv_attn_processors(self):
"""Replace default attention processors with KV-cache-aware variants."""
for block in self.transformer.transformer_blocks:
Expand Down Expand Up @@ -789,7 +799,7 @@ def __call__(
continue

self._current_timestep = t
timestep = t.expand(latents.shape[0]).to(latents.dtype)
timestep = self._prepare_timestep(t, latents)

if i == 0 and image_latents is not None:
# Step 0: include ref tokens, extract KV cache
Expand Down
6 changes: 2 additions & 4 deletions src/diffusers/quantizers/bfl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
from accelerate import init_empty_weights
from accelerate.hooks import add_hook_to_module, remove_hook_from_module

if is_flashinfer_available():
import flashinfer
Comment on lines -29 to -30

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an unrelated change to me.



FP8_MAX = 448.0
E2M1_MAX = 6.0
NVFP4_BLOCK = 16
Expand Down Expand Up @@ -217,6 +213,8 @@ def __init__(self, in_features, out_features, bias=True, compute_dtype=None, dev

def forward(self, inputs: torch.Tensor) -> torch.Tensor:
if inputs.is_cuda and is_flashinfer_available() and torch.cuda.get_device_capability(inputs.device)[0] >= 10:
import flashinfer

x = inputs.reshape(-1, self.in_features).to(torch.bfloat16).contiguous()
x_q, x_scale = flashinfer.nvfp4_quantize(
x, (1.0 / self.input_scale).reshape(1), sfLayout=flashinfer.SfLayout.layout_128x4, do_shuffle=False
Expand Down