Skip to content

Add some missing docs to torch.rst, new unittest to enforce torch.rst no longer miss anything#16039

Closed
zasdfgbnm wants to merge 9 commits intopytorch:masterfrom
zasdfgbnm:enforce-doc-coverage
Closed

Add some missing docs to torch.rst, new unittest to enforce torch.rst no longer miss anything#16039
zasdfgbnm wants to merge 9 commits intopytorch:masterfrom
zasdfgbnm:enforce-doc-coverage

Conversation

@zasdfgbnm
Copy link
Copy Markdown
Collaborator

@zasdfgbnm zasdfgbnm commented Jan 15, 2019

This prevent people (reviewer, PR author) from forgetting adding things to torch.rst.

When something new is added to _torch_doc.py or functional.py but intentionally not in torch.rst, people should manually whitelist it in test_docs_coverage.py.

@zasdfgbnm
Copy link
Copy Markdown
Collaborator Author

Use this rather than #15980 . cc: @ezyang

@zasdfgbnm zasdfgbnm changed the title Add some missing docs to torch.rst, net unittest to enforce torch.rst no longer missing anything Add some missing docs to torch.rst, new unittest to enforce torch.rst no longer missing anything Jan 15, 2019
@zasdfgbnm zasdfgbnm changed the title Add some missing docs to torch.rst, new unittest to enforce torch.rst no longer missing anything Add some missing docs to torch.rst, new unittest to enforce torch.rst no longer miss anything Jan 15, 2019
@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Jan 17, 2019

Hi @zasdfgbnm, I was getting ready to merge your patch, but I realized there might be a better and more way to pull out the list of documented identifiers. Basically, instead of groveling through _torch_docs.py to find docs, instead we should just iterate over all identifiers in torch module, and count those with non-empty docstrings as being documented. What do you think? Should save us a lot of code here, and will also work for functions which are directly defined in the file rather than through _add_docstr

@zasdfgbnm
Copy link
Copy Markdown
Collaborator Author

zasdfgbnm commented Jan 17, 2019

@ezyang I just did some experiment, but I still think including only _tensor_docs.py and torch.functional is a better choice.

import torch
import ast
import _ast
import re
import matplotlib.pyplot as plt
from matplotlib_venn import venn3

# copied from PR
r1 = re.compile(r'\.\. autofunction:: (\w*)')
everything = set()
filename = '/home/gaoxiang/pytorch/docs/source/torch.rst'
with open(filename, 'r') as f:
    lines = f.readlines()
    for l in lines:
        l = l.strip()
        name = r1.findall(l)
        if name:
            everything.add(name[0])


pypath = '/home/gaoxiang/pytorch/torch/_torch_docs.py'
everything2 = set()
with open(pypath, 'r') as f:
    body = ast.parse(f.read()).body
    for i in body:
        if not isinstance(i, _ast.Expr):
            continue
        i = i.value
        if not isinstance(i, _ast.Call):
            continue
        if i.func.id != 'add_docstr':
            continue
        i = i.args[0]
        if i.value.id != 'torch':
            continue
        i = i.attr
        everything2.add(i)
    for p in dir(torch.functional):
        if not p.startswith('_') and p[0].islower():
            everything2.add(p)

# all attr in torch with nonempty docstring
alltorch = set(a for a in dir(torch) if getattr(torch, a).__doc__ and not a.startswith('_'))

fig = plt.figure(figsize=(20, 20))
v = venn3(
    [alltorch, everything, everything2],
    set_labels = ('torch.*', 'torch.rst', '_torch_docs and functional'),
    set_colors=('r', 'g', 'b'),
    alpha=0.8)
plt.show()

print(len(alltorch))
print(len(everything))
print(len(everything2))
print()

print(alltorch - everything2)

The Venn diagram is below, read is torch.*, green is torch.rst and blue is _torch_docs and functional. green and blue has better match.
figure_1

The print is:

244
208
199

{'threshold_', 'pdist', 'fork', 'os', 'rrelu_', 'has_lapack', 'optim', 'conv1d', 'enable_grad', 'potrs', 'register_batch_operator', 'set_grad_enabled', 'set_default_dtype', 'has_mkl', 'cosine_similarity', 'has_cudnn', 'name', 'relu_', 'adaptive_avg_pool1d', 'conv_transpose2d', 'cuda', 'celu_', 'to_batch_graph', 'testing', 'load', 'conv3d', 'save', 'selu_', 'wait', 'set_rng_state', 'set_default_tensor_type', 'merge_type_from_type_comment', 'multiprocessing', 'conv_transpose1d', 'pixel_shuffle', 'is_storage', 'sys', 'platform', 'conv_tbc', 'is_tensor', 'autograd', 'initial_seed', 'distributions', 'compiled_with_cxx11_abi', 'conv_transpose3d', 'avg_pool1d', 'set_printoptions', 'no_grad', 'import_ir_module_from_buffer', 'import_ir_module', 'manual_seed', 'conv2d', 'parse_type_comment', 'get_rng_state'}

we can see things in torch but not in _torch_docs and functional are mainly internal functions, things documented somewhere else, imported modules and those ops that should be in nn.functional rather than torch.

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Feb 10, 2019

I guess the question is what the motivation of this test is.

  1. If the motivation is to ensure everything that is explicitly documented in _torch_docs.py is also mentioned in the rst file, this patch as stands is great!
  2. If the motivation is to ensure everything in torch is documented, the alternate mechanism is better.

Still, doing (1) seems like a good step on the way to (2), so we can take this as is.

@zasdfgbnm
Copy link
Copy Markdown
Collaborator Author

@ezyang I just updated the whitelist to add a new symbol attonate introduced in master.

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ezyang is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@zasdfgbnm zasdfgbnm deleted the enforce-doc-coverage branch February 15, 2019 15:19
@zasdfgbnm
Copy link
Copy Markdown
Collaborator Author

@ezyang #16057 is the followup of this testing tensor methods.

facebook-github-bot pushed a commit that referenced this pull request Mar 27, 2019
… to enforce tensors.rst no longer miss anything (#16057)

Summary:
This depend on #16039

This prevent people (reviewer, PR author) from forgetting adding things to `tensors.rst`.

When something new is added to `_tensor_doc.py` or `tensor.py` but intentionally not in `tensors.rst`, people should manually whitelist it in `test_docs_coverage.py`.
Pull Request resolved: #16057

Differential Revision: D14619550

Pulled By: ezyang

fbshipit-source-id: e1c6dd6761142e2e48ec499e118df399e3949fcc
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
… no longer miss anything (pytorch#16039)

Summary:
This prevent people (reviewer, PR author) from forgetting adding things to `torch.rst`.

When something new is added to `_torch_doc.py` or `functional.py` but intentionally not in `torch.rst`, people should manually whitelist it in `test_docs_coverage.py`.
Pull Request resolved: pytorch#16039

Differential Revision: D14070903

Pulled By: ezyang

fbshipit-source-id: 60f2a42eb5efe81be073ed64e54525d143eb643e
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
… to enforce tensors.rst no longer miss anything (pytorch#16057)

Summary:
This depend on pytorch#16039

This prevent people (reviewer, PR author) from forgetting adding things to `tensors.rst`.

When something new is added to `_tensor_doc.py` or `tensor.py` but intentionally not in `tensors.rst`, people should manually whitelist it in `test_docs_coverage.py`.
Pull Request resolved: pytorch#16057

Differential Revision: D14619550

Pulled By: ezyang

fbshipit-source-id: e1c6dd6761142e2e48ec499e118df399e3949fcc
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.

3 participants