
    iCW                     p   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	 ddl
mZ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 d	d
lmZmZ d	dlmZ erddlmZ ddl m!Z! dZ" 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' G d de$      Z( G d de(      Z) G d de$      Z*y)a  
This module provides iterator-related variable tracking functionality for Dynamo.
It implements variable classes for handling Python iterators and itertools functions
during symbolic execution and tracing.

The module includes:
- Base iterator variable classes for tracking iterator state
- Implementations of built-in iterators (zip, map, filter)
- Support for itertools functions (product, accumulate, combinations, etc.)
- Mutation tracking and reconstruction capabilities for iterator operations

These classes integrate with Dynamo's variable tracking system to enable proper
handling of iterator operations during code transformation and optimization.
    N)CallableSequence)AnyTYPE_CHECKINGUnion   )graph_break_hints	polyfills	variables)create_build_tuplecreate_call_functioncreate_call_function_excreate_instruction)handle_observed_exceptionObservedUserStopIterationraise_observed_exceptionunimplemented	UserError   )ValueMutationNewVariableTracker)ConstantVariable)	PyCodegen)InstructionTranslatori  c                   f     e Zd Zdededdf fdZdefdZdefdZdd	d
ed   ddddf fdZ	 xZ
S )ItertoolsVariablevaluekwargsreturnNc                 2    t        |   di | || _        y N )super__init__r   )selfr   r   	__class__s      V/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/variables/iter.pyr$   zItertoolsVariable.__init__0   s    "6"
    c                 "    d| j                    dS )NzItertoolsVariable()r   r%   s    r'   __repr__zItertoolsVariable.__repr__4   s    #DJJ<q11r(   c                     | j                   S Nr+   r,   s    r'   as_python_constantz$ItertoolsVariable.as_python_constant7   s    zzr(   txr   argsr   zdict[str, VariableTracker]c                      j                   t        j                  u rt        d D              rUt	        dd  d d ddj                  t        j                               dhz
         g t        j                         dv rd   j                         }nd	}D cg c]  }|j                         }}t        j                  |d|iD cg c]   }t        j                  t        |            " }}t        j                  |t!               
      S  j                   t        j"                  u rǉst%              dk(  rd   j'                        rd	   j)                         rd   j+                        }	d	   j                         }g }t        j"                  |	|      D ]/  }|j-                  t        j                  t        |                   1 t        j                  |t!               
      S  j                   t        j.                  u rt        d D              rUt	        dd  d d ddj                  t        j                               dhz
         g t        j                         dt0        dt2        f fdt%              d	k(  r)d   j'                        rd   j+                        }
