U
    d
                     @   s8   d dl Zd dlmZmZ d dlmZ G dd deZdS )    N)coreschema)
ModelLayerc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )UniformSamplingz
    Uniform sampling `num_samples - len(input_record)` unique elements from the
    range [0, num_elements). `samples` is the concatenation of input_record and
    the samples. input_record is expected to be unique.
    uniform_samplingc           	   	      s   t t| j|||f| ||  kr,dks2n tt|tjsBt|| _dd|gif}| jdd||j	d| _
dt|| tjjdf}| jd	|f||j	d| _td
ttj| d
fdttj| jf| _d S )Nr   ZGivenTensorInt64FillvaluesZnum_examples)   )
param_nameshapeZinitializerZ	optimizerZConstantFill)valueZdtypeZprobsamplessampling_prob)superr   __init__AssertionError
isinstancer   ZScalarnum_elementsZcreate_paramZNoOptimnum_samplesfloatr   ZDataTypeFLOATr   StructnpZint32Zget_next_blob_referenceZfloat32output_schema)	selfmodelinput_recordr   r   namekwargsZnum_examples_initZsampling_blob_init	__class__ I/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layers/uniform_sampling.pyr      sJ    	
  
  zUniformSampling.__init__c                 C   s   | | j| j ||  g|d}|| j|g|}|j||  g|dd| jd dd}|j	|  |g| j
 |dgdd | | j
 | j
  d S )	Nr
   Zsamples_before_concatr   r   T)minmaxZinput_as_shapeZ
split_info)Zaxis)ZStopGradientr   ZShaper   ZNextScopedBlobZSubr   ZUniqueUniformFillr   ZConcatr   r   )r   netr
   r   r    r    r!   add_ops?   s&    

 zUniformSampling.add_ops)r   )__name__
__module____qualname____doc__r   r%   __classcell__r    r    r   r!   r      s    *r   )Znumpyr   Zcaffe2.pythonr   r   Zcaffe2.python.layers.layersr   r   r    r    r    r!   <module>   s   