Skip to content

Commit

Permalink
Remove dependency on fp16
Browse files Browse the repository at this point in the history
This dependency is causing issues on some platforms. We have a workaround: https://github.com/Maratyszcza/fp16 that some users have implemented (e.g. https://github.com/microsoft/onnxruntime/pull/22294/files), but it doesn't make sense to hack this workaround into everything that depends on XNNPACK.

This PR pulls the small part of the fp16 library we actually need in XNNPACK, and removes the dependency.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#7128 from imaginationtech:img_patch30_f32_f16_vcvt 2f40edd
PiperOrigin-RevId: 682156616
  • Loading branch information
dsharletg authored and xnnpack-bot committed Oct 4, 2024
1 parent 331e107 commit d79f803
Show file tree
Hide file tree
Showing 142 changed files with 4,884 additions and 286 deletions.
17 changes: 9 additions & 8 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ xnnpack_cc_library(
":microparams",
":mutex",
":xnnpack_h",
"@FP16",
],
)

Expand Down Expand Up @@ -428,7 +427,6 @@ xnnpack_cc_library(
":packing",
":prod_microkernels",
":xnnpack_h",
"@FP16",
] + select({
":cpuinfo_enabled": ["@cpuinfo"],
"//conditions:default": [],
Expand All @@ -449,13 +447,19 @@ xnnpack_cc_library(
],
)

xnnpack_cc_library(
name = "fp16",
hdrs = ["src/xnnpack/fp16.h"],
compatible_with = [],
)

xnnpack_cc_library(
name = "math",
hdrs = ["src/xnnpack/math.h"],
deps = [
":common",
":config_hdrs",
"@FP16",
":fp16",
],
)

Expand Down Expand Up @@ -598,7 +602,6 @@ xnnpack_cc_library(
":common",
":math",
":microparams",
"@FP16",
],
)