n0t	        dd  d d d d ddgt        j4                         dv rdt0        dt2        ffd}ndt0        dt2        ffd}g }	 t        j.                  
|      D ]  \  }}|j-                  t        j                  t        j6                  j9                  |      rt        j6                  j;                  |      n|t        j                  t        |      t!               
      gt!               
              	 t        j                  |t!               
      S  j                   t        j>                  u r`t%              dk  rt        j@                  dt!               iS jC                  t1        jD                  tF        j>                              S  j                   t        jH                  u rt        jJ                  dt!               iS  j                   t        jL                  u rt%              d	k(  s!t%              dk(  rd	   j)                         rst%              dk(  rd	   j                         }nd }t        jL                  d   j                        |      D cg c]   }t        j                  t        |            " }}t        j                  |t!               
      S tN                 S c c}w c c}w # t<        $ r4}t	        dd  d d dg t        j4                  |       Y d }~d }~ww xY wc c}w ) Nc              3   &   K   | ]	  }|d k7    yw)repeatNr"   .0kws     r'   	<genexpr>z2ItertoolsVariable.call_function.<locals>.<genexpr>C   s     3b2>3   z(Unsupported kwargs for itertools.productcall_function  z#Expected kwargs: 'repeat', but got ,r5   gb_typecontextexplanationhintsr   )mutation_typer   r   c              3   &   K   | ]	  }|d k7    yw)keyNr"   r6   s     r'   r9   z2ItertoolsVariable.call_function.<locals>.<genexpr>k   s     022;0r:   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got rE   r   c           
         t        | t        j                        r| j                         S | j	                         r| j                         S t        dd d d dt        t        |              dg t        j                         y )Nz*Unsupported key type for itertools.groupbyr;   r<   zCDynamo does not know how to trace itertools.groupby with key type: zJ. We only support grouping keys that are constants (int, float, str, etc.)r>   )
isinstancer   SymNodeVariableevaluate_expris_python_constantr0   r   strtyper	   SUPPORTABLE)rE   r2   r   r%   s    r'   retrieve_const_keyz;ItertoolsVariable.call_function.<locals>.retrieve_const_keyt   s    c9#<#<=,,..++-1133! L"0avQvh G%<<?S	N;K Lc%c ? 1 = =>r(   z+Unsupported arguments for itertools.groupbyz?Dynamo does not know how to trace itertools.groupby with args: z and kwargs: ze. itertools.groupby expects an iterable to group and an optional key function to determine groupings.z9Make sure the arguments to itertools.groupby are correct.xc                 V     j                  d      j                  | gi             S )NrE   )getcall_function)rO   r   rN   r1   s    r'   keyfuncz0ItertoolsVariable.call_function.<locals>.keyfunc   s-    -

5)77QCD r(   c                      |       S r/   r"   )rO   rN   s    r'   rS   z0ItertoolsVariable.call_function.<locals>.keyfunc   s    -a00r(   )rE   z7Unexpected failure during itertools.groupby() iterationz6Unexpected failure in invoking function during groupby)r?   r@   rA   rB   from_excrC   ))r   	itertoolsproductanyr   joinsetkeysr	   
USER_ERRORr0   force_unpack_var_sequencer   TupleVariablelistListIteratorVariabler   combinationslenhas_unpack_var_sequencerJ   unpack_var_sequenceappendgroupbyr   r   rM   r   
is_literalcreate	Exceptionr5   RepeatIteratorVariableinline_user_function_returnbuildr
   countCountIteratorVariablepermutationsr#   rR   )r%   r1   r2   r   rargseqsitemitemsiterableseqrS   resultkverN   r&   s   ````            @r'   rR   zItertoolsVariable.call_function:   s    ::***3F33F,TF!D66(C"ExxFKKM 2hZ ?@A!C9-889 6!8$779AEF#C11"5FDF &--t>A> ''T
3E  11.0 
 JJ)000D	QQ//3Q**,Aw2226HQ**,AE!..x; BY44T$Z@AB11.0  ZZ9,,,000F,TF!D66(C"BxxFKKM 2eW <=>!@9-889 C  4yA~$q'"A"A""E1g11"5I,TF!D66(C!4486vh OD!D
 T*66  3  1 13 1 F%--cw? DAqMM!// (1'A'A'L'LQ'O %.$>$>$E$Ea$H)* ) > >$(G;K;M!"	 +;*<0 11.0  ZZ9+++4y1} 77)9);  11%%b)*:*:;T6  ZZ9??*22%5%7  JJ)000TaCINtAw7Q7Q7S4yA~G..0 &22G55b91 ''T
3E  11.0 
 7(T6::_ GB  U,TF!D66(C X:-99: Ds+   (V)%V./B3V3 %W33	W0<)W++W0)__name__
__module____qualname__r   r$   rK   r-   r0   r   rR   __classcell__r&   s   @r'   r   r   /   sq    c S T 2# 2C e;#e; ()e; -	e;
 
e; e;r(   r   c            
            e Zd Zdeddf fdZdddefdZdddee   fdZddd	e	egef   ddfd
Z
dddefdZdddeddf fdZdddedee   deeef   def
 fdZ xZS )IteratorVariabler   r   Nc                 $    t        |   di | y r!   )r#   r$   )r%   r   r&   s     r'   r$   zIteratorVariable.__init__   s    "6"r(   r1   r   c                 J    t        dd|  ddg t        j                         y )NzUnimplemented next() callznext(r*   z(This abstract method must be implementedr>   )r   r	   
DYNAMO_BUGr%   r1   s     r'   next_variablezIteratorVariable.next_variable   s*    /D6OB1%001		
r(   c                 B    g }| j                  ||j                         |S r/   )force_apply_to_var_sequencere   )r%   r1   rw   s      r'   r]   z*IteratorVariable.force_unpack_var_sequence   s"     )+((V]];r(   fnc                 j    	 	  || j                  |             # t        $ r t        |       Y y w xY wr/   )r   r   r   )r%   r1   r   s      r'   r   z,IteratorVariable.force_apply_to_var_sequence   s@     4%%b)*  - )"-s    22c                      yNTr"   r   s     r'   has_force_unpack_var_sequencez.IteratorVariable.has_force_unpack_var_sequence  s    r(   namer   c                 v    |dk(  s|dk(  rt         j                  j                  d      S t        |   ||      S )N__iter____next__T)r   r   rh   r#   call_obj_hasattr)r%   r1   r   r&   s      r'   r   z!IteratorVariable.call_obj_hasattr  s=     :!3--44T::w'D11r(   r2   c                 b    |dk(  r| S |dk(  r| j                  |      S t        | 	  ||||      S )Nr   r   )r   r#   call_method)r%   r1   r   r2   r   r&   s        r'   r   zIteratorVariable.call_method  sA     :KZ%%b))w"2tT6::r(   )r{   r|   r}   r   r$   r   r   r_   r]   r   r   boolr   rK   r   dictr   r~   r   s   @r'   r   r      s    # # #
 7 
O 
)	o	)/7s
/C	0G D 2)2142	2;#; ; ?#	;
 S/)*; 
; ;r(   r   c                   @     e Zd ZdZdededdf fdZdddefd	Z xZS )
ObjectIteratorVariableaZ  
    VariableTracker for iter(obj) that implements the iterator protocol (i.e.,
    has a `__next__` method).

    We use this class to track the state of the iterator and handle the case
    when the iterator is exhausted:

    Example usage:
        > b = iter(obj)
        > list(b)  # exhaust the iterator
        > list(b)  # empty list
    objr   r   Nc                 @    t        |   di | || _        d| _        y )NFr"   )r#   r$   r   generator_exhausted)r%   r   r   r&   s      r'   r$   zObjectIteratorVariable.__init__+  s"    "6"#( r(   r1   r   c                     | j                   rt        t        |       	 | j                  j	                  |      S # t
        $ r	 d| _          w xY wr   )r   r   StopIterationr   r   r   r   s     r'   r   z$ObjectIteratorVariable.next_variable0  sK    ##$]B7	88))"--( 	 (,D$		s	   9 A)	r{   r|   r}   __doc__r   r   r$   r   r~   r   s   @r'   r   r     s6    )O )s )t )

 7 
O 
r(   r   c                   D     e Zd Zdededdf fdZdddefdZd
d	Z xZS )rj   rs   r   r   Nc                 2    t        |   di | || _        y r!   )r#   r$   rs   )r%   rs   r   r&   s      r'   r$   zRepeatIteratorVariable.__init__>  s    "6"	r(   r1   r   c                     | j                   S r/   )rs   r   s     r'   r   z$RepeatIteratorVariable.next_variableC  s    yyr(   c                     j                  fd        | j                         j                  t        dd             y )Nc                  n     j                   j                  t               j                  d      g      S )Nr5   extend_outputcreate_load_python_modulerV   create_load_attrcodegens   r'   <lambda>z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>H  s3    G))55i@,,X6 r(   r   F)add_push_nullrs   r   r   r%   r   s    `r'   reconstructz"RepeatIteratorVariable.reconstructF  s:    	
 			21e<=r(   r   r   r   N)	r{   r|   r}   r   r   r$   r   r   r~   r   s   @r'   rj   rj   =  s6    _   
 7 O 
