
    9jr\                     l   d Z ddl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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   )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                   r     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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      \/media/conek/DATA/Code/OCR/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       zzr'   c                     | j                   S r.   r*   r+   s    r&   get_real_python_backed_valuez.ItertoolsVariable.get_real_python_backed_value:   r0   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st%              dk(  rt        jJ                  t!               
      S t%              d	k(  r"t        jJ                  d   t!                      S t%              dk(  r&t        jJ                  d   d	   t!               !      S tL                 S  j                   t        jP                  u rt%              d	k(  s!t%              dk(  rd	   j)                         rst%              dk(  rd	   j                         }nd }t        jP                  d   j                        |      D cg c]   }t        j                  t        |            " }}t        j                  |t!               
      S tL                 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>F   s     3b2>3   z(Unsupported kwargs for itertools.productcall_function  z#Expected kwargs: 'repeat', but got ,r7   gb_typecontextexplanationhintsr   )mutation_typer   r   c              3   &   K   | ]	  }|d k7    yw)keyNr!   r8   s     r&   r;   z2ItertoolsVariable.call_function.<locals>.<genexpr>n   s     022;0r<   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got rG   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_constantr/   r   strtyper   SUPPORTABLE)rG   r4   r   r$   s    r&   retrieve_const_keyz;ItertoolsVariable.call_function.<locals>.retrieve_const_keyw   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 )NrG   )getcall_function)rQ   r   rP   r3   s    r&   keyfuncz0ItertoolsVariable.call_function.<locals>.keyfunc   s-    -

5)77QCD r'   c                      |       S r.   r!   )rQ   rP   s    r&   rU   z0ItertoolsVariable.call_function.<locals>.keyfunc   s    -a00r'   )rG   z7Unexpected failure during itertools.groupby() iterationz6Unexpected failure in invoking function during groupby)rA   rB   rC   rD   from_excrE   )itemrE   )rX   steprE   ))r   	itertoolsproductanyr   joinsetkeysr   
USER_ERRORr/   force_unpack_var_sequencer
   TupleVariablelistListIteratorVariabler   combinationslenhas_unpack_var_sequencerL   unpack_var_sequenceappendgroupbyr   r   rO   r   
is_literalcreate	Exceptionr7   RepeatIteratorVariableinline_user_function_returnbuildr	   countCountIteratorVariabler"   rT   permutations)r$   r3   r4   r   rargseqsrX   itemsiterableseqrU   resultkverP   r%   s   ````            @r&   rT   zItertoolsVariable.call_function=   s9    ::***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??*64yA~ 66EUEWXX4yA~ 66a0@0B  4yA~ 66aa"2"4 
 7(T6::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::q G@  U,TF!D66(C X:-99: Xs+   (X/%X4/B3X9 %Y99	Y6)Y11Y6)__name__
__module____qualname__r   r#   rM   r,   r/   r2   r   rT   __classcell__r%   s   @r&   r   r   /   s}    c S T 2# 2C c n;#n; ()n; -	n;
 
n; n;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'   r3   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$   r3   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_sequenceri   )r$   r3   rz   s      r&   ra   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$   r3   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   rl   r"   call_obj_hasattr)r$   r3   r   r%   s      r&   r   z!IteratorVariable.call_obj_hasattr  s=     :!3--44T::w'D11r'   r4   c                 b    |dk(  r| S |dk(  r| j                  |      S t        | 	  ||||      S )Nr   r   )r   r"   call_method)r$   r3   r   r4   r   r%   s        r&   r   zIteratorVariable.call_method  sA     :KZ%%b))w"2tT6::r'   )r~   r   r   r   r#   r   r   rc   ra   r   r   boolr   rM   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__7  s"    "6"#( r'   r3   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_variable<  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                   P     e Zd Zdededdf fdZdefdZdddefd	Zdd
Z	 xZ
S )rn   rX   r   r   Nc                 2    t        |   di | || _        y r    )r"   r#   rX   )r$   rX   r   r%   s      r&   r#   zRepeatIteratorVariable.__init__J  s    "6"	r'   c                 "    t         j                  S r.   )rZ   r7   r+   s    r&   python_typez"RepeatIteratorVariable.python_typeN  s    r'   r3   r   c                     | j                   S r.   )rX   r   s     r&   r   z$RepeatIteratorVariable.next_variableR  s    yyr'   c                     j                  fd        | j                         j                  t        dd             y )Nc                  n     j                   j                  t               j                  d      g      S )Nr7   extend_outputcreate_load_python_modulerZ   create_load_attrcodegens   r&   <lambda>z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>W  s3    G))55i@,,X6 r'   r   F)add_push_nullrX   r   r   r$   r   s    `r&   reconstructz"RepeatIteratorVariable.reconstructU  s:    	
 			21e<=r'   r   r   r   N)r~   r   r   r   r   r#   rN   r   r   r   r   r   s   @r&   rn   rn   I  sB    _    T   7 O 
>r'   rn   c                        e Zd Zdhej                  ZdefdZ	 	 	 ddeez  deez  dede	ddf
 fdZ
