@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 3d925165f2
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 17, 03:00 UTC

Add example scripts to documentation (#4225)

Co-authored-by: Harry Mellor <hmellor@oxts.com>

Harry Mellor committed Apr 22, 2024 at 17:36 UTC 3d925165f2b18379640a63fbb42de95440d63b64
6 files changed +80 -1
.gitignore
+2
@@ -70,6 +70,8 @@ instance/
70
71 # Sphinx documentation
72 docs/_build/
73 +docs/source/getting_started/examples/*.rst
74 +!**/*.template.rst
75
76 # PyBuilder
77 .pybuilder/
docs/source/conf.py
+8 -1
@@ -48,7 +48,7 @@ templates_path = ['_templates']
48 # List of patterns, relative to source directory, that match files and
49 # directories to ignore when looking for source files.
50 # This pattern also affects html_static_path and html_extra_path.
51 -exclude_patterns: List[str] = []
51 +exclude_patterns: List[str] = ["**/*.template.rst"]
52
53 # Exclude the prompt "$" when copying code
54 copybutton_prompt_text = r"\$ "
@@ -73,6 +73,13 @@ html_theme_options = {
73 # so a file named "default.css" will overwrite the builtin "default.css".
74 # html_static_path = ['_static']
75
76 +
77 +# Generate additional rst documentation here.
78 +def setup(app):
79 + from docs.source.generate_examples import generate_examples
80 + generate_examples()
81 +
82 +
83 # Mock out external dependencies here.
84 autodoc_mock_imports = [
85 "cpuinfo",
docs/source/generate_examples.py new
+61
@@ -0,0 +1,61 @@
1 +import re
2 +from pathlib import Path
3 +
4 +
5 +def fix_case(text: str) -> str:
6 + subs = [
7 + ("api", "API"),
8 + ("llm", "LLM"),
9 + ("vllm", "vLLM"),
10 + ("openai", "OpenAI"),
11 + ("multilora", "MultiLoRA"),
12 + ]
13 + for sub in subs:
14 + text = re.sub(*sub, text, flags=re.IGNORECASE)
15 + return text
16 +
17 +
18 +def underline(title: str, character: str = "=") -> str:
19 + return f"{title}\n{character * len(title)}"
20 +
21 +
22 +def generate_title(filename: str) -> str:
23 + # Turn filename into a title
24 + title = filename.replace("_", " ").title()
25 + # Handle acronyms and names
26 + title = fix_case(title)
27 + # Underline title
28 + title = underline(title)
29 + return title
30 +
31 +
32 +def generate_examples():
33 + root_dir = Path(__file__).parent.parent.parent.resolve()
34 +
35 + # Source paths
36 + script_dir = root_dir / "examples"
37 + script_paths = sorted(script_dir.glob("*.py"))
38 +
39 + # Destination paths
40 + doc_dir = root_dir / "docs/source/getting_started/examples"
41 + doc_paths = [doc_dir / f"{path.stem}.rst" for path in script_paths]
42 +
43 + # Generate the example docs for each example script
44 + for script_path, doc_path in zip(script_paths, doc_paths):
45 + script_url = f"https://github.com/vllm-project/vllm/blob/main/examples/{script_path.name}"
46 + # Make script_path relative to doc_path and call it include_path
47 + include_path = '../../../..' / script_path.relative_to(root_dir)
48 + content = (f"{generate_title(doc_path.stem)}\n\n"
49 + f"Source {script_url}.\n\n"
50 + f".. literalinclude:: {include_path}\n"
51 + " :language: python\n"
52 + " :linenos:\n")
53 + with open(doc_path, "w+") as f:
54 + f.write(content)
55 +
56 + # Generate the toctree for the example scripts
57 + with open(doc_dir / "examples_index.template.rst") as f:
58 + examples_index = f.read()
59 + with open(doc_dir / "examples_index.rst", "w+") as f:
60 + example_docs = "\n ".join(path.stem for path in script_paths)
61 + f.write(examples_index.replace(r"%EXAMPLE_DOCS%", example_docs))
docs/source/getting_started/examples/examples_index.template.rst new
+8
@@ -0,0 +1,8 @@
1 +Examples
2 +=================================
3 +
4 +.. toctree::
5 + :maxdepth: 1
6 + :caption: Scripts
7 +
8 + %EXAMPLE_DOCS%
docs/source/index.rst
+1
@@ -65,6 +65,7 @@ Documentation
65 getting_started/neuron-installation
66 getting_started/cpu-installation
67 getting_started/quickstart
68 + getting_started/examples/examples_index
69
70 .. toctree::
71 :maxdepth: 1
examples/openai_chat_completion_client.py renamed