
    Ph|                        d dl mZ d dlmZmZmZ d dlZd dlZd dlm	Z	 d dl
mZ  G d dej                        Z G d d	ej                        Zy)
    )annotations)ListOptionalTupleN)	_beartype)_passc                  6    e Zd ZdZej
                  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	            xC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/onnx/_internal/fx/passes/virtualization.py_runzMovePlaceholderToFront._run   s    {{"" $KKDww-'##D)$,M1I(,%	  
 !('K!))+6 (    Nreturnztorch.fx.GraphModule)__name__
__module____qualname____doc__r   beartyper    r   r   r
   r
      s!      r   r
   c                  T    e Zd ZU dZded<   edd       Zej                  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"Optional[Tuple[torch.Tensor, ...]]_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_attrs/   s,       ,	:9	:,###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._run7   s    {{""-/KKDww*$8<I$0$>$>t{{$KM ( #kk11#s; $	%%m4A  D  %^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   r&   r&   &   s;     87$ $ ( (r   r&   )
__future__r   typingr   r   r   torchtorch.fxtorch.onnx._internalr   torch.onnx._internal.fxr   	Transformr
   r&   r$   r   r   <module>r>      s:    " ( (   * )U__ 4:EOO :r   