
    קgϡ                        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mZm	Z	m
Z
mZmZmZ d dlZddlmZmZ ddlmZmZ ddlmZmZ ddlmZmZ ddlmZmZmZmZ dd	l m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d
dl(m)Z)m*Z*m+Z+ d
dl,m-Z- 	 d dl.m/Z/ n# e0$ r dZ/Y nw xY werd dl1m2Z2 d dl3m4Z4  ede          Z5d6d7dZ6d7dZ7d Z8d Z9 G d de+          Z: G d de:          Z; G d de;          Z< G d de<          Z= G d  d!e;          Z>d7d"Z? G d# d$e:          Z@ G d% d&e+          ZA G d' d(e+          ZBd) ZCd7d*ZD G d+ d,e;          ZE G d- d.e+          ZF G d/ d0e+          ZGd d1lHmIZI  G d2 d3eI          ZJ eJ            ZK G d4 d5e+          ZLdS )8    N)AnyCallableDictListOptionalTYPE_CHECKINGTypeVarUnion   )	polyfills	variables)create_call_functioncreate_rot_n)unimplementedUnsupported)GuardBuilderinstall_guard)
AttrSourceConstantSourceDefaultsSourceGetItemSource)check_constant_argscheck_unspec_or_constant_argsidentityis_functionis_wrapper_or_member_descriptoristype	make_cell   )MutableLocaltypestrVariableTrackerConstantVariable)_fsdp_param_group)InstructionTranslator)Source_F)boundtxr&   c                     t          |t                    r|S |sddlm} |                    | |          S t
          j                            ||          S )Nr   SourcelessBuilder)
isinstancer"   torch._dynamo.variables.builderr-   creater   LazyVariableTracker)r*   valsourcer-   s       ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_dynamo/variables/functions.pywrap_bound_argr5   ,   sf    #'' 	A
 AEEEEEE ''C000 ,33C@@@    c                     t          |                                          D ]4\  }}t          |t          t          f          rt          | |          ||<   5d S N)listitemsr.   tupledictr5   )r*   resultkvs       r4   wrap_args_kwargsr@   :   sY    V\\^^$$ . .1a%'' 	.&r1--F1I. .r6   c                     i }| j         j        }|j        D ]L}|                                ||<   ||v r/|                    ||         |                    |                     M|S r8   )outputside_effectsco_cellvarstrack_cell_new
store_cellpop)parentr=   codeclosure_cellsrC   names         r4   init_cellvarsrL   A   st    M=-L   K K*99;;d6>>##M$$7D9I9IJJJr6   c                     ddl m}  || ||||          }||_        t          |t                    rddlm}	 t           |	|                    }|t          |t                    sJ ||_        |S )Nr   )FunctionTypepairwise)	typesrN   __kwdefaults__r.   r;   	itertoolsrP   r<   __annotations__)
rI   	f_globalsrK   defaultsclosure
kwdefaultsannotationsrN   funcrP   s
             r4   _create_nested_fnr[   N   s     #"""""<ixAAD$D+u%% 2&&&&&&88K0011 *[$"?"?&DKr6   c                   R    e Zd Zd Zd Z	 	 	 	 	 	 	 	 ddZddded	efdZd Z	d Z
dS )BaseUserFunctionVariablec                 4    |                                  j        S r8   )get_codeco_filenameselfs    r4   get_filenamez%BaseUserFunctionVariable.get_filenamec   s    }}**r6   c                 4    |                                  j        S r8   )r_   co_namera   s    r4   get_namez!BaseUserFunctionVariable.get_namef   s    }}&&r6   r*   r&   argsList[VariableTracker]kwargsDict[str, VariableTracker]returnr"   c                 \    |                     | g |                                 ||          S r8   )inline_user_function_return	self_argsrb   r*   rg   ri   s       r4   call_functionz&BaseUserFunctionVariable.call_functioni   s2     --d4Ndnn6F6F4N4NPVWWWr6   rK   c                     d}	 t          |                                 |          }n-# t          $ r  |dk    rt          | t                    rd}Y nw xY wt
          j                            |          S )NF__name__T)hasattrget_functionNotImplementedErrorr.   NestedUserFunctionVariabler   r$   r0   rb   r*   rK   r=   s       r4   call_hasattrz%BaseUserFunctionVariable.call_hasattrq   s    	T..00$77FF" 	 	 	z!!j7Q&R&R!	 )00888s   "' 'AAc                 r    t          t          j        |                                           j                  S r8   )r9   inspect	signaturert   
parametersra   s    r4   inspect_parameter_namesz0BaseUserFunctionVariable.inspect_parameter_names{   s*    G%d&7&7&9&9::EFFFr6   c                     i S r8    )rb   r*   s     r4   closure_varsz%BaseUserFunctionVariable.closure_vars~       	r6   Nr*   r&   rg   rh   ri   rj   rk   r"   )rr   
__module____qualname__rc   rf   rp   strr"   rx   r}   r   r   r6   r4   r]   r]   b   s        + + +' ' 'X#X &X -	X
 
X X X X96 9c 9o 9 9 9 9G G G    r6   r]   c                        e Zd ZdZddhej        Zed             Zdd fdZ fd	Z	d
 Z
