U
    d                     @   sV   d dl Z d dlZd dlmZ d dlmZmZ d dlm	Z	 e 
eZG dd deZdS )    N)schema)get_categorical_limit
ModelLayer)Tagsc                       s.   e Zd Zd	 fdd	Zdd Zdd Z  ZS )
PositionWeightedNposition_weightsc              	      s   t t| ||| t|tjs(td|jj}|d k	r>|j	nd }|d k	rR|| _
n&t|| _
tdt| | j
 | jd| j
gdddif|d| _tdttj| j
f| d	f| _| jtjh d S )
NzIncorrect input typezZ{}: categorical_limit of lengths is not available, using categorical_limit of the keys: {}pos_wZConstantFillvalueg      ?)
param_nameshapeZinitializerZ	optimizerr   Zpos_w_gather)superr   __init__
isinstancer   ListAssertionErrorlengthsmetadataZcategorical_limitr   r   loggerwarningformatstrZcreate_paramr   StructZScalarnpZfloat32Zget_next_blob_referenceoutput_schematagsupdater   ZHANDLE_AS_SPARSE_LAYER)selfmodelinput_recordZweight_optimnameZlength_metadata
max_length	__class__ J/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layers/position_weighted.pyr      s<    


 
zPositionWeighted.__init__c                 C   s   | j S )N)r   )r   r#   r#   r$   get_memory_usage5   s    z!PositionWeighted.get_memory_usagec                 C   s<   | | j g| j d }|| j|g| jj  d S )NZ
_pos_w_seq)ZLengthsRangeFillr   r   ZGatherr   r   r   Zfield_blobs)r   netZinc_seqr#   r#   r$   add_ops8   s    

zPositionWeighted.add_ops)Nr   )__name__
__module____qualname__r   r%   r'   __classcell__r#   r#   r!   r$   r      s
     r   )loggingZnumpyr   Zcaffe2.pythonr   Zcaffe2.python.layers.layersr   r   Zcaffe2.python.layers.tagsr   	getLoggerr(   r   r   r#   r#   r#   r$   <module>   s   
