o
    ϯit                  	   @   s   d Z ddlZddlZddlmZmZ ddlZddlmZ ddl	m
Z
mZmZ ddgZd	ee fd
dZej r:dnddejdddfdedeeejf dedefddZdS )z OpenAI pretrained model functions

Adapted from https://github.com/openai/CLIP. Originally MIT License, Copyright (c) 2021 OpenAI.
    N)UnionList   )"build_model_from_openai_state_dict)get_pretrained_urllist_pretrained_tag_modelsdownload_pretrainedlist_openai_modelsload_openai_modelreturnc                   C   s   t dS )z*Returns the names of available CLIP modelsopenai)r    r   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/laion_clap/clap_module/openai.pyr	      s   cudacpuTz~/.cache/clipFNonenamedeviceenable_fusionfusion_typec                    s  t | drtt | d|d}ntj| r| }ntd|  dt  ztjj	||r, ndd
 }d}	W n tyR   |rItd| d	 d
}tj	|dd}	Y nw |szt|	p\| ||| }W n ty   dd |	d  D }
t|
||| }Y nw t dkr|  |S tjj fddg d}dd |jdD d fdd}|| ||j ||j t dkrtjjdd g d}t|jd d }| fdd}|| ||j ||j |  |jj|j _|S )a  Load a CLIP model, preserve its text pretrained part, and set in the CLAP model

    Parameters
    ----------
    name : str
        A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict
    device : Union[str, torch.device]
        The device to put the loaded model
    jit : bool
        Whether to load the optimized JIT model (default) or more hackable non-JIT model.

    Returns
    -------
    model : torch.nn.Module
        The CLAP model
    preprocess : Callable[[PIL.Image], torch.Tensor]
        A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input
    r   )rootzModel z not found; available models = r   )map_locationNzFile z6 is not a JIT archive. Loading as a state dict insteadFc                 S   s   i | ]\}}|d d |qS )   Nr   ).0kvr   r   r   
<dictcomp>H   s    z%load_openai_model.<locals>.<dictcomp>
state_dictc                      s   t g t  S N)torchonestor   r   )r   r   r   <lambda>P   s    z#load_openai_model.<locals>.<lambda>)example_inputsc                 S   s   g | ]
}d t |v r|qS )Device)repr)r   nr   r   r   
<listcomp>Q   s    z%load_openai_model.<locals>.<listcomp>prim::Constantc                    s   zt | dr
| jgng }W n ty   g }Y nw t | dr%|| jj |D ]}|dD ]}d| v rDt|d drD|	  q.q'd S )Ngraphforward1r(   valuer   )
hasattrr*   RuntimeErrorappendr+   findAllNodesattributeNamesstr
startswithcopyAttributes)modulegraphsr*   node)device_noder   r   patch_deviceS   s   

z'load_openai_model.<locals>.patch_devicec                   S   s   t g  S r   )r   r    floatr   r   r   r   r"   g   s    aten::tor   c                    s   zt | dr
| jgng }W n ty   g }Y nw t | dr%|| jj |D ](}|dD ] }t| }dD ]}||  d dkrM||  	  q8q.q'd S )Nr*   r+   r;   )r      r,      )
r-   r*   r.   r/   r+   r0   listinputsr7   r4   )r5   r6   r*   r7   r?   i)
float_noder   r   patch_floatk   s"   
z&load_openai_model.<locals>.patch_float)!r   r   ospathisfiler.   r	   r   jitloadevalwarningswarnr   r   r!   KeyErroritemsr2   r:   tracer*   r0   applyencode_audioencode_textr>   findNoder?   r7   	audio_cfgaudio_lengthaudio_branch)r   	model_cfgr   rF   	cache_dirr   r   
model_pathmodelr   sddevice_holderr9   float_holderfloat_inputrB   r   )r   r8   rA   r   r
      sR   
 





)__doc__rC   rI   typingr   r   r   rX   r   
pretrainedr   r   r   __all__r2   r	   r   is_availablerD   
expanduserr   boolr
   r   r   r   r   <module>   s.    
