MLCompilerBridge
Tools for streamlining communication with ML models for compiler optimizations.
Loading...
Searching...
No Matches
GrpcCompilerInterface.py
Go to the documentation of this file.
1# ------------------------------------------------------------------------------
2#
3# Part of the MLCompilerBridge Project, under the Apache License v2.0 with LLVM
4# Exceptions. See the LICENSE file for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7# ------------------------------------------------------------------------------
8
14
15
16from .BaseCompilerInterface import BaseCompilerInterface
17import time
18
19import grpc
20from concurrent import futures
21
22# requires grpc stub evaluate, service_server obj and adder method
23
24
26
34 self,
35 mode,
36 stub_class=None,
37 hostip="127.0.0.1",
38 hostport=50051,
39 add_server_method=None,
40 grpc_service_obj=None,
41 ):
42 super().__init__("protobuf")
43 self.mode = mode
44 self.host = hostip
45 self.server_port = hostport
46
47 if self.mode == "client":
48 self.channel = grpc.insecure_channel(
49 "{}:{}".format(self.host, self.server_port)
50 )
51 print("Setting stub", stub_class)
52 self.stub = stub_class(self.channel)
53
54 elif self.mode == "server":
55 self.grpc_service_obj = grpc_service_obj
56 self.add_server_method = add_server_method
57 self.start_server()
58
59 def __del__(self):
60 pass
61
62
63 def evaluate(self, mode=None):
64 out = self.serdes_obj.getOutputBuffer()
65 return self.stub.queryCompiler(out)
66
67
68 def start_server(self):
69 if self.mode == "server":
70 server = grpc.server(
71 futures.ThreadPoolExecutor(max_workers=20),
72 options=[
73 ("grpc.max_send_message_length", 200 * 1024 * 1024), # 50MB
74 ("grpc.max_receive_message_length", 200 * 1024 * 1024), # 50MB
75 ],
76 )
77
78 self.add_server_method(self.grpc_service_obj, server)
79
80 retries = 0
81 max_retries = 30
82 wait_seconds = 0.2
83 retry_wait_backoff_exponent = 1.2
84
85 while retries < max_retries:
86 added_port = server.add_insecure_port(
87 "{}:{}".format(self.host, self.server_port)
88 )
89
90 if str(added_port) == str(self.server_port):
91 server.start()
92 print("Server Running")
93 server.wait_for_termination()
94 break
95 else:
96 retries += 1
97 print(
98 "The port",
99 self.server_port,
100 "is already in use retrying! attempt: ",
101 retries,
102 )
103
104 time.sleep(wait_seconds)
105 wait_seconds *= retry_wait_backoff_exponent
This base class specifies methods for communication with compiler.
This class implements methods for communication with compiler using gRPC.
__init__(self, mode, stub_class=None, hostip="127.0.0.1", hostport=50051, add_server_method=None, grpc_service_obj=None)
Initializes GrpcCompilerInterface object.
evaluate(self, mode=None)
Sends query to compiler and returns deserialized result.