
    ^jW]                        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mZ ddlmZ d	d
lmZmZ d	dlmZ d	dlmZ d	dlmZ erddlm Z  ddl!m"Z" dZ#dddede$f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- G d& d'e-      Z. G d( d)e-      Z/ G d* d+e-      Z0 G d, d-e-      Z1 G d. d/e-      Z2y)0a  
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)AnyTYPE_CHECKING   )graph_break_hints	polyfills	variables)create_call_functioncreate_call_function_excreate_instruction)handle_observed_exceptionObservedUserStopIterationraise_observed_exceptionraise_value_errorunimplemented)unpack_iterable   )ValueMutationNewVariableTracker)ConstantVariable)HashableTracker)generic_iternext)	PyCodegen)InstructionTranslatorBasei  txr   iteratorreturnc                 R    	 t        | |       y# t        $ r t        |        Y yw xY w)NFT)r   r   r   )r   r   s     g/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/variables/iter.pyis_iterator_exhaustedr   0   s.    X&$ !"%s    &&c                        e Zd Zdededdf fdZddded	ede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kwargsr   Nc                 2    t        |   di | || _        y N )super__init__r"   )selfr"   r#   	__class__s      r   r(   zItertoolsVariable.__init__<   s    "6"
    r   r   otheropc                 $    ddl m}  || |||      S )Nr   ) python_constant_richcompare_impl)object_protocolr/   )r)   r   r,   r-   r/   s        r   richcompare_implz"ItertoolsVariable.richcompare_impl@   s     	F/b%DDr+   c                 "    d| j                    dS )NzItertoolsVariable()r"   r)   s    r   __repr__zItertoolsVariable.__repr__G   s    #DJJ<q11r+   c                     | j                   S Nr4   r5   s    r   as_python_constantz$ItertoolsVariable.as_python_constantJ       zzr+   c                     | j                   S r8   r4   r5   s    r   get_real_python_backed_valuez.ItertoolsVariable.get_real_python_backed_valueM   r:   r+   argsr   zdict[str, VariableTracker]c                 n     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]  }t        |       }}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'                         rt        d         }	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t,        dt.        f fdt%              d	k7  r0t	        dd  d d d d ddgt        j0                         t        d         }
dv rdt,        dt.        ffd}ndt,        dt.        ffd}g }	 t        j*                  |
|      D ]  \  }}|j)                  t        j                  t        j2                  j5                  |      rt        j2                  j7                  |      n|t        j                  t        |      t!               
      gt!               
              	 t        j                  |t!               
      S  j                   t        j:                  u rVt%              dk  rt=        dt!               iS j?                  t-        j@                  tB        j:                              S  j                   t        jD                  u rst%              dk(  rt        jF                  t!               
      S t%              d	k(  r"t        jF                  d   t!                      S t%              dk(  r&t        jF                  d   d	   t!               !      S tH                 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                  t        d         |      D cg c]   }t        j                  t        |            " }}t        j                  |t!               
      S tH                 S c c}w c c}w # t8        $ r4}t	        dd  d d dg t        j0                  |       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>Y   s     3b2>3   z(Unsupported kwargs for itertools.productcall_function  z#Expected kwargs: 'repeat', but got ,r@   gb_typecontextexplanationhintsr   )mutation_typer   r   c              3   &   K   | ]	  }|d k7    yw)keyNr&   rA   s     r   rD   z2ItertoolsVariable.call_function.<locals>.<genexpr>   s     022;0rE   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got rP   r   c           
      2   ddl m}  ||       } t        | t        j                        r| j                         S | j                         r| j                         S t        dd d d dt        t        |              dg t        j                         y )	Nr   )specialize_symnodez*Unsupported key type for itertools.groupbyrF   rG   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.)rI   )utilsrR   
isinstancer   SymNodeVariableevaluate_expris_python_constantr9   r   strtyper   SUPPORTABLE)rP   rR   r=   r#   r)   s     r   retrieve_const_keyz;ItertoolsVariable.call_function.<locals>.retrieve_const_key   s    6 )-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 )NrP   )getcall_function)r\   r#   r[   r   s    r   keyfuncz0ItertoolsVariable.call_function.<locals>.keyfunc   s-    -

