o
    vij                     @   s0   d dl Zd dlZd dlmZ G dd deZdS )    N)EDMSDEc                       sf   e Zd ZdZ							dd	ed
edededededef fddZdedej	f fddZ
  ZS )HybridEDMSDEag  
    Extended EDMSDE for Cosmos Policy that supports hybrid sigma distribution.

    This class adds a hybrid sampling strategy that combines:
    - 70% samples from the original log-normal distribution
    - 30% samples from a uniform distribution over higher sigma values

    This approach helps put more weight on higher sigma values during training.
    333333333333?      T@Mb`?F      ?     @U@p_meanp_std	sigma_max	sigma_minhybrid_sigma_distributionuniform_loweruniform_upperc                    s*   t  j||||d || _|| _|| _d S )N)r
   r   r   r   )super__init__r   r   r   )selfr
   r   r   r   r   r   r   	__class__ J/data/cameron/vidgen/cosmos-policy/cosmos_policy/modules/hybrid_edm_sde.pyr   !   s   

zHybridEDMSDE.__init__
batch_sizereturnc                    s    j rStj|dd}tj|dddk }|  }|| }|dkr=tjj|d}tj	 fdd|D dd}t
|||< |dkrQtj|dd j j|| < |S t |S )a  
        Sample timesteps (sigma values) for training.

        When hybrid_sigma_distribution is enabled, uses a mixture of:
        - Log-normal distribution (70% of samples)
        - Uniform distribution between uniform_lower and uniform_upper (30% of samples)

        Otherwise, uses the base class implementation (pure log-normal).

        Args:
            batch_size: Number of samples to generate

        Returns:
            Tensor of sigma values with shape (batch_size,)
        cuda)devicegffffff?r   )sizec                    s   g | ]} j |qS r   )gaussian_distinv_cdf).0cdf_valr   r   r   
<listcomp>O   s    z)HybridEDMSDE.sample_t.<locals>.<listcomp>)r   torchzerosrandsumitemnprandomuniformtensorexpemptyuniform_r   r   r   sample_t)r   r   samplesZdistribution_choiceZnum_lognormalZnum_uniformcdf_valsZ
log_sigmasr   r!   r   r/   0   s   
zHybridEDMSDE.sample_t)r   r   r   r   Fr   r	   )__name__
__module____qualname____doc__floatboolr   intr#   Tensorr/   __classcell__r   r   r   r   r      s2     r   )numpyr(   r#   -cosmos_policy._src.imaginaire.modules.edm_sder   r   r   r   r   r   <module>   s   