Skip to content

Commit a50889f

Browse files
committed
introduce xbyak
1 parent 29fac3c commit a50889f

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ endif()
160160
########################################################################################
161161

162162
include(external/mklml) # download mklml package
163+
include(external/xbyak) # download xbyak package
163164
include(external/libxsmm) # download, build, install libxsmm
164165
include(external/zlib) # download, build, install zlib
165166
include(external/gflags) # download, build, install gflags

cmake/external/xbyak.cmake

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set(WITH_XBYAK ON)
16+
if(WIN32 OR APPLE)
17+
SET(WITH_XBYAK OFF CACHE STRING "Disable XBYAK in Windows and MacOS" FORCE)
18+
return()
19+
endif()
20+
21+
include(ExternalProject)
22+
23+
set(XBYAK_PROJECT extern_xbyak)
24+
set(XBYAK_PREFIX_DIR ${THIRD_PARTY_PATH}/xbyak)
25+
set(XBYAK_INSTALL_ROOT ${THIRD_PARTY_PATH}/install/xbyak)
26+
set(XBYAK_INC_DIR ${XBYAK_INSTALL_ROOT}/include)
27+
28+
include_directories(${XBYAK_INC_DIR})
29+
include_directories(${XBYAK_INC_DIR}/xbyak)
30+
31+
add_definitions(-DPADDLE_WITH_XBYAK)
32+
33+
# xbyak options
34+
add_definitions(-DXBYAK64)
35+
add_definitions(-DXBYAK_NO_OP_NAMES)
36+
37+
ExternalProject_Add(
38+
${XBYAK_PROJECT}
39+
${EXTERNAL_PROJECT_LOG_ARGS}
40+
DEPENDS ""
41+
GIT_REPOSITORY "https://github.com/herumi/xbyak.git"
42+
GIT_TAG "v5.661" # Jul 26th
43+
PREFIX ${XBYAK_PREFIX_DIR}
44+
UPDATE_COMMAND ""
45+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${XBYAK_INSTALL_ROOT}
46+
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${XBYAK_INSTALL_ROOT}
47+
)
48+
49+
if (${CMAKE_VERSION} VERSION_LESS "3.3.0")
50+
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/xbyak_dummy.c)
51+
file(WRITE ${dummyfile} "const char *dummy_xbyak = \"${dummyfile}\";")
52+
add_library(xbyak STATIC ${dummyfile})
53+
else()
54+
add_library(xbyak INTERFACE)
55+
endif()
56+
57+
add_dependencies(xbyak ${XBYAK_PROJECT})
58+
list(APPEND external_project_dependencies xbyak)

paddle/fluid/platform/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ else()
1818
endif()
1919
cc_test(enforce_test SRCS enforce_test.cc DEPS stringpiece enforce)
2020

21-
cc_library(cpu_info SRCS cpu_info.cc DEPS gflags glog enforce)
21+
set(CPU_INFO_DEPS gflags glog enforce)
22+
IF(WITH_XBYAK)
23+
list(APPEND CPU_INFO_DEPS xbyak)
24+
ENDIF()
25+
cc_library(cpu_info SRCS cpu_info.cc DEPS ${CPU_INFO_DEPS})
2226
cc_test(cpu_info_test SRCS cpu_info_test.cc DEPS cpu_info)
2327

2428
nv_library(gpu_info SRCS gpu_info.cc DEPS gflags glog enforce)

paddle/fluid/platform/cpu_info.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/platform/cpu_info.h"
16+
#include "xbyak/xbyak.h"
17+
#include "xbyak/xbyak_util.h"
1618

1719
#ifdef __APPLE__
1820
#include <sys/sysctl.h>
@@ -98,5 +100,39 @@ size_t CUDAPinnedMaxChunkSize() {
98100
return CUDAPinnedMaxAllocSize() / 256;
99101
}
100102

103+
#ifdef PADDLE_WITH_XBYAK
104+
namespace jit {
105+
106+
static Xbyak::util::Cpu cpu;
107+
bool MayIUse(const cpu_isa_t cpu_isa) {
108+
using namespace Xbyak::util; // NOLINT
109+
switch (cpu_isa) {
110+
case sse42:
111+
return cpu.has(Cpu::tSSE42);
112+
case avx2:
113+
return cpu.has(Cpu::tAVX2);
114+
case avx512_common:
115+
return cpu.has(Cpu::tAVX512F);
116+
case avx512_core:
117+
return true && cpu.has(Cpu::tAVX512F) && cpu.has(Cpu::tAVX512BW) &&
118+
cpu.has(Cpu::tAVX512VL) && cpu.has(Cpu::tAVX512DQ);
119+
case avx512_core_vnni:
120+
return true && cpu.has(Cpu::tAVX512F) && cpu.has(Cpu::tAVX512BW) &&
121+
cpu.has(Cpu::tAVX512VL) && cpu.has(Cpu::tAVX512DQ) &&
122+
cpu.has(Cpu::tAVX512_VNNI);
123+
case avx512_mic:
124+
return true && cpu.has(Cpu::tAVX512F) && cpu.has(Cpu::tAVX512CD) &&
125+
cpu.has(Cpu::tAVX512ER) && cpu.has(Cpu::tAVX512PF);
126+
case avx512_mic_4ops:
127+
return true && MayIUse(avx512_mic) && cpu.has(Cpu::tAVX512_4FMAPS) &&
128+
cpu.has(Cpu::tAVX512_4VNNIW);
129+
case isa_any:
130+
return true;
131+
}
132+
return false;
133+
}
134+
135+
} // namespace jit
136+
#endif
101137
} // namespace platform
102138
} // namespace paddle

paddle/fluid/platform/cpu_info.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,25 @@ size_t CUDAPinnedMinChunkSize();
3737
//! Get the maximum chunk size for buddy allocator.
3838
size_t CUDAPinnedMaxChunkSize();
3939

40+
#ifdef PADDLE_WITH_XBYAK
41+
namespace jit {
42+
43+
typedef enum {
44+
isa_any,
45+
sse42,
46+
avx2,
47+
avx512_common,
48+
avx512_core,
49+
avx512_core_vnni,
50+
avx512_mic,
51+
avx512_mic_4ops,
52+
} cpu_isa_t; // Instruction set architecture
53+
54+
// May I use some instruction
55+
inline bool MayIUse(const cpu_isa_t cpu_isa);
56+
57+
} // namespace jit
58+
#endif
59+
4060
} // namespace platform
4161
} // namespace paddle

0 commit comments

Comments
 (0)