Diff Coverage

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

Source File Diff Coverage (%) Missing Lines
hyper_parallel/core/optimizer/dtensor_compat.py 74.1% 62-65,69,94-97,103-104,112-113,120-124,130,142-143,200
hyper_parallel/rl/examples/consistency/scripts/run_consistency.py 100%  
hyper_parallel/core/optimizer/dtensor_compat.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    if sample_param is None:
        sample_param = _extract_first_param(adamw_params)

    if sample_param is None:
        logger.info_rank0("No parameters found for backend detection; defaulting to 'hyper'.")
        _DTENSOR_BACKEND = "hyper"
        _invalidate_lazy_cache()
        return _DTENSOR_BACKEND

    param_cls_module = type(sample_param).__module__
    if param_cls_module.startswith("torch.distributed"):
        _DTENSOR_BACKEND = "torch"
    else:
        _DTENSOR_BACKEND = "hyper"

    logger.info_rank0("Detected DTensor backend: '%s'.", _DTENSOR_BACKEND)
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101

# Accessor functions
def get_dtensor_cls():
    """Return the DTensor class for the active backend."""
    if _DTENSOR_BACKEND == "torch":
        return torch_dt.DTensor
    from hyper_parallel.core.dtensor.dtensor import DTensor  # pylint: disable=import-outside-toplevel
    return DTensor


def get_device_mesh_cls():
    """Return the DeviceMesh class for the active backend."""
 99
100
101
102
103
104
105
106
107

def get_device_mesh_cls():
    """Return the DeviceMesh class for the active backend."""
    if _DTENSOR_BACKEND == "torch":
        from torch.distributed.device_mesh import DeviceMesh  # pylint: disable=import-outside-toplevel
        return DeviceMesh
    from hyper_parallel.core.dtensor.device_mesh import DeviceMesh  # pylint: disable=import-outside-toplevel
    return DeviceMesh

108
109
110
111
112
113
114
115
116

def get_shard_cls():
    """Return the Shard placement class for the active backend."""
    if _DTENSOR_BACKEND == "torch":
        from torch.distributed._tensor.placement_types import Shard  # pylint: disable=import-outside-toplevel
        return Shard
    from hyper_parallel.core.dtensor.placement_types import Shard  # pylint: disable=import-outside-toplevel
    return Shard

116
117
118
119
120
121
122
123
124
125
126
127
128


def get_replicate_cls():
    """Return the Replicate placement class for the active backend."""
    if _DTENSOR_BACKEND == "torch":
        from torch.distributed._tensor.placement_types import Replicate  # pylint: disable=import-outside-toplevel
        return Replicate
    from hyper_parallel.core.dtensor.placement_types import Replicate  # pylint: disable=import-outside-toplevel
    return Replicate


def get_strided_shard_cls():
    """Return the StridedShard placement class. Returns _NEVER_MATCH for 'torch'."""
126
127
128
129
130
131
132
133

def get_strided_shard_cls():
    """Return the StridedShard placement class. Returns _NEVER_MATCH for 'torch'."""
    if _DTENSOR_BACKEND == "torch":
        return _NeverMatch

    from hyper_parallel.core.dtensor.placement_types import StridedShard  # pylint: disable=import-outside-toplevel
    return StridedShard
138
139
140
141
142
143
144
145
146
147
    """Import hyper DTensor class; return torch DTensor as fallback."""
    try:
        from hyper_parallel.core.dtensor.dtensor import DTensor  # pylint: disable=import-outside-toplevel
        return DTensor
    except ImportError:
        return torch_dt.DTensor


def _resolve_dtensor_union():
    """Build ``torch_dt.DTensor | hyper_dt.DTensor`` on demand."""
196
197
198
199
200


def __dir__():  # type: ignore[no-untyped-def]  # pylint: disable=invalid-name
    """Include lazy-exported names in dir() for IDE autocomplete."""
    return list(globals().keys()) + list(_LAZY_RESOLVERS.keys())