
    9jBB                       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#      jI                         jJ                  Z&e&jJ                  Z' G d de      Z(dZ)d"dZ* ed      Z+ ed      Z,d#dZ-d$dZ.ej^                  d%d       Z0ejb                  d&d       Z2d'dZ3 G d d      Z4	 d(	 	 	 	 	 d)dZ5	 	 d*	 	 	 	 	 	 	 d+dZ6	 	 	 	 	 	 	 	 	 	 d,dZ7	 	 	 	 	 	 	 	 	 	 d-dZ8	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.dZ9 G d d      Z: G d  d!ee+         Z;y)/    )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	y)TargetN)
__name__
__module____qualname__r   
DEFINITIONDECLARATIONREGISTRATIONANONYMOUS_DEFINITIONNAMESPACED_DEFINITIONNAMESPACED_DECLARATION     N/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torchgen/utils.pyr   r   %   s/    J&K6L6 F!Vr"   r   z(^|\W){}($|\W)c                    t        j                  d|       }|t        d|        |j                         \  }}}||j	                  d      fS )Nz(\w+)(\.\w+)?\((.*)\)zUnsupported function schema: , )rematchRuntimeErrorgroupssplit)schemamname_paramss        r#   split_name_paramsr0   8   sP    
)62Ay:6(CDDhhjOD!Vd###r"   TSc              #  8   K   |D ]  } | |      }||  y wNr!   )funcxsxrs       r#   mapMayber9   H   s(      G=Gs   c              #  >   K   |D ]  } | |      E d {     y 7 wr4   r!   )r5   r6   r7   s      r#   	concatMapr;   P   s%      7s   c              #     K   	 d  y # t         $ r]} |        }t        j                  |d      }|j                  r|j                  d    d| n|}|f|j                  dd  z   |_         d }~ww xY ww)Nz  r   
   )	Exceptiontextwrapindentargs)msg_fnemsgs      r#   contextrF   X   so      hooc4('(vv2cU#3!&&*$s%   A2	 A2	A/AA**A//A2c                ,    t        j                  |       S r4   )r   	from_file)template_fns    r#   _read_templaterJ   e   s    !!+..r"   c                    t        j                  | j                  d      d      j                         }t        j                  |d      S )Nlatin1F)usedforsecuritylittle)	byteorder)hashlibsha1encodedigestint
from_bytes)srQ   s     r#   string_stable_hashrW   k   s7    <<*EBIIKD>>$(>33r"   c                      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ddZy)FileManagerc                p    t        |      | _        t        |      | _        t               | _        || _        y r4   )r   install_dirtemplate_dirsetfilesdry_run)selfr[   r\   r_   s       r#   __init__zFileManager.__init__t   s.      , . #
r"   c                n    t        | j                  D ch c]  }|j                          c}      S c c}w r4   )	frozensetr^   as_posix)r`   files     r#   	filenameszFileManager.filenames   s%    djjAd$--/ABBAs   2c                    t        |      }d }	 |j                  d      }||k7  r1|j                  j	                  dd       |j                  |d       y y # t        $ r Y Bw xY w)Nzutf-8)encodingT)parentsexist_ok)r   	read_textOSErrorparentmkdir
write_text)r`   filenamecontentsre   old_contentss        r#   _write_if_changedzFileManager._write_if_changed   so    H~#'	>>7>;L |#KKdT:OOHwO7 $  		s   A 	A$#A$c                   t        |      j                         rt        d|       | j                  |z  } |       }t	        |t
              rd|vrwt        dz  }	 t        t        j                  d   j                  xs |      j                         }	 |j                  t              j                         }i |ddd| d| z   i}t%        |      }|j'                  |      }	t)        j*                  dd	|	t(        j,                  
      S t	        |t.              r|S t1        |       y # t        t        f$ r |j                         }Y w xY w# t         $ r |j"                  }Y w xY w)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_absoluteAssertionErrorr\   
isinstancedictTORCHGEN_ROOTsysmodules__file__absoluteKeyErrorAttributeErrorrelative_to	REPO_ROOTrd   
ValueErrorr-   rJ   
substituter&   subVERBOSEstrr   )
r`   rI   env_callabletemplate_pathenvgenerator_default	generatorgenerator_pathtemplatesubstitute_outs
             r#   substitute_with_templatez$FileManager.substitute_with_template   sr   
 ((* #A+!OPP))K7nc4 "#-$1H$<!= $J/88M<M!hj 4%.%:%:9%E%N%N%PN'n-=VK=QQ &m4H%005N 66 "jj  c3JSG !.1 = 1 : : <I=
 " 4%.^^N4s$   8D* #E *EEE'&E'c                H   t        |      }|j                         rt        d|       | j                  |z  }|| j                  v rt        d|       | j                  j                  |       | j                  s'| j                  ||      }| j                  ||       y y )Nfilename must be relative: zduplicate file write )rI   r   )rp   rq   )	r   ry   rz   r[   r^   addr_   r   rs   )r`   rp   rI   r   re   r   s         r#   write_with_templatezFileManager.write_with_template   s     >! #>xj!IJJ(*4:: #8!?@@

t||!::') ; N ""D>"J r"   c                *    | j                  |||       y r4   )r   )r`   rp   r   s      r#   writezFileManager.write   s    
 	  8\Br"   N)base_envc          
     6    | j                  ||||||||       y )N)key_fnr   
