Skip to content

Commit 8e21fc8

Browse files
Pull request pytorch#63: [NO UPSTREAM] Unify unit test seeding for input data
Merge in AITEC/executorch from feature/nxg11066/EIEX-155-Unify-unit-test-seeding-for-input-data to main-nxp * commit '97a27f2d47369a71597c622f487a06638deee3bc': [NO UPSTREAM] Unify unit test seeding for input data
2 parents cf59bb9 + 97a27f2 commit 8e21fc8

File tree

9 files changed

+51
-60
lines changed

9 files changed

+51
-60
lines changed

backends/nxp/tests/ir/converter/node_converter/test_avg_pool2d_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture(autouse=True)
1313
def reseed_model_per_test_run():
14-
torch.seed()
14+
torch.manual_seed(23)
1515
np.random.seed(23)
1616

1717

backends/nxp/tests/ir/converter/node_converter/test_constant_pad_nd_converter.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
from executorch.backends.nxp.tests.models import ConstantPadNDModule, ConstantPadNDConvModule, Conv2dConstantPadNDModule
99
from torch.export import ExportedProgram
1010

11+
1112
@pytest.fixture(autouse=True)
1213
def reseed_model_per_test_run():
13-
torch.seed()
14+
torch.manual_seed(23)
15+
np.random.seed(23)
16+
1417

1518
@pytest.mark.parametrize("constant", [0.0, 42., -13.37])
1619
def test_constant_pad_nd_conversion__specific_constant(constant):
@@ -19,8 +22,7 @@ def test_constant_pad_nd_conversion__specific_constant(constant):
1922

2023
edge_program = to_edge_program(ConstantPadNDModule(paddings, constant), input_shape).exported_program()
2124

22-
torch.manual_seed(23)
23-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
25+
input_data = np.random.random(input_shape).astype(np.float32)
2426

2527
convert_run_compare(edge_program, input_data)
2628

@@ -41,7 +43,6 @@ def test_constant_pad_nd_quant_conversion__specific_constant(mocker, constant):
4143
# Capture converted program
4244
edge_program: ExportedProgram = converter_spy.call_args.args[1]
4345

44-
np.random.seed(23)
4546
input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8)
4647

