Skip to content
This repository was archived by the owner on May 23, 2024. It is now read-only.

Commit bb54ee3

Browse files
author
Jinpeng Qi
committed
using plural ports instead of switching to singular
1 parent f783aef commit bb54ee3

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

docker/build_artifacts/sagemaker/python_service.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
SAGEMAKER_BATCHING_ENABLED = os.environ.get("SAGEMAKER_TFS_ENABLE_BATCHING", "false").lower()
3333
MODEL_CONFIG_FILE_PATH = "/sagemaker/model-config.cfg"
34-
TFS_GRPC_PORT = os.environ.get("TFS_GRPC_PORT")
35-
TFS_REST_PORT = os.environ.get("TFS_REST_PORT")
34+
TFS_GRPC_PORTS = os.environ.get("TFS_GRPC_PORTS")
35+
TFS_REST_PORTS = os.environ.get("TFS_REST_PORTS")
3636
SAGEMAKER_TFS_PORT_RANGE = os.environ.get("SAGEMAKER_SAFE_PORT_RANGE")
3737
TFS_INSTANCE_COUNT = int(os.environ.get("SAGEMAKER_TFS_INSTANCE_COUNT", "1"))
3838

@@ -69,11 +69,11 @@ def __init__(self):
6969
# during the _handle_load_model_post()
7070
self.model_handlers = {}
7171
else:
72-
self._tfs_grpc_port = self._parse_concat_port(TFS_GRPC_PORT)
73-
self._tfs_rest_port = self._parse_concat_port(TFS_REST_PORT)
72+
self._tfs_grpc_ports = self._parse_concat_ports(TFS_GRPC_PORTS)
73+
self._tfs_rest_ports = self._parse_concat_ports(TFS_REST_PORTS)
7474

7575
self._channels = {}
76-
for grpc_port in self._tfs_grpc_port:
76+
for grpc_port in self._tfs_grpc_ports:
7777
# Initialize grpc channel here so gunicorn worker could have mapping
7878
# between each grpc port and channel
7979
self._setup_channel(grpc_port)
@@ -98,8 +98,8 @@ def on_post(self, req, res, model_name=None):
9898
data = json.loads(req.stream.read().decode("utf-8"))
9999
self._handle_load_model_post(res, data)
100100

101-
def _parse_concat_port(self, concat_port):
102-
return concat_port.split(",")
101+
def _parse_concat_ports(self, concat_ports):
102+
return concat_ports.split(",")
103103

104104
def _pick_port(self, ports):
105105
return str(random.choice(ports))
@@ -245,8 +245,8 @@ def _handle_invocation_post(self, req, res, model_name=None):
245245
})
246246
else:
247247
# Randomly pick port used for routing incoming request.
248-
grpc_port = self._pick_port(self._tfs_grpc_port)
249-
rest_port = self._pick_port(self._tfs_rest_port)
248+
grpc_port = self._pick_port(self._tfs_grpc_ports)
249+
rest_port = self._pick_port(self._tfs_rest_ports)
250250
data, context = tfs_utils.parse_request(req, rest_port, grpc_port,
251251
self._tfs_default_model_name,
252252
channel=self._channels[int(grpc_port)])

docker/build_artifacts/sagemaker/serve.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,29 @@ def __init__(self):
8989
parts = self._sagemaker_port_range.split("-")
9090
low = int(parts[0])
9191
hi = int(parts[1])
92-
self._tfs_grpc_port = []
93-
self._tfs_rest_port = []
92+
self._tfs_grpc_ports = []
93+
self._tfs_rest_ports = []
9494
if low + 2 * self._tfs_instance_count > hi:
9595
raise ValueError("not enough ports available in SAGEMAKER_SAFE_PORT_RANGE ({})"
9696
.format(self._sagemaker_port_range))
9797
# select non-overlapping grpc and rest ports based on tfs instance count
9898
for i in range(self._tfs_instance_count):
99-
self._tfs_grpc_port.append(str(low + 2 * i))
100-
self._tfs_rest_port.append(str(low + 2 * i + 1))
99+
self._tfs_grpc_ports.append(str(low + 2 * i))
100+
self._tfs_rest_ports.append(str(low + 2 * i + 1))
101101
# concat selected ports respectively in order to pass them to python service
102-
self._tfs_grpc_concat_port = self._concat_port(self._tfs_grpc_port)
103-
self._tfs_rest_concat_port = self._concat_port(self._tfs_rest_port)
102+
self._tfs_grpc_concat_ports = self._concat_ports(self._tfs_grpc_ports)
103+
self._tfs_rest_concat_ports = self._concat_ports(self._tfs_rest_ports)
104104
else:
105105
# just use the standard default ports
106-
self._tfs_grpc_port = ["9000"]
107-
self._tfs_rest_port = ["8501"]
106+
self._tfs_grpc_ports = ["9000"]
107+
self._tfs_rest_ports = ["8501"]
108108
# provide single concat port here for default case
109-
self._tfs_grpc_concat_port = "9000"
110-
self._tfs_rest_concat_port = "8501"
109+
self._tfs_grpc_concat_ports = "9000"
110+
self._tfs_rest_concat_ports = "8501"
111111

