Skip to content

use decorator to log API usage #4976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

use decorator to log API usage #4976

wants to merge 1 commit into from

Conversation

kazhang
Copy link
Contributor

@kazhang kazhang commented Nov 22, 2021

fix #4957

@facebook-github-bot
Copy link

facebook-github-bot commented Nov 22, 2021

💊 CI failures summary and remediations

As of commit 4a004a9 (more details on the Dr. CI page):


  • 8/8 failures introduced in this PR

🕵️ 5 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages:

See CircleCI build cmake_linux_gpu (1/5)

Step: "Build torchvision C++ distribution and test" (full log | diagnosis details | 🔁 rerun)

/home/circleci/project/torchvision/csrc/ops/cud...e "error_unused" was declared but never referenced
/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(86): warning: parameter "spatial_scale" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(163): warning: variable "error_unused" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(183): warning: variable "error_unused" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(141): warning: variable "device_guard" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(226): warning: variable "error_unused" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(256): warning: variable "error_unused" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(210): warning: variable "device_guard" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(85): warning: parameter "num_rois" was declared but never referenced



/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.cu(86): warning: parameter "spatial_scale" was declared but never referenced



creating build/lib.linux-x86_64-3.8

creating build/lib.linux-x86_64-3.8/torchvision

g++ -pthread -shared -B /home/circleci/project/env/compiler_compat -L/home/circleci/project/env/lib -Wl,-rpath=/home/circleci/project/env/lib -Wl,--no-as-needed -Wl,--sysroot=/ /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autocast/deform_conv2d_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autocast/nms_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autocast/ps_roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autocast/ps_roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autocast/roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autocast/roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autograd/deform_conv2d_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autograd/ps_roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autograd/ps_roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autograd/roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/autograd/roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/deform_conv2d_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/interpolate_aa_kernels.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/nms_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/ps_roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/ps_roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cpu/roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/deform_conv2d_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/interpolate_aa_kernels.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/nms_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/ps_roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/ps_roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/roi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/cuda/roi_pool_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/deform_conv2d.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/interpolate_aa.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/nms.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/ps_roi_align.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/ps_roi_pool.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/quantized/cpu/qnms_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/quantized/cpu/qroi_align_kernel.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/roi_align.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/ops/roi_pool.o /home/circleci/project/build/temp.linux-x86_64-3.8/home/circleci/project/torchvision/csrc/vision.o -L/home/circleci/project/env/lib/python3.8/site-packages/torch/lib -L/usr/local/cuda-11.3/lib64 -lc10 -ltorch -ltorch_cpu -ltorch_python -lcudart -lc10_cuda -ltorch_cuda_cu -ltorch_cuda_cpp -o build/lib.linux-x86_64-3.8/torchvision/_C.so

See CircleCI build cmake_windows_cpu (2/5)

Step: "set -ex
source packaging/windows/internal/vc_install_helper.sh
packaging/build_cmake.sh
" (full log | diagnosis details | 🔁 rerun)

warnings.warn(f'Error checking compiler version for {compiler}: {error}')
Installing torchrun.exe script to C:\tools\miniconda3\Scripts

Using c:\tools\miniconda3\lib\site-packages
Searching for typing-extensions==3.10.0.2
Best match: typing-extensions 3.10.0.2
Adding typing-extensions 3.10.0.2 to easy-install.pth file

Using c:\tools\miniconda3\lib\site-packages
Finished processing dependencies for torchvision==0.12.0.dev20211122
C:\tools\miniconda3\lib\site-packages\torch\utils\cpp_extension.py:317: UserWarning: Error checking compiler version for cl: [WinError 2] The system cannot find the file specified
  warnings.warn(f'Error checking compiler version for {compiler}: {error}')
+ pushd test/tracing/frcnn
~/project/test/tracing/frcnn ~/project
+ mkdir build
+ python trace_model.py
Downloading: "https://download.pytorch.org/models/resnet50-0676ba61.pth" to C:\Users\circleci/.cache\torch\hub\checkpoints\resnet50-0676ba61.pth

  0%|          | 0.00/97.8M [00:00<?, ?B/s]
  1%|1         | 1.41M/97.8M [00:00<00:07, 13.8MB/s]
 17%|#6        | 16.4M/97.8M [00:00<00:00, 89.5MB/s]
 50%|#####     | 49.0M/97.8M [00:00<00:00, 190MB/s] 
 84%|########3 | 82.0M/97.8M [00:00<00:00, 237MB/s]
100%|##########| 97.8M/97.8M [00:00<00:00, 211MB/s]
Traceback (most recent call last):
  File "C:\tools\miniconda3\lib\site-packages\torch\jit\_recursive.py", line 152, in infer_type
    ann_to_type = torch.jit.annotations.ann_to_type(class_annotations[name], fake_range())
  File "C:\tools\miniconda3\lib\site-packages\torch\jit\annotations.py", line 392, in ann_to_type

