
    9iCW                        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*dS )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                   l     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                 H     t                      j        di | || _        d S N )super__init__r   )selfr   r   	__class__s      O/var/www/icac/venv/lib/python3.11/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    1DJ1111r(   c                     | j         S Nr+   r,   s    r'   as_python_constantz$ItertoolsVariable.as_python_constant7   s
    zr(   txr   argsr   zdict[str, VariableTracker]c                 8     j         t          j        u rt          d D                       rat	          dd  d d dd                    t                                                    dhz
             g t          j	                   dv rd         
                                }nd	}fd
D             }d t          j        |d|iD             }t          j        |t                                S  j         t          j        u rst                    dk    rډd                                       rd	                                         rd                                       }d	         
                                }g }t          j        ||          D ]6}|                    t          j        t+          |                               7t          j        |t                                S  j         t          j        u r]t          d D                       rat	          dd  d d dd                    t                                                    dhz
             g t          j	                   dt.          dt0          f fdt                    d	k    r7d                                       rd                                       }	n0t	          dd  d d d d ddgt          j                   dv rdt.          dt0          ffd}
ndt.          dt0          ffd}
g }	 t          j        |	|
          D ]\  }}|                    t          j        t          j                            |          rt          j                            |          n|t          j        t+          |          t                                gt                                           n@# t:          $ r3}t	          dd  d d dg t          j        |            Y d }~nd }~ww xY wt          j        |t                                S  j         t          j        u rdt                    dk     rt          j        d!t                      iS                      t/          j!        tD          j                            S  j         t          j#        u rt          j$        d!t                      iS  j         t          j%        u rt                    d	k    s-t                    dk    rd	                                         rst                    dk    rd	         
                                }nd }d" t          j%        d         &                              |          D             }t          j        |t                                S tO                      (                              S )#Nc              3   "   K   | ]
}|d k    V  dS )repeatNr"   .0kws     r'   	<genexpr>z2ItertoolsVariable.call_function.<locals>.<genexpr>C   s&      33b2>333333r(   z(Unsupported kwargs for itertools.productcall_function  z#Expected kwargs: 'repeat', but got ,r5   gb_typecontextexplanationhintsr   c                 :    g | ]}|                               S r"   )force_unpack_var_sequence)r7   argr1   s     r'   
<listcomp>z3ItertoolsVariable.call_function.<locals>.<listcomp>P   s'    FFF#C11"55FFFr(   c                 P    g | ]#}t          j        t          |                    $S r"   r   TupleVariablelistr7   items     r'   rE   z3ItertoolsVariable.call_function.<locals>.<listcomp>Q   s9        'T

33  r(   )mutation_typer   r   c              3   "   K   | ]
}|d k    V  dS )keyNr"   r6   s     r'   r9   z2ItertoolsVariable.call_function.<locals>.<genexpr>k   s&      0022;000000r(   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got rN   r   c           
      @   t          | t          j                  r|                                 S |                                 r|                                 S t          dd d d dt          t          |                      dg t          j
                   d S )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)rN   r2   r   r%   s    r'   retrieve_const_keyz;ItertoolsVariable.call_function.<locals>.retrieve_const_keyt   s    c9#<== ,,...++-- 
11333! L G G G G Gv G G%c<?S		NN%c %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                 l                          d                              | gi                     S )NrN   )getcall_function)rX   r   rW   r1   s    r'   keyfuncz0ItertoolsVariable.call_function.<locals>.keyfunc   s;    --

