U
    9%eU                     @   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   sf   d|kst dt|tr|S |s@ddlm} | | ||S ddlm} || |d||S d S )NsourcezOSource needs to be separate from options due to recursive calls for lists/dictsr   SourcelessBuilderVariableBuilderr   )AssertionError
isinstancer   Ztorch._dynamo.variables.builderr   add_optionsr   )txvaloptionsr   r   r    r   `/var/www/html/Darija-Ai-API/env/lib/python3.8/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 ].}| ||< ||kr||| || q|S r"   )r&   outputside_effectsco_cellvarsZtrack_cell_new
store_cellpop)parentr'   codeclosure_cellsr,   namer   r   r    init_cellvars/   s    
r4   c           
      C   sd   ddl m} || ||||}||_t|trDddlm}	 t|	|}|d ksZt|tsZt||_	|S )Nr   )FunctionTypepairwise)
typesr5   __kwdefaults__r   r%   	itertoolsr7   r&   r   __annotations__)
r1   	f_globalsr3   defaultsclosure
kwdefaultsannotationsr5   funcr7   r   r   r    _create_nested_fn<   s    
rB   c                   @   s>   e Zd Zdd Zdd Zddddd	d
Zdd Zdd ZdS )BaseUserFunctionVariablec                 C   s
   |   jS r"   )get_codeco_filenameselfr   r   r    get_filenameQ   s    z%BaseUserFunctionVariable.get_filenamec                 C   s
   |   jS r"   )rD   co_namerF   r   r   r    get_nameT   s    z!BaseUserFunctionVariable.get_nameList[VariableTracker]Dict[str, VariableTracker]r   argskwargsreturnc                 C   s   | | t|  t| |S r"   )Zinline_user_function_returnr#   	self_argsrG   r   rN   rO   r   r   r    call_functionW   s
      z&BaseUserFunctionVariable.call_functionc                 C   s   t t|  jS r"   )leninspect	signatureget_function
parametersrF   r   r   r    num_parameters^   s    z'BaseUserFunctionVariable.num_parametersc                 C   s   i S r"   r   )rG   r   r   r   r    closure_varsa   s    z%BaseUserFunctionVariable.closure_varsN)__name__
__module____qualname__rH   rJ   rS   rY   rZ   r   r   r   r    rC   P   s    rC   c                       sx   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ddd fddZ  ZS )UserFunctionVariablez-Some unsupported user-defined global functionFc                    s   t  jf | t|ddr"d| _nd| _t|tjtjj	fsTt
dt| d| t|d|}t|ddr~t|d|}|| _d S )	NZ_dynamo_marked_constantFTzexpected FunctionType found  Z_torchdynamo_inlineZ__script_if_tracing_wrapperZ__original_fn)super__init__getattris_constantr   r8   r5   torchZjitZScriptFunctionr   r   rU   getattr_staticfn)rG   rf   rc   rO   	__class__r   r    ra   h   s     zUserFunctionVariable.__init__c                 C   s   g S r"   r   rF   r   r   r    rQ   z   s    zUserFunctionVariable.self_argsc                 C   s   | j S r"   )rf   rF   r   r   r    rW   }   s    z!UserFunctionVariable.get_functionc                 C   s   | j jS r"   )rf   __code__rF   r   r   r    rD      s    zUserFunctionVariable.get_codec                 C   s   t jS r"   )r8   r5   rF   r   r   r    python_type   s    z UserFunctionVariable.python_typec                 C   s   t | jdd d k	S )N__self__)rb   rf   rF   r   r   r    has_self   s    zUserFunctionVariable.has_selfc                 C   s   | j jS r"   )rf   __globals__rF   r   r   r    get_globals   s    z UserFunctionVariable.get_globalsc              
      sl  j r
t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||}
|
  t|
j }t||| t|||j}j	jp
d}t|tj	jjks(ttt ! j	jj|D ]"\}}}|d	krj"rht#j"d	nd }t$j%|j&|d
||< n|'||}|d k	r|||< nj"r<ddl(m)} |jj*}||kr|| }n`t+t#j"d|}t#|d}||||j&}|, |j-kr|||< q>|.||}|/|| |||< n$ddl(m0} | ||j&1|||< q>||fS )N)r   r   c                    s*   g | ]"\}} j d krd n
t j |qS r"   r   r   ).0idx_rF   r   r    
<listcomp>   s   z2UserFunctionVariable.bind_args.<locals>.<listcomp>c                    s   g | ]\}} ||d qS )r   r   r   )rp   argr   )wrapr   r    rs      s   c                    s,   i | ]$}| j d krd nt j |ddqS )NT)Zis_kwro   )rp   r(   rF   r   r    
<dictcomp>   s
   z2UserFunctionVariable.bind_args.<locals>.<dictcomp>c                    s"   i | ]\}}|| | d qS rt   r   rp   r(   r)   )kwdefaults_sourcesrv   r   r    rw      s    r   rh   r   r   r   __closure__cell_contentsr   )2rc   r   r   	propagater+   root_tx	functoolspartialr!   rf   __defaults__	enumerater8   r5   ri   rm   r[   r%   ziprz   r9   r$   rU   rV   bindapply_defaultsr&   	argumentsr*   r4   rT   co_freevarsr:   countr   r	   r   ZUserDefinedClassVariabler{   Zmatch_nested_cellZbuilderr   r,   r   r3   Zmutated_closure_cell_contentsZtrack_cell_existingr.   r   r   )rG   r0   rN   rO   r   r   rf   r=   Zdefaults_sourcesZ	fake_funcboundr'   r2   r>   rq   r3   cellr   varr   r,   outZclosure_cellZclosure_cell_contentsZcontents_varr   r   )ry   rG   rv   r    	bind_args   s    




  





  
 zUserFunctionVariable.bind_argsc                 C   s   d S r"   r   )rG   r0   childr   r   r    export_freevars   s    z$UserFunctionVariable.export_freevarsrK   rL   r   rM   c                    s@   | j r0t| || }t|| j|  |||S t |||S r"   )	rc   r   r|   valuesinvoke_and_store_as_constantrf   rJ   r`   rS   )rG   r   rN   rO   r   rg   r   r    rS      s         z"UserFunctionVariable.call_function)F)r[   r\   r]   __doc__ra   rQ   rW   rD   rj   rl   rn   r   r   rS   __classcell__r   r   rg   r    r^   e   s   m r^   c                       sZ   e Zd ZdZ fddZdd Zdd Zdd	 Zd
ddd fddZ fddZ	  Z
S )UserMethodVariablez$Some unsupported user-defined methodc                    s    t  jf d|i| || _d S )Nrf   )r`   ra   obj)rG   rf   r   rO   rg   r   r    ra     s    zUserMethodVariable.__init__c                 C   s   | j j d| j d| j dS )N(z, ))rh   r[   rf   r   rF   r   r   r    __str__  s    zUserMethodVariable.__str__c                 C   s   | j gS r"   )r   rF   r   r   r    rQ     s    zUserMethodVariable.self_argsc                 C   s   t jS r"   )r8   
MethodTyperF   r   r   r    rj     s    zUserMethodVariable.python_typerK   rL   r   rM   c                    sp   |j  r`t| jtjr`t| jdd}|d k	r8|ds>| j	r`| jj
|| jj||| j	d| S t |||S )Nr\    z	torch.nn.)Zconstant)r+   Zis_root_tracerr   r   r   ZNNModuleVariablerb   rf   
startswithrc   Zcall_methodr[   r   r`   rS   )rG   r   rN   rO   Zmodule_attrrg   r   r    rS     s*         z UserMethodVariable.call_functionc                    s   t   d S )Nr   )r`   rY   rF   rg   r   r    rY   5  s    z!UserMethodVariable.num_parameters)r[   r\   r]   r   ra   r   rQ   rj   rS   rY   r   r   r   rg   r    r     s    r   c                       s2   e Zd Z fddZdddd fddZ  ZS )	WrappedUserMethodVariablec                    s>   | dd  | dd  t j|j|jf| || _|| _d S Nrf   r   )r/   r`   ra   rf   r   wrappedcontextrG   r   r   rO   rg   r   r    ra   :  s
    z"WrappedUserMethodVariable.__init__rK   rL   r   rM   c                    s,   | j | t |||}| j | |S r"   r   Zenterr`   rS   exitrG   r   rN   rO   r'   rg   r   r    rS   A  s    z'WrappedUserMethodVariable.call_functionr[   r\   r]   ra   rS   r   r   r   rg   r    r   9  s
    r   c                       s2   e Zd Z fddZdddd fddZ  ZS )	WrappedUserFunctionVariablec                    s:   | dd  | dd  t j|jf| || _|| _d S r   )r/   r`   ra   rf   r   r   r   rg   r   r    ra   K  s
    z$WrappedUserFunctionVariable.__init__rK   rL   r   rM   c                    s,   | j | t |||}| j | |S r"   r   r   rg   r   r    rS   R  s    z)WrappedUserFunctionVariable.call_functionr   r   r   rg   r    r   J  s
    r   c                    sX   dd   fdd|D } fdd|  D }|||}| jj||fdt|i|S )Nc                 S   s   t | tjr|  S |  S r"   )r   r   ZTensorVariableZ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   )rp   r   r   r   r    rs   a  s     z0invoke_and_store_as_constant.<locals>.<listcomp>c                    s   i | ]\}}| |qS r   r   rx   r   r   r    rw   b  s      z0invoke_and_store_as_constant.<locals>.<dictcomp>r   )r$   r+   Zregister_attr_or_moduler
   )r   rf   r3   r   rN   rO   resr   r   r    r   [  s    
r   c                       sf   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f |
 t| ts tt| tjs4tt|tsBt|| _	|| _
|| _|| _|| _|| _|| _|d krxd }|| _|	| _d S r"   )r`   ra   r   r   strr   r8   CodeTyper&   fn_namer1   r<   r=   r?   r@   r>   closure_scopewraps_source)rG   r   r1   r<   r=   r?   r@   r>   r   r   rO   rg   r   r    ra   m  s    z#NestedUserFunctionVariable.__init__c                 C   s   g S r"   r   rF   r   r   r    rQ     s    z$NestedUserFunctionVariable.self_argsc                 C   s
   | j  S r"   )r1   r   rF   r   r   r    rD     s    z#NestedUserFunctionVariable.get_codec                 C   s   | j rt t| j | j| j }| jr:| j |_	| j
