a
    .=icÎ  ã                   @   s6   d Z ddlmZ G dd„ deƒZG dd„ dejƒZdS )z3Functionality for Python <-> C++ frontend inter-op.é    )Únnc                   @   s\   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚOrderedDictWrapperaª  
    A wrapper around a C++ OrderedDict that 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.
    c                 C   s   || _ || _d S ©N)Ú
cpp_moduleÚattr©Úselfr   r   © r	   ú]/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/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   s
   | j  ¡ S r   )r   Úitemsr   r	   r	   r
   r      s    zOrderedDictWrapper.itemsc                 C   s
   | j  ¡ S r   )r   Úkeysr   r	   r	   r
   r      s    zOrderedDictWrapper.keysc                 C   s
   | j  ¡ S r   )r   Úvaluesr   r	   r	   r
   r   !   s    zOrderedDictWrapper.valuesc                 C   s
   | j  ¡ S r   )r   Ú__iter__r   r	   r	   r
   r   $   s    zOrderedDictWrapper.__iter__c                 C   s
   | j  ¡ S r   )r   Ú__len__r   r	   r	   r
   r   '   s    zOrderedDictWrapper.__len__c                 C   s   | j  |¡S r   )r   Ú__contains__©r   Úkeyr	   r	   r
   r   *   s    zOrderedDictWrapper.__contains__c                 C   s   | j  |¡S r   )r   Ú__getitem__r   r	   r	   r
   r   -   s    zOrderedDictWrapper.__getitem__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r   r   r   r   r	   r	   r	   r
   r      s   	
r   c                       sJ   e Zd ZdZ‡ fdd„Zdd„ Zedd„ ƒZejdd„ ƒZd	d
„ Z	‡  Z
S )ÚModuleWrapperzj
    A subclass of ``torch.nn.Module`` that wraps a C++ frontend module and
    delegates all access.
    c                    sh   || _ t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   r    ÚdirÚ
startswithÚsetattrr   r   ©Ú	__class__r	   r
   r   7   s    
zModuleWrapper.__init__c                 C   sR   |   ¡ D ]*}||jƒ|_|jd ur||jjƒ|j_q|  ¡ D ]}||jƒ|_q<| S r   )Ú
parametersÚdataZ_gradÚbuffers)r   ÚfnÚparamÚbufr	   r	   r
   Ú_applyD   s    
zModuleWrapper._applyc                 C   s   | j jS r   )r   Útrainingr   r	   r	   r
   r/   R   s    zModuleWrapper.trainingc                 C   s   | j  |¡ d S r   )r   Útrain)r   Úmoder	   r	   r
   r/   V   s    c                 C   s
   | j  ¡ S r   )r   Ú__repr__r   r	   r	   r
   r2   Z   s    zModuleWrapper.__repr__)r   r   r   r   r   r.   r   r/   Úsetterr2   Ú__classcell__r	   r	   r&   r
   r   1   s   

r   N)r   Ztorchr   Úobjectr   ÚModuler   r	   r	   r	   r
   Ú<module>   s   +