-
Notifications
You must be signed in to change notification settings - Fork 237
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
90890d8
commit 88a1416
Showing
16 changed files
with
530 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ func mulVectorNumber(v1 []float64, num float64) []float64 { | |
result[i] = v1[i] * num | ||
} | ||
return result | ||
} | ||
} |
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,50 @@ | ||
import contextlib | ||
|
||
from m2cgen.ast import CompOpType | ||
from m2cgen.interpreters.code_generator import BaseCodeGenerator, CodeTemplate | ||
|
||
|
||
class HaskellCodeGenerator(BaseCodeGenerator): | ||
tpl_num_value = CodeTemplate("${value}") | ||
tpl_infix_expression = CodeTemplate("(${left}) ${op} (${right})") | ||
tpl_module_definition = CodeTemplate("module ${module_name} where") | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(HaskellCodeGenerator, self).__init__(*args, **kwargs) | ||
|
||
def array_index_access(self, array_name, index): | ||
return self.tpl_infix_expression( | ||
left=array_name, op="!!", right=index) | ||
|
||
def function_invocation(self, function_name, *args): | ||
return (function_name + " " + | ||
" ".join(map(lambda x: "({})".format(x), args))) | ||
|
||
def add_function_def(self, name, args, is_scalar_output): | ||
signature = name + " :: " | ||
signature += " -> ".join( | ||
["[Double]" if is_vector else "Double" | ||
for is_vector, _ in [*args, (not is_scalar_output, None)]]) | ||
self.add_code_line(signature) | ||
|
||
function_def = name + " " | ||
function_def += " ".join([n for _, n in args]) | ||
function_def += " =" | ||
self.add_code_line(function_def) | ||
|
||
self.increase_indent() | ||
|
||
@contextlib.contextmanager | ||
def function_definition(self, name, args, is_scalar_output): | ||
self.add_function_def(name, args, is_scalar_output) | ||
yield | ||
self.decrease_indent() | ||
|
||
def vector_init(self, values): | ||
return "[" + ", ".join(values) + "]" | ||
|
||
def _comp_op_overwrite(self, op): | ||
if op == CompOpType.NOT_EQ: | ||
return "/=" | ||
else: | ||
return op.value |
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,58 @@ | ||
import os | ||
|
||
from m2cgen import ast | ||
from m2cgen.interpreters import mixins, utils | ||
from m2cgen.interpreters.haskell.code_generator import HaskellCodeGenerator | ||
from m2cgen.interpreters.interpreter import ToCodeInterpreter | ||
|
||
|
||
class HaskellInterpreter(ToCodeInterpreter, | ||
mixins.LinearAlgebraMixin): | ||
supported_bin_vector_ops = { | ||
ast.BinNumOpType.ADD: "addVectors", | ||
} | ||
|
||
supported_bin_vector_num_ops = { | ||
ast.BinNumOpType.MUL: "mulVectorNumber", | ||
} | ||
|
||
exponent_function_name = "exp" | ||
tanh_function_name = "tanh" | ||
|
||
def __init__(self, module_name="Model", indent=4, function_name="score", | ||
*args, **kwargs): | ||
self.module_name = module_name | ||
self.function_name = function_name | ||
|
||
cg = HaskellCodeGenerator(indent=indent) | ||
super(HaskellInterpreter, self).__init__(cg, *args, **kwargs) | ||
|
||
def interpret(self, expr): | ||
self._cg.reset_state() | ||
self._reset_reused_expr_cache() | ||
|
||
self._cg.add_code_line(self._cg.tpl_module_definition( | ||
module_name=self.module_name)) | ||
|
||
args = [(True, self._feature_array_name)] | ||
func_name = self.function_name | ||
|
||
with self._cg.function_definition( | ||
name=func_name, | ||
args=args, | ||
is_scalar_output=expr.output_size == 1): | ||
last_result = self._do_interpret(expr) | ||
self._cg.add_code_line(last_result) | ||
|
||
if self.with_linear_algebra: | ||
filename = os.path.join( | ||
os.path.dirname(__file__), "linear_algebra.hs") | ||
self._cg.prepend_code_lines(utils.get_file_content(filename)) | ||
|
||
return self._cg.code | ||
|
||
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,5 @@ | ||
addVectors :: [Double] -> [Double] -> [Double] | ||
addVectors v1 v2 = zipWith (+) v1 v2 | ||
|
||
mulVectorNumber :: [Double] -> Double -> [Double] | ||
mulVectorNumber v1 num = [i * num | i <- v1] |
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,71 @@ | ||
import os | ||
import string | ||
import subprocess | ||
|
||
from m2cgen import assemblers, interpreters | ||
from tests import utils | ||
from tests.e2e.executors import base | ||
|
||
EXECUTOR_CODE_TPL = """ | ||
module Main where | ||
import System.Environment (getArgs) | ||
import ${model_name} | ||
main = do | ||
args <- getArgs | ||
let inputArray = [read i::Double | i <- args] | ||
let res = score inputArray | ||
${print_code} | ||
""" | ||
|
||
EXECUTE_AND_PRINT_SCALAR = "print res" | ||
|
||
EXECUTE_AND_PRINT_VECTOR = \ | ||
r"""mapM_ (putStr . \x -> show x ++ " ") res""" | ||
|
||
|
||
class HaskellExecutor(base.BaseExecutor): | ||
|
||
executor_name = "Main" | ||
model_name = "Model" | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
self.interpreter = interpreters.HaskellInterpreter() | ||
|
||
assembler_cls = assemblers.get_assembler_cls(model) | ||
self.model_ast = assembler_cls(model).assemble() | ||
|
||
self._ghc = "ghc" | ||
|
||
def predict(self, X): | ||
app_name = os.path.join(self._resource_tmp_dir, | ||
self.executor_name) | ||
exec_args = [app_name, *map(str, X)] | ||
return utils.predict_from_commandline(exec_args) | ||
|
||
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 = string.Template(EXECUTOR_CODE_TPL).substitute( | ||
model_name=self.model_name, | ||
print_code=print_code) | ||
model_code = self.interpreter.interpret(self.model_ast) | ||
|
||
executor_file_name = os.path.join( | ||
self._resource_tmp_dir, "{}.hs".format(self.executor_name)) | ||
model_file_name = os.path.join( | ||
self._resource_tmp_dir, "{}.hs".format(self.model_name)) | ||
with open(executor_file_name, "w") as f: | ||
f.write(executor_code) | ||
with open(model_file_name, "w") as f: | ||
f.write(model_code) | ||
|
||
exec_args = [self._ghc, executor_file_name, | ||
"-i{}".format(self._resource_tmp_dir), | ||
"-o", os.path.join(self._resource_tmp_dir, | ||
self.executor_name)] | ||
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.