julien-c HF staff commited on
Commit
796d506
β€’
1 Parent(s): c4bb746

initial import

Browse files
Files changed (4) hide show
  1. .vscode/settings.json +3 -0
  2. README.md +2 -2
  3. app.py +136 -0
  4. requirements.txt +2 -0
.vscode/settings.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true
3
+ }
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Mergekit Gui
3
  emoji: πŸ†
4
  colorFrom: yellow
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 4.24.0
8
  app_file: app.py
 
1
  ---
2
+ title: mergekit-gui
3
  emoji: πŸ†
4
  colorFrom: yellow
5
+ colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.24.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pathlib
2
+ import subprocess
3
+ import tempfile
4
+ from typing import Generator
5
+ import gradio as gr
6
+ import huggingface_hub
7
+ import torch
8
+ import yaml
9
+
10
+ has_gpu = torch.cuda.is_available()
11
+
12
+ cli = "mergekit-yaml config.yaml merge --copy-tokenizer" + (
13
+ " --cuda --low-cpu-memory"
14
+ if has_gpu
15
+ else " --allow-crimes --out-shard-size 1B --lazy-unpickle"
16
+ )
17
+
18
+ print(cli)
19
+
20
+ ## This Space is heavily inspired by LazyMergeKit by Maxime Labonne
21
+ ## https://colab.research.google.com/drive/1obulZ1ROXHjYLn6PPZJwRR6GzgQogxxb
22
+
23
+
24
+ MARKDOWN_DESCRIPTION = """
25
+ # mergekit-gui
26
+
27
+ The fastest way to perform a model merge πŸ”₯
28
+
29
+ Specify a YAML configuration file (see examples below) and a HF token and this app will perform the merge and upload the merged model to your user profile.
30
+ """
31
+
32
+ MARKDOWN_ARTICLE = """
33
+ ___
34
+
35
+ ## Merge Configuration
36
+
37
+ [Mergekit](https://github.com/arcee-ai/mergekit) configurations are YAML documents specifying the operations to perform in order to produce your merged model.
38
+ Below are the primary elements of a configuration file:
39
+
40
+ - `merge_method`: Specifies the method to use for merging models. See [Merge Methods](https://github.com/arcee-ai/mergekit#merge-methods) for a list.
41
+ - `slices`: Defines slices of layers from different models to be used. This field is mutually exclusive with `models`.
42
+ - `models`: Defines entire models to be used for merging. This field is mutually exclusive with `slices`.
43
+ - `base_model`: Specifies the base model used in some merging methods.
44
+ - `parameters`: Holds various parameters such as weights and densities, which can also be specified at different levels of the configuration.
45
+ - `dtype`: Specifies the data type used for the merging operation.
46
+ - `tokenizer_source`: Determines how to construct a tokenizer for the merged model.
47
+
48
+ ## Merge Methods
49
+
50
+ A quick overview of the currently supported merge methods:
51
+
52
+ | Method | `merge_method` value | Multi-Model | Uses base model |
53
+ | -------------------------------------------------------------------------------------------- | -------------------- | ----------- | --------------- |
54
+ | Linear ([Model Soups](https://arxiv.org/abs/2203.05482)) | `linear` | βœ… | ❌ |
55
+ | SLERP | `slerp` | ❌ | βœ… |
56
+ | [Task Arithmetic](https://arxiv.org/abs/2212.04089) | `task_arithmetic` | βœ… | βœ… |
57
+ | [TIES](https://arxiv.org/abs/2306.01708) | `ties` | βœ… | βœ… |
58
+ | [DARE](https://arxiv.org/abs/2311.03099) [TIES](https://arxiv.org/abs/2306.01708) | `dare_ties` | βœ… | βœ… |
59
+ | [DARE](https://arxiv.org/abs/2311.03099) [Task Arithmetic](https://arxiv.org/abs/2212.04089) | `dare_linear` | βœ… | βœ… |
60
+ | Passthrough | `passthrough` | ❌ | ❌ |
61
+ | [Model Stock](https://arxiv.org/abs/2403.19522) | `model_stock` | βœ… | βœ… |
62
+
63
+ """
64
+
65
+ examples = [[f.name, f.read_text()] for f in pathlib.Path("examples").glob("*.yml")]
66
+
67
+
68
+ def merge(
69
+ example_filename: str, yaml_config: str, hf_token: str, repo_name: str
70
+ ) -> Generator[str, None, None]:
71
+ output = ""
72
+ if not yaml_config:
73
+ raise gr.Error("Empty yaml, pick an example below")
74
+ try:
75
+ _ = yaml.safe_load(yaml_config)
76
+ except:
77
+ raise gr.Error("Invalid yaml")
78
+
79
+ with tempfile.TemporaryDirectory() as tmpdirname:
80
+ tmpdir = pathlib.Path(tmpdirname)
81
+ output += f"About to start merging in directory {tmpdir}\n\n"
82
+ yield output
83
+ with open(tmpdir / "config.yaml", "w", encoding="utf-8") as f:
84
+ f.write(yaml_config)
85
+ output += cli + "\n\n"
86
+ yield output
87
+
88
+ cmd = cli.split()
89
+ popen = subprocess.Popen(
90
+ cmd,
91
+ cwd=tmpdir,
92
+ stdout=subprocess.PIPE,
93
+ stderr=subprocess.STDOUT,
94
+ universal_newlines=True,
95
+ )
96
+ for stdout_line in iter(popen.stdout.readline, ""):
97
+ output += stdout_line
98
+ yield output
99
+ popen.stdout.close()
100
+ return_code = popen.wait()
101
+ if return_code:
102
+ raise gr.Error(f"subprocess error: {return_code}")
103
+
104
+
105
+ demo = gr.Interface(
106
+ description=MARKDOWN_DESCRIPTION,
107
+ article=MARKDOWN_ARTICLE,
108
+ fn=merge,
109
+ inputs=[
110
+ gr.Textbox(visible=False, label="filename"),
111
+ gr.Code(
112
+ language="yaml",
113
+ lines=10,
114
+ label="config.yaml",
115
+ ),
116
+ gr.Textbox(
117
+ lines=1,
118
+ label="HF Write Token",
119
+ info="https://hf.co/settings/token",
120
+ type="password",
121
+ placeholder="optional, will not upload merge if empty (dry-run)",
122
+ ),
123
+ gr.Textbox(
124
+ lines=1,
125
+ label="Repo name",
126
+ placeholder="optional, will create a random name if empty",
127
+ ),
128
+ ],
129
+ outputs=gr.Textbox(label="output", lines=12, show_copy_button=True),
130
+ allow_flagging="never",
131
+ submit_btn="Merge",
132
+ examples=examples,
133
+ ).queue(default_concurrency_limit=1)
134
+
135
+
136
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ git+https://github.com/arcee-ai/mergekit.git