"server/vllm/docs/source/serving/distributed_serving.rst" did not exist on "12d93ad7ea7076c8de7f28c0a050484646440a47"
generate_implicitron_stubs.py 4.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
This script makes the stubs for implicitron in docs/modules.
"""

from pathlib import Path

ROOT_DIR = Path(__file__).resolve().parent.parent
DEST_DIR = Path(__file__).resolve().parent / "modules/implicitron"


def paths_to_modules(paths):
    """
    Given an iterable of paths, return equivalent list of modules.
    """
David Novotny's avatar
David Novotny committed
22
23
24
25
26
    return [
        str(i.relative_to(ROOT_DIR))[:-3].replace("/", ".")
        for i in paths
        if "__pycache__" not in str(i)
    ]
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


def create_one_file(title, description, sources, dest_file):
    with open(dest_file, "w") as f:
        print(title, file=f)
        print("=" * len(title), file=f)
        print(file=f)
        print(description, file=f)
        for source in sources:
            if source.find("._") != -1:
                # ignore internal modules including __init__.py
                continue
            print(f"\n.. automodule:: {source}", file=f)
            print("    :members:", file=f)
            print("    :undoc-members:", file=f)
            print("    :show-inheritance:", file=f)


def iterate_directory(directory_path, dest):
    """
    Create a file for each module in the given path
    """
    toc = []
    if not dest.exists():
        dest.mkdir()
    for file in directory_path.glob("*.py"):
        if file.stem.startswith("_"):
            continue
        module = paths_to_modules([file])
        create_one_file(module[0], file.stem, module, dest / f"{file.stem}.rst")
        toc.append(file.stem)

    for subdir in directory_path.iterdir():
        if not subdir.is_dir():
            continue
        if subdir.name == "fb":
            continue
David Novotny's avatar
David Novotny committed
64
65
        if subdir.name.startswith("_"):
            continue
66
67
68
        iterate_directory(subdir, dest / (subdir.name))
        toc.append(f"{subdir.name}/index")

David Novotny's avatar
David Novotny committed
69
70
71
72
73
    paths_to_modules_ = paths_to_modules([directory_path.with_suffix(".XX")])
    if len(paths_to_modules_) == 0:
        return
    title = paths_to_modules_[0]

74
75
76
77
78
79
80
81
    with open(dest / "index.rst", "w") as f:
        print(title, file=f)
        print("=" * len(title), file=f)
        print("\n.. toctree::\n", file=f)
        for item in toc:
            print(f"    {item}", file=f)


David Novotny's avatar
David Novotny committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def make_directory_index(title: str, directory_path: Path):
    index_file = directory_path / "index.rst"
    directory_rsts = sorted(directory_path.glob("*.rst"))
    subdirs = sorted([f for f in directory_path.iterdir() if f.is_dir()])
    with open(index_file, "w") as f:
        print(title, file=f)
        print("=" * len(title), file=f)
        print("\n.. toctree::\n", file=f)
        for subdir in subdirs:
            print(f"    {subdir.stem}/index.rst", file=f)
        for rst in directory_rsts:
            if rst.stem == "index":
                continue
            print(f"    {rst.stem}", file=f)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
iterate_directory(ROOT_DIR / "pytorch3d/implicitron/models", DEST_DIR / "models")

unwanted_tools = ["configurable", "depth_cleanup", "utils"]
tools_sources = sorted(ROOT_DIR.glob("pytorch3d/implicitron/tools/*.py"))
tools_modules = [
    str(i.relative_to(ROOT_DIR))[:-3].replace("/", ".")
    for i in tools_sources
    if i.stem not in unwanted_tools
]
create_one_file(
    "pytorch3d.implicitron.tools",
    "Tools for implicitron",
    tools_modules,
    DEST_DIR / "tools.rst",
)

dataset_files = sorted(ROOT_DIR.glob("pytorch3d/implicitron/dataset/*.py"))
basic_dataset = [
    "dataset_base",
    "dataset_map_provider",
    "data_loader_map_provider",
    "data_source",
    "scene_batch_sampler",
]
basic_dataset_modules = [f"pytorch3d.implicitron.dataset.{i}" for i in basic_dataset]
create_one_file(
    "pytorch3d.implicitron.dataset",
    "Basics of data for implicitron",
    basic_dataset_modules,
    DEST_DIR / "data_basics.rst",
)

specific_dataset_files = [
    i for i in dataset_files if i.stem.find("_dataset_map_provider") != -1
]
create_one_file(
    "pytorch3d.impliciton.dataset",
    "specific datasets",
    paths_to_modules(specific_dataset_files),
    DEST_DIR / "datasets.rst",
)

evaluation_files = sorted(ROOT_DIR.glob("pytorch3d/implicitron/evaluation/*.py"))
create_one_file(
    "pytorch3d.impliciton.evaluation",
    "evaluation",
    paths_to_modules(evaluation_files),
    DEST_DIR / "evaluation.rst",
)
David Novotny's avatar
David Novotny committed
147
148

make_directory_index("pytorch3d.implicitron", DEST_DIR)