d Zd Zd Zd Zd Zd Zd ZdddefdZdddedefdZ	 	 	 	 	 	 	 	 d fdZ xZS ) UserFunctionVariablez-Some unsupported user-defined global functionfnis_constantc                 t    t          |                    t          j                              | ||          S Nr3   )r   
make_guardr   CLOSURE_MATCHclsvaluer3   s      r4   create_with_sourcez'UserFunctionVariable.create_with_source   s6    f''(BCCDDDs5((((r6   Frk   Nc                 D    t                      j        di | t          |dd          rd| _        nd| _        t	          |t
          j        t          j        j	        f          sJ dt          |           d|             t          j        |d|          }|| _        d S )N_dynamo_marked_constantFTzexpected FunctionType found  _torchdynamo_inliner   )super__init__getattrr   r.   rQ   rN   torchjitScriptFunctionr!   rz   getattr_staticr   )rb   r   r   ri   	__class__s       r4   r   zUserFunctionVariable.__init__   s    ""6"""20%88 	%#D$D#UY%=>
 
 	= 	=<'"++<<<<	= 	= 	= #B(=rBB&(r6   c                 |    t          | t                    r| j        S t                                                      S r8   )r   r   r   r   as_python_constantrb   r   s    r4   r   z'UserFunctionVariable.as_python_constant   s3    $,-- 	7Nww))+++r6   c                     g S r8   r   ra   s    r4   rn   zUserFunctionVariable.self_args   r   r6   c                     | j         S r8   r   ra   s    r4   rt   z!UserFunctionVariable.get_function   	    wr6   c                     | j         j        S r8   )r   __code__ra   s    r4   r_   zUserFunctionVariable.get_code   s    wr6   c                     t           j        S r8   )rQ   rN   ra   s    r4   python_typez UserFunctionVariable.python_type   s    !!r6   c                 2    t          | j        dd           d uS )N__self__)r   r   ra   s    r4   has_selfzUserFunctionVariable.has_self   s    tw
D11==r6   c                     | j         j        S r8   )r   __globals__ra   s    r4   get_globalsz UserFunctionVariable.get_globals   s    w""r6   c                      j         rJ |j        j        }t          j        t
          |           j        }|j        pg } fdt          |          D             }t          j
        |j        |j        |j        t          fdt          ||          D                       |j                  }|j        r> fd|j        D             fd|j                                        D             |_         t'          j        |          j        |i |}	|	                                 t/          |	j                                                  }
t3          ||
           t5          ||
|j                  } j        j        pd}t7          |          t7           j        j        j                  k    sJ t          t;          j                     j        j        j        |          D ]y\  }}}|dk    r= j        rtA           j        d          nd }tC          j"        |j#        |          |
|<   J|$                    ||          }|||
|<   h j        rd	d
l%m&} |j        j'        }||v r	||         }ntQ          tA           j        d          |          }tA          |d          }	   |||          |j#                  }n## tR          $ r tC          j*                    }Y nw xY w|+                                |j,        vr||
|<   "|-                    ||          }|.                    ||           ||
|<   Ud	dl%m/} |0                    ||j#                  |
|<   {|
|fS )N)r*   c                 R    g | ]#\  }}j         d nt          j         |          $S r8   r3   r   ).0idx_rb   s      r4   
<listcomp>z2UserFunctionVariable.bind_args.<locals>.<listcomp>   sD     
 
 
Q K'DD^DK-M-M
 
 
r6   c                 0    g | ]\  }} ||           S )r2   r3   r   )r   argr3   wraps      r4   r   z2UserFunctionVariable.bind_args.<locals>.<listcomp>   s<       #V DS000  r6   c                 R    i | ]#}|j         d nt          j         |d          $S )NT)is_kwr   )r   r>   rb   s     r4   
<dictcomp>z2UserFunctionVariable.bind_args.<locals>.<dictcomp>   sN     " " "  ;& 4#DK$???" " "r6   c                 >    i | ]\  }}| ||                    S r   r   )r   r>   r?   kwdefaults_sourcesr   s      r4   r   z2UserFunctionVariable.bind_args.<locals>.<dictcomp>   sD     ( ( (Aq 44A&8&;<<<( ( (r6   r   r   r   r   VariableBuilder__closure__cell_contentsr,   )1r   rB   root_tx	functoolspartialr5   r   __defaults__	enumeraterQ   rN   r   r   rr   r;   zipr   rR   r:   rz   r{   bindapply_defaultsr<   	argumentsr@   rL   lenco_freevarsrS   countr3   r   r   UserDefinedClassVariabler   match_nested_cellbuilderr   rC   r   
ValueErrorDeletedVariablerK   mutated_closure_cell_contentstrack_cell_existingrF   r-   r0   )rb   rH   rg   ri   r*   r   rV   defaults_sources	fake_funcr)   r=   rJ   rW   r   rK   cellr3   varr   rC   outclosure_cellclosure_cell_contentscontents_varr-   r   r   s   `                        @@r4   	bind_argszUserFunctionVariable.bind_args   s>   ####]" B777!%?(b
 
 
 
#H--
 
 
 &KNK   '*85E'F'F    N
 
	  
	" " " " *	" " "( ( ( ( (-3355( ( (I$
 2!),,14B6BBeo++--..V$$$%ffbkBB'%+7||s47#3#?@@@@@@"Otw/;W 
  
 ?	T ?	TOCt {""AEVDK===RV(A&!     t
 **466?#&F4LL[ 1T888888#)=#=L|++*40'4&t{MBBC( ( 1;(/1 1-G,?? &(=, ,"0,2 ,2LL  * G G G+4+D+F+FLLLG
 26688#%#CD D ,8F4L$ +>>|TRR$//(  
 $'F4LL ;:::::#4#;#;B@R#S#SF4LL}$$s   (KK#"K#c                     d S r8   r   )rb   rH   childs      r4   export_freevarsz$UserFunctionVariable.export_freevars%  s    r6   r*   r&   rK   c                 @   | j         rt          | j         |          nd }	 t          j        | j        |          }n'# t
          $ r d|i}t          j        | |fi |cY S w xY w|r t          j        	                    ||          S ddl
m} |	                    ||          S )Nr3   r   r,   )r3   r   rz   r   r   AttributeErrorr   GetAttrVariabler1   r0   r   r-   )rb   r*   rK   r3   subobjoptionsr-   s          r4   var_getattrz UserFunctionVariable.var_getattr(  s    26+GDK...4	D+DGT::FF 	D 	D 	D(G,T4CC7CCCCC	D  	H077GGG...... ''F333s   ; !AAc                 j    t          | j        |          }t          j                            |          S r8   )rs   r   r   r$   r0   rw   s       r4   rx   z!UserFunctionVariable.call_hasattr5  s*    $'')00888r6   rg   rh   ri   rj   r"   c                     | j         r*t          || j        |                                 ||          S t	                                          |||          S r8   )r   invoke_and_store_as_constantr   rf   r   rp   )rb   r*   rg   ri   r   s       r4   rp   z"UserFunctionVariable.call_function9  sU      	/DGT]]__dF   ww$$Rv666r6   )Frk   Nr   )rr   r   r   __doc__r]   _nonvar_fieldsclassmethodr   r   r   rn   rt   r_   r   r   r   r   r   r   r   r"   rx   rp   __classcell__r   s   @r4   r   r      s       77 	 
"	0N ) ) [)) ) ) ) ) ) ), , , , ,         " " "> > ># # #l% l% l%\  45 4S 4 4 4 496 9c 9o 9 9 9 97#7 &7 -	7
 
7 7 7 7 7 7 7 7 7 7r6   r   c                   b     e Zd ZdZd fdZdefdZd Zd Z	 	 	 	 	 	 	 	 d fdZ	 fdZ
 xZS )UserMethodVariablez$Some unsupported user-defined methodrk   Nc                 L     t                      j        dd|i| || _        d S )Nr   r   )r   r   obj)rb   r   r   ri   r   s       r4   r   zUserMethodVariable.__init__J  s0    ))B)&)))r6   c                 @    | j         j         d| j         d| j         dS )N(z, ))r   rr   r   r   ra   s    r4   __str__zUserMethodVariable.__str__N  s*    .)BBDGBBtxBBBBr6   c                     | j         gS r8   )r   ra   s    r4   rn   zUserMethodVariable.self_argsQ  s    zr6   c                     t           j        S r8   )rQ   
MethodTypera   s    r4   r   zUserMethodVariable.python_typeT  s    r6   r*   r&   rg   rh   ri   rj   r"   c                    |j                                         rt          | j        t          j                  rit          | j        dd          }||                    d          r|dk    s| j	        r.| j        
                    || j        j        ||| j	                  S nVt          O| j        t          j        j        u r7t	          j        | j                                      || j        g|R |          S | j	        rIt          | j        j        | j        j                  }t%          |||                                 ||          S t)                                          |||          S )Nr    z	torch.nn.ztorch.nn.utils.parametrize)constant)rB   is_root_tracerr.   r   r   NNModuleVariabler   r   
startswithr   call_methodrr   r%   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariablerp   r   r   rf   r   )rb   r*   rg   ri   module_attrr   r   s         r4   rp   z UserMethodVariable.call_functionW  sp   $ 9##%% 	*Hi0+
 +
 	 "$'<<<K '**;77 (#???# @ x++($AQ ,    @ ),;NNN9$'BBPPTX%%%v    	W)9::B/BvVVVww$$Rv666r6   c                 T    t                                                      dd          S )Nr   )r   r}   r   s    r4   r}   z*UserMethodVariable.inspect_parameter_names  s"    ww..0044r6   r   r   )rr   r   r   r   r   r   r   rn   r   rp   r}   r   r   s   @r4   r   r   G  s        ..     C C C C C       *7#*7 &*7 -	*7
 
*7 *7 *7 *7 *7 *7X5 5 5 5 5 5 5 5 5r6   r   c                   <     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZ xZS )WrappedUserMethodVariablerk   Nc                     |                     dd            |                     dd             t                      j        |j        |j        fi | || _        || _        d S Nr   r   )rG   r   r   r   r   wrappedcontextrb   r  r  ri   r   s       r4   r   z"WrappedUserMethodVariable.__init__  sb    

4

5$W[;;F;;;r6   r*   r&   rg   rh   ri   rj   r"   c                     | j                             |           t                                          |||          }| j                             |           |S r8   r  enterr   rp   exitrb   r*   rg   ri   r=   r   s        r4   rp   z'WrappedUserMethodVariable.call_function  Q     	2&&r488"r6   r   r   rr   r   r   r   rp   r   r   s   @r4   r
  r
                 	#	 &	 -		
 
	 	 	 	 	 	 	 	 	 	r6   r
  c                   <     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZ xZS )WrappedUserFunctionVariablerk   Nc                     |                     dd            |                     dd             t                      j        |j        fi | || _        || _        d S r  )rG   r   r   r   r  r  r  s       r4   r   z$WrappedUserFunctionVariable.__init__  s^    

4

5$..v...r6   r*   r&   rg   rh   ri   rj   r"   c                     | j                             |           t                                          |||          }| j                             |           |S r8   r  r  s        r4   rp   z)WrappedUserFunctionVariable.call_function  r  r6   r   r   r  r   s   @r4   r  r    r  r6   r  c                     d fd|D             }fd|                                 D             } ||i |}| j                            ||t          |                    S )Nc                     t          | t          j                  r|                                 S |                                 S r8   )r.   r   TensorVariableget_real_valuer   )xs    r4   convertz-invoke_and_store_as_constant.<locals>.convert  s;    a122 	&##%%%##%%%r6   c                 &    g | ]} |          S r   r   )r   r   r!  s     r4   r   z0invoke_and_store_as_constant.<locals>.<listcomp>  s!    %%%1GGAJJ%%%r6   c                 .    i | ]\  }}| |          S r   r   )r   r>   r?   r!  s      r4   r   z0invoke_and_store_as_constant.<locals>.<dictcomp>  s'    7771a777r6   r   )r:   rB   register_attr_or_moduler   )r*   r   rK   rg   ri   resr!  s         @r4   r   r     s    & & &
 &%%%%%%D7777777F
"d
f

C9,,d## -   r6   c                   r     e Zd Zddhej        Z	 d	 d fdZd Zd Zd Zd	 Z	d
 Z
d Zd Zd Zd Z xZS )rv   closure_scoperU   Nrk   c
                     t                      j        di |
 t          |                                t                    sJ t          |                                t
          j                  sJ t          |t                    sJ || _        || _	        || _
        || _        || _        || _        || _        |d }|| _        |	| _        d S Nr   )r   r   r.   r   r   rQ   CodeTyper<   fn_namerI   rU   rV   rX   rY   rW   r'  wrapped_reconstructible)rb   r+  rI   rU   rV   rX   rY   rW   r'  r,  ri   r   s              r4   r   z#NestedUserFunctionVariable.__init__  s     	""6"""'4466<<<<<$1133U^DDDDD)T*****	" $&? M* $ 	$$$r6   c                     g S r8   r   ra   s    r4   rn   z$NestedUserFunctionVariable.self_args  r   r6   c                 4    | j                                         S r8   )rI   r   ra   s    r4   r_   z#NestedUserFunctionVariable.get_code  s    y++---r6   c                 *   | j         rt          t          j        | j                                        | j        | j                                                  }| j        r| j                                        |_	        | j
        r| j
                                        |_        | j        rj| j                                        }t          |t                    rddlm} t#           ||                    }t          |t"                    sJ ||_        |S )Nr   rO   )rW   ru   rQ   rN   rI   r   rU   r+  rV   r   rX   rR   rY   r.   r;   rS   rP   r<   rT   )rb   rZ   rY   rP   s       r4   rt   z'NestedUserFunctionVariable.get_function  s   < 	&%%!I((**NL++--
 

 = 	C $ @ @ B BD? 	G"&/"D"D"F"FD 		/*==??K+u-- :......"88K#8#899 k400000#.D r6   c                     | j         d uS r8   )rW   ra   s    r4   has_closurez&NestedUserFunctionVariable.has_closure  s    |4''r6   c                     dS )NFr   ra   s    r4   r   z#NestedUserFunctionVariable.has_self  s    ur6   c                     | j         S r8   )rU   ra   s    r4   r   z&NestedUserFunctionVariable.get_globals
  s
    ~r6   c                     ddl m} |                                 }t          j        || j        | j                                        | j        rt          | j        j
                  nd t          d t          t          |                                 j                            D                                 }| j        r| j                                        |_         t#          j        |          j        |i |}|                                 t+          |j        
                                          }t/          |j        j        |           t5          |||          }	t7          |j                  D ]\  }
}| j        j
        |
         }||vsJ t;          ||          rE|}|r||j        vr|j        }|r	||j        v|tA          d| d          |j        |         ||<   r| j        j
        |
         |	|<   ||	fS )Nr   )InlinedClosureVariablec              3   4   K   | ]}t          d           V  d S r8   )r   )r   r   s     r4   	<genexpr>z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s(      SSa)D//SSSSSSr6   zCouldn't find z7 in the symbolic_locals of the inline interpreter stack)!miscr5  r_   rQ   rN   rU   r+  r   rV   r;   r:   ranger   r   rX   keys_as_python_constantrR   rz   r{   r   r   r<   r   r@   rB   r   rL   r   rW   r.   symbolic_localsrH   RuntimeError)rb   rH   rg   ri   r5  rI   rZ   r)   r=   rJ   r   rK   r   cands                 r4   r   z$NestedUserFunctionVariable.bind_args  s   000000}}!NL++--*.-AE$-%&&&TSS5T]]__5P1Q1Q+R+RSSSSS
 
 ? 	L"&/"I"I"K"KD,!$'',d=f==eo++--...777%ffd;;"4#344 	> 	>IC<%c*Dv%%%%$ 677 >  't4+???;D  't4+???<&ffff    $3D9t&*l&8&=d##}$$r6   c                 ~    |                                  }|j        D ] }||j        v r|j        |         |j        |<   !d S r8   )r_   r   r;  )rb   rH   r   rI   r   s        r4   r   z*NestedUserFunctionVariable.export_freevars7  sS    }}# 	I 	ICe+++.3.CC.H&s+	I 	Ir6   c                 2                        fd            | j                                                           | j                  g            t          j        | j        j        j                             | j	        r | j	                   n)                    
                    d           g           | j        r | j                   n)                    
                    d           g           | j        r | j                   n)                    
                    d           g           | j        rd	 | j                                        }                                        |          g           nI# t          $ r  | j                   Y n-w xY w                    
                    d           g                               t!          dd                     | j        r                     fd            | j                                       t!          dd                                         t%          d                                         t!          dd                     d S d S )Nc                  :                          t          d          S )Nr[   )load_import_fromrr   codegens   r4   <lambda>z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>?  s    G,,X7JKK r6      Fc                  0                          dd          S )Nr   wrapsrA  rB  s   r4   rD  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>a  s    00gFF r6   r   r   T)add_push_nullrI   extend_output_create_load_constrU   r$   r0   r   re   rV   create_load_constrW   rX   rY   r   ru   r   r,  r   )rb   rC  rY   s    ` r4   reconstructz&NestedUserFunctionVariable.reconstruct=  s   KKKK	
 	
 	
 		w99$.IIJKKK '	(?@@AAA= 	EGDM""""!!7#<#<T#B#B"CDDD< 	EGDL!!!!!!7#<#<T#B#B"CDDD? 	EGDO$$$$!!7#<#<T#B#B"CDDD 	E*".AACC%%w'A'A+'N'N&OPPPP& * * *()))))* !!7#<#<T#B#B"CDDD21e<<===' 	A!!FFFF   GD0111!!"6q%"@"@AAA!!,q//222!!"6q$"?"?@@@@@	A 	As   AF F/.F/r8   r   )rr   r   r   r]   r   r   rn   r_   rt   r1  r   r   r   r   rM  r   r   s   @r4   rv   rv     s         
"	0N  !%$ 
$ $ $ $ $ $@  . . .  0( ( (    (% (% (%TI I I)A )A )A )A )A )A )Ar6   rv   c                        e Zd Zddhej        Zdd fdZd Zed             Ze	 e
j        d          d                         Z	 	 	 	 	 	 	 	 ddZ xZS )SkipFunctionVariabler   reasonNrk   c                 V     t                      j        di | || _        || _        d S r)  )r   r   r   rP  )rb   r   rP  ri   r   s       r4   r   zSkipFunctionVariable.__init__p  s2    ""6"""
r6   c                     | j         S r8   r   ra   s    r4   r   z'SkipFunctionVariable.as_python_constantu  s
    zr6   c                     t          |          s,t          |                    t          j                              | ||          S r   )r   r   r   r   FUNCTION_MATCHr   s      r4   r   z'SkipFunctionVariable.create_with_sourcex  sI    .u55 	J &++L,GHHIIIs5((((r6   c                  2    t           j        t          j        iS r8   )collections
namedtupler   r   r   r6   r4    fold_through_function_to_wrapperz5SkipFunctionVariable.fold_through_function_to_wrapper  s     "I$F
 	
r6   r*   r&   rg   rh   ri   rj   r"   c                 @   t          j        | j        dd          rt          d| j                    d S | j        |                                                                 v rt          |          rv | j        d D             i d |                                D             } |                                                     | j                  |t                                S | j        t          j        u r`|s^t                    dk    rKd         j        %d                             |j        j                  rfd	}t#          j        |          S 	 t          j        | j                  }d
| j        j         d| d}n# t*          $ r ddh}| j        j        |v rd| j        j         d| j        j         d}n| j        j        a| j        j                            d          rBd| j        j         d| j        j         d}t0          j        j                            |           nAd| j        j         d| j        j         d}t0          j        j                            |           Y nw xY w|| j        rd| j         dndz  }t          |           d S )N_torchdynamo_disableFz.call torch._dynamo.disable() wrapped function c                 6    g | ]}|                                 S r   r   r   r   s     r4   r   z6SkipFunctionVariable.call_function.<locals>.<listcomp>  s$    777Q!&&((777r6   c                 >    i | ]\  }}||                                 S r   r]  r   r>   r?   s      r4   r   z6SkipFunctionVariable.call_function.<locals>.<dictcomp>  s*    HHHA1a**,,HHHr6   )mutable_localr   r   c                     t          | t          j                  r9d         j        rd         j        }nd         }|                     |          S t          d|  d           d S )Nr   )r,  zfunctools.wraps(r   )r.   r   rv   r3   cloner   )r   reconstructiblerg   s     r4   rG  z1SkipFunctionVariable.call_function.<locals>.wraps  sq    b)"FGG MAw~ 2*.q'.*.q'88O8LLL666677777r6   z'skip function z	 in file '_abc	_warningsz.Graph break due to unsupported Python builtin .zM. Please file an issue on GitHub so the PyTorch team can add support for it. optreez)Graph break for an optree C/C++ function zj. Consider using torch.utils._pytree - https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.pyz'Graph break due to unsupported builtin a  . This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind). If it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround. If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use torch.compiler.allow_in_graph.z', r   )rz   r   r   r   rY  keysr   r:   getr    r   rG  r   r3   can_reconstructrB   r   r   LambdaVariablegetfiler   	TypeErrorr   r  r   _dynamoutils	warn_oncerP  )	rb   r*   rg   ri   r   rG  pathmsgknown_python_builtin_moduless	     `      r4   rp   z"SkipFunctionVariable.call_function  s,    !$*.DeLL I	W4:WWXXXXX J$??AAFFHHHH#D&11 I DJ77$777HHHHH E K488::>>tzJJ\^^    J)/)) *D		QQ*d1g.E.EbiFW.X.X*8 8 8 8 8 +E222$7tz22Q
(?QQ$QQQ !7 !7 !706/D,:(,HHHHI^ H Haeakax H H H C J)5
-88BB 6_DJDY _ _\`\f\s _ _ _  M'11#6666	:$*BW 	: 	:Z^ZdZq 	: 	: 	:  M'11#666C!7D 4;>'''''B>C#s   ,F
 
C(I54I5r8   r   r   )rr   r   r   r"   r   r   r   r   r   staticmethodr   	lru_cacherY  rp   r   r   s   @r4   rO  rO  i  s         
	'N      
   ) ) [) Y
 
  \

O#O &O -	O
 
O O O O O O O Or6   rO  c                   H     e Zd ZdZd fdZd fdZ	 	 	 	 	 	 	 	 ddZ xZS )WrapperUserFunctionVariablea3  
    Used to represent a wrapper object that contains the actual callable as an
    attribute. For example, torch.jit.script/trace have the original function at
    their _torchdynamo_inline attribute. Similarly, functions with
    __script_if_tracing_wrapper have the original attr at "__original_fn".
    rk   Nc                 V     t                      j        di | || _        || _        d S r)  )r   r   wrapper_objattr_to_trace)rb   r{  r|  ri   r   s       r4   r   z$WrapperUserFunctionVariable.__init__  s6    ""6"""&*r6   r*   r&   c                 4   || j         k    rkt          | j        | j                   }| j        r.ddlm}   ||t          | j        |                    |          S ddlm} |                    ||          S t                      
                    ||          S )Nr   r   r,   )r|  r   r{  r3   r   r   r   r-   r0   r   r   )rb   r*   rK   r2   r   r-   r   s         r4   r   z'WrapperUserFunctionVariable.var_getattr  s    4%%%$*D,>??C{ 9444444Ir:dk4+H+HII#NNN666666(//C888ww""2t,,,r6   rg   rh   ri   rj   r"   c                     t          j        t          j                                      || t          j        | j                  g||          S r8   )r   r   r   getattr_and_tracerp   r$   r|  ro   s       r4   rp   z)WrapperUserFunctionVariable.call_function  sM     -'
 

-y1$2DEEMMv
 
	
r6   r   r*   r&   r   )rr   r   r   r   r   r   rp   r   r   s   @r4   ry  ry    s         + + + + + +
- - - - - -

#

 &

 -	


 


 

 

 

 

 

 

 

r6   ry  c                  R    t           j                                        rddlm}  | S i S )Nr   traceable_collective_remaps)r   distributedis_available)torch.distributed._functional_collectivesr  r  s    r4   _traceable_collective_remapsr    sD    %%'' +	
 	
 	
 	
 	
 	
 +*Ir6   c                     t           j                                        s
J d            |t                                                      v sJ |j        }|                     d          }t          ||          S )NzIllegal invocation.z)torch.distributed._functional_collectives)r   r  r  r  valuesrr   import_sourcer   )r*   r   
inner_namepath_sources       r4   _traceable_collectives_sourcer    su    ))++BB-BBBB-//66888888J""#NOOKk:...r6   c                        e Zd ZdZd fdZedd            Zed             Zedd	            Z	 	 	 	 	 	 	 	 ddZ	 x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.
    rk   Nc                x     t                      j        |fi | t          |t                    sJ || _        d S r8   )r   r   r.   r   replacement_var)rb   r   r  ri   r   s       r4   r   z*CollectiveFunctionRewriteVariable.__init__!  sG    &&v&&&/+?@@@@@.r6   r*   r&   c                 x    t                               | |          \  }}t          |ft          |fd|i||d|S )Nr3   )r  r3   )r  rewriter   )r*   old_fnr3   r   new_fn
new_sources         r4   r0   z(CollectiveFunctionRewriteVariable.create&  s`    >FFr6RR
0
0VV
VgVV
 
 	
 
 	
r6   c                 J    t          j        |           o| t                      v S r8   )rz   
isfunctionr  )variables    r4   can_rewritez-CollectiveFunctionRewriteVariable.can_rewrite0  s(     x((WX9U9W9W-W	
r6   c                 N    t                      |         }|t          | |          fS r8   )r  r  )r*   r   r  s      r4   r  z)CollectiveFunctionRewriteVariable.rewrite6  s(    -//34R@@@@r6   rg   rh   ri   rj   r"   c                 B   dd l m} ddlm} t	          j        | j                  }t           |j        |i |j	                  }d}d|v r1|d         
                                rt          d| j                    | j        |j        |j        |j        fv rn|                    d          }||j        n|j        d         j        }||vrt'          d|           t(          j                            ||                   |d<   | j                            |||          S )Nr   )REDUCE_OP_TO_STRr   async_opzBCollectiveFunctionRewriteVariable can't support async_op=True for opzUnsupported all_reduce op: )torch.distributedr  r  r  rz   r{   r   r<   r   r   r   r   
all_reducereduce_scatter_tensor_reduce_scatter_baserk  r   r|   defaultr   r   r$   r0   r  rp   )	rb   r*   rg   ri   distr  r{   reduce_op_var	reduce_ops	            r4   rp   z/CollectiveFunctionRewriteVariable.call_function;  sj    	)(((((NNNNNN %dg..	nind5f55?@@F:$6$I$I$K$K^UYU\^^   7O&%
 
 

 #JJt,,M !, ##)$/7 
  000 !Jy!J!JKKK$5<< + F4L #11"dFCCCr6   r   r  r   )rr   r   r   r   r   rv  r0   r  r  rp   r   r   s   @r4   r  r    s         / / / / / /
 
 
 
 \
 
 
 \

 A A A \A'D#'D &'D -	'D
 
'D 'D 'D 'D 'D 'D 'D 'Dr6   r  c                   l     e Zd Zdeddf fdZd Zd Z	 	 	 	 	 	 	 	 ddZdddedefdZ	d Z
d Z xZS )FunctoolsPartialVariablerZ   rk   Nc                      t                      j        di | || _        t          |t                    sJ || _        t          |t                    sJ || _        d S r)  )r   r   rZ   r.   r9   rg   r<   keywords)rb   rZ   rg   r  ri   r   s        r4   r   z!FunctoolsPartialVariable.__init__f  se    ""6"""	$%%%%%	(D))))) r6   c                 ^                        fd            | j                   | j        r                    | j                   | j        s:                    t          t          | j                  dz   d                     d S                     | j                                                   t          | j        
                                          }                                        t          |          t          | j                  z   dz   |d                     d S )Nc                  0                          dd          S )Nr   r   rH  rB  s   r4   rD  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>o  s    g&>&>{I&V&V r6   r   F)rI  rZ   rg   foreachr  rJ  r   r   r  r;   rj  create_call_function_kw)rb   rC  rj  s    ` r4   rM  z$FunctoolsPartialVariable.reconstructn  s   VVVVWWW	9 	'OODI&&&} 	!!"6s49~~7I5"Q"QRRRF,,..///T]''))**++CIIDI,F,JDRWXX	
 	
 	
 	
 	
r6   c                 *    |                                  S r8   r]  ra   s    r4   rt   z%FunctoolsPartialVariable.get_function}      &&(((r6   r*   r&   rg   rh   ri   rj   r"   c                 d    | j         |z   }i | j        |}| j                            |||          S r8   )rg   r  rZ   rp   )rb   r*   rg   ri   merged_argsmerged_kwargss         r4   rp   z&FunctoolsPartialVariable.call_function  s;     i$&34=3F3y&&r;FFFr6   rK   c                     t           j                            t          t	          j        t                    |                    S r8   )r   r$   r0   rs   r   r   r   )rb   r*   rK   s      r4   rx   z%FunctoolsPartialVariable.call_hasattr  s5    )00I%h//66
 
 	
r6   c                     t          j        | j                                        gd | j        D             R i d | j                                        D             S )Nc                 6    g | ]}|                                 S r   r]  )r   r   s     r4   r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<listcomp>  s$    <<<3c$$&&<<<r6   c                 >    i | ]\  }}||                                 S r   r]  r`  s      r4   r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<dictcomp>  s*    KKKTQq!&&((KKKr6   )r   r   rZ   r   rg   r  r:   ra   s    r4   r   z+FunctoolsPartialVariable.as_python_constant  sq     I((**
<<$)<<<
 
 
 LKT]5H5H5J5JKKK
 
 	
r6   c                     t          j        | j                                        gd | j        D             R i d | j                                        D             S )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constantsc                 6    g | ]}|                                 S r   guard_as_python_constant)r   r?   s     r4   r   zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<listcomp>  s$    >>>qa((**>>>r6   c                 >    i | ]\  }}||                                 S r   r  r`  s      r4   r   zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<dictcomp>  s*    QQQ41aq!,,..QQQr6   )r   r   rZ   r  rg   r  r:   ra   s    r4   r  z1FunctoolsPartialVariable.guard_as_python_constant  sq     I..00
>>DI>>>
 
 
 RQ4=;N;N;P;PQQQ
 
 	
r6   r   )rr   r   r   r"   r   rM  rt   rp   r   rx   r   r  r   r   s   @r4   r  r  e  s        !_ !4 ! ! ! ! ! !
 
 
) ) )G#G &G -	G
 
G G G G
6 
c 
o 
 
 
 

 
 

 
 
 
 
 
 
r6   r  c                   "    e Zd Zdddhej        Ze ej        d          dee	de
f         ej        f         fd                        Zed             Zdeddf fd	Zedefd
            Zd Zd Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd Z xZS )PolyfilledFunctionVariabler   
wrapped_fntraceable_fnNrk   .c                     i S r8   r   )r   s    r4   _get_polyfill_handlersz1PolyfilledFunctionVariable._get_polyfill_handlers  s	     	r6   c                 t    t          |                    t          j                              | ||          S r   )r   r   r   rU  r   s      r4   r   z-PolyfilledFunctionVariable.create_with_source  s6    f''(CDDEEEs5((((r6   c                 r    t                      j        di | || _        |                                                     ||          }t          |          sJ d| d|             dD ]*}t          ||d           }|rt          |          sJ |} n+t          d| d          || _        || _	        d S )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionr   )
r   r   r   r  rk  callabler   r<  r  r  )rb   r   ri   handlercandidate_attr	candidater  r   s          r4   r   z#PolyfilledFunctionVariable.__init__  s    ""6"""--//33B;;  XX"Xg"X"XTV"X"XXXX
 	 	N  >>I 	*****(
 PGPPP   & ,r6   c                     | j         S r8   )r  ra   s    r4   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fn  s      r6   c                 .    t          | j        dd          S )N*__torch_dynamo_can_constant_fold_through__F)r   r  ra   s    r4   can_constant_fold_throughz4PolyfilledFunctionVariable.can_constant_fold_through  s    OI5
 
 	