4748
convert_run_compare(edge_program, input_data, tfl_model=tflite_flatbuffers_model, atol=1.,
@@ -54,8 +55,7 @@ def test_constant_pad_nd_conversion__default_constant():
5455

5556
edge_program = to_edge_program(ConstantPadNDModule(paddings), input_shape).exported_program()
5657

57-
torch.manual_seed(23)
58-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
58+
input_data = np.random.random(input_shape).astype(np.float32)
5959

6060
convert_run_compare(edge_program, input_data)
6161

@@ -84,8 +84,7 @@ def test_constant_pad_nd_conversion__default_constant():
8484
def test_constant_pad_nd_conversion__format_less(input_shape, paddings):
8585
edge_program = to_edge_program(ConstantPadNDModule(paddings), input_shape).exported_program()
8686

87-
torch.manual_seed(23)
88-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
87+
input_data = np.random.random(input_shape).astype(np.float32)
8988

9089
convert_run_compare(edge_program, input_data)
9190

@@ -99,8 +98,7 @@ def test_constant_pad_nd_conversion__format_less(input_shape, paddings):
9998
def test_constant_pad_nd_conversion__channels_first(input_shape, paddings):
10099
edge_program = to_edge_program(ConstantPadNDConvModule(paddings), input_shape).exported_program() # Extra `Conv` after the padding.
101100

102-
torch.manual_seed(23)
103-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
101+
input_data = np.random.random(input_shape).astype(np.float32)
104102

105103
convert_run_compare(edge_program, input_data, tflite_input_preprocess=ToNHWCPreprocess(),
106104
tflite_output_preprocess=ToNCHWPreprocess())

backends/nxp/tests/ir/converter/node_converter/test_conv_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture(autouse=True)
1313
def reseed_model_per_test_run():
14-
torch.seed()
14+
torch.manual_seed(23)
1515
np.random.seed(23)
1616

1717

backends/nxp/tests/ir/converter/node_converter/test_linear_converter.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@
66
from executorch.backends.nxp.tests.executors import convert_run_compare
77
from executorch.backends.nxp.tests.models import LinearModule
88

9+
910
@pytest.fixture(autouse=True)
1011
def reseed_model_per_test_run():
11-
torch.seed()
12+
torch.manual_seed(23)
13+
np.random.seed(23)
14+
1215

1316
def test_linear_conversion__with_bias():
1417
input_shape = (10, 32)
1518
edge_program = to_edge_program(LinearModule(bias=True), input_shape).exported_program()
1619

17-
torch.manual_seed(23)
18-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
20+
input_data = np.random.random(input_shape).astype(np.float32)
1921

2022
convert_run_compare(edge_program, input_data=input_data, atol=1.e-6)
2123

2224
def test_linear_conversion__without_bias():
2325
input_shape = (10, 32)
2426
edge_program = to_edge_program(LinearModule(bias=True), input_shape).exported_program()
2527

26-
torch.manual_seed(23)
27-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
28+
input_data = np.random.random(input_shape).astype(np.float32)
2829

2930
convert_run_compare(edge_program, input_data=input_data, atol=1.e-6)

backends/nxp/tests/ir/converter/node_converter/test_max_pool_2d_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@pytest.fixture(autouse=True)
1515
def reseed_model_per_test_run():
16-
torch.seed()
16+
torch.manual_seed(23)
1717
np.random.seed(23)
1818

1919

backends/nxp/tests/ir/converter/node_converter/test_permute_copy_converter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
from executorch.backends.nxp.tests.models import Conv2dModule
99
from torch.export import ExportedProgram
1010

11+
1112
@pytest.fixture(autouse=True)
1213
def reseed_model_per_test_run():
13-
torch.seed()
14+
torch.manual_seed(23)
15+
np.random.seed(23)
16+
1417

1518
class Conv2dPermuteCopyModule(torch.nn.Module):
1619
def __init__(self, new_dims: tuple[int, ...]):
@@ -38,7 +41,6 @@ def test_permute_copy_quant_conversion__with_bias(mocker):
3841
# Capture converted program
3942
edge_program: ExportedProgram = converter_spy.call_args.args[1]
4043

41-
np.random.seed(23)
4244
input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8)
4345

