o
    8} i"C                     @  s  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
mZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZ d dlmZ d dlmZ erhd d	lmZ d d
lm Z m!Z!m"Z" ee#$ j%j%Z&G dd deZ'dZ(dTddZ)edZ*edZ+dUddZ,dVddZ-ej.dWd#d$Z/dXd'd(Z0ej1dYd+d,Z2dZd/d0Z3G d1d2 d2Z4	d[d\d7d8Z5	 	9d]d^d>d?Z6	 d_d`dAdBZ7dadEdFZ8dbdHdIZ9dcdNdOZ:G dPdQ dQZ;G dRdS dSee* Z<dS )d    )annotationsN)fieldsis_dataclass)autoEnum)Path)AnyCallableGenericLiteralNoReturnTYPE_CHECKINGTypeVar)Self)CodeTemplate)	Namespace)IterableIteratorSequencec                   @  s0   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 r   r   0/home/app/PyTorch/test/pytorch/torchgen/utils.pyr   #   s    
r   z(^|\W){}($|\W)schemastrreturntuple[str, list[str]]c                 C  s>   t d| }|d u rtd|  | \}}}||dfS )Nz(\w+)(\.\w+)?\((.*)\)zUnsupported function schema: , )rematchRuntimeErrorgroupssplit)r!   mname_paramsr   r   r    split_name_params6   s
   r/   TSfuncCallable[[T], S | None]xsIterable[T]Iterator[S]c                 c  s&    |D ]}| |}|d ur|V  qd S Nr   )r2   r4   xrr   r   r    mapMaybeF   s   r:   Callable[[T], Sequence[S]]c                 c  s    |D ]	}| |E d H  qd S r7   r   )r2   r4   r8   r   r   r    	concatMapN   s   r<   msg_fnCallable[[], str]Iterator[None]c              
   c  sp    zd V  W d S  t y7 } z$|  }t|d}|jr%|jd  d| n|}|f|jdd   |_ d }~ww )Nz  r   
   )	Exceptiontextwrapindentargs)r=   emsgr   r   r    contextV   s   rH   r8   r   c                 C  s   t dt| j )NzUnhandled type: )AssertionErrortyper   )r8   r   r   r    assert_neverf   s   rK   template_fnr   c                 C  s
   t | S r7   )r   Z	from_file)rL   r   r   r    _read_templatej   s   
rM   sintc                 C  s&   t j| ddd }tj|ddS )Nlatin1F)Zusedforsecuritylittle)	byteorder)hashlibsha1encodeZdigestrO   
from_bytes)rN   rT   r   r   r    string_stable_hashp   s   rW   c                   @  s   e Zd ZU ded< ded< ded< ded< d.d
dZd/ddZd0ddZd1ddZd2ddZddd3d%d&Z	ddd4d'd(Z
d5d*d+Zd6d,d-ZdS )7FileManagerr"   install_dirtemplate_dirbooldry_runset[str]	filenamesr#   Nonec                 C  s   || _ || _t | _|| _d S r7   )rY   rZ   setr^   r\   )selfrY   rZ   r\   r   r   r    __init__~   s   
zFileManager.__init__filenamecontentsc                 C  s   zt |}| }W d    n1 sw   Y  W n ty%   d }Y nw ||krRtjtj|dd t |d}|| W d    d S 1 sKw   Y  d S d S )NT)exist_okw)openreadOSErrorosmakedirspathdirnamewrite)ra   rc   rd   fZold_contentsr   r   r    _write_if_changed   s   

"zFileManager._write_if_changedrL   env_callable"Callable[[], str | dict[str, Any]]c           	   	   C  s   t j| j|}| }t|trgd|vr^td d }zttj	d j
p#| }W n ttfy7   | }Y nw z	|t }W n tyM   |j}Y nw i |ddd| d|  i}t|}||S t|trn|S t| d S )NZgenerated_commenttorchgenzgen.py__main__@zgenerated by z from )rj   rl   joinrZ   
isinstancedict	REPO_ROOTr   sysmodules__file__absoluteKeyErrorAttributeErrorrelative_toas_posix
ValueErrorr,   rM   Z
substituter"   rK   )	ra   rL   rq   Ztemplate_pathenvZgenerator_default	generatorZgenerator_pathtemplater   r   r    substitute_with_template   s<   



z$FileManager.substitute_with_templatec                 C  sX   | j  d| }|| jvsJ d| j| | js*| j||d}| j||d d S d S )N/zduplicate file write {filename})rL   rq   )rc   rd   )rY   r^   addr\   r   rp   )ra   rc   rL   rq   Zsubstitute_outr   r   r    write_with_template   s   zFileManager.write_with_templatec                 C  s   |  ||| d S r7   )r   )ra   rc   rq   r   r   r    rn      s   zFileManager.writeN)base_envitemsr5   key_fnCallable[[T], str]#Callable[[T], dict[str, list[str]]]
num_shardsrO   r   dict[str, Any] | Nonesharded_keysc             
   C  s   | j ||||||||d d S )N)r   rq   r   r   r   )write_sharded_with_template)ra   rc   r   r   rq   r   r   r   r   r   r    write_sharded   s   
