o
    }oiq                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlmZ 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 d dlZd dlmZmZ d dlmZmZ d	Zzd d
l m!Z!m"Z"m#Z# d dl m$Z% d dl&m'Z' W n e(y   dZY nw erd dl)m*Z+ d dl,m-Z-m.Z. d dl/m0Z0 dZ1dZ2G dd deZ3e	G dd dZ4dee5 de5fddZ6de5de!fddZ7dUdeeeee f  fddZ8d d! Z9d"d#dee5 fd$d%Z:d&e5de5fd'd(Z;d)eee5  fd*d+Z<dVd.d/Z=dVd0d1Z>ej?dWd4d5Z@d"ed6 dd#fd7d8ZAdXd:d;ZBdYd<d=ZCed>d?d@e5fdAdBZDdedC fdDdEZEdFe5dGe5deeeF e5f fdHdIZGdJdK ZHdZdLdMZIdNee5ef defdOdPZJdZdQe5dReFddfdSdTZKdS )[    N)	dataclassis_dataclass)Enum)	lru_cache)Path)TYPE_CHECKINGListOptionalTupleTypeUnion)AppStatelogging)is_datastore_pathresolve_cache_dirT)
DictConfig
ListConfig	OmegaConf)errors)versionF)ModelPTPretrainedModelInfo)
NemoConfigzmodel_config.yamlds_itemc                   @   s   e Zd ZdZdZdZdS )ArtifactPathTypea  
    ArtifactPathType refers to the type of the path that the artifact is located at.

    LOCAL_PATH: A user local filepath that exists on the file system.
    TAR_PATH: A (generally flattened) filepath that exists inside of an archive (that may have its own full path).
    r      N)__name__
__module____qualname____doc__
LOCAL_PATHTAR_PATH r"   r"   J/home/ubuntu/.local/lib/python3.10/site-packages/nemo/utils/model_utils.pyr   ;   s    r   c                   @   s8   e Zd ZU dZeed< ejZeed< dZ	e
e ed< dS )ArtifactItem path	path_typeNhashed_path)r   r   r   r&   str__annotations__r   r    r'   r(   r	   r"   r"   r"   r#   r$   G   s   
 r$   namesreturnc                 C   s@   t | dt }t|dksJ d| d|d t}|S )zyDetect model config prefix for a list of file names.

    Useful to identify prefix used within .nemo tarball checkpoint.*r   z/Exactly one model config path expected, found: .r   )fnmatchfilterMODEL_CONFIGlenremovesuffix)r+   model_configprefixr"   r"   r#   detect_prefixN   s   r6   
model_filec              	   C   s   t j| rVt B}t| d#}t| }|j	| t
 |d tt j|t
}W d   n1 s7w   Y  W d   |S W d   |S 1 sOw   Y  |S t j| rhtt j| t
}|S t| )z>Load model config from extracted directory or '.nemo' tarball.r:r&   N)osr&   isfiletempfileTemporaryDirectorytarfileopenr6   getnamesextractr1   r   loadjoinisdirFileNotFoundError)r7   tmptarr5   r4   r"   r"   r#   load_configX   s   (			rH   module_instancesc                 C   sz   d}t | ts| g} d}g }| D ]$}|r"t ||r!|j}t ||snt|dr/|j}t|ds'|| q|s;|d S |S )zBUnwrap model from wrapper classes like Float16Module, for example.TFmoduler   )
isinstancelistrJ   hasattrappend)modelrI   return_listunwrapped_modelmodel_moduler"   r"   r#   unwrap_modelg   s$   




