-
Notifications
You must be signed in to change notification settings - Fork 236
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
6802632
commit 1553c4c
Showing
18 changed files
with
515 additions
and
6 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,50 @@ | ||
import contextlib | ||
|
||
from m2cgen.ast import CompOpType | ||
from m2cgen.interpreters.code_generator import CLikeCodeGenerator | ||
from m2cgen.interpreters.code_generator import CodeTemplate | ||
|
||
|
||
class PhpCodeGenerator(CLikeCodeGenerator): | ||
|
||
tpl_array_index_access = CodeTemplate("$$${array_name}[${index}]") | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(PhpCodeGenerator, self).__init__(*args, **kwargs) | ||
|
||
def add_function_def(self, name, args): | ||
function_def = "function " + name + "(" | ||
function_def += ", ".join([ | ||
("array " if is_vector else "") + "$" + 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): | ||
self.add_function_def(name, args) | ||
yield | ||
self.add_block_termination() | ||
|
||
def get_var_name(self): | ||
return "$" + super().get_var_name() | ||
|
||
def add_var_declaration(self, size): | ||
var_name = self.get_var_name() | ||
self.add_var_assignment( | ||
var_name=var_name, | ||
value="array()" if size > 1 else "null", | ||
value_size=size) | ||
return var_name | ||
|
||
def vector_init(self, values): | ||
return "array(" + ", ".join(values) + ")" | ||
|
||
def _comp_op_overwrite(self, op): | ||
if op == CompOpType.EQ: | ||
return "===" | ||
elif 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,44 @@ | ||
import os | ||
|
||
from m2cgen import ast | ||
from m2cgen.interpreters import utils, mixins | ||
from m2cgen.interpreters.php.code_generator import PhpCodeGenerator | ||
from m2cgen.interpreters.interpreter import ToCodeInterpreter | ||
|
||
|
||
class PhpInterpreter(ToCodeInterpreter, 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 = "exp" | ||
power_function_name = "pow" | ||
tanh_function_name = "tanh" | ||
|
||
def __init__(self, indent=4, *args, **kwargs): | ||
cg = PhpCodeGenerator(indent=indent) | ||
super(PhpInterpreter, self).__init__(cg, *args, **kwargs) | ||
|
||
def interpret(self, expr): | ||
self._cg.reset_state() | ||
self._reset_reused_expr_cache() | ||
|
||
with self._cg.function_definition( | ||
name="score", | ||
args=[(True, 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.php") | ||
self._cg.prepend_code_lines(utils.get_file_content(filename)) | ||
|
||
self._cg.prepend_code_line("<?php") | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function add_vectors(array $v1, array $v2) { | ||
$result = array(); | ||
for ($i = 0; $i < count($v1); ++$i) { | ||
$result[] = $v1[$i] + $v2[$i]; | ||
} | ||
return $result; | ||
} | ||
function mul_vector_number(array $v1, $num) { | ||
$result = array(); | ||
for ($i = 0; $i < count($v1); ++$i) { | ||
$result[] = $v1[$i] * $num; | ||
} | ||
return $result; | ||
} |
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,71 @@ | ||
import os | ||
import string | ||
|
||
from m2cgen import assemblers, interpreters | ||
from tests import utils | ||
from tests.e2e.executors import base | ||
|
||
EXECUTOR_CODE_TPL = """ | ||
<?php | ||
$$input_array = array(); | ||
for ($$i = 1; $$i < $$argc; ++$$i) { | ||
$$input_array[] = floatval($$argv[$$i]); | ||
} | ||
require '${model_file}.php'; | ||
$$res = score($$input_array); | ||
${print_code} | ||
""" | ||
|
||
EXECUTE_AND_PRINT_SCALAR = """ | ||
echo($res); | ||
""" | ||
|
||
EXECUTE_AND_PRINT_VECTOR = """ | ||
echo(implode(" ", $res)); | ||
""" | ||
|
||
|
||
class PhpExecutor(base.BaseExecutor): | ||
|
||
executor_name = "score" | ||
model_name = "model" | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
self.interpreter = interpreters.PhpInterpreter() | ||
|
||
assembler_cls = assemblers.get_assembler_cls(model) | ||
self.model_ast = assembler_cls(model).assemble() | ||
|
||
self._php = "php" | ||
|
||
def predict(self, X): | ||
file_name = os.path.join(self._resource_tmp_dir, | ||
"{}.php".format(self.executor_name)) | ||
exec_args = [self._php, | ||
"-f", | ||
file_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_file=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, "{}.php".format(self.executor_name)) | ||
model_file_name = os.path.join( | ||
self._resource_tmp_dir, "{}.php".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) |
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.