o
    hU                     @   sF  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Zddl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 ddlmZ d	d
lmZmZ d'ddZdd Zdd Zdd ZG dd deZG dd deZG dd deZ G dd de Z!G dd deZ"dd Z#G dd  d eZ$d!d" Z%d#d$ Z&G d%d& d&eZ'dS )(    N)DictList   )	variables)create_call_functioncreate_rot_n)unimplemented)
AttrSourceConstantSourceDefaultsSourceGetItemSourceGlobalSource	make_cell   )typestrVariableTrackerc                 C   sb   d|vsJ dt |tr|S |s ddlm} | | ||S ddlm} || |d||S )NsourcezOSource needs to be separate from options due to recursive calls for lists/dictsr   SourcelessBuilderVariableBuilderr   )
isinstancer   torch._dynamo.variables.builderr   add_optionsr   )txvaloptionsr   r   r    r   W/var/www/html/ai/venv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.pywrap_bound_arg   s   

r!   c                 C   s8   t | D ]\}}t|ttfrt| ||||< qd S N)listitemsr   tupledictr!   )r   resultr   kvr   r   r    wrap_args_kwargs(   s
   r*   c                 C   sH   t  }| jj}|jD ]}| ||< ||v r!||| || q
|S r"   )r&   outputside_effectsco_cellvarstrack_cell_new
store_cellpop)parentr'   codeclosure_cellsr,   namer   r   r    init_cellvars/   s   
r5   c           
      C   sd   ddl m} || ||||}||_t|tr"ddlm}	 t|	|}|d u s-t|ts-J ||_|S )Nr   )FunctionTypepairwise)	typesr6   __kwdefaults__r   r%   	itertoolsr8   r&   __annotations__)
r2   	f_globalsr4   defaultsclosure
kwdefaultsannotationsr6   funcr8   r   r   r    _create_nested_fn<   s   
rC   c                   @   sB   e Zd Zdd Zdd Z							
dddZdd Zdd ZdS )BaseUserFunctionVariablec                 C   
   |   jS r"   )get_codeco_filenameselfr   r   r    get_filenameQ      
z%BaseUserFunctionVariable.get_filenamec                 C   rE   r"   )rF   co_namerH   r   r   r    get_nameT   rK   z!BaseUserFunctionVariable.get_nameargsList[VariableTracker]kwargsDict[str, VariableTracker]returnr   c                 C   s   | | t|  t| |S r"   )inline_user_function_returnr#   	self_argsrI   r   rN   rP   r   r   r    call_functionW   s   z&BaseUserFunctionVariable.call_functionc                 C   s   t t|  jS r"   )leninspect	signatureget_function
parametersrH   r   r   r    num_parameters^   s   z'BaseUserFunctionVariable.num_parametersc                 C   s   i S r"   r   )rI   r   r   r   r    closure_varsa      z%BaseUserFunctionVariable.closure_varsNrN   rO   rP   rQ   rR   r   )__name__
__module____qualname__rJ   rM   rV   r\   r]   r   r   r   r    rD   P   s    
rD   c                       s|   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Z						d fddZ  ZS )UserFunctionVariablez-Some unsupported user-defined global functionFc                    s   t  jd	i | t|ddrd| _nd| _t|tjtjj	fs,J dt
| d| t|d|}t|ddrAt|d|}|| _d S )
N_dynamo_marked_constantFTzexpected FunctionType found  _torchdynamo_inline__script_if_tracing_wrapper__original_fnr   )super__init__getattris_constantr   r9   r6   torchjitScriptFunctionr   rX   getattr_staticfn)rI   rq   rl   rP   	__class__r   r    rj   h   s   
zUserFunctionVariable.__init__c                 C      g S r"   r   rH   r   r   r    rT   z   r^   zUserFunctionVariable.self_argsc                 C      | j S r"   )rq   rH   r   r   r    rZ   }      z!UserFunctionVariable.get_functionc                 C      | j jS r"   )rq   __code__rH   r   r   r    rF         zUserFunctionVariable.get_codec                 C      t jS r"   )r9   r6   rH   r   r   r    python_type   rv   z UserFunctionVariable.python_typec                 C   s   t | jdd d uS )N__self__)rk   rq   rH   r   r   r    has_self   s   zUserFunctionVariable.has_selfc                 C   rw   r"   )rq   __globals__rH   r   r   r    get_globals   ry   z UserFunctionVariable.get_globalsc              
      s^  j rJ tg}|jj}tjt||dj}|j	pg }fddt
