
    rhZ                     `   d Z ddlZddlZddl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 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)OptionalTYPE_CHECKINGUnion   )graph_break_hints	polyfills	variables)create_call_functioncreate_instruction)handle_observed_exceptionObservedUserStopIterationraise_observed_exceptionunimplemented_v2	UserError   )ValueMutationNewVariableTracker)ConstantVariable)	PyCodegen)InstructionTranslatori  c                   N     e Zd Zd fdZdefdZd Z	 	 	 	 	 	 	 	 d fdZ xZS )ItertoolsVariablereturnc                 2    t        |   di | || _        y N )super__init__value)selfr   kwargs	__class__s      o/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/variables/iter.pyr   zItertoolsVariable.__init__-   s    "6"
    c                 "    d| j                    dS )NzItertoolsVariable()r   r    s    r#   __repr__zItertoolsVariable.__repr__1   s    #DJJ<q11r$   c                     | j                   S Nr'   r(   s    r#   as_python_constantz$ItertoolsVariable.as_python_constant4   s    zzr$   c                 $     j                   t        j                  u rst        fdD              rxD cg c]  }|j	                         }}t        j                  | D cg c]   }t        j                  t        |            " }}t        j                  |t                     S  j                   t        j                  u rddlm} t        d j                         D              rVt        dd  d d d	d
j!                  t#        j                               ddhz
         g t$        j&                         t)              dv rd   j+                        rd   j	                        }	dv rt)              dk(  rd   j,                  }
nt)              dk(  rd   j,                  }
nt)              dk(  r! |t.        j0                        j,                  }
nYt        dd  d d dg t$        j&                         n0t        dd  d d d d ddgt$        j2                         g }j5                  d      }||j7                  |       	D ]%  }||}n	  
||gi       }|j7                  |       ' t        j                  |t                     S  j                   t        j<                  u rǉst)              dk(  rd   j+                        rd   j?                         rd   j	                        }d   jA                         }g }t        j<                  ||      D ]/  }|j7                  t        j                  t        |                   1 t        j                  |t                     S  j                   t        jB                  u rt        d j                         D              rUt        dd  d d dd
j!                  t#        j                               d hz
         g t$        j&                          fd!t)              dk(  r)d   j+                        rd   j	                        }	n0t        d"d  d d d# d d$d%gt$        j2                         d v rfd&}nfd'}g }	 t        jB                  	|(      D ]  \  }}|j7                  t        j                  t
        jD                  jG                  |      rt
        jD                  jI                  |      n|t        j                  t        |      t                     gt                             	 t        j                  |t                     S  j                   t        jJ                  u r`t)              dk  rt        jL                  d+t               iS jO                  tQ        jR                  tT        jJ                              S  j                   t        jV                  u rt        jX                  d+t               iS  j                   t        jZ                  u rt        j\                  d+t               iS t^         Y        S c c}w c c}w # t8        $ r=}t        dd  d d d
 d| | dg t$        j:                  |       Y d }~Fd }~ww xY w# t8        $ r4}t        d)d  d d d*g t$        j2                  |       Y d }~d }~ww xY w),Nc              3   @   K   | ]  }|j                          y wr+   )has_unpack_var_sequence).0argtxs     r#   	<genexpr>z2ItertoolsVariable.call_function.<locals>.<genexpr>B   s     DC//3Ds   )mutation_typer   )BuiltinVariablec              3   $   K   | ]  }|d v 
 yw))initialfuncNr   )r0   keys     r#   r3   z2ItertoolsVariable.call_function.<locals>.<genexpr>N   s     Kc311Ks   z+Unsupported kwargs for itertools.accumulatecall_function  z,Expected kwargs: 'initial', 'func', but got ,r7   r8   gb_typecontextexplanationhints)r   r   r   r   z*Unsupported `func` in itertools.accumulatezDynamo does not know how to get the function to use for itertools.accumulate. itertools.accumulate expects the `func` as the second argument or as a keyword argument.z.Unsupported arguments for itertools.accumulatezBDynamo does not know how to trace itertools.accumulate with args: z and kwargs: z. itertools.accumulate expects an iterable, an optional binary function for accumulation, and an optional initial value to set the starting state.z<Make sure the arguments to itertools.accumulate are correct.z:Unexpected failure during itertools.accumulate() iterationzOUnexpected failure in invoking function during accumulate. Failed running func (r&   )r>   r?   r@   rA   from_excc              3   &   K   | ]	  }|d k7    yw)r9   Nr   )r0   kws     r#   r3   z2ItertoolsVariable.call_function.<locals>.<genexpr>   s     722;7s   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got r9   c           
      *   t        | t        j                        r| j                         S t        | t        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_exprr   r,   r   strtyper   SUPPORTABLE)r9   argsr!   r    s    r#   retrieve_const_keyz;ItertoolsVariable.call_function.<locals>.retrieve_const_key   s    c9#<#<=,,..Y%?%?@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: 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.c                 V     j                  d      j                  | gi             S )Nr9   )getcall_function)xr!   rN   r2   s    r#   keyfuncz0ItertoolsVariable.call_function.<locals>.keyfunc   s-    -