rL| j
 |_| jr| j }t|tr~ddlm} t||}t|tst||_|S )Nr   r6   )r>   NotImplementedErrorr8   r5   r1   r   r<   r   r=   r   r?   r9   r@   r   r%   r:   r7   r&   r   r;   )rG   rA   r@   r7   r   r   r    rW     s&    

z'NestedUserFunctionVariable.get_functionc                 C   s
   | j d k	S r"   )r>   rF   r   r   r    has_closure  s    z&NestedUserFunctionVariable.has_closurec                 C   s   dS )NFr   rF   r   r   r    rl     s    z#NestedUserFunctionVariable.has_selfc                 C   s   | j S r"   )r<   rF   r   r   r    rn     s    z&NestedUserFunctionVariable.get_globalsc                 C   sd  ddl m} |  }t|| j| j | jr8t	| jj
nd t	dd tt|  jD }| jrl| jj
|_t|j||}|  t|j
 }t|jj|t|  t|||}	t|jD ]\}
}| jj
|
 }t||||kst||kstt ||rJ|}|r ||j!kr |j"}q|d kr:t#d| d|j!| ||< q| jj
|
 |	|< q||	fS )Nr   )InlinedClosureVariablec                 s   s   | ]}t d V  qd S r"   r   )rp   rr   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   rD   r8   r5   r<   r   r   r=   r%   r$   rangerT   r   r?   r9   rU   rV   r   r   r&   r   r*   r+   r}   r   r|   r4   r   r>   rb   r   r   symbolic_localsr0   RuntimeError)rG   r0   rN   rO   r   r1   rA   r   r'   r2   rq   r3   r   candr   r   r    r     s>    



