o
    hj                     @   sJ  d Z ddlZddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
ZddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ dd	lmZmZ dd
lm Z m!Z!m"Z"m#Z# ddlm$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 ej5G dd dZ6G dd dZ7dS )a  
This module provides utilities for generating Python bytecode in PyTorch's Dynamo system.
It includes functionality for:
- Constructing bytecode sequences for Python operations
- Managing stack operations and variable tracking
- Handling graph outputs and their conversions
- Supporting different Python versions (3.11+, 3.12+, 3.13+)
- Converting high-level operations to low-level bytecode instructions
- Managing constant loading and attribute access
- Supporting function creation and closure handling
    N)Counter)OptionalUnion)
OrderedSet   )graph_break_hintsutils)
add_push_nulladd_push_null_call_function_excreate_call_functioncreate_call_methodcreate_dup_topcreate_instructioncreate_load_constcreate_load_methodcreate_rot_nInstruction)IncorrectUsageunimplemented_v2)
AttrSourceChainedSourceDictGetItemSourceSource)is_safe_constantrot_n_helper)ValueMutationExistingVariableTracker)4ContextlibContextManagerLocalGeneratorObjectVariableLocalGeneratorObjectVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariablec                   @   s   e Zd ZU eed< eed< dS )GraphOutputEntryindexvariableN)__name__
__module____qualname__int__annotations__r    r-   r-   i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/_dynamo/codegen.pyr%   9   s   
 r%   c                   @   sJ  e Zd ZdZ					dkdeejj dee ddfddZ	dd	d
dZ
dd Zdd ZdlddZdmddZdd Zdd Zdd Zdd ZdefddZdd  Zd!d" Zd#d$ Zd%d& Zdee fd'd(Zdefd)d*Zdefd+d,Zdefd-d.Zdefd/d0Zdefd1d2Zdldefd3d4Z defd5d6Z!defd7d8Z"d9d: Z#d;d< Z$defd=d>Z%d?d@ Z&dAdB Z'defdCdDZ(dEdF Z)dndHdIZ*dJdK Z+dLdM Z,dNdO Z-dPe.dQe/fdRdSZ0dTdU Z1dVdW Z2dXdY Z3	GdndZed[e4j5dQe/fd\d]Z6defd^d_Z7d`e8ddfdadbZ9dZeddfdcddZ:dodedfZ;dee fdgdhZ<defdidjZ=dS )p	PyCodegenz<
    Helper class uses for constructing Python bytecode
    Nrootgraph_output_varreturnc                 C   sn   || _ d | _t | _i | _g | _|pi | _|| _|| _	| jj
j| _| jj| _| jj
j| _d| _|p3i | _d S )NT)r0   top_of_stackcollectionsr   usesgraph_outputs_outputtempvarstxr1   outputcode_optionscell_and_freevarsnew_varvalue_from_sourceoverridden_sources)selfr9   r0   r1   r8   r?   r-   r-   r.   __init__D   s   


zPyCodegen.__init__T)r>   c                C   s4   | j }|  j |M  _ z| | W || _ d S || _ w N)r>   foreach)r@   stack_valuesr>   prevr-   r-   r.   restore_stacka   s
   zPyCodegen.restore_stackc                 C   s   dd | j  D S )Nc                 S   s   g | ]}|j qS r-   )r'   .0xr-   r-   r.   
<listcomp>j   s    z/PyCodegen.graph_output_vars.<locals>.<listcomp>)r6   valuesr@   r-   r-   r.   graph_output_varsi      zPyCodegen.graph_output_varsc                 C   s$   | | }|d u sJ d| d S )Nzreconstruct!=None )reconstruct)r@   valueresr-   r-   r.   call_reconstructl   s   
zPyCodegen.call_reconstructFc                 C   s|   t | j}tjdk r|   |  | j|d }| j|d= |r)| jt| n| jt| tjdkr<|   dS dS )a  
        `gen_fn` generates instructions via PyCodegen methods
        that push a single callable to the stack.

        `add_push_null` pushes a NULL to the stack before or after the
        instructions generated by `gen_fn`, depending on Python version.

        Will attempt to use the NULL push bit for instructions
        with such bits (LOAD_GLOBAL 3.11+, LOAD_ATTR 3.12+, LOAD_SUPER_ATTR).
              N)lenr7   sysversion_info	clear_tosextendr
   r	   )r@   gen_fncall_function_exold_lenadded_instsr-   r-   r.   r	   p   s   