>r(   rj   c            	       b     e Zd Z	 	 ddeeef   deeef   deddf fdZdddefd	Zdd
Z	 xZ
S )rn   rs   stepr   r   Nc                     t        |   di | t        |t              st	        j
                  |      }t        |t              st	        j
                  |      }|| _        || _        y r!   )r#   r$   rG   r   r   rh   rs   r   )r%   rs   r   r   r&   s       r'   r$   zCountIteratorVariable.__init__T  sW     	"6"$0#**40D$0#**40D		r(   r1   r   c                     | j                         sJ | j                  }|j                  j                  j	                  |        | j                  j                  |d| j                  gi       | _        |S )N__add__)
is_mutablers   outputside_effectsmutationr   r   )r%   r1   old_items      r'   r   z#CountIteratorVariable.next_variableb  sZ       99
		''-II))"i$))bI	r(   c                     j                  fd        | j                          | j                         j                  t	        dd             y )Nc                  n     j                   j                  t               j                  d      g      S )Nrm   r   r   s   r'   r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>k  s3    G))55i@,,W5 r(   r   F)r   rs   r   r   r   r   s    `r'   r   z!CountIteratorVariable.reconstructi  sF    	
 					21e<=r(   )r   r   r   )r{   r|   r}   r   intr   r   r$   r   r   r~   r   s   @r'   rn   rn   S  sc     -.,-C() C() 	
 
 7 O >r(   rn   c            	            e Zd ZdZddhej
                  Z	 ddee   dede	ddf fdZ
dee   fd	Zd
ddefdZd
dded   fdZd
ddefdZddZddZ xZS )ZipVariablez$
    Represents zip(*iterables)
    indexstrict	iterablesr   r   Nc                 r    t        |   di | t        |t              sJ || _        d| _        || _        y Nr   r"   )r#   r$   rG   r_   r   r   r   )r%   r   r   r   r&   s       r'   r$   zZipVariable.__init__  s:     	"6")T***"
