Fix exception chaining in `torch/` (#43836)
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] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L35
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L150
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L158
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L231
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/_trace.py#L432
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/nn/utils/prune.py#L192
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/cuda/nvtx.py#L7
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/cpp_extension.py#L1537
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/tensorboard/_pytorch_graph.py#L292
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/data/dataloader.py#L835
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/data/dataloader.py#L849
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/data/dataloader.py#L856
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L186
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L189
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L424
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1279
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1283
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1356
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1388
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1391
- [ ] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1412
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/codegen/random_topo_test.py#L310
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/codegen/random_topo_test.py#L329
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/codegen/random_topo_test.py#L332
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/jit_utils.py#L183
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_nn.py#L4789
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L367
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L659
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L892
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L897
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/serialization.py#L108
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/serialization.py#L754
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributed/rpc/_testing/faulty_agent_backend_registry.py#L76
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributed/rpc/backend_registry.py#L260
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributed/distributed_c10d.py#L184
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/_utils_internal.py#L57
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/hub.py#L494
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/contrib/_tensorboard_vis.py#L16
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributions/lowrank_multivariate_normal.py#L100
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/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