4446
convert_run_compare(edge_program, input_data, tfl_model=tflite_flatbuffers_model, atol=1.,

backends/nxp/tests/ir/converter/node_converter/test_relu_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture(autouse=True)
1313
def reseed_model_per_test_run():
14-
torch.seed()
14+
torch.manual_seed(23)
1515
np.random.seed(23)
1616

1717

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1+
import numpy as np
12
import pytest
23
import torch
34

4-
from executorch import exir
55
from executorch.backends.nxp.backend.edge_program_converter import EdgeProgramToIRConverter
66
from executorch.backends.nxp.backend.ir.conversion_config import ConversionConfig
7+
from executorch.backends.nxp.tests.executorch_pipeline import to_edge_program
78
from executorch.backends.nxp.tests.executors import convert_run_compare
89
from executorch.backends.nxp.tests.models import SoftmaxModule, SoftmaxConvModule
910

11+
1012
@pytest.fixture(autouse=True)
1113
def reseed_model_per_test_run():
12-
torch.seed()
14+
torch.manual_seed(23)
15+
np.random.seed(23)
16+
1317

1418
@pytest.mark.parametrize("input_shape,dim", [
1519
pytest.param((10,), -1, id="1D,dim=-1"),
1620
pytest.param((10,), 0, id="1D,dim=0"),
1721
pytest.param((10, 32), -1, id="2D,dim=-1"),
1822
pytest.param((10, 32), 1, id="2D,dim=1"),
1923
])
20-
def test_softmax_conversion__formatless_input(input_shape, dim):
24+
def test_softmax_conversion__formatless_input(input_shape, dim: int):
2125
model = SoftmaxModule(dim)
2226

23-
example_input = (torch.ones(input_shape),)
24-
exir_program = torch.export.export(model, example_input)
25-
edge_program_manager = exir.to_edge(exir_program)
27+
edge_program = to_edge_program(model, input_shape).exported_program()
2628

27-
torch.manual_seed(23)
28-
input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
29+
input_data = np.random.random(input_shape).astype(np.float32)
2930

30-
convert_run_compare(edge_program_manager.exported_program(), input_data=input_data)
31+
convert_run_compare(edge_program, input_data=input_data)
3132

3233

3334
@pytest.mark.parametrize("input_shape,dim", [
@@ -38,39 +39,33 @@ def test_softmax_conversion__formatless_input(input_shape, dim):
3839
pytest.param((10, 32, 32, 8, 8), -1, id="5D,dim=-1"),
3940
pytest.param((10, 32, 32, 8, 8), 4, id="5D,dim=4"),
4041
])
41-
def test_softmax_conversion__unknown_input_format(input_shape, dim):
42+
def test_softmax_conversion__unknown_input_format(input_shape, dim: int):
4243
model = SoftmaxModule(dim)
4344

44-
example_input = (torch.ones(input_shape),)
45-
exir_program = torch.export.export(model, example_input)
46-
edge_program_manager = exir.to_edge(exir_program)
45+
edge_program = to_edge_program(model, input_shape).exported_program()
4746

4847
# Currently this test not pass because the convertibility checker doesn't use tensor formats.
4948
with pytest.raises(AssertionError, match='`aten__softmax_default` is not convertible'):
50-
EdgeProgramToIRConverter().convert_program(edge_program_manager.exported_program(), ConversionConfig())
49+
EdgeProgramToIRConverter().convert_program(edge_program, ConversionConfig())
5150

52-
# torch.manual_seed(23)
53-
# input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
51+
# input_data = np.random.random(input_shape).astype(np.float32)
5452
# convert_run_compare(edge_program_manager.exported_program(), input_data=input_data, atol=5e-7)
5553

5654

5755
@pytest.mark.parametrize("input_shape,dim", [
5856
pytest.param((10, 4, 32, 32), 1, id="4D,dim=1"),
5957
pytest.param((10, 4, 16, 16), -3, id="4D,dim=-3"),
6058
])
61-
def test_softmax_conversion_channel_last(input_shape, dim):
59+
def test_softmax_conversion_channel_last(input_shape, dim: int):
6260
model = SoftmaxConvModule(dim)
6361

64-
example_input = (torch.ones(input_shape),)
65-
exir_program = torch.export.export(model, example_input)
66-
edge_program_manager = exir.to_edge(exir_program)
62+
edge_program = to_edge_program(model, input_shape).exported_program()
6763

6864
# Currently this test not pass because the convertibility checker doesn't use tensor formats.
6965
with pytest.raises(AssertionError, match='`aten__softmax_default` is not convertible'):
70-
EdgeProgramToIRConverter().convert_program(edge_program_manager.exported_program(), ConversionConfig())
66+
EdgeProgramToIRConverter().convert_program(edge_program, ConversionConfig())
7167

72-
# torch.manual_seed(23)
73-
# input_data = torch.randn(input_shape, dtype=torch.float32).detach().numpy()
68+
# input_data = np.random.random(input_shape).astype(np.float32)
7469
# convert_run_compare(edge_program_manager.exported_program(), tflite_input_preprocess=ToNHWCPreprocess(),
7570
# tflite_output_preprocess=ToNCHWPreprocess(), input_data=input_data, atol=5e-7)
7671

@@ -82,12 +77,10 @@ def test_softmax_conversion_channel_last(input_shape, dim):
8277
pytest.param((10, 32, 32, 8, 8), 3, id="5D,dim=3"),
8378
pytest.param((10, 32, 32, 8, 8), 2, id="5D,dim=2"),
8479
])
85-
def test_softmax_conversion_unsupported_dims(input_shape, dim):
80+
def test_softmax_conversion_unsupported_dims(input_shape, dim: int):
8681
model = SoftmaxModule(dim)
8782

