|
| 1 | +# Copyright (c) 2023-2024 Microsoft Corporation and Intel Corporation |
| 2 | + |
| 3 | +# This code is based on Microsoft Corporation's DeepSpeed library and |
| 4 | +# the accelerators implementation in this library. It has been modified |
| 5 | +# from its original forms to simplify and adapt it for use in |
| 6 | +# the Intel® Neural Compressor. |
| 7 | + |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | + |
| 20 | +# NOTICE: The design adapted from: |
| 21 | +# https://github.com/microsoft/DeepSpeed/blob/master/accelerator/abstract_accelerator.py. |
| 22 | +# TODO: move it into torch/utils |
| 23 | + |
| 24 | + |
| 25 | +# To keep it simply, only add the APIs we need. |
| 26 | + |
| 27 | +import os |
| 28 | +from abc import ABC, abstractmethod |
| 29 | +from typing import Any, Callable, List |
| 30 | + |
| 31 | +import torch |
| 32 | + |
| 33 | +from neural_compressor.torch.utils import logger |
| 34 | + |
| 35 | +PRIORITY_CUDA = 100 |
| 36 | +PRIORITY_CPU = 90 |
| 37 | + |
| 38 | + |
| 39 | +class AcceleratorRegistry: |
| 40 | + registered_accelerators = {} |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def register_accelerator_impl(cls, name: str, priority: float = 0): |
| 44 | + """Register new accelerator implementation. |
| 45 | +
|
| 46 | + Usage example: |
| 47 | + @AcceleratorRegistry.register_accelerator(name="cpu", priority=100) |
| 48 | + class CPU_Accelerator: |
| 49 | + ... |
| 50 | +
|
| 51 | + Args: |
| 52 | + name: the accelerator name. |
| 53 | + priority: priority: the priority of the accelerator. A larger number indicates a higher priority, |
| 54 | + """ |
| 55 | + |
| 56 | + def decorator(accelerator_cls): |
| 57 | + cls.registered_accelerators.setdefault(name, {}) |
| 58 | + cls.registered_accelerators[name] = (accelerator_cls, priority) |
| 59 | + return accelerator_cls |
| 60 | + |
| 61 | + return decorator |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def get_sorted_accelerators(cls) -> List["Auto_Accelerator"]: |
| 65 | + """Get registered accelerators sorted by priority.""" |
| 66 | + accelerator_pairs = cls.registered_accelerators.values() |
| 67 | + sorted_accelerators_pairs = sorted(accelerator_pairs, key=lambda x: x[1], reverse=True) |
| 68 | + sorted_accelerators = [pair[0] for pair in sorted_accelerators_pairs] |
| 69 | + return sorted_accelerators |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def get_accelerator_cls_by_name(cls, name: str) -> "Auto_Accelerator": |
| 73 | + """Get accelerator by name.""" |
| 74 | + accelerator_cls, _ = cls.registered_accelerators.get(name, (None, None)) |
| 75 | + return accelerator_cls |
| 76 | + |
| 77 | + |
| 78 | +accelerator_registry = AcceleratorRegistry() |
| 79 | + |
| 80 | + |
| 81 | +def register_accelerator(name: str, priority: float = 0) -> Callable[..., Any]: |
| 82 | + """Register new accelerator. |
| 83 | +
|
| 84 | + Usage example: |
| 85 | + @register_accelerator(name="cuda", priority=100) |
| 86 | + class CUDA_Accelerator: |
| 87 | + ... |
| 88 | +
|
| 89 | + Args: |
| 90 | + name: the accelerator name. |
| 91 | + priority: the priority of the accelerator. A larger number indicates a higher priority, |
| 92 | + """ |
| 93 | + |
| 94 | + return accelerator_registry.register_accelerator_impl(name=name, priority=priority) |
| 95 | + |
| 96 | + |
| 97 | +class Auto_Accelerator(ABC): |
| 98 | + @classmethod |
| 99 | + @abstractmethod |
| 100 | + def is_available(cls) -> bool: |
| 101 | + pass |
| 102 | + |
| 103 | + @abstractmethod |
| 104 | + def name(self) -> str: |
| 105 | + pass |
| 106 | + |
| 107 | + @abstractmethod |
| 108 | + def device_name(self, device_indx) -> str: |
| 109 | + pass |
| 110 | + |
| 111 | + @abstractmethod |
| 112 | + def set_device(self, device_index): |
| 113 | + pass |
| 114 | + |
| 115 | + @abstractmethod |
| 116 | + def current_device(self): |
| 117 | + pass |
| 118 | + |
| 119 | + @abstractmethod |
| 120 | + def current_device_name(self): |
| 121 | + pass |
| 122 | + |
| 123 | + @abstractmethod |
| 124 | + def device(self, device_index=None): |
| 125 | + pass |
| 126 | + |
| 127 | + @abstractmethod |
| 128 | + def empty_cache(self): |
| 129 | + pass |
| 130 | + |
| 131 | + @abstractmethod |
| 132 | + def synchronize(self): |
| 133 | + pass |
| 134 | + |
| 135 | + |
| 136 | +@register_accelerator(name="cpu", priority=PRIORITY_CPU) |
| 137 | +class CPU_Accelerator(Auto_Accelerator): |
| 138 | + def __init__(self) -> None: |
| 139 | + self._name = "cpu" |
| 140 | + |
| 141 | + def name(self) -> str: |
| 142 | + return self._name |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def is_available(cls) -> bool: |
| 146 | + return True |
| 147 | + |
| 148 | + def device_name(self, device_indx) -> str: |
| 149 | + return "cpu" |
| 150 | + |
| 151 | + def set_device(self, device_index): |
| 152 | + pass |
| 153 | + |
| 154 | + def current_device(self): |
| 155 | + return "cpu" |
| 156 | + |
| 157 | + def current_device_name(self): |
| 158 | + return "cpu" |
| 159 | + |
| 160 | + def device(self, device_index=None): |
| 161 | + pass |
| 162 | + |
| 163 | + def empty_cache(self): |
| 164 | + pass |
| 165 | + |
| 166 | + def synchronize(self): |
| 167 | + pass |
| 168 | + |
| 169 | + |
| 170 | +@register_accelerator(name="cuda", priority=PRIORITY_CUDA) |
| 171 | +class CUDA_Accelerator(Auto_Accelerator): |
| 172 | + def __init__(self) -> None: |
| 173 | + self._name = "cuda" |
| 174 | + |
| 175 | + def name(self) -> str: |
| 176 | + return self._name |
| 177 | + |
| 178 | + @classmethod |
| 179 | + def is_available(cls) -> bool: |
| 180 | + return torch.cuda.is_available() |
| 181 | + |
| 182 | + def device_name(self, device_indx) -> str: |
| 183 | + if device_indx is None: |
| 184 | + return "cuda" |
| 185 | + return f"cuda:{device_indx}" |
| 186 | + |
| 187 | + def synchronize(self): |
| 188 | + return torch.cuda.synchronize() |
| 189 | + |
| 190 | + def set_device(self, device_index): |
| 191 | + return torch.cuda.set_device(device_index) |
| 192 | + |
| 193 | + def current_device(self): |
| 194 | + return torch.cuda.current_device() |
| 195 | + |
| 196 | + def current_device_name(self): |
| 197 | + return "cuda:{}".format(torch.cuda.current_device()) |
| 198 | + |
| 199 | + def device(self, device_index=None): |
| 200 | + return torch.cuda.device(device_index) |
| 201 | + |
| 202 | + def empty_cache(self): |
| 203 | + return torch.cuda.empty_cache() |
| 204 | + |
| 205 | + |
| 206 | +def auto_detect_accelerator() -> Auto_Accelerator: |
| 207 | + # if runtime_accelerator.accelerator: |
| 208 | + # return runtime_accelerator.accelerator |
| 209 | + FORCE_DEVICE = os.environ.get("FORCE_DEVICE", None) |
| 210 | + if FORCE_DEVICE and accelerator_registry.get_accelerator_cls_by_name(FORCE_DEVICE) is not None: |
| 211 | + logger.warning("Force use %s accelerator.", FORCE_DEVICE) |
| 212 | + return accelerator_registry.get_accelerator_cls_by_name(FORCE_DEVICE)() |
| 213 | + for accelerator_cls in accelerator_registry.get_sorted_accelerators(): |
| 214 | + if accelerator_cls.is_available(): |
| 215 | + logger.debug("Auto detect accelerator: %s.", accelerator_cls.__name__) |
| 216 | + accelerator = accelerator_cls() |
| 217 | + return accelerator |
| 218 | + |
| 219 | + |
| 220 | +# Force use cpu accelerator even if cuda is available. |
| 221 | +# FORCE_DEVICE = "cpu" python ... |
| 222 | +# or |
| 223 | +# CUDA_VISIBLE_DEVICES="" python ... |
0 commit comments