See CircleCI build unittest_linux_cpu_py3.7 (3/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

/root/project/torchvision/io/video.py:399: Runt...log: [mov,mp4,m4a,3gp,3g2,mj2] moov atom not found

test/test_internet.py::TestDatasetUtils::test_download_url_dont_exist
  /root/project/env/lib/python3.7/email/feedparser.py:164: ResourceWarning: unclosed <ssl.SSLSocket [closed] fd=13, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
    self._parse = self._parsegen().__next__

test/test_internet.py::TestDatasetUtils::test_download_url_dont_exist
  /root/project/env/lib/python3.7/email/feedparser.py:164: ResourceWarning: unclosed <ssl.SSLSocket [closed] fd=14, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
    self._parse = self._parsegen().__next__

test/test_io.py::TestVideo::test_read_video_timestamps_corrupted_file
  /root/project/torchvision/io/video.py:399: RuntimeWarning: Failed to open container for /tmp/tmp2ig211b8.mp4; Caught error: [Errno 1094995529] Invalid data found when processing input: '/tmp/tmp2ig211b8.mp4'; last error log: [mov,mp4,m4a,3gp,3g2,mj2] moov atom not found
    warnings.warn(msg, RuntimeWarning)

test/test_models.py::test_memory_efficient_densenet[densenet121]
test/test_models.py::test_memory_efficient_densenet[densenet169]
test/test_models.py::test_memory_efficient_densenet[densenet201]
test/test_models.py::test_memory_efficient_densenet[densenet161]
  /root/project/env/lib/python3.7/site-packages/torch/autocast_mode.py:162: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling
    warnings.warn('User provided device_type of \'cuda\', but CUDA is not available. Disabling')

test/test_models.py::test_inception_v3_eval

See CircleCI build unittest_linux_gpu_py3.8 (4/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

test/test_utils.py::test_draw_keypoints_errors �[33mSKIPPED�[0m (We're in a C...)�[31m [ 99%]�[0m
test/test_utils.py::test_draw_segmentation_masks[1-blue] �[33mSKIPPED�[0m (We...)�[31m [ 99%]�[0m

test/test_utils.py::test_draw_segmentation_masks[1-#FF00FF] �[33mSKIPPED�[0m�[31m      [ 99%]�[0m

test/test_utils.py::test_draw_segmentation_masks[1-colors3] �[33mSKIPPED�[0m�[31m      [ 99%]�[0m

test/test_utils.py::test_draw_segmentation_masks[1-colors4] �[33mSKIPPED�[0m�[31m      [ 99%]�[0m

test/test_utils.py::test_draw_segmentation_masks[1-colors5] �[33mSKIPPED�[0m�[31m      [ 99%]�[0m

test/test_utils.py::test_draw_segmentation_masks_errors �[33mSKIPPED�[0m (We'...)�[31m [ 99%]�[0m

test/test_utils.py::test_draw_keypoints_vanilla �[33mSKIPPED�[0m (We're in a ...)�[31m [ 99%]�[0m

test/test_utils.py::test_draw_keypoints_colored[red] �[33mSKIPPED�[0m (We're ...)�[31m [ 99%]�[0m

test/test_utils.py::test_draw_keypoints_colored[#FF00FF] �[33mSKIPPED�[0m (We...)�[31m [ 99%]�[0m

test/test_utils.py::test_draw_keypoints_colored[colors2] �[33mSKIPPED�[0m (We...)�[31m [ 99%]�[0m

test/test_utils.py::test_draw_keypoints_errors �[33mSKIPPED�[0m (We're in a C...)�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_stress_test_read_video_from_file �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_read_single_stream_only �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_rescale_min_dimension �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_rescale_max_dimension �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_rescale_both_min_max_dimension �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_rescale_width �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_rescale_height �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_rescale_width_and_height �[33mSKIPPED�[0m�[31m [ 99%]�[0m

test/test_video_reader.py::TestVideoReader::test_read_video_from_file_audio_resampling �[33mSKIPPED�[0m�[31m [ 99%]�[0m

See CircleCI build unittest_linux_cpu_py3.9 (5/5)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun)

/root/project/torchvision/io/video.py:399: Runt...log: [mov,mp4,m4a,3gp,3g2,mj2] moov atom not found

test/test_internet.py::TestDatasetUtils::test_download_url_dont_exist
  /root/project/env/lib/python3.9/os.py:676: ResourceWarning: unclosed <ssl.SSLSocket [closed] fd=12, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
    value = self._data[self.encodekey(key)]

test/test_internet.py::TestDatasetUtils::test_download_url_dont_exist
  /root/project/env/lib/python3.9/os.py:676: ResourceWarning: unclosed <ssl.SSLSocket [closed] fd=13, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
    value = self._data[self.encodekey(key)]

test/test_io.py::TestVideo::test_read_video_timestamps_corrupted_file
  /root/project/torchvision/io/video.py:399: RuntimeWarning: Failed to open container for /tmp/tmpw_gntfmd.mp4; Caught error: [Errno 1094995529] Invalid data found when processing input: '/tmp/tmpw_gntfmd.mp4'; last error log: [mov,mp4,m4a,3gp,3g2,mj2] moov atom not found
    warnings.warn(msg, RuntimeWarning)

test/test_models.py::test_memory_efficient_densenet[densenet121]
test/test_models.py::test_memory_efficient_densenet[densenet169]
test/test_models.py::test_memory_efficient_densenet[densenet201]
test/test_models.py::test_memory_efficient_densenet[densenet161]
  /root/project/env/lib/python3.9/site-packages/torch/autocast_mode.py:162: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling
    warnings.warn('User provided device_type of \'cuda\', but CUDA is not available. Disabling')

test/test_models.py::test_inception_v3_eval

3 failures not recognized by patterns:

Job Step Action
CircleCI unittest_linux_cpu_py3.8 Run tests 🔁 rerun
CircleCI cmake_macos_cpu curl -o conda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh conda.sh -b
source $HOME/miniconda3/bin/activate
conda install -yq conda-build cmake
packaging/build_cmake.sh
🔁 rerun
CircleCI cmake_linux_cpu packaging/build_cmake.sh 🔁 rerun

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@kazhang
Copy link
Contributor Author

kazhang commented Nov 23, 2021

Unfortunately, torchscript doesn't like decorator. When using the decorator on boxes functions, JIT looks for helper functions definition(e.g. _upcast) in utils.py
@fmassa @datumbox do you have any workarounds?

@datumbox
Copy link
Contributor

@kazhang I tried to reproduce the problem locally but I can't. Aka pulling your branch and running the tests doesn't cause issues to me. I'm using pytorch nightly 1.11.0.dev20211117. Have you tried it locally and fails? It could be good to investigate and eliminate that it is caused by an upstream issue that was recently introduced.

event = f.__module__
if f.__name__.endswith("__init__"):
# inside module instantiation
event += args[0].__class__.__name__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need a dot in the middle to preserve the previous naming (same below)

Suggested change
event += args[0].__class__.__name__
event += "." + args[0].__class__.__name__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I addressed in local, but haven't pushed here yet.

@kazhang
Copy link
Contributor Author

kazhang commented Nov 23, 2021

@kazhang I tried to reproduce the problem locally but I can't. Aka pulling your branch and running the tests doesn't cause issues to me. I'm using pytorch nightly 1.11.0.dev20211117. Have you tried it locally and fails? It could be good to investigate and eliminate that it is caused by an upstream issue that was recently introduced.

@datumbox yes, I tested locally with Python

from torchvision.ops.boxes import box_area,box_iou
import torch
s = torch.jit.script(box_iou)

And saw the same failure as in unit test.

@datumbox
Copy link
Contributor

datumbox commented Nov 23, 2021

I can see now why the tests passed locally. We need to pass the PYTORCH_TEST_WITH_SLOW=1 var to tests to be executed with JIT support.

I don't believe decorators will be supported by JIT. I believe what happens is the functools.wraps(f) modifies information about the method and confuses JIT. If you remove it you will see it failing with another error:

from torchvision.ops.boxes import box_area,box_iou
import torch
s = torch.jit.script(box_iou)
Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "./torch/jit/_script.py", line 1308, in script
    ast = get_jit_def(obj, obj.__name__)
  File "./torch/jit/frontend.py", line 264, in get_jit_def
    return build_def(parsed_def.ctx, fn_def, type_line, def_name, self_name=self_name, pdt_arg_types=pdt_arg_types)
  File "./torch/jit/frontend.py", line 302, in build_def
    param_list = build_param_list(ctx, py_def.args, self_name, pdt_arg_types)
  File "./torch/jit/frontend.py", line 326, in build_param_list
    raise NotSupportedError(ctx_range, _vararg_kwarg_err)
torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults:
  File "./vision/torchvision/utils.py", line 380
    def wrapper(*args, **kwargs):
                        ~~~~~~~ <--- HERE
        event = f.__module__
        if f.__name__.endswith("__init__"):

This is a known limitation for JIT, so I don't think there is any way around it.

@vadimkantorov
Copy link

Maybe JIT could provide its own version of functools.wraps? Or its own decorator-maker? Worth opening an issue about this in PyTorch core?

Various decorators like logging or forced autocasts are useful in many contexts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What's purpose of logging?
5 participants