
    iriA                    j   d dl mZ d dlZd dlZ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mZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZmZ d d	lmZ erd d
lmZ d dlmZm Z m!Z!m"Z"  ee#          $                                j%        Z&e&j%        Z' G d de          Z(dZ)dDdZ* ed          Z+ ed          Z,dEdZ-dFdZ.ej/        dGd!            Z0ej1        dHd$            Z2dId'Z3 G d( d)          Z4	 dJdKd.Z5	 	 dLdMd4Z6dNd8Z7dOd:Z8dPd?Z9 G d@ dA          Z: G dB dCee+                   Z;dS )Q    )annotationsN)is_dataclass)autoEnum)Pathpformat)AnyGenericTYPE_CHECKINGTypeVar)assert_neverSelf)CodeTemplate)	Namespace)CallableIterableIteratorSequencec                      e Zd Z e            Z e            Z e            Z e            Z e            Z e            Z	dS )TargetN)
__name__
__module____qualname__r   
DEFINITIONDECLARATIONREGISTRATIONANONYMOUS_DEFINITIONNAMESPACED_DEFINITIONNAMESPACED_DECLARATION     f/var/www/html/bestrading.cuttalo.com/models/btc_v9/venv/lib/python3.11/site-packages/torchgen/utils.pyr   r   %   sU        J$&&K466L466 DFF!TVVr"   r   z(^|\W){}($|\W)schemastrreturntuple[str, list[str]]c                    t          j        d|           }|t          d|            |                                \  }}}||                    d          fS )Nz(\w+)(\.\w+)?\((.*)\)zUnsupported function schema: , )rematchRuntimeErrorgroupssplit)r$   mname_paramss        r#   split_name_paramsr3   8   sZ    
)622AyC6CCDDDhhjjOD!Vd####r"   TSfuncCallable[[T], S | None]xsIterable[T]Iterator[S]c              #  6   K   |D ]} | |          }||V  d S Nr!   )r6   r8   xrs       r#   mapMayber?   H   s;        DGG=GGG r"   Callable[[T], Sequence[S]]c              #  6   K   |D ]} | |          E d {V  d S r<   r!   )r6   r8   r=   s      r#   	concatMaprB   P   sB        477 r"   msg_fnCallable[[], str]Iterator[None]c              #     K   	 d V  d S # t           $ rX} |             }t          j        |d          }|j        r|j        d          d| n|}|f|j        dd          z   |_         d }~ww xY w)Nz  r   
   )	Exceptiontextwrapindentargs)rC   emsgs      r#   contextrO   X   s         fhhoc4(('(v6##c###3!&*$s   
 
A,AA''A,template_fnr   c                *    t          j        |           S r<   )r   	from_file)rP   s    r#   _read_templaterS   e   s    !+...r"   sintc                    t          j        |                     d          d                                          }t                              |d          S )Nlatin1F)usedforsecuritylittle)	byteorder)hashlibsha1encodedigestrU   
from_bytes)rT   r\   s     r#   string_stable_hashr`   k   sD    <**EBBBIIKKD>>$(>333r"   c                  z    e Zd Zd'dZed(d
            Zd)dZd*dZd+dZd,dZ	ddd-d"Z