d	d
defdZddZ xZS )rr   advance_countr   c                 "    t         j                  S r.   )rZ   rq   r+   s    r&   r   z!CountIteratorVariable.python_typej  s    r'   rX   rY   r   Nc                     t        |   di | t        |t              st	        j
                  |      }t        |t              st	        j
                  |      }|| _        || _        || _        y r    )	r"   r#   rI   r   r   rl   rX   rY   r   )r$   rX   rY   r   r   r%   s        r&   r#   zCountIteratorVariable.__init__m  s_     	"6"$0#**40D$0#**40D		*r'   r3   r   c                    | j                         sJ | j                  }|j                  j                  j	                  |        | j                  j                  |d| j                  gi       | _        | xj                  dz  c_        |S )N__add__r   )
is_mutablerX   outputside_effectsmutationr   rY   r   )r$   r3   old_items      r&   r   z#CountIteratorVariable.next_variable}  sk       99
		''-II))"i$))bI	ar'   c                     j                  fd        | j                          | j                         j                  t	        dd             y )Nc                  n     j                   j                  t               j                  d      g      S )Nrq   r   r   s   r&   r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>  s3    G))55i@,,W5 r'   r   F)r   rX   rY   r   r   r   s    `r&   r   z!CountIteratorVariable.reconstruct  sF    	
 					21e<=r'   )r   r   r   r   )r~   r   r   r   _nonvar_fieldsrN   r   intr   r   r#   r   r   r   r   s   @r&   rr   rr   b  s     			(	(N
T 
 '(&'	+O#+ O#+ 	+
 + 
+  7 O >r'   rr   c            	            e Zd ZdZe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#   rI   rc   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&   r   zZipVariable.python_type      
r'   r3   r   c                 @    t        fd| j                  D              S )Nc              3   d   K   | ]'  }t        |t              xs |j                         ) y wr.   )rI   rc   rg   )r9   itr3   s     r&   r;   z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>  s3      
 r4 BB$>$>r$BB
s   -0)allr   r   s    `r&   rg   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   )rg   r   rI   rc   ri   r   rh   r   r   r
   rb   )r$   r3   r   r   r   zippedvars          r&   rh   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        z  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.   )rI   rc   rf   r   r   r   )r   	old_indexr3   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   rf   r   r   r   r   rc   r   	enumerateri   r   r   r   r   
ValueErrorr   r   r   r
   rb   )r$   r3   r4   r   idxr   r   s    `    @r&   r   zZipVariable.next_variable  sR      t~~!#$]B7JJ		,_%7	,	, 	$T^^4 *RHRL)*0 			''-

a
&&t,,1 ) 	{{!8"nn 
%$RL8 %5b9$% 
 )"-I  )	s0   /4C3 3)E"D&%E"&D=:E"<D==%E"c                     | j                   D ]^  }t        |t              rD|| j                  d  }|j	                  |       |j                  t        t        |                   W ||       ` y r.   )r   rI   rc   r   foreachappend_outputr   rf   )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   ru   F)r   r   r   r   rf   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   _cpython_typer   r   rc   r   r   r   r#   rN   r   rg   rh   r   r   r   r   r   s   @r&   r   r     s    
 M 	 
	(	(N (  	
 
T#Y 
*A 
d 
F)F		 F/- 7 /-O /-b
r'   r   c                   v     e Zd ZdZe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'   r3   r   c                      y)NFr!   r   s     r&   rg   z#MapVariable.has_unpack_var_sequence#  s    r'   c                 p    t         |   |      }| j                  j                  ||j                  i       S r.   )r"   r   r   rT   rw   )r$   r3   r4   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   rf   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   rc   r   r#   rN   r   r   rg   r   r   r   r   s   @r&   r   r     sy    
 M ( 	
 
T *A d 9 7 9O 9Ir'   r   c                        e Zd ZdZe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   rx   r   r   Nc                 N    t        |   di | || _        || _        d| _        y r   )r"   r#   r   rx   r   )r$   r   rx   r   r%   s       r&   r#   zFilterVariable.__init__N  s*     	"6" 
r'   c                     t         S r.   )filterr+   s    r&   r   zFilterVariable.python_typeY  s    r'   r3   r   c                 p    t        | j                  t              xs | j                  j                  |      S r.   )rI   rx   rc   rg   r   s     r&   rg   z&FilterVariable.has_unpack_var_sequence\  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.   )
rg   rI   rx   rc   r   rh   r   rT   r
   rb   )r$   r3   r   filtereds       r&   rh   z"FilterVariable.unpack_var_sequencea  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   rI   rx   rc   rf   r   r   r   )r   r$   r3   s    r&   _nextz+FilterVariable.next_variable.<locals>._nextn  sY    

I$--.DMM 22,]B?}}Y//}}22266r'   r   )
r   r   r   is_constant_nonerT   r
   UserFunctionVariabler	   	predicater/   )r$   r3   r  rX   respred_ress   ``    r&   r   zFilterVariable.next_variablem  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.   )rI   rx   rc   r   r   r   r   rf   )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>  s    g&>&>z8&T r'   r   F)r   r   r   r   r   r   s    `r&   r   zFilterVariable.reconstruct  sB    TUw'21e<=r'   r   )r~   r   r   r   r   r   r   r   r   rc   r   r#   rN   r   r   rg   rh   r   r   r   r   r   s   @r&   r   r   A  s    
 M 			(	(N
		 '	 		
 
	T 
*A 
d 


5)
5		 
5 7 O 0#>r'   r   )*r   rZ   r   collections.abcr   r   typingr   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   rn   rr   r   r   r   r!   r'   r&   <module>r     s     
 . % 6 6   4 & /D   |; |;~8; 8;v- @>- >2.>, .>by
" y
x/I+ /IdP>% P>r'   