o
    }o™i)  ã                   @   s†   d dl mZ dededeeef fdd„Zdededefdd„Zdededefd	d
„Zdededefdd„Zdededefdd„ZdS )é    )ÚTensorÚxÚyÚreturnc                 C   s–   | j }|j }t||ƒ}t|ƒD ]}| j| |j| ks"J d |¡ƒ‚q||k r7|  | jd||   ¡} | |fS ||k rG| |jd||   ¡}| |fS )a³  
    Broadcasts two tensors to have the same shape by adding singleton dimensions where necessary.

    Args:
        x (Tensor): The first input tensor.
        y (Tensor): The second input tensor.

    Returns:
        tuple[Tensor, Tensor]: A tuple containing the two tensors with broadcasted shapes.

    Raises:
        AssertionError: If the dimensions of the tensors do not match at any axis within their common dimensions.
    zDimensions not equal at axis {})é   )ÚndimÚminÚrangeÚshapeÚformatÚreshape)r   r   Úndims1Úndims2Úcommon_ndimsÚaxis© r   ú`/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/diffusion/sampler/batch_ops.pyÚcommon_broadcast   s   
$ýr   c                 C   s   t | |ƒ\} }| | S )a  
    Adds two tensors element-wise after broadcasting them to a common shape.

    Args:
        x (Tensor): The first input tensor.
        y (Tensor): The second input tensor.

    Returns:
        Tensor: The element-wise sum of the input tensors after broadcasting.
    ©r   ©r   r   r   r   r   Ú	batch_add/   ó   r   c                 C   s   t | |ƒ\} }| | S )a  
    Multiplies two tensors element-wise after broadcasting them to a common shape.

    Args:
        x (Tensor): The first input tensor.
        y (Tensor): The second input tensor.

    Returns:
        Tensor: The element-wise product of the input tensors after broadcasting.
    r   r   r   r   r   Ú	batch_mul>   r   r   c                 C   s   t | |ƒ\} }| | S )a  
    Subtracts two tensors element-wise after broadcasting them to a common shape.

    Args:
        x (Tensor): The first input tensor.
        y (Tensor): The second input tensor.

    Returns:
        Tensor: The result of element-wise subtraction of the input tensors.
    r   r   r   r   r   Ú	batch_subM   r   r   c                 C   s   t | |ƒ\} }| | S )a  
    Divides two tensors element-wise after broadcasting them to a common shape.

    Args:
        x (Tensor): The first input tensor.
        y (Tensor): The second input tensor.

    Returns:
        Tensor: The result of element-wise division of `x` by `y` after broadcasting.
    r   r   r   r   r   Ú	batch_div\   r   r   N)Útorchr   Útupler   r   r   r   r   r   r   r   r   Ú<module>   s   