o
    .wi                     @   s   d dl mZmZmZ d dlZd dl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)
functional)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)aB  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   _/home/ubuntu/sommelier/.venv/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sC|du rE|S td| d	| )
a  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                    sb   t    fddt|D }t j| | W d    n1 s"w   Y   |t j|< |S )Nc                       g | ]}t  qS r   r   
zeros_like.0_r    r   r   
<listcomp>]       z._simple_gather_all_tensors.<locals>.<listcomp>)r   no_gradrangedistributed
all_gatherget_rank)r    r!   r"   gathered_resultr   r)   r   _simple_gather_all_tensors[   s   
r2   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 t  V g }    }t|D ]}|d ||  qrt| |fd
dt|D }t j|| t|D ]\}	}
dd |
D }||	 | ||	< qW d   n1 sw   Y  | |t j|< |S )a5  Gather all tensors from several ddp processes onto a list that is broadcast 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:
        list with size equal to the process group where element 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ndimr2   tensorshaper3   r-   r/   stackmaxvaluesr7   r,   detachcpureversedappenditemFpad	enumerater0   )r    r!   r"   local_sizesall_sizes_equalpad_dimspad_byvalr1   idx	item_sizeslice_paramr   )r4   r9   r;   r   gather_all_tensorsd   s<   



rX   )r   r6   )typingr   r   r   r   r   torch.nnr   rM   typing_extensionsr   r   r   intr2   rX   r   r   r   r   <module>   s(   
.$	