o
    }oi[                     @   s   d dl mZ d dlmZ d dlmZmZmZmZ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mZ d dlmZ d dlmZmZmZ erVd d	l m!Z! e
d
ej"dZ#G dd dej$eZ$eG dd dee# Z%dS )    deepcopy)Path)TYPE_CHECKINGOptionalProtocolSequenceTypeTypeVarUnionruntime_checkableN)nn)Selfoverride)ckpt_to_context_subdir)IOMixinserializationtrack_io)OptimizerConfigModelT)boundc                       s   e Zd Zdeje fddZ	ddeee	f de
e ddfdd	Zdeee	f d
ee ddfddZeddejdedef fddZddejdedejfddZ  ZS )Fabricreturnc                 K   sP   dd |  D }| D ]}tt|stt| qtjt| fi |S )Nc                 S   s   i | ]	\}}|t |qS  r   ).0kvr   r   P/home/ubuntu/.local/lib/python3.10/site-packages/nemo/lightning/fabric/fabric.py
<dictcomp>%   s    z"Fabric.io_init.<locals>.<dictcomp>)itemsvaluesr   find_node_traversertyper   fdlConfig)selfkwargs
cfg_kwargsvalr   r   r   io_init#   s   zFabric.io_initNpathmodelzDistributedModel[ModelT]c                 C   sT   |    ddlm} t|}|du r|t|}|j}| |}| |d|i |S )aZ  Load and set up a model for distributed training.

        This method loads a model from the given path, sets it up for distributed training
        using the current Fabric instance, and returns a DistributedModel.

        Args:
            path (Union[str, Path]): The path to the saved model checkpoint.
            model (Optional[ModelT], optional): An optional pre-instantiated model. If not
            provided, the model will be loaded from the checkpoint. Defaults to None.

        Returns:
            DistributedModel[ModelT]: The loaded and distributed model.

        Example:
            >>> from nemo import lightning as nl
            >>>
            >>> trainer = nl.Trainer(
            ...     devices=2,
            ...     strategy=nl.MegatronStrategy(tensor_model_parallel_size=2),
            ...     plugins=nl.MegatronMixedPrecision(precision='16-mixed')
            ... )
            >>> fabric = trainer.to_fabric()
            >>> distributed_model = fabric.load_model("path/to/checkpoint/dir")
            >>>
            >>> # You can now interact with the parallel model
        r   )load_contextN
state_dict)launchnemo.lightning.ior,   r   r   r+   setup_moduleload)r%   r*   r+   r,   context
dist_modelr   r   r   
load_model-   s   
zFabric.load_model
model_typec                 C   s6   ddl m} t||std||}| |j|S )a8  
        Import a model from a given path and set it up for distributed training.

        This method imports a model of the specified type from the given path, loads it,
        and sets it up for distributed training using the current Fabric instance.

        Args:
            path (Union[str, Path]): The path to the model. Can be a local path or a
                Hugging Face model identifier.
            model_type (Type[ModelT]): The type of the model to import. Must be a subclass
                of ConnectorMixin.

        Returns:
            DistributedModel[ModelT]: The imported and distributed model.

        Raises:
            TypeError: If the provided model_type is not a subclass of ConnectorMixin.

        Example:
            >>> from nemo import lightning as nl
            >>> from nemo.collections.llm import MistralModel
            >>>
            >>> trainer = nl.Trainer(
            ...     devices=2,
            ...     strategy=nl.MegatronStrategy(tensor_model_parallel_size=2),
            ...     plugins=nl.MegatronMixedPrecision(precision='16-mixed')
            ... )
            >>> fabric = trainer.to_fabric()
            >>> model = fabric.import_model("hf://mistralai/Mistral-7B-v0.1", MistralModel)
            >>>
            >>> # You can now interact with the parallel model
        r   )ConnectorMixinz=The provided model class must be a subclass of ConnectorMixin)r/   r6   
issubclass	TypeErrorimport_fromr4   	ckpt_path)r%   r*   r5   r6   r+   r   r   r   import_modelZ   s
   %

zFabric.import_modelTmodulemove_to_device_reapply_compilec                    s4   ddl m} t j|||d}t| j|r|jS |S )Nr   )FabricMegatronStrategy)r=   r>   ) nemo.lightning.fabric.strategiesr?   superr0   
isinstancestrategy_forward_module)r%   r<   r=   r>   r?   out	__class__r   r   r0      s
   zFabric.setup_module 
datamodulestagec                 C   s&   | | t| jdr| j|}|S )Nprocess_datamodule)setuphasattrrC   rK   )r%   rI   rJ   r   r   r   setup_datamodule   s   
zFabric.setup_datamodule)N)TT)rH   )__name__
__module____qualname__r#   r$   r   r)   r   strr   r   r   r4   r	   r;   r   r   Moduleboolr0   plLightningDataModulerN   __classcell__r   r   rF   r   r   "   s(    

-

. $r   c                   @   s   e Zd ZU eed< dS )DistributedModelr<   N)rO   rP   rQ   r   __annotations__r   r   r   r   rX      s   
 rX   )&copyr   pathlibr   typingr   r   r   r   r	   r
   r   r   fiddler#   lightning.fabricfabriclblightning.pytorchpytorchrU   torchr   typing_extensionsr   r   nemo.lightning.ckpt_utilsr   nemo.lightning.io.mixinr   r   r   megatron.core.optimizerr   rS   r   r   rX   r   r   r   r   <module>   s    (|