Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkornaukhov03 committed May 6, 2024
1 parent eb6bc25 commit 9f136a0
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/python/lib/kphp_run_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ def run_with_php(self, extra_options=[], runs_cnt=1):

return php_proc.returncode == 0

def run_with_kphp(self, runs_cnt=1):
def run_with_kphp(self, runs_cnt=1, args=[]):
self._clear_working_dir(self._kphp_runtime_tmp_dir)

sanitizer_log_name = "kphp_runtime_sanitizer_log"
env, sanitizer_glob_mask = self._prepare_sanitizer_env(self._kphp_runtime_tmp_dir, sanitizer_log_name)

cmd = [self._kphp_runtime_bin, "--once={}".format(runs_cnt), "--profiler-log-prefix", "profiler.log",
"--worker-queries-to-reload", "1"]
"--worker-queries-to-reload", "1"] + args
if not os.getuid():
cmd += ["-u", "root", "-g", "root"]
kphp_server_proc = subprocess.Popen(cmd,
Expand Down
Empty file.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/** @return shape(features_map:float[], ans:float[])[] */
function getInput() {
return [
shape([
"features_map" => [
"year" => 1996,
"count" => 197,
"season" => 1
],
"ans" => [
-0.4315705250918395,
-0.07602514583990287,
0.5075956709317426
]
]),
shape([
"features_map" => [
"year" => 1968,
"count" => 37,
"season" => 1
],
"ans" => [
-0.7547556359399779,
-0.9511000859169567,
1.7058557218569348
]
]),
shape([
"features_map" => [
"year" => 2002,
"count" => 77,
"season" => 0
],
"ans" => [
-0.1531870128758389,
0.3682398885336728,
-0.21505287565783404
]
]),
shape([
"features_map" => [
"year" => 1948,
"count" => 59,
"season" => 0
],
"ans" => [
-0.04081236490074001,
-0.7956756034401067,
0.8364879683408469
]
])
];
}

const eps = 1e-5;

function ensure($x)
{
if (!$x) {
die(1);
}
}

function main()
{
$input = getInput();
foreach ($input as $row) {
$predictions = kml_catboost_predict_ht_multi("catboost_multiclass_tutorial_ht_catnum", $row['features_map']);
ensure(count($predictions) == count($row['ans']));
for ($i = 0; $i < count($predictions); $i++) {
ensure(abs($predictions[$i] - $row['ans'][$i]) < eps);
}
}
}
main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/** @return shape(float_features:float[], cat_features:string[], ans:float)[] */
function getInput() {
return [
shape([
"float_features" => [
1
],
"cat_features" => [
"a"
],
"ans" => 0.7363564860973357
]),
shape([
"float_features" => [
2
],
"cat_features" => [
"a"
],
"ans" => 1.4672203311538694
]),
shape([
"float_features" => [
3
],
"cat_features" => [
"a"
],
"ans" => -2.276542165549472
]),
shape([
"float_features" => [
3
],
"cat_features" => [
"b"
],
"ans" => -0.9489540606737137
]),
shape([
"float_features" => [
5
],
"cat_features" => [
"b"
],
"ans" => -0.9489540606737137
]),
];
}

const eps = 1e-5;

function ensure($x)
{
if (!$x) {
die(1);
}
}

function main()
{
$input = getInput();
foreach ($input as $row) {
$prediction = kml_catboost_predict_vectors("catboost_tiny_1float_1hot_10trees", $row['float_features'], $row['cat_features']);
ensure(abs($prediction - $row['ans']) < eps);
}
}
main();
20 changes: 20 additions & 0 deletions tests/python/tests/kml/catboost/php/unrelated_to_inference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

function ensure($x) {
if (!$x) {
die(1);
}
}

function main() {
$model_name = "catboost_tiny_1float_1hot_10trees";

ensure(kml_model_exists($model_name));
ensure(["x", "y"] === kml_get_feature_names($model_name));
ensure("val_1" == kml_get_custom_property($model_name, "property_1"));
ensure("val_2, val_3" === kml_get_custom_property($model_name, "property_2"));
ensure(null === kml_get_custom_property($model_name, "property_absent"));
ensure(null === kml_xgboost_predict_matrix("absent_model", []));
}

main()
36 changes: 36 additions & 0 deletions tests/python/tests/kml/test_kml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

from python.lib.testcase import KphpCompilerAutoTestCase


class TestKML(KphpCompilerAutoTestCase):
def compile_and_check_success(self, php_script_path, kml_dir):
kml_dir = os.path.join(self.test_dir, kml_dir)
args = ["--kml-dir={}".format(kml_dir)]

once_runner = self.make_kphp_once_runner(php_script_path)

self.assertTrue(once_runner.compile_with_kphp({}))
self.assertTrue(once_runner.run_with_kphp(args=args), "Got KPHP runtime error")

def test_xgboost_tiny_ht_direct_int_keys_to_fvalue(self):
self.compile_and_check_success(php_script_path="xgboost/php/xgb_tiny_ht_direct_int_keys_to_fvalue.php",
kml_dir="xgboost/kml")
self.compile_and_check_success(php_script_path="xgboost/php/xgb_tiny_ht_direct_int_keys_to_fvalue.php",
kml_dir="xgboost") # Traverse directories recursively

def test_xgb_tiny_ht_remap_str_keys_to_fvalue(self):
self.compile_and_check_success(php_script_path="xgboost/php/xgb_tiny_ht_remap_str_keys_to_fvalue.php",
kml_dir="xgboost/kml")

def test_catboost_tiny_1float_1hot_10trees(self):
self.compile_and_check_success(php_script_path="catboost/php/catboost_tiny_1float_1hot_10trees.php",
kml_dir="catboost/kml")

def test_catboost_multiclass_tutorial_ht_catnum(self):
self.compile_and_check_success(php_script_path="catboost/php/catboost_multiclass_tutorial_ht_catnum.php",
kml_dir="catboost/kml")

def test_unrelated_to_inference(self):
self.compile_and_check_success(php_script_path="catboost/php/unrelated_to_inference.php",
kml_dir="catboost/kml")
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
const eps = 1e-5;

function ensure($x)
{
if (!$x) {
die(1);
}
}

function main()
{
$inputs = array(
array("0" => 1, "1" => 1),
array("0" => 2, "1" => 2),
array("0" => 100, "1" => 100),
array("0" => 4, "1" => -10),
array("0" => 5, "1" => -555),
);
$expected = array(
0.745589,
0.437018,
0.437018,
0.156840,
0.156840,
);

$ans = kml_xgboost_predict_matrix("xgb_tiny_ht_direct_int_keys_to_fvalue", $inputs);

$length = count($expected);
ensure($length == count($ans));
for ($i = 0; $i < $length; $i++) {
ensure(abs($expected[$i] - $ans[$i]) < eps);
}
}
main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
const eps = 1e-5;

function ensure($x)
{
if (!$x) {
die(1);
}
}

function main()
{
$inputs = array(
array("height" => 1, "weight" => 1, "useless_feature" => 0.1),
array("height" => 2, "weight" => 2, "useless_feature" => 0.12),
array("height" => 100, "weight" => 100, "useless_feature" => 0.123),
array("height" => 4, "weight" => -10, "useless_feature" => 0.1234),
array("height" => 5, "weight" => -555, "useless_feature" => 0.12345),
);
$expected = array(
0.745589,
0.437018,
0.437018,
0.156840,
0.156840,
);

$ans = kml_xgboost_predict_matrix("xgb_tiny_ht_remap_str_keys_to_fvalue", $inputs);

$length = count($expected);
ensure($length == count($ans));
for ($i = 0; $i < $length; $i++) {
ensure(abs($expected[$i] - $ans[$i]) < eps);
}
}
main();

0 comments on commit 9f136a0

Please sign in to comment.