o
    vi3                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZmZmZ 		dd
edededededededefddZdS )zs
Imaginaire4 Attention Subpackage:
Unified implementation for all Attention implementations.

cudNN backend checks
    )partial)Tensor)attention_param_checksattention_tensor_checks)CUDNN_DISALLOWEDCUDNN_SUPPORTED)get_bwd_dtypesget_fwd_dtypes)
CausalType)get_arch_tagis_torch_compilinglog_or_raise_errorFquerykeyvalue	is_causalcausal_type	is_varlenraise_errorreturnc                 C   s   t t|d}ts|dtd dS tr|dtd dS t r%|dtd dS t| j}t|}	t	|}
t
| |||	|
dd|dd	sG|d	td dS |rQ|d
td dS t| ||||d |rl|tjtjfvrl|dtd dS dS )a  
    Input validation function for the cuDNN backend.
    Runs the common and cuDNN-specific checks. Returns False if any checks fail, otherwise True.

    Parameters:
        query (Tensor): 4-D query tensor, with the heads-last contiguous layout
            (`[batch, seqlen, heads, head_dim]`).

        key (Tensor): 4-D key tensor, with the heads-last contiguous layout
            (`[batch, seqlen_kv, heads_kv, head_dim]`).

        value (Tensor): 4-D value tensor, with heads-last contiguous layout
            (`[batch, seqlen_kv, heads_kv, head_dim_v]`).

        is_causal (bool): whether or not causal masking is enabled.

        causal_type (CausalType): causal masking mode. Choices: `CausalType.TopLeft`,
            `CausalType.BottomRight`. Required when `is_causal = True`.

        is_varlen (bool): whether or not a variable length (varlen) use case. Must be inferred
            beforehand based on arguments such as seqlens_{Q,KV} or cumulative_seqlen_{Q,KV} being
            passed.

        raise_error (bool): whether to raise an error if any checks fail or no backend is selected,
            instead of just returning False. Default is False.

    Returns:
        success (bool): whether use case is compatible with cuDNN backend.

    )r   zkcuDNN is not supported in this environment. Run with debug logs to find out why, or choose another backend.)	exceptionFz9cuDNN backend is disabled. Please choose another backend.z1cuDNN backend does not support torch.compile yet.zcuDNN Attention)	r   r   r   Zsupported_dtypes_forwardZsupported_dtypes_backwardZsupports_mlaZsupports_gqa_mqar   backend_namez(cuDNN does not support the given inputs.z1Varlen for cuDNN Attention is not integrated yet.)r   r   r   r   r   z>cuDNN Attention only supports top-left causal masking for now.T)r   r   r   RuntimeErrorr   r   r   devicer	   r   r   r   r
   TopLeftDontCare)r   r   r   r   r   r   r   	target_fnarch_tagZ
fwd_dtypesZ
bwd_dtypes r   Z/data/cameron/vidgen/cosmos-policy/cosmos_policy/_src/imaginaire/attention/cudnn/checks.pycudnn_attention_check"   s\   '
r    N)F)__doc__	functoolsr   torchr   .cosmos_policy._src.imaginaire.attention.checksr   r   -cosmos_policy._src.imaginaire.attention.cudnnr   r   Z2cosmos_policy._src.imaginaire.attention.cudnn.metar   r	   -cosmos_policy._src.imaginaire.attention.masksr
   -cosmos_policy._src.imaginaire.attention.utilsr   r   r   boolr    r   r   r   r   <module>   s4   