r6   c                 *    |                                  S r8   r]  ra   s    r4   rt   z'PolyfilledFunctionVariable.get_function  r  r6   r*   r&   rg   rh   ri   rj   r"   c                 L   ddl m} |                                 rYt          ||          rI | j        d |D             i d |                                D             }|                    ||          S |                    || j                  }|                    |||          S )Nr   r,   c                 6    g | ]}|                                 S r   r]  r^  s     r4   r   z<PolyfilledFunctionVariable.call_function.<locals>.<listcomp>  s$    ;;;a**,,;;;r6   c                 >    i | ]\  }}||                                 S r   r]  r`  s      r4   r   z<PolyfilledFunctionVariable.call_function.<locals>.<dictcomp>  s*    LLLTQq!..00LLLr6   )	r/   r-   r  r   r   r:   r0   r  rp   )rb   r*   rg   ri   r-   r=   traceable_function_variables          r4   rp   z(PolyfilledFunctionVariable.call_function  s     	FEEEEE))++ 		80M&1
 1
 		8 ;;d;;;LLV\\^^LLL   %++B777&7&>&>r4CT&U&U#*88T6JJJr6   c                 h   |dk    r|                      |||          S t          | j        |d           }|J d| d| j                     t          |          sJ d| d| j                     i }| j        rt          | j        |          |d<   t          |fi |}|                     |||          S )N__call__zMember z not found in z is not callable in r3   )rp   r   r   r   r3   r   r  )rb   r*   rK   rg   ri   methodr   polyfilled_method_variables           r4   r  z&PolyfilledFunctionVariable.call_method  s     :%%b$777$--!!#JT#J#J#J#J!!!6""QQ$Qd$Q$Q$Q$QQQQ; 	> *4; = =GH%?%R%R'%R%R")77D&IIIr6   c                     | j         S r8   r   ra   s    r4   r   z-PolyfilledFunctionVariable.as_python_constant  r   r6   r   rg   rh   ri   rj   rk   r"   )rr   r   r   r"   r   r   r   rw  r   r   r   rQ   rN   r  r   r(   r   propertyr  r  rt   rp   r  r   r   r   s   @r4   r  r    s        
	'	N YtHS#X,>@R,R'S     [ ) ) [)
