Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / codegen / inline / meta_plan.py: 94%
35 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"""Normalize frozen meta so it matches the inline-generated source shape."""
17from __future__ import annotations
19import logging
20from copy import copy
21from typing import Any
23logger = logging.getLogger(__name__)
26def normalize_inline_meta(meta: Any) -> Any:
27 """Copy emission metadata and protect classes shared with non-boundary instances."""
28 normalized = _copy_inline_meta(meta)
29 _mark_shared_class_boundaries(normalized, normalized.boundary_classes)
30 return normalized
33def _copy_inline_meta(meta: Any) -> Any:
34 """Copy only fields normalized by source emission, preserving all other context."""
35 normalized = copy(meta)
36 normalized.param_plan = dict(getattr(meta, "param_plan", None) or {})
37 normalized.boundary_classes = dict(
38 getattr(meta, "boundary_classes", None) or {}
39 )
40 return normalized
43def _mark_shared_class_boundaries(meta: Any, full_class_map: dict[str, str]) -> None:
44 """Avoid imposing one instance's boundary on every instance of its class.
46 Keep the boundary and class mapping visible, but mark the class-level
47 rewrite as skipped. Native per-instance boundary application remains
48 Trainer-owned; the review artifact does not install runtime state.
49 """
50 param_plan = getattr(meta, "param_plan", None) or {}
51 boundary_classes = getattr(meta, "boundary_classes", None) or {}
53 non_boundary_classes: set[str] = set()
54 for fqn, entry in param_plan.items():
55 if not isinstance(entry, dict):
56 continue
57 if entry.get("is_boundary") is False:
58 cls = full_class_map.get(fqn)
59 if cls is not None:
60 non_boundary_classes.add(cls)
62 if not non_boundary_classes:
63 return
65 for fqn, class_name in list(boundary_classes.items()):
66 if class_name not in non_boundary_classes:
67 continue
68 entry = param_plan.get(fqn)
69 if isinstance(entry, dict):
70 param_plan[fqn] = {**entry, "skip_source_lowering": True}
71 logger.info(
72 "codegen: boundary %s (class %s) marked skip_source_lowering "
73 "-- the class is shared with a non-boundary instance; the "
74 "class forward is preserved for native per-instance application",
75 fqn, class_name,
76 )
79__all__ = ["normalize_inline_meta"]