
    %h>                        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mZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlm Z  d dl!m"Z"  G d d	e          Z#d
Z$de%dee%ee%         f         fdZ& ed          Z' ed          Z(dee'gee(         f         dee'         dee(         fdZ)dee'gee(         f         dee'         dee(         fdZ*e j+        deg e%f         ded         fd            Z,dedefdZ- ej.        d          de%de"fd            Z/de%de0fdZ1 G d d          Z2	 d5ded ee%         de2fd!Z3	 	 d6d#ed$e0d%e0de%fd&Z4	 d7d#ed$e0d%e0d'e0de%f
d(Z5d)eeef         d$e0d%e0d'e0de%f
d*Z6d)eee         ee         eed+f         f         d$e0d%e0d'e0de%f
d,Z7d-ee%         d$e0d%e0d'e0d.e%d/e%de%fd0Z8 G d1 d2          Z9 G d3 d4ee'                   Z:dS )8    N)	Namespace)fieldsis_dataclass)autoEnum)AnyCallableDictGenericIterableIteratorListLiteralNoReturnOptionalSequenceSetTupleTypeVarUnion)Self)CodeTemplatec                       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     N/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/torchgen/utils.pyr   r   *   sU        J$&&K466L466 DFF!TVVr%   r   z(^|\W){}($|\W)schemareturnc                     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_paramsr4   =   sZ    
)622AyC6CCDDDhhjjOD!Vd####r%   TSfuncxsc              #   6   K   |D ]} | |          }||V  d S Nr$   )r7   r8   xrs       r&   mapMayber=   M   s;        DGG=GGG r%   c              #   6   K   |D ]} | |          E d {V  d S r:   r$   )r7   r8   r;   s      r&   	concatMapr?   U   sB        477 r%   msg_fnc              #      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)r@   emsgs      r&   contextrJ   ]   s         fhhoc4(('(v6##c###3!&*$s   
 
A,AA''A,r;   c                 J    t          dt          |           j                   )NzUnhandled type: )AssertionErrortyper   )r;   s    r&   assert_neverrN   m   s"    
>DGG,<>>
?
??r%   )maxsizetemplate_fnc                 *    t          j        |           S r:   )r   	from_file)rP   s    r&   _read_templaterS   q   s    !+...r%   sc                     t          j        |                     d                                                    }t                              |d          S )Nlatin1little)	byteorder)hashlibsha1encodedigestint
from_bytes)rT   rZ   s     r&   string_stable_hashr_   w   s?    <**++2244D>>$(>333r%   c                      e Zd ZU eed<   eed<   eed<   ee         ed<   dedededdfdZded	eddfd
Zdede	g e
eeeef         f         f         defdZdedede	g e
eeeef         f         f         ddfdZdede	g e
eeeef         f         f         ddfdZdddedee         de	egef         de	egeeee         f         f         dedeeeef                  dee         ddfdZdededdfdZdefdZdS )FileManagerinstall_dirtemplate_dirdry_run	filenamesr(   Nc                 V    || _         || _        t                      | _        || _        d S r:   )rb   rc   setre   rd   )selfrb   rc   rd   s       r&   __init__zFileManager.__init__   s(    &(r%   filenamecontentsc                    	 t          |          5 }|                                }d d d            n# 1 swxY w Y   n# t          $ r d }Y nw xY w||k    rst          j        t          j                            |          d           t          |d          5 }|                    |           d d d            d S # 1 swxY w Y   d S d S )NT)exist_okw)openreadOSErrorosmakedirspathdirnamewrite)rh   rj   rk   fold_contentss        r&   _write_if_changedzFileManager._write_if_changed   sP   	 h (1 vvxx( ( ( ( ( ( ( ( ( ( ( ( ( ( ( 	  	  	 LLL	 |##K11DAAAAh$$ "!!!" " " " " " " " " " " " " " " " " " $#s9   > 2> 6> 6> AAB<<C C rP   env_callablec                    t           j                            | j        |          } |            }t	          |t
                    rTd|vr,d}|dt           j                            |           z  }||d<   t          |          }|                    |          S t	          |t                    r|S t          |           d S )Ngenerated_commentz@generated by torchgen/gen.pyz from )rr   rt   joinrc   
