Skip to content

Commit eb3bc48

Browse files
mcremon-metafacebook-github-bot
authored andcommitted
Cadence - Move primary code to backends folder (#3353)
Summary: Pull Request resolved: #3353 See design and discussion in https://docs.google.com/document/d/1HPDTbN07WXB9PCdezmvSs_0Yy89D57F1pHHAF9UAgGg/edit#heading=h.828btb3wp67h. Previous folder structure: ``` executorch └── examples ├── aot ├── kernels ├── ops ├── tests ├── third-party/hifi4-nnlib └── utils ``` New folder structure: ``` executorch ├── backends │ └── cadence │ ├── aot │ ├── ops_registration │ ├── tests │ ├── utils │ ├── hifi │ │ ├── kernels │ │ ├── operators │ │ └── third-party │ │ └── nnlib │ └── [other cadence DSP families] │ ├── kernels │ ├── operators │ └── third-party │ └── [any required lib] └── examples └── cadence ├── models └── operators ``` Reviewed By: tarun292, cccclai Differential Revision: D56577399
1 parent f227fc3 commit eb3bc48

35 files changed

Lines changed: 122 additions & 60 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ set_property(
9494
"${CMAKE_CURRENT_LIST_DIR}/../../cmake-out/extension/runner_util/libextension_runner_util.a"
9595
)
9696

97-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ops)
98-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels)
97+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/operators)
98+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/kernels)
9999

100100
# Generate the model header file
101101
add_custom_command(

backends/cadence/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Cadence DSP Backends
2+
3+
## Supported DSPs (in progress)
4+
- HiFi Audio
5+
- ...
6+
7+
## Tutorial
8+
9+
Please follow the [tutorial](https://pytorch.org/executorch/main/build-run-xtensa.html) for more information on how to run models on Cadence/Xtensa DSPs.
10+
11+
## Directory Structure
12+
13+
```
14+
executorch
15+
├── backends
16+
│ └── cadence
17+
│ ├── aot
18+
│ ├── ops_registration
19+
│ ├── tests
20+
│ ├── utils
21+
│ └── hifi
22+
│ ├── kernels
23+
│ ├── operators
24+
│ └── third-party
25+
│ └── nnlib
26+
└── examples
27+
└── cadence
28+
├── models
29+
└── operators
30+
```

backends/cadence/aot/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,49 @@
88

99
import logging
1010

11-
from .meta_registrations import * # noqa
11+
from executorch.backends.cadence.aot.ops_registrations import * # noqa
1212

13-
from torch._export import capture_pre_autograd_graph
14-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
15-
16-
from ...portable.utils import save_pte_program
13+
import os
14+
from typing import Any, Tuple
1715

18-
from .compiler import export_to_edge
19-
from .quantizer import (
16+
from executorch.backends.cadence.aot.compiler import export_to_edge
17+
from executorch.backends.cadence.aot.quantizer import (
2018
CadenceBaseQuantizer,
2119
QuantFusion,
2220
ReplacePT2DequantWithCadenceDequant,
2321
ReplacePT2QuantWithCadenceQuant,
2422
)
23+
from executorch.exir import ExecutorchProgramManager
24+
from torch import nn
25+
from torch._export import capture_pre_autograd_graph
26+
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
27+
2528
from .utils import print_ops_info
2629

2730

2831
FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
2932
logging.basicConfig(level=logging.INFO, format=FORMAT)
3033

3134

32-
def export_model(model, example_inputs):
35+
def _save_pte_program(
36+
prog: ExecutorchProgramManager, model_name: str, output_dir: str = ""
37+
) -> None:
38+
if model_name.endswith(".pte"):
39+
filename = model_name
40+
else:
41+
filename = os.path.join(output_dir, f"{model_name}.pte")
42+
43+
try:
44+
with open(filename, "wb") as file:
45+
prog.write_to_file(file)
46+
logging.info(f"Saved exported program to {filename}")
47+
except Exception as e:
48+
logging.error(f"Error while saving to {filename}: {e}")
49+
50+
51+
def export_model(
52+
model: nn.Module, example_inputs: Tuple[Any], file_name: str = "CadenceDemoModel"
53+
):
3354
# Quantizer
3455
quantizer = CadenceBaseQuantizer()
3556

@@ -70,5 +91,5 @@ def export_model(model, example_inputs):
7091
cadence_prog_manager.exported_program().graph_module,
7192
)
7293

73-
# Save the program as CadenceDemoModel.pte
74-
save_pte_program(exec_prog, "CadenceDemoModel")
94+
# Save the program as (default name is CadenceDemoModel.pte)
95+
_save_pte_program(exec_prog, file_name)
File renamed without changes.

0 commit comments

Comments
 (0)