Skip to content

hg mv backend example to a different place #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 9 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ It also includes a list of modules, from a simple `Add` to a full model like `Mo
## Directory structure
```bash
examples
|── backend # Contains examples for exporting delegate models and running them using custom executor runners
├── custom_ops # Contains examples to register custom operators into PyTorch as well as register its kernels into Executorch runtime
├── executor_runner # This is an example C++ wrapper around the ET runtime
├── export # Python helper scripts to illustrate export workflow
├── ios_demo_apps # Contains iOS demo apps
├── models # Contains a set of simple to PyTorch models
├── quantization # Contains examples of quantization workflow
└── README.md # This file
|── backend # Contains examples for exporting delegate models and running them using custom executor runners
├── custom_ops # Contains examples to register custom operators into PyTorch as well as register its kernels into Executorch runtime
├── example_quantizer_and_delegate # Contains examples to to fully lowered a MobileNetV2 model to the example backend with an example quantizer
├── executor_runner # This is an example C++ wrapper around the ET runtime
├── export # Python helper scripts to illustrate export workflow
├── ios_demo_apps # Contains iOS demo apps
├── models # Contains a set of simple to PyTorch models
├── quantization # Contains examples of quantization workflow
└── README.md # This file
```

## Using the examples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ python_library(
],
deps = [
"//caffe2:torch",
"//executorch/backends/example/example_operators:example_operators_lib",
"//executorch/examples/example_quantizer_and_delegate/example_operators:example_operators_lib",
],
)