5)77QCD r+   c                      |       S r8   r&   )r\   r[   s    r   r`   z0ItertoolsVariable.call_function.<locals>.keyfunc   s    -a00r+   )rP   z7Unexpected failure during itertools.groupby() iterationz6Unexpected failure in invoking function during groupby)rJ   rK   rL   rM   from_excrN   )itemrN   )rc   steprN   )'r"   	itertoolsproductanyr   joinsetkeysr   
USER_ERRORr9   r   r   TupleVariablelistListIteratorVariabler   combinationslenrW   appendgroupbyr   r   rZ   r   
is_literalcreate	Exceptionr@   RepeatIteratorVariableinline_user_function_returnbuildr   countCountIteratorVariabler'   r_   permutations)r)   r   r=   r#   rargseqsrc   itemsiterableseqr`   resultkver[   r*   s   ````            @r   r_   zItertoolsVariable.call_functionP   s    ::***3F33F,TF!D66(C"ExxFKKM 2hZ ?@A!C9-889 6!8$7798<=OB,=D= &--t>A> ''T
3E  11.0 
 JJ)000D	QQ**,&r473H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~I,TF!D66(C!4486vh OD!D
 T*66 ""d1g.C 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}-tVCSCUVV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?2tAw3OQRS ''T
3E  11.0 
 7(T6::m >D  U,TF!D66(C X:-99: Ts+   (W(%W-7B3W2 %X22	X/;)X**X/)__name__
__module____qualname__r   r(   r   rX   r1   r6   r9   r<   rm   r_   __classcell__r*   s   @r   r!   r!   ;   s    c S T E-E6EEKNE	E2# 2C c l;'l; $%l; -	l;
 
l; l;r+   r!   c                   p     e Zd Zdeddf fdZdddededefd	Zdddefd
Zdddeddf fdZ	d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+   r   r   r,   r-   c                 $    ddl m}  || |||      S )Nr   )object_richcompare)r0   r   )r)   r   r,   r-   r   s        r   r1   z!IteratorVariable.richcompare_impl  s     	8!$E266r+   c                 J    t        dd|  ddg t        j                         y )NzUnimplemented next() callznext(r3   z(This abstract method must be implementedrI   )r   r   
DYNAMO_BUGr)   r   s     r   tp_iternext_implz!IteratorVariable.tp_iternext_impl
  s*    /D6OB1%001		
r+   namer   c                 v    |dk(  s|dk(  rt         j                  j                  d      S t        |   ||      S )N__iter____next__T)r   r   rt   r'   call_obj_hasattr)r)   r   r   r*   s      r   r   z!IteratorVariable.call_obj_hasattr  s=     :!3--44T::w'D11r+   c                     | S )z!Iterators are their own iterator.r&   r   s     r   tp_iter_implzIteratorVariable.tp_iter_impl  s    r+   )r   r   r   r   )r   r   r   r   r(   r   rX   r1   r   r   r   r   r   s   @r   r   r      sq    # # #7-76E7KN7	7
#> 
? 
2-2582	2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 )rv   rc   r#   r   Nc                 2    t        |   di | || _        y r%   )r'   r(   rc   )r)   rc   r#   r*   s      r   r(   zRepeatIteratorVariable.__init__  s    "6"	r+   c                 "    t         j                  S r8   )re   r@   r5   s    r   python_typez"RepeatIteratorVariable.python_type#  s    r+   r   r   c                     | j                   S r8   )rc   r   s     r   r   z'RepeatIteratorVariable.tp_iternext_impl'  s     yyr+   c                     j                  fd        | j                         j                  t        dd             y )Nc                  n     j                   j                  t               j                  d      g      S )Nr@   extend_outputcreate_load_python_modulere   create_load_attrcodegens   r   <lambda>z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>.  s3    G))55i@,,X6 r+   r   F)add_push_nullrc   r   r	   r)   r   s    `r   reconstructz"RepeatIteratorVariable.reconstruct,  s:    	
 			21e<=r+   r   r   r   N)r   r   r   r   r   r(   rY   r   r   r   r   r   s   @r   rv   rv     sB    _    T  #> ? 

>r+   rv   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 )rz   advance_countr   c                 "    t         j                  S r8   )re   ry   r5   s    r   r   z!CountIteratorVariable.python_typeA  s    r+   rc   rd   r#   Nc                     t        |   di | t        |t              st	        j
                  |      }t        |t              st	        j
                  |      }|| _        || _        || _        y r%   )	r'   r(   rT   r   r   rt   rc   rd   r   )r)   rc   rd   r   r#   r*   s        r   r(   zCountIteratorVariable.__init__D  s_     	"6"$0#**40D$0#**40D		*r+   r   r   c                 $   | j                         st        d      | j                  }|j                  j                  j                  |        | j                  j                  |d| j                  gi       | _        | xj                  dz  c_        |S )Nz0CountIteratorVariable must be mutable for next()__add__r   )	
