a
    –¥»aÝ  ã                   @   s,   d dl mZ d dlmZ G dd„ deƒZdS )é    )Úschema)Ú
ModelLayerc                       s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )ÚPairwiseSimilarityÚdotÚpairwise_similarityc                    s2  t t| ƒj|||fi |¤Ž t|tjƒs6J d |¡ƒ‚d|v d|v oJd|v A sVJ dƒ‚|| _d|v rv|d }|d }n|d }|d }t|tjƒs¤J dd |¡ ƒ‚t|tjƒsÂJ dd |¡ ƒ‚d	|v rô|d	 }	t|	tjƒsìJ d
 |	¡ƒ‚|	| _	nd | _	|| _
|| _| ¡ d j}
t |
|ff|  d¡¡| _d S )Nz8Incorrect input type. Expected Struct, but received: {0}Zall_embeddingsÚx_embeddingsÚy_embeddingszLeither (all_embeddings) xor (x_embeddings and y_embeddings) should be given.z-Incorrect input type for x. Expected Scalar, zbut received: {0}z-Incorrect input type for y. Expected Scalar, Úindices_to_gatherzGIncorrect type of indices_to_gather. Expected Scalar, but received: {0}r   Úoutput)Úsuperr   Ú__init__Ú
isinstancer   ÚStructÚformatÚpairwise_similarity_funcZScalarr	   r   r   Zfield_typesÚbaseÚget_next_blob_referenceÚoutput_schema)ÚselfÚmodelZinput_recordZ
output_dimr   ÚnameÚkwargsr   r   r	   Zdtype©Ú	__class__© úb/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/layers/pairwise_similarity.pyr      sX    ÿÿÿÿü
ÿÿÿÿÿÿþzPairwiseSimilarity.__init__c                 C   sÜ   | j dkrP|j|  ¡ dd�}|j|  ¡ dd�}|j||g|  |d ¡gdd�}nH| j dkrˆ|j|  ¡ |  ¡ g|  |  ¡ d ¡gdd�}ntd | j ¡ƒ‚| jrÈ| 	||d ¡}| 
||  ¡ g|  ¡ ¡ n| 	||  ¡ ¡ d S )	NZcosine_similarityé   )ZaxisZ_matmul)Ztrans_br   z(pairwise_similarity_func={} is not validÚ_flatten)r   Z	Normalizer   r   ZBatchMatMulr   ÚNotImplementedErrorr   r	   ZFlattenZBatchGatherr   )r   ÚnetZx_embeddings_normZy_embeddings_normÚYZ	flattenedr   r   r   Úadd_opsA   s8    
ý
ýÿÿÿ
þzPairwiseSimilarity.add_ops)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r!   Ú__classcell__r   r   r   r   r      s     ÿ1r   N)Zcaffe2.pythonr   Zcaffe2.python.layers.layersr   r   r   r   r   r   Ú<module>   s   