zPyCodegen.add_push_nullc                    sr  t |trsj||}|du sJ dj|u r"jt  dS j|dur;j	j|  |_dS z
| W n tya   tdt|dt| d| dg tjd Y nw jt  | |_dS t |tszJ jj |rj|u rt  dS j|dur	j|  |_dS | rt |trtd	|jdur|r| rt |tst |jts͈jr҈|jS | rt| r|  nLt |t r!|"fd
d #  j$ j%|&j'dd (t)dd nt |t*rP|+ t,krPj'j-sP!|.j't/j0 fdd}"| (t)dd nt |t1t*t2t3fr!|t |t3r"fdd #  j$ (t)dd nt |t2r|j4r fdd}"| (t)dd n#  j$ nt |t5r|j67d}|d j8d v rЈ	|d  |dd }nj9dusJ :j9 |D ]};| qnDj<|  d7  < z
| W n ty   tdt|d| ddgtj=dd Y nw |r4|jv r4jt  | |_dS )a$  
        Generate code such that top-of-stack (TOS) is set to value.

        `allow_cache` controls the behavior in the following manner. `value` can
        either be a VariableTracker or a Source.

        If `value` is a `Source`, `allow_cache` must be True (invariant asserted
        below). If the source was reconstructed earlier, we will reuse the
        generated code by loading from top of stack or tempvars.

        If `value` is a `VariableTracker`, we have the following cases:

        1) `allow_cache=True`
            a) If the value.source is not None, we will emit the code based on
            `value.source` to handle aliasing.
            b) If value.source is None (example reconstructing a local list
            returned by the compiled function), we will reconstruct the variable
            tracker (w/o any source) to emit bytecode that generates a new
            python object.

            In both cases of value.source being None or not, if the value was
            reconstructed earlier, we will reuse the generated code by loading from
            top of stack or tempvars.

        2) `allow_cache=False` - This is a special case (allow_cache defaults to
        True).
            a) If the value.source is not None, we reconstruct the variable
            tracker and emit a new python object. You might wonder what about
            aliasing? The place where we use this config also has the followup
            code where the original python object is assigned to this new python
            value to handle aliasing (check side_effects.py and search for
            allow_cache=False).

            b) If value.source is None, this is not allowed. TODO - assert this.

        Notable effects:
        1. `self.top_of_stack` will be set to `value`, if we don't codegen
           `value` based on source.
        2. `self.uses[value]` will increment, if we don't codegen `value` based
           on source or cache/top-of-stack reuse; in other words, if we codegen
           as if `value` is modelling some brand new python value.
        Tz#allow_cache must be True for SourceNz:Reconstruction failure: source.reconstruct not implementedz6Dynamo has no bytecode reconstruction implemented for z
 variable .)gb_typecontextexplanationhintszENYI: Returning a @contextmanager object from a torch.compile functionc                           tjdS )Nto_subclassload_import_fromr   r(   r-   rL   r-   r.   <lambda>      z$PyCodegen.__call__.<locals>.<lambda>add   Fc                      $      j d d S Nitemload_graph_outputr&   appendcreate_load_attrr-   r6   graph_outputs_keyr:   r@   r-   r.   r[   '     z"PyCodegen.__call__.<locals>.gen_fnr   c                      rd   )Nto_numpy_helperrf   r-   rL   r-   r.   rh   :  ri   r   c                      rm   rn   rp   r-   rt   r-   r.   r[   @  rv   co_varnameszReconstruction failurezJDynamo has no bytecode reconstruction implemented for sourceless variable zIf Dynamo attempting to trace a return statement and your code is attempting to return a variable that Dynamo cannot reconstruct, then remove it from the return statement.zReport an issue to PyTorch if you need reconstrtuction support. Note that objects that don't havereconstruction rules may be fundamentally unreconstructable.)>
isinstancer   r?   getr3   r7   rr   r   r8   create_loadrR   NotImplementedErrorr   strtyper   
DYNAMO_BUG	add_cacher   r6   is_realizedr   r   sourcer   mutation_typer   r>   is_python_constantr   as_python_constantr   r$   add_graph_outputr	   rq   r&   create_load_globalglobal_mangled_class_namer9   rZ   r   r!   python_typefloatexport	as_tensortorchfloat64r"   r#   r    need_unwrapr   
module_keysplitr;   r0   create_load_const_uncheckedrs   r5   CAUSED_BY_EARLIER_GRAPH_BREAK)r@   rP   allow_cacher   r[   partspartr-   rt   r.   __call__   s   
+












	





zPyCodegen.__call__c                 C   s0   t | }|| jvrtt| j|| j|< |S rB   )idas_proxyr6   r%   rV   )r@   rP   ru   r-   r-   r.   r   i  s   


