o
    Iiy
                     @   s<   d dl Z d dlm  mZ d dlmZ dddZdd ZdS )    N)	rearrangec           	      C   s   |dur|| n|}|j dtjd}|j dtjd}tj| dd }|  }ttj	|dtjdd}t
| d| ||||fS )	a  
    Arguments:
        hidden_states: (batch, seqlen, ...)
        attention_mask: (batch, seqlen), bool / int, 1 means valid and 0 means not valid.
        unused_mask: (batch, seqlen), bool / int, 1 means the element is allocated but unused.
    Return:
        hidden_states: (total_nnz, ...), where total_nnz = number of tokens selected in attention_mask + unused_mask.
        indices: (total_nnz), the indices of masked tokens from the flattened input sequence.
        cu_seqlens: (batch + 1), the cumulative sequence lengths, used to index into hidden_states.
        max_seqlen_in_batch: int
        seqused: (batch), returns the number of tokens selected in attention_mask + unused_mask.
    N)dimdtypeF)as_tupler   )   r   zb s ... -> (b s) ...)sumtorchint32nonzeroflattenmaxitemFpadcumsumr   )	hidden_statesattention_maskunused_mask	all_masksseqlens_in_batchused_seqlens_in_batchindicesmax_seqlen_in_batch
cu_seqlens r   B/home/ubuntu/.local/lib/python3.10/site-packages/hopper/padding.pyunpad_input   s   r   c                 C   sF   | j dd }tj|| g|R | j| jd}| ||< t|d|dS )a  
    Arguments:
        hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
        indices: (total_nnz), the indices that represent the non-masked tokens of the original padded input sequence.
        batch: int, batch size for the padded sequence.
        seqlen: int, maximum sequence length for the padded sequence.
    Return:
        hidden_states: (batch, seqlen, ...)
    r   N)devicer   z(b s) ... -> b s ...)b)shaper	   zerosr   r   r   )r   r   batchseqlenr   outputr   r   r   	pad_input(   s   
"r%   )N)	r	   torch.nn.functionalnn
functionalr   einopsr   r   r%   r   r   r   r   <module>   s
   
 