rS   c                 C   s   t | d p	| j S )Nshared)rM   rT   )paramr"   r"   r#   param_is_not_shared}   s   rV   cfgr   c                 C   s   t | v r| t  dur| t  }t|tr|| v r| | S t S |  D ]R\}}t|tttfv rYd}|D ]}t|}tj	
|sHtj	|sHt|rM|d7 }q2 |t|krX|  S q!tj	
t|sotj	t|sott|rs|  S q!dS )a  
    Parses items of the provided sub-config to find the first potential key that
    resolves to an existing file or directory.

    # Fast-path Resolution
    In order to handle cases where we need to resolve items that are not paths, a fastpath
    key can be provided as defined in the global `_VAL_TEST_FASTPATH_KEY`.

    This key can be used in two ways :

    ## _VAL_TEST_FASTPATH_KEY points to another key in the config

    If this _VAL_TEST_FASTPATH_KEY points to another key in this config itself,
    then we assume we want to loop through the values of that key.

    This allows for any key in the config to become a fastpath key.

    Example:
    validation_ds:
        splits: "val"
        ...
        <_VAL_TEST_FASTPATH_KEY>: "splits"  <-- this points to the key name "splits"

    Then we can write the following when overriding in hydra:
    ```python
    python train_file.py ...         model.validation_ds.splits=[val1, val2, dev1, dev2] ...
    ```

    ## _VAL_TEST_FASTPATH_KEY itself acts as the resolved key

    If this _VAL_TEST_FASTPATH_KEY does not point to another key in the config, then
    it is assumed that the items of this key itself are used for resolution.

    Example:
    validation_ds:
        ...
        <_VAL_TEST_FASTPATH_KEY>: "val"  <-- this points to the key name "splits"

    Then we can write the following when overriding in hydra:
    ```python
    python train_file.py ...         model.validation_ds.<_VAL_TEST_FASTPATH_KEY>=[val1, val2, dev1, dev2] ...
    ```

    # IMPORTANT NOTE:
    It <can> potentially mismatch if there exist more than 2 valid paths, and the
    first path does *not* resolve the the path of the data file (but does resolve to
    some other valid path).

    To avoid this side-effect, place the data path as the first item on the config file.

    Args:
        cfg: DictConfig (Sub-config) that should be parsed.

    Returns:
        A str representing the `key` of the config which hosts the filepath(s),
        or None in case path could not be resolved.
    Nr   r   )_VAL_TEST_FASTPATH_KEYrK   r)   itemstyperL   tupler   r:   r&   existsrD   r   r2   )rW   fastpath_keykeyvaluevalues_are_pathsval_ir"   r"   r#   resolve_dataset_name_from_cfg   s(   < 
,rb   namec                 C   s   t jt| st jt| stt| rt| j} nt| } | dd} d| v r0| dd} d| v r:| dd} | dkrBt	dd| d krL| d } | S )a;  
    Constructs a valid prefix-name from a provided file path.

    Args:
        name: str path to some valid data/manifest file or a python object that
            will be used as a name for the data loader (via str() cast).

    Returns:
        str prefix used to identify uniquely this data/manifest file.
    -_manifestr%   dataseta"  Provided dataset / manifest filename was `manifest.json` or `dataset.json`.
Such a name is invalid, since multiple datasets/manifests can share the same name,
thereby overriding their results during logging. Please pick a more discriptive filename 
for the provided dataset / manifest file.)
r:   r&   r\   r)   rD   r   r   stemreplace
ValueErrorrc   r"   r"   r#   parse_dataset_as_name   s   ,rm   	name_listc                 C   sD   | du rdS t  }| D ]}||v rtd|  q|| qdS )z
    Performs a uniqueness check on the name list resolved, so that it can warn users
    about non-unique keys.

    Args:
        name_list: List of strings resolved for data loaders.
    NzName resolution has found more than one data loader having the same name !
In such cases, logs will nor be properly generated. Please rename the item to have unique names.
Resolved name : )setr   warningadd)rn   r+   rc   r"   r"   r#   unique_names_check  s   rr   rO   r   c              	   C   s  t std td t| j}g }d|jv r*t	|}|d 
d}t|}nd}|| _t|j}|du s<|dk rMtd|j | |j dS |j| }t|tttfr|D ]}t|ttfri||_n||j|< | |j || j q\|| _t|dkrt|d ttfrdd	 |D | _n:|jd
g }t|dkrt|t|krtdt| dt| d| d| dd	 |D | _ndd	 |D | _t| jd dS | |j |jd
d}|durt|tstd| t|g| _nt|g| _t| jd dS )a  
    Helper method that operates on the ModelPT class to automatically support
    multiple dataloaders for the validation set.

    It does so by first resolving the path to one/more data files via `resolve_dataset_name_from_cfg()`.
    If this resolution fails, it assumes the data loader is prepared to manually support / not support
    multiple data loaders and simply calls the appropriate setup method.

    If resolution succeeds:
        Checks if provided path is to a single file or a list of files.
        If a single file is provided, simply tags that file as such and loads it via the setup method.
        If multiple files are provided:
            Inject a new manifest path at index "i" into the resolved key.
            Calls the appropriate setup method to set the data loader.
            Collects the initialized data loader in a list and preserves it.
            Once all data loaders are processed, assigns the list of loaded loaders to the ModelPT.
            Finally assigns a list of unique names resolved from the file paths to the ModelPT.

    Args:
        model: ModelPT subclass, which requires >=1 Validation Dataloaders to be setup.
    @This function requires Hydra/Omegaconf and it was not installed.r   