isinstancedictbasenamerS   
substitutestrrN   )rh   rP   rz   template_pathenvcommenttemplates          r&   substitute_with_templatez$FileManager.substitute_with_template   s     T%6DDlnnc4   	"#-->EBG$4$4]$C$CEEE+2'(%m44H&&s+++S!! 	Jr%   c                     | j          d| }|| j        vs
J d            | j                            |           | j        s0|                     ||          }|                     ||           d S d S )N/zduplicate file write {filename})rP   rz   )rj   rk   )rb   re   addrd   r   ry   )rh   rj   rP   rz   substitute_outs        r&   write_with_templatezFileManager.write_with_template   s     &3333t~---/P---8$$$| 	O!::') ;  N ""H~"NNNNN	O 	Or%   c                 4    |                      |||           d S r:   )r   )rh   rj   rz   s      r&   rv   zFileManager.write   s"    
 	  8\BBBBBr%   )base_envitemskey_fn
num_shardsr   sharded_keysc                   ddi}d t          |          D             }	|g|	z   }
||
D ]                    |           D ]S}|
D ]N|v rCt          |         t                    s
J d            |                                         |<   Ig |<   OTdt
          t          t          t                   f         dt
          t          t          t                   f         dd ffd}| j        rd	}|D ]H} ||          }t          |          |z  } ||          } ||	|         |            |||           I|
                    d
          }|dk    rt          |          }|d |         }||d          }|