|D }t|j|j|jtfddt||D |j}	|jr^fdd|jD   fdd|j D |	_t|	j|i |}
|
  t|
j }t||| t|||j}jjpd}t|tjjjksJ tt  jjj|D ]\}}}|d	krj!rt"j!d	nd }t#j$|j%|d
||< q|&||}|d ur|||< qj!rddl'm(} |jj)}||v r|| }n/t*t"j!d|}t"|d}||||j%}|+ |j,vr|||< q|-||}|.|| |||< qddl'm/} | ||j%0|||< q||fS )N)r   r   c                    s*   g | ]\}} j d u rd nt j |qS r"   r   r   ).0idx_rH   r   r    
<listcomp>   s    z2UserFunctionVariable.bind_args.<locals>.<listcomp>c                    s   g | ]
\}} ||d qS )r   r   r   )r   argr   )wrapr   r    r      s    
c                    s,   i | ]}| j d u rd nt j |ddqS )NT)is_kwr   )r   r(   rH   r   r    
<dictcomp>   s    
z2UserFunctionVariable.bind_args.<locals>.<dictcomp>c                    s"   i | ]\}}|| | d qS r   r   r   r(   r)   )kwdefaults_sourcesr   r   r    r      s    r   rs   r   r   r   __closure__cell_contentsr   )1rl   r   	propagater+   root_tx	functoolspartialr!   rq   __defaults__	enumerater9   r6   rx   r~   r`   r%   zipr   r:   r$   rX   rY   bindapply_defaultsr&   	argumentsr*   r5   rW   co_freevarsr;   countr   r	   r   UserDefinedClassVariabler   match_nested_cellbuilderr   r,   r   r4   mutated_closure_cell_contentstrack_cell_existingr/   r   r   )rI   r1   rN   rP   r   r   rq   r>   defaults_sources	fake_funcboundr'   r3   r?   r   r4   cellr   varr   r,   outclosure_cellclosure_cell_contentscontents_varr   r   )r   rI   r   r    	bind_args   s   







zUserFunctionVariable.bind_argsc                 C   s   d S r"   r   )rI   r1   childr   r   r    export_freevars   r^   z$UserFunctionVariable.export_freevarsrN   rO   rP   rQ   rR   r   c                    s@   | j rt| || }t|| j|  |||S t |||S r"   )	rl   r   r   valuesinvoke_and_store_as_constantrq   rM   ri   rV   )rI   r   rN   rP   r   rr   r   r    rV      s   z"UserFunctionVariable.call_function)Fr_   )r`   ra   rb   __doc__rj   rT   rZ   rF   r{   r}   r   r   r   rV   __classcell__r   r   rr   r    rc   e   s$    mrc   c                       s^   e Zd ZdZ fddZdd Zdd Zdd	 Z	
					d fddZ fddZ	  Z
S )UserMethodVariablez$Some unsupported user-defined methodc                    s    t  jdd|i| || _d S )Nrq   r   )ri   rj   obj)rI   rq   r   rP   rr   r   r    rj     s   
zUserMethodVariable.__init__c                 C   s   | j j d| j d| j dS )N(z, ))rs   r`   rq   r   rH   r   r   r    __str__  s   zUserMethodVariable.__str__c                 C   s   | j gS r"   )r   rH   r   r   r    rT     ry   zUserMethodVariable.self_argsc                 C   rz   r"   )r9   
MethodTyperH   r   r   r    r{     rv   zUserMethodVariable.python_typerN   rO   rP   rQ   rR   r   c                    sp   |j  r0t| jtjr0t| jdd}|d ur|ds| j	r0| jj
|| jj||| j	d| S t |||S )Nra    z	torch.nn.)constant)r+   is_root_tracerr   r   r   NNModuleVariablerk   rq   
startswithrl   call_methodr`   r   ri   rV   )rI   r   rN   rP   module_attrrr   r   r    rV     s   z UserMethodVariable.call_functionc                    s   t   d S )Nr   )ri   r\   rH   rr   r   r    r\   5  s   z!UserMethodVariable.num_parametersr_   )r`   ra   rb   r   rj   r   rT   r{   rV   r\   r   r   r   rr   r    r     s    r   c                       6   e Zd Z fddZ						d fd	d
Z  ZS )WrappedUserMethodVariablec                    sB   | dd  | dd  t j|j|jfi | || _|| _d S Nrq   r   )r0   ri   rj   rq   r   wrappedcontextrI   r   r   rP   rr   r   r    rj   :  s
   
z"WrappedUserMethodVariable.__init__rN   rO   rP   rQ   rR   r   c                    ,   | j | t |||}| j | |S r"   r   enterri   rV   exitrI   r   rN   rP   r'   rr   r   r    rV   A     z'WrappedUserMethodVariable.call_functionr_   r`   ra   rb   rj   rV   r   r   r   rr   r    r   9      r   c                       r   )WrappedUserFunctionVariablec                    s>   | dd  | dd  t j|jfi | || _|| _d S r   )r0   ri   rj   rq   r   r   r   rr   r   r    rj   K  s
   
