mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:34:18 +08:00
Summary: I've written custom parsers and emitters for everything from docstrings to classes and functions. However, I recently came across an issue when I was parsing/generating from the TensorFlow codebase: inconsistent use of `Args:` and `Arguments:` in its docstrings. ```sh (pytorch#c348fae)$ for name in 'Args:' 'Arguments:'; do printf '%-10s %04d\n' "$name" "$(rg -IFtpy --count-matches "$name" | paste -s -d+ -- | bc)"; done Args: 1095 Arguments: 0336 ``` It is easy enough to extend my parsers to support both variants, however it looks like `Arguments:` is wrong anyway, as per: - https://google.github.io/styleguide/pyguide.html#doc-function-args @ [`ddccc0f`](https://github.com/google/styleguide/blob/ddccc0f/pyguide.md) - https://chromium.googlesource.com/chromiumos/docs/+/master/styleguide/python.md#describing-arguments-in-docstrings @ [`9fc0fc0`](https://chromium.googlesource.com/chromiumos/docs/+/9fc0fc0/styleguide/python.md) - https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html @ [`c0ae8e3`](https://github.com/sphinx-contrib/napoleon/blob/c0ae8e3/docs/source/example_google.rst) Therefore, only `Args:` is valid. This PR replaces them throughout the codebase. PS: For related PRs, see tensorflow/tensorflow/pull/45420 PPS: The trackbacks automatically appearing below are sending the same changes to other repositories in the [PyTorch](https://github.com/pytorch) organisation. Pull Request resolved: https://github.com/pytorch/pytorch/pull/49736 Reviewed By: albanD Differential Revision: D25710534 Pulled By: soumith fbshipit-source-id: 61e8ff01abb433e9f78185c2d1d0cbd7c22c1619
50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
import types
|
|
import torch._C
|
|
|
|
class _ClassNamespace(types.ModuleType):
|
|
def __init__(self, name):
|
|
super(_ClassNamespace, self).__init__('torch.classes' + name)
|
|
self.name = name
|
|
|
|
def __getattr__(self, attr):
|
|
proxy = torch._C._get_custom_class_python_wrapper(self.name, attr)
|
|
if proxy is None:
|
|
raise RuntimeError(f'Class {self.name}.{attr} not registered!')
|
|
return proxy
|
|
|
|
class _Classes(types.ModuleType):
|
|
def __init__(self):
|
|
super(_Classes, self).__init__('torch.classes')
|
|
|
|
def __getattr__(self, name):
|
|
namespace = _ClassNamespace(name)
|
|
setattr(self, name, namespace)
|
|
return namespace
|
|
|
|
@property
|
|
def loaded_libraries(self):
|
|
return torch.ops.loaded_libraries
|
|
|
|
def load_library(self, path):
|
|
"""
|
|
Loads a shared library from the given path into the current process.
|
|
|
|
The library being loaded may run global initialization code to register
|
|
custom classes with the PyTorch JIT runtime. This allows dynamically
|
|
loading custom classes. For this, you should compile your class
|
|
and the static registration code into a shared library object, and then
|
|
call ``torch.classes.load_library('path/to/libcustom.so')`` to load the
|
|
shared object.
|
|
|
|
After the library is loaded, it is added to the
|
|
``torch.classes.loaded_libraries`` attribute, a set that may be inspected
|
|
for the paths of all libraries loaded using this function.
|
|
|
|
Args:
|
|
path (str): A path to a shared library to load.
|
|
"""
|
|
torch.ops.load_library(path)
|
|
|
|
# The classes "namespace"
|
|
classes = _Classes()
|