z$NestedUserFunctionVariable.bind_argsc                 C   s2   |   }|jD ]}||jkr|j| |j|< qd S r"   )rD   r   r   )rG   r0   r   r1   r   r   r   r    r     s    

z*NestedUserFunctionVariable.export_freevarsc                 C   s  | td || j ||| jg || j | jrF|| j n||d g | j	rj|| j	 n||d g | j
r|| j
 n||d g | jrzRt| jtjrdd | jj D }ntdd | jjD }|||g W n  tk
r   || j Y nX n||d g |tddd | jr| d	d
 || j |tdd |td |tdd g S )NrB   c                 S   s   i | ]\}}||  qS r   r   rx   r   r   r    rw     s    z:NestedUserFunctionVariable.reconstruct.<locals>.<dictcomp>c                 S   s   g | ]}|  qS r   r   )rp   r)   r   r   r    rs      s     z:NestedUserFunctionVariable.reconstruct.<locals>.<listcomp>   T)Z	push_nullr~   wrapsr   r   )Zload_import_fromr[   r1   Zextend_outputZ_create_load_constr<   r   r=   Zcreate_load_constr>   r?   r@   r   r   ZConstDictVariabler$   r%   r   r   r   r   )rG   Zcodegenr@   r   r   r    reconstruct  sD    