D ]*d         }|                     | | | |fd           +| j                            | j         d| d|            d S )Nshard_id
Everythingc                     g | ]	}d d| i
S )r   r2   r$   ).0is     r&   
<listcomp>z-FileManager.write_sharded.<locals>.<listcomp>   s0     (
 (
 (
&'ZQ!(
 (
 (
r%   z'sharded keys in base_env must be a listintofrom_r(   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.<locals>.merge_env   sa      1L(((*GA*G*G(((Q1 r%   r$   .c                       S r:   r$   )shards   r&   <lambda>z+FileManager.write_sharded.<locals>.<lambda>   s    5 r%   r   )rangeupdater~   listcopyr
   r   r   rd   r_   rfindlenr   re   discardrb   )rh   rj   r   r   rz   r   r   r   
everythingshards
all_shardskeyr   itemsidr   dot_posbase_filename	extensionr   r   s          `            @r&   write_shardedzFileManager.write_sharded   s    '1,%?
(
 (
+0+<+<(
 (
 (
 !\F*
# ' 'X&&&& 	$ 	$C# $ $%<<%c
D  A A@A A  "'s!2!2E#JJ!#E#JJ$	Dd3i0 	c49n9M 	RV 	 	 	 	 	 	
 < 	E 	' 	'D&,,C$S))J6C,t$$CIfSk3'''Ij#&&&&..%%b==(mmG '*WXX&	 	 	EZ(H$$ 7(7I77====   
 	FF-FF9FF	
 	
 	
 	
 	
r%   variable_namec           	          d                     |d                    d t          | j                  D                                 }|                     ||           dS )z[Write a file containing the list of all outputs which are
        generated by this script.zset({}
    {})z
    c              3   &   K   | ]}d |z   d z   V  dS )"Nr$   )r   r1   s     r&   	<genexpr>z,FileManager.write_outputs.<locals>.<genexpr>  s+      NNt#*s*NNNNNNr%   N)formatr}   sortedre   ry   )rh   r   rj   contents       r&   write_outputszFileManager.write_outputs  sb     $**MMNNvdn7M7MNNNNN
 
 	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.
        )rr   rt   relpathrc   ru   __file__rh   s    r&   template_dir_for_commentsz%FileManager.template_dir_for_comments  s+     wt0"'//(2K2KLLLr%   )r   r   r   r   __annotations__boolr   ri   ry   r	   r   r
   r   r   r   rv   r   r5   r   r]   r   r   r   r   r$   r%   r&   ra   ra      s        MMM3xC s T d    "# " " " " " ".6r5d3PS8nAT;U7U.V	   $OO O r5d38n)<#==>	O
 
O O O O CC r5d38n)<#==>C 
	C C C C .2?
 ?
 ?
?
 {?

 !c"?
 sDd3i$889?
 ?
 4S>*?
 #h?
 
?
 ?
 ?
 ?
B23 2# 2$ 2 2 2 2M3 M M M M M Mr%   ra   optionsrb   c                     t           j                            | j        d          }|r|n| j        }t          ||| j                  S )N	templates)rb   rc   rd   )rr   rt   r}   source_pathrb   ra   rd   )r   rb   rc   s      r&   make_file_managerr     sM     7<< 3[AAL!,E++'2EKlGO   r%   P   objrF   widthc                 l    t           j        dk    rddlm}  || ||          S t	          | ||          S )N)   
   r   )pformat)rF   r   )sysversion_infopprintr   _pformat)r   rF   r   r   s       r&   dataclass_reprr   !  sM     7""""""""wsFE***Ce4444r%   curr_indentc                     t                     sJ dt                                   j        j        }|t	          |          dz   z  } fdt                     D             }g }|D ]\  }}|t	          |          z   dz   }	t          |          rt          ||||	          }
nlt          |t                    rt          ||||	          }
nDt          |t          t          t          f          rt          ||||	          }
nt          |          }
|                    | d|
            |dz  }d|                     |          }| d| dS )	Nz%obj should be a dataclass, received: rC   c                 V    g | ]%}|j         	|j        t          |j                  f&S r$   )reprr1   getattr)r   rw   r   s     r&   r   z_pformat.<locals>.<listcomp>;  s3    QQQa!&QAFGC001QQQr%   = z,
())r   rM   	__class__r   r   r   r   r~   r   _format_dictr   rg   tuple_format_listr   appendr}   )r   rF   r   r   
class_namefields_list
fields_strr1   attr_curr_indentstr_repr
indent_strbodys   `            r&   r   r   /  s    QQQd3iiQQQQ'J3z??Q&&KQQQQ6#;;QQQKJ! 0 0
d #SYY.2 	"fe\BBHHd## 	"#D&%FFHHtS%011 	"#D&%FFHHDzzHT..H..////s"J"":..D""4""""r%   r   c           
      D   ||dz   z  }g }|                                  D ]n\  }}t          |          }t          |          r"t          ||||t	          |          z             nt          |          }|                    | d|            ot          ||||dd          S )Nr   z: {})r   r   r   r   r   r   _format)	r   rF   r   r   	dict_reprr   r   k_reprv_strs	            r&   r   r   R  s     6A:KI

 / /1a AHQ{S[['@AAAa 	
 	F--e--....9fe[#sCCCr%   .c                     dz   z  fd| D             }t          | t                    rdnd\  }}t          |||          S )NrC   c                 p    g | ]2}t          |          rt          |          nt          |          3S r$   )r   r   r   )r   lr   rF   r   s     r&   r   z _format_list.<locals>.<listcomp>m  sN        4@??OFE;///Q  r%   )[])r   r   )r~   r   r   )r   rF   r   r   	list_reprstartends    ```   r&   r   r   f  s}     6A:K       I  *$55E:JE39fe[%EEEr%   r   r   r   c                     d\  }}t          t          |                     |k    rd}d|z  }d|z  }d| |                     |           }	| | |	 | S )N) r   rB   r   r*   )r   r   r}   )
r   rF   r   r   r   r   	delimitercurr_indent_strr   r   s
             r&   r   r   u  s|     "(I
4
%%	+vJ,	,?,,11*==D,Z,,s,,,r%   c                       e Zd ZdZddededefdZe	 ddeded	d fd
            Ze	d	efd            Z
e	d	efd            Ze	d	efd            Zdded	ef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_strentity_name	max_levelc                 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 r   rB   c                     g | ]}d | d	S )z
namespace z {r$   r   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_)rh   r  r  r  cpp_namespacess        r&   ri   zNamespaceHelper.__init__  s    &,,T229,,,n	nn^knnn -,,+#P#P#P#P#PQQFFXn-E-EFFF
 
 *'r%   namespaced_entityr(   c                     |                      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  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  r   s    r&   prologuezNamespaceHelper.prologue  
    ~r%   c                     | j         S r:   )r  r   s    r&   epiloguezNamespaceHelper.epilogue  r  r%   c                     | j         S r:   )r  r   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  )rh   r  s     r&   get_cpp_namespacez!NamespaceHelper.get_cpp_namespace  s    
 '+&9Ft""wFr%   N)r   r  )r  )r   )r   r   r   __doc__r   r]   ri   staticmethodr  propertyr  r  r  r  r$   r%   r&   r  r    s1        ( (c ( (S ( ( ( ( 12
 

