o
    ‡ZŽhŽ  ã                   @   sl   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„ deejjef ƒZdS )é    )ÚCallableN)Ú_collect_all_valid_cia_opsÚ-_collect_all_valid_cia_ops_for_aten_namespaceÚ_get_decomp_for_ciaÚ_is_aten_opÚCustomDecompTablec                       sÄ   e Zd ZdZ‡ fdd„Zdd„ Zd$dd	„Zd
d„ Zd$dd„Zdd„ Z	de
fdd„Zde
fdd„Zdefdd„Zdd„ Zdd„ Zd%dd„Zdd„ Zdd„ Zdeejjef fd d!„Zd$d"d#„Z‡  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                    sR   t ƒ  ¡  ddlm} |ƒ | _tƒ D ]}|tvrt|ƒ| j|< qtƒ | _	d| _
d S )Nr   )Ú'_core_aten_decompositions_post_autogradF)ÚsuperÚ__init__Ztorch._decompr   Údecomp_tabler   ÚPRESERVED_ATEN_CIA_OPSr   ÚsetÚdeleted_custom_opsÚhas_materialized)Úselfr   Úop©Ú	__class__© úH/var/www/auris/lib/python3.10/site-packages/torch/export/decomp_utils.pyr
   ,   s   

€
zCustomDecompTable.__init__c                 C   ó   |   ¡  | j |¡S ©N)Ú_materialize_if_neededr   Ú__getitem__©r   Úkeyr   r   r   r   <   ó   zCustomDecompTable.__getitem__ÚreturnNc                 C   s,   | j  ||¡ || jv r| j |¡ d S d S r   )r   Ú__setitem__r   Úremove)r   r   Úvaluer   r   r   r   @   s   
ÿzCustomDecompTable.__setitem__c                 C   ó   |   ¡  | j ¡ S r   )r   r   Úkeys©r   r   r   r   r"   F   ó   
zCustomDecompTable.keysc                 C   s   |   |¡ d S r   )Úpopr   r   r   r   Ú__delitem__J   s   zCustomDecompTable.__delitem__c                 C   s$   |  ¡ D ]\}}| j ||¡ qd S r   )Úitemsr   r   )r   Ú
other_dictÚkÚvr   r   r   ÚupdateM   s   ÿzCustomDecompTable.updatec                 C   s   |   |¡ S r   )Ú__contains__r   r   r   r   Ú__missing__Q   s   zCustomDecompTable.__missing__c                 C   r   r   )r   r   r,   r   r   r   r   r,   T   r   zCustomDecompTable.__contains__c                 C   r!   r   )r   r   Ú__len__r#   r   r   r   r.   X   r$   zCustomDecompTable.__len__c                 C   r!   r   )r   r   Ú__iter__r#   r   r   r   r/   \   r$   zCustomDecompTable.__iter__c                 C   r!   r   )r   r   Ú__reversed__r#   r   r   r   r0   `   r$   zCustomDecompTable.__reversed__c                 C   s*   t ƒ }| j ¡ |_| j ¡ |_| j|_|S r   )r   r   Úcopyr   r   )r   Znew_dictr   r   r   r1   d   s
   zCustomDecompTable.copyc                    s^   ‡ fdd„}t |ƒdkr||d ƒS t |ƒdkr-z||d ƒW S  ty,   |d  Y S w d S )Nc                    sb   t | ƒr
ˆ j | ¡S | ˆ jv rˆ j | ¡ ˆ j | ¡S | ˆ jv r't| › dƒ‚ˆ j | ¡ t| ƒS )Nz doesn't exist in the table)r   r   r%   r   ÚaddÚKeyErrorr   )r   r#   r   r   Ú_pop_if_canl   s   

z*CustomDecompTable.pop.<locals>._pop_if_cané   r   é   )Úlenr3   )r   Úargsr4   r   r#   r   r%   k   s   ÿýzCustomDecompTable.popc                 C   r!   r   )r   r   r'   r#   r   r   r   r'   ‰   r$   zCustomDecompTable.itemsc                 C   sR   t ƒ D ]}t|ƒr
q|| jv rq|| jvrt|ƒ| j|< qd| _tƒ | _i | j¥S )NT)r   r   r   r   r   r   r   )r   r   r   r   r   Úmaterialize   s   


€
zCustomDecompTable.materializec                 C   s   | j s	|  ¡  d S d S r   )r   r9   r#   r   r   r   r   š   s   ÿz(CustomDecompTable._materialize_if_needed)r   N)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r"   r&   r+   Úboolr-   r,   Úintr.   r/   r0   r1   r%   r'   ÚdictÚtorchÚ_opsÚOperatorBaser   r9   r   Ú__classcell__r   r   r   r   r      s$    


)Útypingr   rA   Ztorch._export.utilsr   r   r   r   Ú__all__ÚopsZatenZupsample_bilinear2dZvecZupsample_nearest2dr   r@   rB   rC   r   r   r   r   r   Ú<module>   s   

þ 