-2 -D - - - - - -. !R ! ! ! X!
 
 

) ) )K#K &K -	K
 
K K K K,J &	J
 -J 
J J J J&      r6   r  )TritonHOPifierc                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )DynamoTritonHOPifierc                      t          |          r8   )r   )rb   rt  s     r4   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupported  s    #r6   c                 :    t          |t          t          f          S r8   )r.   rv   r   )rb   maybe_callables     r4   is_callablez DynamoTritonHOPifier.is_callable
  s     79MN
 
 	
r6   c                     |j         S r8   rS  )rb   r2   s     r4   	get_valuezDynamoTritonHOPifier.get_value  s
    yr6   c                     ddl m} t          ||          r|                                S t	          dt          |                      d S )Nr   )BaseListVariablezgrid for the triton kernel is )listsr  r.   as_proxyr   type)rb   gridr  s      r4   
check_gridzDynamoTritonHOPifier.check_grid  sY    ++++++d,-- 	I==??"G4::GGHHHHHr6   c                 r    d |                                 D             }|                    ||gi           }|S )Nc                 T    i | ]%\  }}t           j                            |          |&S r   r   r$   r0   r`  s      r4   r   z2DynamoTritonHOPifier.call_grid.<locals>.<dictcomp>  s/    QQQDAq	*11!44aQQQr6   )r:   rp   )rb   r  metar*   s       r4   	call_gridzDynamoTritonHOPifier.call_grid  s;    QQDJJLLQQQ!!"tfb11r6   c           
         ddl m ddlm} d |                                D             }ddlm}m} fd|                                D             }	fd|                                D             }