5)77QCD r$   c                      |       S r+   r   )rR   rN   s    r#   rS   z0ItertoolsVariable.call_function.<locals>.keyfunc   s    -a00r$   )r9   z7Unexpected failure during itertools.groupby() iterationz6Unexpected failure in invoking function during groupbyr4   )0r   	itertoolsproductallunpack_var_sequencer	   TupleVariablelistListIteratorVariabler   
accumulatebuiltinr5   anykeysr   joinsetr   
USER_ERRORlenr/   rQ   operatoraddrL   rP   append	Exception	DIFFICULTcombinationsis_python_constantr,   groupbyr   
is_literalcreaterepeatRepeatIteratorVariableinline_user_function_returnr   buildr   countCountIteratorVariablecycleCycleIteratorVariabler   )r    r2   rM   r!   r1   seqsitemitemsr5   seqr8   acceiterablerrS   resultkvrN   r"   s   ````               @r#   rQ   zItertoolsVariable.call_function7   s    JJ)+++DtDD;?@CC++B/@D@@I@Q@QSW@X8<	''T
3E  11%5%7  ZZ9///0KV[[]KK I,TF!D66(C"NxxFKKM 2i5H HIJ!L9-889 4yF"tAw'F'Fr'J1g11"5V#D	Q!&>77DY!^700DY!^*8<<8FFD$ L"0avQvh G%= > 1 < <= !L,TF!D66(C!77;fM& R7!7 W*66 E**Y'CS! ";C
"2T{B7 S!"" 11%5%7  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%5%7  ZZ9,,,777 F,TF!D66(C"BxxFKKM 2eW <=>!@9-889 4yA~$q'"A"A""E1g11"5 I,TF!D66(C!4486vh OD!D
 T*66 1 F%--cw? DAqMM!// $-#=#=#H#H#K !* : : A A! D%& ) > >$(G;K;M!"	 +;*<
