Skip to content

Commit 2624a27

Browse files
committed
Xu isa help (#1435)
* add isa_help module * switch isa check to isa_help backend. * remove cpuid dependency.
1 parent 820e005 commit 2624a27

File tree

10 files changed

+113
-54
lines changed

10 files changed

+113
-54
lines changed

intel_extension_for_pytorch/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
pass # skip if torchvision is not available
66

77
from ._version import __version__
8-
from .utils import _cpu_isa, _custom_fx_tracer
8+
from .utils import _custom_fx_tracer
9+
from .cpu import _cpu_isa
910
_cpu_isa.check_minimal_isa_support()
1011

1112
torch_version = ''
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import intel_extension_for_pytorch._isa_help as isa
2+
import sys
3+
4+
def check_avx2_support():
5+
return isa._check_isa_avx2()
6+
7+
def check_minimal_isa_support():
8+
err_msg = "ERROR! Intel® Extension for PyTorch* only works on machines with instruction sets equal or newer than AVX2, which are not detected on the current machine."
9+
if not check_avx2_support():
10+
sys.exit(err_msg)

intel_extension_for_pytorch/csrc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ endif()
1919
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PYBIND11_CL_FLAGS}")
2020

2121
if(BUILD_WITH_CPU)
22+
add_subdirectory(cpu/isa_help)
2223
add_subdirectory(cpu)
2324
list(APPEND PY_CPP_OBJS $<TARGET_OBJECTS:PY_CPU_OBJ>)
2425
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <pybind11/pybind11.h>
2+
#include "cpu/isa_help/isa_help.h"
3+
4+
namespace py = pybind11;
5+
6+
void InitIsaHelpModuleBindings(py::module m) {
7+
m.def("_check_isa_avx2", []() { return isa_help::check_isa_avx2(); });
8+
9+
m.def("_check_isa_avx512", []() { return isa_help::check_isa_avx512(); });
10+
}
11+
12+
PYBIND11_MODULE(_isa_help, m) {
13+
InitIsaHelpModuleBindings(m);
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set(ISA_HELP_NAME "isa_help")
2+
3+
# ---[ Build flags
4+
include(${IPEX_ROOT_DIR}/cmake/cpu/BuildFlags.cmake)
5+
6+
set(IPEX_CPU_CPP_ISA_SRCS)
7+
add_subdirectory(${IPEX_CPU_ROOT_DIR}/isa ${ISA_HELP_NAME}_isa_code)
8+
9+
# Current Directory Sources
10+
FILE(GLOB IPEX_ISA_HELP_SRCS *.cpp *.h)
11+
12+
set(ISA_HELP_SRCS ${IPEX_ISA_HELP_SRCS} ${IPEX_CPU_CPP_ISA_SRCS})
13+
14+
add_library(${ISA_HELP_NAME} ${ISA_HELP_SRCS})
15+
16+
target_include_directories(${ISA_HELP_NAME} PUBLIC ${IPEX_CPU_ROOT_DIR}/isa)
17+
18+
install(TARGETS ${ISA_HELP_NAME}
19+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
20+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "isa_help.h"
2+
3+
#include "cpu_feature.hpp"
4+
#include "embedded_function.h"
5+
6+
namespace isa_help {
7+
bool check_isa_avx2() {
8+
return torch_ipex::cpu::CPUFeature::get_instance().os_avx2();
9+
}
10+
11+
bool check_isa_avx512() {
12+
return torch_ipex::cpu::CPUFeature::get_instance().os_avx512();
13+
}
14+
15+
} // namespace isa_help
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
namespace isa_help {
4+
bool check_isa_avx2();
5+
bool check_isa_avx512();
6+
} // namespace isa_help

intel_extension_for_pytorch/utils/_cpu_isa.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ wheel>=0.36
55
setuptools>=50.0
66
packaging
77
psutil
8-
cpuid

setup.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def _build_installation_dependency():
193193
install_requires = []
194194
install_requires.append('psutil')
195195
install_requires.append('numpy')
196-
install_requires.append('cpuid')
197196
return install_requires
198197

199198

@@ -773,6 +772,50 @@ def pyi_module():
773772
extra_link_args=[make_relative_rpath('lib')])
774773
return C_ext
775774

775+
def pyi_isa_help_module():
776+
main_libraries = ['isa_help']
777+
main_sources = [os.path.join(PACKAGE_NAME, "csrc", "_isa_help_main.cpp")]
778+
779+
include_dirs = [
780+
os.path.realpath("."),
781+
os.path.realpath(os.path.join(PACKAGE_NAME, "csrc")),
782+
os.path.join(pytorch_install_dir, "include"),
783+
os.path.join(pytorch_install_dir, "include", "torch", "csrc", "api", "include")]
784+
785+
library_dirs = [
786+
"lib",
787+
os.path.join(pytorch_install_dir, "lib")
788+
]
789+
790+
extra_compile_args = [
791+
'-Wall',
792+
'-Wextra',
793+
'-Wno-strict-overflow',
794+
'-Wno-unused-parameter',
795+
'-Wno-missing-field-initializers',
796+
'-Wno-write-strings',
797+
'-Wno-unknown-pragmas',
798+
# This is required for Python 2 declarations that are deprecated in 3.
799+
'-Wno-deprecated-declarations',
800+
# Python 2.6 requires -fno-strict-aliasing, see
801+
# http://legacy.python.org/dev/peps/pep-3123/
802+
# We also depend on it in our code (even Python 3).
803+
'-fno-strict-aliasing',
804+
# Clang has an unfixed bug leading to spurious missing
805+
# braces warnings, see
806+
# https://bugs.llvm.org/show_bug.cgi?id=21629
807+
'-Wno-missing-braces']
808+
809+
C_ext = CppExtension(
810+
"{}._isa_help".format(PACKAGE_NAME),
811+
libraries=main_libraries,
812+
sources=main_sources,
813+
language='c++',
814+
extra_compile_args=extra_compile_args,
815+
include_dirs=include_dirs,
816+
library_dirs=library_dirs,
817+
extra_link_args=[make_relative_rpath('lib')])
818+
return C_ext
776819

777820
ext_modules=[]
778821
cmdclass = {
@@ -797,6 +840,7 @@ def run(self):
797840
cmdclass['egg_info'] = IPEXEggInfoBuild
798841
cmdclass['install'] = IPEXInstallCmd
799842
ext_modules.append(pyi_module())
843+
ext_modules.append(pyi_isa_help_module())
800844

801845

802846
if _get_build_target() == 'python':

0 commit comments

Comments
 (0)