
    hX+                         S SK r S SKJr  S SKJr  \" 5       (       a  S SKJr  S SKJrJ	r	  S\
S\
4S jr " S	 S
5      rS\S\
4S jrg)    N)HfApi)is_mergekit_available)MergeConfiguration)MergeOptions	run_mergefolder_pathrepo_idc                     [        5       nUR                  USS9nUR                  U UR                  UR                  S9  g )Nmodel)	repo_type)r   r	   r   )r   create_repoupload_folderr	   r   )r   r	   apirepos       L/home/james-whalen/.local/lib/python3.13/site-packages/trl/mergekit_utils.pyupload_model_to_hfr      sA    
'C??7g?6D ..      c                   \    \ rS rSrSrSS\4S jjrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)MergeConfig'   a  
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                    [        5       (       d  [        S5      eXl        S U l        S U l        US:X  a  SU l        SU l        SU l        g US:X  a.  SU l        / SQU l        SU l        S/U l	        SU l
        SU l        g US:X  a.  SU l        / SQU l        SU l        S/U l	        SU l
        SU l        g US	:X  a  SU l        SU l        g [        S
U 35      e)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__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                     U R                   SU R                  SU R                  0S.U R                  SU R                  0S./S.n[
        R                  " U5      nU$ )zX
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_linear&MergeConfig.create_merge_config_lineari   sf     ZZ$004KcKc@de004KcKc@de
 *889JKr   c                 J   SSU R                   SS.SSS.U R                  U R                  S.S.U R                  SS.SSS.U R                  U R
                  S.S./SU R                  0U R                  SS.SSS.U R                  SSSSS.
n[        R                  " U5      nU$ )	ze
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'   
r1   slicesr2   r0   
base_modelr$   tokenizer_source	tokenizerchat_template	out_dtype
r!   r&   r#   r    r%   r"   r'   r$   r   r3   r4   s      r   create_merge_config_ties$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                 J   SSU R                   SS.SSS.U R                  U R                  S.S.U R                  SS.SSS.U R                  U R
                  S.S./SU R                  0U R                  SS.SSS.U R                  SSSSS.
n[        R                  " U5      nU$ )	zj
Creates a merge configuration for a DARE TIES merge of two models, with specified weights and densities.
r   Nr:   r=   r@   r/   r'   rB   rI   r4   s      r   create_merge_config_dare_ties)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                     SSU R                   SS.SSS.SS./SU R                  0U R                  SS.SSS.U R                  SSSSS.
n[        R
                  " U5      nU$ )zO
Creates a merge configuration for a SLERP merge of a model with a base model.
r   Nr:   r=   r/   trB   )r!   r(   r    r$   r   r3   r4   s      r   create_merge_config_slerp%MergeConfig.create_merge_config_slerp   s     $ +/*@*@d!S $15
 #'	 T]] #'"8"8dK)-
 ZZ $!3
: *889JKr   c                    U R                   S:X  a  U R                  5       $ U R                   S:X  a  U R                  5       $ U R                   S:X  a  U R                  5       $ U R                   S:X  a  U R	                  5       $ g )Nr   r   r   r   )r   r7   rJ   rM   rQ   )r*   s    r   createMergeConfig.create   ss    ;;("2244[[F"0022[[K'5577[[G#1133 $r   )
r$   r   r'   r%   r    r"   r(   r&   r!   r#   N)r   )returnr   )__name__
__module____qualname____firstlineno____doc__strr+   r7   rJ   rM   rQ   rT   __static_attributes__ r   r   r   r   '   s3    BDs D@&*X*X%N4r   r   configout_pathc                     [        5       (       d  [        S5      e[        U U[        S[        R
                  R                  5       SSSS9S9  g)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`.autoTF)devicecudacopy_tokenizerlazy_unpicklelow_cpu_memory)r`   optionsN)r   r   r   r   torchrd   is_available)r_   r`   s     r   merge_modelsrk     sK     !""opp((* 

r   )ri   huggingface_hubr   trl.import_utilsr   mergekit.configr   mergekit.merger   r   r\   r   r   rk   r^   r   r   <module>rp      sT     ! 2 26
C 
# 
\4 \4~  r   