Diff Coverage

Diff: origin/master...HEAD, staged and unstaged changes

Source File Diff Coverage (%) Missing Lines
hyper_parallel/codegen/__init__.py 100%  
hyper_parallel/codegen/artifact.py 13.8% 141,181,256-280,283-285,287-292,297-303,305-307,310-311,313-323,328-344,354-365,368-373,375,380-384
hyper_parallel/codegen/build.py 37.9% 52,57-58,60-62,78-81,84-85,90-91,95,99-101
hyper_parallel/codegen/cli.py 22.2% 42-44,46,52-53,58-59,65,87,89,93-95
hyper_parallel/codegen/emit/bundle.py 24.4% 97-100,107-108,110,115,140-143,147,150,154-156,166-171,174-175,180-183,187-188,196-201,209,220-221,227-229,239-242,253-254,259,265,274-275,283-287,292,299,304,312
hyper_parallel/codegen/emit/context.py 100%  
hyper_parallel/codegen/emit/modeling.py 15.2% 103,114-117,128-147,152-155,160,167-179,184-198,201-202,205-207,214,219,323-325,330,338-339,352-353,356,359,362-370,373,376-377,385,394,403-405,412-413,420-421,430-434,441-445,448-449,457-458,465,468-471,475-476,484-485,490-491,496,501,508,513-514,526-529,533-535,542-550,554-555,560-564,573,576,579,582,587-588,592-593,601-602,606-607,614-617,626-630,633-635,639,644,650,662-666,671-674,681,692,716,721
hyper_parallel/codegen/emit/parallel.py 16.7% 122-123,170,217,492-494,629,870,890
hyper_parallel/codegen/emit/patches.py 64.6% 78-81,89,102,112,117-118,120-122,124-126,129,133-139
hyper_parallel/codegen/emit/recorded.py 16.9% 56-59,61,63-65,68-70,72-73,76-78,81-84,87-88,93-95,100-107,111-115,118-121,127,139-146,151-153,158,162-163,183-197,201,206-207,211,218,226,234-237,244-246,249,253-256,261-264,266-267,269-272,275,289-291,296-299,306-308,323-324,329-337,341,351-353,356-357,362-365,370-381,391-397,400
hyper_parallel/codegen/emit/replacement.py 11.1% 27-41,44-46,49-50,59-62
hyper_parallel/codegen/emit/snapshot_patches.py 13.5% 32-33,41,46-54,62-66,82-85,88-93,103,110,115,131-135,139-141,146,163-168,172,182-190,197-203,205-215,219,226-234,236-243,247,252-255,261-265,272-279,281-283,287-291,295,300,315,320-328,333-337
hyper_parallel/codegen/emit/validation.py 23.9% 26-35,38-40,50,55-60,63-65,68-69,79,88-90,95-98,102,113
hyper_parallel/codegen/inline/__init__.py 100%  
hyper_parallel/codegen/inline/component_source.py 0.0% 17,19-20,23,27,30
hyper_parallel/codegen/inline/declaration_edits.py 27.6% 34-37,43-45,51,58-70
hyper_parallel/codegen/inline/definition_edits.py 19.7% 28-33,41,48-58,63-69,80-88,93,102-108,116-119,124-130
hyper_parallel/codegen/inline/forward_edits.py 21.4% 35-56,61,66,77-79,83-85,88-90,95-98,102,107-116,121-127
hyper_parallel/codegen/inline/framework_spec.py 20.6% 77,148-150,154-155,262-267,313,315-316,343,353,379,403,409-410,432-436,441
hyper_parallel/codegen/inline/ir.py 69.4% 137-145,149-150
hyper_parallel/codegen/inline/meta_plan.py 17.3% 58-63,68-70,73,76,96,115,118-125,138-139,141,144,148-155,176,183-190,240
hyper_parallel/codegen/inline/observations.py 25.0% 27-31,38-42,49,54-58,63-70
hyper_parallel/codegen/inline/patch_engine.py 13.6% 46-48,55-56,63-72,81,86-92,99-100,119-122,125-128,131-134,143-147,162,166,274,279-295,303-310,321-322,327,332-334
hyper_parallel/codegen/inline/pipeline.py 15.7% 65,71,82,84-86,89,94-95,102-103,116-117,133,138,144,149,153-159,162,167-168,173-177,182-183,190,195-201,209-221,226-227,231,238-239,250-253,258,263-264,270,275,279-284
hyper_parallel/codegen/inline/recognition.py 15.2% 215-228,233,238-243,252,265-267,272,276-277,282-290,295,300-304,310-320,325-331,336-342,347-354,359,368-374,380-381,386-387,397-398,403-413,426,428,437-442,444,447,449-452,461,463-465,474,476-477,483-488,497-504,509-510,515-520,525
hyper_parallel/codegen/inline/replacement_pass.py 60.0% 62,73
hyper_parallel/codegen/inline/source_plan.py 42.1% 34-35,41,46-53
hyper_parallel/codegen/inline/strategy_pass.py 16.3% 40-44,82-83,92-97,102,104-105,107-108,117-119,122-129,149-154,158-162,176
hyper_parallel/codegen/inline/targets.py 24.0% 25-34,39-47
hyper_parallel/codegen/manager.py 22.9% 65-68,72-77,82-83,88,94-99,105-111,114
hyper_parallel/codegen/model_build_overlay.py 25.2% 41-47,61,64,75-79,101-105,115-116,121-122,127,136-138,140,142-148,163-165,167,172,183,187-198,200-202,211-213,221,248-249,251,253-254,256-257,259,264,292,303,307-311,321,328,333,340-341,345,349-350,355-360,362,371-374,377-378,380
hyper_parallel/codegen/plan/boundary_forms.py 80.0% 146
hyper_parallel/codegen/plan/observation.py 23.3% 41,46-48,60-77,82-94,99-101,109-115,120-126,131-143,148
hyper_parallel/codegen/plan/placements.py 15.4% 32-44,52-54,62-68,72-91,94,99-101,106,111-116,120-124,129-132,136-138
hyper_parallel/codegen/planner_overlay.py 24.4% 34-37,42-45,53-55,59-68,72-81,87-88,98-111,117-118,120,122,124,126-129,131-134,136-137
hyper_parallel/codegen/session.py 24.6% 37-48,53,58,62-66,76-79,84,88-92,96-97,106,110-114,118-122,126-127,131-132,136-140,149,155-156,178-191,193,199-204,206,211-213,217,228-232,238,242-244
hyper_parallel/codegen/snapshot.py 57.8% 40-44,51,74,86,104-108,122-124,128,148-155,160-163,171,175,196-199
hyper_parallel/codegen/source/composition.py 20.6% 42-53,58-65,70-73,76-77,82-89,93-100,104,108-109,113,118-127,131,139,143-146,154,161-167,175-178,181,189,193-194,204-207,211-213
hyper_parallel/codegen/source/resolver.py 0.0% 360,423-426,492,496
hyper_parallel/codegen/train_sidecar.py 36.6% 48-51,56-60,85-86,88-89,99-100,105-106,111,113,119-121,136-139
hyper_parallel/distributed/_builder/forward_rewriter.py 100%  
hyper_parallel/distributed/context_parallel/wrappers.py 4.8% 430-431,433,436,445-450,452,454-455,457-459,461-463,472
hyper_parallel/distributed/mesh.py 48.7% 46,51-53,72-74,98,105-108,112-115,120-122,126-128,132,142,148,161,167,172-176,183-185,190,229-230,454
hyper_parallel/trainer/config/parallelism.py 0.0% 650,660
hyper_parallel/trainer/config/resolver.py 0.0% 690,864-865
hyper_parallel/trainer/runtime/distributed.py 0.0% 181,183-184,186,188,190
hyper_parallel/codegen/artifact.py
137
138
139
140
141
142
143
144
145
    no aliasing between model types.  When ``models/<model_type>/`` does not
    exist yet, ``ArtifactLayout.build_model_anchored`` warns and the directory is
    created on demand by :func:`ensure_artifact_dir`.
    """
    return ArtifactLayout.build_model_anchored(model_type, model_name=model_name)


def ensure_artifact_dir(layout: ArtifactLayout) -> None:
    """Create the artifact directory (recursive, no-op if present).
177
178
179
180
181
182
183
184
185

    tmpdir = tempfile.mkdtemp(prefix=".codegen_tmp_", dir=parent)
    old_dir = None
    try:
        _stage_bundle(tmpdir, files)

        if os.path.exists(layout.artifact_dir):
            old_dir = tempfile.mkdtemp(prefix=".codegen_old_", dir=parent)
            os.rmdir(old_dir)  # reserve a name; must be absent for os.replace
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    *,
    timeout_s: float = 600.0,
) -> Iterator[None]:
    """Serialize bundle publication and recover locks left by dead owners."""
    parent = os.path.dirname(layout.artifact_dir) or "."
    bundle_name = os.path.basename(layout.artifact_dir.rstrip(os.sep)) or "generated"
    lock_path = os.path.join(parent, f".{bundle_name}.codegen.lock")
    deadline = time.monotonic() + timeout_s
    descriptor = None
    owner_identity = None
    while descriptor is None:
        try:
            descriptor = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
            try:
                os.write(descriptor, str(os.getpid()).encode("ascii"))
            except BaseException:
                os.close(descriptor)
                descriptor = None
                try:
                    os.unlink(lock_path)
                except FileNotFoundError:
                    pass
                raise
            owner_identity = _file_identity(lock_path)
        except FileExistsError:
            if _remove_stale_lock(lock_path):
                continue
            if time.monotonic() >= deadline:
                raise TimeoutError(
                    f"codegen: timed out waiting for artifact lock {lock_path}"
                ) from None
            time.sleep(0.1)
    try:
        yield
    finally:
        os.close(descriptor)
        if owner_identity is not None and _file_identity(lock_path) == owner_identity:
            try:
                os.unlink(lock_path)
            except FileNotFoundError:
                pass


def _remove_stale_lock(lock_path: str) -> bool:
    """Remove ``lock_path`` when its recorded owner process is dead."""
    try:
        identity = _file_identity(lock_path)
        stat_result = os.stat(lock_path)
        with open(lock_path, "r", encoding="ascii") as handle:
            value = handle.read().strip()
    except (FileNotFoundError, OSError, UnicodeError):
        return False

    try:
        pid = int(value)
    except ValueError:
        # There is a tiny interval between O_EXCL creation and writing the PID.
        # Only reclaim malformed locks after that initialization window.
        age_s = max(0.0, time.time() - stat_result.st_mtime)
        stale = age_s >= _LOCK_INITIALIZATION_GRACE_S
    else:
        stale = not _pid_is_alive(pid)
    if not stale or _file_identity(lock_path) != identity:
        return False
    try:
        os.unlink(lock_path)
    except FileNotFoundError:
        return True
    except OSError:
        return False
    _LOGGER.warning("codegen: removed stale artifact lock %s", lock_path)
    return True


def _pid_is_alive(pid: int) -> bool:
    """Return whether ``pid`` still identifies a live process."""
    if pid <= 0:
        return False
    if os.name == "nt":
        return _windows_pid_is_alive(pid)
    try:
        os.kill(pid, 0)
    except ProcessLookupError:
        return False
    except PermissionError:
        return True
    except OSError as exc:
        if exc.errno == errno.ESRCH:
            return False
        if exc.errno == errno.EPERM:
            return True
        return True
    return True


def _windows_pid_is_alive(pid: int) -> bool:
    """Query a Windows process without using ``os.kill(pid, 0)``.
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    Windows does not implement the POSIX signal-zero probe; using it may
    terminate the target process. ``OpenProcess`` plus ``GetExitCodeProcess``
    is read-only and reports ``STILL_ACTIVE`` for a live owner.
    """
    kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
    open_process = kernel32.OpenProcess
    open_process.argtypes = (ctypes.c_ulong, ctypes.c_int, ctypes.c_ulong)
    open_process.restype = ctypes.c_void_p
    get_exit_code = kernel32.GetExitCodeProcess
    get_exit_code.argtypes = (ctypes.c_void_p, ctypes.POINTER(ctypes.c_ulong))
    get_exit_code.restype = ctypes.c_int
    close_handle = kernel32.CloseHandle
    close_handle.argtypes = (ctypes.c_void_p,)
    close_handle.restype = ctypes.c_int
    process = open_process(0x1000, False, pid)
    if not process:
        # Access denied proves that the process exists. Other errors are
        # treated conservatively except ERROR_INVALID_PARAMETER (no such PID).
        return ctypes.get_last_error() != 87
    try:
        exit_code = ctypes.c_ulong()
        if not get_exit_code(process, ctypes.byref(exit_code)):
            return True
        return exit_code.value == 259  # STILL_ACTIVE
    finally:
        close_handle(process)


def _file_identity(path: str) -> tuple[int, int, int] | None:
    """Return enough stat identity to avoid unlinking a replaced lock file."""
    try:
        result = os.stat(path)
    except FileNotFoundError:
        return None
    return result.st_ino, result.st_mtime_ns, result.st_size