zPyCodegen.add_graph_outputc                 C   s:   | j }|| | j || | ||   d S rB   )r7   rr   r{   r1   r   create_binary_subscr)r@   r&   r:   r-   r-   r.   rq   q  s   zPyCodegen.load_graph_outputc                 C   s(   |   }|| j|< | j| | d S rB   )r=   r8   r7   rr   create_store)r@   rP   varr-   r-   r.   r   w  s   
zPyCodegen.add_cachec                 C   s   |D ]}| | qd S rB   r-   )r@   itemsir-   r-   r.   rC   |  s   
zPyCodegen.foreachc                 C   s   t dS )NBINARY_SUBSCRr   rL   r-   r-   r.   r        zPyCodegen.create_binary_subscrc                 C   sP   t dd|}| jj}||v rt|| t|ksJ n|||< | j|ddgS )zStore value in a new globalz[^a-zA-Z0-9_]+_Trj   )resubr9   	f_globalsr   r   )r@   namerP   r   r-   r-   r.   setup_globally_cached  s   zPyCodegen.setup_globally_cachedc                 C   s
   d | _ d S rB   )r3   rL   r-   r-   r.   rY     s   
zPyCodegen.clear_tosc                 C   s&   t |tsJ | j| |   d S rB   )ry   r   r7   rr   rY   )r@   instr-   r-   r.   append_output  s   zPyCodegen.append_outputc                 C   s.   t dd |D sJ | j| |   d S )Nc                 s   s    | ]}t |tV  qd S rB   )ry   r   rG   r-   r-   r.   	<genexpr>  s    z*PyCodegen.extend_output.<locals>.<genexpr>)allr7   rZ   rY   )r@   instsr-   r-   r.   extend_output  s   zPyCodegen.extend_outputc                 C   s   | j S rB   )r7   rL   r-   r-   r.   get_instructions  s   zPyCodegen.get_instructionsc                 C   (   || j d v sJ | dtd|dS )Nrx    missing	LOAD_FASTargvalr;   r   r@   r   r-   r-   r.   r{        zPyCodegen.create_loadc                 C   s.   ||   v sJ tjdkrdnd}t||dS )NrS   r   LOAD_CLOSUREr   )r<   rW   rX   r   )r@   r   	inst_namer-   r-   r.   create_load_closure  s   zPyCodegen.create_load_closurec                 C      ||   v sJ td|dS )N
LOAD_DEREFr   r<   r   r   r-   r-   r.   create_load_deref     zPyCodegen.create_load_derefc                 C   r   )Nrx   r   
STORE_FASTr   r   r   r-   r-   r.   r     r   zPyCodegen.create_storec                 C   r   )NSTORE_DEREFr   r   r   r-   r-   r.   create_store_deref  r   zPyCodegen.create_store_derefc                 C   s:   |r	| j j| || jd v sJ | dtd|dS )Nco_namesz not in co_namesLOAD_GLOBALr   )r9   r:   update_co_namesr;   r   )r@   r   rk   r-   r-   r.   r     s   zPyCodegen.create_load_globalc                 C   s   t |S rB   r   r@   rP   r-   r-   r.   r     r   zPyCodegen.create_load_constc                 C   s   t |ddS )NF)checkedr   r   r-   r-   r.   r        z%PyCodegen.create_load_const_uncheckedc                 C   s    | j j| | t| d S rB   )r9   r:   r   r   r   r   r-   r-   r.   load_method  s   zPyCodegen.load_methodc                 C   s   |  t| d S rB   )r   r   )r@   nargsr-   r-   r.   call_method     zPyCodegen.call_methodc                 C   .   || j d vr| j d  |f7  < td|dS )Nr   	LOAD_ATTRr   r   r   r-   r-   r.   rs        zPyCodegen.create_load_attrc                 C      |  | | d S rB   )r   rs   r   r-   r-   r.   	load_attr  rN   zPyCodegen.load_attrc                    s    fdd| dD S )Nc                    s   g | ]}  |qS r-   )rs   )rH   r   rL   r-   r.   rJ     s    z/PyCodegen.create_load_attrs.<locals>.<listcomp>r_   )r   )r@   namesr-   rL   r.   create_load_attrs  s   zPyCodegen.create_load_attrsc                 C   r   )Nr   
STORE_ATTRr   r   r   r-   r-   r.   create_store_attr  r   zPyCodegen.create_store_attrc                 C   r   rB   )r   r   r   r-   r-   r.   
store_attr  rN   zPyCodegen.store_attrr   c                 C   s   g }|r.t jdkr.|t| j|dd |dkr,|g | |d | |d  |S || j|ddg| |d  |S )z6Load the global fn_name on the stack num_on_stack downrT      Trj   r   rl   r   )rW   rX   rZ   r	   r   rot_n)r@   fn_name	push_nullnum_on_stackr:   r-   r-   r.   load_function_name  s&   zPyCodegen.load_function_namec                 C   sX   zt |W S  ty+   td|d| t|gt dtdddtd|d Y S w )NBUILD_TUPLEargrl   CALL_FUNCTION_EXr   UNPACK_SEQUENCE)r   AttributeErrorr   r   r   )r@   nr-   r-   r.   r     s   