|                    |	          } ||
t                    }|j
                            d	|d
|j        |||                                d           t          j        d           S )Nr   r#   )ConstDictVariablec                 T    i | ]%\  }}t           j                            |          |&S r   r  r`  s      r4   r   z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>#  s?     
 
 
1 &--a00!
 
 
r6   r   )kernel_side_tabletriton_kernel_wrapper_mutationc                 `    i | ]*\  }}t          |          ||                                +S r   )r.   r   r   r>   r?   r$   s      r4   r   z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>0  sL     
 
 
1!-..
q##%%
 
 
r6   c                 <    i | ]\  }}t          |          ||S r   )r.   r  s      r4   r   z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>5  s@     
 
 
1a!122
q
 
 
r6   rp   r   )
kernel_idxconstant_args_idxr  ri   )r   r$   dictsr  r:   *torch._higher_order_ops.triton_kernel_wrapr  r  add_constant_argsr<   rB   create_proxyr  r  r   )rb   r  gridscombined_args_rawr*   r  combined_argsr  r  constant_argsnon_constant_argsr  r  r$   s                @r4   call_HOPzDynamoTritonHOPifier.call_HOP  si   ......,,,,,,
 
)//11
 
 

	
 	
 	
 	
 	
 	
 	
 	

 
 
 
)//11
 
 


 
 
 
%++--
 
 
 .??NN  !2D99
	*&1%6--//	 	
	
 
	
 
	
 )
 
 	
r6   N)	rr   r   r   r  r  r  r  r  r  r   r6   r4   r  r    sq          
 
 

  I I I  
,
 ,
 ,
 ,
 ,
r6   r  c                   P     e Zd Zd fdZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 d fdZ xZS )TritonKernelVariablerk   Nc                 t     t                      j        di | t                              | |||           d S r)  )r   r    dynamo_triton_hopifier_singletoninit_variable)rb   kernelr  r  ri   r   s        r4   r   zTritonKernelVariable.__init__R  sA    ""6"""(66tVZQUVVVVVr6   r*   r&   rg   rh   ri   rj   r"   c                 <    t                               | |||          S r8   )r
  call_triton_kernelro   s       r4   rp   z"TritonKernelVariable.call_functionV  s%     0BB$
 
 	
r6   c                     |dk    rt                               | |          S |dk    rt                               | |||          S t                                          ||||          S )N__getitem__run)r
  call_getitemcall_runr   r  )rb   r*   rK   rg   ri   r   s        r4   r  z TritonKernelVariable.call_method`  sj     =  3@@tLLLU]]3<<T4QSTTT ww""2tT6:::r6   r   r   r  )rr   r   r   r   rp   r  r   r   s   @r4   r  r  Q  s        W W W W W W