is_mutableAssertionErrorrc   outputside_effectsmutationcall_methodrd   r   )r)   r   old_items      r   r   z&CountIteratorVariable.tp_iternext_implT  sq      !STT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 )Nry   r   r   s   r   r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>`  s3    G))55i@,,W5 r+   r   F)r   rc   rd   r   r	   r   s    `r   r   z!CountIteratorVariable.reconstruct^  sF    	
 					21e<=r+   )r   r   r   r   )r   r   r   r   _nonvar_fieldsrY   r   intr   r   r(   r   r   r   r   s   @r   rz   rz   9  s     			(	(N
T 
 '(&'	+O#+ O#+ 	+
 + 
+ #> ? >r+   rz   c            	            e Zd ZdZeZdhej                  Z	 dd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Z xZS )ZipVariablez$
    Represents zip(*iterables)
    strictr   r#   r   Nc                     t        |   di | t        |t        j                        st        dt        |             || _        || _        y Nz#Expected a tuple of iterables, got r&   )	r'   r(   rT   r   rl   r   rY   r   r   )r)   r   r   r#   r*   s       r   r(   zZipVariable.__init__y  sK     	"6"(I$;$;< #FtH~FV!WXX r+   c                     t         S r8   )zipr5   s    r   r   zZipVariable.python_type      
r+   r   r   c                 h   | j                         st        d      t        | j                  j                        }|dk(  rt        t        |       g }t        |      D ]7  }| j                  j                  |   }	 |j                  t        ||             9 t        j                   |      S # t        $ r| | j                  s |dkD  rt        |d| d       t        |dz   |      D ])  }| j                  j                  |   }t        ||      r) n  t        |       t        |d| d       Y w xY w)Nz&ZipVariable must be mutable for next()r   zzip() argument z  shorter than previous argumentsr   z" is longer than previous arguments)r   r   rp   r   r   r   StopIterationrangerq   r   r   r   r   r   r   r   rl   r)   r   	tuplesizer   iitjit_js           r   r   zZipVariable.tp_iternext_impl  s3     !IJJ++,	>$]B7y! 	A$$Q'B-b"56	8 &&u--1 - {{q5%oaS0PQ
 q1ui0 A==..q1D,R6 	 )"-!/!,NO)s   :B,,BD10D1c                     j                  fdd        | 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   r}   F)r   r   r   create_load_constr   r   r
   r   s    `r   r   zZipVariable.reconstruct  sx    ?RV 	 	
 	))(3))$++6";A6 )u5		
r+   Fr   )r   r   r   __doc__r   _cpython_typer   r   r   boolr   r(   rY   r   r   r   r   r   s   @r   r   r   l  s    
 M 			(	(N !  	
 
T#Y &.#> &.? &.P
r+   r   c                        e Zd ZdZeZdhej                  Z	 ddede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 )MapVariablez(
    Represents map(fn, *iterables)
    r   fn	iterablesr#   r   Nc                     t        |   di | || _        t        |t        j
                        st        dt        |             || _        || _	        y r   )
r'   r(   r   rT   r   rl   r   rY   r   r   )r)   r   r   r   r#   r*   s        r   r(   zMapVariable.__init__  sX     	"6")Y%<%<= 5d9o5FG  "r+   c                     t         S r8   )mapr5   s    r   r   zMapVariable.python_type  r   r+   r   r   c           
      r   | j                         st        d      t        | j                  j                        }g }t        |      D ]7  }| j                  j                  |   }	 |j                  t        ||             9 | j                  j!                  ||i       S # t        $ r | j                  s t        |       |rt        |d|dz    d|        t        d|      D ]=  }| j                  j                  |   }t        ||      r)t        |d|dz    d|        ? t        t        |       Y w xY w)Nz&MapVariable must be mutable for next()zmap() argument r   z shorter than argument z is longer than argument )r   r   rp   r   r   r   rq   r   r   r   r   r   r   r   r   r   r_   r   s           r   r   zMapVariable.tp_iternext_impl  s6     !IJJ++,	y! 	<A$$Q'B<-b"56	<6 ww$$R33/ - <{{)"-%)!a%0GsK q), A==..q1D0T:)/!a%8QRSQT U );)<s   %BA,D6'D65D6c                    j                  fdd        t        j                  | j                  g| j                  j
                               | j                  rrt        j                  dk  rt        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   )      z6Unexpected bug: map(strict=True) requires Python 3.14+r   r   r   r   F)r   r   rl   r   r   r   r   sysversion_infor   r   r   r   r
   r   s    `r   r   zMapVariable.reconstruct  s    ?RV 	 	
 		''(G4==3F3F(GHI;;')$L  !!--h7--dkk:&{: -T59	 !!"9%"GHr+   r   r   )r   r   r   r   r   r   r   r   r   r   r   r(   rY   r   r   r   r   r   s   @r   r   r     s    
 M 			(	(N 	 # 	
  
 T #4#> #4? #4JIr+   r   c                   \     e Zd ZdZeZde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 )FilterVariablez)
    Represents filter(fn, iterable)
    r   r   r#   r   Nc                 @    t        |   di | || _        || _        y r%   )r'   r(   r   r   )r)   r   r   r#   r*   s       r   r(   zFilterVariable.__init__#  s#     	"6" r+   c                     t         S r8   )filterr5   s    r   r   zFilterVariable.python_type-  s    r+   r   r   c                 6   	 t        || j                        }| j                  j                         r|}n| j                  j	                  ||gi       }t        j                  t        j                        j	                  ||gi       }|j                         r|S r8   )
r   r   r   is_constant_noner_   r   UserFunctionVariabler   	predicater9   )r)   r   rc   respred_ress        r   r   zFilterVariable.tp_iternext_impl0  s     #B6Dww'')gg++B; 55##mBr*  **, 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?  s@    TU21e<=r+   r   )r   r   r   r   r   r   r   r   r(   rY   r   r   r   r   r   s   @r   r   r     s_    
 M!! "! 	!
 
!T #> ? >r+   r   c                        e Zd ZU dZdhej
                  ZdZeed<   de	e
ef   deddf fdZd	d
defdZdefdZ xZS )DictViewIteratorz<Base class for dict view iterators (keys, values, or items).	view_typerj   r   r#   r   Nc                 J   t        |   di | | j                  dk(  rt        |      | _        y | j                  dk(  rt        |j                               | _        y | j                  dk7  rt        d| j                        t        |j                               | _        y )Nrj   valuesr   z Expected view_type 'items', got r&   )r'   r(   r   iter_iterr   r   r   )r)   r   r#   r*   s      r   r(   zDictViewIterator.__init__P  s    
 	"6">>V#eDJ^^x'elln-DJ~~($6t~~6HI  ekkm,DJr+   r   r   c                    	 t        | j                        }| j                  dk(  r|j                  S | j                  dk(  r|S |\  }}t	        j
                  ||j                  |f      S # t        t        f$ rS}t        t        |      ||j                  D cg c]  }t	        j
                  ||       nc c}w c}       Y d }~y d }~ww xY w)Nrj   r   )r=   )nextr   r   vtr   rx   RuntimeErrorr   r   rY   r=   )r)   r   rc   r   r   r   as          r   r   z!DictViewIterator.tp_iternext_impla  s    	

