diff --git a/tests/transformation/test_absorb_mul_into_topk.py b/tests/transformation/streamline/test_absorb_mul_into_topk.py similarity index 100% rename from tests/transformation/test_absorb_mul_into_topk.py rename to tests/transformation/streamline/test_absorb_mul_into_topk.py diff --git a/tests/transformation/test_absorb_opposite_transposes.py b/tests/transformation/streamline/test_absorb_opposite_transposes.py similarity index 100% rename from tests/transformation/test_absorb_opposite_transposes.py rename to tests/transformation/streamline/test_absorb_opposite_transposes.py diff --git a/tests/transformation/test_absorb_transp_into_flatten.py b/tests/transformation/streamline/test_absorb_transp_into_flatten.py similarity index 100% rename from tests/transformation/test_absorb_transp_into_flatten.py rename to tests/transformation/streamline/test_absorb_transp_into_flatten.py diff --git a/tests/transformation/test_collapse_repeated_op.py b/tests/transformation/streamline/test_collapse_repeated_op.py similarity index 100% rename from tests/transformation/test_collapse_repeated_op.py rename to tests/transformation/streamline/test_collapse_repeated_op.py diff --git a/tests/transformation/test_factor_out_mul_sign_magnitude.py b/tests/transformation/streamline/test_factor_out_mul_sign_magnitude.py similarity index 100% rename from tests/transformation/test_factor_out_mul_sign_magnitude.py rename to tests/transformation/streamline/test_factor_out_mul_sign_magnitude.py diff --git a/tests/transformation/test_linear_past_eltwise.py b/tests/transformation/streamline/test_linear_past_eltwise.py similarity index 100% rename from tests/transformation/test_linear_past_eltwise.py rename to tests/transformation/streamline/test_linear_past_eltwise.py diff --git a/tests/transformation/test_move_add_past_mul.py b/tests/transformation/streamline/test_move_add_past_mul.py similarity index 100% rename from tests/transformation/test_move_add_past_mul.py rename to tests/transformation/streamline/test_move_add_past_mul.py diff --git a/tests/transformation/test_move_chw_add_past_conv.py b/tests/transformation/streamline/test_move_chw_add_past_conv.py similarity index 100% rename from tests/transformation/test_move_chw_add_past_conv.py rename to tests/transformation/streamline/test_move_chw_add_past_conv.py diff --git a/tests/transformation/test_move_flatten_past_affine.py b/tests/transformation/streamline/test_move_flatten_past_affine.py similarity index 100% rename from tests/transformation/test_move_flatten_past_affine.py rename to tests/transformation/streamline/test_move_flatten_past_affine.py diff --git a/tests/transformation/test_move_flatten_past_topk.py b/tests/transformation/streamline/test_move_flatten_past_topk.py similarity index 100% rename from tests/transformation/test_move_flatten_past_topk.py rename to tests/transformation/streamline/test_move_flatten_past_topk.py diff --git a/tests/transformation/test_move_maxpool_past_multithreshold.py b/tests/transformation/streamline/test_move_maxpool_past_multithreshold.py similarity index 100% rename from tests/transformation/test_move_maxpool_past_multithreshold.py rename to tests/transformation/streamline/test_move_maxpool_past_multithreshold.py diff --git a/tests/transformation/test_move_mul_past_dw_conv.py b/tests/transformation/streamline/test_move_mul_past_dw_conv.py similarity index 100% rename from tests/transformation/test_move_mul_past_dw_conv.py rename to tests/transformation/streamline/test_move_mul_past_dw_conv.py diff --git a/tests/transformation/test_move_past_fork.py b/tests/transformation/streamline/test_move_past_fork.py similarity index 100% rename from tests/transformation/test_move_past_fork.py rename to tests/transformation/streamline/test_move_past_fork.py diff --git a/tests/transformation/test_move_scalar_past_conv.py b/tests/transformation/streamline/test_move_scalar_past_conv.py similarity index 100% rename from tests/transformation/test_move_scalar_past_conv.py rename to tests/transformation/streamline/test_move_scalar_past_conv.py diff --git a/tests/transformation/test_move_scalar_past_matmul.py b/tests/transformation/streamline/test_move_scalar_past_matmul.py similarity index 100% rename from tests/transformation/test_move_scalar_past_matmul.py rename to tests/transformation/streamline/test_move_scalar_past_matmul.py diff --git a/tests/transformation/test_move_transpose_past_scalar_mul.py b/tests/transformation/streamline/test_move_transpose_past_scalar_mul.py similarity index 100% rename from tests/transformation/test_move_transpose_past_scalar_mul.py rename to tests/transformation/streamline/test_move_transpose_past_scalar_mul.py diff --git a/tests/transformation/test_remove_identity_ops.py b/tests/transformation/streamline/test_remove_identity_ops.py similarity index 100% rename from tests/transformation/test_remove_identity_ops.py rename to tests/transformation/streamline/test_remove_identity_ops.py diff --git a/tests/transformation/test_round_thresholds.py b/tests/transformation/streamline/test_round_thresholds.py similarity index 100% rename from tests/transformation/test_round_thresholds.py rename to tests/transformation/streamline/test_round_thresholds.py diff --git a/tests/transformation/test_sign_to_thres.py b/tests/transformation/streamline/test_sign_to_thres.py similarity index 100% rename from tests/transformation/test_sign_to_thres.py rename to tests/transformation/streamline/test_sign_to_thres.py diff --git a/tests/transformation/test_batchnorm_to_affine.py b/tests/transformation/test_batchnorm_to_affine_bnn_pynq.py similarity index 100% rename from tests/transformation/test_batchnorm_to_affine.py rename to tests/transformation/test_batchnorm_to_affine_bnn_pynq.py diff --git a/tests/transformation/test_fold_constants.py b/tests/transformation/test_fold_constants.py deleted file mode 100644 index 34d7d80169101b5151712871a70f591750540bfd..0000000000000000000000000000000000000000 --- a/tests/transformation/test_fold_constants.py +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (c) 2020, Xilinx -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# * Neither the name of FINN nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -import os -from pkgutil import get_data - -import brevitas.onnx as bo -import numpy as np -import onnx -import onnx.numpy_helper as np_helper - -import finn.core.onnx_exec as oxe -from finn.core.modelwrapper import ModelWrapper -from finn.transformation.fold_constants import FoldConstants -from finn.transformation.infer_shapes import InferShapes -from finn.util.test import get_test_model_untrained - -export_onnx_path = "test_fold_constants.onnx" - - -def test_const_folding(): - raw_m = get_data("finn.base-data", "onnx/mnist-conv/model.onnx") - model = ModelWrapper(raw_m) - model = model.transform(InferShapes()) - model = model.transform(FoldConstants()) - raw_i = get_data("finn.base-data", "onnx/mnist-conv/test_data_set_0/input_0.pb") - raw_o = get_data("finn.base-data", "onnx/mnist-conv/test_data_set_0/output_0.pb") - input_tensor = onnx.load_tensor_from_string(raw_i) - output_tensor = onnx.load_tensor_from_string(raw_o) - input_dict = {"Input3": np_helper.to_array(input_tensor)} - output_dict = oxe.execute_onnx(model, input_dict) - assert np.isclose( - np_helper.to_array(output_tensor), output_dict["Plus214_Output_0"], atol=1e-3 - ).all() - - -def test_const_folding_shapes(): - lfc = get_test_model_untrained("LFC", 1, 1) - bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path) - model = ModelWrapper(export_onnx_path) - model = model.transform(InferShapes()) - model = model.transform(FoldConstants()) - reshape_node = model.graph.node[0] - assert reshape_node.op_type == "Reshape" - assert list(model.get_tensor_shape(reshape_node.input[0])) == [1, 1, 28, 28] - assert list(model.get_tensor_shape(reshape_node.output[0])) == [1, 784] - os.remove(export_onnx_path) diff --git a/tests/transformation/test_infer_data_layouts.py b/tests/transformation/test_infer_data_layouts_cnv.py similarity index 99% rename from tests/transformation/test_infer_data_layouts.py rename to tests/transformation/test_infer_data_layouts_cnv.py index 0bc30ea0eb48087606545c86e705328217b004ca..a8ba81dff608994b8e5efb33ec23bd0e3f894175 100644 --- a/tests/transformation/test_infer_data_layouts.py +++ b/tests/transformation/test_infer_data_layouts_cnv.py @@ -46,7 +46,7 @@ import finn.core.data_layout as DataLayout export_onnx_path_cnv = "test_infer_data_layouts.onnx" -def test_infer_data_layouts(): +def test_infer_data_layouts_cnv(): cnv = get_test_model_trained("CNV", 1, 1) bo.export_finn_onnx(cnv, (1, 3, 32, 32), export_onnx_path_cnv) model = ModelWrapper(export_onnx_path_cnv) diff --git a/tests/transformation/test_infer_datatypes.py b/tests/transformation/test_infer_datatypes_lfc.py similarity index 98% rename from tests/transformation/test_infer_datatypes.py rename to tests/transformation/test_infer_datatypes_lfc.py index 097ae03f6153843fbb7956a72b38431559d5d0f1..0802c50c7d15a649182529a4e6897b9bbe273336 100644 --- a/tests/transformation/test_infer_datatypes.py +++ b/tests/transformation/test_infer_datatypes_lfc.py @@ -41,7 +41,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = "test_infer_datatypes.onnx" -def test_infer_datatypes(): +def test_infer_datatypes_lfc(): lfc = get_test_model_trained("LFC", 1, 1) bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path) model = ModelWrapper(export_onnx_path) diff --git a/tests/util/test_data_packing.py b/tests/util/test_data_packing_hls.py similarity index 56% rename from tests/util/test_data_packing.py rename to tests/util/test_data_packing_hls.py index f96ce276efecb1ffa6ca2a0f3916bc406022cdfe..a926bc4068831a552ccfb728511ddda4a8670ca8 100644 --- a/tests/util/test_data_packing.py +++ b/tests/util/test_data_packing_hls.py @@ -36,13 +36,7 @@ import numpy as np import finn.util.basic as cutil from finn.core.datatype import DataType -from finn.util.data_packing import ( - array2hexstring, - finnpy_to_packed_bytearray, - numpy_to_hls_code, - pack_innermost_dim_as_hex_string, - packed_bytearray_to_finnpy, -) +from finn.util.data_packing import numpy_to_hls_code @pytest.mark.parametrize("dtype", [DataType.BINARY, DataType.INT2, DataType.INT32]) @@ -121,34 +115,6 @@ g++ -o test_npy2apintstream test.cpp /workspace/cnpy/cnpy.cpp \ assert success -def test_array2hexstring(): - assert array2hexstring([1, 1, 1, 0], DataType.BINARY, 4) == "0xe" - assert array2hexstring([1, 1, 1, 0], DataType.BINARY, 8) == "0x0e" - assert array2hexstring([1, 1, 1, -1], DataType.BIPOLAR, 8) == "0x0e" - assert array2hexstring([3, 3, 3, 3], DataType.UINT2, 8) == "0xff" - assert array2hexstring([1, 3, 3, 1], DataType.UINT2, 8) == "0x7d" - assert array2hexstring([1, -1, 1, -1], DataType.INT2, 8) == "0x77" - assert array2hexstring([1, 1, 1, -1], DataType.INT4, 16) == "0x111f" - assert array2hexstring([-1], DataType.FLOAT32, 32) == "0xbf800000" - assert array2hexstring([17.125], DataType.FLOAT32, 32) == "0x41890000" - assert array2hexstring([1, 1, 0, 1], DataType.BINARY, 4, reverse=True) == "0xb" - assert array2hexstring([1, 1, 1, 0], DataType.BINARY, 8, reverse=True) == "0x07" - - -def test_pack_innermost_dim_as_hex_string(): - A = [[1, 1, 1, 0], [0, 1, 1, 0]] - eA = np.asarray(["0x0e", "0x06"]) - assert (pack_innermost_dim_as_hex_string(A, DataType.BINARY, 8) == eA).all() - B = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] - eB = np.asarray([["0x0f", "0x0f"], ["0x07", "0x0d"]]) - assert (pack_innermost_dim_as_hex_string(B, DataType.UINT2, 8) == eB).all() - C = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] - eC = np.asarray([["0x0f", "0x0f"], ["0x0d", "0x07"]]) - assert ( - pack_innermost_dim_as_hex_string(C, DataType.UINT2, 8, reverse_inner=True) == eC - ).all() - - def test_numpy_to_hls_code(): def remove_all_whitespace(s): return "".join(s.split()) @@ -168,68 +134,3 @@ def test_numpy_to_hls_code(): eB = """{{ap_uint<4>("0xf", 16), ap_uint<4>("0xf", 16)}, {ap_uint<4>("0x7", 16), ap_uint<4>("0xd", 16)}};""" assert remove_all_whitespace(ret) == remove_all_whitespace(eB) - - -def test_finnpy_to_packed_bytearray(): - A = [[1, 1, 1, 0], [0, 1, 1, 0]] - eA = np.asarray([[14], [6]], dtype=np.uint8) - assert (finnpy_to_packed_bytearray(A, DataType.BINARY) == eA).all() - B = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] - eB = np.asarray([[[15], [15]], [[7], [13]]], dtype=np.uint8) - assert (finnpy_to_packed_bytearray(B, DataType.UINT2) == eB).all() - C = [1, 7, 2, 5] - eC = np.asarray([23, 37], dtype=np.uint8) - assert (finnpy_to_packed_bytearray(C, DataType.UINT4) == eC).all() - D = [[1, 7, 2, 5], [2, 5, 1, 7]] - eD = np.asarray([[23, 37], [37, 23]], dtype=np.uint8) - assert (finnpy_to_packed_bytearray(D, DataType.UINT4) == eD).all() - E = [[-4, 0, -4, -4]] - eE = np.asarray( - [[255, 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 252, 255, 255, 255, 252]], - dtype=np.uint8, - ) - assert (finnpy_to_packed_bytearray(E, DataType.INT32) == eE).all() - - -def test_packed_bytearray_to_finnpy(): - A = np.asarray([[14], [6]], dtype=np.uint8) - eA = [[1, 1, 1, 0], [0, 1, 1, 0]] - eA = np.asarray(eA, dtype=np.float32) - shapeA = eA.shape - assert (packed_bytearray_to_finnpy(A, DataType.BINARY, shapeA) == eA).all() - B = np.asarray([[[15], [15]], [[7], [13]]], dtype=np.uint8) - eB = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] - eB = np.asarray(eB, dtype=np.float32) - shapeB = eB.shape - assert (packed_bytearray_to_finnpy(B, DataType.UINT2, shapeB) == eB).all() - C = np.asarray([23, 37], dtype=np.uint8) - eC = [1, 7, 2, 5] - eC = np.asarray(eC, dtype=np.float32) - shapeC = eC.shape - assert (packed_bytearray_to_finnpy(C, DataType.UINT4, shapeC) == eC).all() - D = np.asarray([[23, 37], [37, 23]], dtype=np.uint8) - eD = [[1, 7, 2, 5], [2, 5, 1, 7]] - eD = np.asarray(eD, dtype=np.float32) - shapeD = eD.shape - assert (packed_bytearray_to_finnpy(D, DataType.UINT4, shapeD) == eD).all() - E = np.asarray( - [[255, 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 252, 255, 255, 255, 252]], - dtype=np.uint8, - ) - eE = [[-4, 0, -4, -4]] - eE = np.asarray(eE, dtype=np.float32) - shapeE = eE.shape - assert (packed_bytearray_to_finnpy(E, DataType.INT32, shapeE) == eE).all() - F = np.asarray( - [[252, 255, 255, 255, 0, 0, 0, 0, 252, 255, 255, 255, 252, 255, 255, 255]], - dtype=np.uint8, - ) - eF = [[-4, 0, -4, -4]] - eF = np.asarray(eE, dtype=np.float32) - shapeF = eF.shape - assert ( - packed_bytearray_to_finnpy( - F, DataType.INT32, shapeF, reverse_inner=True, reverse_endian=True - ) - == eF - ).all()