val_dl_idxvalidation_dsr   N_Could not resolve file path from provided config - {}. Disabling support for multi-dataloaders.c                 S      g | ]}|j qS r"   rl   .0dsr"   r"   r#   
<listcomp>_      z2resolve_validation_dataloaders.<locals>.<listcomp>rc   Number of names (%) does not match number of datasets (). Got  and c                 S      g | ]}t |qS r"   rm   ry   nr"   r"   r#   r{   h      c                 S   r   r"   r   rx   r"   r"   r#   r{   j  r   rn   1`name` must be a string for single manifest, got ) 
_HAS_HYDRAr   errorexitcopydeepcopy_cfgru   r   to_containerpopcreate_val_dl_idxrb   debugformatsetup_validation_datarK   rL   r[   r   dictr   rN   _validation_dlr2   _validation_namesgetrk   rr   r)   rm   )rO   rW   dataloadersrt   ds_key	ds_valuesds_valueds_namesr"   r"   r#   resolve_validation_dataloaders  sl   






r   c              	   C   s  t std td t| j}g }d|jv r*t	|}|d 
d}t|}nd}|| _t|j}|du rItd|j | |j dS |j| }t|tttfr|D ]}t|ttfre||_n||j|< | |j || j qX|| _t|dkrt|d ttfrdd	 |D | _n:|jd
g }t|dkrt|t|krtdt| dt| d| d| dd	 |D | _ndd	 |D | _t| jd dS | |j |jd
d}|durt|tstd| t|g| _nt|g| _t| jd dS )a  
    Helper method that operates on the ModelPT class to automatically support
    multiple dataloaders for the test set.

    It does so by first resolving the path to one/more data files via `resolve_dataset_name_from_cfg()`.
    If this resolution fails, it assumes the data loader is prepared to manually support / not support
    multiple data loaders and simply calls the appropriate setup method.

    If resolution succeeds:
        Checks if provided path is to a single file or a list of files.
        If a single file is provided, simply tags that file as such and loads it via the setup method.
        If multiple files are provided:
            Inject a new manifest path at index "i" into the resolved key.
            Calls the appropriate setup method to set the data loader.
            Collects the initialized data loader in a list and preserves it.
            Once all data loaders are processed, assigns the list of loaded loaders to the ModelPT.
            Finally assigns a list of unique names resolved from the file paths to the ModelPT.

    Args:
        model: ModelPT subclass, which requires >=1 Test Dataloaders to be setup.
    rs   r   test_dl_idxtest_dsr   Nrv   c                 S   rw   r"   rl   rx   r"   r"   r#   r{     r|   z,resolve_test_dataloaders.<locals>.<listcomp>rc   r}   r~   r   r   c                 S   r   r"   r   r   r"   r"   r#   r{     r   c                 S   r   r"   r   rx   r"   r"   r#   r{     r   r   r   ) r   r   r   r   r   r   r   r   r   r   r   r   _test_dl_idxrb   r   r   setup_test_datarK   rL   r[   r   r   r   rN   _test_dlr2   _test_namesr   rk   rr   r)   rm   )rO   rW   r   r   r   r   r   r   r"   r"   r#   resolve_test_dataloaders{  sl   






r   instancepl.LightningModulec                 C   sD   | |i |}t |tr |d ur d|v r |d}|j|dd |S )NlogT)on_step)rK   r   r   log_dict)wrappedr   argskwargsoutput_dictr   r"   r"   r#   wrap_training_step  s
   
