Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[1.x] Backport of intgemm #17559 (#19099)
Browse files Browse the repository at this point in the history
* cherry-pick intgemm from master, fix build

* Fix test to conform to 1.x

* Makefile supporting intgemm compilation

* Stricter dependencies on git checkout of intgemm

* Operators depend on mkldnn

* Don't compile intgemm with gcc older than 5

* Fix intgemm test for windows on 1.x by not using pytest

* Update intgemm to use template arguments for integer immediates

* Try to fix clang3.6

* Ban gcc < 5 in cmake

* Update intgemm with gcc 5.5 debug workaround
  • Loading branch information
kpuatamazon committed Sep 16, 2020
1 parent 606933f commit d2e6452
Show file tree
Hide file tree
Showing 11 changed files with 1,228 additions and 2 deletions.
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ if(USE_MKL_IF_AVAILABLE AND (NOT APPLE) AND (NOT MSVC) AND (CMAKE_HOST_SYSTEM_PR
else()
option(USE_MKLDNN "Build with MKL-DNN support" OFF)
endif()
if ((CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) AND ((NOT CMAKE_COMPILER_IS_GNUCC) OR (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0)))
option(USE_INTGEMM "Build with x86_64 intgemm library for low-precision multiplication" ON)
else()
option(USE_INTGEMM "Build with x86_64 intgemm library for low-precision multiplication" OFF)
endif()
if(NOT MSVC)
option(USE_OPERATOR_TUNING "Enable auto-tuning of operators" ON)
else()
Expand Down Expand Up @@ -306,6 +311,22 @@ if(USE_CPP_PACKAGE)
add_definitions(-DMXNET_USE_CPP_PACKAGE=1)
endif()

if(USE_INTGEMM)
message(STATUS "Using intgemm")
include(FetchContent)
FetchContent_Declare(
intgemm
GIT_REPOSITORY https://github.com/kpu/intgemm.git
GIT_TAG 4172dcc209e6793dd920dec9cf9c9fc81605bd9d
)
FetchContent_GetProperties(intgemm)
if(NOT intgemm_POPULATED)
FetchContent_Populate(intgemm)
endif()
add_subdirectory(${intgemm_SOURCE_DIR} ${intgemm_BINARY_DIR} EXCLUDE_FROM_ALL)
add_definitions(-DMXNET_USE_INTGEMM=1)
endif()

# Allow Cuda compiles outside of src tree to find things in 'src' and 'include'
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
Expand Down Expand Up @@ -497,6 +518,11 @@ endif()
FILE(GLOB_RECURSE SOURCE "src/*.cc" "src/*.h" "include/*.h")
FILE(GLOB_RECURSE CUDA "src/*.cu" "src/*.cuh")

if(NOT USE_INTGEMM)
FILE(GLOB_RECURSE INTGEMM_OPERATOR_SOURCE "src/operator/contrib/intgemm/*.cc" "src/operator/contrib/intgemm/*.h")
list(REMOVE_ITEM SOURCE ${INTGEMM_OPERATOR_SOURCE})
endif()

# add nnvm to source
FILE(GLOB_RECURSE NNVMSOURCE
3rdparty/tvm/nnvm/src/c_api/*.cc
Expand Down Expand Up @@ -791,6 +817,10 @@ if(USE_MKLDNN)
${CMAKE_BINARY_DIR}/3rdparty/mkldnn/include/dnnl_version.h ${CMAKE_SOURCE_DIR}/include/mkldnn/)
endif()

if(USE_INTGEMM)
target_link_libraries(mxnet_static PRIVATE intgemm)
endif()

function(BuildTVMOP)
# scope the variables in BuildTVM.cmake to avoid conflict
include(cmake/BuildTVM.cmake)
Expand Down
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@
Licensed MIT © Zeno Rocha
11. mx-theme - For details, see docs/python_docs/themes/mx-theme/LICENSE
Copyright (c) 2016 myyasuda
12. intgemm - Refer to 3rdparty/intgemm/LICENSE
Copyright (c) 2017--2019 University of Edinburgh, Nikolay Bogoychev, Mateusz Chudyk, Kenneth Heafield, and Microsoft Corporation


=======================================================================================
Expand Down
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ ifeq ($(USE_MKLDNN), 1)
MKLDNNROOT = $(ROOTDIR)/3rdparty/mkldnn/build/install
endif

ifndef USE_INTGEMM
ifeq ($(UNAME_P), x86_64)
COMPILER := $(shell $(CXX) --version |head -n 1 |cut -d " " -f 1)
COMPILER_VERSION := $(shell $(CXX) -dumpversion |cut -d . -f 1)
ifeq ($(COMPILER), clang)
USE_INTGEMM=1
endif
ifeq ($(COMPILER), Apple)
USE_INTGEMM=1
endif
# If it's not clang and not Apple clang, it's probably gcc and we need at least 5.
# gcc --version gives the name of the program it was called with, which makes it hard to detect.
COMPILER_VERSION_GE_5 := $(shell expr $(COMPILER_VERSION) \>= 5)
ifeq ($(COMPILER_VERSION_GE_5), 1)
USE_INTGEMM=1
endif
endif
endif

include $(TPARTYDIR)/mshadow/make/mshadow.mk
include $(DMLC_CORE)/make/dmlc.mk

Expand Down Expand Up @@ -463,6 +482,46 @@ endif
all: lib/libmxnet.a lib/libmxnet.so $(BIN) extra-packages extension_libs

SRC = $(wildcard src/*/*/*/*.cc src/*/*/*.cc src/*/*.cc src/*.cc)

ifeq ($(USE_INTGEMM), 1)
ifndef INTGEMM_PATH
INTGEMM_PATH = build/3rdparty/intgemm
endif
CFLAGS += -DMXNET_USE_INTGEMM=1
LIB_DEP += $(INTGEMM_PATH)/libintgemm.a

# Download intgemm if it isn't already
$(INTGEMM_PATH):
@mkdir -p $(INTGEMM_PATH)
rm -rf $(INTGEMM_PATH)
git clone https://github.com/kpu/intgemm $(INTGEMM_PATH)
cd $(INTGEMM_PATH) && git checkout -q 4172dcc209e6793dd920dec9cf9c9fc81605bd9d

$(INTGEMM_PATH)/compile_test_avx512bw.cc: $(INTGEMM_PATH)
@
$(INTGEMM_PATH)/compile_test_avx512vnni.cc: $(INTGEMM_PATH)
@
$(INTGEMM_PATH)/intgemm/intgemm.cc: $(INTGEMM_PATH)
@

# Compiler tests for AVX512BW and AVX512VNNI.
$(INTGEMM_PATH)/intgemm/intgemm_config.h: $(INTGEMM_PATH)/compile_test_avx512bw.cc $(INTGEMM_PATH)/compile_test_avx512vnni.cc
echo '#pragma once' >$(INTGEMM_PATH)/intgemm/intgemm_config.h
$(CXX) $(CFLAGS) $(INTGEMM_PATH)/compile_test_avx512bw.cc 2>/dev/null && echo \#define INTGEMM_COMPILER_SUPPORTS_AVX512BW >>$(INTGEMM_PATH)/intgemm/intgemm_config.h || echo Your compiler is missing AVX512BW support
$(CXX) $(CFLAGS) $(INTGEMM_PATH)/compile_test_avx512vnni.cc 2>/dev/null && echo \#define INTGEMM_COMPILER_SUPPORTS_AVX512VNNI >>$(INTGEMM_PATH)/intgemm/intgemm_config.h || echo Your compiler is missing AVX512VNNI support

$(INTGEMM_PATH)/intgemm/intgemm.o: $(INTGEMM_PATH)/intgemm/intgemm_config.h $(INTGEMM_PATH)/intgemm/intgemm.cc $(wildcard $(INTGEMM_PATH)/intgemm/*.h $(INTGEMM_PATH)/intgemm/*/*.h)
$(CXX) $(CFLAGS) -I$(INTGEMM_PATH) -std=c++11 -c $(INTGEMM_PATH)/intgemm/intgemm.cc -o $@

$(INTGEMM_PATH)/libintgemm.a: $(INTGEMM_PATH)/intgemm/intgemm.o
@mkdir -p $(@D)
ar crv $@ $(filter %.o, $?)
else
#If we're not using intgemm, remove the operators from src.
INTGEMM_OPS := $(wildcard src/operator/contrib/intgemm/*.cc)
SRC := $(filter-out $(INTGEMM_OPS),$(SRC))
endif

OBJ = $(patsubst %.cc, build/%.o, $(SRC))
CUSRC = $(wildcard src/*/*/*/*.cu src/*/*/*.cu src/*/*.cu src/*.cu)
CUOBJ = $(patsubst %.cu, build/%_gpu.o, $(CUSRC))
Expand Down Expand Up @@ -560,6 +619,13 @@ endif
# For quick compile test, used smaller subset
ALLX_DEP= $(ALL_DEP)

ifeq ($(USE_INTGEMM), 1)
# Enforce a dependency on $(INTGEMM_PATH)/intgemm/intgemm_config.h which is a generated header based on compiler support.
build/src/operator/contrib/intgemm/%.o: src/operator/contrib/intgemm/%.cc $(INTGEMM_PATH)/intgemm/intgemm_config.h | mkldnn
@mkdir -p $(@D)
$(CXX) -std=c++11 -c $(CFLAGS) -MMD -I$(INTGEMM_PATH) -Isrc/operator -c $< -o $@
endif

build/src/%.o: src/%.cc | mkldnn
@mkdir -p $(@D)
$(CXX) -std=c++11 -c $(CFLAGS) -MMD -c $< -o $@
Expand Down
2 changes: 1 addition & 1 deletion include/mxnet/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ inline std::ostream& operator<<(std::ostream &out, const Context &ctx) {
#define ADD_FILELINE "\n\nDefined in " __FILE__ ":L" STRINGIZE(__LINE__)


#if MXNET_USE_MKLDNN == 1
#if MXNET_USE_MKLDNN == 1 || MXNET_USE_INTGEMM == 1
constexpr size_t kMKLDNNAlign = 64;
#endif

Expand Down
Loading

0 comments on commit d2e6452

Please sign in to comment.