24
24
from executorch .devtools .bundled_program .serialize import (
25
25
serialize_from_bundled_program_to_flatbuffer ,
26
26
)
27
+ from executorch .extension .pybindings .portable_lib import (
28
+ _load_for_executorch_from_bundled_program ,
29
+ _load_bundled_program_from_buffer
30
+ )
27
31
28
32
# 定义一个简单的模型用于测试
29
33
class SimpleAddModel (torch .nn .Module ):
@@ -34,9 +38,9 @@ def forward(self, x, y):
34
38
return x + y
35
39
36
40
37
- class TestDevtoolsEndToEnd (unittest . TestCase ):
38
- def setUp (self ):
39
- self .tmp_dir = tempfile . mkdtemp ()
41
+ class TestDevtoolsEndToEnd ():
42
+ def __init__ (self ):
43
+ self .tmp_dir = "./"
40
44
self .etrecord_path = os .path .join (self .tmp_dir , "etrecord.bin" )
41
45
self .etdump_path = os .path .join (self .tmp_dir , "etdump.bin" )
42
46
@@ -45,10 +49,10 @@ def setUp(self):
45
49
def tearDown (self ):
46
50
shutil .rmtree (self .tmp_dir )
47
51
48
- def generate_etrecord (self ):
52
+ def generate_etrecord_ (self ):
49
53
aten_model : ExportedProgram = export (
50
54
self .model ,
51
- (torch .randn (1 , 1 , 32 , 32 ),),
55
+ (torch .randn (1 , 1 , 32 , 32 ), torch . randn ( 1 , 1 , 32 , 32 ) ),
52
56
)
53
57
edge_program_manager = to_edge (
54
58
aten_model ,
@@ -64,13 +68,13 @@ def generate_etrecord(self):
64
68
65
69
def generate_bundled_program (self ):
66
70
method_name = "forward"
67
- method_graphs = {method_name : export (self .model , (torch .randn (1 , 1 , 32 , 32 ),))}
71
+ method_graphs = {method_name : export (self .model , (torch .randn (1 , 1 , 32 , 32 ), torch . randn ( 1 , 1 , 32 , 32 ) ))}
68
72
69
- inputs = [torch .randn (1 , 1 , 32 , 32 )]
73
+ inputs = [( torch .randn (1 , 1 , 32 , 32 ), torch . randn ( 1 , 1 , 32 , 32 ) )]
70
74
method_test_suites = [
71
75
MethodTestSuite (
72
76
method_name = method_name ,
73
- test_cases = [MethodTestCase (inputs = inp , expected_outputs = self .model (inp )) for inp in inputs ],
77
+ test_cases = [MethodTestCase (inputs = inp , expected_outputs = self .model (* inp )) for inp in inputs ],
74
78
)
75
79
]
76
80
@@ -80,4 +84,33 @@ def generate_bundled_program(self):
80
84
method_test_suites = method_test_suites ,
81
85
)
82
86
83
- return bundled_program
87
+ return bundled_program
88
+
89
+ def generate_etdump (self ):
90
+ bundled_program_py = self .generate_bundled_program ()
91
+
92
+ bundled_program_bytes = serialize_from_bundled_program_to_flatbuffer (
93
+ bundled_program_py
94
+ )
95
+
96
+ bundled_program_cpp = _load_bundled_program_from_buffer (bundled_program_bytes )
97
+
98
+ program = _load_for_executorch_from_bundled_program (
99
+ bundled_program_cpp ,
100
+ enable_etdump = True
101
+ )
102
+
103
+ example_inputs = (torch .randn (1 , 1 , 32 , 32 ), torch .randn (1 , 1 , 32 , 32 ))
104
+ program .forward (example_inputs )
105
+
106
+ program .write_etdump_result_to_file (self .etdump_path )
107
+
108
+ def test_profile (self ):
109
+ pass
110
+
111
+
112
+ if __name__ == "__main__" :
113
+ tester = TestDevtoolsEndToEnd ()
114
+ tester .generate_etrecord_ ()
115
+ tester .generate_bundled_program ()
116
+ tester .generate_etdump ()
0 commit comments