Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xnnpack: conan v2 support #13648

Merged
merged 2 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions recipes/xnnpack/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
cmake_minimum_required(VERSION 3.4)
cmake_minimum_required(VERSION 3.15)
project(cmake_wrapper)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_library(cpuinfo INTERFACE IMPORTED)
add_library(clog INTERFACE IMPORTED)
set_property(TARGET cpuinfo PROPERTY INTERFACE_LINK_LIBRARIES CONAN_PKG::cpuinfo)
set_property(TARGET clog PROPERTY INTERFACE_LINK_LIBRARIES CONAN_PKG::cpuinfo)
if(NOT CMAKE_SYSTEM_PROCESSOR AND CONAN_XNNPACK_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CONAN_XNNPACK_SYSTEM_PROCESSOR})
endif()

add_library(pthreadpool INTERFACE IMPORTED)
set_property(TARGET pthreadpool PROPERTY INTERFACE_LINK_LIBRARIES CONAN_PKG::pthreadpool)
find_package(cpuinfo REQUIRED CONFIG)
if(NOT TARGET cpuinfo)
add_library(cpuinfo INTERFACE IMPORTED)
set_property(TARGET cpuinfo PROPERTY INTERFACE_LINK_LIBRARIES cpuinfo::cpuinfo)
endif()
if(NOT TARGET clog)
add_library(clog INTERFACE IMPORTED)
set_property(TARGET clog PROPERTY INTERFACE_LINK_LIBRARIES cpuinfo::cpuinfo)
endif()

add_library(fxdiv INTERFACE IMPORTED)
set_property(TARGET fxdiv PROPERTY INTERFACE_LINK_LIBRARIES CONAN_PKG::fxdiv)
find_package(pthreadpool REQUIRED CONFIG)
if(NOT TARGET pthreadpool)
add_library(pthreadpool INTERFACE IMPORTED)
set_property(TARGET pthreadpool PROPERTY INTERFACE_LINK_LIBRARIES pthreadpool::pthreadpool)
endif()

add_library(fp16 INTERFACE IMPORTED)
set_property(TARGET fp16 PROPERTY INTERFACE_LINK_LIBRARIES CONAN_PKG::fp16)
find_package(fp16 REQUIRED CONFIG)
if(NOT TARGET fp16)
add_library(fp16 INTERFACE IMPORTED)
set_property(TARGET fp16 PROPERTY INTERFACE_LINK_LIBRARIES fp16::fp16)
endif()

