mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 13:44:15 +08:00
Summary: ## Motivation Fixes https://github.com/pytorch/pytorch/issues/43770. ## Description of the change This PR fixes exception chaining only in files under `torch/` where appropriate. To fix exception chaining, I used either: 1. `raise new_exception from old_exception` where `new_exception` itself seems not descriptive enough to debug or `old_exception` delivers valuable information. 2. `raise new_exception from None` where raising both of `new_exception` and `old_exception` seems a bit noisy and redundant. I subjectively chose which one to use from the above options. ## List of lines containing raise in except clause: I wrote [this simple script](https://gist.github.com/akihironitta/4223c1b32404b36c1b349d70c4c93b4d) using [ast](https://docs.python.org/3.8/library/ast.html#module-ast) to list lines where `raise`ing in `except` clause. - [x]000739c31a/torch/jit/annotations.py (L35)
- [x]000739c31a/torch/jit/annotations.py (L150)
- [x]000739c31a/torch/jit/annotations.py (L158)
- [x]000739c31a/torch/jit/annotations.py (L231)
- [x]000739c31a/torch/jit/_trace.py (L432)
- [x]000739c31a/torch/nn/utils/prune.py (L192)
- [x]000739c31a/torch/cuda/nvtx.py (L7)
- [x]000739c31a/torch/utils/cpp_extension.py (L1537)
- [x]000739c31a/torch/utils/tensorboard/_pytorch_graph.py (L292)
- [x]000739c31a/torch/utils/data/dataloader.py (L835)
- [x]000739c31a/torch/utils/data/dataloader.py (L849)
- [x]000739c31a/torch/utils/data/dataloader.py (L856)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L186)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L189)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L424)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L1279)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L1283)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L1356)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L1388)
- [x]000739c31a/torch/testing/_internal/common_utils.py (L1391)
- [ ]000739c31a/torch/testing/_internal/common_utils.py (L1412)
- [x]000739c31a/torch/testing/_internal/codegen/random_topo_test.py (L310)
- [x]000739c31a/torch/testing/_internal/codegen/random_topo_test.py (L329)
- [x]000739c31a/torch/testing/_internal/codegen/random_topo_test.py (L332)
- [x]000739c31a/torch/testing/_internal/jit_utils.py (L183)
- [x]000739c31a/torch/testing/_internal/common_nn.py (L4789)
- [x]000739c31a/torch/onnx/utils.py (L367)
- [x]000739c31a/torch/onnx/utils.py (L659)
- [x]000739c31a/torch/onnx/utils.py (L892)
- [x]000739c31a/torch/onnx/utils.py (L897)
- [x]000739c31a/torch/serialization.py (L108)
- [x]000739c31a/torch/serialization.py (L754)
- [x]000739c31a/torch/distributed/rpc/_testing/faulty_agent_backend_registry.py (L76)
- [x]000739c31a/torch/distributed/rpc/backend_registry.py (L260)
- [x]000739c31a/torch/distributed/distributed_c10d.py (L184)
- [x]000739c31a/torch/_utils_internal.py (L57)
- [x]000739c31a/torch/hub.py (L494)
- [x]000739c31a/torch/contrib/_tensorboard_vis.py (L16)
- [x]000739c31a/torch/distributions/lowrank_multivariate_normal.py (L100)
- [x]000739c31a/torch/distributions/constraint_registry.py (L142)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/43836 Reviewed By: ailzhang Differential Revision: D23431212 Pulled By: malfet fbshipit-source-id: 5f7f41b391164a5ad0efc06e55cd58c23408a921
70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
from __future__ import absolute_import, division, print_function, unicode_literals
|
|
|
|
import os
|
|
import inspect
|
|
import tempfile
|
|
|
|
# this arbitrary-looking assortment of functionality is provided here
|
|
# to have a central place for overrideable behavior. The motivating
|
|
# use is the FB build environment, where this source file is replaced
|
|
# by an equivalent.
|
|
|
|
if os.path.basename(os.path.dirname(__file__)) == 'shared':
|
|
torch_parent = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
|
else:
|
|
torch_parent = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
|
|
def get_file_path(*path_components):
|
|
return os.path.join(torch_parent, *path_components)
|
|
|
|
|
|
def get_file_path_2(*path_components):
|
|
return os.path.join(*path_components)
|
|
|
|
|
|
def get_writable_path(path):
|
|
if os.access(path, os.W_OK):
|
|
return path
|
|
return tempfile.mkdtemp(suffix=os.path.basename(path))
|
|
|
|
|
|
|
|
def prepare_multiprocessing_environment(path):
|
|
pass
|
|
|
|
|
|
def resolve_library_path(path):
|
|
return os.path.realpath(path)
|
|
|
|
|
|
def get_source_lines_and_file(obj, error_msg=None):
|
|
"""
|
|
Wrapper around inspect.getsourcelines and inspect.getsourcefile.
|
|
|
|
Returns: (sourcelines, file_lino, filename)
|
|
"""
|
|
filename = None # in case getsourcefile throws
|
|
try:
|
|
filename = inspect.getsourcefile(obj)
|
|
sourcelines, file_lineno = inspect.getsourcelines(obj)
|
|
except OSError as e:
|
|
msg = (f"Can't get source for {obj}. TorchScript requires source access in "
|
|
"order to carry out compilation, make sure original .py files are "
|
|
"available.")
|
|
if error_msg:
|
|
msg += '\n' + error_msg
|
|
raise OSError(msg) from e
|
|
|
|
return sourcelines, file_lineno, filename
|
|
|
|
|
|
TEST_MASTER_ADDR = '127.0.0.1'
|
|
TEST_MASTER_PORT = 29500
|
|
# USE_GLOBAL_DEPS controls whether __init__.py tries to load
|
|
# libtorch_global_deps, see Note [Global dependencies]
|
|
USE_GLOBAL_DEPS = True
|
|
# USE_RTLD_GLOBAL_WITH_LIBTORCH controls whether __init__.py tries to load
|
|
# _C.so with RTLD_GLOBAL during the call to dlopen.
|
|
USE_RTLD_GLOBAL_WITH_LIBTORCH = False
|