Skip to content

Commit fe7b5d3

Browse files
authored
[microNPU] Setting a random seed for the codegen tests (#10640)
Although a random seed is set while generating the input for the codegen tests (`infra.py/generate_ref_data_tflite`), other sources of random data such as weight constants were not being deterministically generated. This commit fixes that by setting a seed at the start of each codegen test. Change-Id: I25bb27a131cfc8aa318a8d808e78fb5ad628ad27
1 parent e8c8409 commit fe7b5d3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/python/contrib/test_ethosu/test_codegen.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def test_ethosu_conv2d_single(
6969
accel_type,
7070
activation,
7171
):
72+
np.random.seed(0)
7273
dtype = "int8"
7374

7475
def create_tflite_graph_single():
@@ -157,6 +158,7 @@ def test_ethosu_conv2d_double(
157158
accel_type,
158159
activation,
159160
):
161+
np.random.seed(0)
160162
dtype = "int8"
161163

162164
def create_tflite_graph_double():
@@ -244,6 +246,7 @@ def representative_dataset():
244246

245247
@pytest.mark.parametrize("weight_min, weight_max", [(0.0, 1e-11), (-1e10, 1e10)])
246248
def test_out_of_range_scaling(weight_min, weight_max):
249+
np.random.seed(0)
247250
ifm_shape = (1, 6, 6, 2)
248251
strides = (1, 1)
249252
kernel_shape = (1, 1)
@@ -408,6 +411,8 @@ def test_tflite_depthwise_conv2d(
408411
dilation,
409412
activation_function,
410413
):
414+
np.random.seed(0)
415+
411416
@tf.function
412417
def depthwise_conv2d(x):
413418
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 1]
@@ -443,6 +448,8 @@ def test_ethosu_pooling(
443448
activation_function,
444449
padding,
445450
):
451+
np.random.seed(0)
452+
446453
@tf.function
447454
def pooling(x):
448455
if pooling_type == "MAX":
@@ -475,6 +482,8 @@ def test_ethosu_binary_elementwise(
475482
ifm2_shape,
476483
activation_function,
477484
):
485+
np.random.seed(0)
486+
478487
@tf.function
479488
def binary_elementwise(lhs, rhs):
480489
if operator_type == "ADD":
@@ -514,6 +523,8 @@ def test_binary_add_with_non_4d_shapes(
514523
ifm_shape,
515524
ifm2_shape,
516525
):
526+
np.random.seed(0)
527+
517528
@tf.function
518529
def binary_elementwise(lhs, rhs):
519530
return tf.math.add(lhs, rhs)
@@ -549,6 +560,7 @@ def binary_elementwise(lhs, rhs):
549560
],
550561
)
551562
def test_mean(accel_type, ifm_shape, axis, keep_dims, use_same_quantization):
563+
np.random.seed(0)
552564
dtype = "int8"
553565

554566
def create_mod_from_tflite():
@@ -632,6 +644,7 @@ def create_mod_from_relay():
632644
@pytest.mark.parametrize("dtype", ["int8", "uint8"])
633645
@pytest.mark.parametrize("constant", [np.ones((1, 1, 1, 1)), np.array(1)])
634646
def test_elementwise_add_from_constant_scalar(accel_type, dtype, constant):
647+
np.random.seed(0)
635648
ifm_shape = (1, 4, 4, 8)
636649

637650
def create_relay_graph():
@@ -679,6 +692,7 @@ def test_ethosu_left_shift_binary_elemwise(
679692
ifm_shape,
680693
ifm2_shape,
681694
):
695+
np.random.seed(0)
682696
dtype = "int32"
683697

684698
def create_model():
@@ -715,6 +729,7 @@ def create_model():
715729
def test_ethosu_right_shift_binary_elemwise(
716730
ifm_shape, ifm2_shape, reversed_operands, accel_type, ofm_dtype
717731
):
732+
np.random.seed(0)
718733
dtype = "int32"
719734