zPyCodegen.rot_nc                 C   sL   t jdksJ | dd gt jdkrtdddfndtd	d
tdS )Nr   c                   S   s   d S rB   r-   r-   r-   r-   r.   rh     s    z$PyCodegen.pop_null.<locals>.<lambda>rS   SWAPrl   r   r-   r   FPOP_TOP)rW   rX   r   r   r   rL   r-   r-   r.   pop_null  s   
	zPyCodegen.pop_nullc                 C   s   |  td d S )Nr   )r   r   rL   r-   r-   r.   pop_top
  r   zPyCodegen.pop_topr   r   c                 C   s   |  t||d d S )N)r   )r   r   )r@   r   r   r-   r-   r.   call_function  s   zPyCodegen.call_functionc                 C   s   |  t  d S rB   )r   r   rL   r-   r-   r.   dup_top  s   zPyCodegen.dup_topc                 C   r   rB   )r   r   r@   varnamer-   r-   r.   store  rN   zPyCodegen.storec                 C   r   rB   )r   r   r   r-   r-   r.   
load_deref  rN   zPyCodegen.load_derefr   codec                    s    j sJ j fdd}|r5tjdkr5| |d  |d  n|  |d    d S )Nc                     s   D ]} |   v sJ |  qtdtd   tjdk r5 tjdkrItdtdddg d S tddd d S )Nr   r   r   rS   MAKE_FUNCTIONSET_FUNCTION_ATTRIBUTE   )	r<   rr   r   r   rV   r   rW   rX   rZ   )r   r   r   freevarsr:   r@   r-   r.   r[      s   


z4PyCodegen.make_function_with_closure.<locals>.gen_fnr   rl   r   )co_freevarsr7   rW   rX   r	   rZ   r   rY   )r@   r   r   r   r   r[   r-   r   r.   make_function_with_closure  s   
z$PyCodegen.make_function_with_closurec                 C   sd   | j j}|j}tdd|j}||d|u r| j|ddS d| }| j j||}| j|ddS )zT
        Generate a LOAD_GLOBAL instruction to fetch a given python module.
        z^.*[.] NTrj   
