Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / tools / dryrun / patches / registry.py: 96%
27 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"""Process-local patch registry separating trainer and model patches."""
17from collections.abc import Callable
20class _PatchState:
21 """Mutable process-wide state for dry-run patch installation."""
23 def __init__(self) -> None:
24 self.installed = False
25 self.model_patches: dict[str, Callable[[], None]] = {}
26 self.installed_model_patches: set[str] = set()
29_STATE = _PatchState()
32def register_model_patch(name: str, installer: Callable[[], None]) -> None:
33 """Register a model-specific patch for dry-run execution.
35 Model-specific modules register their patches at import time so the generic
36 dry-run package never imports concrete model implementations.
38 Args:
39 name: Unique patch identifier, e.g. ``"deepseek_v41.csa2"``.
40 installer: Zero-argument callable applying the patch idempotently.
41 """
42 if not callable(installer):
43 raise TypeError("model patch installer must be callable")
44 _STATE.model_patches[name] = installer
45 # Apply immediately if dry-run patches were already installed earlier in
46 # this process (import order is controlled by the model plugin).
47 if _STATE.installed and name not in _STATE.installed_model_patches:
48 installer()
49 _STATE.installed_model_patches.add(name)
52def install_registered_model_patches() -> None:
53 """Apply all registered model-specific patches once."""
54 for name, installer in _STATE.model_patches.items():
55 if name not in _STATE.installed_model_patches:
56 installer()
57 _STATE.installed_model_patches.add(name)
60def mark_patches_installed() -> bool:
61 """Mark dry-run patches installed and return whether this is the first call."""
62 if _STATE.installed:
63 return False
64 _STATE.installed = True
65 return True
68def is_patches_installed() -> bool:
69 """Return whether process-local dry-run patches have been installed."""
70 return _STATE.installed
73__all__ = [
74 "install_registered_model_patches",
75 "is_patches_installed",
76 "mark_patches_installed",
77 "register_model_patch",
78]