112112
# set environment variable for python service
113-
os.environ["TFS_GRPC_PORT"] = self._tfs_grpc_concat_port
114-
os.environ["TFS_REST_PORT"] = self._tfs_rest_concat_port
113+
os.environ["TFS_GRPC_PORTS"] = self._tfs_grpc_concat_ports
114+
os.environ["TFS_REST_PORTS"] = self._tfs_rest_concat_ports
115115

116116
def _need_python_service(self):
117117
if os.path.exists(INFERENCE_PATH):
@@ -120,7 +120,7 @@ def _need_python_service(self):
120120
and os.environ.get("SAGEMAKER_MULTI_MODEL_UNIVERSAL_PREFIX"):
121121
self._enable_python_service = True
122122

123-
def _concat_port(self, ports):
123+
def _concat_ports(self, ports):
124124
str_ports = [str(port) for port in ports]
125125
concat_str_ports = ",".join(str_ports)
126126
return concat_str_ports
@@ -198,13 +198,13 @@ def _setup_gunicorn(self):
198198
gunicorn_command = (
199199
"gunicorn -b unix:/tmp/gunicorn.sock -k {} --chdir /sagemaker "
200200
"--workers {} --threads {} "
201-
"{}{} -e TFS_GRPC_PORT={} -e TFS_REST_PORT={} "
201+
"{}{} -e TFS_GRPC_PORTS={} -e TFS_REST_PORTS={} "
202202
"-e SAGEMAKER_MULTI_MODEL={} -e SAGEMAKER_SAFE_PORT_RANGE={} "
203203
"-e SAGEMAKER_TFS_WAIT_TIME_SECONDS={} "
204204
"python_service:app").format(self._gunicorn_worker_class,
205205
self._gunicorn_workers, self._gunicorn_threads,
206206
python_path_option, ",".join(python_path_content),
207-
self._tfs_grpc_concat_port, self._tfs_rest_concat_port,
207+
self._tfs_grpc_concat_ports, self._tfs_rest_concat_ports,
208208
self._tfs_enable_multi_model_endpoint,
209209
self._sagemaker_port_range,
210210
self._tfs_wait_time_seconds)
@@ -234,7 +234,7 @@ def _download_scripts(self, bucket, prefix):
234234
def _create_nginx_tfs_upstream(self):
235235
indentation = " "
236236
tfs_upstream = ""
237-
for port in self._tfs_rest_port:
237+
for port in self._tfs_rest_ports:
238238
tfs_upstream += "{}server localhost:{};\n".format(indentation, port)
239239
tfs_upstream = tfs_upstream[len(indentation):-2]
240240

@@ -338,7 +338,7 @@ def _wait_for_gunicorn(self):
338338

339339
def _wait_for_tfs(self):
340340
for i in range(self._tfs_instance_count):
341-
tfs_utils.wait_for_model(self._tfs_rest_port[i],
341+
tfs_utils.wait_for_model(self._tfs_rest_ports[i],
342342
self._tfs_default_model_name, self._tfs_wait_time_seconds)
343343

344344
@contextmanager
@@ -374,8 +374,8 @@ def _restart_single_tfs(self, pid):
374374

375375
def _start_single_tfs(self, instance_id):
376376
cmd = tfs_utils.tfs_command(
377-
self._tfs_grpc_port[instance_id],
378-
self._tfs_rest_port[instance_id],
377+
self._tfs_grpc_ports[instance_id],
378+
self._tfs_rest_ports[instance_id],
379379
self._tfs_config_path,
380380
self._tfs_enable_batching,
381381
self._tfs_batching_config_path,

0 commit comments

Comments
 (0)