o
    iN                     @   s$  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 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rNddlmZ ddlmZ dZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd de"Z#G d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)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                       sR   e Zd Zd fddZdefddZdd Z			
						d fddZ  ZS )ItertoolsVariablereturnNc                       t  jdi | || _d S N )super__init__value)selfr   kwargs	__class__r   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/torch/_dynamo/variables/iter.pyr   ,      
zItertoolsVariable.__init__c                 C   s   d| j  dS )NzItertoolsVariable()r   r   r   r   r!   __repr__0   s   zItertoolsVariable.__repr__c                 C      | j S Nr$   r%   r   r   r!   as_python_constant3      z$ItertoolsVariable.as_python_constanttxr   argslist[VariableTracker]r   dict[str, VariableTracker]r   c                    s  j tju r_tdd  D r2tdd d  d ddt dh  g tj	d	 d v r?d 
 }nd
}fdd D }dd tj|d|iD }tj|t dS j tju rst dkr d r d
  r d } d
 
 }g }t||D ]}|tt| qtj|t dS j tju rtdd  D rtdd d  d ddt dh  g tj	d	  fddt d
kr d r d }	ntdd d  d d  d ddgtjd	 dv rfdd}
nfdd}
g }z1tj|	|
dD ]'\}}|tjtj|rCtj|n|tjt|t dgt d q.W n( ty } ztd d d  d d!g tj|d" W Y d }~nd }~ww tj|t dS j tju rt dk rtj d#t iS t t!j S j tj"u rtj# d#t iS j tj$u rt d
kst dkr d
  rst dkr d
 
 }nd }d$d t$ d %|D }tj|t dS t& ' S )%Nc                 s       | ]}|d kV  qdS )repeatNr   .0kwr   r   r!   	<genexpr>?       z2ItertoolsVariable.call_function.<locals>.<genexpr>z(Unsupported kwargs for itertools.productcall_function  z#Expected kwargs: 'repeat', but got ,r0   gb_typecontextexplanationhintsr   c                    s   g | ]}|  qS r   )force_unpack_var_sequence)r2   argr+   r   r!   
<listcomp>L   s    z3ItertoolsVariable.call_function.<locals>.<listcomp>c                 S      g | ]	}t t|qS r   r   TupleVariablelistr2   itemr   r   r!   rA   M       )mutation_typer   r   c                 s   r/   )keyNr   r1   r   r   r!   r4   e   r5   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got rJ   c                    sd   t | tjr
|  S t | tjr|  S tdd d  d dtt|  dg t	j
d d S )Nz*Unsupported key type for itertools.groupbyr6   r7   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.)r9   )
isinstancer   SymNodeVariableevaluate_exprr   r)   r   strtyper   SUPPORTABLErJ   )r,   r   r   r   r!   retrieve_const_keyn   s   

z;ItertoolsVariable.call_function.<locals>.retrieve_const_keyz+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.c                    s     d| gi S )NrJ   )getcall_functionx)r   rR   r+   r   r!   keyfunc   s   z0ItertoolsVariable.call_function.<locals>.keyfuncc                    s    | S r(   r   rU   )rR   r   r!   rW      s   rQ   z7Unexpected failure during itertools.groupby() iterationz6Unexpected failure in invoking function during groupby)r:   r;   r<   r=   from_excrI   c                 S   rB   r   rC   rF   r   r   r!   rA      rH   )(r   	itertoolsproductanykeysr   joinsetr   
USER_ERRORr)   r   ListIteratorVariabler   combinationslenhas_unpack_var_sequenceis_python_constantunpack_var_sequenceappendrD   rE   groupbyrP   r   
is_literalcreate	Exceptionr0   RepeatIteratorVariableinline_user_function_returnr   buildr   countCountIteratorVariablepermutationsr>   r   rT   )r   r+   r,   r   rseqsitemsiterablerG   seqrW   resultkver   )r,   r   rR   r   r+   r!   rT   6   s   



*zItertoolsVariable.call_functionr   N)r+   r   r,   r-   r   r.   r   r   )	__name__
__module____qualname__r   rN   r&   r)   rT   __classcell__r   r   r   r!   r   +   s    r   c                       sP   e Zd Zd fddZdd Zdee fddZdd	d
Zde	fddZ
  ZS )IteratorVariabler   Nc                    s   t  jdi | d S r   )r   r   )r   r   r   r   r!   r      s   zIteratorVariable.__init__c                 C   s"   t dd|  ddg tjd d S )NzUnimplemented next() callznext(r#   z(This abstract method must be implementedr9   )r   r   
DYNAMO_BUGr   r+   r   r   r!   next_variable   s   

