-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_model.py
More file actions
149 lines (132 loc) · 4.35 KB
/
sample_model.py
File metadata and controls
149 lines (132 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
# SPDX-License-Identifier: Apache-2.0
from pathlib import Path
import torch
from executorch.backends.arm.quantizer.arm_quantizer import QuantizationSpec
from torchao.quantization.pt2e.observer import FixedQParamsObserver
from export_executorch import export_model
from export_scenario import build_scenario_from_edge_program
from utils import load_tensor, reset_generated_artifact_dirs
BUNDLE_ROOT = Path(__file__).resolve().parent
ARTIFACTS_ROOT = BUNDLE_ROOT / "artifacts"
SCENARIO_DIR = ARTIFACTS_ROOT / "scenario"
SAMPLE_INPUT_IMAGE = BUNDLE_ROOT / "sample_inputs" / "input.png"
SAMPLE_SAMPLING_GRID = BUNDLE_ROOT / "sample_inputs" / "grid.npy"
class SampleModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
blur_kernel = (
torch.tensor(
[
[1.0, 2.0, 1.0],
[2.0, 4.0, 2.0],
[1.0, 2.0, 1.0],
],
dtype=torch.float32,
)
/ 16.0
)
sharpen_kernel = torch.tensor(
[
[0.0, -1.0, 0.0],
[-1.0, 5.0, -1.0],
[0.0, -1.0, 0.0],
],
dtype=torch.float32,
)
self.register_buffer(
"blur_weight",
blur_kernel.view(1, 1, 3, 3).repeat(4, 1, 1, 1),
)
self.register_buffer(
"sharpen_weight",
sharpen_kernel.view(1, 1, 3, 3).repeat(4, 1, 1, 1),
)
def forward(
self,
input_image: torch.Tensor,
sampling_grid: torch.Tensor,
) -> torch.Tensor:
blurred = torch.nn.functional.conv2d(
input_image,
self.blur_weight,
padding=1,
groups=input_image.shape[1],
)
rotated = torch.nn.functional.grid_sample(
blurred,
sampling_grid,
mode="bilinear",
padding_mode="zeros",
align_corners=False,
)
return torch.nn.functional.conv2d(
rotated,
self.sharpen_weight,
padding=1,
groups=rotated.shape[1],
)
def int8_symmetric_qspec() -> QuantizationSpec:
return QuantizationSpec(
dtype=torch.int8,
observer_or_fake_quant_ctr=FixedQParamsObserver.with_args(
scale=1.0 / 127.0,
zero_point=0,
dtype=torch.qint8,
qscheme=torch.per_tensor_symmetric,
quant_min=-127,
quant_max=127,
),
quant_min=-127,
quant_max=127,
qscheme=torch.per_tensor_symmetric,
is_dynamic=False,
)
def int16_symmetric_qspec() -> QuantizationSpec:
return QuantizationSpec(
dtype=torch.int16,
observer_or_fake_quant_ctr=FixedQParamsObserver.with_args(
scale=1.0 / 32767.0,
zero_point=0,
dtype=torch.int16,
qscheme=torch.per_tensor_symmetric,
quant_min=-32767,
quant_max=32767,
),
quant_min=-32767,
quant_max=32767,
qscheme=torch.per_tensor_symmetric,
is_dynamic=False,
)
def main() -> None:
model = SampleModel().eval()
ARTIFACTS_ROOT.mkdir(parents=True, exist_ok=True)
reset_generated_artifact_dirs(ARTIFACTS_ROOT, SCENARIO_DIR)
input_image = load_tensor(SAMPLE_INPUT_IMAGE)
sampling_grid = load_tensor(SAMPLE_SAMPLING_GRID)
model_inputs = (input_image, sampling_grid)
edge_program, example_inputs, io_quant_params = export_model(
model,
model_inputs,
artifacts_root=SCENARIO_DIR,
intermediate_artifacts_root=ARTIFACTS_ROOT,
quantized_input_qspecs=[int8_symmetric_qspec(), int16_symmetric_qspec()],
quantized_output_qspecs=[int8_symmetric_qspec()],
enable_pt2e_quantization=True,
)
scenario_path = build_scenario_from_edge_program(
edge_program,
SCENARIO_DIR / "scenario.json",
example_inputs,
io_quant_params=io_quant_params,
)
print()
print()
print(f"Scenario: {scenario_path}")
print("Run the scenario when ready with:")
print(
f'scenario-runner --scenario "{scenario_path}" '
f'--output "{SCENARIO_DIR}" --log-level debug'
)
if __name__ == "__main__":
main()