Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all \u003cpre\u003e\u003ccode\u003e blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks"); } } catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); } })(); (function(){ try { var __m = "github.com"; var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length \u003e 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 85 additions & 48 deletions backends/arm/quantizer/arm_quantizer_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,6 +243,18 @@ class PatternQuantizer(Quantizer, QuantizerReporterUser):

"""

PARAMETER_TARGETS = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

def __init__(
self,
quantization_config: QuantizationConfig | None,
Expand DownExpand Up@@ -275,75 +287,59 @@ def get_quantizer_info(self):
support_config_path,
)

def is_parameter(self, node: Node, model: torch.fx.GraphModule) -> bool:
"""Returns True if the given node is a parameter of the model."""
try:
_ = model.get_parameter(node.target) # type: ignore[arg-type]
return True
except Exception:
def is_weight(self, node: Node) -> bool:
"""Returns True if node is used as a weight by all users."""
if node.op != "get_attr":
return False

def is_weight(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the first parameter of the given
parameters.
"""
return len(params) > 0 and node == params[0]
# Ensure that the node is used as a weight by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

def is_bias(
self, node: Node, params: list[Node], model: torch.fx.GraphModule
) -> bool:
"""Returns True if node is the second parameter of the given
parameters.
"""
return len(params) == 2 and node == params[1]
args = list(user_node.args)
if not (len(args) > 1 and node == args[1]):
return False

return True

def is_bias(self, node: Node) -> bool:
"""Returns True if node is used as a bias by all users."""
if node.op != "get_attr":
return False

# Ensure that the node is used as a bias by all users
for user_node in node.users:
if user_node.target not in self.PARAMETER_TARGETS:
return False

args = list(user_node.args)
if not (len(args) > 2 and node == args[2]):
return False

return True

def annotate_match(
self,
match: list[Node],
config: QuantizationConfig | None,
model: torch.fx.GraphModule,
) -> None:
"""Annotates a matched pattern according to the given quantization
config.
"""
parameter_targets = {
torch.ops.aten.linear.default,
torch.ops.aten.convolution.default,
torch.ops.aten.conv1d.default,
torch.ops.aten.conv1d.padding,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
torch.ops.aten.conv3d.default,
torch.ops.aten.conv3d.padding,
torch.ops.aten.conv_transpose2d.input,
}

for node in match:
input_qspec_map = {}
output_qspec = None

params = [n for n in node.all_input_nodes if self.is_parameter(n, model)]
if node.target in parameter_targets:
if len(params) == 0 or len(params) > 2:
logger.warning(
f"{node.name} is expected to have parameter tensors for weight/bias but no such inputs found, which may cause unexpected quantization annotations. This is likely caused by incorrect tensor instantiations or non-constant weight/biases."
)
else:
if len(params) > 0:
logger.warning(
f"{node.name} is not expected to not have parameter tensors but found {[n.name for n in params]}, which may cause unexpected quantization annotations."
)

