
    bi=+                         d dl Z d dlmZ d dlmZ  e       rd dlmZ d dlmZm	Z	 de
de
fdZ G d	 d
      Zdede
fdZy)    N)HfApi)is_mergekit_available)MergeConfiguration)MergeOptions	run_mergefolder_pathrepo_idc                     t               }|j                  |d      }|j                  | |j                  |j                         y )Nmodel)	repo_type)r   r	   r   )r   create_repoupload_folderr	   r   )r   r	   apirepos       M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/trl/mergekit_utils.pyupload_model_to_hfr      sA    
'C??7g?6D ..      c                   F    e Zd ZdZd
defdZddZddZddZddZ	ddZ
y	)MergeConfiga  
    Configuration class for merging two models using `mergekit`.

    This class provides a structured way to configure and generate merge configurations for various merge methods, such
    as `linear`, `ties`, `dare_ties`, and `slerp`.

    Args:
        method (`str`, *optional*, defaults to `"linear"`):
            Merge method to use. Supported methods include:

            - `"linear"`: Linearly combines two models with specified weights.
            - `"ties"`: Combines two models using the TIES method with density parameters.
            - `"dare_ties"`: A variant of TIES for domain adaptation.
            - `"slerp"`: Combines models using spherical linear interpolation.

    Note:

        For more details about the merge methods and how they are implemented, see the [MergeKit GitHub
        repository](https://github.com/arcee-ai/mergekit?tab=readme-ov-file#merge-methods).

    Attributes:
        method (`str`): The merge method to use.
        policy_model_path (`str` or `None`): Path to the policy model.
        target_model_path (`str` or `None`): Path to the target model.
        policy_model_weight (`float`): Weight for the policy model (for `linear` and `ties` methods).
        target_model_weight (`float`): Weight for the target model (for `linear` and `ties` methods).
        policy_model_density (`list[float]`): Density parameters for the policy model (for `ties` and `dare_ties`).
        target_model_density (`list[float]`): Density parameters for the target model (for `ties` and `dare_ties`).
        normalize (`float` or `None`): Normalization factor for the TIES method.
        t_values (`float` or `None`): Interpolation factor for the SLERP method.
        dtype (`str`): Data type to use for merging, e.g., `"float16"`.
    methodc                    t               st        d      || _        d | _        d | _        |dk(  rd| _        d| _        d| _        y |dk(  r.d| _        g d| _        d| _        dg| _	        d| _
        d| _        y |dk(  r.d| _        g d| _        d| _        dg| _	        d| _
        d| _        y |d	k(  rd| _        d| _        y t        d
|       )NzRMergeConfig requires the `mergekit` extra. To install, run `pip install mergekit`.linearg      ?float16ties      ?)r   gffffff?g?	dare_tiesslerpzUnsupported merge method: )r   ImportErrorr   policy_model_pathtarget_model_pathpolicy_model_weighttarget_model_weightdtypepolicy_model_densitytarget_model_density	normalizet_values
ValueError)selfr   s     r   __init__zMergeConfig.__init__I   s    $&rss!%!% X'*D$'*D$"DJv'*D$(7D%'*D$),D% DN"DJ{"'*D$(7D%'*D$),D% DN"DJwDM"DJ9&BCCr   c                     | j                   d| j                  d| j                  id| j                  d| j                  idgd}t        j                  |      }|S )zh
        Creates a merge configuration for a linear merge of two models with specified weights.
        r   weightr   
parameters)r#   merge_methodmodels)r#   r   r!   r    r"   r   model_validater)   merge_config_dictmerge_configs      r   create_merge_config_linearz&MergeConfig.create_merge_config_lineari   sf     ZZ$004KcKc@de004KcKc@de
 *889JKr   c                 H   dd| j                   ddddd| j                  | j                  dd| j                  ddddd| j                  | j
                  ddgd| j                  i| j                  ddddd| j                  ddddd
}t        j                  |      }|S )	zu
        Creates a merge configuration for a TIES merge of two models, with specified weights and densities.
        r   Npathrevisionr   loraoverride_architecturedensityr,   r-   r&   
r/   slicesr0   r.   
base_modelr#   tokenizer_source	tokenizerchat_template	out_dtype
r    r%   r"   r   r$   r!   r&   r#   r   r1   r2   s      r   create_merge_config_tiesz$MergeConfig.create_merge_config_ties|   s     # +/*@*@d!S $15
 /3.G.GSWSkSk"l +/*@*@d!S $15
 /3.G.GSWSkSk"l$ '7"&"8"8dK)-
 ZZ $!? 
F *889JKr   c                 H   dd| j                   ddddd| j                  | j                  dd| j                  ddddd| j                  | j
                  ddgd| j                  i| j                  ddddd| j                  ddddd
}t        j                  |      }|S )	zz
        Creates a merge configuration for a DARE TIES merge of two models, with specified weights and densities.
        r   Nr7   r:   r=   r-   r&   r?   rF   r2   s      r   create_merge_config_dare_tiesz)MergeConfig.create_merge_config_dare_ties   s     ( +/*@*@d!S $15
 /3.G.GSWSkSk"l +/*@*@d!S $15
 /3.G.GSWSkSk"l$ '7"&"8"8dK)-
 ZZ $!? 
F *889JKr   c                     dd| j                   dddddddgd| j                  i| j                  ddddd| j                  ddddd
}t	        j
                  |      }|S )z_
        Creates a merge configuration for a SLERP merge of a model with a base model.
        r   Nr7   r:   r-   tr?   )r    r'   r   r#   r   r1   r2   s      r   create_merge_config_slerpz%MergeConfig.create_merge_config_slerp   s     $ +/*@*@d!S $15
 #'	 T]] #'"8"8dK)-
 ZZ $!3
: *889JKr   c                     | j                   dk(  r| j                         S | j                   dk(  r| j                         S | j                   dk(  r| j                         S | j                   dk(  r| j	                         S y )Nr   r   r   r   )r   r5   rG   rI   rL   )r)   s    r   createzMergeConfig.create   ss    ;;("2244[[F"0022[[K'5577[[G#1133 $r   N)r   )returnr   )__name__
__module____qualname____doc__strr*   r5   rG   rI   rL   rN    r   r   r   r   '   s3    BDs D@&*X*X%N4r   r   configout_pathc           
          t               st        d      t        | |t        t        j
                  j                         ddd             y)z
    Merge two models using mergekit

    Args:
        config (`MergeConfig`): The merge configuration.
        out_path (`str`): The output path for the merged model.
    zSmerge_models requires the `mergekit` extra. To install, run `pip install mergekit`.TF)cudacopy_tokenizerlazy_unpicklelow_cpu_memory)rW   optionsN)r   r   r   r   torchrY   is_available)rV   rW   s     r   merge_modelsr`     sE     !"opp((* 	
	r   )r^   huggingface_hubr   trl.import_utilsr   mergekit.configr   mergekit.merger   r   rT   r   r   r`   rU   r   r   <module>re      sQ     ! 2 26
C 
# 
\4 \4~  r   