5))77QCDD  r(   c                      |           S r/   r"   )rX   rW   s    r'   r\   z0ItertoolsVariable.call_function.<locals>.keyfunc   s    --a000r(   )rN   z7Unexpected failure during itertools.groupby() iterationz6Unexpected failure in invoking function during groupby)r>   r?   r@   rA   from_excrL   c                 P    g | ]#}t          j        t          |                    $S r"   rG   rJ   s     r'   rE   z3ItertoolsVariable.call_function.<locals>.<listcomp>   s9        'T

33  r(   ))r   	itertoolsproductanyr   joinsetkeysr	   
USER_ERRORr0   r   ListIteratorVariabler   combinationslenhas_unpack_var_sequencerS   unpack_var_sequenceappendrH   rI   groupbyr   r   rV   r   
is_literalcreate	Exceptionr5   RepeatIteratorVariableinline_user_function_returnbuildr
   countCountIteratorVariablepermutationsrC   r#   r[   )r%   r1   r2   r   rseqsitemsiterablerK   seqr\   resultkverW   r&   s   ````           @r'   r[   zItertoolsVariable.call_function:   sq    :***33F33333 FCTCCDCC6CC!CxxFKKMM 2 2hZ ?@@!C !C9-89    6!!8$7799FFFFFFFD %-t>A>>  E 1.00   
 J)000 1D		QQ//33 Q**,,  Aw22266HQ**,,AE!.x;; B BY4T$ZZ@@AAAA1.00    Z9,,,0000000 FCTCCDCC6CC!@xxFKKMM 2 2eW <==!@ !@9-89    C         4yyA~~$q'"A"A""E"E~1g11"55ICTCCDCC6CC!D48!D !DGM!D !D !D
 T*6     3         1 13 1 1 1 1 1 1 F%-cw???  DAqMM!/ (1'A'L'LQ'O'O%+I$>$E$Ea$H$H$H)* ) >$(GG;K;M;M!" !" !"	 +;*<*<          UCTCCDCC6CC X:-9:         1.00    Z9+++4yy1}} 7)9););   11%b)*:;;T6   Z9?**2%5%7%7   J)000TaCIINNtAw7Q7Q7S7SN %3 4yyA~~G..00 %2G55b991   E 1.00   
 77((T6:::s   B>P 
Q)Q		Q)__name__
__module____qualname__r   r$   rT   r-   r0   r   r[   __classcell__r&   s   @r'   r   r   /   s        c S T      2# 2 2 2 2C    e;#e; ()e; -	e;
 
e; e; e; e; 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                      j        di | d S r!   )r#   r$   )r%   r   r&   s     r'   r$   zIteratorVariable.__init__   s&    ""6"""""r(   r1   r   c                 L    t          dd|  ddg t          j                   d S )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@    /#DOOOB1%01		
 	
 	
 	
 	
 	
r(   c                 @    g }|                      ||j                   |S r/   )force_apply_to_var_sequencerl   )r%   r1   r|   s      r'   rC   z*IteratorVariable.force_unpack_var_sequence   s&     )+((V];;;r(   fnc                     	 	  ||                      |                     n # t          $ r t          |           Y d S w xY wAr/   )r   r   r   )r%   r1   r   s      r'   r   z,IteratorVariable.force_apply_to_var_sequence   sb    	4%%b))****,   )"---	s   " ??c                     dS NTr"   r   s     r'   has_force_unpack_var_sequencez.IteratorVariable.has_force_unpack_var_sequence  s    tr(   namer   c                     |dk    s|dk    rt           j                            d          S t                                          ||          S )N__iter____next__T)r   r   ro   r#   call_obj_hasattr)r%   r1   r   r&   s      r'   r   z!IteratorVariable.call_obj_hasattr  sK     :!3!3-44T:::ww''D111r(   r2   c                     |dk    r| S |dk    r|                      |          S t                                          ||||          S )Nr   r   )r   r#   call_method)r%   r1   r   r2   r   r&   s        r'   r   zIteratorVariable.call_method  sR     :KZ%%b)))ww""2tT6:::r(   )r   r   r   r   r$   r   r   rI   rC   r   r   boolr   rT   r   dictr   r   r   s   @r'   r   r      s       # # # # # # # #
 7 