num_shardsr   sharded_keys)write_sharded_with_template)r`   rp   itemsr   r   r   r   r   s           r#   write_shardedzFileManager.write_sharded   s/     	((%!% 	) 		
r"   c                   t        |      }	|	j                         rt        d|       ddi}
t        |      D cg c]	  }dd| i }}|
g|z   }||D ]  j	                  |        D ]G  }|D ]@  |v r5t        |   t              st        d      |   j                         |<   <g |<   B I d	fd}| j                  rd}|D ]5  } ||      }t        |      |z  } ||      } |||   |        ||
|       7 |D ]:  d   }| j                  |	j                  |	j                   |       |fd       < | j                  j                  | j                  |	j                  |	j                   d      z         y c c}w )
Nr   shard_id
Everythingr.   z'sharded keys in base_env must be a listc                t    |j                         D ]$  \  }}|vrt        d|       | |xx   |z  cc<   & y )Nzundeclared sharded key )r   rz   )intofrom_kvr   s       r#   	merge_envz:FileManager.write_sharded_with_template.<locals>.merge_env  sD     1L((+B1#)FGGQ1r"   r!   c                      S r4   r!   )shards   r#   <lambda>z9FileManager.write_sharded_with_template.<locals>.<lambda>/  s     r"   )r   dict[str, list[str]]r   r   returnNone)r   ry   rz   rangeupdater{   listcopyr_   rW   r   	with_stemstemr^   discardr[   )r`   rp   rI   r   r   r   r   r   r   re   
everythingishards
all_shardskeyr   itemsidr   r   r   s           `           @r#   r   z'FileManager.write_sharded_with_template   s    H~ #>xj!IJJ&0,%?
+0+<(
&'Z1QC!(
 (
 !\F*
# 'X&'   	$C# $%<%eCj$7,-VWW!&s!2E#J!#E#J$	$	 <<E 	'D,C$S)J6Ct$CfSk3'j#&	'   	EZ(H$$$))XJ78	 	

4++dnn		{*=U.VVW[(
s   Fc                    dj                  d|gd t        | j                        D        d      }| j                  ||       y)zSWrite a file containing the list of all outputs which are generated by this script.r=   zset(c              3  D   K   | ]  }d |j                          d  yw)z    ""N)rd   ).0re   s     r#   	<genexpr>z,FileManager.write_outputs.<locals>.<genexpr><  s      LE$--/*!,Ls    )N)joinsortedr^   rs   )r`   variable_namerp   contents       r#   write_outputszFileManager.write_outputs5  sP    )) M

9KL	
 
 	x1r"   c                    t         j                  j                  | j                  t         j                  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relpathr\   dirnamer   r`   s    r#   template_dir_for_commentsz%FileManager.template_dir_for_commentsB  s,     wwt00"''//(2KLLr"   )r[   
