o
    i                     @   s   d dl mZ d dlmZ d dlZddejdejdB dedejfd	d
Zdede	deddfddZ
ejfdejdeejB dejdejdejf
ddZejfdejdejdeejB dejdejf
ddZde	de	defddZdS )    )Path)AnyNư>xweightepsreturnc                 C   s   t jjj| | jd f||dS )zRoot-mean-square (RMS) normalize `x` over its last dimension.
    Thin wrapper around `torch.nn.functional.rms_norm` that infers the normalized
    shape and forwards `weight` and `eps`.
    )r   r   )torchnn
functionalrms_normshape)r   r   r    r   :/home/ubuntu/LTX-2/packages/ltx-core/src/ltx_core/utils.pyr      s   r   configkeyexpectedc                 C   s0   |  |}||krtd| d| d| d S )NzConfig value z is z, expected )get
ValueError)r   r   r   actualr   r   r   check_config_value   s   
r   samplesigmadenoised_sample
calc_dtypec                 C   sJ   t |tjr|| }|dkrtd| ||| | | jS )z`
    Convert the sample and its denoised version to velocity.
    Returns:
        Velocity
    r   zSigma can't be 0.0)
isinstancer
   Tensortoitemr   dtype)r   r   r   r   r   r   r   to_velocity   s
    r!   velocityc                 C   s6   t |tjr||}| ||||  | jS )zp
    Convert the sample and its denoising velocity to denoised sample.
    Returns:
        Denoised sample
    )r   r
   r   r   r    )r   r"   r   r   r   r   r   to_denoised'   s   
 r#   	root_pathpatternc                 C   s2   t t| |}|std| d|  |d S )zZ
    Recursively search for files matching a glob pattern and return the first match.
    zNo files matching pattern 'z' found under r   )listr   rglobFileNotFoundError)r$   r%   matchesr   r   r   find_matching_file7   s   r*   )Nr   )pathlibr   typingr   r
   r   floatr   dictstrr   float32r    r!   r#   r*   r   r   r   r   <module>   s<    &


