Skip to content

Commit

Permalink
Make some GPU tests deterministic (#5229)
Browse files Browse the repository at this point in the history
  • Loading branch information
RAMitchell authored Jan 25, 2020
1 parent 3eb1279 commit 1b3947d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions tests/python-gpu/test_from_columnar.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,23 @@ def test_from_cudf(self):
def test_cudf_training(self):
from cudf import DataFrame as df
import pandas as pd
np.random.seed(1)
X = pd.DataFrame(np.random.randn(50, 10))
y = pd.DataFrame(np.random.randn(50))
weights = np.random.random(50)
weights = np.random.random(50) + 1.0
cudf_weights = df.from_pandas(pd.DataFrame(weights))
base_margin = np.random.random(50)
cudf_base_margin = df.from_pandas(pd.DataFrame(base_margin))

evals_result_cudf = {}
dtrain_cudf = xgb.DMatrix(df.from_pandas(X), df.from_pandas(y), weight=cudf_weights,
base_margin=cudf_base_margin)
xgb.train({'gpu_id': 0}, dtrain_cudf, evals=[(dtrain_cudf, "train")],
params = {'gpu_id': 0, 'nthread': 1}
xgb.train(params, dtrain_cudf, evals=[(dtrain_cudf, "train")],
evals_result=evals_result_cudf)
evals_result_np = {}
dtrain_np = xgb.DMatrix(X, y, weight=weights, base_margin=base_margin)
xgb.train({}, dtrain_np, evals=[(dtrain_np, "train")],
xgb.train(params, dtrain_np, evals=[(dtrain_np, "train")],
evals_result=evals_result_np)
assert np.array_equal(evals_result_cudf["train"]["rmse"], evals_result_np["train"]["rmse"])

Expand Down
9 changes: 6 additions & 3 deletions tests/python-gpu/test_from_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,24 @@ def test_from_cupy(self):
@pytest.mark.skipif(**tm.no_cupy())
def test_cupy_training(self):
import cupy as cp
np.random.seed(1)
cp.random.seed(1)
X = cp.random.randn(50, 10, dtype="float32")
y = cp.random.randn(50, dtype="float32")
weights = np.random.random(50)
weights = np.random.random(50) + 1
cupy_weights = cp.array(weights)
base_margin = np.random.random(50)
cupy_base_margin = cp.array(base_margin)

evals_result_cupy = {}
dtrain_cp = xgb.DMatrix(X, y, weight=cupy_weights, base_margin=cupy_base_margin)
xgb.train({'gpu_id': 0}, dtrain_cp, evals=[(dtrain_cp, "train")],
params = {'gpu_id': 0, 'nthread': 1}
xgb.train(params, dtrain_cp, evals=[(dtrain_cp, "train")],
evals_result=evals_result_cupy)
evals_result_np = {}
dtrain_np = xgb.DMatrix(cp.asnumpy(X), cp.asnumpy(y), weight=weights,
base_margin=base_margin)
xgb.train({'gpu_id': 0}, dtrain_np, evals=[(dtrain_np, "train")],
xgb.train(params, dtrain_np, evals=[(dtrain_np, "train")],
evals_result=evals_result_np)
assert np.array_equal(evals_result_cupy["train"]["rmse"], evals_result_np["train"]["rmse"])

Expand Down

0 comments on commit 1b3947d

Please sign in to comment.