#
 &
 -	

 

 
 
 
; &	;
 -; 
; ; ; ; ; ; ; ; ; ;r6   r  r8   r  )MrW  r   rz   rS   rQ   typingr   r   r   r   r   r   r	   r
   r   r   r   r   bytecode_transformationr   r   excr   r   guardsr   r   r3   r   r   r   r   rq  r   r   r   r   r   r   r   baser    r!   r"   r   r$   "torch.distributed._composable.fsdpr%   ModuleNotFoundErrortorch._dynamo.symbolic_convertr&   torch._guardsr'   r(   r5   r@   rL   r[   r]   r   r   r
  r  r   rv   rO  ry  r  r  r  r  r  r  r  r  r
  r  r   r6   r4   <module>r     s@                 U U U U U U U U U U U U U U U U U U U U  # # # # # # # # H H H H H H H H , , , , , , , , 0 0 0 0 0 0 0 0 N N N N N N N N N N N N                  9 8 8 8 8 8 8 8 8 8 & & & & & &DDDDDDD     %DDDDDD$$$$$$ WT"""A A A A A. . . .
 
 
  (       @B7 B7 B7 B7 B73 B7 B7 B7J=5 =5 =5 =5 =5- =5 =5 =5@     2   (    "6   (    gA gA gA gA gA!9 gA gA gATn n n n n? n n nb%
 %
 %
 %
 %
/ %
 %
 %
P  / / / /LD LD LD LD LD(< LD LD LD^8
 8
 8
 8
 8
 8
 8
 8
v` ` ` ` ` ` ` `F F E E E E EE
 E
 E
 E
 E
> E
 E
 E
P $8#7#9#9  ; ; ; ; ;? ; ; ; ; ;s   <B BB