Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / auto_parallel / config_adapter / _normalized_config.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-07-06 05:41 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-07-06 05:41 +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"""Shared type definitions for auto parallel strategy search configuration."""
17from dataclasses import dataclass, field
18from typing import Any, Dict, List, Optional, Literal
21@dataclass
22class NormalizedConfig:
23 """Aggregate container for a parallel strategy search task.
25 Holds all configuration sections as plain dicts for maximum
26 compatibility with PR631's Config class (sapp_nd.nd.common.config).
27 The dict keys
28 follow PR631's HyperParallel TOML naming conventions.
30 **Required model_spec fields**: ``n_layers``, ``dim``, ``n_heads``,
31 ``vocab_size``.
33 **Optional model_spec fields**: ``inter_dim``, ``n_kv_heads``,
34 ``seq_len``, ``local_batch_size``, ``params_dtype``, ``compute_dtype``,
35 ``softmax_compute_dtype``, ``moe_enabled``, ``num_experts``,
36 ``num_experts_per_tok``, ``num_shared_experts``, ``moe_inter_dim``,
37 ``use_flash_attention``, ``use_clip_grad``, ``use_seq_parallel``,
38 ``vocab_emb_dp``, ``enable_parallel_optimizer``,
39 ``gradient_accumulation_shard``, ``optimizer_weight_shard_size``,
40 ``enable_weight_tying``, ``multiple_of``, ``ffn_dim_multiplier``,
41 ``mtp_depth``, ``n_dense_layers``, ``kv_lora_rank``, ``q_lora_rank``,
42 ``qk_rope_head_dim``, ``v_head_dim``, ``qk_nope_head_dim``,
43 ``capacity_factor``, ``first_k_dense_replace``, ``topk_group``,
44 ``n_group``, ``routed_scaling_factor``.
46 Args:
47 model_spec: Model architecture parameters. Must contain at least
48 ``n_layers``, ``dim``, ``n_heads``, ``vocab_size``.
49 cluster_spec: Hardware cluster description.
50 search_space: Parallel dimension candidate values, e.g.
51 ``{"dp": [1,2,4], "tp": [1,2,4,8], "pp": [1,2], "cp": [1], "ep": [1]}``.
52 constraint: User-imposed constraints (global_batch_size,
53 memory_limit_gb, fixed_*_degree).
54 estimator: Estimation algorithm parameters.
55 pp_config: Pipeline-parallel specific configuration.
56 resolved_strategy: Final resolved strategy, populated after search.
57 """
59 model_spec: Dict[str, Any] = field(default_factory=dict)
60 cluster_spec: Dict[str, Any] = field(default_factory=dict)
61 search_space: Dict[str, List[int]] = field(default_factory=dict)
62 constraint: Dict[str, Any] = field(default_factory=dict)
63 estimator: Dict[str, Any] = field(default_factory=dict)
64 pp_config: Dict[str, Any] = field(default_factory=dict)
65 resolved_strategy: Optional[Dict[str, Any]] = None
67 def to_dict(self) -> Dict[str, Any]:
68 """Serialize all config sections to a nested dictionary."""
69 result: Dict[str, Any] = {
70 "model_spec": dict(self.model_spec),
71 "cluster_spec": dict(self.cluster_spec),
72 "search_space": dict(self.search_space),
73 "constraint": dict(self.constraint),
74 "estimator": dict(self.estimator),
75 "pp_config": dict(self.pp_config),
76 }
77 if self.resolved_strategy is not None:
78 result["resolved_strategy"] = dict(self.resolved_strategy)
79 return result
82@dataclass
83class ValidationError:
84 """A single validation error or warning discovered during config validation.
86 Args:
87 field_path: Dot-separated path to the offending field
88 (e.g. ``"model_spec.dim"``).
89 message: Human-readable description of the problem.
90 severity: Error severity level (``"error"`` or ``"warning"``).
91 """
93 field_path: str
94 message: str
95 severity: Literal["error", "warning"] = "error"
98ValidationSeverity = Literal["error", "warning"]