Wauplin HF staff commited on
Commit
7295302
1 Parent(s): 205190d

return to normal commandl ine

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -1,34 +1,46 @@
1
  import pathlib
2
  import tempfile
3
- from typing import Generator
4
 
5
  import gradio as gr
6
  import huggingface_hub
7
  import torch
8
  import yaml
9
- from gradio_logsview.logsview import LogsView, LogsViewRunner
10
- from mergekit.common import parse_kmb
11
  from mergekit.config import MergeConfiguration
12
- from mergekit.merge import run_merge
13
- from mergekit.options import MergeOptions
14
 
15
  has_gpu = torch.cuda.is_available()
16
 
17
- merge_options = (
18
- MergeOptions(
19
- copy_tokenizer=True,
20
- cuda=True,
21
- low_cpu_memory=True,
22
- write_model_card=True,
23
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  if has_gpu
25
- else MergeOptions(
26
- allow_crimes=True,
27
- out_shard_size=parse_kmb("1B"),
28
- lazy_unpickle=True,
29
- write_model_card=True,
30
- )
31
  )
 
32
  ## This Space is heavily inspired by LazyMergeKit by Maxime Labonne
33
  ## https://colab.research.google.com/drive/1obulZ1ROXHjYLn6PPZJwRR6GzgQogxxb
34
 
@@ -78,8 +90,8 @@ examples = [[str(f)] for f in pathlib.Path("examples").glob("*.yml")]
78
 
79
 
80
  def merge(
81
- example_filename: str, yaml_config: str, hf_token: str | None, repo_name: str | None
82
- ) -> Generator[str, None, None]:
83
  if not yaml_config:
84
  raise gr.Error("Empty yaml, pick an example below")
85
  try:
@@ -101,7 +113,9 @@ def merge(
101
  name = "-".join(
102
  model.model.path for model in merge_config.referenced_models()
103
  )
104
- repo_name = f"mergekit-{merge_config.merge_method}-{name}".replace("/", "-").strip("-")
 
 
105
  if len(repo_name) > 50:
106
  repo_name = repo_name[:25] + "-etc-" + repo_name[25:]
107
  runner.log(f"Will save merged in {repo_name} once process is done.")
@@ -111,17 +125,12 @@ def merge(
111
  "No token provided, merge will run in dry-run mode (no upload at the end of the process)."
112
  )
113
 
114
- # Taken from https://github.com/arcee-ai/mergekit/blob/main/mergekit/scripts/run_yaml.py
115
- yield from runner.run_python(
116
- run_merge,
117
- merge_config=merge_config,
118
- out_path=str(merged_path),
119
- options=merge_options,
120
- config_source=str(config_path),
121
- )
122
 
123
  if runner.exit_code != 0:
124
- yield runner.log("Merge failed. Terminating here. No model has been uploaded.")
 
 
125
  return
126
 
127
  if hf_token is not None:
@@ -168,6 +177,6 @@ with gr.Blocks() as demo:
168
  )
169
  gr.Markdown(MARKDOWN_ARTICLE)
170
 
171
- button.click(fn=merge, inputs=[filename, config, token, repo_name], outputs=[logs])
172
 
173
  demo.queue(default_concurrency_limit=1).launch()
 
1
  import pathlib
2
  import tempfile
3
+ from typing import Iterable, List
4
 
5
  import gradio as gr
6
  import huggingface_hub
7
  import torch
8
  import yaml
9
+ from gradio_logsview.logsview import Log, LogsView, LogsViewRunner
 
10
  from mergekit.config import MergeConfiguration
 
 
11
 
12
  has_gpu = torch.cuda.is_available()
13
 
14
+ # Running directly from Python doesn't work well with Gradio+run_process because of:
15
+ # Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method
16
+ # Let's use the CLI instead.
17
+ #
18
+ # import mergekit.merge
19
+ # from mergekit.common import parse_kmb
20
+ # from mergekit.options import MergeOptions
21
+ #
22
+ # merge_options = (
23
+ # MergeOptions(
24
+ # copy_tokenizer=True,
25
+ # cuda=True,
26
+ # low_cpu_memory=True,
27
+ # write_model_card=True,
28
+ # )
29
+ # if has_gpu
30
+ # else MergeOptions(
31
+ # allow_crimes=True,
32
+ # out_shard_size=parse_kmb("1B"),
33
+ # lazy_unpickle=True,
34
+ # write_model_card=True,
35
+ # )
36
+ # )
37
+
38
+ cli = "mergekit-yaml config.yaml merge --copy-tokenizer" + (
39
+ " --cuda --low-cpu-memory"
40
  if has_gpu
41
+ else " --allow-crimes --out-shard-size 1B --lazy-unpickle"
 
 
 
 
 
42
  )
43
+
44
  ## This Space is heavily inspired by LazyMergeKit by Maxime Labonne
45
  ## https://colab.research.google.com/drive/1obulZ1ROXHjYLn6PPZJwRR6GzgQogxxb
46
 
 
90
 
91
 
92
  def merge(
93
+ yaml_config: str, hf_token: str | None, repo_name: str | None
94
+ ) -> Iterable[List[Log]]:
95
  if not yaml_config:
96
  raise gr.Error("Empty yaml, pick an example below")
97
  try:
 
113
  name = "-".join(
114
  model.model.path for model in merge_config.referenced_models()
115
  )
116
+ repo_name = f"mergekit-{merge_config.merge_method}-{name}".replace(
117
+ "/", "-"
118
+ ).strip("-")
119
  if len(repo_name) > 50:
120
  repo_name = repo_name[:25] + "-etc-" + repo_name[25:]
121
  runner.log(f"Will save merged in {repo_name} once process is done.")
 
125
  "No token provided, merge will run in dry-run mode (no upload at the end of the process)."
126
  )
127
 
128
+ yield from runner.run_command(cli.split(), cwd=merged_path)
 
 
 
 
 
 
 
129
 
130
  if runner.exit_code != 0:
131
+ yield runner.log(
132
+ "Merge failed. Terminating here. No model has been uploaded."
133
+ )
134
  return
135
 
136
  if hf_token is not None:
 
177
  )
178
  gr.Markdown(MARKDOWN_ARTICLE)
179
 
180
+ button.click(fn=merge, inputs=[config, token, repo_name], outputs=[logs])
181
 
182
  demo.queue(default_concurrency_limit=1).launch()