zFileManager.write_shardedc                  sj  ddi}	dd t |D }
|	g|
 }|d ur |D ]  | qD ]#}|D ] | v r@t | ts7J d |   |< q&g  |< q&q"dfdd}| jrRd}|D ]}||}t|| }||}||
| | ||	| qT|d}|dkr~t|}|d | }||d  }|D ]  d }| 	| | | | fdd q| j
| j d| d|  d S )Nshard_idZ
Everythingc                 S  s   g | ]	}d d| iqS )r   r-   r   ).0ir   r   r    
<listcomp>   s    z;FileManager.write_sharded_with_template.<locals>.<listcomp>z'sharded keys in base_env must be a listintodict[str, list[str]]from_r#   r_   c                   s<   |  D ]\}}| v sJ d| | |  |7  < qd S )Nzundeclared sharded key )r   )r   r   kv)r   r   r    	merge_env   s   z:FileManager.write_sharded_with_template.<locals>.merge_envr   .c                     s    S r7   r   r   )shardr   r    <lambda>  s    z9FileManager.write_sharded_with_template.<locals>.<lambda>r   )r   r   r   r   r#   r_   )rangeupdaterw   listcopyr\   rW   rfindlenr   r^   discardrY   )ra   rc   rL   r   r   rq   r   r   r   Z
everythingZshardsZ
all_shardskeyr   itemZsidr   Zdot_posbase_filename	extensionr   r   )r   r   r    r      sR   

	

z'FileManager.write_sharded_with_templatevariable_namec                 C  s2   d |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                 s  s    | ]	}d | d  V  qdS )"Nr   )r   r,   r   r   r    	<genexpr>)  s    z,FileManager.write_outputs.<locals>.<genexpr>N)formatrv   sortedr^   rp   )ra   r   rc   contentr   r   r    write_outputs$  s
   zFileManager.write_outputsc                 C  s   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.
        )rj   rl   relpathrZ   rm   r|   ra   r   r   r    template_dir_for_comments-  s   z%FileManager.template_dir_for_comments)rY   r"   rZ   r"   r\   r[   r#   r_   )rc   r"   rd   r"   r#   r_   )rL   r"   rq   rr   r#   r"   )rc   r"   rL   r"   rq   rr   r#   r_   )rc   r"   rq   rr   r#   r_   )rc   r"   r   r5   r   r   rq   r   r   rO   r   r   r   r]   r#   r_   )rc   r"   rL   r"   r   r5   r   r   rq   r   r   rO   r   r   r   r]   r#   r_   )r   r"   rc   r"   r#   r_   r#   r"   )r   r   r   __annotations__rb   rp   r   r   rn   r   r   r   r   r   r   r   r    rX   x   s    
 



!

D	rX   optionsr   rY   
str | Nonec                 C  s.   t j| jd}|r|n| j}t||| jdS )NZ	templates)rY   rZ   r\   )rj   rl   rv   source_pathrY   rX   r\   )r   rY   rZ   r   r   r    make_file_manager7  s
   r   P   objr   rD   widthc                 C  s0   t jdkrddlm} || ||S t| ||dS )N)   
   r   )pformat)rD   r   )rz   version_infoZpprintr   _pformat)r   rD   r   r   r   r   r    dataclass_reprB  s   
r   curr_indentc                   s   t  sJ dt   jj}|t|d 7 } fddt D }g }|D ]C\}}|t| d }	t |r@t||||	}
n!t|trMt	||||	}
nt|t
ttfr]t||||	}
nt|}
|| d|
  q(|d }d| |}| d| d	S )
