
!git clone https://github.com/cg123/mergekit.git
%cd mergekit
%pip install -e .
slices:
- sources:
- model: Huggingface model1
layer_range: [0, 48]
- model: Huggingface model2
layer_range: [0, 48]
merge_method: slerp
base_model: Huggingface model1
parameters:
t:
- filter: self_attn
value: [0, 0.5, 0.3, 0.7, 1]
- filter: mlp
value: [1, 0.5, 0.7, 0.3, 0]
- value: 0.5 # fallback for rest of tensors
dtype: float16
Linear : 가중치 평균을 통해 병합. 정규화 기능 지원, 다중 모델을 지원하여 2개 이상의 LLM 모델 결합 가능
SLERP : 두 모델의 파라미터를 구면 보간을 하는 방식으로 병합, 2개의 모델 병합만 지원
구면 보간 : 데이터 포인트를 구면상의 점으로 간주하여, 두 점 사이를 구면 상의 최단 경로로 보간하는 방식
Ties : 작업 벡터를 희소화하고 부호 합의 알고리즘을 적용하여 모델 간 간섭을 해결하여, 더 많은 수의 모델을 병합하고 더 많은 강점을 유지
OUTPUT_PATH = "./merged" # folder to store the result in
LORA_MERGE_CACHE = "/tmp" # change if you want to keep these for some reason
CONFIG_YML = "./examples/gradient-slerp.yml" # merge configuration file
COPY_TOKENIZER = True # you want a tokenizer? yeah, that's what i thought
LAZY_UNPICKLE = False # experimental low-memory model loader
LOW_CPU_MEMORY = False # enable if you somehow have more VRAM than RAM+swap
# actually do merge
import torch
import yaml
from mergekit.config import MergeConfiguration
from mergekit.merge import MergeOptions, run_merge
with open(CONFIG_YML, "r", encoding="utf-8") as fp:
merge_config = MergeConfiguration.model_validate(yaml.safe_load(fp))
run_merge(
merge_config,
out_path=OUTPUT_PATH,
options=MergeOptions(
lora_merge_cache=LORA_MERGE_CACHE,
cuda=torch.cuda.is_available(),
copy_tokenizer=COPY_TOKENIZER,
lazy_unpickle=LAZY_UNPICKLE,
low_cpu_memory=LOW_CPU_MEMORY,
),
)
print("Done!")