-
Notifications
You must be signed in to change notification settings - Fork 687
Add some basic xnnpack recipes #10035
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
e42c965
910ac24
fab9fe8
6ae1d97
3c52f3a
be8de30
a02decd
62592eb
3e56ded
2b1e447
ec902d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@fbcode_macros//build_defs:python_library.bzl", "python_library") | ||
|
||
|
||
oncall("executorch") | ||
|
||
python_library( | ||
name = "xnnpack_recipes", | ||
srcs = [ | ||
"recipes.py", | ||
], | ||
deps = [ | ||
"//caffe2:torch", | ||
"//executorch/exir:lib", | ||
"//executorch/backends/transforms:duplicate_dynamic_quant_chain", | ||
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer", | ||
"//executorch/backends/xnnpack/partition:xnnpack_partitioner", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||||||
# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||||||||||
|
||||||||||
# pyre-strict | ||||||||||
from typing import Any, Callable | ||||||||||
|
||||||||||
from executorch.backends.transforms.duplicate_dynamic_quant_chain import ( | ||||||||||
duplicate_dynamic_quant_chain_pass, | ||||||||||
DuplicateDynamicQuantChainPass, | ||||||||||
) | ||||||||||
|
||||||||||
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner | ||||||||||
|
||||||||||
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import ( | ||||||||||
get_symmetric_quantization_config, | ||||||||||
XNNPACKQuantizer, | ||||||||||
) | ||||||||||
from executorch.exir import ExportRecipe | ||||||||||
|
||||||||||
def get_generic_fp32_cpu_recipe() -> ExportRecipe: | ||||||||||
quantizer = XNNPACKQuantizer() | ||||||||||
operator_config = get_symmetric_quantization_config(is_per_channel=False) | ||||||||||
quantizer.set_global(operator_config) | ||||||||||
|
quantizer = XNNPACKQuantizer() | |
operator_config = get_symmetric_quantization_config(is_per_channel=False) | |
quantizer.set_global(operator_config) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
name = "fp32_recipe", | |
name = "fp32", |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
organizationally maybe quant recipes can be in a separate folder?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've actually done some work to remove the need for this. I guess it's ok to have for now as it should still work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove it if not needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
"FP32_CPU_ACCELERATED_RECIPE": get_generic_fp32_cpu_recipe, | |
"DYNAMIC_QUANT_CPU_ACCELERATED_RECIPE": get_dynamic_quant_recipe, | |
"FP32_RECIPE": get_fp32_recipe, | |
"DYNAMIC_QUANT_RECIPE": get_dynamic_quant_recipe, |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
|
||
# pyre-strict | ||
|
||
import unittest | ||
|
||
import torch | ||
from executorch.backends.xnnpack import get_xnnpack_recipe | ||
from executorch.export import export | ||
from torch.testing._internal.common_quantization import TestHelperModules | ||
|
||
class TestXnnpackRecipes(unittest.TestCase): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
|
||
def tearDown(self) -> None: | ||
super().tearDown() | ||
Comment on lines
+20
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need these? |
||
|
||
def test_basic_recipe(self) -> None: | ||
m_eager = TestHelperModules.TwoLinearModule().eval() | ||
example_inputs = [(torch.randn(9, 8),)] | ||
export_session = export( | ||
model=m_eager, | ||
example_inputs=example_inputs, | ||
export_recipe=get_xnnpack_recipe("FP32_CPU_ACCELERATED_RECIPE") | ||
) | ||
export_session.export() | ||
|
||
|
||
def test_dynamic_quant_recipe(self) -> None: | ||
m_eager = TestHelperModules.TwoLinearModule().eval() | ||
example_inputs = [(torch.randn(9, 8),)] | ||
export_session = export( | ||
model=m_eager, | ||
example_inputs=example_inputs, | ||
export_recipe=get_xnnpack_recipe("DYNAMIC_QUANT_CPU_ACCELERATED_RECIPE") | ||
) | ||
export_session.export() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
load("@fbcode_macros//build_defs:python_library.bzl", "python_library") | ||
|
||
oncall("executorch") | ||
|
||
python_library( | ||
name = "recipe", | ||
srcs = [ | ||
"_recipe.py", | ||
], | ||
deps = [ | ||
"//caffe2:torch", | ||
"//executorch/exir/backend:backend_api", | ||
"//executorch/exir:pass_manager", | ||
] | ||
) | ||
|
||
python_library( | ||
name = "export", | ||
srcs = [ | ||
"_export.py", | ||
], | ||
deps = [ | ||
":recipe", | ||
] | ||
) | ||
|
||
python_library( | ||
name = "lib", | ||
srcs = [ | ||
"__init__.py", | ||
], | ||
deps = [ | ||
":export", | ||
":recipe", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
""" | ||
ExecuTorch export module. | ||
|
||
This module provides the tools and utilities for exporting PyTorch models | ||
to the ExecuTorch format, including configuration, quantization, and | ||
export management. | ||
""" | ||
|
||
# pyre-strict | ||
|
||
from ._export import ExportSession, export | ||
from ._recipe import ExportRecipe | ||
|
||
__all__ = [ | ||
"ExportRecipe", | ||
"ExportSession", | ||
"export", | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if namespaced to XNNPACK then cpu may not be needed?