
    Vh                         d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ dgZ e	d	
       G d dej                   j"                               Zy)    )OptionalN)
FakeTensorFakeTensorMode)Node)compatibility)py_sym_typessnapshot_fake)map_aggregateFakeTensorPropF)is_backward_compatiblec                   ~     e Zd ZdZ	 d	dej
                  j                  dee   f fdZ	de
f fdZd Z fdZ xZS )
r   aS  
    Execute an FX graph Node-by-Node and record a fake tensor representing
    the metadata for the node.  Unlike ShapeProp, (1) this propagation
    is cheap--it does the propagation with meta tensors which do not actually
    store data, and (2) the fake tensors have much more fine grained information,
    e.g., they have accurate alias information that can be consulted by looking
    at the storages.

    Args:
         module (GraphModule): The module to be executed
         mode (Optional[FakeTensorMode]): The dispatch mode used to execute computation indicated by each FX Node.
    modulemodec                     t         |   |       |
t               }|| _        |xj                  dz  c_        |j                          y )N   )super__init__r   _modeepochreset_nt_tensor_id_counter)selfr   r   	__class__s      P/home/dcms/DCMS/lib/python3.12/site-packages/torch/fx/passes/fake_tensor_prop.pyr   zFakeTensorProp.__init__   s?     	 <!#D


a
'')    nc                     ddl m}m} t        	   |      } | j
                  j                  ||        fd}t        ||      }|A||j                  d<    j
                  j                  x}r |||      x}r||j                  d<   |S )Nr   )compute_unbacked_bindingsrebind_unbackedc                     t        | t              rt        |       S t        | t        j                        r&t        j
                  j                  | d            S t        | t              r| S y )NT)static_shapes)
isinstancer   r	   torchTensorr   from_tensorr   )objr   s    r   extract_valz,FakeTensorProp.run_node.<locals>.extract_val1   sY    #z*$S))C. %TZZ%;%;Ct%;%TUUC.
r   valunbacked_bindings)	%torch.fx.experimental.symbolic_shapesr   r   r   run_noder   	shape_envr
   meta)
r   r   r   r   resultr&   r,   r+   symbol_to_pathr   s
   `        r   r*   zFakeTensorProp.run_node(   s    	

 !!$

,,a8
	 V[1 AFF5M!ZZ111	1";Iv"NNN.<*+r   c                     |D cg c]9  }t        |t        j                        r| j                  j	                  |      n|; }} | j
                  | S c c}w N)r!   r"   r#   r   r$   propagate_dont_convert_inputs)r   argsa	fake_argss       r   	propagatezFakeTensorProp.propagateG   s\     
 *4Au||)DDJJ""1%!K
	 
 2t119==	
s   >Ac                 `    | j                   5  t        |   | cd d d        S # 1 sw Y   y xY wr0   )r   r   run)r   r2   r   s     r   r1   z,FakeTensorProp.propagate_dont_convert_inputsN   s+    ZZ 	&7;%	& 	& 	&s   $-r0   )__name__
__module____qualname____doc__r"   fxGraphModuler   r   r   r   r*   r5   r1   __classcell__)r   s   @r   r   r      sN     NR*hh***2:>2J*$ >>& &r   )typingr   torch.fxr"   torch._subclasses.fake_tensorr   r   r   torch.fx._compatibilityr   "torch.fx.experimental.proxy_tensorr   r	   torch.fx.noder
   __all__r<   Interpreterr    r   r   <module>rH      sQ      D  1 J ' 
 e,@&UXX)) @& -@&r   