z$WrappedUserFunctionVariable.__init__rN   rO   rP   rQ   rR   r   c                    r   r"   r   r   rr   r   r    rV   R  r   z)WrappedUserFunctionVariable.call_functionr_   r   r   r   rr   r    r   J  r   r   c                    s\   dd   fdd|D } fdd|  D }||i |}| jj||fdt|i|S )Nc                 S   s   t | tjr
|  S |  S r"   )r   r   TensorVariableget_real_valueas_python_constant)xr   r   r    convert\  s   z-invoke_and_store_as_constant.<locals>.convertc                    s   g | ]} |qS r   r   )r   r   r   r   r    r   a      z0invoke_and_store_as_constant.<locals>.<listcomp>c                    s   i | ]	\}}| |qS r   r   r   r   r   r    r   b  s    z0invoke_and_store_as_constant.<locals>.<dictcomp>r   )r$   r+   register_attr_or_moduler
   )r   rq   r4   r   rN   rP   resr   r   r    r   [  s   r   c                       sh   e Zd Z	d fdd	Zdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Z  ZS )NestedUserFunctionVariableNc
                    s   t  jdi |
 t| tsJ t| tjsJ t|ts#J || _|| _	|| _
|| _|| _|| _|| _|d u r>d }|| _|	| _d S )Nr   )ri   rj   r   r   strr9   CodeTyper&   fn_namer2   r=   r>   r@   rA   r?   closure_scopewraps_source)rI   r   r2   r=   r>   r@   rA   r?   r   r   rP   rr   r   r    rj   m  s   
z#NestedUserFunctionVariable.__init__c                 C   rt   r"   r   rH   r   r   r    rT     r^   z$NestedUserFunctionVariable.self_argsc                 C   s
   | j  S r"   )r2   r   rH   r   r   r    rF     rK   z#NestedUserFunctionVariable.get_codec                 C   s   | j rt t| j | j| j }| jr| j |_	| j
r&| j
 |_| jrI| j }t|tr?ddlm} t||}t|tsFJ ||_|S )Nr   r7   )r?   NotImplementedErrorr9   r6   r2   r   r=   r   r>   r   r@   r:   rA   r   r%   r;   r8   r&   r<   )rI   rB   rA   r8   r   r   r    rZ     s&   

z'NestedUserFunctionVariable.get_functionc                 C   s
   | j d uS r"   )r?   rH   r   r   r    has_closure  rK   z&NestedUserFunctionVariable.has_closurec                 C   s   dS )NFr   rH   r   r   r    r}     r^   z#NestedUserFunctionVariable.has_selfc                 C   ru   r"   )r=   rH   r   r   r    r     rv   z&NestedUserFunctionVariable.get_globalsc                 C   sj  ddl m} |  }t|| j| j | jrt	| jj
nd t	dd tt|  jD }| jr6| jj
|_t|j|i |}|  t|j
 }t|jj|t|  t|||}	t|jD ]N\}
}| jj
|
 }t||||ksvJ ||vs|J t||r|}|r||j vr|j!}|r||j vs|d u rt"d| d|j | ||< qb| jj
