Skip to content

Commit ea3952a

Browse files
mcremon-metafacebook-github-bot
authored andcommitted
Update OSS repo (#2033)
Summary: Update the OSS Xtensa repo with more up to date compiler and quantizer things. Introduce a test folder and a conv1d test. Reviewed By: cccclai Differential Revision: D54034581
1 parent 20714e7 commit ea3952a

File tree

10 files changed

+663
-107
lines changed

10 files changed

+663
-107
lines changed

docs/source/build-run-xtensa.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ examples/xtensa/
6868
├── aot
6969
├── kernels
7070
├── ops
71+
├── tests
7172
├── third-party
7273
└── utils
7374
```
7475

7576
***AoT (Ahead-of-Time) Components***:
7677

77-
The AoT folder contains all of the python scripts and functions needed to export the model to an ExecuTorch `.pte` file. In our case, [export_example.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/export_example.py) defines a model and some example inputs (set to a vector of ones), and runs it through the quantizer (from [quantizer.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/quantizer.py)). Then a few compiler passes, also defined in [quantizer.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/quantizer.py), will replace operators with custom ones that are supported and optimized on the chip. Any operator needed to compute things should be defined in [meta_registrations.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/meta_registrations.py) and have corresponding implemetations in the other folders.
78+
The AoT folder contains all of the python scripts and functions needed to export the model to an ExecuTorch `.pte` file. In our case, [export_example.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/export_example.py) is an API that takes a model (nn.Module) and representative inputs and runs it through the quantizer (from [quantizer.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/quantizer.py)). Then a few compiler passes, also defined in [quantizer.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/quantizer.py), will replace operators with custom ones that are supported and optimized on the chip. Any operator needed to compute things should be defined in [meta_registrations.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/meta_registrations.py) and have corresponding implemetations in the other folders.
7879

7980
***Operators***:
8081

@@ -99,13 +100,15 @@ python3 -m examples.portable.scripts.export --model_name="add"
99100

100101
***Quantized Linear***:
101102

102-
The second, more complex model is a quantized [linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) operation. The model is defined [here](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/export_example.py#L88). Linear is the backbone of most Automatic Speech Recognition (ASR) models.
103+
The other, more complex model are custom operators, including:
104+
- a quantized [linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) operation. The model is defined [here](https://github.com/pytorch/executorch/blob/main/examples/xtensa/tests/quantized_linear_example.py#L28). Linear is the backbone of most Automatic Speech Recognition (ASR) models.
105+
- a quantized [conv1d](https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html) operation. The model is defined [here](https://github.com/pytorch/executorch/blob/main/examples/xtensa/tests/quantized_conv1d_example.py#L36). Convolutions are important in wake word and many denoising models.
103106

104-
The generated file is called `XtensaDemoModel.pte`.
107+
In both cases the generated file is called `XtensaDemoModel.pte`.
105108

106109
```bash
107110
cd executorch
108-
python3 -m examples.xtensa.aot.export_example
111+
python3 -m examples.xtensa.tests.quantized_<linear,conv1d>_example
109112
```
110113

111114
### Runtime
@@ -189,6 +192,6 @@ First 20 elements of output 0
189192

190193
In this tutorial, you have learned how to export a quantized operation, build the ExecuTorch runtime and run this model on the Xtensa HiFi4 DSP chip.
191194

192-
The model in this tutorial is a typical operation appearing in ASR models, and can be extended to a complete ASR model by creating the model in [export_example.py](https://github.com/pytorch/executorch/blob/main/examples/xtensa/aot/export_example.py) and adding the needed operators/kernels to [operators](https://github.com/pytorch/executorch/blob/main/examples/xtensa/ops) and [kernels](https://github.com/pytorch/executorch/blob/main/examples/xtensa/kernels).
195+
The (quantized linear) model in this tutorial is a typical operation appearing in ASR models, and can be extended to a complete ASR model by creating the model as a new test and adding the needed operators/kernels to [operators](https://github.com/pytorch/executorch/blob/main/examples/xtensa/ops) and [kernels](https://github.com/pytorch/executorch/blob/main/examples/xtensa/kernels).
193196

194197
Other models can be created following the same structure, always assuming that operators and kernels are available.

examples/xtensa/aot/export_example.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,7 @@
2929
logging.basicConfig(level=logging.INFO, format=FORMAT)
3030

3131

32-
if __name__ == "__main__":
33-
in_features = 32
34-
out_features = 16
35-
bias = True
36-
shape = [64, in_features]
37-
38-
class QuantizedLinear(torch.nn.Module):
39-
def __init__(self, in_features: int, out_features: int, bias: bool):
40-
super().__init__()
41-
self.output_linear = torch.nn.Linear(in_features, out_features, bias=bias)
42-
43-
def forward(self, x: torch.Tensor):
44-
output_linear_out = self.output_linear(x)
45-
return output_linear_out
46-
47-
model = QuantizedLinear(in_features, out_features, bias)
48-
model.eval()
49-
50-
example_inputs = (torch.ones(shape),)
51-
32+
def export_xtensa_model(model, example_inputs):
5233
# Quantizer
5334
quantizer = XtensaQuantizer()
5435

@@ -77,14 +58,14 @@ def forward(self, x: torch.Tensor):
7758
export_to_edge(
7859
converted_model_exp,
7960
example_inputs,
80-
EdgeCompileConfig(
61+
edge_compile_config=EdgeCompileConfig(
8162
_check_ir_validity=False,
8263
),
8364
)
8465
.transform(
8566
[ReplacePT2QuantWithXtensaQuant(), ReplacePT2DequantWithXtensaDequant()]
8667
)
87-
.to_executorch(config=ExecutorchBackendConfig(extract_constant_segment=False))
68+
.to_executorch(config=ExecutorchBackendConfig())
8869
)
8970

9071
logging.info(f"Final exported graph:\n{exec_prog.exported_program().graph}")

examples/xtensa/aot/meta_registrations.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
from typing import Tuple
8+
79
import torch
810
from executorch.exir.scalar_type import ScalarType
911
from torch.library import impl, Library
1012

13+
from .utils import get_conv1d_output_size
14+
1115
lib = Library("xtensa", "DEF")
1216

1317
lib.define(
@@ -25,10 +29,17 @@
2529
)
2630

2731
lib.define(
28-
"quantized_linear_pt2(Tensor src, Tensor weight, Tensor bias, float src_scale, int src_zero_point, float weight_scale, int weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point) -> (Tensor Z)"
32+
"quantized_linear(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point) -> (Tensor Z)"
33+
)
34+
lib.define(
35+
"quantized_linear.out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
36+
)
37+
38+
lib.define(
39+
"quantized_conv(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, int groups, int input_zero_point, Tensor weight_zero_point, Tensor bias_scale, float out_scale, int out_zero_point, Tensor out_multiplier, Tensor out_shift, bool channel_last=False) -> (Tensor Z)"
2940
)
3041
lib.define(
31-
"quantized_linear_pt2.out(Tensor src, Tensor weight, Tensor bias, float src_scale, int src_zero_point, float weight_scale, int weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, *, Tensor(a!) out) -> Tensor(a!)"
42+
"quantized_conv.out(Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding, int[] dilation, int groups, int input_zero_point, Tensor weight_zero_point, Tensor bias_scale, float out_scale, int out_zero_point, Tensor out_multiplier, Tensor out_shift, bool channel_last=False, *, Tensor(a!) out) -> Tensor(a!)"
3243
)
3344

3445
m = Library("xtensa", "IMPL", "Meta")
@@ -58,17 +69,15 @@ def dequantize_per_tensor_meta(
5869
return input.new_empty(input.size(), dtype=torch.float)
5970

6071

61-
@impl(m, "quantized_linear_pt2")
62-
def quantized_linear_pt2_meta(
72+
@impl(m, "quantized_linear")
73+
def quantized_linear_meta(
6374
src: torch.Tensor,
6475
weight: torch.Tensor,
6576
bias: torch.Tensor,
66-
in_scale: float,
6777
in_zero_point: int,
68-
weight_scale: float,
69-
weight_zero_point: int,
70-
out_multiplier: int,
71-
out_shift: int,
78+
weight_zero_point: torch.Tensor,
79+
out_multiplier: torch.Tensor,
80+
out_shift: torch.Tensor,
7281
out_zero_point: int,
7382
):
7483
# src comes in shape [leading_dims, in_dim]
@@ -79,3 +88,35 @@ def quantized_linear_pt2_meta(
7988
assert len(weight_size) == 2
8089
out_size[-1] = weight_size[0]
8190
return src.new_empty(out_size, dtype=torch.uint8)
91+
92+
93+
@impl(m, "quantized_conv")
94+
def quantized_conv_meta(
95+
input: torch.Tensor,
96+
weight: torch.Tensor,
97+
bias: torch.Tensor,
98+
stride: Tuple[int],
99+
padding: Tuple[int],
100+
dilation: Tuple[int],
101+
groups: int,
102+
in_zero_point: int,
103+
weight_zero_point: torch.Tensor,
104+
bias_scale: torch.Tensor,
105+
output_scale: float,
106+
output_zero_point: int,
107+
out_multiplier: torch.Tensor,
108+
out_shift: torch.Tensor,
109+
channel_last: bool = False,
110+
):
111+
out_channels, _in_channels, *kernel_size = weight.shape
112+
in_size = input.shape
113+
# Assert that the input tensor has at least 3 dimensions, and at most 6
114+
assert len(in_size) > 2
115+
assert len(in_size) < 6
116+
117+
# Compute the output tensor size
118+
output_size = get_conv1d_output_size(
119+
in_size, out_channels, stride[0], padding[0], dilation[0], kernel_size[0]
120+
)
121+
122+
return input.new_empty(output_size, dtype=input.dtype)

0 commit comments

Comments
 (0)