mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 12:54:11 +08:00
Summary: Fixed a bunch of fbcode imports that happened to work but confused autodeps. After this autodeps still suggests "improvements" to TARGETS (which breaks our builds) but at least it can find all the imports. Test Plan: ``` fbpython fbcode/tools/build/buck/linters/lint_autoformat.py --linter=autodeps --default-exec-timeout=1800 -- fbcode/caffe2/TARGETS fbcode/caffe2/test/TARGETS ``` Before: ``` ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "test_export" (from caffe2/test/export/testing.py:229) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See https://fbur$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "testing" (from caffe2/test/export/test_export.py:87) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See https://fburl$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "test_export" (from caffe2/test/export/test_serdes.py:9) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See https://fb$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "testing" (from caffe2/test/export/test_serdes.py:10) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See https://fburl$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "testing" (from caffe2/test/export/test_retraceability.py:7) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See https:$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "test_export" (from caffe2/test/export/test_retraceability.py:6) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See ht$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "testing" (from caffe2/test/export/test_export_nonstrict.py:7) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See http$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "test_export" (from caffe2/test/export/test_export_nonstrict.py:6) when processing rule "test_export". Please make sure it's listed in the srcs parameter of another rule. See $ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "test_export" (from caffe2/test/export/test_export_training_ir_to_run_decomp.py:8) when processing rule "test_export". Please make sure it's listed in the srcs parameter of an$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "testing" (from caffe2/test/export/test_export_training_ir_to_run_decomp.py:10) when processing rule "test_export". Please make sure it's listed in the srcs parameter of anoth$ ERROR while processing caffe2/test/TARGETS: Found "//python/typeshed_internal:typeshed_internal_library" owner for "cv2" but it is protected by visibility rules: [] (from caffe2/test/test_bundled_images.py:7) when processing rule "test_bundled_$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "caffe2.test.profiler_test_cpp_thread_lib" (from caffe2/test/profiler/test_cpp_thread.py:29) when processing rule "profiler_test_cpp_thread". Please make sure it's listed in t$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "torch._utils_internal.get_file_path_2" (from caffe2/test/test_custom_ops.py:23) when processing rule "custom_ops". Please make sure it's listed in the srcs parameter of anoth$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "torch._utils_internal.get_file_path_2" (from caffe2/test/test_public_bindings.py:13) when processing rule "public_bindings". Please make sure it's listed in the srcs paramete$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "torch._C._profiler.symbolize_tracebacks" (from caffe2/test/test_cuda.py:3348) when processing rule "test_cuda". Please make sure it's listed in the srcs parameter of another $ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for "torch._C._profiler.gather_traceback" (from caffe2/test/test_cuda.py:3348) when processing rule "test_cuda". Please make sure it's listed in the srcs parameter of another rule$ ERROR while processing caffe2/test/TARGETS: Cannot find an owner for include <torch/csrc/autograd/profiler_kineto.h> (from caffe2/test/profiler/test_cpp_thread.cpp:2) when processing profiler_test_cpp_thread_lib. Some things to try: ``` Differential Revision: D62049222 Pull Request resolved: https://github.com/pytorch/pytorch/pull/135614 Approved by: https://github.com/oulgen, https://github.com/laithsakka
95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
# Owner(s): ["oncall: mobile"]
|
|
# mypy: allow-untyped-defs
|
|
|
|
import io
|
|
|
|
import cv2 # @manual
|
|
|
|
import torch
|
|
import torch.utils.bundled_inputs
|
|
from torch.testing._internal.common_utils import TestCase
|
|
|
|
|
|
torch.ops.load_library("//caffe2/torch/fb/operators:decode_bundled_image")
|
|
|
|
|
|
def model_size(sm):
|
|
buffer = io.BytesIO()
|
|
torch.jit.save(sm, buffer)
|
|
return len(buffer.getvalue())
|
|
|
|
|
|
def save_and_load(sm):
|
|
buffer = io.BytesIO()
|
|
torch.jit.save(sm, buffer)
|
|
buffer.seek(0)
|
|
return torch.jit.load(buffer)
|
|
|
|
|
|
"""Return an InflatableArg that contains a tensor of the compressed image and the way to decode it
|
|
|
|
keyword arguments:
|
|
img_tensor -- the raw image tensor in HWC or NCHW with pixel value of type unsigned int
|
|
if in NCHW format, N should be 1
|
|
quality -- the quality needed to compress the image
|
|
"""
|
|
|
|
|
|
def bundle_jpeg_image(img_tensor, quality):
|
|
# turn NCHW to HWC
|
|
if img_tensor.dim() == 4:
|
|
assert img_tensor.size(0) == 1
|
|
img_tensor = img_tensor[0].permute(1, 2, 0)
|
|
pixels = img_tensor.numpy()
|
|
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), quality]
|
|
_, enc_img = cv2.imencode(".JPEG", pixels, encode_param)
|
|
enc_img_tensor = torch.from_numpy(enc_img)
|
|
enc_img_tensor = torch.flatten(enc_img_tensor).byte()
|
|
obj = torch.utils.bundled_inputs.InflatableArg(
|
|
enc_img_tensor, "torch.ops.fb.decode_bundled_image({})"
|
|
)
|
|
return obj
|
|
|
|
|
|
def get_tensor_from_raw_BGR(im) -> torch.Tensor:
|
|
raw_data = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
|
raw_data = torch.from_numpy(raw_data).float()
|
|
raw_data = raw_data.permute(2, 0, 1)
|
|
raw_data = torch.div(raw_data, 255).unsqueeze(0)
|
|
return raw_data
|
|
|
|
|
|
class TestBundledImages(TestCase):
|
|
def test_single_tensors(self):
|
|
class SingleTensorModel(torch.nn.Module):
|
|
def forward(self, arg):
|
|
return arg
|
|
|
|
im = cv2.imread("caffe2/test/test_img/p1.jpg")
|
|
tensor = torch.from_numpy(im)
|
|
inflatable_arg = bundle_jpeg_image(tensor, 90)
|
|
input = [(inflatable_arg,)]
|
|
sm = torch.jit.script(SingleTensorModel())
|
|
torch.utils.bundled_inputs.augment_model_with_bundled_inputs(sm, input)
|
|
loaded = save_and_load(sm)
|
|
inflated = loaded.get_all_bundled_inputs()
|
|
decoded_data = inflated[0][0]
|
|
|
|
# raw image
|
|
raw_data = get_tensor_from_raw_BGR(im)
|
|
|
|
self.assertEqual(len(inflated), 1)
|
|
self.assertEqual(len(inflated[0]), 1)
|
|
self.assertEqual(raw_data.shape, decoded_data.shape)
|
|
self.assertEqual(raw_data, decoded_data, atol=0.1, rtol=1e-01)
|
|
|
|
# Check if fb::image_decode_to_NCHW works as expected
|
|
with open("caffe2/test/test_img/p1.jpg", "rb") as fp:
|
|
weight = torch.full((3,), 1.0 / 255.0).diag()
|
|
bias = torch.zeros(3)
|
|
byte_tensor = torch.tensor(list(fp.read())).byte()
|
|
im2_tensor = torch.ops.fb.image_decode_to_NCHW(byte_tensor, weight, bias)
|
|
self.assertEqual(raw_data.shape, im2_tensor.shape)
|
|
self.assertEqual(raw_data, im2_tensor, atol=0.1, rtol=1e-01)
|