if(MSVC AND BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
# not a direct requirement, but we have to define this target to avoid xnnpack to download it
if(NOT TARGET fxdiv)
add_library(fxdiv INTERFACE IMPORTED)
endif()

add_subdirectory(source_subfolder)
add_subdirectory(src)
24 changes: 12 additions & 12 deletions recipes/xnnpack/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
sources:
"cci.20210310":
url: "https://github.com/google/XNNPACK/archive/24c2dec2c451b7594eb6ef70c538923899bd541c.tar.gz"
sha256: "f049f55d57d6e608dd1125c3d40323763dc842949b31437be7158b7e91687ed8"
"cci.20211026":
url: "https://github.com/google/XNNPACK/archive/ccbaedf11c70a3ff4db0ef17cfeacd710ffc3492.tar.gz"
sha256: "9324aea663d21cea4538095c40928572ddb685e0601853f278b7e5ead697fe81"
"cci.20211210":
url: "https://github.com/google/XNNPACK/archive/113092317754c7dea47bfb3cb49c4f59c3c1fa10.tar.gz"
sha256: "065bb9c85438c453f9300251f263118c4d123d79b21acf8f66582a3124d95fb2"
"cci.20220621":
url: "https://github.com/google/XNNPACK/archive/b725ca1a40b53d8087d1be0c53cb49fa05e2f1bc.tar.gz"
sha256: "a745c629dea5fc76e5a545e412a408fde1a523b71027f07d1b0670ec9ae54819"
"cci.20220801":
url: "https://github.com/google/XNNPACK/archive/8e3d3359f9bec608e09fac1f7054a2a14b1bd73c.tar.gz"
sha256: "c82327543249bd333034bbaa0300ed1fc9c7060fa73673a285042e3f042540e0"
"cci.20220621":
url: "https://github.com/google/XNNPACK/archive/b725ca1a40b53d8087d1be0c53cb49fa05e2f1bc.tar.gz"
sha256: "a745c629dea5fc76e5a545e412a408fde1a523b71027f07d1b0670ec9ae54819"
"cci.20211210":
url: "https://github.com/google/XNNPACK/archive/113092317754c7dea47bfb3cb49c4f59c3c1fa10.tar.gz"
sha256: "065bb9c85438c453f9300251f263118c4d123d79b21acf8f66582a3124d95fb2"
"cci.20211026":
url: "https://github.com/google/XNNPACK/archive/ccbaedf11c70a3ff4db0ef17cfeacd710ffc3492.tar.gz"
sha256: "9324aea663d21cea4538095c40928572ddb685e0601853f278b7e5ead697fe81"
"cci.20210310":
url: "https://github.com/google/XNNPACK/archive/24c2dec2c451b7594eb6ef70c538923899bd541c.tar.gz"
sha256: "f049f55d57d6e608dd1125c3d40323763dc842949b31437be7158b7e91687ed8"
119 changes: 64 additions & 55 deletions recipes/xnnpack/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import glob
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, replace_in_file
from conan.tools.microsoft import check_min_vs
from conan.tools.scm import Version
import os

required_conan_version = ">=1.51.1"


class XnnpackConan(ConanFile):
name = "xnnpack"
description = "XNNPACK is a highly optimized library of floating-point " \
"neural network inference operators for ARM, WebAssembly, " \
"and x86 platforms."
license = "BSD-3-Clause"
topics = ("conan", "xnnpack", "neural-network", "inference", "multithreading",
"inference-optimization", "matrix-multiplication", "simd")
topics = ("neural-network", "inference", "multithreading", "inference-optimization",
"matrix-multiplication", "simd")
homepage = "https://github.com/google/XNNPACK"
url = "https://github.com/conan-io/conan-center-index"

Expand All @@ -32,85 +37,89 @@ class XnnpackConan(ConanFile):
}

exports_sources = "CMakeLists.txt"
generators = "cmake"
_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
compiler = self.settings.compiler
compiler_version = tools.Version(compiler.version)
if (compiler == "gcc" and compiler_version < "6") or \
(compiler == "clang" and compiler_version < "5") or \
(compiler == "Visual Studio" and compiler_version < "16"):
raise ConanInvalidConfiguration("xnnpack doesn't support {} {}".format(str(compiler), compiler.version))
try:
del self.options.fPIC
except Exception:
pass
try:
del self.settings.compiler.libcxx
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
self.requires("cpuinfo/cci.20201217")
self.requires("cpuinfo/cci.20220228")
self.requires("fp16/cci.20210320")
# Note: using newer version of pthreadpool compared to reference cci.20201205
self.requires("pthreadpool/cci.20210218")

def _patch_sources(self):
tools.replace_in_file(os.path.join(self.source_folder, self._source_subfolder, "CMakeLists.txt"),
"LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}",
"LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}")
def validate(self):
check_min_vs(self, 192)
compiler = self.info.settings.compiler
compiler_version = Version(compiler.version)
if (compiler == "gcc" and compiler_version < "6") or \
(compiler == "clang" and compiler_version < "5"):
raise ConanInvalidConfiguration(f"{self.ref} doesn't support {compiler} {compiler.version}")

