-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
143 lines (110 loc) · 3.53 KB
/
main.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
import os
import sys
import json
import torch
import socket
import signal
import argparse
from threading import Thread
from functools import partial
from model import Model
from infer import infer
from typing import List
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--socket_path", type=str,
help="The path to the socket")
parser.add_argument("--device", type=str, default="0" if torch.cuda.is_available()
else "cpu", help="The device for the model")
parser.add_argument("--max_length", type=int, default=2048,
help="The max length for the context window")
parser.add_argument("--mode", type=str, default="PSM",
help="The mode for FIM (PSM, SPM)")
args = parser.parse_args()
return args
args = get_args()
BUFF_SIZE = 4096
# checks if in use
try:
os.unlink(args.socket_path)
except OSError:
if os.path.exists(args.socket_path):
print(f'{args.socket_path} already exists')
sys.exit(1)
# used to store and close all sockets before exit
class SocketManager:
def __init__(self) -> None:
self._sockets = set()
def __call__(self, c: socket.socket) -> None:
self._sockets.add(c)
def close_all(self) -> None:
while len(self._sockets) > 0:
s = self._sockets.pop()
s.close()
# an unbounded recv
def recvall(s: socket.socket) -> bytes:
data = b''
while True:
part = s.recv(BUFF_SIZE)
data += part
if len(part) < BUFF_SIZE:
break
return data
END_TOKEN = "??END??"
# handles a single client
def on_client(c: socket.socket) -> None:
try:
complete_data = ""
while True:
data = recvall(c).decode("utf-8")
if len(data) == 0:
break
if not data.endswith(END_TOKEN):
complete_data += data
continue
complete_data += data[:-len(END_TOKEN)]
req = json.loads(complete_data)
code = req["code"]
num_samples = req["num_samples"]
temperature = req["temperature"]
type_annotations: List[str] = infer(
model, code, num_samples, args.mode, args.max_length, temperature)
print(f'Result: {type_annotations}')
resp = json.dumps({
"type": "single",
'type_annotations': [item for item in type_annotations]
}).encode("utf-8") # [Vec<String>]
c.sendall(resp)
finally:
c.close()
# listen for clients
def init_wait(s: socket.socket, sm: SocketManager) -> None:
while True:
c, _ = s.accept()
sm(c)
thread = Thread(target=on_client, args=(c,))
thread.start()
# called on exit signal
def close(_, __, sm: SocketManager) -> None:
print(f'Closing {args.socket_path}')
sm.close_all()
sys.exit(0)
# load model on device
print(f'Loading SantaCoder on device: `{args.device}`')
if args.device == "cpu":
device = torch.device("cpu")
else:
device = torch.device("cuda", int(args.device))
model = Model(device=device)
# init socket manager
sm = SocketManager()
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(args.socket_path)
sock.listen(1)
# store socket for future close
sm(sock)
# this should work but should be tested
# other way is to use a lambdas
signal.signal(signal.SIGINT, partial(close, sm)) # type: ignore
print(f'Listening on {args.socket_path}\n', flush=True)
init_wait(sock, sm)