-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathray_tpu.py
142 lines (104 loc) · 3.29 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
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',
}
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(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):
conn.sudo('rm -rf *.py')
conn.sudo('rm -rf swarm_jax')
for i in glob.glob("*.py"):
print(i)
conn.put(i, "")
conn.run("mkdir swarm_jax -p")
for i in glob.glob("swarm_jax/*.py"):
print(i)
conn.put(i, "swarm_jax/")
conn.sudo('python3 setup.py install')
conn.put("scripts/init_ray.sh", "/tmp/ray-tpu.sh")
print(conn.sudo('chmod +x /tmp/ray-tpu.sh'))
print(conn.sudo('/tmp/ray-tpu.sh'))
try:
print(conn.run('ray stop -f'))
except:
pass
print(conn.run(f"ray start --address={address} --load-code-from-local --resources='" + '{"tpu": 1}\''))