|
 |	|< qb||	fS )Nr   )InlinedClosureVariablec                 s   s    | ]}t d V  qd S r"   r   )r   r   r   r   r    	<genexpr>  s    z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>zCouldn't find z7 in the symbolic_locals of the inline interpreter stack)#miscr   rF   r9   r6   r=   r   r   r>   r%   r$   rangerW   r   r@   r:   rX   rY   r   r   r&   r   r*   r+   r   r   r   r5   r   r?   rk   r   symbolic_localsr1   RuntimeError)rI   r1   rN   rP   r   r2   rB   r   r'   r3   r   r4   r   candr   r   r    r     s@   


z$NestedUserFunctionVariable.bind_argsc                 C   s2   |   }|jD ]}||jv r|j| |j|< qd S r"   )rF   r   r   )rI   r1   r   r2   r   r   r   r    r     s   

z*NestedUserFunctionVariable.export_freevarsc                 C   s  | td || j ||| jg || j | jr#|| j n	||d g | j	r5|| j	 n	||d g | j
rG|| j
 n	||d g | jrz)t| jtjrgdd | jj D }ntdd | jjD }|||g W n ty   || j Y n
w ||d g |tddd | jr| d	d
 || j |tdd |td |tdd g S )NrC   c                 S   s   i | ]	\}}||  qS r   r   r   r   r   r    r     s    z:NestedUserFunctionVariable.reconstruct.<locals>.<dictcomp>c                 S   s   g | ]}|  qS r   r   )r   r)   r   r   r    r      r   z:NestedUserFunctionVariable.reconstruct.<locals>.<listcomp>   T)	push_nullr   wrapsr   r   )load_import_fromr`   r2   extend_output_create_load_constr=   r   r>   create_load_constr?   r@   rA   r   r   ConstDictVariabler$   r%   r   r   r   r   )rI   codegenrA   r   r   r    reconstruct  sF   



z&NestedUserFunctionVariable.reconstructr"   )r`   ra   rb   rj   rT   rF   rZ   r   r}   r   r   r   r   r   r   r   rr   r    r   l  s    +r   c                  C   s   t j rddlm}  | S i S )Nr   traceable_collective_remaps)rm   distributedis_available)torch.distributed._functional_collectivesr   r   r   r   r    _traceable_collective_remaps  s   
r   c                 C   s`   t j s	J dddlm}m} ||h}| |v sJ | j}tttdddddd}t||S )	NzIllegal invocation.r   )all_gather_tensor_inplacereduce_scatter_tensor_inplacerm   )global_namer   )basemember_functional_collectives)	rm   r   r   r   r   r   r`   r	   r   )rq   r   r   valid_values
inner_namepath_sourcer   r   r    _traceable_collectives_source  s   
r   c                       sR   e Zd ZdZ fddZedd Zedd Z				
			d fddZ  Z	S )!CollectiveFunctionRewriteVariablea  
    Some of the torch.distributed.* collective APIs are possible to rewrite to 'traceable' collectives.

    This class provides both a way to check if a function is remappable, and perform the remapping.

    In the case that a function is 'remappable' but only for some combinations of call-time arguments,
    we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
    than status-quo as we currently graph-break on all distributed.* collectives.
    c                   s$   || _ || _t j|fi | d S r"   )orig_fnorig_sourceri   rj   )rI   rq   r   r   rP   rr   r   r    rj   ;  s   z*CollectiveFunctionRewriteVariable.__init__c                 C   s   t | o	| t v S r"   )rX   
isfunctionr   )variabler   r   r    can_rewriteC  s   z-CollectiveFunctionRewriteVariable.can_rewritec                 C   s   t  |  }|t|fS r"   )r   r   )rq   new_fnr   r   r    rewriteI  s   
z)CollectiveFunctionRewriteVariable.rewriterN   rO   rP   rQ   rR   r   c                    s4   | ddr| j| _td| j  t |||S )Nasync_opFzBCollectiveFunctionRewriteVariable can't support async_op=True for )getr   r   r   r   ri   rV   rU   rr   r   r    rV   N  s   
z/CollectiveFunctionRewriteVariable.call_functionr_   )
r`   ra   rb   r   rj   staticmethodr  r  rV   r   r   r   rr   r    r   0  s    


r   r"   )(r   rX   r;   r9   typingr   r   rm   r   r   bytecode_transformationr   r   excr   r   r	   r
   r   r   r   utilsr   r   r   r   r!   r*   r5   rC   rD   rc   r   r   r   r   r   r   r   r   r   r   r   r    <module>   s8    
 $1 )