U
    Mh                     @   s   U d Z ddlZddlmZmZ ddlZg Zee ed< ej	e
dddZej	eej	dd	d
Zeee edddZdej	ej	ej	eej	 dddZdS )zCDefines utilities for interacting with scaled_dot_product_attention    N)ListOptional__all__)tensorsreturnc                  G   s   t dd | D S )z0Returns True if any of the tensors requires gradc                 s   s   | ]}|j V  qd S )N)Zrequires_grad).0t r	   K/var/www/html/venv/lib/python3.8/site-packages/torch/nn/attention/_utils.py	<genexpr>   s     z'_input_requires_grad.<locals>.<genexpr>)any)r   r	   r	   r
   _input_requires_grad   s    r   )inpt_tensorog_sizer   c                 C   s"   |  d|kr| dd|f S | S )z'Handles the unpad of the last dimension.N)size)r   r   r	   r	   r
   _postprocess_flash_output   s    r   )head_dim_sizescaler   c                 C   s   |dk	r|S dt |  S )z
    For FlashAttention we pad the head dimension to be a multiple of 8 so we need to scale the output
    by the original head size and not the padded.
    Ng      ?)mathsqrt)r   r   r	   r	   r
   _calculate_scale   s    r           F)querykeyvalue	attn_maskc                 C   s   | j |j ks| j |j kr:td| j  d|j  d|j  d| j|jksR| j|jkrttd| j d|j d|j d|  dk s| dk s| dk rtd	|   d
|  d|  dd S )NzLExpected query, key, and value to have the same dtype, but got query.dtype: z, key.dtype: z, and value.dtype: z	 instead.zSExpected query, key, and value to have the same device type, but got query.device: z, key.device: z, and value.device:    zUExpected query, key, and value to all be  at least 2 dimensional, but got query.dim: z, key.dim: z and value.dim: )Zdtype
ValueErrorZdevicedim)r   r   r   r   Z	dropout_pZ	is_causalr   r	   r	   r
   _validate_sdpa_input!   s    	$"r    )Nr   FN)__doc__r   typingr   r   Ztorchr   str__annotations__ZTensorboolr   intr   floatr   r    r	   r	   r	   r
   <module>   s"       