o
    pi                     @   s4   d dl Z ddlmZ ddlmZ G dd deZdS )    N   )common_functions)ModuleWithRecordsAndDistancec                       s6   e Zd Z fddZdd Zdd Zd
dd	Z  ZS )	BaseMinerc                    s(   t  jdi | | jg ddd d S )N)num_pos_pairsnum_neg_pairsnum_tripletsT)list_of_namesis_stat )super__init__add_to_recordable_attributes)selfkwargs	__class__r   ]/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_metric_learning/miners/base_miner.pyr      s
   
zBaseMiner.__init__c                 C   s   t )N)NotImplementedError)r   
embeddingslabelsref_emb
ref_labelsr   r   r   mine   s   zBaseMiner.minec                 C   s   t |dkr&t |d | _| jt |d   kr!t |d ks$J  J dS t |dkrRt |d | _t |d | _| jt |d ksEJ | jt |d ksPJ dS t)a  
        Args:
            output: the output of self.mine
        This asserts that the mining function is outputting
        properly formatted indices. The default is to require a tuple representing
        a,p,n indices or a1,p,a2,n indices within a batch of embeddings.
        For example, a tuple of (anchors, positives, negatives) will be
        (torch.tensor, torch.tensor, torch.tensor)
           r      r      N)lenr   r   r   	TypeError)r   outputr   r   r   output_assertion   s   
2zBaseMiner.output_assertionNc                 C   sz   |    t & t|| t||}t||||\}}| ||||}W d   n1 s1w   Y  | | |S )a  
        Args:
            embeddings: tensor of size (batch_size, embedding_size)
            labels: tensor of size (batch_size)
        Does any necessary preprocessing, then does mining, and then checks the
        shape of the mining output before returning it
        N)	reset_statstorchno_gradc_fcheck_shapes	to_deviceset_ref_embr   r    )r   r   r   r   r   mining_outputr   r   r   forward'   s   

zBaseMiner.forward)NN)__name__
__module____qualname__r   r   r    r)   __classcell__r   r   r   r   r      s
    r   )r"   utilsr   r$   %utils.module_with_records_and_reducerr   r   r   r   r   r   <module>   s    