diff --git a/benchmarks/framework_overhead_benchmark/C2Module.py b/benchmarks/framework_overhead_benchmark/C2Module.py index 6c4c1bbad29..0b93836e594 100644 --- a/benchmarks/framework_overhead_benchmark/C2Module.py +++ b/benchmarks/framework_overhead_benchmark/C2Module.py @@ -1,8 +1,9 @@ import numpy as np -from caffe2.python import core, workspace from utils import NUM_LOOP_ITERS +from caffe2.python import core, workspace + workspace.GlobalInit(["caffe2"]) diff --git a/benchmarks/operator_benchmark/benchmark_caffe2.py b/benchmarks/operator_benchmark/benchmark_caffe2.py index 24b61e52d5b..2d238e593fc 100644 --- a/benchmarks/operator_benchmark/benchmark_caffe2.py +++ b/benchmarks/operator_benchmark/benchmark_caffe2.py @@ -2,6 +2,7 @@ from collections import namedtuple import benchmark_utils from benchmark_test_generator import _register_test + from caffe2.proto import caffe2_pb2 from caffe2.python import core, workspace diff --git a/benchmarks/operator_benchmark/c2/add_test.py b/benchmarks/operator_benchmark/c2/add_test.py index 4f38e8cd1ae..c3b71f3e851 100644 --- a/benchmarks/operator_benchmark/c2/add_test.py +++ b/benchmarks/operator_benchmark/c2/add_test.py @@ -1,8 +1,8 @@ import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for element-wise Add operator. Supports both Caffe2/PyTorch.""" diff --git a/benchmarks/operator_benchmark/c2/batch_box_cox_test.py b/benchmarks/operator_benchmark/c2/batch_box_cox_test.py index 8f833e8fa6f..7c40f513cd6 100644 --- a/benchmarks/operator_benchmark/c2/batch_box_cox_test.py +++ b/benchmarks/operator_benchmark/c2/batch_box_cox_test.py @@ -1,8 +1,8 @@ import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for BatchBoxCox operator.""" diff --git a/benchmarks/operator_benchmark/c2/batch_gather_test.py b/benchmarks/operator_benchmark/c2/batch_gather_test.py index 6252f7a25a9..c0ff2c06f06 100644 --- a/benchmarks/operator_benchmark/c2/batch_gather_test.py +++ b/benchmarks/operator_benchmark/c2/batch_gather_test.py @@ -1,9 +1,9 @@ import benchmark_caffe2 as op_bench_c2 import numpy from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for element-wise BatchGather operator.""" diff --git a/benchmarks/operator_benchmark/c2/clip_ranges_test.py b/benchmarks/operator_benchmark/c2/clip_ranges_test.py index e883eaeb2ee..57bcd9858a8 100644 --- a/benchmarks/operator_benchmark/c2/clip_ranges_test.py +++ b/benchmarks/operator_benchmark/c2/clip_ranges_test.py @@ -1,8 +1,8 @@ import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core, dyndep import operator_benchmark as op_bench +from caffe2.python import core, dyndep dyndep.InitOpsLibrary("@/caffe2/caffe2/fb/operators:clip_ranges_op") diff --git a/benchmarks/operator_benchmark/c2/concat_test.py b/benchmarks/operator_benchmark/c2/concat_test.py index 17e041a682a..4e91c30f2a7 100644 --- a/benchmarks/operator_benchmark/c2/concat_test.py +++ b/benchmarks/operator_benchmark/c2/concat_test.py @@ -2,9 +2,9 @@ import random import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for Concat operator. Supports both Caffe2/PyTorch.""" diff --git a/benchmarks/operator_benchmark/c2/matmul_test.py b/benchmarks/operator_benchmark/c2/matmul_test.py index ed18388305c..72bc4c78d71 100644 --- a/benchmarks/operator_benchmark/c2/matmul_test.py +++ b/benchmarks/operator_benchmark/c2/matmul_test.py @@ -1,8 +1,8 @@ import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for MatMul operator""" diff --git a/benchmarks/operator_benchmark/c2/quantile_op_test.py b/benchmarks/operator_benchmark/c2/quantile_op_test.py index bbbfcd6832e..296b6bf189e 100644 --- a/benchmarks/operator_benchmark/c2/quantile_op_test.py +++ b/benchmarks/operator_benchmark/c2/quantile_op_test.py @@ -1,8 +1,8 @@ import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for QuantileOp operator.""" diff --git a/benchmarks/operator_benchmark/c2/replace_nan_test.py b/benchmarks/operator_benchmark/c2/replace_nan_test.py index c1b2521a5df..c735a69b4ab 100644 --- a/benchmarks/operator_benchmark/c2/replace_nan_test.py +++ b/benchmarks/operator_benchmark/c2/replace_nan_test.py @@ -1,8 +1,8 @@ import benchmark_caffe2 as op_bench_c2 from benchmark_caffe2 import Caffe2BenchmarkBase # noqa: F401 -from caffe2.python import core import operator_benchmark as op_bench +from caffe2.python import core """Microbenchmarks for element-wise ReplaceNaN operator.""" diff --git a/benchmarks/operator_benchmark/common/tests/c2_cpu_gpu_forward_backward_test.py b/benchmarks/operator_benchmark/common/tests/c2_cpu_gpu_forward_backward_test.py index d97fc9affea..ff34a58533f 100644 --- a/benchmarks/operator_benchmark/common/tests/c2_cpu_gpu_forward_backward_test.py +++ b/benchmarks/operator_benchmark/common/tests/c2_cpu_gpu_forward_backward_test.py @@ -1,4 +1,5 @@ import operator_benchmark as op_bench + from caffe2.python import core diff --git a/pyproject.toml b/pyproject.toml index 11ffdc5017e..3e6fbf9c7c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,10 +34,11 @@ include_trailing_comma = true [tool.usort.known] +first_party = ["caffe2"] standard_library = ["typing_extensions"] [tool.usort.kown] -first_party = ["caffe2", "torch", "torchgen", "functorch", "tests"] +first_party = ["torch", "torchgen", "functorch", "tests"] [tool.ruff] diff --git a/scripts/model_zoo/update-models-from-caffe2.py b/scripts/model_zoo/update-models-from-caffe2.py index b79e0bf0002..3d4d4d5d1c0 100644 --- a/scripts/model_zoo/update-models-from-caffe2.py +++ b/scripts/model_zoo/update-models-from-caffe2.py @@ -11,12 +11,14 @@ import tempfile from urllib.request import urlretrieve import boto3 -import caffe2.python.onnx.backend -import caffe2.python.onnx.frontend -import caffe2.python.workspace as c2_workspace import numpy as np import onnx import onnx.backend +from onnx import numpy_helper + +import caffe2.python.onnx.backend +import caffe2.python.onnx.frontend +import caffe2.python.workspace as c2_workspace from caffe2.proto import caffe2_pb2 from caffe2.python.models.download import ( @@ -24,7 +26,6 @@ from caffe2.python.models.download import ( downloadFromURLToFile, getURLFromName, ) -from onnx import numpy_helper """A script converting Caffe2 models to ONNX, and updating ONNX model zoos. diff --git a/test/onnx/debug_embed_params.py b/test/onnx/debug_embed_params.py index 3bee953dd4e..ea6db1de1e8 100644 --- a/test/onnx/debug_embed_params.py +++ b/test/onnx/debug_embed_params.py @@ -1,13 +1,13 @@ import sys -import caffe2.python.onnx.backend as c2 - import onnx import pytorch_test_common import torch import torch.jit from torch.autograd import Variable +import caffe2.python.onnx.backend as c2 + torch.set_default_tensor_type("torch.FloatTensor") try: import torch diff --git a/test/onnx/pytorch_helper.py b/test/onnx/pytorch_helper.py index 2191a3bb13c..ff1c9faadea 100644 --- a/test/onnx/pytorch_helper.py +++ b/test/onnx/pytorch_helper.py @@ -3,6 +3,7 @@ import io import onnx import torch.onnx + from caffe2.python.core import BlobReference, Net from caffe2.python.onnx.backend import Caffe2Backend diff --git a/test/onnx_caffe2/test_caffe2_common.py b/test/onnx_caffe2/test_caffe2_common.py index 45f5a44ff6b..e85f4b8aef3 100644 --- a/test/onnx_caffe2/test_caffe2_common.py +++ b/test/onnx_caffe2/test_caffe2_common.py @@ -3,12 +3,12 @@ import glob import os -import caffe2.python.onnx.backend as c2 - import numpy as np import onnx.backend.test from onnx import numpy_helper +import caffe2.python.onnx.backend as c2 + def load_tensor_as_numpy_array(f): tensor = onnx.TensorProto() diff --git a/test/onnx_caffe2/test_custom_ops.py b/test/onnx_caffe2/test_custom_ops.py index 560b42cca64..649db9f663e 100644 --- a/test/onnx_caffe2/test_custom_ops.py +++ b/test/onnx_caffe2/test_custom_ops.py @@ -1,6 +1,5 @@ # Owner(s): ["module: onnx"] -import caffe2.python.onnx.backend as c2 import numpy as np import onnx import pytorch_test_common @@ -9,6 +8,8 @@ import torch.utils.cpp_extension from test_pytorch_onnx_caffe2 import do_export from torch.testing._internal import common_utils +import caffe2.python.onnx.backend as c2 + class TestCaffe2CustomOps(pytorch_test_common.ExportTestCase): def test_custom_add(self): diff --git a/test/onnx_caffe2/test_pytorch_helper.py b/test/onnx_caffe2/test_pytorch_helper.py index 7d7f3ade7f5..778b8553b65 100644 --- a/test/onnx_caffe2/test_pytorch_helper.py +++ b/test/onnx_caffe2/test_pytorch_helper.py @@ -8,13 +8,14 @@ import pytorch_test_common import torch.nn.init as init import torch.onnx -from caffe2.python.core import workspace -from caffe2.python.model_helper import ModelHelper from pytorch_helper import PyTorchModule from torch import nn from torch.testing._internal import common_utils from torch.testing._internal.common_utils import skipIfNoLapack +from caffe2.python.core import workspace +from caffe2.python.model_helper import ModelHelper + class TestCaffe2Backend(pytorch_test_common.ExportTestCase): @skipIfNoLapack diff --git a/test/onnx_caffe2/test_pytorch_onnx_caffe2.py b/test/onnx_caffe2/test_pytorch_onnx_caffe2.py index a3254995a25..dd425ee5b70 100644 --- a/test/onnx_caffe2/test_pytorch_onnx_caffe2.py +++ b/test/onnx_caffe2/test_pytorch_onnx_caffe2.py @@ -6,8 +6,6 @@ import sys import unittest from typing import Tuple -import caffe2.python.onnx.backend as c2 - import model_defs.dcgan as dcgan import model_defs.word_language_model as word_language_model import numpy as np @@ -17,10 +15,6 @@ import torch.onnx import torch.onnx.operators import torch.utils.model_zoo as model_zoo import verify -from caffe2.python.operator_test.torch_integration_test import ( - create_bbox_transform_inputs, - generate_rois_rotated, -) from debug_embed_params import run_embed_params from model_defs.lstm_flattening_result import LstmFlatteningResult from model_defs.mnist import MNIST @@ -53,6 +47,12 @@ from torchvision.models.inception import inception_v3 from torchvision.models.resnet import resnet50 from torchvision.models.vgg import vgg16, vgg16_bn, vgg19, vgg19_bn +import caffe2.python.onnx.backend as c2 +from caffe2.python.operator_test.torch_integration_test import ( + create_bbox_transform_inputs, + generate_rois_rotated, +) + skip = unittest.skip diff --git a/test/onnx_caffe2/test_pytorch_onnx_caffe2_quantized.py b/test/onnx_caffe2/test_pytorch_onnx_caffe2_quantized.py index 92079ebbe6d..deb46102c87 100644 --- a/test/onnx_caffe2/test_pytorch_onnx_caffe2_quantized.py +++ b/test/onnx_caffe2/test_pytorch_onnx_caffe2_quantized.py @@ -2,8 +2,6 @@ import io -import caffe2.python.onnx.backend as c2 - import numpy as np import onnx import pytorch_test_common @@ -12,6 +10,8 @@ import torch.nn as nn import torch.onnx from torch.testing._internal import common_utils +import caffe2.python.onnx.backend as c2 + class TestQuantizedOps(pytorch_test_common.ExportTestCase): def generic_test( diff --git a/test/onnx_caffe2/test_verify.py b/test/onnx_caffe2/test_verify.py index 3a5dc271484..f5b3000eb9c 100644 --- a/test/onnx_caffe2/test_verify.py +++ b/test/onnx_caffe2/test_verify.py @@ -1,12 +1,13 @@ # Owner(s): ["module: onnx"] -import caffe2.python.onnx.backend as backend import torch from torch.autograd import Function from torch.nn import Module, Parameter from torch.testing._internal import common_utils from verify import verify +import caffe2.python.onnx.backend as backend + class TestVerify(common_utils.TestCase): maxDiff = None