Expand All @@ -18,7 +18,7 @@ python_library(
"example_backend.py",
],
deps = [
"//executorch/backends/example/example_backend_delegate_passes:lib",
"//executorch/examples/example_quantizer_and_delegate/example_backend_delegate_passes:lib",
"//executorch/exir/backend:backend_details",
"//executorch/exir/backend:compile_spec_schema",
],
Expand All @@ -32,7 +32,7 @@ python_library(
deps = [
":example_backend",
"//caffe2:torch",
"//executorch/backends/example/example_operators:example_operators_lib",
"//executorch/examples/example_quantizer_and_delegate/example_operators:example_operators_lib",
"//executorch/exir:graph_module",
"//executorch/exir/backend:partitioner",
"//executorch/exir/backend/canonical_partitioners:canonical_partitioner_lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import copy
from typing import final, List

from executorch.backends.example.example_backend_delegate_passes.merge_to_dim_pass import (
from executorch.examples.example_quantizer_and_delegate.example_backend_delegate_passes.merge_to_dim_pass import (
MergeToDimPass,
)
from executorch.backends.example.example_backend_delegate_passes.permute_memory_formats_pass import (
from executorch.examples.example_quantizer_and_delegate.example_backend_delegate_passes.permute_memory_formats_pass import (
PermuteMemoryFormatsPass,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ python_library(
],
deps = [
"//caffe2:torch",
"//executorch/backends/example/example_operators:example_operators_lib",
"//executorch/examples/example_quantizer_and_delegate/example_operators:example_operators_lib",
"//executorch/exir:dim_order_utils",
"//executorch/exir:pass_base",
"//executorch/exir/dialects:lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from itertools import chain

import torch
from executorch.backends.example.example_operators.ops import module_to_annotator
from executorch.examples.example_quantizer_and_delegate.example_operators.ops import (
module_to_annotator,
)
from executorch.exir.dialects._ops import ops as exir_ops
from executorch.exir.dim_order_utils import get_dim_order
from executorch.exir.pass_base import ExportPass, PassResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from dataclasses import dataclass

import torch
from executorch.backends.example.example_operators.op_base import OpBase
from executorch.backends.example.example_operators.utils import (
from executorch.examples.example_quantizer_and_delegate.example_operators.op_base import (
OpBase,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.utils import (
_annotate_nodes,
_nodes_are_annotated,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@

from collections import OrderedDict

from executorch.backends.example.example_operators.adaptive_avg_pool2d import (
from executorch.examples.example_quantizer_and_delegate.example_operators.adaptive_avg_pool2d import (
AdaptiveAvgPool2dNode,
)

from executorch.backends.example.example_operators.add import AddNode
from executorch.backends.example.example_operators.conv2d import Conv2DNode
from executorch.backends.example.example_operators.conv_relu import ConvReluNode
from executorch.backends.example.example_operators.dropout import DropOutNode
from executorch.backends.example.example_operators.flatten import FlattenNode
from executorch.backends.example.example_operators.linear import LinearNode
from executorch.examples.example_quantizer_and_delegate.example_operators.add import (
AddNode,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.conv2d import (
Conv2DNode,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.conv_relu import (
ConvReluNode,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.dropout import (
DropOutNode,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.flatten import (
FlattenNode,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.linear import (
LinearNode,
)

# The ordering of this is important as the quantizer will try to match the patterns in this order.
# That's why we want to match the fused patterns first and then the non-fused ones.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
from typing import Dict, final

import torch
from executorch.backends.example.example_backend import TosaBackend
from executorch.backends.example.example_operators.ops import module_to_annotator
from executorch.examples.example_quantizer_and_delegate.example_backend import (
TosaBackend,
)
from executorch.examples.example_quantizer_and_delegate.example_operators.ops import (
module_to_annotator,
)
from executorch.exir.backend.canonical_partitioners.pattern_op_partitioner import (
generate_partitions_from_list_of_nodes,
)
from executorch.exir.backend.partitioner import DelegationSpec, Partitioner
from executorch.exir.backend.partitioner import (
DelegationSpec,
Partitioner,
PartitionResult,
)
from executorch.exir.dialects._ops import ops as exir_ops
from executorch.exir.graph_module import get_control_flow_submodules
from torch.ao.quantization.pt2e.graph_utils import find_sequential_partitions
Expand All @@ -28,7 +36,6 @@ class ExamplePartitioner(Partitioner):
def __init__(self) -> None:
self.patterns = module_to_annotator.keys()
self.delegation_spec = DelegationSpec(TosaBackend.__name__, [])
self.partition_tags: Dict[str, DelegationSpec] = {}

class DequantQuantOperatorSupport(OperatorSupportBase):
def is_node_supported(self, _submodules, node: torch.fx.Node) -> bool:
Expand All @@ -39,9 +46,8 @@ def is_node_supported(self, _submodules, node: torch.fx.Node) -> bool:

self.dequant_quant_support = DequantQuantOperatorSupport()

def partition(
self, edge_graph_module: torch.fx.GraphModule
) -> torch.fx.GraphModule:
def partition(self, edge_graph_module: torch.fx.GraphModule) -> PartitionResult:
partition_tags: Dict[str, DelegationSpec] = {}
partition_nodes = []
for pattern in self.patterns:
fused_partitions = find_sequential_partitions(
Expand All @@ -68,9 +74,11 @@ def partition(
and arg_node.op == "get_attr"
):
arg_node.meta["delegation_tag"] = delegation_tag
self.partition_tags[delegation_tag] = self.delegation_spec
partition_tags[delegation_tag] = self.delegation_spec

for _, submodule, _ in get_control_flow_submodules(edge_graph_module):
self.partition(submodule)

return edge_graph_module
return PartitionResult(
tagged_graph=edge_graph_module, partition_tags=partition_tags
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from typing import List

import torch
from executorch.backends.example.example_operators.ops import module_to_annotator
from executorch.examples.example_quantizer_and_delegate.example_operators.ops import (
module_to_annotator,
)
from torch import fx
from torch.ao.quantization.observer import HistogramObserver, MinMaxObserver
from torch.ao.quantization.pt2e.graph_utils import find_sequential_partitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
import torch
import torch._export as export
from executorch import exir
from executorch.backends.example.example_partitioner import ExamplePartitioner
from executorch.backends.example.example_quantizer import ExampleQuantizer
from executorch.examples.example_quantizer_and_delegate.example_partitioner import (
ExamplePartitioner,
)
from executorch.examples.example_quantizer_and_delegate.example_quantizer import (
ExampleQuantizer,
)
from executorch.exir.backend.backend_api import to_backend

from executorch.exir.backend.canonical_partitioners.duplicate_dequant_node_pass import (
Expand Down