render.py 7.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import argparse
import re
from pathlib import Path

import yaml
10
from jinja2 import Environment, FileSystemLoader, StrictUndefined
11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
_VALID_ARCHS = {"amd64", "arm64"}


def parse_platform(platform_str: str) -> str:
    """Normalize a --platform value to the template variable used by Jinja2.

    Accepts Docker-style values (linux/amd64, linux/arm64) or short form (amd64,
    arm64), and comma-separated lists for multi-arch (linux/amd64,linux/arm64).

    Returns one of: 'amd64', 'arm64', or 'multi'.

    Raises ValueError for unrecognized architecture values.
    """
    parts = [p.strip() for p in platform_str.split(",")]
    archs = [p.split("/")[-1] for p in parts]
    for arch in archs:
        if arch not in _VALID_ARCHS:
            raise ValueError(
                f"Unrecognized architecture '{arch}' in --platform '{platform_str}'. "
                f"Valid architectures: {', '.join(sorted(_VALID_ARCHS))}"
            )
    if len(archs) > 1:
        return "multi"
    return archs[0]

37
38
39
40
41
42
43
44
45

def parse_args():
    parser = argparse.ArgumentParser(
        description="Renders dynamo Dockerfiles from templates"
    )
    parser.add_argument(
        "--framework",
        type=str,
        default="vllm",
46
47
        choices=["dynamo", "vllm", "sglang", "trtllm"],
        help="Dockerfile framework to use",
48
    )
49
50
51
52
53

    parser.add_argument(
        "--device",
        type=str,
        default="cuda",
54
        choices=["cuda", "xpu", "cpu"],
55
56
57
        help="Dockerfile device to use",
    )

58
59
60
61
62
63
64
65
66
    parser.add_argument(
        "--target",
        type=str,
        default="runtime",
        help="Dockerfile target to use. Non-exhaustive examples: [runtime, dev, local-dev]",
    )
    parser.add_argument(
        "--platform",
        type=str,
67
68
69
70
71
72
73
74
75
        default="linux/amd64",
        help=(
            "Target platform(s), Docker-style. Examples:\n"
            "  linux/amd64            single-arch amd64 build\n"
            "  linux/arm64            single-arch arm64 build\n"
            "  linux/amd64,linux/arm64  multi-arch build; the rendered Dockerfile uses\n"
            "                         Docker BuildX TARGETARCH directly (set per platform\n"
            "                         by: docker buildx build --platform linux/amd64,linux/arm64)"
        ),
76
77
78
79
80
    )
    parser.add_argument(
        "--cuda-version",
        type=str,
        default="12.9",
81
82
        choices=["12.9", "13.0", "13.1"],
        help="CUDA version to use. [12.9 or 13.0 for vllm and sglang, 13.1 for trtllm]",
83
84
85
    )
    parser.add_argument("--make-efa", action="store_true", help="Enable AWS EFA")
    parser.add_argument(
86
        "--output-short-filename",
87
        action="store_true",
88
        help="Output filename is rendered.Dockerfile instead of <framework>-<target>-cuda<cuda_version>-<arch>-rendered.Dockerfile",
89
90
91
92
93
94
95
96
97
98
99
    )
    parser.add_argument(
        "--show-result",
        action="store_true",
        help="Prints the rendered Dockerfile to stdout.",
    )
    args = parser.parse_args()
    return args


