Repository navigation
Expand file tree
/
Copy pathruntime_cli.py
More file actions
229 lines (199 loc) · 7.09 KB
/
Copy pathruntime_cli.py
File metadata and controls
229 lines (199 loc) · 7.09 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
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
222
223
224
225
226
227
228
229
import sys
import argparse
import datetime
from deprecated import deprecated
from time import sleep
from quafu_runtime.clients.account import Account
from quafu_runtime.quafu_runtime_service import RuntimeService
API_TOKEN = "0NjkF0-LizJ8P09GIeHLL95_ShrXozyJff3RGhd5qw8.9RDNzQTMxADM3EjOiAHelJCL3ITM6ICZpJye.9JCVXpkI6ICc5RnIsIiN1IzUIJiOicGbhJye"
class RuntimeAPI:
"""Collection of quafu runtime APIs"""
@staticmethod
def run_upload(args):
check_non_empty_args(
program_name=args.program_name, program_path=args.program_path
)
account = Account(api_token=args.api_token)
service = RuntimeService(account)
metadata = {"name": args.program_name, "backend": "testbackend"}
pid = service.upload_program(data=args.program_path, metadata=metadata)
print(f"program_id: {pid}")
# TODO(zhaoyilun): refactor
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_upload_more(num: int):
account = Account(api_token=API_TOKEN)
service = RuntimeService(account)
for i in range(num):
metadata = {"name": "testname" + str(i + 10), "backend": "testbackend"}
program_id = service.upload_program(
data="examples/program_source/hello.py", metadata=metadata
)
print(program_id)
@staticmethod
def run_get_programs(args):
account = Account(api_token=args.api_token)
service = RuntimeService(account)
service.list_programs(refresh=True, detailed=False, limit=20)
# TODO(zhaoyilun): refine
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_get_program():
account = Account(api_token=API_TOKEN)
service = RuntimeService(account)
res = service.program(name="long-run-task")
print(res)
@staticmethod
def run_update_program(args):
# Check arguments
check_non_empty_args(program_id=args.program_id, program_path=args.program_path)
# Init account
account = Account(api_token=args.api_token)
service = RuntimeService(account)
# Update program
service.update_program(
program_id=args.program_id,
data=args.program_path,
is_public=True,
description=args.description,
)
# TODO(zhaoyilun): refine
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_check_source_code():
account = Account(api_token=API_TOKEN)
service = RuntimeService(account)
metadata = {"name": "program_with_error", "backend": "testbackend"}
service.upload_program(
data="examples/program_source/program_with_error.py", metadata=metadata
)
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_del_program():
account = Account(api_token=API_TOKEN)
service = RuntimeService(account)
service.delete_program(program_id="8ca38b518f1546c89e9dec112b997e4b")
# TODO(zhaoyilun): refine
@deprecated(reason="needs refine")
@staticmethod
def legacy_run():
account = Account(api_token=API_TOKEN)
service = RuntimeService(account)
job = service.run(name="hello", backend="py_simu")
result = job.result(wait=True)
print(result)
# TODO(zhaoyilun): refine
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_job_async():
account = Account(api_token=API_TOKEN)
service = RuntimeService(account)
job = service.run(
program_id="409c55020cda4eedbae341fe316c1970", backend="py_simu"
)
print(job.job_id())
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
sleep(5)
response = job.result(wait=False)
print(response)
# TODO(zhaoyilun): refine
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_parallel(num):
import multiprocessing
nums = num
plist = []
for i in range(nums):
p = multiprocessing.Process(target=RuntimeAPI.run_job_sync)
plist.append(p)
p.start()
for p in plist:
p.join()
@staticmethod
def run_job_sync(args):
# Check arguments
check_non_empty_args(program_name=args.program_name)
# Init account
account = Account(api_token=args.api_token)
service = RuntimeService(account)
# Launch a job
job = service.run(name=args.program_name, backend="py_simu")
print(job.job_id())
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(job.result(wait=True))
print(job.logs())
# TODO(zhaoyilun): needs refine
@deprecated(reason="needs refine")
@staticmethod
def legacy_run_websockets():
account = Account(api_token=API_TOKEN)
servie = RuntimeService(account)
job = servie.run(name="long-run-task")
job.interim_results(callback=callback)
print(job.result(wait=True))
def callback(job_id, message):
print(f"job_id:{job_id}, message:{message}")
def check_non_empty_args(**kwargs):
required_params = []
for k, v in kwargs.items():
if v == "" or v is None:
required_params.append(k)
if required_params:
print(f"{','.join(required_params)} must be set!")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="Run different RuntimeAPI methods based on arguments."
)
parser.add_argument(
"--run",
type=str,
help="The name of the RuntimeAPI static method to run (e.g., run_upload, run_job_sync, etc.)",
)
parser.add_argument(
"--show-methods", action="store_true", help="Show available RuntimeAPI methods"
)
parser.add_argument(
"--program-name", type=str, help="name of the program", default=""
)
parser.add_argument(
"--program-path", type=str, help="path of the program", default=""
)
parser.add_argument(
"--program-id",
type=str,
help="id of the program, used to update the program",
default="",
)
parser.add_argument(
"--program-description",
type=str,
help="description, used when updating the program",
default="",
)
parser.add_argument(
"--api-token",
type=str,
help="API token from quafu website: https://quafu.baqis.ac.cn/",
default=API_TOKEN,
)
args = parser.parse_args()
if args.show_methods:
print("Available runtime methods:")
for method_name in dir(RuntimeAPI):
if method_name.startswith("run") and callable(
getattr(RuntimeAPI, method_name)
):
print(f" - {method_name}")
elif args.run:
run_method = getattr(RuntimeAPI, args.run, None)
if run_method and callable(run_method):
run_method(args)
else:
print(f"Invalid run method: {args.run}")
else:
print(
"No run method provided. Use --run argument to specify the method to run."
)
if __name__ == "__main__":
main()