-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathutils.py
221 lines (190 loc) · 6.11 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2024 Advanced Micro Devices, Inc.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import logging
import multiprocessing
import os
from pathlib import Path
import subprocess
import sys
import time
import requests
from transformers import AutoTokenizer
logger = logging.getLogger(__name__)
class AccuracyValidationException(RuntimeError):
pass
def download_huggingface_model(local_dir, repo_id, model_file):
model_path = local_dir / model_file
logger.info(f"Preparing model_path: {model_path}..")
if not os.path.exists(model_path):
logger.info(f"Downloading model {repo_id} {model_file} from Hugging Face...")
subprocess.run(
f"huggingface-cli download --local-dir {local_dir} {repo_id} {model_file}",
shell=True,
check=True,
)
logger.info(f"Model downloaded to {model_path}")
else:
logger.info("Using cached model")
def download_with_hf_datasets(local_dir: Path | str, model_name: str):
"""Download a model using `sharktank.utils.hf_datasets` script.
Args:
local_dir (Path | str): Local directory to download model to.
model_name (str): Name of model to download.
"""
if isinstance(local_dir, Path):
local_dir = str(local_dir)
logger.info(f"Download model {model_name} with `hf_datasets` to {local_dir}...")
subprocess.run(
[
"python",
"-m",
"sharktank.utils.hf_datasets",
model_name,
"--local-dir",
local_dir,
],
check=True,
)
logger.info(f"Model {model_name} successfully downloaded.")
def download_tokenizer(local_dir, tokenizer_id):
# Set up tokenizer if it doesn't exist
tokenizer_path = local_dir / "tokenizer.json"
logger.info(f"Preparing tokenizer_path: {tokenizer_path}...")
if not os.path.exists(tokenizer_path):
logger.info(f"Downloading tokenizer {tokenizer_id} from Hugging Face...")
tokenizer = AutoTokenizer.from_pretrained(
tokenizer_id,
)
tokenizer.save_pretrained(local_dir)
logger.info(f"Tokenizer saved to {tokenizer_path}")
else:
logger.info("Using cached tokenizer")
def export_paged_llm_v1(mlir_path, config_path, model_path, batch_sizes):
bs_string = ",".join(map(str, batch_sizes))
logger.info(
"Exporting model with following settings:\n"
f" MLIR Path: {mlir_path}\n"
f" Config Path: {config_path}\n"
f" Batch Sizes: {bs_string}"
)
subprocess.run(
[
"python",
"-m",
"sharktank.examples.export_paged_llm_v1",
"--block-seq-stride=16",
f"--{model_path.suffix.strip('.')}-file={model_path}",
f"--output-mlir={mlir_path}",
f"--output-config={config_path}",
f"--bs={bs_string}",
],
check=True,
)
logger.info(f"Model successfully exported to {mlir_path}")
def compile_model(mlir_path, vmfb_path, device_settings):
logger.info(f"Compiling model to {vmfb_path}")
subprocess.run(
[
"iree-compile",
mlir_path,
"-o",
vmfb_path,
]
+ device_settings["device_flags"],
check=True,
)
logger.info(f"Model successfully compiled to {vmfb_path}")
def find_available_port():
import socket
from contextlib import closing
logger.info(f"Finding available port...")
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = s.getsockname()[1]
logger.info(f"Found available port: {port}")
return port
def wait_for_server(url, timeout=10):
logger.info(f"Waiting for server to start at {url}...")
start = time.time()
while time.time() - start < timeout:
try:
requests.get(f"{url}/health")
logger.info("Server successfully started")
return
except requests.exceptions.ConnectionError:
time.sleep(1)
raise TimeoutError(f"Server did not start within {timeout} seconds")
def _start_llm_server_args(
tokenizer_path,
model_config_path,
vmfb_path,
parameters_path,
settings,
port,
):
return [
sys.executable,
"-m",
"shortfin_apps.llm.server",
f"--tokenizer_json={tokenizer_path}",
f"--model_config={model_config_path}",
f"--vmfb={vmfb_path}",
f"--parameters={parameters_path}",
f"--device={settings['device']}",
f"--port={port}",
]
def start_llm_server(
port,
tokenizer_path,
model_config_path,
vmfb_path,
parameters_path,
settings,
timeout=10,
multi=False,
):
logger.info("Starting LLM server...")
if multi:
server_process = multiprocessing.Process(
target=subprocess.Popen(
_start_llm_server_args(
tokenizer_path,
model_config_path,
vmfb_path,
parameters_path,
settings,
port,
),
)
)
server_process.start()
else:
# Start the server
server_process = subprocess.Popen(
_start_llm_server_args(
tokenizer_path,
model_config_path,
vmfb_path,
parameters_path,
settings,
port,
)
)
logger.info("Process started... waiting for server")
# Wait for server to start
wait_for_server(f"http://localhost:{port}", timeout)
return server_process
def start_log_group(headline):
# check if we are in github ci
if os.environ.get("GITHUB_ACTIONS") == "true":
return f"\n::group::{headline}"
return ""
def end_log_group():
# check if we are in github ci
if os.environ.get("GITHUB_ACTIONS") == "true":
return "\n::endgroup::"
return ""