2024-06-08 18:41:16 +00:00
|
|
|
# mypy: allow-untyped-defs
|
2024-01-28 03:30:07 +00:00
|
|
|
from typing import Optional
|
2023-01-09 14:57:21 +00:00
|
|
|
import torch
|
2024-08-19 22:10:56 +00:00
|
|
|
from torch.overrides import TorchFunctionMode, _pop_mode, _push_mode
|
2023-01-09 14:57:21 +00:00
|
|
|
from torch.utils._contextlib import context_decorator
|
2024-08-19 22:10:56 +00:00
|
|
|
from torch._C import _len_torch_function_stack
|
2023-01-09 14:57:21 +00:00
|
|
|
import functools
|
|
|
|
|
|
2024-01-28 03:30:07 +00:00
|
|
|
CURRENT_DEVICE: Optional[torch.device] = None
|
2023-04-20 12:57:00 +00:00
|
|
|
|
2023-01-09 14:57:21 +00:00
|
|
|
@functools.lru_cache(1)
|
|
|
|
|
def _device_constructors():
|
|
|
|
|
return {
|
|
|
|
|
# standard ones
|
|
|
|
|
torch.empty,
|
2023-02-21 14:13:06 +00:00
|
|
|
torch.empty_permuted,
|
2023-01-09 14:57:21 +00:00
|
|
|
torch.empty_strided,
|
|
|
|
|
torch.empty_quantized,
|
|
|
|
|
torch.ones,
|
|
|
|
|
torch.arange,
|
|
|
|
|
torch.bartlett_window,
|
|
|
|
|
torch.blackman_window,
|
|
|
|
|
torch.eye,
|
|
|
|
|
torch.fft.fftfreq,
|
|
|
|
|
torch.fft.rfftfreq,
|
|
|
|
|
torch.full,
|
|
|
|
|
torch.fill,
|
|
|
|
|
torch.hamming_window,
|
|
|
|
|
torch.hann_window,
|
|
|
|
|
torch.kaiser_window,
|
|
|
|
|
torch.linspace,
|
|
|
|
|
torch.logspace,
|
|
|
|
|
torch.nested.nested_tensor,
|
2025-01-14 17:41:58 +00:00
|
|
|
# This function doesn't actually take a device argument
|
|
|
|
|
# torch.normal,
|
2023-01-09 14:57:21 +00:00
|
|
|
torch.ones,
|
|
|
|
|
torch.rand,
|
|
|
|
|
torch.randn,
|
|
|
|
|
torch.randint,
|
|
|
|
|
torch.randperm,
|
|
|
|
|
torch.range,
|
|
|
|
|
torch.sparse_coo_tensor,
|
|
|
|
|
torch.sparse_compressed_tensor,
|
|
|
|
|
torch.sparse_csr_tensor,
|
|
|
|
|
torch.sparse_csc_tensor,
|
|
|
|
|
torch.sparse_bsr_tensor,
|
|
|
|
|
torch.sparse_bsc_tensor,
|
|
|
|
|
torch.tril_indices,
|
|
|
|
|
torch.triu_indices,
|
|
|
|
|
torch.vander,
|
|
|
|
|
torch.zeros,
|
|
|
|
|
torch.asarray,
|
|
|
|
|
# weird ones
|
|
|
|
|
torch.tensor,
|
|
|
|
|
torch.as_tensor,
|
|
|
|
|
torch.scalar_tensor,
|
2023-08-09 21:23:40 +00:00
|
|
|
torch.asarray,
|
2023-01-09 14:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# NB: This is directly called from C++ in torch/csrc/Device.cpp
|
|
|
|
|
class DeviceContext(TorchFunctionMode):
|
|
|
|
|
def __init__(self, device):
|
|
|
|
|
self.device = torch.device(device)
|
|
|
|
|
|
2023-04-20 12:57:00 +00:00
|
|
|
def __enter__(self):
|
|
|
|
|
global CURRENT_DEVICE
|
|
|
|
|
self.old_device = CURRENT_DEVICE
|
|
|
|
|
CURRENT_DEVICE = self.device
|
2024-08-19 22:10:56 +00:00
|
|
|
# We need to put the device at the bottom of the stack
|
|
|
|
|
# If we set default device within a function mode context
|
|
|
|
|
# exiting that context mode will pop the device function mode off
|
|
|
|
|
# of the stack incorrectly
|
2024-11-20 17:52:07 +00:00
|
|
|
cur_stack = [_pop_mode() for _ in range(_len_torch_function_stack())]
|
2024-08-19 22:10:56 +00:00
|
|
|
|
|
|
|
|
_push_mode(self)
|
|
|
|
|
|
|
|
|
|
for mode in reversed(cur_stack):
|
|
|
|
|
_push_mode(mode)
|
|
|
|
|
|
2023-04-20 12:57:00 +00:00
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
|
global CURRENT_DEVICE
|
|
|
|
|
CURRENT_DEVICE = self.old_device
|
2024-08-19 22:10:56 +00:00
|
|
|
cur_stack = []
|
|
|
|
|
# Invariant: there should only be one DeviceContext on the stack at any time
|
|
|
|
|
# (At the bottom), pop all mdoes until we hit the bottom, assert it's a DeviceContext
|
|
|
|
|
# or else someone else has popped it!
|
|
|
|
|
for _ in range(_len_torch_function_stack() - 1):
|
|
|
|
|
mode = _pop_mode()
|
|
|
|
|
assert not isinstance(mode, DeviceContext)
|
|
|
|
|
cur_stack.append(mode)
|
|
|
|
|
|
|
|
|
|
if _len_torch_function_stack() > 0:
|
|
|
|
|
mode = _pop_mode()
|
|
|
|
|
assert isinstance(mode, DeviceContext)
|
|
|
|
|
|
|
|
|
|
for mode in reversed(cur_stack):
|
|
|
|
|
_push_mode(mode)
|
2023-04-20 12:57:00 +00:00
|
|
|
|
2023-01-09 14:57:21 +00:00
|
|
|
def __torch_function__(self, func, types, args=(), kwargs=None):
|
|
|
|
|
kwargs = kwargs or {}
|
|
|
|
|
if func in _device_constructors() and kwargs.get('device') is None:
|
|
|
|
|
kwargs['device'] = self.device
|
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
# NB: This is directly called from C++ in torch/csrc/Device.cpp
|
|
|
|
|
def device_decorator(device, func):
|
|
|
|
|
return context_decorator(lambda: device, func)
|
|
|
|
|
|
|
|
|
|
def set_device(device):
|
|
|
|
|
"""
|
Resolve docstring errors in throughput_benchmark.py, weak.py, _traceback.py, file_baton.py, _contextlib.py, _device.py, cpp_backtrace.py, bundled_inputs.py, run_cpu.py, hooks.py, mobile_optimizer.py, _freeze.py, __init__.py, mkldnn.py, dlpack.py (#113311)
Fixes #112633
Fixed errors relating to pydocstyle in the following files. The remaining errors are not covered in this issue. `torch/utils/dlpack.py` was not modified as the errors are relating to the function signature in the first line in the docstring which must be maintained as is for proper Sphinx interpretation.
```python
def from_dlpack(ext_tensor: Any) -> 'torch.Tensor':
"""from_dlpack(ext_tensor) -> Tensor
.....
"""
```
pydocstyle torch/utils/_contextlib.py --count
before: 4
after: 0
pydocstyle torch/backends/mps/__init__.py --count
before: 8
after: 1
**remaining errors**
```
torch/backends/mps/__init__.py:1 at module level:
D104: Missing docstring in public package
```
pydocstyle torch/backends/xeon/run_cpu.py --count
before: 13
after: 1
**remaining errors**
```
torch/backends/xeon/run_cpu.py:864 in public function `main`:
D103: Missing docstring in public function
```
pydocstyle torch/backends/cpu/__init__.py --count
before: 2
after: 1
**remaining errors**
```
torch/backends/cpu/__init__.py:1 at module level:
D104: Missing docstring in public package
```
pydocstyle torch/utils/cpp_backtrace.py --count
before: 4
after: 1
**remaining errors**
```
torch/utils/cpp_backtrace.py:1 at module level:
D100: Missing docstring in public module
```
pydocstyle torch/utils/bundled_inputs.py --count
before: 8
after: 1
**remaining errors**
```
torch/utils/bundled_inputs.py:1 at module level:
D100: Missing docstring in public module
```
pydocstyle torch/utils/file_baton.py --count
before: 8
after: 1
**remaining errors**
```
torch/utils/file_baton.py:1 at module level:
D100: Missing docstring in public module
```
pydocstyle torch/utils/mobile_optimizer.py --count
before: 6
after: 1
**remaining errors**
```
torch/utils/mobile_optimizer.py:8 in public class `LintCode`:
D101: Missing docstring in public class
```
pydocstyle torch/backends/opt_einsum/__init__.py --count
before: 7
after: 5
**remaining errors**
```
torch/backends/opt_einsum/__init__.py:1 at module level:
D104: Missing docstring in public package
torch/backends/opt_einsum/__init__.py:67 in public function `set_flags`:
D103: Missing docstring in public function
torch/backends/opt_einsum/__init__.py:77 in public function `flags`:
D103: Missing docstring in public function
torch/backends/opt_einsum/__init__.py:93 in public class `OptEinsumModule`:
D101: Missing docstring in public class
torch/backends/opt_einsum/__init__.py:94 in public method `__init__`:
D107: Missing docstring in __init__
```
pydocstyle torch/utils/_device.py --count
before: 9
after: 6
**remaining errors**
```
torch/utils/_device.py:58 in public class `DeviceContext`:
D101: Missing docstring in public class
torch/utils/_device.py:59 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/_device.py:62 in public method `__enter__`:
D105: Missing docstring in magic method
torch/utils/_device.py:68 in public method `__exit__`:
D105: Missing docstring in magic method
torch/utils/_device.py:73 in public method `__torch_function__`:
D105: Missing docstring in magic method
torch/utils/_device.py:80 in public function `device_decorator`:
D103: Missing docstring in public function
```
pydocstyle torch/utils/_freeze.py --count
before: 15
after: 7
**remaining errors**
```
torch/utils/_freeze.py:77 in public function `indent_msg`:
D103: Missing docstring in public function
torch/utils/_freeze.py:89 in public class `FrozenModule`:
D101: Missing docstring in public class
torch/utils/_freeze.py:100 in public class `Freezer`:
D101: Missing docstring in public class
torch/utils/_freeze.py:101 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/_freeze.py:106 in public method `msg`:
D102: Missing docstring in public method
torch/utils/_freeze.py:185 in public method `get_module_qualname`:
D102: Missing docstring in public method
torch/utils/_freeze.py:206 in public method `compile_string`:
D102: Missing docstring in public method
```
pydocstyle torch/utils/throughput_benchmark.py --count
before: 25
after: 8
**remaining errors**
```
torch/utils/throughput_benchmark.py:1 at module level:
D100: Missing docstring in public module
torch/utils/throughput_benchmark.py:27 in public class `ExecutionStats`:
D101: Missing docstring in public class
torch/utils/throughput_benchmark.py:28 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/throughput_benchmark.py:33 in public method `latency_avg_ms`:
D102: Missing docstring in public method
torch/utils/throughput_benchmark.py:37 in public method `num_iters`:
D102: Missing docstring in public method
torch/utils/throughput_benchmark.py:46 in public method `total_time_seconds`:
D102: Missing docstring in public method
torch/utils/throughput_benchmark.py:50 in public method `__str__`:
D105: Missing docstring in magic method
torch/utils/throughput_benchmark.py:94 in public method `__init__`:
D107: Missing docstring in __init__
```
pydocstyle torch/utils/hooks.py --count
before: 14
after: 11
**remaining errors**
```
torch/utils/hooks.py:1 at module level:
D100: Missing docstring in public module
torch/utils/hooks.py:23 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/hooks.py:34 in public method `remove`:
D102: Missing docstring in public method
torch/utils/hooks.py:44 in public method `__getstate__`:
D105: Missing docstring in magic method
torch/utils/hooks.py:50 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/hooks.py:64 in public method `__enter__`:
D105: Missing docstring in magic method
torch/utils/hooks.py:67 in public method `__exit__`:
D105: Missing docstring in magic method
torch/utils/hooks.py:82 in public function `warn_if_has_hooks`:
D103: Missing docstring in public function
torch/utils/hooks.py:103 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/hooks.py:188 in public method `setup_input_hook`:
D102: Missing docstring in public method
torch/utils/hooks.py:197 in public method `setup_output_hook`:
D102: Missing docstring in public method
```
pydocstyle torch/utils/_traceback.py --count
before: 19
after: 14
**remaining errors**
```
torch/utils/_traceback.py:47 in public function `report_compile_source_on_error`:
D103: Missing docstring in public function
torch/utils/_traceback.py:160 in public class `CapturedTraceback`:
D101: Missing docstring in public class
torch/utils/_traceback.py:163 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/_traceback.py:167 in public method `cleanup`:
D102: Missing docstring in public method
torch/utils/_traceback.py:170 in public method `summary`:
D102: Missing docstring in public method
torch/utils/_traceback.py:182 in public method `__getstate__`:
D105: Missing docstring in magic method
torch/utils/_traceback.py:190 in public method `extract`:
D205: 1 blank line required between summary line and description (found 0)
torch/utils/_traceback.py:190 in public method `extract`:
D400: First line should end with a period (not 't')
torch/utils/_traceback.py:213 in public method `format`:
D205: 1 blank line required between summary line and description (found 0)
torch/utils/_traceback.py:213 in public method `format`:
D400: First line should end with a period (not 'f')
torch/utils/_traceback.py:213 in public method `format`:
D401: First line should be in imperative mood (perhaps 'Format', not 'Formats')
torch/utils/_traceback.py:224 in public method `format_all`:
D200: One-line docstring should fit on one line with quotes (found 3)
torch/utils/_traceback.py:247 in private function `_extract_symbolized_tb`:
D205: 1 blank line required between summary line and description (found 0)
torch/utils/_traceback.py:247 in private function `_extract_symbolized_tb`:
D400: First line should end with a period (not 'f')
```
pydocstyle torch/utils/mkldnn.py --count
before: 28
after: 26
**remaining errors**
```
torch/utils/mkldnn.py:1 at module level:
D100: Missing docstring in public module
torch/utils/mkldnn.py:4 in public class `MkldnnLinear`:
D101: Missing docstring in public class
torch/utils/mkldnn.py:5 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/mkldnn.py:19 in public method `__getstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:23 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:29 in public method `forward`:
D102: Missing docstring in public method
torch/utils/mkldnn.py:75 in public class `MkldnnConv1d`:
D101: Missing docstring in public class
torch/utils/mkldnn.py:76 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/mkldnn.py:82 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:88 in public class `MkldnnConv2d`:
D101: Missing docstring in public class
torch/utils/mkldnn.py:89 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/mkldnn.py:100 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:110 in public class `MkldnnConv3d`:
D101: Missing docstring in public class
torch/utils/mkldnn.py:111 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/mkldnn.py:122 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:133 in public class `MkldnnBatchNorm`:
D101: Missing docstring in public class
torch/utils/mkldnn.py:136 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/mkldnn.py:155 in public method `__getstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:163 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:171 in public method `forward`:
D102: Missing docstring in public method
torch/utils/mkldnn.py:184 in public class `MkldnnPrelu`:
D101: Missing docstring in public class
torch/utils/mkldnn.py:185 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/mkldnn.py:190 in public method `__getstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:194 in public method `__setstate__`:
D105: Missing docstring in magic method
torch/utils/mkldnn.py:199 in public method `forward`:
D102: Missing docstring in public method
torch/utils/mkldnn.py:205 in public function `to_mkldnn`:
D103: Missing docstring in public function
```
pydocstyle torch/utils/weak.py --count
before: 32
after: 30
**remaining errors**
```
torch/utils/weak.py:1 at module level:
D100: Missing docstring in public module
torch/utils/weak.py:42 in public class `WeakIdRef`:
D101: Missing docstring in public class
torch/utils/weak.py:45 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/weak.py:54 in public method `__call__`:
D102: Missing docstring in public method
torch/utils/weak.py:61 in public method `__hash__`:
D105: Missing docstring in magic method
torch/utils/weak.py:64 in public method `__eq__`:
D105: Missing docstring in magic method
torch/utils/weak.py:84 in public class `WeakIdKeyDictionary`:
D101: Missing docstring in public class
torch/utils/weak.py:87 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/weak.py:131 in public method `__delitem__`:
D105: Missing docstring in magic method
torch/utils/weak.py:135 in public method `__getitem__`:
D105: Missing docstring in magic method
torch/utils/weak.py:138 in public method `__len__`:
D105: Missing docstring in magic method
torch/utils/weak.py:145 in public method `__repr__`:
D105: Missing docstring in magic method
torch/utils/weak.py:148 in public method `__setitem__`:
D105: Missing docstring in magic method
torch/utils/weak.py:151 in public method `copy`:
D102: Missing docstring in public method
torch/utils/weak.py:162 in public method `__deepcopy__`:
D105: Missing docstring in magic method
torch/utils/weak.py:172 in public method `get`:
D102: Missing docstring in public method
torch/utils/weak.py:175 in public method `__contains__`:
D105: Missing docstring in magic method
torch/utils/weak.py:182 in public method `items`:
D102: Missing docstring in public method
torch/utils/weak.py:189 in public method `keys`:
D102: Missing docstring in public method
torch/utils/weak.py:198 in public method `values`:
D102: Missing docstring in public method
torch/utils/weak.py:216 in public method `popitem`:
D102: Missing docstring in public method
torch/utils/weak.py:224 in public method `pop`:
D102: Missing docstring in public method
torch/utils/weak.py:228 in public method `setdefault`:
D102: Missing docstring in public method
torch/utils/weak.py:231 in public method `update`:
D102: Missing docstring in public method
torch/utils/weak.py:241 in public method `__ior__`:
D105: Missing docstring in magic method
torch/utils/weak.py:245 in public method `__or__`:
D105: Missing docstring in magic method
torch/utils/weak.py:252 in public method `__ror__`:
D105: Missing docstring in magic method
torch/utils/weak.py:262 in public method `__eq__`:
D105: Missing docstring in magic method
torch/utils/weak.py:276 in public method `__init__`:
D107: Missing docstring in __init__
torch/utils/weak.py:280 in public method `__call__`:
D102: Missing docstring in public method
```
@mikaylagawarecki @jbschlosser @svekars
Pull Request resolved: https://github.com/pytorch/pytorch/pull/113311
Approved by: https://github.com/ezyang
2023-11-15 17:40:04 +00:00
|
|
|
Set the default device inside of the wrapped function by decorating it with this function.
|
|
|
|
|
|
|
|
|
|
If you would like to use this as a context manager, use device as a
|
|
|
|
|
context manager directly, e.g., ``with torch.device(device)``.
|
2023-01-09 14:57:21 +00:00
|
|
|
"""
|
|
|
|
|
return lambda func: device_decorator(torch.device(device), func)
|