-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-08-27 nightly release (dc66414)
- Loading branch information
pytorchbot
committed
Aug 27, 2024
1 parent
8e15cbd
commit 1207cd9
Showing
56 changed files
with
2,479 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from typing import List | ||
|
||
import serializer.tosa_serializer as ts | ||
from executorch.backends.arm.operators.node_visitor import ( | ||
NodeVisitor, | ||
register_node_visitor, | ||
) | ||
from executorch.backends.arm.tosa_mapping import TosaArg | ||
from serializer.tosa_serializer import TosaOp | ||
from torch.fx import Node | ||
|
||
|
||
@register_node_visitor | ||
class CatVisitor(NodeVisitor): | ||
target = "aten.cat.default" | ||
|
||
def __init__(self, *args): | ||
super().__init__(*args) | ||
|
||
def define_node( | ||
self, | ||
node: Node, | ||
tosa_graph: ts.TosaSerializer, | ||
inputs: List[TosaArg], | ||
output: TosaArg, | ||
is_quant_node: bool, | ||
) -> None: | ||
|
||
tensors = inputs[0].special | ||
dim = 0 if len(inputs) < 2 else inputs[1].number | ||
rank = len(output.shape) | ||
dim = (dim + rank) % rank | ||
dim = output.dim_order.index(dim) | ||
|
||
attr = ts.TosaSerializerAttribute() | ||
attr.AxisAttribute(dim) | ||
|
||
tosa_graph.addOperator( | ||
TosaOp.Op().CONCAT, [tensor.name for tensor in tensors], [output.name], attr | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import executorch.backends.arm.tosa_quant_utils as tqutils | ||
import serializer.tosa_serializer as ts | ||
import torch.fx | ||
from executorch.backends.arm.operators.node_visitor import ( | ||
NodeVisitor, | ||
register_node_visitor, | ||
) | ||
from executorch.backends.arm.tosa_mapping import TosaArg | ||
from serializer.tosa_serializer import TosaOp | ||
|
||
|
||
@register_node_visitor | ||
class ReluVisitor(NodeVisitor): | ||
target = "aten.relu.default" | ||
|
||
def __init__(self, *args): | ||
super().__init__(*args) | ||
|
||
def define_node( | ||
self, | ||
node: torch.fx.Node, | ||
tosa_graph: ts.TosaSerializer, | ||
inputs: list[TosaArg], | ||
output: TosaArg, | ||
is_quant_node: bool, | ||
) -> None: | ||
attr = ts.TosaSerializerAttribute() | ||
|
||
clamp_min_fp = 0.0 | ||
clamp_max_fp = 0.0 | ||
clamp_min_qs = 0 | ||
clamp_max_qs = 0 | ||
if is_quant_node: | ||
out_qargs = tqutils.get_quant_node_args(list(node.users)[0]) | ||
clamp_min_qs = tqutils.quantize_value(0, out_qargs) | ||
clamp_max_qs = tqutils.quantize_value(float("inf"), out_qargs) | ||
|
||
else: | ||
clamp_min_fp = 0 | ||
clamp_max_fp = float("inf") | ||
|
||
attr.ClampAttribute( | ||
tosa_graph.builder, | ||
clamp_min_qs, | ||
clamp_max_qs, | ||
clamp_min_fp, | ||
clamp_max_fp, | ||
) | ||
|
||
tosa_graph.addOperator(TosaOp.Op().CLAMP, [inputs[0].name], [output.name], attr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
# Follows this specification: https://pytorch.org/docs/stable/generated/torch.unsqueeze.html | ||
|
||
import serializer.tosa_serializer as ts | ||
import torch.fx | ||
from executorch.backends.arm.operators.node_visitor import ( | ||
NodeVisitor, | ||
register_node_visitor, | ||
) | ||
from executorch.backends.arm.tosa_mapping import TosaArg | ||
from executorch.backends.arm.tosa_utils import tosa_shape | ||
from serializer.tosa_serializer import TosaOp | ||
|
||
|
||
@register_node_visitor | ||
class UnsqueezeVisitor(NodeVisitor): | ||
target = "aten.unsqueeze_copy.default" | ||
|
||
def __init__(self, *args): | ||
super().__init__(*args) | ||
|
||
def define_node( | ||
self, | ||
node: torch.fx.Node, | ||
tosa_graph: ts.TosaSerializer, | ||
inputs: list[TosaArg], | ||
output: TosaArg, | ||
is_quant_node: bool, | ||
) -> None: | ||
|
||
dim = inputs[1].number | ||
shape = inputs[0].shape | ||
rank = len(shape) | ||
|
||
assert -rank - 1 <= dim < rank + 1 | ||
if dim < 0: | ||
dim = dim + rank + 1 | ||
|
||
new_shape = list(shape) | ||
new_shape.insert(dim, 1) | ||
new_shape = tosa_shape(new_shape, output.dim_order) | ||
|
||
attr = ts.TosaSerializerAttribute() | ||
attr.ReshapeAttribute(new_shape) | ||
tosa_graph.addOperator( | ||
TosaOp.Op().RESHAPE, [inputs[0].name], [output.name], attr | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,6 +267,7 @@ class ArmQuantizer(Quantizer): | |
"mul", | ||
"sigmoid", | ||
"mm", | ||
"cat", | ||
] | ||
|
||
def __init__(self) -> None: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
backends/arm/quantizer/quantization_annotation/cat_annotator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import itertools | ||
from typing import Callable, List, Optional | ||
|
||
import torch.fx | ||
from executorch.backends.arm.quantizer import arm_quantizer_utils | ||
from executorch.backends.arm.quantizer.quantization_annotation import register_annotator | ||
from executorch.backends.arm.quantizer.quantization_config import QuantizationConfig | ||
from torch.ao.quantization.quantizer import ( | ||
QuantizationAnnotation, | ||
SharedQuantizationSpec, | ||
) | ||
from torch.fx import Node | ||
from torch.fx.passes.utils.source_matcher_utils import get_source_partitions | ||
|
||
|
||
@register_annotator("cat") | ||
def _annotate_cat( | ||
gm: torch.fx.GraphModule, | ||
quantization_config: QuantizationConfig, | ||
filter_fn: Optional[Callable[[Node], bool]] = None, | ||
) -> Optional[List[List[Node]]]: | ||
cat_partitions = get_source_partitions(gm.graph, [torch.cat], filter_fn) | ||
cat_partitions = list(itertools.chain.from_iterable(cat_partitions.values())) | ||
annotated_partitions = [] | ||
for cat_partition in cat_partitions: | ||
annotated_partitions.append(cat_partition.nodes) | ||
cat_node = cat_partition.output_nodes[0] | ||
if arm_quantizer_utils.is_annotated(cat_node): | ||
continue | ||
|
||
input_acts = cat_node.args[0] | ||
input_act0 = input_acts[0] | ||
|
||
input_act_qspec = quantization_config.get_input_act_qspec() | ||
shared_with_input0_qspec = SharedQuantizationSpec((input_act0, cat_node)) | ||
|
||
input_qspec_map = {} | ||
|
||
# First input is set to input qspec from the quantization config. | ||
if isinstance(input_act0, Node): | ||
if not arm_quantizer_utils.is_input_ok_for_quantization(input_act0, gm): | ||
continue | ||
input_qspec_map[input_act0] = input_act_qspec | ||
|
||
# For the rest of the inputs, share qspec with first. | ||
# If we can't quantize any of the inputs, abort annotation. | ||
for input_act in input_acts[1:]: | ||
if isinstance(input_act, Node): | ||
if not arm_quantizer_utils.is_input_ok_for_quantization(input_act, gm): | ||
continue | ||
if input_act is not input_act0: | ||
input_qspec_map[input_act] = shared_with_input0_qspec | ||
|
||
if input_qspec_map is not None: | ||
cat_node.meta["quantization_annotation"] = QuantizationAnnotation( | ||
input_qspec_map=input_qspec_map, | ||
output_qspec=shared_with_input0_qspec, | ||
_annotated=True, | ||
) | ||
return annotated_partitions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
|
||
import torch | ||
from executorch.backends.arm.test import common | ||
from executorch.backends.arm.test.tester.arm_tester import ArmTester | ||
|
||
|
||
class LiftedTensor(torch.nn.Module): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.lifted_tensor = torch.Tensor([[1, 2], [3, 4]]) | ||
|
||
def forward(self, x: torch.Tensor, length) -> torch.Tensor: | ||
sliced = self.lifted_tensor[:, :length] | ||
return sliced + x | ||
|
||
|
||
class TestLiftedTensor(unittest.TestCase): | ||
"""Tests the ArmPartitioner with a placeholder of type lifted tensor.""" | ||
|
||
def test_partition_lifted_tensor(self): | ||
tester = ( | ||
ArmTester( | ||
LiftedTensor(), | ||
example_inputs=(torch.ones(2, 2), 2), | ||
compile_spec=common.get_tosa_compile_spec(), | ||
) | ||
.export() | ||
.to_edge() | ||
.dump_artifact() | ||
) | ||
signature = tester.get_artifact().exported_program().graph_signature | ||
assert len(signature.lifted_tensor_constants) > 0 | ||
tester.partition() | ||
tester.to_executorch() | ||
tester.run_method_and_compare_outputs((torch.ones(2, 2), 2)) |
Oops, something went wrong.