o
    ٷi                     @   sn   d dl Z d dlmZ d dlmZ d dlmZ eeZde	fddZ
dedefd	d
ZededefddZdS )    N)	lru_cache)init_logger)get_hf_file_to_dictreturnc                 C   s   ddl m} || }|S )Nr   )DiffusionPipeline)"diffusers.pipelines.pipeline_utilsr   load_config)
model_namer   config r   V/home/ubuntu/.local/lib/python3.10/site-packages/vllm_omni/diffusion/utils/hf_utils.pyload_diffusers_config
   s   
r   r	   c                 C   sP   zt d| }|d}|dkrW dS |dpg }d|v W S  ty'   Y dS w )zABest-effort detection for Bagel (non-diffusers) diffusion models.zconfig.json
model_typebagelTarchitecturesBagelForConditionalGenerationF)r   get	Exception)r	   cfgr   r   r   r   r   _looks_like_bagel   s   


r   c              
   C   s  t j| r]t j| d}t j|r]z1ddl}t|}||}W d   n1 s,w   Y  |drC|drCt	
d W dS W n ty\ } zt	
d| W Y d}~nd}~ww ztd| }|dru|drut	
d	 W dS W n ty } zt	
d
| W Y d}~nd}~ww zt|  W dS  ttfy } zt	
d| t	
d W Y d}~t| S d}~w ty } zt	
d| W Y d}~t| S d}~ww )a9  Check if a model is a diffusion model.

    Uses multiple fallback strategies to detect diffusion models:
    1. Check local file system for model_index.json (fastest, no imports)
    2. Check using vllm's get_hf_file_to_dict utility
    3. Try the standard diffusers approach (may fail due to import issues)
    zmodel_index.jsonr   N_class_name_diffusers_versionz3Detected diffusion model via local model_index.jsonTz)Failed to read local model_index.json: %sz-Detected diffusion model via model_index.jsonz<Failed to check model_index.json via get_hf_file_to_dict: %sz+Failed to import diffusers dependencies: %sz6This may be due to flash_attn/PyTorch version mismatchz9Failed to load diffusers config via DiffusionPipeline: %s)ospathisdirjoinexistsjsonopenloadr   loggerdebugr   r   r   ImportErrorModuleNotFoundErrorr   )r	   model_index_pathr   fconfig_dicter   r   r   is_diffusion_model   sN   




r(   )r   	functoolsr   vllm.loggerr   vllm.transformers_utils.configr   __name__r    dictr   strboolr   r(   r   r   r   r   <module>   s    