Nz%obj should be a dataclass, received: rA   c                   s$   g | ]}|j r|jt |jfqS r   )reprr,   getattr)r   ro   r   r   r    r   \  s   $ z_pformat.<locals>.<listcomp>= z,
())r   rJ   	__class__r   r   r   r   rw   rx   _format_dictr   r`   tuple_format_listr   appendrv   )r   rD   r   r   
class_nameZfields_list
fields_strr,   attrZ_curr_indentZstr_repr
indent_strbodyr   r   r    r   P  s$   
r   r   dict[Any, Any]c           	      C  sv   ||d 7 }g }|   D ]%\}}t|}t|r#t||||t| nt|}|| d|  qt||||ddS )Nr   z: {})r   r   r   r   r   r   _format)	r   rD   r   r   Z	dict_reprr   r   Zk_reprZv_strr   r   r    r   s  s   r   &list[Any] | set[Any] | tuple[Any, ...]c                   sJ    d 7   fdd| D }t | trdnd\}}t| ||S )NrA   c                   s*   g | ]}t |rt| nt|qS r   )r   r   r   )r   lr   rD   r   r   r    r     s    z _format_list.<locals>.<listcomp>)[])r   r   )rw   r   r   )r   rD   r   r   Z	list_reprstartendr   r   r    r     s   r   r   	list[str]r   r   c           
      C  sT   d\}}t t| |krd}d| }d| }d| | | }	| | |	 | S )N) r   r@   r   r%   )r   r   rv   )
r   rD   r   r   r   r   Z	delimiterZcurr_indent_strr   r   r   r   r    r     s   r   c                   @  sf   e Zd ZdZ	ddddZe	ddddZedddZedddZ	edddZ
d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_levelrO   r#   r_   c                 C  sr   | d}t||ksJ d| d| 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   r@   c                 S  s   g | ]}d | dqS )z
namespace z {r   r   nr   r   r    r     s    z,NamespaceHelper.__init__.<locals>.<listcomp>c                 S  s   g | ]}d | qS )z} // namespace r   r   r   r   r    r     s    )	r*   r   cpp_namespace_rv   	prologue_reversed	epilogue_Znamespaces_entity_name_)ra   r   r   r   Zcpp_namespacesr   r   r    rb     s   

zNamespaceHelper.__init__namespaced_entityc                 C  s2   |  d}|d }d|dd }t|||d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*   rv   r   )r   r   namesr   r   r   r   r    from_namespaced_entity  s   
z&NamespaceHelper.from_namespaced_entityc                 C     | j S r7   )r   r   r   r   r    prologue     zNamespaceHelper.prologuec                 C  r   r7   )r   r   r   r   r    epilogue  r   zNamespaceHelper.epiloguec                 C  r   r7   )r   r   r   r   r    r     r   zNamespaceHelper.entity_namedefaultc                 C  s   | 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   )ra   r   r   r   r    get_cpp_namespace  s   z!NamespaceHelper.get_cpp_namespaceN)r   r   )r   r"   r   r"   r   rO   r#   r_   )r   )r   r"   r   rO   r#   r   r   )r   )r   r"   r#   r"   )r   r   r   __doc__rb   staticmethodr   propertyr   r   r   r   r   r   r   r    r     s    r   c                   @  s   e Zd ZU ded< d&d'dd	Zd(ddZd)ddZd*ddZd+ddZd,ddZ	e
d-ddZd.ddZd/d!d"Zd0d$d%ZdS )1
OrderedSetzdict[T, Literal[None]]storageNiterableIterable[T] | Noner#   r_   c                 C  s"   |d u r	i | _ d S t|| _ d S r7   )r   rx   fromkeys)ra   r   r   r   r    rb     s   
zOrderedSet.__init__r   r0   r[   c                 C  s
   || j v S r7   r   ra   r   r   r   r    __contains__  s   
zOrderedSet.__contains__Iterator[T]c                 C  s   t | j S r7   )iterr   keysr   r   r   r    __iter__     zOrderedSet.__iter__r   OrderedSet[T]c                 C  s   | j |j  d S r7   )r   r   )ra   r   r   r   r    r     s   zOrderedSet.updatec                 C  s   d | j |< d S r7   r  r  r   r   r    r     r
  zOrderedSet.addc                 C  s   t  }| j |_|S r7   )r   r   r   )ra   retr   r   r    r     s   zOrderedSet.copyrE   c                  G  s,   | d   }| dd  D ]}|| q|S )Nr   rA   )r   r   )rE   r  rN   r   r   r    union  s   zOrderedSet.unionotherc                 C  s   t | |S r7   )r   r  ra   r  r   r   r    __or__  s   zOrderedSet.__or__r   c                 C  s   |  | | S r7   )r   r  r   r   r    __ior__  s   
zOrderedSet.__ior__objectc                 C  s(   t |tr| j|jkS t| j |kS r7   )rw   r   r   r`   r  r  r   r   r    __eq__  s   
zOrderedSet.__eq__r7   )r   r  r#   r_   )r   r0   r#   r[   )r#   r  )r   r  r#   r_   )r   r0   r#   r_   )r#   r  )rE   r  r#   r  )r  r  r#   r  )r  r  r#   r   )r  r  r#   r[   )r   r   r   r   rb   r  r	  r   r   r   r   r  r  r  r  r   r   r   r    r     s   
 






r   )r!   r"   r#   r$   )r2   r3   r4   r5   r#   r6   )r2   r;   r4   r5   r#   r6   )r=   r>   r#   r?   )r8   r   r#   r   )rL   r"   r#   r   )rN   r"   r#   rO   r7   )r   r   rY   r   r#   rX   )r   r   )r   r   rD   rO   r   rO   r#   r"   )r   )
r   r   rD   rO   r   rO   r   rO   r#   r"   )
r   r   rD   rO   r   rO   r   rO   r#   r"   )
r   r   rD   rO   r   rO   r   rO   r#   r"   )r   r   rD   rO   r   rO   r   rO   r   r"   r   r"   r#   r"   )=
__future__r   
contextlib	functoolsrS   rj   r&   rz   rC   dataclassesr   r   enumr   r   pathlibr   typingr   r	   r
   r   r   r   r   Ztyping_extensionsr   Ztorchgen.code_templater   argparser   collections.abcr   r   r   r|   r}   parentry   r   ZIDENT_REGEXr/   r0   r1   r:   r<   contextmanagerrH   rK   cacherM   rW   rX   r   r   r   r   r   r   r   r   r   r   r   r    <module>   sZ    $




 A
#

A