o
     …›i  ã                   @   s4   d Z ddlmZ G dd„ dƒZG dd„ dejƒZdS )z3Functionality for Python <-> C++ frontend inter-op.é    )Únnc                   @   sj   e Zd ZdZddd„Zedd„ ƒZdd	„ Zd
d„ Zdd„ Z	dd„ Z
defdd„Zdefdd„Zdd„ ZdS )ÚOrderedDictWrappera¥  A wrapper around a C++ OrderedDict.

    It dynamically evaluates the OrderedDict getter on a bound C++ module, such
    that new changes on the C++ side are picked up. Otherwise accessing e.g.
    ``cpp_module._parameters`` just once would get a frozen copy of the parameters
    at the time of access. ``torch.nn.Module`` accesses ``_parameters`` et al. via ``self.__dict__``
    so using properties does not work.
    ÚreturnNc                 C   s   || _ || _d S ©N)Ú
cpp_moduleÚattr©Úselfr   r   © r
   úN/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/nn/cpp.pyÚ__init__   s   
zOrderedDictWrapper.__init__c                 C   s   t | j| jƒS r   )Úgetattrr   r   ©r	   r
   r
   r   Úcpp_dict   s   zOrderedDictWrapper.cpp_dictc                 C   ó
   | j  ¡ S r   )r   Úitemsr   r
   r
   r   r      ó   
zOrderedDictWrapper.itemsc                 C   r   r   )r   Úkeysr   r
   r
   r   r      r   zOrderedDictWrapper.keysc                 C   r   r   )r   Úvaluesr   r
   r
   r   r   "   r   zOrderedDictWrapper.valuesc                 C   r   r   )r   Ú__iter__r   r
   r
   r   r   %   r   zOrderedDictWrapper.__iter__c                 C   r   r   )r   Ú__len__r   r
   r
   r   r   (   r   zOrderedDictWrapper.__len__c                 C   ó   | j  |¡S r   )r   Ú__contains__©r	   Úkeyr
   r
   r   r   +   ó   zOrderedDictWrapper.__contains__c                 C   r   r   )r   Ú__getitem__r   r
   r
   r   r   .   r   zOrderedDictWrapper.__getitem__©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r   Úintr   Úboolr   r   r
   r
   r
   r   r      s    
	
r   c                       sV   e Zd ZdZd‡ fdd„Zddd„Zed	d
„ ƒZejddd
„ƒZde	fdd„Z
‡  ZS )ÚModuleWrapperz\A subclass of ``torch.nn.Module`` that wraps a C++ frontend module and delegates all access.r   Nc                    sd   || _ tƒ  ¡  t|dƒ| _t|dƒ| _t|dƒ| _t|ƒD ]}| d¡s/t	| |t
| j |ƒƒ qd S )NÚ_parametersÚ_buffersÚ_modulesÚ_)r   Úsuperr   r   r&   r'   r(   ÚdirÚ
startswithÚsetattrr   r   ©Ú	__class__r
   r   r   5   s   

€ýzModuleWrapper.__init__Tc                 C   sR   |   ¡ D ]}||jƒ|_|jd ur||jjƒ|j_q|  ¡ D ]}||jƒ|_q| S r   )Ú
parametersÚdataÚ_gradÚbuffers)r	   ÚfnÚrecurseÚparamÚbufr
   r
   r   Ú_applyB   s   
€zModuleWrapper._applyc                 C   s   | j jS r   )r   Útrainingr   r
   r
   r   r9   P   s   zModuleWrapper.trainingc                 C   s   | j  |¡ d S r   )r   Útrain)r	   Úmoder
   r
   r   r9   U   s   c                 C   r   r   )r   Ú__repr__r   r
   r
   r   r<   Y   r   zModuleWrapper.__repr__r   )T)r   r   r    r!   r   r8   r"   r9   ÚsetterÚstrr<   Ú__classcell__r
   r
   r.   r   r%   2   s    

r%   N)r!   Útorchr   r   ÚModuler%   r
   r
   r
   r   Ú<module>   s   +