Expand Down Expand Up @@ -777,7 +780,6 @@ xnnpack_cc_library(
":microparams",
":operator_h",
":xnnpack_h",
"@FP16",
"@FXdiv",
],
)
Expand All @@ -796,7 +798,6 @@ xnnpack_cxx_library(
":params",
":unaligned",
":xnnpack_h",
"@FP16",
] + xnnpack_if_kleidiai_enabled([
"@KleidiAI//kai/ukernels/matmul",
"@KleidiAI//kai/ukernels/matmul:rhs_pack_kxn_qsi4cxp_qsu4cxs1s0",
Expand Down Expand Up @@ -931,6 +932,7 @@ xnnpack_cc_library(
":allocator",
":cache",
":common",
":fp16",
":indirection",
":logging",
":math",
Expand All @@ -946,7 +948,6 @@ xnnpack_cc_library(
":params",
":quantization",
":xnnpack_h",
"@FP16",
"@pthreadpool",
] + select({
"//conditions:default": [],
Expand All @@ -969,6 +970,7 @@ xnnpack_cc_library(
":cache",
":common",
":config_hdrs",
":fp16",
":hardware_config",
":internal",
":logging",
Expand All @@ -983,7 +985,6 @@ xnnpack_cc_library(
":params",
":requantization",
":xnnpack_h",
"@FP16",
"@pthreadpool",
] + xnnpack_slinky_deps(),
)
Expand Down
84 changes: 11 additions & 73 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,6 @@ IF(NOT XNNPACK_USE_SYSTEM_LIBS)
SET(CPUINFO_SOURCE_DIR "${CMAKE_BINARY_DIR}/cpuinfo-source" CACHE STRING "cpuinfo source directory")
ENDIF()

IF(NOT DEFINED FP16_SOURCE_DIR)
MESSAGE(STATUS "Downloading FP16 to ${CMAKE_BINARY_DIR}/FP16-source (define FP16_SOURCE_DIR to avoid it)")
CONFIGURE_FILE(cmake/DownloadFP16.cmake "${CMAKE_BINARY_DIR}/FP16-download/CMakeLists.txt")
EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/FP16-download")
EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/FP16-download")
SET(FP16_SOURCE_DIR "${CMAKE_BINARY_DIR}/FP16-source" CACHE STRING "FP16 source directory")
ENDIF()

IF(NOT DEFINED FXDIV_SOURCE_DIR)
MESSAGE(STATUS "Downloading FXdiv to ${CMAKE_BINARY_DIR}/FXdiv-source (define FXDIV_SOURCE_DIR to avoid it)")
CONFIGURE_FILE(cmake/DownloadFXdiv.cmake "${CMAKE_BINARY_DIR}/FXdiv-download/CMakeLists.txt")
Expand Down Expand Up @@ -1116,42 +1106,7 @@ IF(XNNPACK_BUILD_LIBRARY)
TARGET_LINK_LIBRARIES(XNNPACK PRIVATE fxdiv)
ENDIF()

# ---[ Configure FP16
IF(NOT TARGET fp16)
IF(NOT XNNPACK_USE_SYSTEM_LIBS)
SET(FP16_BUILD_TESTS OFF CACHE BOOL "")
SET(FP16_BUILD_BENCHMARKS OFF CACHE BOOL "")
ADD_SUBDIRECTORY(
"${FP16_SOURCE_DIR}"
"${CMAKE_BINARY_DIR}/FP16")
ELSE()
FIND_FILE(FP16_HDR fp16.h PATH_SUFFIXES include PATHS "${FP16_SOURCE_DIR}")
IF(NOT FP16_HDR)
MESSAGE(FATAL_ERROR "Cannot find fp16")
ENDIF()
ADD_LIBRARY(fp16 STATIC "${FP16_HDR}")
TARGET_INCLUDE_DIRECTORIES(fp16 INTERFACE "${FP16_SOURCE_DIR}/include")
SET_PROPERTY(TARGET fp16 PROPERTY LINKER_LANGUAGE C)
ENDIF()
ENDIF()
IF(XNNPACK_BUILD_ALL_MICROKERNELS)
TARGET_LINK_LIBRARIES(microkernels-all PRIVATE fp16)
ENDIF()
TARGET_LINK_LIBRARIES(microkernels-prod PRIVATE fp16)
TARGET_LINK_LIBRARIES(microparams-init PRIVATE fp16)
TARGET_LINK_LIBRARIES(packing PRIVATE fp16)
TARGET_LINK_LIBRARIES(indirection PRIVATE fp16)
TARGET_LINK_LIBRARIES(memory PRIVATE fp16)
TARGET_LINK_LIBRARIES(normalization PRIVATE fp16)
TARGET_LINK_LIBRARIES(microkernel-utils PRIVATE fp16)
TARGET_LINK_LIBRARIES(cache PRIVATE fp16)
TARGET_LINK_LIBRARIES(operator-utils PRIVATE fp16)
IF(XNNPACK_BUILD_LIBRARY)
TARGET_LINK_LIBRARIES(subgraph PRIVATE fp16)
TARGET_LINK_LIBRARIES(operators PRIVATE fp16)
TARGET_LINK_LIBRARIES(operator-run PRIVATE fp16)

TARGET_LINK_LIBRARIES(XNNPACK PRIVATE fp16)
INSTALL(TARGETS XNNPACK
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down Expand Up @@ -1212,7 +1167,7 @@ IF(XNNPACK_BUILD_TESTS)

ADD_LIBRARY(gemm-microkernel-tester STATIC test/gemm-microkernel-tester.cc)
TARGET_INCLUDE_DIRECTORIES(gemm-microkernel-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(gemm-microkernel-tester PRIVATE xnnpack-base fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(gemm-microkernel-tester PRIVATE xnnpack-base pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(gemm-microkernel-tester PRIVATE packing)
IF(XNNPACK_ENABLE_KLEIDIAI)
TARGET_LINK_LIBRARIES(gemm-microkernel-tester PRIVATE kleidiai)
Expand All @@ -1221,34 +1176,34 @@ IF(XNNPACK_BUILD_TESTS)

ADD_LIBRARY(unary-operator-tester STATIC test/unary-operator-tester.cc)
TARGET_INCLUDE_DIRECTORIES(unary-operator-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(unary-operator-tester PRIVATE XNNPACK fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(unary-operator-tester PRIVATE XNNPACK pthreadpool GTest::gtest)

ADD_LIBRARY(dwconv-microkernel-tester STATIC test/dwconv-microkernel-tester.cc)
TARGET_INCLUDE_DIRECTORIES(dwconv-microkernel-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(dwconv-microkernel-tester PRIVATE XNNPACK fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(dwconv-microkernel-tester PRIVATE XNNPACK pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(dwconv-microkernel-tester PUBLIC next-prime)

ADD_LIBRARY(vbinary-microkernel-tester STATIC test/vbinary-microkernel-tester.cc)
SET_TARGET_PROPERTIES(vbinary-microkernel-tester PROPERTIES CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(vbinary-microkernel-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(vbinary-microkernel-tester PRIVATE XNNPACK fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(vbinary-microkernel-tester PRIVATE XNNPACK pthreadpool GTest::gtest)

ADD_LIBRARY(vcvt-microkernel-tester STATIC test/vcvt-microkernel-tester.cc)
TARGET_INCLUDE_DIRECTORIES(vcvt-microkernel-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(vcvt-microkernel-tester PRIVATE XNNPACK fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(vcvt-microkernel-tester PRIVATE XNNPACK pthreadpool GTest::gtest)

ADD_LIBRARY(vunary-microkernel-tester STATIC test/vunary-microkernel-tester.cc)
TARGET_INCLUDE_DIRECTORIES(vunary-microkernel-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(vunary-microkernel-tester PRIVATE XNNPACK fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(vunary-microkernel-tester PRIVATE XNNPACK pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(vunary-microkernel-tester PUBLIC next-prime)

ADD_LIBRARY(convolution-test-helpers OBJECT test/convolution-test-helpers.cc)
TARGET_INCLUDE_DIRECTORIES(convolution-test-helpers PRIVATE include src)
TARGET_LINK_LIBRARIES(convolution-test-helpers PRIVATE xnnpack-base fp16)
TARGET_LINK_LIBRARIES(convolution-test-helpers PRIVATE xnnpack-base)

ADD_LIBRARY(packq-microkernel-tester STATIC test/packq-microkernel-tester.cc)
TARGET_INCLUDE_DIRECTORIES(packq-microkernel-tester PRIVATE . include src test)
TARGET_LINK_LIBRARIES(packq-microkernel-tester PRIVATE XNNPACK fp16 pthreadpool GTest::gtest)
TARGET_LINK_LIBRARIES(packq-microkernel-tester PRIVATE XNNPACK pthreadpool GTest::gtest)
IF(XNNPACK_ENABLE_KLEIDIAI)
TARGET_LINK_LIBRARIES(packq-microkernel-tester PRIVATE kleidiai)
ENDIF()
Expand All @@ -1268,7 +1223,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(${TEST}-test test/${TEST}.cc)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
fp16
GTest::gtest
GTest::gtest_main
hardware-config
Expand All @@ -1295,7 +1249,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(${TEST}-test test/${TEST}.cc)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1333,7 +1286,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(${TEST}-test test/${TEST}.cc)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
fp16
GTest::gtest
GTest::gtest_main
unary-operator-tester
Expand All @@ -1344,7 +1296,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(binary-elementwise-nd-test test/binary-elementwise-nd.cc)
TARGET_INCLUDE_DIRECTORIES(binary-elementwise-nd-test PRIVATE src test)
TARGET_LINK_LIBRARIES(binary-elementwise-nd-test PRIVATE
fp16
GTest::gtest
GTest::gtest_main
XNNPACK)
Expand All @@ -1362,7 +1313,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(${TEST}-test test/${TEST}.cc)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1425,7 +1375,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(${TEST}-test test/${TEST}.cc)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand All @@ -1444,7 +1393,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
convolution-test-helpers
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1539,7 +1487,6 @@ IF(XNNPACK_BUILD_TESTS)
ADD_EXECUTABLE(${TEST}-test test/${TEST}.cc)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1580,7 +1527,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
dwconv-microkernel-tester
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1633,7 +1579,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
gemm-microkernel-tester
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand All @@ -1654,7 +1599,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
packq-microkernel-tester
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1727,7 +1671,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
vbinary-microkernel-tester
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1760,7 +1703,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
vcvt-microkernel-tester
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1814,7 +1756,6 @@ IF(XNNPACK_BUILD_TESTS)
TARGET_INCLUDE_DIRECTORIES(${TEST}-test PRIVATE . include src test)
TARGET_LINK_LIBRARIES(${TEST}-test PRIVATE
vunary-microkernel-tester
fp16
GTest::gmock
GTest::gtest
GTest::gtest_main
Expand Down Expand Up @@ -1849,7 +1790,7 @@ IF(XNNPACK_BUILD_TESTS)

ADD_EXECUTABLE(operator-utils-test test/operator-utils.cc)
TARGET_INCLUDE_DIRECTORIES(operator-utils-test PRIVATE include src)
TARGET_LINK_LIBRARIES(operator-utils-test PRIVATE XNNPACK GTest::gtest GTest::gtest_main pthreadpool fp16)
TARGET_LINK_LIBRARIES(operator-utils-test PRIVATE XNNPACK GTest::gtest GTest::gtest_main pthreadpool)
ENDIF()


Expand Down Expand Up @@ -1903,14 +1844,14 @@ IF(XNNPACK_BUILD_BENCHMARKS)
# Helper libraries
ADD_LIBRARY(packq-benchmark STATIC bench/packq-benchmark.cc)
TARGET_INCLUDE_DIRECTORIES(packq-benchmark PRIVATE . include src bench)
TARGET_LINK_LIBRARIES(packq-benchmark PRIVATE XNNPACK benchmark::benchmark bench-utils fp16)
TARGET_LINK_LIBRARIES(packq-benchmark PRIVATE XNNPACK benchmark::benchmark bench-utils)
IF(XNNPACK_ENABLE_KLEIDIAI)
TARGET_LINK_LIBRARIES(packq-benchmark PRIVATE kleidiai)
ENDIF()

ADD_LIBRARY(gemm-benchmark STATIC bench/gemm-benchmark.cc)
TARGET_INCLUDE_DIRECTORIES(gemm-benchmark PRIVATE . include src bench)
TARGET_LINK_LIBRARIES(gemm-benchmark PRIVATE XNNPACK benchmark::benchmark bench-utils fp16)
TARGET_LINK_LIBRARIES(gemm-benchmark PRIVATE XNNPACK benchmark::benchmark bench-utils)
IF(XNNPACK_ENABLE_KLEIDIAI)
TARGET_LINK_LIBRARIES(gemm-benchmark PUBLIC kleidiai)
ENDIF()
Expand All @@ -1936,7 +1877,6 @@ IF(XNNPACK_BUILD_BENCHMARKS)
TARGET_LINK_LIBRARIES(bench-models PRIVATE
bench-utils
benchmark::benchmark
fp16
models
XNNPACK)

Expand Down Expand Up @@ -1971,7 +1911,6 @@ IF(XNNPACK_BUILD_BENCHMARKS)
TARGET_LINK_LIBRARIES(${BENCH}-bench PRIVATE
bench-utils
benchmark::benchmark
fp16
XNNPACK
)
ENDFOREACH()
Expand Down Expand Up @@ -2068,7 +2007,6 @@ IF(XNNPACK_BUILD_BENCHMARKS)
TARGET_LINK_LIBRARIES(${BENCH}-bench PRIVATE
bench-utils
benchmark::benchmark
fp16
gemm-benchmark
hardware-config
im2col
Expand Down
13 changes: 0 additions & 13 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,6 @@ http_archive(
)
# LINT.ThenChange(cmake/DownloadGoogleBenchmark.cmake)

# LINT.IfChange
# FP16 library, used for half-precision conversions
http_archive(
name = "FP16",
build_file = "@//third_party:FP16.BUILD",
sha256 = "e66e65515fa09927b348d3d584c68be4215cfe664100d01c9dbc7655a5716d70",
strip_prefix = "FP16-0a92994d729ff76a58f692d3028ca1b64b145d91",
urls = [
"https://github.com/Maratyszcza/FP16/archive/0a92994d729ff76a58f692d3028ca1b64b145d91.zip",
],
)
# LINT.ThenChange(cmake/DownloadFP16.cmake)

# LINT.IfChange
# FXdiv library, used for repeated integer division by the same factor
http_archive(
Expand Down
Loading

0 comments on commit d79f803

Please sign in to comment.