O 
 
 
 
)	o	   )/7s
/C	   0G D    2)2142	2 2 2 2 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                 V     t                      j        di | || _        d| _        d S )NFr"   )r#   r$   r   generator_exhausted)r%   r   r   r&   s      r'   r$   zObjectIteratorVariable.__init__+  s5    ""6"""#(   r(   r1   r   c                     | j         rt          t          |           	 | j                            |          S # t
          $ r	 d| _          w xY wr   )r   r   StopIterationr   r   r   r   s     r'   r   z$ObjectIteratorVariable.next_variable0  s`    # 	8$]B777	8))"---( 	 	 	 (,D$		s	   8 A)	r   r   r   __doc__r   r   r$   r   r   r   s   @r'   r   r     s         )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 )rq   rK   r   r   Nc                 H     t                      j        di | || _        d S r!   )r#   r$   rK   )r%   rK   r   r&   s      r'   r$   zRepeatIteratorVariable.__init__>  s+    ""6"""			r(   r1   r   c                     | j         S r/   )rK   r   s     r'   r   z$RepeatIteratorVariable.next_variableC  s
    yr(   codegenr   c                                          fd            | j                                       t          dd                     d S )Nc                                                                 t                                         d          g          S )Nr5   extend_outputcreate_load_python_moduler`   create_load_attrr   s   r'   <lambda>z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>H  s>    G))55i@@,,X66  r(   r   F)add_push_nullrK   r   r   r%   r   s    `r'   reconstructz"RepeatIteratorVariable.reconstructF  sh       	
 	
 	
 		21e<<=====r(   r   r   r   N)	r   r   r   r   r   r$   r   r   r   r   s   @r'   rq   rq   =  s        _        
 7 O    
> 
> 
> 
> 
> 
> 
> 
>r(   rq   c            	       n     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 )ru   r   r   rK   stepr   r   Nc                      t                      j        di | t          |t                    st	          j        |          }t          |t                    st	          j        |          }|| _        || _        d S r!   )r#   r$   rP   r   r   ro   rK   r   )r%   rK   r   r   r&   s       r'   r$   zCountIteratorVariable.__init__T  sx     	""6"""$00 	1#*400D$00 	1#*400D				r(   r1   r   c                     |                                  sJ | j        }|j        j                            |            | j                            |d| j        gi           | _        |S )N__add__)
is_mutablerK   outputside_effectsmutationr   r   )r%   r1   old_items      r'   r   z#CountIteratorVariable.next_variableb  s^         9
	''---I))"i$)bII	r(   r   r   c                                          fd            | j                    | j                                       t	          dd                     d S )Nc                                                                 t                                         d          g          S )Nrt   r   r   s   r'   r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>k  s>    G))55i@@,,W55  r(   r   F)r   rK   r   r   r   r   s    `r'   r   z!CountIteratorVariable.reconstructi  sz       	
 	
 	
 			21e<<=====r(   )r   r   r   )r   r   r   r   intr   r   r$   r   r   r   r   s   @r'   ru   ru   S  s         -.,- C() C() 	
 
      7 O    > > > > > > > >r(   ru   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strictF	iterablesr   r   Nc                      t                      j        di | t          |t                    sJ || _        d| _        || _        d S Nr   r"   )r#   r$   rP   rI   r   r   r   )r%   r   r   r   r&   s       r'   r$   zZipVariable.__init__  sQ     	""6""")T*****"
r(   c                     t           S r/   )zipr,   s    r'   python_typezZipVariable.python_type      
r(   r1   r   c                 D    t          fd| j        D                       S )Nc              3   l   K   | ].}t          |t                    p|                              V  /d S r/   )rP   rI   rj   )r7   itr1   s     r'   r9   z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>  sT       
 
 r4  BB$>$>r$B$B
 
 
 
 
 
