From 2ac10fa2b574bc47cb0f33b900e8e6c544e40ac2 Mon Sep 17 00:00:00 2001 From: Andrew Shao Date: Thu, 18 Jan 2024 11:38:03 -0600 Subject: [PATCH] Initial commit for ML Library Builder The ML Library Builder is a Makefile-based tool to help buildt ML backends for Apple arm64 architectures. This is primarily meant to support SmartSim users doing development and prototyping, however we hope that it is also useful more broadly --- .gitignore | 5 +++ .gitmodules | 3 ++ LICENSE.md | 27 ++++++++++++++++ Makefile | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ NOTICE.md | 5 +++ README.md | 38 +++++++++++++++++++++++ pytorch | 1 + 7 files changed, 168 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 LICENSE.md create mode 100644 Makefile create mode 100644 NOTICE.md create mode 100644 README.md create mode 160000 pytorch diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e688371 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build +install +*.tgz +*.zip +*.tar.gz diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..07e14d8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "pytorch"] + path = pytorch + url = https://github.com/pytorch/pytorch.git diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..7b639af --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,27 @@ +ML Library Builder is licensed under BSD 2-Clause +--- +BSD 2-Clause License + +Copyright (c) 2024, Hewlett Packard Enterprise +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..56ec3e9 --- /dev/null +++ b/Makefile @@ -0,0 +1,89 @@ +# BSD 2-Clause License +# +# Copyright (c) 2024, Hewlett Packard Enterprise +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +PYTORCH_VERSION=2.0.1 +OSX_ARCHITECTURE=arm64 + +TORCH_TARGET = libtorch-macos-$(OSX_ARCHITECTURE)-$(PYTORCH_VERSION).zip +TORCH_BUILD = $(PWD)/build/libtorch +TORCH_INSTALL = $(PWD)/install/libtorch + +TORCH_CMAKE_OPTIONS = +TORCH_CMAKE_OPTIONS += -DCMAKE_OSX_ARCHITECTURES=$(OSX_ARCHITECTURE) +TORCH_CMAKE_OPTIONS += -DUSE_MKL=OFF -DUSE_MKLDNN=OFF -DUSE_ITT=OFF +TORCH_CMAKE_OPTIONS += -DUSE_QNNPACK=OFF -DUSE_KINETO=OFF + +.PHONY: help +help: + @grep "^# help\:" Makefile | grep -v grep | sed 's/\# help\: //' | sed 's/\# help\://' + +ifneq ($(shell uname), Darwin) + $(error This tool requires Mac OSX) +endif + +# help: +# help: ----Overview---- +# help: This makefile can be used to builds ML backends for use on arm64. Generally +# help: all that needs to be done to accomplish this is +# help: +# help: pip install -r pytorch/requirements.txt +# help: make torch +# help: +# help: ----Meta targets---- +# help: clean -- Cleans all build and install directories +.PHONY: clean +clean: clean_torch + +# help: +# help: ----Build Targets---- +# help: torch -- Builds libtorch +# help: +.PHONY: torch +torch: $(TORCH_TARGET) + +# Checkout a specific version of Torch and update all of the torch submodules +.PHONY: checkout_torch +checkout_torch: + cd pytorch && git checkout v$(PYTORCH_VERSION) && \ + git submodule foreach --recursive git reset --hard && \ + git submodule update --init --recursive + +$(TORCH_BUILD) $(TORCH_INSTALL): + mkdir -p $@ + +.PHONY: build_torch +build_torch: $(TORCH_BUILD) $(TORCH_INSTALL) checkout_torch + cd $< && \ + cmake -DCMAKE_INSTALL_PREFIX=$(TORCH_INSTALL) $(TORCH_CMAKE_OPTIONS) ../../pytorch && \ + make install -j 6 + +$(TORCH_TARGET): build_torch + cd install && zip -r ../$@ libtorch + +.PHONY: clean_torch +clean_torch: + rm -rf $(TORCH_BUILD) $(TORCH_TARGET) $(TORCH_INSTALL) + diff --git a/NOTICE.md b/NOTICE.md new file mode 100644 index 0000000..931be86 --- /dev/null +++ b/NOTICE.md @@ -0,0 +1,5 @@ +==================== +Third-Party Licenses +==================== +pyTorch and Torchlib are licensed under a modified BSD clause and can be found here: +https://github.com/pytorch/pytorch/blob/main/LICENSE diff --git a/README.md b/README.md new file mode 100644 index 0000000..c112bc9 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# ML Library Builder + +## Description +Many popular machine-learning packages have C++ libraries associated with them. +Like with any compiled library, many options exist to modify their +functionality. This repository was originally intended to build libtorch for use +on Mac OSX platforms using ARM64 architectures (e.g. Apple Silicon M1). While +targeted primarily for users and developers of SmartSim, we hope that it will +be useful more broadly. Note that because of this, we are likely only going +to upgrade the backend versions as we need to and will likely not do a new +release for every version. + +Future work includes building other variants of the libraries as needed or +including support for other backends like TensorFlow and ONNX. + +## Requirements +To use this library, please ensure that the following requirements are fulfilled: + +- Mac OSX (Intel or Apple Silicon) +- Xcode version >=12.2 +- Make >=4.3 +- Cmake >=3.24 +- Python 3.10 + +Note: lower versions than those might also work + +## Instructions +Briefly, the following should be sufficient to clone and build from this repository. + +``` +git clone --recursive https://github.com/CrayLabs/ml_lib_builder.git +cd ml_lib_builder +pip install -r pytorch/requirements.txt +make torch +``` + +This will result in a zipped tarfile containing all the shared libraries that +comprise libtorch. \ No newline at end of file diff --git a/pytorch b/pytorch new file mode 160000 index 0000000..e9ebda2 --- /dev/null +++ b/pytorch @@ -0,0 +1 @@ +Subproject commit e9ebda29d87ce0916ab08c06ab26fd3766a870e5