r   )r   r   c                 C   sp   t std td t| ttfst| rt| } t| ts*t	dt
|  dtj| dd}t|}|S )z
    Converts its input into a standard DictConfig.
    Possible input values are:
    -   DictConfig
    -   A dataclass which is a subclass of NemoConfig

    Args:
        cfg: A dict-like object.

    Returns:
        The equivalent DictConfig
    rs   r   zAcfg constructor argument must be of type DictConfig/dict but got z	 instead.T)resolve)r   r   r   r   rK   r   r   r   
structuredrk   rZ   r   r   )rW   configr"   r"   r#   #convert_model_config_to_dict_config  s   



r   r   c              
   C   s   t std td d| v rd| vr| d| _d| v r/| d}| D ]\}}|| |< q&z|  D ]\}}t|trAt	| q4W d	S  t
jyd } ztd|  d| d W Y d	}~d	S d	}~ww )
zURecursive function convertint the configuration from old hydra format to the new one.rs   r   cls_target_paramsz)Skipped conversion for config/subconfig:
z

 Reason: r.   N)r   r   r   r   r   r   rY   rK   r   _convert_configomegaconf_errorsOmegaConfBaseExceptionrp   )rW   r   	param_key	param_valre   sub_cfger"   r"   r#   r     s&   



&r   c                 C   s   t std td | dur*t| ts*z	t| }|} W n tj	y)   |  Y S w t
| } t| d t|  t| d | S )a  
    Recursively convert Hydra 0.x configs to Hydra 1.x configs.

    Changes include:
    -   `cls` -> `_target_`.
    -   `params` -> drop params and shift all arguments to parent.
    -   `target` -> `_target_` cannot be performed due to ModelPT injecting `target` inside class.

    Args:
        cfg: Any Hydra compatible DictConfig

    Returns:
        An updated DictConfig that conforms to Hydra 1.x format.
    rs   r   NFT)r   r   r   r   rK   r   r   r   r   r   r   r   
set_structr   )rW   temp_cfgr"   r"   r#   maybe_update_config_version  s   


r   i   )maxsizer&   c                 C   s@   |  d}d|dd } |d }t| |gd}t||}|S )z3
    Recursive import of class by path string.
    r.   Nrh   )fromlist)splitrC   
__import__getattr)r&   paths
class_namemodr"   r"   r#   import_class_by_pathA  s   

r   r   c                    s,   t    fdd|  tt   S )a  
    Recursively traverses the inheritance graph of subclasses to extract all pretrained model info.
    First constructs a set of unique pretrained model info by performing DFS over the inheritance graph.
    All model info belonging to the same class is added together.

    Args:
        base_class: The root class, whose subclass graph will be traversed.

    Returns:
        A list of unique pretrained model infos belonging to all of the inherited subclasses of
        this baseclass.
    c                    sd   |   D ]+}| | }|d ur/t|dkr/|D ]
}|jd u r$||_q|D ]} | q'qd S )Nr   )__subclasses__list_available_modelsr2   class_rq   )r   subclasssubclass_models
model_infolist_of_modelsrecursive_subclass_walkr"   r#   r   ]  s   
zGresolve_subclass_pretrained_model_info.<locals>.recursive_subclass_walk)ro   rL   sorted)
base_classr"   r   r#   &resolve_subclass_pretrained_model_infoN  s
   r   lib_namechecked_versionc              
   C   s   zNd| v r
t | }nt| }t|drDt|j}t|}|||r/d|  d}d|fW S d|  d| d|j d| d		}d
|fW S d|  d}d
|fW S  tt	t
fyZ   Y nw d|  d}d|fS )a  
    Checks if a library is installed, and if it is, checks the operator(lib.__version__, checked_version) as a result.
    This bool result along with a string analysis of result is returned.

    If the library is not installed at all, then returns None instead, along with a string explaining
    that the library is not installed

    Args:
        lib_name: lower case str name of the library that must be imported.
        checked_version: semver string that is compared against lib.__version__.
        operator: binary callable function func(a, b) -> bool; that compares lib.__version__ against version in
            some manner. Must return a boolean.

    Returns:
        A tuple of results:
        -   Bool or None. Bool if the library could be imported, and the result of
            operator(lib.__version__, checked_version) or False if __version__ is not implemented in lib.
            None is passed if the library is not installed at all.
        -   A string analysis of the check.
    r.   __version__zLib z version is satisfied !Tz
 version (z	) is not z than required version zI.
