|
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 | )
|
@@ -2003,6 +2007,81 @@ def test_qnn_backend_context_direct(self):
|
2003 | 2007 | bundle_program["edge_program_manager"].to_executorch(),
|
2004 | 2008 | )
|
2005 | 2009 |
|
| 2010 | + def test_qnn_backend_context_extraction(self): |
| 2011 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2012 | + |
| 2013 | + module = SimpleModel() # noqa: F405 |
| 2014 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2015 | + backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 2016 | + compiler_specs = [ |
| 2017 | + self.compiler_specs, |
| 2018 | + generate_qnn_executorch_compiler_spec( |
| 2019 | + soc_model=self.chipset_table[TestQNN.model], |
| 2020 | + backend_options=backend_options, |
| 2021 | + online_prepare=True, |
| 2022 | + ), |
| 2023 | + ] |
| 2024 | + validators = [validate_context_binary, validate_qcir] |
| 2025 | + |
| 2026 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2027 | + edge_prog_mgr = EdgeProgramManager( |
| 2028 | + edge_programs={ |
| 2029 | + "forward": capture_program(module, sample_input).exported_program |
| 2030 | + }, |
| 2031 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2032 | + ).to_backend(QnnPartitioner(compiler_spec)) |
| 2033 | + lowered_module = edge_prog_mgr.exported_program().graph_module._modules[ |
| 2034 | + "lowered_module_0" |
| 2035 | + ] |
| 2036 | + qnn_mgr = PyQnnManagerAdaptor.QnnManager( |
| 2037 | + lowered_module.compile_specs[0].value |
| 2038 | + ) |
| 2039 | + qnn_mgr.Init() |
| 2040 | + binary = qnn_mgr.StripProtocol(lowered_module.processed_bytes) |
| 2041 | + validate(binary) |
| 2042 | + |
| 2043 | + def test_qnn_backend_dump_context_from_pte(self): |
| 2044 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2045 | + |
| 2046 | + module = SimpleModel() # noqa: F405 |
| 2047 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2048 | + backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 2049 | + compiler_specs = [ |
| 2050 | + self.compiler_specs, |
| 2051 | + generate_qnn_executorch_compiler_spec( |
| 2052 | + soc_model=self.chipset_table[TestQNN.model], |
| 2053 | + backend_options=backend_options, |
| 2054 | + online_prepare=True, |
| 2055 | + ), |
| 2056 | + ] |
| 2057 | + validators = [validate_context_binary, validate_qcir] |
| 2058 | + |
| 2059 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2060 | + edge_prog_mgr = ( |
| 2061 | + EdgeProgramManager( |
| 2062 | + edge_programs={ |
| 2063 | + "forward": capture_program( |
| 2064 | + module, sample_input |
| 2065 | + ).exported_program |
| 2066 | + }, |
| 2067 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2068 | + ) |
| 2069 | + .to_backend(QnnPartitioner(compiler_spec)) |
| 2070 | + .to_executorch() |
| 2071 | + ) |
| 2072 | + |
| 2073 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 2074 | + pte_path = f"{tmp_dir}/model.pte" |
| 2075 | + with open(pte_path, "wb") as f: |
| 2076 | + edge_prog_mgr.write_to_file(f) |
| 2077 | + |
| 2078 | + dump_context_from_pte(pte_path) |
| 2079 | + binary_name = f"{tmp_dir}/forward_0.bin" |
| 2080 | + self.assertTrue(os.path.isfile(binary_name)) |
| 2081 | + with open(binary_name, "rb") as f: |
| 2082 | + stripped_binary = f.read() |
| 2083 | + validate(stripped_binary) |
| 2084 | + |
2006 | 2085 | def test_qnn_backend_draw_graph(self):
|
2007 | 2086 | golden_data = """digraph test {
|
2008 | 2087 | rankdir=TB
|
@@ -2395,7 +2474,7 @@ def test_qnn_backend_multi_graphs(self):
|
2395 | 2474 | for module, sample_input in zip(modules, sample_inputs)
|
2396 | 2475 | ]
|
2397 | 2476 | backend_options = generate_htp_compiler_spec(
|
2398 |
| - use_fp16=True, |
| 2477 | + use_fp16=False, |
2399 | 2478 | )
|
2400 | 2479 | compiler_specs = [
|
2401 | 2480 | generate_qnn_executorch_compiler_spec(
|
@@ -2494,6 +2573,83 @@ def test_qnn_backend_context_direct(self):
|
2494 | 2573 | bundle_program["edge_program_manager"].to_executorch(),
|
2495 | 2574 | )
|
2496 | 2575 |
|
| 2576 | + def test_qnn_backend_context_extraction(self): |
| 2577 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2578 | + |
| 2579 | + module = SimpleModel() # noqa: F405 |
| 2580 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2581 | + module = self.get_qdq_module(module, sample_input) |
| 2582 | + backend_options = generate_htp_compiler_spec(use_fp16=False) |
| 2583 | + compiler_specs = [ |
| 2584 | + self.compiler_specs, |
| 2585 | + generate_qnn_executorch_compiler_spec( |
| 2586 | + soc_model=self.chipset_table[TestQNN.model], |
| 2587 | + backend_options=backend_options, |
| 2588 | + online_prepare=True, |
| 2589 | + ), |
| 2590 | + ] |
| 2591 | + validators = [validate_context_binary, validate_qcir] |
| 2592 | + |
| 2593 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2594 | + edge_prog_mgr = EdgeProgramManager( |
| 2595 | + edge_programs={ |
| 2596 | + "forward": capture_program(module, sample_input).exported_program |
| 2597 | + }, |
| 2598 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2599 | + ).to_backend(QnnPartitioner(compiler_spec)) |
| 2600 | + lowered_module = edge_prog_mgr.exported_program().graph_module._modules[ |
| 2601 | + "lowered_module_0" |
| 2602 | + ] |
| 2603 | + qnn_mgr = PyQnnManagerAdaptor.QnnManager( |
| 2604 | + lowered_module.compile_specs[0].value |
| 2605 | + ) |
| 2606 | + qnn_mgr.Init() |
| 2607 | + binary = qnn_mgr.StripProtocol(lowered_module.processed_bytes) |
| 2608 | + validate(binary) |
| 2609 | + |
| 2610 | + def test_qnn_backend_dump_context_from_pte(self): |
| 2611 | + from executorch.exir import EdgeCompileConfig, EdgeProgramManager |
| 2612 | + |
| 2613 | + module = SimpleModel() # noqa: F405 |
| 2614 | + sample_input = (torch.ones(1, 32, 28, 28), torch.ones(1, 32, 28, 28)) |
| 2615 | + module = self.get_qdq_module(module, sample_input) |
| 2616 | + backend_options = generate_htp_compiler_spec(use_fp16=True) |
| 2617 | + compiler_specs = [ |
| 2618 | + self.compiler_specs, |
| 2619 | + generate_qnn_executorch_compiler_spec( |
| 2620 | + soc_model=self.chipset_table[TestQNN.model], |
| 2621 | + backend_options=backend_options, |
| 2622 | + online_prepare=True, |
| 2623 | + ), |
| 2624 | + ] |
| 2625 | + validators = [validate_context_binary, validate_qcir] |
| 2626 | + |
| 2627 | + for compiler_spec, validate in zip(compiler_specs, validators): |
| 2628 | + edge_prog_mgr = ( |
| 2629 | + EdgeProgramManager( |
| 2630 | + edge_programs={ |
| 2631 | + "forward": capture_program( |
| 2632 | + module, sample_input |
| 2633 | + ).exported_program |
| 2634 | + }, |
| 2635 | + compile_config=EdgeCompileConfig(_use_edge_ops=False), |
| 2636 | + ) |
| 2637 | + .to_backend(QnnPartitioner(compiler_spec)) |
| 2638 | + .to_executorch() |
| 2639 | + ) |
| 2640 | + |
| 2641 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 2642 | + pte_path = f"{tmp_dir}/model.pte" |
| 2643 | + with open(pte_path, "wb") as f: |
| 2644 | + edge_prog_mgr.write_to_file(f) |
| 2645 | + |
| 2646 | + dump_context_from_pte(pte_path) |
| 2647 | + binary_name = f"{tmp_dir}/forward_0.bin" |
| 2648 | + self.assertTrue(os.path.isfile(binary_name)) |
| 2649 | + with open(binary_name, "rb") as f: |
| 2650 | + stripped_binary = f.read() |
| 2651 | + validate(stripped_binary) |
| 2652 | + |
2497 | 2653 | def test_qnn_backend_draw_graph(self):
|
2498 | 2654 | golden_data = """digraph test {
|
2499 | 2655 | rankdir=TB
|
|
0 commit comments