From e899f6c79f8724849771328839874b8e919d225e Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Thu, 28 Mar 2024 18:17:54 -0400 Subject: [PATCH 1/2] Fixed several errors with calls and tests. The inclusive_scan_operations.py file was not calling the correct function, had to add that in with the correct c pointer calls. Dove into the c++ files for the dim assertion error, realized that was just an issue with the c++ file, fixed that as well. The test_inclusive.py file didn't have F16 coverage, checked for that as well. --- .../inclusive_scan_operations.py | 2 +- tests/test_inclusive.py | 206 ++++++++++++++++++ 2 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 tests/test_inclusive.py diff --git a/arrayfire_wrapper/lib/vector_algorithms/inclusive_scan_operations.py b/arrayfire_wrapper/lib/vector_algorithms/inclusive_scan_operations.py index b08cdb3..4db899f 100644 --- a/arrayfire_wrapper/lib/vector_algorithms/inclusive_scan_operations.py +++ b/arrayfire_wrapper/lib/vector_algorithms/inclusive_scan_operations.py @@ -28,7 +28,7 @@ def scan_by_key(key: AFArray, arr: AFArray, dim: int, op: BinaryOperator, inclus source: https://arrayfire.org/docs/group__scan__func__scanbykey.htm#gaaae150e0f197782782f45340d137b027 """ out = AFArray.create_null_pointer() - call_from_clib(scan.__name__, ctypes.pointer(out), key, arr, dim, op.value, inclusive_scan) + call_from_clib(scan_by_key.__name__, ctypes.pointer(out), key, arr, dim, op.value, inclusive_scan) return out diff --git a/tests/test_inclusive.py b/tests/test_inclusive.py new file mode 100644 index 0000000..92c942d --- /dev/null +++ b/tests/test_inclusive.py @@ -0,0 +1,206 @@ +import pytest + +import arrayfire_wrapper.dtypes as dtype +import arrayfire_wrapper.lib as wrapper +from tests.utility_functions import check_type_supported, get_all_types, get_float_types, get_real_types + +@pytest.mark.parametrize( + "shape", + [ + (), + (3,), + (3, 3), + (3, 3, 3), + (3, 3, 3, 3), + ], +) +@pytest.mark.parametrize("dtype_name", get_all_types()) +def test_accum_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: + """Test accumulate operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16: + pytest.skip() + values = wrapper.randu(shape, dtype_name) + result = wrapper.accum(values, 0) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + +@pytest.mark.parametrize( + "dim", + [ + 0, + 1, + 2, + 3, + ], +) +def test_accum_dims(dim: int) -> None: + """Test accumulate dimensions operation""" + shape = (3, 3) + values = wrapper.randu(shape, dtype.f32) + result = wrapper.accum(values, dim) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa +@pytest.mark.parametrize( + "invdim", + [ + -1, + 5, + ], +) +def test_accum_invdims(invdim: int) -> None: + """Test accumulate invalid dimensions operation""" + with pytest.raises(RuntimeError): + shape = (3, 3) + values = wrapper.randu(shape, dtype.f32) + result = wrapper.accum(values, invdim) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + +@pytest.mark.parametrize( + "shape", + [ + (), + (3,), + (3, 3), + (3, 3, 3), + (3, 3, 3, 3), + ], +) +@pytest.mark.parametrize("dtype_name", get_all_types()) +def test_scan_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: + """Test scan operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16: + pytest.skip() + values = wrapper.randu(shape, dtype_name) + result = wrapper.scan(values, 0, wrapper.BinaryOperator.ADD, True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}, dtype {dtype_name}" # noqa + +@pytest.mark.parametrize( + "dim", + [ + 0, + 1, + 2, + 3, + ], +) +def test_scan_dims(dim: int) -> None: + """Test scan dimensions operation""" + shape = (3, 3) + values = wrapper.randu(shape, dtype.f32) + result = wrapper.scan(values, dim, wrapper.BinaryOperator.ADD, True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for dimension: {dim}" # noqa +@pytest.mark.parametrize( + "invdim", + [ + -1, + 5, + ], +) +def test_scan_invdims(invdim: int) -> None: + """Test scan invalid dimensions operation""" + with pytest.raises(RuntimeError): + shape = (3, 3) + values = wrapper.randu(shape, dtype.f32) + result = wrapper.scan(values, invdim, wrapper.BinaryOperator.ADD, True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa +@pytest.mark.parametrize( + "binaryOp", + [ + 0, + 1, + 2, + 3, + ], +) +def test_scan_binaryOp(binaryOp: int) -> None: + """Test scan dimensions operation""" + shape = (3, 3) + values = wrapper.randu(shape, dtype.f32) + result = wrapper.scan(values, 0, wrapper.BinaryOperator(binaryOp), True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for operation: {binaryOp}" # noqa + +@pytest.mark.parametrize( + "shape", + [ + (), + (3,), + (3, 3), + (3, 3, 3), + (3, 3, 3, 3), + ], +) +@pytest.mark.parametrize("dtype_name", get_real_types()) +def test_scan_by_key_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: + """Test scan_by_key operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16 or dtype_name == dtype.f32 or dtype_name == dtype.uint16 or dtype_name == dtype.uint8 or dtype_name == dtype.int16: + pytest.skip() + values = wrapper.randu(shape, dtype_name) + result = wrapper.scan_by_key(values, values, 0, wrapper.BinaryOperator.ADD, True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}, dtype {dtype_name}" # noqa + +@pytest.mark.parametrize( + "dim", + [ + 0, + 1, + 2, + 3, + ], +) +def test_scan_by_key_dims(dim: int) -> None: + """Test scan_by_key dimensions operation""" + shape = (3, 3) + values = wrapper.randu(shape, dtype.int32) + result = wrapper.scan_by_key(values, values, dim, wrapper.BinaryOperator.ADD, True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for dimension: {dim}" # noqa + +@pytest.mark.parametrize( + "invdim", + [ + -1, + 5, + ], +) +def test_scan_by_key_invdims(invdim: int) -> None: + """Test scan_by_key invalid dimensions operation""" + with pytest.raises(RuntimeError): + shape = (3, 3) + values = wrapper.randu(shape, dtype.int32) + result = wrapper.scan_by_key(values, values, invdim, wrapper.BinaryOperator.ADD, True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa +@pytest.mark.parametrize( + "binaryOp", + [ + 0, + 1, + 2, + 3, + ], +) +def test_scan_by_key_binaryOp(binaryOp: int) -> None: + """Test scan_by_key dimensions operation""" + shape = (3, 3) + values = wrapper.randu(shape, dtype.int32) + result = wrapper.scan_by_key(values, values, 0, wrapper.BinaryOperator(binaryOp), True) + assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for operation: {binaryOp}" # noqa + +@pytest.mark.parametrize( + "shape", + [ + (), + (3,), + (3, 3), + (3, 3, 3), + (3, 3, 3, 3), + ], +) +@pytest.mark.parametrize("dtype_name", get_real_types()) +def test_where_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: + """Test where operation across all supported data types.""" + check_type_supported(dtype_name) + if dtype_name == dtype.f16: + pytest.skip() + values = wrapper.randu(shape, dtype_name) + result = wrapper.where(values) + assert wrapper.get_dims(result)[0] == 3 ** len(shape), f"failed for shape: {shape}, dtype {dtype_name}" # noqa From ee883e37969c1f95cd3858926a55e92eeacb4943 Mon Sep 17 00:00:00 2001 From: AzeezIsh Date: Thu, 28 Mar 2024 18:22:12 -0400 Subject: [PATCH 2/2] Adhered to checkstyle requirements. --- tests/test_inclusive.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/test_inclusive.py b/tests/test_inclusive.py index 92c942d..cdce26c 100644 --- a/tests/test_inclusive.py +++ b/tests/test_inclusive.py @@ -2,7 +2,8 @@ import arrayfire_wrapper.dtypes as dtype import arrayfire_wrapper.lib as wrapper -from tests.utility_functions import check_type_supported, get_all_types, get_float_types, get_real_types +from tests.utility_functions import check_type_supported, get_all_types, get_real_types + @pytest.mark.parametrize( "shape", @@ -24,6 +25,7 @@ def test_accum_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: result = wrapper.accum(values, 0) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + @pytest.mark.parametrize( "dim", [ @@ -39,6 +41,8 @@ def test_accum_dims(dim: int) -> None: values = wrapper.randu(shape, dtype.f32) result = wrapper.accum(values, dim) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + + @pytest.mark.parametrize( "invdim", [ @@ -54,6 +58,7 @@ def test_accum_invdims(invdim: int) -> None: result = wrapper.accum(values, invdim) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + @pytest.mark.parametrize( "shape", [ @@ -74,6 +79,7 @@ def test_scan_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: result = wrapper.scan(values, 0, wrapper.BinaryOperator.ADD, True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}, dtype {dtype_name}" # noqa + @pytest.mark.parametrize( "dim", [ @@ -89,6 +95,8 @@ def test_scan_dims(dim: int) -> None: values = wrapper.randu(shape, dtype.f32) result = wrapper.scan(values, dim, wrapper.BinaryOperator.ADD, True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for dimension: {dim}" # noqa + + @pytest.mark.parametrize( "invdim", [ @@ -103,6 +111,8 @@ def test_scan_invdims(invdim: int) -> None: values = wrapper.randu(shape, dtype.f32) result = wrapper.scan(values, invdim, wrapper.BinaryOperator.ADD, True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + + @pytest.mark.parametrize( "binaryOp", [ @@ -119,6 +129,7 @@ def test_scan_binaryOp(binaryOp: int) -> None: result = wrapper.scan(values, 0, wrapper.BinaryOperator(binaryOp), True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for operation: {binaryOp}" # noqa + @pytest.mark.parametrize( "shape", [ @@ -133,12 +144,19 @@ def test_scan_binaryOp(binaryOp: int) -> None: def test_scan_by_key_shape_dtypes(shape: tuple, dtype_name: dtype.Dtype) -> None: """Test scan_by_key operation across all supported data types.""" check_type_supported(dtype_name) - if dtype_name == dtype.f16 or dtype_name == dtype.f32 or dtype_name == dtype.uint16 or dtype_name == dtype.uint8 or dtype_name == dtype.int16: + if ( + dtype_name == dtype.f16 + or dtype_name == dtype.f32 + or dtype_name == dtype.uint16 + or dtype_name == dtype.uint8 + or dtype_name == dtype.int16 + ): pytest.skip() values = wrapper.randu(shape, dtype_name) result = wrapper.scan_by_key(values, values, 0, wrapper.BinaryOperator.ADD, True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}, dtype {dtype_name}" # noqa + @pytest.mark.parametrize( "dim", [ @@ -155,6 +173,7 @@ def test_scan_by_key_dims(dim: int) -> None: result = wrapper.scan_by_key(values, values, dim, wrapper.BinaryOperator.ADD, True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for dimension: {dim}" # noqa + @pytest.mark.parametrize( "invdim", [ @@ -169,6 +188,8 @@ def test_scan_by_key_invdims(invdim: int) -> None: values = wrapper.randu(shape, dtype.int32) result = wrapper.scan_by_key(values, values, invdim, wrapper.BinaryOperator.ADD, True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for shape: {shape}" # noqa + + @pytest.mark.parametrize( "binaryOp", [ @@ -185,6 +206,7 @@ def test_scan_by_key_binaryOp(binaryOp: int) -> None: result = wrapper.scan_by_key(values, values, 0, wrapper.BinaryOperator(binaryOp), True) assert wrapper.get_dims(result)[0 : len(shape)] == shape, f"failed for operation: {binaryOp}" # noqa + @pytest.mark.parametrize( "shape", [