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
| import argparse import queue import threading from functools import partial
import grpc import inference_pb2 import inference_pb2_grpc import management_pb2 import management_pb2_grpc
def get_inference_stub(): channel = grpc.insecure_channel("localhost:7070") stub = inference_pb2_grpc.InferenceAPIsServiceStub(channel) return stub
def get_management_stub(): channel = grpc.insecure_channel("localhost:7071") stub = management_pb2_grpc.ManagementAPIsServiceStub(channel) return stub
def infer(stub, model_name, model_input, metadata): with open(model_input, "rb") as f: data = f.read()
input_data = {"data": data} response = stub.Predictions( inference_pb2.PredictionsRequest(model_name=model_name, input=input_data), metadata=metadata, )
try: prediction = response.prediction.decode("utf-8") print(prediction) except grpc.RpcError as e: print(f"gRPC error: {e.details()}") exit(1)
def infer_stream(stub, model_name, model_input, metadata): with open(model_input, "rb") as f: data = f.read()
input_data = {"data": data} responses = stub.StreamPredictions( inference_pb2.PredictionsRequest(model_name=model_name, input=input_data), metadata=metadata, )
try: for resp in responses: prediction = resp.prediction.decode("utf-8") print(prediction) except grpc.RpcError as e: print(f"gRPC error: {e.details()}") exit(1)
def infer_stream2(model_name, sequence_id, input_files, metadata): response_queue = queue.Queue() process_response_func = partial( InferStream2.default_process_response, response_queue )
client = InferStream2SimpleClient() try: client.start_stream( model_name=model_name, sequence_id=sequence_id, process_response=process_response_func, metadata=metadata, ) sequence = input_files.split(",")
for input_file in sequence: client.async_send_infer(input_file.strip())
for i in range(0, len(sequence)): response = response_queue.get() print(str(response))
print("Sequence completed!")
except grpc.RpcError as e: print("infer_stream2 received error", e) exit(1) finally: client.stop_stream() client.stop()
def register(stub, model_name, mar_set_str, metadata): mar_set = set() if mar_set_str: mar_set = set(mar_set_str.split(",")) marfile = f"{model_name}.mar" print(f"## Check {marfile} in mar_set :", mar_set) if marfile not in mar_set: marfile = "https://torchserve.s3.amazonaws.com/mar_files/{}.mar".format( model_name )
print(f"## Register marfile: {marfile}\n") params = { "url": marfile, "initial_workers": 1, "synchronous": True, "model_name": model_name, } try: response = stub.RegisterModel( management_pb2.RegisterModelRequest(**params), metadata=metadata ) print(f"Model {model_name} registered successfully") except grpc.RpcError as e: print(f"Failed to register model {model_name}.") print(str(e.details())) exit(1)
def unregister(stub, model_name, metadata): try: response = stub.UnregisterModel( management_pb2.UnregisterModelRequest(model_name=model_name), metadata=metadata, ) print(f"Model {model_name} unregistered successfully") except grpc.RpcError as e: print(f"Failed to unregister model {model_name}.") print(str(e.details())) exit(1)
if __name__ == "__main__": parent_parser = argparse.ArgumentParser(add_help=False) parent_parser.add_argument( "model_name", type=str, default=None, help="Name of the model used.", ) parent_parser.add_argument( "--auth-token", dest="auth_token", type=str, default=None, required=False, help="Authorization token", )
parser = argparse.ArgumentParser( description="TorchServe gRPC client", formatter_class=argparse.RawTextHelpFormatter, ) subparsers = parser.add_subparsers(help="Action", dest="action")
infer_action_parser = subparsers.add_parser( "infer", parents=[parent_parser], add_help=False ) infer_stream_action_parser = subparsers.add_parser( "infer_stream", parents=[parent_parser], add_help=False ) infer_stream2_action_parser = subparsers.add_parser( "infer_stream2", parents=[parent_parser], add_help=False ) register_action_parser = subparsers.add_parser( "register", parents=[parent_parser], add_help=False ) unregister_action_parser = subparsers.add_parser( "unregister", parents=[parent_parser], add_help=False )
infer_action_parser.add_argument( "model_input", type=str, default=None, help="Input for model for inference." ) infer_stream_action_parser.add_argument( "model_input", type=str, default=None, help="Input for model for stream inference.", ) infer_stream2_action_parser.add_argument( "sequence_id", type=str, default=None, help="Input for sequence id for stream inference.", ) infer_stream2_action_parser.add_argument( "input_files", type=str, default=None, help="Comma separated list of input files", ) register_action_parser.add_argument( "mar_set", type=str, default=None, nargs="?", help="Comma separated list of mar models to be loaded using [model_name=]model_location format.", )
args = parser.parse_args() if args.auth_token: metadata = ( ("protocol", "gRPC"), ("session_id", "12345"), ("authorization", f"Bearer {args.auth_token}"), ) else: metadata = (("protocol", "gRPC"), ("session_id", "12345"))
if args.action == "infer": infer(get_inference_stub(), args.model_name, args.model_input, metadata) elif args.action == "infer_stream": infer_stream(get_inference_stub(), args.model_name, args.model_input, metadata) elif args.action == "infer_stream2": infer_stream2(args.model_name, args.sequence_id, args.input_files, metadata) elif args.action == "register": register(get_management_stub(), args.model_name, args.mar_set, metadata) elif args.action == "unregister": unregister(get_management_stub(), args.model_name, metadata)
|