
    sgϡ                        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/ erd dl1m2Z2 d dl3m4Z4  ede      Z5d4d5dZ6d5d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>d5d Z? G d! d"e:      Z@ G d# d$e+      ZA G d% d&e+      ZBd' ZCd5d(ZD G d) d*e;      ZE G d+ d,e+      ZF G d- d.e+      ZGd d/lHmIZI  G d0 d1eI      ZJ eJ       ZK G d2 d3e+      ZLy# e0$ r dZ/Y w xY w)6    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)boundc                     t        |t              r|S |sddlm} |j	                  | |      S t
        j                  j	                  ||      S Nr   SourcelessBuilder)
isinstancer"   torch._dynamo.variables.builderr-   creater   LazyVariableTracker)txvalsourcer-   s       T/var/www/html/venv/lib/python3.12/site-packages/torch/_dynamo/variables/functions.pywrap_bound_argr6   ,   sE    #'
E ''C00 ,,33C@@    c                     t        |j                               D ]+  \  }}t        |t        t        f      st        | |      ||<   - y N)listitemsr.   tupledictr6   )r2   resultkvs       r5   wrap_args_kwargsrA   :   s>    V\\^$ .1a%'&r1-F1I.r7   c                     i }| j                   j                  }|j                  D ]>  }|j                         ||<   ||v s|j	                  ||   |j                  |             @ |S r9   )outputside_effectsco_cellvarstrack_cell_new
store_cellpop)parentr>   codeclosure_cellsrD   names         r5   init_cellvarsrM   A   so    M==--L    K*99;d6>##M$$7D9IJK
 r7   c                     ddl m}  || ||||      }||_        t        |t              rddlm}	 t         |	|            }|t        |t              sJ ||_        |S )Nr   )FunctionTypepairwise)	typesrO   __kwdefaults__r.   r<   	itertoolsrQ   r=   __annotations__)
rJ   	f_globalsrL   defaultsclosure
kwdefaultsannotationsrO   funcrQ   s
             r5   _create_nested_fnr\   N   sa     #ixAD$D+u%&8K01 *[$"???&DKr7   c                   P    e Zd Zd Zd Z	 	 	 	 	 	 	 	 ddZdddedefdZd	 Z	d
 Z
y)BaseUserFunctionVariablec                 6    | j                         j                  S r9   )get_codeco_filenameselfs    r5   get_filenamez%BaseUserFunctionVariable.get_filenamec   s    }}***r7   c                 6    | j                         j                  S r9   )r`   co_namerb   s    r5   get_namez!BaseUserFunctionVariable.get_namef   s    }}&&&r7   r2   r&   returnc                 L    |j                  | g | j                         ||      S r9   )inline_user_function_return	self_argsrc   r2   argskwargss       r5   call_functionz&BaseUserFunctionVariable.call_functioni   s,     --d4Ndnn6F4N4NPVWWr7   rL   c                     d}	 t        | j                         |      }t
        j                  j                  |      S # t        $ r |dk(  rt        | t              rd}Y Aw xY w)NF__name__T)hasattrget_functionNotImplementedErrorr.   NestedUserFunctionVariabler   r$   r0   rc   r2   rL   r>   s       r5   call_hasattrz%BaseUserFunctionVariable.call_hasattrq   sc    	T..0$7F ))0088 # 	z!j7Q&R	s   =  A A c                 n    t        t        j                  | j                               j                        S r9   )r:   inspect	signaturers   
parametersrb   s    r5   inspect_parameter_namesz0BaseUserFunctionVariable.inspect_parameter_names{   s'    G%%d&7&7&9:EEFFr7   c                     i S r9    )rc   r2   s     r5   closure_varsz%BaseUserFunctionVariable.closure_vars~       	r7   Nr2   r&   rm   List[VariableTracker]rn   Dict[str, VariableTracker]rh   r"   )rq   
__module____qualname__rd   rg   ro   strr"   rw   r|   r   r~   r7   r5   r^   r^   b   sb    +'X#X &X -	X
 
X96 9c 9o 9Gr7   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                 f    t        |j                  t        j                                | ||      S Nr4   )r   
make_guardr   CLOSURE_MATCHclsvaluer4   s      r5   create_with_sourcez'UserFunctionVariable.create_with_source   s(    f''(B(BCD5((r7   rh   c                 ,   t        |   di | t        |dd      rd| _        nd| _        t	        |t
        j                  t        j                  j                  f      sJ dt        |       d|        t        j                  |d|      }|| _        y )N_dynamo_marked_constantFTzexpected FunctionType found  _torchdynamo_inliner~   )super__init__getattrr   r.   rR   rO   torchjitScriptFunctionr!   ry   getattr_staticr   )rc   r   r   rn   	__class__s       r5   r   zUserFunctionVariable.__init__   s    "6"20%8#D$D##UYY%=%=>
 	=)'"+at<	= 
 ##B(=rB&(r7   c                 X    t        | t              r| j                  S t        |          S r9   )r   r   r   r   as_python_constantrc   r   s    r5   r   z'UserFunctionVariable.as_python_constant   s%    $,-77Nw)++r7   c                     g S r9   r~   rb   s    r5   rk   zUserFunctionVariable.self_args   r   r7   c                     | j                   S r9   r   rb   s    r5   rs   z!UserFunctionVariable.get_function       wwr7   c                 .    | j                   j                  S r9   )r   __code__rb   s    r5   r`   zUserFunctionVariable.get_code   s    wwr7   c                 "    t         j                  S r9   )rR   rO   rb   s    r5   python_typez UserFunctionVariable.python_type   s    !!!r7   c                 4    t        | j                  dd       d uS )N__self__)r   r   rb   s    r5   has_selfzUserFunctionVariable.has_self   s    tww
