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-08-04 05:18 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-08-04 05:18 +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 ``model_spec`` field names follow the HuggingFace-style
28 ``config_overrides`` convention (e.g. ``hidden_size``,
29 ``num_hidden_layers``), matching the HP ``train.yaml`` format.
31 **Required model_spec fields**: ``num_hidden_layers``, ``hidden_size``,
32 ``num_attention_heads``, ``vocab_size``.
34 **Optional model_spec fields**: ``intermediate_size``,
35 ``num_key_value_heads``, ``max_position_embeddings``,
36 ``local_batch_size``, ``compute_dtype``,
37 ``softmax_compute_type``, ``moe_enabled``, ``num_experts``,
38 ``num_experts_per_tok``, ``num_shared_experts``, ``moe_intermediate_size``,
39 ``use_flash_attention``, ``use_seq_parallel``,
40 ``optimizer_weight_shard_size``,
41 ``enable_parallel_optimizer``,
42 ``multiple_of``, ``ffn_dim_multiplier``,
43 ``mtp_depth``, ``first_k_dense_replace``, ``kv_lora_rank``,
44 ``q_lora_rank``, ``qk_rope_head_dim``, ``v_head_dim``,
45 ``capacity_factor``, ``offset``,
46 ``param_init_type``, ``recompute_slice_activation``.
48 Args:
49 model_spec: Model architecture parameters. Must contain at least
50 ``num_hidden_layers``, ``hidden_size``, ``num_attention_heads``,
51 ``vocab_size``.
52 cluster_spec: Hardware cluster description.
53 search_space: Parallel dimension candidate values, e.g.
54 ``{"dp": [1,2,4], "tp": [1,2,4,8], "pp": [1,2], "cp": [1], "ep": [1]}``.
55 constraint: User-imposed constraints (global_batch_size,
56 memory_limit_gb, fixed_*_degree).
57 estimator: Estimation algorithm parameters. Optional key:
58 ``cp_algo`` (``"colossalai_cp"`` | ``"ulysses_cp"`` | ``"hybrid_cp"``,
59 default ``"colossalai_cp"``).
60 pp_config: Pipeline-parallel specific configuration.
61 resolved_strategy: Final resolved strategy, populated after search.
62 """
64 model_spec: Dict[str, Any] = field(default_factory=dict)
65 cluster_spec: Dict[str, Any] = field(default_factory=dict)
66 search_space: Dict[str, List[int]] = field(default_factory=dict)
67 constraint: Dict[str, Any] = field(default_factory=dict)
68 estimator: Dict[str, Any] = field(default_factory=dict)
69 pp_config: Dict[str, Any] = field(default_factory=dict)
70 resolved_strategy: Optional[Dict[str, Any]] = None
72 def to_dict(self) -> Dict[str, Any]:
73 """Serialize all config sections to a nested dictionary."""
74 result: Dict[str, Any] = {
75 "model_spec": dict(self.model_spec),
76 "cluster_spec": dict(self.cluster_spec),
77 "search_space": dict(self.search_space),
78 "constraint": dict(self.constraint),
79 "estimator": dict(self.estimator),
80 "pp_config": dict(self.pp_config),
81 }
82 if self.resolved_strategy is not None:
83 result["resolved_strategy"] = dict(self.resolved_strategy)
84 return result
87@dataclass
88class ValidationError:
89 """A single validation error or warning discovered during config validation.
91 Args:
92 field_path: Dot-separated path to the offending field
93 (e.g. ``"model_spec.dim"``).
94 message: Human-readable description of the problem.
95 severity: Error severity level (``"error"`` or ``"warning"``).
96 """
98 field_path: str
99 message: str
100 severity: Literal["error", "warning"] = "error"
103ValidationSeverity = Literal["error", "warning"]