zIteratorVariable.next_variablec                 C   s   g }|  ||j |S r(   )force_apply_to_var_sequencerf   )r   r+   rv   r   r   r!   r>      s   z*IteratorVariable.force_unpack_var_sequencec                 C   s4   	 z	||  | W n ty   t| Y d S w qr(   )r   r   r
   )r   r+   fnr   r   r!   r      s   z,IteratorVariable.force_apply_to_var_sequencec                 C      dS NTr   r   r   r   r!   has_force_unpack_var_sequence      z.IteratorVariable.has_force_unpack_var_sequencerz   )r{   r|   r}   r   r   rE   r   r>   r   boolr   r~   r   r   r   r!   r      s    

r   c                       s.   e Zd ZdZdef fddZdd Z  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
    objc                    s"   t  jdi | || _d| _d S )NFr   )r   r   r   generator_exhausted)r   r   r   r   r   r!   r     s   
zObjectIteratorVariable.__init__c                 C   s6   | j rtt| z| j|W S  ty   d| _  w r   )r   r   StopIterationr   r   r   r   r   r   r!   r     s   
z$ObjectIteratorVariable.next_variable)r{   r|   r}   __doc__r   r   r   r~   r   r   r   r!   r      s    r   c                       s8   e Zd Zdeddf fddZdd Zdd
dZ  ZS )rk   rG   r   Nc                    r   r   )r   r   rG   )r   rG   r   r   r   r!   r     r"   zRepeatIteratorVariable.__init__c                 C   r'   r(   )rG   r   r   r   r!   r      r*   z$RepeatIteratorVariable.next_variablecodegenr   c                    s0      fdd  | j  tdd d S )Nc                            t dgS )Nr0   extend_outputcreate_load_python_modulerY   create_load_attrr   r   r   r!   <lambda>%  
    z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>r   F)add_push_nullrG   r   r   r   r   r   r   r!   reconstruct#  s
   

z"RepeatIteratorVariable.reconstructr   r   )r{   r|   r}   r   r   r   r   r~   r   r   r   r!   rk     s    rk   c                       s>   e Zd Zddededdf fddZd	d
 ZdddZ  ZS )ro   r   r   rG   stepr   Nc                    sJ   t  jdi | t|tst|}t|tst|}|| _|| _d S r   )r   r   rK   r   r   ri   rG   r   )r   rG   r   r   r   r   r!   r   1  s   




zCountIteratorVariable.__init__c                 C   s<   |   sJ | j}|jj|  | j|d| jgi | _|S )N__add__)
is_mutablerG   outputside_effectsmutationcall_methodr   )r   r+   old_itemr   r   r!   r   :  s
   z#CountIteratorVariable.next_variabler   r   c                    s:      fdd  | j  | j  tdd d S )Nc                      r   )Nrn   r   r   r   r   r!   r   C  r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>r   F)r   rG   r   r   r   r   r   r   r!   r   A  s   


z!CountIteratorVariable.reconstruct)r   r   r   )r{   r|   r}   intr   r   r   r~   r   r   r   r!   ro   0  s    	ro   c                       s   e Zd ZdZddhejZ	ddee deddf fdd	Z	d
d Z
defddZded fddZdd ZdddZdddZ  ZS )ZipVariablez$
    Represents zip(*iterables)
    indexstrictF	iterablesr   Nc                    s6   t  jdi | t|tsJ || _d| _|| _d S Nr   r   )r   r   rK   rE   r   r   r   )r   r   r   r   r   r   r!   r   Z  s
   
zZipVariable.__init__c                 C      t S r(   )zipr%   r   r   r!   python_typeg  r   zZipVariable.python_typec                    s   t  fdd| jD S )Nc                 3   s$    | ]}t |tp| V  qd S r(   )rK   rE   rc   )r2   itr@   r   r!   r4   k  s
    
z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>)allr   r   r   r@   r!   rc   j  s   z#ZipVariable.has_unpack_var_sequencer   c                 C   s~   |  |sJ g }| jD ]}t|tr||| jd   q||| q| jr/d| jini }t|i |}dd |D S )Nr   c                 S   rB   r   rC   )r2   varr   r   r!   rA   z  s    z3ZipVariable.unpack_var_sequence.<locals>.<listcomp>)	rc   r   rK   rE   rf   r   re   r   r   )r   r+   r   r   r   zippedr   r   r!   re   p  s   

zZipVariable.unpack_var_sequencec                    s   |   sJ t| jdkrtt | j g } fdd}zt| jD ]\}}||| q$W n5 tyf   | j	re|dkr[| jD ]}z|| W n tyW   t
 Y qBw  n t
 ttdd  w jj|  |  jd7  _t|S )Nr   c                    s2   t | tr t| krtt |   S | S r(   )rK   rE   rb   r   r   r   )r   	old_indexr+   r   r!   get_item  s
   


z+ZipVariable.next_variable.<locals>.get_itemz3zip() has one argument of len differing from othersr   )r   rb   r   r   r   r   	enumeraterf   r   r   r
   r   
ValueErrorr   r   r   r   rD   )r   r+   r,   r   idxr   r   r   r!   r   |  sD   


