o
    hZ                     @   s0  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rTd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)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                       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   b/home/www/facesmatcher.com/frenv_anti/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__1   s   zItertoolsVariable.__repr__c                 C      | j S Nr%   r&   r   r   r"   as_python_constant4      z$ItertoolsVariable.as_python_constanttxr   argslist[VariableTracker]r   dict[str, VariableTracker]r   c                    sp  j tju r.s.tfdd D r.fdd D }dd tj| D }tj|t dS j tju r;ddlm	} t
d	d  D rhtd
d d  d ddt ddh  g tjd t dv r d r d }dv rt dkrd j}qt dkr d j}qt dkr|tjj}qtdd d  d dg tjd ntdd d  d d  d ddgtjd g }d}	|	d ur||	 |D ]J}
|	d u r|
}	n<z
||	|
gi }	W n1 ty, } z$tdd d  d d| d|
 |	 dg tj|d  W Y d }~nd }~ww ||	 qtj|t dS j tju rst dkr d r d  r d } d  }g }t||D ]}
|tt |
 qptj|t dS j tj!u rmt
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"#|r(tj"$|n|tjt |t dgt d qW n( tyd } ztd/d d  d d0g tj|d  W Y d }~nd }~ww tj|t dS j tj%u rt dk rtj& d1t iS 't()t*j% S j tj+u rtj, d1t iS j tj-u rtj. d1t iS t/  S )2Nc                 3   s    | ]}|  V  qd S r)   )has_unpack_var_sequence.0argr,   r   r"   	<genexpr>B   s    z2ItertoolsVariable.call_function.<locals>.<genexpr>c                    s   g | ]}|  qS r   )unpack_var_sequencer1   r4   r   r"   
<listcomp>D   s    z3ItertoolsVariable.call_function.<locals>.<listcomp>c                 S      g | ]	}t t|qS r   r   TupleVariablelist)r2   itemr   r   r"   r7   E   s    )mutation_typer   )BuiltinVariablec                 s   s    | ]}|d vV  qdS ))initialfuncNr   )r2   keyr   r   r"   r5   N       z+Unsupported kwargs for itertools.accumulatecall_function  z,Expected kwargs: 'initial', 'func', but got ,r?   r@   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$   )rG   rH   rI   rJ   Zfrom_excc                 s   s    | ]}|d kV  qdS )rA   Nr   )r2   kwr   r   r"   r5      rB   z(Unsupported kwargs for itertools.groupbyz Expected kwargs: 'key', but got rA   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.groupbyrC   rD   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.)rF   )
isinstancer   ZSymNodeVariableZevaluate_exprr   r*   r   strtyper   SUPPORTABLErA   )r-   r   r   r   r"   retrieve_const_key   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: 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 )NrA   )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 groupbyr=   )0r   	itertoolsproductallr   ZListIteratorVariabler   
accumulatebuiltinr>   anykeysr   joinsetr   Z
USER_ERRORlenr0   r6   rT   operatoraddrP   rS   append	ExceptionZ	DIFFICULTcombinationsZis_python_constantr*   r:   r;   groupbyr   Z
is_literalcreaterepeatRepeatIteratorVariableZinline_user_function_returnr   buildr   countCountIteratorVariablecycleCycleIteratorVariabler   )r   r,   r-   r   Zseqsitemsr>   seqr@   accr<   eiterablerrW   resultkvr    )r-   r   rR   r   r,   r"   rT   7   sf  
	


	





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() callnext(r$   z(This abstract method must be implementedrF   )r   r   Z
DYNAMO_BUGr   r,   r   r   r"   next_variable  s   

zIteratorVariable.next_variablec                 C   s   g }|  ||j |S r)   )force_apply_to_var_sequencerd   )r   r,   rv   r   r   r"   force_unpack_var_sequence  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_sequencery   )rz   r{   r|   r   r   r;   r   r   r   boolr   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 )rj   r<   r   Nc                    r   r   )r   r   r<   )r   r<   r   r    r   r"   r   +  r#   zRepeatIteratorVariable.__init__c                 C   r(   r)   )r<   r   r   r   r"   r   0  r+   z$RepeatIteratorVariable.next_variablecodegenr   c                    s0      fdd  | j  tdd d S )Nc                            t dgS )Nri   extend_outputZcreate_load_python_modulerX   Zcreate_load_attrr   r   r   r"   <lambda>5  
    z4RepeatIteratorVariable.reconstruct.<locals>.<lambda>r   F)add_push_nullr<   r   r	   r   r   r   r   r"   reconstruct3  s
   

z"RepeatIteratorVariable.reconstructr   r   )rz   r{   r|   r   r   r   r   r}   r   r   r    r"   rj   *  s    rj   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 )rm   r   r   r<   stepr   Nc                    sJ   t  jdi | t|tst|}t|tst|}|| _|| _d S r   )r   r   rM   r   r   rh   r<   r   )r   r<   r   r   r    r   r"   r   A  s   




zCountIteratorVariable.__init__c                 C   s<   |   sJ | j}|jj|  | j|d| jgi | _|S )N__add__)
is_mutabler<   outputside_effectsmutationZcall_methodr   )r   r,   Zold_itemr   r   r"   r   J  s
   z#CountIteratorVariable.next_variabler   r   c                    s:      fdd  | j  | j  tdd d S )Nc                      r   )Nrl   r   r   r   r   r"   r   S  r   z3CountIteratorVariable.reconstruct.<locals>.<lambda>r   F)r   r<   r   r   r	   r   r   r   r"   r   Q  s   


