forked from foundation-model-stack/fms-model-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_patches.py
More file actions
91 lines (75 loc) · 2.89 KB
/
install_patches.py
File metadata and controls
91 lines (75 loc) · 2.89 KB
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
# Standard
import os
import subprocess
dependencies_with_patch = {
"microxcaling": "https://github.com/microsoft/microxcaling.git",
}
def install_with_patch(
pkg_name: str,
repo_url: str,
patch_file: str,
home_dir: str = None,
) -> None:
"""
Install a dependency with a patch file
Args:
pkg_name (str): Name of package being installed
repo_url (str): Github repo URL
patch_file (str): Patch file in patches/<patch_file>
home_dir (str): Home directory with fms-model-optimizer and other packages.
Defaults to None.
"""
# We want to git clone the repo to $HOME/repo_name
if home_dir is None:
home_dir = os.path.expanduser("~")
# Get fms_mo directory in home_dir
cwd = os.getcwd()
# Get patch file location from fms-model-optimizer
patch_file = os.path.join(cwd, "patches", patch_file)
if not os.path.exists(patch_file):
raise FileNotFoundError(f"Can't find {pkg_name} patch file in {cwd}/patches")
# Check to see if package exists in cwd or home_dir
pkg_path_cwd = os.path.join(cwd, pkg_name)
pkg_path_home = os.path.join(home_dir, pkg_name)
pkg_exists_cwd = os.path.exists(pkg_path_cwd)
pkg_exists_home = os.path.exists(pkg_path_home)
# If pkg already exists in cwd or home_dir, skip clone
if pkg_exists_cwd:
pkg_dir = pkg_path_cwd
print(f"Directory {pkg_dir} already exists. Skipping download.")
elif pkg_exists_home:
pkg_dir = pkg_path_home
print(f"Directory {pkg_dir} already exists. Skipping download.")
else:
# Clone repo to home directory
pkg_dir = pkg_path_home
subprocess.run(["git", "clone", repo_url], cwd=home_dir, check=True)
# Apply patch and pip install package
try:
subprocess.run(["git", "apply", "--check", patch_file], cwd=pkg_dir, check=True)
subprocess.run(["git", "apply", patch_file], cwd=pkg_dir, check=True)
print(
f"FMS Model Optimizer patch for {pkg_name} applied. Installing package now."
)
subprocess.run(["pip", "install", "."], cwd=pkg_dir, check=True)
except subprocess.CalledProcessError as e:
print(
f"FMS Model Optimizer patch for {pkg_name} is already installed "
f"or an error has occured: \n{e}"
)
def install_dependencies_with_patch() -> None:
"""
Script to install depenencies that requires a patch prior to pip install.
To execute, use `python install_patches.py`.
Requirements:
1. The patch file is named <package>.patch
2. Patch file must be located in fms-model-optimizer/patches
"""
for pkg, repo_url in dependencies_with_patch.items():
install_with_patch(
pkg_name=pkg,
repo_url=repo_url,
patch_file=pkg + ".patch",
)
if __name__ == "__main__":
install_dependencies_with_patch()