# Copyright 2026 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0. See the LICENSE file in this repository. import torch from diffusers.modular_pipelines import ModularPipelineBlocks, PipelineState from diffusers.modular_pipelines.modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from diffusers.utils.torch_utils import get_module_execution_device from .components import transformer_spec class JeverationDecideStep(ModularPipelineBlocks): model_name = "jeveration" @property def description(self) -> str: return ( "Step that decides the image in one forward pass: a shared random state is drawn, the backbone reads the " "request and 128 decision tokens built from that state, and every code is sampled at once." ) @property def expected_components(self) -> list[ComponentSpec]: return [transformer_spec()] @property def inputs(self) -> list[InputParam]: return [ InputParam("input_ids", required=True, type_hint=torch.Tensor, description="Request token ids `[B, L]`."), InputParam( "attention_mask", required=True, type_hint=torch.Tensor, description="Padding mask of the request ids." ), InputParam("position_ids", type_hint=torch.Tensor, description="Rotary positions of an image request."), InputParam("pixel_values", type_hint=torch.Tensor, description="The source images of an image request."), InputParam.template("generator"), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [OutputParam("codes", type_hint=torch.Tensor, description="Image codes `[B, 128]`, int64.")] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) transformer = components.transformer device = get_module_execution_device(transformer) generator = block_state.generator random_state, masked_share = transformer.sample_random_state(block_state.input_ids.shape[0], generator) logits = transformer( input_ids=block_state.input_ids.to(device), attention_mask=block_state.attention_mask.to(device), position_ids=None if block_state.position_ids is None else block_state.position_ids.to(device), pixel_values=None if block_state.pixel_values is None else block_state.pixel_values.to(device), random_state=random_state, masked_share=masked_share, ).logits probs = torch.softmax(logits.float(), -1) draw_device = generator.device if generator is not None else probs.device codes = torch.multinomial(probs.view(-1, probs.shape[-1]).to(draw_device), 1, generator=generator) block_state.codes = codes.view(logits.shape[:2]) self.set_block_state(state, block_state) return components, state