
    ^j                      0   d dl Z d dlmZmZ d dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ d	d
gZ ed       G d d	e             Z	 ddej*                  dedefdZ ed       G d d
ej0                  j2                               Zy)    N)Any
NamedTuple)enable_python_dispatcher)detect_fake_mode)(is_contiguous_for_memory_format_or_false)is_sparse_any)compatibility)map_aggregateNodeTensorMetadata	ShapePropT)is_backward_compatiblec                       e Zd ZU dZej
                  ed<   ej                  ed<   eed<   e	e
df   ed<   ej                  dz  ed<   eed	<   eeef   ed
<   y)r   zUA structure containing pertinent information about a tensor within a PyTorch program.shapedtyperequires_grad.strideNmemory_formatis_quantizedqparams)__name__
__module____qualname____doc__torchSize__annotations__r   booltupleintr   dictstrr        e/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/shape_prop.pyr   r      sV    _ ::;;#s(O&&-- #s(^r$   resultinclude_contiguityreturnc           	      :   | j                   }| j                  }| j                  }t        |       s| j	                         nd}d}|rRt        |       sGt
        j                  t
        j                  t
        j                  f}|D ]  }t        | |      s|} n | j                  }	i }
|	r| j                         }||
d<   |t
        j                  t
        j                  fv r'| j                         |
d<   | j                         |
d<   n|t
        j                   t
        j"                  t
        j$                  fv rU| j'                         j)                         |
d<   | j+                         j)                         |
d<   | j-                         |
d<   t/        ||||||	|
      S )zB
    Extract a TensorMetadata NamedTuple describing `result`.
    r#   N)r   qschemescale
zero_pointaxis)r   r   r   r   r   r   contiguous_formatchannels_lastchannels_last_3dr   r   r*   per_tensor_affineper_tensor_symmetricq_scaleq_zero_pointper_channel_affine per_channel_affine_float_qparamsper_channel_symmetricq_per_channel_scalestolistq_per_channel_zero_pointsq_per_channel_axisr   )r&   r'   r   r   r   r   r   memory_formatsquery_formatr   r   r*   s               r%   _extract_tensor_metadatar>   %   s    LLELLE((M$1&$9V]]_rFM-"7##""

 + 	L7l !-	 &&L G.."$	u..0J0JKK%~~/GG$*$7$7$9GL!$$22''
 
  &::<CCEGG$*$D$D$F$M$M$OGL!$779GFOumV]L' r$   c                        e Zd ZdZddej
                  j                  deddf fdZde	def fdZ
d	edef fd
Z xZS )r   aE  
    Execute an FX graph Node-by-Node and
    record the shape and type of the result
    into the corresponding node.

    Example:
         In this example, we record the shape
         and data type of a module given
         an example input ``torch.randn(50, D_in)``.
         We print the name, shape and dtype of each node.

        class TwoLayerNet(torch.nn.Module):
            def __init__(self, D_in, H, D_out):
                super().__init__()
                self.linear1 = torch.nn.Linear(D_in, H)
                self.linear2 = torch.nn.Linear(H, D_out)
            def forward(self, x):
                h_relu = self.linear1(x).clamp(min=0)
                y_pred = self.linear2(h_relu)
                return y_pred
        N, D_in, H, D_out = 64, 1000, 100, 10
        x = torch.randn(N, D_in)
        y = torch.randn(N, D_out)
        model = TwoLayerNet(D_in, H, D_out)
        gm = torch.fx.symbolic_trace(model)
        sample_input = torch.randn(50, D_in)
        ShapeProp(gm).propagate(sample_input)

        for node in gm.graph.nodes:
            print(node.name, node.meta['tensor_meta'].dtype,
                node.meta['tensor_meta'].shape)

        The output of this code is:

        x torch.float32 torch.Size([50, 1000])
        linear1 torch.float32 torch.Size([50, 100])
        clamp_1 torch.float32 torch.Size([50, 100])
        linear2 torch.float32 torch.Size([50, 10])
        output torch.float32 torch.Size([50, 10])

    Args:
         module (GraphModule): The module to be executed
         fake_mode (FakeTensorMode): A fake mode for copying the gm

    Ngm	fake_moder(   c                     t         |   |       |
t               }|&ddlm}  || j
                  |      | _        || _        nd | _        d | _        | j
                  | _        y )Nr   )deepcopy_to_fake_tensor)	super__init__r   torch._dynamo.utilsrC   modulefake_modulerA   real_module)selfr@   rA   rC   	__class__s       r%   rE   zShapeProp.__init__   s^    (*I C  7t{{IND&DN#D!DN;;r$   nc                 f  
 ddl m}m} 	 | j                  | j                  | _        	 | j
                  V| j
                  5  t               5  t        | !  |      } || j
                  j                  ||       d d d        d d d        nt        | !  |      }| j                  | _        	 d
dt"        dt"        f
fd}t%        |      }
r||j                   d	<   | j
                  r2| j
                  j                  x}r |||      x}	r|	|j                   d
<   t'        |      |j                   d<   |S # 1 sw Y   xY w# 1 sw Y   xY w# | j                  | _        w xY w# t        $ rC}t        j                          t        d|j                          d|j                          |d }~ww xY w)Nr   )compute_unbacked_bindingsrebind_unbackedzShapeProp error for: node=z with meta=Fobjr(   c                 V    t        | t        j                        rdt        |       S | S )NT)
isinstancer   Tensorr>   )rP   found_tensors    r%   extract_tensor_metaz/ShapeProp.run_node.<locals>.extract_tensor_meta   s&    #u||,#/44
r$   tensor_metaunbacked_bindingstype)%torch.fx.experimental.symbolic_shapesrN   rO   rH   rG   rA   r   rD   run_node	shape_envrI   	Exception	traceback	print_excRuntimeErrorformat_nodemetar   r
   rX   )rJ   rL   rN   rO   r&   erU   ra   r[   symbol_to_pathrT   rK   s             @r%   rZ   zShapeProp.run_node   s   	

	+ #../>>- M)A)C M!&!1!!4'(@(@!VLM M M #W-a0F".. 	S 	S 	 V%89$(AFF=!>>!^^555	5";Iv"NNN.<*+fvEM M M M #.. 	!,Q]]_,=[Q	s^   E$ E E.D6;EE E$ 6D?	;EEE E!!E$ $	F0->F++F0argsc                     | j                   E|D cg c]9  }t        |t        j                        r| j                   j	                  |      n|; }}n|}t        |   | S c c}w )a  
        Run `module` via interpretation and return the result and
        record the shape and type of each node.

        Args:
            *args (Tensor): the sample input.

        Returns:
            Any: The value returned from executing the Module
        )rA   rR   r   rS   from_tensorrD   run)rJ   rd   t	fake_argsrK   s       r%   	propagatezShapeProp.propagate   sk     >>%  2<Au||1L**1-RSSI 
 Iw{I&&s   >A!)N)r   r   r   r   r   fxGraphModuler   rE   r   rZ   rj   __classcell__)rK   s   @r%   r   r   X   sY    ,\'588// 'C '4 '0/$ /3 /b's 's ' 'r$   )T)r]   typingr   r   r   torch.fxtorch._dispatch.pythonr   torch._guardsr   torch._prims_commonr   torch._subclasses.meta_utilsr   torch.fx._compatibilityr	   torch.fx.noder
   r   __all__r   rS   r   r>   rk   Interpreterr   r#   r$   r%   <module>rx      s     "   ; * H 6 1 - [
) d+Z  ,( 6:0LL0.200f d+J'$$ J' ,J'r$   