3
I4W^
  ã               @   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   ú-/tmp/pip-build-e66j97b2/torch/torch/nn/cpp.pyÚ__init__   s    zOrderedDictWrapper.__init__c             C   s   t | j| jƒS )N)Úgetattrr   r   )r   r   r   r   Úcpp_dict   s    zOrderedDictWrapper.cpp_dictc             C   s
   | j jƒ S )N)r   Úitems)r   r   r   r   r      s    zOrderedDictWrapper.itemsc             C   s
   | j jƒ S )N)r   Úkeys)r   r   r   r   r      s    zOrderedDictWrapper.keysc             C   s
   | j jƒ S )N)r   Úvalues)r   r   r   r   r   !   s    zOrderedDictWrapper.valuesc             C   s
   | j jƒ S )N)r   Ú__iter__)r   r   r   r   r   $   s    zOrderedDictWrapper.__iter__c             C   s
   | j jƒ S )N)r   Ú__len__)r   r   r   r   r   '   s    zOrderedDictWrapper.__len__c             C   s   | j j|ƒS )N)r   Ú__contains__)r   Úkeyr   r   r   r   *   s    zOrderedDictWrapper.__contains__c             C   s   | j j|ƒS )N)r   Ú__getitem__)r   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                sl   || _ tt| ƒjƒ  t|dƒ| _t|dƒ| _t|dƒ| _x.t|ƒD ]"}|j	dƒsBt
| |t| j |ƒƒ qBW d S )NÚ_parametersÚ_buffersÚ_modulesÚ_)r   Úsuperr   r	   r   r   r   r   ÚdirÚ
startswithÚsetattrr
   )r   r   r   )Ú	__class__r   r   r	   7   s    
zModuleWrapper.__init__c             C   sZ   x6| j ƒ D ]*}||jƒ|_|jd k	r
||jjƒ|j_q
W x| jƒ D ]}||jƒ|_qBW | S )N)Ú
parametersÚdataZ_gradÚbuffers)r   ÚfnÚparamÚbufr   r   r   Ú_applyD   s    
zModuleWrapper._applyc             C   s   | j jS )N)r   Útraining)r   r   r   r   r*   Q   s    zModuleWrapper.trainingc             C   s   | j j|ƒ d S )N)r   Ztrain)r   Úmoder   r   r   r*   U   s    c             C   s
   | j jƒ S )N)r   Ú__repr__)r   r   r   r   r,   Y   s    zModuleWrapper.__repr__)r   r   r   r   r	   r)   r   r*   Úsetterr,   Ú__classcell__r   r   )r"   r   r   1   s   r   N)r   Ztorchr   Úobjectr   ÚModuler   r   r   r   r   Ú<module>   s   +