MLCompilerBridge
Tools for streamlining communication with ML models for compiler optimizations.
Loading...
Searching...
No Matches
PipeCompilerInterface.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
15from .BaseCompilerInterface import BaseCompilerInterface
16import os
17import io
18
19
21
24 def __init__(self, data_format=None, pipe_name=None):
25 super().__init__(data_format)
26 self.pipe_name = pipe_name
27 self.to_compiler = None
28 self.from_compiler = None
29 self.tc_buffer = None
30 self.fc_buffer = None
31 self.buffer = None
32 self.init_pipes()
33
34 def __del__(self):
35 self.close_pipes()
36 self.remove_pipes()
37
38
39 def evaluate(self, mode=None):
40 out = self.serdes_obj.getOutputBuffer()
41 if out is not None:
42 self.tc_buffer.write(out)
43 self.tc_buffer.flush()
44
45 if mode == "exit":
46 return None
47
48 result = self.serdes_obj.deserializeData(self.fc_buffer)
49
50 return result
51
52
53 def init_pipes(self):
54 self.to_compiler = self.pipe_name + ".in"
55 self.from_compiler = self.pipe_name + ".out"
56 if os.path.exists(self.to_compiler):
57 os.remove(self.to_compiler)
58 if os.path.exists(self.from_compiler):
59 os.remove(self.from_compiler)
60
61 os.mkfifo(self.to_compiler, 0o666)
62 os.mkfifo(self.from_compiler, 0o666)
63
64
65 def reset_pipes(self):
66 self.tc_buffer = io.BufferedWriter(io.FileIO(self.to_compiler, "wb"))
67 self.fc_buffer = io.BufferedReader(io.FileIO(self.from_compiler, "rb"))
68
69
70 def close_pipes(self):
71 if self.fc_buffer is not None:
72 self.tc_buffer.close()
73 self.fc_buffer.close()
74 self.tc_buffer = None
75 self.fc_buffer = None
76
77
78 def remove_pipes(self):
79 os.remove(self.to_compiler)
80 os.remove(self.from_compiler)
This base class specifies methods for communication with compiler.
This class implements methods for communication with compiler using pipes.
evaluate(self, mode=None)
Sends query to compiler and returns deserialized result.
__init__(self, data_format=None, pipe_name=None)
Initializes PipeCompilerInterface object.