r(   c                     t         S r/   )zipr,   s    r'   python_typezZipVariable.python_type      
r(   r1   r   c                 @    t        fd| j                  D              S )Nc              3   d   K   | ]'  }t        |t              xs |j                         ) y wr/   )rG   r_   rc   )r7   itr1   s     r'   r9   z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>  s3      
 r4 BB$>$>r$BB
s   -0)allr   r   s    `r'   rc   z#ZipVariable.has_unpack_var_sequence  s!     
nn
 
 	
r(   r   c                    | j                  |      sJ g }| j                  D ]Q  }t        |t              r|j	                  || j
                  d         2|j	                  |j                  |             S | j                  rd| j                  ini }t        |i |}|D cg c]   }t        j                  t        |            " c}S c c}w )Nr   )rc   r   rG   r_   re   r   rd   r   r   r   r^   )r%   r1   r   r   r   zippedvars          r'   rd   zZipVariable.unpack_var_sequence  s     ++B///	.. 	=B"d#  DJJL!12  !7!7!;<		=
 -1KK(DKK(Ri*6*>DEs	''S	2EEEs   !%C	c                    | j                         sJ t        | j                        dk(  rt        t               | j
                  g }dt        t        t           t        f   dt        ffd}d }	 t        | j                        D ]  \  }}|j                   ||              	 j                   j"                  j%                  |        | xj
                  dz  c_        t'        j(                  |      S # t        $ rf | j                  rX|dk(  r7| j                  D ]'  }	  ||       n# t        $ r t               Y #w xY w n  t               t        t        d      d  w xY w)Nr   r   r   c                     t        | t              r#t        |       k\  rt        t               |    S | j                        S r/   )rG   r_   rb   r   r   r   )r   	old_indexr1   s    r'   get_itemz+ZipVariable.next_variable.<locals>.get_item  s@     "d#B',]B?)}$''++r(   z3zip() has one argument of len differing from othersr   )r   rb   r   r   r   r   r   r_   r   	enumeratere   r   r   r   r   
ValueErrorr   r   r   r   r^   )r%   r1   r2   r   idxr   r   s    `    @r'   r   zZipVariable.next_variable  sW      t~~!#$]B7JJ		,d?+_<=	,	, 	$T^^4 *RHRL)*0 			''-

