
    Vh                        d dl mZ d dlmZ d dlZd dlmZ erd dlZ G d dej                        Z	 G d dej                        Z
y)	    )annotations)TYPE_CHECKINGN)_passc                      e Zd ZdZddZy)MovePlaceholderToFrontzThis pass move all placeholder nodes to the front of the graph node list.

    In torch.fx.Graph, placeholder is a special assignment node. If it's not
    executed in the beginning, it could overwrite values computed by upstream
    nodes.
    c                   | j                   }|j                  }g }d }|j                  D ]7  }|j                  dk(  r|j	                  |       |&|j                  dk7  s6|}9 ||S |D ]  }|j                  |        |S )Nplaceholder)modulegraphnodesopappendprepend)	selfargskwargsgraph_moduler   placeholdersfirst_not_placeholdernoder	   s	            ]/home/dcms/DCMS/lib/python3.12/site-packages/torch/onnx/_internal/fx/passes/virtualization.py_runzMovePlaceholderToFront._run   s    {{"" $KK 	-Dww-'##D)$,M1I(,%		-
 !(' 	7K!))+6	7    Nreturnztorch.fx.GraphModule)__name__
__module____qualname____doc__r    r   r   r   r      s    r   r   c                  6    e Zd ZU dZded<   edd       ZddZy)	ReplaceGetAttrWithPlaceholderzReplace get_attr with placeholder.

    The parameters and buffers accessed by the original get_attr are returned;
    they are useful when creating random inputs for the modified graph_module.
    ztuple[torch.Tensor, ...] | None_replaced_attrsc                @    | j                   J d       | j                   S )z$The list of replaced weight tensors.z,Must run ReplaceGetAttrWithPlaceholder first)r#   )r   s    r   replaced_attrsz,ReplaceGetAttrWithPlaceholder.replaced_attrs0   s.     ##/ 	
:	
/ ###r   c                   | j                   }|j                  }g }|j                  D ]p  }|j                  dk(  sd }	 |j	                  |j
                        }d|_        |j
                  j                  dd      |_        d|_	        |j                  |       r t        |      | _        |S # t        $ r |j                  |j
                        }Y zw xY w)Nget_attrr	   ._)N)r
   r   r   r   get_parametertargetAttributeError
get_bufferreplacer   r   tupler#   )r   r   r   r   r   r%   r   replaced_attrs           r   r   z"ReplaceGetAttrWithPlaceholder._run8   s    {{""-/KK  	5Dww*$59I$0$>$>t{{$KM ( #kk11#s; $	%%m4A 	5D  %^4; & I %1$;$;DKK$HMIs   B,,$CCN)r   ztuple[torch.Tensor, ...]r   )r   r   r   r   __annotations__propertyr%   r   r    r   r   r"   r"   '   s&     54$ $(r   r"   )
__future__r   typingr   torchtorch.onnx._internal.fxr   torch.fx	Transformr   r"   r    r   r   <module>r9      s9    "    ) U__ 29EOO 9r   