str | Pathr\   r   r_   boolr   r   )r   zfrozenset[str])rp   r   rq   r   r   r   )rI   r   r   "Callable[[], str | dict[str, Any]]r   r   )rp   r   rI   r   r   r   r   r   )rp   r   r   r   r   r   )rp   r   r   Iterable[T]r   Callable[[T], str]r   #Callable[[T], dict[str, list[str]]]r   rT   r   dict[str, Any] | Noner   set[str]r   r   )rp   r   rI   r   r   r   r   r   r   r   r   rT   r   r   r   r   r   r   )r   r   rp   r   r   r   r   r   )r   r   r   ra   propertyrf   rs   r   r   r   r   r   r   r   r!   r"   r#   rY   rY   s   s   		 !	 		
 
	 C C
833 93 
	3jKK  K 9	K
 
K(CC 9C 
	C +/

 

 #
 :
 
 (
 
 

> +/=X=X  =X 	=X #=X :=X =X (=X =X 
=X~2Mr"   rY   c                    t         j                  j                  | j                  d      }|r|n| j                  }t        ||| j                        S )N	templates)r[   r\   r_   )r   r   r   source_pathr[   rY   r_   )optionsr[   r\   s      r#   make_file_managerr   L  sG     77<< 3 3[AL!,+'2E2EK! r"   c                    t        | ||      S r4   r   )objrA   widths      r#   dataclass_reprr   Z  s    
 3&&r"   c           
        ||dz   z  }g }| j                         D ]V  \  }}t        |      }t        |      rt        ||||t	        |      z         n
t        |      }|j                  | d|        X t        ||||dd      S )N   z: {})r   reprr   r	   lenappend_format)	attrrA   r   curr_indent	dict_reprr   r   k_reprv_strs	            r#   _format_dictr   b  s     6A:KI

 /1a A AvukCK&?@a 	
 	F82eW-./ 9fe[#sCCr"   c           	         ||dz   z  }| D cg c]&  }t        |      rt        ||||      n
t        |      ( }}t        | t              rdnd\  }}t        ||||||      S c c}w )Nr>   )[])(r   )r   r	   r   r{   r   r   )r   rA   r   r   l	list_reprstartends           r#   _format_listr   v  s{     6A:K  3?q/65+.tAwNI   *$5:JE39fe[%EEs   +A!c                    d\  }}t        t        |             |k\  rd}d|z  }d|z  }d| | j                  |       }	| | |	 | S )N) r   r=    r%   )r   r   r   )

fields_strrA   r   r   r   r   	delimitercurr_indent_str
indent_strbodys
             r#   r   r     sk     "(I
4
%	+vJ	{?+,11*=DWZLse,,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y)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
    c                T   |j                  d      }t        |      |kD  rt        d| d| d      || _        dj	                  |D cg c]  }d| d
 c}      | _        dj	                  t        |      D cg c]  }d| 	 c}      | _        || _        || _	        y c c}w c c}w )	N::z"Codegen doesn't support more than z# level(s) of custom namespace. Got .r=   z
namespace z {z} // namespace )
r*   r   rz   cpp_namespace_r   	prologue_reversed	epilogue_namespaces_entity_name_)r`   namespace_strentity_name	max_levelcpp_namespacesns         r#   ra   zNamespaceHelper.__init__  s     ',,T2~* 4YK @))6q:  ,#PAj3$7#PQ-5n-EFs#F
 *' $QFs   B 9B%c                r    | j                  d      }|d   }dj                  |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  )namespaced_entityr  namesr  r  s        r#   from_namespaced_entityz&NamespaceHelper.from_namespaced_entity  sE     "''-Bi		%*-'[I
 	
r"   c                    | j                   S r4   )r	  r   s    r#   prologuezNamespaceHelper.prologue      ~~r"   c                    | j                   S r4   )r  r   s    r#   epiloguezNamespaceHelper.epilogue  r  r"   c                    | j                   S r4   )r  r   s    r#   r  zNamespaceHelper.entity_name  s       r"   c                6    | j                   r| j                   S |S )z
        Return the namespace string from joining all the namespaces by "::" (hence no leading "::").
        Return default if namespace string is empty.
        )r  )r`   defaults     r#   get_cpp_namespacez!NamespaceHelper.get_cpp_namespace  s    
 '+&9&9t""FwFr"   N)r      )r  r   r  r   r  rT   r   r   )r!  )r  r   r  rT   r   r  r   )r   )r  r   r   r   )r   r   r   __doc__ra   staticmethodr  r   r  r  r  r   r!   r"   r#   r  r    s      	(( ( 	(
 