ddd.d#Zd/d%Zd0d&ZdS )1FileManagerinstall_dir
str | Pathtemplate_dirdry_runboolr&   Nonec                    t          |          | _        t          |          | _        t                      | _        || _        d S r<   )r   rc   re   setfilesrf   )selfrc   re   rf   s       r#   __init__zFileManager.__init__t   s:      ,, .. #
r"   frozenset[str]c                >    t          d | j        D                       S )Nc                6    h | ]}|                                 S r!   as_posix.0files     r#   	<setcomp>z(FileManager.filenames.<locals>.<setcomp>   s     AAAd$--//AAAr"   )	frozensetrk   rl   s    r#   	filenameszFileManager.filenames   s!    AAdjAAABBBr"   filenamecontentsr%   c                    t          |          }d }	 |                    d          }n# t          $ r Y nw xY w||k    r5|j                            dd           |                    |d           d S d S )Nzutf-8)encodingT)parentsexist_ok)r   	read_textOSErrorparentmkdir
write_text)rl   rz   r{   ru   old_contentss        r#   _write_if_changedzFileManager._write_if_changed   s    H~~#'	>>7>;;LL 	 	 	D	|##KdT:::OOHwO77777 $#s   * 
77rP   env_callable"Callable[[], str | dict[str, Any]]c                    t          |                                          rJ d|             | j        |z  } |            }t          |t                    rd|vrt
          dz  }	 t          t          j        d         j        p|          	                                }n+# t          t          f$ r |	                                }Y nw xY w	 |                    t                                                    }n# t          $ r
 |j        }Y nw xY wi |ddd| d| z   i}t#          |          }|                    |          }	t'          j        dd	|	t&          j        
          S t          |t,                    r|S t/          |           d S )Nztemplate_fn must be relative: generated_commentzgen.py__main__@zgenerated by z from z
                (""")\n+             # match triple quotes
                (
                    (\s*@.+\n)*     # match decorators if any
                    \s*(class|def)  # match class/function definition
                )
                z\g<1>\n\n\g<2>)flags)r   is_absolutere   
isinstancedictTORCHGEN_ROOTsysmodules__file__absoluteKeyErrorAttributeErrorrelative_to	REPO_ROOTrr   
ValueErrorr0   rS   
substituter*   subVERBOSEr%   r   )
rl   rP   r   template_pathenvgenerator_default	generatorgenerator_pathtemplatesubstitute_outs
             r#   substitute_with_templatez$FileManager.substitute_with_template   s   
 $$0022 	
 	
:[::	
 	
 	
 )K7lnnc4   '	"#--$1H$<!= $J/8M<M! !hjj I !.1 = = = 1 : : < <III=4%.%:%:9%E%E%N%N%P%PNN! 4 4 4%.^NNN4'QnQQKQQQ  &m44H%0055N 6 "j    c3 	JSs$   (8B! !%C	C	,C: :DDc                H   t          |          }|                                rJ d|             | j        |z  }|| j        vsJ d|             | j                            |           | j        s0|                     ||          }|                     ||           d S d S )Nfilename must be relative: zduplicate file write )rP   r   )rz   r{   )r   r   rc   rk   addrf   r   r   )rl   rz   rP   r   ru   r   s         r#   write_with_templatezFileManager.write_with_template   s     >>''))SS+S+S+SSSS(*4:%%%'Et'E'E%%%
t| 	K!::') ;  N ""D>"JJJJJ	K 	Kr"   c                4    |                      |||           d S r<   )r   )rl   rz   r   s      r#   writezFileManager.write   s"    
 	  8\BBBBBr"   N)base_envitemsr9   key_fnCallable[[T], str]#Callable[[T], dict[str, list[str]]]
num_shardsrU   r   dict[str, Any] | Nonesharded_keysset[str]c          
     @    |                      ||||||||           d S )N)r   r   r   r   r   )write_sharded_with_template)rl   rz   r   r   r   r   r   r   s           r#   write_shardedzFileManager.write_sharded   sC     	((%!% 	) 		
 		
 		
 		
 		
r"   c               H   t          |          }	|	                                rJ d|             ddi}
d t          |          D             }|
g|z   }||D ]                    |           D ]S}|D ]N|v rCt	          |         t
                    s
J d            |                                         |<   Ig |<   OTdfd}| j        rd}|D ]H} ||          }t          |          |z  } ||          } |||         |            ||
|           I|D ]@d         }| 	                    |	
                    |	j         |           |fd           A| j                            | j        |	
                    |	j         d          z             d S )Nr   shard_id
Everythingc                    g | ]	}d d| i
S )r   r1   r!   )rt   is     r#   
<listcomp>z;FileManager.write_sharded_with_template.<locals>.<listcomp>  s0     (
 (
 (
&'ZQ!(
 (
 (
r"   z'sharded keys in base_env must be a listintodict[str, list[str]]from_r&   rh   c                ~    |                                 D ]&\  }}|v sJ d|             | |xx         |z  cc<   'd S )Nzundeclared sharded key )r   )r   r   kvr   s       r#   	merge_envz:FileManager.write_sharded_with_template.<locals>.merge_env  sa      1L(((*GA*G*G(((Q1 r"   r!   c                      S r<   r!   )shards   r#   <lambda>z9FileManager.write_sharded_with_template.<locals>.<lambda>-  s     r"   )r   r   r   r   r&   rh   )r   r   rangeupdater   listcopyrf   r`   r   	with_stemstemrk   discardrc   )rl   rz   rP   r   r   r   r   r   r   ru   
everythingshards
all_shardskeyr   itemsidr   r   r   s           `          @r#   r   z'FileManager.write_sharded_with_template   sS    H~~##%%OO'OX'O'OOOO&0,%?
(
 (
+0+<+<(
 (
 (
 !\F*
# ' 'X&&&& 	$ 	$C# $ $%<<%eCj$77  A   "'s!2!2E#JJ!#E#JJ$	 	 	 	 	 	
 < 	E 	' 	'D&,,C$S))J6C,t$$CIfSk3'''Ij#&&&& 	 	EZ(H$$$)7X7788    	
4+dnn	=U=U=U.V.VVWWWWWr"   variable_namec                    d                     d|gd t          | j                  D             dR           }|                     ||           dS )zSWrite a file containing the list of all outputs which are generated by this script.rG   zset(c              3  F   K   | ]}d |                                  dV  dS )z    ""Nrq   rs   s     r#   	<genexpr>z,FileManager.write_outputs.<locals>.<genexpr>:  s6      LL,$--//,,,LLLLLLr"   )N)joinsortedrk   r   )rl   r   rz   contents       r#   write_outputszFileManager.write_outputs3  sp    )) ML
9K9KLLL	
  
 
 	x11111r"   c                    t           j                            | j        t           j                            t
                              S )z
        This needs to be deterministic. The template dir is an absolute path
        that varies across builds. So, just use the path relative to this file,
        which will point to the codegen source but will be stable.
        )ospathrelpathre   dirnamer   rx   s    r#   template_dir_for_commentsz%FileManager.template_dir_for_comments@  s+     wt0"'//(2K2KLLLr"   )rc   rd   re   rd   rf   rg   r&   rh   )r&   rn   )rz   rd   r{   r%   r&   rh   )rP   rd   r   r   r&   r%   )rz   rd   rP   rd   r   r   r&   rh   )rz   rd   r   r   r&   rh   )rz   rd   r   r9   r   r   r   r   r   rU   r   r   r   r   r&   rh   )rz   rd   rP   rd   r   r9   r   r   r   r   r   rU   r   r   r   r   r&   rh   )r   r%   rz   rd   r&   rh   r&   r%   )r   r   r   rm   propertyry   r   r   r   r   r   r   r   r   r!   r"   r#   rb   rb   s   s        	 	 	 	 C C C XC
8 
8 
8 
84 4 4 4lK K K K$C C C C +/
 
 
 
 
 
> +/<X <X <X <X <X <X|2 2 2 2M M M M M Mr"   rb   optionsr   rc   str | Path | Nonec                    t           j                            | j        d          }|r|n| j        }t          ||| j                  S )N	templates)rc   re   rf   )r   r   r   source_pathrc   rb   rf   )r   rc   re   s      r#   make_file_managerr   J  sO     7<< 3[AAL!,E++'2EK!   r"   P   objr
   rK   widthc                $    t          | ||          S r<   r   )r   rK   r   s      r#   dataclass_reprr   X  s    
 3&&&r"   attrdict[Any, Any]curr_indentc           
     D   ||dz   z  }g }|                                  D ]n\  }}t          |          }t          |          r"t          ||||t	          |          z             nt          |          }|                    | d|            ot          ||||dd          S )N   z: {})r   reprr   r	   lenappend_format)	r   rK   r   r   	dict_reprr   r   k_reprv_strs	            r#   _format_dictr   `  s     6A:KI

 / /1a AGAvukCKK&?@@@a 	
 	F--e--....9fe[#sCCCr"   &list[Any] | set[Any] | tuple[Any, ...]c                    dz   z  fd| D             }t          | t                    rdnd\  }}t          |||          S )NrH   c                p    g | ]2}t          |          rt          |          nt          |          3S r!   )r   r	   r   )rt   lr   rK   r   s     r#   r   z _format_list.<locals>.<listcomp>{  sM        3?q//N65+...tAww  r"   )[])(r   )r   r   r   )r   rK   r   r   	list_reprstartends    ```   r#   _format_listr  t  s}     6A:K       I  *$55E:JE39fe[%EEEr"   
fields_str	list[str]r	  r
  c                    d\  }}t          t          |                     |k    rd}d|z  }d|z  }d| |                     |           }	| | |	 | S )N) r  rG    r)   )r   r   r   )
r  rK   r   r   r	  r
  	delimitercurr_indent_str
indent_strbodys
             r#   r   r     s|     "(I
4
%%	+vJ,	,?,,11*==D,Z,,s,,,r"   c                      e Zd ZdZ	 	 dddZe	 ddd            Zedd            Zedd            Z	edd            Z
dddZdS )NamespaceHelpera  A helper for constructing the namespace open and close strings for a nested set of namespaces.

    e.g. for namespace_str torch::lazy,

    prologue:
    namespace torch {
    namespace lazy {

    epilogue:
    } // namespace lazy
    } // namespace torch
    r     namespace_strr%   entity_name	max_levelrU   r&   rh   c                L   |                     d          }t          |          |k    sJ d| d| d            || _        d                    d |D                       | _        d                    d t          |          D                       | _        || _        || _        d S )N::z"Codegen doesn't support more than z# level(s) of custom namespace. Got .rG   c                    g | ]}d | d	S )z
namespace z {r!   rt   ns     r#   r   z,NamespaceHelper.__init__.<locals>.<listcomp>  s$    #P#P#PA$7$7$7$7#P#P#Pr"   c                    g | ]}d | S )z} // namespace r!   r  s     r#   r   z,NamespaceHelper.__init__.<locals>.<listcomp>  s!    FFF###FFFr"   )	r.   r   cpp_namespace_r   	prologue_reversed	epilogue_namespaces_entity_name_)rl   r  r  r  cpp_namespacess        r#   rm   zNamespaceHelper.__init__  s     ',,T22>""i///ooo_looo 0// ,#P#P#P#P#PQQFFXn-E-EFFF
 
 *'r"   namespaced_entityc                    |                      d          }|d         }d                    |dd                   }t          |||          S )zq
        Generate helper from nested namespaces as long as class/function name. E.g.: "torch::lazy::add"
        r  N)r  r  r  )r.   r   r  )r)  r  namesr  r  s        r#   from_namespaced_entityz&NamespaceHelper.from_namespaced_entity  sW     "''--Bi		%*--'[I
 
 
 	
r"   c                    | j         S r<   )r#  rx   s    r#   prologuezNamespaceHelper.prologue  
    ~r"   c                    | j         S r<   )r%  rx   s    r#   epiloguezNamespaceHelper.epilogue  r0  r"   c                    | j         S r<   )r'  rx   s    r#   r  zNamespaceHelper.entity_name  s      r"   defaultc                "    | j         r| j         n|S )z
        Return the namespace string from joining all the namespaces by "::" (hence no leading "::").
        Return default if namespace string is empty.
        )r"  )rl   r4  s     r#   get_cpp_namespacez!NamespaceHelper.get_cpp_namespace  s    
 '+&9Ft""wFr"   N)r  r  )r  r%   r  r%   r  rU   r&   rh   )r  )r)  r%   r  rU   r&   r  r   )r  )r4  r%   r&   r%   )r   r   r   __doc__rm   staticmethodr-  r   r/  r2  r  r6  r!   r"   r#   r  r    s           	( ( ( ( (&  
 
 
 
 \
    X    X ! ! ! X!G G G G G G Gr"   r  c                  |    e Zd ZU ded<   dddZddZddZd dZd!dZd"dZ	e
d#d            Zd$dZd%dZd&dZdS )'
OrderedSetzdict[T, None]storageNiterableIterable[T] | Noner&   rh   c                Z    |	i | _         d S t                              |          | _         d S r<   )r;  r   fromkeys)rl   r<  s     r#   rm   zOrderedSet.__init__  s*    DLLL==22DLLLr"   r   r4   rg   c                    || j         v S r<   r;  rl   r   s     r#   __contains__zOrderedSet.__contains__  s    t|##r"   Iterator[T]c                N    t          | j                                                  S r<   )iterr;  keysrx   s    r#   __iter__zOrderedSet.__iter__  s    DL%%''(((r"   r   OrderedSet[T]c                D    | j                             |j                    d S r<   )r;  r   )rl   r   s     r#   r   zOrderedSet.update  s     EM*****r"   c                    d | j         |<   d S r<   rA  rB  s     r#   r   zOrderedSet.add  s    !Tr"   c                ^    t                      }| j                                        |_        |S r<   )r:  r;  r   )rl   rets     r#   r   zOrderedSet.copy  s%    '\\l''))
r"   rL   c                 ~    | d                                          }| dd          D ]}|                    |           |S )Nr   rH   )r   r   )rL   rM  rT   s      r#   unionzOrderedSet.union  s@    1gllnnabb 	 	AJJqMMMM
r"   otherc                8    t                               | |          S r<   )r:  rO  rl   rP  s     r#   __or__zOrderedSet.__or__  s    e,,,r"   r   c                0    |                      |           | S r<   )r   rR  s     r#   __ior__zOrderedSet.__ior__  s    Er"   objectc                    t          |t                    r| j        |j        k    S t          | j                                                  |k    S r<   )r   r:  r;  rj   rG  rR  s     r#   __eq__zOrderedSet.__eq__  sD    eZ(( 	5<5=00t|((**++u44r"   r<   )r<  r=  r&   rh   )r   r4   r&   rg   )r&   rD  )r   rI  r&   rh   )r   r4   r&   rh   )r&   rI  )rL   rI  r&   rI  )rP  rI  r&   rI  )rP  rI  r&   r   )rP  rV  r&   rg   )r   r   r   __annotations__rm   rC  rH  r   r   r   r8  rO  rS  rU  rX  r!   r"   r#   r:  r:    s         3 3 3 3 3$ $ $ $) ) ) )+ + + +" " " "   
    \- - - -   5 5 5 5 5 5r"   r:  )r$   r%   r&   r'   )r6   r7   r8   r9   r&   r:   )r6   r@   r8   r9   r&   r:   )rC   rD   r&   rE   )rP   r%   r&   r   )rT   r%   r&   rU   r<   )r   r   rc   r   r&   rb   )r   r   )r   r
   rK   rU   r   rU   r&   r%   )
r   r   rK   rU   r   rU   r   rU   r&   r%   )
r   r  rK   rU   r   rU   r   rU   r&   r%   )r  r  rK   rU   r   rU   r   rU   r	  r%   r
  r%   r&   r%   )<
__future__r   
contextlib	functoolsr[   r   r*   r   rJ   dataclassesr   enumr   r   pathlibr   pprintr	   typingr
   r   r   r   typing_extensionsr   r   torchgen.code_templater   argparser   collections.abcr   r   r   r   r   r   r   r   r   r   IDENT_REGEXr3   r4   r5   r?   rB   contextmanagerrO   cacherS   r`   rb   r   r   r   r  r   r  r:  r!   r"   r#   <module>ri     so   " " " " " "          				 				 



  $ $ $ $ $ $                     7 7 7 7 7 7 7 7 7 7 7 7 0 0 0 0 0 0 0 0 / / / / / /  G""""""FFFFFFFFFFFF X''))0 	
$ 
$ 
$ 
$ 
$T 
$ 
$ 
$  $ $ $ $ GCLLGCLL       	 	 	 	 / / / /
4 4 4 4SM SM SM SM SM SM SM SMr &*
 
 
 
 
  ' ' ' ' 'D D D D(F F F F- - - -&BG BG BG BG BG BG BG BGJ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5r"   