o
    پi                  
   @   s   d Z ddlmZ ddlZdejdeeef dB fddZdefd	d
Zdede	fddZ
		ddejdejdB dejdB dejfddZdejdejdejdedejf
ddZdS )zUtils for model executor.    )AnyNweightweight_attrsc                 C   sj   |du rdS |  D ](\}}t| |rJ d| ddlm} | r,|dkr,t|}t| || q
dS )a  Set attributes on a weight tensor.

    This method is used to set attributes on a weight tensor. This method
    will not overwrite existing attributes.

    Args:
        weight: The weight tensor.
        weight_attrs: A dictionary of attributes to set on the weight tensor.
    Nz'Overwriting existing tensor attribute: r   )current_platformweight_loader)itemshasattr'sglang.multimodal_gen.runtime.platformsr   is_tpu_make_synced_weight_loadersetattr)r   r   keyvaluer    r   ^/home/ubuntu/.local/lib/python3.10/site-packages/sglang/multimodal_gen/runtime/models/utils.pyset_weight_attrs   s   r   returnc                    s    fdd}|S )Nc                    s$    | g|R i | t |  d S )N)torch_sync)paramargskwargsoriginal_weight_loaderr   r   _synced_weight_loader0   s   z9_make_synced_weight_loader.<locals>._synced_weight_loaderr   )r   r   r   r   r   r   .   s   r   
layer_namec              	   C   s`   |  d}g }|D ]}z	|t| W q	 ty   Y q	w t|dks,J d|  d|d S )z
    Extract the layer index from the module name.
    Examples:
    - "encoder.layers.0" -> 0
    - "encoder.layers.1.self_attn" -> 1
    - "2.self_attn" -> 2
    - "model.encoder.layers.0.sub.1" -> ValueError
    .   zlayer name z  should only contain one integerr   )splitappendint
ValueErrorlen)r   subnamesint_valssubnamer   r   r   extract_layer_index7   s   
	
r&   xshiftscalec                 C   s`   |du r
|du r
| S |du r| d| d  S |du r"| | d S | d| d  | d S )zmodulate by shift and scaleNr   )	unsqueeze)r'   r(   r)   r   r   r   modulateM   s   r+   
pred_noisenoise_input_latenttimestep	schedulerc                 C   s  |j dkr|dd}| |jd ksJ n)|j dkr8|jd dkr,||jd }n| |jd ks7J ntd|j | j}| j}|  	|} | 	|}|j
 	|}|j 	|}tj|d|d  dd}|| dddd}	||	|   }
|
	|S )a  
    Convert predicted noise to clean latent.

    Args:
    pred_noise: the predicted noise with shape [B, C, H, W]
        where B is batch_size or batch_size * num_frames
    noise_input_latent: the noisy latent with shape [B, C, H, W],
    timestep: the timestep with shape [1] or [bs * num_frames] or [bs, num_frames]
    scheduler: the scheduler

    Returns:
        the predicted video with shape [B, C, H, W]
       r   r   z3[pred_noise_to_pred_video] Invalid timestep shape: )dim)ndimflattennumelshapeexpandr!   dtypedevicedoubletosigmas	timestepsr   argminr*   absreshape)r,   r-   r.   r/   r8   r9   r<   r=   timestep_idsigma_t
pred_videor   r   r   pred_noise_to_pred_video_   s,   



rD   )NN)__doc__typingr   r   Tensordictstrr   r   r    r&   r+   rD   r   r   r   r   <module>   s@   
"	