#D~~'ww8+1&,,R!$$;;m, 	$Q<=FFCqo++B2CC 	s.   /A* A* &A* *C9CB54CCc                     | j                   dk(  rt        t        i             S | j                   dk(  r"t        t        i j                                     S t        t        i j	                                     S )Nrj   r   )r   rY   r   r   r   r5   s    r   r   zDictViewIterator.python_typev  sT    >>V#R>!^^x'RYY[)**RXXZ())r+   )r   r   r   r   r   r   r   rX   __annotations__dictr   r   r   r(   r   rY   r   r   r   s   @r   r   r   F  sw    F 			(	(N
 Is-O_45- - 
	-"#> ? **T *r+   r   c                   ,    e Zd Z e ei             ZdZy)DictIteratorrj   N)r   r   r   rY   r   r   r   r&   r+   r   r	  r	    s    bNMIr+   r	  c                   H    e Zd Z e ei j                                     ZdZy)DictKeysIteratorrj   N)r   r   r   rY   r   rj   r   r   r&   r+   r   r  r    s    bggi)MIr+   r  c                   H    e Zd Z e ei j                                     ZdZy)DictValuesIteratorr   N)r   r   r   rY   r   r   r   r   r&   r+   r   r  r    s    biik*+MIr+   r  c                   H    e Zd Z e ei j                                     ZdZy)DictItemsIteratorr   N)r   r   r   rY   r   r   r   r   r&   r+   r   r  r    s    bhhj)*MIr+   r  c                   6    e Zd Z e e e                   ZdZy)SetIteratorrj   N)r   r   r   rY   r   ri   r   r   r&   r+   r   r  r    s    ce%MIr+   r  )3r   re   r   typingr   r    r   r   r   bytecode_transformationr	   r
   r   excr   r   r   r   r   rS   r   baser   r   constantr   hashabler   r0   r   torch._dynamo.codegenr   torch._dynamo.symbolic_convertr   MAX_ITERATOR_LIMITr   r   r!   r   rv   rz   r   r   r   r   r	  r  r  r  r  r&   r+   r   <module>r     s%    
 % 6 6 
  $ 3 & % - /H   #/>	A; A;H >>- >60>, 0>fQ
" Q
hXI" XIv(>% (>V6*' 6*r# 
' 
) 
( 
" r+   