___module_)	r9   r:   global_scoper   r   r(   rz   r   install_global_by_id)r@   modr:   r   r   prefixglobal_namer-   r-   r.   create_load_python_module?  s   
z#PyCodegen.create_load_python_moduler   c                 C   s   || j vrd| j |< dS dS )zM
        Mark a source as a temp variable, so that it can be reused.
        N)r8   )r@   r   r-   r-   r.   mark_source_tempL  s   
zPyCodegen.mark_source_tempc                    s    |d jjj}t  fdd |D ]}|jdur' |j q|D ]!}|jrFfdd 	|  t
dd q*	| q* t
t|d dS )	z2Call the generated code function stored in fn_nameTc                    s`   | v r |  d S |  t| tr | j t| tr,t| jtr. | j d S d S d S rB   )r   rk   ry   r   baser   r&   r   )r   collect_temp_sourceseen_sourcesr@   r-   r.   r  [  s   



z?PyCodegen.make_call_generated_code.<locals>.collect_temp_sourceNc                      s      t dgS )N_as_tensor_fullprec)r   r   r   rs   r-   rL   r-   r.   rh   w  s
    z4PyCodegen.make_call_generated_code.<locals>.<lambda>r   F)r   r   r9   r:   	graphargsr   r   pass_arg_as_tensorr	   rR   r   rV   )r@   r   r  r   r-   r  r.   make_call_generated_codeS  s"   




z"PyCodegen.make_call_generated_codec                 C   s(   t | j||}| | | | d S rB   )r   r9   import_sourcer   )r@   module_nameobject_namer   r-   r-   r.   rg     s   
zPyCodegen.load_import_fromc                 C   s   t jdkr&t||}|d jdksJ |d| | td|d|d< |S t jdkrUt||}t jdkr:d}d}nd}d	}|| j|ksGJ td
|d}||| |S | |td|dgS )NrS   CALLCALL_KWr   r   )rT      PRECALLKW_NAMESr   CALL_FUNCTION_KW)rW   rX   r   opnameinsertr   r   )r@   r   kw_namesr   r:   idxexpected_instkw_names_instr-   r-   r.   create_call_function_kw  s(   





z!PyCodegen.create_call_function_kwc                 C   s   t d|dS )NDELETE_FASTr   r   r   r-   r-   r.   create_delete  r   zPyCodegen.create_delete)NNNNN)F)T)r   )r2   N)>r(   r)   r*   __doc__r   r   nnModuler}   rA   rF   rM   rR   r	   r   r   rq   r   rC   r   r   r   rY   r   r   listr   r{   r   r   r   r   r   r   r   r   r   rs   r   r   r   r   r   r   r   r   r+   boolr   r   r   r   typesCodeTyper   r   r   r   r  rg   r  r  r-   r-   r-   r.   r/   ?   s    



 ]


&
2	r/   )8r  r4   dataclassesr   rW   r!  r   typingr   r   torch.nnr   torch.utils._ordered_setr   r   r   r   bytecode_transformationr	   r
   r   r   r   r   r   r   r   r   excr   r   r   r   r   r   r   r   r   variables.baser   r   variables.functionsr   r   variables.nn_moduler   variables.tensorr    r!   r"   r#   variables.torch_functionr$   	dataclassr%   r/   r-   r-   r-   r.   <module>   s.   0