o
    yi                      @   s   d dl mZmZmZ d dlZd dlm  mZ d dlm	Z	 d dl
mZ de	ded de	fd	d
Z	dde	de	de	ded de	f
ddZde	dededee	 fddZdde	dee dee	 fddZdS )    )AnyListOptionalN)Tensor)Literalx	reduction)elementwise_meansumnoneNreturnc                 C   s@   |dkr	t | S |dks|du r| S |dkrt | S td)aA  Reduces a given tensor by a given reduction method.

    Args:
        x: the tensor, which shall be reduced
        reduction:  a string specifying the reduction method ('elementwise_mean', 'none', 'sum')

    Return:
        reduced Tensor

    Raise:
        ValueError if an invalid reduction parameter was given
    r	   r   Nr
   zReduction parameter unknown.)torchmeanr
   
ValueError)r   r    r   V/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/utilities/distributed.pyreduce   s   

r   r   numdenomweightsclass_reductionmicromacroweightedr   Nc                 C   s   d}|dkrt | t | }n| | }d|||k< |dkr!|S |dkr*t |S |dkr<t || t |  S |dksD|du rF|S td| d	| )
a  
    Function used to reduce classification metrics of the form ``num / denom * weights``.
    For example for calculating standard accuracy the num would be number of
    true positives per class, denom would be the support per class, and weights
    would be a tensor of 1s

    Args:
        num: numerator tensor
        denom: denominator tensor
        weights: weights for each class
        class_reduction: reduction method for multiclass problems:

            - ``'micro'``: calculate metrics globally (default)
            - ``'macro'``: calculate metrics for each label, and find their unweighted mean.
            - ``'weighted'``: calculate metrics for each label, and find their weighted mean.
            - ``'none'`` or ``None``: returns calculated metric per class

    Raises:
        ValueError:
            If ``class_reduction`` is none of ``"micro"``, ``"macro"``, ``"weighted"``, ``"none"`` or ``None``.

    r   r   r   r   r   r   NzReduction parameter z' unknown. Choose between one of these: )r   r
   r   floatr   )r   r   r   r   valid_reductionfractionr   r   r   class_reduce,   s    
r   resultgroup
world_sizec                    s*    fddt |D }tj| | |S )Nc                       g | ]}t  qS r   r   
zeros_like.0_r   r   r   
<listcomp>a       z._simple_gather_all_tensors.<locals>.<listcomp>)ranger   distributed
all_gather)r   r    r!   gathered_resultr   r(   r   _simple_gather_all_tensors`   s   r/   c                    s^  |du r	t jjj}|  } t j|}t jj|d | jdkr%t| ||S t j	| j
| jd  fddt|D }t jj| |d t |jddjtfdd	|D }|r_t| ||S g }    }t|D ]}|d ||  qmt| |fd
dt|D }t j|| t|D ]\}	}
dd |
D }||	 | ||	< q|S )ao  Function to gather all tensors from several ddp processes onto a list that is broadcasted to all processes.
    Works on tensors that have the same number of dimensions, but where each dimension may differ. In this case
    tensors are padded, gathered and then trimmed to secure equal workload for all processes.

    Args:
        result: the value to sync
        group: the process group to gather results from. Defaults to all processes (world)

    Return:
        gathered_result: list with size equal to the process group where
            ``gathered_result[i]`` corresponds to result tensor from process ``i``
    N)r    r   )devicec                    r"   r   r#   r%   )
local_sizer   r   r)      r*   z&gather_all_tensors.<locals>.<listcomp>)dimc                 3   s    | ]	}t | kV  qd S N)all)r&   ls)max_sizer   r   	<genexpr>   s    z%gather_all_tensors.<locals>.<genexpr>c                    r"   r   r#   r%   )result_paddedr   r   r)      r*   c                 S   s   g | ]}t |qS r   )slice)r&   dim_sizer   r   r   r)      s    )r   r,   r    WORLD
contiguousget_world_sizebarrierndimr/   tensorshaper0   r+   r-   stackmaxvaluesr4   detachcpureversedappenditemFpad	enumerate)r   r    r!   local_sizesall_sizes_equalpad_dimspad_byvalr.   idx	item_sizeslice_paramr   )r1   r6   r8   r   gather_all_tensorsf   s4   


rU   )r   r3   )typingr   r   r   r   torch.nn.functionalnn
functionalrJ   r   typing_extensionsr   r   r   intr/   rU   r   r   r   r   <module>   s(   
4$