o
    i                     @   s   d dl mZmZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZmZ e ZG dd	 d	eZed
dG dd deZed
dG dd deZdS )    )	dataclassreplace)ProtocolN)DeviceLikeType)AudioLatentShapeAudioPatchifierVideoLatentPatchifierVideoLatentShapeget_pixel_coords)
Patchifier)LatentStateSpatioTemporalScaleFactorsc                	   @   s   e Zd ZU dZeed< eeB ed< 	ddede	j
de	jdB defd	d
ZdedefddZdedefddZdedefddZdS )LatentToolsz+
    Tools for building latent states.
    
patchifiertarget_shapeNdevicedtypeinitial_latentreturnc                 C   s   dS )z|
        Create an initial latent state. If initial_latent is provided, it will be used to create the latent state.
        N )selfr   r   r   r   r   :/home/ubuntu/LTX-2/packages/ltx-core/src/ltx_core/tools.pycreate_initial_state   s   	z LatentTools.create_initial_statelatent_statec                 C   sr   |j j| j krtd|j j d| j  | }| j|j }| j|j}| j|j	}t
||||dS )z,
        Patchify the latent state.
        zLatent state has shape z, expected shape is latentdenoise_maskclean_latent)r   shaper   to_torch_shape
ValueErrorcloner   patchifyr   r   r   r   r   r   r   r   r   r   r   r"   '   s   zLatentTools.patchifyc                 C   sX   |  }| jj|j| jd}| jj|j| jd}| jj|j| j d}t||||dS )z.
        Unpatchify the latent state.
        )output_shaper   )	r!   r   
unpatchifyr   r   r   r   
mask_shaper   r#   r   r   r   r%   6   s   zLatentTools.unpatchifyc                 C   s   |  }| j| j}|jddd|f }|jddd|f }t|jddd|f }|j	ddddd|f }t
||||ddS )z
        Clear the conditioning from the latent state. This method removes extra tokens from the end of the latent.
        Therefore, conditioning items should add extra tokens ONLY to the end of the latent.
        N)r   r   	positionsr   attention_mask)r!   r   get_token_countr   r   r   torch	ones_liker   r'   r   )r   r   
num_tokensr   r   r   r'   r   r   r   clear_conditioningB   s   zLatentTools.clear_conditioningN)__name__
__module____qualname____doc__r   __annotations__r	   r   r   r*   r   Tensorr   r   r"   r%   r-   r   r   r   r   r      s"   
 
r   T)frozenc                	   @   sh   e Zd ZU dZeed< eed< eed< eZ	e
ed< dZeed< 	dd	ed
ejdejdB defddZdS )VideoLatentToolsz1
    Tools for building video latent states.
    r   r   fpsscale_factorsT
causal_fixNr   r   r   r   c                 C   s   |d ur|j | j ksJ d|j  d| j  ntj| j ||d}| }tj| j  |tjd}| j	j
| j|d}t|| j| jd }|d d ddf | j |d d ddf< | t|||||dS )	NLatent shape  does not match target shape r   r   r$   r   )latent_coordsr8   r9   r   .r   r   r'   r   )r   r   r   r*   zerosr!   onesr&   float32r   get_patch_grid_boundsr
   r8   r9   floatr7   r"   r   to)r   r   r   r   r   r   r>   r'   r   r   r   r   d   sF   &z%VideoLatentTools.create_initial_stater.   )r/   r0   r1   r2   r   r3   r	   rD   DEFAULT_SCALE_FACTORSr8   r   r9   boolr   r*   r   r4   r   r   r   r   r   r   r6   X   s"   
 r6   c                	   @   sH   e Zd ZU dZeed< eed< 	ddedej	dej
dB defd	d
ZdS )AudioLatentToolsz1
    Tools for building audio latent states.
    r   r   Nr   r   r   r   c                 C   s   |d ur|j | j ksJ d|j  d| j  ntj| j ||d}| }tj| j  |tjd}| j	j
| j|d}| t||||dS )Nr:   r;   r<   r=   r?   )r   r   r   r*   r@   r!   rA   r&   rB   r   rC   r"   r   )r   r   r   r   r   r   r>   r   r   r   r      s0   z%AudioLatentTools.create_initial_stater.   )r/   r0   r1   r2   r   r3   r   r   r*   r   r4   r   r   r   r   r   r   rH      s   
 rH   )dataclassesr   r   typingr   r*   torch._prims_commonr   ltx_core.components.patchifiersr   r   r   r	   r
   ltx_core.components.protocolsr   ltx_core.typesr   r   defaultrF   r   r6   rH   r   r   r   r   <module>   s    D: