
    Vh                     (   d dl mZ d dlZd dlmZmZmZmZ dgZ	 ej                  j                  j                  j                  ej                  j                  j                  j                  hZ G d deej                   j"                  ef         Zy)    )CallableN)_collect_all_valid_cia_ops-_collect_all_valid_cia_ops_for_aten_namespace_get_decomp_for_cia_is_aten_opCustomDecompTablec                        e Zd ZdZ fdZd ZddZd ZddZd Z	de
fd	Zde
fd
ZdefdZd Zd ZddZd Zd Zdeej,                  j.                  ef   fdZddZ xZS )r   a_  
    This is a custom dictionary that is specifically used for handling decomp_table in export.
    The reason we need this is because in the new world, you can only *delete* an op from decomp
    table to preserve it. This is problematic for custom ops because we don't know when the custom
    op will actually be loaded to the dispatcher. As a result, we need to record the custom ops operations
    until we really need to materialize it (which is when we run decomposition pass.)

    Invariants we hold are:
     1. All aten decomp is loaded at the init time
     2. We materialize ALL ops when user ever reads from the table to make it more likely
        that dispatcher picks up the custom op.
     3. If it is write operation, we don't necessarily materialize
     4. We load the final time during export, right before calling run_decompositions()

    c                     t         |           ddlm}  |       | _        t               D ]#  }|t        vst        |      | j                  |<   % t               | _	        d| _
        y )Nr   )'_core_aten_decompositions_post_autogradF)super__init__torch._decompr   decomp_tabler   PRESERVED_ATEN_CIA_OPSr   setdeleted_custom_opshas_materialized)selfr   op	__class__s      I/home/dcms/DCMS/lib/python3.12/site-packages/torch/export/decomp_utils.pyr   zCustomDecompTable.__init__,   se    I DE?A 	@B//(;B(?!!"%	@
 #&% %    c                 X    | j                          | j                  j                  |      S N)_materialize_if_neededr   __getitem__r   keys     r   r   zCustomDecompTable.__getitem__<   s%    ##%  ,,S11r   returnc                     | j                   j                  ||       || j                  v r| j                  j                  |       y y r   )r   __setitem__r   remove)r   r   values      r   r!   zCustomDecompTable.__setitem__@   s?    %%c51$)))##**3/ *r   c                 V    | j                          | j                  j                         S r   )r   r   keysr   s    r   r%   zCustomDecompTable.keysF   s#    ##%  %%''r   c                 &    | j                  |       y r   )popr   s     r   __delitem__zCustomDecompTable.__delitem__J   s    r   c                 l    |j                         D ]!  \  }}| j                  j                  ||       # y r   )itemsr   r!   )r   
other_dictkvs       r   updatezCustomDecompTable.updateM   s4    $$& 	0DAq))!Q/	0r   c                 &    | j                  |       S r   )__contains__r   s     r   __missing__zCustomDecompTable.__missing__Q   s    $$S)))r   c                 X    | j                          | j                  j                  |      S r   )r   r   r1   r   s     r   r1   zCustomDecompTable.__contains__T   s%    ##%  --c22r   c                 V    | j                          | j                  j                         S r   )r   r   __len__r&   s    r   r5   zCustomDecompTable.__len__X   s#    ##%  ((**r   c                 V    | j                          | j                  j                         S r   )r   r   __iter__r&   s    r   r7   zCustomDecompTable.__iter__\   s#    ##%  ))++r   c                 V    | j                          | j                  j                         S r   )r   r   __reversed__r&   s    r   r9   zCustomDecompTable.__reversed__`   s#    ##%  --//r   c                     t               }| j                  j                         |_        | j                  j                         |_        | j                  |_        |S r   )r   r   copyr   r   )r   new_dicts     r   r;   zCustomDecompTable.copyd   sK    $& $ 1 1 6 6 8&*&=&=&B&B&D#$($9$9!r   c                       fd}t        |      dk(  r ||d         S t        |      dk(  r	  ||d         S y # t        $ r |d   cY S w xY w)Nc                 \   t        |       rj                  j                  |       S | j                  v r6j                  j	                  |        j                  j                  |       S | j                  v rt        |  d      j                  j	                  |        t        |       S )Nz doesn't exist in the table)r   r   r(   r   addKeyErrorr   )r   r   s    r   _pop_if_canz*CustomDecompTable.pop.<locals>._pop_if_canl   s    3((,,S11d''' ''++C0((,,S11d---#&ABCC##'', 's++r      r      )lenr@   )r   argsrA   s   `  r   r(   zCustomDecompTable.popk   sb    	,( t9>tAw''t9>"47++   Aws   
; AAc                 V    | j                          | j                  j                         S r   )r   r   r+   r&   s    r   r+   zCustomDecompTable.items   s#    ##%  &&((r   c                     t               D ]D  }t        |      r|| j                  v r|| j                  vs-t	        |      | j                  |<   F d| _        t               | _        i | j                  S )NT)r   r   r   r   r   r   r   )r   r   s     r   materializezCustomDecompTable.materialize   sy    ,. 	@B2t(((4222(;B(?!!"%	@ !%"%%$$##$$r   c                 >    | j                   s| j                          y y r   )r   rH   r&   s    r   r   z(CustomDecompTable._materialize_if_needed   s    $$ %r   )r   N)r   r   )__name__
__module____qualname____doc__r   r   r!   r%   r)   r/   boolr2   r1   intr5   r7   r9   r;   r(   r+   dicttorch_opsOperatorBaser   rH   r   __classcell__)r   s   @r   r   r      s     & 20(0*$ *34 3+ +,0<)%T%**"9"98"CD %r   )typingr   rQ   torch._export.utilsr   r   r   r   __all__opsatenupsample_bilinear2dvecupsample_nearest2dr   rP   rR   rS   r    r   r   <module>r^      s~       
 
IINN&&**	IINN%%)) AUZZ44h>? Ar   