Unverified Commit fe77eac5 authored by Nikita Titov's avatar Nikita Titov Committed by GitHub
Browse files

[tests][python] added test for huge string model (#1964)

* added test for huge string model

* fixed tree sizes field

* simplified model structure

* fixed test and added try/except
parent 5c399840
...@@ -30,7 +30,7 @@ install: ...@@ -30,7 +30,7 @@ install:
- ps: $env:LGB_VER = (Get-Content VERSION.txt).trim() - ps: $env:LGB_VER = (Get-Content VERSION.txt).trim()
- conda config --set always_yes yes --set changeps1 no - conda config --set always_yes yes --set changeps1 no
- conda update -q conda - conda update -q conda
- conda create -q -n test-env python=%PYTHON_VERSION% matplotlib nose numpy pandas pytest python-graphviz scikit-learn scipy - conda create -q -n test-env python=%PYTHON_VERSION% matplotlib nose numpy pandas psutil pytest python-graphviz scikit-learn scipy
- activate test-env - activate test-env
build_script: build_script:
......
...@@ -58,7 +58,7 @@ if [[ $TASK == "if-else" ]]; then ...@@ -58,7 +58,7 @@ if [[ $TASK == "if-else" ]]; then
exit 0 exit 0
fi fi
conda install -q -y -n $CONDA_ENV matplotlib nose numpy pandas pytest python-graphviz scikit-learn scipy conda install -q -y -n $CONDA_ENV matplotlib nose numpy pandas psutil pytest python-graphviz scikit-learn scipy
if [[ $OS_NAME == "macos" ]] && [[ $COMPILER == "clang" ]]; then if [[ $OS_NAME == "macos" ]] && [[ $COMPILER == "clang" ]]; then
sudo ln -sf `ls -d "$(brew --cellar libomp)"/*/lib`/* $CONDA_PREFIX/lib || exit -1 # fix "OMP: Error #15: Initializing libiomp5.dylib, but found libomp.dylib already initialized." (OpenMP library conflict due to conda's MKL) sudo ln -sf `ls -d "$(brew --cellar libomp)"/*/lib`/* $CONDA_PREFIX/lib || exit -1 # fix "OMP: Error #15: Initializing libiomp5.dylib, but found libomp.dylib already initialized." (OpenMP library conflict due to conda's MKL)
......
...@@ -146,7 +146,7 @@ jobs: ...@@ -146,7 +146,7 @@ jobs:
createCustomEnvironment: true createCustomEnvironment: true
updateConda: true updateConda: true
environmentName: $(CONDA_ENV) environmentName: $(CONDA_ENV)
packageSpecs: 'python=$(PYTHON_VERSION) matplotlib nose numpy pandas pytest python-graphviz scikit-learn scipy' packageSpecs: 'python=$(PYTHON_VERSION) matplotlib nose numpy pandas psutil pytest python-graphviz scikit-learn scipy'
createOptions: '-q' createOptions: '-q'
- powershell: $(Build.SourcesDirectory)/.ci/test_windows.ps1 - powershell: $(Build.SourcesDirectory)/.ci/test_windows.ps1
displayName: Test displayName: Test
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import copy import copy
import math import math
import os import os
import psutil
import random import random
import unittest import unittest
...@@ -1208,3 +1209,30 @@ class TestEngine(unittest.TestCase): ...@@ -1208,3 +1209,30 @@ class TestEngine(unittest.TestCase):
# binary metric with non-default num_class for custom objective # binary metric with non-default num_class for custom objective
self.assertRaises(lgb.basic.LightGBMError, get_cv_result, self.assertRaises(lgb.basic.LightGBMError, get_cv_result,
params_class_3_verbose, metrics='binary_error', fobj=custom_obj) params_class_3_verbose, metrics='binary_error', fobj=custom_obj)
@unittest.skipIf(psutil.virtual_memory().available / 1024 / 1024 / 1024 < 3, 'not enough RAM')
def test_model_size(self):
X, y = load_boston(True)
data = lgb.Dataset(X, y)
bst = lgb.train({'verbose': -1}, data, num_boost_round=2)
y_pred = bst.predict(X)
model_str = bst.model_to_string()
one_tree = model_str[model_str.find('Tree=1'):model_str.find('end of trees')]
one_tree_size = len(one_tree)
one_tree = one_tree.replace('Tree=1', 'Tree={}')
multiplier = 100
total_trees = multiplier + 2
try:
new_model_str = (model_str[:model_str.find('tree_sizes')]
+ '\n\n'
+ model_str[model_str.find('Tree=0'):model_str.find('end of trees')]
+ (one_tree * multiplier).format(*range(2, total_trees))
+ model_str[model_str.find('end of trees'):]
+ ' ' * (2**31 - one_tree_size * total_trees))
self.assertGreater(len(new_model_str), 2**31)
bst.model_from_string(new_model_str, verbose=False)
self.assertEqual(bst.num_trees(), total_trees)
y_pred_new = bst.predict(X, num_iteration=2)
np.testing.assert_allclose(y_pred, y_pred_new)
except MemoryError:
self.skipTest('not enough RAM')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment