Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / distributed / encoder_parallel / config.py: 86%
36 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"""User configuration for the visual encoder parallel runtime."""
17from __future__ import annotations
19from dataclasses import dataclass, fields
20from typing import Any, Mapping
23@dataclass(frozen=True)
24class EncoderParallelConfig:
25 """Configuration of a derived Encoder-DP/Encoder-CP domain.
27 ``packed_attention`` exchanges the concatenated bucket sequence and calls
28 image-isolated TND attention. ``projector_mode`` selects leader projection
29 after a full gather, or spatially sharded projection with direct input dispatch.
30 Defaults retain per-image attention, leader projection, and patch-square
31 work assignment. ``overlap`` splits Q/K/V projection and overlaps input
32 communication in forward and backward; it requires packed attention.
33 ``balance_cost`` optionally includes the model's linear projection/MLP work.
34 """
36 enabled: bool = False
37 encoder_cp_size: int = 1
38 packed_attention: bool = False
39 projector_mode: str = "leader"
40 overlap: bool = False
41 balance_cost: str = "attention"
43 def __post_init__(self) -> None:
44 if not isinstance(self.enabled, bool):
45 raise ValueError("enabled must be a boolean")
46 if not isinstance(self.packed_attention, bool):
47 raise ValueError("packed_attention must be a boolean")
48 if not isinstance(self.overlap, bool):
49 raise ValueError("overlap must be a boolean")
50 if self.overlap and not self.packed_attention:
51 raise ValueError("encoder_parallel.overlap requires packed_attention=True")
52 if self.projector_mode not in ("leader", "sharded_dispatch"):
53 raise ValueError("projector_mode must be leader or sharded_dispatch")
54 if self.balance_cost not in ("attention", "encoder_flops"):
55 raise ValueError("balance_cost must be attention or encoder_flops")
56 if (isinstance(self.encoder_cp_size, bool) or not isinstance(self.encoder_cp_size, int)
57 or self.encoder_cp_size < 1):
58 raise ValueError(
59 "accelerator.encoder_parallel.encoder_cp_size "
60 f"must be a positive integer, got {self.encoder_cp_size!r}"
61 )
63 @classmethod
64 def from_mapping(cls, raw: Mapping[str, Any] | None) -> "EncoderParallelConfig":
65 """Parse a mapping while rejecting unknown configuration keys.
67 Args:
68 raw: User's optional encoder-parallel configuration mapping.
69 """
70 if raw is None:
71 return cls()
72 if not isinstance(raw, Mapping):
73 raise ValueError("encoder_parallel must be a mapping")
74 unknown = set(raw) - {field.name for field in fields(cls)}
75 if unknown:
76 raise ValueError(f"unknown encoder_parallel keys: {sorted(unknown)}")
77 return cls(**raw)