o
    }oi                     @   s   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 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eG dd deejZdS )    N)	dataclass)Optional)ModelParallelConfig)MegatronModule)TransformerConfig)ConformerEncoder)to_dict_config)Serialization)NeuralModule)ioc                       s.   e Zd ZdZdef fddZdd Z  ZS )MCoreModalityAdapterModuleaQ  
    Wrapper class for modality adapter such as `nemo.collections.asr.modules.ConformerEncoder`.

    `TransformerConfig` is a dummy config to satisfy the `MegatronModule` constructor.
    `num_attention_heads` is set to 16 such that it's divisible by the value of TP.
    `num_layers` and `hidden_size` are set to 1 since not used.
    modulec                    s"   t  jtddddd || _d S )N      )
num_layershidden_sizenum_attention_heads)config)super__init__r   r   selfr   	__class__ f/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/speechlm/modules/modality_adapter.pyr   '   s   
z#MCoreModalityAdapterModule.__init__c                 C   sV   t t| jjjdkr| ||fS | ||\}}t| jtr'|dd}||fS )Nr      )	leninspect	signaturer   forward
parameters
isinstancer   	transpose)r   encodedencoded_lenencoded_outencoded_len_outr   r   r   r    .   s   z"MCoreModalityAdapterModule.forward)__name__
__module____qualname____doc__r
   r   r    __classcell__r   r   r   r   r      s    r   c                   @   sv   e Zd ZU dZee ed< dZee ed< dZ	ee
 ed< dZee
 ed< dZee ed< dZee
 ed< dd	 ZdS )
ModalityAdapterConfigNr   	input_diminput_key_frominput_key_to
output_dim
output_keyc                 C   sL   | j r| jr| j | j| j< | jr| jr| j| j| j< tt| j}t|S )N)	r.   r0   r   r1   r2   r	   from_config_dictr   r   r   r   r   r   configure_modelB   s   z%ModalityAdapterConfig.configure_model)r(   r)   r*   r   r   dict__annotations__r.   intr/   strr0   r1   r2   r4   r   r   r   r   r-   9   s   
 r-   )r   dataclassesr   typingr   #megatron.core.model_parallel_configr    megatron.core.transformer.moduler   ,megatron.core.transformer.transformer_configr   nemo.collections.asr.modulesr   nemo.collections.speechlm.utilsr   nemo.core.classes.commonr	   nemo.core.classes.moduler
   nemo.lightningr   r   IOMixinr-   r   r   r   r   <module>   s   