88-
example_input = (torch.ones(input_shape),)
89-
exir_program = torch.export.export(model, example_input)
90-
edge_program_manager = exir.to_edge(exir_program)
83+
edge_program = to_edge_program(model, input_shape).exported_program()
9184

9285
with pytest.raises(AssertionError, match="`aten__softmax_default` is not convertible"):
93-
EdgeProgramToIRConverter().convert_program(edge_program_manager.exported_program(), ConversionConfig())
86+
EdgeProgramToIRConverter().convert_program(edge_program, ConversionConfig())

backends/nxp/tests/ir/converter/node_converter/test_view_copy_converter.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
from executorch.backends.nxp.tests.executors import convert_run_compare, ToNHWCPreprocess, ToNCHWPreprocess
1414
from torch.export import ExportedProgram
1515

16+
1617
@pytest.fixture(autouse=True)
1718
def reseed_model_per_test_run():
18-
torch.seed()
19+
torch.manual_seed(23)
20+
np.random.seed(23)
21+
1922

2023
class FormatlessToChannelsFirstModule(nn.Module):
2124
def __init__(self, channels: int, new_shape: Sequence[int]):
@@ -69,8 +72,7 @@ def test__channels_first_to_2d(mocker):
6972
torch_model = ConvReshapeModule(channels=input_shape[1], new_shape=new_shape)
7073
edge_program = to_edge_program(torch_model, input_shape).exported_program()
7174

72-
torch.manual_seed(23)
73-
input_data = np.random.random(input_shape).astype('float32')
75+
input_data = np.random.random(input_shape).astype(np.float32)
7476

7577
converter_spy = mocker.spy(ModelBuilder, "finish")
7678

@@ -91,8 +93,7 @@ def test__channels_first_to_4d(mocker):
9193
torch_model = ConvReshapeModule(channels=input_shape[1], new_shape=new_shape)
9294
edge_program = to_edge_program(torch_model, input_shape).exported_program()
9395

94-
torch.manual_seed(23)
95-
input_data = np.random.random(input_shape).astype('float32')
96+
input_data = np.random.random(input_shape).astype(np.float32)
9697

9798
converter_spy = mocker.spy(ModelBuilder, "finish")
9899

@@ -113,8 +114,7 @@ def test__formatless_to_channels_first(mocker):
113114
torch_model = FormatlessToChannelsFirstModule(channels=new_shape[1], new_shape=new_shape)
114115
edge_program = to_edge_program(torch_model, input_shape).exported_program()
115116

116-
torch.manual_seed(23)
117-
input_data = np.random.random(input_shape).astype('float32')
117+
input_data = np.random.random(input_shape).astype(np.float32)
118118

119119
converter_spy = mocker.spy(ModelBuilder, "finish")
120120

@@ -135,8 +135,7 @@ def test__formatless_to_formatless(mocker):
135135
torch_model = FormatlessToFormatlessModule(new_shape=new_shape)
136136
edge_program = to_edge_program(torch_model, input_shape).exported_program()
137137

138-
torch.manual_seed(23)
139-
input_data = np.random.random(input_shape).astype('float32')
138+
input_data = np.random.random(input_shape).astype(np.float32)
140139

141140
converter_spy = mocker.spy(ModelBuilder, "finish")
142141

@@ -163,7 +162,6 @@ def test_view_copy_w_linear_quant_conversion(mocker, input_shape, new_shape):
163162
# Capture converted program
164163
edge_program: ExportedProgram = converter_spy.call_args.args[1]
165164

166-
np.random.seed(23)
167165
input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8)
168166

169167
convert_run_compare(edge_program, input_data, tfl_model=tflite_flatbuffers_model, atol=1.)
@@ -185,7 +183,6 @@ def test_view_copy_w_conv_quant_conversion(mocker, input_shape, new_shape):
185183
# Capture converted program
186184
edge_program: ExportedProgram = converter_spy.call_args.args[1]
187185

188-
np.random.seed(23)
189186
input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8)
190187

191188
convert_run_compare(edge_program, input_data, tflite_input_preprocess=ToNHWCPreprocess(),

0 commit comments

Comments
 (0)