Beispiel #1
0
def test_softmax_expr():
    expr = ast.SoftmaxExpr([ast.NumVal(2.0), ast.NumVal(3.0)])

    expected_code = """
import "math"
func score(input []float64) []float64 {
    return softmax([]float64{2.0, 3.0})
}
func softmax(x []float64) []float64 {
    size := len(x)
    result := make([]float64, size)
    max := x[0]
    for _, v := range x {
        if (v > max) {
            max = v
        }
    }
    sum := 0.0
    for i := 0; i < size; i++ {
        result[i] = math.Exp(x[i] - max)
        sum += result[i]
    }
    for i := 0; i < size; i++ {
        result[i] /= sum
    }
    return result
}
"""

    interpreter = GoInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #2
0
def test_dependable_condition():
    left = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
            ast.NumVal(1), ast.NumVal(2)), ast.NumVal(2), ast.BinNumOpType.ADD)

    right = ast.BinNumExpr(ast.NumVal(1), ast.NumVal(2), ast.BinNumOpType.DIV)
    bool_test = ast.CompExpr(left, right, ast.CompOpType.GTE)

    expr = ast.IfExpr(bool_test, ast.NumVal(1), ast.FeatureRef(0))

    expected_code = """
func score(input []float64) float64 {
    var var0 float64
    var var1 float64
    if (1.0) == (1.0) {
        var1 = 1.0
    } else {
        var1 = 2.0
    }
    if ((var1) + (2.0)) >= ((1.0) / (2.0)) {
        var0 = 1.0
    } else {
        var0 = input[0]
    }
    return var0
}"""

    interpreter = GoInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #3
0
def test_bin_vector_num_expr():
    expr = ast.BinVectorNumExpr(ast.VectorVal([ast.NumVal(1),
                                               ast.NumVal(2)]), ast.NumVal(1),
                                ast.BinNumOpType.MUL)

    interpreter = GoInterpreter()

    expected_code = """
func addVectors(v1, v2 []float64) []float64 {
    result := make([]float64, len(v1))
    for i := 0; i < len(v1); i++ {
        result[i] = v1[i] + v2[i]
    }
    return result
}
func mulVectorNumber(v1 []float64, num float64) []float64 {
    result := make([]float64, len(v1))
    for i := 0; i < len(v1); i++ {
        result[i] = v1[i] * num
    }
    return result
}
func score(input []float64) []float64 {
    return mulVectorNumber([]float64{1.0, 2.0}, 1.0)
}"""
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #4
0
def test_raw_array():
    expr = ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])

    expected_code = """
func score(input []float64) []float64 {
    return []float64{3.0, 4.0}
}"""
    interpreter = GoInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #5
0
    def __init__(self, model):
        self.model_name = "score"
        self.model = model
        self.interpreter = GoInterpreter()

        assembler_cls = get_assembler_cls(model)
        self.model_ast = assembler_cls(model).assemble()

        self.exec_path = None
Beispiel #6
0
def test_atan_expr():
    expr = ast.AtanExpr(ast.NumVal(2.0))

    interpreter = GoInterpreter()

    expected_code = """
import "math"
func score(input []float64) float64 {
    return math.Atan(2.0)
}"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #7
0
def test_log1p_expr():
    expr = ast.Log1pExpr(ast.NumVal(2.0))

    expected_code = """
import "math"
func score(input []float64) float64 {
    return math.Log1p(2.0)
}
"""

    interpreter = GoInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #8
0
def test_pow_expr():
    expr = ast.PowExpr(ast.NumVal(2.0), ast.NumVal(3.0))

    expected_code = """
import "math"
func score(input []float64) float64 {
    return math.Pow(2.0, 3.0)
}
"""

    interpreter = GoInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #9
0
def test_bin_num_expr():
    expr = ast.BinNumExpr(
        ast.BinNumExpr(ast.FeatureRef(0), ast.NumVal(-2),
                       ast.BinNumOpType.DIV), ast.NumVal(2),
        ast.BinNumOpType.MUL)

    interpreter = GoInterpreter()

    expected_code = """