a
&&t,,1 ) 	{{!8"nn 
%$RL8 %5b9$% 
 )"-I  )	s0   54C9 9)E(#D,+E(,E E(E%E(c                     | j                   D ]^  }t        |t              rD|| j                  d  }|j	                  |       |j                  t        t        |                   W ||       ` y r/   )r   rG   r_   r   foreachappend_outputr   rb   )r%   r   r   remaining_itemss       r'   reconstruct_itemszZipVariable.reconstruct_items  s[    .. 	B"d#"$TZZ\"20%%&8_9M&NO	r(   c                 T   j                  fdd       | j                         j                  t        t	        | j
                                     j                  j                  d      j                  | j                        t        dd      gt        dd             y )	Nc                  (     j                  dd      S )Nbuiltinsr   load_import_fromr   s   r'   r   z)ZipVariable.reconstruct.<locals>.<lambda>      G,,Z? r(   Tcall_function_exr   	BUILD_MAPr   rq   F)r   r   r   r   rb   r   r   create_load_constr   r   r   r   s    `r'   r   zZipVariable.reconstruct  s    ?RV 	 	
 	w'0T^^1DEF))(3))$++6";A6 )u5		
r(   )Fr   )r{   r|   r}   r   r   _nonvar_fieldsr_   r   r   r   r$   rL   r   r   rc   rd   r   r   r   r~   r   s   @r'   r   r   w  s    
 	 
	(	(N (  	
 
T#Y 
*A 
d 
F)F		 F/- 7 /-O /-b
r(   r   c                   r     e Zd ZdZdedee   deddf fdZdefdZ	d	d
de
fdZd	d
def fdZddZ xZS )MapVariablez(
    Represents map(fn, *iterables)
    r   r   r   r   Nc                 4    t        |   |fi | || _        y r/   )r#   r$   r   )r%   r   r   r   r&   s       r'   r$   zMapVariable.__init__  s     	-f-r(   c                     t         S r/   )mapr,   s    r'   r   zMapVariable.python_type  r   r(   r1   r   c                      y)NFr"   r   s     r'   rc   z#MapVariable.has_unpack_var_sequence  s    r(   c                 p    t         |   |      }| j                  j                  ||j                  i       S r/   )r#   r   r   rR   rt   )r%   r1   r2   r&   s      r'   r   zMapVariable.next_variable  s/    w$R(ww$$RR88r(   c                    j                  fdd        | j                         | j                         j                  t	        t        | j                        dz                | j                  rnt        j                  dk\  sJ d       j                  j                  d      j                  | j                        t        dd	      gt        dd
             y j                  t        d
d
             y )Nc                  (     j                  dd      S )Nr   r   r   r   s   r'   r   z)MapVariable.reconstruct.<locals>.<lambda>
  r   r(   Tr   r   )      z6Unexpected bug: map(strict=True) requires Python 3.14+r   r   r   F)r   r   r   r   r   rb   r   r   sysversion_infor   r   r   r   r   s    `r'   r   zMapVariable.reconstruct  s    ?RV 	 	
 	w'0T^^1Dq1HIJ;;##w. H. !!--h7--dkk:&{: -T59	 !!"9%"GHr(   r   )r{   r|   r}   r   r   r_   r   r$   rL   r   r   rc   r   r   r~   r   s   @r'   r   r     sr     ( 	
 
T *A d 9 7 9O 9Ir(   r   c                        e Zd ZdZdhej
                  Zdedee   deddf fdZ	de
fd	Zd
ddefdZd
dded   fdZd
ddefdZddZddZ xZS )FilterVariablez)
    Represents filter(fn, iterable)
    r   r   ru   r   r   Nc                 N    t        |   di | || _        || _        d| _        y r   )r#   r$   r   ru   r   )r%   r   ru   r   r&   s       r'   r$   zFilterVariable.__init__)  s*     	"6" 
r(   c                     t         S r/   )filterr,   s    r'   r   zFilterVariable.python_type4  s    r(   r1   r   c                 p    t        | j                  t              xs | j                  j                  |      S r/   )rG   ru   r_   rc   r   s     r'   rc   z&FilterVariable.has_unpack_var_sequence7  s.    $--. 
$--2W2W3
 	
r(   r   c                 2   | j                  |      sJ d }t        | j                  t              r| j                  | j                  d  }n| j                  j                  |      }| j                  j                  ||i       }t        j                  |g      gS r/   )
rc   rG   ru   r_   r   rd   r   rR   r   r^   )r%   r1   r   filtereds       r'   rd   z"FilterVariable.unpack_var_sequence<  s     ++B///dmmT*tzz|,B2226B77((R4''
344r(   c                 `    dt         f fd}	  |       } xj                  dz  c_         j                  j                         r|}n j                  j	                  |gi       }t        j                  t        j                        j	                  |gi       }|j                         r|S )Nr   c                      j                   } t        j                  t              r7| t	        j                        k\  rt        t               j                  |    S j                  j                        S r/   )r   rG   ru   r_   rb   r   r   r   )r   r%   r1   s    r'   _nextz+FilterVariable.next_variable.<locals>._nextI  sY    