(*  


 

 
     ! !Gr"   r  c                  t    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y)
OrderedSetzdict[T, None]storageNc                L    |i | _         y t        j                  |      | _         y r4   )r&  r|   fromkeys)r`   iterables     r#   ra   zOrderedSet.__init__  s    DL==2DLr"   c                    || j                   v S r4   r&  r`   r   s     r#   __contains__zOrderedSet.__contains__  s    t||##r"   c                H    t        | j                  j                               S r4   )iterr&  keysr   s    r#   __iter__zOrderedSet.__iter__  s    DLL%%'((r"   c                N    | j                   j                  |j                          y r4   )r&  r   )r`   r   s     r#   r   zOrderedSet.update  s    EMM*r"   c                "    d | j                   |<   y r4   r+  r,  s     r#   r   zOrderedSet.add  s    !Tr"   c                X    t               }| j                  j                         |_        |S r4   )r%  r&  r   )r`   rets     r#   r   zOrderedSet.copy  s"    '\ll'')
r"   c                 b    | d   j                         }| dd  D ]  }|j                  |        |S )Nr   r>   )r   r   )rB   r5  rV   s      r#   unionzOrderedSet.union  s6    1gllnab 	AJJqM	
r"   c                .    t         j                  | |      S r4   )r%  r7  r`   others     r#   __or__zOrderedSet.__or__   s    e,,r"   c                (    | j                  |       | S r4   )r   r9  s     r#   __ior__zOrderedSet.__ior__  s    Er"   c                    t        |t              r| j                  |j                  k(  S t        | j                  j	                               |k(  S r4   )r{   r%  r&  r]   r0  r9  s     r#   __eq__zOrderedSet.__eq__  s<    eZ(<<5==00t||((*+u44r"   r4   )r)  zIterable[T] | Noner   r   )r   r1   r   r   )r   zIterator[T])r   OrderedSet[T]r   r   )r   r1   r   r   )r   r@  )rB   r@  r   r@  )r:  r@  r   r@  )r:  r@  r   r   )r:  objectr   r   )r   r   r   __annotations__ra   r-  r1  r   r   r   r#  r7  r;  r=  r?  r!   r"   r#   r%  r%    sI    3$)+"
  -5r"   r%  )r+   r   r   ztuple[str, list[str]])r5   zCallable[[T], S | None]r6   r   r   Iterator[S])r5   zCallable[[T], Sequence[S]]r6   r   r   rC  )rC   zCallable[[], str]r   zIterator[None])rI   r   r   r   )rV   r   r   rT   r4   )r   r   r[   zstr | Path | Noner   rY   )r   P   )r   r
   rA   rT   r   rT   r   r   )
r   zdict[Any, Any]rA   rT   r   rT   r   rT   r   r   )
r   z&list[Any] | set[Any] | tuple[Any, ...]rA   rT   r   rT   r   rT   r   r   )r   z	list[str]rA   rT   r   rT   r   rT   r   r   r   r   r   r   )<
__future__r   
contextlib	functoolsrP   r   r&   r~   r@   dataclassesr   enumr   r   pathlibr   pprintr	   typingr
   r   r   r   typing_extensionsr   r   torchgen.code_templater   argparser   collections.abcr   r   r   r   r   r   rm   r}   r   r   IDENT_REGEXr0   r1   r2   r9   r;   contextmanagerrF   cacherJ   rW   rY   r   r   r   r   r   r  r%  r!   r"   r#   <module>rT     s   "    	 	 
  $    7 7 0 / "FF X'')00  	
$T 
$  $ CLCL 	 	 / /
4UM UMv &*

"
 
  '	'' ' 		'D
DD D 	D
 	D(F
0FF F 	F
 	F--- - 	-
 - 
- 	-&DG DGN,5 ,5r"   