-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathutils.py
413 lines (296 loc) · 11 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# coding: utf-8
import torch
from .. import _C
from enum import Enum
from ..utils._logger import logger, WarningType
from .. import frontend
import intel_extension_for_pytorch # noqa
def from_usm(src, dtype, shape, stride=None, device_id: int = -1) -> torch.Tensor:
"""from_usm(src, dtype, shape, stride=None, device_d=-1) -> Tensor
Converts a tensor allocated in USM(United Shared Memory) into a ``torch.Tensor``.
The returned PyTorch tensor will share the memory with the input tensor
(which may have come from another library). Note that in-place operations
will therefore also affect the data of the input tensor. And this API doesn't
manage USM tensor src's lifetime. Please take care it carefully.
Args:
src: A capsule of USM pointer to convert, the name stored in the capsule
is 'USMtensor'.
dtype: the desired data type of returned tensor.
shape: the desired shape of returned tensor.
stride: the desired stride of returned tensor. Default: if None,
returned tensor is contiguous.
device_id: the root device id where the USM pointer is allocated. Default: -1,
if the user is not sure.
Warning: This is decrepated. Please use torch.from_dlpack instead.
"""
logger.warning(
"from_usm is decrepated. Please use torch.from_dlpack instead.",
_type=WarningType.DeprecatedArgument,
)
return _C._from_usm(src, dtype, shape, stride, device_id)
def to_usm(src: torch.Tensor):
"""to_usm(src: torch.Tensor): -> PyCapsule
Converts a torch tensor allocated in USM(United Shared Memory) into a ``PyCapsule``,
which encapsules a USM data pointer address.
Args:
src: a torch tensor.
Warning: This is decrepated. Please use torch.to_dlpack instead.
"""
logger.warning(
"to_usm is decrepated. Please use torch.to_dlpack instead.",
_type=WarningType.DeprecatedArgument,
)
return _C._to_usm(src)
def is_contiguous_channels_last_1d(input):
if 3 != input.dim():
return False
tmpTen = input.view(input.size(0), input.size(1), 1, input.size(2))
if tmpTen.is_contiguous(memory_format=torch.channels_last):
return True
else:
return False
def has_onemkl():
return _C._is_onemkl_enabled()
def has_multi_context():
return _C._is_multi_context_enabled()
def has_channels_last_1d():
return _C._is_channels_last_1d_enabled()
def has_fp64_dtype(device: int = -1) -> bool:
r"""Returns a bool indicating if the current XPU device supports dtype float64"""
return _C._has_fp64_dtype(device)
def has_2d_block_array(device: int = -1) -> bool:
r"""Returns a bool indicating if the platform supports 2d block array load/store"""
return _C._has_2d_block_array(device)
# Basic OnOff
class OnOff:
def __init__(self, checker, enable, disable):
self._init_status = checker()
self._enabled = True
self._disabled = False
self._enable_fn = enable
self._disable_fn = disable
def __enter__(self):
if self._init_status == self._disabled:
self._enable_fn()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self._init_status == self._disabled:
self._disable_fn()
return False
class EnumBase(Enum):
@classmethod
def convert(cls, value):
if isinstance(value, cls):
return value
if isinstance(value, str) and value.isdecimal():
value = int(value)
if isinstance(value, int) and cls.has_value(value):
return cls(value)
raise RuntimeError("Unexpected {} value {}!".format(cls, value))
@classmethod
def has_value(cls, value):
return value in cls._value2member_map_
@classmethod
def get_value(cls, get_func):
return cls(get_func())
@classmethod
def set_value(cls, set_func, value):
return set_func(cls.convert(value).value)
# Verbose Level
class VerbLevel(EnumBase):
OFF = 0
ON = 1
def get_verbose_level():
return VerbLevel.get_value(_C._get_verbose_level)
def set_verbose_level(level):
VerbLevel.set_value(_C._set_verbose_level, level)
# oneDNN Verbose
class OnednnVerbLevel(EnumBase):
OFF = 0
ON = 1
ON_DETAIL = 2
def set_onednn_verbose(level):
st = OnednnVerbLevel.set_value(_C._set_onednn_verbose, level)
assert bool(st), "WARNING: Failed to turn on oneDNN verbose!"
class onednn_verbose(object):
def __init__(self, level):
self.level = OnednnVerbLevel.convert(level)
def __enter__(self):
if self.level != OnednnVerbLevel.OFF:
set_onednn_verbose(self.level)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
set_onednn_verbose(OnednnVerbLevel.OFF)
return False
# oneMKL Verbose
class OnemklVerbLevel(EnumBase):
OFF = 0
ON = 1
ON_SYNC = 2
def set_onemkl_verbose(level):
st = OnemklVerbLevel.set_value(_C._set_onemkl_verbose, level)
assert bool(st), "WARNING: Failed to turn on oneMKL verbose!"
class onemkl_verbose(object):
def __init__(self, level):
self.level = OnemklVerbLevel.convert(level)
def __enter__(self):
if self.level != OnemklVerbLevel.OFF:
set_onemkl_verbose(self.level)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
set_onemkl_verbose(OnemklVerbLevel.OFF)
return False
def optimize(
model,
dtype=None,
optimizer=None,
level="O1",
inplace=False,
conv_bn_folding=None,
linear_bn_folding=None,
weights_prepack=None,
replace_dropout_with_identity=None,
optimize_lstm=None,
split_master_weight_for_bf16=None,
fuse_update_step=None,
auto_kernel_selection=None,
sample_input=None,
graph_mode=None,
):
r"""
torch.xpu.optimize is an alternative of optimize API in Intel® Extension for
PyTorch*, to provide identical usage for XPU device only. The motivation of
adding this alias is to unify the coding style in user scripts base on torch.xpu
modular.
TODO: When finish merge frontend code, add other aurgments describtion here.
Args (Specific default values for XPU device):
inplace (bool): Default set false to save valuable XPU device memory.
weights_prepack (bool): Disabled for XPU device.
sample_input (tuple or torch.Tensor): Disabled for XPU device.
Examples:
>>> # bfloat16 inference case.
>>> model = ...
>>> model.load_state_dict(torch.load(PATH))
>>> model.eval()
>>> optimized_model = torch.xpu.optimize(model, dtype=torch.bfloat16)
>>> # running evaluation step.
>>> # bfloat16 training case.
>>> optimizer = ...
>>> model.train()
>>> optimized_model, optimized_optimizer = torch.xpu.optimize(model, dtype=torch.bfloat16, optimizer=optimizer)
>>> # running training step.
"""
return frontend.optimize(
model,
dtype,
optimizer,
level,
inplace,
conv_bn_folding,
linear_bn_folding,
weights_prepack,
replace_dropout_with_identity,
optimize_lstm,
split_master_weight_for_bf16,
fuse_update_step,
auto_kernel_selection,
sample_input,
graph_mode,
)
class FP32MathMode(EnumBase):
FP32 = intel_extension_for_pytorch._C.XPUFP32MathMode.FP32
TF32 = intel_extension_for_pytorch._C.XPUFP32MathMode.TF32
BF32 = intel_extension_for_pytorch._C.XPUFP32MathMode.BF32
def get_fp32_math_mode():
return FP32MathMode.get_value(intel_extension_for_pytorch._C._get_fp32_math_mode)
def set_fp32_math_mode(mode):
st = FP32MathMode.set_value(
intel_extension_for_pytorch._C._set_fp32_math_mode, mode
)
assert bool(st), "WARNING: Failed to set FP32 math mode!"
class fp32_math_mode(object):
def __init__(self, mode):
self.mode = FP32MathMode.convert(mode)
def __enter__(self):
current_math_mode = get_fp32_math_mode()
if self.mode != current_math_mode:
set_fp32_math_mode(self.mode)
self.mode = current_math_mode
return self
def __exit__(self, exc_type, exc_val, exc_tb):
set_fp32_math_mode(self.mode)
return False
# Sync Execution Mode
def using_sync_mode():
return _C._is_sync_mode()
def enable_sync_mode():
_C._enable_sync_mode()
def disable_sync_mode():
_C._disable_sync_mode()
class sync_mode(OnOff):
def __init__(self):
super().__init__(using_sync_mode, enable_sync_mode, disable_sync_mode)
# Tile Partition As Device
def using_tile_as_device():
return _C._is_tile_as_device_enabled()
# Only work before lazy init
def enable_tile_as_device():
_C._enable_tile_as_device()
# Only work before lazy init
def disable_tile_as_device():
_C._disable_tile_as_device()
################################################################
# PROTOTYPICAL options:
# NOTE: Below options are prototypical.
# They are instable, and may be removed without notice!
################################################################
def has_jit_quantization_save():
return _C._is_jit_quantization_save_enabled()
# oneDNN Layout
def using_onednn_layout():
return _C._is_onednn_layout_enabled()
def is_onednn_layout(tensor):
return torch.ops.torch_ipex.check_onednn_layout(tensor)
def enable_onednn_layout():
_C._enable_onednn_layout()
def disable_onednn_layout():
_C._disable_onednn_layout()
class onednn_layout(OnOff):
def __init__(self):
super().__init__(
using_onednn_layout, enable_onednn_layout, disable_onednn_layout
)
# For several primitive implementations, force to set compute engine
class XPUComputeEng(EnumBase):
RECOMMEND = intel_extension_for_pytorch._C.XPUComputeEng.RECOMMEND
BASIC = intel_extension_for_pytorch._C.XPUComputeEng.BASIC
ONEDNN = intel_extension_for_pytorch._C.XPUComputeEng.ONEDNN
ONEMKL = intel_extension_for_pytorch._C.XPUComputeEng.ONEMKL
XETLA = intel_extension_for_pytorch._C.XPUComputeEng.XETLA
def get_compute_eng():
return XPUComputeEng.get_value(intel_extension_for_pytorch._C._get_compute_eng)
def set_compute_eng(eng):
st = XPUComputeEng.set_value(intel_extension_for_pytorch._C._set_compute_eng, eng)
assert bool(st), "WARNING: Failed to set XPU compute engine!"
class compute_eng(object):
def __init__(self, eng):
self.eng = XPUComputeEng.convert(eng)
def __enter__(self):
current_compute_eng = get_compute_eng()
if self.eng != current_compute_eng:
set_compute_eng(self.eng)
self.eng = current_compute_eng
return self
def __exit__(self, exc_type, exc_val, exc_tb):
set_compute_eng(self.eng)
return False
# Simple Trace
def using_simple_trace():
return _C._is_simple_trace_enabled()
def enable_simple_trace():
_C._enable_simple_trace()
def disable_simple_trace():
_C._disable_simple_trace()
class simple_trace(OnOff):
def __init__(self):
super().__init__(using_simple_trace, enable_simple_trace, disable_simple_trace)