I$--.DMM 22,]B?}}Y//}}22266r(   r   )
r   r   r   is_constant_nonerR   r   UserFunctionVariabler
   	predicater0   )r%   r1   r   rs   respred_ress   ``    r'   r   zFilterVariable.next_variableH  s    	7 	7 7DJJ!OJww'')gg++B; 55##mBr*  **, r(   c                     t        | j                  t              rN| j                  | j                  d  }|j	                  |       |j                  t        t        |                   y  || j                         y r/   )rG   ru   r_   r   r   r   r   rb   )r%   r   r   s      r'   r   z FilterVariable.reconstruct_items`  sU    dmmT*"mmDJJL9OOOO,!!"4S5I"JKDMM"r(   c                     j                  fd        | j                         | j                         j                  t	        dd             y )Nc                  (     j                  dd      S )Nr   r   r   r   s   r'   r   z,FilterVariable.reconstruct.<locals>.<lambda>i  s    g&>&>z8&T r(   r   F)r   r   r   r   r   r   s    `r'   r   zFilterVariable.reconstructh  sB    TUw'21e<=r(   r   )r{   r|   r}   r   r   r   r   r_   r   r$   rL   r   r   rc   rd   r   r   r   r~   r   s   @r'   r   r     s    
 			(	(N
		 '	 		
 
	T 
*A 
d 


5)
5		 
5 7 O 0#>r(   r   )+r   rV   r   collections.abcr   r   typingr   r   r    r	   r
   r   bytecode_transformationr   r   r   r   excr   r   r   r   r   baser   r   constantr   torch._dynamo.codegenr   torch._dynamo.symbolic_convertr   MAX_ITERATOR_LIMITr   r   r   rj   rn   r   r   r   r"   r(   r'   <module>r     s     
 . , , 6 6   4 & /D   p; p;f8; 8;v- @>- >,!>, !>Hv
" v
r,I+ ,I^M>% M>r(   