
    3j                        S SK Jr  S SKrS SKJr  S SKrS SKJr  SSKJ	r	  SSK
Jr  SSKJrJr  SS	KJr  SS
KJr   " S S5      rg)    )annotationsN)Path)	load_file   )canonicalize_state_dict)MAIN_REGISTRY)ModelDescriptor	StateDict)ArchRegistry)RestrictedUnpicklec                  l    \ rS rSrSrS\4   SS jjrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSrg)ModelLoader   z@Class for automatically loading a pth file into any architectureNc                    [        U[        5      (       a  [        R                  " U5      nU=(       d    [        R                  " S5      U l        X l        g )Ncpu)
isinstancestrtorchdeviceregistry)selfr   r   s      S/home/wildlama/miniconda3/lib/python3.13/site-packages/spandrel/__helpers/loader.py__init__ModelLoader.__init__   s>    
 fc""\\&)F$*$Aell5.A&.	    c                F    U R                  U5      nU R                  U5      $ )z
Load a model from the given file path.

Throws a `ValueError` if the file extension is not supported.
Throws an `UnsupportedModelError` if the model architecture is not supported.
)load_state_dict_from_fileload_from_state_dict)r   path
state_dicts      r   load_from_fileModelLoader.load_from_file#   s%     33D9
((44r   c                   [         R                  R                  U5      S   R                  5       nUS:X  a   U R	                  U5      nOEUS:X  d  US:X  a  U R                  U5      nO'US:X  a  U R                  U5      nO[        SU S35      e[        U5      $ ! [
         a.     U R                  U5      nO! [         a    Sn Of = fUc  e Un NBf = f)	z
Load the state dict of a model from the given file path.

State dicts are typically only useful to pass them into the `load`
function of a specific architecture.

Throws a `ValueError` if the file extension is not supported.
r   z.ptNz.pthz.ckptz.safetensorsz!Unsupported model file extension z$. Please try a supported model type.)osr   splitextlower_load_torchscriptRuntimeError	_load_pth	Exception_load_safetensors
ValueErrorr   )r   r   	extensionr    pth_state_dicts        r   r   %ModelLoader.load_state_dict_from_file.   s     GG$$T*1-335	 ,!33D9
 & I$8-J.(//5J3I;>bc  'z22/   ,*%)^^D%9N  *%)N* ") +
,s5   B 
C%B76C7CCC	CCc                j    U R                   R                  U5      R                  U R                  5      $ )zx
Load a model from the given state dict.

Throws an `UnsupportedModelError` if the model architecture is not supported.
)r   loadtor   )r   r    s     r   r    ModelLoader.load_from_state_dictW   s(     }}!!*-00==r   c                J    [         R                  " UU R                  [        S9$ )N)map_locationpickle_module)r   r1   r   r   r   r   s     r   r)   ModelLoader._load_pth`   s     zz,
 	
r   c                l    [         R                  R                  XR                  S9R	                  5       $ )N)r5   )r   jitr1   r   r    r7   s     r   r'   ModelLoader._load_torchscriptg   s*    yy~~{{  

*,	r   c                <    [        U[        U R                  5      S9$ )N)r   )r   r   r   r7   s     r   r+   ModelLoader._load_safetensorsl   s    c$++&677r   )r   r   )r   zstr | torch.device | Noner   r   )r   
str | Pathreturnr	   )r   r>   r?   r
   )r    r
   r?   r	   )__name__
__module____qualname____firstlineno____doc__r   r   r!   r   r   r)   r'   r+   __static_attributes__ r   r   r   r      sC    J -1!.)  	5'3R>

8r   r   )
__future__r   r$   pathlibr   r   safetensors.torchr   canonicalizer   main_registryr   model_descriptorr	   r
   r   r   	unpicklerr   r   rF   r   r   <module>rN      s/    " 	   ' 1 ( 8 " )]8 ]8r   