From 50b306ac855a76e35aacf9ab1571ac41b7243ae8 Mon Sep 17 00:00:00 2001 From: tangleintel Date: Fri, 4 Jun 2021 18:28:44 +0800 Subject: [PATCH 01/35] tmp commit --- CMakeLists.txt | 2 +- cmake/CPU.cmake | 3 +- intel_pytorch_extension_py/__init__.py | 58 +- intel_pytorch_extension_py/launch.py | 4 +- intel_pytorch_extension_py/ops/jit.py | 24 +- intel_pytorch_extension_py/ops/mlp.py | 10 +- intel_pytorch_extension_py/ops/nms.py | 4 +- intel_pytorch_extension_py/ops/roi_align.py | 4 +- intel_pytorch_extension_py/ops/to.py | 2 +- tests/cpu/common_ipex_conf.py | 11 +- tests/cpu/common_utils.py | 4 +- tests/cpu/linear_prepack.py | 10 +- tests/cpu/test_bf16_lazy_reorder.py | 571 +++++++++--------- tests/cpu/test_conf.py | 2 +- tests/cpu/test_emb.py | 2 +- tests/cpu/test_int8.py | 13 +- tests/cpu/test_interaction.py | 3 +- tests/cpu/test_jit.py | 20 +- tests/cpu/test_lazy_reorder.py | 140 ++--- tests/cpu/test_mlp.py | 3 +- tests/cpu/test_rn50_cpu_ops.py | 16 +- tests/cpu/test_torch.py | 2 +- .../utils/test_lazy_reorder_with_pattern.py | 54 +- torch_ipex/csrc/CMakeLists.txt | 3 +- 24 files changed, 486 insertions(+), 479 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ec7a5117..401aae9ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_INSTALL_MESSAGE NEVER) # set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -set(PLUGIN_NAME _torch_ipex) +set(PLUGIN_NAME torch_ipex) set(RPATH_VALUE $ORIGIN) set(CMAKE_SKIP_BUILD_RPATH FALSE) diff --git a/cmake/CPU.cmake b/cmake/CPU.cmake index b9056d057..2647fc257 100644 --- a/cmake/CPU.cmake +++ b/cmake/CPU.cmake @@ -169,7 +169,8 @@ ExternalProject_Add(xsmm ) # Compile code with pybind11 set(DPCPP_SRCS ${DPCPP_ATEN_SRCS} ${DPCPP_COMMON_SRCS} ${DPCPP_CPU_SRCS} ${DPCPP_JIT_SRCS}) -pybind11_add_module(${PLUGIN_NAME} SHARED ${DPCPP_SRCS}) +# pybind11_add_module(${PLUGIN_NAME} SHARED ${DPCPP_SRCS}) +add_library(${PLUGIN_NAME} SHARED ${DPCPP_SRCS}) target_link_libraries(${PLUGIN_NAME} PRIVATE ${DPCPP_THIRD_PARTY_ROOT}/xsmm/lib/libxsmm.a) #link_directories(${PYTORCH_INSTALL_DIR}/lib) diff --git a/intel_pytorch_extension_py/__init__.py b/intel_pytorch_extension_py/__init__.py index cbf83ca69..58eaa69d0 100644 --- a/intel_pytorch_extension_py/__init__.py +++ b/intel_pytorch_extension_py/__init__.py @@ -7,7 +7,7 @@ from .optim import * from .ops import * import _torch_ipex as core -core.enable_torch_ccl() +_C.enable_torch_ccl() DEVICE = 'xpu:0' @@ -17,20 +17,20 @@ def __init__(self, mixed_dtype = torch.bfloat16, configure_file = None): self.configure_file = configure_file if self.dtype != torch.bfloat16: - core.clear_indicators() + _C.clear_indicators() # for int8 path, if user give a exited configure file, load it. if self.configure_file != None and self.dtype != torch.bfloat16: if os.path.exists(self.configure_file) and os.stat(self.configure_file).st_size != 0: with open(self.configure_file, 'r') as f: configures = json.load(f) - core.load_indicators_file(configures) + _C.load_indicators_file(configures) else: assert False, 'Can not load a empty file or none existed file, plese first do calibartion step' # for int8 quantization, will save the date after doing calibration step. def save(self, configure_file): - core.add_indicators() - configures = core.get_int8_configures() + _C.add_indicators() + configures = _C.get_int8_configures() with open(configure_file, 'w') as fp: json.dump(configures, fp, indent = 4) @@ -62,16 +62,16 @@ def generator_context(*args, **kwargs): return generator_context def get_auto_mix_precision(): - if core.get_mix_bf16_fp32(): + if _C.get_mix_bf16_fp32(): return torch.bfloat16 - elif core.get_mix_int8_fp32(): + elif _C.get_mix_int8_fp32(): return torch.int8 else: return None def _enable_auto_optimization(mixed_dtype = None, train = False): if mixed_dtype != None: - core.enable_auto_dnnl() + _C.enable_auto_dnnl() enable_auto_mixed_precision(mixed_dtype, train) def enable_auto_mixed_precision(mixed_dtype = torch.bfloat16, train = False): @@ -93,50 +93,50 @@ def _get_auto_optimization(): return get_auto_mix_precision def get_train(): - return core.get_train() + return _C.get_train() class AutoMixPrecision(_DecoratorContextManager): def __init__(self, conf, running_mode = 'inference'): self.pre_mixed_dtype = get_auto_mix_precision() self.pre_running_mode = get_train() - self.pre_calibration_state = core.get_int8_calibration() + self.pre_calibration_state = _C.get_int8_calibration() self.mixed_dtype = conf.dtype self.running_mode = running_mode def __enter__(self): if self.mixed_dtype == torch.bfloat16: - core.enable_mix_bf16_fp32() - core.disable_mix_int8_fp32() + _C.enable_mix_bf16_fp32() + _C.disable_mix_int8_fp32() elif self.mixed_dtype == torch.int8: - core.enable_mix_int8_fp32() - core.disable_mix_bf16_fp32() + _C.enable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() if self.running_mode == 'inference': - core.disable_int8_calibration() + _C.disable_int8_calibration() elif self.running_mode == 'calibration': - core.enable_int8_calibration() + _C.enable_int8_calibration() else: assert False, 'int8 quantization only suport inference and calibration running mode' else: - core.disable_mix_int8_fp32() - core.disable_mix_bf16_fp32() - core.set_execution_mode(train = True if self.running_mode == 'training' else False) + _C.disable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() + _C.set_execution_mode(train = True if self.running_mode == 'training' else False) def __exit__(self, *args): if self.mixed_dtype == torch.int8: if self.running_mode == 'calibration': - core.calibration_reset() + _C.calibration_reset() # restore previous state if self.pre_calibration_state: - core.enable_int8_calibration() + _C.enable_int8_calibration() else: - core.disable_int8_calibration() + _C.disable_int8_calibration() if self.pre_mixed_dtype == torch.bfloat16: - core.enable_mix_bf16_fp32() - core.disable_mix_int8_fp32() + _C.enable_mix_bf16_fp32() + _C.disable_mix_int8_fp32() elif self.pre_mixed_dtype == torch.int8: - core.enable_mix_int8_fp32() - core.disable_mix_bf16_fp32() + _C.enable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() else: - core.disable_mix_int8_fp32() - core.disable_mix_bf16_fp32() - core.set_execution_mode(train = self.pre_running_mode) + _C.disable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() + _C.set_execution_mode(train = self.pre_running_mode) diff --git a/intel_pytorch_extension_py/launch.py b/intel_pytorch_extension_py/launch.py index a7241d0c0..675bcacbd 100644 --- a/intel_pytorch_extension_py/launch.py +++ b/intel_pytorch_extension_py/launch.py @@ -129,8 +129,8 @@ def _get_socket_info(self): for line in self.cpuinfo: if socket_id == int(line[2]): if line[1] not in cur_socket_physical_core: - cur_socket_physical_core.append(line[1]) - cur_socket_logical_core.append(line[0]) + cur_socket_physical__C.append(line[1]) + cur_socket_logical__C.append(line[0]) self.socket_physical_cores.append(cur_socket_physical_core) self.socket_logical_cores.append(cur_socket_logical_core) diff --git a/intel_pytorch_extension_py/ops/jit.py b/intel_pytorch_extension_py/ops/jit.py index b2d882d05..634dad42b 100644 --- a/intel_pytorch_extension_py/ops/jit.py +++ b/intel_pytorch_extension_py/ops/jit.py @@ -13,33 +13,33 @@ def script_(obj, optimize=None, _frames_up=0, _rcb=None): jit_m = orig_script(obj, optimize=optimize, _frames_up=_frames_up+1, _rcb=_rcb) torch.jit.script = script_ - mix_state = torch.bfloat16 if core.get_mix_bf16_fp32() else torch.int8 if core.get_mix_int8_fp32() else None + mix_state = torch.bfloat16 if _C.get_mix_bf16_fp32() else torch.int8 if _C.get_mix_int8_fp32() else None # Disable mix precision in model fusion, since mixed precision cannot # bring any benefits for inference, but will lead to loss of accuracy - core.disable_mix_bf16_fp32() - core.disable_mix_int8_fp32() - if core.get_jit_opt() and hasattr(jit_m, '_c'): + _C.disable_mix_bf16_fp32() + _C.disable_mix_int8_fp32() + if _C.get_jit_opt() and hasattr(jit_m, '_c'): jit_m = wrap_cpp_module(torch._C._jit_pass_fold_convbn(jit_m._c)) if mix_state == torch.bfloat16: - core.enable_mix_bf16_fp32() + _C.enable_mix_bf16_fp32() elif mix_state == torch.int8: - core.enable_mix_int8_fp32() + _C.enable_mix_int8_fp32() return jit_m def trace_(func, example_inputs, *args, **kwargs): # Disable mix precision. torch.jit.trace will check the traced output # against what is expected. Since mix precision will lead to # loss of accuracy, this will raise warning during torch.jit.trace - mix_state = torch.bfloat16 if core.get_mix_bf16_fp32() else torch.int8 if core.get_mix_int8_fp32() else None - core.disable_mix_bf16_fp32() - core.disable_mix_int8_fp32() + mix_state = torch.bfloat16 if _C.get_mix_bf16_fp32() else torch.int8 if _C.get_mix_int8_fp32() else None + _C.disable_mix_bf16_fp32() + _C.disable_mix_int8_fp32() jit_m = orig_trace(func, example_inputs, *args, **kwargs) - if core.get_jit_opt() and hasattr(jit_m, '_c'): + if _C.get_jit_opt() and hasattr(jit_m, '_c'): jit_m = wrap_cpp_module(torch._C._jit_pass_fold_convbn(jit_m._c)) if mix_state == torch.bfloat16: - core.enable_mix_bf16_fp32() + _C.enable_mix_bf16_fp32() elif mix_state == torch.int8: - core.enable_mix_int8_fp32() + _C.enable_mix_int8_fp32() return jit_m diff --git a/intel_pytorch_extension_py/ops/mlp.py b/intel_pytorch_extension_py/ops/mlp.py index 04e354641..3f328d3ad 100644 --- a/intel_pytorch_extension_py/ops/mlp.py +++ b/intel_pytorch_extension_py/ops/mlp.py @@ -8,7 +8,7 @@ class IpexMLPHandle: def __init__(self, N, C, K, bn, bc, bk, dtype, fuse_bias, act_type): - self.handle = core.mlp_create_handle(N, C, K, bn, bc, bk, 1 if dtype == torch.float32 else 2, fuse_bias, act_type) + self.handle = _C.mlp_create_handle(N, C, K, bn, bc, bk, 1 if dtype == torch.float32 else 2, fuse_bias, act_type) self.N = N self.C = C self.K = K @@ -18,11 +18,11 @@ def __init__(self, N, C, K, bn, bc, bk, dtype, fuse_bias, act_type): self.fuse_bias = fuse_bias self.act_type = act_type if act_type == 1: - self.relu_mask_tensor = core.mlp_set_relu_mask(self.handle) + self.relu_mask_tensor = _C.mlp_set_relu_mask(self.handle) def __del__(self): if self.handle: - core.mlp_release_handle(self.handle) + _C.mlp_release_handle(self.handle) self.handle = None self.relu_mask_tensor = None @@ -34,7 +34,7 @@ def forward(ctx, input, weight, bias, handle): input = input.contiguous() weight = weight.contiguous() bias = bias.contiguous() - output = core.mlp_forward(handle.handle, input, weight, bias) + output = _C.mlp_forward(handle.handle, input, weight, bias) #t2 = time.time() #print("XsmmFCFWD: q=%.3f" % ((t2-t1)*1000.0)) ctx.ipex_mlp_handle = handle @@ -49,7 +49,7 @@ def backward(ctx, grad_output): input, weight = ctx.saved_variables #t1 = time.time() grad_output = grad_output.contiguous() - grad_input, grad_weight, grad_bias = core.mlp_backward(handle.handle, grad_output, input, weight) + grad_input, grad_weight, grad_bias = _C.mlp_backward(handle.handle, grad_output, input, weight) #t2 = time.time() #print("XsmmFCBWD: q=%.3f w=%.3f" % ((t2-t1)*1000.0, (t3-t2)*1000.0)) return (grad_input, grad_weight, grad_bias, None) diff --git a/intel_pytorch_extension_py/ops/nms.py b/intel_pytorch_extension_py/ops/nms.py index 1c8b2730c..0b88dfcf6 100644 --- a/intel_pytorch_extension_py/ops/nms.py +++ b/intel_pytorch_extension_py/ops/nms.py @@ -1,4 +1,4 @@ import _torch_ipex as core -nms = core.nms -batch_score_nms = core.batch_score_nms \ No newline at end of file +nms = _C.nms +batch_score_nms = _C.batch_score_nms \ No newline at end of file diff --git a/intel_pytorch_extension_py/ops/roi_align.py b/intel_pytorch_extension_py/ops/roi_align.py index 43bc08a3b..19585ae2f 100644 --- a/intel_pytorch_extension_py/ops/roi_align.py +++ b/intel_pytorch_extension_py/ops/roi_align.py @@ -16,7 +16,7 @@ def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio): ctx.spatial_scale = spatial_scale ctx.sampling_ratio = sampling_ratio ctx.input_shape = input.size() - output = core.roi_align_forward( + output = _C.roi_align_forward( input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio ) return output @@ -29,7 +29,7 @@ def backward(ctx, grad_output): spatial_scale = ctx.spatial_scale sampling_ratio = ctx.sampling_ratio bs, ch, h, w = ctx.input_shape - grad_input = core.roi_align_backward( + grad_input = _C.roi_align_backward( grad_output, rois, spatial_scale, diff --git a/intel_pytorch_extension_py/ops/to.py b/intel_pytorch_extension_py/ops/to.py index 7ea3d79e7..b8f7c5858 100644 --- a/intel_pytorch_extension_py/ops/to.py +++ b/intel_pytorch_extension_py/ops/to.py @@ -19,7 +19,7 @@ def to(module, *args, **kwargs): def mark_param(t): for param in t.parameters(): - core.set_parameter_tensor(param.data) + _C.set_parameter_tensor(param.data) return apply(m, mark_param) diff --git a/tests/cpu/common_ipex_conf.py b/tests/cpu/common_ipex_conf.py index ee0e9ae1b..35fe22bfb 100644 --- a/tests/cpu/common_ipex_conf.py +++ b/tests/cpu/common_ipex_conf.py @@ -1,5 +1,6 @@ import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex +# import intel_pytorch_extension as ipex class AutoMixPrecision(object): def __init__(self, enable_or_not = False, train = False): @@ -27,12 +28,12 @@ def __init__(self, enable_or_not = False): def __enter__(self): if self.enable_or_not: - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() else: - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def __exit__(self, *args, **kwargs): if self.old_value: - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() else: - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() diff --git a/tests/cpu/common_utils.py b/tests/cpu/common_utils.py index fbd42eb37..068a16e59 100644 --- a/tests/cpu/common_utils.py +++ b/tests/cpu/common_utils.py @@ -576,7 +576,7 @@ def skipIfNotRegistered(op_name, message): """Wraps the decorator to hide the import of the `core`. Args: - op_name: Check if this op is registered in `core._REGISTERED_OPERATORS`. + op_name: Check if this op is registered in `_C._REGISTERED_OPERATORS`. message: message to fail with. Usage: @@ -585,7 +585,7 @@ def skipIfNotRegistered(op_name, message): """ try: from caffe2.python import core - skipper = unittest.skipIf(op_name not in core._REGISTERED_OPERATORS, + skipper = unittest.skipIf(op_name not in _C._REGISTERED_OPERATORS, message) except ImportError: skipper = unittest.skip("Cannot import `caffe2.python.core`") diff --git a/tests/cpu/linear_prepack.py b/tests/cpu/linear_prepack.py index d2ab6540d..55b86601e 100644 --- a/tests/cpu/linear_prepack.py +++ b/tests/cpu/linear_prepack.py @@ -2,7 +2,7 @@ import intel_pytorch_extension as ipex from common_utils import int8_calibration -ipex.core.enable_auto_dnnl() +ipex._C.enable_auto_dnnl() ic = 1024 oc = 1024 @@ -30,8 +30,8 @@ def run_linear(auto_mix_conf=None): run_linear(bf16_conf) print(f"back to fp32, {'*' * 50}") - ipex.core.reorder_to_float32(LL.weight) - ipex.core.reorder_to_float32(LL.bias) + ipex._C.reorder_to_float32(LL.weight) + ipex._C.reorder_to_float32(LL.bias) run_linear() print(f"auto-mix for int8, {'*' * 50}") @@ -40,6 +40,6 @@ def run_linear(auto_mix_conf=None): run_linear(int8_conf) print(f"back to fp32, {'*' * 50}") - ipex.core.reorder_to_float32(LL.weight) - ipex.core.reorder_to_float32(LL.bias) + ipex._C.reorder_to_float32(LL.weight) + ipex._C.reorder_to_float32(LL.bias) run_linear() \ No newline at end of file diff --git a/tests/cpu/test_bf16_lazy_reorder.py b/tests/cpu/test_bf16_lazy_reorder.py index 04b979de3..145a451f5 100644 --- a/tests/cpu/test_bf16_lazy_reorder.py +++ b/tests/cpu/test_bf16_lazy_reorder.py @@ -12,7 +12,8 @@ import sys import itertools import torch -import intel_pytorch_extension as ipex +# import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn import torch.backends.cudnn as cudnn @@ -123,9 +124,9 @@ def test_to(self): def check_param(t, is_param): for param in t.parameters(): if is_param: - self.assertTrue(ipex.core.is_parameter_tensor(param.data)) + self.assertTrue(ipex._C.is_parameter_tensor(param.data)) else: - self.assertFalse(ipex.core.is_parameter_tensor(param.data)) + self.assertFalse(ipex._C.is_parameter_tensor(param.data)) apply(m_cpu, check_param, False) apply(m_data_type, check_param, False) @@ -156,9 +157,9 @@ def test_Conv2d_with_cpu(self): with AutoMixPrecision(True): self.assertEqual(in_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(in_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(in_auto_mix)) res_auto_bf16 = conv_auto_mix(in_auto_mix) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_bf16)) self.assertEqual(res_man_bf16.float(), res_auto_bf16.float()) def test_Conv2d_backward(self): @@ -184,10 +185,10 @@ def test_Conv2d_backward(self): with AutoMixPrecision(True, train=True): self.assertEqual(in_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(in_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(in_auto_mix)) out_auto_bf16 = conv_auto_mix(in_auto_mix).sum() out_auto_bf16.backward() - self.assertTrue(ipex.core.is_bf16_dil_tensor(in_auto_mix.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(in_auto_mix.grad)) self.assertEqual(in_man_bf16.grad.float(), in_auto_mix.grad.float()) class TestDeconv(TestCase): @@ -248,19 +249,19 @@ def _test_deconv(self, dims): with AutoDNNL(True), AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_infer)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.weight)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.weight)) if bias: - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.bias)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.bias)) y_auto_mix_infer = module_auto_mix_infer(x_auto_mix_infer) y_auto_mix_infer.sum().backward() if padding - output_padding + stride > 0: - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_infer.grad)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.weight)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_infer.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.weight)) if bias: - self.assertTrue(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.bias)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.bias)) self.assertEqual(y_aten, y_auto_mix_infer, atol=1e-1, rtol=1e-5) @@ -274,21 +275,21 @@ def _test_deconv(self, dims): with AutoDNNL(True), AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.weight)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.weight)) if bias: - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.bias)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.bias)) y_auto_mix_train = module_auto_mix_train(x_auto_mix_train) y_auto_mix_train.sum().backward() if padding - output_padding + stride > 0: - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train.grad)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.weight)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.weight.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.weight)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.weight.grad)) if bias: - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.bias)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.bias.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.bias)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.bias.grad)) self.assertEqual( y_aten, y_auto_mix_train, atol=1e-1, rtol=1e-5) @@ -338,12 +339,12 @@ def test_batch_norm2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) print(res_bf16.device) print(res_auto_mix_inference.device) self.assertEqual(res_bf16.float().to("cpu"), res_auto_mix_inference.to("cpu")) @@ -351,23 +352,23 @@ def test_batch_norm2d(self): # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16) def test_batch_norm2d_backward(self): @@ -388,21 +389,21 @@ def test_batch_norm2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_batch_norm3d(self): @@ -423,34 +424,34 @@ def test_batch_norm3d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_batch_norm3d_backward(self): @@ -472,21 +473,21 @@ def test_batch_norm3d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestLayerNorm(TestCase): @@ -508,34 +509,34 @@ def test_layer_norm(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16) def test_layer_norm_backward(self): @@ -556,21 +557,21 @@ def test_layer_norm_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestRelu(TestCase): @@ -590,34 +591,34 @@ def test_relu(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_relu_(self): @@ -633,28 +634,28 @@ def test_relu_(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) x_auto_mix_inference.relu_() self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(x_man_bf16.float(), x_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) x_auto_mix_train.relu_() self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(x_cpu, x_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) x_auto_mix_train_bf16.relu_() self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(x_man_bf16.float(), x_auto_mix_train_bf16, 1e-3) def test_relu_backward(self): @@ -676,21 +677,21 @@ def test_relu_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestGelu(TestCase): @@ -709,34 +710,34 @@ def test_gelu(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train, 1e-3) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_gelu_backward(self): @@ -757,21 +758,21 @@ def test_gelu_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad.float(), x_auto_mix.grad.float(), 1e-3) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad.float()) class TestShape(TestCase): @@ -786,12 +787,12 @@ def test_slice(self): x_cpu_slice = x_cpu[3:7, 3:7, 5] x_dpcpp = x_cpu.to(device=device) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp)) # the storage should be converted to bf16 on slicing x_dpcpp_slice = x_dpcpp[3:7, 3:7, 5] - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp_slice)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp_slice)) # check shape info self._check_tensor_shape(x_cpu, x_dpcpp) @@ -805,8 +806,8 @@ def test_slice(self): # check sliced data. This should convert the storage back to fp32 self.assertEqual(x_cpu_slice, x_dpcpp_slice, atol=1e-1, rtol=1e-5) self.assertEqual(x_cpu, x_dpcpp, atol=1e-1, rtol=1e-5) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp_slice)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp_slice)) # check shape info self._check_tensor_shape(x_cpu, x_dpcpp) @@ -962,11 +963,11 @@ def test_unbind(self): x_cpu_unbind = torch.unbind(x_cpu) with AutoDNNL(True), AutoMixPrecision(True): - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp)) x_dpcpp_unbind = torch.unbind(x_dpcpp) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp_unbind[0])) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp_unbind[1])) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp_unbind[0])) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp_unbind[1])) self._check_tensor_shape(x_cpu_unbind[0], x_dpcpp_unbind[0]) self._check_tensor_shape(x_cpu_unbind[1], x_dpcpp_unbind[1]) @@ -1019,18 +1020,18 @@ def test_add(self): with AutoMixPrecision(True, train=False): # fp32 + fp32 self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) res_auto_mix_infer = x_auto_mix_a_infer + x_auto_mix_b_infer self.assertEqual(res_auto_mix_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_infer)) self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) self.assertEqual(res_auto_mix_infer.float(), res_man_bf16.float()) @@ -1038,34 +1039,34 @@ def test_add(self): with AutoMixPrecision(True, train=True): # bf16 + bf16 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_bf16 = x_auto_mix_bf16_a + x_auto_mix_bf16_b self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) # bf16 + fp32 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_b.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b)) res_auto_mix_reorder_b = x_auto_mix_bf16_a + x_auto_mix_b self.assertEqual(res_auto_mix_reorder_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_b)) self.assertEqual(res_auto_mix_reorder_b.float(), res_man_bf16.float()) # fp32 + bf16 self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_reorder_a = x_auto_mix_a + x_auto_mix_bf16_b self.assertEqual(res_auto_mix_reorder_a.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_a)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_a)) self.assertEqual(res_auto_mix_reorder_a, res_cpu, atol=1e-1, rtol=1e-5) @@ -1088,18 +1089,18 @@ def test_mul(self): with AutoMixPrecision(True, train=False): # fp32 * fp32 self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) res_auto_mix_infer = x_auto_mix_a_infer * x_auto_mix_b_infer self.assertEqual(res_auto_mix_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_infer)) self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) self.assertEqual(res_auto_mix_infer.float(), res_man_bf16.float()) @@ -1107,34 +1108,34 @@ def test_mul(self): with AutoMixPrecision(True, train=True): # bf16 * bf16 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_bf16 = x_auto_mix_bf16_a * x_auto_mix_bf16_b self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) # bf16 * fp32 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_b.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b)) res_auto_mix_reorder_b = x_auto_mix_bf16_a * x_auto_mix_b self.assertEqual(res_auto_mix_reorder_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_b)) self.assertEqual(res_auto_mix_reorder_b.float(), res_man_bf16.float()) # fp32 * bf16 self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_reorder_a = x_auto_mix_a * x_auto_mix_bf16_b self.assertEqual(res_auto_mix_reorder_a.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_a)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_a)) self.assertEqual(res_auto_mix_reorder_a.float(), res_cpu.float(), atol=1e-1, rtol=1e-5) @@ -1179,16 +1180,16 @@ def test_mul_(self): with AutoMixPrecision(True, train=False): # fp32 + fp32 self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) x_auto_mix_a_infer *= x_auto_mix_b_infer self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) self.assertEqual(x_auto_mix_a_infer.float(), res_man_bf16.float()) @@ -1196,34 +1197,34 @@ def test_mul_(self): with AutoMixPrecision(True, train=True): # bf16 * bf16 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) x_auto_mix_bf16_a *= x_auto_mix_bf16_b self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_a.float(), x_man_bf16_a.float()) # bf16 * fp32 self.assertEqual(x_auto_mix_bf16_a_.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) self.assertEqual(x_auto_mix_b.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b)) x_auto_mix_bf16_a_ *= x_auto_mix_b self.assertEqual(x_auto_mix_bf16_a_.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) self.assertEqual(x_auto_mix_bf16_a_.float(), res_man_bf16.float()) # fp32 * bf16 self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) x_auto_mix_a *= x_auto_mix_bf16_b self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_a, x_cpu_a, atol=1e-1, rtol=1e-5) @@ -1240,12 +1241,12 @@ def test_div(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_bf16 = x_auto_mix_bf16_a / x_auto_mix_bf16_b self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) @@ -1262,12 +1263,12 @@ def test_div_(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) x_auto_mix_bf16_a /= x_auto_mix_bf16_b self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_a.float(), x_man_bf16_a.float()) @@ -1283,10 +1284,10 @@ def test_div_scalar(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) res_auto_mix_bf16 = x_auto_mix_bf16_a / 3.3 self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) def test_div__scalar(self): @@ -1301,10 +1302,10 @@ def test_div__scalar(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) x_auto_mix_bf16_a = x_auto_mix_bf16_a / 3.3 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_a.float(), x_man_bf16_a.float()) class TestLinear(TestCase): @@ -1330,11 +1331,11 @@ def test_linear(self): with AutoMixPrecision(True): res_auto_mix = linear_auto_mix(x_auto_mix) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_linear_backward(self): - ipex.core.set_execution_mode(train = True) + ipex._C.set_execution_mode(train = True) rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1359,10 +1360,10 @@ def test_linear_backward(self): with AutoMixPrecision(True, train=True): self.assertEqual(in_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(in_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(in_auto_mix)) out_auto_bf16 = linear_auto_mix(in_auto_mix).sum() out_auto_bf16.backward() - self.assertTrue(ipex.core.is_bf16_dil_tensor(in_auto_mix.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(in_auto_mix.grad)) self.assertEqual(in_man_bf16.grad.float(), in_auto_mix.grad.float()) class TestPool(TestCase): @@ -1394,34 +1395,34 @@ def test_avg_pool2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_avg_pool2d_backward(self): @@ -1456,21 +1457,21 @@ def test_avg_pool2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_avg_pool3d(self): @@ -1501,34 +1502,34 @@ def test_avg_pool3d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_avg_pool3d_backward(self): @@ -1564,23 +1565,23 @@ def test_avg_pool3d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_adaptive_avg_pool2d(self): @@ -1601,34 +1602,34 @@ def test_adaptive_avg_pool2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_adaptive_avg_pool2d_backward(self): @@ -1652,22 +1653,22 @@ def test_adaptive_avg_pool2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad.float(), x_auto_mix.grad.float()) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad.float()) def test_max_pool2d(self): @@ -1700,34 +1701,34 @@ def test_max_pool2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference.float()) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu.float(), res_auto_mix_train.float()) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_max_pool2d_backward(self): @@ -1761,22 +1762,22 @@ def test_max_pool2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_max_pool3d(self): @@ -1806,34 +1807,34 @@ def test_max_pool3d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_max_pool3d_backward(self): @@ -1867,22 +1868,22 @@ def test_max_pool3d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestIndex(TestCase): @@ -1905,9 +1906,9 @@ def test_index_select(self): with AutoMixPrecision(True): res_auto_mix = index_select_x_auto_mix + index_select_x_auto_mix self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) res_idx_select_auto = torch.index_select(res_auto_mix, 0, indices) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_idx_select_auto)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_idx_select_auto)) self.assertEqual(res_idx_select_auto, res_idx_select_man.float()) def test_index(self): @@ -1934,11 +1935,11 @@ def test_index(self): # with AutoMixPrecision(True): # res_auto_mix = index_x_auto_mix + index_x_auto_mix # self.assertEqual(res_auto_mix.dtype, torch.float) - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) # print(res_auto_mix.device) # print(indices.device) # res_idx_auto = res_auto_mix[indices] - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_idx_auto)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_idx_auto)) # self.assertEqual(res_idx_auto, res_idx_man.float()) class TestSoftMax(TestCase): @@ -1959,34 +1960,34 @@ def test_softmax(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_softmax_backward(self): @@ -2010,23 +2011,23 @@ def test_softmax_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) # TODO # grady and y both fp32 after .sum() - # self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) # self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_log_softmax(self): @@ -2071,34 +2072,34 @@ def test_sigmoid(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_sigmoid_(self): @@ -2114,28 +2115,28 @@ def test_sigmoid_(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) x_auto_mix_inference.sigmoid_() self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(x_man_bf16.float(), x_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) x_auto_mix_train.sigmoid_() self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(x_cpu, x_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) x_auto_mix_train_bf16.sigmoid_() self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(x_man_bf16.float(), x_auto_mix_train_bf16, 1e-3) def test_sigmoid_backward(self): @@ -2157,23 +2158,23 @@ def test_sigmoid_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) # TODO # grady and y both fp32 after .sum() - # self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) # self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestTanh(TestCase): @@ -2193,34 +2194,34 @@ def test_tanh(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_tanh_(self): @@ -2236,28 +2237,28 @@ def test_tanh_(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) x_auto_mix_inference.tanh_() self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(x_man_bf16.float(), x_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) x_auto_mix_train.tanh_() self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(x_cpu, x_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) x_auto_mix_train_bf16.tanh_() self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(x_man_bf16.float(), x_auto_mix_train_bf16, 1e-3) def test_tanh_backward(self): @@ -2279,23 +2280,23 @@ def test_tanh_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) # TODO # grady and y both fp32 after .sum() - # self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) # self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestLinearAlgebraOps(TestCase): @@ -2328,7 +2329,7 @@ def test_mm(self): with AutoMixPrecision(True): res_auto_mix = torch.mm(x_auto_mix_a, x_auto_mix_b) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_mm_out(self): @@ -2342,7 +2343,7 @@ def test_mm_out(self): with AutoMixPrecision(True): torch.mm(x_auto_mix_a, x_auto_mix_b, out=res_auto_mix) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_bmm(self): @@ -2356,7 +2357,7 @@ def test_bmm(self): with AutoMixPrecision(True): res_auto_mix = torch.bmm(x_auto_mix_a, x_auto_mix_b) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_bmm_out(self): @@ -2369,7 +2370,7 @@ def test_bmm_out(self): with AutoMixPrecision(True): torch.bmm(x_auto_mix_a, x_auto_mix_b, out=res_auto_mix) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_addmm(self): @@ -2389,7 +2390,7 @@ def test_addmm(self): with AutoMixPrecision(True): res_auto_mix = torch.addmm(input=add_auto_mix, mat1=x_auto_mix_a, mat2=x_auto_mix_b, alpha=alpha, beta=beta) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_addbmm(self): @@ -2408,7 +2409,7 @@ def test_addbmm(self): with AutoMixPrecision(True): res_auto_mix = torch.addbmm(add_auto_mix, x_auto_mix_a, x_auto_mix_b, beta=beta, alpha=alpha) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_baddbmm(self): @@ -2435,7 +2436,7 @@ def test_baddbmm(self): with AutoMixPrecision(True): res_auto_mix = torch.baddbmm(add_auto_mix, x_auto_mix_a, x_auto_mix_b, beta=beta, alpha=alpha) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) class ConvRelu(nn.Module): @@ -2479,7 +2480,7 @@ def test_save_and_load(self): with AutoDNNL(True), AutoMixPrecision(True): output_dpcpp = model_dpcpp(input_dpcpp) torch.save(output_dpcpp.clone().to('cpu'), 'tensor.pt') - self.assertTrue(ipex.core.is_bf16_dil_tensor(output_dpcpp)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(output_dpcpp)) torch.save(output_dpcpp, 'tensor_dpcpp.pt') self.assertEqual(torch.load('tensor.pt'), torch.load('tensor_dpcpp.pt')) @@ -2926,7 +2927,7 @@ def test_permute(self): x_dpcpp = convert_to_bf16(x_cpu) y_cpu = x_cpu.permute(0, 2, 1, 3) y_dpcpp = x_dpcpp.permute(0, 2, 1, 3) - self.assertTrue(ipex.core.is_bf16_dil_tensor(y_dpcpp)) + self.assertTrue(ipex._C.is_bf16_dil_tensor(y_dpcpp)) self.assertEqual(y_cpu.bfloat16().float(), y_dpcpp) if __name__ == '__main__': diff --git a/tests/cpu/test_conf.py b/tests/cpu/test_conf.py index 2c8dad988..628787b56 100644 --- a/tests/cpu/test_conf.py +++ b/tests/cpu/test_conf.py @@ -4,7 +4,7 @@ from functools import reduce import torch -import _torch_ipex as ipex +import torch_ipex._C as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_emb.py b/tests/cpu/test_emb.py index 64c92d27b..8a64337ab 100644 --- a/tests/cpu/test_emb.py +++ b/tests/cpu/test_emb.py @@ -1,6 +1,6 @@ import torch import torch.nn as nn -import intel_pytorch_extension as ipex +import torch_ipex as ipex import unittest import copy from common_utils import TestCase diff --git a/tests/cpu/test_int8.py b/tests/cpu/test_int8.py index 975f0fb36..e400e115f 100644 --- a/tests/cpu/test_int8.py +++ b/tests/cpu/test_int8.py @@ -15,7 +15,8 @@ from torch.jit._recursive import wrap_cpp_module import copy -import intel_pytorch_extension as ipex +import torch_ipex as ipex +# import intel_pytorch_extension as ipex import torch.nn as nn from torch.nn import Parameter @@ -41,7 +42,7 @@ def test_quantization_status(self): conf = ipex.AmpConf(torch.int8, 'configure.json') with ipex.AutoMixPrecision(conf, running_mode='inference'): y = model1(x1) - self.assertTrue(ipex.core.is_int8_dil_tensor(y)) + self.assertTrue(ipex._C.is_int8_dil_tensor(y)) jsonFile = open('configure.json', 'r') data = json.load(jsonFile) jsonFile.close() @@ -70,7 +71,7 @@ def test_quantization_status(self): with ipex.AutoMixPrecision(conf, running_mode='inference'): y = model2(x2) - self.assertTrue(ipex.core.is_fp32_dil_tensor(y)) + self.assertTrue(ipex._C.is_fp32_dil_tensor(y)) os.remove('configure.json') @@ -85,7 +86,7 @@ def _compare_fp32_int8(self, model, x): with ipex.AutoMixPrecision(conf, running_mode='inference'): y = model(x) - self.assertTrue(ipex.core.is_int8_dil_tensor(y)) + self.assertTrue(ipex._C.is_int8_dil_tensor(y)) self.assertEqual(ref, y, atol=1e-1, rtol=1e-5) os.remove('configure.json') @@ -101,7 +102,7 @@ def _lstm_compare_fp32_int8(self, model, *args): with torch.no_grad(): y, hy = model(*args) - self.assertTrue(ipex.core.is_int8_dil_tensor(y)) + self.assertTrue(ipex._C.is_int8_dil_tensor(y)) # self.assertEqual(ref, y, prec=0.1) self.assertEqual(ref, y, atol=0.1, rtol=1e-5) @@ -201,5 +202,5 @@ def test_lstm(self): if __name__ == '__main__': rand_seed = int(time.time() * 1000000000) torch.manual_seed(rand_seed) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() test = unittest.main() diff --git a/tests/cpu/test_interaction.py b/tests/cpu/test_interaction.py index 8904fdd37..99be0a9e2 100644 --- a/tests/cpu/test_interaction.py +++ b/tests/cpu/test_interaction.py @@ -5,7 +5,8 @@ import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex +# import intel_pytorch_extension as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_jit.py b/tests/cpu/test_jit.py index 9d61d781b..a713b77c2 100644 --- a/tests/cpu/test_jit.py +++ b/tests/cpu/test_jit.py @@ -423,8 +423,8 @@ class Tester(TestCase): def _test_output(self, model, x, kind_in_graph=None, kind_not_in_graph=None): modelName = model.__class__.__name__ - core.disable_jit_opt() - core.disable_mix_bf16_fp32() + _C.disable_jit_opt() + _C.disable_mix_bf16_fp32() model = model.to(device).eval() x = x.to(device) @@ -443,7 +443,7 @@ def _test_output(self, model, x, kind_in_graph=None, kind_not_in_graph=None): self.assertEqual(result, sresult) self.assertEqual(result, tresult) - core.enable_jit_opt() + _C.enable_jit_opt() script_fused_model = torch.jit.script(model) trace_fused_model = torch.jit.trace(model, x) with torch.no_grad(): @@ -471,9 +471,9 @@ def _test_output(self, model, x, kind_in_graph=None, kind_not_in_graph=None): def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None, prec=None): modelName = model.__class__.__name__ - core.enable_auto_dnnl() - core.enable_jit_opt() - core.enable_mix_bf16_fp32() + _C.enable_auto_dnnl() + _C.enable_jit_opt() + _C.enable_mix_bf16_fp32() model = model.to(ipex.DEVICE).eval() x = x.to(ipex.DEVICE) @@ -496,7 +496,7 @@ def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None # disable mix_bf16_fp32 when the calculation is done # to avoid affecting other scripts - core.disable_mix_bf16_fp32() + _C.disable_mix_bf16_fp32() self.assertEqual(fused_sresult, result, atol=1e-1, rtol=1e-5) self.assertEqual(fused_tresult, result, atol=1e-1, rtol=1e-5) @@ -515,8 +515,8 @@ def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None def _test_output_int8(self, model, x, kind_in_graph=None, kind_not_in_graph=None, prec=None): modelName = model.__class__.__name__ - core.enable_auto_dnnl() - core.enable_jit_opt() + _C.enable_auto_dnnl() + _C.enable_jit_opt() model = model.to(ipex.DEVICE).eval() x = x.to(ipex.DEVICE) x2 = x.clone() @@ -935,5 +935,5 @@ def test_manmually_fused_linear_relu(self): if __name__ == '__main__': torch.manual_seed(2020) - core.enable_auto_dnnl() + _C.enable_auto_dnnl() test = unittest.main() diff --git a/tests/cpu/test_lazy_reorder.py b/tests/cpu/test_lazy_reorder.py index 76165f559..95fffc16a 100644 --- a/tests/cpu/test_lazy_reorder.py +++ b/tests/cpu/test_lazy_reorder.py @@ -59,10 +59,10 @@ def test_Conv2d_with_cpu(self): input_cpu = torch.rand((1, 1, 7, 7)) input_dpcpp = input_cpu.to(device=device) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() out_dpcpp = conv_dpcpp(input_dpcpp) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() out_dpcpp_cpu = out_dpcpp.to('cpu') out_cpu = conv_cpu(input_cpu) self.assertEqual(out_dpcpp.size(), out_cpu.size()) @@ -72,7 +72,7 @@ def test_Conv2d_backward(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() with torch.backends.mkldnn.flags(enabled=False): input = torch.rand((1, 1, 7, 7)) for bias in [True, False]: @@ -101,12 +101,12 @@ def _seq_conf(self, device, rand_seed): return out_dpcpp3 def test_seq_conv(self): - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_cpu = self._seq_conf('cpu', rand_seed) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() res_dpcpp = self._seq_conf(device, rand_seed) self.assertEqual(res_cpu, res_dpcpp.to('cpu')) @@ -243,19 +243,19 @@ def _seq_conf(self, device, rand_seed): return out_dpcpp3 def test_seq_deconv(self): - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_cpu = self._seq_conf('cpu', rand_seed) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() res_dpcpp = self._seq_conf(device, rand_seed) self.assertEqual(res_cpu, res_dpcpp.to('cpu')) class TestBinaryOp(TestCase): def test_add(self): # rand_seed = 1599794793172034560: AssertionError: tensor(1.5259e-05) not less than or equal to 1e-05 - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -295,12 +295,12 @@ def _test_add_(self, device, rand_seed): return a1 def test_add_(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_dcpp_dnnl = self._test_add_(device, rand_seed) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() res_dcpp_cpu = self._test_add_(device, rand_seed) res_cpu = self._test_add_("cpu", rand_seed) @@ -308,12 +308,12 @@ def test_add_(self): self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) def test_add_scalar(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() a = torch.rand((8, 8)).to(device=device) a += 2 def test_mul(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -350,7 +350,7 @@ def _test_mul_(self, device, rand_seed): return a def test_mul_(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) a1 = self._test_mul_(device, rand_seed) @@ -361,7 +361,7 @@ def test_binary_propagate_group(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() input = torch.rand((1, 64, 7, 7)) @@ -381,7 +381,7 @@ def test_binary_propagate_group(self): self.assertEqual(y_cpu, y_dpcpp) def test_mixed_format(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -460,7 +460,7 @@ def _test_relu_(self, device, rand_seed): return a def test_relu_(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) a1 = self._test_relu_(device, rand_seed) @@ -468,7 +468,7 @@ def test_relu_(self): self.assertEqual(a2, a1.to('cpu')) def test_relu(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -477,7 +477,7 @@ def test_relu(self): self.assertEqual(torch.relu(x_cpu), torch.relu(x_dpcpp)) def test_relu_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -492,7 +492,7 @@ def test_relu_backward(self): class TestGelu(TestCase): def test_gelu(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -501,7 +501,7 @@ def test_gelu(self): self.assertEqual(F.gelu(x_cpu), F.gelu(x_dpcpp), 0.001) def test_gelu_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -527,7 +527,7 @@ def _test_conv_add_relu_(self, device, rand_seed): return conv_op_output, conv_op_input, add_src def _test_conv_relu_(self, device, rand_seed): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() torch.manual_seed(rand_seed) conv_op = torch.nn.Conv2d(1, 1, (7, 7)).to(device=device) conv_op_input = torch.rand((1, 1, 10, 10)).to(device=device) @@ -538,24 +538,24 @@ def _test_conv_relu_(self, device, rand_seed): def test_conv_relu_(self): rand_seed = int(get_rand_seed()) res_dcpp_dnnl = self._test_conv_relu_(device, rand_seed) - self.assertTrue(ipex.core.is_dil_tensor(res_dcpp_dnnl)) + self.assertTrue(ipex._C.is_dil_tensor(res_dcpp_dnnl)) res_cpu = self._test_conv_relu_("cpu", rand_seed) self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) def test_conv_add_relu_(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_dcpp_dnnl, input_dpcpp_dnnl, _ = self._test_conv_add_relu_(device, rand_seed) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() res_dcpp_cpu, input_dpcpp_cpu, _ = self._test_conv_add_relu_(device, rand_seed) res_cpu, input_cpu, _ = self._test_conv_add_relu_("cpu", rand_seed) self.assertEqual(res_cpu, res_dcpp_cpu.to('cpu')) self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() res_dcpp_dnnl.sum().backward() res_dcpp_cpu.sum().backward() res_cpu.sum().backward() @@ -565,7 +565,7 @@ def test_conv_add_relu_(self): class TestLinearAlgebraOps(TestCase): def test_mm(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -588,7 +588,7 @@ def test_mm(self): self.assertEqual(y_cpu, y_dpcpp) def test_bmm(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -612,7 +612,7 @@ def test_bmm(self): self.assertEqual(y_cpu, y_dpcpp) def test_addmm(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -644,7 +644,7 @@ def test_addmm(self): def test_addbmm(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -675,7 +675,7 @@ def test_addbmm(self): self.assertEqual(res_cpu, res_dpcpp, 1e-4) def test_baddbmm(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -706,7 +706,7 @@ def test_baddbmm(self): class TestLinear(TestCase): def test_linear(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -724,8 +724,8 @@ def test_linear(self): # we should first expose aten::linear, depend on https://github.com/pytorch/pytorch/pull/20039 def test_linear_backward(self): - ipex.core.enable_auto_dnnl() - ipex.core.set_execution_mode(train = True) + ipex._C.enable_auto_dnnl() + ipex._C.set_execution_mode(train = True) rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -748,7 +748,7 @@ def test_linear_backward(self): def test_eikan_linear_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(0) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -773,7 +773,7 @@ def test_eikan_linear_backward(self): class TestPool(TestCase): def test_avg_pool2d(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -792,7 +792,7 @@ def test_avg_pool2d(self): self.assertEqual(avg_pool2d(x_cpu), avg_pool2d(x_dpcpp)) def test_avg_pool3d(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -811,7 +811,7 @@ def test_avg_pool3d(self): self.assertEqual(avg_pool3d(x_cpu), avg_pool3d(x_dpcpp)) def test_avg_pool2d_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -833,7 +833,7 @@ def test_avg_pool2d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_avg_pool3d_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -855,7 +855,7 @@ def test_avg_pool3d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_adaptive_avg_pool2d(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -871,7 +871,7 @@ def test_adaptive_avg_pool2d(self): adaptive_avg_pool2d(x_dpcpp)) def test_adaptive_avg_pool2d_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -888,7 +888,7 @@ def test_adaptive_avg_pool2d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_adaptive_avg_pool2d_not_divisible(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -909,7 +909,7 @@ def test_adaptive_avg_pool2d_not_divisible(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_adaptive_avg_pool2d_backward_not_divisible(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -930,7 +930,7 @@ def test_adaptive_avg_pool2d_backward_not_divisible(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_max_pool2d(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -952,7 +952,7 @@ def test_max_pool2d(self): self.assertEqual(max_pool2d(x_cpu), max_pool2d(x_dpcpp)) def test_max_pool2d_double(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -979,7 +979,7 @@ def test_max_pool2d_double(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_max_pool3d(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1001,7 +1001,7 @@ def test_max_pool3d(self): self.assertEqual(max_pool3d(x_cpu), max_pool3d(x_dpcpp)) def test_max_pool2d_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1023,7 +1023,7 @@ def test_max_pool2d_backward(self): self.assertEqual(x1.grad, x2.grad) def test_max_pool2d_backward_double(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1049,7 +1049,7 @@ def test_max_pool2d_backward_double(self): self.assertEqual(torch.device(device), y2.device) def test_max_pool3d_backward(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1129,7 +1129,7 @@ def test_layer_norm(self): m_dpcpp = copy.deepcopy(m).to(device=device) output = m(input) output_dpcpp = m_dpcpp(input_dpcpp) - self.assertTrue(ipex.core.is_dil_tensor(output_dpcpp)) + self.assertTrue(ipex._C.is_dil_tensor(output_dpcpp)) self.assertEqual(output, output_dpcpp) def test_layer_norm_backward(self): @@ -1217,24 +1217,24 @@ def test_view(self): x_cpu = torch.randn(old_shape) x_dpcpp = x_cpu.to(device=device).clone() - self.assertTrue(ipex.core.is_dil_tensor(x_dpcpp)) - self.assertEqual(ipex.core.get_dil_tensor_sizes(x_dpcpp), [4, 16]) - self.assertEqual(ipex.core.get_dil_tensor_strides(x_dpcpp), [16, 1]) + self.assertTrue(ipex._C.is_dil_tensor(x_dpcpp)) + self.assertEqual(ipex._C.get_dil_tensor_sizes(x_dpcpp), [4, 16]) + self.assertEqual(ipex._C.get_dil_tensor_strides(x_dpcpp), [16, 1]) x_cpu_view = x_cpu.view(new_shape) self.assertEqual(x_cpu_view.size(), [1, 4, 4, 4]) self.assertEqual(x_cpu_view.stride(), [64, 16, 4, 1]) x_dpcpp_view = x_dpcpp.view(new_shape) - self.assertTrue(ipex.core.is_dil_tensor(x_dpcpp_view)) + self.assertTrue(ipex._C.is_dil_tensor(x_dpcpp_view)) y = torch.randn(new_shape) out_cpu = x_cpu_view * y # test if the shape of x_dpcpp_view is compatible with y out_dpcpp = x_dpcpp_view * y.to(device) - self.assertTrue(ipex.core.is_dil_tensor(out_dpcpp)) - self.assertEqual(ipex.core.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4]) - self.assertEqual(ipex.core.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1]) + self.assertTrue(ipex._C.is_dil_tensor(out_dpcpp)) + self.assertEqual(ipex._C.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4]) + self.assertEqual(ipex._C.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1]) self.assertEqual(out_cpu, out_dpcpp) # test if metadata of x_dpcpp has not been altered @@ -1251,22 +1251,22 @@ def test_view(self): # input to the data type of the first input if they are different res_bf16 = src_1 + src_2 res_bf16_other = src_1 + src_2 - self.assertTrue(ipex.core.is_dil_tensor(res_bf16)) - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_bf16)) - self.assertTrue(ipex.core.get_dil_tensor_sizes(res_bf16), [5120, 1, 128]) + self.assertTrue(ipex._C.is_dil_tensor(res_bf16)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_bf16)) + self.assertTrue(ipex._C.get_dil_tensor_sizes(res_bf16), [5120, 1, 128]) self.assertEqual(list(res_bf16.size()), [5120, 1, 128]) res_fp32_view = res_bf16.view(1280, 4, 1, 128) - self.assertTrue(ipex.core.is_dil_tensor(res_bf16)) - self.assertTrue(ipex.core.is_dil_tensor(res_fp32_view)) - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_bf16)) - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_fp32_view)) + self.assertTrue(ipex._C.is_dil_tensor(res_bf16)) + self.assertTrue(ipex._C.is_dil_tensor(res_fp32_view)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_bf16)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_fp32_view)) self.assertEqual(list(res_fp32_view.size()), [1280, 4, 1, 128]) tmp_res = res_bf16 + res_bf16_other - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_bf16)) - # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_fp32_view)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_bf16)) + # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_fp32_view)) tmp_res = res_fp32_view.index_select(0, torch.LongTensor([0, 1])) - self.assertTrue(ipex.core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) - self.assertTrue(ipex.core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) + self.assertTrue(ipex._C.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) + self.assertTrue(ipex._C.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) self.assertEqual(list(tmp_res.size()), [2, 4, 1, 128]) def test_view_blocked(self): @@ -1565,7 +1565,7 @@ def forward(self, x): class TestSave(TestCase): def test_save_and_load_tensor(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1576,7 +1576,7 @@ def test_save_and_load_tensor(self): self.assertEqual(torch.load('tensor.pt'), torch.load('tensor_dpcpp.pt')) def test_save_and_load_model(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -2026,7 +2026,7 @@ def test_upsample_trilinear3d_size(self): class TestPermute(TestCase): def test_permute(self): - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) diff --git a/tests/cpu/test_mlp.py b/tests/cpu/test_mlp.py index 62d085095..7ae04ce80 100644 --- a/tests/cpu/test_mlp.py +++ b/tests/cpu/test_mlp.py @@ -5,7 +5,8 @@ from functools import reduce import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex +# import intel_pytorch_extension as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_rn50_cpu_ops.py b/tests/cpu/test_rn50_cpu_ops.py index a43db2bd7..3c56d2327 100644 --- a/tests/cpu/test_rn50_cpu_ops.py +++ b/tests/cpu/test_rn50_cpu_ops.py @@ -332,7 +332,7 @@ def test_mul(self): a1 = torch.randn((1, 1, 3, 2), device=device) a2 = torch.randn((3, 2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex.core.is_dil_tensor(res1)) + self.assertTrue(ipex._C.is_dil_tensor(res1)) with AutoDNNL(False): a1 = a1.to(device='cpu') a2 = a2.to(device='cpu') @@ -352,7 +352,7 @@ def test_mul(self): a1 = torch.randn((1, 2, 3, 2), device=device) a2 = torch.randn((1, 3, 2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex.core.is_dil_tensor(res1)) + self.assertTrue(ipex._C.is_dil_tensor(res1)) with AutoDNNL(False): a1 = a1.to(device='cpu') a2 = a2.to(device='cpu') @@ -363,7 +363,7 @@ def test_mul(self): a1 = torch.randn((1, 2, 3, 2), device=device) a2 = torch.randn((1, 2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex.core.is_dil_tensor(res1)) + self.assertTrue(ipex._C.is_dil_tensor(res1)) with AutoDNNL(False): a1 = a1.to(device='cpu') a2 = a2.to(device='cpu') @@ -374,7 +374,7 @@ def test_mul(self): a1 = torch.randn((1, 2, 3, 2), device=device) a2 = torch.randn((2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex.core.is_dil_tensor(res1)) + self.assertTrue(ipex._C.is_dil_tensor(res1)) def test_div(self): a1 = torch.tensor([4.2, 6.2], device=device) @@ -467,8 +467,8 @@ def test_view(self): self.assertRaises(RuntimeError, lambda: tensor.view(15, -1, -1)) # TODO(Eikan): DNNL OP does not support >6 dim tensor, so we disable it temporily. When we fix it, we will open it - old_dnnl_conf = ipex.core.get_auto_dnnl() - ipex.core.disable_auto_dnnl() + old_dnnl_conf = ipex._C.get_auto_dnnl() + ipex._C.disable_auto_dnnl() # test view when tensor is not contiguous in every dimension, but only # contiguous dimensions are touched. tensor = torch.rand(4, 2, 5, 1, 6, 2, 9, 3, device=device).transpose(-1, 2).transpose(-2, 3) @@ -495,9 +495,9 @@ def test_view(self): view_size = [1, 1, 2, 1, 4, 3, 1, 1, 9, 1, 2, 1, 2, 3, 1, 5, 1, 1] self.assertEqual(tensor.view(*view_size), contig_tensor.view(*view_size)) if old_dnnl_conf: - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() else: - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() # invalid views self.assertRaises(RuntimeError, lambda: tensor.view(-1)) diff --git a/tests/cpu/test_torch.py b/tests/cpu/test_torch.py index 206b0e962..93f3c4284 100644 --- a/tests/cpu/test_torch.py +++ b/tests/cpu/test_torch.py @@ -164,7 +164,7 @@ def __exit__(self, *args): pass -# This is intentionally prefixed by an underscore. Otherwise pytest will try to +# This is intentionally prefixed by an unders_C. Otherwise pytest will try to # run its methods as test cases. class _TestTorchMixin(object): def _make_tensors(self, shape, val_range=(-100, 100), use_floating=True, use_integral=True): diff --git a/tests/cpu/utils/test_lazy_reorder_with_pattern.py b/tests/cpu/utils/test_lazy_reorder_with_pattern.py index fcbeafc6a..3fe34d22f 100644 --- a/tests/cpu/utils/test_lazy_reorder_with_pattern.py +++ b/tests/cpu/utils/test_lazy_reorder_with_pattern.py @@ -22,14 +22,14 @@ def test_conv_add_relu_000(self): ### 2 reorder rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() conv_op_input = torch.rand((1, 1, 10, 10)).to(device="cpu") conv_op = torch.nn.Conv2d(1, 1, (7, 7)).to(device="cpu") conv_op_output = conv_op(conv_op_input) add_src = torch.rand((1, 1, 4, 4)).to(device="cpu") conv_op_output += add_src conv_op_output.relu_() - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_add_relu_111(self): ### 1 reorder rand_seed = int(get_rand_seed()) @@ -42,20 +42,20 @@ def test_conv_add_relu_111(self): ### 1 reorder conv_op_output_ref += add_src_ref conv_op_output_ref.relu_() - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() conv_op_input = conv_op_input_ref.to(device=ipex_device) conv_op = conv_op_ref.to(device=ipex_device) conv_op_output = conv_op(conv_op_input) add_src = add_src_ref.to(device=ipex_device) conv_op_output += add_src conv_op_output.relu_() - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() self.assertEqual(conv_op_output_ref.size(), conv_op_output.size()) self.assertEqual(conv_op_output_ref, conv_op_output) def test_conv_add_bn_110(self): ##2 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -68,10 +68,10 @@ def test_conv_add_bn_110(self): ##2 reorder conv_op_output += add_src bn_op=torch.nn.BatchNorm2d(1).to(device="cpu") bn_op_output=bn_op(conv_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_bn_add_101(self): ##2 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -82,10 +82,10 @@ def test_conv_bn_add_101(self): ##2 reorder bn_op_output=bn_op(conv_op_output) add_src = torch.rand((1, 1, 4, 4)).to(device=ipex_device) bn_op_output += add_src - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_bn_conv_add_011(self): ##1 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -98,10 +98,10 @@ def test_bn_conv_add_011(self): ##1 reorder add_src = torch.rand((1, 1, 4, 4)).to(device=ipex_device) conv_op_output += add_src - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_bn_pool_100(self): ##2reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -112,12 +112,12 @@ def test_conv_bn_pool_100(self): ##2reorder bn_op_output=bn_op(conv_op_output) pool_op=torch.nn.MaxPool2d(kernel_size=3,stride=2,padding=1).to(device="cpu") pool_op_output=pool_op(bn_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() pool_op_output=pool_op(bn_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_bn_conv_pool_010(self): ##1 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -128,10 +128,10 @@ def test_bn_conv_pool_010(self): ##1 reorder conv_op_output = conv_op(bn_op_output) pool_op=torch.nn.MaxPool2d(kernel_size=3,stride=2,padding=1).to(device="cpu") pool_op_output=pool_op(conv_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_bn_pool_conv_001(self): ##1 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -142,10 +142,10 @@ def test_bn_pool_conv_001(self): ##1 reorder pool_op_output=pool_op(bn_op_output) conv_op = torch.nn.Conv2d(1, 1, (3, 3)).to(device=ipex_device) conv_op_output = conv_op(pool_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_conv_concate(self): ##2 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -155,10 +155,10 @@ def test_conv_conv_concate(self): ##2 reorder conv_op_output1 = conv_op1(conv_op_input) conv_op_output2 = conv_op2(conv_op_input) concate_out=torch.cat([conv_op_output1,conv_op_output2],dim=1).to(device=ipex_device) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_conv_add(self): ##3 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -168,10 +168,10 @@ def test_conv_conv_add(self): ##3 reorder conv_op_output = conv_op(bn_op_output) pool_op=torch.nn.MaxPool2d(kernel_size=3,stride=2,padding=1).to(device="cpu") pool_op_output=pool_op(conv_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_bn_pool_conv_001(self): ##1 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -182,10 +182,10 @@ def test_bn_pool_conv_001(self): ##1 reorder pool_op_output=pool_op(bn_op_output) conv_op = torch.nn.Conv2d(1, 1, (3, 3)).to(device=ipex_device) conv_op_output = conv_op(pool_op_output) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_conv_concate(self): ##2 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -195,10 +195,10 @@ def test_conv_conv_concate(self): ##2 reorder conv_op_output1 = conv_op1(conv_op_input) conv_op_output2 = conv_op2(conv_op_input) concate_out=torch.cat([conv_op_output1,conv_op_output2],dim=1).to(device=ipex_device) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() def test_conv_conv_add(self): ##3 reorder - ipex.core.enable_auto_dnnl() + ipex._C.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -208,4 +208,4 @@ def test_conv_conv_add(self): ##3 reorder conv_op_output1 = conv_op1(conv_op_input) conv_op_output2 = conv_op2(conv_op_input) add_out=torch.add(conv_op_output1,conv_op_output2).to(device=ipex_device) - ipex.core.disable_auto_dnnl() + ipex._C.disable_auto_dnnl() diff --git a/torch_ipex/csrc/CMakeLists.txt b/torch_ipex/csrc/CMakeLists.txt index bb482c5df..48c1dc013 100644 --- a/torch_ipex/csrc/CMakeLists.txt +++ b/torch_ipex/csrc/CMakeLists.txt @@ -5,7 +5,8 @@ LIST(APPEND DPCPP_COMMON_SRCS ${DPCPP_ROOT}/aten_ipex_bridge.cpp ${DPCPP_ROOT}/aten_ipex_type.cpp ${DPCPP_ROOT}/dpcpp_allocator.cpp - ${DPCPP_ROOT}/init_python_bindings.cpp + # ${DPCPP_ROOT}/init_python_bindings.cpp + ${DPCPP_ROOT}/py_init.cpp ${DPCPP_ROOT}/ipex_tensor_impl.cpp ${DPCPP_ROOT}/ipex_sparse_tensor_impl.cpp ${DPCPP_ROOT}/version.cpp From e9d43b77a1f4758ec6c7ba0ab5ef82a1a0a66e7c Mon Sep 17 00:00:00 2001 From: tangleintel Date: Sat, 5 Jun 2021 21:13:28 +0800 Subject: [PATCH 02/35] pass most UT --- setup.py | 89 +++- tests/cpu/common_device_type.py | 2 +- tests/cpu/test_jit.py | 26 +- tests/cpu/test_lazy_reorder.py | 143 +++--- tests/cpu/test_rn50_cpu_ops.py | 2 +- tests/cpu/test_sparse.py | 2 +- tests/cpu/test_torch.py | 2 +- tests/cpu/utils/utils.py | 3 +- torch_ipex/__init__.py | 142 ++++++ torch_ipex/csrc/_C.cpp | 5 + torch_ipex/csrc/py_init.cpp | 243 +++++++++++ torch_ipex/csrc/py_init.h | 12 + torch_ipex/launch.py | 650 ++++++++++++++++++++++++++++ torch_ipex/ops/__init__.py | 16 + torch_ipex/ops/embeddingbag.py | 14 + torch_ipex/ops/frozen_batch_norm.py | 21 + torch_ipex/ops/gru.py | 21 + torch_ipex/ops/interaction.py | 26 ++ torch_ipex/ops/jit.py | 47 ++ torch_ipex/ops/layer_norm.py | 13 + torch_ipex/ops/linear.py | 17 + torch_ipex/ops/lstm.py | 59 +++ torch_ipex/ops/mlp.py | 238 ++++++++++ torch_ipex/ops/nms.py | 4 + torch_ipex/ops/pooling.py | 25 ++ torch_ipex/ops/rnn.py | 415 ++++++++++++++++++ torch_ipex/ops/roi_align.py | 68 +++ torch_ipex/ops/save.py | 31 ++ torch_ipex/ops/to.py | 26 ++ torch_ipex/optim/__init__.py | 2 + torch_ipex/optim/split_sgd.py | 71 +++ torch_ipex/tensor.py | 13 + torch_ipex/version.py | 4 + 33 files changed, 2352 insertions(+), 100 deletions(-) create mode 100644 torch_ipex/__init__.py create mode 100644 torch_ipex/csrc/_C.cpp create mode 100644 torch_ipex/csrc/py_init.cpp create mode 100644 torch_ipex/csrc/py_init.h create mode 100644 torch_ipex/launch.py create mode 100644 torch_ipex/ops/__init__.py create mode 100644 torch_ipex/ops/embeddingbag.py create mode 100644 torch_ipex/ops/frozen_batch_norm.py create mode 100644 torch_ipex/ops/gru.py create mode 100644 torch_ipex/ops/interaction.py create mode 100644 torch_ipex/ops/jit.py create mode 100644 torch_ipex/ops/layer_norm.py create mode 100644 torch_ipex/ops/linear.py create mode 100644 torch_ipex/ops/lstm.py create mode 100644 torch_ipex/ops/mlp.py create mode 100644 torch_ipex/ops/nms.py create mode 100644 torch_ipex/ops/pooling.py create mode 100644 torch_ipex/ops/rnn.py create mode 100644 torch_ipex/ops/roi_align.py create mode 100644 torch_ipex/ops/save.py create mode 100644 torch_ipex/ops/to.py create mode 100644 torch_ipex/optim/__init__.py create mode 100644 torch_ipex/optim/split_sgd.py create mode 100644 torch_ipex/tensor.py create mode 100644 torch_ipex/version.py diff --git a/setup.py b/setup.py index 8b1f7c524..a8ad4b4db 100644 --- a/setup.py +++ b/setup.py @@ -60,6 +60,7 @@ try: import torch + from torch.utils.cpp_extension import include_paths, library_paths except ImportError as e: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch=='+TORCH_VERSION+'+cpu', '-f', 'https://download.pytorch.org/whl/torch_stable.html']) import torch @@ -106,7 +107,14 @@ import inspect import multiprocessing import multiprocessing.pool +import os +import platform +import re import shutil +import subprocess +import sys +import pathlib + pytorch_install_dir = os.path.dirname(os.path.abspath(torch.__file__)) base_dir = os.path.dirname(os.path.abspath(__file__)) @@ -172,9 +180,9 @@ def get_git_head_sha(base_dir): cwd=base_dir).decode('ascii').strip() if os.path.isdir(os.path.join(base_dir, '..', '.git')): torch_git_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], - cwd=os.path.join( - base_dir, - '..')).decode('ascii').strip() + cwd=os.path.join( + base_dir, + '..')).decode('ascii').strip() except Exception: pass return ipex_git_sha, torch_git_sha @@ -192,7 +200,7 @@ def get_build_version(ipex_git_sha): def create_version_files(base_dir, version, ipex_git_sha, torch_git_sha): print('Building torch_ipex version: {}'.format(version)) - py_version_path = os.path.join(base_dir, 'intel_pytorch_extension_py', 'version.py') + py_version_path = os.path.join(base_dir, 'torch_ipex', 'version.py') with open(py_version_path, 'w') as f: f.write('# Autogenerated file, do not edit!\n') f.write("__version__ = '{}'\n".format(version)) @@ -282,10 +290,16 @@ def run(self): if platform.system() == "Windows": raise RuntimeError("Does not support windows") - for ext in self.extensions: - self.build_extension(ext) + ipex_exts = [ext for ext in self.extensions if isinstance(ext, IPEXExt)] + for ext in ipex_exts: + self.build_ipex_extension(ext) + + self.extensions = [ext for ext in self.extensions if not isinstance(ext, IPEXExt)] + super(IPEXBuild, self).run() - def build_extension(self, ext): + def build_ipex_extension(self, ext): + if not isinstance(ext, IPEXExt): + return super(IPEXBuild, self).build_extension(ext) ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) if not os.path.exists(ext.build_dir): os.mkdir(ext.build_dir) @@ -365,6 +379,49 @@ def make_relative_rpath(path): install_requires=[ TORCH_URL, ] +def get_c_module(): + main_compile_args = [] + main_libraries = ['torch_ipex'] + main_link_args = [] + main_sources = ["torch_ipex/csrc/_C.cpp"] + cwd = os.path.dirname(os.path.abspath(__file__)) + # lib_path = os.path.join(cwd, "torch_ipex", "lib") + lib_path = os.path.join(cwd, "build") + lib_path_1 = os.path.join(cwd, "build", "lib.linux-x86_64-3.8") + library_dirs = [lib_path, lib_path_1] + extra_link_args = [] + extra_compile_args = [ + '-Wall', + '-Wextra', + '-Wno-strict-overflow', + '-Wno-unused-parameter', + '-Wno-missing-field-initializers', + '-Wno-write-strings', + '-Wno-unknown-pragmas', + # This is required for Python 2 declarations that are deprecated in 3. + '-Wno-deprecated-declarations', + # Python 2.6 requires -fno-strict-aliasing, see + # http://legacy.python.org/dev/peps/pep-3123/ + # We also depend on it in our code (even Python 3). + '-fno-strict-aliasing', + # Clang has an unfixed bug leading to spurious missing + # braces warnings, see + # https://bugs.llvm.org/show_bug.cgi?id=21629 + '-Wno-missing-braces', + ] + + def make_relative_rpath(path): + return '-Wl,-rpath,$ORIGIN/' + path + + C_ext = Extension("torch_ipex._C", + libraries=main_libraries, + sources=main_sources, + language='c', + extra_compile_args=main_compile_args + extra_compile_args, + include_dirs=include_paths(), + library_dirs=library_dirs, + extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) + return C_ext setup( name='torch_ipex', @@ -377,12 +434,20 @@ def make_relative_rpath(path): #packages=find_packages(exclude=['build']), packages=[ 'torch_ipex', - 'intel_pytorch_extension', - 'intel_pytorch_extension.optim', - 'intel_pytorch_extension.ops'], - package_dir={'intel_pytorch_extension': 'intel_pytorch_extension_py'}, + 'torch_ipex.ops', + 'torch_ipex.optim'], + package_data={ + 'torch_ipex':[ + 'README.md', + 'requirements.txt', + '*.py', + 'lib/*.so', + 'include/*.h', + 'include/core/*.h', + 'include/utils/*.h'] + }, zip_safe=False, - ext_modules=[IPEXExt('_torch_ipex')], + ext_modules=[IPEXExt('torch_ipex'), get_c_module()], cmdclass={ 'build_ext': IPEXBuild, 'clean': IPEXClean, diff --git a/tests/cpu/common_device_type.py b/tests/cpu/common_device_type.py index 805a493bd..fc5c71eca 100644 --- a/tests/cpu/common_device_type.py +++ b/tests/cpu/common_device_type.py @@ -49,7 +49,7 @@ from functools import wraps import unittest import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import copy from common_utils import TestCase, TEST_WITH_ROCM, TEST_MKL, \ skipCUDANonDefaultStreamIf diff --git a/tests/cpu/test_jit.py b/tests/cpu/test_jit.py index a713b77c2..ea246e537 100644 --- a/tests/cpu/test_jit.py +++ b/tests/cpu/test_jit.py @@ -60,8 +60,10 @@ from torch.jit._recursive import wrap_cpp_module import copy -import intel_pytorch_extension as ipex -from intel_pytorch_extension import core +# import intel_pytorch_extension as ipex +import torch_ipex as ipex +import torch_ipex._C as core +# from intel_pytorch_extension import core import torch.nn as nn import torch.backends.cudnn as cudnn @@ -423,8 +425,8 @@ class Tester(TestCase): def _test_output(self, model, x, kind_in_graph=None, kind_not_in_graph=None): modelName = model.__class__.__name__ - _C.disable_jit_opt() - _C.disable_mix_bf16_fp32() + core.disable_jit_opt() + core.disable_mix_bf16_fp32() model = model.to(device).eval() x = x.to(device) @@ -443,7 +445,7 @@ def _test_output(self, model, x, kind_in_graph=None, kind_not_in_graph=None): self.assertEqual(result, sresult) self.assertEqual(result, tresult) - _C.enable_jit_opt() + core.enable_jit_opt() script_fused_model = torch.jit.script(model) trace_fused_model = torch.jit.trace(model, x) with torch.no_grad(): @@ -471,9 +473,9 @@ def _test_output(self, model, x, kind_in_graph=None, kind_not_in_graph=None): def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None, prec=None): modelName = model.__class__.__name__ - _C.enable_auto_dnnl() - _C.enable_jit_opt() - _C.enable_mix_bf16_fp32() + core.enable_auto_dnnl() + core.enable_jit_opt() + core.enable_mix_bf16_fp32() model = model.to(ipex.DEVICE).eval() x = x.to(ipex.DEVICE) @@ -496,7 +498,7 @@ def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None # disable mix_bf16_fp32 when the calculation is done # to avoid affecting other scripts - _C.disable_mix_bf16_fp32() + core.disable_mix_bf16_fp32() self.assertEqual(fused_sresult, result, atol=1e-1, rtol=1e-5) self.assertEqual(fused_tresult, result, atol=1e-1, rtol=1e-5) @@ -515,8 +517,8 @@ def _test_output_bf16(self, model, x, kind_in_graph=None, kind_not_in_graph=None def _test_output_int8(self, model, x, kind_in_graph=None, kind_not_in_graph=None, prec=None): modelName = model.__class__.__name__ - _C.enable_auto_dnnl() - _C.enable_jit_opt() + core.enable_auto_dnnl() + core.enable_jit_opt() model = model.to(ipex.DEVICE).eval() x = x.to(ipex.DEVICE) x2 = x.clone() @@ -935,5 +937,5 @@ def test_manmually_fused_linear_relu(self): if __name__ == '__main__': torch.manual_seed(2020) - _C.enable_auto_dnnl() + core.enable_auto_dnnl() test = unittest.main() diff --git a/tests/cpu/test_lazy_reorder.py b/tests/cpu/test_lazy_reorder.py index 95fffc16a..ef82061bb 100644 --- a/tests/cpu/test_lazy_reorder.py +++ b/tests/cpu/test_lazy_reorder.py @@ -12,7 +12,8 @@ import sys import itertools import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex +import torch_ipex._C as core import contextlib import io @@ -59,10 +60,10 @@ def test_Conv2d_with_cpu(self): input_cpu = torch.rand((1, 1, 7, 7)) input_dpcpp = input_cpu.to(device=device) - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() out_dpcpp = conv_dpcpp(input_dpcpp) - ipex._C.disable_auto_dnnl() + core.disable_auto_dnnl() out_dpcpp_cpu = out_dpcpp.to('cpu') out_cpu = conv_cpu(input_cpu) self.assertEqual(out_dpcpp.size(), out_cpu.size()) @@ -72,7 +73,7 @@ def test_Conv2d_backward(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() with torch.backends.mkldnn.flags(enabled=False): input = torch.rand((1, 1, 7, 7)) for bias in [True, False]: @@ -101,12 +102,12 @@ def _seq_conf(self, device, rand_seed): return out_dpcpp3 def test_seq_conv(self): - ipex._C.disable_auto_dnnl() + core.disable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_cpu = self._seq_conf('cpu', rand_seed) - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() res_dpcpp = self._seq_conf(device, rand_seed) self.assertEqual(res_cpu, res_dpcpp.to('cpu')) @@ -243,19 +244,19 @@ def _seq_conf(self, device, rand_seed): return out_dpcpp3 def test_seq_deconv(self): - ipex._C.disable_auto_dnnl() + core.disable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_cpu = self._seq_conf('cpu', rand_seed) - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() res_dpcpp = self._seq_conf(device, rand_seed) self.assertEqual(res_cpu, res_dpcpp.to('cpu')) class TestBinaryOp(TestCase): def test_add(self): # rand_seed = 1599794793172034560: AssertionError: tensor(1.5259e-05) not less than or equal to 1e-05 - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -295,12 +296,12 @@ def _test_add_(self, device, rand_seed): return a1 def test_add_(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_dcpp_dnnl = self._test_add_(device, rand_seed) - ipex._C.disable_auto_dnnl() + core.disable_auto_dnnl() res_dcpp_cpu = self._test_add_(device, rand_seed) res_cpu = self._test_add_("cpu", rand_seed) @@ -308,12 +309,12 @@ def test_add_(self): self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) def test_add_scalar(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() a = torch.rand((8, 8)).to(device=device) a += 2 def test_mul(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -350,7 +351,7 @@ def _test_mul_(self, device, rand_seed): return a def test_mul_(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) a1 = self._test_mul_(device, rand_seed) @@ -361,7 +362,7 @@ def test_binary_propagate_group(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() input = torch.rand((1, 64, 7, 7)) @@ -381,7 +382,7 @@ def test_binary_propagate_group(self): self.assertEqual(y_cpu, y_dpcpp) def test_mixed_format(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -460,7 +461,7 @@ def _test_relu_(self, device, rand_seed): return a def test_relu_(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) a1 = self._test_relu_(device, rand_seed) @@ -468,7 +469,7 @@ def test_relu_(self): self.assertEqual(a2, a1.to('cpu')) def test_relu(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -477,7 +478,7 @@ def test_relu(self): self.assertEqual(torch.relu(x_cpu), torch.relu(x_dpcpp)) def test_relu_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -492,7 +493,7 @@ def test_relu_backward(self): class TestGelu(TestCase): def test_gelu(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -501,7 +502,7 @@ def test_gelu(self): self.assertEqual(F.gelu(x_cpu), F.gelu(x_dpcpp), 0.001) def test_gelu_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -527,7 +528,7 @@ def _test_conv_add_relu_(self, device, rand_seed): return conv_op_output, conv_op_input, add_src def _test_conv_relu_(self, device, rand_seed): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() torch.manual_seed(rand_seed) conv_op = torch.nn.Conv2d(1, 1, (7, 7)).to(device=device) conv_op_input = torch.rand((1, 1, 10, 10)).to(device=device) @@ -538,24 +539,24 @@ def _test_conv_relu_(self, device, rand_seed): def test_conv_relu_(self): rand_seed = int(get_rand_seed()) res_dcpp_dnnl = self._test_conv_relu_(device, rand_seed) - self.assertTrue(ipex._C.is_dil_tensor(res_dcpp_dnnl)) + self.assertTrue(core.is_dil_tensor(res_dcpp_dnnl)) res_cpu = self._test_conv_relu_("cpu", rand_seed) self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) def test_conv_add_relu_(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_dcpp_dnnl, input_dpcpp_dnnl, _ = self._test_conv_add_relu_(device, rand_seed) - ipex._C.disable_auto_dnnl() + core.disable_auto_dnnl() res_dcpp_cpu, input_dpcpp_cpu, _ = self._test_conv_add_relu_(device, rand_seed) res_cpu, input_cpu, _ = self._test_conv_add_relu_("cpu", rand_seed) self.assertEqual(res_cpu, res_dcpp_cpu.to('cpu')) self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() res_dcpp_dnnl.sum().backward() res_dcpp_cpu.sum().backward() res_cpu.sum().backward() @@ -565,7 +566,7 @@ def test_conv_add_relu_(self): class TestLinearAlgebraOps(TestCase): def test_mm(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -588,7 +589,7 @@ def test_mm(self): self.assertEqual(y_cpu, y_dpcpp) def test_bmm(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -612,7 +613,7 @@ def test_bmm(self): self.assertEqual(y_cpu, y_dpcpp) def test_addmm(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -644,7 +645,7 @@ def test_addmm(self): def test_addbmm(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -675,7 +676,7 @@ def test_addbmm(self): self.assertEqual(res_cpu, res_dpcpp, 1e-4) def test_baddbmm(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -706,7 +707,7 @@ def test_baddbmm(self): class TestLinear(TestCase): def test_linear(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -724,8 +725,8 @@ def test_linear(self): # we should first expose aten::linear, depend on https://github.com/pytorch/pytorch/pull/20039 def test_linear_backward(self): - ipex._C.enable_auto_dnnl() - ipex._C.set_execution_mode(train = True) + core.enable_auto_dnnl() + core.set_execution_mode(train = True) rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -748,7 +749,7 @@ def test_linear_backward(self): def test_eikan_linear_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(0) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -773,7 +774,7 @@ def test_eikan_linear_backward(self): class TestPool(TestCase): def test_avg_pool2d(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -792,7 +793,7 @@ def test_avg_pool2d(self): self.assertEqual(avg_pool2d(x_cpu), avg_pool2d(x_dpcpp)) def test_avg_pool3d(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -811,7 +812,7 @@ def test_avg_pool3d(self): self.assertEqual(avg_pool3d(x_cpu), avg_pool3d(x_dpcpp)) def test_avg_pool2d_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -833,7 +834,7 @@ def test_avg_pool2d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_avg_pool3d_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -855,7 +856,7 @@ def test_avg_pool3d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_adaptive_avg_pool2d(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -871,7 +872,7 @@ def test_adaptive_avg_pool2d(self): adaptive_avg_pool2d(x_dpcpp)) def test_adaptive_avg_pool2d_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -888,7 +889,7 @@ def test_adaptive_avg_pool2d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_adaptive_avg_pool2d_not_divisible(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -909,7 +910,7 @@ def test_adaptive_avg_pool2d_not_divisible(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_adaptive_avg_pool2d_backward_not_divisible(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -930,7 +931,7 @@ def test_adaptive_avg_pool2d_backward_not_divisible(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_max_pool2d(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -952,7 +953,7 @@ def test_max_pool2d(self): self.assertEqual(max_pool2d(x_cpu), max_pool2d(x_dpcpp)) def test_max_pool2d_double(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -979,7 +980,7 @@ def test_max_pool2d_double(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_max_pool3d(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1001,7 +1002,7 @@ def test_max_pool3d(self): self.assertEqual(max_pool3d(x_cpu), max_pool3d(x_dpcpp)) def test_max_pool2d_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1023,7 +1024,7 @@ def test_max_pool2d_backward(self): self.assertEqual(x1.grad, x2.grad) def test_max_pool2d_backward_double(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1049,7 +1050,7 @@ def test_max_pool2d_backward_double(self): self.assertEqual(torch.device(device), y2.device) def test_max_pool3d_backward(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1129,7 +1130,7 @@ def test_layer_norm(self): m_dpcpp = copy.deepcopy(m).to(device=device) output = m(input) output_dpcpp = m_dpcpp(input_dpcpp) - self.assertTrue(ipex._C.is_dil_tensor(output_dpcpp)) + self.assertTrue(core.is_dil_tensor(output_dpcpp)) self.assertEqual(output, output_dpcpp) def test_layer_norm_backward(self): @@ -1217,24 +1218,24 @@ def test_view(self): x_cpu = torch.randn(old_shape) x_dpcpp = x_cpu.to(device=device).clone() - self.assertTrue(ipex._C.is_dil_tensor(x_dpcpp)) - self.assertEqual(ipex._C.get_dil_tensor_sizes(x_dpcpp), [4, 16]) - self.assertEqual(ipex._C.get_dil_tensor_strides(x_dpcpp), [16, 1]) + self.assertTrue(core.is_dil_tensor(x_dpcpp)) + self.assertEqual(core.get_dil_tensor_sizes(x_dpcpp), [4, 16]) + self.assertEqual(core.get_dil_tensor_strides(x_dpcpp), [16, 1]) x_cpu_view = x_cpu.view(new_shape) self.assertEqual(x_cpu_view.size(), [1, 4, 4, 4]) self.assertEqual(x_cpu_view.stride(), [64, 16, 4, 1]) x_dpcpp_view = x_dpcpp.view(new_shape) - self.assertTrue(ipex._C.is_dil_tensor(x_dpcpp_view)) + self.assertTrue(core.is_dil_tensor(x_dpcpp_view)) y = torch.randn(new_shape) out_cpu = x_cpu_view * y # test if the shape of x_dpcpp_view is compatible with y out_dpcpp = x_dpcpp_view * y.to(device) - self.assertTrue(ipex._C.is_dil_tensor(out_dpcpp)) - self.assertEqual(ipex._C.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4]) - self.assertEqual(ipex._C.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1]) + self.assertTrue(core.is_dil_tensor(out_dpcpp)) + self.assertEqual(core.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4]) + self.assertEqual(core.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1]) self.assertEqual(out_cpu, out_dpcpp) # test if metadata of x_dpcpp has not been altered @@ -1251,22 +1252,22 @@ def test_view(self): # input to the data type of the first input if they are different res_bf16 = src_1 + src_2 res_bf16_other = src_1 + src_2 - self.assertTrue(ipex._C.is_dil_tensor(res_bf16)) - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_bf16)) - self.assertTrue(ipex._C.get_dil_tensor_sizes(res_bf16), [5120, 1, 128]) + self.assertTrue(core.is_dil_tensor(res_bf16)) + # self.assertTrue(core.is_bf16_dil_tensor(res_bf16)) + self.assertTrue(core.get_dil_tensor_sizes(res_bf16), [5120, 1, 128]) self.assertEqual(list(res_bf16.size()), [5120, 1, 128]) res_fp32_view = res_bf16.view(1280, 4, 1, 128) - self.assertTrue(ipex._C.is_dil_tensor(res_bf16)) - self.assertTrue(ipex._C.is_dil_tensor(res_fp32_view)) - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_bf16)) - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_fp32_view)) + self.assertTrue(core.is_dil_tensor(res_bf16)) + self.assertTrue(core.is_dil_tensor(res_fp32_view)) + # self.assertTrue(core.is_bf16_dil_tensor(res_bf16)) + # self.assertTrue(core.is_bf16_dil_tensor(res_fp32_view)) self.assertEqual(list(res_fp32_view.size()), [1280, 4, 1, 128]) tmp_res = res_bf16 + res_bf16_other - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_bf16)) - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_fp32_view)) + # self.assertTrue(core.is_bf16_dil_tensor(res_bf16)) + # self.assertTrue(core.is_bf16_dil_tensor(res_fp32_view)) tmp_res = res_fp32_view.index_select(0, torch.LongTensor([0, 1])) - self.assertTrue(ipex._C.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) - self.assertTrue(ipex._C.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) + self.assertTrue(core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) + self.assertTrue(core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) self.assertEqual(list(tmp_res.size()), [2, 4, 1, 128]) def test_view_blocked(self): @@ -1565,7 +1566,7 @@ def forward(self, x): class TestSave(TestCase): def test_save_and_load_tensor(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1576,7 +1577,7 @@ def test_save_and_load_tensor(self): self.assertEqual(torch.load('tensor.pt'), torch.load('tensor_dpcpp.pt')) def test_save_and_load_model(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -2026,7 +2027,7 @@ def test_upsample_trilinear3d_size(self): class TestPermute(TestCase): def test_permute(self): - ipex._C.enable_auto_dnnl() + core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) diff --git a/tests/cpu/test_rn50_cpu_ops.py b/tests/cpu/test_rn50_cpu_ops.py index 3c56d2327..9f3b3534c 100644 --- a/tests/cpu/test_rn50_cpu_ops.py +++ b/tests/cpu/test_rn50_cpu_ops.py @@ -55,7 +55,7 @@ from functools import reduce import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex from common_ipex_conf import AutoMixPrecision, AutoDNNL import torch.nn as nn diff --git a/tests/cpu/test_sparse.py b/tests/cpu/test_sparse.py index 6b89ebc23..53f494d7c 100644 --- a/tests/cpu/test_sparse.py +++ b/tests/cpu/test_sparse.py @@ -2,7 +2,7 @@ import copy import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn from common_utils import TestCase from numbers import Number diff --git a/tests/cpu/test_torch.py b/tests/cpu/test_torch.py index 93f3c4284..0166d5f3c 100644 --- a/tests/cpu/test_torch.py +++ b/tests/cpu/test_torch.py @@ -83,7 +83,7 @@ skipIf, skipCPUIfNoLapack, skipCUDAIfNoMagma, skipCUDAIfRocm, onlyCUDA, onlyCPU, \ dtypes, dtypesIfCUDA, deviceCountAtLeast, skipCUDAIf, precisionOverride, ipex import torch.backends.quantized -import intel_pytorch_extension as ipex +import torch_ipex as ipex # load_tests from common_utils is used to automatically filter tests for diff --git a/tests/cpu/utils/utils.py b/tests/cpu/utils/utils.py index 7e754a353..5038eb64b 100644 --- a/tests/cpu/utils/utils.py +++ b/tests/cpu/utils/utils.py @@ -2,7 +2,8 @@ import unittest from torch.testing._internal import expecttest from functools import wraps -import intel_pytorch_extension as ipex +# import intel_pytorch_extension as ipex +import torch_ipex as ipex class VerboseTestCase(expecttest.TestCase): def __init__(self, method_name='runTest'): diff --git a/torch_ipex/__init__.py b/torch_ipex/__init__.py new file mode 100644 index 000000000..bbb61648a --- /dev/null +++ b/torch_ipex/__init__.py @@ -0,0 +1,142 @@ +import os +import json +import warnings +import torch +from .version import __version__ +from .tensor import * +from .optim import * +from .ops import * +from . import _C + +_C.enable_torch_ccl() +DEVICE = 'xpu:0' + +class AmpConf(object): + def __init__(self, mixed_dtype = torch.bfloat16, configure_file = None): + self.dtype = mixed_dtype + self.configure_file = configure_file + + if self.dtype != torch.bfloat16: + _C.clear_indicators() + # for int8 path, if user give a exited configure file, load it. + if self.configure_file != None and self.dtype != torch.bfloat16: + if os.path.exists(self.configure_file) and os.stat(self.configure_file).st_size != 0: + with open(self.configure_file, 'r') as f: + configures = json.load(f) + _C.load_indicators_file(configures) + else: + assert False, 'Can not load a empty file or none existed file, plese first do calibartion step' + + # for int8 quantization, will save the date after doing calibration step. + def save(self, configure_file): + _C.add_indicators() + configures = _C.get_int8_configures() + with open(configure_file, 'w') as fp: + json.dump(configures, fp, indent = 4) + +class _DecoratorContextManager: + """Allow a context manager to be used as a decorator, copy form pytorch FW""" + + def __call__(self, func): + if inspect.isgeneratorfunction(func): + return self._wrap_generator(func) + + @functools.wraps(func) + def decorate_context(*args, **kwargs): + with self: + return func(*args, **kwargs) + return decorate_context + + def _wrap_generator(self, func): + """Wrap each generator invocation with the context manager""" + @functools.wraps(func) + def generator_context(*args, **kwargs): + gen = func(*args, **kwargs) + while True: + try: + with self: + x = next(gen) + yield x + except StopIteration: + break + return generator_context + +def get_auto_mix_precision(): + if _C.get_mix_bf16_fp32(): + return torch.bfloat16 + elif _C.get_mix_int8_fp32(): + return torch.int8 + else: + return None + +def _enable_auto_optimization(mixed_dtype = None, train = False): + if mixed_dtype != None: + _C.enable_auto_dnnl() + enable_auto_mixed_precision(mixed_dtype, train) + +def enable_auto_mixed_precision(mixed_dtype = torch.bfloat16, train = False): + r""" Enable auto-mixed-precision to improve performance for global scope. + + The auto-mixed-precision auto reorders the tensor to the specified low precision data type. + You don't need to convert the input tensors and the model to the specified data type manually, + the extension will do it automatically and then dispatch the extension backend to accelerate + computation + + Args: + mixed_dtype(torch.dtype): Auto reorder the input tensors to the specified low precision data type + and dispatch to oneDNN backend for computation, can be torch.bfloat16 or None. + """ + running_mode = 'training' if train else 'inference' + AutoMixPrecision(AmpConf(mixed_dtype), running_mode).__enter__() + +def _get_auto_optimization(): + return get_auto_mix_precision + +def get_train(): + return _C.get_train() + +class AutoMixPrecision(_DecoratorContextManager): + def __init__(self, conf, running_mode = 'inference'): + self.pre_mixed_dtype = get_auto_mix_precision() + self.pre_running_mode = get_train() + self.pre_calibration_state = _C.get_int8_calibration() + self.mixed_dtype = conf.dtype + self.running_mode = running_mode + + def __enter__(self): + if self.mixed_dtype == torch.bfloat16: + _C.enable_mix_bf16_fp32() + _C.disable_mix_int8_fp32() + elif self.mixed_dtype == torch.int8: + _C.enable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() + if self.running_mode == 'inference': + _C.disable_int8_calibration() + elif self.running_mode == 'calibration': + _C.enable_int8_calibration() + else: + assert False, 'int8 quantization only suport inference and calibration running mode' + else: + _C.disable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() + _C.set_execution_mode(train = True if self.running_mode == 'training' else False) + + def __exit__(self, *args): + if self.mixed_dtype == torch.int8: + if self.running_mode == 'calibration': + _C.calibration_reset() + # restore previous state + if self.pre_calibration_state: + _C.enable_int8_calibration() + else: + _C.disable_int8_calibration() + if self.pre_mixed_dtype == torch.bfloat16: + _C.enable_mix_bf16_fp32() + _C.disable_mix_int8_fp32() + elif self.pre_mixed_dtype == torch.int8: + _C.enable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() + else: + _C.disable_mix_int8_fp32() + _C.disable_mix_bf16_fp32() + _C.set_execution_mode(train = self.pre_running_mode) \ No newline at end of file diff --git a/torch_ipex/csrc/_C.cpp b/torch_ipex/csrc/_C.cpp new file mode 100644 index 000000000..333a27b4b --- /dev/null +++ b/torch_ipex/csrc/_C.cpp @@ -0,0 +1,5 @@ +#include "py_init.h" + +PYBIND11_MODULE(_C, m) { + torch_ipex::InitIpexBindings(m); +} \ No newline at end of file diff --git a/torch_ipex/csrc/py_init.cpp b/torch_ipex/csrc/py_init.cpp new file mode 100644 index 000000000..8e3d6e962 --- /dev/null +++ b/torch_ipex/csrc/py_init.cpp @@ -0,0 +1,243 @@ +#include +#include "version.h" + +#include +#include +#include + +#include +#include +#include +#include +#include "jit/fusion_pass.h" + +#include +#include +#include +#include + +#include "aten_ipex_type.h" +#include "utils.h" +#include "auto_opt_config.h" + +#include "cpu/dil/dil.hpp" +#include "cpu/dbl/Common.h" +#include "cpu/ShadeDataContext.h" +#include "cpu/ExtendOPs.h" +#include "cpu/MlpOPs.h" +#include "cpu/ExternalOPs.h" +#include "cpu/FusionOPs.h" +#include "cpu/int8/Config.h" +#include "cpu/int8/quantization/Observer.h" +#include "ProcessGroupCCL.hpp" +#include + +namespace torch_ipex { +// namespace { + +py::object GetRevisions() { + auto py_dict = py::dict(); + py_dict["ipex"] = std::string(IPEX_GITREV); + py_dict["torch"] = std::string(TORCH_GITREV); + return py_dict; +} + +void setAutoDNNL(bool val) { + AutoOptConfig::singleton().set_auto_dnnl(val); +} + +void setParameterTensor(const at::Tensor &tensor) { + cpu::ShadeDataContext::setParameterTensor(tensor); +} + +bool isParameterTensor(const at::Tensor &tensor) { + return cpu::ShadeDataContext::isParameterTensor(tensor); +} + +/// **** Only for unit test **** +bool isDilTensor(const at::Tensor &tensor) { + return cpu::ShadeDataContext::isDilTensor(tensor); +} + +bool isINT8DilTensor(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_data_type() == dil::data_type::s8 + || dil_tensor.get_data_type() == dil::data_type::u8; + } + + return false; +} + +bool isBF16DilTensor(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_data_type() == dil::data_type::bf16; + } + + return false; +} + +bool isFP32DilTensor(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_data_type() == dil::data_type::f32; + } + + return false; +} + +dil::dims getDilStorageSizes(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_dims(); + } + return dil::dims(); +} + +dil::dims getDilStorageStrides(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_strides(); + } + return dil::dims(); +} + +void reorder_to_float32(at::Tensor &tensor){ + cpu::dbl::comm::reorder_to_dtype(tensor, at::kFloat); +} +/// **************************** + +void InitIpexModuleBindings(py::module m) { + m.def("_get_git_revs", []() { return GetRevisions(); }); + m.def("enable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(true); }); + m.def("disable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(false); }); + m.def("get_auto_dnnl", []() { return AutoOptConfig::singleton().get_auto_dnnl(); }); + m.def("enable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(true); }); + m.def("disable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(false); }); + m.def("get_mix_bf16_fp32", []() { return AutoOptConfig::singleton().get_mix_bf16_fp32(); }); + m.def("packed_add_", + [](at::Tensor &top_half, at::Tensor &bot_half, + const at::Tensor &grad, float alpha) { + AtenIpexTypeExt::packed_add_(top_half, bot_half, grad, alpha); + }); + m.def("mlp_forward", &AtenIpexTypeMLPExt::forward); + m.def("mlp_backward", &AtenIpexTypeMLPExt::backward); + m.def("mlp_create_handle", &AtenIpexTypeMLPExt::create_handle); + m.def("mlp_set_relu_mask", &AtenIpexTypeMLPExt::set_relu_mask); + m.def("mlp_release_handle", &AtenIpexTypeMLPExt::release_handle); + m.def("is_dil_tensor", &isDilTensor); + m.def("is_int8_dil_tensor", &isINT8DilTensor); + m.def("is_bf16_dil_tensor", &isBF16DilTensor); + m.def("is_fp32_dil_tensor", &isFP32DilTensor); + m.def("get_dil_tensor_sizes", &getDilStorageSizes); + m.def("get_dil_tensor_strides", &getDilStorageStrides); + m.def("set_parameter_tensor", &setParameterTensor); + m.def("is_parameter_tensor", &isParameterTensor); + m.def("reorder_to_float32", &reorder_to_float32); + m.def("enable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(true); }); + m.def("disable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(false); }); + m.def("get_jit_opt", []() { return AutoOptConfig::singleton().get_jit_fuse(); }); + m.def("set_execution_mode", [](bool train) { AutoOptConfig::singleton().set_train(train); }, py::arg("train")); + m.def("get_train", []() { return AutoOptConfig::singleton().get_train(); }); + + // int8 path + + m.def("enable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(true); }); + m.def("disable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(false); }); + m.def("get_mix_int8_fp32", []() { return AutoOptConfig::singleton().get_mix_int8_fp32(); }); + m.def("enable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(true); }); + m.def("disable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(false); }); + m.def("get_int8_calibration", + []() { AutoOptConfig::singleton().get_int8_calibration(); }); + m.def("calibration_reset", []() { Int8OptConfig::calibration_reset(); }); + m.def("add_indicators", + []() { Int8OptConfig::get_config().add_indicators(); }); + m.def("clear_indicators", + []() { Int8OptConfig::get_config().clear_indicators(); }); + // clear indicators for case having many scopes which have different structure + m.def("get_int8_configures", []() { + py::list output_list; + auto indicators = Int8OptConfig::get_config().get_indicators(); + IPEX_CHECK(indicators.size() > 0, "can't load a empty indicators, please first do calibration step"); + for (auto indicator: indicators) { + py::dict d; + d["id"] = indicator.get_indicator_id(); + d["name"] = indicator.get_indicator_name(); + d["algorithm"] = indicator.get_indicator_algorithm(); + d["weight_granularity"] = indicator.get_indicator_weight_granularity(); + std::vector i_scale, o_scale; + std::tie(i_scale, o_scale) = indicator.get_indicator_scales(); + d["inputs_scale"] = i_scale; + d["outputs_scale"] = o_scale; + std::vector i_uint8_used, o_uint8_used; + std::tie(i_uint8_used, o_uint8_used)= indicator.get_indicator_uint8_status(); + d["inputs_uint8_used"] = i_uint8_used; + d["outputs_uint8_used"] = o_uint8_used; + d["quantized"] = indicator.get_indicator_quantized_status(); + output_list.append(d); + } + return output_list; } ); + m.def("load_indicators_file", [](const py::list &l) { + IPEX_CHECK( + py::len(l) > 0, + "can't load a empty configures, please first do calibration step"); + std::vector indicators; + for (py::handle i : l) { + int64_t id = py::cast(i["id"]); + std::string op_name = py::cast(i["name"]); + std::string algorithm = py::cast(i["algorithm"]); + std::string weight_granularity = + py::cast(i["weight_granularity"]); + std::vector i_scale = + py::cast>(i["inputs_scale"]); + std::vector o_scale = + py::cast>(i["outputs_scale"]); + std::vector i_uint8_used = + py::cast>(i["inputs_uint8_used"]); + std::vector o_uint8_used = + py::cast>(i["outputs_uint8_used"]); + bool quantized = py::cast(i["quantized"]); + Indicator temp(id, op_name, algorithm, weight_granularity, i_scale, + o_scale, i_uint8_used, o_uint8_used, quantized); + indicators.push_back(temp); + } + Int8OptConfig::get_config().set_indicators(indicators); + }); + + m.def("enable_torch_ccl", [=]() { + py::object module = py::module::import("torch.distributed"); + py::object register_backend = module.attr("Backend").attr("register_backend"); + register_backend("ccl", py::cpp_function(&c10d::ProcessGroupCCL::createProcessGroupCCL, + py::arg("store"), + py::arg("rank"), + py::arg("size"), + py::arg("timeout") = std::chrono::milliseconds( + ::c10d::ProcessGroupCCL::OP_TIMEOUT_MILLIS))); + + }); + m.def("set_xpu_mode", [=](std::string mode){ + AutoOptConfig::singleton().set_xpu_mode(torch_ipex::stringToXPUMode(mode));}); + + // external OPs + m.def("roi_align_forward", &IpexExternal::ROIAlign_forward); + m.def("roi_align_backward", &IpexExternal::ROIAlign_backward); + m.def("nms", &IpexExternal::nms); + m.def("batch_score_nms", &IpexExternal::batch_score_nms); + m.def("linear_relu", &AtenIpexTypeExt::linear_relu); +} + +// } // namespace +using namespace torch::jit; + +__attribute__ ((visibility ("default"))) void InitIpexBindings(py::module m) { + InitIpexModuleBindings(m); + // jit fusion pass + torch::jit::registerPrePass([](std::shared_ptr& g) { + if (AutoOptConfig::singleton().get_jit_fuse()) { + torch::jit::FusionPass(g); + } + }); +} + +} // namespace torch_ipex \ No newline at end of file diff --git a/torch_ipex/csrc/py_init.h b/torch_ipex/csrc/py_init.h new file mode 100644 index 000000000..5c840dc91 --- /dev/null +++ b/torch_ipex/csrc/py_init.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace py = pybind11; + +namespace torch_ipex { + +// Initialize bindings for IPE module, tensor and optimization passes. +void InitIpexBindings(py::module m); + +} // namespace torch_ipex \ No newline at end of file diff --git a/torch_ipex/launch.py b/torch_ipex/launch.py new file mode 100644 index 000000000..675bcacbd --- /dev/null +++ b/torch_ipex/launch.py @@ -0,0 +1,650 @@ +from __future__ import absolute_import, division, print_function, unicode_literals +import sys +import platform +import subprocess +import os +from os.path import expanduser +import re +import glob +import numpy as np +from argparse import ArgumentParser, REMAINDER +from argparse import RawTextHelpFormatter +import logging +import psutil + +logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s') +logger = logging.getLogger(__name__) + +r""" +This is a script for launching PyTorch training and inference on Intel Xeon CPU with optimal configurations. +Now, single instance inference/training, multi-instance inference/training and distributed training +with oneCCL backend is enabled. + +To get the peak performance on Intel Xeon CPU, the script optimizes the configuration of thread and memory +management. For thread management, the script configures thread affinity and the preload of Intel OMP library. +For memory management, it configures NUMA binding and preload optimized memory allocation library (e.g. tcmalloc, jemalloc). + +**How to use this module:** + +*** Single instance inference/training *** + +1. Run single-instance inference or training on a single node with all CPU sockets. + +:: + + >>> python -m intel_pytorch_extension.launch script.py args + +2. Run single-instance inference or training on a single CPU socket. + +:: + + >>> python -m intel_pytorch_extension.launch --socket_id 1 script.py args + +*** Multi-instance inference *** + +1. Multi-instance + By default, one instance per socket. if you want to set the instance numbers and core per instance, + --nintances and --ncore_per_instance should be set. + + + >>> python -m intel_pytorch_extension.launch --multi_instance python_script args + + eg: on CLX8280 with 14 instance, 4 cores per instance +:: + + >>> python -m intel_pytorch_extension.launch --multi_instance --nintances 14 --ncore_per_instance 4 python_script args + + +*** Distributed Training *** + +spawns up multiple distributed training processes on each of the training nodes. For intel_pytorch_extension, oneCCL +is used as the communication backend and MPI used to launch multi-proc. To get the better +performance, you should specify the different cores for oneCCL communication and computation +process seperately. This tool can automatically set these ENVs(such as I_MPI_PIN_DOMIN) and launch +multi-proc for you. + +The utility can be used for single-node distributed training, in which one or +more processes per node will be spawned. It can also be used in +multi-node distributed training, by spawning up multiple processes on each node +for well-improved multi-node distributed training performance as well. + + +1. Single-Node multi-process distributed training + +:: + + >>> python -m intel_pytorch_extension.launch --distributed python_script --arg1 --arg2 --arg3 and all other + arguments of your training script + +2. Multi-Node multi-process distributed training: (e.g. two nodes) + + +rank 0: *(IP: 192.168.10.10, and has a free port: 295000)* + +:: + + >>> python -m intel_pytorch_extension.launch --distributed --nproc_per_node=xxx + --nnodes=2 --hostfile hostfile python_sript --arg1 --arg2 --arg3 + and all other arguments of your training script) + + +3. To look up what optional arguments this module offers: + +:: + + >>> python -m intel_pytorch_extension.launch --help + +*** Memory allocator *** + +"--enable_tcmalloc" and "--enable_jemalloc" can be used to enable different memory allcator. + +""" + +class CPUinfo(): + def __init__(self): + + self.cpuinfo = [] + if platform.system() == "Windows": + raise RuntimeError("Windows platform is not supported!!!") + elif platform.system() == "Linux": + args = ["lscpu", "--parse=CPU,Core,Socket,Node"] + lscpu_info = subprocess.check_output(args, universal_newlines=True).split("\n") + + # Get information about cpu, core, socket and node + for line in lscpu_info: + pattern = r"^([\d]+,[\d]+,[\d]+,[\d]+)" + regex_out = re.search(pattern, line) + if regex_out: + self.cpuinfo.append(regex_out.group(1).strip().split(",")) + self._get_socket_info() + + def _get_socket_info(self): + + self.socket_physical_cores = [] #socket_id is index + self.socket_logical_cores = [] #socket_id is index + self.sockets = int(max([line[2] for line in self.cpuinfo])) + 1 + for socket_id in range(self.sockets): + cur_socket_physical_core = [] + cur_socket_logical_core = [] + for line in self.cpuinfo: + if socket_id == int(line[2]): + if line[1] not in cur_socket_physical_core: + cur_socket_physical__C.append(line[1]) + cur_socket_logical__C.append(line[0]) + self.socket_physical_cores.append(cur_socket_physical_core) + self.socket_logical_cores.append(cur_socket_logical_core) + + + def socket_nums(self): + return self.sockets + + def physical_core_nums(self): + return len(self.socket_physical_cores) * len(self.socket_physical_cores[0]) + + def logical_core_nums(self): + return len(self.socket_logical_cores) * len(self.socket_logical_cores[0]) + + def get_socket_physical_cores(self, socket_id): + if socket_id < 0 or socket_id > self.sockets - 1: + logger.error("Invalid socket id") + return self.socket_physical_cores[socket_id] + + def get_socket_logical_cores(self, socket_id): + if socket_id < 0 or socket_id > self.sockets - 1: + logger.error("Invalid socket id") + return self.socket_logical_cores[socket_id] + + def get_all_physical_cores(self): + return np.array(self.socket_physical_cores).flatten().tolist() + + def get_all_logical_cores(self): + return np.array(self.socket_logical_cores).flatten().tolist() + + +def set_mpi_pin_domain(args): + ''' + I_MPI_PIN_DOMAIN specify the cores used for every MPI process. + The first ccl_worker_count cores of every rank for ccl communication + and the other cores will be used to do computation. + For example: on CascadeLake 8280 CPU, 2 ranks on one node. ccl_worker_count=4 + CCL_WORKER_COUNT=4 + CCL_WORKER_AFFINITY="0,1,2,3,28,29,30,31" + I_MPI_PIN_DOMAIN=[0xffffff0,0xffffff0000000] + ''' + cpuinfo = CPUinfo() + ppn = args.nproc_per_node + total_cores = cpuinfo.physical_core_nums() + if args.use_logical_core: + total_cores = cpuinfo.logcal_core_nums() + cores_per_rank = total_cores // ppn + pin_domain = "[" + for proc in range(ppn): + domain_binary = 0 + begin = proc * cores_per_rank + args.ccl_worker_count + end = proc * cores_per_rank + cores_per_rank -1 + for i in range(begin, end + 1): + domain_binary |= (1 << i) + pin_domain += hex(domain_binary) + "," + return pin_domain + "]" + +def set_ccl_worker_affinity(args): + ''' + computation and communication use different cores when using oneCCL + backend for distributed training. we use first ccl_worker_count cores of + every rank for ccl communication + ''' + cpuinfo = CPUinfo() + ppn = args.nproc_per_node + total_cores = cpuinfo.physical_core_nums() + if args.use_logical_core: + total_cores = cpuinfo.logcal_core_nums() + cores_per_rank = total_cores // ppn + affinity = '' + for proc in range(ppn): + for ccl_worker in range(args.ccl_worker_count): + affinity += str(proc * cores_per_rank + ccl_worker)+ "," + os.environ["CCL_WORKER_AFFINITY"] = affinity + + +def add_lib_preload(lib_type=None): + ''' + Enale TCMalloc/JeMalloc/iomp + ''' + library_paths = [] + if "CONDA_PREFIX" in os.environ: + library_paths.append(os.environ["CONDA_PREFIX"] + "/lib/") + + library_paths += ["{}/.local/lib/".format(expanduser("~")), "/usr/local/lib/", + "/usr/local/lib64/", "/usr/lib/", "/usr/lib64/"] + lib_find = False + for lib_path in library_paths: + library_file = lib_path + "lib" + lib_type + ".so" + matches = glob.glob(library_file) + if len(matches) > 0: + if "LD_PRELOAD" in os.environ: + os.environ["LD_PRELOAD"] = matches[0] + ":" + os.environ["LD_PRELOAD"] + else: + os.environ["LD_PRELOAD"] = matches[0] + lib_find = True + break + return lib_find + +def set_memory_allocator(args): + if args.enable_tcmalloc and args.enable_jemalloc: + logger.error("Unable to enable TCMalloc and JEMalloc at the same time") + exit(-1) + + if args.enable_tcmalloc: + find_tc = add_lib_preload(lib_type="tcmalloc") + if not find_tc: + logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" + " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " + "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." + .format("TCmalloc", "tcmalloc", expanduser("~"))) + else: + logger.info("Use TCMalloc memory allocator") + + elif args.enable_jemalloc: + find_je = add_lib_preload(lib_type="jemalloc") + if not find_je: + logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" + " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " + "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." + .format("JeMalloc", "jemalloc", expanduser("~"))) + else: + logger.info("Use JeMallocl memory allocator") + + elif args.use_default_allocator: + pass + + else: + find_tc = add_lib_preload(lib_type="tcmalloc") + if find_tc: + logger.info("Use TCMalloc memory allocator") + return + find_je = add_lib_preload(lib_type="jemalloc") + if find_je: + logger.info("Use JeMallocl memory allocator") + return + logger.warning("Both TCMalloc and JeMalloc are not fount in $CONDA_PREFIX/lib or /.local/lib/" + " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " + "~/.local/lib/ so the LD_PRELOAD environment variable will not be set. This may drop the performance" + .format(expanduser("~"))) + +def set_multi_thread_and_allcator(args): + + set_memory_allocator(args) + if "OMP_NUM_THREADS" not in os.environ: + os.environ["OMP_NUM_THREADS"] = str(args.ncore_per_instance) + elif "OMP_NUM_THREADS" in os.environ: + args.ncore_per_instance = int(os.environ["OMP_NUM_THREADS"]) + + if "KMP_AFFINITY" not in os.environ: + os.environ["KMP_AFFINITY"] = args.kmp_affinity + + if "KMP_BLOCKTIME" not in os.environ: + os.environ["KMP_BLOCKTIME"] = "1" + + if "DNNL_PRIMITIVE_CACHE_CAPACITY" not in os.environ: + os.environ["DNNL_PRIMITIVE_CACHE_CAPACITY"] = '1024' + + logger.info("OMP_NUM_THREADS={} ".format(os.environ["OMP_NUM_THREADS"])) + logger.info("KMP_AFFINITY={}".format(os.environ["KMP_AFFINITY"])) + logger.info("KMP_BLOCKTIME={}".format(os.environ["KMP_BLOCKTIME"])) + logger.info("DNNL_PRIMITIVE_CACHE_CAPACITY={}".format(os.environ["DNNL_PRIMITIVE_CACHE_CAPACITY"])) + + if args.enable_iomp: + find_iomp = add_lib_preload(lib_type="iomp") + if not find_iomp: + logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" + " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " + "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." + .format("iomp", "iomp", expanduser("~"))) + else: + logger.info("User iomp") + +def launch(args): + ''' + single-instance / multi-instance launcher + ''' + processes = [] + cores = [] + + cpuinfo = CPUinfo() + if args.core_list:#user specify what cores will be used by params + cores = args.core_list.strip().split(",") + if args.ncore_per_instance == -1: + logger.error("please specify the '--ncore_per_instance' if you have pass the --core_list params") + exit(-1) + elif args.ninstances > 1 and args.ncore_per_instance * args.ninstances < len(cores): + logger.warning("only first {} cores will be used, but you specify {} cores in core_list".format + (args.ncore_per_instance * args.ninstances, len(cores))) + else: + args.ninstances = len(cores) // args.ncore_per_instance + else: + if args.use_logical_core: + if args.socket_id != -1: + cores = cpuinfo.get_socket_logical_cores(args.socket_id) + else: + cores = cpuinfo.get_all_logical_cores() + else: + if args.socket_id != -1: + cores = cpuinfo.get_socket_physical_cores(args.socket_id) + else: + cores = cpuinfo.get_all_physical_cores() + if not args.multi_instance and args.ninstances == -1 and args.ncore_per_instance == -1: + args.ninstances = 1; + args.ncore_per_instance = len(cores) + elif args.multi_instance and args.ninstances == -1 and args.ncore_per_instance == -1: + args.throughput_performance = True + elif args.ncore_per_instance == -1 and args.ninstances != -1: + args.ncore_per_instance = len(cores) // args.ninstances + elif args.ncore_per_instance != -1 and args.ninstances == -1: + args.ninstances = len(cores) // args.ncore_per_instance + else: + if args.ninstances * args.ncore_per_instance > len(cores): + logger.error("Please make sure ninstances * ncore_per_instance <= total_cores") + exit(-1) + if args.latency_performance: + if args.ncore_per_instance !=4: + logger.warning("latency_performance is a specail mode, args.ncore_per_instance can only be set to be 4") + args.ncore_per_instance = 4 + cores = cpuinfo.get_all_physical_cores() + args.ninstances = len(cores) // args.ncore_per_instance + + if args.throughput_performance: + args.ninstances = cpuinfo.socket_nums() + cores = cpuinfo.get_all_physical_cores() + args.ncore_per_instance = len(cores) // args.ninstances + + os.environ["LAUNCH_CMD"] = "#" + set_multi_thread_and_allcator(args) + for i in range(args.ninstances): + cmd = [] + cur_process_cores = "" + if not args.disable_numactl: + cmd = ["numactl"] + for core in cores[i * args.ncore_per_instance:(i + 1) * args.ncore_per_instance]: + cur_process_cores = cur_process_cores + str(core) + "," + numa_params = "-C {} ".format(cur_process_cores[:-1]) + cmd.extend(numa_params.split()) + with_python = not args.no_python + if with_python: + cmd.append(sys.executable) + if args.module: + cmd.append("-m") + cmd.append(args.program) + cmd.extend(args.program_args) + os.environ["LAUNCH_CMD"] += " ".join(cmd) + ",#" + process = subprocess.Popen(cmd, env=os.environ) + processes.append(process) + os.environ["LAUNCH_CMD"] = os.environ["LAUNCH_CMD"][:-2] + for process in processes: + process.wait() + if process.returncode != 0: + raise subprocess.CalledProcessError(returncode=process.returncode, + cmd=cmd) + +def mpi_dist_launch(args): + ''' + Set ENVs and launch MPI process for distributed training. + ''' + if args.nnodes > 1 and not os.path.exists(args.hostfile): + raise ValueError("hostfile is necessary when you use multi-node distributed training," + "Please create hostfile which include the ip list you used for distributed running") + elif args.nnodes > 1: + ipv4_addr_pattern = r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" + ip_list = [] + with open(args.hostfile) as f: + for line in f: + line = line.strip().strip("\n") + is_valid = re.match(ipv4_addr_pattern, line) + if not is_valid: + logger.error("{} is not valid IPV4 address".format(line)) + exit(-1) + else: + ip_list.append(line) + if len(ip_list) < args.nnodes: + logger.error("The number of IP {} should greater than nnodes parameters {}".format(len(ip_list), args.nnodes)) + exit(-1) + master_check = False + dic = psutil.net_if_addrs() + for adapter in dic: + snicList = dic[adapter] + for snic in snicList: + if snic.address == ip_list[0]: + master_check = True + if not master_check: + logger.error("MASTER_ADDR is not right. Please make sure the first ip {} in your hostfile is the current node".format(ip_list[0])) + exit(-1) + + logger.info("Begin to validate the ip connect") + args.master_addr = ip_list[0] + for ip in ip_list[1:]: + completed_process = subprocess.run("ssh -o PasswordAuthentication=no {} ':'".format(ip), shell=True) + if completed_process.returncode != 0: + logger.error("Passwordless SSH login to {} failed, please make sure you have setup SSH public key right") + exit(-1) + else: + logger.info("connection from master node {} to slave node {} is OK".format(args.master_addr, ip)) + + set_memory_allocator(args) + # set distributed related environmental variables + os.environ["MASTER_ADDR"] = args.master_addr + os.environ["MASTER_PORT"] = str(args.master_port) + if "I_MPI_PIN_DOMAIN" not in os.environ: + mpi_pin_domain = set_mpi_pin_domain(args) + else: + mpi_pin_domain = os.environ["I_MPI_PIN_DOMAIN"] + + cpuinfo = CPUinfo() + ppn = args.nproc_per_node + total_cores = len(cpuinfo.get_all_physical_cores()) + cores_per_rank = total_cores // ppn + + if "OMP_NUM_THREADS" not in os.environ: + opm_num_threads = cores_per_rank - args.ccl_worker_count + else: + opm_num_threads = os.environ["OMP_NUM_THREADS"] + + os.environ["CCL_WORKER_COUNT"] = str(args.ccl_worker_count) + + if "CCL_WORKER_AFFINITY" not in os.environ: + set_ccl_worker_affinity(args) + + if "CCL_ATL_TRANSPORT" not in os.environ: + os.environ["CCL_ATL_TRANSPORT"] = "ofi" + + if args.enable_iomp: + find_iomp = add_lib_preload(lib_type="iomp") + if not find_iomp: + logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" + " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " + "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." + .format("iomp", "iomp", expanduser("~"))) + else: + logger.info("Enale iomp by set LD_PRELOAD") + + logger.info("MASTER_ADDR={}".format(args.master_addr)) + logger.info("MASTER_PORT={}".format(args.master_port)) + logger.info("I_MPI_PIN_DOMAIN={}".format(mpi_pin_domain)) + logger.info("OMP_NUM_THREADS={} ".format(opm_num_threads)) + logger.info("CCL_WORKER_COUNT={}".format(args.ccl_worker_count)) + logger.info("CCL_WORKER_AFFINITY={}".format(os.environ["CCL_WORKER_AFFINITY"])) + + os.environ["LAUNCH_CMD"] = "#" + cmd = ['mpiexec.hydra'] + mpi_config = "-l -np {} -ppn {} -genv I_MPI_PIN_DOMAIN={} -genv OMP_NUM_THREADS={} ".format(args.nnodes*args.nproc_per_node, + args.nproc_per_node, mpi_pin_domain, opm_num_threads) + mpi_config += args.more_mpi_parms + if args.nnodes > 1: + mpi_config += " -hostfile {}".format(args.hostfile) + cmd.extend(mpi_config.split()) + with_python = not args.no_python + if with_python: + cmd.append(sys.executable) + cmd.append("-u") + if args.module: + cmd.append("-m") + cmd.append(args.program) + cmd.extend(args.program_args) + process = subprocess.Popen(cmd, env=os.environ) + process.wait() + os.environ["LAUNCH_CMD"] += " ".join(cmd) + ",#" + os.environ["LAUNCH_CMD"] = os.environ["LAUNCH_CMD"][:-2] + +def add_distributed_training_params(parser): + + cpuinfo = CPUinfo() + socket_nums = cpuinfo.socket_nums() + + group = parser.add_argument_group("Distributed Training Parameters With oneCCL backend") + group.add_argument("--nnodes", metavar='\b', type=int, default=1, + help="The number of nodes to use for distributed " + "training") + group.add_argument("--nproc_per_node", metavar='\b', type=int, default=socket_nums, + help="The number of processes to launch on each node") + #ccl control + group.add_argument("--ccl_worker_count", metavar='\b', default=4, type=int, + help="Core numbers per rank used for ccl communication") + #mpi control + group.add_argument("--master_addr", metavar='\b', default="127.0.0.1", type=str, + help="Master node (rank 0)'s address, should be either " + "the IP address or the hostname of node 0, for " + "single node multi-proc training, the " + "--master_addr can simply be 127.0.0.1") + group.add_argument("--master_port", metavar='\b', default=29500, type=int, + help="Master node (rank 0)'s free port that needs to " + "be used for communication during distributed " + "training") + group.add_argument("--hostfile", metavar='\b', default="hostfile", type=str, + help="Hostfile is necessary for multi-node multi-proc " + "training. hostfile includes the node address list " + "node address which should be either the IP address" + "or the hostname.") + group.add_argument("--more_mpi_parms", metavar='\b', default="", type=str, + help="User can pass more parameters for mpiexec.hydra " + "except for -np -ppn -hostfile and -genv I_MPI_PIN_DOMAIN") + +def add_memory_allocator_params(parser): + + group = parser.add_argument_group("Memory Allocator Parameters") + #allocator control + group.add_argument("--enable_tcmalloc", action='store_true', default=False, + help="Enable tcmalloc allocator") + group.add_argument("--enable_jemalloc", action='store_true', default=False, + help="Enable jemalloc allocator") + group.add_argument("--use_default_allocator", action='store_true', default=False, + help="Use default memory allocator") + +def add_multi_instance_params(parser): + + group = parser.add_argument_group("Multi-instance Parameters") + #multi-instance control + group.add_argument("--ncore_per_instance", metavar='\b', default=-1, type=int, + help="Cores per instance") + group.add_argument("--ninstances", metavar='\b', default=-1, type=int, + help="For multi-instance, you should give the cores number you used for per insantance.") + group.add_argument("--latency_performance", action='store_true', default=False, + help="By detault 4 core per instance and use all physical cores") + group.add_argument("--throughput_performance", action='store_true', default=False, + help="By default one instance per socket and use all physical cores") + group.add_argument("--socket_id", metavar='\b', default=-1, type=int, + help="Socket id for multi-instance, by default all sockets will be used") + group.add_argument("--use_logical_core", action='store_true', default=False, + help="Whether only use physical cores") + group.add_argument("--disable_numactl", action='store_true', default=False, + help="Disable numactl") + group.add_argument("--core_list", metavar='\b', default=None, type=str, + help="Specify the core list as 'core_id, core_id, ....', otherwise, all the cores will be used.") + +def add_kmp_iomp_params(parser): + + group = parser.add_argument_group("KMP/IOMP Affinity Parameters") + group.add_argument("--kmp_affinity", metavar='\b', default="granularity=fine,compact,1,0", type=str, + help="KMP_AFFINITY setup, environment variable has higher priority than this args." + "defualt value is : granularity=fine,compact,1,0") + group.add_argument("--enable_iomp", action='store_true', default=False, + help="Enable iomp and libiomp.so will be add to LD_PRELOAD") + + +def parse_args(): + """ + Helper function parsing the command line options + @retval ArgumentParser + """ + parser = ArgumentParser(description="This is a script for launching PyTorch training and inference on Intel Xeon CPU " + "with optimal configurations. Now, single instance inference/training, multi-instance " + "inference/training and distributed training with oneCCL backend is enabled. " + "To get the peak performance on Intel Xeon CPU, the script optimizes the configuration " + "of thread and memory management. For thread management, the script configures thread " + "affinity and the preload of Intel OMP library. For memory management, it configures " + "NUMA binding and preload optimized memory allocation library (e.g. tcmalloc, jemalloc) " + "\n################################# Basic usage ############################# \n" + "\n 1. single instance\n" + "\n >>> python -m intel_pytorch_extension.launch python_script args \n" + "\n2. multi-instance \n" + "\n >>> python -m intel_pytorch_extension.launch --multi_instance python_script args\n" + "\n3. Single-Node multi-process distributed training\n" + "\n >>> python -m intel_pytorch_extension.launch --distributed python_script args\n" + "\n4. Multi-Node multi-process distributed training: (e.g. two nodes)\n" + "\n rank 0: *(IP: 192.168.10.10, and has a free port: 295000)*\n" + "\n >>> python -m intel_pytorch_extension.launch --distributed --nproc_per_node=2\n" + "\n --nnodes=2 --hostfile hostfile python_script args\n", + formatter_class=RawTextHelpFormatter) + + parser.add_argument("--multi_instance", action='store_true', default=False, + help="Enable multi-instance, by default one instance per socket") + + parser.add_argument('--distributed', action='store_true', default=False, + help='Enable distributed training.') + parser.add_argument("-m", "--module", default=False, action="store_true", + help="Changes each process to interpret the launch script " + "as a python module, executing with the same behavior as" + "'python -m'.") + + parser.add_argument("--no_python", default=False, action="store_true", + help="Do not prepend the --program script with \"python\" - just exec " + "it directly. Useful when the script is not a Python script.") + add_memory_allocator_params(parser) + add_kmp_iomp_params(parser) + + add_distributed_training_params(parser) + add_multi_instance_params(parser) + # positional + parser.add_argument("program", type=str, + help="The full path to the proram/script to be launched. " + "followed by all the arguments for the script") + + # rest from the training program + parser.add_argument('program_args', nargs=REMAINDER) + return parser.parse_args() + +def main(): + + env_before = set(os.environ.keys()) + if platform.system() == "Windows": + raise RuntimeError("Windows platform is not supported!!!") + + args = parse_args() + + if args.distributed and args.multi_instance: + raise RuntimeError("Either args.distributed or args.multi_instance should be set") + + if args.latency_performance and args.throughput_performance: + raise RuntimeError("Either args.latency_performance or args.throughput_performance should be set") + + if args.nnodes > 1: + args.distributed = True + + if args.distributed: + mpi_dist_launch(args) + else: + launch(args) + + for x in sorted(set(os.environ.keys()) - env_before): + logger.debug(f'{x}={os.environ[x]}') + +if __name__ == "__main__": + main() + diff --git a/torch_ipex/ops/__init__.py b/torch_ipex/ops/__init__.py new file mode 100644 index 000000000..277184b8f --- /dev/null +++ b/torch_ipex/ops/__init__.py @@ -0,0 +1,16 @@ +from .interaction import interaction +from .embeddingbag import embeddingbag +from .linear import * +from .pooling import * +from .mlp import * +from .jit import * +from .save import * +from .to import * +from .roi_align import ROIAlign +from .roi_align import roi_align +from .nms import * +from .lstm import * +from .rnn import * +from .gru import * +from .layer_norm import * +from .frozen_batch_norm import * diff --git a/torch_ipex/ops/embeddingbag.py b/torch_ipex/ops/embeddingbag.py new file mode 100644 index 000000000..823963d4a --- /dev/null +++ b/torch_ipex/ops/embeddingbag.py @@ -0,0 +1,14 @@ +import torch +from torch import nn +from torch.autograd import Function +import torch_ipex._C as core + +# # extension for BF16 fast path only + + +def embeddingbag(weights, indices, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset): + ret = torch.ops.torch_ipex.embedding_bag(weights, indices, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset) + if len(ret)==1: + ret += [torch.Tensor(), torch.Tensor(), torch.Tensor()] + return ret +torch.embedding_bag = embeddingbag diff --git a/torch_ipex/ops/frozen_batch_norm.py b/torch_ipex/ops/frozen_batch_norm.py new file mode 100644 index 000000000..300098ded --- /dev/null +++ b/torch_ipex/ops/frozen_batch_norm.py @@ -0,0 +1,21 @@ +import torch +from torch import nn + +def frozen_batch_norm(x, weight, bias, running_mean, running_var): + return torch.ops.torch_ipex.frozen_batch_norm(x, weight, bias, running_mean, running_var) + +class FrozenBatchNorm2d(nn.Module): + """ + BatchNorm2d where the batch statistics and the affine parameters + are fixed + """ + + def __init__(self, n): + super(FrozenBatchNorm2d, self).__init__() + self.register_buffer("weight", torch.ones(n)) + self.register_buffer("bias", torch.zeros(n)) + self.register_buffer("running_mean", torch.zeros(n)) + self.register_buffer("running_var", torch.ones(n)) + + def forward(self, x): + return frozen_batch_norm(x, self.weight, self.bias, self.running_mean, self.running_var) diff --git a/torch_ipex/ops/gru.py b/torch_ipex/ops/gru.py new file mode 100644 index 000000000..a8412f5ff --- /dev/null +++ b/torch_ipex/ops/gru.py @@ -0,0 +1,21 @@ +import math +import torch +from torch.nn.modules.rnn import RNNBase +from torch.nn.utils.rnn import PackedSequence +from torch import _VF + +VF_gru = _VF.gru + +def ipex_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): + if input.device.type == 'xpu' and (dropout == 0 or training == False): + return torch.ops.torch_ipex.gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + else: + return VF_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + +def gru(*args): + if isinstance(args[2], torch.Tensor): + return VF_gru(*args) + else: + return ipex_gru(*args) + +_VF.gru = gru \ No newline at end of file diff --git a/torch_ipex/ops/interaction.py b/torch_ipex/ops/interaction.py new file mode 100644 index 000000000..2fc9033f0 --- /dev/null +++ b/torch_ipex/ops/interaction.py @@ -0,0 +1,26 @@ +import torch +from torch import nn +from torch.autograd import Function +import torch_ipex._C as core + +def interaction(*args): + # Current pytorch dose not support vector input for c++ custom function + # So we preserve python custom function while need backward + # Since python custom function will meet GIL when run multi-thread in one process + # We will drop python custom function after c++ are supported + if torch.is_grad_enabled(): + return InteractionFunc.apply(*args) + return torch.ops.torch_ipex.interaction_forward(args) + +class InteractionFunc(Function): + @staticmethod + def forward(ctx, *args): + ctx.save_for_backward(*args) + output = torch.ops.torch_ipex.interaction_forward(args) + return output + + @staticmethod + def backward(ctx, grad_out): + args = ctx.saved_tensors + grad_in = torch.ops.torch_ipex.interaction_backward(grad_out.contiguous(), args) + return tuple(grad_in) diff --git a/torch_ipex/ops/jit.py b/torch_ipex/ops/jit.py new file mode 100644 index 000000000..216bc9d04 --- /dev/null +++ b/torch_ipex/ops/jit.py @@ -0,0 +1,47 @@ +import torch +import torch_ipex._C as core +from torch.jit._recursive import wrap_cpp_module + +torch._C._jit_set_profiling_mode(False) +torch._C._jit_set_profiling_executor(False) + +orig_script = torch.jit.script +orig_trace = torch.jit.trace + +def script_(obj, optimize=None, _frames_up=0, _rcb=None): + torch.jit.script = orig_script + jit_m = orig_script(obj, optimize=optimize, _frames_up=_frames_up+1, _rcb=_rcb) + torch.jit.script = script_ + + mix_state = torch.bfloat16 if core.get_mix_bf16_fp32() else torch.int8 if core.get_mix_int8_fp32() else None + # Disable mix precision in model fusion, since mixed precision cannot + # bring any benefits for inference, but will lead to loss of accuracy + core.disable_mix_bf16_fp32() + core.disable_mix_int8_fp32() + if core.get_jit_opt() and hasattr(jit_m, '_c'): + jit_m = wrap_cpp_module(torch._C._jit_pass_fold_convbn(jit_m._c)) + if mix_state == torch.bfloat16: + core.enable_mix_bf16_fp32() + elif mix_state == torch.int8: + core.enable_mix_int8_fp32() + return jit_m + +def trace_(func, example_inputs, *args, **kwargs): + # Disable mix precision. torch.jit.trace will check the traced output + # against what is expected. Since mix precision will lead to + # loss of accuracy, this will raise warning during torch.jit.trace + mix_state = torch.bfloat16 if core.get_mix_bf16_fp32() else torch.int8 if core.get_mix_int8_fp32() else None + core.disable_mix_bf16_fp32() + core.disable_mix_int8_fp32() + jit_m = orig_trace(func, example_inputs, *args, **kwargs) + if core.get_jit_opt() and hasattr(jit_m, '_c'): + jit_m = wrap_cpp_module(torch._C._jit_pass_fold_convbn(jit_m._c)) + if mix_state == torch.bfloat16: + core.enable_mix_bf16_fp32() + elif mix_state == torch.int8: + core.enable_mix_int8_fp32() + return jit_m + + +torch.jit.script = script_ +torch.jit.trace = trace_ diff --git a/torch_ipex/ops/layer_norm.py b/torch_ipex/ops/layer_norm.py new file mode 100644 index 000000000..c9f32342b --- /dev/null +++ b/torch_ipex/ops/layer_norm.py @@ -0,0 +1,13 @@ +import torch +import torch_ipex._C as core +from typing import Optional + +torch_layer_norm = torch.layer_norm + +def _layer_norm(input, normalized_shape, weight, bias, eps, cudnn_enabled): + if input.device.type != "xpu": + return torch_layer_norm(input, normalized_shape, weight, bias, eps, cudnn_enabled) + else: + return torch.ops.torch_ipex.layer_norm(input, normalized_shape, weight, bias, eps) + +torch.layer_norm = _layer_norm diff --git a/torch_ipex/ops/linear.py b/torch_ipex/ops/linear.py new file mode 100644 index 000000000..b92cf2910 --- /dev/null +++ b/torch_ipex/ops/linear.py @@ -0,0 +1,17 @@ +import torch +from torch.autograd import Function +import torch.nn.functional as F +import torch_ipex._C as core +from typing import Optional + +def linear(input, weight, bias: Optional[torch.Tensor] = None): + return torch.ops.torch_ipex.linear(input, weight, bias) + +F.linear = linear + +class LinearRelu(torch.nn.Linear): + def __init__(self, in_features, out_features, bias=True): + super(LinearRelu, self).__init__(in_features, out_features, bias) + + def forward(self, input): + return torch.ops.torch_ipex.linear_relu(input, self.weight, self.bias) \ No newline at end of file diff --git a/torch_ipex/ops/lstm.py b/torch_ipex/ops/lstm.py new file mode 100644 index 000000000..25ad8ccfd --- /dev/null +++ b/torch_ipex/ops/lstm.py @@ -0,0 +1,59 @@ +import torch +from torch import _VF + +VF_lstm = _VF.lstm + +def ipex_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device): + # For LSTM training with dropout, fallback to cpu due to performance issue in oneDNN mode + if training and dropout != 0: + return fallback_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device=device) + else: + return torch.ops.torch_ipex.lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + +# users may only transfer the data but not the module to IPEX device, need to check if every item in the args is on "cpu" device +def get_device(*args): + for item in args: + if isinstance(item, (tuple, list)): + for x in item: + if x.device.type != "cpu": + return x.device.type + elif isinstance(item, torch.Tensor): + if item.device.type != "cpu": + return item.device.type + return "cpu" + +def fallback_lstm(*args, device): + # move args to cpu device + args_cpu = [] + # args is a tuple which does not support item assignment + for item in args: + if isinstance(item, (tuple, list)): + item_cpu = [x.to("cpu") for x in item] + elif isinstance(item, torch.Tensor): + item_cpu = item.to("cpu") + else: + item_cpu = item + args_cpu.append(item_cpu) + + output = VF_lstm(*args_cpu) + + # move output to the original device + output_device = [] + # output is a tuple which does not support item assignment + for item in output: + item_device = item.to(device) + output_device.append(item_device) + return tuple(output_device) + +def lstm(*args): + device = get_device(*args) + if device == "cpu": + return VF_lstm(*args) + + # For LSTM with pack_padded_sequence as input, fallback to cpu due to performance issue in oneDNN mode + if isinstance(args[1], torch.Tensor): + return fallback_lstm(*args, device=device) + else: + return ipex_lstm(*args, device=device) + +_VF.lstm = lstm diff --git a/torch_ipex/ops/mlp.py b/torch_ipex/ops/mlp.py new file mode 100644 index 000000000..ec8a31799 --- /dev/null +++ b/torch_ipex/ops/mlp.py @@ -0,0 +1,238 @@ +import math +import torch +from torch import nn +from torch.nn.parameter import Parameter +from torch.nn import init +from torch.autograd import Function +import torch_ipex._C as core + +class IpexMLPHandle: + def __init__(self, N, C, K, bn, bc, bk, dtype, fuse_bias, act_type): + self.handle = core.mlp_create_handle(N, C, K, bn, bc, bk, 1 if dtype == torch.float32 else 2, fuse_bias, act_type) + self.N = N + self.C = C + self.K = K + self.bn = bn + self.bc = bc + self.bk = bk + self.fuse_bias = fuse_bias + self.act_type = act_type + if act_type == 1: + self.relu_mask_tensor = core.mlp_set_relu_mask(self.handle) + + def __del__(self): + if self.handle: + core.mlp_release_handle(self.handle) + self.handle = None + self.relu_mask_tensor = None + +class IpexMLPFC(Function): + @staticmethod + def forward(ctx, input, weight, bias, handle): + #print("Inside XsmmFCForward") + #t1 = time.time() + input = input.contiguous() + weight = weight.contiguous() + bias = bias.contiguous() + output = core.mlp_forward(handle.handle, input, weight, bias) + #t2 = time.time() + #print("XsmmFCFWD: q=%.3f" % ((t2-t1)*1000.0)) + ctx.ipex_mlp_handle = handle + ctx.save_for_backward(input, weight) + return output + + @staticmethod + def backward(ctx, grad_output): + #print("Inside XsmmFCBackward") + handle = ctx.ipex_mlp_handle + del ctx.ipex_mlp_handle + input, weight = ctx.saved_variables + #t1 = time.time() + grad_output = grad_output.contiguous() + grad_input, grad_weight, grad_bias = core.mlp_backward(handle.handle, grad_output, input, weight) + #t2 = time.time() + #print("XsmmFCBWD: q=%.3f w=%.3f" % ((t2-t1)*1000.0, (t3-t2)*1000.0)) + return (grad_input, grad_weight, grad_bias, None) + +class IpexMLPLinear(nn.Module): + r"""PCL Linear module for using libxsmm blocked GEMM""" + + __constants__ = ['bias', 'C', 'K'] + + def __init__(self, C, K, bias=True, act_type=None, output_stays_blocked=True, default_blocking=None): + super(IpexMLPLinear, self).__init__() + self.C = C + self.K = K + self.bc = 0 #self.get_blocking_factor(C, default_blocking) # 64 if C % 64 == 0 else C + self.bk = 0 #self.get_blocking_factor(K, default_blocking) # 64 if K % 64 == 0 else K + self.nbc = 0 # C // self.bc + self.nbk = 0 # K // self.bk + self.C_pad = 0 + self.padded_C = self.C + self.N = 0 + self.nbn = 0 + self.bn = 0 + self.default_blocking = default_blocking + self.ipex_mlp_handle = None + self.set_activation_type(act_type) + self.output_stays_blocked = output_stays_blocked + self.weight = Parameter(torch.Tensor(K, C)) + + if bias: + self.bias = Parameter(torch.Tensor(K)) + else: + self.register_parameter('bias', None) + self.reset_parameters() + + def set_activation_type(self, act_type): + if not act_type: + self.act_type = 0 + elif act_type == 'relu': + self.act_type = 1 + elif act_type == 'sigmoid': + self.act_type = 2 + else: + raise RuntimeError("XsmmLinear: Unknown activation type %s" % act_type) + + def get_blocking_factor(self, dim_size, default_blocking=None): + blocking_prio_list = [64, 48, 32, 50] + if default_blocking: + blocking_prio_list = [default_blocking] + blocking_prio_list + for bs in blocking_prio_list: + if dim_size % bs == 0: + #print("Returning block size of %d for dim_size of %d" % ( bs, dim_size)) + return bs + #print("Returning block size of %d for dim_size of %d" % ( dim_size, dim_size)) + return dim_size + + def is_dtype_supported(self, dtype): + if dtype == torch.float32: + return True + elif dtype == torch.bfloat16 and self.C % 2 == 0: + return True + else: + return False + + def maybe_pad_input(self, input): + if input.dim() == 2 and input.size(1) != self.padded_C: + input = torch.cat([input, input.new_zeros([input.size(0), self.C_pad])], dim=1) + return input + + def maybe_pad_weight(self, weight): + if weight.dim() == 2 and weight.size(1) != self.padded_C: + weight = torch.cat([weight, weight.new_zeros([self.K, self.C_pad])], dim=1) + # elif weight.dim() == 4 and weight.size(1) * weight.size(2) != self.padded_C: + # raise RuntimeError("Trying to ad 4D weights") + # elif weight.dim() == 5 and weight.size(1) * weight.size(2) * weight.size(4) != self.padded_C: + # raise RuntimeError("Trying to ad 5D weights") + return weight + + def get_blocked_weight(self, to_dtype=None, block_for_dtype=None): + weight = self.weight + new_weight = None + if to_dtype: + weight = weight.to(to_dtype) + if not block_for_dtype: + block_for_dtype = weight.dtype + if self.bc == 0 or self.bk == 0: + self.update_blocking(block_for_dtype) + + weight = self.maybe_pad_weight(weight) + if weight.dim() == 2: + if block_for_dtype == torch.bfloat16: + l_view = [self.nbk, self.bk, self.nbc, self.bc // 2, 2] + l_perm = [0, 2, 3, 1, 4] + new_weight = weight.view(l_view).permute(l_perm).contiguous() + elif block_for_dtype == torch.float32: + l_view = [self.nbk, self.bk, self.nbc, self.bc] + l_perm = [0, 2, 3, 1] + new_weight = weight.view(l_view).permute(l_perm).contiguous() + else: + raise RuntimeError("Invalid datatype for blocking: %s" % block_for_dtype) + elif weight.dim() == 4: + if block_for_dtype == torch.bfloat16: + l_view = [self.nbk, self.nbc, self.bc // 2, 2, self.bk] + l_perm = [0, 1, 2, 4, 3] + new_weight = weight.view(l_view).permute(l_perm).contiguous() + elif block_for_dtype == torch.float32: + # We are already in correct format, do nothing + new_weight = weight + else: + raise RuntimeError("Invalid datatype for blocking: %s" % block_for_dtype) + elif weight.dim() == 5: + if block_for_dtype == torch.bfloat16: + # We are already in correct format, do nothing + new_weight = weight + elif block_for_dtype == torch.float32: + l_view = [self.nbk, self.nbc, self.bc, self.bk] + l_perm = [0, 1, 2, 4, 3] + new_weight = weight.permute(l_perm).view(l_view).contiguous() + else: + raise RuntimeError("Invalid datatype for blocking: %s" % block_for_dtype) + + return new_weight + + def update_blocking(self, dtype): + if dtype == torch.bfloat16 and self.padded_C % 2 != 0: + self.C_pad = 1 + self.padded_C = self.C + self.C_pad + self.bc = self.get_blocking_factor(self.padded_C, self.default_blocking) + if dtype == torch.bfloat16 and self.bc % 2 != 0: self.bc *= 2 + self.nbc = self.padded_C // self.bc + self.bk = self.get_blocking_factor(self.K, self.default_blocking) + self.nbk = self.K // self.bk + + def reset_weight_shape(self, block_for_dtype=None): + #if not self.is_dtype_supported(block_for_dtype): + # block_for_dtype = torch.float32 + #self.update_bc(block_for_dtype) + self.weight = Parameter(self.get_blocked_weight(block_for_dtype=block_for_dtype)) + + def reset_parameters(self): + init.kaiming_uniform_(self.weight, a=math.sqrt(5)) + if self.bias is not None: + bound = 1 / math.sqrt(self.C) + init.uniform_(self.bias, -bound, bound) + + def forward(self, input): + input_type = input.dtype + #if not self.is_dtype_supported(input_type): + # input = input.to(torch.float32) + if self.bc == 0 or self.bk == 0: + self.update_blocking(input_type) + input = self.maybe_pad_input(input) + if input.dtype == torch.bfloat16: + if self.bc % 2 != 0: raise RuntimeError("Bfloat16 requires even bc") + + if input.dim() == 2: + N = input.size(0) + bn = self.get_blocking_factor(N, 48) #64 if N % 64 == 0 else N + input = input.view(N//bn, bn, self.nbc, self.bc).permute(0,2,1,3) + elif input.dim() == 4: + N = input.size(0) * input.size(2) + bn = input.size(2) + else: + print("Invalid Input dimensions (%d)" % input.dim()) + + input = input.contiguous() + + if N != self.N or bn != self.bn: + # print("Create handle: ", N, self.padded_C, self.K, bn, self.bc, self.bk, input.dtype, 0 if self.bias is None else 1, self.act_type) + self.ipex_mlp_handle = IpexMLPHandle(N, self.padded_C, self.K, bn, self.bc, self.bk, input.dtype, 0 if self.bias is None else 1, self.act_type) + self.N = N + self.bn = bn + self.nbn = N // bn + + wtensor = self.get_blocked_weight(to_dtype=input.dtype) + btensor = self.bias.to(input.dtype) + output = IpexMLPFC.apply(input, wtensor, btensor, self.ipex_mlp_handle) + if not self.output_stays_blocked: + #output = output.permute(0, 2, 1, 3).view(self.N, self.K).contiguous() + output = output.permute(0, 2, 1, 3).reshape(self.N, self.K).contiguous() + output = output.to(input_type) + return output + + def extra_repr(self): + return 'C={}, K={}, bias={}'.format( + self.C, self.K, self.bias is not None + ) diff --git a/torch_ipex/ops/nms.py b/torch_ipex/ops/nms.py new file mode 100644 index 000000000..bb2629391 --- /dev/null +++ b/torch_ipex/ops/nms.py @@ -0,0 +1,4 @@ +import torch_ipex._C as core + +nms = core.nms +batch_score_nms = core.batch_score_nms \ No newline at end of file diff --git a/torch_ipex/ops/pooling.py b/torch_ipex/ops/pooling.py new file mode 100644 index 000000000..f13a33b38 --- /dev/null +++ b/torch_ipex/ops/pooling.py @@ -0,0 +1,25 @@ +import torch +from torch.autograd import Function +import torch.nn.functional as F +import torch_ipex._C as core +from torch.nn.modules.utils import _single, _pair +from typing import List + +Vector = List[int] + +def adaptive_avg_pool2d(input, output_size: Vector): + return torch.ops.torch_ipex.adaptive_avg_pool2d(input, _pair(output_size)) + +def max_pool3d(input, kernel_size: Vector, stride: Vector, padding: Vector, dilation: Vector, ceil_mode: bool): + if len(_single(stride)) == 0: + stride = kernel_size + return torch.ops.torch_ipex.max_pool3d(input, _single(kernel_size), _single(stride), _single(padding), _single(dilation), ceil_mode) + +def max_pool2d(input, kernel_size: Vector, stride: Vector, padding: Vector, dilation: Vector, ceil_mode: bool): + if len(_pair(stride)) == 0: + stride = kernel_size + return torch.ops.torch_ipex.max_pool2d(input, _pair(kernel_size), _pair(stride), _pair(padding), _pair(dilation), ceil_mode) + +torch._C._nn.adaptive_avg_pool2d = adaptive_avg_pool2d +torch.max_pool2d = max_pool2d +torch.max_pool3d = max_pool3d diff --git a/torch_ipex/ops/rnn.py b/torch_ipex/ops/rnn.py new file mode 100644 index 000000000..7f710c720 --- /dev/null +++ b/torch_ipex/ops/rnn.py @@ -0,0 +1,415 @@ +import math +import torch +import warnings +import numbers + +from torch.nn.modules import Module +from torch.nn.parameter import Parameter +from torch.nn.utils.rnn import PackedSequence +from torch.nn import init +from torch import _VF + +def rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): + if input.device.type == 'xpu' and (dropout == 0 or training == False): + return torch.ops.torch_ipex.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + else: + return _VF.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + +def rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): + if input.device.type == 'xpu' and (dropout == 0 or training == False): + return torch.ops.torch_ipex.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + else: + return _VF.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) + +_rnn_impls = { + 'RNN_TANH': _VF.rnn_tanh, + 'RNN_RELU': _VF.rnn_relu, +} + +ipex_rnn_impls = { + 'RNN_TANH': rnn_tanh, + 'RNN_RELU': rnn_relu, +} + + +def apply_permutation(tensor, permutation, dim=1): + # type: (Tensor, Tensor, int) -> Tensor + return tensor.index_select(dim, permutation) + + +class RNNBase(Module): + __constants__ = ['mode', 'input_size', 'hidden_size', 'num_layers', 'bias', + 'batch_first', 'dropout', 'bidirectional'] + + def __init__(self, mode, input_size, hidden_size, + num_layers=1, bias=True, batch_first=False, + dropout=0., bidirectional=False): + super(RNNBase, self).__init__() + self.mode = mode + self.input_size = input_size + self.hidden_size = hidden_size + self.num_layers = num_layers + self.bias = bias + self.batch_first = batch_first + self.dropout = float(dropout) + self.bidirectional = bidirectional + num_directions = 2 if bidirectional else 1 + + if not isinstance(dropout, numbers.Number) or not 0 <= dropout <= 1 or \ + isinstance(dropout, bool): + raise ValueError("dropout should be a number in range [0, 1] " + "representing the probability of an element being " + "zeroed") + if dropout > 0 and num_layers == 1: + warnings.warn("dropout option adds dropout after all but last " + "recurrent layer, so non-zero dropout expects " + "num_layers greater than 1, but got dropout={} and " + "num_layers={}".format(dropout, num_layers)) + + if mode == 'LSTM': + gate_size = 4 * hidden_size + elif mode == 'GRU': + gate_size = 3 * hidden_size + elif mode == 'RNN_TANH': + gate_size = hidden_size + elif mode == 'RNN_RELU': + gate_size = hidden_size + else: + raise ValueError("Unrecognized RNN mode: " + mode) + + self._flat_weights_names = [] + self._all_weights = [] + for layer in range(num_layers): + for direction in range(num_directions): + layer_input_size = input_size if layer == 0 else hidden_size * num_directions + + w_ih = Parameter(torch.Tensor(gate_size, layer_input_size)) + w_hh = Parameter(torch.Tensor(gate_size, hidden_size)) + b_ih = Parameter(torch.Tensor(gate_size)) + # Second bias vector included for CuDNN compatibility. Only one + # bias vector is needed in standard definition. + b_hh = Parameter(torch.Tensor(gate_size)) + layer_params = (w_ih, w_hh, b_ih, b_hh) + + suffix = '_reverse' if direction == 1 else '' + param_names = ['weight_ih_l{}{}', 'weight_hh_l{}{}'] + if bias: + param_names += ['bias_ih_l{}{}', 'bias_hh_l{}{}'] + param_names = [x.format(layer, suffix) for x in param_names] + + for name, param in zip(param_names, layer_params): + setattr(self, name, param) + self._flat_weights_names.extend(param_names) + self._all_weights.append(param_names) + + self._flat_weights = [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names] + self.flatten_parameters() + self.reset_parameters() + + def __setattr__(self, attr, value): + if hasattr(self, "_flat_weights_names") and attr in self._flat_weights_names: + # keep self._flat_weights up to date if you do self.weight = ... + idx = self._flat_weights_names.index(attr) + self._flat_weights[idx] = value + super(RNNBase, self).__setattr__(attr, value) + + def flatten_parameters(self): + """Resets parameter data pointer so that they can use faster code paths. + + Right now, this works only if the module is on the GPU and cuDNN is enabled. + Otherwise, it's a no-op. + """ + # Short-circuits if _flat_weights is only partially instantiated + if len(self._flat_weights) != len(self._flat_weights_names): + return + + for w in self._flat_weights: + if not torch.is_tensor(w): + return + # Short-circuits if any tensor in self._flat_weights is not acceptable to cuDNN + # or the tensors in _flat_weights are of different dtypes + + first_fw = self._flat_weights[0] + dtype = first_fw.dtype + for fw in self._flat_weights: + if (not torch.is_tensor(fw.data) or not (fw.data.dtype == dtype) or + not fw.data.is_cuda or + not torch.backends.cudnn.is_acceptable(fw.data)): + return + + # If any parameters alias, we fall back to the slower, copying code path. This is + # a sufficient check, because overlapping parameter buffers that don't completely + # alias would break the assumptions of the uniqueness check in + # Module.named_parameters(). + unique_data_ptrs = set(p.data_ptr() for p in self._flat_weights) + if len(unique_data_ptrs) != len(self._flat_weights): + return + + with torch.cuda.device_of(first_fw): + import torch.backends.cudnn.rnn as rnn + + # Note: no_grad() is necessary since _cudnn_rnn_flatten_weight is + # an inplace operation on self._flat_weights + with torch.no_grad(): + if torch._use_cudnn_rnn_flatten_weight(): + torch._cudnn_rnn_flatten_weight( + self._flat_weights, (4 if self.bias else 2), + self.input_size, rnn.get_cudnn_mode(self.mode), self.hidden_size, self.num_layers, + self.batch_first, bool(self.bidirectional)) + + def _apply(self, fn): + ret = super(RNNBase, self)._apply(fn) + + # Resets _flat_weights + # Note: be v. careful before removing this, as 3rd party device types + # likely rely on this behavior to properly .to() modules like LSTM. + self._flat_weights = [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names] + # Flattens params (on CUDA) + self.flatten_parameters() + + return ret + + def reset_parameters(self): + stdv = 1.0 / math.sqrt(self.hidden_size) + for weight in self.parameters(): + init.uniform_(weight, -stdv, stdv) + + def check_input(self, input, batch_sizes): + # type: (Tensor, Optional[Tensor]) -> None + expected_input_dim = 2 if batch_sizes is not None else 3 + if input.dim() != expected_input_dim: + raise RuntimeError( + 'input must have {} dimensions, got {}'.format( + expected_input_dim, input.dim())) + if self.input_size != input.size(-1): + raise RuntimeError( + 'input.size(-1) must be equal to input_size. Expected {}, got {}'.format( + self.input_size, input.size(-1))) + + def get_expected_hidden_size(self, input, batch_sizes): + # type: (Tensor, Optional[Tensor]) -> Tuple[int, int, int] + if batch_sizes is not None: + mini_batch = batch_sizes[0] + mini_batch = int(mini_batch) + else: + mini_batch = input.size(0) if self.batch_first else input.size(1) + num_directions = 2 if self.bidirectional else 1 + expected_hidden_size = (self.num_layers * num_directions, + mini_batch, self.hidden_size) + return expected_hidden_size + + def check_hidden_size(self, hx, expected_hidden_size, msg='Expected hidden size {}, got {}'): + # type: (Tensor, Tuple[int, int, int], str) -> None + if hx.size() != expected_hidden_size: + raise RuntimeError(msg.format(expected_hidden_size, tuple(hx.size()))) + + def check_forward_args(self, input, hidden, batch_sizes): + # type: (Tensor, Tensor, Optional[Tensor]) -> None + self.check_input(input, batch_sizes) + expected_hidden_size = self.get_expected_hidden_size(input, batch_sizes) + + self.check_hidden_size(hidden, expected_hidden_size) + + def permute_hidden(self, hx, permutation): + # type: (Tensor, Optional[Tensor]) -> Tensor + if permutation is None: + return hx + return apply_permutation(hx, permutation) + + def forward(self, input, hx=None): + is_packed = isinstance(input, PackedSequence) + if is_packed: + input, batch_sizes, sorted_indices, unsorted_indices = input + max_batch_size = batch_sizes[0] + max_batch_size = int(max_batch_size) + else: + batch_sizes = None + max_batch_size = input.size(0) if self.batch_first else input.size(1) + sorted_indices = None + unsorted_indices = None + + if hx is None: + num_directions = 2 if self.bidirectional else 1 + hx = torch.zeros(self.num_layers * num_directions, + max_batch_size, self.hidden_size, + dtype=input.dtype, device=input.device) + else: + # Each batch of the hidden state should match the input sequence that + # the user believes he/she is passing in. + hx = self.permute_hidden(hx, sorted_indices) + + self.check_forward_args(input, hx, batch_sizes) + _impl = _rnn_impls[self.mode] + ipex_impl = ipex_rnn_impls[self.mode] + if batch_sizes is None: + result = ipex_impl(input, hx, self._flat_weights, self.bias, self.num_layers, + self.dropout, self.training, self.bidirectional, self.batch_first) + else: + result = _impl(input, batch_sizes, hx, self._flat_weights, self.bias, + self.num_layers, self.dropout, self.training, self.bidirectional) + output = result[0] + hidden = result[1] + + if is_packed: + output = PackedSequence(output, batch_sizes, sorted_indices, unsorted_indices) + return output, self.permute_hidden(hidden, unsorted_indices) + + def extra_repr(self): + s = '{input_size}, {hidden_size}' + if self.num_layers != 1: + s += ', num_layers={num_layers}' + if self.bias is not True: + s += ', bias={bias}' + if self.batch_first is not False: + s += ', batch_first={batch_first}' + if self.dropout != 0: + s += ', dropout={dropout}' + if self.bidirectional is not False: + s += ', bidirectional={bidirectional}' + return s.format(**self.__dict__) + + def __setstate__(self, d): + super(RNNBase, self).__setstate__(d) + if 'all_weights' in d: + self._all_weights = d['all_weights'] + + if isinstance(self._all_weights[0][0], str): + return + num_layers = self.num_layers + num_directions = 2 if self.bidirectional else 1 + self._flat_weights_names = [] + self._all_weights = [] + for layer in range(num_layers): + for direction in range(num_directions): + suffix = '_reverse' if direction == 1 else '' + weights = ['weight_ih_l{}{}', 'weight_hh_l{}{}', 'bias_ih_l{}{}', 'bias_hh_l{}{}'] + weights = [x.format(layer, suffix) for x in weights] + if self.bias: + self._all_weights += [weights] + self._flat_weights_names.extend(weights) + else: + self._all_weights += [weights[:2]] + self._flat_weights_names.extend(weights[:2]) + self._flat_weights = [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names] + + @property + def all_weights(self): + return [[getattr(self, weight) for weight in weights] for weights in self._all_weights] + + def _replicate_for_data_parallel(self): + replica = super(RNNBase, self)._replicate_for_data_parallel() + # Need to copy these caches, otherwise the replica will share the same + # flat weights list. + replica._flat_weights = replica._flat_weights[:] + replica._flat_weights_names = replica._flat_weights_names[:] + return replica + + +class RNN(RNNBase): + r"""Applies a multi-layer Elman RNN with :math:`\tanh` or :math:`\text{ReLU}` non-linearity to an + input sequence. + + + For each element in the input sequence, each layer computes the following + function: + + .. math:: + h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh}) + + where :math:`h_t` is the hidden state at time `t`, :math:`x_t` is + the input at time `t`, and :math:`h_{(t-1)}` is the hidden state of the + previous layer at time `t-1` or the initial hidden state at time `0`. + If :attr:`nonlinearity` is ``'relu'``, then :math:`\text{ReLU}` is used instead of :math:`\tanh`. + + Args: + input_size: The number of expected features in the input `x` + hidden_size: The number of features in the hidden state `h` + num_layers: Number of recurrent layers. E.g., setting ``num_layers=2`` + would mean stacking two RNNs together to form a `stacked RNN`, + with the second RNN taking in outputs of the first RNN and + computing the final results. Default: 1 + nonlinearity: The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'`` + bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. + Default: ``True`` + batch_first: If ``True``, then the input and output tensors are provided + as `(batch, seq, feature)`. Default: ``False`` + dropout: If non-zero, introduces a `Dropout` layer on the outputs of each + RNN layer except the last layer, with dropout probability equal to + :attr:`dropout`. Default: 0 + bidirectional: If ``True``, becomes a bidirectional RNN. Default: ``False`` + + Inputs: input, h_0 + - **input** of shape `(seq_len, batch, input_size)`: tensor containing the features + of the input sequence. The input can also be a packed variable length + sequence. See :func:`torch.nn.utils.rnn.pack_padded_sequence` + or :func:`torch.nn.utils.rnn.pack_sequence` + for details. + - **h_0** of shape `(num_layers * num_directions, batch, hidden_size)`: tensor + containing the initial hidden state for each element in the batch. + Defaults to zero if not provided. If the RNN is bidirectional, + num_directions should be 2, else it should be 1. + + Outputs: output, h_n + - **output** of shape `(seq_len, batch, num_directions * hidden_size)`: tensor + containing the output features (`h_t`) from the last layer of the RNN, + for each `t`. If a :class:`torch.nn.utils.rnn.PackedSequence` has + been given as the input, the output will also be a packed sequence. + + For the unpacked case, the directions can be separated + using ``output.view(seq_len, batch, num_directions, hidden_size)``, + with forward and backward being direction `0` and `1` respectively. + Similarly, the directions can be separated in the packed case. + - **h_n** of shape `(num_layers * num_directions, batch, hidden_size)`: tensor + containing the hidden state for `t = seq_len`. + + Like *output*, the layers can be separated using + ``h_n.view(num_layers, num_directions, batch, hidden_size)``. + + Shape: + - Input1: :math:`(L, N, H_{in})` tensor containing input features where + :math:`H_{in}=\text{input\_size}` and `L` represents a sequence length. + - Input2: :math:`(S, N, H_{out})` tensor + containing the initial hidden state for each element in the batch. + :math:`H_{out}=\text{hidden\_size}` + Defaults to zero if not provided. where :math:`S=\text{num\_layers} * \text{num\_directions}` + If the RNN is bidirectional, num_directions should be 2, else it should be 1. + - Output1: :math:`(L, N, H_{all})` where :math:`H_{all}=\text{num\_directions} * \text{hidden\_size}` + - Output2: :math:`(S, N, H_{out})` tensor containing the next hidden state + for each element in the batch + + Attributes: + weight_ih_l[k]: the learnable input-hidden weights of the k-th layer, + of shape `(hidden_size, input_size)` for `k = 0`. Otherwise, the shape is + `(hidden_size, num_directions * hidden_size)` + weight_hh_l[k]: the learnable hidden-hidden weights of the k-th layer, + of shape `(hidden_size, hidden_size)` + bias_ih_l[k]: the learnable input-hidden bias of the k-th layer, + of shape `(hidden_size)` + bias_hh_l[k]: the learnable hidden-hidden bias of the k-th layer, + of shape `(hidden_size)` + + .. note:: + All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` + where :math:`k = \frac{1}{\text{hidden\_size}}` + + .. include:: cudnn_persistent_rnn.rst + + Examples:: + + >>> rnn = nn.RNN(10, 20, 2) + >>> input = torch.randn(5, 3, 10) + >>> h0 = torch.randn(2, 3, 20) + >>> output, hn = rnn(input, h0) + """ + + def __init__(self, *args, **kwargs): + self.nonlinearity = kwargs.pop('nonlinearity', 'tanh') + if self.nonlinearity == 'tanh': + mode = 'RNN_TANH' + elif self.nonlinearity == 'relu': + mode = 'RNN_RELU' + else: + raise ValueError("Unknown nonlinearity '{}'".format(self.nonlinearity)) + super(RNN, self).__init__(mode, *args, **kwargs) + +torch.nn.RNN = RNN \ No newline at end of file diff --git a/torch_ipex/ops/roi_align.py b/torch_ipex/ops/roi_align.py new file mode 100644 index 000000000..f91136dce --- /dev/null +++ b/torch_ipex/ops/roi_align.py @@ -0,0 +1,68 @@ +# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. +import torch +from torch import nn +from torch.autograd import Function +from torch.autograd.function import once_differentiable +from torch.nn.modules.utils import _pair + +import torch_ipex._C as core + + +class _ROIAlign(Function): + @staticmethod + def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio): + ctx.save_for_backward(roi) + ctx.output_size = _pair(output_size) + ctx.spatial_scale = spatial_scale + ctx.sampling_ratio = sampling_ratio + ctx.input_shape = input.size() + output = core.roi_align_forward( + input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio + ) + return output + + @staticmethod + @once_differentiable + def backward(ctx, grad_output): + rois, = ctx.saved_tensors + output_size = ctx.output_size + spatial_scale = ctx.spatial_scale + sampling_ratio = ctx.sampling_ratio + bs, ch, h, w = ctx.input_shape + grad_input = core.roi_align_backward( + grad_output, + rois, + spatial_scale, + output_size[0], + output_size[1], + bs, + ch, + h, + w, + sampling_ratio, + ) + return grad_input, None, None, None, None + + +roi_align = _ROIAlign.apply + + +class ROIAlign(nn.Module): + def __init__(self, output_size, spatial_scale, sampling_ratio): + super(ROIAlign, self).__init__() + self.output_size = output_size + self.spatial_scale = spatial_scale + self.sampling_ratio = sampling_ratio + + def forward(self, input, rois): + return roi_align( + input, rois, self.output_size, self.spatial_scale, self.sampling_ratio + ) + + def __repr__(self): + tmpstr = self.__class__.__name__ + "(" + tmpstr += "output_size=" + str(self.output_size) + tmpstr += ", spatial_scale=" + str(self.spatial_scale) + tmpstr += ", sampling_ratio=" + str(self.sampling_ratio) + tmpstr += ")" + return tmpstr diff --git a/torch_ipex/ops/save.py b/torch_ipex/ops/save.py new file mode 100644 index 000000000..23aaf9f0c --- /dev/null +++ b/torch_ipex/ops/save.py @@ -0,0 +1,31 @@ +import torch +import copy +from torch._six import string_classes as _string_classes +import copyreg +import pickle +import pathlib + +DEFAULT_PROTOCOL = 2 + +torch_save = torch.save + +def save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL, _use_new_zipfile_serialization=False): + def to_cpu(obj): + for k in obj.keys(): + if isinstance(obj[k], dict): + to_cpu(obj[k]) + elif torch.is_tensor(obj[k]) and obj[k].device.type == 'xpu': + obj[k] = obj[k].to('cpu') + + if isinstance(obj, dict): + obj_copy = copy.deepcopy(obj) + to_cpu(obj_copy) + elif torch.is_tensor(obj) and obj.device.type == 'xpu': + obj_copy = copy.deepcopy(obj).to('cpu') + elif isinstance(obj, torch.nn.Module): + obj_copy = copy.deepcopy(obj).to('cpu') + else: + obj_copy = obj + return torch_save(obj_copy, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization) + +torch.save = save \ No newline at end of file diff --git a/torch_ipex/ops/to.py b/torch_ipex/ops/to.py new file mode 100644 index 000000000..6fa6c3889 --- /dev/null +++ b/torch_ipex/ops/to.py @@ -0,0 +1,26 @@ +import torch +import torch_ipex._C as core + +torch_to = torch.nn.Module.to + +def apply(m, fn): + for sub_module in m.children(): + apply(sub_module, fn) + fn(m) + return m + +def to(module, *args, **kwargs): + m = torch_to(module, *args, **kwargs) + + device, dtype, non_blocking, convert_to_format = torch._C._nn._parse_to(*args, **kwargs) + + if not device or device.type != "xpu": + return m + + def mark_param(t): + for param in t.parameters(): + core.set_parameter_tensor(param.data) + + return apply(m, mark_param) + +torch.nn.Module.to = to diff --git a/torch_ipex/optim/__init__.py b/torch_ipex/optim/__init__.py new file mode 100644 index 000000000..b58308cc4 --- /dev/null +++ b/torch_ipex/optim/__init__.py @@ -0,0 +1,2 @@ +from .split_sgd import is_available +from .split_sgd import SplitSGD diff --git a/torch_ipex/optim/split_sgd.py b/torch_ipex/optim/split_sgd.py new file mode 100644 index 000000000..898a3d1e3 --- /dev/null +++ b/torch_ipex/optim/split_sgd.py @@ -0,0 +1,71 @@ +import torch +from torch.optim.optimizer import Optimizer, required +import torch_ipex + +_available = False +try: + from torch_ipex._C import packed_add_ + _available = True +except ImportError as e: + pass + +def is_available(): + return _available + +class SplitSGD(Optimizer): + r"""Implements low precision stochastic gradient descent with extra state.""" + + def __init__(self, params, lr=required, momentum=0, dampening=0, + weight_decay=0, nesterov=False): + if not is_available(): + raise ValueError("Module function 'packed_add_' not available for SplitSGD") + if lr is not required and lr < 0.0: + raise ValueError("Invalid learning rate: {}".format(lr)) + if momentum != 0.0: + raise ValueError("Invalid momentum value: {}".format(momentum)) + if weight_decay != 0.0: + raise ValueError("Invalid weight_decay value: {}".format(weight_decay)) + + defaults = dict(lr=lr, momentum=momentum, dampening=dampening, + weight_decay=weight_decay, nesterov=nesterov) + if nesterov: + raise ValueError("Invalid nesterov value") + super(SplitSGD, self).__init__(params, defaults) + + def __setstate__(self, state): + super(SplitSGD, self).__setstate__(state) + for group in self.param_groups: + group.setdefault('nesterov', False) + + def step(self, closure=None): + """Performs a single optimization step. + + Arguments: + closure (callable, optional): A closure that reevaluates the model + and returns the loss. + """ + loss = None + if closure is not None: + loss = closure() + + for group in self.param_groups: + weight_decay = group['weight_decay'] + + for p in group['params']: + if p.grad is None: + continue + d_p = p.grad.data + if p.dtype == torch.bfloat16: + param_state = self.state[p] + if 'bottom_half' not in param_state: + b_d = param_state['bottom_half'] = torch.zeros_like( + p.data, dtype=torch.bfloat16, device=p.data.device) + else: + b_d = param_state['bottom_half'] + + if p.dtype == torch.bfloat16: + packed_add_(p.data, b_d, d_p, -group['lr']) + else: + p.data.add_(d_p, alpha=-group['lr']) + + return loss diff --git a/torch_ipex/tensor.py b/torch_ipex/tensor.py new file mode 100644 index 000000000..590fe5e77 --- /dev/null +++ b/torch_ipex/tensor.py @@ -0,0 +1,13 @@ +import torch + +org_tensor_deep_copy = torch.Tensor.__deepcopy__ + +def __ipex_tensor_deepcopy__(self, memo): + if self.device.type == 'xpu': + with torch.no_grad(): + new_tensor = self.clone() + return new_tensor + else: + return org_tensor_deep_copy(self, memo) + +torch.Tensor.__deepcopy__ = __ipex_tensor_deepcopy__ diff --git a/torch_ipex/version.py b/torch_ipex/version.py new file mode 100644 index 000000000..dcfc2b3e9 --- /dev/null +++ b/torch_ipex/version.py @@ -0,0 +1,4 @@ +# Autogenerated file, do not edit! +__version__ = '1.2.0' +__ipex_gitrev__ = '50b306ac855a76e35aacf9ab1571ac41b7243ae8' +__torch_gitrev__ = '' From c3c4368d2f14fcc0d2874d7ff0c0321e2ddac2a2 Mon Sep 17 00:00:00 2001 From: tangleintel Date: Sat, 5 Jun 2021 22:16:01 +0800 Subject: [PATCH 03/35] modified _C.cpython.xxxx.so's rpath --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a8ad4b4db..dfd6ca8c4 100644 --- a/setup.py +++ b/setup.py @@ -420,7 +420,8 @@ def make_relative_rpath(path): extra_compile_args=main_compile_args + extra_compile_args, include_dirs=include_paths(), library_dirs=library_dirs, - extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) + # extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) + extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('..')]) return C_ext setup( From 8aba98f3f79e021b372a52d0eebfaa6373e6662c Mon Sep 17 00:00:00 2001 From: tangleintel Date: Mon, 7 Jun 2021 14:40:31 +0800 Subject: [PATCH 04/35] Keep intel_pytorch_extension to ensure backward-compatibility --- intel_pytorch_extension_py/__init__.py | 142 ---- intel_pytorch_extension_py/launch.py | 650 ------------------ intel_pytorch_extension_py/ops/__init__.py | 16 - .../ops/embeddingbag.py | 14 - .../ops/frozen_batch_norm.py | 21 - intel_pytorch_extension_py/ops/gru.py | 21 - intel_pytorch_extension_py/ops/interaction.py | 26 - intel_pytorch_extension_py/ops/jit.py | 47 -- intel_pytorch_extension_py/ops/layer_norm.py | 13 - intel_pytorch_extension_py/ops/linear.py | 17 - intel_pytorch_extension_py/ops/lstm.py | 59 -- intel_pytorch_extension_py/ops/mlp.py | 238 ------- intel_pytorch_extension_py/ops/nms.py | 4 - intel_pytorch_extension_py/ops/pooling.py | 25 - intel_pytorch_extension_py/ops/rnn.py | 415 ----------- intel_pytorch_extension_py/ops/roi_align.py | 68 -- intel_pytorch_extension_py/ops/save.py | 31 - intel_pytorch_extension_py/ops/to.py | 26 - intel_pytorch_extension_py/optim/__init__.py | 2 - intel_pytorch_extension_py/optim/split_sgd.py | 71 -- intel_pytorch_extension_py/tensor.py | 13 - setup.py | 8 +- tests/cpu/common_device_type.py | 2 +- tests/cpu/common_ipex_conf.py | 11 +- tests/cpu/common_utils.py | 4 +- tests/cpu/linear_prepack.py | 10 +- tests/cpu/test_bf16_lazy_reorder.py | 571 ++++++++------- tests/cpu/test_emb.py | 2 +- tests/cpu/test_int8.py | 13 +- tests/cpu/test_interaction.py | 3 +- tests/cpu/test_jit.py | 6 +- tests/cpu/test_lazy_reorder.py | 143 ++-- tests/cpu/test_mlp.py | 3 +- tests/cpu/test_rn50_cpu_ops.py | 18 +- tests/cpu/test_sparse.py | 2 +- tests/cpu/test_torch.py | 4 +- .../utils/test_lazy_reorder_with_pattern.py | 54 +- tests/cpu/utils/utils.py | 3 +- torch_ipex/__init__.py | 59 +- 39 files changed, 455 insertions(+), 2380 deletions(-) delete mode 100644 intel_pytorch_extension_py/__init__.py delete mode 100644 intel_pytorch_extension_py/launch.py delete mode 100644 intel_pytorch_extension_py/ops/__init__.py delete mode 100644 intel_pytorch_extension_py/ops/embeddingbag.py delete mode 100644 intel_pytorch_extension_py/ops/frozen_batch_norm.py delete mode 100644 intel_pytorch_extension_py/ops/gru.py delete mode 100644 intel_pytorch_extension_py/ops/interaction.py delete mode 100644 intel_pytorch_extension_py/ops/jit.py delete mode 100644 intel_pytorch_extension_py/ops/layer_norm.py delete mode 100644 intel_pytorch_extension_py/ops/linear.py delete mode 100644 intel_pytorch_extension_py/ops/lstm.py delete mode 100644 intel_pytorch_extension_py/ops/mlp.py delete mode 100644 intel_pytorch_extension_py/ops/nms.py delete mode 100644 intel_pytorch_extension_py/ops/pooling.py delete mode 100644 intel_pytorch_extension_py/ops/rnn.py delete mode 100644 intel_pytorch_extension_py/ops/roi_align.py delete mode 100644 intel_pytorch_extension_py/ops/save.py delete mode 100644 intel_pytorch_extension_py/ops/to.py delete mode 100644 intel_pytorch_extension_py/optim/__init__.py delete mode 100644 intel_pytorch_extension_py/optim/split_sgd.py delete mode 100644 intel_pytorch_extension_py/tensor.py diff --git a/intel_pytorch_extension_py/__init__.py b/intel_pytorch_extension_py/__init__.py deleted file mode 100644 index 58eaa69d0..000000000 --- a/intel_pytorch_extension_py/__init__.py +++ /dev/null @@ -1,142 +0,0 @@ -import os -import json -import warnings -import torch -from .version import __version__ -from .tensor import * -from .optim import * -from .ops import * -import _torch_ipex as core -_C.enable_torch_ccl() - -DEVICE = 'xpu:0' - -class AmpConf(object): - def __init__(self, mixed_dtype = torch.bfloat16, configure_file = None): - self.dtype = mixed_dtype - self.configure_file = configure_file - - if self.dtype != torch.bfloat16: - _C.clear_indicators() - # for int8 path, if user give a exited configure file, load it. - if self.configure_file != None and self.dtype != torch.bfloat16: - if os.path.exists(self.configure_file) and os.stat(self.configure_file).st_size != 0: - with open(self.configure_file, 'r') as f: - configures = json.load(f) - _C.load_indicators_file(configures) - else: - assert False, 'Can not load a empty file or none existed file, plese first do calibartion step' - - # for int8 quantization, will save the date after doing calibration step. - def save(self, configure_file): - _C.add_indicators() - configures = _C.get_int8_configures() - with open(configure_file, 'w') as fp: - json.dump(configures, fp, indent = 4) - -class _DecoratorContextManager: - """Allow a context manager to be used as a decorator, copy form pytorch FW""" - - def __call__(self, func): - if inspect.isgeneratorfunction(func): - return self._wrap_generator(func) - - @functools.wraps(func) - def decorate_context(*args, **kwargs): - with self: - return func(*args, **kwargs) - return decorate_context - - def _wrap_generator(self, func): - """Wrap each generator invocation with the context manager""" - @functools.wraps(func) - def generator_context(*args, **kwargs): - gen = func(*args, **kwargs) - while True: - try: - with self: - x = next(gen) - yield x - except StopIteration: - break - return generator_context - -def get_auto_mix_precision(): - if _C.get_mix_bf16_fp32(): - return torch.bfloat16 - elif _C.get_mix_int8_fp32(): - return torch.int8 - else: - return None - -def _enable_auto_optimization(mixed_dtype = None, train = False): - if mixed_dtype != None: - _C.enable_auto_dnnl() - enable_auto_mixed_precision(mixed_dtype, train) - -def enable_auto_mixed_precision(mixed_dtype = torch.bfloat16, train = False): - r""" Enable auto-mixed-precision to improve performance for global scope. - - The auto-mixed-precision auto reorders the tensor to the specified low precision data type. - You don't need to convert the input tensors and the model to the specified data type manually, - the extension will do it automatically and then dispatch the extension backend to accelerate - computation - - Args: - mixed_dtype(torch.dtype): Auto reorder the input tensors to the specified low precision data type - and dispatch to oneDNN backend for computation, can be torch.bfloat16 or None. - """ - running_mode = 'training' if train else 'inference' - AutoMixPrecision(AmpConf(mixed_dtype), running_mode).__enter__() - -def _get_auto_optimization(): - return get_auto_mix_precision - -def get_train(): - return _C.get_train() - -class AutoMixPrecision(_DecoratorContextManager): - def __init__(self, conf, running_mode = 'inference'): - self.pre_mixed_dtype = get_auto_mix_precision() - self.pre_running_mode = get_train() - self.pre_calibration_state = _C.get_int8_calibration() - self.mixed_dtype = conf.dtype - self.running_mode = running_mode - - def __enter__(self): - if self.mixed_dtype == torch.bfloat16: - _C.enable_mix_bf16_fp32() - _C.disable_mix_int8_fp32() - elif self.mixed_dtype == torch.int8: - _C.enable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() - if self.running_mode == 'inference': - _C.disable_int8_calibration() - elif self.running_mode == 'calibration': - _C.enable_int8_calibration() - else: - assert False, 'int8 quantization only suport inference and calibration running mode' - else: - _C.disable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() - _C.set_execution_mode(train = True if self.running_mode == 'training' else False) - - def __exit__(self, *args): - if self.mixed_dtype == torch.int8: - if self.running_mode == 'calibration': - _C.calibration_reset() - # restore previous state - if self.pre_calibration_state: - _C.enable_int8_calibration() - else: - _C.disable_int8_calibration() - if self.pre_mixed_dtype == torch.bfloat16: - _C.enable_mix_bf16_fp32() - _C.disable_mix_int8_fp32() - elif self.pre_mixed_dtype == torch.int8: - _C.enable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() - else: - _C.disable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() - _C.set_execution_mode(train = self.pre_running_mode) diff --git a/intel_pytorch_extension_py/launch.py b/intel_pytorch_extension_py/launch.py deleted file mode 100644 index 675bcacbd..000000000 --- a/intel_pytorch_extension_py/launch.py +++ /dev/null @@ -1,650 +0,0 @@ -from __future__ import absolute_import, division, print_function, unicode_literals -import sys -import platform -import subprocess -import os -from os.path import expanduser -import re -import glob -import numpy as np -from argparse import ArgumentParser, REMAINDER -from argparse import RawTextHelpFormatter -import logging -import psutil - -logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s') -logger = logging.getLogger(__name__) - -r""" -This is a script for launching PyTorch training and inference on Intel Xeon CPU with optimal configurations. -Now, single instance inference/training, multi-instance inference/training and distributed training -with oneCCL backend is enabled. - -To get the peak performance on Intel Xeon CPU, the script optimizes the configuration of thread and memory -management. For thread management, the script configures thread affinity and the preload of Intel OMP library. -For memory management, it configures NUMA binding and preload optimized memory allocation library (e.g. tcmalloc, jemalloc). - -**How to use this module:** - -*** Single instance inference/training *** - -1. Run single-instance inference or training on a single node with all CPU sockets. - -:: - - >>> python -m intel_pytorch_extension.launch script.py args - -2. Run single-instance inference or training on a single CPU socket. - -:: - - >>> python -m intel_pytorch_extension.launch --socket_id 1 script.py args - -*** Multi-instance inference *** - -1. Multi-instance - By default, one instance per socket. if you want to set the instance numbers and core per instance, - --nintances and --ncore_per_instance should be set. - - - >>> python -m intel_pytorch_extension.launch --multi_instance python_script args - - eg: on CLX8280 with 14 instance, 4 cores per instance -:: - - >>> python -m intel_pytorch_extension.launch --multi_instance --nintances 14 --ncore_per_instance 4 python_script args - - -*** Distributed Training *** - -spawns up multiple distributed training processes on each of the training nodes. For intel_pytorch_extension, oneCCL -is used as the communication backend and MPI used to launch multi-proc. To get the better -performance, you should specify the different cores for oneCCL communication and computation -process seperately. This tool can automatically set these ENVs(such as I_MPI_PIN_DOMIN) and launch -multi-proc for you. - -The utility can be used for single-node distributed training, in which one or -more processes per node will be spawned. It can also be used in -multi-node distributed training, by spawning up multiple processes on each node -for well-improved multi-node distributed training performance as well. - - -1. Single-Node multi-process distributed training - -:: - - >>> python -m intel_pytorch_extension.launch --distributed python_script --arg1 --arg2 --arg3 and all other - arguments of your training script - -2. Multi-Node multi-process distributed training: (e.g. two nodes) - - -rank 0: *(IP: 192.168.10.10, and has a free port: 295000)* - -:: - - >>> python -m intel_pytorch_extension.launch --distributed --nproc_per_node=xxx - --nnodes=2 --hostfile hostfile python_sript --arg1 --arg2 --arg3 - and all other arguments of your training script) - - -3. To look up what optional arguments this module offers: - -:: - - >>> python -m intel_pytorch_extension.launch --help - -*** Memory allocator *** - -"--enable_tcmalloc" and "--enable_jemalloc" can be used to enable different memory allcator. - -""" - -class CPUinfo(): - def __init__(self): - - self.cpuinfo = [] - if platform.system() == "Windows": - raise RuntimeError("Windows platform is not supported!!!") - elif platform.system() == "Linux": - args = ["lscpu", "--parse=CPU,Core,Socket,Node"] - lscpu_info = subprocess.check_output(args, universal_newlines=True).split("\n") - - # Get information about cpu, core, socket and node - for line in lscpu_info: - pattern = r"^([\d]+,[\d]+,[\d]+,[\d]+)" - regex_out = re.search(pattern, line) - if regex_out: - self.cpuinfo.append(regex_out.group(1).strip().split(",")) - self._get_socket_info() - - def _get_socket_info(self): - - self.socket_physical_cores = [] #socket_id is index - self.socket_logical_cores = [] #socket_id is index - self.sockets = int(max([line[2] for line in self.cpuinfo])) + 1 - for socket_id in range(self.sockets): - cur_socket_physical_core = [] - cur_socket_logical_core = [] - for line in self.cpuinfo: - if socket_id == int(line[2]): - if line[1] not in cur_socket_physical_core: - cur_socket_physical__C.append(line[1]) - cur_socket_logical__C.append(line[0]) - self.socket_physical_cores.append(cur_socket_physical_core) - self.socket_logical_cores.append(cur_socket_logical_core) - - - def socket_nums(self): - return self.sockets - - def physical_core_nums(self): - return len(self.socket_physical_cores) * len(self.socket_physical_cores[0]) - - def logical_core_nums(self): - return len(self.socket_logical_cores) * len(self.socket_logical_cores[0]) - - def get_socket_physical_cores(self, socket_id): - if socket_id < 0 or socket_id > self.sockets - 1: - logger.error("Invalid socket id") - return self.socket_physical_cores[socket_id] - - def get_socket_logical_cores(self, socket_id): - if socket_id < 0 or socket_id > self.sockets - 1: - logger.error("Invalid socket id") - return self.socket_logical_cores[socket_id] - - def get_all_physical_cores(self): - return np.array(self.socket_physical_cores).flatten().tolist() - - def get_all_logical_cores(self): - return np.array(self.socket_logical_cores).flatten().tolist() - - -def set_mpi_pin_domain(args): - ''' - I_MPI_PIN_DOMAIN specify the cores used for every MPI process. - The first ccl_worker_count cores of every rank for ccl communication - and the other cores will be used to do computation. - For example: on CascadeLake 8280 CPU, 2 ranks on one node. ccl_worker_count=4 - CCL_WORKER_COUNT=4 - CCL_WORKER_AFFINITY="0,1,2,3,28,29,30,31" - I_MPI_PIN_DOMAIN=[0xffffff0,0xffffff0000000] - ''' - cpuinfo = CPUinfo() - ppn = args.nproc_per_node - total_cores = cpuinfo.physical_core_nums() - if args.use_logical_core: - total_cores = cpuinfo.logcal_core_nums() - cores_per_rank = total_cores // ppn - pin_domain = "[" - for proc in range(ppn): - domain_binary = 0 - begin = proc * cores_per_rank + args.ccl_worker_count - end = proc * cores_per_rank + cores_per_rank -1 - for i in range(begin, end + 1): - domain_binary |= (1 << i) - pin_domain += hex(domain_binary) + "," - return pin_domain + "]" - -def set_ccl_worker_affinity(args): - ''' - computation and communication use different cores when using oneCCL - backend for distributed training. we use first ccl_worker_count cores of - every rank for ccl communication - ''' - cpuinfo = CPUinfo() - ppn = args.nproc_per_node - total_cores = cpuinfo.physical_core_nums() - if args.use_logical_core: - total_cores = cpuinfo.logcal_core_nums() - cores_per_rank = total_cores // ppn - affinity = '' - for proc in range(ppn): - for ccl_worker in range(args.ccl_worker_count): - affinity += str(proc * cores_per_rank + ccl_worker)+ "," - os.environ["CCL_WORKER_AFFINITY"] = affinity - - -def add_lib_preload(lib_type=None): - ''' - Enale TCMalloc/JeMalloc/iomp - ''' - library_paths = [] - if "CONDA_PREFIX" in os.environ: - library_paths.append(os.environ["CONDA_PREFIX"] + "/lib/") - - library_paths += ["{}/.local/lib/".format(expanduser("~")), "/usr/local/lib/", - "/usr/local/lib64/", "/usr/lib/", "/usr/lib64/"] - lib_find = False - for lib_path in library_paths: - library_file = lib_path + "lib" + lib_type + ".so" - matches = glob.glob(library_file) - if len(matches) > 0: - if "LD_PRELOAD" in os.environ: - os.environ["LD_PRELOAD"] = matches[0] + ":" + os.environ["LD_PRELOAD"] - else: - os.environ["LD_PRELOAD"] = matches[0] - lib_find = True - break - return lib_find - -def set_memory_allocator(args): - if args.enable_tcmalloc and args.enable_jemalloc: - logger.error("Unable to enable TCMalloc and JEMalloc at the same time") - exit(-1) - - if args.enable_tcmalloc: - find_tc = add_lib_preload(lib_type="tcmalloc") - if not find_tc: - logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" - " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " - "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." - .format("TCmalloc", "tcmalloc", expanduser("~"))) - else: - logger.info("Use TCMalloc memory allocator") - - elif args.enable_jemalloc: - find_je = add_lib_preload(lib_type="jemalloc") - if not find_je: - logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" - " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " - "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." - .format("JeMalloc", "jemalloc", expanduser("~"))) - else: - logger.info("Use JeMallocl memory allocator") - - elif args.use_default_allocator: - pass - - else: - find_tc = add_lib_preload(lib_type="tcmalloc") - if find_tc: - logger.info("Use TCMalloc memory allocator") - return - find_je = add_lib_preload(lib_type="jemalloc") - if find_je: - logger.info("Use JeMallocl memory allocator") - return - logger.warning("Both TCMalloc and JeMalloc are not fount in $CONDA_PREFIX/lib or /.local/lib/" - " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " - "~/.local/lib/ so the LD_PRELOAD environment variable will not be set. This may drop the performance" - .format(expanduser("~"))) - -def set_multi_thread_and_allcator(args): - - set_memory_allocator(args) - if "OMP_NUM_THREADS" not in os.environ: - os.environ["OMP_NUM_THREADS"] = str(args.ncore_per_instance) - elif "OMP_NUM_THREADS" in os.environ: - args.ncore_per_instance = int(os.environ["OMP_NUM_THREADS"]) - - if "KMP_AFFINITY" not in os.environ: - os.environ["KMP_AFFINITY"] = args.kmp_affinity - - if "KMP_BLOCKTIME" not in os.environ: - os.environ["KMP_BLOCKTIME"] = "1" - - if "DNNL_PRIMITIVE_CACHE_CAPACITY" not in os.environ: - os.environ["DNNL_PRIMITIVE_CACHE_CAPACITY"] = '1024' - - logger.info("OMP_NUM_THREADS={} ".format(os.environ["OMP_NUM_THREADS"])) - logger.info("KMP_AFFINITY={}".format(os.environ["KMP_AFFINITY"])) - logger.info("KMP_BLOCKTIME={}".format(os.environ["KMP_BLOCKTIME"])) - logger.info("DNNL_PRIMITIVE_CACHE_CAPACITY={}".format(os.environ["DNNL_PRIMITIVE_CACHE_CAPACITY"])) - - if args.enable_iomp: - find_iomp = add_lib_preload(lib_type="iomp") - if not find_iomp: - logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" - " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " - "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." - .format("iomp", "iomp", expanduser("~"))) - else: - logger.info("User iomp") - -def launch(args): - ''' - single-instance / multi-instance launcher - ''' - processes = [] - cores = [] - - cpuinfo = CPUinfo() - if args.core_list:#user specify what cores will be used by params - cores = args.core_list.strip().split(",") - if args.ncore_per_instance == -1: - logger.error("please specify the '--ncore_per_instance' if you have pass the --core_list params") - exit(-1) - elif args.ninstances > 1 and args.ncore_per_instance * args.ninstances < len(cores): - logger.warning("only first {} cores will be used, but you specify {} cores in core_list".format - (args.ncore_per_instance * args.ninstances, len(cores))) - else: - args.ninstances = len(cores) // args.ncore_per_instance - else: - if args.use_logical_core: - if args.socket_id != -1: - cores = cpuinfo.get_socket_logical_cores(args.socket_id) - else: - cores = cpuinfo.get_all_logical_cores() - else: - if args.socket_id != -1: - cores = cpuinfo.get_socket_physical_cores(args.socket_id) - else: - cores = cpuinfo.get_all_physical_cores() - if not args.multi_instance and args.ninstances == -1 and args.ncore_per_instance == -1: - args.ninstances = 1; - args.ncore_per_instance = len(cores) - elif args.multi_instance and args.ninstances == -1 and args.ncore_per_instance == -1: - args.throughput_performance = True - elif args.ncore_per_instance == -1 and args.ninstances != -1: - args.ncore_per_instance = len(cores) // args.ninstances - elif args.ncore_per_instance != -1 and args.ninstances == -1: - args.ninstances = len(cores) // args.ncore_per_instance - else: - if args.ninstances * args.ncore_per_instance > len(cores): - logger.error("Please make sure ninstances * ncore_per_instance <= total_cores") - exit(-1) - if args.latency_performance: - if args.ncore_per_instance !=4: - logger.warning("latency_performance is a specail mode, args.ncore_per_instance can only be set to be 4") - args.ncore_per_instance = 4 - cores = cpuinfo.get_all_physical_cores() - args.ninstances = len(cores) // args.ncore_per_instance - - if args.throughput_performance: - args.ninstances = cpuinfo.socket_nums() - cores = cpuinfo.get_all_physical_cores() - args.ncore_per_instance = len(cores) // args.ninstances - - os.environ["LAUNCH_CMD"] = "#" - set_multi_thread_and_allcator(args) - for i in range(args.ninstances): - cmd = [] - cur_process_cores = "" - if not args.disable_numactl: - cmd = ["numactl"] - for core in cores[i * args.ncore_per_instance:(i + 1) * args.ncore_per_instance]: - cur_process_cores = cur_process_cores + str(core) + "," - numa_params = "-C {} ".format(cur_process_cores[:-1]) - cmd.extend(numa_params.split()) - with_python = not args.no_python - if with_python: - cmd.append(sys.executable) - if args.module: - cmd.append("-m") - cmd.append(args.program) - cmd.extend(args.program_args) - os.environ["LAUNCH_CMD"] += " ".join(cmd) + ",#" - process = subprocess.Popen(cmd, env=os.environ) - processes.append(process) - os.environ["LAUNCH_CMD"] = os.environ["LAUNCH_CMD"][:-2] - for process in processes: - process.wait() - if process.returncode != 0: - raise subprocess.CalledProcessError(returncode=process.returncode, - cmd=cmd) - -def mpi_dist_launch(args): - ''' - Set ENVs and launch MPI process for distributed training. - ''' - if args.nnodes > 1 and not os.path.exists(args.hostfile): - raise ValueError("hostfile is necessary when you use multi-node distributed training," - "Please create hostfile which include the ip list you used for distributed running") - elif args.nnodes > 1: - ipv4_addr_pattern = r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" - ip_list = [] - with open(args.hostfile) as f: - for line in f: - line = line.strip().strip("\n") - is_valid = re.match(ipv4_addr_pattern, line) - if not is_valid: - logger.error("{} is not valid IPV4 address".format(line)) - exit(-1) - else: - ip_list.append(line) - if len(ip_list) < args.nnodes: - logger.error("The number of IP {} should greater than nnodes parameters {}".format(len(ip_list), args.nnodes)) - exit(-1) - master_check = False - dic = psutil.net_if_addrs() - for adapter in dic: - snicList = dic[adapter] - for snic in snicList: - if snic.address == ip_list[0]: - master_check = True - if not master_check: - logger.error("MASTER_ADDR is not right. Please make sure the first ip {} in your hostfile is the current node".format(ip_list[0])) - exit(-1) - - logger.info("Begin to validate the ip connect") - args.master_addr = ip_list[0] - for ip in ip_list[1:]: - completed_process = subprocess.run("ssh -o PasswordAuthentication=no {} ':'".format(ip), shell=True) - if completed_process.returncode != 0: - logger.error("Passwordless SSH login to {} failed, please make sure you have setup SSH public key right") - exit(-1) - else: - logger.info("connection from master node {} to slave node {} is OK".format(args.master_addr, ip)) - - set_memory_allocator(args) - # set distributed related environmental variables - os.environ["MASTER_ADDR"] = args.master_addr - os.environ["MASTER_PORT"] = str(args.master_port) - if "I_MPI_PIN_DOMAIN" not in os.environ: - mpi_pin_domain = set_mpi_pin_domain(args) - else: - mpi_pin_domain = os.environ["I_MPI_PIN_DOMAIN"] - - cpuinfo = CPUinfo() - ppn = args.nproc_per_node - total_cores = len(cpuinfo.get_all_physical_cores()) - cores_per_rank = total_cores // ppn - - if "OMP_NUM_THREADS" not in os.environ: - opm_num_threads = cores_per_rank - args.ccl_worker_count - else: - opm_num_threads = os.environ["OMP_NUM_THREADS"] - - os.environ["CCL_WORKER_COUNT"] = str(args.ccl_worker_count) - - if "CCL_WORKER_AFFINITY" not in os.environ: - set_ccl_worker_affinity(args) - - if "CCL_ATL_TRANSPORT" not in os.environ: - os.environ["CCL_ATL_TRANSPORT"] = "ofi" - - if args.enable_iomp: - find_iomp = add_lib_preload(lib_type="iomp") - if not find_iomp: - logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" - " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " - "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." - .format("iomp", "iomp", expanduser("~"))) - else: - logger.info("Enale iomp by set LD_PRELOAD") - - logger.info("MASTER_ADDR={}".format(args.master_addr)) - logger.info("MASTER_PORT={}".format(args.master_port)) - logger.info("I_MPI_PIN_DOMAIN={}".format(mpi_pin_domain)) - logger.info("OMP_NUM_THREADS={} ".format(opm_num_threads)) - logger.info("CCL_WORKER_COUNT={}".format(args.ccl_worker_count)) - logger.info("CCL_WORKER_AFFINITY={}".format(os.environ["CCL_WORKER_AFFINITY"])) - - os.environ["LAUNCH_CMD"] = "#" - cmd = ['mpiexec.hydra'] - mpi_config = "-l -np {} -ppn {} -genv I_MPI_PIN_DOMAIN={} -genv OMP_NUM_THREADS={} ".format(args.nnodes*args.nproc_per_node, - args.nproc_per_node, mpi_pin_domain, opm_num_threads) - mpi_config += args.more_mpi_parms - if args.nnodes > 1: - mpi_config += " -hostfile {}".format(args.hostfile) - cmd.extend(mpi_config.split()) - with_python = not args.no_python - if with_python: - cmd.append(sys.executable) - cmd.append("-u") - if args.module: - cmd.append("-m") - cmd.append(args.program) - cmd.extend(args.program_args) - process = subprocess.Popen(cmd, env=os.environ) - process.wait() - os.environ["LAUNCH_CMD"] += " ".join(cmd) + ",#" - os.environ["LAUNCH_CMD"] = os.environ["LAUNCH_CMD"][:-2] - -def add_distributed_training_params(parser): - - cpuinfo = CPUinfo() - socket_nums = cpuinfo.socket_nums() - - group = parser.add_argument_group("Distributed Training Parameters With oneCCL backend") - group.add_argument("--nnodes", metavar='\b', type=int, default=1, - help="The number of nodes to use for distributed " - "training") - group.add_argument("--nproc_per_node", metavar='\b', type=int, default=socket_nums, - help="The number of processes to launch on each node") - #ccl control - group.add_argument("--ccl_worker_count", metavar='\b', default=4, type=int, - help="Core numbers per rank used for ccl communication") - #mpi control - group.add_argument("--master_addr", metavar='\b', default="127.0.0.1", type=str, - help="Master node (rank 0)'s address, should be either " - "the IP address or the hostname of node 0, for " - "single node multi-proc training, the " - "--master_addr can simply be 127.0.0.1") - group.add_argument("--master_port", metavar='\b', default=29500, type=int, - help="Master node (rank 0)'s free port that needs to " - "be used for communication during distributed " - "training") - group.add_argument("--hostfile", metavar='\b', default="hostfile", type=str, - help="Hostfile is necessary for multi-node multi-proc " - "training. hostfile includes the node address list " - "node address which should be either the IP address" - "or the hostname.") - group.add_argument("--more_mpi_parms", metavar='\b', default="", type=str, - help="User can pass more parameters for mpiexec.hydra " - "except for -np -ppn -hostfile and -genv I_MPI_PIN_DOMAIN") - -def add_memory_allocator_params(parser): - - group = parser.add_argument_group("Memory Allocator Parameters") - #allocator control - group.add_argument("--enable_tcmalloc", action='store_true', default=False, - help="Enable tcmalloc allocator") - group.add_argument("--enable_jemalloc", action='store_true', default=False, - help="Enable jemalloc allocator") - group.add_argument("--use_default_allocator", action='store_true', default=False, - help="Use default memory allocator") - -def add_multi_instance_params(parser): - - group = parser.add_argument_group("Multi-instance Parameters") - #multi-instance control - group.add_argument("--ncore_per_instance", metavar='\b', default=-1, type=int, - help="Cores per instance") - group.add_argument("--ninstances", metavar='\b', default=-1, type=int, - help="For multi-instance, you should give the cores number you used for per insantance.") - group.add_argument("--latency_performance", action='store_true', default=False, - help="By detault 4 core per instance and use all physical cores") - group.add_argument("--throughput_performance", action='store_true', default=False, - help="By default one instance per socket and use all physical cores") - group.add_argument("--socket_id", metavar='\b', default=-1, type=int, - help="Socket id for multi-instance, by default all sockets will be used") - group.add_argument("--use_logical_core", action='store_true', default=False, - help="Whether only use physical cores") - group.add_argument("--disable_numactl", action='store_true', default=False, - help="Disable numactl") - group.add_argument("--core_list", metavar='\b', default=None, type=str, - help="Specify the core list as 'core_id, core_id, ....', otherwise, all the cores will be used.") - -def add_kmp_iomp_params(parser): - - group = parser.add_argument_group("KMP/IOMP Affinity Parameters") - group.add_argument("--kmp_affinity", metavar='\b', default="granularity=fine,compact,1,0", type=str, - help="KMP_AFFINITY setup, environment variable has higher priority than this args." - "defualt value is : granularity=fine,compact,1,0") - group.add_argument("--enable_iomp", action='store_true', default=False, - help="Enable iomp and libiomp.so will be add to LD_PRELOAD") - - -def parse_args(): - """ - Helper function parsing the command line options - @retval ArgumentParser - """ - parser = ArgumentParser(description="This is a script for launching PyTorch training and inference on Intel Xeon CPU " - "with optimal configurations. Now, single instance inference/training, multi-instance " - "inference/training and distributed training with oneCCL backend is enabled. " - "To get the peak performance on Intel Xeon CPU, the script optimizes the configuration " - "of thread and memory management. For thread management, the script configures thread " - "affinity and the preload of Intel OMP library. For memory management, it configures " - "NUMA binding and preload optimized memory allocation library (e.g. tcmalloc, jemalloc) " - "\n################################# Basic usage ############################# \n" - "\n 1. single instance\n" - "\n >>> python -m intel_pytorch_extension.launch python_script args \n" - "\n2. multi-instance \n" - "\n >>> python -m intel_pytorch_extension.launch --multi_instance python_script args\n" - "\n3. Single-Node multi-process distributed training\n" - "\n >>> python -m intel_pytorch_extension.launch --distributed python_script args\n" - "\n4. Multi-Node multi-process distributed training: (e.g. two nodes)\n" - "\n rank 0: *(IP: 192.168.10.10, and has a free port: 295000)*\n" - "\n >>> python -m intel_pytorch_extension.launch --distributed --nproc_per_node=2\n" - "\n --nnodes=2 --hostfile hostfile python_script args\n", - formatter_class=RawTextHelpFormatter) - - parser.add_argument("--multi_instance", action='store_true', default=False, - help="Enable multi-instance, by default one instance per socket") - - parser.add_argument('--distributed', action='store_true', default=False, - help='Enable distributed training.') - parser.add_argument("-m", "--module", default=False, action="store_true", - help="Changes each process to interpret the launch script " - "as a python module, executing with the same behavior as" - "'python -m'.") - - parser.add_argument("--no_python", default=False, action="store_true", - help="Do not prepend the --program script with \"python\" - just exec " - "it directly. Useful when the script is not a Python script.") - add_memory_allocator_params(parser) - add_kmp_iomp_params(parser) - - add_distributed_training_params(parser) - add_multi_instance_params(parser) - # positional - parser.add_argument("program", type=str, - help="The full path to the proram/script to be launched. " - "followed by all the arguments for the script") - - # rest from the training program - parser.add_argument('program_args', nargs=REMAINDER) - return parser.parse_args() - -def main(): - - env_before = set(os.environ.keys()) - if platform.system() == "Windows": - raise RuntimeError("Windows platform is not supported!!!") - - args = parse_args() - - if args.distributed and args.multi_instance: - raise RuntimeError("Either args.distributed or args.multi_instance should be set") - - if args.latency_performance and args.throughput_performance: - raise RuntimeError("Either args.latency_performance or args.throughput_performance should be set") - - if args.nnodes > 1: - args.distributed = True - - if args.distributed: - mpi_dist_launch(args) - else: - launch(args) - - for x in sorted(set(os.environ.keys()) - env_before): - logger.debug(f'{x}={os.environ[x]}') - -if __name__ == "__main__": - main() - diff --git a/intel_pytorch_extension_py/ops/__init__.py b/intel_pytorch_extension_py/ops/__init__.py deleted file mode 100644 index 277184b8f..000000000 --- a/intel_pytorch_extension_py/ops/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -from .interaction import interaction -from .embeddingbag import embeddingbag -from .linear import * -from .pooling import * -from .mlp import * -from .jit import * -from .save import * -from .to import * -from .roi_align import ROIAlign -from .roi_align import roi_align -from .nms import * -from .lstm import * -from .rnn import * -from .gru import * -from .layer_norm import * -from .frozen_batch_norm import * diff --git a/intel_pytorch_extension_py/ops/embeddingbag.py b/intel_pytorch_extension_py/ops/embeddingbag.py deleted file mode 100644 index 03fa33d33..000000000 --- a/intel_pytorch_extension_py/ops/embeddingbag.py +++ /dev/null @@ -1,14 +0,0 @@ -import torch -from torch import nn -from torch.autograd import Function -import _torch_ipex as core - -# # extension for BF16 fast path only - - -def embeddingbag(weights, indices, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset): - ret = torch.ops.torch_ipex.embedding_bag(weights, indices, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset) - if len(ret)==1: - ret += [torch.Tensor(), torch.Tensor(), torch.Tensor()] - return ret -torch.embedding_bag = embeddingbag diff --git a/intel_pytorch_extension_py/ops/frozen_batch_norm.py b/intel_pytorch_extension_py/ops/frozen_batch_norm.py deleted file mode 100644 index 300098ded..000000000 --- a/intel_pytorch_extension_py/ops/frozen_batch_norm.py +++ /dev/null @@ -1,21 +0,0 @@ -import torch -from torch import nn - -def frozen_batch_norm(x, weight, bias, running_mean, running_var): - return torch.ops.torch_ipex.frozen_batch_norm(x, weight, bias, running_mean, running_var) - -class FrozenBatchNorm2d(nn.Module): - """ - BatchNorm2d where the batch statistics and the affine parameters - are fixed - """ - - def __init__(self, n): - super(FrozenBatchNorm2d, self).__init__() - self.register_buffer("weight", torch.ones(n)) - self.register_buffer("bias", torch.zeros(n)) - self.register_buffer("running_mean", torch.zeros(n)) - self.register_buffer("running_var", torch.ones(n)) - - def forward(self, x): - return frozen_batch_norm(x, self.weight, self.bias, self.running_mean, self.running_var) diff --git a/intel_pytorch_extension_py/ops/gru.py b/intel_pytorch_extension_py/ops/gru.py deleted file mode 100644 index a8412f5ff..000000000 --- a/intel_pytorch_extension_py/ops/gru.py +++ /dev/null @@ -1,21 +0,0 @@ -import math -import torch -from torch.nn.modules.rnn import RNNBase -from torch.nn.utils.rnn import PackedSequence -from torch import _VF - -VF_gru = _VF.gru - -def ipex_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): - if input.device.type == 'xpu' and (dropout == 0 or training == False): - return torch.ops.torch_ipex.gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - else: - return VF_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - -def gru(*args): - if isinstance(args[2], torch.Tensor): - return VF_gru(*args) - else: - return ipex_gru(*args) - -_VF.gru = gru \ No newline at end of file diff --git a/intel_pytorch_extension_py/ops/interaction.py b/intel_pytorch_extension_py/ops/interaction.py deleted file mode 100644 index 0194cb3b3..000000000 --- a/intel_pytorch_extension_py/ops/interaction.py +++ /dev/null @@ -1,26 +0,0 @@ -import torch -from torch import nn -from torch.autograd import Function -import _torch_ipex as core - -def interaction(*args): - # Current pytorch dose not support vector input for c++ custom function - # So we preserve python custom function while need backward - # Since python custom function will meet GIL when run multi-thread in one process - # We will drop python custom function after c++ are supported - if torch.is_grad_enabled(): - return InteractionFunc.apply(*args) - return torch.ops.torch_ipex.interaction_forward(args) - -class InteractionFunc(Function): - @staticmethod - def forward(ctx, *args): - ctx.save_for_backward(*args) - output = torch.ops.torch_ipex.interaction_forward(args) - return output - - @staticmethod - def backward(ctx, grad_out): - args = ctx.saved_tensors - grad_in = torch.ops.torch_ipex.interaction_backward(grad_out.contiguous(), args) - return tuple(grad_in) diff --git a/intel_pytorch_extension_py/ops/jit.py b/intel_pytorch_extension_py/ops/jit.py deleted file mode 100644 index 634dad42b..000000000 --- a/intel_pytorch_extension_py/ops/jit.py +++ /dev/null @@ -1,47 +0,0 @@ -import torch -import _torch_ipex as core -from torch.jit._recursive import wrap_cpp_module - -torch._C._jit_set_profiling_mode(False) -torch._C._jit_set_profiling_executor(False) - -orig_script = torch.jit.script -orig_trace = torch.jit.trace - -def script_(obj, optimize=None, _frames_up=0, _rcb=None): - torch.jit.script = orig_script - jit_m = orig_script(obj, optimize=optimize, _frames_up=_frames_up+1, _rcb=_rcb) - torch.jit.script = script_ - - mix_state = torch.bfloat16 if _C.get_mix_bf16_fp32() else torch.int8 if _C.get_mix_int8_fp32() else None - # Disable mix precision in model fusion, since mixed precision cannot - # bring any benefits for inference, but will lead to loss of accuracy - _C.disable_mix_bf16_fp32() - _C.disable_mix_int8_fp32() - if _C.get_jit_opt() and hasattr(jit_m, '_c'): - jit_m = wrap_cpp_module(torch._C._jit_pass_fold_convbn(jit_m._c)) - if mix_state == torch.bfloat16: - _C.enable_mix_bf16_fp32() - elif mix_state == torch.int8: - _C.enable_mix_int8_fp32() - return jit_m - -def trace_(func, example_inputs, *args, **kwargs): - # Disable mix precision. torch.jit.trace will check the traced output - # against what is expected. Since mix precision will lead to - # loss of accuracy, this will raise warning during torch.jit.trace - mix_state = torch.bfloat16 if _C.get_mix_bf16_fp32() else torch.int8 if _C.get_mix_int8_fp32() else None - _C.disable_mix_bf16_fp32() - _C.disable_mix_int8_fp32() - jit_m = orig_trace(func, example_inputs, *args, **kwargs) - if _C.get_jit_opt() and hasattr(jit_m, '_c'): - jit_m = wrap_cpp_module(torch._C._jit_pass_fold_convbn(jit_m._c)) - if mix_state == torch.bfloat16: - _C.enable_mix_bf16_fp32() - elif mix_state == torch.int8: - _C.enable_mix_int8_fp32() - return jit_m - - -torch.jit.script = script_ -torch.jit.trace = trace_ diff --git a/intel_pytorch_extension_py/ops/layer_norm.py b/intel_pytorch_extension_py/ops/layer_norm.py deleted file mode 100644 index 3c3186499..000000000 --- a/intel_pytorch_extension_py/ops/layer_norm.py +++ /dev/null @@ -1,13 +0,0 @@ -import torch -import _torch_ipex as core -from typing import Optional - -torch_layer_norm = torch.layer_norm - -def _layer_norm(input, normalized_shape, weight, bias, eps, cudnn_enabled): - if input.device.type != "xpu": - return torch_layer_norm(input, normalized_shape, weight, bias, eps, cudnn_enabled) - else: - return torch.ops.torch_ipex.layer_norm(input, normalized_shape, weight, bias, eps) - -torch.layer_norm = _layer_norm diff --git a/intel_pytorch_extension_py/ops/linear.py b/intel_pytorch_extension_py/ops/linear.py deleted file mode 100644 index 9d89fed1a..000000000 --- a/intel_pytorch_extension_py/ops/linear.py +++ /dev/null @@ -1,17 +0,0 @@ -import torch -from torch.autograd import Function -import torch.nn.functional as F -import _torch_ipex as core -from typing import Optional - -def linear(input, weight, bias: Optional[torch.Tensor] = None): - return torch.ops.torch_ipex.linear(input, weight, bias) - -F.linear = linear - -class LinearRelu(torch.nn.Linear): - def __init__(self, in_features, out_features, bias=True): - super(LinearRelu, self).__init__(in_features, out_features, bias) - - def forward(self, input): - return torch.ops.torch_ipex.linear_relu(input, self.weight, self.bias) \ No newline at end of file diff --git a/intel_pytorch_extension_py/ops/lstm.py b/intel_pytorch_extension_py/ops/lstm.py deleted file mode 100644 index 25ad8ccfd..000000000 --- a/intel_pytorch_extension_py/ops/lstm.py +++ /dev/null @@ -1,59 +0,0 @@ -import torch -from torch import _VF - -VF_lstm = _VF.lstm - -def ipex_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device): - # For LSTM training with dropout, fallback to cpu due to performance issue in oneDNN mode - if training and dropout != 0: - return fallback_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device=device) - else: - return torch.ops.torch_ipex.lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - -# users may only transfer the data but not the module to IPEX device, need to check if every item in the args is on "cpu" device -def get_device(*args): - for item in args: - if isinstance(item, (tuple, list)): - for x in item: - if x.device.type != "cpu": - return x.device.type - elif isinstance(item, torch.Tensor): - if item.device.type != "cpu": - return item.device.type - return "cpu" - -def fallback_lstm(*args, device): - # move args to cpu device - args_cpu = [] - # args is a tuple which does not support item assignment - for item in args: - if isinstance(item, (tuple, list)): - item_cpu = [x.to("cpu") for x in item] - elif isinstance(item, torch.Tensor): - item_cpu = item.to("cpu") - else: - item_cpu = item - args_cpu.append(item_cpu) - - output = VF_lstm(*args_cpu) - - # move output to the original device - output_device = [] - # output is a tuple which does not support item assignment - for item in output: - item_device = item.to(device) - output_device.append(item_device) - return tuple(output_device) - -def lstm(*args): - device = get_device(*args) - if device == "cpu": - return VF_lstm(*args) - - # For LSTM with pack_padded_sequence as input, fallback to cpu due to performance issue in oneDNN mode - if isinstance(args[1], torch.Tensor): - return fallback_lstm(*args, device=device) - else: - return ipex_lstm(*args, device=device) - -_VF.lstm = lstm diff --git a/intel_pytorch_extension_py/ops/mlp.py b/intel_pytorch_extension_py/ops/mlp.py deleted file mode 100644 index 3f328d3ad..000000000 --- a/intel_pytorch_extension_py/ops/mlp.py +++ /dev/null @@ -1,238 +0,0 @@ -import math -import torch -from torch import nn -from torch.nn.parameter import Parameter -from torch.nn import init -from torch.autograd import Function -import _torch_ipex as core - -class IpexMLPHandle: - def __init__(self, N, C, K, bn, bc, bk, dtype, fuse_bias, act_type): - self.handle = _C.mlp_create_handle(N, C, K, bn, bc, bk, 1 if dtype == torch.float32 else 2, fuse_bias, act_type) - self.N = N - self.C = C - self.K = K - self.bn = bn - self.bc = bc - self.bk = bk - self.fuse_bias = fuse_bias - self.act_type = act_type - if act_type == 1: - self.relu_mask_tensor = _C.mlp_set_relu_mask(self.handle) - - def __del__(self): - if self.handle: - _C.mlp_release_handle(self.handle) - self.handle = None - self.relu_mask_tensor = None - -class IpexMLPFC(Function): - @staticmethod - def forward(ctx, input, weight, bias, handle): - #print("Inside XsmmFCForward") - #t1 = time.time() - input = input.contiguous() - weight = weight.contiguous() - bias = bias.contiguous() - output = _C.mlp_forward(handle.handle, input, weight, bias) - #t2 = time.time() - #print("XsmmFCFWD: q=%.3f" % ((t2-t1)*1000.0)) - ctx.ipex_mlp_handle = handle - ctx.save_for_backward(input, weight) - return output - - @staticmethod - def backward(ctx, grad_output): - #print("Inside XsmmFCBackward") - handle = ctx.ipex_mlp_handle - del ctx.ipex_mlp_handle - input, weight = ctx.saved_variables - #t1 = time.time() - grad_output = grad_output.contiguous() - grad_input, grad_weight, grad_bias = _C.mlp_backward(handle.handle, grad_output, input, weight) - #t2 = time.time() - #print("XsmmFCBWD: q=%.3f w=%.3f" % ((t2-t1)*1000.0, (t3-t2)*1000.0)) - return (grad_input, grad_weight, grad_bias, None) - -class IpexMLPLinear(nn.Module): - r"""PCL Linear module for using libxsmm blocked GEMM""" - - __constants__ = ['bias', 'C', 'K'] - - def __init__(self, C, K, bias=True, act_type=None, output_stays_blocked=True, default_blocking=None): - super(IpexMLPLinear, self).__init__() - self.C = C - self.K = K - self.bc = 0 #self.get_blocking_factor(C, default_blocking) # 64 if C % 64 == 0 else C - self.bk = 0 #self.get_blocking_factor(K, default_blocking) # 64 if K % 64 == 0 else K - self.nbc = 0 # C // self.bc - self.nbk = 0 # K // self.bk - self.C_pad = 0 - self.padded_C = self.C - self.N = 0 - self.nbn = 0 - self.bn = 0 - self.default_blocking = default_blocking - self.ipex_mlp_handle = None - self.set_activation_type(act_type) - self.output_stays_blocked = output_stays_blocked - self.weight = Parameter(torch.Tensor(K, C)) - - if bias: - self.bias = Parameter(torch.Tensor(K)) - else: - self.register_parameter('bias', None) - self.reset_parameters() - - def set_activation_type(self, act_type): - if not act_type: - self.act_type = 0 - elif act_type == 'relu': - self.act_type = 1 - elif act_type == 'sigmoid': - self.act_type = 2 - else: - raise RuntimeError("XsmmLinear: Unknown activation type %s" % act_type) - - def get_blocking_factor(self, dim_size, default_blocking=None): - blocking_prio_list = [64, 48, 32, 50] - if default_blocking: - blocking_prio_list = [default_blocking] + blocking_prio_list - for bs in blocking_prio_list: - if dim_size % bs == 0: - #print("Returning block size of %d for dim_size of %d" % ( bs, dim_size)) - return bs - #print("Returning block size of %d for dim_size of %d" % ( dim_size, dim_size)) - return dim_size - - def is_dtype_supported(self, dtype): - if dtype == torch.float32: - return True - elif dtype == torch.bfloat16 and self.C % 2 == 0: - return True - else: - return False - - def maybe_pad_input(self, input): - if input.dim() == 2 and input.size(1) != self.padded_C: - input = torch.cat([input, input.new_zeros([input.size(0), self.C_pad])], dim=1) - return input - - def maybe_pad_weight(self, weight): - if weight.dim() == 2 and weight.size(1) != self.padded_C: - weight = torch.cat([weight, weight.new_zeros([self.K, self.C_pad])], dim=1) - # elif weight.dim() == 4 and weight.size(1) * weight.size(2) != self.padded_C: - # raise RuntimeError("Trying to ad 4D weights") - # elif weight.dim() == 5 and weight.size(1) * weight.size(2) * weight.size(4) != self.padded_C: - # raise RuntimeError("Trying to ad 5D weights") - return weight - - def get_blocked_weight(self, to_dtype=None, block_for_dtype=None): - weight = self.weight - new_weight = None - if to_dtype: - weight = weight.to(to_dtype) - if not block_for_dtype: - block_for_dtype = weight.dtype - if self.bc == 0 or self.bk == 0: - self.update_blocking(block_for_dtype) - - weight = self.maybe_pad_weight(weight) - if weight.dim() == 2: - if block_for_dtype == torch.bfloat16: - l_view = [self.nbk, self.bk, self.nbc, self.bc // 2, 2] - l_perm = [0, 2, 3, 1, 4] - new_weight = weight.view(l_view).permute(l_perm).contiguous() - elif block_for_dtype == torch.float32: - l_view = [self.nbk, self.bk, self.nbc, self.bc] - l_perm = [0, 2, 3, 1] - new_weight = weight.view(l_view).permute(l_perm).contiguous() - else: - raise RuntimeError("Invalid datatype for blocking: %s" % block_for_dtype) - elif weight.dim() == 4: - if block_for_dtype == torch.bfloat16: - l_view = [self.nbk, self.nbc, self.bc // 2, 2, self.bk] - l_perm = [0, 1, 2, 4, 3] - new_weight = weight.view(l_view).permute(l_perm).contiguous() - elif block_for_dtype == torch.float32: - # We are already in correct format, do nothing - new_weight = weight - else: - raise RuntimeError("Invalid datatype for blocking: %s" % block_for_dtype) - elif weight.dim() == 5: - if block_for_dtype == torch.bfloat16: - # We are already in correct format, do nothing - new_weight = weight - elif block_for_dtype == torch.float32: - l_view = [self.nbk, self.nbc, self.bc, self.bk] - l_perm = [0, 1, 2, 4, 3] - new_weight = weight.permute(l_perm).view(l_view).contiguous() - else: - raise RuntimeError("Invalid datatype for blocking: %s" % block_for_dtype) - - return new_weight - - def update_blocking(self, dtype): - if dtype == torch.bfloat16 and self.padded_C % 2 != 0: - self.C_pad = 1 - self.padded_C = self.C + self.C_pad - self.bc = self.get_blocking_factor(self.padded_C, self.default_blocking) - if dtype == torch.bfloat16 and self.bc % 2 != 0: self.bc *= 2 - self.nbc = self.padded_C // self.bc - self.bk = self.get_blocking_factor(self.K, self.default_blocking) - self.nbk = self.K // self.bk - - def reset_weight_shape(self, block_for_dtype=None): - #if not self.is_dtype_supported(block_for_dtype): - # block_for_dtype = torch.float32 - #self.update_bc(block_for_dtype) - self.weight = Parameter(self.get_blocked_weight(block_for_dtype=block_for_dtype)) - - def reset_parameters(self): - init.kaiming_uniform_(self.weight, a=math.sqrt(5)) - if self.bias is not None: - bound = 1 / math.sqrt(self.C) - init.uniform_(self.bias, -bound, bound) - - def forward(self, input): - input_type = input.dtype - #if not self.is_dtype_supported(input_type): - # input = input.to(torch.float32) - if self.bc == 0 or self.bk == 0: - self.update_blocking(input_type) - input = self.maybe_pad_input(input) - if input.dtype == torch.bfloat16: - if self.bc % 2 != 0: raise RuntimeError("Bfloat16 requires even bc") - - if input.dim() == 2: - N = input.size(0) - bn = self.get_blocking_factor(N, 48) #64 if N % 64 == 0 else N - input = input.view(N//bn, bn, self.nbc, self.bc).permute(0,2,1,3) - elif input.dim() == 4: - N = input.size(0) * input.size(2) - bn = input.size(2) - else: - print("Invalid Input dimensions (%d)" % input.dim()) - - input = input.contiguous() - - if N != self.N or bn != self.bn: - # print("Create handle: ", N, self.padded_C, self.K, bn, self.bc, self.bk, input.dtype, 0 if self.bias is None else 1, self.act_type) - self.ipex_mlp_handle = IpexMLPHandle(N, self.padded_C, self.K, bn, self.bc, self.bk, input.dtype, 0 if self.bias is None else 1, self.act_type) - self.N = N - self.bn = bn - self.nbn = N // bn - - wtensor = self.get_blocked_weight(to_dtype=input.dtype) - btensor = self.bias.to(input.dtype) - output = IpexMLPFC.apply(input, wtensor, btensor, self.ipex_mlp_handle) - if not self.output_stays_blocked: - #output = output.permute(0, 2, 1, 3).view(self.N, self.K).contiguous() - output = output.permute(0, 2, 1, 3).reshape(self.N, self.K).contiguous() - output = output.to(input_type) - return output - - def extra_repr(self): - return 'C={}, K={}, bias={}'.format( - self.C, self.K, self.bias is not None - ) diff --git a/intel_pytorch_extension_py/ops/nms.py b/intel_pytorch_extension_py/ops/nms.py deleted file mode 100644 index 0b88dfcf6..000000000 --- a/intel_pytorch_extension_py/ops/nms.py +++ /dev/null @@ -1,4 +0,0 @@ -import _torch_ipex as core - -nms = _C.nms -batch_score_nms = _C.batch_score_nms \ No newline at end of file diff --git a/intel_pytorch_extension_py/ops/pooling.py b/intel_pytorch_extension_py/ops/pooling.py deleted file mode 100644 index 1e41dc35c..000000000 --- a/intel_pytorch_extension_py/ops/pooling.py +++ /dev/null @@ -1,25 +0,0 @@ -import torch -from torch.autograd import Function -import torch.nn.functional as F -import _torch_ipex as core -from torch.nn.modules.utils import _single, _pair -from typing import List - -Vector = List[int] - -def adaptive_avg_pool2d(input, output_size: Vector): - return torch.ops.torch_ipex.adaptive_avg_pool2d(input, _pair(output_size)) - -def max_pool3d(input, kernel_size: Vector, stride: Vector, padding: Vector, dilation: Vector, ceil_mode: bool): - if len(_single(stride)) == 0: - stride = kernel_size - return torch.ops.torch_ipex.max_pool3d(input, _single(kernel_size), _single(stride), _single(padding), _single(dilation), ceil_mode) - -def max_pool2d(input, kernel_size: Vector, stride: Vector, padding: Vector, dilation: Vector, ceil_mode: bool): - if len(_pair(stride)) == 0: - stride = kernel_size - return torch.ops.torch_ipex.max_pool2d(input, _pair(kernel_size), _pair(stride), _pair(padding), _pair(dilation), ceil_mode) - -torch._C._nn.adaptive_avg_pool2d = adaptive_avg_pool2d -torch.max_pool2d = max_pool2d -torch.max_pool3d = max_pool3d diff --git a/intel_pytorch_extension_py/ops/rnn.py b/intel_pytorch_extension_py/ops/rnn.py deleted file mode 100644 index 7f710c720..000000000 --- a/intel_pytorch_extension_py/ops/rnn.py +++ /dev/null @@ -1,415 +0,0 @@ -import math -import torch -import warnings -import numbers - -from torch.nn.modules import Module -from torch.nn.parameter import Parameter -from torch.nn.utils.rnn import PackedSequence -from torch.nn import init -from torch import _VF - -def rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): - if input.device.type == 'xpu' and (dropout == 0 or training == False): - return torch.ops.torch_ipex.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - else: - return _VF.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - -def rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): - if input.device.type == 'xpu' and (dropout == 0 or training == False): - return torch.ops.torch_ipex.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - else: - return _VF.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) - -_rnn_impls = { - 'RNN_TANH': _VF.rnn_tanh, - 'RNN_RELU': _VF.rnn_relu, -} - -ipex_rnn_impls = { - 'RNN_TANH': rnn_tanh, - 'RNN_RELU': rnn_relu, -} - - -def apply_permutation(tensor, permutation, dim=1): - # type: (Tensor, Tensor, int) -> Tensor - return tensor.index_select(dim, permutation) - - -class RNNBase(Module): - __constants__ = ['mode', 'input_size', 'hidden_size', 'num_layers', 'bias', - 'batch_first', 'dropout', 'bidirectional'] - - def __init__(self, mode, input_size, hidden_size, - num_layers=1, bias=True, batch_first=False, - dropout=0., bidirectional=False): - super(RNNBase, self).__init__() - self.mode = mode - self.input_size = input_size - self.hidden_size = hidden_size - self.num_layers = num_layers - self.bias = bias - self.batch_first = batch_first - self.dropout = float(dropout) - self.bidirectional = bidirectional - num_directions = 2 if bidirectional else 1 - - if not isinstance(dropout, numbers.Number) or not 0 <= dropout <= 1 or \ - isinstance(dropout, bool): - raise ValueError("dropout should be a number in range [0, 1] " - "representing the probability of an element being " - "zeroed") - if dropout > 0 and num_layers == 1: - warnings.warn("dropout option adds dropout after all but last " - "recurrent layer, so non-zero dropout expects " - "num_layers greater than 1, but got dropout={} and " - "num_layers={}".format(dropout, num_layers)) - - if mode == 'LSTM': - gate_size = 4 * hidden_size - elif mode == 'GRU': - gate_size = 3 * hidden_size - elif mode == 'RNN_TANH': - gate_size = hidden_size - elif mode == 'RNN_RELU': - gate_size = hidden_size - else: - raise ValueError("Unrecognized RNN mode: " + mode) - - self._flat_weights_names = [] - self._all_weights = [] - for layer in range(num_layers): - for direction in range(num_directions): - layer_input_size = input_size if layer == 0 else hidden_size * num_directions - - w_ih = Parameter(torch.Tensor(gate_size, layer_input_size)) - w_hh = Parameter(torch.Tensor(gate_size, hidden_size)) - b_ih = Parameter(torch.Tensor(gate_size)) - # Second bias vector included for CuDNN compatibility. Only one - # bias vector is needed in standard definition. - b_hh = Parameter(torch.Tensor(gate_size)) - layer_params = (w_ih, w_hh, b_ih, b_hh) - - suffix = '_reverse' if direction == 1 else '' - param_names = ['weight_ih_l{}{}', 'weight_hh_l{}{}'] - if bias: - param_names += ['bias_ih_l{}{}', 'bias_hh_l{}{}'] - param_names = [x.format(layer, suffix) for x in param_names] - - for name, param in zip(param_names, layer_params): - setattr(self, name, param) - self._flat_weights_names.extend(param_names) - self._all_weights.append(param_names) - - self._flat_weights = [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names] - self.flatten_parameters() - self.reset_parameters() - - def __setattr__(self, attr, value): - if hasattr(self, "_flat_weights_names") and attr in self._flat_weights_names: - # keep self._flat_weights up to date if you do self.weight = ... - idx = self._flat_weights_names.index(attr) - self._flat_weights[idx] = value - super(RNNBase, self).__setattr__(attr, value) - - def flatten_parameters(self): - """Resets parameter data pointer so that they can use faster code paths. - - Right now, this works only if the module is on the GPU and cuDNN is enabled. - Otherwise, it's a no-op. - """ - # Short-circuits if _flat_weights is only partially instantiated - if len(self._flat_weights) != len(self._flat_weights_names): - return - - for w in self._flat_weights: - if not torch.is_tensor(w): - return - # Short-circuits if any tensor in self._flat_weights is not acceptable to cuDNN - # or the tensors in _flat_weights are of different dtypes - - first_fw = self._flat_weights[0] - dtype = first_fw.dtype - for fw in self._flat_weights: - if (not torch.is_tensor(fw.data) or not (fw.data.dtype == dtype) or - not fw.data.is_cuda or - not torch.backends.cudnn.is_acceptable(fw.data)): - return - - # If any parameters alias, we fall back to the slower, copying code path. This is - # a sufficient check, because overlapping parameter buffers that don't completely - # alias would break the assumptions of the uniqueness check in - # Module.named_parameters(). - unique_data_ptrs = set(p.data_ptr() for p in self._flat_weights) - if len(unique_data_ptrs) != len(self._flat_weights): - return - - with torch.cuda.device_of(first_fw): - import torch.backends.cudnn.rnn as rnn - - # Note: no_grad() is necessary since _cudnn_rnn_flatten_weight is - # an inplace operation on self._flat_weights - with torch.no_grad(): - if torch._use_cudnn_rnn_flatten_weight(): - torch._cudnn_rnn_flatten_weight( - self._flat_weights, (4 if self.bias else 2), - self.input_size, rnn.get_cudnn_mode(self.mode), self.hidden_size, self.num_layers, - self.batch_first, bool(self.bidirectional)) - - def _apply(self, fn): - ret = super(RNNBase, self)._apply(fn) - - # Resets _flat_weights - # Note: be v. careful before removing this, as 3rd party device types - # likely rely on this behavior to properly .to() modules like LSTM. - self._flat_weights = [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names] - # Flattens params (on CUDA) - self.flatten_parameters() - - return ret - - def reset_parameters(self): - stdv = 1.0 / math.sqrt(self.hidden_size) - for weight in self.parameters(): - init.uniform_(weight, -stdv, stdv) - - def check_input(self, input, batch_sizes): - # type: (Tensor, Optional[Tensor]) -> None - expected_input_dim = 2 if batch_sizes is not None else 3 - if input.dim() != expected_input_dim: - raise RuntimeError( - 'input must have {} dimensions, got {}'.format( - expected_input_dim, input.dim())) - if self.input_size != input.size(-1): - raise RuntimeError( - 'input.size(-1) must be equal to input_size. Expected {}, got {}'.format( - self.input_size, input.size(-1))) - - def get_expected_hidden_size(self, input, batch_sizes): - # type: (Tensor, Optional[Tensor]) -> Tuple[int, int, int] - if batch_sizes is not None: - mini_batch = batch_sizes[0] - mini_batch = int(mini_batch) - else: - mini_batch = input.size(0) if self.batch_first else input.size(1) - num_directions = 2 if self.bidirectional else 1 - expected_hidden_size = (self.num_layers * num_directions, - mini_batch, self.hidden_size) - return expected_hidden_size - - def check_hidden_size(self, hx, expected_hidden_size, msg='Expected hidden size {}, got {}'): - # type: (Tensor, Tuple[int, int, int], str) -> None - if hx.size() != expected_hidden_size: - raise RuntimeError(msg.format(expected_hidden_size, tuple(hx.size()))) - - def check_forward_args(self, input, hidden, batch_sizes): - # type: (Tensor, Tensor, Optional[Tensor]) -> None - self.check_input(input, batch_sizes) - expected_hidden_size = self.get_expected_hidden_size(input, batch_sizes) - - self.check_hidden_size(hidden, expected_hidden_size) - - def permute_hidden(self, hx, permutation): - # type: (Tensor, Optional[Tensor]) -> Tensor - if permutation is None: - return hx - return apply_permutation(hx, permutation) - - def forward(self, input, hx=None): - is_packed = isinstance(input, PackedSequence) - if is_packed: - input, batch_sizes, sorted_indices, unsorted_indices = input - max_batch_size = batch_sizes[0] - max_batch_size = int(max_batch_size) - else: - batch_sizes = None - max_batch_size = input.size(0) if self.batch_first else input.size(1) - sorted_indices = None - unsorted_indices = None - - if hx is None: - num_directions = 2 if self.bidirectional else 1 - hx = torch.zeros(self.num_layers * num_directions, - max_batch_size, self.hidden_size, - dtype=input.dtype, device=input.device) - else: - # Each batch of the hidden state should match the input sequence that - # the user believes he/she is passing in. - hx = self.permute_hidden(hx, sorted_indices) - - self.check_forward_args(input, hx, batch_sizes) - _impl = _rnn_impls[self.mode] - ipex_impl = ipex_rnn_impls[self.mode] - if batch_sizes is None: - result = ipex_impl(input, hx, self._flat_weights, self.bias, self.num_layers, - self.dropout, self.training, self.bidirectional, self.batch_first) - else: - result = _impl(input, batch_sizes, hx, self._flat_weights, self.bias, - self.num_layers, self.dropout, self.training, self.bidirectional) - output = result[0] - hidden = result[1] - - if is_packed: - output = PackedSequence(output, batch_sizes, sorted_indices, unsorted_indices) - return output, self.permute_hidden(hidden, unsorted_indices) - - def extra_repr(self): - s = '{input_size}, {hidden_size}' - if self.num_layers != 1: - s += ', num_layers={num_layers}' - if self.bias is not True: - s += ', bias={bias}' - if self.batch_first is not False: - s += ', batch_first={batch_first}' - if self.dropout != 0: - s += ', dropout={dropout}' - if self.bidirectional is not False: - s += ', bidirectional={bidirectional}' - return s.format(**self.__dict__) - - def __setstate__(self, d): - super(RNNBase, self).__setstate__(d) - if 'all_weights' in d: - self._all_weights = d['all_weights'] - - if isinstance(self._all_weights[0][0], str): - return - num_layers = self.num_layers - num_directions = 2 if self.bidirectional else 1 - self._flat_weights_names = [] - self._all_weights = [] - for layer in range(num_layers): - for direction in range(num_directions): - suffix = '_reverse' if direction == 1 else '' - weights = ['weight_ih_l{}{}', 'weight_hh_l{}{}', 'bias_ih_l{}{}', 'bias_hh_l{}{}'] - weights = [x.format(layer, suffix) for x in weights] - if self.bias: - self._all_weights += [weights] - self._flat_weights_names.extend(weights) - else: - self._all_weights += [weights[:2]] - self._flat_weights_names.extend(weights[:2]) - self._flat_weights = [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names] - - @property - def all_weights(self): - return [[getattr(self, weight) for weight in weights] for weights in self._all_weights] - - def _replicate_for_data_parallel(self): - replica = super(RNNBase, self)._replicate_for_data_parallel() - # Need to copy these caches, otherwise the replica will share the same - # flat weights list. - replica._flat_weights = replica._flat_weights[:] - replica._flat_weights_names = replica._flat_weights_names[:] - return replica - - -class RNN(RNNBase): - r"""Applies a multi-layer Elman RNN with :math:`\tanh` or :math:`\text{ReLU}` non-linearity to an - input sequence. - - - For each element in the input sequence, each layer computes the following - function: - - .. math:: - h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh}) - - where :math:`h_t` is the hidden state at time `t`, :math:`x_t` is - the input at time `t`, and :math:`h_{(t-1)}` is the hidden state of the - previous layer at time `t-1` or the initial hidden state at time `0`. - If :attr:`nonlinearity` is ``'relu'``, then :math:`\text{ReLU}` is used instead of :math:`\tanh`. - - Args: - input_size: The number of expected features in the input `x` - hidden_size: The number of features in the hidden state `h` - num_layers: Number of recurrent layers. E.g., setting ``num_layers=2`` - would mean stacking two RNNs together to form a `stacked RNN`, - with the second RNN taking in outputs of the first RNN and - computing the final results. Default: 1 - nonlinearity: The non-linearity to use. Can be either ``'tanh'`` or ``'relu'``. Default: ``'tanh'`` - bias: If ``False``, then the layer does not use bias weights `b_ih` and `b_hh`. - Default: ``True`` - batch_first: If ``True``, then the input and output tensors are provided - as `(batch, seq, feature)`. Default: ``False`` - dropout: If non-zero, introduces a `Dropout` layer on the outputs of each - RNN layer except the last layer, with dropout probability equal to - :attr:`dropout`. Default: 0 - bidirectional: If ``True``, becomes a bidirectional RNN. Default: ``False`` - - Inputs: input, h_0 - - **input** of shape `(seq_len, batch, input_size)`: tensor containing the features - of the input sequence. The input can also be a packed variable length - sequence. See :func:`torch.nn.utils.rnn.pack_padded_sequence` - or :func:`torch.nn.utils.rnn.pack_sequence` - for details. - - **h_0** of shape `(num_layers * num_directions, batch, hidden_size)`: tensor - containing the initial hidden state for each element in the batch. - Defaults to zero if not provided. If the RNN is bidirectional, - num_directions should be 2, else it should be 1. - - Outputs: output, h_n - - **output** of shape `(seq_len, batch, num_directions * hidden_size)`: tensor - containing the output features (`h_t`) from the last layer of the RNN, - for each `t`. If a :class:`torch.nn.utils.rnn.PackedSequence` has - been given as the input, the output will also be a packed sequence. - - For the unpacked case, the directions can be separated - using ``output.view(seq_len, batch, num_directions, hidden_size)``, - with forward and backward being direction `0` and `1` respectively. - Similarly, the directions can be separated in the packed case. - - **h_n** of shape `(num_layers * num_directions, batch, hidden_size)`: tensor - containing the hidden state for `t = seq_len`. - - Like *output*, the layers can be separated using - ``h_n.view(num_layers, num_directions, batch, hidden_size)``. - - Shape: - - Input1: :math:`(L, N, H_{in})` tensor containing input features where - :math:`H_{in}=\text{input\_size}` and `L` represents a sequence length. - - Input2: :math:`(S, N, H_{out})` tensor - containing the initial hidden state for each element in the batch. - :math:`H_{out}=\text{hidden\_size}` - Defaults to zero if not provided. where :math:`S=\text{num\_layers} * \text{num\_directions}` - If the RNN is bidirectional, num_directions should be 2, else it should be 1. - - Output1: :math:`(L, N, H_{all})` where :math:`H_{all}=\text{num\_directions} * \text{hidden\_size}` - - Output2: :math:`(S, N, H_{out})` tensor containing the next hidden state - for each element in the batch - - Attributes: - weight_ih_l[k]: the learnable input-hidden weights of the k-th layer, - of shape `(hidden_size, input_size)` for `k = 0`. Otherwise, the shape is - `(hidden_size, num_directions * hidden_size)` - weight_hh_l[k]: the learnable hidden-hidden weights of the k-th layer, - of shape `(hidden_size, hidden_size)` - bias_ih_l[k]: the learnable input-hidden bias of the k-th layer, - of shape `(hidden_size)` - bias_hh_l[k]: the learnable hidden-hidden bias of the k-th layer, - of shape `(hidden_size)` - - .. note:: - All the weights and biases are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` - where :math:`k = \frac{1}{\text{hidden\_size}}` - - .. include:: cudnn_persistent_rnn.rst - - Examples:: - - >>> rnn = nn.RNN(10, 20, 2) - >>> input = torch.randn(5, 3, 10) - >>> h0 = torch.randn(2, 3, 20) - >>> output, hn = rnn(input, h0) - """ - - def __init__(self, *args, **kwargs): - self.nonlinearity = kwargs.pop('nonlinearity', 'tanh') - if self.nonlinearity == 'tanh': - mode = 'RNN_TANH' - elif self.nonlinearity == 'relu': - mode = 'RNN_RELU' - else: - raise ValueError("Unknown nonlinearity '{}'".format(self.nonlinearity)) - super(RNN, self).__init__(mode, *args, **kwargs) - -torch.nn.RNN = RNN \ No newline at end of file diff --git a/intel_pytorch_extension_py/ops/roi_align.py b/intel_pytorch_extension_py/ops/roi_align.py deleted file mode 100644 index 19585ae2f..000000000 --- a/intel_pytorch_extension_py/ops/roi_align.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. -import torch -from torch import nn -from torch.autograd import Function -from torch.autograd.function import once_differentiable -from torch.nn.modules.utils import _pair - -import _torch_ipex as core - - -class _ROIAlign(Function): - @staticmethod - def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio): - ctx.save_for_backward(roi) - ctx.output_size = _pair(output_size) - ctx.spatial_scale = spatial_scale - ctx.sampling_ratio = sampling_ratio - ctx.input_shape = input.size() - output = _C.roi_align_forward( - input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio - ) - return output - - @staticmethod - @once_differentiable - def backward(ctx, grad_output): - rois, = ctx.saved_tensors - output_size = ctx.output_size - spatial_scale = ctx.spatial_scale - sampling_ratio = ctx.sampling_ratio - bs, ch, h, w = ctx.input_shape - grad_input = _C.roi_align_backward( - grad_output, - rois, - spatial_scale, - output_size[0], - output_size[1], - bs, - ch, - h, - w, - sampling_ratio, - ) - return grad_input, None, None, None, None - - -roi_align = _ROIAlign.apply - - -class ROIAlign(nn.Module): - def __init__(self, output_size, spatial_scale, sampling_ratio): - super(ROIAlign, self).__init__() - self.output_size = output_size - self.spatial_scale = spatial_scale - self.sampling_ratio = sampling_ratio - - def forward(self, input, rois): - return roi_align( - input, rois, self.output_size, self.spatial_scale, self.sampling_ratio - ) - - def __repr__(self): - tmpstr = self.__class__.__name__ + "(" - tmpstr += "output_size=" + str(self.output_size) - tmpstr += ", spatial_scale=" + str(self.spatial_scale) - tmpstr += ", sampling_ratio=" + str(self.sampling_ratio) - tmpstr += ")" - return tmpstr diff --git a/intel_pytorch_extension_py/ops/save.py b/intel_pytorch_extension_py/ops/save.py deleted file mode 100644 index 23aaf9f0c..000000000 --- a/intel_pytorch_extension_py/ops/save.py +++ /dev/null @@ -1,31 +0,0 @@ -import torch -import copy -from torch._six import string_classes as _string_classes -import copyreg -import pickle -import pathlib - -DEFAULT_PROTOCOL = 2 - -torch_save = torch.save - -def save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL, _use_new_zipfile_serialization=False): - def to_cpu(obj): - for k in obj.keys(): - if isinstance(obj[k], dict): - to_cpu(obj[k]) - elif torch.is_tensor(obj[k]) and obj[k].device.type == 'xpu': - obj[k] = obj[k].to('cpu') - - if isinstance(obj, dict): - obj_copy = copy.deepcopy(obj) - to_cpu(obj_copy) - elif torch.is_tensor(obj) and obj.device.type == 'xpu': - obj_copy = copy.deepcopy(obj).to('cpu') - elif isinstance(obj, torch.nn.Module): - obj_copy = copy.deepcopy(obj).to('cpu') - else: - obj_copy = obj - return torch_save(obj_copy, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization) - -torch.save = save \ No newline at end of file diff --git a/intel_pytorch_extension_py/ops/to.py b/intel_pytorch_extension_py/ops/to.py deleted file mode 100644 index b8f7c5858..000000000 --- a/intel_pytorch_extension_py/ops/to.py +++ /dev/null @@ -1,26 +0,0 @@ -import torch -import _torch_ipex as core - -torch_to = torch.nn.Module.to - -def apply(m, fn): - for sub_module in m.children(): - apply(sub_module, fn) - fn(m) - return m - -def to(module, *args, **kwargs): - m = torch_to(module, *args, **kwargs) - - device, dtype, non_blocking, convert_to_format = torch._C._nn._parse_to(*args, **kwargs) - - if not device or device.type != "xpu": - return m - - def mark_param(t): - for param in t.parameters(): - _C.set_parameter_tensor(param.data) - - return apply(m, mark_param) - -torch.nn.Module.to = to diff --git a/intel_pytorch_extension_py/optim/__init__.py b/intel_pytorch_extension_py/optim/__init__.py deleted file mode 100644 index b58308cc4..000000000 --- a/intel_pytorch_extension_py/optim/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .split_sgd import is_available -from .split_sgd import SplitSGD diff --git a/intel_pytorch_extension_py/optim/split_sgd.py b/intel_pytorch_extension_py/optim/split_sgd.py deleted file mode 100644 index 422edac44..000000000 --- a/intel_pytorch_extension_py/optim/split_sgd.py +++ /dev/null @@ -1,71 +0,0 @@ -import torch -from torch.optim.optimizer import Optimizer, required -import _torch_ipex - -_available = False -try: - from _torch_ipex import packed_add_ - _available = True -except ImportError as e: - pass - -def is_available(): - return _available - -class SplitSGD(Optimizer): - r"""Implements low precision stochastic gradient descent with extra state.""" - - def __init__(self, params, lr=required, momentum=0, dampening=0, - weight_decay=0, nesterov=False): - if not is_available(): - raise ValueError("Module function 'packed_add_' not available for SplitSGD") - if lr is not required and lr < 0.0: - raise ValueError("Invalid learning rate: {}".format(lr)) - if momentum != 0.0: - raise ValueError("Invalid momentum value: {}".format(momentum)) - if weight_decay != 0.0: - raise ValueError("Invalid weight_decay value: {}".format(weight_decay)) - - defaults = dict(lr=lr, momentum=momentum, dampening=dampening, - weight_decay=weight_decay, nesterov=nesterov) - if nesterov: - raise ValueError("Invalid nesterov value") - super(SplitSGD, self).__init__(params, defaults) - - def __setstate__(self, state): - super(SplitSGD, self).__setstate__(state) - for group in self.param_groups: - group.setdefault('nesterov', False) - - def step(self, closure=None): - """Performs a single optimization step. - - Arguments: - closure (callable, optional): A closure that reevaluates the model - and returns the loss. - """ - loss = None - if closure is not None: - loss = closure() - - for group in self.param_groups: - weight_decay = group['weight_decay'] - - for p in group['params']: - if p.grad is None: - continue - d_p = p.grad.data - if p.dtype == torch.bfloat16: - param_state = self.state[p] - if 'bottom_half' not in param_state: - b_d = param_state['bottom_half'] = torch.zeros_like( - p.data, dtype=torch.bfloat16, device=p.data.device) - else: - b_d = param_state['bottom_half'] - - if p.dtype == torch.bfloat16: - packed_add_(p.data, b_d, d_p, -group['lr']) - else: - p.data.add_(d_p, alpha=-group['lr']) - - return loss diff --git a/intel_pytorch_extension_py/tensor.py b/intel_pytorch_extension_py/tensor.py deleted file mode 100644 index 590fe5e77..000000000 --- a/intel_pytorch_extension_py/tensor.py +++ /dev/null @@ -1,13 +0,0 @@ -import torch - -org_tensor_deep_copy = torch.Tensor.__deepcopy__ - -def __ipex_tensor_deepcopy__(self, memo): - if self.device.type == 'xpu': - with torch.no_grad(): - new_tensor = self.clone() - return new_tensor - else: - return org_tensor_deep_copy(self, memo) - -torch.Tensor.__deepcopy__ = __ipex_tensor_deepcopy__ diff --git a/setup.py b/setup.py index dfd6ca8c4..4d1b2294b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from __future__ import print_function TORCH_VERSION = '1.8.0' -TORCH_IPEX_VERSION = '1.3.0' +TORCH_IPEX_VERSION = '1.8.0' # import torch import platform @@ -436,7 +436,11 @@ def make_relative_rpath(path): packages=[ 'torch_ipex', 'torch_ipex.ops', - 'torch_ipex.optim'], + 'torch_ipex.optim', + 'intel_pytorch_extension', + 'intel_pytorch_extension.ops', + 'intel_pytorch_extension.optim'], + package_dir={'intel_pytorch_extension': 'torch_ipex'}, package_data={ 'torch_ipex':[ 'README.md', diff --git a/tests/cpu/common_device_type.py b/tests/cpu/common_device_type.py index fc5c71eca..805a493bd 100644 --- a/tests/cpu/common_device_type.py +++ b/tests/cpu/common_device_type.py @@ -49,7 +49,7 @@ from functools import wraps import unittest import torch -import torch_ipex as ipex +import intel_pytorch_extension as ipex import copy from common_utils import TestCase, TEST_WITH_ROCM, TEST_MKL, \ skipCUDANonDefaultStreamIf diff --git a/tests/cpu/common_ipex_conf.py b/tests/cpu/common_ipex_conf.py index 35fe22bfb..ee0e9ae1b 100644 --- a/tests/cpu/common_ipex_conf.py +++ b/tests/cpu/common_ipex_conf.py @@ -1,6 +1,5 @@ import torch -import torch_ipex as ipex -# import intel_pytorch_extension as ipex +import intel_pytorch_extension as ipex class AutoMixPrecision(object): def __init__(self, enable_or_not = False, train = False): @@ -28,12 +27,12 @@ def __init__(self, enable_or_not = False): def __enter__(self): if self.enable_or_not: - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() else: - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def __exit__(self, *args, **kwargs): if self.old_value: - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() else: - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() diff --git a/tests/cpu/common_utils.py b/tests/cpu/common_utils.py index 068a16e59..fbd42eb37 100644 --- a/tests/cpu/common_utils.py +++ b/tests/cpu/common_utils.py @@ -576,7 +576,7 @@ def skipIfNotRegistered(op_name, message): """Wraps the decorator to hide the import of the `core`. Args: - op_name: Check if this op is registered in `_C._REGISTERED_OPERATORS`. + op_name: Check if this op is registered in `core._REGISTERED_OPERATORS`. message: message to fail with. Usage: @@ -585,7 +585,7 @@ def skipIfNotRegistered(op_name, message): """ try: from caffe2.python import core - skipper = unittest.skipIf(op_name not in _C._REGISTERED_OPERATORS, + skipper = unittest.skipIf(op_name not in core._REGISTERED_OPERATORS, message) except ImportError: skipper = unittest.skip("Cannot import `caffe2.python.core`") diff --git a/tests/cpu/linear_prepack.py b/tests/cpu/linear_prepack.py index 55b86601e..d2ab6540d 100644 --- a/tests/cpu/linear_prepack.py +++ b/tests/cpu/linear_prepack.py @@ -2,7 +2,7 @@ import intel_pytorch_extension as ipex from common_utils import int8_calibration -ipex._C.enable_auto_dnnl() +ipex.core.enable_auto_dnnl() ic = 1024 oc = 1024 @@ -30,8 +30,8 @@ def run_linear(auto_mix_conf=None): run_linear(bf16_conf) print(f"back to fp32, {'*' * 50}") - ipex._C.reorder_to_float32(LL.weight) - ipex._C.reorder_to_float32(LL.bias) + ipex.core.reorder_to_float32(LL.weight) + ipex.core.reorder_to_float32(LL.bias) run_linear() print(f"auto-mix for int8, {'*' * 50}") @@ -40,6 +40,6 @@ def run_linear(auto_mix_conf=None): run_linear(int8_conf) print(f"back to fp32, {'*' * 50}") - ipex._C.reorder_to_float32(LL.weight) - ipex._C.reorder_to_float32(LL.bias) + ipex.core.reorder_to_float32(LL.weight) + ipex.core.reorder_to_float32(LL.bias) run_linear() \ No newline at end of file diff --git a/tests/cpu/test_bf16_lazy_reorder.py b/tests/cpu/test_bf16_lazy_reorder.py index 145a451f5..04b979de3 100644 --- a/tests/cpu/test_bf16_lazy_reorder.py +++ b/tests/cpu/test_bf16_lazy_reorder.py @@ -12,8 +12,7 @@ import sys import itertools import torch -# import intel_pytorch_extension as ipex -import torch_ipex as ipex +import intel_pytorch_extension as ipex import torch.nn as nn import torch.backends.cudnn as cudnn @@ -124,9 +123,9 @@ def test_to(self): def check_param(t, is_param): for param in t.parameters(): if is_param: - self.assertTrue(ipex._C.is_parameter_tensor(param.data)) + self.assertTrue(ipex.core.is_parameter_tensor(param.data)) else: - self.assertFalse(ipex._C.is_parameter_tensor(param.data)) + self.assertFalse(ipex.core.is_parameter_tensor(param.data)) apply(m_cpu, check_param, False) apply(m_data_type, check_param, False) @@ -157,9 +156,9 @@ def test_Conv2d_with_cpu(self): with AutoMixPrecision(True): self.assertEqual(in_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(in_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(in_auto_mix)) res_auto_bf16 = conv_auto_mix(in_auto_mix) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_bf16)) self.assertEqual(res_man_bf16.float(), res_auto_bf16.float()) def test_Conv2d_backward(self): @@ -185,10 +184,10 @@ def test_Conv2d_backward(self): with AutoMixPrecision(True, train=True): self.assertEqual(in_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(in_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(in_auto_mix)) out_auto_bf16 = conv_auto_mix(in_auto_mix).sum() out_auto_bf16.backward() - self.assertTrue(ipex._C.is_bf16_dil_tensor(in_auto_mix.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(in_auto_mix.grad)) self.assertEqual(in_man_bf16.grad.float(), in_auto_mix.grad.float()) class TestDeconv(TestCase): @@ -249,19 +248,19 @@ def _test_deconv(self, dims): with AutoDNNL(True), AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_infer)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.weight)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.weight)) if bias: - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.bias)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.bias)) y_auto_mix_infer = module_auto_mix_infer(x_auto_mix_infer) y_auto_mix_infer.sum().backward() if padding - output_padding + stride > 0: - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_infer.grad)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.weight)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_infer.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.weight)) if bias: - self.assertTrue(ipex._C.is_bf16_dil_tensor(module_auto_mix_infer.bias)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(module_auto_mix_infer.bias)) self.assertEqual(y_aten, y_auto_mix_infer, atol=1e-1, rtol=1e-5) @@ -275,21 +274,21 @@ def _test_deconv(self, dims): with AutoDNNL(True), AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.weight)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.weight)) if bias: - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.bias)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.bias)) y_auto_mix_train = module_auto_mix_train(x_auto_mix_train) y_auto_mix_train.sum().backward() if padding - output_padding + stride > 0: - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train.grad)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.weight)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.weight.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.weight)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.weight.grad)) if bias: - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.bias)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(module_auto_mix_train.bias.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.bias)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(module_auto_mix_train.bias.grad)) self.assertEqual( y_aten, y_auto_mix_train, atol=1e-1, rtol=1e-5) @@ -339,12 +338,12 @@ def test_batch_norm2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) print(res_bf16.device) print(res_auto_mix_inference.device) self.assertEqual(res_bf16.float().to("cpu"), res_auto_mix_inference.to("cpu")) @@ -352,23 +351,23 @@ def test_batch_norm2d(self): # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16) def test_batch_norm2d_backward(self): @@ -389,21 +388,21 @@ def test_batch_norm2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_batch_norm3d(self): @@ -424,34 +423,34 @@ def test_batch_norm3d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_batch_norm3d_backward(self): @@ -473,21 +472,21 @@ def test_batch_norm3d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestLayerNorm(TestCase): @@ -509,34 +508,34 @@ def test_layer_norm(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16) def test_layer_norm_backward(self): @@ -557,21 +556,21 @@ def test_layer_norm_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestRelu(TestCase): @@ -591,34 +590,34 @@ def test_relu(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_relu_(self): @@ -634,28 +633,28 @@ def test_relu_(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) x_auto_mix_inference.relu_() self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(x_man_bf16.float(), x_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) x_auto_mix_train.relu_() self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(x_cpu, x_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) x_auto_mix_train_bf16.relu_() self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(x_man_bf16.float(), x_auto_mix_train_bf16, 1e-3) def test_relu_backward(self): @@ -677,21 +676,21 @@ def test_relu_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestGelu(TestCase): @@ -710,34 +709,34 @@ def test_gelu(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train, 1e-3) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_gelu_backward(self): @@ -758,21 +757,21 @@ def test_gelu_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad.float(), x_auto_mix.grad.float(), 1e-3) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad.float()) class TestShape(TestCase): @@ -787,12 +786,12 @@ def test_slice(self): x_cpu_slice = x_cpu[3:7, 3:7, 5] x_dpcpp = x_cpu.to(device=device) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp)) # the storage should be converted to bf16 on slicing x_dpcpp_slice = x_dpcpp[3:7, 3:7, 5] - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp_slice)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp_slice)) # check shape info self._check_tensor_shape(x_cpu, x_dpcpp) @@ -806,8 +805,8 @@ def test_slice(self): # check sliced data. This should convert the storage back to fp32 self.assertEqual(x_cpu_slice, x_dpcpp_slice, atol=1e-1, rtol=1e-5) self.assertEqual(x_cpu, x_dpcpp, atol=1e-1, rtol=1e-5) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp_slice)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp_slice)) # check shape info self._check_tensor_shape(x_cpu, x_dpcpp) @@ -963,11 +962,11 @@ def test_unbind(self): x_cpu_unbind = torch.unbind(x_cpu) with AutoDNNL(True), AutoMixPrecision(True): - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_dpcpp)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_dpcpp)) x_dpcpp_unbind = torch.unbind(x_dpcpp) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp_unbind[0])) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_dpcpp_unbind[1])) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp_unbind[0])) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_dpcpp_unbind[1])) self._check_tensor_shape(x_cpu_unbind[0], x_dpcpp_unbind[0]) self._check_tensor_shape(x_cpu_unbind[1], x_dpcpp_unbind[1]) @@ -1020,18 +1019,18 @@ def test_add(self): with AutoMixPrecision(True, train=False): # fp32 + fp32 self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) res_auto_mix_infer = x_auto_mix_a_infer + x_auto_mix_b_infer self.assertEqual(res_auto_mix_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_infer)) self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) self.assertEqual(res_auto_mix_infer.float(), res_man_bf16.float()) @@ -1039,34 +1038,34 @@ def test_add(self): with AutoMixPrecision(True, train=True): # bf16 + bf16 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_bf16 = x_auto_mix_bf16_a + x_auto_mix_bf16_b self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) # bf16 + fp32 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_b.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b)) res_auto_mix_reorder_b = x_auto_mix_bf16_a + x_auto_mix_b self.assertEqual(res_auto_mix_reorder_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_b)) self.assertEqual(res_auto_mix_reorder_b.float(), res_man_bf16.float()) # fp32 + bf16 self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_reorder_a = x_auto_mix_a + x_auto_mix_bf16_b self.assertEqual(res_auto_mix_reorder_a.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_a)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_a)) self.assertEqual(res_auto_mix_reorder_a, res_cpu, atol=1e-1, rtol=1e-5) @@ -1089,18 +1088,18 @@ def test_mul(self): with AutoMixPrecision(True, train=False): # fp32 * fp32 self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) res_auto_mix_infer = x_auto_mix_a_infer * x_auto_mix_b_infer self.assertEqual(res_auto_mix_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_infer)) self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) self.assertEqual(res_auto_mix_infer.float(), res_man_bf16.float()) @@ -1108,34 +1107,34 @@ def test_mul(self): with AutoMixPrecision(True, train=True): # bf16 * bf16 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_bf16 = x_auto_mix_bf16_a * x_auto_mix_bf16_b self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) # bf16 * fp32 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_b.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b)) res_auto_mix_reorder_b = x_auto_mix_bf16_a * x_auto_mix_b self.assertEqual(res_auto_mix_reorder_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_b)) self.assertEqual(res_auto_mix_reorder_b.float(), res_man_bf16.float()) # fp32 * bf16 self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_reorder_a = x_auto_mix_a * x_auto_mix_bf16_b self.assertEqual(res_auto_mix_reorder_a.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_reorder_a)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_reorder_a)) self.assertEqual(res_auto_mix_reorder_a.float(), res_cpu.float(), atol=1e-1, rtol=1e-5) @@ -1180,16 +1179,16 @@ def test_mul_(self): with AutoMixPrecision(True, train=False): # fp32 + fp32 self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) x_auto_mix_a_infer *= x_auto_mix_b_infer self.assertEqual(x_auto_mix_a_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_a_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_a_infer)) self.assertEqual(x_auto_mix_b_infer.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_b_infer)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_b_infer)) self.assertEqual(x_auto_mix_a_infer.float(), res_man_bf16.float()) @@ -1197,34 +1196,34 @@ def test_mul_(self): with AutoMixPrecision(True, train=True): # bf16 * bf16 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) x_auto_mix_bf16_a *= x_auto_mix_bf16_b self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_a.float(), x_man_bf16_a.float()) # bf16 * fp32 self.assertEqual(x_auto_mix_bf16_a_.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) self.assertEqual(x_auto_mix_b.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_b)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_b)) x_auto_mix_bf16_a_ *= x_auto_mix_b self.assertEqual(x_auto_mix_bf16_a_.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a_)) self.assertEqual(x_auto_mix_bf16_a_.float(), res_man_bf16.float()) # fp32 * bf16 self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) x_auto_mix_a *= x_auto_mix_bf16_b self.assertEqual(x_auto_mix_a.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_a)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_a)) self.assertEqual(x_auto_mix_a, x_cpu_a, atol=1e-1, rtol=1e-5) @@ -1241,12 +1240,12 @@ def test_div(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) res_auto_mix_bf16 = x_auto_mix_bf16_a / x_auto_mix_bf16_b self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) @@ -1263,12 +1262,12 @@ def test_div_(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_b.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_b)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_b)) x_auto_mix_bf16_a /= x_auto_mix_bf16_b self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_a.float(), x_man_bf16_a.float()) @@ -1284,10 +1283,10 @@ def test_div_scalar(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) res_auto_mix_bf16 = x_auto_mix_bf16_a / 3.3 self.assertEqual(res_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_bf16)) self.assertEqual(res_auto_mix_bf16.float(), res_man_bf16.float()) def test_div__scalar(self): @@ -1302,10 +1301,10 @@ def test_div__scalar(self): with AutoMixPrecision(True): self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) x_auto_mix_bf16_a = x_auto_mix_bf16_a / 3.3 self.assertEqual(x_auto_mix_bf16_a.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16_a)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16_a)) self.assertEqual(x_auto_mix_bf16_a.float(), x_man_bf16_a.float()) class TestLinear(TestCase): @@ -1331,11 +1330,11 @@ def test_linear(self): with AutoMixPrecision(True): res_auto_mix = linear_auto_mix(x_auto_mix) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_linear_backward(self): - ipex._C.set_execution_mode(train = True) + ipex.core.set_execution_mode(train = True) rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1360,10 +1359,10 @@ def test_linear_backward(self): with AutoMixPrecision(True, train=True): self.assertEqual(in_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(in_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(in_auto_mix)) out_auto_bf16 = linear_auto_mix(in_auto_mix).sum() out_auto_bf16.backward() - self.assertTrue(ipex._C.is_bf16_dil_tensor(in_auto_mix.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(in_auto_mix.grad)) self.assertEqual(in_man_bf16.grad.float(), in_auto_mix.grad.float()) class TestPool(TestCase): @@ -1395,34 +1394,34 @@ def test_avg_pool2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_avg_pool2d_backward(self): @@ -1457,21 +1456,21 @@ def test_avg_pool2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_avg_pool3d(self): @@ -1502,34 +1501,34 @@ def test_avg_pool3d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_avg_pool3d_backward(self): @@ -1565,23 +1564,23 @@ def test_avg_pool3d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_adaptive_avg_pool2d(self): @@ -1602,34 +1601,34 @@ def test_adaptive_avg_pool2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_adaptive_avg_pool2d_backward(self): @@ -1653,22 +1652,22 @@ def test_adaptive_avg_pool2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad.float(), x_auto_mix.grad.float()) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad.float()) def test_max_pool2d(self): @@ -1701,34 +1700,34 @@ def test_max_pool2d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference.float()) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu.float(), res_auto_mix_train.float()) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_max_pool2d_backward(self): @@ -1762,22 +1761,22 @@ def test_max_pool2d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_max_pool3d(self): @@ -1807,34 +1806,34 @@ def test_max_pool3d(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_max_pool3d_backward(self): @@ -1868,22 +1867,22 @@ def test_max_pool3d_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestIndex(TestCase): @@ -1906,9 +1905,9 @@ def test_index_select(self): with AutoMixPrecision(True): res_auto_mix = index_select_x_auto_mix + index_select_x_auto_mix self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) res_idx_select_auto = torch.index_select(res_auto_mix, 0, indices) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_idx_select_auto)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_idx_select_auto)) self.assertEqual(res_idx_select_auto, res_idx_select_man.float()) def test_index(self): @@ -1935,11 +1934,11 @@ def test_index(self): # with AutoMixPrecision(True): # res_auto_mix = index_x_auto_mix + index_x_auto_mix # self.assertEqual(res_auto_mix.dtype, torch.float) - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) # print(res_auto_mix.device) # print(indices.device) # res_idx_auto = res_auto_mix[indices] - # self.assertTrue(ipex._C.is_bf16_dil_tensor(res_idx_auto)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_idx_auto)) # self.assertEqual(res_idx_auto, res_idx_man.float()) class TestSoftMax(TestCase): @@ -1960,34 +1959,34 @@ def test_softmax(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_softmax_backward(self): @@ -2011,23 +2010,23 @@ def test_softmax_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) # TODO # grady and y both fp32 after .sum() - # self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) # self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) def test_log_softmax(self): @@ -2072,34 +2071,34 @@ def test_sigmoid(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_sigmoid_(self): @@ -2115,28 +2114,28 @@ def test_sigmoid_(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) x_auto_mix_inference.sigmoid_() self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(x_man_bf16.float(), x_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) x_auto_mix_train.sigmoid_() self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(x_cpu, x_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) x_auto_mix_train_bf16.sigmoid_() self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(x_man_bf16.float(), x_auto_mix_train_bf16, 1e-3) def test_sigmoid_backward(self): @@ -2158,23 +2157,23 @@ def test_sigmoid_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) # TODO # grady and y both fp32 after .sum() - # self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) # self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestTanh(TestCase): @@ -2194,34 +2193,34 @@ def test_tanh(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) res_auto_mix_inference = op_auto_mix_inference(x_auto_mix_inference) self.assertEqual(res_auto_mix_inference.dtype, torch.float) self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_inference)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(res_bf16.float(), res_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) res_auto_mix_train = op_auto_mix_train(x_auto_mix_train) self.assertEqual(res_auto_mix_train.dtype, torch.float) self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(res_auto_mix_train)) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(res_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(ref_cpu, res_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) res_auto_mix_train_bf16 = op_auto_mix_train_bf16(x_auto_mix_train_bf16) self.assertEqual(res_auto_mix_train_bf16.dtype, torch.float) self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix_train_bf16)) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(res_bf16.float(), res_auto_mix_train_bf16, 1e-3) def test_tanh_(self): @@ -2237,28 +2236,28 @@ def test_tanh_(self): # FW inference with AutoMixPrecision(True, train=False): self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) x_auto_mix_inference.tanh_() self.assertEqual(x_auto_mix_inference.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_inference)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_inference)) self.assertEqual(x_man_bf16.float(), x_auto_mix_inference) # FW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) x_auto_mix_train.tanh_() self.assertEqual(x_auto_mix_train.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix_train)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix_train)) self.assertEqual(x_cpu, x_auto_mix_train) # FW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) x_auto_mix_train_bf16.tanh_() self.assertEqual(x_auto_mix_train_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_train_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_train_bf16)) self.assertEqual(x_man_bf16.float(), x_auto_mix_train_bf16, 1e-3) def test_tanh_backward(self): @@ -2280,23 +2279,23 @@ def test_tanh_backward(self): # BW train (input is not bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix)) out_auto_mix = op_auto_mix(x_auto_mix).sum() out_auto_mix.backward() self.assertEqual(x_auto_mix.grad.dtype, torch.float) - self.assertFalse(ipex._C.is_bf16_dil_tensor(x_auto_mix.grad)) + self.assertFalse(ipex.core.is_bf16_dil_tensor(x_auto_mix.grad)) self.assertEqual(x_cpu.grad, x_auto_mix.grad) # BW train (input is bf16 dil tensor) with AutoMixPrecision(True, train=True): self.assertEqual(x_auto_mix_bf16.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16)) out_auto_mix_bf16 = op_auto_mix_bf16(x_auto_mix_bf16).sum() out_auto_mix_bf16.backward() self.assertEqual(x_auto_mix_bf16.grad.dtype, torch.float) # TODO # grady and y both fp32 after .sum() - # self.assertTrue(ipex._C.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(x_auto_mix_bf16.grad)) # self.assertEqual(x_man_bf16.grad.float(), x_auto_mix_bf16.grad) class TestLinearAlgebraOps(TestCase): @@ -2329,7 +2328,7 @@ def test_mm(self): with AutoMixPrecision(True): res_auto_mix = torch.mm(x_auto_mix_a, x_auto_mix_b) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_mm_out(self): @@ -2343,7 +2342,7 @@ def test_mm_out(self): with AutoMixPrecision(True): torch.mm(x_auto_mix_a, x_auto_mix_b, out=res_auto_mix) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_bmm(self): @@ -2357,7 +2356,7 @@ def test_bmm(self): with AutoMixPrecision(True): res_auto_mix = torch.bmm(x_auto_mix_a, x_auto_mix_b) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_bmm_out(self): @@ -2370,7 +2369,7 @@ def test_bmm_out(self): with AutoMixPrecision(True): torch.bmm(x_auto_mix_a, x_auto_mix_b, out=res_auto_mix) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_addmm(self): @@ -2390,7 +2389,7 @@ def test_addmm(self): with AutoMixPrecision(True): res_auto_mix = torch.addmm(input=add_auto_mix, mat1=x_auto_mix_a, mat2=x_auto_mix_b, alpha=alpha, beta=beta) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_addbmm(self): @@ -2409,7 +2408,7 @@ def test_addbmm(self): with AutoMixPrecision(True): res_auto_mix = torch.addbmm(add_auto_mix, x_auto_mix_a, x_auto_mix_b, beta=beta, alpha=alpha) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) def test_baddbmm(self): @@ -2436,7 +2435,7 @@ def test_baddbmm(self): with AutoMixPrecision(True): res_auto_mix = torch.baddbmm(add_auto_mix, x_auto_mix_a, x_auto_mix_b, beta=beta, alpha=alpha) self.assertEqual(res_auto_mix.dtype, torch.float) - self.assertTrue(ipex._C.is_bf16_dil_tensor(res_auto_mix)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(res_auto_mix)) self.assertEqual(res_auto_mix, res_man_bf16.float()) class ConvRelu(nn.Module): @@ -2480,7 +2479,7 @@ def test_save_and_load(self): with AutoDNNL(True), AutoMixPrecision(True): output_dpcpp = model_dpcpp(input_dpcpp) torch.save(output_dpcpp.clone().to('cpu'), 'tensor.pt') - self.assertTrue(ipex._C.is_bf16_dil_tensor(output_dpcpp)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(output_dpcpp)) torch.save(output_dpcpp, 'tensor_dpcpp.pt') self.assertEqual(torch.load('tensor.pt'), torch.load('tensor_dpcpp.pt')) @@ -2927,7 +2926,7 @@ def test_permute(self): x_dpcpp = convert_to_bf16(x_cpu) y_cpu = x_cpu.permute(0, 2, 1, 3) y_dpcpp = x_dpcpp.permute(0, 2, 1, 3) - self.assertTrue(ipex._C.is_bf16_dil_tensor(y_dpcpp)) + self.assertTrue(ipex.core.is_bf16_dil_tensor(y_dpcpp)) self.assertEqual(y_cpu.bfloat16().float(), y_dpcpp) if __name__ == '__main__': diff --git a/tests/cpu/test_emb.py b/tests/cpu/test_emb.py index 8a64337ab..64c92d27b 100644 --- a/tests/cpu/test_emb.py +++ b/tests/cpu/test_emb.py @@ -1,6 +1,6 @@ import torch import torch.nn as nn -import torch_ipex as ipex +import intel_pytorch_extension as ipex import unittest import copy from common_utils import TestCase diff --git a/tests/cpu/test_int8.py b/tests/cpu/test_int8.py index e400e115f..975f0fb36 100644 --- a/tests/cpu/test_int8.py +++ b/tests/cpu/test_int8.py @@ -15,8 +15,7 @@ from torch.jit._recursive import wrap_cpp_module import copy -import torch_ipex as ipex -# import intel_pytorch_extension as ipex +import intel_pytorch_extension as ipex import torch.nn as nn from torch.nn import Parameter @@ -42,7 +41,7 @@ def test_quantization_status(self): conf = ipex.AmpConf(torch.int8, 'configure.json') with ipex.AutoMixPrecision(conf, running_mode='inference'): y = model1(x1) - self.assertTrue(ipex._C.is_int8_dil_tensor(y)) + self.assertTrue(ipex.core.is_int8_dil_tensor(y)) jsonFile = open('configure.json', 'r') data = json.load(jsonFile) jsonFile.close() @@ -71,7 +70,7 @@ def test_quantization_status(self): with ipex.AutoMixPrecision(conf, running_mode='inference'): y = model2(x2) - self.assertTrue(ipex._C.is_fp32_dil_tensor(y)) + self.assertTrue(ipex.core.is_fp32_dil_tensor(y)) os.remove('configure.json') @@ -86,7 +85,7 @@ def _compare_fp32_int8(self, model, x): with ipex.AutoMixPrecision(conf, running_mode='inference'): y = model(x) - self.assertTrue(ipex._C.is_int8_dil_tensor(y)) + self.assertTrue(ipex.core.is_int8_dil_tensor(y)) self.assertEqual(ref, y, atol=1e-1, rtol=1e-5) os.remove('configure.json') @@ -102,7 +101,7 @@ def _lstm_compare_fp32_int8(self, model, *args): with torch.no_grad(): y, hy = model(*args) - self.assertTrue(ipex._C.is_int8_dil_tensor(y)) + self.assertTrue(ipex.core.is_int8_dil_tensor(y)) # self.assertEqual(ref, y, prec=0.1) self.assertEqual(ref, y, atol=0.1, rtol=1e-5) @@ -202,5 +201,5 @@ def test_lstm(self): if __name__ == '__main__': rand_seed = int(time.time() * 1000000000) torch.manual_seed(rand_seed) - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() test = unittest.main() diff --git a/tests/cpu/test_interaction.py b/tests/cpu/test_interaction.py index 99be0a9e2..8904fdd37 100644 --- a/tests/cpu/test_interaction.py +++ b/tests/cpu/test_interaction.py @@ -5,8 +5,7 @@ import torch -import torch_ipex as ipex -# import intel_pytorch_extension as ipex +import intel_pytorch_extension as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_jit.py b/tests/cpu/test_jit.py index ea246e537..9d61d781b 100644 --- a/tests/cpu/test_jit.py +++ b/tests/cpu/test_jit.py @@ -60,10 +60,8 @@ from torch.jit._recursive import wrap_cpp_module import copy -# import intel_pytorch_extension as ipex -import torch_ipex as ipex -import torch_ipex._C as core -# from intel_pytorch_extension import core +import intel_pytorch_extension as ipex +from intel_pytorch_extension import core import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_lazy_reorder.py b/tests/cpu/test_lazy_reorder.py index ef82061bb..76165f559 100644 --- a/tests/cpu/test_lazy_reorder.py +++ b/tests/cpu/test_lazy_reorder.py @@ -12,8 +12,7 @@ import sys import itertools import torch -import torch_ipex as ipex -import torch_ipex._C as core +import intel_pytorch_extension as ipex import contextlib import io @@ -60,10 +59,10 @@ def test_Conv2d_with_cpu(self): input_cpu = torch.rand((1, 1, 7, 7)) input_dpcpp = input_cpu.to(device=device) - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() out_dpcpp = conv_dpcpp(input_dpcpp) - core.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() out_dpcpp_cpu = out_dpcpp.to('cpu') out_cpu = conv_cpu(input_cpu) self.assertEqual(out_dpcpp.size(), out_cpu.size()) @@ -73,7 +72,7 @@ def test_Conv2d_backward(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() with torch.backends.mkldnn.flags(enabled=False): input = torch.rand((1, 1, 7, 7)) for bias in [True, False]: @@ -102,12 +101,12 @@ def _seq_conf(self, device, rand_seed): return out_dpcpp3 def test_seq_conv(self): - core.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_cpu = self._seq_conf('cpu', rand_seed) - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() res_dpcpp = self._seq_conf(device, rand_seed) self.assertEqual(res_cpu, res_dpcpp.to('cpu')) @@ -244,19 +243,19 @@ def _seq_conf(self, device, rand_seed): return out_dpcpp3 def test_seq_deconv(self): - core.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_cpu = self._seq_conf('cpu', rand_seed) - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() res_dpcpp = self._seq_conf(device, rand_seed) self.assertEqual(res_cpu, res_dpcpp.to('cpu')) class TestBinaryOp(TestCase): def test_add(self): # rand_seed = 1599794793172034560: AssertionError: tensor(1.5259e-05) not less than or equal to 1e-05 - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -296,12 +295,12 @@ def _test_add_(self, device, rand_seed): return a1 def test_add_(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_dcpp_dnnl = self._test_add_(device, rand_seed) - core.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() res_dcpp_cpu = self._test_add_(device, rand_seed) res_cpu = self._test_add_("cpu", rand_seed) @@ -309,12 +308,12 @@ def test_add_(self): self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) def test_add_scalar(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() a = torch.rand((8, 8)).to(device=device) a += 2 def test_mul(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -351,7 +350,7 @@ def _test_mul_(self, device, rand_seed): return a def test_mul_(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) a1 = self._test_mul_(device, rand_seed) @@ -362,7 +361,7 @@ def test_binary_propagate_group(self): rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() input = torch.rand((1, 64, 7, 7)) @@ -382,7 +381,7 @@ def test_binary_propagate_group(self): self.assertEqual(y_cpu, y_dpcpp) def test_mixed_format(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -461,7 +460,7 @@ def _test_relu_(self, device, rand_seed): return a def test_relu_(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) a1 = self._test_relu_(device, rand_seed) @@ -469,7 +468,7 @@ def test_relu_(self): self.assertEqual(a2, a1.to('cpu')) def test_relu(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -478,7 +477,7 @@ def test_relu(self): self.assertEqual(torch.relu(x_cpu), torch.relu(x_dpcpp)) def test_relu_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -493,7 +492,7 @@ def test_relu_backward(self): class TestGelu(TestCase): def test_gelu(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -502,7 +501,7 @@ def test_gelu(self): self.assertEqual(F.gelu(x_cpu), F.gelu(x_dpcpp), 0.001) def test_gelu_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -528,7 +527,7 @@ def _test_conv_add_relu_(self, device, rand_seed): return conv_op_output, conv_op_input, add_src def _test_conv_relu_(self, device, rand_seed): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() torch.manual_seed(rand_seed) conv_op = torch.nn.Conv2d(1, 1, (7, 7)).to(device=device) conv_op_input = torch.rand((1, 1, 10, 10)).to(device=device) @@ -539,24 +538,24 @@ def _test_conv_relu_(self, device, rand_seed): def test_conv_relu_(self): rand_seed = int(get_rand_seed()) res_dcpp_dnnl = self._test_conv_relu_(device, rand_seed) - self.assertTrue(core.is_dil_tensor(res_dcpp_dnnl)) + self.assertTrue(ipex.core.is_dil_tensor(res_dcpp_dnnl)) res_cpu = self._test_conv_relu_("cpu", rand_seed) self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) def test_conv_add_relu_(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) res_dcpp_dnnl, input_dpcpp_dnnl, _ = self._test_conv_add_relu_(device, rand_seed) - core.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() res_dcpp_cpu, input_dpcpp_cpu, _ = self._test_conv_add_relu_(device, rand_seed) res_cpu, input_cpu, _ = self._test_conv_add_relu_("cpu", rand_seed) self.assertEqual(res_cpu, res_dcpp_cpu.to('cpu')) self.assertEqual(res_cpu, res_dcpp_dnnl.to('cpu')) - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() res_dcpp_dnnl.sum().backward() res_dcpp_cpu.sum().backward() res_cpu.sum().backward() @@ -566,7 +565,7 @@ def test_conv_add_relu_(self): class TestLinearAlgebraOps(TestCase): def test_mm(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -589,7 +588,7 @@ def test_mm(self): self.assertEqual(y_cpu, y_dpcpp) def test_bmm(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -613,7 +612,7 @@ def test_bmm(self): self.assertEqual(y_cpu, y_dpcpp) def test_addmm(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -645,7 +644,7 @@ def test_addmm(self): def test_addbmm(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -676,7 +675,7 @@ def test_addbmm(self): self.assertEqual(res_cpu, res_dpcpp, 1e-4) def test_baddbmm(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -707,7 +706,7 @@ def test_baddbmm(self): class TestLinear(TestCase): def test_linear(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -725,8 +724,8 @@ def test_linear(self): # we should first expose aten::linear, depend on https://github.com/pytorch/pytorch/pull/20039 def test_linear_backward(self): - core.enable_auto_dnnl() - core.set_execution_mode(train = True) + ipex.core.enable_auto_dnnl() + ipex.core.set_execution_mode(train = True) rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -749,7 +748,7 @@ def test_linear_backward(self): def test_eikan_linear_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(0) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -774,7 +773,7 @@ def test_eikan_linear_backward(self): class TestPool(TestCase): def test_avg_pool2d(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -793,7 +792,7 @@ def test_avg_pool2d(self): self.assertEqual(avg_pool2d(x_cpu), avg_pool2d(x_dpcpp)) def test_avg_pool3d(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -812,7 +811,7 @@ def test_avg_pool3d(self): self.assertEqual(avg_pool3d(x_cpu), avg_pool3d(x_dpcpp)) def test_avg_pool2d_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -834,7 +833,7 @@ def test_avg_pool2d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_avg_pool3d_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -856,7 +855,7 @@ def test_avg_pool3d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_adaptive_avg_pool2d(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -872,7 +871,7 @@ def test_adaptive_avg_pool2d(self): adaptive_avg_pool2d(x_dpcpp)) def test_adaptive_avg_pool2d_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -889,7 +888,7 @@ def test_adaptive_avg_pool2d_backward(self): self.assertEqual(x_cpu.grad, x_dpcpp.grad) def test_adaptive_avg_pool2d_not_divisible(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -910,7 +909,7 @@ def test_adaptive_avg_pool2d_not_divisible(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_adaptive_avg_pool2d_backward_not_divisible(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -931,7 +930,7 @@ def test_adaptive_avg_pool2d_backward_not_divisible(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_max_pool2d(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -953,7 +952,7 @@ def test_max_pool2d(self): self.assertEqual(max_pool2d(x_cpu), max_pool2d(x_dpcpp)) def test_max_pool2d_double(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -980,7 +979,7 @@ def test_max_pool2d_double(self): self.assertEqual(torch.device(device), y_dpcpp.device) def test_max_pool3d(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1002,7 +1001,7 @@ def test_max_pool3d(self): self.assertEqual(max_pool3d(x_cpu), max_pool3d(x_dpcpp)) def test_max_pool2d_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1024,7 +1023,7 @@ def test_max_pool2d_backward(self): self.assertEqual(x1.grad, x2.grad) def test_max_pool2d_backward_double(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1050,7 +1049,7 @@ def test_max_pool2d_backward_double(self): self.assertEqual(torch.device(device), y2.device) def test_max_pool3d_backward(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1130,7 +1129,7 @@ def test_layer_norm(self): m_dpcpp = copy.deepcopy(m).to(device=device) output = m(input) output_dpcpp = m_dpcpp(input_dpcpp) - self.assertTrue(core.is_dil_tensor(output_dpcpp)) + self.assertTrue(ipex.core.is_dil_tensor(output_dpcpp)) self.assertEqual(output, output_dpcpp) def test_layer_norm_backward(self): @@ -1218,24 +1217,24 @@ def test_view(self): x_cpu = torch.randn(old_shape) x_dpcpp = x_cpu.to(device=device).clone() - self.assertTrue(core.is_dil_tensor(x_dpcpp)) - self.assertEqual(core.get_dil_tensor_sizes(x_dpcpp), [4, 16]) - self.assertEqual(core.get_dil_tensor_strides(x_dpcpp), [16, 1]) + self.assertTrue(ipex.core.is_dil_tensor(x_dpcpp)) + self.assertEqual(ipex.core.get_dil_tensor_sizes(x_dpcpp), [4, 16]) + self.assertEqual(ipex.core.get_dil_tensor_strides(x_dpcpp), [16, 1]) x_cpu_view = x_cpu.view(new_shape) self.assertEqual(x_cpu_view.size(), [1, 4, 4, 4]) self.assertEqual(x_cpu_view.stride(), [64, 16, 4, 1]) x_dpcpp_view = x_dpcpp.view(new_shape) - self.assertTrue(core.is_dil_tensor(x_dpcpp_view)) + self.assertTrue(ipex.core.is_dil_tensor(x_dpcpp_view)) y = torch.randn(new_shape) out_cpu = x_cpu_view * y # test if the shape of x_dpcpp_view is compatible with y out_dpcpp = x_dpcpp_view * y.to(device) - self.assertTrue(core.is_dil_tensor(out_dpcpp)) - self.assertEqual(core.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4]) - self.assertEqual(core.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1]) + self.assertTrue(ipex.core.is_dil_tensor(out_dpcpp)) + self.assertEqual(ipex.core.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4]) + self.assertEqual(ipex.core.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1]) self.assertEqual(out_cpu, out_dpcpp) # test if metadata of x_dpcpp has not been altered @@ -1252,22 +1251,22 @@ def test_view(self): # input to the data type of the first input if they are different res_bf16 = src_1 + src_2 res_bf16_other = src_1 + src_2 - self.assertTrue(core.is_dil_tensor(res_bf16)) - # self.assertTrue(core.is_bf16_dil_tensor(res_bf16)) - self.assertTrue(core.get_dil_tensor_sizes(res_bf16), [5120, 1, 128]) + self.assertTrue(ipex.core.is_dil_tensor(res_bf16)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_bf16)) + self.assertTrue(ipex.core.get_dil_tensor_sizes(res_bf16), [5120, 1, 128]) self.assertEqual(list(res_bf16.size()), [5120, 1, 128]) res_fp32_view = res_bf16.view(1280, 4, 1, 128) - self.assertTrue(core.is_dil_tensor(res_bf16)) - self.assertTrue(core.is_dil_tensor(res_fp32_view)) - # self.assertTrue(core.is_bf16_dil_tensor(res_bf16)) - # self.assertTrue(core.is_bf16_dil_tensor(res_fp32_view)) + self.assertTrue(ipex.core.is_dil_tensor(res_bf16)) + self.assertTrue(ipex.core.is_dil_tensor(res_fp32_view)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_bf16)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_fp32_view)) self.assertEqual(list(res_fp32_view.size()), [1280, 4, 1, 128]) tmp_res = res_bf16 + res_bf16_other - # self.assertTrue(core.is_bf16_dil_tensor(res_bf16)) - # self.assertTrue(core.is_bf16_dil_tensor(res_fp32_view)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_bf16)) + # self.assertTrue(ipex.core.is_bf16_dil_tensor(res_fp32_view)) tmp_res = res_fp32_view.index_select(0, torch.LongTensor([0, 1])) - self.assertTrue(core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) - self.assertTrue(core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) + self.assertTrue(ipex.core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) + self.assertTrue(ipex.core.get_dil_tensor_sizes(res_fp32_view), [5120, 1, 128]) self.assertEqual(list(tmp_res.size()), [2, 4, 1, 128]) def test_view_blocked(self): @@ -1566,7 +1565,7 @@ def forward(self, x): class TestSave(TestCase): def test_save_and_load_tensor(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -1577,7 +1576,7 @@ def test_save_and_load_tensor(self): self.assertEqual(torch.load('tensor.pt'), torch.load('tensor_dpcpp.pt')) def test_save_and_load_model(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -2027,7 +2026,7 @@ def test_upsample_trilinear3d_size(self): class TestPermute(TestCase): def test_permute(self): - core.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) diff --git a/tests/cpu/test_mlp.py b/tests/cpu/test_mlp.py index 7ae04ce80..62d085095 100644 --- a/tests/cpu/test_mlp.py +++ b/tests/cpu/test_mlp.py @@ -5,8 +5,7 @@ from functools import reduce import torch -import torch_ipex as ipex -# import intel_pytorch_extension as ipex +import intel_pytorch_extension as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_rn50_cpu_ops.py b/tests/cpu/test_rn50_cpu_ops.py index 9f3b3534c..a43db2bd7 100644 --- a/tests/cpu/test_rn50_cpu_ops.py +++ b/tests/cpu/test_rn50_cpu_ops.py @@ -55,7 +55,7 @@ from functools import reduce import torch -import torch_ipex as ipex +import intel_pytorch_extension as ipex from common_ipex_conf import AutoMixPrecision, AutoDNNL import torch.nn as nn @@ -332,7 +332,7 @@ def test_mul(self): a1 = torch.randn((1, 1, 3, 2), device=device) a2 = torch.randn((3, 2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex._C.is_dil_tensor(res1)) + self.assertTrue(ipex.core.is_dil_tensor(res1)) with AutoDNNL(False): a1 = a1.to(device='cpu') a2 = a2.to(device='cpu') @@ -352,7 +352,7 @@ def test_mul(self): a1 = torch.randn((1, 2, 3, 2), device=device) a2 = torch.randn((1, 3, 2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex._C.is_dil_tensor(res1)) + self.assertTrue(ipex.core.is_dil_tensor(res1)) with AutoDNNL(False): a1 = a1.to(device='cpu') a2 = a2.to(device='cpu') @@ -363,7 +363,7 @@ def test_mul(self): a1 = torch.randn((1, 2, 3, 2), device=device) a2 = torch.randn((1, 2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex._C.is_dil_tensor(res1)) + self.assertTrue(ipex.core.is_dil_tensor(res1)) with AutoDNNL(False): a1 = a1.to(device='cpu') a2 = a2.to(device='cpu') @@ -374,7 +374,7 @@ def test_mul(self): a1 = torch.randn((1, 2, 3, 2), device=device) a2 = torch.randn((2), device=device) res1 = torch.mul(a1, a2) - self.assertTrue(ipex._C.is_dil_tensor(res1)) + self.assertTrue(ipex.core.is_dil_tensor(res1)) def test_div(self): a1 = torch.tensor([4.2, 6.2], device=device) @@ -467,8 +467,8 @@ def test_view(self): self.assertRaises(RuntimeError, lambda: tensor.view(15, -1, -1)) # TODO(Eikan): DNNL OP does not support >6 dim tensor, so we disable it temporily. When we fix it, we will open it - old_dnnl_conf = ipex._C.get_auto_dnnl() - ipex._C.disable_auto_dnnl() + old_dnnl_conf = ipex.core.get_auto_dnnl() + ipex.core.disable_auto_dnnl() # test view when tensor is not contiguous in every dimension, but only # contiguous dimensions are touched. tensor = torch.rand(4, 2, 5, 1, 6, 2, 9, 3, device=device).transpose(-1, 2).transpose(-2, 3) @@ -495,9 +495,9 @@ def test_view(self): view_size = [1, 1, 2, 1, 4, 3, 1, 1, 9, 1, 2, 1, 2, 3, 1, 5, 1, 1] self.assertEqual(tensor.view(*view_size), contig_tensor.view(*view_size)) if old_dnnl_conf: - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() else: - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() # invalid views self.assertRaises(RuntimeError, lambda: tensor.view(-1)) diff --git a/tests/cpu/test_sparse.py b/tests/cpu/test_sparse.py index 53f494d7c..6b89ebc23 100644 --- a/tests/cpu/test_sparse.py +++ b/tests/cpu/test_sparse.py @@ -2,7 +2,7 @@ import copy import torch -import torch_ipex as ipex +import intel_pytorch_extension as ipex import torch.nn as nn from common_utils import TestCase from numbers import Number diff --git a/tests/cpu/test_torch.py b/tests/cpu/test_torch.py index 0166d5f3c..206b0e962 100644 --- a/tests/cpu/test_torch.py +++ b/tests/cpu/test_torch.py @@ -83,7 +83,7 @@ skipIf, skipCPUIfNoLapack, skipCUDAIfNoMagma, skipCUDAIfRocm, onlyCUDA, onlyCPU, \ dtypes, dtypesIfCUDA, deviceCountAtLeast, skipCUDAIf, precisionOverride, ipex import torch.backends.quantized -import torch_ipex as ipex +import intel_pytorch_extension as ipex # load_tests from common_utils is used to automatically filter tests for @@ -164,7 +164,7 @@ def __exit__(self, *args): pass -# This is intentionally prefixed by an unders_C. Otherwise pytest will try to +# This is intentionally prefixed by an underscore. Otherwise pytest will try to # run its methods as test cases. class _TestTorchMixin(object): def _make_tensors(self, shape, val_range=(-100, 100), use_floating=True, use_integral=True): diff --git a/tests/cpu/utils/test_lazy_reorder_with_pattern.py b/tests/cpu/utils/test_lazy_reorder_with_pattern.py index 3fe34d22f..fcbeafc6a 100644 --- a/tests/cpu/utils/test_lazy_reorder_with_pattern.py +++ b/tests/cpu/utils/test_lazy_reorder_with_pattern.py @@ -22,14 +22,14 @@ def test_conv_add_relu_000(self): ### 2 reorder rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() conv_op_input = torch.rand((1, 1, 10, 10)).to(device="cpu") conv_op = torch.nn.Conv2d(1, 1, (7, 7)).to(device="cpu") conv_op_output = conv_op(conv_op_input) add_src = torch.rand((1, 1, 4, 4)).to(device="cpu") conv_op_output += add_src conv_op_output.relu_() - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_add_relu_111(self): ### 1 reorder rand_seed = int(get_rand_seed()) @@ -42,20 +42,20 @@ def test_conv_add_relu_111(self): ### 1 reorder conv_op_output_ref += add_src_ref conv_op_output_ref.relu_() - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() conv_op_input = conv_op_input_ref.to(device=ipex_device) conv_op = conv_op_ref.to(device=ipex_device) conv_op_output = conv_op(conv_op_input) add_src = add_src_ref.to(device=ipex_device) conv_op_output += add_src conv_op_output.relu_() - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() self.assertEqual(conv_op_output_ref.size(), conv_op_output.size()) self.assertEqual(conv_op_output_ref, conv_op_output) def test_conv_add_bn_110(self): ##2 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -68,10 +68,10 @@ def test_conv_add_bn_110(self): ##2 reorder conv_op_output += add_src bn_op=torch.nn.BatchNorm2d(1).to(device="cpu") bn_op_output=bn_op(conv_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_bn_add_101(self): ##2 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -82,10 +82,10 @@ def test_conv_bn_add_101(self): ##2 reorder bn_op_output=bn_op(conv_op_output) add_src = torch.rand((1, 1, 4, 4)).to(device=ipex_device) bn_op_output += add_src - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_bn_conv_add_011(self): ##1 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -98,10 +98,10 @@ def test_bn_conv_add_011(self): ##1 reorder add_src = torch.rand((1, 1, 4, 4)).to(device=ipex_device) conv_op_output += add_src - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_bn_pool_100(self): ##2reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -112,12 +112,12 @@ def test_conv_bn_pool_100(self): ##2reorder bn_op_output=bn_op(conv_op_output) pool_op=torch.nn.MaxPool2d(kernel_size=3,stride=2,padding=1).to(device="cpu") pool_op_output=pool_op(bn_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() pool_op_output=pool_op(bn_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_bn_conv_pool_010(self): ##1 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -128,10 +128,10 @@ def test_bn_conv_pool_010(self): ##1 reorder conv_op_output = conv_op(bn_op_output) pool_op=torch.nn.MaxPool2d(kernel_size=3,stride=2,padding=1).to(device="cpu") pool_op_output=pool_op(conv_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_bn_pool_conv_001(self): ##1 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -142,10 +142,10 @@ def test_bn_pool_conv_001(self): ##1 reorder pool_op_output=pool_op(bn_op_output) conv_op = torch.nn.Conv2d(1, 1, (3, 3)).to(device=ipex_device) conv_op_output = conv_op(pool_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_conv_concate(self): ##2 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -155,10 +155,10 @@ def test_conv_conv_concate(self): ##2 reorder conv_op_output1 = conv_op1(conv_op_input) conv_op_output2 = conv_op2(conv_op_input) concate_out=torch.cat([conv_op_output1,conv_op_output2],dim=1).to(device=ipex_device) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_conv_add(self): ##3 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -168,10 +168,10 @@ def test_conv_conv_add(self): ##3 reorder conv_op_output = conv_op(bn_op_output) pool_op=torch.nn.MaxPool2d(kernel_size=3,stride=2,padding=1).to(device="cpu") pool_op_output=pool_op(conv_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_bn_pool_conv_001(self): ##1 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -182,10 +182,10 @@ def test_bn_pool_conv_001(self): ##1 reorder pool_op_output=pool_op(bn_op_output) conv_op = torch.nn.Conv2d(1, 1, (3, 3)).to(device=ipex_device) conv_op_output = conv_op(pool_op_output) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_conv_concate(self): ##2 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -195,10 +195,10 @@ def test_conv_conv_concate(self): ##2 reorder conv_op_output1 = conv_op1(conv_op_input) conv_op_output2 = conv_op2(conv_op_input) concate_out=torch.cat([conv_op_output1,conv_op_output2],dim=1).to(device=ipex_device) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() def test_conv_conv_add(self): ##3 reorder - ipex._C.enable_auto_dnnl() + ipex.core.enable_auto_dnnl() rand_seed = int(get_rand_seed()) print("******{} rand sed: {}".format(sys._getframe().f_code.co_name, rand_seed)) torch.manual_seed(rand_seed) @@ -208,4 +208,4 @@ def test_conv_conv_add(self): ##3 reorder conv_op_output1 = conv_op1(conv_op_input) conv_op_output2 = conv_op2(conv_op_input) add_out=torch.add(conv_op_output1,conv_op_output2).to(device=ipex_device) - ipex._C.disable_auto_dnnl() + ipex.core.disable_auto_dnnl() diff --git a/tests/cpu/utils/utils.py b/tests/cpu/utils/utils.py index 5038eb64b..7e754a353 100644 --- a/tests/cpu/utils/utils.py +++ b/tests/cpu/utils/utils.py @@ -2,8 +2,7 @@ import unittest from torch.testing._internal import expecttest from functools import wraps -# import intel_pytorch_extension as ipex -import torch_ipex as ipex +import intel_pytorch_extension as ipex class VerboseTestCase(expecttest.TestCase): def __init__(self, method_name='runTest'): diff --git a/torch_ipex/__init__.py b/torch_ipex/__init__.py index bbb61648a..3df067fb8 100644 --- a/torch_ipex/__init__.py +++ b/torch_ipex/__init__.py @@ -6,9 +6,8 @@ from .tensor import * from .optim import * from .ops import * -from . import _C -_C.enable_torch_ccl() +core.enable_torch_ccl() DEVICE = 'xpu:0' class AmpConf(object): @@ -17,20 +16,20 @@ def __init__(self, mixed_dtype = torch.bfloat16, configure_file = None): self.configure_file = configure_file if self.dtype != torch.bfloat16: - _C.clear_indicators() + core.clear_indicators() # for int8 path, if user give a exited configure file, load it. if self.configure_file != None and self.dtype != torch.bfloat16: if os.path.exists(self.configure_file) and os.stat(self.configure_file).st_size != 0: with open(self.configure_file, 'r') as f: configures = json.load(f) - _C.load_indicators_file(configures) + core.load_indicators_file(configures) else: assert False, 'Can not load a empty file or none existed file, plese first do calibartion step' # for int8 quantization, will save the date after doing calibration step. def save(self, configure_file): - _C.add_indicators() - configures = _C.get_int8_configures() + core.add_indicators() + configures = core.get_int8_configures() with open(configure_file, 'w') as fp: json.dump(configures, fp, indent = 4) @@ -62,16 +61,16 @@ def generator_context(*args, **kwargs): return generator_context def get_auto_mix_precision(): - if _C.get_mix_bf16_fp32(): + if core.get_mix_bf16_fp32(): return torch.bfloat16 - elif _C.get_mix_int8_fp32(): + elif core.get_mix_int8_fp32(): return torch.int8 else: return None def _enable_auto_optimization(mixed_dtype = None, train = False): if mixed_dtype != None: - _C.enable_auto_dnnl() + core.enable_auto_dnnl() enable_auto_mixed_precision(mixed_dtype, train) def enable_auto_mixed_precision(mixed_dtype = torch.bfloat16, train = False): @@ -93,50 +92,50 @@ def _get_auto_optimization(): return get_auto_mix_precision def get_train(): - return _C.get_train() + return core.get_train() class AutoMixPrecision(_DecoratorContextManager): def __init__(self, conf, running_mode = 'inference'): self.pre_mixed_dtype = get_auto_mix_precision() self.pre_running_mode = get_train() - self.pre_calibration_state = _C.get_int8_calibration() + self.pre_calibration_state = core.get_int8_calibration() self.mixed_dtype = conf.dtype self.running_mode = running_mode def __enter__(self): if self.mixed_dtype == torch.bfloat16: - _C.enable_mix_bf16_fp32() - _C.disable_mix_int8_fp32() + core.enable_mix_bf16_fp32() + core.disable_mix_int8_fp32() elif self.mixed_dtype == torch.int8: - _C.enable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() + core.enable_mix_int8_fp32() + core.disable_mix_bf16_fp32() if self.running_mode == 'inference': - _C.disable_int8_calibration() + core.disable_int8_calibration() elif self.running_mode == 'calibration': - _C.enable_int8_calibration() + core.enable_int8_calibration() else: assert False, 'int8 quantization only suport inference and calibration running mode' else: - _C.disable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() - _C.set_execution_mode(train = True if self.running_mode == 'training' else False) + core.disable_mix_int8_fp32() + core.disable_mix_bf16_fp32() + core.set_execution_mode(train = True if self.running_mode == 'training' else False) def __exit__(self, *args): if self.mixed_dtype == torch.int8: if self.running_mode == 'calibration': - _C.calibration_reset() + core.calibration_reset() # restore previous state if self.pre_calibration_state: - _C.enable_int8_calibration() + core.enable_int8_calibration() else: - _C.disable_int8_calibration() + core.disable_int8_calibration() if self.pre_mixed_dtype == torch.bfloat16: - _C.enable_mix_bf16_fp32() - _C.disable_mix_int8_fp32() + core.enable_mix_bf16_fp32() + core.disable_mix_int8_fp32() elif self.pre_mixed_dtype == torch.int8: - _C.enable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() + core.enable_mix_int8_fp32() + core.disable_mix_bf16_fp32() else: - _C.disable_mix_int8_fp32() - _C.disable_mix_bf16_fp32() - _C.set_execution_mode(train = self.pre_running_mode) \ No newline at end of file + core.disable_mix_int8_fp32() + core.disable_mix_bf16_fp32() + core.set_execution_mode(train = self.pre_running_mode) \ No newline at end of file From 562a8047a17551d70ed4cbab95e7e98bcab500cb Mon Sep 17 00:00:00 2001 From: tangleintel Date: Thu, 10 Jun 2021 00:14:03 +0800 Subject: [PATCH 05/35] fix test_int8.py's regression --- torch_ipex/csrc/py_init.cpp | 17 ++++++++++++++++- torch_ipex/version.py | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/torch_ipex/csrc/py_init.cpp b/torch_ipex/csrc/py_init.cpp index 8e3d6e962..32b8eeec5 100644 --- a/torch_ipex/csrc/py_init.cpp +++ b/torch_ipex/csrc/py_init.cpp @@ -170,6 +170,10 @@ void InitIpexModuleBindings(py::module m) { std::tie(i_scale, o_scale) = indicator.get_indicator_scales(); d["inputs_scale"] = i_scale; d["outputs_scale"] = o_scale; + std::vector i_zero_point, o_zero_point; + std::tie(i_zero_point, o_zero_point) = indicator.get_indicator_zero_point(); + d["inputs_zero_point"] = i_zero_point; + d["outputs_zero_point"] = o_zero_point; std::vector i_uint8_used, o_uint8_used; std::tie(i_uint8_used, o_uint8_used)= indicator.get_indicator_uint8_status(); d["inputs_uint8_used"] = i_uint8_used; @@ -193,13 +197,24 @@ void InitIpexModuleBindings(py::module m) { py::cast>(i["inputs_scale"]); std::vector o_scale = py::cast>(i["outputs_scale"]); + + // TODO: what should be the default value here? different for u8 and s8 + std::vector i_zero_point = {0}; + std::vector o_zero_point = {0}; + if (i.contains("inputs_zero_point")) { + i_zero_point = py::cast>(i["inputs_zero_point"]); + } + if (i.contains("outputs_zero_point")) { + o_zero_point = py::cast>(i["outputs_zero_point"]); + } + std::vector i_uint8_used = py::cast>(i["inputs_uint8_used"]); std::vector o_uint8_used = py::cast>(i["outputs_uint8_used"]); bool quantized = py::cast(i["quantized"]); Indicator temp(id, op_name, algorithm, weight_granularity, i_scale, - o_scale, i_uint8_used, o_uint8_used, quantized); + o_scale, i_uint8_used, o_uint8_used, quantized, i_zero_point, o_zero_point); indicators.push_back(temp); } Int8OptConfig::get_config().set_indicators(indicators); diff --git a/torch_ipex/version.py b/torch_ipex/version.py index dcfc2b3e9..73477d18b 100644 --- a/torch_ipex/version.py +++ b/torch_ipex/version.py @@ -1,4 +1,4 @@ # Autogenerated file, do not edit! -__version__ = '1.2.0' -__ipex_gitrev__ = '50b306ac855a76e35aacf9ab1571ac41b7243ae8' +__version__ = '1.8.0' +__ipex_gitrev__ = '8aba98f3f79e021b372a52d0eebfaa6373e6662c' __torch_gitrev__ = '' From ca185f3d8d47c1310f8c78a8de2709bca5b55d26 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Mon, 21 Jun 2021 17:24:20 +0900 Subject: [PATCH 06/35] setup.py: 1. fix include_paths and library_paths missing issue if torch is installed via setup.py. 2. sovled libstdc++ dual abi issue. 3. removed duplicated package importings. torch-ccl: 1. fixed oneCCL library path patching not taking effect issue --- setup.py | 12 ++++-------- third_party/torch_ccl | 2 +- torch_ipex/version.py | 4 ---- 3 files changed, 5 insertions(+), 13 deletions(-) delete mode 100644 torch_ipex/version.py diff --git a/setup.py b/setup.py index 4d1b2294b..27f336b31 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from __future__ import print_function TORCH_VERSION = '1.8.0' -TORCH_IPEX_VERSION = '1.8.0' +TORCH_IPEX_VERSION = '1.8.0.1' # import torch import platform @@ -64,6 +64,7 @@ except ImportError as e: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch=='+TORCH_VERSION+'+cpu', '-f', 'https://download.pytorch.org/whl/torch_stable.html']) import torch + from torch.utils.cpp_extension import include_paths, library_paths PYTHON_VERSION = sys.version_info IS_WINDOWS = (platform.system() == 'Windows') @@ -107,12 +108,7 @@ import inspect import multiprocessing import multiprocessing.pool -import os -import platform -import re import shutil -import subprocess -import sys import pathlib @@ -250,7 +246,6 @@ class IPEXClean(distutils.command.clean.clean, object): def run(self): import glob - import re with open('.gitignore', 'r') as f: ignores = f.read() pat = re.compile(r'^#( BEGIN NOT-CLEAN-FILES )?') @@ -293,7 +288,7 @@ def run(self): ipex_exts = [ext for ext in self.extensions if isinstance(ext, IPEXExt)] for ext in ipex_exts: self.build_ipex_extension(ext) - + self.extensions = [ext for ext in self.extensions if not isinstance(ext, IPEXExt)] super(IPEXBuild, self).run() @@ -321,6 +316,7 @@ def build_ipex_extension(self, ext): '-DCMAKE_INSTALL_PREFIX=' + ext_dir, '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + ext_dir, '-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + ext_dir, + '-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI)), '-DPYTHON_INCLUDE_DIR=' + python_include_dir, '-DPYTORCH_INCLUDE_DIRS=' + pytorch_install_dir + "/include", '-DPYTORCH_LIBRARY_DIRS=' + pytorch_install_dir + "/lib", diff --git a/third_party/torch_ccl b/third_party/torch_ccl index 314168610..064d9eb3a 160000 --- a/third_party/torch_ccl +++ b/third_party/torch_ccl @@ -1 +1 @@ -Subproject commit 31416861014996e9d6f89eabcd83c8254fdfd7c1 +Subproject commit 064d9eb3aeeb10ed37a349e6175161bb3da36104 diff --git a/torch_ipex/version.py b/torch_ipex/version.py deleted file mode 100644 index 73477d18b..000000000 --- a/torch_ipex/version.py +++ /dev/null @@ -1,4 +0,0 @@ -# Autogenerated file, do not edit! -__version__ = '1.8.0' -__ipex_gitrev__ = '8aba98f3f79e021b372a52d0eebfaa6373e6662c' -__torch_gitrev__ = '' From 7d64b1bd07006dd06b43ed39d1cce99f3383f365 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Fri, 25 Jun 2021 00:22:33 +0900 Subject: [PATCH 07/35] clean ipex installation folder structure --- CMakeLists.txt | 2 +- cmake/CPU.cmake | 8 ++++- setup.py | 78 ++++++++++++++++++++++++++++--------------------- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 401aae9ae..440b61f9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(PLUGIN_NAME torch_ipex) set(RPATH_VALUE $ORIGIN) set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) -set(CMAKE_INSTALL_RPATH "${RPATH_VALUE}/lib/") +set(CMAKE_INSTALL_RPATH "${RPATH_VALUE}") set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) set(DPCPP_ROOT "${PROJECT_SOURCE_DIR}/torch_ipex/csrc") diff --git a/cmake/CPU.cmake b/cmake/CPU.cmake index 2647fc257..c6e9138ff 100644 --- a/cmake/CPU.cmake +++ b/cmake/CPU.cmake @@ -11,7 +11,7 @@ SET(DNNL_ENABLE_PRIMITIVE_CACHE TRUE CACHE BOOL "" FORCE) SET(DNNL_LIBRARY_TYPE STATIC CACHE STRING "" FORCE) set(DPCPP_CPU_ROOT "${PROJECT_SOURCE_DIR}/torch_ipex/csrc/cpu") -add_subdirectory(${DPCPP_THIRD_PARTY_ROOT}/mkl-dnn) +add_subdirectory(${DPCPP_THIRD_PARTY_ROOT}/mkl-dnn EXCLUDE_FROM_ALL) find_package(TorchCCL REQUIRED) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) @@ -141,6 +141,7 @@ endif() include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/torch_ipex) include_directories(${PROJECT_SOURCE_DIR}/torch_ipex/csrc/) +include_directories(${PYTHON_INCLUDE_DIR}) include_directories(${DPCPP_THIRD_PARTY_ROOT}/pybind11/include) include_directories(${DPCPP_THIRD_PARTY_ROOT}/xsmm/include) include_directories(${TORCHCCL_INCLUDE_DIR}) @@ -201,3 +202,8 @@ target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libtorch_ target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libc10.so) target_compile_options(${PLUGIN_NAME} PRIVATE "-DC10_BUILD_MAIN_LIB") + +#set_property(TARGET ${PLUGIN_NAME} PROPERTY VERSION "${IPEX_VERSION}") +#set_property(TARGET ${PLUGIN_NAME} PROPERTY SOVERSION "${IPEX_VERSION}") +install(TARGETS ${PLUGIN_NAME} + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_WRITE GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) diff --git a/setup.py b/setup.py index 27f336b31..61314c68e 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from __future__ import print_function TORCH_VERSION = '1.8.0' -TORCH_IPEX_VERSION = '1.8.0.1' +TORCH_IPEX_VERSION = '1.8.0' # import torch import platform @@ -64,7 +64,6 @@ except ImportError as e: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch=='+TORCH_VERSION+'+cpu', '-f', 'https://download.pytorch.org/whl/torch_stable.html']) import torch - from torch.utils.cpp_extension import include_paths, library_paths PYTHON_VERSION = sys.version_info IS_WINDOWS = (platform.system() == 'Windows') @@ -108,7 +107,12 @@ import inspect import multiprocessing import multiprocessing.pool +import os +import platform +import re import shutil +import subprocess +import sys import pathlib @@ -239,13 +243,14 @@ class IPEXExt(Extension, object): def __init__(self, name, project_dir=os.path.dirname(__file__)): Extension.__init__(self, name, sources=[]) self.project_dir = os.path.abspath(project_dir) - self.build_dir = os.path.join(project_dir, 'build') + #self.build_dir = os.path.join(project_dir, 'build_' + self.name) class IPEXClean(distutils.command.clean.clean, object): def run(self): import glob + import re with open('.gitignore', 'r') as f: ignores = f.read() pat = re.compile(r'^#( BEGIN NOT-CLEAN-FILES )?') @@ -296,8 +301,9 @@ def build_ipex_extension(self, ext): if not isinstance(ext, IPEXExt): return super(IPEXBuild, self).build_extension(ext) ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) - if not os.path.exists(ext.build_dir): - os.mkdir(ext.build_dir) + build_dir = os.path.join(ext_dir, '..', 'build_' + ext.name) + if not os.path.exists(build_dir): + os.mkdir(build_dir) build_type = 'Release' use_ninja = False @@ -306,20 +312,23 @@ def build_ipex_extension(self, ext): build_type = 'Debug' # install _torch_ipex.so as python module - if ext.name == 'torch_ipex' and _check_env_flag("USE_SYCL"): - ext_dir = ext_dir + '/torch_ipex' + if ext.name == 'torch_ipex': + ext_dir = os.path.join(ext_dir, ext.name) + if not os.path.exists(ext_dir): + os.mkdir(ext_dir) cmake_args = [ '-DCMAKE_BUILD_TYPE=' + build_type, - '-DPYTORCH_INSTALL_DIR=' + pytorch_install_dir, - '-DPYTHON_EXECUTABLE=' + sys.executable, '-DCMAKE_INSTALL_PREFIX=' + ext_dir, - '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + ext_dir, - '-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + ext_dir, '-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI)), '-DPYTHON_INCLUDE_DIR=' + python_include_dir, + '-DPYTHON_EXECUTABLE=' + sys.executable, + '-DPYTORCH_INSTALL_DIR=' + pytorch_install_dir, + '-DIPEX_VERSION=' + TORCH_IPEX_VERSION, '-DPYTORCH_INCLUDE_DIRS=' + pytorch_install_dir + "/include", '-DPYTORCH_LIBRARY_DIRS=' + pytorch_install_dir + "/lib", + #'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + ext_dir, + #'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + ext_dir, ] if _check_env_flag("IPEX_DISP_OP"): @@ -343,16 +352,18 @@ def build_ipex_extension(self, ext): env = os.environ.copy() if _check_env_flag("USE_SYCL"): os.environ['CXX'] = 'compute++' - check_call([self.cmake, ext.project_dir] + cmake_args, cwd=ext.build_dir, env=env) + check_call([self.cmake, ext.project_dir] + cmake_args, cwd=build_dir, env=env) else: - check_call([self.cmake, ext.project_dir] + cmake_args, cwd=ext.build_dir, env=env) + check_call([self.cmake, ext.project_dir] + cmake_args, cwd=build_dir, env=env) # build_args += ['VERBOSE=1'] if use_ninja: - check_call(['ninja'] + build_args, cwd=ext.build_dir, env=env) + print('use_ninja') + check_call(['ninja'] + build_args, cwd=build_dir, env=env) else: - check_call(['make'] + build_args, cwd=ext.build_dir, env=env) - check_call(['make', 'install'] + build_args, cwd=ext.build_dir, env=env) + print('make') + check_call(['make'] + build_args, cwd=build_dir, env=env) + check_call(['make', 'install'] + build_args, cwd=build_dir, env=env) ipex_git_sha, torch_git_sha = get_git_head_sha(base_dir) version = get_build_version(ipex_git_sha) @@ -382,9 +393,11 @@ def get_c_module(): main_sources = ["torch_ipex/csrc/_C.cpp"] cwd = os.path.dirname(os.path.abspath(__file__)) # lib_path = os.path.join(cwd, "torch_ipex", "lib") - lib_path = os.path.join(cwd, "build") - lib_path_1 = os.path.join(cwd, "build", "lib.linux-x86_64-3.8") - library_dirs = [lib_path, lib_path_1] + #lib_path = os.path.join(cwd, "build") + lib_path = os.path.join(cwd, "build", "build_torch_ipex") + library_dirs = [lib_path] + #lib_path_1 = os.path.join(cwd, "build", "lib.linux-x86_64-3.8") + #library_dirs = [lib_path, lib_path_1] extra_link_args = [] extra_compile_args = [ '-Wall', @@ -406,9 +419,6 @@ def get_c_module(): '-Wno-missing-braces', ] - def make_relative_rpath(path): - return '-Wl,-rpath,$ORIGIN/' + path - C_ext = Extension("torch_ipex._C", libraries=main_libraries, sources=main_sources, @@ -416,8 +426,8 @@ def make_relative_rpath(path): extra_compile_args=main_compile_args + extra_compile_args, include_dirs=include_paths(), library_dirs=library_dirs, - # extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) - extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('..')]) + extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) + # extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('..')]) return C_ext setup( @@ -437,16 +447,16 @@ def make_relative_rpath(path): 'intel_pytorch_extension.ops', 'intel_pytorch_extension.optim'], package_dir={'intel_pytorch_extension': 'torch_ipex'}, - package_data={ - 'torch_ipex':[ - 'README.md', - 'requirements.txt', - '*.py', - 'lib/*.so', - 'include/*.h', - 'include/core/*.h', - 'include/utils/*.h'] - }, + #package_data={ + # 'torch_ipex':[ + # 'README.md', + # 'requirements.txt', + # '*.py', + # 'lib/*.so', + # 'include/*.h', + # 'include/core/*.h', + # 'include/utils/*.h'] + # }, zip_safe=False, ext_modules=[IPEXExt('torch_ipex'), get_c_module()], cmdclass={ From 1598f205a1db236a4f813bc6e88cba5e44c41c06 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Fri, 25 Jun 2021 05:44:08 +0900 Subject: [PATCH 08/35] clean ipex installation folder structure --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 61314c68e..46e274146 100644 --- a/setup.py +++ b/setup.py @@ -324,9 +324,9 @@ def build_ipex_extension(self, ext): '-DPYTHON_INCLUDE_DIR=' + python_include_dir, '-DPYTHON_EXECUTABLE=' + sys.executable, '-DPYTORCH_INSTALL_DIR=' + pytorch_install_dir, - '-DIPEX_VERSION=' + TORCH_IPEX_VERSION, '-DPYTORCH_INCLUDE_DIRS=' + pytorch_install_dir + "/include", '-DPYTORCH_LIBRARY_DIRS=' + pytorch_install_dir + "/lib", + '-DIPEX_VERSION=' + TORCH_IPEX_VERSION, #'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + ext_dir, #'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + ext_dir, ] From c32ff52e0037ebe0a672ec13fc7d0b8df9eecd80 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Fri, 25 Jun 2021 05:57:07 +0900 Subject: [PATCH 09/35] clean ipex installation folder structure --- setup.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 46e274146..c9ef1381d 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from __future__ import print_function TORCH_VERSION = '1.8.0' -TORCH_IPEX_VERSION = '1.8.0' +TORCH_IPEX_VERSION = '1.8.0.1' # import torch import platform @@ -64,6 +64,7 @@ except ImportError as e: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch=='+TORCH_VERSION+'+cpu', '-f', 'https://download.pytorch.org/whl/torch_stable.html']) import torch + from torch.utils.cpp_extension import include_paths, library_paths PYTHON_VERSION = sys.version_info IS_WINDOWS = (platform.system() == 'Windows') @@ -107,12 +108,7 @@ import inspect import multiprocessing import multiprocessing.pool -import os -import platform -import re import shutil -import subprocess -import sys import pathlib @@ -250,7 +246,6 @@ class IPEXClean(distutils.command.clean.clean, object): def run(self): import glob - import re with open('.gitignore', 'r') as f: ignores = f.read() pat = re.compile(r'^#( BEGIN NOT-CLEAN-FILES )?') From 7c5290572a322125dbb8ba2e90d0d9dcffce9516 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Fri, 25 Jun 2021 15:59:38 +0900 Subject: [PATCH 10/35] Add a warning message of deprecation of intel_pytorch_extension --- torch_ipex/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/torch_ipex/__init__.py b/torch_ipex/__init__.py index 3df067fb8..c80463529 100644 --- a/torch_ipex/__init__.py +++ b/torch_ipex/__init__.py @@ -7,6 +7,10 @@ from .optim import * from .ops import * +base_dir = os.path.basename(os.path.dirname(os.path.abspath(__file__))) +if base_dir == 'intel_pytorch_extension': + print('[WARNING] "import intel_pytorch_extension" will be deprecated in future releases. Please use "import torch_ipex" instead.') + core.enable_torch_ccl() DEVICE = 'xpu:0' @@ -138,4 +142,4 @@ def __exit__(self, *args): else: core.disable_mix_int8_fp32() core.disable_mix_bf16_fp32() - core.set_execution_mode(train = self.pre_running_mode) \ No newline at end of file + core.set_execution_mode(train = self.pre_running_mode) From 2b32dd3d49fadc2a90371b2aed04cea8a2f04d31 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Sat, 26 Jun 2021 01:21:25 +0900 Subject: [PATCH 11/35] fix rpath issue to libtorch_ccl.so after hierarchy adjustment --- CMakeLists.txt | 2 +- cmake/Modules/FindTorchCCL.cmake | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 440b61f9b..e628af1e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(PLUGIN_NAME torch_ipex) set(RPATH_VALUE $ORIGIN) set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) -set(CMAKE_INSTALL_RPATH "${RPATH_VALUE}") +set(CMAKE_INSTALL_RPATH "${RPATH_VALUE}/../../torch_ccl/lib") set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) set(DPCPP_ROOT "${PROJECT_SOURCE_DIR}/torch_ipex/csrc") diff --git a/cmake/Modules/FindTorchCCL.cmake b/cmake/Modules/FindTorchCCL.cmake index 64435eb82..dc1259707 100644 --- a/cmake/Modules/FindTorchCCL.cmake +++ b/cmake/Modules/FindTorchCCL.cmake @@ -17,7 +17,10 @@ SET(TORCHCCL_INCLUDE_DIR) SET(TORCHCCL_ROOT "${PROJECT_SOURCE_DIR}/third_party/torch_ccl") +SET(CMAKE_INSTALL_PREFIX_SAVED "${CMAKE_INSTALL_PREFIX}") +SET(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX_SAVED}/../torch_ccl") ADD_SUBDIRECTORY(${TORCHCCL_ROOT}) +SET(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX_SAVED}") IF(NOT TARGET torch_ccl) MESSAGE(FATAL_ERROR "Failed to include torch_ccl target") ENDIF() From 1570cf633c1956d5dd5dbbb6b1cb342c2a2eed68 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Tue, 29 Jun 2021 04:25:25 +0900 Subject: [PATCH 12/35] 1. removed execute bit of libtorch_ipex.so permission 2. upgraded torch-ccl to make libtorch_ccl.so installed to torch_ccl folder --- cmake/CPU.cmake | 3 +-- third_party/torch_ccl | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmake/CPU.cmake b/cmake/CPU.cmake index c6e9138ff..4c81e0916 100644 --- a/cmake/CPU.cmake +++ b/cmake/CPU.cmake @@ -205,5 +205,4 @@ target_compile_options(${PLUGIN_NAME} PRIVATE "-DC10_BUILD_MAIN_LIB") #set_property(TARGET ${PLUGIN_NAME} PROPERTY VERSION "${IPEX_VERSION}") #set_property(TARGET ${PLUGIN_NAME} PROPERTY SOVERSION "${IPEX_VERSION}") -install(TARGETS ${PLUGIN_NAME} - PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_WRITE GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) +install(TARGETS ${PLUGIN_NAME}) diff --git a/third_party/torch_ccl b/third_party/torch_ccl index 064d9eb3a..431c45f27 160000 --- a/third_party/torch_ccl +++ b/third_party/torch_ccl @@ -1 +1 @@ -Subproject commit 064d9eb3aeeb10ed37a349e6175161bb3da36104 +Subproject commit 431c45f2760f557ded88d0e31952e8523164ae8b From dd4c1c492aa16f7e79f1d0370394d1fa411bf034 Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Tue, 29 Jun 2021 22:56:23 +0800 Subject: [PATCH 13/35] Pass build for pytorch 1.9.0 --- cmake/CPU.cmake | 2 +- tests/cpu/common_utils.py | 44 +++++++++---------- torch_ipex/csrc/cpu/CustomOPs.h | 4 +- torch_ipex/csrc/cpu/DevOPs.cpp | 4 +- torch_ipex/csrc/cpu/DevOPs.h | 4 +- .../csrc/cpu/aten/operators/embedding_bag.cpp | 6 +-- torch_ipex/csrc/ipex_tensor_impl.cpp | 9 +++- torch_ipex/csrc/utils.h | 11 +++-- torch_ipex/ops/__init__.py | 2 +- torch_ipex/ops/embeddingbag.py | 28 +++++++++--- 10 files changed, 66 insertions(+), 48 deletions(-) diff --git a/cmake/CPU.cmake b/cmake/CPU.cmake index 4c81e0916..c00e4682c 100644 --- a/cmake/CPU.cmake +++ b/cmake/CPU.cmake @@ -205,4 +205,4 @@ target_compile_options(${PLUGIN_NAME} PRIVATE "-DC10_BUILD_MAIN_LIB") #set_property(TARGET ${PLUGIN_NAME} PROPERTY VERSION "${IPEX_VERSION}") #set_property(TARGET ${PLUGIN_NAME} PROPERTY SOVERSION "${IPEX_VERSION}") -install(TARGETS ${PLUGIN_NAME}) +install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib) diff --git a/tests/cpu/common_utils.py b/tests/cpu/common_utils.py index fbd42eb37..39a6a2333 100644 --- a/tests/cpu/common_utils.py +++ b/tests/cpu/common_utils.py @@ -1,6 +1,5 @@ ''' From PyTorch: - Copyright (c) 2016- Facebook, Inc (Adam Paszke) Copyright (c) 2014- Facebook, Inc (Soumith Chintala) Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) @@ -10,37 +9,28 @@ Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston) Copyright (c) 2006 Idiap Research Institute (Samy Bengio) Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz) - From Caffe2: - Copyright (c) 2016-present, Facebook Inc. All rights reserved. - All contributions by Facebook: Copyright (c) 2016 Facebook Inc. - All contributions by Google: Copyright (c) 2015 Google Inc. All rights reserved. - All contributions by Yangqing Jia: Copyright (c) 2015 Yangqing Jia All rights reserved. - All contributions from Caffe: Copyright(c) 2013, 2014, 2015, the respective contributors All rights reserved. - All other contributions: Copyright(c) 2015, 2016 the respective contributors All rights reserved. - Caffe2 uses a copyright model similar to Caffe: each contributor holds copyright over their contributions to Caffe2. The project versioning records all such contribution and copyright details. If a contributor wants to further mark their specific copyright on a particular contribution, they should indicate their copyright solely in the commit message of the change when it is committed. - All rights reserved. ''' @@ -48,7 +38,6 @@ r"""Importing this file must **not** initialize CUDA context. test_distributed relies on this assumption to properly run. This means that when this is imported no CUDA calls shall be made, including torch.cuda.device_count(), etc. - torch.testing._internal.common_cuda.py can freely initialize CUDA context when imported. """ @@ -88,9 +77,8 @@ from typing import cast, Any, Dict, Iterable, Iterator, Optional from torch.testing._internal import expecttest -from torch.testing import \ - (_compare_tensors_internal, _compare_scalars_internal, _compare_return_type, - floating_types_and, integral_types, complex_types) +from torch.testing._core import \ + (_compare_tensors_internal, _compare_scalars_internal, _compare_return_type) import torch import torch.cuda @@ -574,11 +562,9 @@ def wrapper(*args, **kwargs): def skipIfNotRegistered(op_name, message): """Wraps the decorator to hide the import of the `core`. - Args: op_name: Check if this op is registered in `core._REGISTERED_OPERATORS`. message: message to fail with. - Usage: @skipIfNotRegistered('MyOp', 'MyOp is not linked!') This will check if 'MyOp' is in the caffe2.python.core @@ -1315,7 +1301,6 @@ def assertNotWarn(self, callable, msg=''): @contextmanager def maybeWarnsRegex(self, category, regex=''): """Context manager for code that *may* warn, e.g. ``TORCH_WARN_ONCE``. - This filters expected warnings from the test log and fails the test if any unexpected warnings are caught. """ @@ -1341,7 +1326,6 @@ def assertExpected(self, s, subname=None): is placed in the 'expect' directory in the same directory as the test script. You can automatically update the recorded test output using --accept. - If you call this multiple times in a single function, you must give a unique subname each time. """ @@ -1444,6 +1428,24 @@ def runWithPytorchAPIUsageStderr(code): return stderr.decode('ascii') + def get_src_dtype_from_auto_dtype_conversion_info(self, line): + return line.strip().split(",")[1].split("=")[1] + + def get_dst_dtype_from_auto_dtype_conversion_info(self, line): + return line.strip().split(",")[2].split("=")[1] + + def get_op_name_from_auto_dtype_conversion_info(self, line): + return line.strip().split(",")[4].split("=")[1] + + def get_src_dtype_from_auto_dtype_conversion_info(self, line): + return line.strip().split(",")[1].split("=")[1] + + def get_dst_dtype_from_auto_dtype_conversion_info(self, line): + return line.strip().split(",")[2].split("=")[1] + + def get_op_name_from_auto_dtype_conversion_info(self, line): + return line.strip().split(",")[4].split("=")[1] + def download_file(url, binary=True): from urllib.parse import urlsplit from urllib import request, error @@ -1683,7 +1685,6 @@ def random_fullrank_matrix_distinct_singular_value(matrix_size, *batch_dims, def random_matrix(rows, columns, *batch_dims, **kwargs): """Return rectangular matrix or batches of rectangular matrices. - Parameters: dtype - the data type device - the device kind @@ -1723,7 +1724,6 @@ def random_lowrank_matrix(rank, rows, columns, *batch_dims, **kwargs): def random_sparse_matrix(rows, columns, density=0.01, **kwargs): """Return rectangular random sparse matrix within given density. - The density of the result approaches to given density as the size of the matrix is increased and a relatively small value of density is specified but higher than min(rows, columns)/(rows * columns) @@ -1750,10 +1750,8 @@ def random_sparse_matrix(rows, columns, density=0.01, **kwargs): def random_sparse_pd_matrix(matrix_size, density=0.01, **kwargs): """Return random sparse positive-definite matrix with given density. - The eigenvalues of the matrix are defined as:: arange(1, matrix_size+1)/matrix_size - Algorithm: A = diag(arange(1, matrix_size+1)/matrix_size) while : @@ -1994,4 +1992,4 @@ def set_cwd(path: str) -> Iterator[None]: dtype2prec_DONTUSE = {torch.float: 1e-5, torch.double: 1e-5, torch.half: 1e-2, - torch.bfloat16: 1e-1} + torch.bfloat16: 1e-1} \ No newline at end of file diff --git a/torch_ipex/csrc/cpu/CustomOPs.h b/torch_ipex/csrc/cpu/CustomOPs.h index 92f35b05f..2b6f574c0 100644 --- a/torch_ipex/csrc/cpu/CustomOPs.h +++ b/torch_ipex/csrc/cpu/CustomOPs.h @@ -778,7 +778,7 @@ class NewEmbeddingBagOp : public torch::autograd::Function { _ipex_bag_size, num_weights, scale_grad_by_freq, mode, _ipex_per_sample_weights) : at::_embedding_bag_dense_backward( - _ipex_grad, _ipex_indices, _ipex_offsets, _ipex_offset2bag_, + _ipex_grad, _ipex_indices, _ipex_offset2bag_, _ipex_bag_size, _ipex_maximum_indices, num_weights, scale_grad_by_freq, mode, _ipex_per_sample_weights); auto &&_ipex_per_sample_weights_grad = @@ -806,7 +806,7 @@ class NewEmbeddingBagOp : public torch::autograd::Function { grad, indices, offsets, offset2bag_, bag_size, num_weights, scale_grad_by_freq, mode, per_sample_weights) : at::_embedding_bag_dense_backward( - grad, indices, offsets, offset2bag_, bag_size, + grad, indices, offset2bag_, bag_size, maximum_indices, num_weights, scale_grad_by_freq, mode, per_sample_weights); auto per_sample_weights_grad = diff --git a/torch_ipex/csrc/cpu/DevOPs.cpp b/torch_ipex/csrc/cpu/DevOPs.cpp index 1904d5d7c..9b21d25af 100644 --- a/torch_ipex/csrc/cpu/DevOPs.cpp +++ b/torch_ipex/csrc/cpu/DevOPs.cpp @@ -2924,7 +2924,7 @@ at::Tensor AtenIpexCPUDev::dil_div(const at::Tensor &self, return at::Tensor(); } -at::Tensor AtenIpexCPUDev::dil_div(const at::Tensor &self, at::Scalar &other) { +at::Tensor AtenIpexCPUDev::dil_div(const at::Tensor &self, const at::Scalar &other) { auto tensor = at::scalar_to_tensor(other); DEBUG("AtenIpexCPUDev::dil_div_Scalar\n"); auto impl = tensor.unsafeGetTensorImpl(); @@ -2940,7 +2940,7 @@ at::Tensor &AtenIpexCPUDev::dil_div_(at::Tensor &self, return AtenIpexCPUDev::dil_div_out(self, self, other); } -at::Tensor &AtenIpexCPUDev::dil_div_(at::Tensor &self, at::Scalar &other) { +at::Tensor &AtenIpexCPUDev::dil_div_(at::Tensor &self, const at::Scalar &other) { auto tensor = at::scalar_to_tensor(other); DEBUG("AtenIpexCPUDev::dil_div_Scalar\n"); auto impl = tensor.unsafeGetTensorImpl(); diff --git a/torch_ipex/csrc/cpu/DevOPs.h b/torch_ipex/csrc/cpu/DevOPs.h index 5e7bc9b79..f07ec5681 100644 --- a/torch_ipex/csrc/cpu/DevOPs.h +++ b/torch_ipex/csrc/cpu/DevOPs.h @@ -115,9 +115,9 @@ class AtenIpexCPUDev { static at::Tensor dil_upsample_trilinear3d_backward(const at::Tensor & grad_output, at::IntArrayRef output_size, at::IntArrayRef input_size, bool align_corners, c10::optional scales_d, c10::optional scales_h, c10::optional scales_w); static at::Tensor dil_unsqueeze(const at::Tensor& self, int64_t dim); static at::Tensor dil_div(const at::Tensor &self, const at::Tensor &other); - static at::Tensor dil_div(const at::Tensor &self, at::Scalar &other); + static at::Tensor dil_div(const at::Tensor &self, const at::Scalar &other); static at::Tensor &dil_div_(at::Tensor &self, const at::Tensor &other); - static at::Tensor &dil_div_(at::Tensor &self, at::Scalar &other); + static at::Tensor &dil_div_(at::Tensor &self, const at::Scalar &other); static at::Tensor &dil_div_out(at::Tensor &out, const at::Tensor &self, const at::Tensor &other); static at::Tensor dil_permute(const at::Tensor & self, at::IntArrayRef dims); diff --git a/torch_ipex/csrc/cpu/aten/operators/embedding_bag.cpp b/torch_ipex/csrc/cpu/aten/operators/embedding_bag.cpp index f46d32f4c..1061744ca 100755 --- a/torch_ipex/csrc/cpu/aten/operators/embedding_bag.cpp +++ b/torch_ipex/csrc/cpu/aten/operators/embedding_bag.cpp @@ -181,7 +181,7 @@ static inline at::Tensor embedding_bag_dense_backward_sum_fast(const at::Tensor auto offset_numel = offsets.numel(); at::Tensor offset2bag_ ; if (offset_numel != indices_numel) { - offset2bag_ = at::native::full({indices.sizes()[0] + 1}, 0, indices.options()); + offset2bag_ = at::empty({indices.sizes()[0] + 1}, indices.options()).zero_(); make_offset2bag(offsets, indices, offset2bag_); offset2bag_.resize_({indices.sizes()[0]}); } else { @@ -261,7 +261,7 @@ embedding_bag_get_offset2bag(const at::Tensor indices, const at::Tensor & offset int64_t indices_numel = indices.numel(); at::Tensor offset2bag_ ; if (indices_numel != 0 && offset2bag.numel() == 0) { - offset2bag_ = at::native::full({indices.sizes()[0] + 1}, 0, indices.options()); + offset2bag_ = at::empty({indices.sizes()[0] + 1}, indices.options()).zero_(); make_offset2bag(offsets, indices, offset2bag_); offset2bag_.resize_({indices.sizes()[0]}); } else { @@ -279,7 +279,7 @@ at::Tensor embedding_bag_backward_impl(const at::Tensor & grad, const at::Tensor return embedding_bag_sparse_backward_sum_fast(grad, indices, offsets, num_weights, mode); } else { return embedding_bag_sparse_backward_sum_fast(grad, indices, offsets, num_weights, mode); - } + } } else { auto grad_c = grad.contiguous(); if (is_bfloat16_tensor(grad)) { diff --git a/torch_ipex/csrc/ipex_tensor_impl.cpp b/torch_ipex/csrc/ipex_tensor_impl.cpp index a01b56a53..5f07cb984 100644 --- a/torch_ipex/csrc/ipex_tensor_impl.cpp +++ b/torch_ipex/csrc/ipex_tensor_impl.cpp @@ -68,7 +68,7 @@ void IPEXTensorImpl::copy_auto_grad(c10::TensorImpl *src_impl) { return; } - if (! this->requires_grad()){ + if (! this->requires_grad()) { auto cpu_autograd_meta = static_cast(src_impl->autograd_meta()); if (cpu_autograd_meta->is_view_){ auto cpu_view_meta = static_cast(src_impl->autograd_meta()); @@ -76,16 +76,20 @@ void IPEXTensorImpl::copy_auto_grad(c10::TensorImpl *src_impl) { c10::optional backward_info_; c10::optional forward_info_; - if (cpu_view_meta->has_fw_view()) { + if (cpu_view_meta->has_fw_view() && (!cpu_view_meta->shared_view_info())) { auto fw_view_info = cpu_view_meta->get_forward_view(); torch::autograd::ViewInfo fw_view_info_copy(fw_view_info.base_, fw_view_info.view_fn_); forward_info_ = fw_view_info_copy; + } else { + forward_info_ = c10::nullopt; } if (cpu_view_meta->has_bw_view()) { auto bw_view_info = cpu_view_meta->get_backward_view(); torch::autograd::ViewInfo bw_view_info_copy(bw_view_info.base_, bw_view_info.view_fn_); backward_info_ = bw_view_info_copy; + } else { + backward_info_ = c10::nullopt; } this->set_autograd_meta( @@ -93,6 +97,7 @@ void IPEXTensorImpl::copy_auto_grad(c10::TensorImpl *src_impl) { this, backward_info_, forward_info_, + cpu_view_meta->shared_view_info(), cpu_view_meta->get_creation_meta() ) ); diff --git a/torch_ipex/csrc/utils.h b/torch_ipex/csrc/utils.h index 649066aa5..870e44860 100644 --- a/torch_ipex/csrc/utils.h +++ b/torch_ipex/csrc/utils.h @@ -72,16 +72,15 @@ void set_ipex_func_status(IPEXFuncStatus ipex_fun_status); // A light-weight TORCH_CHECK that does not collect any backtrace info #if defined(_DEBUG) -#define IPEX_CHECK(cond, ...) \ + #define IPEX_CHECK(cond, ...) \ if (!(cond)) { \ throw std::runtime_error( \ - c10::detail::if_empty_then( \ - c10::str(__VA_ARGS__), \ - "Expected " #cond " to be true, but got false.")); \ + c10::detail::torchCheckMsgImpl( \ + "Expected " #cond " to be true, but got false.", ##__VA_ARGS__)); \ } #else -// quick path of IPEX_CHECK without reporting message -#define IPEX_CHECK(cond, ...) \ + // quick path of IPEX_CHECK without reporting message + #define IPEX_CHECK(cond, ...) \ if (!(cond)) { throw std::exception(); } #endif diff --git a/torch_ipex/ops/__init__.py b/torch_ipex/ops/__init__.py index 277184b8f..339356524 100644 --- a/torch_ipex/ops/__init__.py +++ b/torch_ipex/ops/__init__.py @@ -1,5 +1,5 @@ from .interaction import interaction -from .embeddingbag import embeddingbag +from .embeddingbag import ipex_embedding_bag from .linear import * from .pooling import * from .mlp import * diff --git a/torch_ipex/ops/embeddingbag.py b/torch_ipex/ops/embeddingbag.py index 823963d4a..bedfca8e5 100644 --- a/torch_ipex/ops/embeddingbag.py +++ b/torch_ipex/ops/embeddingbag.py @@ -1,14 +1,30 @@ import torch from torch import nn from torch.autograd import Function +import intel_pytorch_extension as ipex import torch_ipex._C as core +from typing import Callable, List, Optional, Tuple # # extension for BF16 fast path only +Tensor = torch.Tensor +torch_embedding_bag = torch.embedding_bag +def ipex_embedding_bag( + weight: Tensor, + input: Tensor, + offsets: Optional[Tensor] = None, + scale_grad_by_freq: bool = False, + mode: int = 0, + sparse: bool = False, + per_sample_weights: Optional[Tensor] = None, + include_last_offset: bool = False, + padding_idx: Optional[int] = None, +) -> Tuple[Tensor, Tensor, Tensor, Tensor]: + if weight.device.type in ipex.DEVICE: + assert padding_idx == None + ret = torch.ops.torch_ipex.embedding_bag(weight, input, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset) + return ret[0], torch.rand(0), torch.rand(0), torch.rand(0) + else: + return torch_embedding_bag(weight, input, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset, padding_idx) -def embeddingbag(weights, indices, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset): - ret = torch.ops.torch_ipex.embedding_bag(weights, indices, offsets, scale_grad_by_freq, mode, sparse, per_sample_weights, include_last_offset) - if len(ret)==1: - ret += [torch.Tensor(), torch.Tensor(), torch.Tensor()] - return ret -torch.embedding_bag = embeddingbag +torch.embedding_bag = ipex_embedding_bag From ffc05dc8badff4eff85143307affe7232a768c3d Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Tue, 29 Jun 2021 10:15:02 -0700 Subject: [PATCH 14/35] Enable batch_norm operator --- scripts/cpu/gen-dense-cpu-ops.py | 1 + torch_ipex/csrc/cpu/DevOPs.cpp | 21 +++++++++++++++++++++ torch_ipex/csrc/cpu/DevOPs.h | 1 + 3 files changed, 23 insertions(+) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index 6fcac50c5..031c8973c 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -32,6 +32,7 @@ 'aten::mul_.Tensor(Tensor(a!) self, Tensor other) -> Tensor(a!)', 'aten::mul.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)', 'aten::linear(Tensor input, Tensor weight, Tensor? bias=None) -> Tensor', + 'aten::batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> Tensor', 'aten::native_batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps) -> (Tensor, Tensor, Tensor)', 'aten::native_batch_norm_backward(Tensor grad_out, Tensor input, Tensor? weight, Tensor? running_mean, Tensor? running_var, Tensor? save_mean, Tensor? save_invstd, bool train, float eps, bool[3] output_mask) -> (Tensor, Tensor, Tensor)', 'aten::avg_pool2d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=0, bool ceil_mode=False, bool count_include_pad=True, int? divisor_override=None) -> Tensor', diff --git a/torch_ipex/csrc/cpu/DevOPs.cpp b/torch_ipex/csrc/cpu/DevOPs.cpp index 9b21d25af..9c10ac0fe 100644 --- a/torch_ipex/csrc/cpu/DevOPs.cpp +++ b/torch_ipex/csrc/cpu/DevOPs.cpp @@ -1232,6 +1232,27 @@ at::Tensor AtenIpexCPUDev::dil_dropout_backward( return dbl::comm::gen_aten_tensor_by(std::move(dX)); } +at::Tensor AtenIpexCPUDev::dil_batch_norm( + const at::Tensor& input, + const at::Tensor& weight, + const at::Tensor& bias, + const at::Tensor& running_mean, + const at::Tensor& running_var, + bool train, + double momentum, + double eps, + bool cudnn_enabled) { + return std::get<0>(at::native_batch_norm( + input, + weight, + bias, + running_mean, + running_var, + train, + momentum, + eps)); +} + std::tuple AtenIpexCPUDev::dil_native_batch_norm( const at::Tensor& input, const at::Tensor& weight, diff --git a/torch_ipex/csrc/cpu/DevOPs.h b/torch_ipex/csrc/cpu/DevOPs.h index f07ec5681..4df0e7026 100644 --- a/torch_ipex/csrc/cpu/DevOPs.h +++ b/torch_ipex/csrc/cpu/DevOPs.h @@ -45,6 +45,7 @@ class AtenIpexCPUDev { static std::tuple dil_linear_backward(const at::Tensor& input, const at::Tensor& grad_output, const at::Tensor& weight, std::array output_mask); static at::Tensor dil_dropout(const at::Tensor& self, double ratio, bool train); static at::Tensor dil_dropout_backward(const at::Tensor& grady, const at::Tensor& mask, double ratio); + static at::Tensor dil_batch_norm(const at::Tensor& input, const at::Tensor& weight, const at::Tensor& bias, const at::Tensor& running_mean, const at::Tensor& running_var, bool train, double momentum, double eps, bool cudnn_enabled); static std::tuple dil_native_batch_norm(const at::Tensor& input, const at::Tensor& weight, const at::Tensor& bias, const at::Tensor& running_mean, const at::Tensor& running_var, bool train, double momentum, double eps); static std::tuple dil_native_batch_norm_backward(const at::Tensor& grad_output, const at::Tensor& input, const at::Tensor& weight, const at::Tensor& running_mean, const at::Tensor& running_var, const at::Tensor& save_mean, const at::Tensor& save_invstd, bool train,double eps, std::array grad_input_mask); static at::Tensor dil_frozen_batch_norm(const at::Tensor& input, const at::Tensor& weight, const at::Tensor& bias, const at::Tensor& running_mean, const at::Tensor& running_var, double eps); From 5de5e1ff1fb428ebfec0849b2a7431d924428322 Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Wed, 30 Jun 2021 19:09:55 -0700 Subject: [PATCH 15/35] Exclude the operators that do not run into autograd --- scripts/cpu/gen-dense-cpu-ops.py | 36 +++++++++- tests/cpu/test_bf16_lazy_reorder.py | 2 +- torch_ipex/csrc/cpu/DevOPs.cpp | 104 ++++++++++++++++++++++++++++ torch_ipex/csrc/cpu/DevOPs.h | 4 ++ torch_ipex/ops/gru.py | 4 +- torch_ipex/ops/lstm.py | 3 +- torch_ipex/ops/rnn.py | 6 +- 7 files changed, 154 insertions(+), 5 deletions(-) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index 031c8973c..c592562a5 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -76,7 +76,7 @@ 'aten::clone(Tensor self, *, MemoryFormat? memory_format=None) -> Tensor', 'aten::gelu(Tensor self) -> Tensor', 'aten::gelu_backward(Tensor grad, Tensor self) -> Tensor', - 'aten::slice.Tensor(Tensor(a) self, int dim=0, int? start=0, int? end=9223372036854775807, int step=1) -> Tensor(a)', + 'aten::slice.Tensor(Tensor(a) self, int dim=0, int? start=None, int? end=None, int step=1) -> Tensor(a)', 'aten::select.int(Tensor(a) self, int dim, int index) -> Tensor(a)', 'aten::select.Dimname(Tensor(a) self, Dimname dim, int index) -> Tensor(a)', 'aten::unbind.int(Tensor(a) self, int dim=0) -> Tensor(a)[]', @@ -113,6 +113,10 @@ 'aten::div.Scalar(Tensor self, Scalar other) -> Tensor', 'aten::div.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)', 'aten::permute(Tensor(a) self, int[] dims) -> Tensor(a)', + 'aten::to.dtype_layout(Tensor self, *, ScalarType? dtype=None, Layout? layout=None, Device? device=None, bool? pin_memory=None, bool non_blocking=False, bool copy=False, MemoryFormat? memory_format=None) -> Tensor', + 'aten::to.device(Tensor self, Device device, ScalarType dtype, bool non_blocking=False, bool copy=False, MemoryFormat? memory_format=None) -> Tensor', + 'aten::to.dtype(Tensor self, ScalarType dtype, bool non_blocking=False, bool copy=False, MemoryFormat? memory_format=None) -> Tensor', + 'aten::to.other(Tensor self, Tensor other, bool non_blocking=False, bool copy=False, MemoryFormat? memory_format=None) -> Tensor', ] _FN_IPEX_FUNCS_WITH_SIMPLE_ATEN_SIG = [ @@ -127,6 +131,26 @@ 'aten::div.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)', ] +_FN_EXCLUDE_FUNCS_WITH_SIMPLE_ATEN_SIG = [ + "aten::conv1d(Tensor input, Tensor weight, Tensor? bias=None, int[1] stride=1, int[1] padding=0, int[1] dilation=1, int groups=1) -> Tensor", + "aten::conv2d(Tensor input, Tensor weight, Tensor? bias=None, int[2] stride=1, int[2] padding=0, int[2] dilation=1, int groups=1) -> Tensor", + "aten::conv3d(Tensor input, Tensor weight, Tensor? bias=None, int[3] stride=1, int[3] padding=0, int[3] dilation=1, int groups=1) -> Tensor", + "aten::conv1d.padding(Tensor input, Tensor weight, Tensor? bias=None, int[1] stride=1, str padding=\"valid\", int[1] dilation=1, int groups=1) -> Tensor", + "aten::conv2d.padding(Tensor input, Tensor weight, Tensor? bias=None, int[2] stride=1, str padding=\"valid\", int[2] dilation=1, int groups=1) -> Tensor", + "aten::conv3d.padding(Tensor input, Tensor weight, Tensor? bias=None, int[3] stride=1, str padding=\"valid\", int[3] dilation=1, int groups=1) -> Tensor", + "aten::convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups) -> Tensor", + "aten::_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool cudnn_enabled, bool allow_tf32) -> Tensor", + "aten::_convolution.deprecated(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool cudnn_enabled) -> Tensor", + "aten::conv_transpose1d(Tensor input, Tensor weight, Tensor? bias=None, int[1] stride=1, int[1] padding=0, int[1] output_padding=0, int groups=1, int[1] dilation=1) -> Tensor", + "aten::conv_transpose2d.input(Tensor input, Tensor weight, Tensor? bias=None, int[2] stride=1, int[2] padding=0, int[2] output_padding=0, int groups=1, int[2] dilation=1) -> Tensor", + "aten::conv_transpose3d.input(Tensor input, Tensor weight, Tensor? bias=None, int[3] stride=1, int[3] padding=0, int[3] output_padding=0, int groups=1, int[3] dilation=1) -> Tensor", + "aten::log_softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor", + "aten::log_softmax.Dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor", + "aten::softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor", + "aten::softmax.Dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor", + "aten::contiguous(Tensor(a) self, *, MemoryFormat memory_format=contiguous_format) -> Tensor(a)", +] + _SHALLOW_FALLBACK_TO_CPU_TENSOR_LIST = 'shallowFallbackToCPUTensorList' _SHALLOW_FALLBACK_TO_CPU_TENSOR = 'shallowFallbackToCPUTensor' _SHALLOW_UPGRADE_TO_DPCPP_TENSOR = 'shallowUpgradeToDPCPPTensor' @@ -222,6 +246,13 @@ def is_dnnl_func(self, simple_aten_sig): return True return False + def is_exclude_func(self, simple_aten_sig): + stripped_str = simple_aten_sig.replace(' ', '') + for item in _FN_EXCLUDE_FUNCS_WITH_SIMPLE_ATEN_SIG: + if stripped_str == item.replace(' ', ''): + return True + return False + def is_ipex_func(self, simple_aten_sig): stripped_str = simple_aten_sig.replace(' ', '') for item in _FN_IPEX_FUNCS_WITH_SIMPLE_ATEN_SIG: @@ -581,6 +612,9 @@ def is_conv_overrideable_func(fname): func_defs = [] for cpp_sig, aten_sig, native_cpp_sig, cpp_func_sig_str, aten_func_sig_str in self._sigs: + if self.is_exclude_func(aten_func_sig_str): + continue + # The operator name should be unique because the new registration mechanism of PyTorch 1.7 new_cpp_func_name = aten_sig.def_name.replace('.', '_') cpp_func_str_h, cpp_func_str_cpp = self.gen_func_signature(cpp_func_sig_str, cpp_sig.def_name, new_cpp_func_name) diff --git a/tests/cpu/test_bf16_lazy_reorder.py b/tests/cpu/test_bf16_lazy_reorder.py index 04b979de3..c0c80cf32 100644 --- a/tests/cpu/test_bf16_lazy_reorder.py +++ b/tests/cpu/test_bf16_lazy_reorder.py @@ -2488,7 +2488,7 @@ def test__pack_padded_sequence(self): seqs = [torch.FloatTensor(random.randint(1, 6)).to(ipex.DEVICE) for _ in range(5)] seqs = [s.random_(-128, 128) for s in seqs] ordered = sorted(seqs, key=len, reverse=True) - lengths = list(map(len, ordered)) + lengths = torch.as_tensor(list(map(len, ordered)), dtype=torch.int64).to(ipex.DEVICE) padded_tensor = rnn_utils.pad_sequence(ordered) with AutoDNNL(True): for enforce_sorted in [True, False]: diff --git a/torch_ipex/csrc/cpu/DevOPs.cpp b/torch_ipex/csrc/cpu/DevOPs.cpp index 9c10ac0fe..85f32a674 100644 --- a/torch_ipex/csrc/cpu/DevOPs.cpp +++ b/torch_ipex/csrc/cpu/DevOPs.cpp @@ -1253,6 +1253,11 @@ at::Tensor AtenIpexCPUDev::dil_batch_norm( eps)); } +void check_dims_match_num_input_features(const char* arg_name, int64_t expected, int64_t actual){ + IPEX_CHECK(actual == expected, + arg_name, " should contain ", expected, " elements not ", actual); +} + std::tuple AtenIpexCPUDev::dil_native_batch_norm( const at::Tensor& input, const at::Tensor& weight, @@ -1263,6 +1268,22 @@ std::tuple AtenIpexCPUDev::dil_native_batch_ double momentum, double eps) { DEBUG("AtenIpexCPUDev::dil_native_batch_norm\n"); +#define CHECK_MISMATCH(arg_name, expected, actual) \ + IPEX_CHECK(actual == expected, arg_name, " should contain ", expected, " elements not ", actual) + + auto num_features = input.sizes()[1]; + if (running_mean.defined()) { + CHECK_MISMATCH("running_mean", num_features, running_mean.numel()); + } + if (running_var.defined()) { + CHECK_MISMATCH("running_var", num_features, running_var.numel()); + } + if (weight.defined()) { + CHECK_MISMATCH("weight", num_features, weight.numel()); + } + if (bias.defined()) { + CHECK_MISMATCH("bias", num_features, bias.numel()); + } bool is_layer_norm = (!weight.defined()) && (!bias.defined()) && (!running_mean.defined()) && (!running_var.defined()); if (is_layer_norm) { @@ -3017,5 +3038,88 @@ at::Tensor AtenIpexCPUDev::dil_permute(const at::Tensor & self, at::IntArrayRef return dil_as_strided(self, newSizes, newStrides, self.storage_offset()); } +inline at::Tensor to_impl(const at::Tensor& self, const at::TensorOptions& options, bool non_blocking, bool copy) { + auto memory_format = options.memory_format_opt().value_or(at::MemoryFormat::Preserve); + if (self.dtype() == options.dtype() && + self.layout() == options.layout() && + self.device() == options.device() && + !copy && + (memory_format == at::MemoryFormat::Preserve || self.suggest_memory_format() == memory_format)) { + return self; + } + + bool pin_out = false; + if (memory_format == at::MemoryFormat::Preserve) { + if (self.is_non_overlapping_and_dense()) { + // Copy all strides + auto r = at::empty_strided(self.sizes(), + self.strides(), + options.memory_format(c10::nullopt).pinned_memory(pin_out)); + r.copy_(self, non_blocking); + return r; + } else { + memory_format = self.suggest_memory_format(); + } + } + // See Note [Explicit nullopt MemoryFormat argument] + auto r = at::empty(self.sizes(), + options.memory_format(memory_format).pinned_memory(pin_out), + c10::nullopt); + r.copy_(self, non_blocking); + return r; +} + +at::Tensor AtenIpexCPUDev::dil_to(const at::Tensor & self, c10::optional dtype, c10::optional layout, c10::optional device, c10::optional pin_memory, bool non_blocking, bool copy, c10::optional optional_memory_format){ + DEBUG("AtenIpexCPUDev::dil_to_dtype_layout\n"); + // See [Note: hacky wrapper removal for TensorOptions] + auto options_ = at::TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory); + + TORCH_CHECK( + !(options_.has_memory_format() && optional_memory_format.has_value()), + "Cannot set memory_format both in TensorOptions and explicit argument; please delete " + "the redundant setter."); + auto options = options_.merge_memory_format(optional_memory_format); + + TORCH_CHECK(options.requires_grad_opt() == c10::nullopt, + "to(options) expects unset requires_grad flag, but got " + "options.requires_grad set as ", options.requires_grad()); + + TORCH_CHECK(!options.has_layout() || self.layout() == options.layout(), + "to(options) doesn't support converting to a different layout, " + "but got self.layout being ", self.layout(), + " and options.layout set as ", options.layout()); + + auto specified_options = self.options().merge_in(options); + return to_impl(self, specified_options, non_blocking, copy); +} + +at::Tensor AtenIpexCPUDev::dil_to(const at::Tensor & self, c10::Device device, at::ScalarType dtype, bool non_blocking, bool copy, c10::optional optional_memory_format){ + DEBUG("AtenIpexCPUDev::dil_to_device\n"); + return to_impl( + self, + self.options().device(device).dtype(dtype).memory_format(optional_memory_format), + non_blocking, + copy); +} + +at::Tensor AtenIpexCPUDev::dil_to(const at::Tensor & self, at::ScalarType dtype, bool non_blocking, bool copy, c10::optional optional_memory_format) { + DEBUG("AtenIpexCPUDev::dil_to_dtype\n"); + return to_impl( + self, + self.options().dtype(dtype).memory_format(optional_memory_format), + non_blocking, + copy); +} + +at::Tensor AtenIpexCPUDev::dil_to(const at::Tensor& self, const at::Tensor& other, bool non_blocking, bool copy, c10::optional optional_memory_format) { + DEBUG("AtenIpexCPUDev::dil_to_other\n"); + auto options = other.options(); + return to_impl( + self, + options.memory_format(optional_memory_format), + non_blocking, + copy); +} + } // namespace cpu } // namespace torch_ipex diff --git a/torch_ipex/csrc/cpu/DevOPs.h b/torch_ipex/csrc/cpu/DevOPs.h index 4df0e7026..151a77942 100644 --- a/torch_ipex/csrc/cpu/DevOPs.h +++ b/torch_ipex/csrc/cpu/DevOPs.h @@ -122,6 +122,10 @@ class AtenIpexCPUDev { static at::Tensor &dil_div_out(at::Tensor &out, const at::Tensor &self, const at::Tensor &other); static at::Tensor dil_permute(const at::Tensor & self, at::IntArrayRef dims); + static at::Tensor dil_to(const at::Tensor & self, c10::optional dtype, c10::optional layout, c10::optional device, c10::optional pin_memory, bool non_blocking, bool copy, c10::optional memory_format); + static at::Tensor dil_to(const at::Tensor & self, c10::Device device, at::ScalarType dtype, bool non_blocking, bool copy, c10::optional memory_format); + static at::Tensor dil_to(const at::Tensor & self, at::ScalarType dtype, bool non_blocking, bool copy, c10::optional memory_format); + static at::Tensor dil_to(const at::Tensor& self, const at::Tensor& other, bool non_blocking, bool copy, c10::optional optional_memory_format); }; } // namespace cpu diff --git a/torch_ipex/ops/gru.py b/torch_ipex/ops/gru.py index a8412f5ff..58c0e5ece 100644 --- a/torch_ipex/ops/gru.py +++ b/torch_ipex/ops/gru.py @@ -10,6 +10,8 @@ def ipex_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidi if input.device.type == 'xpu' and (dropout == 0 or training == False): return torch.ops.torch_ipex.gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) else: + if training: + assert input.device.type != 'xpu' return VF_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) def gru(*args): @@ -18,4 +20,4 @@ def gru(*args): else: return ipex_gru(*args) -_VF.gru = gru \ No newline at end of file +_VF.gru = gru diff --git a/torch_ipex/ops/lstm.py b/torch_ipex/ops/lstm.py index 25ad8ccfd..5587b8145 100644 --- a/torch_ipex/ops/lstm.py +++ b/torch_ipex/ops/lstm.py @@ -6,6 +6,7 @@ def ipex_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device): # For LSTM training with dropout, fallback to cpu due to performance issue in oneDNN mode if training and dropout != 0: + assert input.device.type != 'xpu' return fallback_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device=device) else: return torch.ops.torch_ipex.lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) @@ -49,7 +50,7 @@ def lstm(*args): device = get_device(*args) if device == "cpu": return VF_lstm(*args) - + # For LSTM with pack_padded_sequence as input, fallback to cpu due to performance issue in oneDNN mode if isinstance(args[1], torch.Tensor): return fallback_lstm(*args, device=device) diff --git a/torch_ipex/ops/rnn.py b/torch_ipex/ops/rnn.py index 7f710c720..ccd527977 100644 --- a/torch_ipex/ops/rnn.py +++ b/torch_ipex/ops/rnn.py @@ -13,12 +13,16 @@ def rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidi if input.device.type == 'xpu' and (dropout == 0 or training == False): return torch.ops.torch_ipex.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) else: + if training: + assert input.device.type != 'xpu' return _VF.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) def rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): if input.device.type == 'xpu' and (dropout == 0 or training == False): return torch.ops.torch_ipex.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) else: + if training: + assert input.device.type != 'xpu' return _VF.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) _rnn_impls = { @@ -412,4 +416,4 @@ def __init__(self, *args, **kwargs): raise ValueError("Unknown nonlinearity '{}'".format(self.nonlinearity)) super(RNN, self).__init__(mode, *args, **kwargs) -torch.nn.RNN = RNN \ No newline at end of file +torch.nn.RNN = RNN From 80410a16149717455551f59e566276ed2520752b Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Thu, 1 Jul 2021 00:50:50 -0700 Subject: [PATCH 16/35] Pass all test cases except test_torch --- scripts/cpu/gen-dense-cpu-ops.py | 2 ++ scripts/cpu/pytorch_headers/SparseCPUType.h | 24 +++++++++++---------- tests/cpu/test_bf16_lazy_reorder.py | 2 +- tests/cpu/test_lazy_reorder.py | 2 +- torch_ipex/csrc/aten_ipex_bridge.cpp | 3 ++- torch_ipex/ops/gru.py | 5 +++-- torch_ipex/ops/lstm.py | 3 +++ torch_ipex/ops/rnn.py | 10 +++++---- 8 files changed, 31 insertions(+), 20 deletions(-) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index c592562a5..c21bf7b00 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -149,6 +149,8 @@ "aten::softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor", "aten::softmax.Dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor", "aten::contiguous(Tensor(a) self, *, MemoryFormat memory_format=contiguous_format) -> Tensor(a)", + "aten::dropout(Tensor input, float p, bool train) -> Tensor", + "aten::dropout_(Tensor(a!) self, float p, bool train) -> Tensor(a!)", ] _SHALLOW_FALLBACK_TO_CPU_TENSOR_LIST = 'shallowFallbackToCPUTensorList' diff --git a/scripts/cpu/pytorch_headers/SparseCPUType.h b/scripts/cpu/pytorch_headers/SparseCPUType.h index 05d2fc47d..96628b78f 100644 --- a/scripts/cpu/pytorch_headers/SparseCPUType.h +++ b/scripts/cpu/pytorch_headers/SparseCPUType.h @@ -35,8 +35,8 @@ namespace SparseCPUType { Tensor empty(IntArrayRef size, optional names, optional dtype, optional layout, optional device, optional pin_memory, optional memory_format); Tensor empty(IntArrayRef size, optional dtype, optional layout, optional device, optional pin_memory, optional memory_format); Tensor add(const Tensor & self, const Tensor & other, Scalar alpha); - Tensor & add_(Tensor & self, const Tensor & other, Scalar alpha); - Tensor & add_out(const Tensor & self, const Tensor & other, Scalar alpha, Tensor & out); + Tensor & add_(Tensor & self, const Tensor & other, const Scalar & alpha); + Tensor & add_out(const Tensor & self, const Tensor & other, const Scalar & alpha, Tensor & out); Tensor div(const Tensor & self, const Tensor & other); Tensor & div_(Tensor & self, const Tensor & other); Tensor & div_out(const Tensor & self, const Tensor & other, Tensor & out); @@ -53,22 +53,23 @@ namespace SparseCPUType { Tensor & mul_out(const Tensor & self, const Tensor & other, Tensor & out); Tensor narrow_copy(const Tensor & self, int64_t dim, int64_t start, int64_t length); Tensor & narrow_copy_out(const Tensor & self, int64_t dim, int64_t start, int64_t length, Tensor & out); - Tensor & sspaddmm_out(const Tensor & self, const Tensor & mat1, const Tensor & mat2, Scalar beta, Scalar alpha, Tensor & out); + Tensor & sspaddmm_out(const Tensor & self, const Tensor & mat1, const Tensor & mat2, const Scalar & beta, const Scalar & alpha, Tensor & out); Tensor true_divide(const Tensor & self, const Tensor & other); Tensor & true_divide_(Tensor & self, const Tensor & other); Tensor & true_divide_out(const Tensor & self, const Tensor & other, Tensor & out); - Tensor native_norm(const Tensor & self, Scalar p); + Tensor native_norm(const Tensor & self, const Scalar & p); Tensor _sparse_sum_backward(const Tensor & grad, const Tensor & self, IntArrayRef dim); Tensor clone(const Tensor & self, optional memory_format); Tensor & pow_out(const Tensor & self, const Tensor & exponent, Tensor & out); - Tensor pow(const Tensor & self, Scalar exponent); + Tensor pow(const Tensor & self, const Scalar & exponent); Tensor & zero_(Tensor & self); - Tensor & sub_out(const Tensor & self, const Tensor & other, Scalar alpha, Tensor & out); - Tensor sub(const Tensor & self, const Tensor & other, Scalar alpha); - Tensor & sub_(Tensor & self, const Tensor & other, Scalar alpha); - Tensor & addmm_out(const Tensor & self, const Tensor & mat1, const Tensor & mat2, Scalar beta, Scalar alpha, Tensor & out); - Tensor addmm(const Tensor & self, const Tensor & mat1, const Tensor & mat2, Scalar beta, Scalar alpha); - Tensor & addmm_(Tensor & self, const Tensor & mat1, const Tensor & mat2, Scalar beta, Scalar alpha); + Tensor & sub_out(const Tensor & self, const Tensor & other, const Scalar & alpha, Tensor & out); + Tensor sub(const Tensor & self, const Tensor & other, const Scalar & alpha); + Tensor & sub_(Tensor & self, const Tensor & other, const Scalar & alpha); + Tensor & addmm_out(const Tensor & self, const Tensor & mat1, const Tensor & mat2, const Scalar & beta, const Scalar & alpha, Tensor & out); + Tensor addmm(const Tensor & self, const Tensor & mat1, const Tensor & mat2, const Scalar & beta, const Scalar & alpha); + Tensor & addmm_(Tensor & self, const Tensor & mat1, const Tensor & mat2, const Scalar & beta, const Scalar & alpha); + Tensor _sparse_coo_tensor_unsafe(const Tensor & indices, const Tensor & values, IntArrayRef size, optional dtype, optional layout, optional device, optional pin_memory); Tensor _sparse_coo_tensor_with_dims(int64_t sparse_dim, int64_t dense_dim, IntArrayRef size, optional dtype, optional layout, optional device, optional pin_memory); Tensor _sparse_coo_tensor_with_dims_and_tensors(int64_t sparse_dim, int64_t dense_dim, IntArrayRef size, const Tensor & indices, const Tensor & values, optional dtype, optional layout, optional device, optional pin_memory); Tensor & sparse_resize_(Tensor & self, IntArrayRef size, int64_t sparse_dim, int64_t dense_dim); @@ -81,6 +82,7 @@ namespace SparseCPUType { int64_t _dimV(const Tensor & self); int64_t _nnz(const Tensor & self); Tensor coalesce(const Tensor & self); + Tensor _coalesce(const Tensor & self); bool is_coalesced(const Tensor & self); Tensor _indices(const Tensor & self); Tensor _values(const Tensor & self); diff --git a/tests/cpu/test_bf16_lazy_reorder.py b/tests/cpu/test_bf16_lazy_reorder.py index c0c80cf32..dea1d8ca1 100644 --- a/tests/cpu/test_bf16_lazy_reorder.py +++ b/tests/cpu/test_bf16_lazy_reorder.py @@ -2508,7 +2508,7 @@ def _lstm_params_list(self, cell): "bias": [False, True], "empty_state": [False, True], "batch_first": [False, True], - "dropout": [0, 1], # [0, 0.5, 1] # TODO 0.5 will fail + "dropout": [0], # [0, 0.5, 1] # TODO 0.5 will fail "batch_size": [1, 2], "seq_len": [1, 3] } diff --git a/tests/cpu/test_lazy_reorder.py b/tests/cpu/test_lazy_reorder.py index 76165f559..56717a314 100644 --- a/tests/cpu/test_lazy_reorder.py +++ b/tests/cpu/test_lazy_reorder.py @@ -1603,7 +1603,7 @@ def _lstm_params_list(self, cell): "bias": [False, True], "empty_state": [False, True], "batch_first": [False, True], - "dropout": [0, 1], # [0, 0.5, 1] # TODO 0.5 will fail + "dropout": [0], # [0, 0.5, 1] # TODO 0.5 will fail "batch_size": [1, 2], "seq_len": [1, 3] } diff --git a/torch_ipex/csrc/aten_ipex_bridge.cpp b/torch_ipex/csrc/aten_ipex_bridge.cpp index d32cb4fef..9460a56a0 100644 --- a/torch_ipex/csrc/aten_ipex_bridge.cpp +++ b/torch_ipex/csrc/aten_ipex_bridge.cpp @@ -221,7 +221,8 @@ at::Tensor shallowUpgradeToDPCPPTensor(const at::Tensor& cpuTensor) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY(_tensor.device().type() == at::DeviceType::XPU); IPEXTensorImpl* ipex_impl = (IPEXTensorImpl *)_tensor.unsafeGetTensorImpl(); ipex_impl->copy_meta_info(cpu_tensor_impl); - TORCH_INTERNAL_ASSERT_DEBUG_ONLY(! cpuTensor.requires_grad()); + ipex_impl->copy_auto_grad(cpu_tensor_impl); + // TORCH_INTERNAL_ASSERT_DEBUG_ONLY(! cpuTensor.requires_grad()); CHECK_TENSOR_CRITICAL(_tensor, cpuTensor, true); //TODO: Cannot set reserved_ // dest_impl->reserved_ = src_impl->reserved_; diff --git a/torch_ipex/ops/gru.py b/torch_ipex/ops/gru.py index 58c0e5ece..bea095958 100644 --- a/torch_ipex/ops/gru.py +++ b/torch_ipex/ops/gru.py @@ -10,8 +10,9 @@ def ipex_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidi if input.device.type == 'xpu' and (dropout == 0 or training == False): return torch.ops.torch_ipex.gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) else: - if training: - assert input.device.type != 'xpu' + if training and input.device.type == 'xpu': + raise Exception("IPEX does not support LSTM training if its dropout is not 0. \ + Please explicity convert the gru module and its tensors to CPU and convert the output tensor back to ipex.DEVICE.") return VF_gru(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) def gru(*args): diff --git a/torch_ipex/ops/lstm.py b/torch_ipex/ops/lstm.py index 5587b8145..167dafc47 100644 --- a/torch_ipex/ops/lstm.py +++ b/torch_ipex/ops/lstm.py @@ -6,6 +6,9 @@ def ipex_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device): # For LSTM training with dropout, fallback to cpu due to performance issue in oneDNN mode if training and dropout != 0: + if input.device.type == 'xpu': + raise Exception("IPEX does not support LSTM training if its dropout is not 0. \ + Please explicity convert the gru module and its tensors to CPU and convert the output tensor back to ipex.DEVICE.") assert input.device.type != 'xpu' return fallback_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device=device) else: diff --git a/torch_ipex/ops/rnn.py b/torch_ipex/ops/rnn.py index ccd527977..ee9f24492 100644 --- a/torch_ipex/ops/rnn.py +++ b/torch_ipex/ops/rnn.py @@ -13,16 +13,18 @@ def rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidi if input.device.type == 'xpu' and (dropout == 0 or training == False): return torch.ops.torch_ipex.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) else: - if training: - assert input.device.type != 'xpu' + if training and input.device.type == 'xpu': + raise Exception("IPEX does not support RNN-Tanh training if its dropout is not 0. \ + Please explicity convert the gru module and its tensors to CPU and convert the output tensor back to ipex.DEVICE.") return _VF.rnn_tanh(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) def rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first): if input.device.type == 'xpu' and (dropout == 0 or training == False): return torch.ops.torch_ipex.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) else: - if training: - assert input.device.type != 'xpu' + if training and input.device.type == 'xpu': + raise Exception("IPEX does not support RNN-ReLU training if its dropout is not 0. \ + Please explicity convert the gru module and its tensors to CPU and convert the output tensor back to ipex.DEVICE.") return _VF.rnn_relu(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first) _rnn_impls = { From bd3412c4ab75882637712e2464e68554f4fc8737 Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Thu, 1 Jul 2021 09:23:12 -0700 Subject: [PATCH 17/35] Fix the issues 1. LSTM indents error 2. Check batch_normalization --- torch_ipex/csrc/cpu/DevOPs.cpp | 39 ++++++++++++++++------------------ torch_ipex/ops/lstm.py | 6 +++--- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/torch_ipex/csrc/cpu/DevOPs.cpp b/torch_ipex/csrc/cpu/DevOPs.cpp index 85f32a674..e252cdef5 100644 --- a/torch_ipex/csrc/cpu/DevOPs.cpp +++ b/torch_ipex/csrc/cpu/DevOPs.cpp @@ -1242,6 +1242,24 @@ at::Tensor AtenIpexCPUDev::dil_batch_norm( double momentum, double eps, bool cudnn_enabled) { + + #define CHECK_MISMATCH(arg_name, expected, actual) \ + IPEX_CHECK(actual == expected, arg_name, " should contain ", expected, " elements not ", actual) + + auto num_features = input.sizes()[1]; + if (running_mean.defined()) { + CHECK_MISMATCH("running_mean", num_features, running_mean.numel()); + } + if (running_var.defined()) { + CHECK_MISMATCH("running_var", num_features, running_var.numel()); + } + if (weight.defined()) { + CHECK_MISMATCH("weight", num_features, weight.numel()); + } + if (bias.defined()) { + CHECK_MISMATCH("bias", num_features, bias.numel()); + } + return std::get<0>(at::native_batch_norm( input, weight, @@ -1253,11 +1271,6 @@ at::Tensor AtenIpexCPUDev::dil_batch_norm( eps)); } -void check_dims_match_num_input_features(const char* arg_name, int64_t expected, int64_t actual){ - IPEX_CHECK(actual == expected, - arg_name, " should contain ", expected, " elements not ", actual); -} - std::tuple AtenIpexCPUDev::dil_native_batch_norm( const at::Tensor& input, const at::Tensor& weight, @@ -1268,22 +1281,6 @@ std::tuple AtenIpexCPUDev::dil_native_batch_ double momentum, double eps) { DEBUG("AtenIpexCPUDev::dil_native_batch_norm\n"); -#define CHECK_MISMATCH(arg_name, expected, actual) \ - IPEX_CHECK(actual == expected, arg_name, " should contain ", expected, " elements not ", actual) - - auto num_features = input.sizes()[1]; - if (running_mean.defined()) { - CHECK_MISMATCH("running_mean", num_features, running_mean.numel()); - } - if (running_var.defined()) { - CHECK_MISMATCH("running_var", num_features, running_var.numel()); - } - if (weight.defined()) { - CHECK_MISMATCH("weight", num_features, weight.numel()); - } - if (bias.defined()) { - CHECK_MISMATCH("bias", num_features, bias.numel()); - } bool is_layer_norm = (!weight.defined()) && (!bias.defined()) && (!running_mean.defined()) && (!running_var.defined()); if (is_layer_norm) { diff --git a/torch_ipex/ops/lstm.py b/torch_ipex/ops/lstm.py index 167dafc47..e6d05d7f5 100644 --- a/torch_ipex/ops/lstm.py +++ b/torch_ipex/ops/lstm.py @@ -6,9 +6,9 @@ def ipex_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device): # For LSTM training with dropout, fallback to cpu due to performance issue in oneDNN mode if training and dropout != 0: - if input.device.type == 'xpu': - raise Exception("IPEX does not support LSTM training if its dropout is not 0. \ - Please explicity convert the gru module and its tensors to CPU and convert the output tensor back to ipex.DEVICE.") + if input.device.type == 'xpu': + raise Exception("IPEX does not support LSTM training if its dropout is not 0. \ + Please explicity convert the gru module and its tensors to CPU and convert the output tensor back to ipex.DEVICE.") assert input.device.type != 'xpu' return fallback_lstm(input, hx, _flat_weights, bias, num_layers, dropout, training, bidirectional, batch_first, device=device) else: From d066274f439cd4fccdc3c4f60589be018410032a Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Thu, 1 Jul 2021 20:25:37 -0700 Subject: [PATCH 18/35] Fix the issue that the grad of nll_loss input is none --- scripts/cpu/gen-dense-cpu-ops.py | 3 +++ tests/cpu/test_bf16_lazy_reorder.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index c21bf7b00..5c18b954f 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -151,6 +151,9 @@ "aten::contiguous(Tensor(a) self, *, MemoryFormat memory_format=contiguous_format) -> Tensor(a)", "aten::dropout(Tensor input, float p, bool train) -> Tensor", "aten::dropout_(Tensor(a!) self, float p, bool train) -> Tensor(a!)", + "aten::nll_loss_nd(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", + "aten::nll_loss(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", + "aten::nll_loss.out(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100, *, Tensor(a!) out) -> Tensor(a!)", ] _SHALLOW_FALLBACK_TO_CPU_TENSOR_LIST = 'shallowFallbackToCPUTensorList' diff --git a/tests/cpu/test_bf16_lazy_reorder.py b/tests/cpu/test_bf16_lazy_reorder.py index dea1d8ca1..72474c8d2 100644 --- a/tests/cpu/test_bf16_lazy_reorder.py +++ b/tests/cpu/test_bf16_lazy_reorder.py @@ -293,7 +293,7 @@ def _test_deconv(self, dims): self.assertEqual( y_aten, y_auto_mix_train, atol=1e-1, rtol=1e-5) self.assertEqual( - module.weight.grad, module_auto_mix_train.weight.grad, atol=1e-1, rtol=1e-5) + module.weight.grad, module_auto_mix_train.weight.grad, atol=2e-1, rtol=1e-3) self.assertEqual( x_aten.grad, x_auto_mix_train.grad, atol=1e-1, rtol=1e-5) if bias: From 623c58e8a2619064040d96c6dc66954b7eb96e2c Mon Sep 17 00:00:00 2001 From: jianangu Date: Thu, 1 Jul 2021 22:06:45 -0700 Subject: [PATCH 19/35] update build version from 1.8.0.1 to 1.9.0 (along with pytorch version) --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c9ef1381d..0af323766 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,8 @@ #!/usr/bin/env python from __future__ import print_function -TORCH_VERSION = '1.8.0' -TORCH_IPEX_VERSION = '1.8.0.1' +TORCH_VERSION = '1.9.0' +TORCH_IPEX_VERSION = '1.9.0' # import torch import platform From 72aed73a27f813851a503adef99cab8eb9232eef Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Sat, 3 Jul 2021 04:16:51 +0900 Subject: [PATCH 20/35] fix dil_cat bug when concating empty tensors with customized shape --- torch_ipex/csrc/cpu/DevOPs.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/torch_ipex/csrc/cpu/DevOPs.cpp b/torch_ipex/csrc/cpu/DevOPs.cpp index e252cdef5..549c05e87 100644 --- a/torch_ipex/csrc/cpu/DevOPs.cpp +++ b/torch_ipex/csrc/cpu/DevOPs.cpp @@ -2113,9 +2113,8 @@ at::Tensor& AtenIpexCPUDev::dil_cat_out(at::Tensor& result, at::TensorList tenso dim = at::legacy_cat_wrap_dim(dim, tensors); std::vector x; for (auto i =0; i< tensors.size(); i++) { - IPEX_CHECK(!(tensors[i].dim() == 1 && tensors[i].sizes()[0] == 0), - "Currently Mkldnn cat operators do not support empty tensor."); - + if(tensors[i].numel() == 0) + continue; dbl::comm::reorder_to_bf16_for_mix_prec(tensors[i], true); x.push_back(dbl::comm::try_gen_dil_tensor(tensors[i])); @@ -2141,8 +2140,8 @@ at::Tensor AtenIpexCPUDev::dil_cat(at::TensorList tensors, int64_t dim) { std::vector data_shift; for (auto i = 0; i < tensors.size(); i++) { - IPEX_CHECK(!(tensors[i].dim() == 1 && tensors[i].sizes()[0] == 0), - "Currently Mkldnn cat operators do not support empty tensor."); + if(tensors[i].numel() == 0) + continue; tensors_contiguous[i] = IS_CONTIGUOUS_ANY(tensors[i]) ? tensors[i] : tensors[i].contiguous(); dbl::comm::reorder_to_bf16_for_mix_prec(tensors_contiguous[i], true); From 3594c0b823fb8acede2871491a84622eb9176ec4 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Sun, 4 Jul 2021 04:16:38 +0900 Subject: [PATCH 21/35] 1. moved python codes out from libtorch_ipex.so to _C.so 2. removed pybind11 as denpendency library from third_party folder 3. changed "import intel_pytorch_extension" to "import torch_ipex" in tests folder, Readme.md, torch_ipex/ops/embeddingbag.py and torch_ipex/launch.py 4. commented "core.enable_torch_ccl()" out in torch_ipex/__init__.py, to avoid the following error when "import torch_ipex" Traceback (most recent call last): File "", line 1, in File "/home/jingxu1/dl/pytorch/srcs/venv_test_py38/lib/python3.8/site-packages/torch_ipex/__init__.py", line 14, in core.enable_torch_ccl() RuntimeError: arg(): could not convert default argument into a Python object (type not registered yet?). Compile in debug mode for more information. --- .gitmodules | 3 - CMakeLists.txt | 2 - README.md | 4 +- cmake/CPU.cmake | 6 - setup.py | 41 ++- tests/cpu/common_device_type.py | 2 +- tests/cpu/common_ipex_conf.py | 2 +- tests/cpu/linear_prepack.py | 14 +- tests/cpu/override.py | 2 +- tests/cpu/test_bf16_lazy_reorder.py | 2 +- tests/cpu/test_emb.py | 2 +- tests/cpu/test_int8.py | 10 +- tests/cpu/test_interaction.py | 2 +- tests/cpu/test_jit.py | 4 +- tests/cpu/test_lazy_reorder.py | 2 +- tests/cpu/test_mlp.py | 2 +- tests/cpu/test_rn50_cpu_ops.py | 2 +- tests/cpu/test_sparse.py | 2 +- tests/cpu/test_torch.py | 2 +- .../utils/test_lazy_reorder_with_pattern.py | 2 +- tests/cpu/utils/utils.py | 2 +- third_party/pybind11 | 1 - torch_ipex/__init__.py | 2 +- torch_ipex/csrc/CMakeLists.txt | 2 - torch_ipex/csrc/_C.cpp | 264 +++++++++++++++++- torch_ipex/csrc/init_python_bindings.cpp | 263 ----------------- torch_ipex/csrc/init_python_bindings.h | 12 - torch_ipex/csrc/py_init.cpp | 258 ----------------- torch_ipex/csrc/py_init.h | 12 - torch_ipex/launch.py | 164 +++++------ torch_ipex/ops/embeddingbag.py | 2 +- 31 files changed, 402 insertions(+), 688 deletions(-) delete mode 160000 third_party/pybind11 delete mode 100644 torch_ipex/csrc/init_python_bindings.cpp delete mode 100644 torch_ipex/csrc/init_python_bindings.h delete mode 100644 torch_ipex/csrc/py_init.cpp delete mode 100644 torch_ipex/csrc/py_init.h diff --git a/.gitmodules b/.gitmodules index 3569e75e4..a0dbdf925 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "third_party/pybind11"] - path = third_party/pybind11 - url = https://github.com/pybind/pybind11.git [submodule "third_party/mkl-dnn"] path = third_party/mkl-dnn url = https://github.com/oneapi-src/oneDNN diff --git a/CMakeLists.txt b/CMakeLists.txt index e628af1e0..86a953e52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,4 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) # Common dependencies -add_subdirectory(${DPCPP_THIRD_PARTY_ROOT}/pybind11) - include(cmake/CPU.cmake) diff --git a/README.md b/README.md index a86526b0c..7cc15b0be 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ import torch import torch.nn as nn # Import Extension -import intel_pytorch_extension as ipex +import torch_ipex as ipex class Model(nn.Module): def __init__(self): @@ -129,7 +129,7 @@ The extension can simply the case, you just need to enable the auto-mix-precisio import torch import torch.nn as nn -import intel_pytorch_extension as ipex +import torch_ipex as ipex # Automatically mix precision ipex.enable_auto_mixed_precision(mixed_dtype = torch.bfloat16) diff --git a/cmake/CPU.cmake b/cmake/CPU.cmake index c00e4682c..fe3ee87e4 100644 --- a/cmake/CPU.cmake +++ b/cmake/CPU.cmake @@ -141,8 +141,6 @@ endif() include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/torch_ipex) include_directories(${PROJECT_SOURCE_DIR}/torch_ipex/csrc/) -include_directories(${PYTHON_INCLUDE_DIR}) -include_directories(${DPCPP_THIRD_PARTY_ROOT}/pybind11/include) include_directories(${DPCPP_THIRD_PARTY_ROOT}/xsmm/include) include_directories(${TORCHCCL_INCLUDE_DIR}) @@ -168,9 +166,7 @@ ExternalProject_Add(xsmm "-j" INSTALL_COMMAND "" ) -# Compile code with pybind11 set(DPCPP_SRCS ${DPCPP_ATEN_SRCS} ${DPCPP_COMMON_SRCS} ${DPCPP_CPU_SRCS} ${DPCPP_JIT_SRCS}) -# pybind11_add_module(${PLUGIN_NAME} SHARED ${DPCPP_SRCS}) add_library(${PLUGIN_NAME} SHARED ${DPCPP_SRCS}) target_link_libraries(${PLUGIN_NAME} PRIVATE ${DPCPP_THIRD_PARTY_ROOT}/xsmm/lib/libxsmm.a) @@ -190,14 +186,12 @@ else() message(FATAL_ERROR "Unknown ATen parallel backend: ${ATEN_THREADING}") endif() -add_dependencies(${PLUGIN_NAME} pybind11) add_dependencies(${PLUGIN_NAME} torch_ccl) add_dependencies(${PLUGIN_NAME} dnnl) target_link_libraries(${PLUGIN_NAME} PUBLIC dnnl) add_dependencies(${PLUGIN_NAME} xsmm) target_link_libraries(${PLUGIN_NAME} PUBLIC torch_ccl) link_directories(${PYTORCH_INSTALL_DIR}/lib) -target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libtorch_python.so) target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libtorch_cpu.so) target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libc10.so) diff --git a/setup.py b/setup.py index 0af323766..680b2a982 100644 --- a/setup.py +++ b/setup.py @@ -295,10 +295,9 @@ def run(self): def build_ipex_extension(self, ext): if not isinstance(ext, IPEXExt): return super(IPEXBuild, self).build_extension(ext) - ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) - build_dir = os.path.join(ext_dir, '..', 'build_' + ext.name) + build_dir = os.path.join(ext.project_dir, 'build', 'build_' + ext.name) if not os.path.exists(build_dir): - os.mkdir(build_dir) + os.makedirs(build_dir) build_type = 'Release' use_ninja = False @@ -307,16 +306,17 @@ def build_ipex_extension(self, ext): build_type = 'Debug' # install _torch_ipex.so as python module + ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) if ext.name == 'torch_ipex': ext_dir = os.path.join(ext_dir, ext.name) if not os.path.exists(ext_dir): - os.mkdir(ext_dir) + os.makedirs(ext_dir) cmake_args = [ '-DCMAKE_BUILD_TYPE=' + build_type, '-DCMAKE_INSTALL_PREFIX=' + ext_dir, '-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI)), - '-DPYTHON_INCLUDE_DIR=' + python_include_dir, + '-DPYTHON_INCLUDE_DIRS=' + python_include_dir, '-DPYTHON_EXECUTABLE=' + sys.executable, '-DPYTORCH_INSTALL_DIR=' + pytorch_install_dir, '-DPYTORCH_INCLUDE_DIRS=' + pytorch_install_dir + "/include", @@ -382,15 +382,32 @@ def make_relative_rpath(path): TORCH_URL, ] def get_c_module(): - main_compile_args = [] + main_compile_args = ['-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI))] main_libraries = ['torch_ipex'] - main_link_args = [] - main_sources = ["torch_ipex/csrc/_C.cpp"] + main_link_args = [ + '-ltorch_python', + '-ldnnl' + ] + main_sources = [os.path.join("torch_ipex", "csrc", "_C.cpp")] cwd = os.path.dirname(os.path.abspath(__file__)) - # lib_path = os.path.join(cwd, "torch_ipex", "lib") + include_dirs = [ + ".", + os.path.join("torch_ipex", "csrc"), + os.path.join("third_party", "mkl-dnn", "include"), + os.path.join("third_party", "torch_ccl", "src"), + os.path.join("third_party", "torch_ccl", "third_party", "oneCCL", "include"), + os.path.join("build", "build_torch_ipex", "third_party", "mkl-dnn", "include"), + os.path.join(pytorch_install_dir, "include"), + os.path.join(pytorch_install_dir, "include", "torch", "csrc", "api", "include") + ] + #lib_path = os.path.join(cwd, "torch_ipex", "lib") #lib_path = os.path.join(cwd, "build") - lib_path = os.path.join(cwd, "build", "build_torch_ipex") - library_dirs = [lib_path] + #lib_path = os.path.join(cwd, "build", "build_torch_ipex") + library_dirs = [ + os.path.join(cwd, "build", "build_torch_ipex"), + os.path.join(cwd, "build", "build_torch_ipex", "third_party", "mkl-dnn", "src"), + os.path.join(pytorch_install_dir, "lib") + ] #lib_path_1 = os.path.join(cwd, "build", "lib.linux-x86_64-3.8") #library_dirs = [lib_path, lib_path_1] extra_link_args = [] @@ -419,7 +436,7 @@ def get_c_module(): sources=main_sources, language='c', extra_compile_args=main_compile_args + extra_compile_args, - include_dirs=include_paths(), + include_dirs=include_dirs, library_dirs=library_dirs, extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) # extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('..')]) diff --git a/tests/cpu/common_device_type.py b/tests/cpu/common_device_type.py index 805a493bd..fc5c71eca 100644 --- a/tests/cpu/common_device_type.py +++ b/tests/cpu/common_device_type.py @@ -49,7 +49,7 @@ from functools import wraps import unittest import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import copy from common_utils import TestCase, TEST_WITH_ROCM, TEST_MKL, \ skipCUDANonDefaultStreamIf diff --git a/tests/cpu/common_ipex_conf.py b/tests/cpu/common_ipex_conf.py index ee0e9ae1b..b9d19fc0e 100644 --- a/tests/cpu/common_ipex_conf.py +++ b/tests/cpu/common_ipex_conf.py @@ -1,5 +1,5 @@ import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex class AutoMixPrecision(object): def __init__(self, enable_or_not = False, train = False): diff --git a/tests/cpu/linear_prepack.py b/tests/cpu/linear_prepack.py index d2ab6540d..9c12fec83 100644 --- a/tests/cpu/linear_prepack.py +++ b/tests/cpu/linear_prepack.py @@ -1,5 +1,5 @@ import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex from common_utils import int8_calibration ipex.core.enable_auto_dnnl() @@ -22,24 +22,24 @@ def run_linear(auto_mix_conf=None): LL(get_input()) if __name__ == "__main__": - print(f"fp32, {'*' * 50}") + print(f"fp32, {'*' * 50}") run_linear() - print(f"auto-mix for bf16, {'*' * 50}") + print(f"auto-mix for bf16, {'*' * 50}") bf16_conf = ipex.AmpConf(torch.bfloat16) run_linear(bf16_conf) - print(f"back to fp32, {'*' * 50}") + print(f"back to fp32, {'*' * 50}") ipex.core.reorder_to_float32(LL.weight) ipex.core.reorder_to_float32(LL.bias) run_linear() - print(f"auto-mix for int8, {'*' * 50}") + print(f"auto-mix for int8, {'*' * 50}") int8_calibration(LL, [get_input() for i in range(3)], "./int8.config") int8_conf = ipex.AmpConf(torch.int8, "./int8.config") run_linear(int8_conf) - print(f"back to fp32, {'*' * 50}") + print(f"back to fp32, {'*' * 50}") ipex.core.reorder_to_float32(LL.weight) ipex.core.reorder_to_float32(LL.bias) - run_linear() \ No newline at end of file + run_linear() diff --git a/tests/cpu/override.py b/tests/cpu/override.py index 32e1995b9..456b54c65 100644 --- a/tests/cpu/override.py +++ b/tests/cpu/override.py @@ -1,5 +1,5 @@ import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex torch_function = ['rand', 'randint', 'arange', 'bartlett_window', 'blackman_window', \ 'empty', '_empty_affine_quantized', '_empty_per_channel_affine_quantized', \ diff --git a/tests/cpu/test_bf16_lazy_reorder.py b/tests/cpu/test_bf16_lazy_reorder.py index 72474c8d2..b1a7a3562 100644 --- a/tests/cpu/test_bf16_lazy_reorder.py +++ b/tests/cpu/test_bf16_lazy_reorder.py @@ -12,7 +12,7 @@ import sys import itertools import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_emb.py b/tests/cpu/test_emb.py index 64c92d27b..8a64337ab 100644 --- a/tests/cpu/test_emb.py +++ b/tests/cpu/test_emb.py @@ -1,6 +1,6 @@ import torch import torch.nn as nn -import intel_pytorch_extension as ipex +import torch_ipex as ipex import unittest import copy from common_utils import TestCase diff --git a/tests/cpu/test_int8.py b/tests/cpu/test_int8.py index 975f0fb36..f91efe7fc 100644 --- a/tests/cpu/test_int8.py +++ b/tests/cpu/test_int8.py @@ -15,7 +15,7 @@ from torch.jit._recursive import wrap_cpp_module import copy -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn from torch.nn import Parameter @@ -191,13 +191,13 @@ def _lstm_int8(self, seq_len, batch_size, input_size, hidden_size, num_layers, b def test_lstm(self): self._lstm_int8(seq_len=5, batch_size=2, input_size=16, hidden_size=16, num_layers=1, bidirectional=False, bias=True, empty_state=False) - + self._lstm_int8(seq_len=5, batch_size=2, input_size=16, hidden_size=16, num_layers=1, bidirectional=True, bias=True, empty_state=False) - + self._lstm_int8(seq_len=5, batch_size=2, input_size=16, hidden_size=16, num_layers=1, bidirectional=False, bias=False, empty_state=False) - + self._lstm_int8(seq_len=5, batch_size=2, input_size=16, hidden_size=16, num_layers=1, bidirectional=True, bias=False, empty_state=False) - + if __name__ == '__main__': rand_seed = int(time.time() * 1000000000) torch.manual_seed(rand_seed) diff --git a/tests/cpu/test_interaction.py b/tests/cpu/test_interaction.py index 8904fdd37..a8d12ef56 100644 --- a/tests/cpu/test_interaction.py +++ b/tests/cpu/test_interaction.py @@ -5,7 +5,7 @@ import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_jit.py b/tests/cpu/test_jit.py index 9d61d781b..3a73cc6a3 100644 --- a/tests/cpu/test_jit.py +++ b/tests/cpu/test_jit.py @@ -60,8 +60,8 @@ from torch.jit._recursive import wrap_cpp_module import copy -import intel_pytorch_extension as ipex -from intel_pytorch_extension import core +import torch_ipex as ipex +from torch_ipex import core import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_lazy_reorder.py b/tests/cpu/test_lazy_reorder.py index 56717a314..5cf9d8c4c 100644 --- a/tests/cpu/test_lazy_reorder.py +++ b/tests/cpu/test_lazy_reorder.py @@ -12,7 +12,7 @@ import sys import itertools import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import contextlib import io diff --git a/tests/cpu/test_mlp.py b/tests/cpu/test_mlp.py index 62d085095..f01b9d4b1 100644 --- a/tests/cpu/test_mlp.py +++ b/tests/cpu/test_mlp.py @@ -5,7 +5,7 @@ from functools import reduce import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn import torch.backends.cudnn as cudnn diff --git a/tests/cpu/test_rn50_cpu_ops.py b/tests/cpu/test_rn50_cpu_ops.py index a43db2bd7..586503e5b 100644 --- a/tests/cpu/test_rn50_cpu_ops.py +++ b/tests/cpu/test_rn50_cpu_ops.py @@ -55,7 +55,7 @@ from functools import reduce import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex from common_ipex_conf import AutoMixPrecision, AutoDNNL import torch.nn as nn diff --git a/tests/cpu/test_sparse.py b/tests/cpu/test_sparse.py index 6b89ebc23..53f494d7c 100644 --- a/tests/cpu/test_sparse.py +++ b/tests/cpu/test_sparse.py @@ -2,7 +2,7 @@ import copy import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch.nn as nn from common_utils import TestCase from numbers import Number diff --git a/tests/cpu/test_torch.py b/tests/cpu/test_torch.py index 206b0e962..9063e2aa0 100644 --- a/tests/cpu/test_torch.py +++ b/tests/cpu/test_torch.py @@ -83,7 +83,7 @@ skipIf, skipCPUIfNoLapack, skipCUDAIfNoMagma, skipCUDAIfRocm, onlyCUDA, onlyCPU, \ dtypes, dtypesIfCUDA, deviceCountAtLeast, skipCUDAIf, precisionOverride, ipex import torch.backends.quantized -import intel_pytorch_extension as ipex +import torch_ipex as ipex # load_tests from common_utils is used to automatically filter tests for diff --git a/tests/cpu/utils/test_lazy_reorder_with_pattern.py b/tests/cpu/utils/test_lazy_reorder_with_pattern.py index fcbeafc6a..1e2237fc8 100644 --- a/tests/cpu/utils/test_lazy_reorder_with_pattern.py +++ b/tests/cpu/utils/test_lazy_reorder_with_pattern.py @@ -5,7 +5,7 @@ import sys import unittest import torch -import intel_pytorch_extension as ipex +import torch_ipex as ipex sys.path.append("..") from common_utils import TestCase diff --git a/tests/cpu/utils/utils.py b/tests/cpu/utils/utils.py index 7e754a353..4a6b13885 100644 --- a/tests/cpu/utils/utils.py +++ b/tests/cpu/utils/utils.py @@ -2,7 +2,7 @@ import unittest from torch.testing._internal import expecttest from functools import wraps -import intel_pytorch_extension as ipex +import torch_ipex as ipex class VerboseTestCase(expecttest.TestCase): def __init__(self, method_name='runTest'): diff --git a/third_party/pybind11 b/third_party/pybind11 deleted file mode 160000 index 373524912..000000000 --- a/third_party/pybind11 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 37352491225358b97ce302273bf2d887a477efb0 diff --git a/torch_ipex/__init__.py b/torch_ipex/__init__.py index c80463529..294f2c211 100644 --- a/torch_ipex/__init__.py +++ b/torch_ipex/__init__.py @@ -11,7 +11,7 @@ if base_dir == 'intel_pytorch_extension': print('[WARNING] "import intel_pytorch_extension" will be deprecated in future releases. Please use "import torch_ipex" instead.') -core.enable_torch_ccl() +#core.enable_torch_ccl() DEVICE = 'xpu:0' class AmpConf(object): diff --git a/torch_ipex/csrc/CMakeLists.txt b/torch_ipex/csrc/CMakeLists.txt index 48c1dc013..1e5122e34 100644 --- a/torch_ipex/csrc/CMakeLists.txt +++ b/torch_ipex/csrc/CMakeLists.txt @@ -5,8 +5,6 @@ LIST(APPEND DPCPP_COMMON_SRCS ${DPCPP_ROOT}/aten_ipex_bridge.cpp ${DPCPP_ROOT}/aten_ipex_type.cpp ${DPCPP_ROOT}/dpcpp_allocator.cpp - # ${DPCPP_ROOT}/init_python_bindings.cpp - ${DPCPP_ROOT}/py_init.cpp ${DPCPP_ROOT}/ipex_tensor_impl.cpp ${DPCPP_ROOT}/ipex_sparse_tensor_impl.cpp ${DPCPP_ROOT}/version.cpp diff --git a/torch_ipex/csrc/_C.cpp b/torch_ipex/csrc/_C.cpp index 333a27b4b..7a9f2c59d 100644 --- a/torch_ipex/csrc/_C.cpp +++ b/torch_ipex/csrc/_C.cpp @@ -1,5 +1,261 @@ -#include "py_init.h" +#include "version.h" -PYBIND11_MODULE(_C, m) { - torch_ipex::InitIpexBindings(m); -} \ No newline at end of file +#include +#include +#include +#include + +#include +#include +#include +#include +#include "jit/fusion_pass.h" + +#include +#include +#include +#include + +#include "aten_ipex_type.h" +#include "utils.h" +#include "auto_opt_config.h" + +#include "cpu/dil/dil.hpp" +#include "cpu/dbl/Common.h" +#include "cpu/ShadeDataContext.h" +#include "cpu/ExtendOPs.h" +#include "cpu/MlpOPs.h" +#include "cpu/ExternalOPs.h" +#include "cpu/FusionOPs.h" +#include "cpu/int8/Config.h" +#include "cpu/int8/quantization/Observer.h" +#include "ProcessGroupCCL.hpp" +#include +#include +#include + +namespace torch_ipex { +namespace { + +py::object GetRevisions() { + auto py_dict = py::dict(); + py_dict["ipex"] = std::string(IPEX_GITREV); + py_dict["torch"] = std::string(TORCH_GITREV); + return py_dict; +} + +void setAutoDNNL(bool val) { + AutoOptConfig::singleton().set_auto_dnnl(val); +} + +void setParameterTensor(const at::Tensor &tensor) { + cpu::ShadeDataContext::setParameterTensor(tensor); +} + +bool isParameterTensor(const at::Tensor &tensor) { + return cpu::ShadeDataContext::isParameterTensor(tensor); +} + +/// **** Only for unit test **** +bool isDilTensor(const at::Tensor &tensor) { + return cpu::ShadeDataContext::isDilTensor(tensor); +} + +bool isINT8DilTensor(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_data_type() == dil::data_type::s8 + || dil_tensor.get_data_type() == dil::data_type::u8; + } + + return false; +} + +bool isBF16DilTensor(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_data_type() == dil::data_type::bf16; + } + + return false; +} + +bool isFP32DilTensor(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_data_type() == dil::data_type::f32; + } + + return false; +} + +dil::dims getDilStorageSizes(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_dims(); + } + return dil::dims(); +} + +dil::dims getDilStorageStrides(const at::Tensor &tensor) { + if (isDilTensor(tensor)) { + auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); + return dil_tensor.get_strides(); + } + return dil::dims(); +} + +void reorder_to_float32(at::Tensor &tensor){ + cpu::dbl::comm::reorder_to_dtype(tensor, at::kFloat); +} +/// **************************** + +void InitIpexModuleBindings(py::module m) { + m.def("_get_git_revs", []() { return GetRevisions(); }); + m.def("enable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(true); }); + m.def("disable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(false); }); + m.def("get_auto_dnnl", []() { return AutoOptConfig::singleton().get_auto_dnnl(); }); + m.def("enable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(true); }); + m.def("disable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(false); }); + m.def("get_mix_bf16_fp32", []() { return AutoOptConfig::singleton().get_mix_bf16_fp32(); }); + m.def("packed_add_", + [](at::Tensor &top_half, at::Tensor &bot_half, + const at::Tensor &grad, float alpha) { + AtenIpexTypeExt::packed_add_(top_half, bot_half, grad, alpha); + }); + m.def("mlp_forward", &AtenIpexTypeMLPExt::forward); + m.def("mlp_backward", &AtenIpexTypeMLPExt::backward); + m.def("mlp_create_handle", &AtenIpexTypeMLPExt::create_handle); + m.def("mlp_set_relu_mask", &AtenIpexTypeMLPExt::set_relu_mask); + m.def("mlp_release_handle", &AtenIpexTypeMLPExt::release_handle); + m.def("is_dil_tensor", &isDilTensor); + m.def("is_int8_dil_tensor", &isINT8DilTensor); + m.def("is_bf16_dil_tensor", &isBF16DilTensor); + m.def("is_fp32_dil_tensor", &isFP32DilTensor); + m.def("get_dil_tensor_sizes", &getDilStorageSizes); + m.def("get_dil_tensor_strides", &getDilStorageStrides); + m.def("set_parameter_tensor", &setParameterTensor); + m.def("is_parameter_tensor", &isParameterTensor); + m.def("reorder_to_float32", &reorder_to_float32); + m.def("enable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(true); }); + m.def("disable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(false); }); + m.def("get_jit_opt", []() { return AutoOptConfig::singleton().get_jit_fuse(); }); + m.def("set_execution_mode", [](bool train) { AutoOptConfig::singleton().set_train(train); }, py::arg("train")); + m.def("get_train", []() { return AutoOptConfig::singleton().get_train(); }); + + // int8 path + + m.def("enable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(true); }); + m.def("disable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(false); }); + m.def("get_mix_int8_fp32", []() { return AutoOptConfig::singleton().get_mix_int8_fp32(); }); + m.def("enable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(true); }); + m.def("disable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(false); }); + m.def("get_int8_calibration", + []() { AutoOptConfig::singleton().get_int8_calibration(); }); + m.def("calibration_reset", []() { Int8OptConfig::calibration_reset(); }); + m.def("add_indicators", + []() { Int8OptConfig::get_config().add_indicators(); }); + m.def("clear_indicators", + []() { Int8OptConfig::get_config().clear_indicators(); }); + // clear indicators for case having many scopes which have different structure + m.def("get_int8_configures", []() { + py::list output_list; + auto indicators = Int8OptConfig::get_config().get_indicators(); + IPEX_CHECK(indicators.size() > 0, "can't load a empty indicators, please first do calibration step"); + for (auto indicator: indicators) { + py::dict d; + d["id"] = indicator.get_indicator_id(); + d["name"] = indicator.get_indicator_name(); + d["algorithm"] = indicator.get_indicator_algorithm(); + d["weight_granularity"] = indicator.get_indicator_weight_granularity(); + std::vector i_scale, o_scale; + std::tie(i_scale, o_scale) = indicator.get_indicator_scales(); + d["inputs_scale"] = i_scale; + d["outputs_scale"] = o_scale; + std::vector i_zero_point, o_zero_point; + std::tie(i_zero_point, o_zero_point) = indicator.get_indicator_zero_point(); + d["inputs_zero_point"] = i_zero_point; + d["outputs_zero_point"] = o_zero_point; + std::vector i_uint8_used, o_uint8_used; + std::tie(i_uint8_used, o_uint8_used)= indicator.get_indicator_uint8_status(); + d["inputs_uint8_used"] = i_uint8_used; + d["outputs_uint8_used"] = o_uint8_used; + d["quantized"] = indicator.get_indicator_quantized_status(); + output_list.append(d); + } + return output_list; } ); + m.def("load_indicators_file", [](const py::list &l) { + IPEX_CHECK( + py::len(l) > 0, + "can't load a empty configures, please first do calibration step"); + std::vector indicators; + for (py::handle i : l) { + int64_t id = py::cast(i["id"]); + std::string op_name = py::cast(i["name"]); + std::string algorithm = py::cast(i["algorithm"]); + std::string weight_granularity = + py::cast(i["weight_granularity"]); + std::vector i_scale = + py::cast>(i["inputs_scale"]); + std::vector o_scale = + py::cast>(i["outputs_scale"]); + + // TODO: what should be the default value here? different for u8 and s8 + std::vector i_zero_point = {0}; + std::vector o_zero_point = {0}; + if (i.contains("inputs_zero_point")) { + i_zero_point = py::cast>(i["inputs_zero_point"]); + } + if (i.contains("outputs_zero_point")) { + o_zero_point = py::cast>(i["outputs_zero_point"]); + } + + std::vector i_uint8_used = + py::cast>(i["inputs_uint8_used"]); + std::vector o_uint8_used = + py::cast>(i["outputs_uint8_used"]); + bool quantized = py::cast(i["quantized"]); + Indicator temp(id, op_name, algorithm, weight_granularity, i_scale, + o_scale, i_uint8_used, o_uint8_used, quantized, i_zero_point, o_zero_point); + indicators.push_back(temp); + } + Int8OptConfig::get_config().set_indicators(indicators); + }); + + m.def("enable_torch_ccl", [=]() { + py::object module = py::module::import("torch.distributed"); + py::object register_backend = module.attr("Backend").attr("register_backend"); + register_backend("ccl", py::cpp_function(&c10d::ProcessGroupCCL::createProcessGroupCCL, + py::arg("store"), + py::arg("rank"), + py::arg("size"), + py::arg("timeout") = std::chrono::milliseconds( + ::c10d::ProcessGroupCCL::OP_TIMEOUT_MILLIS))); + + }); + m.def("set_xpu_mode", [=](std::string mode){ + AutoOptConfig::singleton().set_xpu_mode(torch_ipex::stringToXPUMode(mode));}); + + // external OPs + m.def("roi_align_forward", &IpexExternal::ROIAlign_forward); + m.def("roi_align_backward", &IpexExternal::ROIAlign_backward); + m.def("nms", &IpexExternal::nms); + m.def("batch_score_nms", &IpexExternal::batch_score_nms); + m.def("linear_relu", &AtenIpexTypeExt::linear_relu); +} +} // namespace +using namespace torch::jit; + +void InitIpexBindings(py::module m) { + InitIpexModuleBindings(m); + // jit fusion pass + torch::jit::registerPrePass([](std::shared_ptr& g) { + if (AutoOptConfig::singleton().get_jit_fuse()) { + torch::jit::FusionPass(g); + } + }); +} + +} // namespace torch_ipex + +PYBIND11_MODULE(_C, m) { torch_ipex::InitIpexBindings(m); } diff --git a/torch_ipex/csrc/init_python_bindings.cpp b/torch_ipex/csrc/init_python_bindings.cpp deleted file mode 100644 index 139109133..000000000 --- a/torch_ipex/csrc/init_python_bindings.cpp +++ /dev/null @@ -1,263 +0,0 @@ -#include "init_python_bindings.h" -#include "version.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include "jit/fusion_pass.h" - -#include -#include -#include -#include - -#include "aten_ipex_type.h" -#include "utils.h" -#include "auto_opt_config.h" - -#include "cpu/dil/dil.hpp" -#include "cpu/dbl/Common.h" -#include "cpu/ShadeDataContext.h" -#include "cpu/ExtendOPs.h" -#include "cpu/MlpOPs.h" -#include "cpu/ExternalOPs.h" -#include "cpu/FusionOPs.h" -#include "cpu/int8/Config.h" -#include "cpu/int8/quantization/Observer.h" -#include "ProcessGroupCCL.hpp" -#include -#include -#include -#include - -namespace torch_ipex { -namespace { - -py::object GetRevisions() { - auto py_dict = py::dict(); - py_dict["ipex"] = std::string(IPEX_GITREV); - py_dict["torch"] = std::string(TORCH_GITREV); - return py_dict; -} - -void setAutoDNNL(bool val) { - AutoOptConfig::singleton().set_auto_dnnl(val); -} - -void setParameterTensor(const at::Tensor &tensor) { - cpu::ShadeDataContext::setParameterTensor(tensor); -} - -bool isParameterTensor(const at::Tensor &tensor) { - return cpu::ShadeDataContext::isParameterTensor(tensor); -} - -/// **** Only for unit test **** -bool isDilTensor(const at::Tensor &tensor) { - return cpu::ShadeDataContext::isDilTensor(tensor); -} - -bool isINT8DilTensor(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_data_type() == dil::data_type::s8 - || dil_tensor.get_data_type() == dil::data_type::u8; - } - - return false; -} - -bool isBF16DilTensor(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_data_type() == dil::data_type::bf16; - } - - return false; -} - -bool isFP32DilTensor(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_data_type() == dil::data_type::f32; - } - - return false; -} - -dil::dims getDilStorageSizes(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_dims(); - } - return dil::dims(); -} - -dil::dims getDilStorageStrides(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_strides(); - } - return dil::dims(); -} - -void reorder_to_float32(at::Tensor &tensor){ - cpu::dbl::comm::reorder_to_dtype(tensor, at::kFloat); -} -/// **************************** - -void InitIpexModuleBindings(py::module m) { - m.def("_get_git_revs", []() { return GetRevisions(); }); - m.def("enable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(true); }); - m.def("disable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(false); }); - m.def("get_auto_dnnl", []() { return AutoOptConfig::singleton().get_auto_dnnl(); }); - m.def("enable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(true); }); - m.def("disable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(false); }); - m.def("get_mix_bf16_fp32", []() { return AutoOptConfig::singleton().get_mix_bf16_fp32(); }); - m.def("packed_add_", - [](at::Tensor &top_half, at::Tensor &bot_half, - const at::Tensor &grad, float alpha) { - AtenIpexTypeExt::packed_add_(top_half, bot_half, grad, alpha); - }); - m.def("mlp_forward", &AtenIpexTypeMLPExt::forward); - m.def("mlp_backward", &AtenIpexTypeMLPExt::backward); - m.def("mlp_create_handle", &AtenIpexTypeMLPExt::create_handle); - m.def("mlp_set_relu_mask", &AtenIpexTypeMLPExt::set_relu_mask); - m.def("mlp_release_handle", &AtenIpexTypeMLPExt::release_handle); - m.def("is_dil_tensor", &isDilTensor); - m.def("is_int8_dil_tensor", &isINT8DilTensor); - m.def("is_bf16_dil_tensor", &isBF16DilTensor); - m.def("is_fp32_dil_tensor", &isFP32DilTensor); - m.def("get_dil_tensor_sizes", &getDilStorageSizes); - m.def("get_dil_tensor_strides", &getDilStorageStrides); - m.def("set_parameter_tensor", &setParameterTensor); - m.def("is_parameter_tensor", &isParameterTensor); - m.def("reorder_to_float32", &reorder_to_float32); - m.def("enable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(true); }); - m.def("disable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(false); }); - m.def("get_jit_opt", []() { return AutoOptConfig::singleton().get_jit_fuse(); }); - m.def("set_execution_mode", [](bool train) { AutoOptConfig::singleton().set_train(train); }, py::arg("train")); - m.def("get_train", []() { return AutoOptConfig::singleton().get_train(); }); - - // int8 path - - m.def("enable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(true); }); - m.def("disable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(false); }); - m.def("get_mix_int8_fp32", []() { return AutoOptConfig::singleton().get_mix_int8_fp32(); }); - m.def("enable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(true); }); - m.def("disable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(false); }); - m.def("get_int8_calibration", - []() { AutoOptConfig::singleton().get_int8_calibration(); }); - m.def("calibration_reset", []() { Int8OptConfig::calibration_reset(); }); - m.def("add_indicators", - []() { Int8OptConfig::get_config().add_indicators(); }); - m.def("clear_indicators", - []() { Int8OptConfig::get_config().clear_indicators(); }); - // clear indicators for case having many scopes which have different structure - m.def("get_int8_configures", []() { - py::list output_list; - auto indicators = Int8OptConfig::get_config().get_indicators(); - IPEX_CHECK(indicators.size() > 0, "can't load a empty indicators, please first do calibration step"); - for (auto indicator: indicators) { - py::dict d; - d["id"] = indicator.get_indicator_id(); - d["name"] = indicator.get_indicator_name(); - d["algorithm"] = indicator.get_indicator_algorithm(); - d["weight_granularity"] = indicator.get_indicator_weight_granularity(); - std::vector i_scale, o_scale; - std::tie(i_scale, o_scale) = indicator.get_indicator_scales(); - d["inputs_scale"] = i_scale; - d["outputs_scale"] = o_scale; - std::vector i_zero_point, o_zero_point; - std::tie(i_zero_point, o_zero_point) = indicator.get_indicator_zero_point(); - d["inputs_zero_point"] = i_zero_point; - d["outputs_zero_point"] = o_zero_point; - std::vector i_uint8_used, o_uint8_used; - std::tie(i_uint8_used, o_uint8_used)= indicator.get_indicator_uint8_status(); - d["inputs_uint8_used"] = i_uint8_used; - d["outputs_uint8_used"] = o_uint8_used; - d["quantized"] = indicator.get_indicator_quantized_status(); - output_list.append(d); - } - return output_list; } ); - m.def("load_indicators_file", [](const py::list &l) { - IPEX_CHECK( - py::len(l) > 0, - "can't load a empty configures, please first do calibration step"); - std::vector indicators; - for (py::handle i : l) { - int64_t id = py::cast(i["id"]); - std::string op_name = py::cast(i["name"]); - std::string algorithm = py::cast(i["algorithm"]); - std::string weight_granularity = - py::cast(i["weight_granularity"]); - std::vector i_scale = - py::cast>(i["inputs_scale"]); - std::vector o_scale = - py::cast>(i["outputs_scale"]); - - // TODO: what should be the default value here? different for u8 and s8 - std::vector i_zero_point = {0}; - std::vector o_zero_point = {0}; - if (i.contains("inputs_zero_point")) { - i_zero_point = py::cast>(i["inputs_zero_point"]); - } - if (i.contains("outputs_zero_point")) { - o_zero_point = py::cast>(i["outputs_zero_point"]); - } - - std::vector i_uint8_used = - py::cast>(i["inputs_uint8_used"]); - std::vector o_uint8_used = - py::cast>(i["outputs_uint8_used"]); - bool quantized = py::cast(i["quantized"]); - Indicator temp(id, op_name, algorithm, weight_granularity, i_scale, - o_scale, i_uint8_used, o_uint8_used, quantized, i_zero_point, o_zero_point); - indicators.push_back(temp); - } - Int8OptConfig::get_config().set_indicators(indicators); - }); - - m.def("enable_torch_ccl", [=]() { - py::object module = py::module::import("torch.distributed"); - py::object register_backend = module.attr("Backend").attr("register_backend"); - register_backend("ccl", py::cpp_function(&c10d::ProcessGroupCCL::createProcessGroupCCL, - py::arg("store"), - py::arg("rank"), - py::arg("size"), - py::arg("timeout") = std::chrono::milliseconds( - ::c10d::ProcessGroupCCL::OP_TIMEOUT_MILLIS))); - - }); - m.def("set_xpu_mode", [=](std::string mode){ - AutoOptConfig::singleton().set_xpu_mode(torch_ipex::stringToXPUMode(mode));}); - - // external OPs - m.def("roi_align_forward", &IpexExternal::ROIAlign_forward); - m.def("roi_align_backward", &IpexExternal::ROIAlign_backward); - m.def("nms", &IpexExternal::nms); - m.def("batch_score_nms", &IpexExternal::batch_score_nms); - m.def("linear_relu", &AtenIpexTypeExt::linear_relu); -} -} // namespace -using namespace torch::jit; - -void InitIpexBindings(py::module m) { - InitIpexModuleBindings(m); - // jit fusion pass - torch::jit::registerPrePass([](std::shared_ptr& g) { - if (AutoOptConfig::singleton().get_jit_fuse()) { - torch::jit::FusionPass(g); - } - }); -} - -} // namespace torch_ipex - -PYBIND11_MODULE(_torch_ipex, m) { torch_ipex::InitIpexBindings(m); } diff --git a/torch_ipex/csrc/init_python_bindings.h b/torch_ipex/csrc/init_python_bindings.h deleted file mode 100644 index f0ee26e9a..000000000 --- a/torch_ipex/csrc/init_python_bindings.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -namespace py = pybind11; - -namespace torch_ipex { - -// Initialize bindings for IPE module, tensor and optimization passes. -void InitIpexBindings(py::module m); - -} // namespace torch_ipex diff --git a/torch_ipex/csrc/py_init.cpp b/torch_ipex/csrc/py_init.cpp deleted file mode 100644 index 32b8eeec5..000000000 --- a/torch_ipex/csrc/py_init.cpp +++ /dev/null @@ -1,258 +0,0 @@ -#include -#include "version.h" - -#include -#include -#include - -#include -#include -#include -#include -#include "jit/fusion_pass.h" - -#include -#include -#include -#include - -#include "aten_ipex_type.h" -#include "utils.h" -#include "auto_opt_config.h" - -#include "cpu/dil/dil.hpp" -#include "cpu/dbl/Common.h" -#include "cpu/ShadeDataContext.h" -#include "cpu/ExtendOPs.h" -#include "cpu/MlpOPs.h" -#include "cpu/ExternalOPs.h" -#include "cpu/FusionOPs.h" -#include "cpu/int8/Config.h" -#include "cpu/int8/quantization/Observer.h" -#include "ProcessGroupCCL.hpp" -#include - -namespace torch_ipex { -// namespace { - -py::object GetRevisions() { - auto py_dict = py::dict(); - py_dict["ipex"] = std::string(IPEX_GITREV); - py_dict["torch"] = std::string(TORCH_GITREV); - return py_dict; -} - -void setAutoDNNL(bool val) { - AutoOptConfig::singleton().set_auto_dnnl(val); -} - -void setParameterTensor(const at::Tensor &tensor) { - cpu::ShadeDataContext::setParameterTensor(tensor); -} - -bool isParameterTensor(const at::Tensor &tensor) { - return cpu::ShadeDataContext::isParameterTensor(tensor); -} - -/// **** Only for unit test **** -bool isDilTensor(const at::Tensor &tensor) { - return cpu::ShadeDataContext::isDilTensor(tensor); -} - -bool isINT8DilTensor(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_data_type() == dil::data_type::s8 - || dil_tensor.get_data_type() == dil::data_type::u8; - } - - return false; -} - -bool isBF16DilTensor(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_data_type() == dil::data_type::bf16; - } - - return false; -} - -bool isFP32DilTensor(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_data_type() == dil::data_type::f32; - } - - return false; -} - -dil::dims getDilStorageSizes(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_dims(); - } - return dil::dims(); -} - -dil::dims getDilStorageStrides(const at::Tensor &tensor) { - if (isDilTensor(tensor)) { - auto dil_tensor = cpu::ShadeDataContext::getDilStorage(tensor); - return dil_tensor.get_strides(); - } - return dil::dims(); -} - -void reorder_to_float32(at::Tensor &tensor){ - cpu::dbl::comm::reorder_to_dtype(tensor, at::kFloat); -} -/// **************************** - -void InitIpexModuleBindings(py::module m) { - m.def("_get_git_revs", []() { return GetRevisions(); }); - m.def("enable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(true); }); - m.def("disable_auto_dnnl", []() { AutoOptConfig::singleton().set_auto_dnnl(false); }); - m.def("get_auto_dnnl", []() { return AutoOptConfig::singleton().get_auto_dnnl(); }); - m.def("enable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(true); }); - m.def("disable_mix_bf16_fp32", []() { AutoOptConfig::singleton().set_mix_bf16_fp32(false); }); - m.def("get_mix_bf16_fp32", []() { return AutoOptConfig::singleton().get_mix_bf16_fp32(); }); - m.def("packed_add_", - [](at::Tensor &top_half, at::Tensor &bot_half, - const at::Tensor &grad, float alpha) { - AtenIpexTypeExt::packed_add_(top_half, bot_half, grad, alpha); - }); - m.def("mlp_forward", &AtenIpexTypeMLPExt::forward); - m.def("mlp_backward", &AtenIpexTypeMLPExt::backward); - m.def("mlp_create_handle", &AtenIpexTypeMLPExt::create_handle); - m.def("mlp_set_relu_mask", &AtenIpexTypeMLPExt::set_relu_mask); - m.def("mlp_release_handle", &AtenIpexTypeMLPExt::release_handle); - m.def("is_dil_tensor", &isDilTensor); - m.def("is_int8_dil_tensor", &isINT8DilTensor); - m.def("is_bf16_dil_tensor", &isBF16DilTensor); - m.def("is_fp32_dil_tensor", &isFP32DilTensor); - m.def("get_dil_tensor_sizes", &getDilStorageSizes); - m.def("get_dil_tensor_strides", &getDilStorageStrides); - m.def("set_parameter_tensor", &setParameterTensor); - m.def("is_parameter_tensor", &isParameterTensor); - m.def("reorder_to_float32", &reorder_to_float32); - m.def("enable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(true); }); - m.def("disable_jit_opt", []() { AutoOptConfig::singleton().set_jit_fuse(false); }); - m.def("get_jit_opt", []() { return AutoOptConfig::singleton().get_jit_fuse(); }); - m.def("set_execution_mode", [](bool train) { AutoOptConfig::singleton().set_train(train); }, py::arg("train")); - m.def("get_train", []() { return AutoOptConfig::singleton().get_train(); }); - - // int8 path - - m.def("enable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(true); }); - m.def("disable_mix_int8_fp32", []() { AutoOptConfig::singleton().set_mix_int8_fp32(false); }); - m.def("get_mix_int8_fp32", []() { return AutoOptConfig::singleton().get_mix_int8_fp32(); }); - m.def("enable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(true); }); - m.def("disable_int8_calibration", []() { AutoOptConfig::singleton().set_int8_calibration(false); }); - m.def("get_int8_calibration", - []() { AutoOptConfig::singleton().get_int8_calibration(); }); - m.def("calibration_reset", []() { Int8OptConfig::calibration_reset(); }); - m.def("add_indicators", - []() { Int8OptConfig::get_config().add_indicators(); }); - m.def("clear_indicators", - []() { Int8OptConfig::get_config().clear_indicators(); }); - // clear indicators for case having many scopes which have different structure - m.def("get_int8_configures", []() { - py::list output_list; - auto indicators = Int8OptConfig::get_config().get_indicators(); - IPEX_CHECK(indicators.size() > 0, "can't load a empty indicators, please first do calibration step"); - for (auto indicator: indicators) { - py::dict d; - d["id"] = indicator.get_indicator_id(); - d["name"] = indicator.get_indicator_name(); - d["algorithm"] = indicator.get_indicator_algorithm(); - d["weight_granularity"] = indicator.get_indicator_weight_granularity(); - std::vector i_scale, o_scale; - std::tie(i_scale, o_scale) = indicator.get_indicator_scales(); - d["inputs_scale"] = i_scale; - d["outputs_scale"] = o_scale; - std::vector i_zero_point, o_zero_point; - std::tie(i_zero_point, o_zero_point) = indicator.get_indicator_zero_point(); - d["inputs_zero_point"] = i_zero_point; - d["outputs_zero_point"] = o_zero_point; - std::vector i_uint8_used, o_uint8_used; - std::tie(i_uint8_used, o_uint8_used)= indicator.get_indicator_uint8_status(); - d["inputs_uint8_used"] = i_uint8_used; - d["outputs_uint8_used"] = o_uint8_used; - d["quantized"] = indicator.get_indicator_quantized_status(); - output_list.append(d); - } - return output_list; } ); - m.def("load_indicators_file", [](const py::list &l) { - IPEX_CHECK( - py::len(l) > 0, - "can't load a empty configures, please first do calibration step"); - std::vector indicators; - for (py::handle i : l) { - int64_t id = py::cast(i["id"]); - std::string op_name = py::cast(i["name"]); - std::string algorithm = py::cast(i["algorithm"]); - std::string weight_granularity = - py::cast(i["weight_granularity"]); - std::vector i_scale = - py::cast>(i["inputs_scale"]); - std::vector o_scale = - py::cast>(i["outputs_scale"]); - - // TODO: what should be the default value here? different for u8 and s8 - std::vector i_zero_point = {0}; - std::vector o_zero_point = {0}; - if (i.contains("inputs_zero_point")) { - i_zero_point = py::cast>(i["inputs_zero_point"]); - } - if (i.contains("outputs_zero_point")) { - o_zero_point = py::cast>(i["outputs_zero_point"]); - } - - std::vector i_uint8_used = - py::cast>(i["inputs_uint8_used"]); - std::vector o_uint8_used = - py::cast>(i["outputs_uint8_used"]); - bool quantized = py::cast(i["quantized"]); - Indicator temp(id, op_name, algorithm, weight_granularity, i_scale, - o_scale, i_uint8_used, o_uint8_used, quantized, i_zero_point, o_zero_point); - indicators.push_back(temp); - } - Int8OptConfig::get_config().set_indicators(indicators); - }); - - m.def("enable_torch_ccl", [=]() { - py::object module = py::module::import("torch.distributed"); - py::object register_backend = module.attr("Backend").attr("register_backend"); - register_backend("ccl", py::cpp_function(&c10d::ProcessGroupCCL::createProcessGroupCCL, - py::arg("store"), - py::arg("rank"), - py::arg("size"), - py::arg("timeout") = std::chrono::milliseconds( - ::c10d::ProcessGroupCCL::OP_TIMEOUT_MILLIS))); - - }); - m.def("set_xpu_mode", [=](std::string mode){ - AutoOptConfig::singleton().set_xpu_mode(torch_ipex::stringToXPUMode(mode));}); - - // external OPs - m.def("roi_align_forward", &IpexExternal::ROIAlign_forward); - m.def("roi_align_backward", &IpexExternal::ROIAlign_backward); - m.def("nms", &IpexExternal::nms); - m.def("batch_score_nms", &IpexExternal::batch_score_nms); - m.def("linear_relu", &AtenIpexTypeExt::linear_relu); -} - -// } // namespace -using namespace torch::jit; - -__attribute__ ((visibility ("default"))) void InitIpexBindings(py::module m) { - InitIpexModuleBindings(m); - // jit fusion pass - torch::jit::registerPrePass([](std::shared_ptr& g) { - if (AutoOptConfig::singleton().get_jit_fuse()) { - torch::jit::FusionPass(g); - } - }); -} - -} // namespace torch_ipex \ No newline at end of file diff --git a/torch_ipex/csrc/py_init.h b/torch_ipex/csrc/py_init.h deleted file mode 100644 index 5c840dc91..000000000 --- a/torch_ipex/csrc/py_init.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -namespace py = pybind11; - -namespace torch_ipex { - -// Initialize bindings for IPE module, tensor and optimization passes. -void InitIpexBindings(py::module m); - -} // namespace torch_ipex \ No newline at end of file diff --git a/torch_ipex/launch.py b/torch_ipex/launch.py index 675bcacbd..fdd4ada88 100644 --- a/torch_ipex/launch.py +++ b/torch_ipex/launch.py @@ -17,51 +17,51 @@ r""" This is a script for launching PyTorch training and inference on Intel Xeon CPU with optimal configurations. -Now, single instance inference/training, multi-instance inference/training and distributed training +Now, single instance inference/training, multi-instance inference/training and distributed training with oneCCL backend is enabled. -To get the peak performance on Intel Xeon CPU, the script optimizes the configuration of thread and memory -management. For thread management, the script configures thread affinity and the preload of Intel OMP library. +To get the peak performance on Intel Xeon CPU, the script optimizes the configuration of thread and memory +management. For thread management, the script configures thread affinity and the preload of Intel OMP library. For memory management, it configures NUMA binding and preload optimized memory allocation library (e.g. tcmalloc, jemalloc). - + **How to use this module:** -*** Single instance inference/training *** +*** Single instance inference/training *** 1. Run single-instance inference or training on a single node with all CPU sockets. :: - >>> python -m intel_pytorch_extension.launch script.py args + >>> python -m torch_ipex.launch script.py args 2. Run single-instance inference or training on a single CPU socket. :: - >>> python -m intel_pytorch_extension.launch --socket_id 1 script.py args + >>> python -m torch_ipex.launch --socket_id 1 script.py args + +*** Multi-instance inference *** -*** Multi-instance inference *** +1. Multi-instance + By default, one instance per socket. if you want to set the instance numbers and core per instance, + --nintances and --ncore_per_instance should be set. -1. Multi-instance - By default, one instance per socket. if you want to set the instance numbers and core per instance, - --nintances and --ncore_per_instance should be set. - - >>> python -m intel_pytorch_extension.launch --multi_instance python_script args + >>> python -m torch_ipex.launch --multi_instance python_script args - eg: on CLX8280 with 14 instance, 4 cores per instance + eg: on CLX8280 with 14 instance, 4 cores per instance :: - >>> python -m intel_pytorch_extension.launch --multi_instance --nintances 14 --ncore_per_instance 4 python_script args + >>> python -m torch_ipex.launch --multi_instance --nintances 14 --ncore_per_instance 4 python_script args *** Distributed Training *** -spawns up multiple distributed training processes on each of the training nodes. For intel_pytorch_extension, oneCCL -is used as the communication backend and MPI used to launch multi-proc. To get the better -performance, you should specify the different cores for oneCCL communication and computation +spawns up multiple distributed training processes on each of the training nodes. For torch_ipex, oneCCL +is used as the communication backend and MPI used to launch multi-proc. To get the better +performance, you should specify the different cores for oneCCL communication and computation process seperately. This tool can automatically set these ENVs(such as I_MPI_PIN_DOMIN) and launch -multi-proc for you. +multi-proc for you. The utility can be used for single-node distributed training, in which one or more processes per node will be spawned. It can also be used in @@ -73,7 +73,7 @@ :: - >>> python -m intel_pytorch_extension.launch --distributed python_script --arg1 --arg2 --arg3 and all other + >>> python -m torch_ipex.launch --distributed python_script --arg1 --arg2 --arg3 and all other arguments of your training script 2. Multi-Node multi-process distributed training: (e.g. two nodes) @@ -83,8 +83,8 @@ :: - >>> python -m intel_pytorch_extension.launch --distributed --nproc_per_node=xxx - --nnodes=2 --hostfile hostfile python_sript --arg1 --arg2 --arg3 + >>> python -m torch_ipex.launch --distributed --nproc_per_node=xxx + --nnodes=2 --hostfile hostfile python_sript --arg1 --arg2 --arg3 and all other arguments of your training script) @@ -92,11 +92,11 @@ :: - >>> python -m intel_pytorch_extension.launch --help + >>> python -m torch_ipex.launch --help *** Memory allocator *** -"--enable_tcmalloc" and "--enable_jemalloc" can be used to enable different memory allcator. +"--enable_tcmalloc" and "--enable_jemalloc" can be used to enable different memory allcator. """ @@ -143,7 +143,7 @@ def physical_core_nums(self): def logical_core_nums(self): return len(self.socket_logical_cores) * len(self.socket_logical_cores[0]) - + def get_socket_physical_cores(self, socket_id): if socket_id < 0 or socket_id > self.sockets - 1: logger.error("Invalid socket id") @@ -156,14 +156,14 @@ def get_socket_logical_cores(self, socket_id): def get_all_physical_cores(self): return np.array(self.socket_physical_cores).flatten().tolist() - + def get_all_logical_cores(self): return np.array(self.socket_logical_cores).flatten().tolist() - + def set_mpi_pin_domain(args): ''' - I_MPI_PIN_DOMAIN specify the cores used for every MPI process. + I_MPI_PIN_DOMAIN specify the cores used for every MPI process. The first ccl_worker_count cores of every rank for ccl communication and the other cores will be used to do computation. For example: on CascadeLake 8280 CPU, 2 ranks on one node. ccl_worker_count=4 @@ -181,7 +181,7 @@ def set_mpi_pin_domain(args): for proc in range(ppn): domain_binary = 0 begin = proc * cores_per_rank + args.ccl_worker_count - end = proc * cores_per_rank + cores_per_rank -1 + end = proc * cores_per_rank + cores_per_rank -1 for i in range(begin, end + 1): domain_binary |= (1 << i) pin_domain += hex(domain_binary) + "," @@ -190,7 +190,7 @@ def set_mpi_pin_domain(args): def set_ccl_worker_affinity(args): ''' computation and communication use different cores when using oneCCL - backend for distributed training. we use first ccl_worker_count cores of + backend for distributed training. we use first ccl_worker_count cores of every rank for ccl communication ''' cpuinfo = CPUinfo() @@ -202,18 +202,18 @@ def set_ccl_worker_affinity(args): affinity = '' for proc in range(ppn): for ccl_worker in range(args.ccl_worker_count): - affinity += str(proc * cores_per_rank + ccl_worker)+ "," + affinity += str(proc * cores_per_rank + ccl_worker)+ "," os.environ["CCL_WORKER_AFFINITY"] = affinity def add_lib_preload(lib_type=None): ''' - Enale TCMalloc/JeMalloc/iomp + Enale TCMalloc/JeMalloc/iomp ''' library_paths = [] if "CONDA_PREFIX" in os.environ: library_paths.append(os.environ["CONDA_PREFIX"] + "/lib/") - + library_paths += ["{}/.local/lib/".format(expanduser("~")), "/usr/local/lib/", "/usr/local/lib64/", "/usr/lib/", "/usr/lib64/"] lib_find = False @@ -234,7 +234,7 @@ def set_memory_allocator(args): logger.error("Unable to enable TCMalloc and JEMalloc at the same time") exit(-1) - if args.enable_tcmalloc: + if args.enable_tcmalloc: find_tc = add_lib_preload(lib_type="tcmalloc") if not find_tc: logger.warning("Unable to find the {} library file lib{}.so in $CONDA_PREFIX/lib or /.local/lib/" @@ -261,38 +261,38 @@ def set_memory_allocator(args): find_tc = add_lib_preload(lib_type="tcmalloc") if find_tc: logger.info("Use TCMalloc memory allocator") - return + return find_je = add_lib_preload(lib_type="jemalloc") if find_je: logger.info("Use JeMallocl memory allocator") - return + return logger.warning("Both TCMalloc and JeMalloc are not fount in $CONDA_PREFIX/lib or /.local/lib/" " or /usr/local/lib/ or /usr/local/lib64/ or /usr/lib or /usr/lib64 or " "~/.local/lib/ so the LD_PRELOAD environment variable will not be set. This may drop the performance" .format(expanduser("~"))) - + def set_multi_thread_and_allcator(args): - + set_memory_allocator(args) if "OMP_NUM_THREADS" not in os.environ: os.environ["OMP_NUM_THREADS"] = str(args.ncore_per_instance) elif "OMP_NUM_THREADS" in os.environ: args.ncore_per_instance = int(os.environ["OMP_NUM_THREADS"]) - + if "KMP_AFFINITY" not in os.environ: os.environ["KMP_AFFINITY"] = args.kmp_affinity - + if "KMP_BLOCKTIME" not in os.environ: os.environ["KMP_BLOCKTIME"] = "1" - - if "DNNL_PRIMITIVE_CACHE_CAPACITY" not in os.environ: + + if "DNNL_PRIMITIVE_CACHE_CAPACITY" not in os.environ: os.environ["DNNL_PRIMITIVE_CACHE_CAPACITY"] = '1024' logger.info("OMP_NUM_THREADS={} ".format(os.environ["OMP_NUM_THREADS"])) logger.info("KMP_AFFINITY={}".format(os.environ["KMP_AFFINITY"])) logger.info("KMP_BLOCKTIME={}".format(os.environ["KMP_BLOCKTIME"])) logger.info("DNNL_PRIMITIVE_CACHE_CAPACITY={}".format(os.environ["DNNL_PRIMITIVE_CACHE_CAPACITY"])) - + if args.enable_iomp: find_iomp = add_lib_preload(lib_type="iomp") if not find_iomp: @@ -301,21 +301,21 @@ def set_multi_thread_and_allcator(args): "~/.local/lib/ so the LD_PRELOAD environment variable will not be set." .format("iomp", "iomp", expanduser("~"))) else: - logger.info("User iomp") - + logger.info("User iomp") + def launch(args): ''' - single-instance / multi-instance launcher - ''' + single-instance / multi-instance launcher + ''' processes = [] cores = [] - + cpuinfo = CPUinfo() if args.core_list:#user specify what cores will be used by params cores = args.core_list.strip().split(",") if args.ncore_per_instance == -1: logger.error("please specify the '--ncore_per_instance' if you have pass the --core_list params") - exit(-1) + exit(-1) elif args.ninstances > 1 and args.ncore_per_instance * args.ninstances < len(cores): logger.warning("only first {} cores will be used, but you specify {} cores in core_list".format (args.ncore_per_instance * args.ninstances, len(cores))) @@ -324,14 +324,14 @@ def launch(args): else: if args.use_logical_core: if args.socket_id != -1: - cores = cpuinfo.get_socket_logical_cores(args.socket_id) + cores = cpuinfo.get_socket_logical_cores(args.socket_id) else: - cores = cpuinfo.get_all_logical_cores() + cores = cpuinfo.get_all_logical_cores() else: if args.socket_id != -1: cores = cpuinfo.get_socket_physical_cores(args.socket_id) else: - cores = cpuinfo.get_all_physical_cores() + cores = cpuinfo.get_all_physical_cores() if not args.multi_instance and args.ninstances == -1 and args.ncore_per_instance == -1: args.ninstances = 1; args.ncore_per_instance = len(cores) @@ -383,8 +383,8 @@ def launch(args): process.wait() if process.returncode != 0: raise subprocess.CalledProcessError(returncode=process.returncode, - cmd=cmd) - + cmd=cmd) + def mpi_dist_launch(args): ''' Set ENVs and launch MPI process for distributed training. @@ -417,13 +417,13 @@ def mpi_dist_launch(args): if not master_check: logger.error("MASTER_ADDR is not right. Please make sure the first ip {} in your hostfile is the current node".format(ip_list[0])) exit(-1) - + logger.info("Begin to validate the ip connect") args.master_addr = ip_list[0] for ip in ip_list[1:]: completed_process = subprocess.run("ssh -o PasswordAuthentication=no {} ':'".format(ip), shell=True) if completed_process.returncode != 0: - logger.error("Passwordless SSH login to {} failed, please make sure you have setup SSH public key right") + logger.error("Passwordless SSH login to {} failed, please make sure you have setup SSH public key right") exit(-1) else: logger.info("connection from master node {} to slave node {} is OK".format(args.master_addr, ip)) @@ -436,12 +436,12 @@ def mpi_dist_launch(args): mpi_pin_domain = set_mpi_pin_domain(args) else: mpi_pin_domain = os.environ["I_MPI_PIN_DOMAIN"] - + cpuinfo = CPUinfo() - ppn = args.nproc_per_node + ppn = args.nproc_per_node total_cores = len(cpuinfo.get_all_physical_cores()) cores_per_rank = total_cores // ppn - + if "OMP_NUM_THREADS" not in os.environ: opm_num_threads = cores_per_rank - args.ccl_worker_count else: @@ -454,7 +454,7 @@ def mpi_dist_launch(args): if "CCL_ATL_TRANSPORT" not in os.environ: os.environ["CCL_ATL_TRANSPORT"] = "ofi" - + if args.enable_iomp: find_iomp = add_lib_preload(lib_type="iomp") if not find_iomp: @@ -494,7 +494,7 @@ def mpi_dist_launch(args): os.environ["LAUNCH_CMD"] = os.environ["LAUNCH_CMD"][:-2] def add_distributed_training_params(parser): - + cpuinfo = CPUinfo() socket_nums = cpuinfo.socket_nums() @@ -504,7 +504,7 @@ def add_distributed_training_params(parser): "training") group.add_argument("--nproc_per_node", metavar='\b', type=int, default=socket_nums, help="The number of processes to launch on each node") - #ccl control + #ccl control group.add_argument("--ccl_worker_count", metavar='\b', default=4, type=int, help="Core numbers per rank used for ccl communication") #mpi control @@ -528,7 +528,7 @@ def add_distributed_training_params(parser): def add_memory_allocator_params(parser): - group = parser.add_argument_group("Memory Allocator Parameters") + group = parser.add_argument_group("Memory Allocator Parameters") #allocator control group.add_argument("--enable_tcmalloc", action='store_true', default=False, help="Enable tcmalloc allocator") @@ -536,12 +536,12 @@ def add_memory_allocator_params(parser): help="Enable jemalloc allocator") group.add_argument("--use_default_allocator", action='store_true', default=False, help="Use default memory allocator") - + def add_multi_instance_params(parser): - + group = parser.add_argument_group("Multi-instance Parameters") #multi-instance control - group.add_argument("--ncore_per_instance", metavar='\b', default=-1, type=int, + group.add_argument("--ncore_per_instance", metavar='\b', default=-1, type=int, help="Cores per instance") group.add_argument("--ninstances", metavar='\b', default=-1, type=int, help="For multi-instance, you should give the cores number you used for per insantance.") @@ -557,16 +557,16 @@ def add_multi_instance_params(parser): help="Disable numactl") group.add_argument("--core_list", metavar='\b', default=None, type=str, help="Specify the core list as 'core_id, core_id, ....', otherwise, all the cores will be used.") - -def add_kmp_iomp_params(parser): - group = parser.add_argument_group("KMP/IOMP Affinity Parameters") +def add_kmp_iomp_params(parser): + + group = parser.add_argument_group("KMP/IOMP Affinity Parameters") group.add_argument("--kmp_affinity", metavar='\b', default="granularity=fine,compact,1,0", type=str, help="KMP_AFFINITY setup, environment variable has higher priority than this args." "defualt value is : granularity=fine,compact,1,0") group.add_argument("--enable_iomp", action='store_true', default=False, - help="Enable iomp and libiomp.so will be add to LD_PRELOAD") - + help="Enable iomp and libiomp.so will be add to LD_PRELOAD") + def parse_args(): """ @@ -578,23 +578,23 @@ def parse_args(): "inference/training and distributed training with oneCCL backend is enabled. " "To get the peak performance on Intel Xeon CPU, the script optimizes the configuration " "of thread and memory management. For thread management, the script configures thread " - "affinity and the preload of Intel OMP library. For memory management, it configures " + "affinity and the preload of Intel OMP library. For memory management, it configures " "NUMA binding and preload optimized memory allocation library (e.g. tcmalloc, jemalloc) " "\n################################# Basic usage ############################# \n" - "\n 1. single instance\n" - "\n >>> python -m intel_pytorch_extension.launch python_script args \n" + "\n 1. single instance\n" + "\n >>> python -m torch_ipex.launch python_script args \n" "\n2. multi-instance \n" - "\n >>> python -m intel_pytorch_extension.launch --multi_instance python_script args\n" + "\n >>> python -m torch_ipex.launch --multi_instance python_script args\n" "\n3. Single-Node multi-process distributed training\n" - "\n >>> python -m intel_pytorch_extension.launch --distributed python_script args\n" + "\n >>> python -m torch_ipex.launch --distributed python_script args\n" "\n4. Multi-Node multi-process distributed training: (e.g. two nodes)\n" "\n rank 0: *(IP: 192.168.10.10, and has a free port: 295000)*\n" - "\n >>> python -m intel_pytorch_extension.launch --distributed --nproc_per_node=2\n" + "\n >>> python -m torch_ipex.launch --distributed --nproc_per_node=2\n" "\n --nnodes=2 --hostfile hostfile python_script args\n", formatter_class=RawTextHelpFormatter) - + parser.add_argument("--multi_instance", action='store_true', default=False, - help="Enable multi-instance, by default one instance per socket") + help="Enable multi-instance, by default one instance per socket") parser.add_argument('--distributed', action='store_true', default=False, help='Enable distributed training.') @@ -608,7 +608,7 @@ def parse_args(): "it directly. Useful when the script is not a Python script.") add_memory_allocator_params(parser) add_kmp_iomp_params(parser) - + add_distributed_training_params(parser) add_multi_instance_params(parser) # positional @@ -630,7 +630,7 @@ def main(): if args.distributed and args.multi_instance: raise RuntimeError("Either args.distributed or args.multi_instance should be set") - + if args.latency_performance and args.throughput_performance: raise RuntimeError("Either args.latency_performance or args.throughput_performance should be set") @@ -644,7 +644,7 @@ def main(): for x in sorted(set(os.environ.keys()) - env_before): logger.debug(f'{x}={os.environ[x]}') - + if __name__ == "__main__": main() diff --git a/torch_ipex/ops/embeddingbag.py b/torch_ipex/ops/embeddingbag.py index bedfca8e5..2a1b64ee6 100644 --- a/torch_ipex/ops/embeddingbag.py +++ b/torch_ipex/ops/embeddingbag.py @@ -1,7 +1,7 @@ import torch from torch import nn from torch.autograd import Function -import intel_pytorch_extension as ipex +import torch_ipex as ipex import torch_ipex._C as core from typing import Callable, List, Optional, Tuple From 5b4776e59a5232a3ba3b5724b80ed1ca4609f31f Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 7 Jul 2021 07:24:03 +0900 Subject: [PATCH 22/35] 1. removed torch-ccl 2. added debug info into version.py 3. removed pytorch wheel file binding in debug mode --- .gitmodules | 3 - CMakeLists.txt | 1 - cmake/CPU.cmake | 4 - setup.py | 380 +++++++++++++++++---------------- third_party/torch_ccl | 1 - torch_ipex/csrc/CMakeLists.txt | 1 - torch_ipex/csrc/_C.cpp | 12 -- 7 files changed, 192 insertions(+), 210 deletions(-) delete mode 160000 third_party/torch_ccl diff --git a/.gitmodules b/.gitmodules index a0dbdf925..7761a5ad7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,6 +4,3 @@ [submodule "third_party/xsmm"] path = third_party/xsmm url = https://github.com/hfp/libxsmm.git -[submodule "third_party/torch_ccl"] - path = third_party/torch_ccl - url = https://github.com/intel/torch-ccl.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 86a953e52..3b9e49828 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,6 @@ set(PLUGIN_NAME torch_ipex) set(RPATH_VALUE $ORIGIN) set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) -set(CMAKE_INSTALL_RPATH "${RPATH_VALUE}/../../torch_ccl/lib") set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) set(DPCPP_ROOT "${PROJECT_SOURCE_DIR}/torch_ipex/csrc") diff --git a/cmake/CPU.cmake b/cmake/CPU.cmake index fe3ee87e4..eb675d5fe 100644 --- a/cmake/CPU.cmake +++ b/cmake/CPU.cmake @@ -12,7 +12,6 @@ SET(DNNL_LIBRARY_TYPE STATIC CACHE STRING "" FORCE) set(DPCPP_CPU_ROOT "${PROJECT_SOURCE_DIR}/torch_ipex/csrc/cpu") add_subdirectory(${DPCPP_THIRD_PARTY_ROOT}/mkl-dnn EXCLUDE_FROM_ALL) -find_package(TorchCCL REQUIRED) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) FIND_PACKAGE(AVX) @@ -142,7 +141,6 @@ include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/torch_ipex) include_directories(${PROJECT_SOURCE_DIR}/torch_ipex/csrc/) include_directories(${DPCPP_THIRD_PARTY_ROOT}/xsmm/include) -include_directories(${TORCHCCL_INCLUDE_DIR}) # sources set(DPCPP_SRCS) @@ -186,11 +184,9 @@ else() message(FATAL_ERROR "Unknown ATen parallel backend: ${ATEN_THREADING}") endif() -add_dependencies(${PLUGIN_NAME} torch_ccl) add_dependencies(${PLUGIN_NAME} dnnl) target_link_libraries(${PLUGIN_NAME} PUBLIC dnnl) add_dependencies(${PLUGIN_NAME} xsmm) -target_link_libraries(${PLUGIN_NAME} PUBLIC torch_ccl) link_directories(${PYTORCH_INSTALL_DIR}/lib) target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libtorch_cpu.so) target_link_libraries(${PLUGIN_NAME} PUBLIC ${PYTORCH_INSTALL_DIR}/lib/libc10.so) diff --git a/setup.py b/setup.py index 680b2a982..9907b1e23 100644 --- a/setup.py +++ b/setup.py @@ -15,56 +15,56 @@ import urllib.request try: - from packaging import version + from packaging import version except Exception: - subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'packaging']) - from packaging import version + subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'packaging']) + from packaging import version installed_raw = {pkg for pkg in pkg_resources.working_set} installed = {} for i in installed_raw: - installed[i.key] = i.version + installed[i.key] = i.version requires = {} requires_raw = {} try: - with open('requirements.txt', 'r') as reader: - for line in reader.readlines(): - line_raw = line.replace('\n', '') - line = line_raw.replace('=', '') - tmp = re.split('[=<>]', line) - if len(tmp) == 2: - requires[tmp[0]] = tmp[1] - else: - requires[tmp[0]] = '' - requires_raw[tmp[0]] = line_raw + with open('requirements.txt', 'r') as reader: + for line in reader.readlines(): + line_raw = line.replace('\n', '') + line = line_raw.replace('=', '') + tmp = re.split('[=<>]', line) + if len(tmp) == 2: + requires[tmp[0]] = tmp[1] + else: + requires[tmp[0]] = '' + requires_raw[tmp[0]] = line_raw except Exception: - pass + pass restart = False for k in requires.keys(): - if k in installed.keys(): - if requires[k] != '' and version.parse(installed[k]) < version.parse(requires[k]): - subprocess.check_call([sys.executable, '-m', 'pip', 'install', requires_raw[k]]) - if k == 'wheel': - restart = True - else: - subprocess.check_call([sys.executable, '-m', 'pip', 'install', k]) - if k == 'wheel': - restart = True + if k in installed.keys(): + if requires[k] != '' and version.parse(installed[k]) < version.parse(requires[k]): + subprocess.check_call([sys.executable, '-m', 'pip', 'install', requires_raw[k]]) + if k == 'wheel': + restart = True + else: + subprocess.check_call([sys.executable, '-m', 'pip', 'install', k]) + if k == 'wheel': + restart = True if restart: - os.execv(sys.executable, ['python'] + sys.argv) - exit(1) + os.execv(sys.executable, ['python'] + sys.argv) + exit(1) TORCH_VERSION = os.getenv('TORCH_VERSION', TORCH_VERSION) try: - import torch - from torch.utils.cpp_extension import include_paths, library_paths + import torch + from torch.utils.cpp_extension import include_paths, library_paths except ImportError as e: - subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch=='+TORCH_VERSION+'+cpu', '-f', 'https://download.pytorch.org/whl/torch_stable.html']) - import torch - from torch.utils.cpp_extension import include_paths, library_paths + subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch=='+TORCH_VERSION+'+cpu', '-f', 'https://download.pytorch.org/whl/torch_stable.html']) + import torch + from torch.utils.cpp_extension import include_paths, library_paths PYTHON_VERSION = sys.version_info IS_WINDOWS = (platform.system() == 'Windows') @@ -73,27 +73,27 @@ TORCH_URL = 'torch @ https://download.pytorch.org/whl/cpu/torch-{0}%2Bcpu-cp{1}{2}-cp{1}{2}-linux_x86_64.whl'.format(TORCH_VERSION, PYTHON_VERSION.major, PYTHON_VERSION.minor) if IS_DARWIN: - TORCH_URL = 'torch=={}'.format(TORCH_VERSION) + TORCH_URL = 'torch=={}'.format(TORCH_VERSION) else: - OS_VER = 'linux_x86_64' - if IS_WINDOWS: - TORCH_URL = 'torch @ https://download.pytorch.org/whl/cpu/torch-{0}%2Bcpu-cp{1}{2}-cp{1}{2}-win_amd64.whl'.format(TORCH_VERSION, PYTHON_VERSION.major, PYTHON_VERSION.minor) - OS_VER = 'win_amd64' + OS_VER = 'linux_x86_64' + if IS_WINDOWS: + TORCH_URL = 'torch @ https://download.pytorch.org/whl/cpu/torch-{0}%2Bcpu-cp{1}{2}-cp{1}{2}-win_amd64.whl'.format(TORCH_VERSION, PYTHON_VERSION.major, PYTHON_VERSION.minor) + OS_VER = 'win_amd64' - try: - fp = urllib.request.urlopen('https://download.pytorch.org/whl/torch_stable.html', timeout=30) - cont_bytes = fp.read() - cont = cont_bytes.decode('utf8').replace('\n', '') - fp.close() - lines = re.split(r'
', cont) - - for line in lines: - matches = re.match('
(.*)<\/a>'.format(TORCH_VERSION, PYTHON_VERSION.major, PYTHON_VERSION.minor, OS_VER), line) - if matches and len(matches.groups()) == 2: - TORCH_URL = 'torch @ https://download.pytorch.org/whl/{}'.format(matches.group(2)) - break - except Exception: - pass + try: + fp = urllib.request.urlopen('https://download.pytorch.org/whl/torch_stable.html', timeout=30) + cont_bytes = fp.read() + cont = cont_bytes.decode('utf8').replace('\n', '') + fp.close() + lines = re.split(r'
', cont) + + for line in lines: + matches = re.match('
(.*)<\/a>'.format(TORCH_VERSION, PYTHON_VERSION.major, PYTHON_VERSION.minor, OS_VER), line) + if matches and len(matches.groups()) == 2: + TORCH_URL = 'torch @ https://download.pytorch.org/whl/{}'.format(matches.group(2)) + break + except Exception: + pass from subprocess import check_call, check_output from setuptools import setup, Extension, find_packages, distutils @@ -118,37 +118,37 @@ # from https://github.com/pytorch/pytorch/blob/master/tools/setup_helpers/__init__.py def which(thefile): - path = os.environ.get("PATH", os.defpath).split(os.pathsep) - for d in path: - fname = os.path.join(d, thefile) - fnames = [fname] - if sys.platform == 'win32': - exts = os.environ.get('PATHEXT', '').split(os.pathsep) - fnames += [fname + ext for ext in exts] - for name in fnames: - if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name): - return name - return None + path = os.environ.get("PATH", os.defpath).split(os.pathsep) + for d in path: + fname = os.path.join(d, thefile) + fnames = [fname] + if sys.platform == 'win32': + exts = os.environ.get('PATHEXT', '').split(os.pathsep) + fnames += [fname + ext for ext in exts] + for name in fnames: + if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name): + return name + return None def get_cmake_command(): - def _get_version(cmd): - for line in check_output([cmd, '--version']).decode('utf-8').split('\n'): - if 'version' in line: - return LooseVersion(line.strip().split(' ')[2]) - raise RuntimeError('no version found') - "Returns cmake command." - cmake_command = 'cmake' - if platform.system() == 'Windows': - return cmake_command - cmake3 = which('cmake3') - cmake = which('cmake') - if cmake3 is not None and _get_version(cmake3) >= LooseVersion("3.13.0"): - cmake_command = 'cmake3' - return cmake_command - elif cmake is not None and _get_version(cmake) >= LooseVersion("3.13.0"): - return cmake_command - else: - raise RuntimeError('no cmake or cmake3 with version >= 3.13.0 found') + def _get_version(cmd): + for line in check_output([cmd, '--version']).decode('utf-8').split('\n'): + if 'version' in line: + return LooseVersion(line.strip().split(' ')[2]) + raise RuntimeError('no version found') + "Returns cmake command." + cmake_command = 'cmake' + if platform.system() == 'Windows': + return cmake_command + cmake3 = which('cmake3') + cmake = which('cmake') + if cmake3 is not None and _get_version(cmake3) >= LooseVersion("3.13.0"): + cmake_command = 'cmake3' + return cmake_command + elif cmake is not None and _get_version(cmake) >= LooseVersion("3.13.0"): + return cmake_command + else: + raise RuntimeError('no cmake or cmake3 with version >= 3.13.0 found') def _check_env_flag(name, default=''): return os.getenv(name, default).upper() in ['ON', '1', 'YES', 'TRUE', 'Y'] @@ -167,18 +167,19 @@ def _get_env_backend(): else: return env_backend_val +debug = _check_env_flag('DEBUG') def get_git_head_sha(base_dir): ipex_git_sha = '' torch_git_sha = '' try: ipex_git_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], - cwd=base_dir).decode('ascii').strip() + cwd=base_dir).decode('ascii').strip() if os.path.isdir(os.path.join(base_dir, '..', '.git')): - torch_git_sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], - cwd=os.path.join( - base_dir, - '..')).decode('ascii').strip() + torch_git_sha = subprocess.check_output( + ['git', 'rev-parse', 'HEAD'], + cwd=os.path.join(base_dir, '..') + ).decode('ascii').strip() except Exception: pass return ipex_git_sha, torch_git_sha @@ -202,6 +203,10 @@ def create_version_files(base_dir, version, ipex_git_sha, torch_git_sha): f.write("__version__ = '{}'\n".format(version)) f.write("__ipex_gitrev__ = '{}'\n".format(ipex_git_sha)) f.write("__torch_gitrev__ = '{}'\n".format(torch_git_sha)) + if debug: + f.write("__mode__ = 'debug'\n") + else: + f.write("__mode__ = 'release'\n") cpp_version_path = os.path.join(base_dir, 'torch_ipex', 'csrc', 'version.cpp') with open(cpp_version_path, 'w') as f: @@ -279,7 +284,7 @@ def run(self): if cmake is None: raise RuntimeError( "CMake must be installed to build the following extensions: " + - ", ".join(e.name for e in self.extensions)) + ", ".join(e.name for e in self.extensions)) self.cmake = cmake if platform.system() == "Windows": @@ -302,7 +307,7 @@ def build_ipex_extension(self, ext): build_type = 'Release' use_ninja = False - if _check_env_flag('DEBUG'): + if debug: build_type = 'Debug' # install _torch_ipex.so as python module @@ -326,26 +331,26 @@ def build_ipex_extension(self, ext): #'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + ext_dir, ] - if _check_env_flag("IPEX_DISP_OP"): + if _check_env_flag('IPEX_DISP_OP'): cmake_args += ['-DIPEX_DISP_OP=1'] - if _check_env_flag("IPEX_PROFILE_OP"): + if os.getenv('IPEX_PROFILE_OP', 'UNSET') == 'UNSET' or _check_env_flag('IPEX_PROFILE_OP'): cmake_args += ['-DIPEX_PROFILE_OP=1'] - if _check_env_flag("USE_SYCL"): + if _check_env_flag('USE_SYCL'): cmake_args += ['-DUSE_SYCL=1'] - if _check_env_flag("DPCPP_ENABLE_PROFILING"): + if os.getenv('DPCPP_ENABLE_PROFILING', 'UNSET') == 'UNSET' or _check_env_flag('DPCPP_ENABLE_PROFILING'): cmake_args += ['-DDPCPP_ENABLE_PROFILING=1'] - if _check_env_flag("USE_NINJA"): + if _check_env_flag('USE_NINJA'): use_ninja = True cmake_args += ['-GNinja'] build_args = ['-j', str(multiprocessing.cpu_count())] env = os.environ.copy() - if _check_env_flag("USE_SYCL"): + if _check_env_flag('USE_SYCL'): os.environ['CXX'] = 'compute++' check_call([self.cmake, ext.project_dir] + cmake_args, cwd=build_dir, env=env) else: @@ -353,10 +358,8 @@ def build_ipex_extension(self, ext): # build_args += ['VERBOSE=1'] if use_ninja: - print('use_ninja') check_call(['ninja'] + build_args, cwd=build_dir, env=env) else: - print('make') check_call(['make'] + build_args, cwd=build_dir, env=env) check_call(['make', 'install'] + build_args, cwd=build_dir, env=env) @@ -378,100 +381,101 @@ def make_relative_rpath(path): else: return '-Wl,-rpath,$ORIGIN/' + path -install_requires=[ - TORCH_URL, -] def get_c_module(): - main_compile_args = ['-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI))] - main_libraries = ['torch_ipex'] - main_link_args = [ - '-ltorch_python', - '-ldnnl' - ] - main_sources = [os.path.join("torch_ipex", "csrc", "_C.cpp")] - cwd = os.path.dirname(os.path.abspath(__file__)) - include_dirs = [ - ".", - os.path.join("torch_ipex", "csrc"), - os.path.join("third_party", "mkl-dnn", "include"), - os.path.join("third_party", "torch_ccl", "src"), - os.path.join("third_party", "torch_ccl", "third_party", "oneCCL", "include"), - os.path.join("build", "build_torch_ipex", "third_party", "mkl-dnn", "include"), - os.path.join(pytorch_install_dir, "include"), - os.path.join(pytorch_install_dir, "include", "torch", "csrc", "api", "include") - ] - #lib_path = os.path.join(cwd, "torch_ipex", "lib") - #lib_path = os.path.join(cwd, "build") - #lib_path = os.path.join(cwd, "build", "build_torch_ipex") - library_dirs = [ - os.path.join(cwd, "build", "build_torch_ipex"), - os.path.join(cwd, "build", "build_torch_ipex", "third_party", "mkl-dnn", "src"), - os.path.join(pytorch_install_dir, "lib") - ] - #lib_path_1 = os.path.join(cwd, "build", "lib.linux-x86_64-3.8") - #library_dirs = [lib_path, lib_path_1] - extra_link_args = [] - extra_compile_args = [ - '-Wall', - '-Wextra', - '-Wno-strict-overflow', - '-Wno-unused-parameter', - '-Wno-missing-field-initializers', - '-Wno-write-strings', - '-Wno-unknown-pragmas', - # This is required for Python 2 declarations that are deprecated in 3. - '-Wno-deprecated-declarations', - # Python 2.6 requires -fno-strict-aliasing, see - # http://legacy.python.org/dev/peps/pep-3123/ - # We also depend on it in our code (even Python 3). - '-fno-strict-aliasing', - # Clang has an unfixed bug leading to spurious missing - # braces warnings, see - # https://bugs.llvm.org/show_bug.cgi?id=21629 - '-Wno-missing-braces', - ] - - C_ext = Extension("torch_ipex._C", - libraries=main_libraries, - sources=main_sources, - language='c', - extra_compile_args=main_compile_args + extra_compile_args, - include_dirs=include_dirs, - library_dirs=library_dirs, - extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) - # extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('..')]) - return C_ext + main_compile_args = ['-D_GLIBCXX_USE_CXX11_ABI=' + str(int(torch._C._GLIBCXX_USE_CXX11_ABI))] + main_libraries = ['torch_ipex'] + main_link_args = [ + '-ltorch_python', + '-ldnnl' + ] + main_sources = [os.path.join("torch_ipex", "csrc", "_C.cpp")] + cwd = os.path.dirname(os.path.abspath(__file__)) + include_dirs = [ + ".", + os.path.join("torch_ipex", "csrc"), + os.path.join("third_party", "mkl-dnn", "include"), + os.path.join("build", "build_torch_ipex", "third_party", "mkl-dnn", "include"), + os.path.join(pytorch_install_dir, "include"), + os.path.join(pytorch_install_dir, "include", "torch", "csrc", "api", "include") + ] + #lib_path = os.path.join(cwd, "torch_ipex", "lib") + #lib_path = os.path.join(cwd, "build") + #lib_path = os.path.join(cwd, "build", "build_torch_ipex") + library_dirs = [ + os.path.join(cwd, "build", "build_torch_ipex"), + os.path.join(cwd, "build", "build_torch_ipex", "third_party", "mkl-dnn", "src"), + os.path.join(pytorch_install_dir, "lib") + ] + #lib_path_1 = os.path.join(cwd, "build", "lib.linux-x86_64-3.8") + #library_dirs = [lib_path, lib_path_1] + extra_link_args = [] + extra_compile_args = [ + '-Wall', + '-Wextra', + '-Wno-strict-overflow', + '-Wno-unused-parameter', + '-Wno-missing-field-initializers', + '-Wno-write-strings', + '-Wno-unknown-pragmas', + # This is required for Python 2 declarations that are deprecated in 3. + '-Wno-deprecated-declarations', + # Python 2.6 requires -fno-strict-aliasing, see + # http://legacy.python.org/dev/peps/pep-3123/ + # We also depend on it in our code (even Python 3). + '-fno-strict-aliasing', + # Clang has an unfixed bug leading to spurious missing + # braces warnings, see + # https://bugs.llvm.org/show_bug.cgi?id=21629 + '-Wno-missing-braces', + ] + + C_ext = Extension("torch_ipex._C", + libraries=main_libraries, + sources=main_sources, + language='c', + extra_compile_args=main_compile_args + extra_compile_args, + include_dirs=include_dirs, + library_dirs=library_dirs, + extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('lib')]) + # extra_link_args=extra_link_args + main_link_args + [make_relative_rpath('..')]) + return C_ext + +install_requires=[] +if not debug: + install_requires.append([ + TORCH_URL, + ]) setup( - name='torch_ipex', - version=version, - description='Intel PyTorch Extension', - url='https://github.com/intel/intel-extension-for-pytorch', - author='Intel/PyTorch Dev Team', - install_requires=install_requires, - # Exclude the build files. - #packages=find_packages(exclude=['build']), - packages=[ - 'torch_ipex', - 'torch_ipex.ops', - 'torch_ipex.optim', - 'intel_pytorch_extension', - 'intel_pytorch_extension.ops', - 'intel_pytorch_extension.optim'], - package_dir={'intel_pytorch_extension': 'torch_ipex'}, - #package_data={ - # 'torch_ipex':[ - # 'README.md', - # 'requirements.txt', - # '*.py', - # 'lib/*.so', - # 'include/*.h', - # 'include/core/*.h', - # 'include/utils/*.h'] - # }, - zip_safe=False, - ext_modules=[IPEXExt('torch_ipex'), get_c_module()], - cmdclass={ - 'build_ext': IPEXBuild, - 'clean': IPEXClean, - }) + name='torch_ipex', + version=version, + description='Intel PyTorch Extension', + url='https://github.com/intel/intel-extension-for-pytorch', + author='Intel/PyTorch Dev Team', + install_requires=install_requires, + # Exclude the build files. + #packages=find_packages(exclude=['build']), + packages=[ + 'torch_ipex', + 'torch_ipex.ops', + 'torch_ipex.optim', + 'intel_pytorch_extension', + 'intel_pytorch_extension.ops', + 'intel_pytorch_extension.optim'], + package_dir={'intel_pytorch_extension': 'torch_ipex'}, + #package_data={ + # 'torch_ipex':[ + # 'README.md', + # 'requirements.txt', + # '*.py', + # 'lib/*.so', + # 'include/*.h', + # 'include/core/*.h', + # 'include/utils/*.h'] + # }, + zip_safe=False, + ext_modules=[IPEXExt('torch_ipex'), get_c_module()], + cmdclass={ + 'build_ext': IPEXBuild, + 'clean': IPEXClean, + }) diff --git a/third_party/torch_ccl b/third_party/torch_ccl deleted file mode 160000 index 431c45f27..000000000 --- a/third_party/torch_ccl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 431c45f2760f557ded88d0e31952e8523164ae8b diff --git a/torch_ipex/csrc/CMakeLists.txt b/torch_ipex/csrc/CMakeLists.txt index 1e5122e34..8d85998cc 100644 --- a/torch_ipex/csrc/CMakeLists.txt +++ b/torch_ipex/csrc/CMakeLists.txt @@ -9,7 +9,6 @@ LIST(APPEND DPCPP_COMMON_SRCS ${DPCPP_ROOT}/ipex_sparse_tensor_impl.cpp ${DPCPP_ROOT}/version.cpp ${DPCPP_ROOT}/utils.cpp - ${DPCPP_ROOT}/distributed/xpu_ccl.cpp ) # Pass to parent diff --git a/torch_ipex/csrc/_C.cpp b/torch_ipex/csrc/_C.cpp index 7a9f2c59d..bdd8df60a 100644 --- a/torch_ipex/csrc/_C.cpp +++ b/torch_ipex/csrc/_C.cpp @@ -29,7 +29,6 @@ #include "cpu/FusionOPs.h" #include "cpu/int8/Config.h" #include "cpu/int8/quantization/Observer.h" -#include "ProcessGroupCCL.hpp" #include #include #include @@ -222,17 +221,6 @@ void InitIpexModuleBindings(py::module m) { Int8OptConfig::get_config().set_indicators(indicators); }); - m.def("enable_torch_ccl", [=]() { - py::object module = py::module::import("torch.distributed"); - py::object register_backend = module.attr("Backend").attr("register_backend"); - register_backend("ccl", py::cpp_function(&c10d::ProcessGroupCCL::createProcessGroupCCL, - py::arg("store"), - py::arg("rank"), - py::arg("size"), - py::arg("timeout") = std::chrono::milliseconds( - ::c10d::ProcessGroupCCL::OP_TIMEOUT_MILLIS))); - - }); m.def("set_xpu_mode", [=](std::string mode){ AutoOptConfig::singleton().set_xpu_mode(torch_ipex::stringToXPUMode(mode));}); From 195ae4e12410f85915daf6edcb3575e18396c819 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 7 Jul 2021 07:27:54 +0900 Subject: [PATCH 23/35] updated dockerfile to 1.9.0 --- docker/Dockerfile | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 30b6b1a87..19da8d752 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -25,6 +25,7 @@ RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \ cmake \ libjpeg-dev \ libpng-dev \ + pybind11-dev \ && rm -rf /var/lib/apt/lists/* RUN /usr/sbin/update-ccache-symlinks RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache @@ -41,21 +42,18 @@ RUN curl -fsSL -v -o ~/miniconda.sh -O https://repo.anaconda.com/miniconda/Mini FROM dev-base AS build COPY --from=conda /opt/conda /opt/conda -ARG TORCHVISION_VERSION=0.6 +ARG TORCHVISION_VERSION=0.10.0+cpu +ARG TORCHAUDIO_VERSION=0.9.0 RUN --mount=type=cache,target=/opt/ccache \ - pip install torchvision==${TORCHVISION_VERSION}+cpu --no-deps \ - -f https://download.pytorch.org/whl/torch_stable.html && \ - pip install lark-parser hypothesis && \ git clone https://github.com/intel/intel-extension-for-pytorch && \ - cd intel-extension-for-pytorch && git submodule sync && \ + cd intel-extension-for-pytorch && \ + git checkout v1.9.0 && \ + git submodule sync && \ git submodule update --init --recursive && \ - git clone https://github.com/pytorch/pytorch && \ - cd pytorch && git checkout v1.7.0 && git submodule sync && \ - git submodule update --init --recursive && \ - git apply ../torch_patches/xpu-1.7.patch && \ - USE_MKLDNN=1 USE_CUDA=0 USE_NNPACK=0 USE_CUDNN=0 \ - CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" pip install -v . && \ - cd .. && pip install -v . && rm -rf * + python setup.py bdist_wheel && \ + pip install dist/*.whl && \ + cd .. && rm -rf intel-extension-for-pytorch && \ + pip install torchvision==${TORCHVISION_VERSION} torchaudio==${TORCHAUDIO_VERSION} -f https://download.pytorch.org/whl/torch_stable.html FROM dev-base as dev COPY --from=build /opt/conda /opt/conda From 514214f2c254a0221cc14b134a9e8512866ecc27 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 7 Jul 2021 07:32:28 +0900 Subject: [PATCH 24/35] removed core.enable_torch_ccl() --- torch_ipex/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/torch_ipex/__init__.py b/torch_ipex/__init__.py index 294f2c211..af625b912 100644 --- a/torch_ipex/__init__.py +++ b/torch_ipex/__init__.py @@ -11,7 +11,6 @@ if base_dir == 'intel_pytorch_extension': print('[WARNING] "import intel_pytorch_extension" will be deprecated in future releases. Please use "import torch_ipex" instead.') -#core.enable_torch_ccl() DEVICE = 'xpu:0' class AmpConf(object): From 227a87e9bc7f2b7f78e665839bda8329b8df9d11 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 7 Jul 2021 15:16:50 +0900 Subject: [PATCH 25/35] updated README.md for 1.9.0 --- README.md | 83 +++++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 7cc15b0be..bd1808f6e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It is designed to make the Out-of-Box user experience of PyTorch CPU better while achieving good performance. The extension also will be the PR(Pull-Request) buffer for the Intel PyTorch framework dev team. The PR buffer will not only contain functions, but also optimization (for example, take advantage of Intel's new hardware features). - [Installation](#installation) - - [Install PyTorch from Source](#install-pytorch-from-source) + - [Install PyTorch](#install-pytorch) - [Install Intel Extension for PyTorch from Source](#install-intel-extension-for-pytorch-from-source) - [Getting Started](#getting-started) - [Automatically Mix Precison](#automatically-mix-precision) @@ -17,62 +17,30 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It i ## Installation -### Install PyTorch from Source +### Install PyTorch |IPEX Version|PyTorch Version| |--|--| + |[v1.9.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.9.0)|[v1.9.0](https://github.com/pytorch/pytorch/tree/v1.9.0 "v1.9.0")| + |[v1.8.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.8.0)|[v1.8.0](https://github.com/pytorch/pytorch/tree/v1.8.0 "v1.8.0")| |[v1.2.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.2.0)|[v1.7.0](https://github.com/pytorch/pytorch/tree/v1.7.0 "v1.7.0")| |[v1.1.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.1.0)|[v1.5.0-rc3](https://github.com/pytorch/pytorch/tree/v1.5.0-rc3 "v1.5.0-rc3")| |[v1.0.2](https://github.com/intel/intel-extension-for-pytorch/tree/v1.0.2)|[v1.5.0-rc3](https://github.com/pytorch/pytorch/tree/v1.5.0-rc3 "v1.5.0-rc3")| |[v1.0.1](https://github.com/intel/intel-extension-for-pytorch/tree/v1.0.1)|[v1.5.0-rc3](https://github.com/pytorch/pytorch/tree/v1.5.0-rc3 "v1.5.0-rc3")| |[v1.0.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.0.0)|[v1.5.0-rc3](https://github.com/pytorch/pytorch/tree/v1.5.0-rc3 "v1.5.0-rc3")| - Take Intel-Extension-for-Pytorch v1.2.0 as the example. - - 1. Get PyTorch v1.7.0 source(Refer to [PyTorch guide](https://github.com/pytorch/pytorch#get-the-pytorch-source) for more details) - ```bash - git clone --recursive https://github.com/pytorch/pytorch - cd pytorch - - # checkout source code to the specified version - git checkout v1.7.0 - - # update submodules for the specified PyTorch version - git submodule sync - git submodule update --init --recursive - ``` - - 2. Get the source code of Intel Extension for PyTorch - ```bash - git clone --recursive https://github.com/intel/intel-extension-for-pytorch - cd intel-extension-for-pytorch - - # if you are updating an existing checkout - git submodule sync - git submodule update --init --recursive - ``` - - 3. Add an new backend for Intel Extension for PyTorch - ```bash - # Apply git patch to pytorch code - cd ${pytorch_directory} - git apply ${intel_extension_for_pytorch_directory}/torch_patches/xpu-1.7.patch - ``` - - 4. Build and install PyTorch (Refer to [PyTorch guide](https://github.com/pytorch/pytorch#install-pytorch) for more details) - ```bash - cd ${pytorch_directory} - python setup.py install - ``` - -### Install Intel Extension for PyTorch from Source -Install dependencies -```bash -pip install lark-parser hypothesis -``` +For IPEX version earlier than 1.8.0, a patch has to be manually applied to PyTorch source code. Please check previous installation guide. + +From IPEX 1.8.0, compiling PyTorch from source is not required. If you still want to compile PyTorch, please follow instructions [here](https://github.com/pytorch/pytorch#installation). Please make sure to checkout the correct PyTorch version according to the table above. -Install the extension ```bash -cd ${intel_extension_for_pytorch_directory} +git clone --recursive https://github.com/intel/intel-extension-for-pytorch +cd intel-extension-for-pytorch + +# if you are updating an existing checkout +git submodule sync +git submodule update --init --recursive + +# run setup.py to compile IPEX and install the binaries python setup.py install ``` @@ -254,10 +222,27 @@ Supported Quantization Operators: ### Supported Customized Operators - +* ROIAlign +* NMS +* BatchScoreNMS +* MLP +* Interaction +* FrozenBatchNorm2d ### Supported Fusion Patterns - +* Conv2D + ReLU +* Conv2D + SUM +* Conv2D + SUM + ReLU +* Conv2D + Sigmoid +* Conv2D + Sigmoid + MUL +* Conv2D + HardTanh +* Conv2D + ELU +* Conv3D + ReLU +* Conv3D + SUM +* Conv3D + SUM + ReLU +* Linear + ReLU +* Linear + GELU +* View + Transpose + Contiguous + View ## Tutorials * [Performance Tuning](tutorials/Performance_Tuning.md) From 27a1937bb1783c9d72b73ab705aecff61754711f Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 7 Jul 2021 15:22:21 +0900 Subject: [PATCH 26/35] updated README.md for 1.9.0 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd1808f6e..d1f4e0aab 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It is designed to make the Out-of-Box user experience of PyTorch CPU better while achieving good performance. The extension also will be the PR(Pull-Request) buffer for the Intel PyTorch framework dev team. The PR buffer will not only contain functions, but also optimization (for example, take advantage of Intel's new hardware features). - [Installation](#installation) - - [Install PyTorch](#install-pytorch) - [Install Intel Extension for PyTorch from Source](#install-intel-extension-for-pytorch-from-source) - [Getting Started](#getting-started) - [Automatically Mix Precison](#automatically-mix-precision) @@ -17,7 +16,6 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It i ## Installation -### Install PyTorch |IPEX Version|PyTorch Version| |--|--| |[v1.9.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.9.0)|[v1.9.0](https://github.com/pytorch/pytorch/tree/v1.9.0 "v1.9.0")| @@ -30,6 +28,8 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It i For IPEX version earlier than 1.8.0, a patch has to be manually applied to PyTorch source code. Please check previous installation guide. +### Install Intel Extension for PyTorch from Source + From IPEX 1.8.0, compiling PyTorch from source is not required. If you still want to compile PyTorch, please follow instructions [here](https://github.com/pytorch/pytorch#installation). Please make sure to checkout the correct PyTorch version according to the table above. ```bash From c895f361f11f73b532ff8d1b364c1c99625f161f Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 7 Jul 2021 19:28:28 +0900 Subject: [PATCH 27/35] updated .gitignore to delete torch_ipex/version.py when performing clean --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 83b135eb0..329246fbb 100644 --- a/.gitignore +++ b/.gitignore @@ -89,7 +89,7 @@ torch/share/ torch/test/ torch/version.py -intel_pytorch_extension_py/version.py +torch_ipex/version.py torch_ipex/csrc/version.cpp torch_ipex/csrc/aten_ipex_sparse_type_default.* torch_ipex/csrc/cpu/SparseOPs* From 731de2be4c817995321a0685ffd336fc275eba2a Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Mon, 2 Aug 2021 03:10:18 -0700 Subject: [PATCH 28/35] Exclude flatten.using_ints and cross_entropy_loss because the two operators do not generate backward functions --- scripts/cpu/gen-dense-cpu-ops.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index 5c18b954f..3da2632dc 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -145,10 +145,12 @@ "aten::conv_transpose2d.input(Tensor input, Tensor weight, Tensor? bias=None, int[2] stride=1, int[2] padding=0, int[2] output_padding=0, int groups=1, int[2] dilation=1) -> Tensor", "aten::conv_transpose3d.input(Tensor input, Tensor weight, Tensor? bias=None, int[3] stride=1, int[3] padding=0, int[3] output_padding=0, int groups=1, int[3] dilation=1) -> Tensor", "aten::log_softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor", + "aten::cross_entropy_loss(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", "aten::log_softmax.Dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor", "aten::softmax.int(Tensor self, int dim, ScalarType? dtype=None) -> Tensor", "aten::softmax.Dimname(Tensor self, Dimname dim, *, ScalarType? dtype=None) -> Tensor", "aten::contiguous(Tensor(a) self, *, MemoryFormat memory_format=contiguous_format) -> Tensor(a)", + "aten::flatten.using_ints(Tensor(a) self, int start_dim=0, int end_dim=-1) -> Tensor(a)", "aten::dropout(Tensor input, float p, bool train) -> Tensor", "aten::dropout_(Tensor(a!) self, float p, bool train) -> Tensor(a!)", "aten::nll_loss_nd(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", From c6095a67f49f3dd4c47f47616cb651c0080a2a75 Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Mon, 2 Aug 2021 21:28:16 -0700 Subject: [PATCH 29/35] Does not capture batch_norm and _batch_norm_impl_index --- scripts/cpu/gen-dense-cpu-ops.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index 3da2632dc..142704eb5 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -32,7 +32,7 @@ 'aten::mul_.Tensor(Tensor(a!) self, Tensor other) -> Tensor(a!)', 'aten::mul.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)', 'aten::linear(Tensor input, Tensor weight, Tensor? bias=None) -> Tensor', - 'aten::batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> Tensor', + # 'aten::batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> Tensor', 'aten::native_batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps) -> (Tensor, Tensor, Tensor)', 'aten::native_batch_norm_backward(Tensor grad_out, Tensor input, Tensor? weight, Tensor? running_mean, Tensor? running_var, Tensor? save_mean, Tensor? save_invstd, bool train, float eps, bool[3] output_mask) -> (Tensor, Tensor, Tensor)', 'aten::avg_pool2d(Tensor self, int[2] kernel_size, int[2] stride=[], int[2] padding=0, bool ceil_mode=False, bool count_include_pad=True, int? divisor_override=None) -> Tensor', @@ -156,6 +156,8 @@ "aten::nll_loss_nd(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", "aten::nll_loss(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", "aten::nll_loss.out(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100, *, Tensor(a!) out) -> Tensor(a!)", + "aten::batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> Tensor", + "aten::_batch_norm_impl_index(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> (Tensor, Tensor, Tensor, Tensor, int)", ] _SHALLOW_FALLBACK_TO_CPU_TENSOR_LIST = 'shallowFallbackToCPUTensorList' From dcccdd07e3bd46a40a935a862028a88b68975c47 Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Tue, 3 Aug 2021 01:40:55 -0700 Subject: [PATCH 30/35] Exclude reshape and where --- scripts/cpu/gen-dense-cpu-ops.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index 142704eb5..e27c734a3 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -158,6 +158,11 @@ "aten::nll_loss.out(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100, *, Tensor(a!) out) -> Tensor(a!)", "aten::batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> Tensor", "aten::_batch_norm_impl_index(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, float momentum, float eps, bool cudnn_enabled) -> (Tensor, Tensor, Tensor, Tensor, int)", + "aten::reshape(Tensor(a) self, int[] shape) -> Tensor(a)", + "aten::where.self(Tensor condition, Tensor self, Tensor other) -> Tensor", + "aten::where.ScalarSelf(Tensor condition, Scalar self, Tensor other) -> Tensor", + "aten::where.ScalarOther(Tensor condition, Tensor self, Scalar other) -> Tensor", + "aten::where.Scalar(Tensor condition, Scalar self, Scalar other) -> Tensor", ] _SHALLOW_FALLBACK_TO_CPU_TENSOR_LIST = 'shallowFallbackToCPUTensorList' From 846cf246a09ca6160368aa92698d450fdf7f04c2 Mon Sep 17 00:00:00 2001 From: "Wang, Eikan" Date: Tue, 3 Aug 2021 10:10:15 -0700 Subject: [PATCH 31/35] Exclude nll_loss2d --- scripts/cpu/gen-dense-cpu-ops.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cpu/gen-dense-cpu-ops.py b/scripts/cpu/gen-dense-cpu-ops.py index e27c734a3..0c7b1cf72 100755 --- a/scripts/cpu/gen-dense-cpu-ops.py +++ b/scripts/cpu/gen-dense-cpu-ops.py @@ -163,6 +163,7 @@ "aten::where.ScalarSelf(Tensor condition, Scalar self, Tensor other) -> Tensor", "aten::where.ScalarOther(Tensor condition, Tensor self, Scalar other) -> Tensor", "aten::where.Scalar(Tensor condition, Scalar self, Scalar other) -> Tensor", + "aten::nll_loss2d(Tensor self, Tensor target, Tensor? weight=None, int reduction=Mean, int ignore_index=-100) -> Tensor", ] _SHALLOW_FALLBACK_TO_CPU_TENSOR_LIST = 'shallowFallbackToCPUTensorList' From ed319df30cfabda6350e49be41ec2e693d6b953c Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Thu, 12 Aug 2021 21:46:14 +0900 Subject: [PATCH 32/35] added denormal numbers section to performance_tuning.md --- tutorials/Performance_Tuning.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tutorials/Performance_Tuning.md b/tutorials/Performance_Tuning.md index f06b5406f..82f809bbf 100644 --- a/tutorials/Performance_Tuning.md +++ b/tutorials/Performance_Tuning.md @@ -21,6 +21,7 @@ Although by default primitives of PyTorch and IPEX are highly optimized, there a - Memory Allocator - Jemalloc - TCMalloc + - Denormal Number # Hardware Configuration @@ -214,3 +215,11 @@ cd gperftools- make make install ``` + +## Denormal Number + +[Denormal number](https://en.wikipedia.org/wiki/Denormal_number) is used to store extremely small numbers which are close to 0. Computations with denormal numbers are remarkably slower than normalized number. To solve the low performance issue caused by denormal numbers, users can use the following PyTorch API function. + +``` +torch.set_flush_denormal(True) +``` From d3b61c871438f0b4c8756b16e36bd6c8fb749c2b Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Tue, 17 Aug 2021 11:59:53 +0900 Subject: [PATCH 33/35] Add installation guide for 1.9.0 --- README.md | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d1f4e0aab..703db3468 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # Intel® Extension for PyTorch -Intel Extension for PyTorch is a Python package to extend official PyTorch. It is designed to make the Out-of-Box user experience of PyTorch CPU better while achieving good performance. The extension also will be the PR(Pull-Request) buffer for the Intel PyTorch framework dev team. The PR buffer will not only contain functions, but also optimization (for example, take advantage of Intel's new hardware features). +Intel® Extension for PyTorch (IPEX) is a Python package to extend official PyTorch. It is designed to make the Out-of-Box user experience of PyTorch CPU better while achieving good performance. The extension also will be the PR(Pull-Request) buffer for the Intel PyTorch framework dev team. The PR buffer will not only contain functions, but also optimization (for example, take advantage of Intel's new hardware features). - [Installation](#installation) + - [Install PyTorch](#install-pytorch) - [Install Intel Extension for PyTorch from Source](#install-intel-extension-for-pytorch-from-source) - [Getting Started](#getting-started) - [Automatically Mix Precison](#automatically-mix-precision) @@ -16,6 +17,7 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It i ## Installation +### Install PyTorch (Optional) |IPEX Version|PyTorch Version| |--|--| |[v1.9.0](https://github.com/intel/intel-extension-for-pytorch/tree/v1.9.0)|[v1.9.0](https://github.com/pytorch/pytorch/tree/v1.9.0 "v1.9.0")| @@ -28,10 +30,27 @@ Intel Extension for PyTorch is a Python package to extend official PyTorch. It i For IPEX version earlier than 1.8.0, a patch has to be manually applied to PyTorch source code. Please check previous installation guide. -### Install Intel Extension for PyTorch from Source - From IPEX 1.8.0, compiling PyTorch from source is not required. If you still want to compile PyTorch, please follow instructions [here](https://github.com/pytorch/pytorch#installation). Please make sure to checkout the correct PyTorch version according to the table above. +**Note:** Compiling with gcc 7 on some environments, like CentOS 7, may fail. Please use GCC >= 8 to compile. + +**Note:** Installing IPEX will automatically invoke installation of the corresponding version of PyTorch. + +### Install IPEX via wheel file + +``` +python -m pip install torch_ipex==1.9.0 -f https://software.intel.com/ipex-whl-stable +``` + +:information_source: Wheel files availability for Python versions + +| IPEX Version | Python 3.6 | Python 3.7 | Python 3.8 | Python 3.9 | +| :--: | :--: | :--: | :--: | :--: | +| 1.8.0 | | :heavy_check_mark: | | | +| 1.9.0 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | + +### Install IPEX by compiling from source + ```bash git clone --recursive https://github.com/intel/intel-extension-for-pytorch cd intel-extension-for-pytorch @@ -69,7 +88,7 @@ import torch import torch.nn as nn # Import Extension -import torch_ipex as ipex +import intel_pytorch_extension as ipex class Model(nn.Module): def __init__(self): @@ -97,7 +116,7 @@ The extension can simply the case, you just need to enable the auto-mix-precisio import torch import torch.nn as nn -import torch_ipex as ipex +import intel_pytorch_extension as ipex # Automatically mix precision ipex.enable_auto_mixed_precision(mixed_dtype = torch.bfloat16) From 2488e17f9abaaa52d47572206fe07ddd8617c2e3 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Tue, 17 Aug 2021 12:04:48 +0900 Subject: [PATCH 34/35] Add installation guide for 1.9.0 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 703db3468..3c03b6bb1 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,8 @@ python -m pip install torch_ipex==1.9.0 -f https://software.intel.com/ipex-whl-s | IPEX Version | Python 3.6 | Python 3.7 | Python 3.8 | Python 3.9 | | :--: | :--: | :--: | :--: | :--: | -| 1.8.0 | | :heavy_check_mark: | | | | 1.9.0 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | +| 1.8.0 | | :heavy_check_mark: | | | ### Install IPEX by compiling from source From ba72badb0162e195b122a05764035616e8ca6bcc Mon Sep 17 00:00:00 2001 From: Wang Weihan Date: Wed, 18 Aug 2021 17:21:21 +0800 Subject: [PATCH 35/35] Update README.md The default IPEX and PyTorch versions are v1.9.0 --- docker/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/README.md b/docker/README.md index 6a58f7822..f85bcce7d 100644 --- a/docker/README.md +++ b/docker/README.md @@ -10,6 +10,6 @@ ```console $ cd $DOCKERFILE_DIR - $ DOCKER_BUILDKIT=1 docker build --build-arg IPEX_VERSION=v1.8.0 --build-arg PYTORCH_VERSION=v1.8.0 -t intel-extension-for-pytorch:test . + $ DOCKER_BUILDKIT=1 docker build -t intel-extension-for-pytorch:test . $ docker run intel-extension-for-pytorch:test python -c "import torch;import intel_pytorch_extension as ipex;print('torch:', torch.__version__,' ipex:',ipex.__version__)" ```