From be9f5ce16ad5dec1af3c1ef744ee68fd6a9b9c55 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 13 Dec 2024 18:48:05 -0600 Subject: [PATCH 1/3] Add test file for top_k functionality --- dpctl/tests/test_usm_ndarray_top_k.py | 94 +++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 dpctl/tests/test_usm_ndarray_top_k.py diff --git a/dpctl/tests/test_usm_ndarray_top_k.py b/dpctl/tests/test_usm_ndarray_top_k.py new file mode 100644 index 0000000000..954c21a990 --- /dev/null +++ b/dpctl/tests/test_usm_ndarray_top_k.py @@ -0,0 +1,94 @@ +# Data Parallel Control (dpctl) +# +# Copyright 2020-2024 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import dpctl.tensor as dpt +from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported + + +@pytest.mark.parametrize( + "dtype", + [ + "i1", + "u1", + "i2", + "u2", + "i4", + "u4", + "i8", + "u8", + "f2", + "f4", + "f8", + "c8", + "c16", + ], +) +@pytest.mark.parametrize("n", [33, 255, 511, 1021, 8193]) +def test_topk_1d_largest(dtype, n): + q = get_queue_or_skip() + skip_if_dtype_not_supported(dtype, q) + + o = dpt.ones(n, dtype=dtype) + z = dpt.zeros(n, dtype=dtype) + zo = dpt.concat((o, z)) + inp = dpt.roll(zo, 734) + k = 5 + + s = dpt.top_k(inp, k, mode="largest") + assert s.values.shape == (k,) + assert s.values.dtype == inp.dtype + assert s.indices.shape == (k,) + assert dpt.all(s.values == dpt.ones(k, dtype=dtype)) + assert dpt.all(s.values == inp[s.indices]) + + +@pytest.mark.parametrize( + "dtype", + [ + "i1", + "u1", + "i2", + "u2", + "i4", + "u4", + "i8", + "u8", + "f2", + "f4", + "f8", + "c8", + "c16", + ], +) +@pytest.mark.parametrize("n", [33, 255, 257, 513, 1021, 8193]) +def test_topk_1d_smallest(dtype, n): + q = get_queue_or_skip() + skip_if_dtype_not_supported(dtype, q) + + o = dpt.ones(n, dtype=dtype) + z = dpt.zeros(n, dtype=dtype) + zo = dpt.concat((o, z)) + inp = dpt.roll(zo, 734) + k = 5 + + s = dpt.top_k(inp, k, mode="smallest") + assert s.values.shape == (k,) + assert s.values.dtype == inp.dtype + assert s.indices.shape == (k,) + assert dpt.all(s.values == dpt.zeros(k, dtype=dtype)) + assert dpt.all(s.values == inp[s.indices]) From ff813652572f0c6e70985d6083d9e96b0bcf143d Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Tue, 17 Dec 2024 15:05:19 -0800 Subject: [PATCH 2/3] Adds test to triage AMD CPU top_k failure --- dpctl/tests/test_usm_ndarray_top_k.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dpctl/tests/test_usm_ndarray_top_k.py b/dpctl/tests/test_usm_ndarray_top_k.py index 954c21a990..e78f96f399 100644 --- a/dpctl/tests/test_usm_ndarray_top_k.py +++ b/dpctl/tests/test_usm_ndarray_top_k.py @@ -92,3 +92,24 @@ def test_topk_1d_smallest(dtype, n): assert s.indices.shape == (k,) assert dpt.all(s.values == dpt.zeros(k, dtype=dtype)) assert dpt.all(s.values == inp[s.indices]) + + +# triage failing top k radix implementation on CPU +# replicates from Python behavior of radix sort topk implementation +def test_topk_largest_1d_radix_i1_255(): + get_queue_or_skip() + n = 255 + dt = "i1" + + o = dpt.ones(n, dtype=dt) + z = dpt.zeros(n, dtype=dt) + zo = dpt.concat((o, z)) + inp = dpt.roll(zo, 734) + k = 5 + + sorted = dpt.copy(dpt.sort(inp, descending=True, kind="radixsort")[:k]) + argsorted = dpt.copy( + dpt.argsort(inp, descending=True, kind="radixsort")[:k] + ) + assert dpt.all(sorted == dpt.ones(k, dtype=dt)) + assert dpt.all(sorted == inp[argsorted]) From 58da4169f58ef6ffb0cddecd33cecb2e18437924 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Wed, 18 Dec 2024 10:12:23 -0800 Subject: [PATCH 3/3] radix sort triage test now parametrizes for identical sizes to topk_1d_largest test --- dpctl/tests/test_usm_ndarray_top_k.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dpctl/tests/test_usm_ndarray_top_k.py b/dpctl/tests/test_usm_ndarray_top_k.py index e78f96f399..4e05615c80 100644 --- a/dpctl/tests/test_usm_ndarray_top_k.py +++ b/dpctl/tests/test_usm_ndarray_top_k.py @@ -96,9 +96,9 @@ def test_topk_1d_smallest(dtype, n): # triage failing top k radix implementation on CPU # replicates from Python behavior of radix sort topk implementation -def test_topk_largest_1d_radix_i1_255(): +@pytest.mark.parametrize("n", [33, 255, 511, 1021, 8193]) +def test_topk_largest_1d_radix_i1_255(n): get_queue_or_skip() - n = 255 dt = "i1" o = dpt.ones(n, dtype=dt)