for input_node in node.all_input_nodes:
if not has_float_output(input_node):
continue
if self.is_weight(input_node, params, model):
if self.is_weight(input_node):
input_qspec_map[input_node] = (
config.get_weight_qspec(node) if config else None
)
elif self.is_bias(input_node, params, model):
elif self.is_bias(input_node):
input_qspec_map[input_node] = (
config.get_bias_qspec(node) if config else None # type: ignore[assignment]
)
Expand All@@ -370,7 +366,7 @@ def annotate(self, model: torch.fx.GraphModule) -> None: # type: ignore[overrid
)
for result in matches:
if result.accepted:
self.annotate_match(result.pattern, self.quantization_config, model)
self.annotate_match(result.pattern, self.quantization_config)
self.report_accept(result.pattern)
else:
self.report_reject(
Expand DownExpand Up@@ -424,6 +420,9 @@ class SharedQspecQuantizer(Quantizer, QuantizerReporterUser):
torch.ops.aten.flip.default,
torch.ops.aten.index_select.default,
torch.ops.aten.index_put.default,
torch.ops.aten.index_put_.default,
torch.ops.aten.index_copy.default,
torch.ops.aten.index_copy_.default,
torch.ops.aten.contiguous.default,
torch.ops.aten.as_strided_copy.default,
torch.ops.aten.pixel_shuffle.default,
Expand DownExpand Up@@ -571,6 +570,42 @@ def _get_shared_clique(self, root_node: Node) -> tuple[set[Node], list[Any]]:

return shared_nodes, adjacent_qspecs

def _should_skip_while_shared_qspec(self, node: Node) -> bool:
return node.target == torch.ops.higher_order.while_loop and bool(
node.meta.get("additional_inputs")
)

def _annotate_while_with_additional_inputs(
self,
root_node: Node,
adjacent_qspecs: list[Any],
) -> bool:
if not self._should_skip_while_shared_qspec(root_node):
return False
if len(adjacent_qspecs) == 0:
self.report_reject(
[root_node],
"Couldn't find any adjacent quantization spec to annotate while_loop.",
)
return True

input_qspec = adjacent_qspecs[0]
input_qspec_map: dict[Node, Optional[QuantizationSpec]] = {
n: input_qspec for n in self._get_input_nodes_with_float_output(root_node)
}
output_qspec: Optional[QuantizationSpec] = None
if len(self._get_user_nodes_with_float_input(root_node)) > 0:
output_qspec = input_qspec

_mark_node_as_quantized(
root_node,
input_qspec_map,
output_qspec,
is_quantized=True,
)
self.report_accept([root_node])
return True

def _annotate_shared_cluster(self, root_node: Node) -> None:
if (
len(self._get_input_nodes_with_float_output(root_node)) == 0
Expand All@@ -592,9 +627,11 @@ def _annotate_shared_cluster(self, root_node: Node) -> None:
node_order = {node: index for index, node in enumerate(root_node.graph.nodes)}
ordered_nodes = sorted(shared_nodes, key=lambda node: node_order.get(node, 0))

if self._annotate_while_with_additional_inputs(root_node, adjacent_qspecs):
return

# Ensure the root node is the first one in the graph.
root_node = ordered_nodes[0]

if len(adjacent_qspecs) > 0:
root_node_float_inputs = self._get_input_nodes_with_float_output(root_node)
if len(root_node_float_inputs) > 0:
Expand Down
9 changes: 5 additions & 4 deletions backends/arm/quantizer/quantization_annotator.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
from executorch.backends.arm.common.type import ensure_type
from executorch.backends.arm.quantizer import QuantizationConfig

from torch._ops import OpOverload
from torch._subclasses import FakeTensor
from torch.fx import Node
from torchao.quantization.pt2e import (
Expand DownExpand Up@@ -441,7 +442,7 @@ def _match_pattern(
return left_condition and right_condition


_conv_ops = {
_conv_ops: set[OpOverload] = {
torch.ops.aten.conv1d.default,
torch.ops.aten.conv2d.default,
torch.ops.aten.conv2d.padding,
Expand DownExpand Up@@ -473,7 +474,7 @@ def _match_pattern(
},
}

_one_to_one = {
_one_to_one: set[OpOverload] = {
torch.ops.aten.abs.default,
torch.ops.aten.ceil.default,
torch.ops.aten.erf.default,
Expand DownExpand Up@@ -514,7 +515,7 @@ def _match_pattern(
torch.ops.aten.tan.default,
}

_one_to_one_shared_input_qspec = {
_one_to_one_shared_input_qspec: set[OpOverload] = {
torch.ops.aten.squeeze.default,
torch.ops.aten.squeeze_copy.default,
torch.ops.aten.squeeze_copy.dim,
Expand DownExpand Up@@ -574,7 +575,7 @@ def _match_pattern(
torch.ops.aten.detach_copy.default,
}

_one_to_one_shared_input_or_input_act_qspec = {
_one_to_one_shared_input_or_input_act_qspec: set[OpOverload] = {
torch.ops.aten.alias.default,
torch.ops.aten.clone.default,
torch.ops.aten.hardtanh.default,
Expand Down
36 changes: 34 additions & 2 deletions backends/arm/quantizer/quantization_config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

from torchao.quantization.pt2e.quantizer import (
DerivedQuantizationSpec,
FixedQParamsQuantizationSpec,
QuantizationSpec,
QuantizationSpecBase,
SharedQuantizationSpec,
Expand DownExpand Up@@ -284,10 +285,18 @@ def get_input_act_qspec(self, node=None, input_node=None):

For comparison operators, make sure that both inputs share the same
quantization spec, by returning a SharedQuantizationSpec that ties the
quantization of both inputs together. For other operators, return the
default input activation spec.
quantization of both inputs together.

For trigonometric ops, ensure that input spec has fixed qparams.

For other operators, return the default input activation spec.

"""
# MLETORCH-1853: Fix lazy import when moving files around
from executorch.backends.arm.quantizer.quantization_annotator import (
_fixed_input_qspec_ops,
)

if node is None or input_node is None:
return super().get_input_act_qspec(node, input_node)

Expand All@@ -296,6 +305,29 @@ def get_input_act_qspec(self, node=None, input_node=None):
return super().get_input_act_qspec(node, input_node)
else:
return SharedQuantizationSpec((node.args[0], node))
elif node.target in _fixed_input_qspec_ops:

input_act_qspec = super().get_input_act_qspec(node, input_node)
if not hasattr(input_act_qspec, "dtype") or not isinstance(
input_act_qspec.dtype, torch.dtype
):
raise ValueError(
f"{node.target} requires an input activation quantization "
"spec to use fixed input qparams."
)
dtype = getattr(input_act_qspec, "dtype", None)
num_bits = torch.iinfo(dtype).bits

qparams = _fixed_input_qspec_ops[node.target][num_bits]
return FixedQParamsQuantizationSpec(
dtype=dtype,
scale=qparams.scale,
zero_point=qparams.zero_point,
quant_min=input_act_qspec.quant_min,
quant_max=input_act_qspec.quant_max,
qscheme=input_act_qspec.qscheme,
is_dynamic=input_act_qspec.is_dynamic,
)

return super().get_input_act_qspec(node, input_node)

Expand Down
10 changes: 8 additions & 2 deletions backends/arm/quantizer/quantizer_support.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,8 +77,6 @@ def check_pattern(cls, pattern):
torch.ops.aten.relu_.default,
torch.ops.aten.hardtanh.default,
torch.ops.aten.hardtanh_.default,
torch.ops.aten.hardsigmoid.default,
torch.ops.aten.hardsigmoid_.default,
torch.ops.aten.clamp.default,
torch.ops.aten.clamp_.default,
]
Expand DownExpand Up@@ -168,6 +166,14 @@ def check_pattern(cls, pattern):
(torch.ops.aten.ge.Scalar,),
(torch.ops.aten.eq.Scalar,),
(torch.ops.aten.ne.Scalar,),
(torch.ops.aten.lstm.input,),
(torch.ops.aten.rnn_tanh.input,),
(torch.ops.aten.rnn_relu.input,),
(torch.ops.aten.gru.input,),
(torch.ops.aten.asin.default,),
(torch.ops.aten.acos.default,),
(torch.ops.aten.atanh.default,),
(torch.ops.aten.einsum.default,),
]
)
TOSA_QUANTIZER_SUPPORT_DICT: dict[tuple[OpOverload, ...], type[PatternCheck] | None] = {
Expand Down
4 changes: 3 additions & 1 deletion backends/arm/scripts/docgen/docgen.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,9 @@ def get_docstring(obj) -> str:

lines = docstring.split("\n")
for line in lines:
if ":" in line and line.startswith(" "):
# Only first-level arg lines should become bullets.
is_arg_line = line.startswith(" ") and not line.startswith(" ")
if ":" in line and is_arg_line:
new_line = line.strip()
pos = new_line.index(":")
new_line = f"- **{new_line[:pos]}**" + new_line[pos:]
Expand Down
30 changes: 30 additions & 0 deletions backends/cortex_m/test/misc/test_portable_int8.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,36 @@ def _quantize_and_export(
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_put_": OpCase(
torch.ops.aten.index_put_.default,
_build_module(
lambda x, y: torch.ops.aten.index_put_.default(
x, (torch.tensor([1, 3]),), torch.tensor([1.0, 2.0]), False
)
),
Comment on lines +304 to +310
(torch.randn(6), torch.randn(6)),
torch.int64,
),
"index_copy": OpCase(
torch.ops.aten.index_copy.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy.default(
x, 0, torch.tensor([0, 2]), y
)
),
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"index_copy_": OpCase(
torch.ops.aten.index_copy_.default,
_build_module(
lambda x, y: torch.ops.aten.index_copy_.default(
x, 0, torch.tensor([0, 2]), y
)
),
Comment on lines +324 to +330
(torch.randn(4, 5), torch.randn(2, 5)),
torch.int64,
),
"contiguous": OpCase(
torch.ops.aten.contiguous.default,
_build_module(lambda x, y: torch.ops.aten.contiguous.default(x)),
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ In this tutorial you will learn how to export a simple PyTorch model for the Exe
```{tip}
If you are already familiar with this delegate, you may want to jump directly to the examples:
* [Examples in the ExecuTorch repository](https://github.com/pytorch/executorch/tree/main/examples/arm)
* [A commandline compiler for example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
* [A commandline compiler for quick tests and example models](https://github.com/pytorch/executorch/blob/main/backends/arm/scripts/aot_arm_compiler.py)
```

This tutorial serves as an introduction to using ExecuTorch to deploy PyTorch models on Arm® Ethos™-U targets. It is based on `ethos_u_minimal_example.ipynb`, provided in Arm’s examples folder.
Expand DownExpand Up@@ -142,9 +142,10 @@ save_pte_program(executorch_program_manager, "ethos_u_minimal_example.pte")


```{tip}
For a quick start, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
For a quick test, you can use the script `backends/arm/scripts/aot_arm_compiler.py` to produce the pte.
To produce a pte file equivalent to the one above, run
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`
`python -m backends.arm.scripts.aot_arm_compiler --model_name=add --delegate --quantize --output=ethos_u_minimal_example.pte`.
For production use, you should instead use the stable Python API shown above.
```

### Runtime:
Expand Down
Loading
Loading