z!CountIteratorVariable.reconstruct)r   r   r   )rz   r{   r|   intr   r   r   r}   r   r   r    r"   rm   @  s    	rm   c                       sN   e Zd Z			ddedeee  dedee ddf
 fdd	Zd
d Z	  Z
S )ro   Nr   iteratorsavedsaved_indexr<   r   c                    s:   |d u rg }t  jdi | || _|| _|| _|| _d S r   )r   r   r   r   r   r<   )r   r   r   r   r<   r   r    r   r"   r   `  s   
zCycleIteratorVariable.__init__c                 C   s   |   sJ | jd ur\z;| j|}t| jtkr(tdd|  ddt g d |jj	|  | j
| || _| jd u rC| |W S | jW S  ty[   t| d | _| | Y S w t| jdkrx|jj	|  | jd t| j | _| jS tt| d S )Nz4input iterator to itertools.cycle has too many itemsr   r$   z0Has reached internal Dynamo max iterator limit: rF   r   r   )r   r   r   ra   r   MAX_ITERATOR_LIMITr   r   r   r   rd   r<   r   r   r   r   StopIteration)r   r,   Znew_itemr   r   r"   r   p  s6   


z#CycleIteratorVariable.next_variable)Nr   N)rz   r{   r|   r~   r   r;   r   r   r   r   r}   r   r   r    r"   ro   _  s     
ro   c                       s   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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   rM   r;   r   r   r   )r   r   r   r   r    r   r"   r     s
   
zZipVariable.__init__c                 C      t S r)   )zipr&   r   r   r"   python_type  r   zZipVariable.python_typec                    s   t  fdd| jD S )Nc                 3   s$    | ]}t |tp| V  qd S r)   )rM   r;   r0   )r2   itr4   r   r"   r5     s
    
z6ZipVariable.has_unpack_var_sequence.<locals>.<genexpr>)rZ   r   r   r   r4   r"   r0     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   r8   r   r9   )r2   varr   r   r"   r7     s    z3ZipVariable.unpack_var_sequence.<locals>.<listcomp>)	r0   r   rM   r;   rd   r   r6   r   r   )r   r,   r   r   r   Zzippedr   r   r"   r6     s   

zZipVariable.unpack_var_sequencec                    s   |   sJ | j g } fdd}zt| jD ]\}}||| qW n5 tyZ   | jrY|dkrO| jD ]}z|| W n tyK   t Y q6w  qO t tt	dd  w j
j|  |  jd7  _t|S )Nc                    s2   t | tr t| krtt |   S | S r)   )rM   r;   ra   r   r   r   )r   	old_indexr,   r   r"   get_item  s
   


z+ZipVariable.next_variable.<locals>.get_itemr   z3zip() has one argument of len differing from othersr   )r   r   	enumerater   rd   r   r   r   r   
ValueErrorr   r   r   r   r:   )r   r,   r-   r   idxr   r   r   r"   r     s@   

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r3   )r   rM   r;   r   foreachappend_outputr
   ra   )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   Zload_import_fromr   r   r   r"   r         z)ZipVariable.reconstruct.<locals>.<lambda>TZcall_function_exr   r   )   
   r   	BUILD_MAPr   CALL_FUNCTION_EXr   )r   r   r   r
   ra   r   sysversion_infor   Zcreate_load_constr   r   r   r   r"   r     s    




	zZipVariable.reconstruct)Fr   )rz   r{   r|   __doc__r~   _nonvar_fieldsr;   r   r   r   r   r   r0   r6   r   r   r   r}   r   r   r    r"   r     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"   r0     r   z#MapVariable.has_unpack_var_sequencec                    s   t  |}| j||ji S r)   )r   r   r   rT   rp   )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
   ra   r   r   r   r   r"   r     s   


zMapVariable.reconstructr   )rz   r{   r|   r   r   r;   r   r   r   r   r0   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   6  s   
zFilterVariable.__init__c                 C   r   r)   )filterr&   r   r   r"   r   A  r   zFilterVariable.python_typec                 C   s   t | jtp| j|S r)   )rM   rt   r;   r0   r   r   r   r"   r0   D  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)   )
r0   rM   rt   r;   r   r6   r   rT   r   r:   )r   r,   r   filteredr   r   r"   r6   I  s   z"FilterVariable.unpack_var_sequencec                    s\    fdd}	 | }  j d7  _  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   rM   rt   r;   ra   r   r   r   )r   r   r   r"   _nextT  s   

z+FilterVariable.next_variable.<locals>._nextTr   )r   r   rT   r   ZUserFunctionVariabler   	predicater*   )r   r,   r   r<   resZpred_resr   r   r"   r   S  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   )rM   rt   r;   r   r   r   r
   ra   )r   r   r   r   r   r"   r   h  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   s  r   z,FilterVariable.reconstruct.<locals>.<lambda>r   F)r   r   r   r   r	   r   r   r   r"   r   r  s   

zFilterVariable.reconstructr   )rz   r{   r|   r   r~   r   r   r   r;   r   r   r   r0   r6   r   r   r   r}   r   r   r    r"   r   ,  s&    


r   )'r   rX   rb   r   typingr   r   r    r   r   r   Zbytecode_transformationr	   r
   excr   r   r   r   r   baser   r   Zconstantr   Ztorch._dynamo.codegenr   Ztorch._dynamo.symbolic_convertr   r   r   r~   rj   rm   ro   r   r   r   r   r   r   r"   <module>   s.    \#0w&