Skip to content

Commit 7e2e6cb

Browse files
authored
[Bug Fix] Set random seed for torch and fix invalid torch cv samples. (#302)
* Fix invalid torch cv samples. * Set random seed.
1 parent b35eece commit 7e2e6cb

File tree

12 files changed

+753
-738
lines changed

12 files changed

+753
-738
lines changed

graph_net/torch/test_compiler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from contextlib import contextmanager
1313
import time
1414
import json
15+
import random
1516
import numpy as np
1617
import platform
1718
from graph_net.torch.backend.graph_compiler_backend import GraphCompilerBackend
@@ -33,6 +34,15 @@
3334
}
3435

3536

37+
def set_seed(random_seed):
38+
random.seed(random_seed)
39+
np.random.seed(random_seed)
40+
torch.manual_seed(random_seed)
41+
if torch.cuda.is_available():
42+
torch.cuda.manual_seed(random_seed)
43+
torch.cuda.manual_seed_all(random_seed)
44+
45+
3646
def load_class_from_file(
3747
args: argparse.Namespace, class_name: str, device: str
3848
) -> Type[torch.nn.Module]:
@@ -226,6 +236,7 @@ def test_single_model(args):
226236
flush=True,
227237
)
228238

239+
runtime_seed = 1024
229240
eager_failure = False
230241
expected_out = None
231242
eager_types = []
@@ -239,6 +250,8 @@ def test_single_model(args):
239250
file=sys.stderr,
240251
flush=True,
241252
)
253+
254+
torch.manual_seed(runtime_seed)
242255
expected_out = eager_model_call()
243256
if not isinstance(expected_out, tuple):
244257
expected_out = (expected_out,)
@@ -270,6 +283,7 @@ def test_single_model(args):
270283
else:
271284
compiled_model = compiler(model)
272285

286+
torch.manual_seed(runtime_seed)
273287
compiled_model_call = lambda: compiled_model(**input_dict)
274288
compiled_stats = measure_performance(compiled_model_call, args, compiler)
275289
print(
@@ -480,6 +494,9 @@ def is_single_model_dir(model_dir):
480494

481495
def main(args):
482496
assert os.path.isdir(args.model_path)
497+
498+
initalize_seed = 123
499+
set_seed(random_seed=initalize_seed)
483500
if is_single_model_dir(args.model_path):
484501
test_single_model(args)
485502
else:

samples/mmseg/SegNeXt-b/weight_meta.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,7 +2455,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block2_modules_
24552455
dtype = "torch.float32"
24562456
device = "cuda:0"
24572457
mean = 0.477
2458-
std = 1.551
2458+
std = 0.551
24592459
data = None
24602460

24612461

@@ -4341,7 +4341,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
43414341
dtype = "torch.float32"
43424342
device = "cuda:0"
43434343
mean = 0.335
4344-
std = 1.516
4344+
std = 0.516
43454345
data = None
43464346

43474347

@@ -4451,7 +4451,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
44514451
dtype = "torch.float32"
44524452
device = "cuda:0"
44534453
mean = 0.365
4454-
std = 1.702
4454+
std = 0.702
44554455
data = None
44564456

44574457

@@ -4701,7 +4701,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
47014701
dtype = "torch.float32"
47024702
device = "cuda:0"
47034703
mean = 0.504
4704-
std = 3.092
4704+
std = 1.092
47054705
data = None
47064706

47074707

@@ -4811,7 +4811,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
48114811
dtype = "torch.float32"
48124812
device = "cuda:0"
48134813
mean = 0.585
4814-
std = 3.823
4814+
std = 1.823
48154815
data = None
48164816

48174817

@@ -5061,7 +5061,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
50615061
dtype = "torch.float32"
50625062
device = "cuda:0"
50635063
mean = 0.845
5064-
std = 6.773
5064+
std = 0.773
50655065
data = None
50665066

50675067

@@ -5171,7 +5171,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
51715171
dtype = "torch.float32"
51725172
device = "cuda:0"
51735173
mean = 0.955
5174-
std = 7.576
5174+
std = 0.576
51755175
data = None
51765176

51775177

@@ -5421,7 +5421,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
54215421
dtype = "torch.float32"
54225422
device = "cuda:0"
54235423
mean = 1.469
5424-
std = 14.217
5424+
std = 1.217
54255425
data = None
54265426

54275427

@@ -5531,7 +5531,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
55315531
dtype = "torch.float32"
55325532
device = "cuda:0"
55335533
mean = 1.598
5534-
std = 14.929
5534+
std = 0.929
55355535
data = None
55365536

55375537

@@ -5781,7 +5781,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
57815781
dtype = "torch.float32"
57825782
device = "cuda:0"
57835783
mean = 2.097
5784-
std = 20.441
5784+
std = 1.441
57855785
data = None
57865786

57875787

@@ -5891,7 +5891,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
58915891
dtype = "torch.float32"
58925892
device = "cuda:0"
58935893
mean = 2.233
5894-
std = 20.387
5894+
std = 1.387
58955895
data = None
58965896

58975897

@@ -6141,7 +6141,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
61416141
dtype = "torch.float32"
61426142
device = "cuda:0"
61436143
mean = 3.568
6144-
std = 38.412
6144+
std = 1.412
61456145
data = None
61466146

61476147

@@ -6251,7 +6251,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
62516251
dtype = "torch.float32"
62526252
device = "cuda:0"
62536253
mean = 3.865
6254-
std = 39.828
6254+
std = 1.828
62556255
data = None
62566256

62576257

@@ -6500,8 +6500,8 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
65006500
shape = [320]
65016501
dtype = "torch.float32"
65026502
device = "cuda:0"
6503-
mean = 6.457
6504-
std = 76.325
6503+
mean = 1.457
6504+
std = 1.325
65056505
data = None
65066506

65076507

@@ -6510,7 +6510,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
65106510
shape = [320]
65116511
dtype = "torch.float32"
65126512
device = "cuda:0"
6513-
mean = 1.964
6513+
mean = 0.564
65146514
std = 0.359
65156515
data = None
65166516

@@ -6611,7 +6611,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
66116611
dtype = "torch.float32"
66126612
device = "cuda:0"
66136613
mean = 6.959
6614-
std = 79.050
6614+
std = 1.050
66156615
data = None
66166616

66176617

@@ -6861,7 +6861,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block3_modules_
68616861
dtype = "torch.float32"
68626862
device = "cuda:0"
68636863
mean = 14.760
6864-
std = 197.511
6864+
std = 1.511
68656865
data = None
68666866

68676867

@@ -7667,7 +7667,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block4_modules_
76677667
dtype = "torch.float32"
76687668
device = "cuda:0"
76697669
mean = 2.187
7670-
std = 41.543
7670+
std = 1.543
76717671
data = None
76727672

76737673

@@ -7777,7 +7777,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block4_modules_
77777777
dtype = "torch.float32"
77787778
device = "cuda:0"
77797779
mean = 2.757
7780-
std = 49.021
7780+
std = 1.021
77817781
data = None
77827782

77837783

@@ -8027,7 +8027,7 @@ class Program_weight_tensor_meta_L_self_modules_backbone_modules_block4_modules_
80278027
dtype = "torch.float32"
80288028
device = "cuda:0"
80298029
mean = 7.591
8030-
std = 111.366
8030+
std = 1.366
80318031
data = None
80328032

80338033

0 commit comments

Comments
 (0)