diff --git a/docs/source/en/optimization/neuron.md b/docs/source/en/optimization/neuron.md index fb0f9ac25077..012942966201 100644 --- a/docs/source/en/optimization/neuron.md +++ b/docs/source/en/optimization/neuron.md @@ -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 + +[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 +rejects any other resolution. The number of inference steps isn't restricted. + +```bash +export VLLM_OMNI_NEURON_DIFFUSERS_RESOLUTIONS=256x256,512x512 +vllm serve --omni --diffusion-load-format diffusers --dtype bfloat16 +``` + +See the vllm-omni-neuron repository for installation and supported pipelines. diff --git a/src/diffusers/pipelines/flux2/pipeline_flux2_klein.py b/src/diffusers/pipelines/flux2/pipeline_flux2_klein.py index 2a8c9075cc91..a2bdae0da0ab 100644 --- a/src/diffusers/pipelines/flux2/pipeline_flux2_klein.py +++ b/src/diffusers/pipelines/flux2/pipeline_flux2_klein.py @@ -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], @@ -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) latent_model_input = latents.to(self.transformer.dtype) latent_image_ids = latent_ids diff --git a/src/diffusers/pipelines/flux2/pipeline_flux2_klein_inpaint.py b/src/diffusers/pipelines/flux2/pipeline_flux2_klein_inpaint.py index d56dab1678d2..4d08613481f8 100644 --- a/src/diffusers/pipelines/flux2/pipeline_flux2_klein_inpaint.py +++ b/src/diffusers/pipelines/flux2/pipeline_flux2_klein_inpaint.py @@ -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) + # Copied from diffusers.pipelines.flux2.pipeline_flux2_klein.Flux2KleinPipeline.encode_prompt def encode_prompt( self, @@ -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 diff --git a/src/diffusers/pipelines/flux2/pipeline_flux2_klein_kv.py b/src/diffusers/pipelines/flux2/pipeline_flux2_klein_kv.py index 4671ad0ae3fd..90a1167665a2 100644 --- a/src/diffusers/pipelines/flux2/pipeline_flux2_klein_kv.py +++ b/src/diffusers/pipelines/flux2/pipeline_flux2_klein_kv.py @@ -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: @@ -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 diff --git a/src/diffusers/quantizers/bfl/utils.py b/src/diffusers/quantizers/bfl/utils.py index e75b79c4ee7d..2dd609832b98 100644 --- a/src/diffusers/quantizers/bfl/utils.py +++ b/src/diffusers/quantizers/bfl/utils.py @@ -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 - - FP8_MAX = 448.0 E2M1_MAX = 6.0 NVFP4_BLOCK = 16 @@ -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