Skip to content

Commit 8a47ced

Browse files
authored
Add docstring for auto accelerator (#1956)
Signed-off-by: yiliu30 <[email protected]>
1 parent 0cc6db6 commit 8a47ced

File tree

4 files changed

+118
-16
lines changed

4 files changed

+118
-16
lines changed

.azure-pipelines/scripts/codeScan/pydocstyle/scan_path.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
/neural-compressor/neural_compressor/strategy
1616
/neural-compressor/neural_compressor/training.py
1717
/neural-compressor/neural_compressor/utils
18-
/neural_compressor/torch/algorithms/layer_wise
19-
/neural_compressor/torch/algorithms/mixed_precision
20-
/neural_compressor/torch/algorithms/mx_quant
21-
/neural_compressor/torch/algorithms/pt2e_quant
18+
/neural-compressor/neural_compressor/common
19+
/neural-compressor/neural_compressor/tensorflow
20+
/neural-compressor/neural_compressor/torch/algorithms/layer_wise
21+
/neural-compressor/neural_compressor/torch/algorithms/mixed_precision
22+
/neural-compressor/neural_compressor/torch/algorithms/mx_quant
23+
/neural-compressor/neural_compressor/torch/algorithms/pt2e_quant
2224
/neural-compressor/neural_compressor/torch/algorithms/smooth_quant
2325
/neural-compressor/neural_compressor/torch/algorithms/static_quant
24-
/neural_compressor/torch/algorithms/weight_only
25-
/neural_compressor/torch/export
26-
/neural_compressor/torch/quantization
27-
/neural_compressor/torch/utils
28-
/neural_compressor/common
29-
/neural_compressor/tensorflow
26+
/neural-compressor/neural_compressor/torch/algorithms/weight_only
27+
/neural-compressor/neural_compressor/torch/export
28+
/neural-compressor/neural_compressor/torch/quantization
29+
/neural-compressor/neural_compressor/torch/utils

neural_compressor/common/benchmark.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""Benchmark API for Intel Neural Compressor."""
1415

1516
import argparse
1617
import os
@@ -242,7 +243,9 @@ def get_numa_node(core_list, reversed_numa_info):
242243

243244

244245
def set_cores_for_instance(args, numa_info):
245-
"""All use cases are listed below:
246+
"""Set cores for each instance based on the input args.
247+
248+
All use cases are listed below:
246249
Params: a=num_instance; b=num_cores_per_instance; c=cores;
247250
- no a, b, c: a=1, c=numa:0
248251
- no a, b: a=1, c=c
@@ -357,6 +360,7 @@ def generate_prefix(args, core_list):
357360
Args:
358361
args (argparse): arguments for setting different configurations
359362
core_list: ["node_index", "cpu_index", num_cpu]
363+
360364
Returns:
361365
command_prefix (str): command_prefix with specific core list for Linux or Windows.
362366
"""

neural_compressor/common/utils/utility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ def wrapper(*args, **kwargs):
328328

329329

330330
class ProcessorType(enum.Enum):
331+
"""The processor type."""
332+
331333
Client = "Client"
332334
Server = "Server"
333335

neural_compressor/torch/utils/auto_accelerator.py

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
# NOTICE: The design adapted from:
2121
# https://github.com/microsoft/DeepSpeed/blob/master/accelerator/abstract_accelerator.py.
22+
"""Auto Accelerator Module."""
2223

2324

2425
# To keep it simply, only add the APIs we need.
@@ -40,6 +41,8 @@
4041

4142

4243
class AcceleratorRegistry:
44+
"""Accelerator Registry."""
45+
4346
registered_accelerators = {}
4447

4548
@classmethod
@@ -94,171 +97,253 @@ class CUDA_Accelerator:
9497
name: the accelerator name.
9598
priority: the priority of the accelerator. A larger number indicates a higher priority,
9699
"""
97-
98100
return accelerator_registry.register_accelerator_impl(name=name, priority=priority)
99101

100102

101103
class Auto_Accelerator(ABC): # pragma: no cover
104+
"""Auto Accelerator Base class."""
105+
102106
@classmethod
103107
@abstractmethod
104108
def is_available(cls) -> bool:
109+
"""Check if the accelerator is available."""
105110
pass
106111

107112
@abstractmethod
108113
def name(self) -> str:
114+
"""Get the accelerator name."""
109115
pass
110116

111117
@abstractmethod
112118
def device_name(self, device_indx) -> str:
119+
"""Get the device name."""
113120
pass
114121

115122
@abstractmethod
116123
def set_device(self, device_index):
124+
"""Set the device."""
117125
pass
118126

119127
@abstractmethod
120128
def current_device(self):
129+
"""Get the current device."""
121130
pass
122131

123132
@abstractmethod
124133
def current_device_name(self):
134+
"""Get the current device name."""
125135
pass
126136

127137
@abstractmethod
128138
def device(self, device_index=None):
139+
"""Get the device."""
129140
pass
130141

131142
@abstractmethod
132143
def empty_cache(self):
144+
"""Empty the cache."""
133145
pass
134146

135147
@abstractmethod
136148
def synchronize(self):
149+
"""Synchronize the accelerator."""
137150
pass
138151

139152
def mark_step(self):
153+
"""Trigger graph to run."""
140154
pass
141155

142156

143157
@register_accelerator(name="cpu", priority=PRIORITY_CPU)
144158
class CPU_Accelerator(Auto_Accelerator):
159+
"""CPU Accelerator."""
160+
145161
def __init__(self) -> None:
162+
"""Initialize CPU Accelerator."""
146163
self._name = "cpu"
147164

148165
def name(self) -> str:
166+
"""Get the accelerator name."""
149167
return self._name
150168

151169
@classmethod
152170
def is_available(cls) -> bool:
171+
"""Always return True."""
153172
return True
154173

155174
def device_name(self, device_indx) -> str:
175+
"""Get the device name."""
156176
return "cpu"
157177

158178
def set_device(self, device_index):
179+
"""Do nothing."""
159180
pass
160181

161182
def current_device(self):
183+
"""Get the current device."""
162184
return "cpu"
163185

164186
def current_device_name(self):
187+
"""Get the current device name."""
165188
return "cpu"
166189

167190
def device(self, device_index=None):
191+
"""Do nothing."""
168192
pass
169193

170194
def empty_cache(self):
195+
"""Do nothing."""
171196
pass
172197

173198
def synchronize(self):
199+
"""Do nothing."""
174200
pass
175201

176202

177203
@register_accelerator(name="cuda", priority=PRIORITY_CUDA)
178204
class CUDA_Accelerator(Auto_Accelerator): # pragma: no cover
205+
"""CUDA Accelerator."""
206+
179207
def __init__(self) -> None:
208+
"""Initialize CUDA Accelerator."""
180209
self._name = "cuda"
181210

182211
def name(self) -> str:
212+
"""Get the accelerator name."""
183213
return self._name
184214

185215
@classmethod
186216
def is_available(cls) -> bool:
217+
"""Check if the 'cuda' device is available."""
187218
return torch.cuda.is_available()
188219

189220
def device_name(self, device_indx) -> str:
221+
"""Returns the name of the 'cuda' device with the given index."""
190222
if device_indx is None:
191223
return "cuda"
192224
return f"cuda:{device_indx}"
193225

194226
def synchronize(self):
227+
"""Synchronizes the 'cuda' device."""
195228
return torch.cuda.synchronize()
196229

197230
def set_device(self, device_index):
231+
"""Sets the current 'cuda' device to the one with the given index."""
198232
return torch.cuda.set_device(device_index)
199233

200234
def current_device(self):
235+
"""Returns the index of the current 'cuda' device."""
201236
return torch.cuda.current_device()
202237

203238
def current_device_name(self):
239+
"""Returns the name of the current 'cuda' device."""
204240
return "cuda:{}".format(torch.cuda.current_device())
205241

206242
def device(self, device_index=None):
243+
"""Returns a torch.device object for the 'cuda' device with the given index."""
207244
return torch.cuda.device(device_index)
208245

209246
def empty_cache(self):
247+
"""Empties the cuda cache."""
210248
return torch.cuda.empty_cache()
211249

212250

213251
@register_accelerator(name="xpu", priority=PRIORITY_XPU)
214252
class XPU_Accelerator(Auto_Accelerator): # pragma: no cover
253+
"""XPU Accelerator."""
254+
215255
def __init__(self) -> None:
256+
"""Initialize XPU Accelerator."""
216257
self._name = "xpu"
217258

218259
def name(self) -> str:
260+
"""Get the accelerator name."""
219261
return self._name
220262

221263
@classmethod
222264
def is_available(cls) -> bool:
265+
"""Checks if the 'xpu' device is available.
266+
267+
Returns:
268+
bool: True if the 'xpu' device is available, False otherwise.
269+
"""
223270
if hasattr(torch, "xpu") and torch.xpu.is_available():
224271
return True
225272
else:
226273
return False
227274

228275
def device_name(self, device_indx) -> str:
276+
"""Returns the name of the 'xpu' device with the given index.
277+
278+
Args:
279+
device_indx (int): The index of the 'xpu' device.
280+
281+
Returns:
282+
str: The name of the 'xpu' device.
283+
"""
229284
if device_indx is None:
230285
return "xpu"
231286
return f"xpu:{device_indx}"
232287

233288
def synchronize(self):
289+
"""Synchronizes the 'xpu' device."""
234290
return torch.xpu.synchronize()
235291

236292
def set_device(self, device_index):
293+
"""Sets the current 'xpu' device to the one with the given index.
294+
295+
Args:
296+
device_index (int): The index of the 'xpu' device.
297+
"""
237298
return torch.xpu.set_device(device_index)
238299

239300
def current_device(self):
301+
"""Returns the index of the current 'xpu' device.
302+
303+
Returns:
304+
int: The index of the current 'xpu' device.
305+
"""
240306
return torch.xpu.current_device()
241307

242308
def current_device_name(self):
309+
"""Returns the name of the current 'xpu' device.
310+
311+
Returns:
312+
str: The name of the current 'xpu' device.
313+
"""
243314
return "xpu:{}".format(torch.xpu.current_device())
244315

245316
def device(self, device_index=None):
317+
"""Returns a torch.device object for the 'xpu' device with the given index.
318+
319+
Args:
320+
device_index (int, optional): The index of the 'xpu' device. Defaults to None.
321+
322+
Returns:
323+
torch.device: The torch.device object for the 'xpu' device.
324+
"""
246325
return torch.xpu.device(device_index)
247326

248327
def empty_cache(self):
328+
"""Empties the xpu cache."""
249329
return torch.xpu.empty_cache()
250330

251331

252332
@register_accelerator(name="hpu", priority=PRIORITY_HPU)
253333
class HPU_Accelerator(Auto_Accelerator): # pragma: no cover
334+
"""HPU Accelerator."""
335+
254336
def __init__(self) -> None:
337+
"""Initialize HPU Accelerator."""
255338
self._name = "hpu"
256339

257340
def name(self) -> str:
341+
"""Get the accelerator name."""
258342
return self._name
259343

260344
@classmethod
261345
def is_available(cls) -> bool:
346+
"""Checks if the 'hpu' device is available."""
262347
from .environ import is_hpex_available
263348

264349
if is_hpex_available():
@@ -267,43 +352,54 @@ def is_available(cls) -> bool:
267352
return False
268353

269354
def device_name(self, device_indx) -> str:
355+
"""Returns the name of the 'hpu' device with the given index."""
270356
if device_indx is None:
271357
return "hpu"
272358
return f"hpu:{device_indx}"
273359

274360
def synchronize(self):
361+
"""Synchronizes the 'hpu' device."""
275362
return torch.hpu.synchronize()
276363

277364
def set_device(self, device_index):
365+
"""Sets the current 'hpu' device to the one with the given index."""
278366
try:
279367
torch.hpu.set_device(device_index)
280368
except Exception as e:
281369
logger.warning(e)
282370

283371
def current_device(self):
372+
"""Returns the index of the current 'hpu' device."""
284373
return torch.hpu.current_device()
285374

286375
def current_device_name(self):
376+
"""Returns the name of the current 'hpu' device."""
287377
return "hpu:{}".format(torch.hpu.current_device())
288378

289379
def device(self, device_index=None):
380+
"""Returns a torch.device object for the 'hpu' device with the given index."""
290381
return torch.hpu.device(device_index)
291382

292383
def empty_cache(self):
384+
"""Empties the hpu cache."""
293385
try:
294386
torch.hpu.empty_cache()
295387
except Exception as e:
296388
logger.warning(e)
297389

298390
def mark_step(self):
391+
"""Trigger graph to run."""
299392
return htcore.mark_step()
300393

301394

302395
def auto_detect_accelerator(device_name="auto") -> Auto_Accelerator:
303-
# Force use the cpu on node has both cpu and gpu: `FORCE_DEVICE=cpu` python main.py ...
304-
# The `FORCE_DEVICE` is case insensitive.
305-
# The environment variable `FORCE_DEVICE` has higher priority than the `device_name`.
306-
# TODO: refine the docs and logic later
396+
"""Automatically detects and selects the appropriate accelerator.
397+
398+
Force use the cpu on node has both cpu and gpu: `FORCE_DEVICE=cpu` python main.py ...
399+
The `FORCE_DEVICE` is case insensitive.
400+
The environment variable `FORCE_DEVICE` has higher priority than the `device_name`.
401+
TODO: refine the docs and logic later
402+
"""
307403
# 1. Get the device setting from environment variable `FORCE_DEVICE`.
308404
FORCE_DEVICE = os.environ.get("FORCE_DEVICE", None)
309405
if FORCE_DEVICE:

0 commit comments

Comments
 (0)