, 11&6&8  ZZ9+++4y1} 77)9);  11%%b)*:*:;T6  ZZ9??*22%5%7  ZZ9??*22%5%7  7(T6::A A~ % ($`&4TF!D66($K)337&$uA)G"@$5$?$?"@%& F   U,TF!D66(C X:-99: s<   Y?+%Z8Z	-B3[ 		[2[

[	\)\

\r   N)r2   r   rM   zlist[VariableTracker]r!   zdict[str, VariableTracker]r   r   )	__name__
__module____qualname__r   rJ   r)   r,   rQ   __classcell__r"   s   @r#   r   r   ,   sO    2# 2M;#M; &M; -	M;
 
M; M;r$   r   c                   L     e Zd Zd fdZd Zdee   fdZddZde	fdZ
 xZS )IteratorVariabler   c                 $    t        |   di | y r   )r   r   )r    r!   r"   s     r#   r   zIteratorVariable.__init__  s    "6"r$   c                 J    t        dd|  ddg t        j                         y )NzUnimplemented next() callnext(r&   z(This abstract method must be implementedr=   )r   r   
DYNAMO_BUGr    r2   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_sequencerf   )r    r2   r~   s      r#   force_unpack_var_sequencez*IteratorVariable.force_unpack_var_sequence  s     ((V]];r$   c                 j    	 	  || j                  |             # t        $ r t        |       Y y w xY wr+   )r   r   r   )r    r2   fns      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$   r   )r   r   r   r   r   rZ   r   r   r   boolr   r   r   s   @r#   r   r     s/    #
tO/D 
4 r$   r   c                   6     e Zd Zdeddf fdZd ZddZ xZS )ro   rw   r   Nc                 2    t        |   di | || _        y r   )r   r   rw   )r    rw   r!   r"   s      r#   r   zRepeatIteratorVariable.__init__+  s    "6"	r$   c                     | j                   S r+   )rw   r   s     r#   r   z$RepeatIteratorVariable.next_variable0  s    yyr$   c                     j                  fd        | j                         j                  t        dd             y )Nc                  n     j                   j                  t               j                  d      g      S )Nrn   extend_outputcreate_load_python_modulerU   create_load_attrcodegens   r#   <lambda>z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>5  s3    G))55i@,,X6 r$   r   F)add_push_nullrw   r   r
   r    r   s    `r#   reconstructz"RepeatIteratorVariable.reconstruct3  s:    	
 			21e<=r$   r   r   )r   r   r   r   r   r   r   r   r   s   @r#   ro   ro   *  s     _ 4 

>r$   ro   c                   <     e Zd Zddededdf fdZd Zd	dZ xZS )
rs   rw   stepr   Nc                     t        |   di | t        |t              st	        j
                  |      }t        |t              st	        j
                  |      }|| _        || _        y r   )r   r   rG   r   r   rm   rw   r   )r    rw   r   r!   r"   s       r#   r   zCountIteratorVariable.__init__A  sU    "6"$0#**40D$0#**40D		r$   c                     | j                         sJ | j                  }|j                  j                  j	                  |        | j                  j                  |d| j                  gi       | _        |S )N__add__)
is_mutablerw   outputside_effectsmutationcall_methodr   )r    r2   old_items      r#   r   z#CountIteratorVariable.next_variableJ  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 )Nrr   r   r   s   r#   r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>S  s3    G))55i@,,W5 r$   r   F)r   rw   r   r   r
   r   s    `r#   r   z!CountIteratorVariable.reconstructQ  sF    	
 					21e<=r$   )r   r   r   )r   r   r   intr   r   r   r   r   s   @r#   rs   rs   @  s'    S C $ >r$   rs   c                   T     e Zd Z	 	 	 d	dedeee      dedee   ddf
 fdZd Z	 xZ
S )
ru   Niteratorsavedsaved_indexrw   r   c                 d    |g }t        |   di | || _        || _        || _        || _        y r   )r   r   r   r   r   rw   )r    r   r   r   rw   r!   r"   s         r#   r   zCycleIteratorVariable.__init__`  s<     =E"6" 
&	r$   c                 .   | j                         sJ | j                  	 | j                  j                  |      }t        | j                        t
        kD  rt        dd|  ddt
         g        |j                  j                  j                  |        | j                  j                  |       || _        | j                  | j                  |      S | j                  S t        | j                        dkD  r[|j                  j                  j                  |        | j                  dz   t        | j                        z  | _        | j                  S t        t         |       y # t        $ r& t        |       d | _        | j                  |      cY S w xY w)Nz4input iterator to itertools.cycle has too many itemsr   r&   z0Has reached internal Dynamo max iterator limit: r=   r   r   )r   r   r   rc   r   MAX_ITERATOR_LIMITr   r   r   r   rf   rw   r   r   r   r   StopIteration)r    r2   new_items      r#   r   z#CycleIteratorVariable.next_variablep  sR      ==$.==66r:tzz?%77$ V"'vQ&VWiVj$k 	 		&&//5

!!(+$	99$--b11yy 
 _q II""++D1 $ 0 01 4DJJGD99$]B7 - .)"- $))"--.s   B4E% E% %,FF)Nr   N)r   r   r   r   r   rZ   r   r   r   r   r   r   s   @r#   ru   ru   _  sW     26*." _-. 	
 ' 
 8r$   ru   c                        e Zd ZdZddhej
                  Z	 ddeeee   ef      de	ddf fdZ
