o
    TÃiÿ  ã                   @   s˜   d dl mZ d dlmZmZmZmZ d dlZd dlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ G dd„ deƒZG dd„ deƒZdS )é    )Úabstractmethod)ÚAnyÚDictÚOptionalÚTypeN)ÚDeepSpeedConfigModelé   )ÚRaggedBatchWrapperé   )ÚDSModuleBase)ÚDSModuleRegistryBase©ÚDSEmbeddingsConfig)ÚInferenceParameterc                       s¶   e Zd ZdZedee fdd„ƒZdede	e
ef ddf‡ fdd	„Zd
ejdefdd„Zeedejfdd„ƒƒZ			ddedejdeej deej deej defdd„Z‡  ZS )ÚDSEmbeddingBasea  
    Base mixin for embedding modules. The interface represented by this module is:

    hidden_out = embedding(input_ids) +
                 position_embedding(position_ids) +
                 token_type_embedding(token_type_ids)
    with optional normalization.
    Úreturnc                   C   ó   t S ©Nr   © r   r   úl/home/ubuntu/.local/lib/python3.10/site-packages/deepspeed/inference/v2/modules/interfaces/embedding_base.pyÚconfig_class   ó   zDSEmbeddingBase.config_classÚconfigÚimplementation_configNc                    s   t ƒ  ||¡ d S r   )ÚsuperÚ__init__)Úselfr   r   ©Ú	__class__r   r   r   !   s   zDSEmbeddingBase.__init__Úembed_paramc                 C   ó   t ƒ ‚)a3  
        Perform any necessary transformations on an embedding parameter. This module assumes
        that all embedding parameters would require the same set of transformations.

        Parameters:
            embed_param (torch.Tensor): Embedding parameter. Shape is of [vocab_size, hidden_size]
        ©ÚNotImplementedError)r   r   r   r   r   Útransform_param$   s   zDSEmbeddingBase.transform_paramc                 C   r    )a  
        Pre-allocated output Tensor. This currently needs to be exposed for gather operations
        on the output.

        TODO(cmikeh2): This is not ideal. We need a better abstraction for this, such as giving
        access to the inference comm object to the DSModule.
        r!   )r   r   r   r   Úoutput.   s   
zDSEmbeddingBase.outputÚragged_batchÚword_embeddingsÚposition_embeddingsÚtoken_type_idsÚtoken_type_embeddingsc                 C   r    )aV  
        Parameters:
            ragged_batch (torch.Tensor): Ragged batch of token ids + associated metadata.
            word_embeddings (torch.Tensor): Word embeddings.
            position_embeddings (torch.Tensor): Position embeddings. If passed, IDs will be
                inferred from the ragged batch itself.
            token_type_ids (torch.Tensor): Token type ids.
            token_type_embeddings (torch.Tensor): Token type embeddings.

        Returns:
            torch.Tensor: Hidden states. This should be the sum of the relevant
                encodings for the model.
        r!   )r   r%   r&   r'   r(   r)   r   r   r   Úforward:   s   zDSEmbeddingBase.forward)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r   Ústrr   r   ÚtorchÚTensorr   r#   Úpropertyr   r$   r	   r   r*   Ú__classcell__r   r   r   r   r      s2    	"
ûÿþýüûûr   c                   @   s0   e Zd ZU i Zeed< edee fdd„ƒZ	dS )ÚDSEmbeddingRegistryÚregistryr   c                   C   r   r   )r   r   r   r   r   Úassociated_classS   r   z$DSEmbeddingRegistry.associated_classN)
r+   r,   r-   r6   r   Ú__annotations__r/   r   r   r7   r   r   r   r   r5   P   s   
 r5   )Úabcr   Útypingr   r   r   r   r1   Údeepspeed.runtime.config_utilsr   Úraggedr	   Ú	ds_moduler   Úmodule_registryr   Úconfigsr   Úinference_parameterr   r   r5   r   r   r   r   Ú<module>   s   =