Skip to content

Commit

Permalink
Fix Pyre problems in executorch (pytorch#3955)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#3955

Current problems:

https://www.internalfb.com/phabricator/paste/view/P1410619602

We will eventually migrate from pyre to mypy at some point. But in the meantime, here's a fix.

Reviewed By: tarun292

Differential Revision: D58473224

fbshipit-source-id: a2e789b6c324c98d64c26f5ad5103de60dce513d
  • Loading branch information
mergennachin authored and facebook-github-bot committed Jun 12, 2024
1 parent 189d548 commit 70a4d98
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 27 deletions.
1 change: 0 additions & 1 deletion backends/xnnpack/test/models/inception_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class TestInceptionV3(unittest.TestCase):
# pyre-ignore
ic3 = models.inception_v3(weights="IMAGENET1K_V1").eval() # noqa
model_inputs = (torch.randn(1, 3, 224, 224),)

Expand Down
2 changes: 0 additions & 2 deletions examples/models/inception_v3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import torch

# pyre-ignore
from torchvision.models import inception_v3 # @manual

from ..model_base import EagerModelBase
Expand All @@ -20,7 +19,6 @@ def __init__(self):

def get_eager_model(self) -> torch.nn.Module:
logging.info("Loading torchvision inception_v3 model")
# pyre-ignore
inception_v3_model = inception_v3(weights="IMAGENET1K_V1")
logging.info("Loaded torchvision inception_v3 model")
return inception_v3_model
Expand Down
8 changes: 6 additions & 2 deletions examples/models/llama2/lib/partitioner_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ def get_coreml_partitioner(args):
), "CoreML backend currently only supports static shape and use_kv_cache=True is the only way to support it at the moment"
try:
import coremltools as ct
from executorch.backends.apple.coreml.compiler import CoreMLBackend
from executorch.backends.apple.coreml.partition import CoreMLPartitioner
from executorch.backends.apple.coreml.compiler import ( # pyre-ignore
CoreMLBackend,
)
from executorch.backends.apple.coreml.partition import ( # pyre-ignore
CoreMLPartitioner,
)
except ImportError:
raise ImportError(
"Please install the CoreML backend follwing https://pytorch.org/executorch/main/build-run-coreml.html"
Expand Down
7 changes: 2 additions & 5 deletions examples/models/llama2/source_transformation/quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
from ..builder import DType

try:
# pyre-ignore[21]: Undefined import.
from fairseq2.nn.embedding import (
Embedding as fsEmbedding,
StandardEmbedding as fsStandardEmbedding,
)

# pyre-ignore[21]: Undefined import.
from fairseq2.nn.projection import Linear as fsLinear

print("Using fairseq2 modules.")
Expand Down Expand Up @@ -98,10 +96,9 @@ def quantize(

try:
# torchao 0.3+
# pyre-ignore
from torchao._eval import InputRecorder
except ImportError:
from torchao.quantization.GPTQ import InputRecorder
from torchao.quantization.GPTQ import InputRecorder # pyre-ignore

from torchao.quantization.quant_api import Int8DynActInt4WeightGPTQQuantizer

Expand All @@ -113,7 +110,7 @@ def quantize(
)

inputs = (
InputRecorder( # pyre-ignore
InputRecorder(
tokenizer,
calibration_seq_length,
None, # input_prep_func
Expand Down
3 changes: 0 additions & 3 deletions examples/models/mobilenet_v2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import torch

# pyre-ignore
from torchvision.models import mobilenet_v2 # @manual
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights

Expand All @@ -21,7 +20,6 @@ def __init__(self):

def get_eager_model(self) -> torch.nn.Module:
logging.info("Loading mobilenet_v2 model")
# pyre-ignore
mv2 = mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT)
logging.info("Loaded mobilenet_v2 model")
return mv2
Expand All @@ -36,7 +34,6 @@ def __init__(self):
pass

def get_eager_model(self) -> torch.nn.Module:
# pyre-ignore
mv2 = mobilenet_v2()
return mv2

Expand Down
2 changes: 0 additions & 2 deletions examples/models/mobilenet_v3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def __init__(self):

def get_eager_model(self) -> torch.nn.Module:
logging.info("Loading mobilenet_v3 model")
# pyre-ignore
mv3_small = models.mobilenet_v3_small(
# pyre-ignore[16]
weights=models.MobileNet_V3_Small_Weights.IMAGENET1K_V1
)
logging.info("Loaded mobilenet_v3 model")
Expand Down
3 changes: 0 additions & 3 deletions examples/models/resnet/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import torch

# pyre-ignore
from torchvision.models import ( # @manual
resnet18,
ResNet18_Weights,
Expand All @@ -25,7 +24,6 @@ def __init__(self):

def get_eager_model(self) -> torch.nn.Module:
logging.info("Loading torchvision resnet18 model")
# pyre-ignore
resnet18_model = resnet18(weights=ResNet18_Weights.IMAGENET1K_V1)
logging.info("Loaded torchvision resnet18 model")
return resnet18_model
Expand All @@ -41,7 +39,6 @@ def __init__(self):

def get_eager_model(self) -> torch.nn.Module:
logging.info("Loading torchvision resnet50 model")
# pyre-ignore
resnet50_model = resnet50(weights=ResNet50_Weights.IMAGENET1K_V1)
logging.info("Loaded torchvision resnet50 model")
return resnet50_model
Expand Down
2 changes: 0 additions & 2 deletions examples/models/torchvision_vit/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import torch

# pyre-ignore
from torchvision.models import vit_b_16 # @manual

from ..model_base import EagerModelBase
Expand All @@ -20,7 +19,6 @@ def __init__(self):

def get_eager_model(self) -> torch.nn.Module:
logging.info("Loading torchvision vit_b_16 model")
# pyre-ignore
vit_b_16_model = vit_b_16(weights="IMAGENET1K_V1")
logging.info("Loaded torchvision vit_b_16 model")
return vit_b_16_model
Expand Down
2 changes: 1 addition & 1 deletion exir/serde/export_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ def deserialize_argument_spec(self, x: Argument) -> ep.ArgumentSpec:
elif x.type == "as_sym_int":
return PySymIntArgument(name=x.as_sym_int.as_name)
else:
return PyConstantArgument(value=self.deserialize_input(x))
return PyConstantArgument(name="", value=self.deserialize_input(x))

def deserialize_module_call_signature(
self, module_call_signature: ModuleCallSignature
Expand Down
8 changes: 4 additions & 4 deletions exir/serde/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def deserialize(

def save(
ep_save: ep.ExportedProgram,
f: Union[str, os.PathLike, io.BytesIO],
f: Union[str, os.PathLike[str], io.BytesIO],
*,
extra_files: Optional[Dict[str, Any]] = None,
opset_version: Optional[Dict[str, int]] = None,
Expand All @@ -767,7 +767,7 @@ def save(
artifact: export_serialize.SerializedArtifact = serialize(ep_save, opset_version)

if isinstance(f, (str, os.PathLike)):
f = os.fspath(f)
f = os.fspath(str(f))

with zipfile.ZipFile(f, "w") as zipf:
# Save every field in the SerializedArtifact to a file.
Expand All @@ -786,13 +786,13 @@ def save(


def load(
f: Union[str, os.PathLike, io.BytesIO],
f: Union[str, os.PathLike[str], io.BytesIO],
*,
extra_files: Optional[Dict[str, Any]] = None,
expected_opset_version: Optional[Dict[str, int]] = None,
) -> ep.ExportedProgram:
if isinstance(f, (str, os.PathLike)):
f = os.fspath(f)
f = os.fspath(str(f))

extra_files = extra_files or {}

Expand Down
2 changes: 1 addition & 1 deletion exir/tests/test_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_resnet(self) -> None:
with override_quantized_engine("qnnpack"):
torch.backends.quantized.engine = "qnnpack"
example_inputs = (torch.randn(1, 3, 224, 224),)
m = torchvision.models.resnet18().eval() # pyre-ignore[16]
m = torchvision.models.resnet18().eval()
m_copy = copy.deepcopy(m)
# program capture
m = torch._export.capture_pre_autograd_graph(
Expand Down
2 changes: 1 addition & 1 deletion exir/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
is_functionaltensor,
)

from torch._functorch.eager_transforms import _assert_wrapped_functional
from torch._functorch.eager_transforms import _assert_wrapped_functional # pyre-ignore


def update_with_proxy(t: torch.Tensor, proxy: torch.fx.Proxy) -> torch.Tensor:
Expand Down

0 comments on commit 70a4d98

Please sign in to comment.