|
20 | 20 | QuantDtype,
|
21 | 21 | TestQNN,
|
22 | 22 | to_backend,
|
| 23 | + validate_context_binary, |
| 24 | + validate_qcir, |
23 | 25 | )
|
24 | 26 | from executorch.backends.qualcomm.utils.constants import (
|
25 | 27 | QCOM_ANNOTATION,
|
|
30 | 32 |
|
31 | 33 | from executorch.backends.qualcomm.utils.utils import (
|
32 | 34 | capture_program,
|
| 35 | + dump_context_from_pte, |
33 | 36 | from_context_binary,
|
34 | 37 | generate_htp_compiler_spec,
|
35 | 38 | generate_multi_graph_program,
|
36 | 39 | generate_qnn_executorch_compiler_spec,
|
| 40 | + PyQnnManagerAdaptor, |
37 | 41 | skip_annotation,
|
38 | 42 | update_spill_fill_size,
|
39 | 43 | )
|
@@ -2030,6 +2034,81 @@ def test_qnn_backend_context_direct(self):
|
2030 | 2034 | bundle_program["edge_program_manager"].to_executorch(),
|
2031 | 2035 | )
|
2032 | 2036 |
|
| 2037 | + def test_qnn_backend_context_extraction(self): |
| 2038 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2039 | + |
| 2040 | + module = SimpleModel() # noqa: F405 |
| 2041 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2042 | + backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 2043 | + compiler_specs = [ |
| 2044 | + self.compiler_specs, |
| 2045 | + generate_qnn_executorch_compiler_spec( |
| 2046 | + soc_model=self.chipset_table[TestQNN.model], |
| 2047 | + backend_options=backend_options, |
| 2048 | + online_prepare=True, |
| 2049 | + ), |
| 2050 | + ] |
| 2051 | + validators = [validate_context_binary, validate_qcir] |
| 2052 | + |
| 2053 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2054 | + edge_prog_mgr = EdgeProgramManager( |
| 2055 | + edge_programs={ |
| 2056 | + "forward": capture_program(module, sample_input).exported_program |
| 2057 | + }, |
| 2058 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2059 | + ).to_backend(QnnPartitioner(compiler_spec)) |
| 2060 | + lowered_module = edge_prog_mgr.exported_program().graph_module._modules[ |
| 2061 | + "lowered_module_0" |
| 2062 | + ] |
| 2063 | + qnn_mgr = PyQnnManagerAdaptor.QnnManager( |
| 2064 | + lowered_module.compile_specs[0].value |
| 2065 | + ) |
| 2066 | + qnn_mgr.Init() |
| 2067 | + binary = qnn_mgr.StripProtocol(lowered_module.processed_bytes) |
| 2068 | + validate(binary) |
| 2069 | + |
| 2070 | + def test_qnn_backend_dump_context_from_pte(self): |
| 2071 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2072 | + |
| 2073 | + module = SimpleModel() # noqa: F405 |
| 2074 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2075 | + backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 2076 | + compiler_specs = [ |
| 2077 | + self.compiler_specs, |
| 2078 | + generate_qnn_executorch_compiler_spec( |
| 2079 | + soc_model=self.chipset_table[TestQNN.model], |
| 2080 | + backend_options=backend_options, |
| 2081 | + online_prepare=True, |
| 2082 | + ), |
| 2083 | + ] |
| 2084 | + validators = [validate_context_binary, validate_qcir] |
| 2085 | + |
| 2086 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2087 | + edge_prog_mgr = ( |
| 2088 | + EdgeProgramManager( |
| 2089 | + edge_programs={ |
| 2090 | + "forward": capture_program( |
| 2091 | + module, sample_input |
| 2092 | + ).exported_program |
| 2093 | + }, |
| 2094 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2095 | + ) |
| 2096 | + .to_backend(QnnPartitioner(compiler_spec)) |
| 2097 | + .to_executorch() |
| 2098 | + ) |
| 2099 | + |
| 2100 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 2101 | + pte_path = f"{tmp_dir}/model.pte" |
| 2102 | + with open(pte_path, "wb") as f: |
| 2103 | + edge_prog_mgr.write_to_file(f) |
| 2104 | + |
| 2105 | + dump_context_from_pte(pte_path) |
| 2106 | + binary_name = f"{tmp_dir}/forward_0.bin" |
| 2107 | + self.assertTrue(os.path.isfile(binary_name)) |
| 2108 | + with open(binary_name, "rb") as f: |
| 2109 | + stripped_binary = f.read() |
| 2110 | + validate(stripped_binary) |
| 2111 | + |
2033 | 2112 | def test_qnn_backend_draw_graph(self):
|
2034 | 2113 | golden_data = """digraph test {
|
2035 | 2114 | rankdir=TB
|
@@ -2422,7 +2501,7 @@ def test_qnn_backend_multi_graphs(self):
|
2422 | 2501 | for module, sample_input in zip(modules, sample_inputs)
|
2423 | 2502 | ]
|
2424 | 2503 | backend_options = generate_htp_compiler_spec(
|
2425 |
| - use_fp16=True, |
| 2504 | + use_fp16=False, |
2426 | 2505 | )
|
2427 | 2506 | compiler_specs = [
|
2428 | 2507 | generate_qnn_executorch_compiler_spec(
|
@@ -2521,6 +2600,83 @@ def test_qnn_backend_context_direct(self):
|
2521 | 2600 | bundle_program["edge_program_manager"].to_executorch(),
|
2522 | 2601 | )
|
2523 | 2602 |
|
| 2603 | + def test_qnn_backend_context_extraction(self): |
| 2604 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2605 | + |
| 2606 | + module = SimpleModel() # noqa: F405 |
| 2607 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2608 | + module = self.get_qdq_module(module, sample_input) |
| 2609 | + backend_options = generate_htp_compiler_spec(use_fp16=False) |
| 2610 | + compiler_specs = [ |
| 2611 | + self.compiler_specs, |
| 2612 | + generate_qnn_executorch_compiler_spec( |
| 2613 | + soc_model=self.chipset_table[TestQNN.model], |
| 2614 | + backend_options=backend_options, |
| 2615 | + online_prepare=True, |
| 2616 | + ), |
| 2617 | + ] |
| 2618 | + validators = [validate_context_binary, validate_qcir] |
| 2619 | + |
| 2620 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2621 | + edge_prog_mgr = EdgeProgramManager( |
| 2622 | + edge_programs={ |
| 2623 | + "forward": capture_program(module, sample_input).exported_program |
| 2624 | + }, |
| 2625 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2626 | + ).to_backend(QnnPartitioner(compiler_spec)) |
| 2627 | + lowered_module = edge_prog_mgr.exported_program().graph_module._modules[ |
| 2628 | + "lowered_module_0" |
| 2629 | + ] |
| 2630 | + qnn_mgr = PyQnnManagerAdaptor.QnnManager( |
| 2631 | + lowered_module.compile_specs[0].value |
| 2632 | + ) |
| 2633 | + qnn_mgr.Init() |
| 2634 | + binary = qnn_mgr.StripProtocol(lowered_module.processed_bytes) |
| 2635 | + validate(binary) |
| 2636 | + |
| 2637 | + def test_qnn_backend_dump_context_from_pte(self): |
| 2638 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2639 | + |
| 2640 | + module = SimpleModel() # noqa: F405 |
| 2641 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2642 | + module = self.get_qdq_module(module, sample_input) |
| 2643 | + backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 2644 | + compiler_specs = [ |
| 2645 | + self.compiler_specs, |
| 2646 | + generate_qnn_executorch_compiler_spec( |
| 2647 | + soc_model=self.chipset_table[TestQNN.model], |
| 2648 | + backend_options=backend_options, |
| 2649 | + online_prepare=True, |
| 2650 | + ), |
| 2651 | + ] |
| 2652 | + validators = [validate_context_binary, validate_qcir] |
| 2653 | + |
| 2654 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2655 | + edge_prog_mgr = ( |
| 2656 | + EdgeProgramManager( |
| 2657 | + edge_programs={ |
| 2658 | + "forward": capture_program( |
| 2659 | + module, sample_input |
| 2660 | + ).exported_program |
| 2661 | + }, |
| 2662 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2663 | + ) |
| 2664 | + .to_backend(QnnPartitioner(compiler_spec)) |
| 2665 | + .to_executorch() |
| 2666 | + ) |
| 2667 | + |
| 2668 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 2669 | + pte_path = f"{tmp_dir}/model.pte" |
| 2670 | + with open(pte_path, "wb") as f: |
| 2671 | + edge_prog_mgr.write_to_file(f) |
| 2672 | + |
| 2673 | + dump_context_from_pte(pte_path) |
| 2674 | + binary_name = f"{tmp_dir}/forward_0.bin" |
| 2675 | + self.assertTrue(os.path.isfile(binary_name)) |
| 2676 | + with open(binary_name, "rb") as f: |
| 2677 | + stripped_binary = f.read() |
| 2678 | + validate(stripped_binary) |
| 2679 | + |
2524 | 2680 | def test_qnn_backend_draw_graph(self):
|
2525 | 2681 | golden_data = """digraph test {
|
2526 | 2682 | rankdir=TB
|
|
0 commit comments