-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Javascript interpreter to support scoring in BigQuery. (#97)
- Loading branch information
1 parent
39ea6e6
commit d3a4760
Showing
14 changed files
with
436 additions
and
2 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
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,27 @@ | ||
import contextlib | ||
|
||
from m2cgen.interpreters.code_generator import CLikeCodeGenerator | ||
|
||
|
||
class JavascriptCodeGenerator(CLikeCodeGenerator): | ||
def __init__(self, *args, **kwargs): | ||
super(JavascriptCodeGenerator, self).__init__(*args, **kwargs) | ||
|
||
def add_function_def(self, name, args): | ||
function_def = "function " + name + "(" | ||
function_def += ",".join([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 vector_init(self, values): | ||
return "[" + ", ".join(values) + "]" | ||
|
||
def _get_var_declare_type(self, is_vector): | ||
return "var" |
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 os | ||
|
||
from m2cgen import ast | ||
from m2cgen.interpreters import mixins | ||
from m2cgen.interpreters import utils | ||
from m2cgen.interpreters.interpreter import ToCodeInterpreter | ||
from m2cgen.interpreters.javascript.code_generator \ | ||
import JavascriptCodeGenerator | ||
|
||
|
||
class JavascriptInterpreter(ToCodeInterpreter, | ||
mixins.LinearAlgebraMixin): | ||
|
||
supported_bin_vector_ops = { | ||
ast.BinNumOpType.ADD: "addVectors", | ||
} | ||
|
||
supported_bin_vector_num_ops = { | ||
ast.BinNumOpType.MUL: "mulVectorNumber", | ||
} | ||
|
||
exponent_function_name = "Math.exp" | ||
power_function_name = "Math.pow" | ||
tanh_function_name = "Math.tanh" | ||
|
||
def __init__(self, indent=4, | ||
*args, **kwargs): | ||
self.indent = indent | ||
|
||
cg = JavascriptCodeGenerator(indent=indent) | ||
super(JavascriptInterpreter, self).__init__(cg, *args, **kwargs) | ||
|
||
def interpret(self, expr): | ||
self._cg.reset_state() | ||
self._reset_reused_expr_cache() | ||
|
||
args = [(True, self._feature_array_name)] | ||
|
||
with self._cg.function_definition( | ||
name="score", | ||
args=args): | ||
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.js") | ||
self._cg.add_code_lines(utils.get_file_content(filename)) | ||
|
||
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,19 @@ | ||
function addVectors(v1, v2) { | ||
let result = new Array(v1.length); | ||
|
||
for (let i = 0; i < v1.length; i++) { | ||
result[i] = v1[i] + v2[i]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function mulVectorNumber(v1, num) { | ||
let result = new Array(v1.length); | ||
|
||
for (let i = 0; i < v1.length; i++) { | ||
result[i] = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ pytest==4.1.1 | |
pytest-mock==1.10.0 | ||
coveralls==1.5.1 | ||
pytest-cov==2.6.1 | ||
py-mini-racer==0.1.18 |
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,31 @@ | ||
import os | ||
from py_mini_racer import py_mini_racer | ||
import m2cgen as m2c | ||
from tests.e2e.executors import base | ||
|
||
|
||
class JavascriptExecutor(base.BaseExecutor): | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
|
||
def predict(self, X): | ||
file_name = os.path.join(self._resource_tmp_dir, "model.js") | ||
|
||
with open(file_name, 'r') as myfile: | ||
code = myfile.read() | ||
|
||
caller = "score([" + ",".join(map(str, X)) + "]);\n" | ||
|
||
ctx = py_mini_racer.MiniRacer() | ||
result = ctx.eval(caller + code) | ||
|
||
return result | ||
|
||
def prepare(self): | ||
code = m2c.export_to_javascript(self.model) | ||
|
||
file_name = os.path.join(self._resource_tmp_dir, "model.js") | ||
|
||
with open(file_name, "w") as f: | ||
f.write(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.