d Zde	fd	Zded
   fdZd ZddZddZ xZS )ZipVariablez$
    Represents zip(*iterables)
    indexstrict	iterablesr   Nc                 r    t        |   di | t        |t              sJ || _        d| _        || _        y Nr   r   )r   r   rG   rZ   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$   c                 @    t        fd| j                  D              S )Nc              3   d   K   | ]'  }t        |t              xs |j                         ) y wr+   )rG   rZ   r/   )r0   itr2   s     r#   r3   z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>  s3      
 r4 BB$>$>r$BB
s   -0)rW   r   r   s    `r#   r/   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   )r/   r   rG   rZ   rf   r   rX   r   r   r	   rY   )r    r2   r   r   r!   zippedvars          r#   rX   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 | j                  g }f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)Nc                     t        | t              r#t        |       k\  rt        t               |    S | j                        S r+   )rG   rZ   rc   r   r   r   )r   	old_indexr2   s    r#   get_itemz+ZipVariable.next_variable.<locals>.get_item  s>    "d#B',]B?)}$''++r$   r   z3zip() has one argument of len differing from othersr   )r   r   	enumerater   rf   r   r   r   r   
ValueErrorr   r   r   r	   rY   )r    r2   rM   r   idxr   r   s    `    @r#   r   zZipVariable.next_variable  s      JJ		,	$T^^4 *RHRL)*0 			''-

a
&&t,,1 ) 	{{!8"nn 
%$RL8 %5b9$% 
 )"-I  )	s/   4B. .)DC! D!C85D7C88%Dc           	          | j                   D ]`  }t        |t              rF|| j                  d  }|j	                  |       |j                  t        dt        |                   Y ||       b y NBUILD_TUPLEr1   )r   rG   rZ   r   foreachappend_outputr   rc   )r    r   r   remaining_itemss       r#   reconstruct_itemszZipVariable.reconstruct_items  sa    .. 	B"d#"$TZZ\"20%%&}#o:NO 	r$   c           	         j                  fdd       | j                         j                  t        dt	        | j
                                     t        j                  dk\  rTj                  j                  d      j                  | j                        t        dd	      t        d
d	      g       y j                  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   r   )   
   r   	BUILD_MAPr   CALL_FUNCTION_EXr   )r   r   r   r   rc   r   sysversion_infor   create_load_constr   r   s    `r#   r   zZipVariable.reconstruct  s    ?RV 	 	
 	w'}#dnn2EF	
 w&!!--h7--dkk:&{:&'9qA	 !!"45GQ"OPr$   )Fr   )r   r   r   __doc__r   _nonvar_fieldsrZ   r   r   r   r   r   r/   rX   r   r   r   r   r   s   @r#   r   r     s    
 	 
	(	(N d?3_DEF 
 

T 

F.?)@ 
F(-T	Qr$   r   c                   j     e Zd ZdZdedeeee   ef      ddf fdZd Zde	fdZ
 fd	Zdd
Z xZS )MapVariablez(
    Represents map(fn, *iterables)
    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$   c                      y)NFr   r   s     r#   r/   z#MapVariable.has_unpack_var_sequence  s    r$   c                 p    t         |   |      }| j                  j                  ||j                  i       S r+   )r   r   r   rQ   rx   )r    r2   rM   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	        dt        | j                        dz         t	        dd      g       y )	Nc                  (     j                  dd      S )Nr   r   r   r   s   r#   r   z)MapVariable.reconstruct.<locals>.<lambda>   r   r$   Tr   r   r   r   r   r   )r   r   r   r   r   rc   r   r   s    `r#   r   zMapVariable.reconstruct  sn    ?RV 	 	
 	w'"=c$..6IA6MN"#51=	
