|
| 1 | +# mypy: allow-untyped-defs |
| 2 | +""" |
| 3 | +Python stubs for backend-specific distributed components. |
| 4 | +
|
| 5 | +Since _C._distributed_c10d always exists now, this module only provides |
| 6 | +stubs for backend-specific functionality that may not be available in all builds |
| 7 | +(e.g., NCCL, UCC, MPI, Gloo, etc.). |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import Optional, TYPE_CHECKING |
| 13 | + |
| 14 | +from torch._C._distributed_c10d import Store |
| 15 | + |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from datetime import timedelta |
| 19 | + |
| 20 | +import torch |
| 21 | + |
| 22 | + |
| 23 | +# Store classes |
| 24 | +class HashStore(Store): |
| 25 | + """Stub HashStore for builds without this functionality.""" |
| 26 | + |
| 27 | + def __init__(self, *args, **kwargs): |
| 28 | + self._data = {} |
| 29 | + |
| 30 | + def set(self, key: str, value: str): |
| 31 | + self._data[key] = value |
| 32 | + |
| 33 | + def get(self, key: str) -> bytes: |
| 34 | + return self._data.get(key, "").encode() |
| 35 | + |
| 36 | + |
| 37 | +# Backend-specific process group stubs |
| 38 | +class ProcessGroupMPI: |
| 39 | + """Stub ProcessGroupMPI for non-MPI builds.""" |
| 40 | + |
| 41 | + def __init__(self, *args, **kwargs): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class ProcessGroupNCCL: |
| 46 | + """Stub ProcessGroupNCCL for non-NCCL builds.""" |
| 47 | + |
| 48 | + def __init__(self, *args, **kwargs): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +class ProcessGroupGloo: |
| 53 | + """Stub ProcessGroupGloo for non-Gloo builds.""" |
| 54 | + |
| 55 | + def __init__(self, *args, **kwargs): |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | +class ProcessGroupUCC: |
| 60 | + """Stub ProcessGroupUCC for non-UCC builds.""" |
| 61 | + |
| 62 | + def __init__(self, *args, **kwargs): |
| 63 | + pass |
| 64 | + |
| 65 | + |
| 66 | +class ProcessGroupXCCL: |
| 67 | + """Stub ProcessGroupXCCL for non-XCCL builds.""" |
| 68 | + |
| 69 | + def __init__(self, *args, **kwargs): |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +class _ProcessGroupWrapper: |
| 74 | + """Stub _ProcessGroupWrapper for non-Gloo builds.""" |
| 75 | + |
| 76 | + def __init__(self, process_group, *args, **kwargs): |
| 77 | + self._process_group = process_group |
| 78 | + |
| 79 | + def __getattr__(self, name): |
| 80 | + return getattr(self._process_group, name) |
| 81 | + |
| 82 | + |
| 83 | +# NCCL-specific function stubs |
| 84 | +_DEFAULT_PG_NCCL_TIMEOUT: Optional[timedelta] = None |
| 85 | + |
| 86 | + |
| 87 | +def _hash_tensors(tensors): |
| 88 | + """Stub function to hash tensors - returns dummy hash.""" |
| 89 | + return 0 |
| 90 | + |
| 91 | + |
| 92 | +def _dump_nccl_trace_json( |
| 93 | + includeCollectives: Optional[bool] = None, onlyActive: Optional[bool] = None |
| 94 | +) -> bytes: |
| 95 | + """Stub function that returns empty JSON trace.""" |
| 96 | + return b"{}" |
| 97 | + |
| 98 | + |
| 99 | +def _dump_nccl_trace( |
| 100 | + includeCollectives: Optional[bool] = None, |
| 101 | + includeStackTraces: Optional[bool] = None, |
| 102 | + onlyActive: Optional[bool] = None, |
| 103 | +) -> bytes: |
| 104 | + """Stub function that returns empty pickle trace.""" |
| 105 | + return b"" |
| 106 | + |
| 107 | + |
| 108 | +# NVSHMEM/SymmetricMemory stubs |
| 109 | +def _is_nvshmem_available() -> bool: |
| 110 | + """Stub function that returns False indicating NVSHMEM is not available.""" |
| 111 | + return False |
| 112 | + |
| 113 | + |
| 114 | +def _nvshmemx_cumodule_init(module: int) -> None: |
| 115 | + """Stub function for NVSHMEM CU module initialization.""" |
| 116 | + |
| 117 | + |
| 118 | +class _SymmetricMemory: |
| 119 | + """Stub _SymmetricMemory class for builds without this functionality.""" |
| 120 | + |
| 121 | + def __init__(self, *args, **kwargs): |
| 122 | + pass |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def empty_strided_p2p(cls, size, stride, dtype, device, group_name=None): |
| 126 | + """Stub that returns a regular tensor.""" |
| 127 | + return torch.empty(size, dtype=dtype, device=device) |
| 128 | + |
| 129 | + @classmethod |
| 130 | + def rendezvous(cls, tensor, group_name=None): |
| 131 | + """Stub that returns None.""" |
| 132 | + return None |
| 133 | + |
| 134 | + @classmethod |
| 135 | + def set_group_info(cls, *args, **kwargs): |
| 136 | + """Stub that does nothing.""" |
| 137 | + |
| 138 | + @classmethod |
| 139 | + def set_backend(cls, name): |
| 140 | + """Stub that does nothing.""" |
| 141 | + |
| 142 | + @classmethod |
| 143 | + def get_backend(cls, device): |
| 144 | + """Stub that returns None.""" |
| 145 | + return None |
| 146 | + |
| 147 | + @classmethod |
| 148 | + def has_multicast_support(cls, device_type, device_index): |
| 149 | + """Stub that returns False.""" |
| 150 | + return False |
0 commit comments