Please upgrade the lib using either pip or conda to the latest version.FzX does not implement __version__ in its init file. Could not check version compatibility.zI has not been installed. Please use pip or conda to install this package.N)r   	importlibimport_modulerM   r   Versionr   r   AttributeErrorImportErrorModuleNotFoundError)r   r   operatorr   lib_ver	match_vermsgr"   r"   r#   check_lib_versionu  s.   








r   c                    sT   t   t fdddD r(tjtj }tj }tj||  S  S )Nc                    s   g | ]}| v r|qS r"   r"   )ry   sfilepathr"   r#   r{     s    z0uninject_model_parallel_rank.<locals>.<listcomp>)mp_ranktp_rank
fsdp_shard)r)   anyr:   r&   dirnamebasenamerC   )r   r   r   r"   r   r#   uninject_model_parallel_rank  s   r   c                 C   s   t | } t }tj| }tj| }|jdurU|jdkrU|r&d|jdnd}|jdu s2|jdkrB| d|j	d| d| } | S | d	|j	dd
|j
dd| } | S |r^d|jdnd}| | d| S )zz
    Injects tensor/pipeline model parallel ranks into the filepath.
    Does nothing if not using model parallelism.
    Nr   _fsdp_shard_05dr%   z	/mp_rank_02d/z	/tp_rank_	_pp_rank_03dz/fsdp_shard_)r   r   r:   r&   r   r   model_parallel_sizedata_parallel_rankpipeline_model_parallel_sizetensor_model_parallel_rankpipeline_model_parallel_rank)r   fsdp_sharded_ckpt	app_stater   r   r   r"   r"   r#   inject_model_parallel_rank  s   "r   r   c                 C   sH   t | } | jdkr|  r| S | jdksJ d|  d| | j}|S )zPTL considers checkpoints as .ckpt files.
    This method removes the extension and returns a path
    to be used as a directory for distributed checkpoints
    z.ckptz
filepath: z must have .ckpt extension)r   suffixis_dir	with_nameri   )r   checkpoint_dirr"   r"   r#   ckpt_to_dir  s   r   
output_diruse_abspathc                 C   s  t  }|j}t| j}|jjdkr| j| t| ds0t|dr.t	
|jtj|d dS tj|r?tj}|dd}ntj|rKtj}i }nt||d
i |f}|durat| }	| j D ]N\}
}|jd\}}tj||
}|dur|j|	 | |d	 ttj||| nttj||| |rtj|ntj|}t	||
| qfW d   n1 sw   Y  t|drt	
|jtj|d dS dS )zJSave all model artifacts and tokenizer config to a given output directory.huggingface	artifacts	tokenizerztokenizer_config.yamlNr8   )rc   modeznemo:r9   r"   ) r   model_restore_pathr   r   rW   r  librarysave_pretrainedrM   r   saver:   r&   rC   r;   r>   r?   rD   
contextlibnullcontextrE   r6   r@   r  rY   r   rA   renameshutilabspathr   update)rO   r  r  r   r7   	model_cfgmodel_file_handlerr   	maybe_tarr5   	arti_name	arti_itemre   	arti_file	arti_pathr"   r"   r#   save_artifacts  sB   


r  )N)rO   r   )r   r   )rW   r   )rW   r   )F)Lr  r   r/   r   r:   r  r>   r<   dataclassesr   r   enumr   	functoolsr   pathlibr   typingr   r   r	   r
   r   r   wrapt
nemo.utilsr   r   nemo.utils.data_utilsr   r   r   	omegaconfr   r   r   r   r   	packagingr   r   lightning.pytorchpytorchplnemo.core.classesr   r   nemo.core.config.modelPTr   r1   rX   r   r$   r)   r6   rH   rS   rV   rb   rm   rr   r   r   	decoratorr   r   r   r   r   r   boolr   r   r   r   r  r"   r"   r"   r#   <module>   sn    
 Z(

_`


'"'6
