forked from kingoflolz/mesh-transformer-jax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ray_tpu.py
165 lines (121 loc) · 3.95 KB
/
ray_tpu.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
import functools
import os
import subprocess
import time
import glob
import requests
from fabric import Connection
@functools.lru_cache()
def get_bearer():
return subprocess.check_output("gcloud auth print-access-token", shell=True).decode("utf-8").strip()
@functools.lru_cache()
def get_project():
return subprocess.check_output("gcloud config list --format 'value(core.project)'", shell=True).decode(
"utf-8").strip()
def create_tpu(
name,
zone,
type,
preemptible,
):
headers = {
'Authorization': f'Bearer {get_bearer()}',
'Content-Type': 'application/json',
}
try:
status = check_tpu(name, zone)
if status["state"] not in ["CREATING", "READY"]:
print("deleting TPU")
delete_tpu(name, zone)
while True:
try:
print("deleting check")
print(check_tpu(name, zone)["state"])
time.sleep(1)
except:
break
except:
pass
params = (
('node_id', name),
)
data = {"accelerator_type":
type,
"runtime_version":
'v2-alpha',
"network_config":
{"enable_external_ips": True},
}
if preemptible:
data["schedulingConfig"] = {"preemptible": True}
response = requests.post(f'https://tpu.googleapis.com/v2alpha1/projects/{get_project()}/locations/{zone}/nodes',
headers=headers, params=params, json=data)
print(response.json())
return response.status_code == 200
def check_tpu(name, zone):
headers = {
'Authorization': f'Bearer {get_bearer()}',
}
response = requests.get(
f'https://tpu.googleapis.com/v2alpha1/projects/{get_project()}/locations/{zone}/nodes/{name}',
headers=headers)
return response.json()
def delete_tpu(name, zone):
headers = {
'Authorization': f'Bearer {get_bearer()}',
}
response = requests.delete(
f'https://tpu.googleapis.com/v2alpha1/projects/{get_project()}/locations/{zone}/nodes/{name}',
headers=headers)
return response.json()
def wait_til(name, zone, state):
while True:
ret = check_tpu(name, zone)
print("wait_til check")
print(ret)
matches = True
for k, expected_v in state.items():
if k not in ret:
matches = False
continue
if ret[k] != expected_v:
matches = False
if "error" in ret:
return False
if ret["state"] == "TERMINATED":
return False
if matches:
return True
time.sleep(1)
def get_connection(
name,
zone,
):
info = check_tpu(name, zone)
outputs = []
for i in info["networkEndpoints"]:
outputs.append(Connection(i["ipAddress"],
connect_kwargs={
"key_filename": os.path.expanduser('~/.ssh/google_compute_engine'), }))
return outputs
def start_ray(conn, address, version=1):
conn.sudo('rm -rf *.py')
conn.sudo('rm -rf mesh_transformer')
for i in glob.glob("*.py"):
conn.put(i, "")
conn.run("mkdir mesh_transformer -p")
for i in glob.glob("mesh_transformer/*.py"):
conn.put(i, "mesh_transformer/")
conn.sudo('python3 setup.py install', hide=True)
if version == 2:
conn.put("scripts/init_ray_v2.sh", "/tmp/ray-tpu.sh")
else:
conn.put("scripts/init_ray.sh", "/tmp/ray-tpu.sh")
conn.sudo('chmod +x /tmp/ray-tpu.sh', hide=True)
conn.sudo('/tmp/ray-tpu.sh', hide=True)
try:
conn.run('ray stop -f', hide=True)
except:
pass
time.sleep(1)
conn.run(f"TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD={32 * 1024**3} ray start --address={address} --resources='" + '{"tpu": 1}\' --include-dashboard False', hide=True)