r(   )allr   r   s    `r'   rj   z#ZipVariable.has_unpack_var_sequence  s=     
 
 
 
n
 
 
 
 
 	
r(   r   c                 \   |                      |          sJ g }| j        D ]b}t          |t                    r#|                    || j        d                     :|                    |                    |                     c| j        r	d| j        ini }t          |i |}d |D             S )Nr   c                 P    g | ]#}t          j        t          |                    $S r"   rG   )r7   vars     r'   rE   z3ZipVariable.unpack_var_sequence.<locals>.<listcomp>  s)    EEEs	'S		22EEEr(   )	rj   r   rP   rI   rl   r   rk   r   r   )r%   r1   r   r   r   zippeds         r'   rk   zZipVariable.unpack_var_sequence  s     ++B/////	. 	= 	=B"d## =  DJLL!12222  !7!7!;!;<<<<,0K?(DK((Ri*6**EEfEEEEr(   c                    |                                  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 ]#\  }}|
                     ||                     $ny# t          $ rl | j        rc|dk    r8| j        D ]/}	  ||           n# t          $ r t                     Y *w xY w n t                     t          t          d          d  w xY wj        j                            |            | xj        dz  c_        t'          j        |          S )Nr   r   r   c                     t          | t                    r0t          |           k    rt          t                     |          S |                               S r/   )rP   rI   ri   r   r   r   )r   	old_indexr1   s    r'   get_itemz+ZipVariable.next_variable.<locals>.get_item  sV     "d## ,B'',]B???)}$''+++r(   z3zip() has one argument of len differing from othersr   )r   ri   r   r   r   r   r   rI   r   	enumeraterl   r   r   r   r   
ValueErrorr   r   r   r   rH   )r%   r1   r2   r   idxr   r   s    `    @r'   r   zZipVariable.next_variable  s        t~!##$]B777J		,d?+_<=	,	, 	, 	, 	, 	, 	, 	, 	$T^44 * *RHHRLL))))*( 	 	 	{ !88"n 
 
%$HRLLLL8 % % %5b999$H%  )"---I   )	, 		''---

a

&t,,,s0   8B= = D3C*)D3*DD3D-D3r   r   c                    | j         D ]v}t          |t                    rT|| j        d          }|                    |           |                    t          t          |                               k ||           wd S r/   )r   rP   rI   r   foreachappend_outputr   ri   )r%   r   r   remaining_itemss       r'   reconstruct_itemszZipVariable.reconstruct_items  s    . 	 	B"d## "$TZ\\"2000%%&8_9M9M&N&NOOOO	 	r(   c                                         fdd           |                                                    t          t	          | j                                                                               d                              | j                  t          dd          gt          dd                     d S )	Nc                  0                          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   rD   F)r   r   r   r   ri   r   r   create_load_constr   r   r   r   s    `r'   r   zZipVariable.reconstruct  s    ????RV 	 	
 	
 	
 	w'''0T^1D1DEEFFF))(33))$+66";A666 )u55		
 	
 	
 	
 	
r(   )Fr   )r   r   r   r   r   _nonvar_fieldsrI   r   r   r   r$   rU   r   r   rj   rk   r   r   r   r   r   s   @r'   r   r   w  sV        
 	 
	(N  (  	
 
     T#Y    
*A 
d 
 
 
 
F)F		 F F F F/- 7 /-O /- /- /- /-b   
 
 
 
 
 
 
 
r(   r   c                   x     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                 J     t                      j        |fi | || _        d S 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                     dS )NFr"   r   s     r'   rj   z#MapVariable.has_unpack_var_sequence  s    ur(   c                     t                                          |          }| j                            ||j        i           S r/   )r#   r   r   r[   ry   )r%   r1   r2   r&   s      r'   r   zMapVariable.next_variable  s6    ww$$R((w$$RR888r(   r   r   c                 D                        fdd            | j                   |                                                    t	          t          | j                  dz                        | j        r~t          j	        dk    s
J d            
                                        d                              | j                  t          dd	          gt          dd
                     d S 
                    t          d
d
                     d S )Nc                  0                          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   ri   r   r   sysversion_infor   r   r   r   r   s    `r'   r   zMapVariable.reconstruct  sE   ????RV 	 	
 	
 	
 	w'''0T^1D1Dq1HIIJJJ; 	I#w...H /.. !!--h77--dk::&{::: -T599	     !!"9%"G"GHHHHHr(   r   )r   r   r   r   r   rI   r   r$   rU   r   r   rj   r   r   r   r   s   @r'   r   r     s          ( 	
 
     T    *A d    9 7 9O 9 9 9 9 9 9I I I I I I I 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   rz   r   r   Nc                 d     t                      j        di | || _        || _        d| _        d S r   )r#   r$   r   rz   r   )r%   r   rz   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                 j    t          | j        t                    p| j                            |          S r/   )rP   rz   rI   rj   r   s     r'   rj   z&FilterVariable.has_unpack_var_sequence7  s2    $-.. 
