Skip to content

Commit ae9d1ce

Browse files
authored
Merge pull request #2137 from ArkciaDragone/pytorch-fix
Update and fix PyTorch docs
2 parents 27f5226 + d9e4d51 commit ae9d1ce

2 files changed

Lines changed: 92 additions & 36 deletions

File tree

lib/docs/filters/pytorch/entries.rb

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,115 @@
11
module Docs
22
class Pytorch
33
class EntriesFilter < Docs::EntriesFilter
4-
NAME_REPLACEMENTS = {
5-
"Distributed communication package - torch.distributed" => "torch.distributed"
4+
TYPE_REPLACEMENTS = {
5+
"torch.Tensor" => "Tensor",
6+
"torch.nn" => "Neuro Network",
7+
"Probability distributions - torch.distributions" => "Probability Distributions",
8+
"torch" => "Torch",
9+
"Quantization" => "Quantization",
10+
"torch.optim" => "Optimization",
11+
"torch.Storage" => "Storage",
12+
"torch.nn.functional" => "NN Functions",
13+
"torch.cuda" => "CUDA",
14+
"Torch Distributed Elastic" => "Distributed Elastic",
15+
"torch.fx" => "FX",
16+
"TorchScript" => "Torch Script",
17+
"torch.onnx" => "ONNX",
18+
"Distributed communication package - torch.distributed" => "Distributed Communication",
19+
"Automatic differentiation package - torch.autograd" => "Automatic Differentiation",
20+
"torch.linalg" => "Linear Algebra",
21+
"Distributed Checkpoint - torch.distributed.checkpoint" => "Distributed Checkpoint",
22+
"Distributed RPC Framework" => "Distributed RPC",
23+
"torch.special" => "SciPy-like Special",
24+
"torch.package" => "Package",
25+
"torch.backends" => "Backends",
26+
"FullyShardedDataParallel" => "Fully Sharded Data Parallel",
27+
"torch.sparse" => "Sparse Tensors",
28+
"torch.export" => "Traced Graph Export",
29+
"torch.fft" => "Discrete Fourier Transforms",
30+
"torch.utils.data" => "Datasets and Data Loaders",
31+
"torch.monitor" => "Monitor",
32+
"Automatic Mixed Precision package - torch.amp" => "Automatic Mixed Precision",
33+
"torch.utils.tensorboard" => "Tensorboard",
34+
"torch.profiler" => "Profiler",
35+
"torch.mps" => "MPS",
36+
"DDP Communication Hooks" => "DDP Communication Hooks",
37+
"Benchmark Utils - torch.utils.benchmark" => "Benchmark Utils",
38+
"torch.nn.init" => "Parameter Initializations",
39+
"Tensor Parallelism - torch.distributed.tensor.parallel" => "Tensor Parallelism",
40+
"torch.func" => "JAX-like Function Transforms",
41+
"Distributed Optimizers" => "Distributed Optimizers",
42+
"torch.signal" => "SciPy-like Signal",
43+
"torch.futures" => "Miscellaneous",
44+
"torch.utils.cpp_extension" => "Miscellaneous",
45+
"torch.overrides" => "Miscellaneous",
46+
"Generic Join Context Manager" => "Miscellaneous",
47+
"torch.hub" => "Miscellaneous",
48+
"torch.cpu" => "Miscellaneous",
49+
"torch.random" => "Miscellaneous",
50+
"torch.compiler" => "Miscellaneous",
51+
"Pipeline Parallelism" => "Miscellaneous",
52+
"Named Tensors" => "Miscellaneous",
53+
"Multiprocessing package - torch.multiprocessing" => "Miscellaneous",
54+
"torch.utils" => "Miscellaneous",
55+
"torch.library" => "Miscellaneous",
56+
"Tensor Attributes" => "Miscellaneous",
57+
"torch.testing" => "Miscellaneous",
58+
"torch.nested" => "Miscellaneous",
59+
"Understanding CUDA Memory Usage" => "Miscellaneous",
60+
"torch.utils.dlpack" => "Miscellaneous",
61+
"torch.utils.checkpoint" => "Miscellaneous",
62+
"torch.__config__" => "Miscellaneous",
63+
"Type Info" => "Miscellaneous",
64+
"torch.utils.model_zoo" => "Miscellaneous",
65+
"torch.utils.mobile_optimizer" => "Miscellaneous",
66+
"torch._logging" => "Miscellaneous",
67+
"torch.masked" => "Miscellaneous",
68+
"torch.utils.bottleneck" => "Miscellaneous"
669
}
770

8-
def get_breadcrumbs()
9-
css('.pytorch-breadcrumbs > li').map { |node| node.content.delete_suffix(' >') }
71+
def get_breadcrumbs
72+
css('.pytorch-breadcrumbs > li').map {
73+
|node| node.content.delete_suffix(' >').strip
74+
}.reject { |item| item.nil? || item.empty? }
1075
end
1176

1277
def get_name
13-
# The id of the container `div.section` indicates the page type.
14-
# If the id starts with `module-`, then it's an API reference,
15-
# otherwise it is a note or design doc.
16-
section_id = at_css('.section[id], section[id]')['id']
17-
if section_id.starts_with? 'module-'
18-
section_id.remove('module-')
19-
else
20-
name = get_breadcrumbs()[1]
21-
NAME_REPLACEMENTS.fetch(name, name)
22-
end
78+
b = get_breadcrumbs
79+
b[(b[1] == 'torch' ? 2 : 1)..].join('.')
2380
end
2481

2582
def get_type
26-
name
83+
t = get_breadcrumbs[1]
84+
TYPE_REPLACEMENTS.fetch(t, t)
2785
end
2886

2987
def include_default_entry?
30-
# Only include API references, and ignore notes or design docs
31-
!subpath.start_with? 'generated/' and type.start_with? 'torch'
88+
# Only include API entries to simplify and unify the list
89+
return name.start_with?('torch.')
3290
end
3391

3492
def additional_entries
3593
return [] if root_page?
3694

3795
entries = []
38-
39-
css('dt').each do |node|
40-
name = node['id']
41-
if name == self.name or name == nil
96+
css('dl').each do |node|
97+
dt = node.at_css('dt')
98+
if dt == nil
99+
next
100+
end
101+
id = dt['id']
102+
if id == name or id == nil
42103
next
43104
end
44105

45-
case node.parent['class']
46-
when 'method', 'function'
47-
if node.at_css('code').content.starts_with? 'property '
48-
# this instance method is a property, so treat it as an attribute
49-
entries << [name, node['id']]
50-
else
51-
entries << [name + '()', node['id']]
52-
end
53-
when 'class', 'attribute'
54-
entries << [name, node['id']]
106+
case node['class']
107+
when 'py method', 'py function'
108+
entries << [id + '()', id]
109+
when 'py class', 'py attribute', 'py property'
110+
entries << [id, id]
111+
when 'footnote brackets', 'field-list simple'
112+
next
55113
end
56114
end
57115

lib/docs/scrapers/pytorch.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ class Pytorch < UrlScraper
33
self.name = 'PyTorch'
44
self.slug = 'pytorch'
55
self.type = 'sphinx'
6-
self.force_gzip = true
76
self.links = {
87
home: 'https://pytorch.org/',
98
code: 'https://github.com/pytorch/pytorch'
109
}
1110

1211
html_filters.push 'pytorch/entries', 'pytorch/clean_html', 'sphinx/clean_html'
1312

14-
options[:skip] = ['cpp_index.html', 'packages.html', 'py-modindex.html', 'genindex.html']
13+
options[:skip] = ['cpp_index.html', 'deploy.html', 'packages.html', 'py-modindex.html', 'genindex.html']
1514
options[:skip_patterns] = [/\Acommunity/, /\A_modules/, /\Anotes/, /\Aorg\/pytorch\//]
16-
options[:max_image_size] = 256_000
1715

1816
options[:attribution] = <<-HTML
19-
&copy; 2019-2024 Torch Contributors<br>
20-
Licensed under the 3-clause BSD License.
17+
&copy; 2024, PyTorch Contributors<br>
18+
PyTorch has a BSD-style license, as found in the <a href="https://github.com/pytorch/pytorch/blob/main/LICENSE">LICENSE</a> file.
2119
HTML
2220

2321
version '2' do

0 commit comments

Comments
 (0)