Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / codegen / build.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +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"""Device-free orchestration for snapshot-driven code generation."""
17from typing import Any, Optional
19from hyper_parallel.codegen.artifact import ArtifactLayout
20from hyper_parallel.codegen.model_build_overlay import use_codegen_model_build_overlay
21from hyper_parallel.codegen.planner_overlay import use_codegen_planner_overlay
22from hyper_parallel.codegen.session import CodegenBuildSession
23from hyper_parallel.distributed.mesh import (
24 DistributedSetup,
25 LogicalDeviceMesh,
26 MeshContext,
27 ParallelTopologySpec,
28 infer_minimum_world_size,
29 resolve_parallel_layout,
30)
31from hyper_parallel.trainer.config import normalize_distributed_setup_overrides
34def generate_from_native_build(
35 config: Any,
36 *,
37 artifact_dir: Optional[str] = None,
38) -> ArtifactLayout:
39 """Observe the configured model on meta and publish a fresh bundle.
41 Args:
42 config: Resolved TrainerConfig. Only model and planning settings are
43 consumed; data, optimizer, scheduler, and training loops are never built.
44 artifact_dir: Optional explicit bundle output directory.
46 Returns:
47 The freshly generated artifact layout.
49 Raises:
50 RuntimeError: If the native model does not publish a complete artifact.
51 """
52 return _build_and_publish(config, artifact_dir)
55def _build_and_publish(config: Any, artifact_dir: Optional[str]) -> ArtifactLayout:
56 """Build and publish from a process-group-free planning topology."""
57 distributed_setup = create_offline_distributed_setup(config)
58 normalize_distributed_setup_overrides(distributed_setup, config)
60 session = CodegenBuildSession()
61 with use_codegen_model_build_overlay(session, artifact_dir, defer_publish=True) as result, \
62 use_codegen_planner_overlay():
63 result.model = config.model.build(
64 distributed_setup=distributed_setup,
65 peft_config=config.peft,
66 activation_checkpoint=config.activation_checkpoint,
67 compile_config=config.compile,
68 model_init_dtype=config.model_init_dtype,
69 )
71 if result.artifact_layout is None:
72 failure = getattr(result.snapshot, "failure", None)
73 if failure is not None:
74 raise RuntimeError(
75 f"codegen {failure.stage} failed: {failure.reason}"
76 )
77 raise RuntimeError("native model build completed without publishing a codegen artifact")
78 return result.artifact_layout
81def create_offline_distributed_setup(config: Any) -> DistributedSetup:
82 """Project YAML parallel settings into a process-group-free setup.
84 Args:
85 config: Trainer configuration describing compute and FSDP parallel degrees.
87 Returns:
88 A setup with named logical dense and expert meshes, without communication groups.
89 """
90 topology_spec = ParallelTopologySpec.from_config(config)
91 layout = resolve_parallel_layout(
92 topology_spec,
93 infer_minimum_world_size(topology_spec),
94 )
95 topology = LogicalDeviceMesh(
96 mesh_dim_names=("dp", "cp", "tp"),
97 mesh_shape=(layout.dp_size, topology_spec.cp_size, topology_spec.tp_size),
98 )
99 mesh_context = MeshContext.from_layout(layout)
100 mesh_context.device_mesh = topology
101 if topology_spec.ep_size > 1:
102 mesh_context.fsdp_moe_mesh = LogicalDeviceMesh(
103 mesh_dim_names=("edp", "ep"),
104 mesh_shape=(topology.size() // topology_spec.ep_size, topology_spec.ep_size),
105 )
106 return DistributedSetup.from_config(
107 config,
108 mesh_context=mesh_context,
109 strategy_enabled=True,
110 )
113__all__ = ["create_offline_distributed_setup", "generate_from_native_build"]