Skip to content

Fix tests for using pytest #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions numba_dppy/tests/test_controllable_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import numba
import numba_dppy
from numba_dppy.testing import unittest
from numba.tests.support import captured_stderr
import dpctl
import warnings


@unittest.skipUnless(dpctl.has_gpu_queues(), "test only on GPU system")
Expand All @@ -24,7 +24,7 @@ def inner_call_fallback():
return a

numba_dppy.compiler.DEBUG = 1
with captured_stderr() as msg_fallback_true:
with warnings.catch_warnings(record=True) as w:
with dpctl.device_context("opencl:gpu") as gpu_queue:
dppy = numba.njit(parallel=True)(inner_call_fallback)
dppy_fallback_true = dppy()
Expand All @@ -33,9 +33,7 @@ def inner_call_fallback():
numba_dppy.compiler.DEBUG = 0

np.testing.assert_array_equal(dppy_fallback_true, ref_result)
self.assertTrue(
"Failed to lower parfor on DPPY-device" in msg_fallback_true.getvalue()
)
self.assertIn("Failed to lower parfor on DPPY-device", str(w[-1].message))

@unittest.expectedFailure
def test_dppy_fallback_false(self):
Expand All @@ -55,7 +53,7 @@ def inner_call_fallback():
try:
numba_dppy.compiler.DEBUG = 1
numba_dppy.config.FALLBACK_ON_CPU = 0
with captured_stderr() as msg_fallback_true:
with warnings.catch_warnings(record=True) as w:
with dpctl.device_context("opencl:gpu") as gpu_queue:
dppy = numba.njit(parallel=True)(inner_call_fallback)
dppy_fallback_false = dppy()
Expand All @@ -66,8 +64,8 @@ def inner_call_fallback():
numba_dppy.compiler.DEBUG = 0

not np.testing.assert_array_equal(dppy_fallback_false, ref_result)
not self.assertTrue(
"Failed to lower parfor on DPPY-device" in msg_fallback_true.getvalue()
self.assertNotIn(
"Failed to lower parfor on DPPY-device", str(w[-1].message)
)


Expand Down
14 changes: 9 additions & 5 deletions numba_dppy/tests/test_dppy_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import numba
import unittest
from numba.tests.support import captured_stderr
import dpctl
import warnings


@unittest.skipUnless(dpctl.has_gpu_queues(), "test only on GPU system")
Expand All @@ -22,14 +22,16 @@ def inner_call_fallback():

return a

with captured_stderr() as msg, dpctl.device_context("opencl:gpu"):
with warnings.catch_warnings(record=True) as w, dpctl.device_context(
"opencl:gpu"
):
dppy = numba.njit(inner_call_fallback)
dppy_result = dppy()

ref_result = inner_call_fallback()

np.testing.assert_array_equal(dppy_result, ref_result)
self.assertTrue("Failed to lower parfor on DPPY-device" in msg.getvalue())
self.assertIn("Failed to lower parfor on DPPY-device", str(w[-1].message))

def test_dppy_fallback_reductions(self):
def reduction(a):
Expand All @@ -39,14 +41,16 @@ def reduction(a):
return b

a = np.ones(10)
with captured_stderr() as msg, dpctl.device_context("opencl:gpu"):
with warnings.catch_warnings(record=True) as w, dpctl.device_context(
"opencl:gpu"
):
dppy = numba.njit(reduction)
dppy_result = dppy(a)

ref_result = reduction(a)

np.testing.assert_array_equal(dppy_result, ref_result)
self.assertTrue("Failed to lower parfor on DPPY-device" in msg.getvalue())
self.assertIn("Failed to lower parfor on DPPY-device", str(w[-1].message))


if __name__ == "__main__":
Expand Down
26 changes: 13 additions & 13 deletions numba_dppy/tests/test_math_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def driver(a, jitfunc):
return b


def test_driver(input_arr, device_ty, jitfunc):
def check_driver(input_arr, device_ty, jitfunc):
out_actual = None
if device_ty == "GPU":
with dpctl.device_context("opencl:gpu") as gpu_queue:
Expand All @@ -79,65 +79,65 @@ def test_driver(input_arr, device_ty, jitfunc):
@unittest.skipUnless(dpctl.has_cpu_queues(), "test only on CPU system")
class TestDPPYMathFunctionsCPU(unittest.TestCase):
def test_fabs_cpu(self):
b_actual = test_driver(a, "CPU", dppy_fabs)
b_actual = check_driver(a, "CPU", dppy_fabs)
b_expected = np.fabs(a)
self.assertTrue(np.all(b_actual == b_expected))

def test_sin_cpu(self):
b_actual = test_driver(a, "CPU", dppy_sin)
b_actual = check_driver(a, "CPU", dppy_sin)
b_expected = np.sin(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_cos_cpu(self):
b_actual = test_driver(a, "CPU", dppy_cos)
b_actual = check_driver(a, "CPU", dppy_cos)
b_expected = np.cos(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_exp_cpu(self):
b_actual = test_driver(a, "CPU", dppy_exp)
b_actual = check_driver(a, "CPU", dppy_exp)
b_expected = np.exp(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_sqrt_cpu(self):
b_actual = test_driver(a, "CPU", dppy_sqrt)
b_actual = check_driver(a, "CPU", dppy_sqrt)
b_expected = np.sqrt(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_log_cpu(self):
b_actual = test_driver(a, "CPU", dppy_log)
b_actual = check_driver(a, "CPU", dppy_log)
b_expected = np.log(a)
self.assertTrue(np.allclose(b_actual, b_expected))


@unittest.skipUnless(dpctl.has_gpu_queues(), "test only on GPU system")
class TestDPPYMathFunctionsGPU(unittest.TestCase):
def test_fabs_gpu(self):
b_actual = test_driver(a, "GPU", dppy_fabs)
b_actual = check_driver(a, "GPU", dppy_fabs)
b_expected = np.fabs(a)
self.assertTrue(np.all(b_actual == b_expected))

def test_sin_gpu(self):
b_actual = test_driver(a, "GPU", dppy_sin)
b_actual = check_driver(a, "GPU", dppy_sin)
b_expected = np.sin(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_cos_gpu(self):
b_actual = test_driver(a, "GPU", dppy_cos)
b_actual = check_driver(a, "GPU", dppy_cos)
b_expected = np.cos(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_exp_gpu(self):
b_actual = test_driver(a, "GPU", dppy_exp)
b_actual = check_driver(a, "GPU", dppy_exp)
b_expected = np.exp(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_sqrt_gpu(self):
b_actual = test_driver(a, "GPU", dppy_sqrt)
b_actual = check_driver(a, "GPU", dppy_sqrt)
b_expected = np.sqrt(a)
self.assertTrue(np.allclose(b_actual, b_expected))

def test_log_gpu(self):
b_actual = test_driver(a, "GPU", dppy_log)
b_actual = check_driver(a, "GPU", dppy_log)
b_expected = np.log(a)
self.assertTrue(np.allclose(b_actual, b_expected))

Expand Down