-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d536e0f
commit 40baf43
Showing
14 changed files
with
322 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from .exporters import export_to_java, export_to_python | ||
from .exporters import export_to_java, export_to_python, export_to_c | ||
|
||
|
||
__all__ = [ | ||
export_to_java, | ||
export_to_python, | ||
export_to_c, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from .java.interpreter import JavaInterpreter | ||
from .python.interpreter import PythonInterpreter | ||
from .c.interpreter import CInterpreter | ||
|
||
__all__ = [ | ||
JavaInterpreter, | ||
PythonInterpreter | ||
PythonInterpreter, | ||
CInterpreter, | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import contextlib | ||
|
||
from m2cgen.interpreters.code_generator import CLikeCodeGenerator | ||
from m2cgen.interpreters.code_generator import CodeTemplate as CT | ||
|
||
|
||
class CCodeGenerator(CLikeCodeGenerator): | ||
|
||
tpl_scalar_var_declare = CT("double ${var_name};") | ||
tpl_vector_var_declare = CT("static double *${var_name}[${size}];") | ||
|
||
scalar_type = "double" | ||
vector_type = "double *" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(CCodeGenerator, self).__init__(*args, **kwargs) | ||
|
||
def add_function_def(self, name, args, is_vector_output): | ||
return_type = self._get_var_type(is_vector_output) | ||
|
||
function_def = return_type + " " + name + "(" | ||
function_def += ",".join([ | ||
self._get_var_type(is_vector) + " " + n | ||
for is_vector, n in args]) | ||
function_def += ") {" | ||
self.add_code_line(function_def) | ||
self.increase_indent() | ||
|
||
@contextlib.contextmanager | ||
def function_definition(self, name, args, is_vector_output): | ||
self.add_function_def(name, args, is_vector_output) | ||
yield | ||
self.add_block_termination() | ||
|
||
def add_var_declaration(self, expr): | ||
var_name = self.get_var_name() | ||
|
||
if expr.is_vector_output: | ||
tpl = self.tpl_vector_var_declare | ||
size = expr.size | ||
else: | ||
tpl = self.tpl_scalar_var_declare | ||
size = None | ||
|
||
self.add_code_line(tpl(var_name=var_name, size=size)) | ||
return var_name | ||
|
||
def _get_var_type(self, is_vector): | ||
return ( | ||
self.vector_type if is_vector | ||
else self.scalar_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from m2cgen.interpreters.interpreter import BaseInterpreter | ||
from m2cgen.interpreters.c.code_generator import CCodeGenerator | ||
|
||
|
||
class CInterpreter(BaseInterpreter): | ||
|
||
def __init__(self, indent=4, *args, **kwargs): | ||
cg = CCodeGenerator(indent=indent) | ||
super(CInterpreter, self).__init__(cg, *args, **kwargs) | ||
|
||
def interpret(self, expr): | ||
self._cg.reset_state() | ||
|
||
with self._cg.function_definition( | ||
name="score", | ||
args=[(True, self._feature_array_name)], | ||
is_vector_output=expr.is_vector_output): | ||
last_result = self._do_interpret(expr) | ||
self._cg.add_return_statement(last_result) | ||
|
||
return self._cg.code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from tests.e2e.executors.java import JavaExecutor | ||
from tests.e2e.executors.python import PythonExecutor | ||
from tests.e2e.executors.c import CExecutor | ||
|
||
__all__ = [ | ||
JavaExecutor, | ||
PythonExecutor, | ||
CExecutor, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
import string | ||
import subprocess | ||
|
||
from tests.e2e.executors import base | ||
|
||
import m2cgen as m2c | ||
|
||
|
||
executor_code_tpl = """ | ||
#include <stdio.h> | ||
${code} | ||
int main(int argc, char *argv[]) | ||
{ | ||
double input [argc-1]; | ||
for (int i = 1; i < argc; ++i) { | ||
sscanf(argv[i], "%lf", &input[i-1]); | ||
} | ||
printf("%f\\n", score(input)); | ||
return 0; | ||
} | ||
""" | ||
|
||
|
||
class CExecutor(base.BaseExecutor): | ||
|
||
model_name = "score" | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
|
||
self._gcc = "gcc" | ||
|
||
def predict(self, X): | ||
|
||
exec_args = [os.path.join(self._resource_tmp_dir, self.model_name)] | ||
exec_args.extend(map(str, X)) | ||
result = subprocess.Popen(exec_args, stdout=subprocess.PIPE) | ||
items = result.stdout.read().decode("utf-8").split(" ") | ||
if len(items) == 1: | ||
return float(items[0]) | ||
else: | ||
return [float(i) for i in items] | ||
|
||
def prepare(self): | ||
code = m2c.export_to_c(self.model) | ||
code = string.Template(executor_code_tpl).substitute(code=code) | ||
|
||
file_name = os.path.join( | ||
self._resource_tmp_dir, "{}.c".format(self.model_name)) | ||
with open(file_name, "w") as f: | ||
f.write(code) | ||
|
||
target = os.path.join(self._resource_tmp_dir, self.model_name) | ||
exec_args = [self._gcc] + [file_name] + ["-o", target, "-std=c99"] | ||
subprocess.call(exec_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.