$-2W2W3
 3
 	
r(   r   c                 *   |                      |          sJ d }t          | j        t                    r| j        | j        d          }n| j                            |          }| j                            ||i           }t          j	        |g          gS r/   )
rj   rP   rz   rI   r   rk   r   r[   r   rH   )r%   r1   r   filtereds       r'   rk   z"FilterVariable.unpack_var_sequence<  s     ++B/////dmT** 	7tz||,BB22266B7((R44'
3344r(   c                 `    dt           f fd}	  |            } xj        dz  c_         j                                        r|}n j                            |gi           }t          j        t          j                                      |gi           }|	                                r|S )Nr   c                      j         } t          j        t                    r:| t	          j                  k    rt          t                     j        |          S j                                      S r/   )r   rP   rz   rI   ri   r   r   r   )r   r%   r1   s    r'   _nextz+FilterVariable.next_variable.<locals>._nextI  sf    
I$-.. 7DM 2 222,]B???}Y//}222666r(   Tr   )
r   r   r   is_constant_noner[   r   UserFunctionVariabler
   	predicater0   )r%   r1   r  rK   respred_ress   ``    r'   r   zFilterVariable.next_variableH  s    	7 	7 	7 	7 	7 	7 	7 	7	577DJJ!OJJw'')) <g++B;; 5# mBr**  **,, 	r(   r   r   c                    t          | j        t                    rZ| j        | j        d          }|                    |           |                    t          t          |                               d S  || j                   d S r/   )rP   rz   rI   r   r   r   r   ri   )r%   r   r   s      r'   r   z FilterVariable.reconstruct_items`  s}    dmT** 	#"mDJLL9OOOO,,,!!"4S5I5I"J"JKKKKKGDM"""""r(   c                                          fd            | j                   |                                                    t	          dd                     d S )Nc                  0                          dd          S )Nr   r   r   r   s   r'   r   z,FilterVariable.reconstruct.<locals>.<lambda>i  s    g&>&>z8&T&T r(   r   F)r   r   r   r   r   r   s    `r'   r   zFilterVariable.reconstructh  sl    TTTTUUUw'''21e<<=====r(   r   )r   r   r   r   r   r   r   rI   r   r$   rU   r   r   rj   rk   r   r   r   r   r   s   @r'   r   r     s<        
 			(N
		 '	 		
 
	 	 	 	 	 	T    
*A 
d 
 
 
 


5)
5		 
5 
5 
5 
5 7 O    0# # # #> > > > > > > >r(   r   )+r   r`   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   rq   ru   r   r   r   r"   r(   r'   <module>r     s        



 . . . . . . . . , , , , , , , , , , 6 6 6 6 6 6 6 6 6 6                         4 3 3 3 3 3 3 3 & & & & & &  E//////DDDDDD   p; p; p; p; p; p; p; p;f8; 8; 8; 8; 8; 8; 8; 8;v    -   @> > > > >- > > >,!> !> !> !> !>, !> !> !>Hv
 v
 v
 v
 v
" v
 v
 v
r,I ,I ,I ,I ,I+ ,I ,I ,I^M> M> M> M> M>% M> M> M> M> M>r(   