r$   r   )r   r   r   r   r   rZ   r   r   r   r   r/   r   r   r   r   s   @r#   r   r     sX     d?3_DEF
 
T 9
r$   r   c                        e Zd ZdZdhej
                  Zdedeee   ef   ddf fdZ	d Z
defd	Zded
   fdZd ZddZddZ xZS )FilterVariablez)
    Represents filter(fn, iterable)
    r   r   r|   r   Nc                 N    t        |   di | || _        || _        d| _        y r   )r   r   r   r|   r   )r    r   r|   r!   r"   s       r#   r   zFilterVariable.__init__6  s*     	"6" 
r$   c                     t         S r+   )filterr(   s    r#   r   zFilterVariable.python_typeA  s    r$   c                 p    t        | j                  t              xs | j                  j                  |      S r+   )rG   r|   rZ   r/   r   s     r#   r/   z&FilterVariable.has_unpack_var_sequenceD  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+   )
r/   rG   r|   rZ   r   rX   r   rQ   r	   rY   )r    r2   r   filtereds       r#   rX   z"FilterVariable.unpack_var_sequenceI  s    ++B///dmmT*tzz|,B2226B77((R4''
344r$   c                      fd}	  |       } xj                   dz  c_          j                  j                  |gi       }t        j                  t
        j                        j                  |gi       }|j                         r|S )Nc                      j                   } t        j                  t              r7| t	        j                        k\  rt        t               j                  |    S j                  j                        S r+   )r   rG   r|   rZ   rc   r   r   r   )r   r    r2   s    r#   _nextz+FilterVariable.next_variable.<locals>._nextT  sY    

I$--.DMM 22,]B?}}Y//}}22266r$   r   )r   r   rQ   r	   UserFunctionVariabler   	predicater,   )r    r2   r  rw   respred_ress   ``    r#   r   zFilterVariable.next_variableS  s    	7 7DJJ!OJ''''TFB7C 55##mBr*  **, r$   c                     t        | j                  t              rP| j                  | j                  d  }|j	                  |       |j                  t        dt        |                   y  || j                         y r   )rG   r|   rZ   r   r   r   r   rc   )r    r   r   s      r#   r   z FilterVariable.reconstruct_itemsh  s[    dmmT*"mmDJJL9OOOO,!!"=c/6J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  s    g&>&>z8&T r$   r   F)r   r   r   r   r
   r   s    `r#   r   zFilterVariable.reconstructr  sB    TUw'21e<=r$   r   )r   r   r   r   r   r   r   r   rZ   r   r   r   r/   rX   r   r   r   r   r   s   @r#   r   r   ,  s    
 			(	(N
		 _->?	
 
	
T 

5.?)@ 5*#>r$   r   )'r   rU   rd   r   typingr   r   r    r   r   r	   bytecode_transformationr
   r   excr   r   r   r   r   baser   r   constantr   torch._dynamo.codegenr   torch._dynamo.symbolic_convertr   r   r   r   ro   rs   ru   r   r   r   r   r$   r#   <module>r     s      
 1 1 6 6 N  4 & /D   X; X;v   F>- >,>, >>-8, -8`tQ" tQn#
+ #
LJ>% J>r$   