o
    }oi                     @   s   d Z ddlZddlZddlZddlmZ ddlZddlm	Z	 ddl
mZ e ZG dd de	Zejdd	 ZdddZedd dd Zedge_dS )a  HuggingFace model serialization support for NeMo's configuration system.

This module provides integration between NeMo's configuration system and HuggingFace's
pretrained models. It enables automatic serialization and deserialization of HuggingFace
models within NeMo's configuration framework.

The integration works by:
1. Detecting HuggingFace models through their characteristic methods (save_pretrained/from_pretrained)
2. Converting them to Fiddle configurations that preserve the model's class and path
3. Providing an artifact handler (HFAutoArtifact) that manages the actual model files

Example:
    ```python
    from transformers import AutoModel
    
    # This model will be automatically handled by the HFAutoArtifact system
    model = AutoModel.from_pretrained("bert-base-uncased")
    
    # When serialized, the model files will be saved to the artifacts directory
    # When deserialized, the model will be loaded from the saved files
    ```
    N)Path)Artifact)	to_configc                   @   s<   e Zd ZdZdedededefddZdedefd	d
ZdS )HFAutoArtifacta4  Artifact handler for HuggingFace pretrained model/processor/tokenizer/etc..

    This handler manages the serialization and deserialization of HuggingFace models
    by utilizing their save_pretrained/from_pretrained methods. It saves models to
    an 'artifacts' subdirectory within the specified path.
    valueabsolute_dirrelative_dirreturnc                 C   s&   | t|d  dtt|d  S )a  Save a HuggingFace model to disk.

        Args:
            instance: The HuggingFace model instance to save
            value: Original path value (unused)
            absolute_dir: Absolute path to the save directory
            relative_dir: Relative path from the config file to the save directory

        Returns:
            str: The relative path to the saved model artifacts
        	artifactsz./)save_pretrainedr   str)selfinstancer   r   r    r   V/home/ubuntu/.local/lib/python3.10/site-packages/nemo/lightning/io/artifact/hf_auto.pydump<   s   zHFAutoArtifact.dumppathc                 C   s   |S )zReturn the path to load a HuggingFace model.

        Args:
            path: Path to the saved model artifacts

        Returns:
            Path: The same path, to be used with from_pretrained
        r   )r   r   r   r   r   loadK   s   	zHFAutoArtifact.loadN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   4   s    r   c                  k   sD    t tds	i t_tj }tj|  z	dV  W |t_dS |t_w )a  Context manager for passing additional kwargs to from_pretrained.

    Args:
        **kwargs: Keyword arguments to pass to from_pretrained

    Example:
        with from_pretrained_kwargs(trust_remote_code=True):
            io.load_context("path/to/checkpoint")
    kwargsN)hasattr_localr   copyupdate)r   previousr   r   r   from_pretrained_kwargsW   s   

r   dummyc                 C   s   t tdi }| j|fi |S )a  Factory function for loading HuggingFace pretrained models.

    This function is used as the serialization target for HuggingFace models.
    When deserialized, it will recreate the model using its from_pretrained method.

    Args:
        auto_cls: The HuggingFace model class (e.g., AutoModel, AutoTokenizer)
        pretrained_model_name_or_path: Path to the saved model or model identifier

    Returns:
        The loaded HuggingFace model
    r   )getattrr   from_pretrained)auto_clspretrained_model_name_or_pathr   r   r   r   r!   l   s   r!   c                 C   s2   t |  ot| dddot| dot| dS )Nr    transformersr   r!   )inspectisclassr    
startswithr   )vr   r   r   <lambda>~   s    
r*   c                 C   s   t jt| jddS )a  Convert a HuggingFace model instance to a Fiddle configuration.

    This handler detects HuggingFace model instances by checking for the presence
    of save_pretrained and from_pretrained methods. It converts them to a Fiddle
    configuration that will recreate the model using from_pretrained.

    Args:
        value: A HuggingFace model instance

    Returns:
        fdl.Config: A Fiddle configuration that will recreate the model
    r   )r"   r#   )fdlConfigr!   	__class__)r   r   r   r   handle_hf_pretrained}   s
   r.   r#   )r   )r   
contextlibr&   	threadingpathlibr   fiddler+   nemo.lightning.io.artifactr   nemo.lightning.io.to_configr   localr   r   contextmanagerr   r!   registerr.   __io_artifacts__r   r   r   r   <module>   s$   #


