Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / auto_parallel / hyper_offload / api / scope.py: 100%
19 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Recognize framework-owned storage without changing the framework runtime."""
17import sys
18from functools import lru_cache
20from torch.utils._pytree import tree_iter
22from hyper_parallel.auto_parallel.hyper_offload.execution.framework import (
23 dispatch_framework_op,
24 dispatch_framework_read,
25)
26from hyper_parallel.auto_parallel.hyper_offload.execution.tensor import ShadowTensor
28__all__ = ["dispatch_framework_op", "dispatch_framework_read", "has_managed_inputs", "inside_fsdp_runtime"]
31def has_managed_inputs(args: tuple, kwargs: dict) -> bool:
32 """Return whether framework code is consuming a tracked activation."""
33 # This is a predicate: rebuilding every argument container dominates the
34 # framework's otherwise metadata-only gradient view operations.
35 return any(isinstance(value, ShadowTensor) for value in tree_iter((args, kwargs)))
38@lru_cache(maxsize=256)
39def _is_fsdp_module(module_name: str) -> bool:
40 """Cache only immutable classification, never the live calling context."""
41 return module_name.startswith((
42 "hyper_parallel.core.fully_shard.", "torch.distributed.fsdp._fully_shard.",
43 ))
46def inside_fsdp_runtime() -> bool:
47 """Identify parameter and collective hooks, excluding ordinary model execution."""
48 frame = sys._getframe(1) # pylint: disable=protected-access
49 while frame is not None:
50 module_name = frame.f_globals.get("__name__", "")
51 if _is_fsdp_module(module_name):
52 return True
53 frame = frame.f_back
54 return False