Skip to content

Commit 263ca35

Browse files
guangy10facebook-github-bot
authored andcommitted
Add torchvision_vit model to the examples (#33)
Summary: Pull Request resolved: #33 Info about the the model: https://pytorch.org/vision/main/models/generated/torchvision.models.vit_b_16.html#torchvision.models.vit_b_16 Reviewed By: kimishpatel, kirklandsign Differential Revision: D48012005 fbshipit-source-id: 62c725871d0ceb42ca0b9167a7d6153050675b63
1 parent d62a934 commit 263ca35

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

examples/export/test/test_export.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
from executorch.examples.export.utils import _CAPTURE_CONFIG, _EDGE_COMPILE_CONFIG
1212
from executorch.examples.models import MODEL_NAME_TO_MODEL
1313

14-
# pyre-ignore[21]: Could not find module `executorch.extension.pybindings.portable`.
15-
from executorch.extension.pybindings.portable import ( # @manual
16-
_load_for_executorch_from_buffer,
17-
)
18-
1914

2015
class ExportTest(unittest.TestCase):
2116
def _assert_eager_lowered_same_result(
@@ -28,13 +23,11 @@ def _assert_eager_lowered_same_result(
2823
)
2924

3025
executorch_model = edge_model.to_executorch()
31-
# pyre-ignore
32-
pte_model = _load_for_executorch_from_buffer(executorch_model.buffer)
3326

3427
with torch.no_grad():
3528
eager_output = eager_model(*example_inputs)
3629
with torch.no_grad():
37-
executorch_output = pte_model.forward(example_inputs)
30+
executorch_output = executorch_model.exported_program(*example_inputs)
3831

3932
if isinstance(eager_output, tuple):
4033
# TODO: Allow validating other items
@@ -65,3 +58,9 @@ def test_emformer_export_to_executorch(self):
6558
eager_model = eager_model.eval()
6659

6760
self._assert_eager_lowered_same_result(eager_model, example_inputs)
61+
62+
def test_vit_export_to_executorch(self):
63+
eager_model, example_inputs = MODEL_NAME_TO_MODEL["vit"]()
64+
eager_model = eager_model.eval()
65+
66+
self._assert_eager_lowered_same_result(eager_model, example_inputs)

examples/models/TARGETS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ python_library(
1111
"//executorch/examples/models/emformer:emformer_export",
1212
"//executorch/examples/models/mobilenet_v2:mv2_export",
1313
"//executorch/examples/models/mobilenet_v3:mv3_export",
14+
"//executorch/examples/models/torchvision_vit:vit_export",
1415
"//executorch/exir/backend:compile_spec_schema",
1516
],
1617
)

examples/models/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def gen_emformer_model_inputs() -> Tuple[torch.nn.Module, Any]:
9595
return EmformerModel.get_model(), EmformerModel.get_example_inputs()
9696

9797

98+
def gen_torchvision_vit_model_and_inputs() -> Tuple[torch.nn.Module, Any]:
99+
from ..models.torchvision_vit import TorchVisionViTModel
100+
101+
return TorchVisionViTModel.get_model(), TorchVisionViTModel.get_example_inputs()
102+
103+
98104
MODEL_NAME_TO_MODEL = {
99105
"mul": lambda: (MulModule(), MulModule.get_example_inputs()),
100106
"linear": lambda: (LinearModule(), LinearModule.get_example_inputs()),
@@ -103,4 +109,5 @@ def gen_emformer_model_inputs() -> Tuple[torch.nn.Module, Any]:
103109
"mv2": gen_mobilenet_v2_model_inputs,
104110
"mv3": gen_mobilenet_v3_model_inputs,
105111
"emformer": gen_emformer_model_inputs,
112+
"vit": gen_torchvision_vit_model_and_inputs,
106113
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@fbcode_macros//build_defs:python_library.bzl", "python_library")
2+
3+
python_library(
4+
name = "vit_export",
5+
srcs = [
6+
"__init__.py",
7+
"export.py",
8+
],
9+
base_module = "executorch.examples.models.torchvision_vit",
10+
deps = [
11+
"//caffe2:torch",
12+
"//pytorch/vision:torchvision",
13+
],
14+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
from .export import TorchVisionViTModel
8+
9+
__all__ = [
10+
TorchVisionViTModel,
11+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import logging
8+
9+
import torch
10+
from torchvision import models
11+
12+
FORMAT = "[%(filename)s:%(lineno)s] %(message)s"
13+
logging.basicConfig(format=FORMAT)
14+
15+
16+
class TorchVisionViTModel:
17+
def __init__(self):
18+
pass
19+
20+
@staticmethod
21+
def get_model():
22+
logging.info("loading torchvision vit_b_16 model")
23+
vit_b_16 = models.vit_b_16(weights="IMAGENET1K_V1")
24+
logging.info("loaded torchvision vit_b_16 model")
25+
return vit_b_16
26+
27+
@staticmethod
28+
def get_example_inputs():
29+
input_shape = (1, 3, 224, 224)
30+
return (torch.randn(input_shape),)

0 commit comments

Comments
 (0)