zZipVariable.next_variabler   r   c                 C   sR   | j D ]#}t|tr"|| jd  }|| |tdt|d q|| qd S NBUILD_TUPLEr?   )r   rK   rE   r   foreachappend_outputr	   rb   )r   r   r   remaining_itemsr   r   r!   reconstruct_items  s   



zZipVariable.reconstruct_itemsc              	      s    j  fdddd |    tdt| jd tjdkr;  	d 	| j
td	d
dtdd
dg d S  tddd d S )Nc                           ddS )Nbuiltinsr   load_import_fromr   r   r   r!   r         z)ZipVariable.reconstruct.<locals>.<lambda>Tcall_function_exr   r   )   
   r   	BUILD_MAPr   CALL_FUNCTION_EXr   )r   r   r   r	   rb   r   sysversion_infor   create_load_constr   r   r   r   r!   r     s    




	zZipVariable.reconstruct)Fr   )r{   r|   r}   r   r   _nonvar_fieldsrE   r   r   r   r   rc   re   r   r   r   r~   r   r   r   r!   r   O  s*    	
.r   c                       sj   e Zd ZdZdedeeee ef  ddf fddZdd	 Zde	fd
dZ
 fddZdddZ  ZS )MapVariablez(
    Represents map(fn, *iterables)
    r   r   r   Nc                    s   t  j|fi | || _d S r(   )r   r   r   )r   r   r   r   r   r   r!   r     s   
zMapVariable.__init__c                 C   r   r(   )mapr%   r   r   r!   r     r   zMapVariable.python_typec                 C   r   )NFr   r   r   r   r!   rc     r   z#MapVariable.has_unpack_var_sequencec                    s   t  |}| j||ji S r(   )r   r   r   rT   rs   )r   r+   r,   r   r   r!   r     s   zMapVariable.next_variabler   r   c                    sV    j  fdddd  | j |    tdt| jd dtdd	dg d S )
Nc                      r   )Nr   r   r   r   r   r   r!   r     r   z)MapVariable.reconstruct.<locals>.<lambda>Tr   r   r   r   r   r   )r   r   r   r   r	   rb   r   r   r   r   r!   r     s   


zMapVariable.reconstructr   )r{   r|   r}   r   r   rE   r   r   r   r   rc   r   r   r~   r   r   r   r!   r     s    	r   c                       s   e Zd ZdZdhejZdedeee ef ddf fddZ	d	d
 Z
defddZded fddZdd ZdddZdddZ  ZS )FilterVariablez)
    Represents filter(fn, iterable)
    r   r   rt   r   Nc                    s(   t  jdi | || _|| _d| _d S r   )r   r   r   rt   r   )r   r   rt   r   r   r   r!   r     s   
zFilterVariable.__init__c                 C   r   r(   )filterr%   r   r   r!   r     r   zFilterVariable.python_typec                 C   s   t | jtp| j|S r(   )rK   rt   rE   rc   r   r   r   r!   rc     s   z&FilterVariable.has_unpack_var_sequencer   c                 C   sZ   |  |sJ d }t| jtr| j| jd  }n| j|}| j||i }t	|ggS r(   )
rc   rK   rt   rE   r   re   r   rT   r   rD   )r   r+   r   filteredr   r   r!   re     s   z"FilterVariable.unpack_var_sequencec                    sz    fdd}	 | }  j d7  _ t jtr! jjd u r!|}n	 j|gi }ttj	|gi }|
 r<|S q)Nc                     s@    j } t jtr| t jkrtt  j|  S  jS r(   )r   rK   rt   rE   rb   r   r   r   )r   r   r   r!   _next  s   

z+FilterVariable.next_variable.<locals>._nextTr   )r   rK   r   r   r   rT   r   UserFunctionVariabler   	predicater)   )r   r+   r   rG   respred_resr   r   r!   r     s   
zFilterVariable.next_variabler   r   c                 C   sN   t | jtr | j| jd  }|| |tdt|d d S || j d S r   )rK   rt   rE   r   r   r   r	   rb   )r   r   r   r   r   r!   r   /  s   
z FilterVariable.reconstruct_itemsc                    s:      fdd  | j |    tdd d S )Nc                      r   )Nr   r   r   r   r   r   r!   r   :  r   z,FilterVariable.reconstruct.<locals>.<lambda>r   F)r   r   r   r   r   r   r   r   r!   r   9  s   

zFilterVariable.reconstructr   )r{   r|   r}   r   r   r   r   r   rE   r   r   r   rc   re   r   r   r   r~   r   r   r   r!   r     s&    


r   )%r   rY   r   typingr   r    r   r   r   bytecode_transformationr   r	   excr
   r   r   r   r   baser   r   constantr   torch._dynamo.codegenr   torch._dynamo.symbolic_convertr   MAX_ITERATOR_LIMITr   r   r   rk   ro   r   r   r   r   r   r   r!   <module>   s,    -# {&