U
    ªÊ¹jûU  ã                   @   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/bwave/python/venv/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ƒsŒt‚||_|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sèt‚||ksôt‚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ƒrÐd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 )