o
    ۷i                     @   sv   d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ G d	d
 d
ZdS )    N)Sequence)get_hf_file_to_dict)OmniDiffusionConfigTransformerConfig)DiffusionEngine)OmniDiffusionRequest)OmniDiffusionSamplingParamsOmniPromptType)OmniRequestOutputc                
   @   s   e Zd ZdZddedB fddZg fdeee B dede	e
 d	e	e fd
dZded	e	e fddZdddZdd Zdde
dB d	dfddZd	efddZdS )OmniDiffusiona  
    It is the main class to interact with vLLM-Omni diffusion models.
    It acts as a high-level interface that prepares requests and
    delegates the actual diffusion process to the DiffusionEngine.

    You can pass either an `OmniDiffusionConfig` via `od_config`, or
    pass kwargs such as `model="Qwen/Qwen-Image"`,
    which will be forwarded to `OmniDiffusionConfig.from_kwargs`.
    N	od_configc                 K   s  | d}| d}|d u rtjdi |}nt|tr6|d u r%| d}|d u r.| d}tjdi |}|| _|d urE| jjd| |d urQ| jjd| z(td|j	}|d urt| dd |_
|  td|j	}t||_ntdW nt ttttfy   td|j	}|d u rtd|j	 | d	}| d
pg }	d }
|dksd|	v rd}
n#|dkr|j
d u rd}
n|dksd|	v rd}
n|	rt|	dkr|	d }
|
d u rtd| d|	 |
|_
t |_|  Y nw t|| _d S )Nstage_idengine_input_sourcezmodel_index.json_class_nameztransformer/config.jsonzmodel_index.json not foundzconfig.jsonz9Could not find config.json or model_index.json for model 
model_typearchitecturesbagelBagelForConditionalGenerationBagelPipelinenextstepNextStep11Pipelinez	glm-image GlmImageForConditionalGenerationGlmImagePipeline   r   zUnknown model type: z, architectures:  )getr   from_kwargs
isinstancedictr   omni_kv_config
setdefaultr   modelmodel_class_nameupdate_multimodal_supportr   	from_dicttf_model_configFileNotFoundErrorAttributeErrorOSError
ValueErrorlenr   make_engineengine)selfr   kwargsr   r   config_dicttf_config_dictcfgr   r   pipeline_classr   r   Z/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm_omni/entrypoints/omni_diffusion.py__init__   sl   






zOmniDiffusion.__init__promptssampling_paramsrequest_idsreturnc                    sl   t |ttfr|g}nt|}t t|k r+  fddtt|t  D  t|| }| |S )Nc                 3   s*    | ]}|t    d t  V  qdS )_N)r*   uuiduuid4).0ir7   r   r3   	<genexpr>p   s   ( z)OmniDiffusion.generate.<locals>.<genexpr>)	r   strr   listr*   extendranger   _run_engine)r-   r5   r6   r7   requestr   r>   r3   generatec   s   (
zOmniDiffusion.generaterE   c                 C   s   | j |S N)r,   step)r-   rE   r   r   r3   rD   u   s   zOmniDiffusion._run_enginec                 C   s   | j   d S rG   )r,   closer-   r   r   r3   rI   x   s   zOmniDiffusion.closec                 C   s$   z|    W d S  ty   Y d S w rG   )rI   	ExceptionrJ   r   r   r3   __del__{   s
   zOmniDiffusion.__del__trace_filenamec                 C   s(   t | dr| jr| j| dS td)zStart profiling for the diffusion model.

        Args:
            trace_filename: Optional base filename for trace files.
                           If None, a timestamp-based name will be generated.
        r,    Diffusion engine not initializedN)hasattrr,   start_profileRuntimeError)r-   rM   r   r   r3   rP      s   zOmniDiffusion.start_profilec                 C   s"   t | dr| jr| j S td)zStop profiling and return profiling results.

        Returns:
            Dictionary containing paths to trace and table files.
        r,   rN   )rO   r,   stop_profilerQ   rJ   r   r   r3   rR      s   
zOmniDiffusion.stop_profilerG   )r8   N)__name__
__module____qualname____doc__r   r4   r	   r   r   rA   r@   r
   rF   r   rD   rI   rL   rP   r   rR   r   r   r   r3   r      s$    
L


r   )r:   collections.abcr   vllm.transformers_utils.configr   vllm_omni.diffusion.datar   r   $vllm_omni.diffusion.diffusion_enginer   vllm_omni.diffusion.requestr   vllm_omni.inputs.datar   r	   vllm_omni.outputsr
   r   r   r   r   r3   <module>   s   