def source(self):
tools.get(**self.conan_data["sources"][self.version])
extracted_dir = glob.glob("XNNPACK-*")[0]
os.rename(extracted_dir, self._source_subfolder)
self._patch_sources()
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
def generate(self):
tc = CMakeToolchain(self)
if self.settings.arch == "armv8":
if self.settings.os == "Linux":
self._cmake.definitions["CMAKE_SYSTEM_PROCESSOR"] = "aarch64"
tc.variables["CONAN_XNNPACK_SYSTEM_PROCESSOR"] = "aarch64"
else:
# Not defined by Conan for Apple Silicon. See https://github.com/conan-io/conan/pull/8026
self._cmake.definitions["CMAKE_SYSTEM_PROCESSOR"] = "arm64"
self._cmake.definitions["XNNPACK_LIBRARY_TYPE"] = "shared" if self.options.shared else "static"
self._cmake.definitions["XNNPACK_ENABLE_ASSEMBLY"] = self.options.assembly
self._cmake.definitions["XNNPACK_ENABLE_MEMOPT"] = self.options.memopt
self._cmake.definitions["XNNPACK_ENABLE_SPARSE"] = self.options.sparse
self._cmake.definitions["XNNPACK_BUILD_TESTS"] = False
self._cmake.definitions["XNNPACK_BUILD_BENCHMARKS"] = False

tc.variables["CONAN_XNNPACK_SYSTEM_PROCESSOR"] = "arm64"
tc.cache_variables["XNNPACK_LIBRARY_TYPE"] = "shared" if self.options.shared else "static"
tc.variables["XNNPACK_ENABLE_ASSEMBLY"] = self.options.assembly
tc.variables["XNNPACK_ENABLE_MEMOPT"] = self.options.memopt
tc.variables["XNNPACK_ENABLE_SPARSE"] = self.options.sparse
tc.variables["XNNPACK_BUILD_TESTS"] = False
tc.variables["XNNPACK_BUILD_BENCHMARKS"] = False
tc.variables["XNNPACK_USE_SYSTEM_LIBS"] = True
# Default fPIC on if it doesn't exist (i.e. for shared library builds)
self._cmake.definitions["CMAKE_POSITION_INDEPENDENT_CODE"] = self.options.get_safe("fPIC", True)

# Use conan dependencies instead of downloading them during configuration
self._cmake.definitions["XNNPACK_USE_SYSTEM_LIBS"] = True

tc.variables["CMAKE_POSITION_INDEPENDENT_CODE"] = self.options.get_safe("fPIC", True)
# Install only built targets, in this case just the XNNPACK target
self._cmake.definitions["CMAKE_SKIP_INSTALL_ALL_DEPENDENCY"] = True
tc.variables["CMAKE_SKIP_INSTALL_ALL_DEPENDENCY"] = True
# To export symbols for shared msvc
tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = True
tc.generate()

deps = CMakeDeps(self)
deps.generate()

self._cmake.configure()
return self._cmake
def _patch_sources(self):
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
"LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}",
"LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}")

def build(self):
cmake = self._configure_cmake()
self._patch_sources()
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
cmake.build(target="XNNPACK")

def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()

def package_info(self):
self.cpp_info.libs = ["XNNPACK"]
if self.settings.os == "Linux":
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs = ["m"]
11 changes: 5 additions & 6 deletions recipes/xnnpack/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(xnnpack REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
target_link_libraries(${PROJECT_NAME} PRIVATE xnnpack::xnnpack)
target_compile_features(${PROJECT_NAME} PRIVATE c_std_99)
19 changes: 14 additions & 5 deletions recipes/xnnpack/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
11 changes: 11 additions & 0 deletions recipes/xnnpack/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(xnnpack REQUIRED CONFIG)

add_executable(${PROJECT_NAME} ../test_package/test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE xnnpack::xnnpack)
target_compile_features(${PROJECT_NAME} PRIVATE c_std_99)
17 changes: 17 additions & 0 deletions recipes/xnnpack/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
8 changes: 4 additions & 4 deletions recipes/xnnpack/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
versions:
"cci.20210310":
"cci.20220801":
folder: all
"cci.20211026":
"cci.20220621":
folder: all
"cci.20211210":
folder: all
"cci.20220621":
"cci.20211026":
folder: all
"cci.20220801":
"cci.20210310":
folder: all