From 3a8f39a3154fb5138359141c085a3758f60b239b Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Mon, 23 Jan 2023 09:32:55 +0100 Subject: [PATCH 1/4] nvcomp tests: update hardcoded lengths --- python/tests/test_nvcomp.py | 42 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/python/tests/test_nvcomp.py b/python/tests/test_nvcomp.py index 5b2bd8c0fa..d3ff09b2c1 100644 --- a/python/tests/test_nvcomp.py +++ b/python/tests/test_nvcomp.py @@ -10,6 +10,17 @@ libnvcomp = pytest.importorskip("kvikio.nvcomp") +# TODO: don't hardcode the following expected values +LEN = { + "ANS": 11144, + "Bitcomp": 3208, + "Cascaded": 600, + "Gdeflate": 760, + "LZ4": 393, + "Snappy": 3548, +} + + def managers(): return [ libnvcomp.ANSManager, @@ -70,7 +81,7 @@ def test_ans_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.ANSManager(**inputs) final = compressor.compress(data) - assert len(final) == 11144 + assert len(final) == LEN["ANS"] @pytest.mark.parametrize( @@ -93,7 +104,7 @@ def test_bitcomp_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.BitcompManager(**inputs) final = compressor.compress(data) - assert len(final) == 3208 + assert len(final) == LEN["Bitcomp"] @pytest.mark.parametrize( @@ -104,7 +115,7 @@ def test_bitcomp_inputs(inputs): {"algo": 1}, {"algo": 2}, ], - [3208, 3208, 3208], + [LEN["Bitcomp"], LEN["Bitcomp"], LEN["Bitcomp"]], ), ) def test_bitcomp_algorithms(inputs, expected): @@ -167,7 +178,7 @@ def test_cascaded_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.CascadedManager(**inputs) final = compressor.compress(data) - assert len(final) == 600 + assert len(final) == LEN["Cascaded"] @pytest.mark.parametrize( @@ -192,7 +203,7 @@ def test_gdeflate_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.GdeflateManager(**inputs) final = compressor.compress(data) - assert len(final) == 732 + assert len(final) == LEN["Gdeflate"] @pytest.mark.parametrize( @@ -201,7 +212,7 @@ def test_gdeflate_inputs(inputs): [ {"algo": 0}, ], - [732], + [LEN["Gdeflate"]], ), ) def test_gdeflate_algorithms(inputs, expected): @@ -215,7 +226,8 @@ def test_gdeflate_algorithms(inputs, expected): @pytest.mark.xfail(raises=ValueError) @pytest.mark.parametrize( - "inputs, expected", zip([{"algo": 1}, {"algo": 2}], [732, 732]) + "inputs, expected", + zip([{"algo": 1}, {"algo": 2}], [LEN["Gdeflate"], LEN["Gdeflate"]]), ) def test_gdeflate_algorithms_not_implemented(inputs, expected): size = 10000 @@ -248,7 +260,7 @@ def test_lz4_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.LZ4Manager(**inputs) final = compressor.compress(data) - assert len(final) == 393 + assert len(final) == LEN["LZ4"] @pytest.mark.parametrize( @@ -268,7 +280,7 @@ def test_snappy_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.SnappyManager(**inputs) final = compressor.compress(data) - assert len(final) == 3548 + assert len(final) == LEN["Snappy"] @pytest.mark.parametrize( @@ -432,17 +444,7 @@ def test_get_required_scratch_buffer_size(manager, expected): @pytest.mark.parametrize( "manager, expected", - zip( - managers(), - [ - 11144, # ANS - 3208, # Bitcomp - 600, # Cascaded - 732, # Gdeflate - 393, # LZ4 - 3548, # Snappy - ], - ), + zip(managers(), list(LEN.values())), ) def test_get_compressed_output_size(manager, expected): length = 10000 From bcd7f222205cbc3affb91ae3cc514009a2ee5524 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Mon, 23 Jan 2023 10:23:51 +0100 Subject: [PATCH 2/4] Use approx. when comparing compression size --- python/tests/test_nvcomp.py | 43 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/python/tests/test_nvcomp.py b/python/tests/test_nvcomp.py index d3ff09b2c1..f7c4332d8a 100644 --- a/python/tests/test_nvcomp.py +++ b/python/tests/test_nvcomp.py @@ -21,6 +21,11 @@ } +def assert_compression_size(actual, desired, rtol=0.01): + """Actual compression ratios might change between library versions""" + np.testing.assert_allclose(actual, desired, rtol=rtol) + + def managers(): return [ libnvcomp.ANSManager, @@ -81,7 +86,7 @@ def test_ans_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.ANSManager(**inputs) final = compressor.compress(data) - assert len(final) == LEN["ANS"] + assert_compression_size(len(final), LEN["ANS"]) @pytest.mark.parametrize( @@ -104,7 +109,7 @@ def test_bitcomp_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.BitcompManager(**inputs) final = compressor.compress(data) - assert len(final) == LEN["Bitcomp"] + assert_compression_size(len(final), LEN["Bitcomp"]) @pytest.mark.parametrize( @@ -124,7 +129,7 @@ def test_bitcomp_algorithms(inputs, expected): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.BitcompManager(**inputs) final = compressor.compress(data) - assert len(final) == expected + assert_compression_size(len(final), expected) @pytest.mark.parametrize( @@ -178,7 +183,7 @@ def test_cascaded_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.CascadedManager(**inputs) final = compressor.compress(data) - assert len(final) == LEN["Cascaded"] + assert_compression_size(len(final), LEN["Cascaded"]) @pytest.mark.parametrize( @@ -203,7 +208,7 @@ def test_gdeflate_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.GdeflateManager(**inputs) final = compressor.compress(data) - assert len(final) == LEN["Gdeflate"] + assert_compression_size(len(final), LEN["Gdeflate"]) @pytest.mark.parametrize( @@ -221,7 +226,7 @@ def test_gdeflate_algorithms(inputs, expected): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.GdeflateManager(**inputs) final = compressor.compress(data) - assert len(final) == expected + assert_compression_size(len(final), expected) @pytest.mark.xfail(raises=ValueError) @@ -235,7 +240,7 @@ def test_gdeflate_algorithms_not_implemented(inputs, expected): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.GdeflateManager(**inputs) final = compressor.compress(data) - assert len(final) == expected + assert_compression_size(len(final), expected) @pytest.mark.parametrize( @@ -260,7 +265,7 @@ def test_lz4_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.LZ4Manager(**inputs) final = compressor.compress(data) - assert len(final) == LEN["LZ4"] + assert_compression_size(len(final), LEN["LZ4"]) @pytest.mark.parametrize( @@ -280,7 +285,7 @@ def test_snappy_inputs(inputs): data = cupy.array(np.arange(0, size // dtype(0).itemsize, dtype=dtype)) compressor = libnvcomp.SnappyManager(**inputs) final = compressor.compress(data) - assert len(final) == LEN["Snappy"] + assert_compression_size(len(final), LEN["Snappy"]) @pytest.mark.parametrize( @@ -288,32 +293,32 @@ def test_snappy_inputs(inputs): zip( managers(), [ - { + { # ANS "max_compressed_buffer_size": 89373, "num_chunks": 1, "uncompressed_buffer_size": 10000, }, - { + { # Bitcomp "max_compressed_buffer_size": 16432, "num_chunks": 1, "uncompressed_buffer_size": 10000, }, - { + { # Cascaded "max_compressed_buffer_size": 12460, "num_chunks": 3, "uncompressed_buffer_size": 10000, }, - { + { # Gdeflate "max_compressed_buffer_size": 131160, "num_chunks": 1, "uncompressed_buffer_size": 10000, }, - { + { # LZ4 "max_compressed_buffer_size": 65888, "num_chunks": 1, "uncompressed_buffer_size": 10000, }, - { + { # Snappy "max_compressed_buffer_size": 76575, "num_chunks": 1, "uncompressed_buffer_size": 10000, @@ -335,7 +340,7 @@ def test_get_compression_config_with_default_options(compressor_size): ) compressor_instance = compressor() result = compressor_instance.configure_compression(len(data)) - assert result == expected + assert_compression_size(result, expected) @pytest.mark.parametrize( @@ -385,7 +390,7 @@ def test_get_decompression_config_with_default_options(manager, expected): result = compressor_instance.configure_decompression_with_compressed_buffer( compressed ) - assert result == expected + assert_compression_size(result, expected) @pytest.mark.parametrize("manager", managers()) @@ -439,7 +444,7 @@ def test_get_required_scratch_buffer_size(manager, expected): compressor_instance = manager() compressor_instance.configure_compression(len(data)) buffer_size = compressor_instance.get_required_scratch_buffer_size() - assert buffer_size == expected + assert_compression_size(buffer_size, expected) @pytest.mark.parametrize( @@ -459,7 +464,7 @@ def test_get_compressed_output_size(manager, expected): compressor_instance = manager() compressed = compressor_instance.compress(data) buffer_size = compressor_instance.get_compressed_output_size(compressed) - assert buffer_size == expected + assert_compression_size(buffer_size, expected) @pytest.mark.parametrize("manager", managers()) From 46ef2df80962a570e3aa717fd05efbb90aed61bb Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Mon, 23 Jan 2023 10:30:39 +0100 Subject: [PATCH 3/4] doc --- python/tests/test_nvcomp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tests/test_nvcomp.py b/python/tests/test_nvcomp.py index f7c4332d8a..c320b4d8e1 100644 --- a/python/tests/test_nvcomp.py +++ b/python/tests/test_nvcomp.py @@ -22,7 +22,7 @@ def assert_compression_size(actual, desired, rtol=0.01): - """Actual compression ratios might change between library versions""" + """Compression ratios might change slightly between library versions""" np.testing.assert_allclose(actual, desired, rtol=rtol) From 963265175bb8f3ff6a18884d3d3e8d5942228cf5 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Mon, 23 Jan 2023 10:55:24 +0100 Subject: [PATCH 4/4] cleanup --- python/tests/test_nvcomp.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/python/tests/test_nvcomp.py b/python/tests/test_nvcomp.py index c320b4d8e1..207ee83bee 100644 --- a/python/tests/test_nvcomp.py +++ b/python/tests/test_nvcomp.py @@ -5,7 +5,6 @@ np = pytest.importorskip("numpy") cupy = pytest.importorskip("cupy") -cudf = pytest.importorskip("cudf") kvikio = pytest.importorskip("kvikio") libnvcomp = pytest.importorskip("kvikio.nvcomp") @@ -21,7 +20,7 @@ } -def assert_compression_size(actual, desired, rtol=0.01): +def assert_compression_size(actual, desired, rtol=0.1): """Compression ratios might change slightly between library versions""" np.testing.assert_allclose(actual, desired, rtol=rtol) @@ -340,7 +339,11 @@ def test_get_compression_config_with_default_options(compressor_size): ) compressor_instance = compressor() result = compressor_instance.configure_compression(len(data)) - assert_compression_size(result, expected) + assert_compression_size( + result.pop("max_compressed_buffer_size"), + expected.pop("max_compressed_buffer_size"), + ) + assert result == expected @pytest.mark.parametrize( @@ -390,7 +393,10 @@ def test_get_decompression_config_with_default_options(manager, expected): result = compressor_instance.configure_decompression_with_compressed_buffer( compressed ) - assert_compression_size(result, expected) + assert_compression_size( + result.pop("decomp_data_size"), expected.pop("decomp_data_size") + ) + assert result == expected @pytest.mark.parametrize("manager", managers())