def validate_args(args):
100
    valid_inputs = {
101
        "vllm": {
102
            "device": ["cuda", "xpu", "cpu"],
Dmitry Tokarev's avatar
Dmitry Tokarev committed
103
104
105
106
107
108
109
110
            "target": [
                "runtime",
                "dev",
                "local-dev",
                "framework",
                "wheel_builder",
                "base",
            ],
111
112
113
            "cuda_version": ["12.9", "13.0"],
        },
        "trtllm": {
114
            "device": ["cuda"],
Dmitry Tokarev's avatar
Dmitry Tokarev committed
115
116
117
118
119
120
121
122
            "target": [
                "runtime",
                "dev",
                "local-dev",
                "framework",
                "wheel_builder",
                "base",
            ],
123
124
125
            "cuda_version": ["13.1"],
        },
        "sglang": {
126
            "device": ["cuda"],
127
128
129
130
131
132
133
            "target": [
                "runtime",
                "dev",
                "local-dev",
                "wheel_builder",
                "base",
            ],
134
135
136
            "cuda_version": ["12.9", "13.0"],
        },
        "dynamo": {
137
            "device": ["cuda"],
Dmitry Tokarev's avatar
Dmitry Tokarev committed
138
139
140
141
142
            "target": [
                "runtime",
                "dev",
                "local-dev",
                "frontend",
143
                "planner",
Dmitry Tokarev's avatar
Dmitry Tokarev committed
144
145
146
                "wheel_builder",
                "base",
            ],
147
148
            "cuda_version": ["12.9", "13.0"],
        },
149
150
151
    }

    if args.framework in valid_inputs:
Dmitry Tokarev's avatar
Dmitry Tokarev committed
152
153
154
        if (
            args.target in valid_inputs[args.framework]["target"]
            and args.cuda_version in valid_inputs[args.framework]["cuda_version"]
155
            and args.device in valid_inputs[args.framework]["device"]
Dmitry Tokarev's avatar
Dmitry Tokarev committed
156
        ):
157
            return
158

159
        raise ValueError(
160
            f"Invalid input combination: [framework={args.framework},target={args.target},cuda_version={args.cuda_version},device={args.device}]"
161
        )
162
163

    raise ValueError(
164
        f"Invalid input combination: [framework={args.framework},target={args.target},cuda_version={args.cuda_version},device={args.device}]"
165
    )
166
167
168
169


def render(args, context, script_dir):
    env = Environment(
170
171
172
173
        loader=FileSystemLoader(script_dir),
        trim_blocks=False,
        lstrip_blocks=True,
        undefined=StrictUndefined,  # Raise an error if a variable in the template is not provided in the context
174
175
176
177
178
    )
    template = env.get_template("Dockerfile.template")
    rendered = template.render(
        context=context,
        framework=args.framework,
179
        device=args.device,
180
        target=args.target,
181
        platform=args.platform,  # normalized: 'amd64', 'arm64', or 'multi'
182
183
184
185
186
187
        cuda_version=args.cuda_version,
        make_efa=args.make_efa,
    )
    # Replace all instances of 3+ newlines with 2 newlines
    cleaned = re.sub(r"\n{3,}", "\n\n", rendered)

188
    if args.output_short_filename:
189
190
        filename = "rendered.Dockerfile"
    else:
191
        filename = f"{args.framework}-{args.target}-{args.device}{args.cuda_version}-{args.platform}-rendered.Dockerfile"
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

    with open(f"{script_dir}/{filename}", "w") as f:
        f.write(cleaned)

    if args.show_result:
        print("##############")
        print("# Dockerfile #")
        print("##############")
        print(cleaned)
        print("##############")

    print(f"INFO: Generated Dockerfile written to {script_dir}/{filename}")

    return


def main():
    args = parse_args()
210
211
212
    # Normalize platform to template variable ('amd64', 'arm64', or 'multi')
    # and store it back so render() and validate_args() both see the normalized form.
    args.platform = parse_platform(args.platform)
213
    validate_args(args)
214
215
216
    # Clear cuda version for non-cuda device
    if args.device != "cuda":
        args.cuda_version = ""
217
    script_dir = Path(__file__).parent
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    with open(f"{script_dir}/context.yaml", "r") as f:
        context = yaml.safe_load(f)

    render(args, context, script_dir)

    if args.target == "local-dev":
        print(
            "INFO: Remember to add --build-arg values for USER_UID and USER_GID when building a local-dev image!"
        )
        print(
            "      Recommendation: --build-arg USER_UID=$(id -u) --build-arg USER_GID=$(id -g)"
        )


if __name__ == "__main__":
    main()