720735
def create_model():
@@ -765,6 +780,8 @@ def rounding_right_shift(lhs, rhs):
765780
@pytest.mark.parametrize("ifm_shape", [(3, 2), (1, 15, 11, 7), (3, 1, 12), (400,)])
766781
@pytest.mark.parametrize("ifm_scale, ifm_zp, ofm_scale, ofm_zp", [(1, 0, 1, 0), (0.015, 3, 0.2, 5)])
767782
def test_ethosu_identity_codegen(ifm_shape, ifm_scale, ifm_zp, ofm_scale, ofm_zp, accel_type):
783+
np.random.seed(0)
784+
768785
def create_model():
769786
ifm = relay.var("ifm", shape=ifm_shape, dtype="int8")
770787
identity = infra.make_ethosu_identity(
@@ -804,6 +821,8 @@ def generate_output_data(input_data):
804821
],
805822
)
806823
def test_relay_reshape_codegen(ifm_shape, new_shape, accel_type):
824+
np.random.seed(0)
825+
807826
def create_model():
808827
ifm = relay.var("ifm", shape=ifm_shape, dtype="int8")
809828
reshape = relay.op.reshape(ifm, newshape=new_shape)
@@ -828,6 +847,8 @@ def create_model():
828847
],
829848
)
830849
def test_tflite_slice(accel_type, ifm_shape, begin, size):
850+
np.random.seed(0)
851+
831852
@tf.function
832853
def slice_func(x):
833854
return tf.slice(x, begin, size)
@@ -841,6 +862,8 @@ def slice_func(x):
841862
[([1, 1, 5, 8], [0, 0, 0, 0], [1, 1, 2, 3]), ([1, 3, 3], [0, 1, 2], [1, 2, 3])],
842863
)
843864
def test_tflite_strided_slice(accel_type, ifm_shape, begin, end):
865+
np.random.seed(0)
866+
844867
@tf.function
845868
def strided_slice_func(x):
846869
return tf.strided_slice(x, begin, end)
@@ -859,6 +882,8 @@ def test_ethosu_unary_elementwise(
859882
operator_type,
860883
ifm_shape,
861884
):
885+
np.random.seed(0)
886+
862887
@tf.function
863888
def abs_func(x):
864889
if operator_type == "ABS":
@@ -869,6 +894,8 @@ def abs_func(x):
869894

870895

871896
def test_ethosu_section_name():
897+
np.random.seed(0)
898+
872899
@tf.function
873900
def depthwise_conv2d(x):
874901
weight_shape = [3, 3, 3, 1]
@@ -902,6 +929,7 @@ def depthwise_conv2d(x):
902929
@pytest.mark.xfail(strict=False, reason="See https://github.com/apache/tvm/issues/10487")
903930
@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
904931
def test_ethosu_clz(accel_type):
932+
np.random.seed(0)
905933
ifm_shape = (1, 42, 5, 4)
906934

907935
def create_model():
@@ -933,6 +961,7 @@ def clz_comp(n):
933961

934962
@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
935963
def test_tflite_tanh(accel_type):
964+
np.random.seed(0)
936965
ifm_shape = [1, 115, 32, 7]
937966

938967
@tf.function
@@ -954,6 +983,8 @@ def tanh_func(x):
954983
],
955984
)
956985
def test_tflite_concat(shapes, axis, accel_type):
986+
np.random.seed(0)
987+
957988
@tf.function
958989
def concat_func(*inputs):
959990
op = tf.concat(list(inputs), axis)
@@ -967,6 +998,7 @@ def concat_func(*inputs):
967998

968999
@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
9691000
def test_tflite_sigmoid(accel_type):
1001+
np.random.seed(0)
9701002
ifm_shape = [1, 135, 41, 6]
9711003

9721004
@tf.function
@@ -991,6 +1023,8 @@ def sigmoid_function(x):
9911023
],
9921024
)
9931025
def test_tflite_split(accel_type, ifm_shape, num_or_size_splits, axis):
1026+
np.random.seed(0)
1027+
9941028
@tf.function
9951029
def split_func(x):
9961030
op = tf.split(x, num_or_size_splits, axis=axis)
@@ -1008,6 +1042,7 @@ def split_func(x):
10081042
],
10091043
)
10101044
def test_ethosu_requantize(accel_type, ifm_shape, ifm_scale, ifm_zp, ofm_scale, ofm_zp):
1045+
np.random.seed(0)
10111046
dtype = "int8"
10121047

