
    QVji$                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
Z
d dlmZ d dlZ
d dlmc mZ d dlmZ d Z ed          d             Zd	d
de
j        dedefdZ G d d          Z G d d          ZdS )    N)defaultdict)Optional)StorageWeakRefc                        fd}|S )zkLazily wrap a function with torch.compile on the first call

    This avoids eagerly importing dynamo.
    c                 J     t          j                    fd            }|S )Nc                      t          j        fi } t          j                  |          t	                      j        <    || i |S N)torchcompile	functoolswrapsglobals__name__)argskwargscompiled_fncompile_kwargsfns      T/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/utils/_content_store.pycompile_hookz7lazy_compile.<locals>.decorate_fn.<locals>.compile_hook5   sS    -==n==K%8Y_R%8%8%E%EGIIbk";////    )r   r   )r   r   r   s   ` r   decorate_fnz!lazy_compile.<locals>.decorate_fn4   s?    				0 	0 	0 	0 	0 
		0
 r    )r   r   s   ` r   lazy_compiler   .   s$         r   T)dynamicc                    t          j        dd| j        | j        t           j                                                  }|dz  dz                                   }t          j        dd| j        | j        t           j                                                                                  }t          j        || z  |z   	                                dg          S )Ni   l        )devicedtypei   r   )
r
   randintshaper   int32abslongprimsxor_sumint)xabs      r   hash_storage_kernelr+   D   s    
 	%	 	 		cee  y/Q	$$&&Ahqwqxu{SSS		  =!a%!)**QC000r   Fstable_hashstorager-   returnc                   dd l }ddlm} | j        j        }|s ||          s|                                 }t          j        |                                z  	                    |
                                          }t          j        d          }|                    |           |                                S |dk    r|j        j        }nx|dk    r|j        j        | j        j                 }nU|dk    r|j                                        }n5|dk    r|j        j        | j        j                 }nt/          d	|           |                                }		 |                    d            |j        d|j        | j        
                              |                                            dz  }
|
dk    rt=          j        d|
fdd                               |j!                  d}fdtE          |          D             }tG          j$        dd|z  z   g|R  %                                |&                    |	           S # |&                    |	           w xY w)Nr   )is_compile_supportedF)usedforsecuritycpucudampsxpuzunhandled device type r   r      constant   c                 R    g | ]#}t                                                    $S r   )r+   item).0_r(   s     r   
<listcomp>z hash_storage.<locals>.<listcomp>   s.    AAA!!$$))++AAAr   >i)'torch._dynamotorch._dynamo.utilsr1   r   typer3   ctypesc_bytenbytesfrom_addressdata_ptrhashlibsha1update	hexdigest_Cdefault_generatorr4   default_generatorsindexr5   _get_default_mps_generatorr6   AssertionError	get_statemanual_seedemptyuint8set_numelFpadviewr"   rangestructpackhex	set_state)r.   r-   r
   r1   device_typecpu_storagebufrK   	generatorstater[   ITERcsr(   s                @r   hash_storageri   _   ss   888888.%K 	 ..{;; 	 kkmm }{11333AA  ""
 
 |E222C~~ eH.					J1'.2FG					I88::					I01EF		CkCCDDD!!E#a   EKW^DDDII'RR wwyyj1n77a!S:q11AFF5; AAAAU4[[AAA{3t+1b1115577E""""	E""""s   CI I&c                   t    e Zd ZddededdfdZdej        defdZdd	ej	        fd
Z
ded	ej	        ddfdZdS )ContentStoreWriterFlocr-   r/   Nc                 H    || _         t                      | _        || _        d S r	   )rl   setseen_storage_hashesr-   )selfrl   r-   s      r   __init__zContentStoreWriter.__init__   s#    -0UU &r   r.   c                    t          || j                  }|| j        v r|S t          j                            | j        d          }t          j        |d           t          j                            ||          }t          j                            |          r|S t          j
        ||           | j                            |           |S )Nr,   storagesTexist_ok)ri   r-   ro   ospathjoinrl   makedirsexistsr
   saveadd)rp   r.   h	subfoldertargets        r   write_storagez ContentStoreWriter.write_storage   s    d.>???(((H GLL:66	
I----i++7>>&!! 	H
7F### $$Q'''r   tc                    |(t          |                                | j                  }|j        ||                                t          |j                  |                                t          j	        
                    |          fS )Nr,   )ri   untyped_storager-   r   storage_offsettupler!   strider
   _utilsget_tensor_metadata)rp   r   r}   s      r   compute_tensor_metadataz*ContentStoreWriter.compute_tensor_metadata   sr    9Q..00d>NOOOAG!'NNHHJJL,,Q//
 	
r   namec                    |                                 }|                     |          }t          j                            |          \  }}|                     ||          }t          j                            | j        d|          }t          j        |d           t          j
        |t          j                            ||                     d S )N)r}   tensorsTrt   )r   r   rv   rw   splitr   rx   rl   ry   r
   r{   )	rp   r   r   r.   r}   dfpayloadr~   s	            r   write_tensorzContentStoreWriter.write_tensor   s    ##%%w''w}}T""1..qA.66GLL9a88	
I----
7BGLLA6677777r   )Fr	   )r   
__module____qualname__strboolrq   r
   UntypedStorager   Tensorr   r   r   r   r   rk   rk      s        ' 'C 'd 't ' ' ' 'U%9 c    

 

 

 

 

 

8 8 8$ 8 8 8 8 8 8r   rk   c                   p    e Zd ZdddeddfdZdddedej        fd	Zd
efdZddd
edej	        fdZ
dS )ContentStoreReaderT)cacherl   r/   Nc                \    || _         d | _        |rt          t                    | _        d S d S r	   )rl   storage_cacher   dict)rp   rl   r   s      r   rq   zContentStoreReader.__init__   s@      	  	3!,T!2!2D	3 	3r   r   r}   c                   |t          j        |          }| j         | j        |                             |          nd }|(t           j                            |j                  }||S t          j        t          j	        
                    | j        d|          d|          j        }|J | j        t          |          | j        |         |<   |S )Nrs   T)weights_onlymap_location)r
   r   r   getr   _new_with_weak_ptrcdataloadrv   rw   rx   rl   _untyped_storager   )rp   r}   r   wsss        r   read_storagezContentStoreReader.read_storage   s    \&))F !- v&**1--- 	 >$77AAA}JGLL:q11
 
 
 	 	

 }}}),:1,=,=Dv&q)r   r   c                     t           j                            | j        d|          }t           j                            |          st          |          t          j        |d          S )Nr   T)r   )rv   rw   rx   rl   rz   FileNotFoundErrorr
   r   )rp   r   r   s      r   read_tensor_metadataz'ContentStoreReader.read_tensor_metadata   sT    W\\$(It44w~~b!! 	(#B'''z"40000r   c                   |                      |          \  }}}}}}|                     ||          }	t          j        g ||	j                  }
|
                    |	|||           t          j                            |
|           |
S )Nr   r7   )r   r   r
   tensorr   rX   r   set_tensor_metadata)rp   r   r   r   r}   r   sizer   metadatar.   r   s              r   read_tensorzContentStoreReader.read_tensor   s    ;?;T;T<
 <
8q.$ ##Af#55L5@@@	wf555((H555r   )r   r   r   r   rq   r
   r   r   r   r   r   r   r   r   r   r      s        *. 3 3 3C 34 3 3 3 3 .2   c e6J    .1 1 1 1 1 04          r   r   )rE   r   rJ   os.pathrv   r^   collectionsr   typingr   r
   torch._prims_primsr%   torch._utilstorch.nn.functionalnn
functionalrZ    torch.multiprocessing.reductionsr   r   r+   r   r   r   ri   rk   r   r   r   r   <module>r      s  >         # # # # # #                           ; ; ; ; ; ;  , d1 1 14 HM ,# ,# ,#%. ,# ,#QT ,# ,# ,# ,#^08 08 08 08 08 08 08 08f. . . . . . . . . .r   