o
    vi                     @   st   d Z ddlZddlmZ ddlmZ 		ddededededB d	edB d
eddeef eeeeef B fddZdS )zo
Imaginaire4 Attention Subpackage:
Unified implementation for all Attention implementations.

Varlen utilities
    N)Tensor)is_torch_compilingquerykeyvalue	seqlens_Q
seqlens_KVreturnc           
      C   s  t  rtd| jd |jd ks| jd |jd kr2td| jd d|jd d|jd d|d u |d u A rEtd|d|d|d u rO|d u rOd	S |d usUJ |d us[J t|tret|tsitd
|j| jksu|j| jkrtd|jd|jd| jd|jtj	ks|jtj	krtd|jd|jd|
 dks|
 dkrtd|
 d|
 d|jd |jd krtd|jd|jd|jd dk rtd|jd|jd| jd dkrtd| jd d|
 |
   krdksJ  J |jd |jd   krdks!J  J |j|j  kr1tj	ks4J  J |  }|  }tjdgtj	|jd}tj||dtj	gdd}tj||dtj	gdd}	t|tssJ t|ts{J ||	||fS )NzRunning 'generate_varlen_parameters' in a torch-compiled region is disallowed as it results in graph breaks. Please consider calling ahead of time and pass 'cumulative_seqlen_{Q,KV}' and 'max_seqlen_{Q,KV}' instead of 'seqlens_{Q,KV}' to 'attention'. r   z9Q, K, and V must match in batch size, got query.shape[0]=z, key.shape[0]=z, value.shape[0]=.z]Variable length Attention requires both of seqlens_Q and seqlens_KV to be set, got seqlens_Q=z, seqlens_KV=)NNr   r   z.seqlens_Q and seqlens_KV must both be tensors.zQseqlens_Q and seqlens_KV must be on the same device as QKV, but seqlens_Q.device=z, seqlens_KV.device=z, query.device=zOseqlens_Q and seqlens_KV must both be torch.int32 tensors, got seqlens_Q.dtype=z, seqlens_KV.dtype=   zGseqlens_Q and seqlens_KV must both be 1-D tensors, got seqlens_Q.dim()=z, seqlens_KV.dim()=zAseqlens_Q and seqlens_KV must match in size, got seqlens_Q.shape=z, seqlens_KV.shape=zPseqlens_Q and seqlens_KV must contain at least one element, got seqlens_Q.shape=zfVariable length attention only supports sequence-packed memory layout (batch = 1), got query.shape[0]=)dtypedevice)dim)r   RuntimeErrorshape
ValueError
isinstancer   r   r   torchint32r   maxitemtensorcatcumsumtoint)
r   r   r   r   r   max_seqlen_Qmax_seqlen_KVzcumulative_seqlen_Qcumulative_seqlen_KV r!   T/data/cameron/vidgen/cosmos-policy/cosmos_policy/_src/imaginaire/attention/varlen.pygenerate_varlen_parameters   s   (((,&  r#   )NN)__doc__r   r   -cosmos_policy._src.imaginaire.attention.utilsr   tupler   r#   r!   r!   r!   r"   <module>   s&   