o
    vi                     @   sl   d Z 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mZmZmZmZmZ G dd deZdS )	z
Extended Sampler for Cosmos Policy with special handling for num_steps.

This sampler extends the base Sampler class to add:
- Adjusted num_steps logic when sample_clean is enabled
- Special case handling for num_steps==1
    )CallableListOptionalN)is_multi_step_fn_supported)is_runge_kutta_fn_supported)SamplerSamplerConfigSolverConfigSolverTimestampConfigdifferential_equation_solver
get_rev_tsc                       s   e Zd ZdZd"dee f fddZe dddd	d
d
e	dddf	de
dejdede	de	de	de	de	de	de	dedejfddZe 			d#de
ejejgejf dejdee deee
  dedejfd d!Z  ZS )$CosmosPolicySamplerz
    Extended Sampler for Cosmos Policy.

    Adds special handling for:
    - Adjusting num_steps when sample_clean is enabled (subtracts 1 for num_steps > 1)
    - Special case for num_steps==1 where we directly denoise without the solver loop
    Ncfgc                    s   t  | d S N)super__init__)selfr   	__class__ J/data/cameron/vidgen/cosmos-policy/cosmos_policy/modules/cosmos_sampler.pyr   1   s   zCosmosPolicySampler.__init__#   gMb`?P      r   inf   2abx0_fnx_sigma_max	num_steps	sigma_min	sigma_maxrhoS_churnS_minS_maxS_noisesolver_optionreturnc              	      s   |j  dtjdtjdtjf fdd}t|}t|}|s'|s'J d| t||	||
|||d}d}|r>|d	kr>|d	 }t||||d
}t|||d}| j||||d	 S )Nx_B_StateShapet_Br(   c                    s   |   |   tjS r   )totorchfloat64)r)   r*   in_dtyper   r   r   float64_x0_fnE   s   z2CosmosPolicySampler.forward.<locals>.float64_x0_fnz2Only support multistep or Runge-Kutta method, got )s_churns_t_maxs_t_mins_noiseis_multirk	multistepTr   )nfet_mint_maxorder)solver
timestampssample_clean)r   )
dtyper,   Tensorr   r   r	   r
   r   _forward_implr+   )r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r0   is_multistepis_rk
solver_cfgr>   timestamps_cfgsampler_cfgr   r.   r   forward4   s(   "zCosmosPolicySampler.forwarddenoiser_fnnoisy_input_B_StateShaperF   callback_fnsc                 C   s   |du r| j n|}|jjrdnt|jjd }|jj| }t|jj|jj	||jj
|j}|dkrXt|||j|d|}	|jrVtj|	d|	j|	jd}
||	|d |
 }	|	S |}	tj|	d|	j|	jd}
||	|d |
 }	|	S )a  
        Internal implementation of the forward pass.

        Args:
            denoiser_fn: Function to denoise the input.
            noisy_input_B_StateShape: Input tensor with noise.
            sampler_cfg: Configuration for the sampler.
            callback_fns: List of callback functions to be called during sampling.
            num_steps: Number of denoising steps.

        Returns:
            torch.Tensor: Denoised output tensor.
        Nr   r   )rJ   )devicer?   )r   r<   r5   intr6   r=   r8   r   r9   r:   r;   r+   rK   r   r>   r,   onessizer?   )r   rH   rI   rF   rJ   r   solver_ordernum_timestampssigmas_Ldenoised_outputrN   r   r   r   rA   _   s,   
z!CosmosPolicySampler._forward_implr   )NNr   )__name__
__module____qualname____doc__r   r   r   r,   no_gradfloatr   r@   rM   strrG   r   rA   __classcell__r   r   r   r   r   (   sn    	
*
r   )rW   typingr   r   r   r,   3cosmos_policy._src.imaginaire.functional.multi_stepr   4cosmos_policy._src.imaginaire.functional.runge_kuttar   1cosmos_policy._src.imaginaire.modules.res_samplerr   r   r	   r
   r   r   r   r   r   r   r   <module>   s    
