Skip to content

Commit cbf3636

Browse files
authored
Merge pull request Xilinx#1273 from eki-project/fix/tests_parallelism_safe
Fix: Make Test Suite Parallelizable
2 parents 76eede6 + 05a22bd commit cbf3636

15 files changed

+165
-96
lines changed

tests/brevitas/test_brevitas_avg_pool_export.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141

4242
import finn.core.onnx_exec as oxe
4343
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
44-
45-
base_export_onnx_path = "test_brevitas_avg_pool_export.onnx"
44+
from finn.util.basic import make_build_dir
4645

4746

4847
@pytest.mark.brevitas_export
@@ -62,7 +61,8 @@ def test_brevitas_avg_pool_export(
6261
channels,
6362
idim,
6463
):
65-
export_onnx_path = base_export_onnx_path.replace(".onnx", "test_QONNX.onnx")
64+
build_dir = make_build_dir(prefix="test_brevitas_avg_pool_export")
65+
export_onnx_path = os.path.join(build_dir, "test.onnx")
6666
if signed:
6767
quant_node = QuantIdentity(
6868
bit_width=input_bit_width,
@@ -112,5 +112,3 @@ def test_brevitas_avg_pool_export(
112112
finn_output = odict[model.graph.output[0].name]
113113
# compare outputs
114114
assert np.isclose(ref_output_array, finn_output).all()
115-
# cleanup
116-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_cnv.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@
4141

4242
import finn.core.onnx_exec as oxe
4343
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
44+
from finn.util.basic import make_build_dir
4445
from finn.util.test import get_test_model_trained
4546

46-
export_onnx_path = "test_brevitas_cnv.onnx"
47-
4847

4948
@pytest.mark.brevitas_export
5049
@pytest.mark.parametrize("abits", [1, 2])
5150
@pytest.mark.parametrize("wbits", [1, 2])
5251
def test_brevitas_cnv_export_exec(wbits, abits):
5352
if wbits > abits:
5453
pytest.skip("No wbits > abits cases at the moment")
54+
build_dir = make_build_dir("test_brevitas_cnv_export_exec")
55+
export_onnx_path = os.path.join(build_dir, "test_brevitas_cnv.onnx")
5556
cnv = get_test_model_trained("CNV", wbits, abits)
5657
ishape = (1, 3, 32, 32)
5758
export_qonnx(cnv, torch.randn(ishape), export_onnx_path)
@@ -78,4 +79,3 @@ def test_brevitas_cnv_export_exec(wbits, abits):
7879
expected = cnv.forward(input_tensor).detach().numpy()
7980
assert np.isclose(produced, expected, atol=1e-3).all()
8081
assert np.argmax(produced) == 3
81-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_debug.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@
4242

4343
import finn.core.onnx_exec as oxe
4444
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
45+
from finn.util.basic import make_build_dir
4546
from finn.util.test import get_test_model_trained
4647

4748

4849
@pytest.mark.brevitas_export
4950
@pytest.mark.parametrize("QONNX_FINN_conversion", [False, True])
5051
def test_brevitas_debug(QONNX_FINN_conversion):
51-
finn_onnx = "test_brevitas_debug.onnx"
52+
build_dir = make_build_dir("test_brevitas_debug")
53+
finn_onnx = os.path.join(build_dir, "test_brevitas_debug.onnx")
5254
fc = get_test_model_trained("TFC", 2, 2)
5355
ishape = (1, 1, 28, 28)
5456
dbg_hook = bo.enable_debug(fc, proxy_level=True)
@@ -94,4 +96,3 @@ def test_brevitas_debug(QONNX_FINN_conversion):
9496
tensor_pytorch = _unpack_quant_tensor(dbg_hook.values[dbg_name]).detach().numpy()
9597
tensor_finn = output_dict[dbg_name]
9698
assert np.isclose(tensor_finn, tensor_pytorch, atol=1e-5).all()
97-
os.remove(finn_onnx)

tests/brevitas/test_brevitas_deconv.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838

3939
import finn.core.onnx_exec as oxe
4040
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
41-
42-
export_path = "test_brevitas_deconv.onnx"
41+
from finn.util.basic import make_build_dir
4342

4443

4544
@pytest.mark.brevitas_export
@@ -66,6 +65,8 @@ def test_brevitas_QTransposeConv(ifm_ch, ofm_ch, mh, mw, padding, stride, kw, bi
6665
padding=padding,
6766
bias=bias,
6867
)
68+
build_dir = make_build_dir("test_brevitas_QTransposeConv")
69+
export_path = os.path.join(build_dir, "test_brevitas_deconv.onnx")
6970
# outp = el(inp) # expects NCHW data format
7071
export_qonnx(b_deconv, input_t=inp, export_path=export_path, opset_version=11)
7172
qonnx_cleanup(export_path, out_file=export_path)
@@ -79,4 +80,3 @@ def test_brevitas_QTransposeConv(ifm_ch, ofm_ch, mh, mw, padding, stride, kw, bi
7980
inp_tensor = torch.from_numpy(inp_tensor).float()
8081
expected = b_deconv.forward(inp_tensor).detach().numpy()
8182
assert np.isclose(produced, expected, atol=1e-3).all()
82-
os.remove(export_path)

tests/brevitas/test_brevitas_non_scaled_quanthardtanh_export.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
import finn.core.onnx_exec as oxe
4545
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
46-
47-
export_onnx_path = "test_brevitas_non_scaled_QuantHardTanh_export.onnx"
46+
from finn.util.basic import make_build_dir
4847

4948

5049
@pytest.mark.brevitas_export
@@ -72,7 +71,8 @@ def get_quant_type(bit_width):
7271
scaling_impl_type=ScalingImplType.CONST,
7372
narrow_range=narrow_range,
7473
)
75-
m_path = export_onnx_path
74+
build_dir = make_build_dir(prefix="test_brevitas_act_export_qhardtanh_nonscaled")
75+
m_path = os.path.join(build_dir, "test_brevitas_non_scaled_QuantHardTanh_export.onnx")
7676
export_qonnx(b_act, torch.randn(ishape), m_path)
7777
qonnx_cleanup(m_path, out_file=m_path)
7878
model = ModelWrapper(m_path)
@@ -85,4 +85,3 @@ def get_quant_type(bit_width):
8585
inp_tensor = torch.from_numpy(inp_tensor).float()
8686
expected = b_act.forward(inp_tensor).detach().numpy()
8787
assert np.isclose(produced, expected, atol=1e-3).all()
88-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_qconv2d.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545

4646
import finn.core.onnx_exec as oxe
4747
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
48-
49-
export_onnx_path = "test_brevitas_conv.onnx"
48+
from finn.util.basic import make_build_dir
5049

5150

5251
@pytest.mark.brevitas_export
@@ -92,7 +91,8 @@ def test_brevitas_QConv2d(dw, bias, in_channels):
9291
weight_tensor = gen_finn_dt_tensor(DataType["INT4"], w_shape)
9392
b_conv.weight = torch.nn.Parameter(torch.from_numpy(weight_tensor).float())
9493
b_conv.eval()
95-
m_path = export_onnx_path
94+
build_dir = make_build_dir(prefix="test_brevitas_QConv2d")
95+
m_path = os.path.join(build_dir, "test_brevitas_conv.onnx")
9696
export_qonnx(b_conv, torch.randn(ishape), m_path)
9797
qonnx_cleanup(m_path, out_file=m_path)
9898
model = ModelWrapper(m_path)
@@ -106,4 +106,3 @@ def test_brevitas_QConv2d(dw, bias, in_channels):
106106
expected = b_conv.forward(inp_tensor).detach().numpy()
107107

108108
assert np.isclose(produced, expected, atol=1e-3).all()
109-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_qlinear.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242

4343
import finn.core.onnx_exec as oxe
4444
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
45-
46-
export_onnx_path = "test_brevitas_qlinear.onnx"
45+
from finn.util.basic import make_build_dir
4746

4847

4948
@pytest.mark.brevitas_export
@@ -67,7 +66,8 @@ def test_brevitas_qlinear(bias, out_features, in_features, w_bits, i_dtype):
6766
weight_tensor_fp = np.random.uniform(low=-1.0, high=1.0, size=w_shape).astype(np.float32)
6867
b_linear.weight.data = torch.from_numpy(weight_tensor_fp)
6968
b_linear.eval()
70-
m_path = export_onnx_path
69+
build_dir = make_build_dir(prefix="test_brevitas_qlinear")
70+
m_path = os.path.join(build_dir, "test_brevitas_qlinear.onnx")
7171
export_qonnx(b_linear, torch.randn(i_shape), m_path)
7272
qonnx_cleanup(m_path, out_file=m_path)
7373
model = ModelWrapper(m_path)
@@ -81,4 +81,3 @@ def test_brevitas_qlinear(bias, out_features, in_features, w_bits, i_dtype):
8181
expected = b_linear.forward(inp_tensor).detach().numpy()
8282

8383
assert np.isclose(produced, expected, atol=1e-3).all()
84-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_relu_act_export.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141

4242
import finn.core.onnx_exec as oxe
4343
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
44-
45-
export_onnx_path = "test_brevitas_relu_act_export.onnx"
44+
from finn.util.basic import make_build_dir
4645

4746

4847
@pytest.mark.brevitas_export
@@ -55,7 +54,8 @@ def test_brevitas_act_export_relu(
5554
b_act = QuantReLU(
5655
bit_width=abits,
5756
)
58-
m_path = export_onnx_path
57+
build_dir = make_build_dir(prefix="test_brevitas_act_export_relu")
58+
m_path = os.path.join(build_dir, "test_brevitas_relu_act_export.onnx")
5959
export_qonnx(b_act, torch.randn(ishape), m_path)
6060
qonnx_cleanup(m_path, out_file=m_path)
6161
model = ModelWrapper(m_path)
@@ -70,7 +70,6 @@ def test_brevitas_act_export_relu(
7070
expected = b_act.forward(inp_tensor).detach().numpy()
7171

7272
assert np.isclose(produced, expected, atol=1e-3).all()
73-
os.remove(export_onnx_path)
7473

7574

7675
@pytest.mark.brevitas_export
@@ -88,7 +87,8 @@ def test_brevitas_act_export_relu_channel(
8887
scaling_per_output_channel=True,
8988
per_channel_broadcastable_shape=(1, ch, 1, 1),
9089
)
91-
m_path = export_onnx_path
90+
build_dir = make_build_dir(prefix="test_brevitas_act_export_relu_channel")
91+
m_path = os.path.join(build_dir, "test_brevitas_relu_act_export.onnx")
9292
export_qonnx(b_act, torch.randn(ishape), m_path)
9393
qonnx_cleanup(m_path, out_file=m_path)
9494
model = ModelWrapper(m_path)
@@ -103,4 +103,3 @@ def test_brevitas_act_export_relu_channel(
103103
expected = b_act.forward(inp_tensor).detach().numpy()
104104

105105
assert np.isclose(produced, expected, atol=1e-3).all()
106-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_scaled_qhardtanh_export.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
import finn.core.onnx_exec as oxe
4545
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
46-
47-
export_onnx_path = "test_brevitas_scaled_QHardTanh_export.onnx"
46+
from finn.util.basic import make_build_dir
4847

4948

5049
@pytest.mark.brevitas_export
@@ -85,7 +84,8 @@ def get_quant_type(bit_width):
8584
)
8685
}
8786
b_act.load_state_dict(checkpoint)
88-
m_path = export_onnx_path
87+
build_dir = make_build_dir(prefix="test_brevitas_act_export_qhardtanh_scaled")
88+
m_path = os.path.join(build_dir, "test_brevitas_scaled_QHardTanh_export.onnx")
8989
export_qonnx(b_act, torch.randn(ishape), m_path)
9090
qonnx_cleanup(m_path, out_file=m_path)
9191
model = ModelWrapper(m_path)
@@ -121,4 +121,3 @@ def get_quant_type(bit_width):
121121
print("expec:", ", ".join(["{:8.4f}".format(x) for x in expected[0]]))
122122

123123
assert np.isclose(produced, expected, atol=1e-3).all()
124-
os.remove(export_onnx_path)

tests/brevitas/test_brevitas_selu_act_export.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@
4040

4141
import finn.core.onnx_exec as oxe
4242
from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN
43+
from finn.util.basic import make_build_dir
4344

4445

4546
@pytest.mark.brevitas_export
4647
@pytest.mark.parametrize("abits", [2, 4, 8])
4748
@pytest.mark.parametrize("ishape", [(1, 15), (1, 32, 1, 1)])
4849
@pytest.mark.parametrize("narrow", [True, False])
4950
def test_brevitas_act_export_selu(abits, ishape, narrow):
50-
export_path = "test_brevitas_selu_act_export_%s.onnx" % str(abits)
51+
build_dir = make_build_dir(prefix="test_brevitas_act_export_selu")
52+
export_path = os.path.join(build_dir, "test_brevitas_selu_act_export_%s.onnx" % str(abits))
5153
b_act = torch.nn.Sequential(torch.nn.SELU(), QuantIdentity(bit_width=abits, narrow=narrow))
5254

5355
export_qonnx(
@@ -69,4 +71,3 @@ def test_brevitas_act_export_selu(abits, ishape, narrow):
6971
expected = b_act.forward(inp_tensor).detach().numpy()
7072

7173
assert np.isclose(produced, expected, atol=1e-3).all()
72-
os.remove(export_path)

0 commit comments

Comments
 (0)