10131048
def create_model():
@@ -1032,6 +1067,8 @@ def create_model():
10321067
@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
10331068
@pytest.mark.parametrize("ifm_shape,axis", [((2,), 0), ((1, 3, 3), 2)])
10341069
def test_tflite_expand_dims(accel_type, ifm_shape, axis):
1070+
np.random.seed(0)
1071+
10351072
@tf.function
10361073
def expand_dims_func(x):
10371074
return tf.expand_dims(x, axis=axis)
@@ -1044,6 +1081,8 @@ def expand_dims_func(x):
10441081
"ifm_shape,axis", [((1, 1, 2, 1), 0), ((1, 3, 3, 1), 3), ((1, 1, 2, 1), None)]
10451082
)
10461083
def test_tflite_squeeze(accel_type, ifm_shape, axis):
1084+
np.random.seed(0)
1085+
10471086
@tf.function
10481087
def squeeze_func(x):
10491088
return tf.squeeze(x, axis=axis)
@@ -1057,6 +1096,7 @@ def squeeze_func(x):
10571096
[[(1, 2, 2, 1), (4, 4)], [(1, 4, 7, 3), (8, 14)], [(1, 3, 5, 3), (3, 5)]],
10581097
)
10591098
def test_tflite_resize2d_nearest_neighbor(accel_type, ifm_shape, size):
1099+
np.random.seed(0)
10601100
align_corners = False
10611101

10621102
@tf.function
@@ -1080,6 +1120,8 @@ def resize_model(x):
10801120
],
10811121
)
10821122
def test_tflite_resize2d_bilinear(accel_type, ifm_shape, size, align_corners):
1123+
np.random.seed(0)
1124+
10831125
@tf.function
10841126
def resize_model(x):
10851127
return tf.compat.v1.image.resize_bilinear(
@@ -1106,6 +1148,7 @@ def resize_model(x):
11061148
def test_tflite_transpose_convolution(
11071149
accel_type, ifm_shape, ofm_shape, kernel_shape, padding, has_bias
11081150
):
1151+
np.random.seed(0)
11091152
dilations = (1, 1)
11101153
strides = (2, 2)
11111154

@@ -1142,6 +1185,8 @@ def conv2d_transpose(x):
11421185
],
11431186
)
11441187
def test_tflite_pack(accel_type, ifm_shapes, axis):
1188+
np.random.seed(0)
1189+
11451190
@tf.function
11461191
def pack_func(*inputs):
11471192
return tf.stack(inputs, axis=axis)
@@ -1158,6 +1203,8 @@ def pack_func(*inputs):
11581203
[[(1, 2, 3, 4), 1], [(2, 3), 1], [(5, 6, 7), 2]],
11591204
)
11601205
def test_tflite_unpack(accel_type, ifm_shape, axis):
1206+
np.random.seed(0)
1207+
11611208
@tf.function
11621209
def unpack_func(x):
11631210
return tf.unstack(x, axis=axis)
@@ -1169,6 +1216,8 @@ def unpack_func(x):
11691216
@pytest.mark.parametrize("ifm_shape", [(1, 15, 15, 3), (1, 8, 9, 1)])
11701217
@pytest.mark.parametrize("alpha", [0.2, 0.634])
11711218
def test_tflite_leaky_relu(accel_type, ifm_shape, alpha):
1219+
np.random.seed(0)
1220+
11721221
@tf.function
11731222
def leaky_relu_func(x):
11741223
return tf.nn.leaky_relu(x, alpha=alpha)
@@ -1188,6 +1237,8 @@ def test_tflite_fully_connected(
11881237
use_bias,
11891238
activation_function,
11901239
):
1240+
np.random.seed(0)
1241+
11911242
@tf.function
11921243
def fully_connected(x):
11931244
bias_shape = ofm_channels

0 commit comments

Comments
 (0)