o
    ۷i#                     @   sz   d dl mZmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ G dd	 d	eZeG d
d dZdS )    )	dataclassfield)AnyN)Image)RequestOutput)ModelRunnerOutput)OmniPromptTypec                   @   s@   e Zd ZU dZdZeeejf dB e	d< dZ
ee dB e	d< dS )OmniModelRunnerOutputaM  Model runner output for omni models.

    Extends the base ModelRunnerOutput with support for multimodal outputs
    that may be produced by non-autoregressive stages.

    Attributes:
        multimodal_outputs: Optional dictionary mapping modality names to
            output tensors (e.g., {"image": tensor, "audio": tensor})
    Nmultimodal_outputskv_extracted_req_ids)__name__
__module____qualname____doc__r
   dictstrtorchTensor__annotations__r   list r   r   G/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm_omni/outputs.pyr	      s   
 
r	   c                   @   s<  e Zd ZU dZdZeed< dZeed< dZ	e
dB ed< dZeed	< dZedB ed
< eedZeej ed< dZedB ed< dZejdB ed< eedZeeef ed< eedZeeef ed< ede
d	ed
edd fddZe					d0dedeej dedB deeef dB dejdB deeef dB d	edd fddZedeeef fddZede
fddZ edee
 dB fddZ!edee fddZ"edee
 dB fd d!Z#edefd"d#Z$ede
dB fd$d%Z%edefd&d'Z&edefd(d)Z'edefd*d+Z(deeef fd,d-Z)defd.d/Z*dS )1OmniRequestOutputaq  Unified request output for both pipeline stages and diffusion models.

    This class handles outputs from:
    1. Multi-stage LLM pipelines (with stage_id, final_output_type, request_output)
    2. Diffusion models (with images, prompt, metrics)

    Attributes:
        request_id: Unique identifier for this request
        finished: Whether generation is complete
        stage_id: Identifier of the stage that produced this output (pipeline mode)
        final_output_type: Type of output ("text", "image", "audio", "latents")
        request_output: The underlying RequestOutput from the stage (pipeline mode)
        images: List of generated PIL images (diffusion mode)
        prompt: The prompt used for generation (diffusion mode)
        latents: Optional tensor of latent representations (diffusion mode)
        metrics: Optional dictionary of generation metrics
     
request_idTfinishedNstage_idtextfinal_output_typerequest_output)default_factoryimagespromptlatentsmetrics_multimodal_outputreturnc                 C   s   | t |dd|||ddS )a  Create output from pipeline stage.

        Args:
            stage_id: Stage identifier
            final_output_type: Type of output
            request_output: The stage's output

        Returns:
            OmniRequestOutput configured for pipeline mode
        r   r   T)r   r   r   r   r   )getattr)clsr   r   r   r   r   r   from_pipeline@   s   
zOmniRequestOutput.from_pipelineimagemultimodal_outputc              
   C   s    | ||||||p	i |pi ddS )aR  Create output from diffusion model.

        Args:
            request_id: Request identifier
            images: Generated images
            prompt: The prompt used
            metrics: Generation metrics
            latents: Optional latent tensors

        Returns:
            OmniRequestOutput configured for diffusion mode
        T)r   r   r!   r"   r#   r$   r%   r   r   )r(   r   r!   r"   r$   r#   r+   r   r   r   r   from_diffusionY   s   z OmniRequestOutput.from_diffusionc                 C   s   | j du r| jS t| j tr| j n| j g}|D ]%}t|dg D ]}t|dd }r/|    S qt|dd }r<|  S q| jS )zReturn multimodal output from the underlying request output or local field.

        For pipeline outputs, this checks completion outputs first, then request_output.
        For diffusion outputs, this returns the local _multimodal_output field.
        Noutputsr+   )r   r%   
isinstancer   r'   )selfrequest_outputsreq_outoutputmmr   r   r   r+   {   s   
z#OmniRequestOutput.multimodal_outputc                 C   s
   t | jS )z&Return the number of generated images.)lenr!   r/   r   r   r   
num_images   s   
zOmniRequestOutput.num_imagesc                 C      | j durt| j ddS dS )zReturn prompt token IDs from the underlying request output.

        This property is required for compatibility with vLLM's streaming
        chat completion generator which checks res.prompt_token_ids.
        Nprompt_token_idsr   r'   r5   r   r   r   r8         
z"OmniRequestOutput.prompt_token_idsc                 C   s   | j durt| j dg S g S )zReturn outputs from the underlying request output.

        This property is required for compatibility with vLLM's streaming
        and non-streaming chat completion generators.
        Nr-   r9   r5   r   r   r   r-      r:   zOmniRequestOutput.outputsc                 C   r7   )zCReturn encoder prompt token IDs from the underlying request output.Nencoder_prompt_token_idsr9   r5   r   r   r   r;         
z*OmniRequestOutput.encoder_prompt_token_idsc                 C   r7   )z:Return prompt logprobs from the underlying request output.Nprompt_logprobsr9   r5   r   r   r   r=      r<   z!OmniRequestOutput.prompt_logprobsc                 C   r7   )zBReturn number of cached tokens from the underlying request output.Nnum_cached_tokensr9   r5   r   r   r   r>      r<   z#OmniRequestOutput.num_cached_tokensc                 C   r7   )z=Return KV transfer params from the underlying request output.Nkv_transfer_paramsr9   r5   r   r   r   r?      r<   z$OmniRequestOutput.kv_transfer_paramsc                 C   s   t | jdkp| jdkS )z*Check if this is a diffusion model output.r   r*   )r4   r!   r   r5   r   r   r   is_diffusion_output   s   z%OmniRequestOutput.is_diffusion_outputc                 C   s   | j duo	| jduS )z)Check if this is a pipeline stage output.N)r   r   r5   r   r   r   is_pipeline_output   s   z$OmniRequestOutput.is_pipeline_outputc                 C   sJ   | j | j| jd}| jr|| j| j| jd | jr#|d| j	i |S )z-Convert to dictionary for JSON serialization.)r   r   r   )r6   r"   r$   r   )
r   r   r   r@   updater6   r"   r$   rA   r   )r/   resultr   r   r   to_dict   s"   zOmniRequestOutput.to_dictc                 C   s   | j rdt| j  dnd}d| jd| j d| j d| jd| j d	| d
| jd| j d| j	 d| j
 g
}dd| dS )zBCustom repr to properly show image count instead of image objects.[z PIL Images]z[]zrequest_id=z	finished=z	stage_id=zfinal_output_type=zrequest_output=zimages=zprompt=zlatents=zmetrics=zmultimodal_output=zOmniRequestOutput(z, ))r!   r4   r   r   r   r   r   r"   r#   r$   r%   join)r/   images_reprpartsr   r   r   __repr__   s   








zOmniRequestOutput.__repr__)NNNNr*   )+r   r   r   r   r   r   r   r   boolr   intr   r   r   r   r   r!   r   r"   r   r#   r   r   r   r$   r   r%   classmethodr)   r,   propertyr+   r6   r8   r-   r;   r=   r>   r?   r@   rA   rD   rJ   r   r   r   r   r      s   
 	!

r   )dataclassesr   r   typingr   r   PILr   vllm.outputsr   vllm.v1.outputsr   vllm_omni.inputs.datar   r	   r   r   r   r   r   <module>   s    