-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathc_sharp.py
92 lines (77 loc) · 2.63 KB
/
c_sharp.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
from m2cgen.assemblers import get_assembler_cls
from m2cgen.interpreters import CSharpInterpreter
from tests import utils
from tests.e2e.executors.base import BaseExecutor
EXECUTOR_CODE_TPL = """
using System;
namespace TestConsoleApp {{
class Program {{
static void Main(string[] args) {{
double[] input_ = new double[args.Length];
for(int i = 0; i < input_.Length; ++i) {{
input_[i] = double.Parse(args[i]);
}}
{print_code}
}}
}}
}}
"""
EXECUTE_AND_PRINT_SCALAR = """
double res = ML.Model.Score(input_);
Console.Write(res);
"""
EXECUTE_AND_PRINT_VECTOR = """
double[] res = ML.Model.Score(input_);
for(int i = 0; i < res.Length; ++i) {
Console.Write("{0} ", res[i]);
}
"""
class CSharpExecutor(BaseExecutor):
target_exec_dir = None
project_name = "test_model"
def __init__(self, model):
self.model = model
self.interpreter = CSharpInterpreter()
assembler_cls = get_assembler_cls(model)
self.model_ast = assembler_cls(model).assemble()
def predict(self, X):
exec_args = [
str(self.target_exec_dir / self.project_name),
*map(utils.format_arg, X)
]
return utils.predict_from_commandline(exec_args)
@classmethod
def prepare_global(cls, **kwargs):
super().prepare_global(**kwargs)
if cls.target_exec_dir is None:
cls.target_exec_dir = cls._global_tmp_dir / "bin"
utils.execute_command([
"dotnet",
"new",
"console",
"--output",
str(cls._global_tmp_dir),
"--name",
cls.project_name,
"--language",
"C#"
])
def prepare(self):
if self.model_ast.output_size > 1:
print_code = EXECUTE_AND_PRINT_VECTOR
else:
print_code = EXECUTE_AND_PRINT_SCALAR
executor_code = EXECUTOR_CODE_TPL.format(
print_code=print_code)
model_code = self.interpreter.interpret(self.model_ast)
model_file_name = self._global_tmp_dir / "Model.cs"
executor_file_name = self._global_tmp_dir / "Program.cs"
utils.write_content_to_file(model_code, model_file_name)
utils.write_content_to_file(executor_code, executor_file_name)
utils.execute_command([
"dotnet",
"build",
str(self._global_tmp_dir / f"{self.project_name}.csproj"),
"--output",
str(self.target_exec_dir)
])