Skip to content

Commit 4a6a222

Browse files
guangy10facebook-github-bot
authored andcommitted
Add torchvision_vit model to the examples (#33)
Summary: Pull Request resolved: #33 This diff is adding a selected model for Apple. Info about the the model: https://pytorch.org/vision/main/models/generated/torchvision.models.vit_b_16.html#torchvision.models.vit_b_16 Differential Revision: D48012005 fbshipit-source-id: 285c7b11704d24b613b6cba1ea64845b60e1b9ee
1 parent 4179cc8 commit 4a6a222

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

examples/models/TARGETS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ python_library(
1111
"//executorch/backends:compile_spec_schema",
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
],
1516
)

examples/models/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import torch
1313
from executorch.backends.compile_spec_schema import CompileSpec
1414

15+
from .torchvision_vit import TorchVisionViTModel
16+
1517

1618
class MulModule(torch.nn.Module):
1719
def __init__(self) -> None:
@@ -89,11 +91,18 @@ def gen_mobilenet_v2_model_inputs() -> Tuple[torch.nn.Module, Any]:
8991
return MV2Model.get_model(), MV2Model.get_example_inputs()
9092

9193

94+
def gen_torchvision_vit_model_and_inputs() -> Tuple[torch.nn.Module, Any]:
95+
from ..models.torchvision_vit import TorchVisionViTModel
96+
97+
return TorchVisionViTModel.get_model(), TorchVisionViTModel.get_example_inputs()
98+
99+
92100
MODEL_NAME_TO_MODEL = {
93101
"mul": lambda: (MulModule(), MulModule.get_example_inputs()),
94102
"linear": lambda: (LinearModule(), LinearModule.get_example_inputs()),
95103
"add": lambda: (AddModule(), AddModule.get_example_inputs()),
96104
"add_mul": lambda: (AddMulModule(), AddMulModule.get_example_inputs()),
97105
"mv2": gen_mobilenet_v2_model_inputs,
98106
"mv3": gen_mobilenet_v3_model_inputs,
107+
"vit": gen_torchvision_vit_model_and_inputs,
99108
}
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)