D1==r7   c                 .    | j                   j                  S r9   )r   __globals__rb   s    r5   get_globalsz UserFunctionVariable.get_globals   s    ww"""r7   c                 n   | j                   rJ |j                  j                  }t        j                  t
        |      }| j                  }|j                  xs g }t        |      D 	cg c])  \  }}	| j                  d nt        | j                  |      + }
}}	t        j                  |j                  |j                  |j                  t!        t#        ||
      D cg c]  \  }} |||       c}}      |j$                        }|j&                  r||j&                  D ci c])  }|| j                  d nt        | j                  |d      + }}|j&                  j)                         D ci c]  \  }}| ||||          c}}|_         t+        j,                  |      j.                  |i |}|j1                          t3        |j4                  j)                               }t7        ||       t9        |||j                        }| j                  j$                  xs d}t;        |      t;        | j                  j                  j<                        k(  sJ t#        t?        j@                         | j                  j                  j<                  |      D ]Z  \  }}}|dk(  rI| j                  rtC        | j                  d      nd }tE        jF                  |jH                  |      ||<   V|jK                  ||      }||||<   p| j                  rdd	l&m'} |j                  jP                  }||v r||   }ntS        tC        | j                  d
      |      }tC        |d      }	   |||      |jH                        }|jY                         |jZ                  vr|||<   |j]                  ||      }|j_                  ||       |||<   6ddl&m0} |jc                  ||jH                        ||<   ] ||fS c c}	}w c c}}w c c}w c c}}w # tT        $ r tE        jV                         }Y w xY w)N)r2   )r3   r4   T)is_kwr~   r   r   r   VariableBuilder__closure__cell_contentsr,   )2r   rC   root_tx	functoolspartialr6   r   __defaults__	enumerater4   r   rR   rO   r   r   rq   r<   zipr   rS   r;   ry   rz   bindapply_defaultsr=   	argumentsrA   rM   lenco_freevarsrT   countr   r   UserDefinedClassVariabler   match_nested_cellbuilderr   rD   r   
ValueErrorDeletedVariablerL   mutated_closure_cell_contentstrack_cell_existingrG   r-   r0   )rc   rI   rm   rn   r2   wrapr   rW   idx_defaults_sourcesargr4   	fake_funcr?   kwdefaults_sourcesr@   r)   r>   rK   rX   rL   cellvarr   rD   outclosure_cellclosure_cell_contentscontents_varr-   s                                  r5   	bind_argszUserFunctionVariable.bind_args   s	   ####]]""  B7!%??(b $H-
Q KK'D^DKK-MM
 
 &&KKNNKK (+85E'F#V S0 NN
	 
 **	"  ;;& #DKK$?@" " --335(Aq 4A&8&;<<(I$
 2!!),114B6Beoo++-.V$%ffbkkB''%%+7|s477#3#3#?#?@@@@"OOtww//;;W 
 ?	TOCt {"AEDKK=RV(AA&&! t
 **46?#&F4L[[8#)==#=#=L|+*40'4&t{{MBC( 1;(/1-G,? &(=,"00,2L 2668#%#C#CD ,8F4L$ +>>|TR$//(
 $'F4L ;#4#;#;B@R@R#SF4L?	TB }$$K
"(X  * G+4+D+D+FLGs*   *.O=P$.P	1P*PP43P4c                      y r9   r~   )rc   rI   childs      r5   export_freevarsz$UserFunctionVariable.export_freevars%  s    r7   r2   r&   rL   c                 T   | j                   rt        | j                   |      nd }	 t        j                  | j                  |      }|r t        j                  j                  ||      S ddl
m} |j                  ||      S # t
        $ r d|i}t        j                  | |fi |cY S w xY w)Nr4   r   r,   )r4   r   ry   r   r   AttributeErrorr   GetAttrVariabler1   r0   r   r-   )rc   r2   rL   r4   subobjoptionsr-   s          r5   var_getattrz UserFunctionVariable.var_getattr(  s    26++DKK.4	D++DGGT:F 0077GG. ''F33  	D(G,,T4C7CC	Ds    B   $B'&B'c                 l    t        | j                  |      }t        j                  j	                  |      S r9   )rr   r   r   r$   r0   rv   s       r5   rw   z!UserFunctionVariable.call_hasattr5  s)    $'))0088r7   c                     | j                   r't        || j                  | j                         ||      S t        |   |||      S r9   )r   invoke_and_store_as_constantr   rg   r   ro   )rc   r2   rm   rn   r   s       r5   ro   z"UserFunctionVariable.call_function9  sF     /DGGT]]_dF  w$Rv66r7   )Frh   Nr   )rq   r   r   __doc__r^   _nonvar_fieldsclassmethodr   r   r   rk   rs   r`   r   r   r   r   r   r   r   r"   rw   ro   __classcell__r   s   @r5   r   r      s    7 	 
"	0	0N ) )), ">#l%\45 4S 496 9c 9o 97#7 &7 -	7
 
7 7r7   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 methodrh   c                 6    t        |   dd|i| || _        y )Nr   r~   )r   r   obj)rc   r   r   rn   r   s       r5   r   zUserMethodVariable.__init__J  s    )B)&)r7   c                 h    | j                   j                   d| j                   d| j                   dS )N(z, ))r   rq   r   r   rb   s    r5   __str__zUserMethodVariable.__str__N  s.    ..))*!DGG9BtxxjBBr7   c                     | j                   gS r9   )r   rb   s    r5   rk   zUserMethodVariable.self_argsQ  s    zr7   c                 "    t         j                  S r9   )rR   
MethodTyperb   s    r5   r   zUserMethodVariable.python_typeT  s    r7   c                 "   |j                   j                         rt        | j                  t        j
                        ryt        | j                  dd      }||j                  d      r|dk7  s| j                  r| j                  j                  || j                  j                  ||| j                        S t        d| j                  t        j                  j                  u r>t	        j                  | j                        j!                  || j                  g||      S | j                  rQt        | j                  j"                  | j                  j                        }t%        ||| j'                         ||      S t(        | A  |||      S )Nr    z	torch.nn.ztorch.nn.utils.parametrize)constant)rC   is_root_tracerr.   r   r   NNModuleVariabler   r   
startswithr   call_methodrq   r%   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariablero   r   r   rg   r   )rc   r2   rm   rn   module_attrr   r   s         r5   ro   z UserMethodVariable.call_functionW  sJ   $ 99##%*HHi00+
 "$''<<K '**;7#??##xx++(($AQAQ ,   ),;;NNN99$''BPPTXX%%v  )9)9:B/BvVVw$Rv66r7   c                 &    t         |          dd  S )Nr   )r   r|   r   s    r5   r|   z*UserMethodVariable.inspect_parameter_names  s    w.044r7   r   r   )rq   r   r   r   r   r   r   rk   r   ro   r|   r   r   s   @r5   r   r   G  sY    .C C *7#*7 &*7 -	*7
 
*7X5 5r7   r   c                   <     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZ xZS )WrappedUserMethodVariablec                     |j                  dd        |j                  dd        t        |   |j                  |j                  fi | || _        || _        y Nr   r   )rH   r   r   r   r   wrappedcontextrc   r  r  rn   r   s       r5   r   z"WrappedUserMethodVariable.__init__  sI    

4

5$W[[;F;r7   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S r9   r  enterr   ro   exitrc   r2   rm   rn   r>   r   s        r5   ro   z'WrappedUserMethodVariable.call_function  A     	2&r48"r7   r   r   rq   r   r   r   ro   r   r   s   @r5   r  r    8    	#	 &	 -		
 
	 	r7   r  c                   <     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZ xZS )WrappedUserFunctionVariablec                     |j                  dd        |j                  dd        t        |   |j                  fi | || _        || _        y r  )rH   r   r   r   r  r  r  s       r5   r   z$WrappedUserFunctionVariable.__init__  sC    

4

5$.v.r7   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S r9   r	  r  s        r5   ro   z)WrappedUserFunctionVariable.call_function  r  r7   r   r   r  r   s   @r5   r  r    r  r7   r  c                     d }|D cg c]
  } ||       }}|j                         D ci c]  \  }}| ||       }}} ||i |}	| j                  j                  |	|t        |            S c c}w c c}}w )Nc                 v    t        | t        j                        r| j                         S | j	                         S r9   )r.   r   TensorVariableget_real_valuer   )xs    r5   convertz-invoke_and_store_as_constant.<locals>.convert  s0    a112##%%##%%r7   r   )r;   rC   register_attr_or_moduler   )
r2   r   rL   rm   rn   r  r  r?   r@   ress
             r5   r   r     s    &
 !%%1GAJ%D%(.71am7F7
d
f
C99,,d# -   &7s
   A1A6c                   |     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 )ru   closure_scoperV   c
                 b   t        |   di |
 t        |j                         t              sJ t        |j                         t
        j                        sJ t        |t              sJ || _        || _	        || _
        || _        || _        || _        || _        |d }|| _        |	| _        y Nr~   )r   r   r.   r   r   rR   CodeTyper=   fn_namerJ   rV   rW   rY   rZ   rX   r  wrapped_reconstructible)rc   r!  rJ   rV   rW   rY   rZ   rX   r  r"  rn   r   s              r5   r   z#NestedUserFunctionVariable.__init__  s     	"6"'446<<<$113U^^DDD)T***	" $&? M* $ 	$r7   c                     g S r9   r~   rb   s    r5   rk   z$NestedUserFunctionVariable.self_args  r   r7   c                 6    | j                   j                         S r9   )rJ   r   rb   s    r5   r`   z#NestedUserFunctionVariable.get_code  s    yy++--r7   c                 D   | j                   rt        t        j                  | j                  j                         | j                  | j                  j                               }| j                  r| j                  j                         |_	        | j                  r| j                  j                         |_        | j                  rZ| j                  j                         }t        |t              rddlm} t#         ||            }t        |t"              sJ ||_        |S )Nr   rP   )rX   rt   rR   rO   rJ   r   rV   r!  rW   r   rY   rS   rZ   r.   r<   rT   rQ   r=   rU   )rc   r[   rZ   rQ   s       r5   rs   z'NestedUserFunctionVariable.get_function  s    <<%%!!II((*NNLL++-

 == $ @ @ BD??"&//"D"D"FD**==?K+u-."8K#89 k4000#.D r7   c                     | j                   d uS r9   )rX   rb   s    r5   has_closurez&NestedUserFunctionVariable.has_closure  s    ||4''r7   c                      y)NFr~   rb   s    r5   r   z#NestedUserFunctionVariable.has_self  s    r7   c                     | j                   S r9   )rV   rb   s    r5   r   z&NestedUserFunctionVariable.get_globals
  s    ~~r7   c                 D   ddl m} | j                         }t        j                  || j
                  | j                  j                         | j                  rt        | j                  j                        nd t        d t        t        | j                         j                              D                    }| j                  r| j                  j                         |_         t#        j$                  |      j&                  |i |}|j)                          t+        |j,                  j                               }t/        |j0                  j2                  |       t5        |||      }	t7        |j                        D ]  \  }
}| j8                  j                  |
   }||vsJ t;        ||      rS|}|r+||j<                  vr|j>                  }|r||j<                  vr|tA        d| d      |j<                  |   ||<   | j8                  j                  |
   |	|<    ||	fS )Nr   )InlinedClosureVariablec              3   2   K   | ]  }t        d         y wr9   )r   ).0r   s     r5   	<genexpr>z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s     Sa)D/Ss   zCouldn't find z7 in the symbolic_locals of the inline interpreter stack)!miscr+  r`   rR   rO   rV   r!  r   rW   r<   r;   ranger   r   rY   keys_as_python_constantrS   ry   rz   r   r   r=   r   rA   rC   r   rM   r   rX   r.   symbolic_localsrI   RuntimeError)rc   rI   rm   rn   r+  rJ   r[   r)   r>   rK   r   rL   r   cands                 r5   r   z$NestedUserFunctionVariable.bind_args  s   0}}!!NNLL++-*.--E$--%%&TS5T]]_5P5P1Q+RSS
 ??"&//"I"I"KD,!!$',,d=f=eoo++-...7%ffd;"4#3#34 	>IC<<%%c*Dv%%%$ 67 t4+?+??;;D t4+?+??<&(.ef   $33D9t&*ll&8&8&=d#'	>* }$$r7   c                     | j                         }|j                  D ]-  }||j                  v s|j                  |   |j                  |<   / y r9   )r`   r   r2  )rc   rI   r   rJ   r   s        r5   r   z*NestedUserFunctionVariable.export_freevars7  sM    }}## 	ICe+++.3.C.CC.H&&s+	Ir7   c                    j                  fd        | j                         j                  j                  | j                        g        t        j                  | j                  j                  j                               | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r=	 | j                  j                         }j                  j                  |      g       n!j                  j                  d       g       j                  t!        dd             | j"                  rwj                  fd        | j"                         j                  t!        dd             j                  t%        d             j                  t!        dd             y y # t        $ r  | j                         Y w xY w)Nc                  0     j                  t        d      S )Nr\   )load_import_fromrq   codegens   r5   <lambda>z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>?  s    G,,X7JK r7      Fc                  (     j                  dd      S )Nr   wrapsr8  r9  s   r5   r;  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>a  s    00gF r7   r   r   T)add_push_nullrJ   extend_output_create_load_constrV   r$   r0   r   rf   rW   create_load_constrX   rY   rZ   r   rt   r   r"  r   )rc   r:  rZ   s    ` r5   reconstructz&NestedUserFunctionVariable.reconstruct=  s   K	
 			w99$..IJK ''		(?(?@A==DMM"!!7#<#<T#B"CD<<DLL!!!7#<#<T#B"CD??DOO$!!7#<#<T#B"CD*"..AAC%%w'A'A+'N&OP !!7#<#<T#B"CD21e<=''!!F D001!!"6q%"@A!!,q/2!!"6q$"?@ ( ' *(()*s   ;I I32I3r9   r   )rq   r   r   r^   r   r   rk   r`   rs   r'  r   r   r   r   rD  r   r   s   @r5   ru   ru     se     
"	0	0N  !%$ 
$@.0((%TI)Ar7   ru   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   reasonNc                 @    t        |   di | || _        || _        y r  )r   r   r   rG  )rc   r   rG  rn   r   s       r5   r   zSkipFunctionVariable.__init__p  s!    "6"
r7   c                     | j                   S r9   r   rb   s    r5   r   z'SkipFunctionVariable.as_python_constantu  s    zzr7   c                 |    t        |      s(t        |j                  t        j                                | ||      S r   )r   r   r   r   FUNCTION_MATCHr   s      r5   r   z'SkipFunctionVariable.create_with_sourcex  s2    .u5 &++L,G,GHI5((r7   c                  B    t         j                  t        j                  iS r9   )collections
namedtupler   r   r~   r7   r5    fold_through_function_to_wrapperz5SkipFunctionVariable.fold_through_function_to_wrapper  s     ""I$F$F
 	
r7   c                    t        j                  | j                  dd      rt        d| j                          y | j                  | j	                         j                         v rt        |      r | j                  D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}} | j	                         j                  | j                        |t                     S | j                  t        j                  u ra|s_t              dk(  rQd   j                  (d   j                  |j                   j"                        rfd}t%        j&                  |      S 	 t        j(                  | j                        }	d| j                  j*                   d	|	 d
}
|
| j:                  rd| j:                   d
ndz  }
t        |
       y c c}w c c}}w # t,        $ rB ddh}| j                  j.                  |v r2d| j                  j.                   d| j                  j*                   d}
n| j                  j.                  | j                  j.                  j1                  d      r[d| j                  j.                   d| j                  j*                   d}
t2        j4                  j6                  j9                  |
       nZd| j                  j.                   d| j                  j*                   d}
t2        j4                  j6                  j9                  |
       Y w xY w)N_torchdynamo_disableFz.call torch._dynamo.disable() wrapped function )mutable_localr   r   c                     t        | t        j                        r6d   j                  rd   j                  }nd   }| j	                  |      S t        d|  d       y )Nr   )r"  zfunctools.wraps(r   )r.   r   ru   r4   cloner   )r   reconstructiblerm   s     r5   r>  z1SkipFunctionVariable.call_function.<locals>.wraps  sW    b)"F"FGAw~~*.q'..*.q'88O8LL 0A67r7   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   )ry   r   r   r   rP  keysr   r   r;   getr    r   r>  r   r4   can_reconstructrC   r   r   LambdaVariablegetfiler   	TypeErrorr   r   r   _dynamoutils	warn_oncerG  )rc   r2   rm   rn   r  r?   r@   r   r>  pathmsgknown_python_builtin_moduless     `         r5   ro   z"SkipFunctionVariable.call_function  s    !!$**.DeLJ4::,WX JJ$??AFFHH#D&1DJJ267Q!&&(79?HA1a**,,HE K488:>>tzzJ\^  JJ)//)D	QQ*d1g.E.EbiiFWFW.X8 ++E22$7tzz2'

(?(?'@	$qQF 4;;SQ'B>C#A 8H8  !706/D,::((,HHHI^I^H__`aeakakaxax`y zG H  JJ))5

--88B DDJJDYDYCZZ[\`\f\f\s\s[t u^ _  MM''11#6 B$**BWBWAXXYZ^ZdZdZqZqYr 	s9 	:  MM''11#6C!7s   G0/G5<G; ;EMMr9   r   r   )rq   r   r   r"   r   r   r   r   r   staticmethodr   	lru_cacherP  ro   r   r   s   @r5   rF  rF  i  s     
	'	'N
 ) ) Y
  

O#O &O -	O
 
Or7   rF  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".
    c                 @    t        |   di | || _        || _        y r  )r   r   wrapper_objattr_to_trace)rc   rm  rn  rn   r   s       r5   r   z$WrapperUserFunctionVariable.__init__  s#    "6"&*r7   c                    || j                   k(  rmt        | j                  | j                         }| j                  r)ddlm}   ||t        | j                  |            |      S ddlm} |j                  ||      S t        | )  ||      S )Nr   r   r,   )rn  r   rm  r4   r   r   r   r-   r0   r   r   )rc   r2   rL   r3   r   r-   r   s         r5   r   z'WrapperUserFunctionVariable.var_getattr  sz    4%%%$**D,>,>?C{{4Ir:dkk4+HI#NN6(//C88w"2t,,r7   c                     t        j                  t        j                        j	                  || t        j
                  | j                        g||      S r9   )r   r   r   getattr_and_tracero   r$   rn  rl   s       r5   ro   z)WrapperUserFunctionVariable.call_function  sL     --''

-y11$2D2DEMMv
	
r7   r   r2   r&   r   )rq   r   r   r   r   r   ro   r   r   s   @r5   rk  rk    s=    +
-

#

 &

 -	


 


r7   rk  c                  R    t         j                  j                         rddlm}  | S i S )Nr   traceable_collective_remaps)r   distributedis_available)torch.distributed._functional_collectivesru  rt  s    r5   _traceable_collective_remapsry    s&    %%'	
 +*Ir7   c                     t         j                  j                         sJ d       |t               j	                         v sJ |j
                  }| j                  d      }t        ||      S )NzIllegal invocation.z)torch.distributed._functional_collectives)r   rv  rw  ry  valuesrq   import_sourcer   )r2   r   
inner_namepath_sources       r5   _traceable_collectives_sourcer    sb    ))+B-BB+-/668888J""#NOKk:..r7   c                   p     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.
    c                X    t        |   |fi | t        |t              sJ || _        y r9   )r   r   r.   r   replacement_var)rc   r   r  rn   r   s       r5   r   z*CollectiveFunctionRewriteVariable.__init__!  s.    &v&/+?@@@.r7   c                 j    t         j                  | |      \  }}t        |ft        |fd|i||d|S )Nr4   )r  r4   )r  rewriter   )r2   old_fnr4   r   new_fn
new_sources         r5   r0   z(CollectiveFunctionRewriteVariable.create&  sM    >FFr6R
0
0V
VgV
 	
 	
r7   c                 H    t        j                  |       xr | t               v S r9   )ry   
isfunctionry  )variables    r5   can_rewritez-CollectiveFunctionRewriteVariable.can_rewrite0  s%     x(WX9U9W-W	
r7   c                 8    t               |   }|t        | |      fS r9   )ry  r  )r2   r   r  s      r5   r  z)CollectiveFunctionRewriteVariable.rewrite6  s"    -/34R@@@r7   c                 z   dd l m} ddlm} t	        j
                  | j                        }t         |j                  |i |j                        }d}d|v r+|d   j                         rt        d| j                          | j                  |j                  |j                  |j                  fv ro|j                  d      }||j                   n|j"                  d   j$                  }||vrt'        d|       t(        j*                  j-                  ||         |d<   | j.                  j1                  |||      S )Nr   )REDUCE_OP_TO_STRr~   async_opzBCollectiveFunctionRewriteVariable can't support async_op=True for opzUnsupported all_reduce op: )torch.distributedrv  rx  r  ry   rz   r   r=   r   r   r   r   
all_reducereduce_scatter_tensor_reduce_scatter_baser]  r   r{   defaultr   r   r$   r0   r  ro   )	rc   r2   rm   rn   distr  rz   reduce_op_var	reduce_ops	            r5   ro   z/CollectiveFunctionRewriteVariable.call_function;  s<    	)N %%dgg.	ninnd5f5??@F:$6$I$I$KTUYU\U\T]^ 77OO&&%%
 

 #JJt,M !, ##))$/77 
  00 #>yk!JKK$55<< +F4L ##11"dFCCr7   r   rr  r   )rq   r   r   r   r   rh  r0   r  r  ro   r   r   s   @r5   r  r    s{    /
 
 
 
 

 A A'D#'D &'D -	'D
 
'Dr7   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 )FunctoolsPartialVariabler[   rh   Nc                     t        |   di | || _        t        |t              sJ || _        t        |t              sJ || _        y r  )r   r   r[   r.   r:   rm   r=   keywords)rc   r[   rm   r  rn   r   s        r5   r   z!FunctoolsPartialVariable.__init__f  sH    "6"	$%%%	(D))) r7   c                 <   j                  fd        | j                         | j                  rj                  | j                         | j                  s2j                  t        t        | j                        dz   d             y j                  | j                  j                                t        | j                  j                               }j                  j                  t        |      t        | j                        z   dz   |d             y )Nc                  (     j                  dd      S )Nr   r   r?  r9  s   r5   r;  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>o  s    g&>&>{I&V r7   r   F)r@  r[   rm   foreachr  rA  r   r   r{  r<   r\  create_call_function_kw)rc   r:  r\  s    ` r5   rD  z$FunctoolsPartialVariable.reconstructn  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r7   c                 "    | j                         S r9   r   rb   s    r5   rs   z%FunctoolsPartialVariable.get_function}      &&((r7   r2   r&   c                 z    | j                   |z   }i | j                  |}| j                  j                  |||      S r9   )rm   r  r[   ro   )rc   r2   rm   rn   merged_argsmerged_kwargss         r5   ro   z&FunctoolsPartialVariable.call_function  s>     ii$&34==3F3yy&&r;FFr7   rL   c                     t         j                  j                  t        t	        j
                  t              |            S r9   )r   r$   r0   rr   r   r   r   )rc   r2   rL   s      r5   rw   z%FunctoolsPartialVariable.call_hasattr  s0    ))00I%%h/6
 	
r7   c                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w r9   )r   r   r[   r   rm   r  r;   )rc   r   r?   r@   s       r5   r   z+FunctoolsPartialVariable.as_python_constant  s      II((*
26))<3c$$&<
 6:]]5H5H5JKTQq!&&((K
 	
<K   B
1Bc                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants)r   r   r[   guard_as_python_constantrm   r  r;   )rc   r@   r?   s      r5   r  z1FunctoolsPartialVariable.guard_as_python_constant  s      II..0
48II>qa((*>
 <@==;N;N;PQ41aq!,,..Q
 	
>Qr  r   )rq   r   r   r"   r   rD  rs   ro   r   rw   r   r  r   r   s   @r5   r  r  e  su    !_ !4 !
)G#G &G -	G
 
G
6 
c 
o 


r7   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_fnNrh   .c                     i S r9   r~   )r   s    r5   _get_polyfill_handlersz1PolyfilledFunctionVariable._get_polyfill_handlers  s	     	r7   c                 f    t        |j                  t        j                                | ||      S r   )r   r   r   rL  r   s      r5   r   z-PolyfilledFunctionVariable.create_with_source  s(    f''(C(CDE5((r7   c                 *   t        |   di | || _        | j                         j	                  ||      }t        |      sJ d| d|        dD ]"  }t        ||d       }|st        |      sJ |} n t        d| d      || _        || _	        y )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionr~   )
r   r   r   r  r]  callabler   r3  r  r  )rc   r   rn   handlercandidate_attr	candidater  r   s          r5   r   z#PolyfilledFunctionVariable.__init__  s    "6"--/33B; X$5gY>STVSW"XX 
 	N  >I	***(	 #G9,OP  & ,r7   c                     | j                   S r9   )r  rb   s    r5   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fn  s       r7   c                 0    t        | j                  dd      S )N*__torch_dynamo_can_constant_fold_through__F)r   r  rb   s    r5   can_constant_fold_throughz4PolyfilledFunctionVariable.can_constant_fold_through  s    OOI5
 	
r7   c                 "    | j                         S r9   r  rb   s    r5   rs   z'PolyfilledFunctionVariable.get_function  r  r7   c                    ddl m} | j                         r|t        ||      rp | j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}}|j                  ||      S |j                  || j                        }	|	j                  |||      S c c}w c c}}w r+   )
r/   r-   r  r   r   r   r;   r0   r  ro   )
rc   r2   rm   rn   r-   r  r?   r@   r>   traceable_function_variables
             r5   ro   z(PolyfilledFunctionVariable.call_function  s     	F))+0M&1
 6:;a**,;=C\\^LTQq!..00L  %++B77&7&>&>r4CTCT&U#*88T6JJ <Ls   C Cc                 b   |dk(  r| j                  |||      S t        | j                  |d       }|J d| d| j                          t        |      sJ d| d| j                          i }| j                  rt        | j                  |      |d<   t        |fi |}|j                  |||      S )N__call__zMember z not found in z is not callable in r4   )ro   r   r   r   r4   r   r  )rc   r2   rL   rm   rn   methodr   polyfilled_method_variables           r5   r   z&PolyfilledFunctionVariable.call_method  s     :%%b$77$-!JWTF.	#JJ!6"QgdV3Gy$QQ";; *4;; =GH%?%R'%R")77D&IIr7   c                     | j                   S r9   r   rb   s    r5   r   z-PolyfilledFunctionVariable.as_python_constant  r   r7   r   rm   r   rn   r   rh   r"   )rq   r   r   r"   r   r   r   ri  r   r   r   rR   rO   r  r   r(   r   propertyr  r  rs   ro   r   r   r   r   s   @r5   r  r    s    
	'	'	N YtHS#X,>@R@R,R'S    ) )
-2 -D -. !R ! !

)K#K &K -	K
 
K,J &	J
 -J 
J&r7   r  )TritonHOPifierc                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)DynamoTritonHOPifierc                     t        |      r9   )r   )rc   rf  s     r5   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupported  s    #r7   c                 .    t        |t        t        f      S r9   )r.   ru   r   )rc   maybe_callables     r5   is_callablez DynamoTritonHOPifier.is_callable
  s    79MN
 	
r7   c                     |j                   S r9   rJ  )rc   r3   s     r5   	get_valuezDynamoTritonHOPifier.get_value  s    yyr7   c                 v    ddl m} t        ||      r|j                         S t	        dt        |              y )Nr   )BaseListVariablezgrid for the triton kernel is )listsr  r.   as_proxyr   type)rc   gridr  s      r5   
check_gridzDynamoTritonHOPifier.check_grid  s0    +d,-==?":4:,GHr7   c                     |j                         D ci c]%  \  }}t        j                  j                  |      |' }}}|j	                  ||gi       }|S c c}}w r9   )r;   r   r$   r0   ro   )rc   r  metar2   r?   r@   s         r5   	call_gridzDynamoTritonHOPifier.call_grid  sV    DHJJLQDAq	**11!4a7QQ!!"tfb1 Rs   *Ac           
         ddl m} ddlm} |j	                         D ci c]%  \  }}t
        j                  j                  |      |' }	}}ddlm}
m	} |j	                         D ci c]"  \  }}t        ||      r||j                         $ }}}|	j	                         D ci c]  \  }}t        ||      s|| }}}|
j                  |      } ||t              }|j                  j                  d|d|j                   |||j#                         d       t        j                  d       S c c}}w c c}}w c c}}w )	Nr   r#   )ConstDictVariabler   )kernel_side_tabletriton_kernel_wrapper_mutationro   r~   )
kernel_idxconstant_args_idxr  rn   )r   r$   dictsr  r;   r   r0   *torch._higher_order_ops.triton_kernel_wrapr  r  r.   r   add_constant_argsr=   rC   create_proxyr  r  )rc   r  gridscombined_args_rawr2   r$   r  r?   r@   combined_argsr  r  constant_argsnon_constant_argsr  r  s                   r5   call_HOPzDynamoTritonHOPifier.call_HOP  sP   ., *//1
1 &&--a0!3
 

	
 *//1
1!-. q##%%
 
 &++-
1a!12 qD
 
 .??N !2D9
		*&11%6--/		
	
 ))
 	
M



s   *D/('D5%D;N)	rq   r   r   r  r  r  r  r  r  r~   r7   r5   r  r    s!    

I
,
r7   r  c                   P     e Zd Zd fdZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 d fdZ xZS )TritonKernelVariablec                 T    t        |   di | t        j                  | |||       y r  )r   r    dynamo_triton_hopifier_singletoninit_variable)rc   kernelr  r  rn   r   s        r5   r   zTritonKernelVariable.__init__R  s(    "6"(66tVZQUVr7   c                 2    t         j                  | |||      S r9   )r  call_triton_kernelrl   s       r5   ro   z"TritonKernelVariable.call_functionV  s      0BB$
 	
r7   c                     |dk(  rt         j                  | |      S |dk(  rt         j                  | |||      S t        |   ||||      S )N__getitem__run)r  call_getitemcall_runr   r   )rc   r2   rL   rm   rn   r   s        r5   r   z TritonKernelVariable.call_method`  sW     = 3@@tLLU]3<<T4QSTT w"2tT6::r7   r   r   r  )rq   r   r   r   ro   r   r   r   s   @r5   r  r  Q  s\    W
#
 &
 -	

 

; &	;
 -; 
; ;r7   r  r9   rr  )MrN  r   ry   rT   rR   typingr   r   r   r   r   r   r	   r
   r   r   r   r   bytecode_transformationr   r   excr   r   guardsr   r   r4   r   r   r   r   rc  r   r   r   r   r   r   r   baser    r!   r"   r   r$   "torch.distributed._composable.fsdpr%   ModuleNotFoundErrortorch._dynamo.symbolic_convertr&   torch._guardsr'   r(   r6   rA   rM   r\   r^   r   r   r  r  r   ru   rF  rk  ry  r  r  r  r  r  r  r  r  r  r~   r7   r5   <module>r     sx        U U U  # H , 0 N N   9 8 &D
 D$ T"A.
( @B73 B7J=5- =5@ 2 ("6 ( gA!9 gATn? nb%
/ %
P/LD(< LD^8
 8
v` `F FE
> E
P $8#9  ;? ;c!  s   <E EE