a
    ==ic                     @   sl   d dl mZmZmZ d dlmZ G dd deZG dd deZG dd deZ	G d	d
 d
eZ
dd ZdS )    )DataTypeBlobReferenceScopedBlobReference)ParameterInfoc                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
Initializerz
    This class abstracts out parameter creation. One can come up with a new
    Initializer in order to implement more complex parameter initialization logic
    Nc                 K   s   || _ || _d S N)operator_nameoperator_kwargsselfr   kwargs r   t/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/caffe2/python/modeling/initializers.py__init__   s    zInitializer.__init__c                 C   s"   | j d urtd|| _ || _d S Nz(Operator name overwrites are not allowedr   	Exceptionr	   r
   r   r   r   update   s    
zInitializer.updatec                 C   s.   | | jg |fd|i| j}td ||dS )Nshapeparam_idparamr   )__getattr__r   r	   r   r   
param_nameinit_netr   r   r   r   r   create_param   s    
zInitializer.create_param)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   
   s   
r   c                   @   s   e Zd ZdZdd ZdS )ExternalInitializerz
    This class is used in cases when the parameter should not be initialized by
    the initializer, but rather provided in the workspace when param_init_net is
    executed.

    Current version is not doing any real sanity checks to the parameter.
    c                 C   sF   t |trtt||}nt |tr0t||}ntdtd ||dS )NzUnsupported type for param_namer   )
isinstancer   strr   	TypeErrorr   r   r   r   r   r   -   s    

z ExternalInitializer.create_paramN)r   r   r   r    r   r   r   r   r   r!   $   s   r!   c                   @   s    e Zd ZdZdd Zdd ZdS )PseudoFP16Initializera  
    Used in cases when the parameter should be used at half (16-bit) precision
    for compute purposes (i.e. on the forward and backward pass) but
    needs to be stored and optimized at single (32-bit) precision so tiny
    gradients with small learning rates don't underflow FP16 precision.
    A 32-bit copy of the 16-bit blob is stored in the ParameterInfo.
    This is helpful for mixed-precision training, see
    https://arxiv.org/abs/1710.03740 for details.
    c                 C   s"   | j d urtd|| _ || _d S r   r   r
   r   r   r   r   F   s    
zPseudoFP16Initializer.updatec                 C   sF   | | jg |d fd|i| j}|||}td ||tj|idS )NZ_fp32r   r   r   r   Z	blob_copy)r   r   r	   FloatToHalfr   r   FLOAT)r   r   r   r   
param_fp32r   r   r   r   r   L   s     
z"PseudoFP16Initializer.create_paramNr   r   r   r    r   r   r   r   r   r   r%   <   s   	r%   c                   @   s    e Zd ZdZdd Zdd ZdS )ReversePseudoFP16Initializerz
    Like PseudoFP16Initializer above, except the primary blob is taken to
    be the 32-bit precision parameter, and the 16-bit version of the blob
    is stored in blob_copy instead.
    c                 C   s"   | j d urtd|| _ || _d S r   r   r
   r   r   r   r   c   s    
z#ReversePseudoFP16Initializer.updatec                 C   sF   | | jg |fd|i| j}|||d }td ||tj|idS )Nr   Z_fp16r&   )r   r   r	   r'   r   r   ZFLOAT16)r   r   r   r   r)   Z
param_fp16r   r   r   r   i   s     
z)ReversePseudoFP16Initializer.create_paramNr*   r   r   r   r   r+   ]   s   r+   c                    sT    fdd}| dur4| | d fi | d }nt | d fi | d }|S )aV  
    A helper function to convert from operator_name_and_kwargs to new
    object of type initializer_class. This function serves two purposes:

    1. Support for custom initialization operators being passed in
    2. Allow user to specify a custom Initializer without overwriting
       default operators used for initialization

    If initializer_class is None, creates a default initializer using
    the Initializer class and operator_name_and_kwargs provided

    If operator_name_and_kwargs is None, uses default_operator_name_and_kwargs

    returns an instantiated Initializer object
    c                      s   p S r   r   r    default_operator_name_and_kwargsoperator_name_and_kwargsr   r   get_initializer_args   s    z0update_initializer.<locals>.get_initializer_argsNr      )r   )Zinitializer_classr.   r-   r/   initr   r,   r   update_initializery   s    r2   N)Zcaffe2.python.corer   r   r   Z%caffe2.python.modeling.parameter_infor   objectr   r!   r%   r+   r2   r   r   r   r   <module>   s   !