Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / codegen / cli.py: 96%
45 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-19 05:29 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-19 05:29 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Thin command-line interface for native-build codegen workflows."""
17from __future__ import annotations
19import argparse
20import logging
21import os
22import sys
23from typing import Optional
25from hyper_parallel.codegen.build import generate_from_native_build
26from hyper_parallel.trainer.config import parser as config_parser
28logger = logging.getLogger(__name__)
31def main(argv: Optional[list[str]] = None) -> int:
32 """Run the CLI and return its process exit code.
34 Args:
35 argv: Optional command-line arguments. Defaults to ``sys.argv``.
37 Returns:
38 Zero on success and a nonzero process exit code on failure.
39 """
40 parser = _build_parser()
41 args, config_overrides = parser.parse_known_args(argv)
42 setattr(args, "_config_overrides", config_overrides)
43 if not getattr(args, "command", None):
44 parser.print_usage(sys.stderr)
45 return 2
46 _configure_logging(getattr(args, "verbose", False))
47 try:
48 return args.func(args)
49 except Exception as exc: # A CLI reports failures without a traceback by default.
50 logger.warning("codegen: %s", exc, exc_info=getattr(args, "verbose", False))
51 return 1
54def cmd_generate(args: argparse.Namespace) -> int:
55 """Observe a meta model and generate a fresh bundle.
57 Args:
58 args: Parsed codegen generation arguments.
60 Returns:
61 Zero after the bundle is generated successfully.
62 """
63 if int(os.environ.get("WORLD_SIZE", "1")) > 1:
64 raise RuntimeError(
65 "codegen generate is a single-process CPU/meta command; "
66 "run it with python instead of torchrun"
67 )
68 config = _load_config(args.config, _cli_overrides(args))
69 layout = generate_from_native_build(config, artifact_dir=args.output)
70 logger.info("codegen: artifact %s", layout.artifact_dir)
71 return 0
74def _load_config(yaml_path: str, overrides: Optional[list[str]] = None):
75 """Load a resolved TrainerConfig without building trainer runtime components."""
76 return config_parser.load_training_config(yaml_path, overrides or [])
79def _cli_overrides(args: argparse.Namespace) -> list[str]:
80 """Return typed config overrides left after argparse processing."""
81 return list(getattr(args, "_config_overrides", None) or [])
84def _configure_logging(verbose: bool) -> None:
85 logging.basicConfig(
86 level=logging.INFO if verbose else logging.WARNING,
87 format="%(levelname)s:%(name)s:%(message)s",
88 )
91def _build_parser() -> argparse.ArgumentParser:
92 """Build the codegen command-line parser."""
93 parser = argparse.ArgumentParser(
94 prog="hyper_parallel.codegen",
95 description="Generate a model bundle from a training YAML.",
96 )
97 parser.add_argument("--verbose", action="store_true", help="verbose logging")
98 subparsers = parser.add_subparsers(dest="command")
100 generate = subparsers.add_parser(
101 "generate",
102 help="observe the model on meta and generate its bundle",
103 )
104 generate.add_argument("--config", required=True, help="path to the training YAML")
105 generate.add_argument("--output", help="explicit artifact output directory")
106 generate.set_defaults(func=cmd_generate)
108 return parser
111__all__ = ["cmd_generate", "main"]