forked from carlini/yet-another-applied-llm-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_controller.py
245 lines (191 loc) · 8.11 KB
/
docker_controller.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
## Copyright (C) 2024, Nicholas Carlini <nicholas@carlini.com>.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
import pickle
import sys
import time
import tarfile
import io
import threading
import signal
import subprocess
import pty
import os
import select
import re
import termios
import struct
import fcntl
import random
import json
# DO NOT SET THIS FLAG TO TRUE UNLESS YOU ARE SURE YOU UNDERSTAND THE CONSEQUENCES
# IT IS VERY DANGEROUS. YOU WILL BE DIRECTLY EVALUATING WHATEVER COMES OUT OF
# A LANGUAGE MODEL DIRECTLY ON YOUR COMPUTER WITH NO SAFETY CHECKS.
I_HAVE_BLIND_FAITH_IN_LLMS_AND_AM_OKAY_WITH_THEM_BRICKING_MY_MACHINE_OR_MAKING_THEM_HALT_AND_CATCH_FIRE = False
BACKEND = json.load(open("config.json"))['container']
def make_tar(files):
file_like_object = io.BytesIO()
tar = tarfile.TarFile(fileobj=file_like_object, mode='w')
for file_name, file_content in files.items():
tarinfo = tarfile.TarInfo(name=file_name)
tarinfo.size = len(file_content)
tarinfo.mtime = time.time()
tar.addfile(tarinfo, io.BytesIO(file_content))
tar.close()
file_like_object.seek(0)
return file_like_object
if BACKEND == "docker":
import docker
def setup_docker(env):
env.docker = docker.from_env()
env.container = env.docker.containers.run("llm-benchmark-image", detach=True, tty=True)
def stop_and_remove_container(client, container_id):
# Stopping the container
client.containers.get(container_id).stop()
# Removing the container
client.containers.get(container_id).remove()
def async_kill_container(client, container):
thread = threading.Thread(target=stop_and_remove_container, args=(client, container.id))
thread.daemon = True
thread.start()
def safe_run(client, container, files, run_cmd):
tarfile = make_tar(files)
path = "/usr/src/app"
container.put_archive(path, tarfile)
exit_code, output = container.exec_run(run_cmd)
return output
elif BACKEND == "podman":
def setup_docker(env):
# Starting a container with Podman
result = subprocess.run(["podman", "run", "-d", "-t", "llm-benchmark-image"], capture_output=True, text=True, check=True)
env.container = result.stdout.strip()
env.docker = "I AM USING PODMAN THIS IS NOT NEEDED"
def stop_and_remove_podman_container(container_id):
# Stopping the container
subprocess.run(["podman", "container", "stop", container_id], check=True)
# Removing the container
subprocess.run(["podman", "container", "rm", container_id], check=True)
def async_kill_container(client, container_id):
thread = threading.Thread(target=stop_and_remove_podman_container, args=(container_id,))
thread.daemon = True
thread.start()
def safe_run(client, container_id, files, run_cmd):
tarfile = make_tar(files)
# Create a temporary directory in the container to store files
subprocess.run(["podman", "exec", container_id, "mkdir", "-p", "/usr/src/app"], check=True)
# Copying files to the container
r = random.randint(0, 1000000)
with open('/tmp/archive%d.tar'%r, 'wb') as out_f:
out_f.write(tarfile.getbuffer())
time.sleep(.1)
subprocess.run(["podman", "cp", "/tmp/archive%d.tar"%r, f"{container_id}:/usr/src/app"], check=True)
time.sleep(.1)
result = subprocess.run(["podman", "exec", container_id, "tar", "-xf", "archive%d.tar"%r], capture_output=True, check=True)
time.sleep(.3)
# Executing command in the container
result = subprocess.run(["podman", "exec", container_id, *run_cmd], capture_output=True)
return result.stdout + result.stderr
else:
raise ValueError("Invalid backend")
import fcntl
def is_fd_closed(fd):
try:
fcntl.fcntl(fd, fcntl.F_GETFD)
return False
except OSError:
return True
class DockerJob:
def __init__(self, container_id, eos_string):
self.eos_string = eos_string
if BACKEND == "docker":
cmd = f"docker exec -it {container_id} /bin/bash"
print("Running", cmd)
else:
cmd = f"podman exec -it {container_id} /bin/bash"
self.process = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
self.master_fd = self.process.stdout.fileno() # If you need a file descriptor for reading output
@staticmethod
def remove_ansi(text):
ansi_escape =re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
return ansi_escape.sub('', text)
def __call__(self, cmd):
# Send the command through the PTY
print("GO", self.process.stdin)
try:
self.process.stdin.write((cmd + "\n"))
self.process.stdin.flush()
except:
print("Process was terminated")
return "Process was terminated"
# Read the output until the EOS string is encountered
output = []
while True:
ready, _, _ = select.select([self.master_fd], [], [], 2) # 2-second timeout
if ready:
line = os.read(self.master_fd, 128).decode()
output.append(line)
if self.eos_string in line:
break
if line == '':
break
else:
# Timeout occurred
print("Timeout - no output received in 2 seconds")
break
output = ''.join(output)
output = self.remove_ansi(output)
print("Output:", repr(output))
return output
def invoke_docker(env, files, run_cmd, out_bytes=False):
if env.docker is None:
setup_docker(env)
def raise_timeout(signum, frame):
raise TimeoutError
signal.signal(signal.SIGALRM, raise_timeout)
signal.alarm(20)
try:
# Function call that might take too long
out = safe_run(env.docker, env.container, files, run_cmd)
except TimeoutError:
out = b"Timeout: function took too long to complete"
signal.alarm(0)
if out_bytes:
return out
else:
return out.decode("utf-8")
if I_HAVE_BLIND_FAITH_IN_LLMS_AND_AM_OKAY_WITH_THEM_BRICKING_MY_MACHINE_OR_MAKING_THEM_HALT_AND_CATCH_FIRE:
class DockerJob:
def __init__(self, container_id, eos_string):
raise NotImplementedError("This test is not implemented in unsafe mode yet")
def setup_docker(env):
import random
env.fake_docker_id = random.randint(0, 1000000)
os.mkdir("/tmp/fakedocker_%d"%env.fake_docker_id)
def invoke_docker(env, files, run_cmd, out_bytes=False):
if env.fake_docker_id is None:
setup_docker(env)
for file_name, file_content in files.items():
with open("/tmp/fakedocker_%d/%s"%(env.fake_docker_id, file_name), "wb") as f:
f.write(file_content)
proc = subprocess.run(run_cmd, cwd="/tmp/fakedocker_%d"%env.fake_docker_id, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if out_bytes:
return proc.stdout + proc.stderr
else:
return proc.stdout.decode("utf-8") + proc.stderr.decode("utf-8")