Skip to content

Commit 44a776f

Browse files
authored
fix etrecord lost after to_backend
Differential Revision: D79823973 Pull Request resolved: #13197
1 parent bbb913b commit 44a776f

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

devtools/etrecord/tests/TARGETS

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ python_unittest(
77
name = "etrecord_test",
88
srcs = ["etrecord_test.py"],
99
deps = [
10-
"//caffe2:torch",
11-
"//executorch/devtools/bundled_program:config",
12-
"//executorch/devtools/bundled_program:core",
13-
"//executorch/devtools/etrecord:etrecord",
14-
"//executorch/exir:lib",
15-
"//executorch/exir/tests:models",
10+
":etrecord_test_library"
1611
],
1712
)
1813

@@ -26,5 +21,6 @@ python_library(
2621
"//executorch/devtools/etrecord:etrecord",
2722
"//executorch/exir:lib",
2823
"//executorch/exir/tests:models",
24+
"//executorch/backends/xnnpack/partition:xnnpack_partitioner",
2925
],
3026
)

devtools/etrecord/tests/etrecord_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import executorch.exir.tests.models as models
1616
import torch
1717
from executorch import exir
18+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
1819
from executorch.devtools.bundled_program.config import MethodTestCase, MethodTestSuite
1920
from executorch.devtools.bundled_program.core import BundledProgram
2021
from executorch.devtools.etrecord import generate_etrecord, parse_etrecord
@@ -369,6 +370,41 @@ def test_get_etrecord_from_executorch_program_manager(self):
369370
self.assertEqual(etrecord._debug_handle_map, et_manager.debug_handle_map)
370371
self.assertEqual(etrecord._delegate_map, et_manager.delegate_map)
371372

373+
def test_get_etrecord_from_executorch_program_manager_with_partitioner(self):
374+
"""Test getting ETRecord from ExecutorchProgramManager using get_etrecord() method."""
375+
f = models.BasicSinMax()
376+
aten_program = export(f, f.get_random_inputs(), strict=True)
377+
378+
# Generate edge manager with ETRecord
379+
edge_manager = to_edge_transform_and_lower(
380+
aten_program,
381+
partitioner=[XnnpackPartitioner()],
382+
generate_etrecord=True,
383+
)
384+
385+
# Convert to executorch
386+
et_manager = edge_manager.to_executorch()
387+
388+
# Test get_etrecord method
389+
etrecord = et_manager.get_etrecord()
390+
self.assertIsNotNone(etrecord)
391+
self.assert_etrecord_saveable(etrecord)
392+
393+
# Verify the data matches the original input
394+
self.check_graph_closeness(
395+
etrecord.exported_program,
396+
aten_program.graph_module,
397+
)
398+
self.assertEqual(
399+
etrecord.export_graph_id,
400+
id(aten_program.graph),
401+
)
402+
403+
# Verify the executorch program data matches
404+
# ETRecord stores data directly (not JSON serialized), so compare with original data
405+
self.assertEqual(etrecord._debug_handle_map, et_manager.debug_handle_map)
406+
self.assertEqual(etrecord._delegate_map, et_manager.delegate_map)
407+
372408
def test_get_etrecord_from_executorch_program_manager_without_generation(self):
373409
"""Test getting ETRecord from ExecutorchProgramManager when ETRecord was not generated."""
374410
f = models.BasicSinMax()
@@ -400,6 +436,7 @@ def test_to_edge_transform_and_lower_etrecord_save_and_parse(self):
400436
# Generate edge manager with ETRecord
401437
edge_manager = to_edge_transform_and_lower(
402438
aten_program,
439+
partitioner=[XnnpackPartitioner()],
403440
generate_etrecord=True,
404441
)
405442

exir/program/_program.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,10 +1583,13 @@ def transform(
15831583
new_programs[name].graph_module
15841584
)
15851585

1586-
return EdgeProgramManager(
1586+
epm = EdgeProgramManager(
15871587
new_programs, copy.deepcopy(self._config_methods), compile_config
15881588
)
15891589

1590+
epm._etrecord = self._etrecord
1591+
return epm
1592+
15901593
@et_logger("to_backend")
15911594
def to_backend(
15921595
self,
@@ -1629,12 +1632,15 @@ def to_backend(
16291632

16301633
new_edge_programs = to_backend(method_to_programs_and_partitioners)
16311634
config = EdgeCompileConfig(_check_ir_validity=False)
1632-
return EdgeProgramManager(
1635+
epm = EdgeProgramManager(
16331636
new_edge_programs,
16341637
copy.deepcopy(self._config_methods),
16351638
config,
16361639
)
16371640

1641+
epm._etrecord = self._etrecord
1642+
return epm
1643+
16381644
@et_logger("to_executorch")
16391645
def to_executorch(
16401646
self,

0 commit comments

Comments
 (0)