o
    vi                     @   s|   d dl Z d dlmZ z
d dlmZmZ W n ey!   ed Y nw de jdede jfdd	Z	de jdede jfd
dZ
dS )    N)
DeviceMesh)	Replicatedistribute_tensorzHtorch.distributed.tensor is not available. DeepSeek model will not work.tensorcp_or_tp_meshreturnc                 C   s.   |  d} | dkrt| |t g } | S )Ncuda   )tosizer   r   to_local)r   r    r   R/data/cameron/vidgen/cosmos-policy/cosmos_policy/_src/reason1/utils/parallelism.py	broadcast   s   
r   c                 C   s^   t | j }t | j }t||}|| r(t j| | j	| j
d} t| |} | S )aV  Broadcast a tensor and check if the shape is the same across CP/TP ranks.
    If not, create a new tensor matching rank 0 and broadcast it.

    Args:
        tensor (torch.Tensor): The tensor to broadcast.
        cp_or_tp_mesh (DeviceMesh): The device mesh used to broadcast.

    Returns:
        torch.Tensor: The broadcasted tensor.
    )dtypedevice)torchr   shaper   r   neanyzerostolistr   r   )r   r   original_shapefinal_shaper   r   r   broadcast_with_shape_check!   s   

r   )r   torch.distributed.device_meshr   torch.distributed.tensorr   r   ImportErrorprintTensorr   r   r   r   r   r   <module>   s   