func score(input []float64) float64 {
    return ((input[0]) / (-2.0)) * (2.0)
}"""
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #10
0
def test_reused_expr():
    reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
    expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)

    interpreter = GoInterpreter()

    expected_code = """
import "math"
func score(input []float64) float64 {
    var var0 float64
    var0 = math.Exp(1.0)
    return (var0) / (var0)
}"""

    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #11
0
def test_if_expr():
    expr = ast.IfExpr(
        ast.CompExpr(ast.NumVal(1), ast.FeatureRef(0), ast.CompOpType.EQ),
        ast.NumVal(2), ast.NumVal(3))

    interpreter = GoInterpreter()

    expected_code = """
func score(input []float64) float64 {
    var var0 float64
    if (1.0) == (input[0]) {
        var0 = 2.0
    } else {
        var0 = 3.0
    }
    return var0
}"""
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #12
0
def test_multi_output():
    expr = ast.IfExpr(
        ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
        ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
        ast.VectorVal([ast.NumVal(3), ast.NumVal(4)]))

    expected_code = """
func score(input []float64) []float64 {
    var var0 []float64
    if (1.0) == (1.0) {
        var0 = []float64{1.0, 2.0}
    } else {
        var0 = []float64{3.0, 4.0}
    }
    return var0
}"""
    interpreter = GoInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #13
0
def test_sigmoid_expr():
    expr = ast.SigmoidExpr(ast.NumVal(2.0))

    expected_code = """
import "math"
func score(input []float64) float64 {
    return sigmoid(2.0)
}
func sigmoid(x float64) float64 {
    if (x < 0.0) {
        z := math.Exp(x)
        return z / (1.0 + z)
    }
    return 1.0 / (1.0 + math.Exp(-x))
}
"""

    interpreter = GoInterpreter()
    assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #14
0
def test_nested_condition():
    left = ast.BinNumExpr(
        ast.IfExpr(
            ast.CompExpr(ast.NumVal(1), ast.NumVal(1), ast.CompOpType.EQ),
            ast.NumVal(1), ast.NumVal(2)), ast.NumVal(2), ast.BinNumOpType.ADD)

    bool_test = ast.CompExpr(ast.NumVal(1), left, ast.CompOpType.EQ)

    expr_nested = ast.IfExpr(bool_test, ast.FeatureRef(2), ast.NumVal(2))

    expr = ast.IfExpr(bool_test, expr_nested, ast.NumVal(2))

    expected_code = """
func score(input []float64) float64 {
    var var0 float64
    var var1 float64
    if (1.0) == (1.0) {
        var1 = 1.0
    } else {
        var1 = 2.0
    }
    if (1.0) == ((var1) + (2.0)) {
        var var2 float64
        if (1.0) == (1.0) {
            var2 = 1.0
        } else {
            var2 = 2.0
        }
        if (1.0) == ((var2) + (2.0)) {
            var0 = input[2]
        } else {
            var0 = 2.0
        }
    } else {
        var0 = 2.0
    }
    return var0
}"""
    interpreter = GoInterpreter()
    utils.assert_code_equal(interpreter.interpret(expr), expected_code)
Beispiel #15
0
class GoExecutor(BaseExecutor):

    def __init__(self, model):
        self.model_name = "score"
        self.model = model
        self.interpreter = GoInterpreter()

        assembler_cls = get_assembler_cls(model)
        self.model_ast = assembler_cls(model).assemble()

        self.exec_path = None

    def predict(self, X):
        exec_args = [str(self.exec_path), *map(utils.format_arg, 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 = EXECUTOR_CODE_TPL.format(
            model_code=self.interpreter.interpret(self.model_ast),
            print_code=print_code)

        file_name = self._resource_tmp_dir / f"{self.model_name}.go"
        utils.write_content_to_file(executor_code, file_name)

        self.exec_path = self._resource_tmp_dir / self.model_name
        subprocess.call([
            "go",
            "build",
            "-o",
            str(self.exec_path),
            str(file_name)
        ])