z&NestedUserFunctionVariable.reconstruct)N)r[   r\   r]   ra   rQ   rD   rW   r   rl   rn   r   r   r   r   r   r   rg   r    r   l  s    +r   c                  C   s   t j rddlm}  | S i S )Nr   traceable_collective_remaps)rd   distributedis_available)torch.distributed._functional_collectivesr   r   r   r   r    _traceable_collective_remaps  s    
r   c                 C   s`   t j stdddlm}m} ||h}| |ks6t| j}ttt	dddddd}t||S )	NzIllegal invocation.r   )all_gather_tensor_inplacereduce_scatter_tensor_inplacerd   )Zglobal_namer   )basememberZ_functional_collectives)
rd   r   r   r   r   r   r   r[   r	   r   )rf   r   r   Zvalid_valuesZ
inner_nameZpath_sourcer   r   r    _traceable_collectives_source  s    r   c                       sN   e Zd ZdZ fddZedd Zedd Zdd	d
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| d S r"   )orig_fnorig_sourcer`   ra   )rG   rf   r   r   rO   rg   r   r    ra   ;  s    z*CollectiveFunctionRewriteVariable.__init__c                 C   s   t | o| t kS r"   )rU   
isfunctionr   )variabler   r   r    can_rewriteC  s    z-CollectiveFunctionRewriteVariable.can_rewritec                 C   s   t  |  }|t|fS r"   )r   r   )rf   Znew_fnr   r   r    rewriteI  s    
z)CollectiveFunctionRewriteVariable.rewriterK   rL   r   rM   c                    s4   | ddr$| j| _td| j  t |||S )NZasync_opFzBCollectiveFunctionRewriteVariable can't support async_op=True for )getr   r   r   r   r`   rS   rR   rg   r   r    rS   N  s    
z/CollectiveFunctionRewriteVariable.call_function)
r[   r\   r]   r   ra   staticmethodr   r   rS   r   r   r   rg   r    r   0  s   


 r   )N)(r~   rU   r:   r8   typingr   r   rd   r   r   Zbytecode_transformationr   r   excr   r   r	   r
   r   r   r   utilsr   r   r   r   r!   r*   r4   rB   rC   r^   r   r   r   r   r   r   r   r   r   r   r   r    <module>   s6   
 $1 )