+.
	
 
 
 \
 #    X #    X !S ! ! ! X!G G Gc G G G G G Gr%   r  c                       e Zd ZU eeed         f         ed<   ddeee                  fdZ	dede
fdZdee         fdZddZdeddfdZddZedd            ZddZdd
defdZdede
fdZdS )
OrderedSetNstorageiterablec                 >    |	i | _         d S d |D             | _         d S )Nc                     i | ]}|d S r:   r$   )r   r   s     r&   
<dictcomp>z'OrderedSet.__init__.<locals>.<dictcomp>  s    666At666r%   r%  )rh   r&  s     r&   ri   zOrderedSet.__init__  s,    DLLL66X666DLLLr%   r   r(   c                     || j         v S r:   r*  rh   r   s     r&   __contains__zOrderedSet.__contains__  s    t|##r%   c                 N    t          | j                                                  S r:   )iterr%  keysr   s    r&   __iter__zOrderedSet.__iter__  s    DL%%''(((r%   r   OrderedSet[T]c                 D    | j                             |j                    d S r:   )r%  r   )rh   r   s     r&   r   zOrderedSet.update  s     EM*****r%   c                     d | j         |<   d S r:   r*  r,  s     r&   r   zOrderedSet.add  s    !Tr%   c                 ^    t                      }| j                                        |_        |S r:   )r$  r%  r   )rh   rets     r&   r   zOrderedSet.copy  s%    '\\l''))
r%   rG   c                  ~    | d                                          }| dd          D ]}|                    |           |S )Nr   rC   )r   r   )rG   r6  rT   s      r&   unionzOrderedSet.union  s@    1gllnnabb 	 	AJJqMMMM
r%   otherc                 8    t                               | |          S r:   )r$  r8  rh   r9  s     r&   __or__zOrderedSet.__or__  s    e,,,r%   c                 0    |                      |           | S r:   )r   r;  s     r&   __ior__zOrderedSet.__ior__  s    Er%   c                     t          |t                    r| j        |j        k    S t          | j                                                  |k    S r:   )r~   r$  r%  rg   r0  r;  s     r&   __eq__zOrderedSet.__eq__  sD    eZ(( 	5<5=00t|((**++u44r%   r:   )r   r2  r(   N)r(   r2  )rG   r2  r(   r2  )r9  r2  r(   r2  )r   r   r   r
   r5   r   r   r   r   ri   r   r-  r   r1  r   r   r   r!  r8  r<  r   r>  objectr@  r$   r%   r&   r$  r$    s\        !WT]"####7 7(1+!6 7 7 7 7$ $t $ $ $ $)(1+ ) ) ) )+ + + +" "d " " " "   
    \- - - -_     5F 5t 5 5 5 5 5 5r%   r$  r:   )r   r   )r   );
contextlib	functoolsrY   rr   r+   r   rE   argparser   dataclassesr   r   enumr   r   typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   torchgen.code_templater   r   IDENT_REGEXr   r4   r5   r6   r=   r?   contextmanagerrJ   rN   	lru_cacherS   r]   r_   ra   r   r   r   r   r   r   r  r$  r$   r%   r&   <module>rM     s            				 				 



        , , , , , , , ,                                         $ # " " " " " / / / / / /
$ 
$ 
$ 
$ 
$T 
$ 
$ 
$  $c $eCcN&; $ $ $ $ GCLLGCLL8QC!,- 8A; 8A;    HaS(1+-. HQK HQK     	HRW% 	(4. 	 	 	 	@H @ @ @ @ @ T"""/ / / / / #"/
4# 4# 4 4 4 4SM SM SM SM SM SM SM SMp 6: %-c]    5 5	55 5 		5 5 5 5$ 	 #  #	 # #  # 	 #
 	 #  #  #  #FD
sCx.DD D 	D
 	D D D D(F
S	3s8U38_4
5FF F 	F
 	F F F F-S	-- - 	-
 - 
- 	- - - -&<G <G <G <G <G <G <G <G~,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5r%   