Skip to content

Commit

Permalink
Initial commit for ML Library Builder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ashao committed Jan 27, 2024
0 parents commit 2ac10fa
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
install
*.tgz
*.zip
*.tar.gz
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "pytorch"]
path = pytorch
url = https://github.com/pytorch/pytorch.git
27 changes: 27 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
89 changes: 89 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)

5 changes: 5 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions pytorch
Submodule pytorch added at e9ebda

0 comments on commit 2ac10fa

Please sign in to comment.