-
Notifications
You must be signed in to change notification settings - Fork 239
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
c3426aa
commit e4eccc5
Showing
18 changed files
with
487 additions
and
15 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
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
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
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,37 @@ | ||
import contextlib | ||
|
||
from m2cgen.interpreters.code_generator import ImperativeCodeGenerator | ||
from m2cgen.interpreters.code_generator import CodeTemplate as CT | ||
|
||
|
||
class RubyCodeGenerator(ImperativeCodeGenerator): | ||
|
||
tpl_var_declaration = CT("") | ||
tpl_num_value = CT("${value}") | ||
tpl_infix_expression = CT("(${left}) ${op} (${right})") | ||
tpl_return_statement = tpl_num_value | ||
tpl_array_index_access = CT("${array_name}[${index}]") | ||
tpl_if_statement = CT("if ${if_def}") | ||
tpl_else_statement = CT("else") | ||
tpl_block_termination = CT("end") | ||
tpl_var_assignment = CT("${var_name} = ${value}") | ||
|
||
def add_function_def(self, name, args): | ||
func_def = "def " + name + "(" | ||
func_def += ", ".join(args) | ||
func_def += ")" | ||
self.add_code_line(func_def) | ||
self.increase_indent() | ||
|
||
@contextlib.contextmanager | ||
def function_definition(self, name, args): | ||
self.add_function_def(name, args) | ||
yield | ||
self.add_block_termination() | ||
|
||
def method_invocation(self, method_name, obj, args): | ||
return ("(" + str(obj) + ")." + method_name + | ||
"(" + ", ".join(map(str, args)) + ")") | ||
|
||
def vector_init(self, values): | ||
return "[" + ", ".join(values) + "]" |
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,61 @@ | ||
import os | ||
|
||
from m2cgen import ast | ||
from m2cgen.interpreters import utils, mixins | ||
from m2cgen.interpreters.ruby.code_generator import RubyCodeGenerator | ||
from m2cgen.interpreters.interpreter import ImperativeToCodeInterpreter | ||
|
||
|
||
class RubyInterpreter(ImperativeToCodeInterpreter, | ||
mixins.LinearAlgebraMixin): | ||
|
||
supported_bin_vector_ops = { | ||
ast.BinNumOpType.ADD: "add_vectors", | ||
} | ||
|
||
supported_bin_vector_num_ops = { | ||
ast.BinNumOpType.MUL: "mul_vector_number", | ||
} | ||
|
||
exponent_function_name = "Math.exp" | ||
sqrt_function_name = "Math.sqrt" | ||
tanh_function_name = "Math.tanh" | ||
|
||
def __init__(self, indent=4, function_name="score", *args, **kwargs): | ||
self.function_name = function_name | ||
|
||
cg = RubyCodeGenerator(indent=indent) | ||
super(RubyInterpreter, self).__init__(cg, *args, **kwargs) | ||
|
||
def interpret(self, expr): | ||
self._cg.reset_state() | ||
self._reset_reused_expr_cache() | ||
|
||
with self._cg.function_definition( | ||
name=self.function_name, | ||
args=[self._feature_array_name]): | ||
last_result = self._do_interpret(expr) | ||
self._cg.add_return_statement(last_result) | ||
|
||
if self.with_linear_algebra: | ||
filename = os.path.join( | ||
os.path.dirname(__file__), "linear_algebra.rb") | ||
self._cg.prepend_code_lines(utils.get_file_content(filename)) | ||
|
||
return self._cg.code | ||
|
||
def interpret_bin_num_expr(self, expr, **kwargs): | ||
if expr.op == ast.BinNumOpType.DIV: | ||
# Always force float result | ||
return self._cg.method_invocation( | ||
method_name="fdiv", | ||
obj=self._do_interpret(expr.left, **kwargs), | ||
args=[self._do_interpret(expr.right, **kwargs)]) | ||
else: | ||
return super().interpret_bin_num_expr(expr, **kwargs) | ||
|
||
def interpret_pow_expr(self, expr, **kwargs): | ||
base_result = self._do_interpret(expr.base_expr, **kwargs) | ||
exp_result = self._do_interpret(expr.exp_expr, **kwargs) | ||
return self._cg.infix_expression( | ||
left=base_result, right=exp_result, op="**") |
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,6 @@ | ||
def add_vectors(v1, v2) | ||
v1.zip(v2).map { |x, y| x + y } | ||
end | ||
def mul_vector_number(v1, num) | ||
v1.map { |i| i * num } | ||
end |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import string | ||
|
||
from m2cgen import assemblers, interpreters | ||
from tests import utils | ||
from tests.e2e.executors import base | ||
|
||
EXECUTOR_CODE_TPL = """ | ||
input_array = ARGV.map(&:to_f) | ||
${model_code} | ||
res = score(input_array) | ||
${print_code} | ||
""" | ||
|
||
PRINT_SCALAR = """ | ||
puts res | ||
""" | ||
|
||
PRINT_VECTOR = """ | ||
puts res.join(" ") | ||
""" | ||
|
||
|
||
class RubyExecutor(base.BaseExecutor): | ||
model_name = "score" | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
self.interpreter = interpreters.RubyInterpreter() | ||
|
||
assembler_cls = assemblers.get_assembler_cls(model) | ||
self.model_ast = assembler_cls(model).assemble() | ||
|
||
self._ruby = "ruby" | ||
|
||
def predict(self, X): | ||
file_name = os.path.join(self._resource_tmp_dir, | ||
"{}.rb".format(self.model_name)) | ||
exec_args = [self._ruby, file_name, *map(str, X)] | ||
return utils.predict_from_commandline(exec_args) | ||
|
||
def prepare(self): | ||
if self.model_ast.output_size > 1: | ||
print_code = PRINT_VECTOR | ||
else: | ||
print_code = PRINT_SCALAR | ||
executor_code = string.Template(EXECUTOR_CODE_TPL).substitute( | ||
model_code=self.interpreter.interpret(self.model_ast), | ||
print_code=print_code) | ||
|
||
file_name = os.path.join( | ||
self._resource_tmp_dir, "{}.rb".format(self.model_name)) | ||
with open(file_name, "w") as f: | ||
f.write(executor_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
Oops, something went wrong.