__all__ = [
    "ArtifactLayout",
hyper_parallel/codegen/build.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    Raises:
        RuntimeError: If the native model does not publish a complete artifact.
    """
    return _build_and_publish(config, artifact_dir)


def _build_and_publish(config: Any, artifact_dir: Optional[str]) -> ArtifactLayout:
    """Build and publish from a process-group-free planning topology."""
    distributed_setup = create_offline_distributed_setup(config)
    normalize_distributed_setup_overrides(distributed_setup, config)

    session = CodegenBuildSession()
    with use_codegen_model_build_overlay(session, artifact_dir) as result, use_codegen_planner_overlay():
        config.model.build(
            distributed_setup=distributed_setup,
            peft_config=config.peft,
            activation_checkpoint=config.activation_checkpoint.mode,
            activation_checkpoint_selection=config.activation_checkpoint.selection,
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
            compile_config=config.compile,
            model_init_dtype=config.model_init_dtype,
        )

    if result.artifact_layout is None:
        failure = getattr(result.snapshot, "failure", None)
        if failure is not None:
            raise RuntimeError(
                f"codegen {failure.stage} failed: {failure.reason}"
            )
        raise RuntimeError("native model build completed without publishing a codegen artifact")
    return result.artifact_layout


def create_offline_distributed_setup(config: Any) -> DistributedSetup:
    """Project YAML parallel settings into a process-group-free setup."""
    topology_spec = ParallelTopologySpec.from_config(config)
    layout = resolve_parallel_layout(
        topology_spec,
        infer_minimum_world_size(topology_spec),
    )
    topology = LogicalDeviceMesh(
        mesh_dim_names=("dp", "cp", "tp"),
        mesh_shape=(layout.dp_size, topology_spec.cp_size, topology_spec.tp_size),
    )
    mesh_context = MeshContext.from_layout(layout)
    mesh_context.device_mesh = topology
    return DistributedSetup.from_config(
        config,
        mesh_context=mesh_context,
        strategy_enabled=True,
    )
hyper_parallel/codegen/cli.py
38
39
40
41
42
43
44
45
46
47
48
49
50
        return 2
    _configure_logging(getattr(args, "verbose", False))
    try:
        return args.func(args)
    except Exception as exc:  # A CLI reports failures without a traceback by default.
        if getattr(args, "verbose", False):
            logger.exception("codegen: %s", exc)
        else:
            logger.error("codegen: %s", exc)
        return 1


def cmd_generate(args: argparse.Namespace) -> int:
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


def cmd_generate(args: argparse.Namespace) -> int:
    """Observe a meta model and generate a fresh bundle."""
    if int(os.environ.get("WORLD_SIZE", "1")) > 1:
        raise RuntimeError(
            "codegen generate is a single-process CPU/meta command; "
            "run it with python instead of torchrun"
        )
    config = _load_config(args.config, _cli_overrides(args))
    layout = generate_from_native_build(config, artifact_dir=args.output)
    logger.info("codegen: artifact %s", layout.artifact_dir)
    return 0


def _load_config(yaml_path: str, overrides: Optional[list[str]] = None):
61
62
63
64
65
66
67
68
69


def _load_config(yaml_path: str, overrides: Optional[list[str]] = None):
    """Load a resolved TrainerConfig without building trainer runtime components."""
    return config_parser.load_training_config(yaml_path, overrides or [])


def _cli_overrides(args: argparse.Namespace) -> list[str]:
    """Return typed config overrides left after argparse processing."""
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        prog="hyper_parallel.codegen",
        description="Generate a model bundle from a training YAML.",
    )
    parser.add_argument("--verbose", action="store_true", help="verbose logging")
    subparsers = parser.add_subparsers(dest="command")

    generate = subparsers.add_parser(
        "generate",
        help="observe the model on meta and generate its bundle",
    )
    generate.add_argument("--config", required=True, help="path to the training YAML")
    generate.add_argument("--output", help="explicit artifact output directory")
    generate.set_defaults(func=cmd_generate)

    return parser

hyper_parallel/codegen/emit/bundle.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
    source, and ``__init__.py``; the caller (manager) stages and atomically
    swaps the complete bundle directory.

    """
    if getattr(meta, "status", None) == "not_required":
        logger.info("codegen: rendered audit-only bundle %s", layout.artifact_dir)
        modeling_name = os.path.basename(layout.modeling_path)
        return {
            modeling_name: _prepare_generated_python(
                copy_original_modeling(meta.source)
            ),
            os.path.basename(layout.diff_path): _NO_PATCH_DIFF,
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
            ),
            os.path.basename(layout.diff_path): _NO_PATCH_DIFF,
            "__init__.py": _strip_trailing_whitespace(emit_init_file(layout)),
        }
    if patch_plan is None:
        patch_plan = emission_patches_from_meta(meta)
    original_text = copy_original_modeling(meta.source)
    generated_text = _prepare_generated_python(
        emit_modeling_file(meta, patch_plan=patch_plan)
    )

    modeling_name = os.path.basename(layout.modeling_path)
    diff_text = _strip_trailing_whitespace(
        emit_diff(original_text, generated_text, layout)
    )

    logger.info(
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    # package ``hyper_parallel_generated.<digest>``, so those siblings must be
    # IN the bundle and registered under that package.  Copy the transitive
    # closure of the source's relative imports here using their original
    # basenames, so the generated bundle remains self-contained.
    resolved = resolve_remote_siblings(meta.source)
    if resolved:
        siblings, sibling_diffs = _emit_remote_siblings(resolved, meta, patch_plan)
        import_map = {
            os.path.basename(path): name
            for path, name in zip(resolved.values(), siblings)
        }
        files[modeling_name] = _rewrite_sibling_imports(
            files[modeling_name], import_map
        )
        files[os.path.basename(layout.diff_path)] = _strip_trailing_whitespace(
            emit_diff(original_text, files[modeling_name], layout)
        )
        files.update(siblings)
        files.update(sibling_diffs)
    verify_generated_source_structure(files)
    verify_recorded_patch_bundle(files, patch_plan=patch_plan)
    return files


def _emit_remote_siblings(
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    meta: Any,
    patch_plan: Any,
) -> tuple[dict[str, str], dict[str, str]]:
    """Materialize remote-code siblings and their visible diffs."""
    siblings, originals, import_map = {}, {}, {}
    for path in resolved.values():
        basename = os.path.basename(path)
        with open(path, "r", encoding="utf-8", errors="replace") as handle:
            original = handle.read()
        materialized = _strip_trailing_whitespace(
            _materialize_sibling(original, meta, patch_plan, source_path=path)
        )
        rendered = _prepare_generated_python(materialized)
        output_name = (
            _generated_sibling_name(basename)
            if materialized != _strip_trailing_whitespace(original)
            else basename
        )
        import_map[basename] = output_name
        siblings[output_name] = rendered
        originals[output_name] = (basename, original)
    siblings = {
        name: _rewrite_sibling_imports(text, import_map)
        for name, text in siblings.items()
    }
    diffs = _remote_sibling_diffs(siblings, originals)
    return siblings, diffs


def _remote_sibling_diffs(
    siblings: dict[str, str],
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    siblings: dict[str, str],
    originals: dict[str, tuple[str, str]],
) -> dict[str, str]:
    """Render diffs only for remote siblings whose source changed."""
    diffs = {}
    for output_name, rendered in siblings.items():
        basename, original = originals[output_name]
        if rendered == _strip_trailing_whitespace(original):
            continue
        diffs[f"{output_name}.diff"] = _strip_trailing_whitespace(
            unified_diff(
                _normalize_copied_source(original),
                rendered,
                fromfile=f"a/{basename}",
205
206
207
208
209
210
211
212
213
                fromfile=f"a/{basename}",
                tofile=f"b/{output_name}",
            )
        )
    return diffs


def _prepare_generated_python(source_text: str) -> str:
    """Mark copied source as generated and normalize comment-only URLs.
216
217
218
219
220
221
222
223
224
225
    source units. Repository lint rules for hand-maintained code therefore do
    not apply to those copied units. The markers are comments and do not alter
    imports, executable statements, or runtime behavior.
    """
    text = _normalize_copied_source(source_text)
    text = re.sub(
        r"(^[ \t]*\#.*https?://\S*[A-Za-z0-9])$",
        r"\1 (link).",
        text,
        flags=re.MULTILINE,
223
224
225
226
227
228
229
230
231
232
233
        r"\1 (link).",
        text,
        flags=re.MULTILINE,
    )
    if text.startswith(_GENERATED_SOURCE_HEADER):
        return text
    return (
        _GENERATED_SOURCE_HEADER
        + text.rstrip()
        + "\n"
        + _GENERATED_SOURCE_FOOTER
235
236
237
238
239
240
241
242
243
244
245
246


def _normalize_copied_source(source_text: str) -> str:
    """Normalize repository-gate-only defects in copied upstream source."""
    text = _strip_trailing_whitespace(source_text)
    for typo, correction in _COPIED_SOURCE_SPELLING_FIXES.items():
        text = text.replace(typo, correction)
    return text


def _materialize_sibling(
    source_text: str,
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
    *,
    source_path: str,
) -> str:
    """Apply source-owned strategy and recorded patches to one sibling."""
    source = getattr(meta, "source", None)
    architecture = (
        source.get("architecture")
        if isinstance(source, dict)
        else getattr(source, "architecture", None)
    )
    rendered = render_inline_strategies_for_source_unit(
        source_text,
        meta,
        model_type=architecture,
        patch_plan=patch_plan,
261
262
263
264
265
266
267
268
269
        meta,
        model_type=architecture,
        patch_plan=patch_plan,
    )
    return materialize_recorded_forwards(
        rendered,
        meta,
        patch_plan=patch_plan,
        source_path=source_path,
270
271
272
273
274
275
276
277
278
279
    )


def _generated_sibling_name(basename: str) -> str:
    stem, extension = os.path.splitext(basename)
    return f"{stem}_gen_npu{extension}"


def _rewrite_sibling_imports(
    source_text: str,
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    source_text: str,
    bundled_siblings: dict[str, str],
) -> str:
    """Retarget sibling imports to source units shipped in the bundle."""
    rendered = source_text
    for original, generated in sorted(bundled_siblings.items()):
        original_module = os.path.splitext(original)[0]
        generated_module = os.path.splitext(generated)[0]
        rendered = re.sub(
            rf"(?m)^(\s*from\s+\.+){re.escape(original_module)}(\s+import\s+)",
            rf"\g<1>{generated_module}\g<2>",
            rendered,
        )
        rendered = re.sub(
            rf"(?m)^(\s*from\s+)"
            rf"transformers_modules(?:\.[A-Za-z_][A-Za-z0-9_]*)+\."
            rf"{re.escape(original_module)}(\s+import\s+)",
            rf"\g<1>.{generated_module}\g<2>",
295
296
297
298
299
300
301
302
303
304
305
306
307
308
            rf"{re.escape(original_module)}(\s+import\s+)",
            rf"\g<1>.{generated_module}\g<2>",
            rendered,
        )
    return rendered


def _strip_trailing_whitespace(text: str) -> str:
    """Keep generated artifacts clean without changing their line structure."""
    return "".join(f"{line.rstrip()}\n" for line in text.splitlines())


def emit_init_file(layout: ArtifactLayout) -> str:
    """Render the bundle's ``__init__.py`` text.
308
309
310
311
312
313
314
315
316
    """Render the bundle's ``__init__.py`` text.

    The bundle is an audit/export artifact, so this file carries no re-exports.
    """
    del layout
    return (
        _APACHE_LICENSE_HEADER
        + '"""Generated codegen bundle — DO NOT EDIT MANUALLY.\n'
        "\n"
hyper_parallel/codegen/emit/modeling.py
 99
100
101
102
103
104
105
106
107
    """
    unless_anchor = _relative_anchor(module_name)
    if unless_anchor is None:
        return text
    text = _rewrite_dynamic_relative_imports(text, unless_anchor)

    try:
        tokens = list(tokenize.generate_tokens(io.StringIO(text).readline))
    except tokenize.TokenError:
110
111
112
113
114
115
116
117
118
119
120
121
        return text

    edits: list[tuple[int, int, str]] = []
    i = 0
    while i < len(tokens):
        edit, i = _relative_import_edit(tokens, i, unless_anchor)
        if edit is not None:
            edits.append(edit)

    if not edits:
        return text
    return _splice(text, edits)
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def _relative_import_edit(
    tokens: list[tokenize.TokenInfo], index: int, anchor: list[str]
) -> tuple[tuple[tuple[int, int], tuple[int, int], str] | None, int]:
    """Parse one token position into an absolute relative-import edit."""
    token = tokens[index]
    if token.type != tokenize.NAME or token.string != "from":
        return None, index + 1
    cursor = _skip_import_trivia(tokens, index + 1)
    if cursor >= len(tokens) or not _is_dot_token(tokens[cursor]):
        return None, index + 1
    dots_token = tokens[cursor]
    dots = 0
    while cursor < len(tokens) and _is_dot_token(tokens[cursor]):
        dots += len(tokens[cursor].string)
        cursor += 1
    path, last_token, cursor = _relative_module_path(tokens, cursor)
    import_index = _skip_import_trivia(tokens, cursor)
    if import_index >= len(tokens) or tokens[import_index].string != "import":
        return None, index + 1
    absolute = _absolute_spec(anchor, dots, path)
    if absolute is None:
        return None, index + 1
    end = last_token.end if last_token is not None else dots_token.end
    return (dots_token.start, end, absolute), cursor + 1


def _skip_import_trivia(tokens: list[tokenize.TokenInfo], index: int) -> int:
    """Skip comments and physical/logical newlines between import tokens."""
    trivia = {tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT}
    while index < len(tokens) and tokens[index].type in trivia:
        index += 1
    return index


def _is_dot_token(token: tokenize.TokenInfo) -> bool:
    """Return whether a token contains only relative-import dots."""
    return token.type == tokenize.OP and bool(token.string) and set(token.string) == {"."}


def _relative_module_path(
    tokens: list[tokenize.TokenInfo], index: int
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def _relative_module_path(
    tokens: list[tokenize.TokenInfo], index: int
) -> tuple[list[str], tokenize.TokenInfo | None, int]:
    """Collect the NAME (dot NAME)* portion of a relative import."""
    path: list[str] = []
    last_token = None
    while index < len(tokens):
        token = tokens[index]
        if token.type != tokenize.NAME or token.string == "import":
            break
        path.append(token.string)
        last_token = token
        index += 1
        if index >= len(tokens) or tokens[index].string != ".":
            break
        index += 1
    return path, last_token, index


def _rewrite_dynamic_relative_imports(text: str, anchor: list[str]) -> str:
    """Rewrite imports whose resolved package is not valid Python syntax."""
    try:
        tree = ast.parse(text)
    except SyntaxError:
        return text
    edits = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.ImportFrom) or node.level == 0:
            continue
        absolute = _absolute_spec(anchor, node.level, (node.module or "").split("."))
        if absolute is None or _is_import_syntax_safe(absolute):
            continue
        assignments = []
        for alias in node.names:
            if alias.name == "*":
                raise ValueError(
                    f"codegen cannot rewrite a wildcard import from dynamic module {absolute!r}"
                )
            target = alias.asname or alias.name
            assignments.append(
                f"{target} = __import__({absolute!r}, fromlist=[{alias.name!r}]).{alias.name}"
            )
        indent = " " * node.col_offset
        replacement = ("\n" + indent).join(assignments)
        edits.append(
            (
                (node.lineno, node.col_offset),
                (node.end_lineno, node.end_col_offset),
                replacement,
210
211
212
213
214
215
216
217
218
219
220
221
222
                (node.end_lineno, node.end_col_offset),
                replacement,
            )
        )
    return _splice(text, edits) if edits else text


def _is_import_syntax_safe(module: str) -> bool:
    """Return whether every dotted component can appear in an import statement."""
    return all(
        part.isidentifier() and not keyword.iskeyword(part)
        for part in module.split(".")
    )
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
        if isinstance(meta.source, dict)
        else getattr(meta.source, "module_name", "")
    )
    text = rewrite_relative_imports(text, module_name)
    text = compose_nested_model_sources(text, meta)
    text, emission_meta = _apply_inline_modeling(text, meta, patch_plan=patch_plan)
    source_path = (
        meta.source.get("file_path")
        if isinstance(meta.source, dict)
        else getattr(meta.source, "file_path", None)
    )
    text = materialize_recorded_forwards(
        text,
        emission_meta,
        patch_plan=patch_plan,
        source_path=(
334
335
336
337
338
339
340
341
342
        source_path=(
            None if getattr(emission_meta, "nested_components", None) else source_path
        ),
    )
    text = _lower_forward_boundaries(text, emission_meta, module_name)
    _verify_recorded_patch_materialization(
        text, emission_meta, patch_plan=patch_plan
    )
    return _banner(meta) + text
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
    *,
    patch_plan: Any = None,
) -> None:
    """Require every trainer-installed source-visible patch to be materialized."""
    patches = patch_plan
    local_records = (
        getattr(patches, "local_computes", ()) if patches is not None else ()
    )
    replacement_records = (
        getattr(patches, "module_replacements", ()) if patches is not None else ()
    )
    wrapper_records = (
        getattr(patches, "inner_wrappers", ()) if patches is not None else ()
    )
    if not local_records and not replacement_records and not wrapper_records:
        return
    tree = ast.parse(text)
    classes = _class_index(tree)
    materialized = getattr(meta, "materialized_patches", None)
    if isinstance(materialized, list):
        materialized.clear()
    missing = _verify_local_records(tree, classes, local_records, materialized)
    missing += _verify_replacement_records(
        tree, classes, replacement_records, materialized
    )
    missing += _verify_wrapper_records(
        classes, wrapper_records, replacement_records, materialized
    )
    if missing:
        raise RuntimeError(
            "codegen: trainer-installed source-visible patches were not "
            "materialized in the generated model source: " + ", ".join(missing)
        )
381
382
383
384
385
386
387
388
389


def _class_index(tree: ast.AST) -> dict[str, ast.ClassDef]:
    """Index top-level classes by name."""
    return {
        node.name: node
        for node in getattr(tree, "body", ())
        if isinstance(node, ast.ClassDef)
    }
390
391
392
393
394
395
396
397
398


def _class_methods(host: ast.ClassDef) -> set[str]:
    """Return method names declared directly by a class."""
    return {
        node.name
        for node in host.body
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
399
400
401
402
403
404
405
406
407
408
409


def _class_state_names(host: ast.ClassDef) -> set[str]:
    """Return simple class attributes declared by assignment."""
    names = set()
    for child in host.body:
        targets = (
            child.targets
            if isinstance(child, ast.Assign)
            else (child.target,)
            if isinstance(child, ast.AnnAssign)
408
409
410
411
412
413
414
415
416
417
            else (child.target,)
            if isinstance(child, ast.AnnAssign)
            else ()
        )
        names.update(target.id for target in targets if isinstance(target, ast.Name))
    return names


def _record_materialized(
    materialized: Any, fqn: str, host: str, kind: str, source_form: str
416
417
418
419
420
421
422
423
424
def _record_materialized(
    materialized: Any, fqn: str, host: str, kind: str, source_form: str
) -> None:
    """Append one materialization record when metadata is mutable."""
    if isinstance(materialized, list):
        materialized.append(
            {"fqn": fqn, "host_class": host, "kind": kind, "source_form": source_form}
        )

426
427
428
429
430
431
432
433
434
435
436
437
438
def _verify_local_records(
    tree: ast.AST, classes: dict[str, ast.ClassDef], records: Any, materialized: Any
) -> list[str]:
    """Verify local-compute records in one source unit."""
    missing = []
    for record in records:
        class_name = record.module_type.__name__
        if _bound_function_patch_materialized(tree, record):
            _record_materialized(
                materialized,
                record.module_fqn,
                class_name,
                "local_compute",
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
                class_name,
                "local_compute",
                "bound_method_toggle_forward",
            )
            continue
        host = classes.get(class_name)
        if host is None:
            continue
        valid = {"_forward_impl", "forward"} <= _class_methods(
            host
        ) and "ep_enable" in _class_state_names(host)
        if valid:
            _record_materialized(
                materialized,
                record.module_fqn,
                class_name,
                "local_compute",
453
454
455
456
457
458
459
460
461
462
                "local_compute",
                "same_class_toggle_forward",
            )
        else:
            missing.append(f"{record.module_fqn} ({class_name})")
    return missing


def _verify_replacement_records(
    tree: ast.AST, classes: dict[str, ast.ClassDef], records: Any, materialized: Any
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
def _verify_replacement_records(
    tree: ast.AST, classes: dict[str, ast.ClassDef], records: Any, materialized: Any
) -> list[str]:
    """Verify replacement constructors in one source unit."""
    bound_names = {
        node.id for node in ast.walk(tree) if isinstance(node, ast.Name)
    } | set(classes)
    missing = []
    for record in records:
        name = record.replacement_type.__name__
        visible = name in bound_names and any(
            isinstance(node, ast.Call) and _call_name(node.func) == name
            for node in ast.walk(tree)
        )
        if visible:
            _record_materialized(
                materialized,
                record.module_fqn,
                name,
                "module_replacement",
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
                "module_replacement",
                "constructor",
            )
        else:
            missing.append(f"{record.module_fqn} (replacement {name})")
    return missing


def _wrapper_identity(record: Any) -> tuple[Any, str, str]:
    """Return target type, host name, and target FQN for a wrapper record."""
    target_type = getattr(record, "target_type", None)
    host_name = (
        target_type.__name__
        if target_type is not None
        else type(record.request.target).__name__
    )
    target_fqn = (
        record.module_fqn
        if record.target_fqn == "self"
        else f"{record.module_fqn}.{record.target_fqn}"
    )
    return target_type, host_name, target_fqn


def _wrapper_source_form(
    classes: dict[str, ast.ClassDef], host: ast.ClassDef, record: Any, replacements: Any
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
def _wrapper_source_form(
    classes: dict[str, ast.ClassDef], host: ast.ClassDef, record: Any, replacements: Any
) -> str | None:
    """Return the concrete source form that materializes a wrapper."""
    if (
        "forward" in _class_methods(host)
        and "cp_enable" in _class_state_names(host)
        and _class_dispatch_reads_state(host, "cp_enable")
    ):
        return "same_class_toggle_forward"
    subclass = next(
        (
            node
            for node in classes.values()
            if any(
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
            and "forward" in _class_methods(node)
        ),
        None,
    )
    if subclass is not None:
        return "subclass_forward"
    target_type = getattr(record, "target_type", None)
    replacement_backed = any(
        item.module_fqn == record.module_fqn and item.replacement_type is target_type
        for item in replacements
    )
    if replacement_backed and {"_forward_impl", "forward"} <= _class_methods(host):
        return "replacement_forward_runtime_binding"
    return None


def _verify_wrapper_records(
    classes: dict[str, ast.ClassDef], records: Any, replacements: Any, materialized: Any
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
def _verify_wrapper_records(
    classes: dict[str, ast.ClassDef], records: Any, replacements: Any, materialized: Any
) -> list[str]:
    """Verify inner-wrapper records in one source unit."""
    missing = []
    for record in records:
        _, host_name, target_fqn = _wrapper_identity(record)
        host = classes.get(host_name)
        if host is None:
            continue
        source_form = _wrapper_source_form(classes, host, record, replacements)
        if source_form:
            _record_materialized(
                materialized, target_fqn, host_name, "inner_wrapper", source_form
            )
        else:
            missing.append(f"{target_fqn} (inner wrapper for {host_name})")
    return missing


def _call_name(node: ast.AST) -> str | None:
    """Return the terminal name of a constructor call expression."""
    if isinstance(node, ast.Name):
        return node.id
    if isinstance(node, ast.Attribute):
        return node.attr
    return None


def verify_recorded_patch_bundle(
    files: dict[str, str],
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
    *,
    patch_plan: Any = None,
) -> None:
    """Verify recorded local-compute patches across all generated source units."""
    records = (
        getattr(patch_plan, "local_computes", ()) if patch_plan is not None else ()
    )
    wrappers = (
        getattr(patch_plan, "inner_wrappers", ()) if patch_plan is not None else ()
    )
    trees = {
        name: ast.parse(text) for name, text in files.items() if name.endswith(".py")
    }
    missing = [
        f"{record.module_fqn} ({record.module_type.__name__})"
        for record in records
        if not any(_tree_has_local_record(tree, record) for tree in trees.values())
    ]
    if missing:
        raise RuntimeError(
            "codegen: recorded local-compute patches were not materialized "
            "in their owning source units: " + ", ".join(missing)
        )
    replacements = getattr(patch_plan, "module_replacements", ())
    missing_wrappers = [
        f"{record.module_fqn} ({_wrapper_identity(record)[1]})"
        for record in wrappers
        if not any(
            _tree_has_wrapper_record(tree, record, replacements)
597
598
599
600
601
602
603
604
605
606
607
608
609
610
            _tree_has_wrapper_record(tree, record, replacements)
            for tree in trees.values()
        )
    ]
    if missing_wrappers:
        raise RuntimeError(
            "codegen: recorded inner-wrapper patches were not materialized "
            "in their owning source units: " + ", ".join(missing_wrappers)
        )
    if any("HPCodegen" in text for text in files.values()):
        raise RuntimeError(
            "codegen: generated bundle contains forbidden HPCodegen class names"
        )

610
611
612
613
614
615
616
617
618
619
620
621


def _tree_has_local_record(tree: ast.AST, record: Any) -> bool:
    """Return whether one source tree materializes a local-compute record."""
    if _bound_function_patch_materialized(tree, record):
        return True
    host = _class_index(tree).get(record.module_type.__name__)
    return (
        host is not None
        and {"_forward_impl", "forward"} <= _class_methods(host)
        and "ep_enable" in _class_state_names(host)
    )
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648


def _tree_has_wrapper_record(tree: ast.AST, record: Any, replacements: Any) -> bool:
    """Return whether one source tree materializes an inner-wrapper record."""
    _, class_name, _ = _wrapper_identity(record)
    host = _class_index(tree).get(class_name)
    if host is None or "forward" not in _class_methods(host):
        return False
    if "cp_enable" in _class_state_names(host) and _class_dispatch_reads_state(
        host, "cp_enable"
    ):
        return True
    target_type = getattr(record, "target_type", None)
    replacement_backed = any(
        item.module_fqn == record.module_fqn and item.replacement_type is target_type
        for item in replacements
    )
    return replacement_backed and {"_forward_impl", "forward"} <= _class_methods(host)


def _class_dispatch_reads_state(host: ast.ClassDef, state_name: str) -> bool:
    """Return whether a class dispatches on one state flag in either layer."""
    methods = (
        child
        for child in host.body
        if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef))
        and child.name in {"forward", "_forward_impl"}
646
647
648
649
650
651
652
653
654
        for child in host.body
        if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef))
        and child.name in {"forward", "_forward_impl"}
    )
    return any(
        isinstance(node, ast.Attribute)
        and isinstance(node.value, ast.Name)
        and node.value.id == "self"
        and node.attr == state_name
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677


def _bound_function_patch_materialized(tree: ast.AST, record: Any) -> bool:
    """Return whether a ``MethodType`` forward has an explicit EP toggle."""
    function_name = getattr(record.original_forward, "__name__", "")
    if not function_name:
        return False
    impl_name = f"{function_name}_hp_impl"
    functions = {
        node.name: node
        for node in getattr(tree, "body", ())
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
    wrapper = functions.get(function_name)
    if wrapper is None or impl_name not in functions:
        return False
    return _function_reads_ep_toggle(wrapper) and _tree_binds_method_impl(
        tree, impl_name
    )

677
678
679
680
681
682
683
684
685


def _function_reads_ep_toggle(wrapper: ast.AST) -> bool:
    """Return whether a function reads the instance EP enable flag."""
    return any(
        isinstance(node, ast.Attribute)
        and isinstance(node.value, ast.Name)
        and node.value.id == "self"
        and node.attr == "ep_enable"
688
689
690
691
692
693
694
695
696


def _tree_binds_method_impl(tree: ast.AST, impl_name: str) -> bool:
    """Return whether a tree binds an implementation through MethodType."""
    return any(
        isinstance(node, ast.Assign)
        and any(
            isinstance(target, ast.Attribute) and target.attr == "_forward_impl"
            for target in node.targets
712
713
714
715
716
717
718
719
720
721
722
723
724
725
) -> tuple[str, Any]:
    """Render the artifact through the adapter-driven inline source pipeline."""

    source = getattr(meta, "source", None)
    architecture = (
        source.get("architecture")
        if isinstance(source, dict)
        else getattr(source, "architecture", None)
    )
    return render_inline_modeling_with_plan(
        text,
        meta,
        model_type=architecture,
        patch_plan=patch_plan,
hyper_parallel/codegen/emit/parallel.py
118
119
120
121
122
123
124
125
126
127
    ``forward`` bodies; the rewritten bodies reference the ``_hp_boundary``
    / ``_hp_tp`` instance attributes that ``hp_install_boundaries`` binds
    at runtime — no per-boundary global constant is emitted.
    """
    if _plan_field(frozen_plan, "status") == "not_required":
        raise ValueError("codegen emit: not_required snapshot has no parallel boundaries")
    edits: list[TextEdit] = []
    for class_name, form, emitted, func, injection, deferred_bias in iter_emitted_forms(
        source_text,
        frozen_plan,
166
167
168
169
170
171
172
173
174
        raise ValueError(
            "lower_forward_boundaries: edits overlap (circular class nesting); "
            "cannot safely rewrite: " + ", ".join(nested)
        )
    return apply_edits(source_text, edits)


#: Class attribute a ``tp_collective`` forward carries, naming its form so the
#: runtime install path and preflight can recognize the static template.
213
214
215
216
217
218
219
220
    """
    index = build_source_index(source_text)
    param_plan = _plan_field(frozen_plan, "param_plan") or {}
    injections = _plan_field(frozen_plan, "injections") or []
    injection_by_class = _index_injections_by_class(
        injections, boundary_classes, index, param_plan
    )
    mesh_dim_names = tuple(_plan_field(frozen_plan, "mesh_dim_names") or ())
488
489
490
491
492
493
494
495
496
497
498
    for rule in injections:
        match = rule.get("match")
        if not isinstance(match, str) or not match:
            continue
        entry = (param_plan or {}).get(match) or {}
        if entry.get("skip_source_lowering"):
            continue
        class_name = _class_for_fqn(match, boundary_classes, index, "")
        if class_name is None:
            continue
        payload = {k: rule[k] for k in _INJECTION_KEYS if k in rule}
625
626
627
628
629
630
631
632
633
    out_redist = _render_output_redistribute(deferred_bias)

    local = injection.get("local_compute_fn") if injection else None
    if local is not None:
        raise RuntimeError(
            "codegen: local_compute_fn reached source boundary lowering; "
            "builder recording must mark it skip_source_lowering so runtime "
            "can install the trainer's native Phase C wrapper"
        )
866
867
868
869
870
871
872
873
874
    tp-collective templates; identity transitions carry no note, so an
    identity in/out region emits no noise.
    """
    compute_name = _callable_name(local)
    binding_name = local_compute_binding_name(local)
    return "\n".join([
        "        # [HYPER LOCAL REGION] boundary entry",
        *_ordered_notes(form.in_notes),
        input_redist,
886
887
888
889
890
891
892
893
894

def _callable_name(value: Any) -> str:
    """Human name for an injection's callable (dict target / string / fn)."""
    if isinstance(value, dict):
        return (
            value.get("_target_")
            or value.get("name")
            or value.get("target")
            or repr(value)
hyper_parallel/codegen/emit/patches.py
74
75
76
77
78
79
80
81
82
83
84
85


def emission_patches_from_meta(meta: Any) -> Optional[EmissionPatchSet]:
    """Rehydrate emitter views from plain snapshot data."""
    data = getattr(meta, "emission_patches", None)
    if not data:
        return None
    replacements = tuple(
        ModuleReplacementPatch(
            module_fqn=item["module_fqn"],
            source_type=_resolve_symbol(item["source_type"]),
            replacement_type=_resolve_symbol(item["replacement_type"]),
85
86
87
88
89
90
91
92
93
            replacement_type=_resolve_symbol(item["replacement_type"]),
        )
        for item in data.get("module_replacements", ())
    )
    local_computes = tuple(
        LocalComputePatch(
            module_fqn=item["module_fqn"],
            module_type=_resolve_symbol(item["module_type"]),
            factory=_resolve_symbol(item["factory"]),
 98
 99
100
101
102
103
104
105
106
            installed_forward=None,
        )
        for item in data.get("local_computes", ())
    )
    inner_wrappers = tuple(
        InnerWrapperPatch(
            module_fqn=item["module_fqn"],
            target_fqn=item["target_fqn"],
            wrapper=_resolve_symbol(item["wrapper"]),
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
            target_type=_resolve_symbol(item["target_type"]),
        )
        for item in data.get("inner_wrappers", ())
    )
    return EmissionPatchSet(replacements, local_computes, inner_wrappers)


def local_compute_binding_name(target: Any) -> str:
    """Return the explicit instance attribute used for one compute factory."""
    if isinstance(target, Mapping):
        target = target.get("_target_") or target.get("target") or target.get("name")
    else:
        target = getattr(target, "_target_", target)
    if isinstance(target, str):
        name = target.rsplit(":", 1)[-1].rsplit(".", 1)[-1]
    else:
        name = getattr(target, "__name__", "")
    if not name or not name.isidentifier() or keyword.iskeyword(name):
        raise ValueError(
            f"codegen: local-compute target {target!r} has no valid binding name"
        )
    return name


def _resolve_symbol(path: str) -> Any:
    module_name, separator, qualname = path.partition(":")
    if not separator:
        raise ValueError(f"codegen: invalid patch symbol path {path!r}")
    value = importlib.import_module(module_name)
    for segment in qualname.split("."):
        value = getattr(value, segment)
    return value


__all__ = ["EmissionPatchSet", "emission_patches_from_meta", "local_compute_binding_name"]
hyper_parallel/codegen/emit/recorded.py
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    patch_plan: Any = None,
    source_path: str | None = None,
) -> str:
    """Lower observed local-compute decisions into their source-owned classes."""
    patches = patch_plan
    records = getattr(patches, "local_computes", ()) if patches is not None else ()
    if not records:
        return _materialize_declared_local_computes(source_text, meta)

    _restore_recorded_boundary_ownership(meta, records)

    groups: dict[tuple[type, str], list[Any]] = defaultdict(list)
    for record in records:
        if source_path is not None and not _record_belongs_to_source(
            record, source_path
        ):
            continue
        original = _unbound(record.original_forward)
        groups[(record.module_type, _source_function_name(original))].append(record)

    rendered = source_text
    for (host_type, source_function), group in sorted(
        groups.items(), key=lambda item: (item[0][0].__name__, item[0][1])
    ):
        _require_one_contract(group, meta)
        if source_function and _has_module_function(rendered, source_function):
            rendered = _materialize_bound_module_function(
                rendered, source_function, group[0], meta
            )
            continue
        if not _has_source_class(rendered, host_type.__name__):
            continue
        rendered = _materialize_source_class(
            rendered, host_type.__name__, group[0], meta
        )
        _mark_class_source_lowering_skipped(meta, host_type.__name__)
    return rendered


def _materialize_declared_local_computes(source_text: str, meta: Any) -> str:
    """Lower explicit frozen local-compute injections when no live record exists."""
    boundary_classes = getattr(meta, "boundary_classes", None) or {}
    injections = getattr(meta, "injections", None) or ()
    source_classes = {
        node.name
        for node in ast.parse(source_text).body
        if isinstance(node, ast.ClassDef)
    }
    grouped: dict[str, list[Any]] = defaultdict(list)
    for injection in injections:
        local = injection.get("local_compute_fn")
        module_fqn = injection.get("match")
        class_name = boundary_classes.get(module_fqn)
        if local is None or not module_fqn or class_name not in source_classes:
            continue
        grouped[class_name].append(SimpleNamespace(
            module_fqn=module_fqn,
            factory=_callable_identity(local),
        ))
    rendered = source_text
    materialized = getattr(meta, "materialized_patches", None)
    for class_name, records in sorted(grouped.items()):
        _require_one_contract(records, meta)
        rendered = _materialize_source_class(
            rendered, class_name, records[0], meta
        )
        _mark_class_source_lowering_skipped(meta, class_name)
        for record in records:
            if isinstance(materialized, list):
                materialized.append({
                    "fqn": record.module_fqn,
                    "host_class": class_name,
                    "kind": "local_compute",
                    "source_form": "same_class_toggle_forward",
123
124
125
126
127
128
129
130
131
                    "host_class": class_name,
                    "kind": "local_compute",
                    "source_form": "same_class_toggle_forward",
                })
    return rendered


def _mark_class_source_lowering_skipped(meta: Any, class_name: str) -> None:
    """Keep generic boundary lowering off a source class with an EP toggle.
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    use the runtime per-instance boundary wrapper. Lowering another instance's
    boundary into the shared method would extract a second ``_forward_impl``
    and wrap the EP toggle a second time.
    """
    param_plan = getattr(meta, "param_plan", None) or {}
    boundary_classes = getattr(meta, "boundary_classes", None) or {}
    for module_fqn, owner_class in boundary_classes.items():
        if owner_class != class_name:
            continue
        entry = param_plan.get(module_fqn)
        if isinstance(entry, dict) and entry.get("is_boundary"):
            param_plan[module_fqn] = {**entry, "skip_source_lowering": True}


def _callable_identity(value: Any) -> str:
    """Return a stable identity for a frozen callable declaration."""
    if isinstance(value, dict):
        return str(value.get("_target_") or value.get("target") or value.get("name"))
    return str(value)


def _record_belongs_to_source(record: Any, source_path: str) -> bool:
    """Return whether a recorded host is defined by this source unit."""
    candidates = (
        inspect.getsourcefile(record.module_type),
        inspect.getsourcefile(_unbound(record.original_forward)),
    )
    expected = os.path.normcase(os.path.abspath(source_path))
    return any(
        candidate is not None
        and os.path.normcase(os.path.abspath(candidate)) == expected
        for candidate in candidates
    )
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    Args:
        meta: Mutable generation metadata.
        records: Builder-recorded local-compute patches.
    """
    param_plan = getattr(meta, "param_plan", None)
    boundary_classes = getattr(meta, "boundary_classes", None)
    external_classes = set(getattr(meta, "external_state_classes", None) or ())
    recorded_classes: set[str] = set()
    for record in records:
        class_name = record.module_type.__name__
        recorded_classes.add(class_name)
        if isinstance(param_plan, dict):
            entry = param_plan.get(record.module_fqn)
            if isinstance(entry, dict):
                param_plan[record.module_fqn] = {**entry, "is_boundary": True}
        if isinstance(boundary_classes, dict):
            boundary_classes[record.module_fqn] = class_name
    if hasattr(meta, "external_state_classes"):
        meta.external_state_classes = sorted(external_classes - recorded_classes)


def _unbound(value: Any) -> Any:
    return getattr(value, "__func__", value)


def _source_function_name(value: Any) -> str:
    """Return a top-level source function name, excluding class methods."""
    name = getattr(value, "__qualname__", "")
    return name if name and "." not in name else ""


def _has_module_function(source_text: str, name: str) -> bool:
    return any(
        isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == name
        for node in ast.parse(source_text).body
    )
214
215
216
217
218
219
220
221
    )


def _has_source_class(source_text: str, name: str) -> bool:
    return any(
        isinstance(node, ast.ClassDef) and node.name == name
        for node in ast.parse(source_text).body
    )
222
223
224
225
226
227
228
229
230


def _require_one_contract(records: list[Any], meta: Any) -> None:
    """A shared source forward can represent only one boundary contract."""
    contracts = {
        repr({
            key: value
            for key, value in _entry_for_record(meta, record).items()
            if key != "params"
230
231
232
233
234
235
236
237
238
239
240
            if key != "params"
        })
        for record in records
    }
    factories = {record.factory for record in records}
    if len(contracts) != 1 or len(factories) != 1:
        fqns = ", ".join(sorted(record.module_fqn for record in records))
        raise RuntimeError(
            "codegen: recorded modules sharing one forward have incompatible "
            f"patch contracts: {fqns}"
        )
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        )


def _entry_for_record(meta: Any, record: Any) -> dict[str, Any]:
    entry = (getattr(meta, "param_plan", None) or {}).get(record.module_fqn)
    if entry is None:
        raise RuntimeError(
            f"codegen: recorded patch {record.module_fqn!r} has no frozen plan entry"
        )
    return entry


def _injection_for_record(meta: Any, record: Any) -> dict[str, Any] | None:
    for injection in getattr(meta, "injections", None) or ():
        if injection.get("match") == record.module_fqn:
            return injection
    return None


def _function_info(node: ast.FunctionDef | ast.AsyncFunctionDef, source_text: str) -> FunctionInfo:
    """Build source-index information for one recorded function."""
    lines = source_text.splitlines(keepends=True)
    offsets = [0]
    for line in lines:
        offsets.append(offsets[-1] + len(line))

    def start(line_number: int) -> int:
        return offsets[line_number - 1]

    positional = [arg.arg for arg in (*node.args.posonlyargs, *node.args.args)]
    if positional and positional[0] in ("self", "module"):
        positional.pop(0)
    first_line = min(
        [node.lineno, *(item.lineno for item in node.decorator_list)]
    )
    return FunctionInfo(
        name=node.name,
        body_start=start(node.body[0].lineno),
        body_end=offsets[node.end_lineno],
        def_offset=start(first_line),
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303


def _recorded_body(source_text: str, func: FunctionInfo, record: Any, meta: Any) -> str:
    """Render the body of one observed local-compute patch."""
    entry = _entry_for_record(meta, record)
    injection = _injection_for_record(meta, record)
    form = classify_boundary_form(
        entry,
        tuple(getattr(meta, "mesh_dim_names", None) or ()),
        injection,
    )
    emitted = FORM_REGION if record.factory is not None else FORM_GENERIC
    local = injection.get("local_compute_fn") if injection else None
    if local is not None:
        body = _render_local_compute(
            local,
            _render_input_redistribute(func),
            _render_output_redistribute(),
            form,
302
303
304
305
306
307
308
309
310
311
312
            _render_output_redistribute(),
            form,
        )
    else:
        body = _build_forward_body(source_text, injection, func, form, emitted)
    fallback = _render_forward_call(func).replace("outputs =", "return", 1)
    return "\n".join([
        "        if self.ep_enable:",
        textwrap.indent(body, "    "),
        "",
        fallback,
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    record: Any,
    meta: Any,
) -> str:
    """Turn an instance-bound module function into impl + explicit wrapper."""
    tree = ast.parse(source_text)
    node = next(
        item for item in tree.body
        if isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef))
        and item.name == function_name
    )
    info = _function_info(node, source_text)
    impl_name = f"{function_name}_hp_impl"
    original = source_text[info.def_offset:info.body_end]
    original = original.replace(f"def {function_name}(", f"def {impl_name}(", 1)
    if not original.endswith("\n"):
        original += "\n"
    body = textwrap.dedent(_recorded_body(source_text, info, record, meta))
    body_edit = TextEdit(info.body_start, info.body_end, textwrap.indent(body, "    "))
    rendered = apply_edits(
        source_text,
        [TextEdit(info.def_offset, info.def_offset, original + "\n"), body_edit],
    )
    return _bind_module_impl(rendered, function_name, impl_name)


def _materialize_source_class(
    source_text: str,
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    record: Any,
    meta: Any,
) -> str:
    """Rewrite a source-defined host even when old meta normalization skips it."""
    func = build_source_index(source_text).find_forward(class_name)
    if func is None:
        raise RuntimeError(
            f"codegen: recorded source host {class_name!r} defines no forward"
        )
    body = _recorded_body(source_text, func, record, meta)
    impl = _build_forward_impl_edit(
        source_text,
        func,
        prefix="    ep_enable = False\n\n",
    )
    edits = list(replace_function_body(source_text, func, body, indent=""))
    if impl is not None:
        edits.append(impl)
    return apply_edits(source_text, edits)


def _bind_module_impl(source_text: str, wrapper_name: str, impl_name: str) -> str:
    """Add the instance ``_forward_impl`` binding beside MethodType(forward)."""
    tree = ast.parse(source_text)
    lines = source_text.splitlines(keepends=True)
    offsets = [0]
    for line in lines:
        offsets.append(offsets[-1] + len(line))
    edits: list[TextEdit] = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.Assign) or len(node.targets) != 1:
            continue
        target = node.targets[0]
        value = node.value
        if not (
            isinstance(target, ast.Attribute)
            and target.attr == "forward"
            and isinstance(value, ast.Call)
            and isinstance(value.func, ast.Name)
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
            and value.args
            and isinstance(value.args[0], ast.Name)
            and value.args[0].id == wrapper_name
        ):
            continue
        owner = ast.unparse(target.value)
        indent = " " * node.col_offset
        binding = f"{indent}{owner}._forward_impl = MethodType({impl_name}, {owner})\n"
        edits.append(TextEdit(offsets[node.lineno - 1], offsets[node.lineno - 1], binding))
    if not edits:
        raise RuntimeError(
            f"codegen: source function {wrapper_name!r} is not installed with MethodType"
        )
    return apply_edits(source_text, edits)


__all__ = ["materialize_recorded_forwards"]
hyper_parallel/codegen/emit/replacement.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    The conversion consumes immutable observation data and never recompiles
    rules or inspects a live model.
    """
    payload = snapshot._emission_view() if hasattr(snapshot, "_emission_view") else snapshot  # pylint: disable=protected-access
    if not isinstance(payload, dict):
        raise TypeError("replacement snapshot must be a CodegenSnapshot or dict")
    replacements = payload.get("replacements", {})
    declarations = replacements.get("declarations", ())
    applications = replacements.get("applications", ())
    declarations_by_id = {item["rule_id"]: item for item in declarations}
    if len(declarations_by_id) != len(declarations):
        raise ValueError("replacement snapshot contains duplicate rule_id values")
    records = []
    for application in applications:
        rule_id = application.get("rule_id")
        declaration = declarations_by_id.get(rule_id)
        if declaration is None:
            raise ValueError(
                f"replacement application references unknown rule_id {rule_id!r}"
            )
        source_identity = application.get("source_type") or declaration.get("source_type")
        if not isinstance(source_identity, str) or not source_identity:
            raise ValueError(
                f"replacement application {rule_id!r} has no resolved source_type"
            )
        source_type = source_identity.replace(":", ".", 1)
        record = {
            "match": list(declaration["match"]),
            "fqn": application["primary_fqn"],
            "fqns": list(application["module_fqns"]),
            "module_type": source_type,
55
56
57
58
59
60
61
62
63
64
65
            "factory": application["replacement_type"].replace(":", ".", 1),
            "replacement_target": declaration["replacement_target"],
            "exact_type": bool(declaration["exact_type"]),
        }
        if declaration.get("static_arguments"):
            record["target_config"] = dict(declaration["static_arguments"])
        records.append(record)
    return tuple(records)


__all__ = ["replacement_records_from_snapshot"]
hyper_parallel/codegen/emit/snapshot_patches.py
28
29
30
31
32
33
34
35
36
37
def emission_patches_from_snapshot_payload(
    payload: dict[str, Any],
) -> dict[str, Any]:
    """Translate resolved native observations into emitter patch records."""
    applications = payload["replacements"]["applications"]
    replacements = [
        {
            "module_fqn": item["primary_fqn"],
            "source_type": item["source_type"],
            "replacement_type": item["replacement_type"],
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            "replacement_type": item["replacement_type"],
        }
        for item in applications
    ]
    replacement_types = {
        module_fqn: item["replacement_type"]
        for item in applications
        for module_fqn in item.get("module_fqns", (item["primary_fqn"],))
    }
    source = payload["source"]
    plan = payload.get("plan") or {}
    injections = plan.get("injections") or ()
    boundary_classes = plan.get("boundary_classes") or {}
    boundary_types = plan.get("boundary_types") or {}
    param_plan = plan.get("param_plan") or {}
    local_computes, inner_wrappers = [], []
    for injection in injections:
        local, wrapper = _injection_patches(
            injection,
            source,
            boundary_classes,
            boundary_types,
58
59
60
61
62
63
64
65
66
67
68
69
70
            boundary_types,
            replacement_types,
            param_plan,
        )
        if local is not None:
            local_computes.append(local)
        if wrapper is not None:
            inner_wrappers.append(wrapper)
    return {
        "module_replacements": replacements,
        "local_computes": local_computes,
        "inner_wrappers": inner_wrappers,
    }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    replacement_types: dict[str, str],
    param_plan: dict[str, Any],
) -> tuple[Optional[dict[str, Any]], Optional[dict[str, Any]]]:
    """Translate one resolved injection into source-emission patches."""
    module_fqn = injection.get("match")
    class_name = boundary_classes.get(module_fqn)
    module_name = source.get("module_name")
    declared_type = (
        f"{module_name}:{class_name}" if module_name and class_name else None
    )
    source_type = _source_type_identity(source, class_name)
    observed_type = boundary_types.get(module_fqn)
    if source_type is None and _is_remote_boundary_type(observed_type):
        source_type = observed_type
    bound_forward = _bound_forward_identity(source, module_fqn)
    local = _local_compute_patch(
        injection,
        source,
        module_fqn,
        class_name,
 99
100
101
102
103
104
105
106
107
        source_type,
        bound_forward,
        param_plan,
    )
    wrapper = _inner_wrapper_patch(
        injection,
        module_fqn,
        declared_type,
        source_type,
106
107
108
109
110
111
112
113
114
115
116
117
118
        declared_type,
        source_type,
        replacement_types,
    )
    return local, wrapper


def _is_remote_boundary_type(value: Any) -> bool:
    """Return whether an observed type identifies non-framework source."""
    return isinstance(value, str) and not value.startswith(
        ("hyper_parallel.", "torch.")
    )

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    bound_forward: Optional[str],
    param_plan: dict[str, Any],
) -> Optional[dict[str, Any]]:
    """Build one local-compute emission record."""
    local_compute = injection.get("local_compute_fn")
    if local_compute is None:
        return None
    if declared_type is None:
        raise ValueError(
            "snapshot local_compute_fn requires a source module and "
            f"boundary class for {module_fqn!r}"
        )
    if source_type is None and bound_forward is None:
        return None
    module_type = (
        (_imported_type_identity(source, class_name) if bound_forward else source_type)
        or source_type
        or declared_type
    )
    return {
        "module_fqn": module_fqn,
        "module_type": module_type,
        "factory": import_target_path(local_compute),
        "region_dispatch": (param_plan.get(module_fqn) or {}).get("region_dispatch"),
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    source_type: Optional[str],
    replacement_types: dict[str, str],
) -> Optional[dict[str, Any]]:
    """Build one inner-wrapper emission record."""
    wrapper = injection.get("inner_wrapper")
    if wrapper is None:
        return None
    target_type = replacement_types.get(module_fqn) or source_type or declared_type
    if target_type is None:
        raise ValueError(
            "snapshot inner_wrapper requires an observed replacement or "
            f"boundary class for {module_fqn!r}"
        )
    return {
        "module_fqn": module_fqn,
        "target_fqn": injection.get("inner_target") or "self",
        "wrapper": import_target_path(wrapper),
        "target_type": target_type,
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194


def _source_class_names(source: dict[str, Any]) -> set[str]:
    """Return classes defined by the snapshot's modeling source file."""
    file_path = source.get("file_path")
    if not file_path:
        return set()
    try:
        with open(file_path, "r", encoding="utf-8", errors="replace") as handle:
            tree = ast.parse(handle.read(), filename=file_path)
    except (OSError, SyntaxError):
        return set()
    return {node.name for node in tree.body if isinstance(node, ast.ClassDef)}


def _source_type_identity(
    source: dict[str, Any], class_name: Optional[str]
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def _source_type_identity(
    source: dict[str, Any], class_name: Optional[str]
) -> Optional[str]:
    """Resolve a class to the remote source unit that actually defines it."""
    if not class_name:
        return None
    module_name = source.get("module_name")
    if not isinstance(module_name, str) or not module_name:
        return None
    if class_name in _source_class_names(source):
        return f"{module_name}:{class_name}"

    package, separator, _ = module_name.rpartition(".")
    candidates = []
    for path in resolve_remote_siblings(source).values():
        sibling_source = {"file_path": path}
        if class_name not in _source_class_names(sibling_source):
            continue
        stem = os.path.splitext(os.path.basename(path))[0]
        sibling_module = f"{package}.{stem}" if separator else stem
        candidates.append(f"{sibling_module}:{class_name}")
    if len(candidates) > 1:
        raise ValueError(
            f"snapshot class {class_name!r} is defined by multiple bundled source units: "
            f"{sorted(candidates)}"
        )
    return candidates[0] if candidates else None


def _bound_forward_identity(
    source: dict[str, Any], module_fqn: Optional[str]
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def _bound_forward_identity(
    source: dict[str, Any], module_fqn: Optional[str]
) -> Optional[str]:
    """Resolve a model-owned function installed through ``MethodType``."""
    file_path = source.get("file_path")
    module_name = source.get("module_name")
    if not file_path or not module_name or not module_fqn:
        return None
    try:
        with open(file_path, "r", encoding="utf-8", errors="replace") as handle:
            tree = ast.parse(handle.read(), filename=file_path)
    except (OSError, SyntaxError):
        return None

    boundary_name = module_fqn.rsplit(".", 1)[-1]
    candidates = set()
    for node in ast.walk(tree):
        candidate = _method_type_forward_name(node, boundary_name)
        if candidate:
            candidates.add(candidate)
    if len(candidates) > 1:
        raise ValueError(
            f"snapshot boundary {module_fqn!r} has multiple MethodType forwards: "
            f"{sorted(candidates)}"
        )
    return f"{module_name}:{next(iter(candidates))}" if candidates else None


def _method_type_forward_name(node: ast.AST, boundary_name: str) -> Optional[str]:
    """Return the function assigned as one boundary's MethodType forward."""
    if not isinstance(node, ast.Assign) or len(node.targets) != 1:
        return None
    target, call = node.targets[0], node.value
    valid_target = (
        isinstance(target, ast.Attribute)
        and target.attr == "forward"
        and isinstance(target.value, ast.Attribute)
        and target.value.attr == boundary_name
257
258
259
260
261
262
263
264
265
266
267
268
269
        and target.attr == "forward"
        and isinstance(target.value, ast.Attribute)
        and target.value.attr == boundary_name
    )
    if not valid_target or not isinstance(call, ast.Call):
        return None
    if _ast_name(call.func) != "MethodType" or not call.args:
        return None
    return call.args[0].id if isinstance(call.args[0], ast.Name) else None


def _imported_type_identity(
    source: dict[str, Any], class_name: Optional[str]
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def _imported_type_identity(
    source: dict[str, Any], class_name: Optional[str]
) -> Optional[str]:
    """Resolve an imported boundary class from modules referenced by the source."""
    file_path = source.get("file_path")
    if not file_path or not class_name:
        return None
    try:
        with open(file_path, "r", encoding="utf-8", errors="replace") as handle:
            tree = ast.parse(handle.read(), filename=file_path)
    except (OSError, SyntaxError):
        return None

    explicit_modules = _explicit_import_modules(tree, class_name)
    if len(explicit_modules) > 1:
        raise ValueError(
            f"snapshot class {class_name!r} is imported from multiple modules: "
            f"{sorted(explicit_modules)}"
        )
    if explicit_modules:
        return f"{next(iter(explicit_modules))}:{class_name}"
    candidates = _exporting_import_modules(tree, class_name)
    if len(candidates) > 1:
        raise ValueError(
            f"snapshot class {class_name!r} is exported by multiple imported modules: "
            f"{candidates}"
        )
    return candidates[0] if candidates else None


def _explicit_import_modules(tree: ast.AST, class_name: str) -> set[str]:
    """Return modules that explicitly import one class name."""
    return {
        node.module
        for node in tree.body
        if isinstance(node, ast.ImportFrom)
        and node.level == 0
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340


def _exporting_import_modules(tree: ast.AST, class_name: str) -> list[str]:
    """Return imported modules that export one class name."""
    modules = {
        node.module
        for node in tree.body
        if isinstance(node, ast.ImportFrom) and node.level == 0 and node.module
    }
    candidates = []
    for module_name in sorted(modules):
        try:
            module = importlib.import_module(module_name)
        except (ImportError, AttributeError):
            continue
        if hasattr(module, class_name):
            candidates.append(f"{module_name}:{class_name}")
    return candidates


def _ast_name(node: ast.AST) -> Optional[str]:
    """Return the terminal name of a callable expression."""
    if isinstance(node, ast.Name):
        return node.id
    if isinstance(node, ast.Attribute):
        return node.attr
    return None


__all__ = ["emission_patches_from_snapshot_payload"]
hyper_parallel/codegen/emit/validation.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43


def verify_generated_source_structure(files: Mapping[str, str]) -> None:
    """Reject ambiguous forward composition in generated Python sources."""
    errors: list[str] = []
    for filename, source_text in sorted(files.items()):
        if not filename.endswith(".py") or filename == "__init__.py":
            continue
        try:
            tree = ast.parse(source_text, filename=filename)
        except SyntaxError as exc:
            errors.append(f"{filename}: invalid Python source ({exc.msg})")
            continue
        for class_node in (
            node for node in ast.walk(tree) if isinstance(node, ast.ClassDef)
        ):
            _verify_class(filename, class_node, errors)
    if errors:
        raise RuntimeError(
            "codegen: generated forward composition is invalid: "
            + "; ".join(errors)
        )
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def _verify_class(
    filename: str, class_node: ast.ClassDef, errors: list[str]
) -> None:
    """Append structural errors found in one generated class."""
    methods = [
        node
        for node in class_node.body
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    ]
    counts = Counter(method.name for method in methods)
    duplicates = sorted(name for name, count in counts.items() if count > 1)
    location = f"{filename}:{class_node.name}"
    if duplicates:
        errors.append(f"{location} defines methods more than once: {duplicates}")
    for implementation in (
        method for method in methods if method.name == "_forward_impl"
    ):
        if _calls_self_method(implementation, "_forward_impl"):
            errors.append(f"{location}._forward_impl calls itself")
    dispatch_methods = [
        method for method in methods if method.name in {"forward", "_forward_impl"}
    ]
    for state_flag in _unused_enable_flags(class_node, dispatch_methods):
        errors.append(
            f"{location} declares {state_flag!r} but its forward pipeline "
            "does not read it"
        )
75
76
77
78
79
80
81
82
83
def _unused_enable_flags(
    class_node: ast.ClassDef, dispatch_methods: list[ast.AST]
) -> list[str]:
    """Return declared enable flags not consumed by the forward pipeline."""
    return [
        flag
        for flag in sorted(_class_enable_flags(class_node))
        if not any(_reads_self_attribute(method, flag) for method in dispatch_methods)
    ]
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106


def _class_enable_flags(class_node: ast.ClassDef) -> set[str]:
    """Return conditional parallel enable flags declared by a class."""
    flags: set[str] = set()
    for statement in class_node.body:
        targets = (
            statement.targets
            if isinstance(statement, ast.Assign)
            else (statement.target,) if isinstance(statement, ast.AnnAssign) else ()
        )
        for target in targets:
            if isinstance(target, ast.Name) and target.id.endswith("_enable"):
                flags.add(target.id)
    return flags


def _calls_self_method(method: ast.AST, method_name: str) -> bool:
    return any(
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Name)
        and node.func.value.id == "self"
        and node.func.attr == method_name
109
110
111
112
113
114
115
116
117
    )


def _reads_self_attribute(method: ast.AST, attribute_name: str) -> bool:
    return any(
        isinstance(node, ast.Attribute)
        and isinstance(node.value, ast.Name)
        and node.value.id == "self"
        and node.attr == attribute_name
hyper_parallel/codegen/inline/component_source.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# limitations under the License.
# ============================================================================
"""Public entry for rendering a real replacement component as source."""

from typing import Any, Callable

from hyper_parallel.codegen.inline.attention import render_attention_class
from hyper_parallel.codegen.inline.expansion import ExpandedSource


def render_component_source(
    component: type, *, interface: Callable[..., Any]
) -> ExpandedSource:
    """Render a component and the source closure of its injected interface."""
    return render_attention_class(component, interface=interface)


__all__ = ["render_component_source"]
hyper_parallel/codegen/inline/declaration_edits.py
30
31
32
33
34
35
36
37
38
39
40
41
    imports: Iterable[ImportPatch],
    snippets: Iterable[ModuleSnippetPatch],
) -> list[TextEdit]:
    """Build ordered edits for generated imports and top-level snippets."""
    edits = []
    lines = _render_imports(source_text, imports)
    if lines:
        edits.append(insert_after_imports(
            source_text,
            import_end,
            "\n" + "\n".join(lines) + "\n",
            default_offset=docstring_end or 0,
39
40
41
42
43
44
45
46
47
48
49
            import_end,
            "\n" + "\n".join(lines) + "\n",
            default_offset=docstring_end or 0,
        ))
    parts = [snippet.text.strip("\n") for snippet in snippets if snippet.text.strip()]
    if parts:
        edits.append(insert_after_imports(
            source_text,
            import_end,
            "\n\n" + "\n\n".join(parts) + "\n",
            default_offset=docstring_end or 0,
47
48
49
50
51
52
53
54
55
            import_end,
            "\n\n" + "\n\n".join(parts) + "\n",
            default_offset=docstring_end or 0,
        ))
    return edits


def _render_imports(
    source_text: str, imports: Iterable[ImportPatch]
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def _render_imports(
    source_text: str, imports: Iterable[ImportPatch]
) -> list[str]:
    """Render import patches as deduplicated, grouped import lines."""
    grouped: dict[str, set[str]] = defaultdict(set)
    lines: list[str] = []
    for patch in imports:
        if patch.raw:
            if patch.raw not in source_text:
                lines.append(patch.raw)
            continue
        grouped[patch.module].update(patch.names)
    for module in sorted(grouped):
        line = f"from {module} import {', '.join(sorted(grouped[module]))}"
        if line not in source_text:
            lines.append(line)
    return lines


__all__ = ["declaration_edits"]
hyper_parallel/codegen/inline/definition_edits.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37


def drop_shadowed_definitions(text: str, imported_names: Iterable[str]) -> str:
    """Remove decorated definitions shadowing imported runtime helpers."""
    names = {name for name in imported_names if name}
    if not names:
        return text
    tree = ast.parse(text)
    offsets = _line_offsets(text)
    edits = [
        TextEdit(start, _skip_trailing_blank_lines(text, end), "")
        for node in tree.body
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef))
        and node.name in names
37
38
39
40
41
42
43
44
45
        and node.name in names
        and node.decorator_list
        for start, end in (_node_span(node, offsets),)
    ]
    return apply_edits(text, edits) if edits else text


def apply_class_removals(
    text: str, patches: Iterable[ClassRemovalPatch]
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def apply_class_removals(
    text: str, patches: Iterable[ClassRemovalPatch]
) -> str:
    """Rewrite surviving references and delete classes made unused by patches."""
    patch_list = list(patches)
    if not patch_list:
        return text
    tree = ast.parse(text)
    offsets = _line_offsets(text)
    names = {patch.old_name for patch in patch_list}
    removable = {name for name in names if not _name_is_referenced(tree, name)}
    if not removable:
        return text
    replacements = {patch.old_name: patch.new_name for patch in patch_list}
    classes = {
        node.name: node
        for node in tree.body
        if isinstance(node, ast.ClassDef) and node.name in removable
    }
    deletion_spans = [_node_span(node, offsets) for node in classes.values()]
    edits = _reference_edits(tree, offsets, removable, replacements, deletion_spans)
    for name, node in classes.items():
        del name
        start, end = _node_span(node, offsets)
        edits.append(TextEdit(start, _skip_trailing_blank_lines(text, end), ""))
    return apply_edits(text, edits) if edits else text


def _reference_edits(
    tree: ast.AST,
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    replacements: dict[str, str],
    deletion_spans: list[tuple[int, int]],
) -> list[TextEdit]:
    """Rewrite references that survive class deletion."""
    edits = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.Name) or node.id not in removable:
            continue
        start, end = _node_span(node, offsets)
        if any(span <= start < stop for span, stop in deletion_spans):
            continue
        edits.append(TextEdit(start, end, replacements[node.id]))
    return edits


def _name_is_referenced(tree: ast.AST, name: str) -> bool:
    """Return whether a name survives outside its own definition."""
    return any(
        (isinstance(node, ast.Name) and node.id == name)
        or (isinstance(node, ast.Attribute) and node.attr == name)
        for node in ast.walk(tree)
    )
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111


def _node_span(node: ast.AST, offsets: tuple[int, ...]) -> tuple[int, int]:
    """Return a node span including decorators."""
    line, column = node.lineno, node.col_offset
    decorators = getattr(node, "decorator_list", None)
    if decorators:
        first = min(decorators, key=lambda item: item.lineno)
        if first.lineno < line:
            line, column = first.lineno, max(0, first.col_offset - 1)
    return (
        offsets[line - 1] + column,
        offsets[node.end_lineno - 1] + node.end_col_offset,
    )
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133


def _line_offsets(source_text: str) -> tuple[int, ...]:
    """Return absolute offsets for source line starts."""
    offsets = [0]
    for line in source_text.splitlines(keepends=True):
        offsets.append(offsets[-1] + len(line))
    return tuple(offsets)


def _skip_trailing_blank_lines(text: str, end: int) -> int:
    """Advance beyond whitespace following a deleted definition."""
    while end < len(text) and text[end] in " \t":
        end += 1
    while end < len(text) and text[end] == "\n":
        end += 1
        while end < len(text) and text[end] in " \t":
            end += 1
    return end


__all__ = ["apply_class_removals", "drop_shadowed_definitions"]
hyper_parallel/codegen/inline/forward_edits.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    source_text: str,
    patches: Iterable[ForwardExtractPatch],
) -> list[TextEdit]:
    """Build edits that preserve original forwards and install dispatch bodies."""
    patch_list = tuple(patches)
    if not patch_list:
        return []
    index = build_source_index(source_text)
    edits: list[TextEdit] = []
    for patch in patch_list:
        cls = index.find_class(patch.class_name)
        if cls is None:
            continue
        func = cls.methods.get(patch.method_name)
        if func is None:
            continue
        body = _bind_forward_args(_bind_hidden_arg(patch.body, func), func)
        prefix = _parallel_state_prefix(body)
        if patch.merge_original_body:
            edits.extend(_merged_forward_edits(source_text, patch, func, body, prefix))
            continue
        impl = _build_forward_impl_edit(source_text, func, prefix=prefix)
        if impl is not None:
            edits.append(impl)
        edits.extend(replace_function_body(source_text, func, body, indent="        "))
    return edits


def _parallel_state_prefix(body: str) -> str:
    """Render static enable flags read by a generated dispatch body."""
    lines = [
        f"    {axis}_enable = False"
        for axis in ("tp", "cp", "ep")
        if f"self.{axis}_enable" in body
    ]
    return "\n".join(lines) + "\n\n" if lines else ""


def _merged_forward_edits(
    source_text: str,
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    body: str,
    prefix: str,
) -> list[TextEdit]:
    """Merge dispatch with the original body when no separate impl is needed."""
    fallback = _forward_impl_fallback(func)
    if fallback not in body:
        raise RuntimeError(
            f"codegen: merged strategy for {patch.class_name}."
            f"{patch.method_name} has no original-forward fallback"
        )
    dispatch = body.replace(fallback, "", 1).rstrip()
    original = textwrap.dedent(source_text[func.body_start:func.body_end]).rstrip()
    edits = list(replace_function_body(
        source_text, func, f"{dispatch}\n\n{original}", indent="        "
    ))
    if prefix:
        edits.insert(0, TextEdit(func.def_offset, func.def_offset, prefix))
    return edits


def _bind_hidden_arg(body: str, func: FunctionInfo) -> str:
    """Bind a strategy shell's hidden-input placeholder to its host signature."""
    if _HIDDEN_ARG_TOKEN not in body:
        return body
    if not func.param_names:
        raise RuntimeError(
            f"codegen: inline strategy body references {_HIDDEN_ARG_TOKEN!r} "
            "but the target method has no positional parameter to bind it to"
        )
    return body.replace(_HIDDEN_ARG_TOKEN, func.param_names[0])


def _bind_forward_args(body: str, func: FunctionInfo) -> str:
    """Bind a dispatch shell's argument token to the original signature."""
    token = "__hp_forward_args__"
    if token not in body:
        return body
    args = list(func.param_names)
    if func.vararg_name:
        args.append(f"*{func.vararg_name}")
    args.extend(f"{name}={name}" for name in func.kwonly_names)
    if func.kwarg_name:
        args.append(f"**{func.kwarg_name}")
    return body.replace(token, ", ".join(args))


def _forward_impl_fallback(func: FunctionInfo) -> str:
    """Render the dispatch-shell fallback that calls the extracted method."""
    args = list(func.param_names)
    if func.vararg_name:
        args.append(f"*{func.vararg_name}")
    args.extend(f"{name}={name}" for name in func.kwonly_names)
    if func.kwarg_name:
        args.append(f"**{func.kwarg_name}")
    return f"return self._forward_impl({', '.join(args)})"


__all__ = ["forward_extract_edits"]
hyper_parallel/codegen/inline/framework_spec.py
73
74
75
76
77
78
79
80


def cp_dispatch_shell_body(helper_name: str) -> str:
    """Render CP dispatch around the extracted original forward."""
    return _CP_DISPATCH_SHELL.format(helper_name=helper_name)

#: The framework CP attention strategy kind (structure-keyed, not family-named).
CP_ATTENTION_KIND = "cp_attention"
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        module = importlib.import_module(module_name)
    except ImportError:
        return None
    component = getattr(module, attr, None)
    if not isinstance(component, type):
        return None
    if (
        target.startswith(_COMPONENTS_PREFIX)
        or getattr(component, "_hp_module_replacement", False)
    ):
        return component
    return None


def _component_replacement_target(component: type) -> ReplacementTarget | None:
    """Read a wrap-style component's own constructor as its replacement target.
258
259
260
261
262
263
264
265
266
267
268
269
270
271
        [] if target.interface else [ImportPatch(target.module, (target.component,))]
    )
    snippets: tuple[ModuleSnippetPatch, ...] = ()
    if target.interface:
        provider = _family_provider(model_type, "codegen")
        renderer = getattr(provider, "render_replacement_source", None)
        if not callable(renderer):
            return None
        render_source = cast(Callable[..., Any], renderer)
        expanded = render_source(  # pylint: disable=not-callable
            component, interface=_interface_function(factory, target)
        )
        imports.extend(
            ImportPatch(module="", names=(), raw=line) for line in expanded.imports
309
310
311
312
313
314
315
316
317
318
319
320
    property of the matched source module, never a hand-written literal.
    """
    if target is None:
        return None
    if _direct_component_class(target) is not None:
        return _component_replacement_spec(target, model_type, module_type=module_type)
    if not adapter_path_parts(target):
        return None
    model_type = _family_key(model_type, target)
    factory = _factory_function(target, model_type)
    if factory is None:
        return None
339
340
341
342
343
344
345
346
347
    needs no codegen declaration.  A factory with no constructor call to sink --
    or a rule with no ``module_type`` naming the source class it replaces -- has
    no spec, and the rule is left to the native path.
    """
    del target
    # The source class the constructor call replaces: without the rule's
    # ``module_type`` there is no call site to rewrite, and guessing one would
    # silently target the wrong class.
    old_ctor = class_name_of(module_type)
349
350
351
352
353
354
355
356
357
        return None
    replacement = replacement_target(factory)
    if replacement is None:
        return None
    return _build_replacement_spec(factory, replacement, old_ctor, model_type)


def _component_replacement_spec(
    target: str, model_type: str | None, *, module_type: str | None
375
376
377
378
379
380
381
382
        replacement = _component_replacement_target(component)
        if replacement is not None:
            # No interface: a direct component hands over no family kernel,
            # so the factory argument of _build_replacement_spec is unused.
            spec = _build_replacement_spec(None, replacement, old_ctor, model_type)
    _REPLACEMENT_CACHE[key] = spec
    return spec

399
400
401
402
403
404
405
406
    model_type = _family_key(model_type, target)
    key = (target, model_type, inner_wrapper)
    if key in _STRATEGY_CACHE:
        return _STRATEGY_CACHE[key]
    spec = _resolve_strategy_spec(target, inner_wrapper)
    _STRATEGY_CACHE[key] = spec
    return spec

405
406
407
408
409
410
411
412
413
414
    return spec


def _resolve_strategy_spec(target: str, inner_wrapper: bool) -> StrategySpec | None:
    if inner_wrapper:
        return _cp_strategy_spec(target)
    return None


def _cp_strategy_spec(target: str) -> StrategySpec:
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

    Families without an inline helper keep the original framework CP
    strategy (imports only), preserving the runtime wrapper path.
    """
    resolved = inner_wrapper_target(target)
    if resolved is None:
        return StrategySpec(kind=CP_ATTENTION_KIND, imports=())
    if resolved.inline_body is not None:
        return StrategySpec(
            kind=CP_ATTENTION_KIND,
            subclass_shell=resolved.inline_body,
            imports=(),
        )
    return StrategySpec(
        kind=CP_ATTENTION_KIND,
        subclass_shell=cp_dispatch_shell_body(resolved.helper_name),
        subclass_state_attr=resolved.state_attr,
        imports=(ImportPatch(resolved.helper_module, (resolved.helper_name,)),),
hyper_parallel/codegen/inline/ir.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    strip_boundary_subpatterns: tuple[str, ...] = ()

    def __post_init__(self) -> None:
        """Require a complete method patch or an imports-only declaration."""
        if self.target_class is not None and self.body_template is None:
            raise ValueError("Inline strategy target_class requires a body_template")
        if self.target_class is not None and (not self.target_class or not self.body_template.strip()):
            raise ValueError("Inline strategy class and body must be nonempty")
        if not self.method_name.isidentifier():
            raise ValueError("Inline strategy method_name must be a Python identifier")
        if self.subclass_shell is not None:
            if self.target_class is not None or self.body_template is not None:
                raise ValueError(
                    "Inline strategy subclass_shell is exclusive with "
                    "target_class and body_template"
                )
            if not self.subclass_shell.strip():
                raise ValueError("Inline strategy subclass shell must be nonempty")
hyper_parallel/codegen/inline/meta_plan.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    # DeepseekV3MLP used as both dense decoder layers and shared experts
    # inside MoE). Source lowering rewrites class.forward, which ALL
    # instances inherit -- a non-boundary instance would get a boundary
    # body it cannot satisfy (no _hp_boundary installed).
    normalized = _copy_inline_meta(meta)
    full_class_map = dict(getattr(normalized, "boundary_classes", None) or {})
    _strip_ep_sub_boundaries(normalized, rules, model_type)
    _strip_external_state_boundaries(normalized)
    _mark_shared_class_boundaries(normalized, full_class_map)
    return normalized


def _copy_inline_meta(meta: Any) -> Any:
    """Copy only fields normalized by source emission, preserving all other context."""
    normalized = copy(meta)
    normalized.param_plan = dict(getattr(meta, "param_plan", None) or {})
    normalized.boundary_classes = dict(
        getattr(meta, "boundary_classes", None) or {}
    )
    normalized.external_state_classes = list(
        getattr(meta, "external_state_classes", None) or ()
    )
    return normalized


def _strip_external_state_boundaries(meta: Any) -> None:
    """Keep parameter sharding without duplicating inline-body-owned collectives.
 92
 93
 94
 95
 96
 97
 98
 99
100
        if class_name not in external_classes:
            continue
        entry = param_plan.get(fqn)
        if isinstance(entry, dict):
            param_plan[fqn] = {**entry, "is_boundary": False}
        boundary_classes.pop(fqn)


def _strip_ep_sub_boundaries(
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    applies and the preflight coverage check passes.
    """
    param_plan = getattr(meta, "param_plan", None) or {}
    boundary_classes = getattr(meta, "boundary_classes", None) or {}
    ep_fqns, strip_patterns = _collect_ep_boundaries(
        meta, rules, model_type, boundary_classes
    )
    if not ep_fqns:
        return
    mark_fqns = _expanded_ep_boundaries(ep_fqns, strip_patterns, param_plan)
    for fqn in mark_fqns:
        entry = param_plan.get(fqn)
        if isinstance(entry, dict):
            param_plan[fqn] = {**entry, "is_boundary": False}
        boundary_classes.pop(fqn, None)


def _collect_ep_boundaries(
    meta: Any,
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    """Collect EP host FQNs and sub-boundary patterns from inline rules."""
    ep_fqns: set[str] = set()
    strip_patterns: tuple[str, ...] = ()
    for rule in rules:
        resolved = _ep_rule_hosts(rule, model_type, boundary_classes)
        if resolved is None:
            continue
        spec, host_classes = resolved
        if spec.strip_boundary_subpatterns:
            strip_patterns = spec.strip_boundary_subpatterns
        if host_classes:
            # Persist the derived classes so a re-emission of this
            # already-normalized meta (whose boundary entries the strip below
            # consumes) can still resolve its patch hosts.
            recorded = set(getattr(meta, "external_state_classes", None) or ())
            meta.external_state_classes = sorted(recorded | host_classes)
        for fqn, cls in boundary_classes.items():
            if cls not in host_classes:
                continue
            if any(fnmatch(fqn, pattern) for pattern in rule.match):
                ep_fqns.add(fqn)
    return ep_fqns, strip_patterns


def _expanded_ep_boundaries(
    ep_fqns: set[str], strip_patterns: tuple[str, ...], param_plan: dict[str, Any]
172
173
174
175
176
177
178
179
180
            for pat in strip_patterns:
                if fnmatch(local_suffix, pat):
                    mark_fqns.add(fqn)
                    break
    return mark_fqns


def _ep_rule_hosts(
    rule: SourceObservation, model_type: str | None, boundary_classes: dict[str, str]
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def _ep_rule_hosts(
    rule: SourceObservation, model_type: str | None, boundary_classes: dict[str, str]
) -> tuple[Any, set[str]] | None:
    """Resolve an inline EP rule and the source classes that host it."""
    if rule.local_compute_target is None:
        return None
    spec = strategy_spec(rule.local_compute_target, model_type)
    if spec is None or spec.body_template is None:
        return None
    if spec.target_class is not None:
        return spec, {spec.target_class}
    return spec, rule_block_classes(rule, boundary_classes)


def _mark_shared_class_boundaries(meta: Any, full_class_map: dict[str, str]) -> None:
    """Mark source lowering as skipped for classes shared with a non-boundary instance.
236
237
238
239
240
241
242
243
244
        if class_name not in non_boundary_classes:
            continue
        entry = param_plan.get(fqn)
        if isinstance(entry, dict):
            param_plan[fqn] = {**entry, "skip_source_lowering": True}
            logger.info(
                "codegen: boundary %s (class %s) marked skip_source_lowering "
                "-- the class is shared with a non-boundary instance; the "
                "runtime per-instance wrapper is used instead",
hyper_parallel/codegen/inline/observations.py
23
24
25
26
27
28
29
30
31
32
33
34
35


def collect_source_observations(meta: Any) -> tuple[SourceObservation, ...]:
    """Return normalized source rules from the already observed native plan."""
    rules: list[SourceObservation] = []
    for record in getattr(meta, "module_overrides", None) or []:
        target = _replacement_target(record)
        if target:
            rules.append(
                SourceObservation(
                    match=_matches(record),
                    module_type=record.get("module_type"),
                    replace_target=target,
34
35
36
37
38
39
40
41
42
43
44
45
46
                    module_type=record.get("module_type"),
                    replace_target=target,
                )
            )
    for record in getattr(meta, "injections", None) or []:
        local = projected_target_path(record.get("local_compute_fn"))
        inner = projected_target_path(record.get("inner_wrapper"))
        if local is not None or inner is not None:
            rules.append(
                SourceObservation(
                    match=_matches(record),
                    inner_wrapper_target=inner,
                    local_compute_target=local,
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                    inner_wrapper_target=inner,
                    local_compute_target=local,
                )
            )
    return tuple(rules)


def _replacement_target(record: dict[str, Any]) -> Any:
    """Prefer model-owned declarations and otherwise use the observed factory."""
    declared = record.get("replacement_target")
    parts = str(declared or "").split(".")
    if len(parts) >= 4 and parts[:2] == ["hyper_parallel", "models"] and parts[3] == "adapter":
        return declared
    return record.get("factory")


def _matches(record: dict[str, Any]) -> tuple[str, ...]:
    """Normalize one observation's selected FQNs."""
    value = record.get("fqns") or record.get("match") or record.get("fqn")
    if value is None:
        return ()
    if isinstance(value, str):
        return (value,)
    if isinstance(value, Iterable):
        return tuple(str(item) for item in value)
    return (str(value),)


__all__ = ["collect_source_observations"]
hyper_parallel/codegen/inline/patch_engine.py
42
43
44
45
46
47
48
49
50
51
52
    Returns:
        The patched source text.
    """

    text, hits = _apply_source_declarations(source_text, patch_set)
    text = _apply_constructor_followups(text, patch_set, hits)
    return _apply_forward_patches(text, patch_set.forward_extracts)


def _apply_source_declarations(
    source_text: str, patch_set: InlinePatchSet
51
52
53
54
55
56
57
58
59
60
def _apply_source_declarations(
    source_text: str, patch_set: InlinePatchSet
) -> tuple[str, dict[str, int]]:
    """Apply top-level declarations and the first constructor rewrite wave."""
    index = build_source_index(source_text)
    edits = declaration_edits(
        source_text,
        index.import_end,
        index.docstring_end,
        patch_set.imports,
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        index.docstring_end,
        patch_set.imports,
        patch_set.module_snippets,
    )
    waves = _constructor_waves(list(patch_set.constructor_replaces))
    hits: dict[str, int] = defaultdict(int)
    if waves:
        edits.extend(_constructor_edits(source_text, waves[0], hits))
    text = apply_edits(source_text, edits) if edits else source_text
    for wave in waves[1:]:
        wave_edits = _constructor_edits(text, wave, hits)
        if wave_edits:
            text = apply_edits(text, wave_edits)
    return text, hits


def _apply_constructor_followups(
    source_text: str,
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    patch_set: InlinePatchSet,
    hits: dict[str, int],
) -> str:
    """Apply deferred replacements, validate scopes, and remove replaced classes."""
    deferred_edits = _deferred_replacement_edits(
        source_text,
        patch_set.constructor_replaces,
        hits,
    )
    if deferred_edits:
        source_text = apply_edits(source_text, deferred_edits)
    if patch_set.constructor_replaces:
        _require_scoped_rewrites(patch_set.constructor_replaces, hits)
    if patch_set.class_removals:
        source_text = apply_class_removals(source_text, patch_set.class_removals)
    return source_text


def _apply_forward_patches(
    source_text: str, patches: Iterable[ForwardExtractPatch]
 95
 96
 97
 98
 99
100
101
102
103
104
def _apply_forward_patches(
    source_text: str, patches: Iterable[ForwardExtractPatch]
) -> str:
    """Apply forward extraction only after the source structure has settled."""
    edits = forward_extract_edits(source_text, patches)
    return apply_edits(source_text, edits) if edits else source_text


def _constructor_waves(
    patch_list: list[ConstructorReplacePatch],
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    scoped wrap with no call site fails loudly, so the order is not
    optional).
    """

    remaining = list(patch_list)
    waves: list[list[ConstructorReplacePatch]] = []
    while remaining:
        introduced = {
            patch.new_ctor for patch in remaining if patch.new_ctor != patch.old_ctor
        }
        wave = [patch for patch in remaining if _is_wave_ready(patch, remaining, introduced)]
        if not wave:
            names = sorted({patch.old_ctor for patch in remaining})
            raise ValueError(
                f"codegen: constructor replacement dependency cycle among {names}"
            )
        chosen = {id(patch) for patch in wave}
        remaining = [patch for patch in remaining if id(patch) not in chosen]
        waves.append(wave)
    return waves


def _is_wave_ready(
    patch: ConstructorReplacePatch,
139
140
141
142
143
144
145
146
147
148
149
150
151
    remaining: list[ConstructorReplacePatch],
    introduced: set[str],
) -> bool:
    """Return whether a constructor patch has no remaining dependency."""
    if patch.old_ctor in introduced:
        return False
    if patch.mode != "name":
        return True
    return not any(
        other is not patch
        and other.mode == "wrap_source"
        and other.old_ctor == patch.old_ctor
        for other in remaining
158
159
160
161
162
163
164
165
166
167
168
169
170
    hits: dict[str, int],
) -> list[TextEdit]:
    """Build the edits that swap constructor calls per patch target."""
    patches_by_old: dict[str, list[ConstructorReplacePatch]] = defaultdict(list)
    for patch in patches:
        patches_by_old[patch.old_ctor].append(patch)
    if not patches_by_old:
        return []
    scopes = _scopes_by_old_ctor(patches)
    tree = ast.parse(source_text)
    offsets = _line_offsets(source_text)
    edits: list[TextEdit] = []
    for node in ast.walk(tree):
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    constructor call to rewrite. For a wrap-style replacement, the native
    operation is still expressible exactly: wait until the root model has
    built the observed FQNs, then replace those module attributes in place.
    """
    pending = [
        patch
        for patch in patches
        if patch.scope_fqns and not hits.get(patch.old_ctor)
    ]
    if not pending:
        return []
    tree = ast.parse(source_text)
    offsets = _line_offsets(source_text)
    insertions: dict[int, list[str]] = defaultdict(list)
    for patch in pending:
        if patch.mode != "wrap_source":
            continue
        root_attr = patch.scope_fqns[0].split(".", 1)[0]
        constructors = _root_constructors(tree, root_attr)
        if len(constructors) != 1:
            continue
        function = constructors[0]
        _, end = _node_span(function.body[-1], offsets)
        insertions[end].append(_render_deferred_replacement(patch))
        hits[patch.old_ctor] += 1
    return [
        TextEdit(offset, offset, "".join(parts))
        for offset, parts in insertions.items()
    ]
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314


def _root_constructors(tree: ast.Module, root_attr: str) -> list[ast.FunctionDef]:
    """Return constructors that establish the first segment of an observed FQN."""
    constructors = []
    for node in tree.body:
        if not isinstance(node, ast.ClassDef):
            continue
        for child in node.body:
            if not isinstance(child, ast.FunctionDef) or child.name != "__init__":
                continue
            if any(
                isinstance(target, ast.Attribute)
                and isinstance(target.value, ast.Name)
                and target.value.id == "self"
                and target.attr == root_attr
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                for target in (
                    item.targets if isinstance(item, ast.Assign) else (item.target,)
                )
            ):
                constructors.append(child)
    return constructors


def _render_deferred_replacement(patch: ConstructorReplacePatch) -> str:
    """Render an exact-FQN replacement loop inside a root constructor."""
    arguments = [
        argument
        for argument in patch.keyword_args
        if not argument.startswith("module_fqn=")
    ]
    extra = "".join(f", {argument}" for argument in arguments)
    fqns = repr(tuple(patch.scope_fqns))
    return (
        "\n        _hp_replacement_cache = {}\n"
        f"        for _hp_fqn in {fqns}:\n"
        "            _hp_parent_fqn, _, _hp_name = _hp_fqn.rpartition('.')\n"
        "            _hp_parent = self.get_submodule(_hp_parent_fqn)\n"
hyper_parallel/codegen/inline/pipeline.py
61
62
63
64
65
66
67
68
69
    Returns:
        The rendered modeling source.
    """

    rendered, _ = render_inline_modeling_with_plan(
        source_text,
        meta,
        model_type,
        patch_plan=patch_plan,
67
68
69
70
71
72
73
74
75
        meta,
        model_type,
        patch_plan=patch_plan,
    )
    return rendered


def render_inline_modeling_with_plan(
    source_text: str,
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    *,
    patch_plan: Any = None,
) -> tuple[str, Any]:
    """Render source and return the independently normalized emission plan."""
    rules = collect_source_observations(meta)
    if not rules:
        return source_text, meta
    patches = patch_plan
    recorded_local_compute = bool(
        getattr(patches, "local_computes", ()) if patches is not None else ()
    )
    strategy_rules = (
        tuple(replace(rule, local_compute_target=None) for rule in rules)
        if recorded_local_compute
        else rules
    )
    _require_inline_coverage(strategy_rules, model_type)
    patch_set = build_source_patch_plan(
        rules,
        strategy_rules,
        model_type,
        _observed_boundary_classes(meta, patch_plan),
 98
 99
100
101
102
103
104
105
106
107
        model_type,
        _observed_boundary_classes(meta, patch_plan),
        getattr(meta, "external_state_classes", None),
    )
    _add_recorded_forward_host_imports(source_text, patch_set, patch_plan)
    _materialize_imported_forward_hosts(source_text, patch_set)
    rendered = apply_patch_set(source_text, patch_set)
    # The copied modeling source brings its own decorated helpers that share
    # names with the library functions the artifact imports, and a local
    # definition wins over the import: the native path calls the library
112
113
114
115
116
117
118
119
120
121
    # on purpose) keep their definitions.
    rendered = drop_shadowed_definitions(
        rendered, _library_import_names(rendered) & _decorated_names(source_text)
    )
    normalized_meta = normalize_inline_meta(meta, strategy_rules, model_type)
    return rendered, normalized_meta


def _add_recorded_forward_host_imports(
    source_text: str,
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    top-level source.  In that case the frozen replacement record is the source
    of truth for the concrete host type.  Add its real import so the generic
    imported-host materializer can emit the CP dispatch class.
    """
    source_classes = {
        node.name
        for node in ast.parse(source_text).body
        if isinstance(node, ast.ClassDef)
    }
    imported_names = {
        name
        for item in patch_set.imports
        if not item.raw
        for name in item.names
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
        for item in patch_set.imports
        if not item.raw
        for name in item.names
    }
    replacements = (
        getattr(patch_plan, "module_replacements", ())
        if patch_plan is not None
        else ()
    )
    replacement_types = {
        record.replacement_type.__name__: record.replacement_type
        for record in replacements
    }
    for patch in patch_set.forward_extracts:
        if patch.class_name in source_classes or patch.class_name in imported_names:
            continue
        replacement_type = replacement_types.get(patch.class_name)
        if replacement_type is None:
            continue
        patch_set.imports.append(
            ImportPatch(replacement_type.__module__, (replacement_type.__name__,))
        )
        imported_names.add(patch.class_name)


def _observed_boundary_classes(meta: Any, patch_plan: Any) -> dict[str, str]:
    """Return boundary owners after native inner-wrapper target resolution."""
    classes = dict(getattr(meta, "boundary_classes", None) or {})
    wrappers = (
        getattr(patch_plan, "inner_wrappers", ())
        if patch_plan is not None
        else ()
    )
    for record in wrappers:
        target_type = getattr(record, "target_type", None)
        if target_type is None:
            continue
        target_fqn = (
            record.module_fqn
            if record.target_fqn == "self"
            else f"{record.module_fqn}.{record.target_fqn}"
        )
        classes[target_fqn] = target_type.__name__
    return classes


def _materialize_imported_forward_hosts(
    source_text: str, patch_set: InlinePatchSet
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def _materialize_imported_forward_hosts(
    source_text: str, patch_set: InlinePatchSet
) -> None:
    """Render same-name subclasses when a strategy host is an imported component."""
    source_classes = {
        node.name
        for node in ast.parse(source_text).body
        if isinstance(node, ast.ClassDef)
    }
    remaining = []
    rendered: set[str] = set()
    for patch in patch_set.forward_extracts:
        if patch.class_name in source_classes:
            remaining.append(patch)
            continue
        import_index = next(
            (
                index
                for index, item in enumerate(patch_set.imports)
                if not item.raw and patch.class_name in item.names
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
                if not item.raw and patch.class_name in item.names
            ),
            None,
        )
        if import_index is None:
            remaining.append(patch)
            continue
        if patch.class_name in rendered:
            continue
        rendered.add(patch.class_name)
        imported = patch_set.imports[import_index]
        other_names = tuple(name for name in imported.names if name != patch.class_name)
        alias = f"_{patch.class_name}Base"
        replacement_imports = []
        if other_names:
            replacement_imports.append(ImportPatch(imported.module, other_names))
        replacement_imports.append(ImportPatch(
            module="",
            names=(),
            raw=f"from {imported.module} import {patch.class_name} as {alias}",
        ))
        patch_set.imports[import_index:import_index + 1] = replacement_imports
        body = patch.body.replace(
            "return self._forward_impl(__hp_forward_args__)",
            "return super().forward(__hp_forward_args__)",
        ).replace("__hp_forward_args__", "*args, **kwargs")
        snippet = (
            f"class {patch.class_name}({alias}):\n"
            "    \"\"\"Generated source-visible parallel form.\"\"\"\n\n"
            "    cp_enable = False\n\n"
            "    def forward(self, *args, **kwargs):\n"
234
235
236
237
238
239
240
241
242
243
            "    cp_enable = False\n\n"
            "    def forward(self, *args, **kwargs):\n"
            f"{textwrap.indent(body.strip(), '        ')}\n"
        )
        patch_set.module_snippets.append(ModuleSnippetPatch(snippet))
    patch_set.forward_extracts = remaining


def render_inline_strategies_for_source_unit(
    source_text: str,
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    *,
    patch_plan: Any = None,
) -> str:
    """Render only strategy forwards owned by one sibling source unit."""
    rules = collect_source_observations(meta)
    if not rules:
        return source_text
    recorded_local_compute = bool(
        getattr(patch_plan, "local_computes", ())
        if patch_plan is not None
        else ()
    )
    strategy_rules = (
        tuple(replace(rule, local_compute_target=None) for rule in rules)
        if recorded_local_compute
        else rules
    )
    _require_inline_coverage(strategy_rules, model_type)
    patches = build_strategy_patches(
        strategy_rules,
        model_type,
        _observed_boundary_classes(meta, patch_plan),
        getattr(meta, "external_state_classes", None),
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        model_type,
        _observed_boundary_classes(meta, patch_plan),
        getattr(meta, "external_state_classes", None),
    )
    source_classes = {
        node.name
        for node in ast.parse(source_text).body
        if isinstance(node, ast.ClassDef)
    }
    patches.forward_extracts = [
        patch for patch in patches.forward_extracts
        if patch.class_name in source_classes
    ]
    if not patches.forward_extracts:
        return source_text
    patches.module_snippets.clear()
    patches.constructor_replaces.clear()
    patches.class_removals.clear()
    return apply_patch_set(source_text, patches)


def _library_import_names(text: str) -> set[str]:
    """Names the artifact imports from the library, i.e. the winning bindings.
hyper_parallel/codegen/inline/recognition.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247


def inner_wrapper_target(factory_path: str | None) -> InnerWrapperTarget | None:
    """Recover an inner wrapper's emitted helper contract from its source."""
    if not factory_path:
        return None
    module_name, _, factory_name = factory_path.rpartition(".")
    if not module_name or not factory_name:
        return None
    tree = _module_source_tree(module_name)
    if tree is None:
        return None
    factory_node = _module_function(tree, factory_name)
    if factory_node is None:
        return None
    request_owner = _request_owner(factory_node, tree)
    replacement_name = _replacement_callable(request_owner)
    helper_name = (
        _forward_helper_name(request_owner, replacement_name)
        if request_owner is not None and replacement_name
        else None
    )
    module_functions = {
        node.name
        for node in tree.body
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
    if not helper_name or helper_name not in module_functions:
        inline_body = _nested_forward_body(request_owner, replacement_name)
        if inline_body is None:
            return None
        return InnerWrapperTarget(inline_body=inline_body)
    return InnerWrapperTarget(
        helper_module=module_name,
        helper_name=helper_name,
        state_attr=_cp_mesh_companion_attr(tree),
    )
248
249
250
251
252
253
254
255
256


def _module_function(tree: ast.Module, name: str) -> ast.AST | None:
    """Return a named top-level function from a parsed module."""
    return next(
        (
            node
            for node in tree.body
            if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308


def _replacement_callable(request_owner: ast.AST | None) -> str | None:
    """Resolve the callable installed by an inner-wrapper request."""
    if request_owner is None:
        return None
    return _rewrite_request_callable(request_owner) or _returned_nested_callable(request_owner)


def _request_owner(factory_node: ast.AST, tree: ast.Module) -> ast.AST | None:
    """Return the factory or its module-level delegate that builds the request."""
    if (
        _rewrite_request_callable(factory_node) is not None
        or _returned_nested_callable(factory_node) is not None
    ):
        return factory_node
    functions = {
        node.name: node
        for node in tree.body
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
    for node in ast.walk(factory_node):
        if not isinstance(node, ast.Return) or not isinstance(node.value, ast.Call):
            continue
        if not isinstance(node.value.func, ast.Name):
            continue
        delegate = functions.get(node.value.func.id)
        if delegate is not None and _rewrite_request_callable(delegate) is not None:
            return delegate
    return None


def _returned_nested_callable(tree: ast.AST) -> str | None:
    """Return a nested callable returned directly by an inner wrapper."""
    nested = {
        node.name
        for node in ast.iter_child_nodes(tree)
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
    for node in ast.walk(tree):
        if isinstance(node, ast.Return) and isinstance(node.value, ast.Name):
            if node.value.id in nested:
                return node.value.id
    return None


@lru_cache(maxsize=None)
def _module_source(module_name: str) -> str | None:
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

@lru_cache(maxsize=None)
def _module_source(module_name: str) -> str | None:
    """Load module source once without importing optional accelerator deps."""
    relative = os.path.join(*module_name.split(".")) + ".py"
    for root in sys.path:
        path = os.path.join(root or os.curdir, relative)
        if not os.path.isfile(path):
            continue
        try:
            with open(path, "r", encoding="utf-8") as handle:
                return handle.read()
        except (OSError, UnicodeError):
            return None
    return None


def _module_source_tree(module_name: str) -> ast.Module | None:
    """Parse cached module source into an isolated syntax tree."""
    source = _module_source(module_name)
    if source is None:
        return None
    try:
        return ast.parse(source)
    except SyntaxError:
        return None


def _rewrite_request_callable(tree: ast.AST) -> str | None:
    """Return the replacement callable passed to ``_ForwardRewriteRequest``."""
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Name):
            continue
        if node.func.id == "_ForwardRewriteRequest" and len(node.args) >= 2:
            if isinstance(node.args[1], ast.Name):
                return node.args[1].id
    return None


def _forward_helper_name(tree: ast.AST, replacement_name: str) -> str | None:
    """Return the module-level helper called by the replacement forward."""
    for node in ast.walk(tree):
        if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) or node.name != replacement_name:
            continue
        for child in ast.walk(node):
            if isinstance(child, ast.Return) and isinstance(child.value, ast.Call):
                if isinstance(child.value.func, ast.Name):
                    return child.value.func.id
    return None


def _cp_mesh_companion_attr(tree: ast.AST) -> str | None:
    """Return the companion attribute whose value is the wrapper's CP mesh."""
    return (
        _cp_mesh_dict_attr(tree)
        or _cp_mesh_setattr_attr(tree)
        or _cp_mesh_assignment_attr(tree)
    )
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378


def _cp_mesh_dict_attr(tree: ast.AST) -> str | None:
    """Return the CP mesh attribute declared in companion_attrs."""
    for node in ast.walk(tree):
        if not isinstance(node, ast.keyword) or node.arg != "companion_attrs":
            continue
        if not isinstance(node.value, ast.Dict):
            continue
        for key, value in zip(node.value.keys, node.value.values):
            if (
                isinstance(key, ast.Constant)
                and isinstance(key.value, str)
                and isinstance(value, ast.Name)
                and value.id == "cp_mesh"
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
                and isinstance(key.value, str)
                and isinstance(value, ast.Name)
                and value.id == "cp_mesh"
            ):
                return key.value
    return None


def _cp_mesh_setattr_attr(tree: ast.AST) -> str | None:
    """Return the CP mesh attribute installed through setattr."""
    for node in ast.walk(tree):
        if (
            isinstance(node, ast.Call)
            and isinstance(node.func, ast.Name)
            and node.func.id == "setattr"
            and len(node.args) == 3
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
            and isinstance(node.args[1].value, str)
            and isinstance(node.args[2], ast.Name)
            and node.args[2].id == "cp_mesh"
        ):
            return node.args[1].value
    return None


def _cp_mesh_assignment_attr(tree: ast.AST) -> str | None:
    """Return the CP mesh attribute installed through assignment."""
    for node in ast.walk(tree):
        if not isinstance(node, (ast.Assign, ast.AnnAssign)):
            continue
        value = node.value
        targets = node.targets if isinstance(node, ast.Assign) else (node.target,)
        if not isinstance(value, ast.Name) or value.id != "cp_mesh":
            continue
        for target in targets:
            if isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name):
                return target.attr
    return None


def _nested_forward_body(tree: ast.AST, replacement_name: str | None) -> str | None:
    """Lower a closure-backed rewrite into an imported-host dispatch body.
422
423
424
425
426
427
428
429
430
431
432
    request's ``companion_attrs`` mapping.  In that form, replace captured
    state reads with instance attributes and the saved original forward with
    ``super().forward``; the imported-host materializer supplies the subclass.
    """
    if not replacement_name:
        return None
    forward = next(
        (
            node
            for node in ast.walk(tree)
            if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
            and node.name == replacement_name
        ),
        None,
    )
    if forward is None:
        return None
    companion_attrs = _companion_attr_bindings(tree)
    original_forward = _original_forward_binding(tree)
    if original_forward is None:
        return None

    class ClosureBindings(ast.NodeTransformer):
        """Rewrite captured closure bindings to generated-instance state."""

        def visit_Name(self, node: ast.Name) -> ast.AST:  # pylint: disable=invalid-name
            """Rewrite a referenced symbol into its resolved source form."""
            attr = companion_attrs.get(node.id)
            if attr is None:
                return node
            return ast.copy_location(
                ast.Attribute(
                    value=ast.Name(id="self", ctx=ast.Load()),
                    attr=attr,
                    ctx=node.ctx,
457
458
459
460
461
462
463
464
465
466
467
468
469
                ),
                node,
            )

        def visit_Call(self, node: ast.Call) -> ast.AST:  # pylint: disable=invalid-name
            """Rewrite a call after recursively resolving its arguments."""
            node = self.generic_visit(node)
            if isinstance(node.func, ast.Name) and node.func.id == original_forward:
                node.func = ast.Attribute(
                    value=ast.Call(
                        func=ast.Name(id="super", ctx=ast.Load()),
                        args=[],
                        keywords=[],
470
471
472
473
474
475
476
477
478
479
480
481
                    ),
                    attr="forward",
                    ctx=ast.Load(),
                )
            return node

    body = list(forward.body)
    if (
        body
        and isinstance(body[0], ast.Expr)
        and isinstance(body[0].value, ast.Constant)
        and isinstance(body[0].value.value, str)
479
480
481
482
483
484
485
486
487
488
489
490
491
492
        and isinstance(body[0], ast.Expr)
        and isinstance(body[0].value, ast.Constant)
        and isinstance(body[0].value.value, str)
    ):
        body = body[1:]
    lowered = [ClosureBindings().visit(ast.fix_missing_locations(node)) for node in body]
    if not lowered:
        return None
    active = "\n".join(ast.unparse(node) for node in lowered)
    return (
        "if self.cp_enable:\n"
        f"{textwrap.indent(active, '    ')}\n\n"
        "return self._forward_impl(__hp_forward_args__)"
    )
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529


def _companion_attr_bindings(tree: ast.AST) -> dict[str, str]:
    """Map closure variable names to their published companion attributes."""
    result: dict[str, str] = {}
    for node in ast.walk(tree):
        if not isinstance(node, ast.keyword) or node.arg != "companion_attrs":
            continue
        if not isinstance(node.value, ast.Dict):
            continue
        for key, value in zip(node.value.keys, node.value.values):
            if (
                isinstance(key, ast.Constant)
                and isinstance(key.value, str)
                and isinstance(value, ast.Name)
            ):
                result[value.id] = key.value
    return result


def _original_forward_binding(tree: ast.AST) -> str | None:
    """Return the local name that saves ``target.forward`` before rewriting."""
    for node in ast.walk(tree):
        if not isinstance(node, ast.Assign) or len(node.targets) != 1:
            continue
        target = node.targets[0]
        value = node.value
        if (
            isinstance(target, ast.Name)
            and isinstance(value, ast.Attribute)
            and value.attr == "forward"
        ):
            return target.id
    return None


__all__ = [
hyper_parallel/codegen/inline/replacement_pass.py
58
59
60
61
62
63
64
65
66
    """

    patch_set = InlinePatchSet()
    scopes_by_target: dict[str, set[str]] = defaultdict(set)
    fqns_by_target: dict[str, set[str]] = defaultdict(set)
    specs_by_target: dict[str, Any] = {}
    for rule in rules:
        target = rule.replace_target
        if target is None:
69
70
71
72
73
74
75
76
77
        if spec is None:
            continue
        specs_by_target.setdefault(target, spec)
        scopes_by_target[target].update(_scope_attrs(rule))
        fqns_by_target[target].update(fqn for fqn in rule.match if fqn)
    for target, spec in specs_by_target.items():
        scope_attrs = tuple(sorted(scopes_by_target[target]))
        if not scope_attrs:
            logger.warning(
hyper_parallel/codegen/inline/source_plan.py
30
31
32
33
34
35
36
37
38
39
    boundary_classes: Mapping[str, str] | None,
    external_state_classes: Collection[str] | None,
) -> InlinePatchSet:
    """Build the complete ordered patch set for one modeling source."""
    replacement = build_replacement_patches(observations, model_type)
    strategy = build_strategy_patches(
        strategy_observations,
        model_type,
        boundary_classes,
        external_state_classes,
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        model_type,
        boundary_classes,
        external_state_classes,
    )
    return merge_patch_sets(replacement, strategy)


def merge_patch_sets(*patch_sets: InlinePatchSet) -> InlinePatchSet:
    """Merge pass outputs without changing their established application order."""
    merged = InlinePatchSet()
    for patch_set in patch_sets:
        merged.imports.extend(patch_set.imports)
        merged.module_snippets.extend(patch_set.module_snippets)
        merged.constructor_replaces.extend(patch_set.constructor_replaces)
        merged.forward_extracts.extend(patch_set.forward_extracts)
        merged.class_removals.extend(patch_set.class_removals)
    return merged


__all__ = ["build_source_patch_plan", "merge_patch_sets"]
hyper_parallel/codegen/inline/strategy_pass.py
36
37
38
39
40
41
42
43
44
45
46
47
48
    Per-model fact, derived (never registered): the frozen boundary map names
    each matched FQN's class, so any custom model class picked up by the
    pattern materializes without a framework-side registration.
    """
    classes: set[str] = set()
    for fqn, class_name in (boundary_classes or {}).items():
        if any(fnmatch(fqn, pattern) for pattern in rule.match):
            classes.add(class_name)
    return classes


def build_strategy_patches(
    rules: tuple[SourceObservation, ...],
78
79
80
81
82
83
84
85
86
87
    """

    patch_set = InlinePatchSet()
    emitted: dict[tuple[str, str], str] = {}
    imported: set[tuple[str, bool]] = set()
    subclassed: set[str] = set()
    for rule in rules:
        for target in (rule.local_compute_target, rule.inner_wrapper_target):
            if target is None:
                continue
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
            inner_wrapper = target == rule.inner_wrapper_target
            spec = strategy_spec(target, model_type, inner_wrapper=inner_wrapper)
            if spec is None:
                continue
            if (target, inner_wrapper) not in imported:
                imported.add((target, inner_wrapper))
                patch_set.imports.extend(spec.imports)
                patch_set.module_snippets.extend(spec.snippets)
            if spec.subclass_shell is not None:
                _materialize_cp_forward(
                    patch_set, spec, rule, target, model_type,
                    boundary_classes, subclassed,
                )
                continue
            if spec.body_template is None:
                continue
            if spec.target_class is not None:
                host_classes = {spec.target_class}
            else:
                host_classes = rule_block_classes(rule, boundary_classes)
                if not host_classes:
                    # Re-emission of an already-normalized meta: the boundary
                    # entries the first pass stripped are gone, but the pass
                    # persisted the derived classes in
                    # ``meta.external_state_classes`` — the surviving record of
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
                    # which classes an inline body replaced. (Today the EP
                    # shell is the only body-bearing strategy, so the whole
                    # record is this rule's host set; a second body-bearing
                    # strategy would need a kind filter here.)
                    host_classes = set(external_state_classes or ())
                if not host_classes:
                    raise _unmaterialized_strategy_error(
                        spec.kind, target, rule, model_type
                    )
            for host_class in sorted(host_classes):
                key = (host_class, spec.method_name)
                if key in emitted:
                    if emitted[key] != spec.body_template:
                        raise ValueError(f"Conflicting inline strategies for {host_class}.{spec.method_name}")
                    continue
                emitted[key] = spec.body_template
                patch_set.forward_extracts.append(
                    ForwardExtractPatch(
                        class_name=host_class,
                        method_name=spec.method_name,
                        body=spec.body_template,
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    boundary_classes: Mapping[str, str] | None,
    subclassed: set[str],
) -> None:
    """Materialize CP dispatch in the original host class exactly once."""
    host_classes = rule_block_classes(rule, boundary_classes)
    if not host_classes:
        raise _unmaterialized_strategy_error(spec.kind, target, rule, model_type)
    helper_names = [name for patch in spec.imports for name in patch.names]
    if len(helper_names) > 1:
        raise ValueError(
            f"codegen: CP strategy {target!r} carries multiple helper "
            f"imports for one subclass shell: {helper_names}"
        )
    for host_class in sorted(host_classes):
        if host_class in subclassed:
            continue
        subclassed.add(host_class)
        patch_set.forward_extracts.append(
            ForwardExtractPatch(
                class_name=host_class,
                method_name=spec.method_name,
                body=spec.subclass_shell,
172
173
174
175
176
177
178
179
180
def _unmaterialized_strategy_error(
    kind: str, target: str, rule: SourceObservation, model_type: str | None
) -> RuntimeError:
    """The error for a declared strategy that selects no boundary module."""
    return RuntimeError(
        f"codegen: {kind} strategy {target!r} matched no boundary "
        f"module (patterns {list(rule.match)}, model_type="
        f"{model_type!r}) — the declared strategy cannot be "
        "materialized; check the match pattern or the plan's "
hyper_parallel/codegen/inline/targets.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


def projected_target_path(value: Any) -> str | None:
    """Return the dotted target represented by a frozen observation value."""
    if value is None:
        return None
    if isinstance(value, str):
        return value or None
    if isinstance(value, Mapping):
        target = value.get("_target_") or value.get("target")
        return str(target) if target else None
    module = getattr(value, "__module__", None)
    qualname = getattr(value, "__qualname__", None)
    return f"{module}.{qualname}" if module and qualname else None


def import_target_path(value: Any) -> str:
    """Return an importable ``module:qualname`` or reject an invalid target."""
    target = projected_target_path(value)
    if not target:
        raise ValueError(f"snapshot injection target is not importable: {target!r}")
    if ":" in target:
        return target
    module_name, separator, name = target.rpartition(".")
    if not separator or not module_name or not name:
        raise ValueError(f"snapshot injection target is not importable: {target!r}")
    return f"{module_name}:{name}"


__all__ = ["import_target_path", "projected_target_path"]
hyper_parallel/codegen/manager.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    construction, offline meshes, replacement matching, and planner calls.
    Distributed launchers must use a dedicated sidecar instead of invoking
    this function independently on every training rank.
    """
    if not isinstance(snapshot, CodegenSnapshot):
        raise TypeError("snapshot must be a CodegenSnapshot")
    if snapshot.status not in (CodegenStatus.DERIVED, CodegenStatus.NOT_REQUIRED):
        raise ValueError(
            "snapshot-driven generation requires status 'derived' or 'not_required', "
            f"got {snapshot.status.value!r}"
        )
    source = snapshot.source
    if not source.get("file_path"):
        raise ValueError("snapshot source must include a modeling file_path")
    model_type = source.get("model_type")
    public_model_type = artifact_model_type(str(model_type)) if model_type else None
    model_name = (
        _model_type_to_modeling_token(str(public_model_type))
        if model_type
        else source.get("architecture")
    )
    if artifact_dir is None and source.get("model_type"):
        layout = resolve_model_artifact_layout(
            family_dir_for_model_type(str(source["model_type"])),
            model_name=str(model_name) if model_name else None,
        )
    else:
        layout = resolve_default_artifact_layout(
            artifact_dir,
            model_name=str(model_name) if model_name else None,
        )
    ensure_artifact_dir(layout)
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
            model_name=str(model_name) if model_name else None,
        )
    ensure_artifact_dir(layout)

    with artifact_generation_lock(layout, timeout_s=timeout_s):
        clean_temp_artifacts(layout)
        logger.info("codegen: generating artifact %s", layout.artifact_dir)
        context = _build_emission_context(snapshot)
        files = emit_bundle(layout, context)
        write_bundle_atomic(layout, files)
    return layout


def _build_emission_context(snapshot: CodegenSnapshot) -> EmissionContext:
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118


def _build_emission_context(snapshot: CodegenSnapshot) -> EmissionContext:
    """Map one immutable observation into an in-memory emission context."""
    payload = snapshot._emission_view()  # pylint: disable=protected-access
    source = dict(payload["source"])
    status = snapshot.status
    frozen_plan = dict(payload["plan"] or {})
    module_overrides = list(replacement_records_from_snapshot(payload))
    is_derived = status is CodegenStatus.DERIVED
    emission_patches = (
        emission_patches_from_snapshot_payload(payload) if is_derived else {}
    )
    return EmissionContext(
        source=source,
        parallel_dims=dict(frozen_plan.get("parallel_dims") or {}),
        status=payload["status"],
        injections=list(frozen_plan.get("injections") or ()),
hyper_parallel/codegen/model_build_overlay.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


def _prepare_model_source_compat(hf_config: Any) -> None:
    """Run an optional model-owned compatibility hook before construction."""
    model_type = getattr(hf_config, "model_type", None)
    adapter = get_model_adapter(model_type) if model_type else None
    loader = getattr(adapter, "codegen", None) if adapter is not None else None
    provider = loader() if loader is not None else None
    prepare = getattr(provider, "prepare_model_import", None)
    if prepare is not None:
        prepare()


@dataclass
class CodegenModelBuildResult:
57
58
59
60
61
62
63
64
65
66
67
68

@contextmanager
def _codegen_model_init_context() -> Iterator[None]:
    """Build codegen's observation model with parameters and buffers on meta."""
    with init_empty_weights(include_buffers=True), auto_model._model_init_context(  # pylint: disable=protected-access
        True
    ):
        yield


def _record_source(
    session: CodegenBuildSession,
71
72
73
74
75
76
77
78
79
80
81
82
83
    pretrained_model_name_or_path: Any,
    is_hf_model: bool,
) -> None:
    """Record deterministic source identity from the constructed meta model."""
    model_type = type(model)
    source_path = inspect.getsourcefile(model_type)
    architectures = list(getattr(hf_config, "architectures", None) or ())
    architecture = architectures[0] if architectures else model_type.__name__
    session.record_source({
        "model_type": getattr(hf_config, "model_type", None),
        "architecture": architecture,
        "architectures": architectures,
        "model_class": f"{model_type.__module__}:{model_type.__qualname__}",
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
    load_base_model: bool,
    session: CodegenBuildSession,
) -> Any:
    """Apply native replacement declarations while recording their result."""
    weights_mapping = get_model_conversion_mapping(model)
    specs = tuple(getattr(distributed_setup, "module_replacements", None) or ())
    try:
        with _observe_replacement_installations(model, specs) as applications:
            model, _ = replacement_api._apply_module_replacement_actions(  # pylint: disable=protected-access
                model,
                specs,
                weights_mapping=weights_mapping,
                context=model_builder._build_replacement_context(  # pylint: disable=protected-access
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                    low_precision_config,
                ),
                capture_checkpoint_metadata=load_base_model,
            )
    except replacement_api.ModuleReplacementMatchError as exc:
        session.record_replacements(
            declarations=replacement_api.describe_replacement_declarations(specs),
            applications=(),
            unmatched=exc.unmatched,
        )
        raise
    session.record_replacements(
        declarations=replacement_api.describe_replacement_declarations(specs),
        applications=applications,
        unmatched=(),
    )
    return model


@contextmanager
def _observe_replacement_installations(
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    model: Any,
    specs: tuple[Any, ...],
) -> Iterator[list[dict[str, Any]]]:
    """Observe native replacement installation without changing its API."""
    applications: list[dict[str, Any]] = []
    spec_indexes = {id(spec): index for index, spec in enumerate(specs)}
    original_install = replacement_api._install_replacement_target  # pylint: disable=protected-access

    def _observe_install(*args: Any, **kwargs: Any) -> None:
        """Record the source and result of the one native installation call."""
        target = args[1]
        transforms = args[3]
        transform_count = len(transforms)
        source_type = _symbol_path(type(target.source))
        original_install(*args, **kwargs)
        replacement = model.get_submodule(target.module_fqns[0])
        applications.append({
            "rule_id": replacement_api._replacement_rule_id(  # pylint: disable=protected-access
                target.spec,
                spec_indexes.get(id(target.spec), 0),
            ),
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                for transform in transforms[transform_count:]
            ],
        })

    replacement_api._install_replacement_target = _observe_install  # pylint: disable=protected-access
    try:
        yield applications
    finally:
        replacement_api._install_replacement_target = original_install  # pylint: disable=protected-access


def _symbol_path(symbol: Any) -> str:
    """Return a stable import path for an observed runtime type."""
    return f"{symbol.__module__}:{symbol.__qualname__}"


def _observe_plan(
    model: Any,
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    is_hf_model: bool,
    session: CodegenBuildSession,
) -> None:
    """Derive a native plan without applying distributed runtime operations."""
    model_sharding_requested = (
        mesh is not None
        and any(size > 1 for size in (mesh.tp_size, mesh.cp_size, mesh.ep_size))
    )
    if sharding_planner is None:
        session.mark_not_required("planner_unavailable")
        return
    if mesh is None:
        session.mark_not_required("mesh_unavailable")
        return
    if mesh.device_mesh is None:
        session.mark_not_required("device_mesh_unavailable")
        return
    if is_hf_model and not model_sharding_requested:
        if session.has_replacement_applications:
            _record_replacement_only_plan(mesh, session)
        else:
            session.mark_not_required("pure_data_parallel")
        return
    plan = sharding_planner.plan(
        model,
        mesh.device_mesh,
        tp_size=mesh.tp_size,
        cp_size=mesh.cp_size,
207
208
209
210
211
212
213
214
215
216
217
        ep_size=mesh.ep_size,
        sequence_parallel=mesh.sequence_parallel,
        loss_parallel=mesh.loss_parallel,
    )
    active_mesh = _get_active_mesh(mesh.device_mesh, plan.mesh_dim_names)
    _preflight_compute_injection(plan, active_mesh, model=model)
    session.observe_plan(plan, model, mesh)


def _record_replacement_only_plan(
    mesh: Any,
217
218
219
220
221
222
223
224
225
    mesh: Any,
    session: CodegenBuildSession,
) -> None:
    """Derive an emitter snapshot when replacement is the only source patch."""
    session.record_plan({
        "param_plan": {},
        "boundary_classes": {},
        "boundary_types": {},
        "nested_components": [],
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    artifact_dir: Optional[str],
    result: CodegenModelBuildResult,
) -> Any:
    """Publish an offline snapshot while preserving host RNG state."""
    cpu_rng_state = torch.get_rng_state()
    python_rng_state = random.getstate()

    def _publish(snapshot: Any) -> None:
        """Publish one snapshot and restore RNG state afterward."""
        try:
            result.artifact_layout = ensure_codegen_artifact_from_snapshot(snapshot, artifact_dir)
        finally:
            torch.set_rng_state(cpu_rng_state)
            random.setstate(python_rng_state)

    return session.finish(_publish)


def _codegen_build_model(session: CodegenBuildSession, artifact_dir: Optional[str], result: CodegenModelBuildResult):
    """Create the classmethod used only while the CLI constructs a meta model."""
    def _build_model(
        cls,
        pretrained_model_name_or_path,
        *model_args,
        is_hf_model,
288
289
290
291
292
293
294
295
296
        model_init_dtype=None,
        **kwargs,
    ) -> Any:
        """Construct and observe a meta model without runtime infrastructure."""
        del (
            fsdp2_manager,
            validate_placement,
            compile_config,
            activation_checkpoint,
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
            allow_cache_entry_mutation,
            activation_swap,
            model_init_dtype,
        )
        init_kwargs = auto_model._model_init_kwargs(  # pylint: disable=protected-access
            dict(kwargs),
            load_base_model=False,
        )
        if "trust_remote_code" in kwargs:
            init_kwargs["trust_remote_code"] = kwargs["trust_remote_code"]
        _prepare_model_source_compat(hf_config)
        with _codegen_model_init_context():
            _, model = auto_model._init_model(  # pylint: disable=protected-access
                cls,
                None,
                hf_config,
                attn_implementation,
317
318
319
320
321
322
323
324
325
                is_hf_model,
                *model_args,
                **init_kwargs,
            )
        _record_source(
            session,
            model,
            hf_config,
            pretrained_model_name_or_path,
324
325
326
327
328
329
330
331
332
333
334
335
336
337
            hf_config,
            pretrained_model_name_or_path,
            is_hf_model,
        )
        model_builder._apply_pre_sharding_features(  # pylint: disable=protected-access
            peft_config,
            qat_config,
            fp8_config,
        )
        model = _observe_replacements(
            model,
            distributed_setup,
            getattr(distributed_setup, "low_precision_config", None),
            load_base_model,
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
            getattr(distributed_setup, "low_precision_config", None),
            load_base_model,
            session,
        )
        if freeze_config is not None:
            model_builder._apply_parameter_freezing(  # pylint: disable=protected-access
                model,
                freeze_config,
            )
        method_context = model_builder._build_replacement_context(  # pylint: disable=protected-access
            distributed_setup,
            getattr(distributed_setup, "low_precision_config", None),
        )
        method_context["mesh_context"] = getattr(distributed_setup, "mesh_context", None)
        model_builder.apply_method_replacements(
            model,
            getattr(distributed_setup, "method_replacements", None) or (),
            context=method_context,
        )
        _observe_plan(model, mesh, sharding_planner, is_hf_model, session)
        snapshot = _publish_session(session, artifact_dir, result)
        result.snapshot = snapshot
        model._hp_codegen_snapshot = snapshot  # pylint: disable=protected-access
        model.train()
        return model

    return _build_model


@contextmanager
def use_codegen_model_build_overlay(
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    session: CodegenBuildSession,
    artifact_dir: Optional[str] = None,
) -> Iterator[CodegenModelBuildResult]:
    """Temporarily replace AutoModel's final build step for offline codegen."""
    model_class = auto_model._BaseHyperAutoModelClass  # pylint: disable=protected-access
    original = model_class.__dict__["_build_model"]
    result = CodegenModelBuildResult()
    model_class._build_model = classmethod(  # pylint: disable=protected-access
        _codegen_build_model(session, artifact_dir, result)
    )
    try:
        yield result
    finally:
        model_class._build_model = original  # pylint: disable=protected-access


__all__ = ["CodegenModelBuildResult", "use_codegen_model_build_overlay"]
hyper_parallel/codegen/plan/boundary_forms.py
142
143
144
145
146
147
148
149
150
        if inner_wrap or _entry_has_ep_placement(entry):
            return BoundaryForm(form=FORM_GENERIC)
        return BoundaryForm(form=FORM_IDENTITY)

    return _classify_active_axes(entry, axes, is_region=is_region, inner_wrap=inner_wrap)


def _classify_active_axes(
    entry: dict[str, Any],
hyper_parallel/codegen/plan/observation.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    mesh_dim_names: tuple[str, ...] = ()

    def to_dict(self) -> dict[str, Any]:
        """Return the mutable mapping consumed by the session boundary."""
        return {item.name: getattr(self, item.name) for item in fields(self)}


def observe_emission_plan(plan: Any, model: Any) -> EmissionPlanObservation:
    """Project a native sharding plan without changing or applying it."""
    if plan is None:
        return EmissionPlanObservation()
    return EmissionPlanObservation(
        param_plan=_observe_boundary_plan(plan),
        injections=_observe_injections(plan),
        boundary_classes=_observe_boundary_classes(plan, model),
        boundary_types=_observe_boundary_types(plan, model),
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104


def _observe_boundary_plan(plan: Any) -> dict[str, dict[str, Any]]:
    """Collect boundary layout contracts required for source emission."""
    result: dict[str, dict[str, Any]] = {}
    for fqn, spec in (getattr(plan, "modules", {}) or {}).items():
        entry: dict[str, Any] = {}
        for name in ("in_src", "in_dst", "out_src", "out_dst"):
            value = getattr(spec, name, None)
            if value is not None:
                entry[name] = named_placement_to_dict(value)
        for name in ("out_names", "_deferred_bias_params"):
            value = getattr(spec, name, None)
            if value:
                key = "deferred_bias_params" if name.startswith("_") else name
                entry[key] = list(value)
        for name in ("is_boundary", "region_dispatch"):
            value = getattr(spec, name, None)
            if value is not None:
                entry[name] = value
        result[fqn] = entry
    return result


def _observe_injections(plan: Any) -> list[dict[str, Any]]:
    """Collect compute and wrapper injections in stable module-path order."""
    result = []
    for fqn, spec in sorted((getattr(plan, "modules", {}) or {}).items()):
        entry: dict[str, Any] = {}
        for name in ("inner_wrapper", "inner_out_src", "local_compute_fn"):
            value = getattr(spec, name, None)
            if value is not None:
                entry[name] = value_to_data(value)
        inner_target = getattr(spec, "inner_target", None)
        if inner_target is not None:
            entry["inner_target"] = inner_target
        if entry:
            result.append({"match": fqn, **entry})
    return result


def _observe_boundary_classes(plan: Any, model: Any) -> dict[str, str]:
    """Map planned module paths to their source class names."""
    if model is None:
        return {}
    return {
        fqn: type(model.get_submodule(fqn)).__name__
        for fqn in (getattr(plan, "modules", {}) or {})
    }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151


def _observe_boundary_types(plan: Any, model: Any) -> dict[str, str]:
    """Map planned module paths to fully qualified runtime type identities."""
    if model is None:
        return {}
    result = {}
    for fqn in (getattr(plan, "modules", {}) or {}):
        module_type = type(model.get_submodule(fqn))
        result[fqn] = f"{module_type.__module__}:{module_type.__qualname__}"
    return result


def _observe_nested_components(plan: Any, model: Any) -> list[dict[str, str]]:
    """Identify nested model source owners needed for source composition."""
    if model is None:
        return []
    root_module = type(model).__module__
    components: dict[tuple[str, str], dict[str, str]] = {}
    for boundary_fqn in (getattr(plan, "modules", {}) or {}):
        owner_module = type(model.get_submodule(boundary_fqn)).__module__
        if (
            owner_module == root_module
            or owner_module.startswith("torch.")
            or owner_module.startswith("hyper_parallel.")
        ):
            continue
        candidates = []
        parts = boundary_fqn.split(".")
        for index in range(1, len(parts) + 1):
            ancestor_fqn = ".".join(parts[:index])
            ancestor_type = type(model.get_submodule(ancestor_fqn))
            if ancestor_type.__module__ == owner_module:
                candidates.append((ancestor_fqn, ancestor_type))
        if not candidates:
            continue
        component_fqn, component_type = candidates[0]
        identity = f"{component_type.__module__}:{component_type.__qualname__}"
        components[(component_fqn, identity)] = {
            "fqn": component_fqn,
            "type": identity,
            "owner_module": owner_module,
        }
    return [components[key] for key in sorted(components)]


__all__ = ["EmissionPlanObservation", "observe_emission_plan"]
hyper_parallel/codegen/plan/placements.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47


def placement_to_string(placement: Any) -> str:
    """Serialize one placement to its canonical source representation."""
    if isinstance(placement, StridedShard):
        if placement.uneven_shard:
            raise TypeError("cannot observe StridedShard(uneven_shard=True)")
        return f"SS({placement.dim},{placement.split_factor})"
    if isinstance(placement, Shard):
        if placement.uneven_shard:
            raise TypeError("cannot observe Shard(uneven_shard=True)")
        return f"S({placement.dim})"
    if isinstance(placement, Replicate):
        return "R"
    if isinstance(placement, Partial):
        return f"P({placement.reduce_op})"
    raise TypeError(
        f"cannot serialize placement {placement!r} of unknown type "
        f"{type(placement).__name__}"
    )
48
49
50
51
52
53
54
55
56
57


def named_placement_to_dict(named: Any) -> dict[str, Any]:
    """Serialize a nested named-placement contract to plain dictionaries."""
    if not named:
        return {}
    return {
        str(getattr(name, "value", name)): _value_to_data(value)
        for name, value in named.items()
    }
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142


def parse_placement(text: str) -> Any:
    """Parse a canonical placement string."""
    if not isinstance(text, str) or not text:
        raise ValueError(f"cannot parse placement from {text!r}")
    if text == "R":
        return Replicate()
    match = re.fullmatch(r"([SP]|SS)\((.*)\)", text)
    if match is None:
        raise ValueError(
            f"unknown placement form {text!r} (expected S(dim) / R / "
            "P(reduce_op) / SS(dim,factor))"
        )
    kind, arguments = match.groups()
    if kind == "P":
        if not arguments:
            raise ValueError(f"malformed partial placement {text!r}: missing reduce_op")
        return Partial(arguments)
    parts = [part.strip() for part in arguments.split(",")]
    try:
        dim = int(parts[0])
    except (IndexError, ValueError) as exc:
        raise ValueError(f"malformed shard placement {text!r}: invalid dim") from exc
    if kind == "S":
        if len(parts) != 1:
            raise ValueError(f"malformed shard placement {text!r}: too many args")
        return Shard(dim)
    if len(parts) != 2:
        raise ValueError(f"malformed strided shard placement {text!r}: expected SS(dim,factor)")
    try:
        split_factor = int(parts[1])
    except ValueError as exc:
        raise ValueError(
            f"malformed strided shard placement {text!r}: invalid split_factor"
        ) from exc
    return StridedShard(dim, split_factor)


def parse_named_placement(data: dict[str, Any]) -> Any:
    """Parse a nested named-placement contract from plain dictionaries."""
    if not data:
        return {}
    return _value_from_data(data)


def value_to_data(value: Any) -> Any:
    """Project a planner value to deterministic source-emission data."""
    return _value_to_data(value)


def _value_to_data(value: Any) -> Any:
    """Recursively project placements and planner values into plain data."""
    if isinstance(value, (Partial, Replicate, Shard, StridedShard)):
        return placement_to_string(value)
    if hasattr(value, "to_dict"):
        return value.to_dict()
    if isinstance(value, dict):
        return {
            str(getattr(key, "value", key)): _value_to_data(item)
            for key, item in value.items()
        }
    if isinstance(value, (list, tuple)):
        return [_value_to_data(item) for item in value]
    if callable(value):
        return getattr(value, "__name__", str(value))
    return value


def _value_from_data(value: Any) -> Any:
    """Recursively reconstruct placements from nested plain-data contracts."""
    if isinstance(value, str):
        return parse_placement(value)
    if isinstance(value, dict):
        return {
            str(getattr(key, "value", key)): _value_from_data(item)
            for key, item in value.items()
        }
    if isinstance(value, list):
        return [_value_from_data(item) for item in value]
    return value


__all__ = [
    "named_placement_to_dict",
hyper_parallel/codegen/planner_overlay.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49


def _override_declared_params(plan_overrides: dict[str, Any], model: Any) -> set[str]:
    """Return parameter FQNs already owned by exact or glob overrides."""
    module_names = tuple(name for name, _ in model.named_modules())
    declared = set()
    for key, spec in plan_overrides.items():
        matches = (
            (name for name in module_names if fnmatchcase(name, key))
            if any(char in key for char in "*?[")
            else (key,)
        )
        for module_fqn in matches:
            prefix = f"{module_fqn}." if module_fqn else ""
            declared.update(prefix + name for name in (spec.params or {}))
    return declared


class CodegenShardingPlanner(ShardingPlanner):
    """Add audit-only coverage without changing the Trainer's native planner."""
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    """Add audit-only coverage without changing the Trainer's native planner."""

    def _infer_boundary_type(self, fqn: str, group: Any) -> str:
        """Let Megatron-style projection leaves merge into their MLP owner."""
        if fqn.rsplit(".", 1)[-1].lower() in _CODEGEN_LEAF_SEGMENTS:
            return "unknown"
        return super()._infer_boundary_type(fqn, group)

    def _group_by_boundary(self, param_roles: dict[str, ParamRole]) -> dict[str, list[tuple[str, ParamRole]]]:
        """Keep unknown root leftovers attached to each real parameter owner."""
        groups = super()._group_by_boundary(param_roles)
        normalized: dict[str, list[tuple[str, ParamRole]]] = {}
        for module_fqn, group in groups.items():
            if self._infer_boundary_type(module_fqn, group) != "unknown":
                normalized[module_fqn] = group
                continue
            for param_fqn, role in group:
                owner = param_fqn.rsplit(".", 1)[0]
                normalized.setdefault(owner, []).append((param_fqn, role))
        return normalized

    def _derive_boundary_specs(self, plan: Any, boundary_groups: Any, **kwargs: Any) -> None:
        """Append explicit replicated audit entries after native derivation."""
        super()._derive_boundary_specs(plan, boundary_groups, **kwargs)
        if not self._derive:
            return
        model = kwargs["model"]
        parameters_by_name = dict(model.named_parameters())
        override_declared = _override_declared_params(self._plan_overrides, model)
        for boundary_fqn, group in boundary_groups.items():
            if boundary_fqn in plan.modules:
                continue
            fallback = self._build_replicated_fallback_spec(
                boundary_fqn,
                group,
                parameters_by_name,
                override_declared,
83
84
85
86
87
88
89
90
91
92
                group,
                parameters_by_name,
                override_declared,
            )
            if fallback is not None:
                plan.modules[boundary_fqn] = fallback

    @staticmethod
    def _build_replicated_fallback_spec(
        boundary_fqn: str,
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        parameters_by_name: dict[str, Any],
        override_declared: set[str],
    ) -> Optional[ModuleShardingSpec]:
        """Declare communication-free metadata for replicated leftovers."""
        replicable = (ParamRole.REPLICATED, ParamRole.NORM, ParamRole.BIAS)
        if any(role not in replicable for _, role in group):
            return None
        prefix = f"{boundary_fqn}." if boundary_fqn else ""
        params: dict[str, NamedPlacement] = {}
        for param_fqn, _ in group:
            parameter = parameters_by_name.get(param_fqn)
            if parameter is None or not parameter.requires_grad or param_fqn in override_declared:
                continue
            relative = param_fqn[len(prefix):] if prefix else param_fqn
            params[relative] = _multi_dim(tp=Replicate(), cp=Replicate(), ep=Replicate())
        if not params:
            return None
        return ModuleShardingSpec(params=params, in_src={}, in_dst={}, is_boundary=False)


@contextmanager
def use_codegen_planner_overlay() -> Iterator[None]:
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

@contextmanager
def use_codegen_planner_overlay() -> Iterator[None]:
    """Scope planner observation and suppress FSDP runtime construction."""
    original = auto_model.instantiate_infrastructure
    original_fsdp = model_builder._instantiate_fsdp2  # pylint: disable=protected-access

    def skip_fsdp_runtime(*_args: Any, **_kwargs: Any) -> None:
        """Keep strategy configuration while suppressing device execution."""
        return None

    def instantiate_infrastructure(*args: Any, **kwargs: Any) -> Any:
        """Upgrade only an exact native planner created inside this context."""
        planner, fsdp_manager = original(*args, **kwargs)
        if type(planner) is ShardingPlanner:  # pylint: disable=unidiomatic-typecheck
            planner.__class__ = CodegenShardingPlanner
        return planner, fsdp_manager

    auto_model.instantiate_infrastructure = instantiate_infrastructure
    model_builder._instantiate_fsdp2 = skip_fsdp_runtime  # pylint: disable=protected-access
    try:
        yield
    finally:
        auto_model.instantiate_infrastructure = original
        model_builder._instantiate_fsdp2 = original_fsdp  # pylint: disable=protected-access


__all__ = ["CodegenShardingPlanner", "use_codegen_planner_overlay"]
hyper_parallel/codegen/session.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    """Collect native build observations without influencing the build."""

    def __init__(self) -> None:
        """Initialize an empty collection session."""
        self._status = CodegenStatus.COLLECTING
        self._source: dict[str, Any] = {}
        self._replacements = ReplacementSnapshot()
        self._plan: Optional[dict[str, Any]] = None
        self._diagnostics: dict[str, Any] = {}
        self._reason: Optional[str] = None
        self._failure: Optional[FailureSnapshot] = None
        self._source_recorded = False
        self._replacements_recorded = False
        self._diagnostics_recorded = False
        self._finished = False
        self._snapshot: Optional[CodegenSnapshot] = None

    @property
    def status(self) -> CodegenStatus:
        """Return the current session status."""
        return self._status

    @property
    def has_replacement_applications(self) -> bool:
        """Whether native model construction installed any replacement."""
        return bool(self._replacements.applications)

    def record_source(self, source: Mapping[str, Any]) -> None:
        """Record source and model identity once while collecting."""
        self._require_open(CodegenStatus.COLLECTING)
        if self._source_recorded:
            raise RuntimeError("codegen source has already been recorded")
        self._source = self._as_mapping(source, "source")
        self._source_recorded = True

    def record_replacements(
        self,
        *,
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
        applications: Sequence[Mapping[str, Any]] = (),
        unmatched: Sequence[Mapping[str, Any]] = (),
    ) -> None:
        """Record native replacement declarations and observed applications."""
        self._require_open(CodegenStatus.COLLECTING)
        if self._replacements_recorded:
            raise RuntimeError("codegen replacements have already been recorded")
        self._replacements = ReplacementSnapshot.from_data(
            declarations=declarations,
            applications=applications,
            unmatched=unmatched,
        )
        self._replacements_recorded = True

    def record_plan(self, plan: Mapping[str, Any]) -> None:
        """Record the native planner result and enter the derived state."""
        self._require_open(CodegenStatus.COLLECTING)
        if not self._source_recorded:
            raise RuntimeError("record_source() must run before record_plan()")
        self._plan = self._as_mapping(plan, "plan")
        self._status = CodegenStatus.DERIVED

    def observe_plan(self, plan: Any, model: Any, mesh: Any) -> None:
        """Observe a native planner result without exposing codegen to the builder."""
        observed_plan = observe_emission_plan(plan, model).to_dict()
        observed_plan["parallel_dims"] = {
            "dp_size": getattr(mesh, "dp_size", 1),
            "tp_size": mesh.tp_size,
            "cp_size": mesh.cp_size,
            "pp_size": getattr(mesh, "pp_size", 1),
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
            "ep_size": mesh.ep_size,
            "sequence_parallel": mesh.sequence_parallel,
            "loss_parallel": mesh.loss_parallel,
        }
        self.record_plan(observed_plan)

    def record_diagnostics(self, diagnostics: Mapping[str, Any]) -> None:
        """Record optional counters and audit facts once."""
        self._require_open()
        if self._diagnostics_recorded:
            raise RuntimeError("codegen diagnostics have already been recorded")
        self._diagnostics = self._as_mapping(diagnostics, "diagnostics")
        self._diagnostics_recorded = True

    def mark_not_required(self, reason: str) -> None:
        """Finish collection when the native build has no sharding plan."""
        self._require_open(CodegenStatus.COLLECTING)
        if not self._source_recorded:
            raise RuntimeError("record_source() must run before mark_not_required()")
        self._reason = self._require_reason(reason)
        self._status = CodegenStatus.NOT_REQUIRED

    def mark_generating(self) -> None:
        """Mark the future side-effecting generation boundary."""
        self._require_open(CodegenStatus.DERIVED)
        self._status = CodegenStatus.GENERATING

    def mark_generated(self) -> None:
        """Mark successful generation."""
        self._require_open(CodegenStatus.GENERATING)
        self._status = CodegenStatus.GENERATED

    def mark_skipped(self, reason: str) -> None:
        """Mark generation as intentionally skipped with a stable reason."""
        self._require_open(CodegenStatus.COLLECTING, CodegenStatus.DERIVED)
        if not self._source_recorded:
            raise RuntimeError("record_source() must run before mark_skipped()")
        self._reason = self._require_reason(reason)
        self._status = CodegenStatus.SKIPPED

    def mark_failed(
        self,
        stage: str,
145
146
147
148
149
150
151
152
153
        reason: str,
        details: Optional[Mapping[str, Any]] = None,
    ) -> None:
        """Record a codegen-owned failure without retaining an exception."""
        self._require_open(
            CodegenStatus.COLLECTING,
            CodegenStatus.DERIVED,
            CodegenStatus.GENERATING,
            CodegenStatus.NOT_REQUIRED,
151
152
153
154
155
156
157
158
159
160
            CodegenStatus.DERIVED,
            CodegenStatus.GENERATING,
            CodegenStatus.NOT_REQUIRED,
        )
        self._failure = FailureSnapshot.from_data(stage, reason, details)
        self._status = CodegenStatus.FAILED

    def finish(
        self,
        generator: Optional[Callable[[CodegenSnapshot], Any]] = None,
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

        Raises:
            RuntimeError: If collection or generation is incomplete.
        """
        if self._finished:
            if self._snapshot is None:
                raise RuntimeError("finished codegen session has no snapshot")
            return self._snapshot
        if self._status == CodegenStatus.COLLECTING:
            raise RuntimeError("codegen session is still collecting")
        if self._status == CodegenStatus.GENERATING:
            raise RuntimeError("codegen session is still generating")
        if self._status == CodegenStatus.DERIVED and generator is not None:
            publish_snapshot = self._build_snapshot(CodegenStatus.DERIVED)
            self.mark_generating()
            try:
                generator(publish_snapshot)
            except Exception as exc:  # pylint: disable=broad-exception-caught
                # Generation must not invalidate an already successful native build.
                self.mark_failed(
                    "generation",
                    str(exc) or type(exc).__name__,
                    {"exception_type": type(exc).__name__},
                )
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                    str(exc) or type(exc).__name__,
                    {"exception_type": type(exc).__name__},
                )
            else:
                self.mark_generated()
        elif self._status == CodegenStatus.NOT_REQUIRED and generator is not None:
            publish_snapshot = self._build_snapshot(CodegenStatus.NOT_REQUIRED)
            try:
                generator(publish_snapshot)
            except Exception as exc:  # pylint: disable=broad-exception-caught
                # Publication must not invalidate an already successful native build.
                self.mark_failed(
                    "generation",
                    str(exc) or type(exc).__name__,
                    {"exception_type": type(exc).__name__},
                )
        self._snapshot = self._build_snapshot(self._status)
        self._finished = True
        return self._snapshot

    def _build_snapshot(self, status: CodegenStatus) -> CodegenSnapshot:
        """Build an immutable view without sealing the session."""
        return CodegenSnapshot._from_owned_data(  # pylint: disable=protected-access
            status=status,
            source=self._source,
            replacements=self._replacements,
            plan=self._plan,
224
225
226
227
228
229
230
231
232
233
234
235
236
            failure=self._failure,
        )

    def _require_open(self, *statuses: CodegenStatus) -> None:
        if self._finished:
            raise RuntimeError("codegen session is already finished")
        if statuses and self._status not in statuses:
            expected = ", ".join(status.value for status in statuses)
            raise RuntimeError(
                f"codegen session is {self._status.value}; expected one of: {expected}"
            )

    @staticmethod
234
235
236
237
238
239
240
241
242
243
244
245
246
247
            )

    @staticmethod
    def _as_mapping(value: Mapping[str, Any], path: str) -> dict[str, Any]:
        return copy_mapping(value, path)

    @staticmethod
    def _require_reason(reason: str) -> str:
        if not isinstance(reason, str) or not reason.strip():
            raise ValueError("codegen reason must be a non-empty string")
        return reason


__all__ = ["CodegenBuildSession"]
hyper_parallel/codegen/snapshot.py
36
37
38
39
40
41
42
43
44
45
46
47
48


def copy_mapping(value: Mapping[str, Any], path: str) -> dict[str, Any]:
    """Return a deep copy of a string-keyed observation mapping."""
    if not isinstance(value, Mapping):
        raise TypeError(f"{path} must be a mapping")
    if not all(isinstance(key, str) for key in value):
        raise ValueError(f"{path} mapping keys must be strings")
    return deepcopy(dict(value))


def _copy_records(
    values: Sequence[Mapping[str, Any]], path: str
47
48
49
50
51
52
53
54
def _copy_records(
    values: Sequence[Mapping[str, Any]], path: str
) -> tuple[dict[str, Any], ...]:
    """Copy a sequence of observation records."""
    return tuple(
        copy_mapping(item, f"{path}[{index}]")
        for index, item in enumerate(values)
    )
70
71
72
73
74
75
76
77
78
        applications: Sequence[Mapping[str, Any]] = (),
        unmatched: Sequence[Mapping[str, Any]] = (),
    ) -> "ReplacementSnapshot":
        """Copy replacement observations into the session boundary."""
        return cls(
            declarations=_copy_records(
                declarations, "replacements.declarations"
            ),
            applications=_copy_records(
82
83
84
85
86
87
88
89
90
        )

    def to_dict(self) -> dict[str, Any]:
        """Return an isolated mutable representation for emission."""
        return deepcopy(
            {
                "declarations": list(self.declarations),
                "applications": list(self.applications),
                "unmatched": list(self.unmatched),
100
101
102
103
104
105
106
107
108
109
110
111
112
    reason: str
    details: dict[str, Any] = field(default_factory=dict)

    def __post_init__(self) -> None:
        if not isinstance(self.stage, str) or not self.stage:
            raise ValueError("failure stage must be a non-empty string")
        if not isinstance(self.reason, str) or not self.reason:
            raise ValueError("failure reason must be a non-empty string")
        object.__setattr__(
            self,
            "details",
            copy_mapping(self.details, "failure.details"),
        )
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        reason: str,
        details: Optional[Mapping[str, Any]] = None,
    ) -> "FailureSnapshot":
        """Build an isolated failure record."""
        if not stage or not reason:
            raise ValueError("failure stage and reason must be non-empty")
        return cls(stage, reason, details or {})

    def to_dict(self) -> dict[str, Any]:
        """Return an isolated mutable representation for emission."""
        return {
            "stage": self.stage,
            "reason": self.reason,
            "details": deepcopy(self.details),
        }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    reason: Optional[str] = None
    failure: Optional[FailureSnapshot] = None

    def __post_init__(self) -> None:
        if not isinstance(self.status, CodegenStatus):
            raise TypeError("snapshot status must be a CodegenStatus")
        if not isinstance(self.replacements, ReplacementSnapshot):
            raise TypeError("snapshot replacements must be a ReplacementSnapshot")
        if self.failure is not None and not isinstance(self.failure, FailureSnapshot):
            raise TypeError("snapshot failure must be a FailureSnapshot or None")
        object.__setattr__(self, "source", copy_mapping(self.source, "snapshot.source"))
        object.__setattr__(
            self,
            "diagnostics",
            copy_mapping(self.diagnostics, "snapshot.diagnostics"),
        )
        for name in ("plan",):
            value = getattr(self, name)
            if value is not None:
                object.__setattr__(
                    self,
                    name,
                    copy_mapping(value, f"snapshot.{name}"),
                )
167
168
169
170
171
172
173
174
175
176
177
178
179
                )

    def to_dict(self) -> dict[str, Any]:
        """Return isolated mutable containers for the emission boundary."""
        return deepcopy(self._emission_view())

    def _emission_view(self) -> dict[str, Any]:
        """Borrow observation data internally; callers must copy before mutation."""
        return {
            "status": self.status.value,
            "source": self.source,
            "replacements": {
                "declarations": list(self.replacements.declarations),
192
193
194
195
196
197
198
199
200
201
202
203

    @classmethod
    def _from_owned_data(cls, **values: Any) -> "CodegenSnapshot":
        """Adopt data already isolated by a session without copying it again."""
        snapshot = object.__new__(cls)
        for name, value in values.items():
            object.__setattr__(snapshot, name, value)
        return snapshot


__all__ = [
    "CodegenSnapshot",
hyper_parallel/codegen/source/composition.py
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

    Returns:
        One source string containing the primary and nested model definitions.
    """
    components = getattr(meta, "nested_components", None) or ()
    if not components:
        return source_text
    rendered = source_text
    included_modules: set[str] = set()
    for component in components:
        identity = component.get("type")
        if not isinstance(identity, str) or ":" not in identity:
            raise ValueError(f"invalid nested component identity: {identity!r}")
        module_name, class_name = identity.split(":", 1)
        class_name = class_name.rsplit(".", 1)[-1]
        replaced = _replace_auto_model_construction(
            rendered,
            component_fqn=str(component.get("fqn") or ""),
            class_name=class_name,
        )
        if replaced is None:
            continue
        rendered = replaced
        if module_name not in included_modules:
            nested_source = _read_module_source(module_name)
            rendered = _append_source_module(rendered, nested_source, module_name)
            included_modules.add(module_name)
    return rendered


def _read_module_source(module_name: str) -> str:
    """Read an installed module without importing its runtime dependencies."""
    spec = importlib.util.find_spec(module_name)
    path = None if spec is None else spec.origin
    if not path or not os.path.isfile(path):
        raise FileNotFoundError(
            f"codegen: nested model source {module_name!r} is not readable"
        )
    with open(path, "r", encoding="utf-8", errors="replace") as handle:
        return handle.read()


def _append_source_module(primary: str, nested: str, module_name: str) -> str:
    """Append one nested module after removing module-only syntax."""
    tree = ast.parse(nested, filename=module_name)
    package = module_name.rpartition(".")[0]
    tree = _AbsoluteImports(package).visit(tree)
    ast.fix_missing_locations(tree)
    primary_tree = ast.parse(primary)
    primary_bindings = _top_level_definitions(primary_tree)
    nested_bindings = _top_level_definitions(tree)
    collisions = {
        name for name in primary_bindings.keys() & nested_bindings.keys()
        if not isinstance(nested_bindings[name], ast.ClassDef)
    }
    if collisions:
        replacements = {name: f"_hp_nested_{name}" for name in collisions}
        for node in tree.body:
            if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                node.name = replacements.get(node.name, node.name)
        tree = _RenameBindings(replacements).visit(tree)
        ast.fix_missing_locations(tree)
    nodes = [
        node for node in tree.body
        if not isinstance(node, ast.Expr) or not _is_docstring(node)
    ]
    nodes = [
        node for node in nodes
        if not isinstance(node, ast.ImportFrom) or node.module != "__future__"
    ]
    body = "\n\n".join(ast.unparse(node) for node in nodes)
    marker = (
        "# [HYPER NESTED MODEL] source merged from "
        f"{module_name}; TP/EP lowering below targets these concrete classes."
    )
    return f"{primary.rstrip()}\n\n\n{marker}\n{body}\n"


def _top_level_definitions(tree: ast.Module) -> dict[str, ast.AST]:
    """Return named functions, classes, and simple assignments."""
    result: dict[str, ast.AST] = {}
    for node in tree.body:
        if isinstance(node, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
            result[node.name] = node
        elif isinstance(node, (ast.Assign, ast.AnnAssign)):
            targets = node.targets if isinstance(node, ast.Assign) else (node.target,)
            for target in targets:
                if isinstance(target, ast.Name):
                    result[target.id] = node
    return result


def _is_docstring(node: ast.Expr) -> bool:
    return isinstance(node.value, ast.Constant) and isinstance(node.value.value, str)


class _RenameBindings(ast.NodeTransformer):
    """Rename colliding nested-module globals and their references."""
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    """Rename colliding nested-module globals and their references."""

    def __init__(self, names: dict[str, str]) -> None:
        """Create a renamer for the supplied module-global bindings."""
        self._names = names

    def visit_Name(self, node: ast.Name) -> ast.AST:  # pylint: disable=invalid-name
        """Rename a matching load or store while preserving its AST context."""
        replacement = self._names.get(node.id)
        if replacement is None:
            return node
        return ast.copy_location(ast.Name(id=replacement, ctx=node.ctx), node)


class _AbsoluteImports(ast.NodeTransformer):
    """Anchor nested-module relative imports on their original package."""
150
151
152
153
154
155
156
157
158
    """Anchor nested-module relative imports on their original package."""

    def __init__(self, package: str) -> None:
        """Create a transformer for one installed source package."""
        self._package = package

    def visit_ImportFrom(  # pylint: disable=invalid-name
        self,
        node: ast.ImportFrom,
157
158
159
160
161
162
163
164
165
166
167
168
169
170
        self,
        node: ast.ImportFrom,
    ) -> ast.AST | list[ast.AST]:
        """Convert one relative import into its installed absolute module."""
        if node.level == 0:
            return node
        relative = "." * node.level + (node.module or "")
        module = importlib.util.resolve_name(relative, self._package)
        if not all(part.isidentifier() and not keyword.iskeyword(part) for part in module.split(".")):
            return self._dynamic_imports(node, module)
        return ast.copy_location(
            ast.ImportFrom(module=module, names=node.names, level=0),
            node,
        )
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

    @staticmethod
    def _dynamic_imports(node: ast.ImportFrom, module: str) -> list[ast.AST]:
        """Render imports whose dynamic package contains non-Python identifiers."""
        assignments = []
        for alias in node.names:
            if alias.name == "*":
                raise ValueError(
                    f"codegen cannot compose a wildcard import from dynamic module {module!r}"
                )
            imported = ast.Call(
                func=ast.Name(id="__import__", ctx=ast.Load()),
                args=[ast.Constant(module)],
                keywords=[ast.keyword(arg="fromlist", value=ast.List(
                    elts=[ast.Constant(alias.name)],
185
186
187
188
189
190
191
192
193
194
195
196
197
198
                    elts=[ast.Constant(alias.name)],
                    ctx=ast.Load(),
                ))],
            )
            assignment = ast.Assign(
                targets=[ast.Name(id=alias.asname or alias.name, ctx=ast.Store())],
                value=ast.Attribute(value=imported, attr=alias.name, ctx=ast.Load()),
            )
            assignments.append(ast.copy_location(assignment, node))
        return assignments


def _replace_auto_model_construction(
    source_text: str,
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    component_fqn: str,
    class_name: str,
) -> str | None:
    """Replace an observed AutoModel construction, or reject the candidate."""
    attribute = component_fqn.rsplit(".", 1)[-1]
    if not attribute:
        raise ValueError("nested component FQN must not be empty")
    pattern = re.compile(
        rf"(?m)^(?P<indent>\s*)self\.{re.escape(attribute)}\s*=\s*"
        r"AutoModel\.from_config\((?P<config>[^\n]+)\)\s*$"
    )
    replacement = rf"\g<indent>self.{attribute} = {class_name}(\g<config>)"
    rendered, count = pattern.subn(replacement, source_text, count=1)
    return rendered if count == 1 else None


__all__ = ["compose_nested_model_sources"]
hyper_parallel/codegen/source/resolver.py
356
357
358
359
360
361
362
363
364
    if not model_id:
        return None
    try:
        # Transformers is optional for repository-only source resolution.
        from transformers import AutoConfig  # pylint: disable=import-outside-toplevel
    except ImportError:
        return None
    kwargs: dict[str, Any] = {}
    if _trust_remote_code(config):
419
420
421
422
423
424
425
426
427
428
429
430
    path = getattr(model, "pretrained_model_name_or_path", None)
    if path is None:
        path = getattr(model, "model_name_or_path", None)
    if path is None:
        path = getattr(model, "config_path", None)
    if path is None:
        kwargs = getattr(model, "_kwargs", {})
        path = kwargs.get("pretrained_model_name_or_path") or kwargs.get("config_path", "")
    return path or ""


def _trust_remote_code(config: Any) -> bool:
488
489
490
491
492
493
494
495
496
497
498
499
500
        file_path = source.get("file_path")
    else:
        module_name = getattr(source, "module_name", None)
        file_path = getattr(source, "file_path", None)
    is_remote = module_name == "<remote>" or (
        isinstance(module_name, str)
        and module_name.startswith("transformers_modules.")
    )
    if not is_remote or not file_path:
        return {}
    base_dir = os.path.dirname(os.path.abspath(file_path))
    seen: dict[str, str] = {}
    queue: list[str] = [os.path.abspath(file_path)]
hyper_parallel/codegen/train_sidecar.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


def _codegen_environment() -> dict[str, str]:
    """Return a child environment without torchrun distributed identity."""
    child_env = os.environ.copy()
    for name in _DISTRIBUTED_ENV_NAMES:
        child_env.pop(name, None)
    return child_env


def _monitor_sidecar(process: subprocess.Popen, config_path: str) -> None:
    """Reap the Codegen process and report failure without stopping training."""
    return_code = process.wait()
    if return_code == 0:
        logger.info("Codegen sidecar completed for %s", config_path)
        return
    logger.warning(
        "Codegen sidecar failed for %s with exit code %s; native training continues",
        config_path,
        return_code,
    )
81
82
83
84
85
86
87
88
89
90
91
92
93
    Returns:
        The rank-0 Codegen child process, or ``None`` on nonzero ranks and when
        the child cannot be started.
    """
    if int(os.environ.get("RANK", "0")) != 0:
        return None

    resolved_config = str(Path(config_path).resolve())
    command = [
        sys.executable,
        "-m",
        "hyper_parallel.codegen",
        "--verbose",
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        "--config",
        resolved_config,
        *overrides,
    ]
    try:
        process = subprocess.Popen(  # pylint: disable=consider-using-with
            command,
            env=_codegen_environment(),
            close_fds=True,
        )
    except OSError as exc:
        logger.warning(
            "Codegen sidecar could not start for %s: %s; native training continues",
            resolved_config,
            exc,
        )
        return None

    monitor = threading.Thread(
        target=_monitor_sidecar,
        args=(process, resolved_config),
        name="hyper-parallel-codegen-sidecar",
        daemon=True,
115
116
117
118
119
120
121
122
123
124
125
        args=(process, resolved_config),
        name="hyper-parallel-codegen-sidecar",
        daemon=True,
    )
    monitor.start()
    logger.info("Started Codegen sidecar process %s for %s", process.pid, resolved_config)
    return process


def run_training_with_codegen(
    parse_training_args: Callable[[], Any],
132
133
134
135
136
137
138
139
140
141
142
        parse_training_args: Native Trainer configuration parser.
        trainer_type: Native Trainer class or compatible factory.
        argv: Complete training command arguments. Defaults to ``sys.argv``.
    """
    arguments = sys.argv if argv is None else argv
    config = parse_training_args()
    start_codegen_sidecar(config._yaml_path, arguments[2:])  # pylint: disable=protected-access
    trainer_type(config).train()


__all__ = ["run_training_with_codegen", "start_codegen_sidecar"]
hyper_parallel/distributed/context_parallel/wrappers.py
426
427
428
429
430
431
432
433
434
435
436
437
438
439
            "cp_mesh=None semantics are then your own responsibility), or use the "
            "local_compute_fn channel (see "
            "examples/distributed/perf_replacement.py)")

    setattr(target_module, "_hp_sdpa_hf_cp_mesh", cp_mesh)
    setattr(target_module, "_hp_sdpa_hf_original_forward", target_module.forward)

    @functools.wraps(target_module.forward)
    def cp_forward(hidden_states: Any, *args: Any, **kwargs: Any) -> Any:
        """Delegate to the source-visible SDPA helper shared with Codegen."""
        return sdpa_hf_cp_forward(target_module, hidden_states, *args, **kwargs)

    return cp_forward

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
def sdpa_hf_cp_forward(
        target_module: Module, hidden_states: Any,
        *args: Any, **kwargs: Any) -> Any:
    """Run an HF attention forward while intercepting SDPA for CP."""
    cp_mesh = getattr(target_module, "_hp_sdpa_hf_cp_mesh")
    original_forward = getattr(target_module, "_forward_impl", None)
    if original_forward is None:
        original_forward = getattr(target_module, "_hp_sdpa_hf_original_forward")
    orig_sdpa = F.scaled_dot_product_attention
    fired = {"hit": False}

    def cp_aware_sdpa(q: Any, k: Any, v: Any, **kw: Any) -> Any:
        """CP-aware SDPA replacement: all-gather K/V plus the D-04 mask."""
        fired["hit"] = True
        return _cp_sdpa_call(orig_sdpa, cp_mesh, q, k, v, kw)

    F.scaled_dot_product_attention = cp_aware_sdpa
    try:
        out = original_forward(hidden_states, *args, **kwargs)
    finally:
        F.scaled_dot_product_attention = orig_sdpa
    if not fired["hit"]:
        raise RuntimeError(
            f"CP wrapper 'sdpa_hf' did not intercept any "
            f"F.scaled_dot_product_attention call on "
            f"{type(target_module).__name__} -- the wrapper does "
            f"not match the module implementation (K/V were not "
468
469
470
471
472
473
474
475
476
            f"all-gathered; continuing would produce silent numerical "
            f"errors). Please explicitly set inner_wrapper='sdpa_qkv' "
            f"(the (q,k,v) convention), or provide a custom inner_wrapper "
            f"callable")
    return out


@inner_wrapper
def flex_hf_cp_wrapper(
hyper_parallel/distributed/mesh.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


def _compatible_parallel_degree(value: Any) -> Any:
    """Preserve the Trainer's historical minimum parallel degree of one."""
    return max(1, value)


def _positive_world_size(value: Any) -> int:
    """Validate the process-group or inferred world size."""
    if isinstance(value, bool) or not isinstance(value, int) or value < 1:
        raise ValueError(f"world_size must be a positive integer, got {value!r}")
    return value


@dataclass(frozen=True)
class ParallelTopologySpec:
68
69
70
71
72
73
74
75
76
77
78

    @classmethod
    def from_config(cls, config: Any) -> "ParallelTopologySpec":
        """Read parallel degrees using the Trainer's compatibility semantics."""
        accelerator = config.accelerator
        fsdp_config = config.fsdp_config
        return cls(
            tp_size=_compatible_parallel_degree(accelerator.tp_size),
            cp_size=_compatible_parallel_degree(accelerator.cp_size),
            pp_size=_compatible_parallel_degree(accelerator.pp_size),
            ep_size=_compatible_parallel_degree(accelerator.ep_size),
 94
 95
 96
 97
 98
 99
100
101
102

    @property
    def dense_world_size(self) -> int:
        """Return the rank count in one pipeline stage."""
        return self.dp_size * self.spec.cp_size * self.spec.tp_size


def resolve_parallel_layout(
    spec: ParallelTopologySpec, world_size: int
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def resolve_parallel_layout(
    spec: ParallelTopologySpec, world_size: int
) -> ParallelLayout:
    """Validate a world size and resolve its DP/FSDP/EP layout."""
    world_size = _positive_world_size(world_size)
    non_dp_size = spec.tp_size * spec.cp_size * spec.pp_size
    if world_size % non_dp_size != 0:
        raise ValueError(
            f"world_size {world_size} is not divisible by non-DP size "
            f"{non_dp_size}"
        )
    dp_size = world_size // non_dp_size
    fsdp_data_parallel_size = dp_size * spec.cp_size
    if fsdp_data_parallel_size % spec.dp_shard_size != 0:
        raise ValueError(
            "DP+CP size "
            f"{fsdp_data_parallel_size} is not divisible by FSDP shard size "
            f"{spec.dp_shard_size}"
        )
    dense_world_size = dp_size * spec.cp_size * spec.tp_size
    if dense_world_size % spec.ep_size != 0:
        raise ValueError(
            f"expert domain size ({dense_world_size}) must be divisible by "
            f"ep_size ({spec.ep_size})"
        )
    expert_data_parallel_size = dense_world_size // spec.ep_size
    if expert_data_parallel_size % spec.edp_shard_size != 0:
        raise ValueError(
            f"expert data-parallel size ({expert_data_parallel_size}) must be "
            f"divisible by edp_shard_size ({spec.edp_shard_size})"
        )
    return ParallelLayout(
        spec=spec,
        world_size=world_size,
        dp_size=dp_size,
        dp_replicate_size=fsdp_data_parallel_size // spec.dp_shard_size,
138
139
140
141
142
143
144
145
146


def infer_minimum_world_size(spec: ParallelTopologySpec) -> int:
    """Return the smallest world size satisfying every parallel contract."""
    dense_world_size = math.lcm(
        spec.tp_size * spec.cp_size,
        spec.ep_size,
        spec.tp_size * spec.dp_shard_size,
        spec.ep_size * spec.edp_shard_size,
144
145
146
147
148
149
150
151
152
        spec.ep_size,
        spec.tp_size * spec.dp_shard_size,
        spec.ep_size * spec.edp_shard_size,
    )
    return dense_world_size * spec.pp_size


@dataclass(frozen=True)
class LogicalDeviceMesh:
157
158
159
160
161
162
163
164
165

    @property
    def shape(self) -> tuple[int, ...]:
        """Return the logical mesh shape."""
        return self.mesh_shape

    def __getitem__(
        self, mesh_dim_names: Union[str, Tuple[str, ...]]
    ) -> "LogicalDeviceMesh":
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    def __getitem__(
        self, mesh_dim_names: Union[str, Tuple[str, ...]]
    ) -> "LogicalDeviceMesh":
        """Return a logical submesh selected by dimension name."""
        names = (
            (mesh_dim_names,)
            if isinstance(mesh_dim_names, str)
            else tuple(mesh_dim_names)
        )
        missing = tuple(name for name in names if name not in self.mesh_dim_names)
        if missing:
            raise KeyError(f"logical mesh has no dimensions {missing!r}")
        shape_by_name = dict(zip(self.mesh_dim_names, self.mesh_shape))
        return LogicalDeviceMesh(
            mesh_dim_names=names,
            mesh_shape=tuple(shape_by_name[name] for name in names),
        )
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        )

    def size(self, mesh_dim: Optional[Union[int, str]] = None) -> int:
        """Return the total or selected logical dimension size."""
        if mesh_dim is None:
            return math.prod(self.mesh_shape)
        index = (
            self.mesh_dim_names.index(mesh_dim)
            if isinstance(mesh_dim, str)
            else mesh_dim
        )
        return self.mesh_shape[index]


def _init_topology_mesh(mesh_kwargs: dict[str, Any]) -> Any:
    """Create a mesh while preserving compatibility with lightweight test fakes."""
225
226
227
228
229
230
231
232
233
234

    @classmethod
    def from_layout(cls, layout: ParallelLayout) -> "MeshContext":
        """Create common mesh metadata from a resolved parallel layout."""
        spec = layout.spec
        return cls(
            dp_size=layout.dp_size,
            dp_replicate_size=layout.dp_replicate_size,
            dp_shard_size=spec.dp_shard_size,
            edp_shard_size=spec.edp_shard_size,
450
451
452
453
454
455
456
457
458

        Returns:
            Setup with common model-build policies populated.
        """
        return cls(
            mesh_context=mesh_context,
            strategy_config=config.fsdp_config if strategy_enabled else None,
            low_precision_config=getattr(
                getattr(config, "training", None), "low_precision", None
hyper_parallel/trainer/config/parallelism.py
646
647
648
649
650
651
652
653
654
    """

    effective_config = low_precision_config if low_precision_enabled else None
    rules = []
    for entry_index, entry in enumerate(entries):
        if entry.replace is None or entry.replace.kind != "module":
            continue
        if entry.when == "low_precision" and not low_precision_enabled:
            continue
656
657
658
659
660
661
662
663
664
        patterns = (entry.match,) if isinstance(entry.match, str) else tuple(entry.match)
        policy = None
        if entry.when == "low_precision" and effective_config is not None:
            policy = effective_config.resolve_dtype_scheme(entry.low_precision_dtype_scheme)
        serialized_target = entry.replace.implementation.to_dict()
        rules.append(
            ModuleReplacementSpec(
                match=patterns,
                factory=_target_replacement_factory(entry.replace.implementation, policy),
hyper_parallel/trainer/config/resolver.py
686
687
688
689
690
691
692
693
694
        raise ConfigResolutionError(path, "DataLoader configuration must be a YAML mapping")

    target_node = dict(node)
    if "dataloader_type" in target_node:
        raise ConfigResolutionError(f"{path}.dataloader_type", "renamed to sampler_type")

    if "batch_adapter" in target_node:
        raise ConfigResolutionError(
            f"{path}.batch_adapter",
860
861
862
863
864
865
866
867
868
            missing, or a field or target declaration is invalid.
    """
    if not isinstance(raw, Mapping):
        raise ConfigResolutionError("$", "YAML root must be a mapping")
    if "codegen" in raw:
        raise ConfigResolutionError(
            "$.codegen",
            "the training-side codegen switch was removed; use the codegen CLI to build artifacts",
        )
hyper_parallel/trainer/runtime/distributed.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    accel = cfg.accelerator if cfg is not None and hasattr(cfg, "accelerator") else None
    if accel is None:
        return DistributedSetup(mesh_context=MeshContext())

    topology_spec = ParallelTopologySpec.from_config(cfg)
    world_size = dist.get_world_size() if dist.is_initialized() else 1
    layout = resolve_parallel_layout(topology_spec, world_size)
    mesh_context = MeshContext.from_layout(layout)
    if dist.is_initialized():
        mesh_context.build_meshs(get_device_type(), world_size)

    _populate_mesh_ranks(mesh_context, topology_spec.dp_shard_size)

    return DistributedSetup.from_config(
        cfg,
        mesh_context=mesh_context,
        strategy_enabled=dist.is_initialized(),
    )