Skip to content

Commit b5dbff2

Browse files
authored
Fix build system and failures for linux (#68)
To build the image, run the following command in the root directory of the project: ```bash $ docker build -t chai-build-env . --platform="linux/amd64" --progress=plain ``` To make a volume for the container, use the following command: ```bash $ docker volume create chai-vol ``` This volume part might not be necessary. To run the container, use the following command: ```bash $ docker run -it --platform="linux/amd64" --name chai-vm --hostname chai-vm --privileged --memory 2g --cpus 2 -v chai-vol chai-build-env ``` Then you may run the following: ```bash root@chai-vm:/app# cd demos/torchtest root@chai-vm:/app/demos/torchtest# mkdir build root@chai-vm:/app/demos/torchtest# cd build root@chai-vm:/app/demos/torchtest/build# cmake .. root@chai-vm:/app/demos/torchtest/build# make root@chai-vm:/app/demos/torchtest/build# ./MyProject Input: [1, 3, 10, 10] Output: [1, 3, 10, 10] ``` Locally, you can do ```bash $ export Torch_DIR=<ChAI-root>/libtorch $ cd demos/torchtest $ mkdir build $ cd build $ cmake -DCMAKE_PREFIX_PATH=<ChAI-root>/libtorch/share/cmake .. $ make $ ./MyProject Input: [1, 3, 10, 10] Output: [1, 3, 10, 10] ``` # Current Status Currently, you need to have a working installation of CMake, PyThon, and Chapel to use ChAI. ## General build instructions After cloning, ```bash $ git clone https://github.com/chapel-lang/ChAI.git $ cd ChAI ``` use CMake to build the project. ```bash $ mkdir build $ cd build $ cmake -DCMAKE_BUILD_TYPE=Release .. ``` This will create a `build` directory and run CMake to generate the necessary build files. To test if the build environment is working, you can run the following command: ```bash $ make MyExample && make TorchTestBridge $ ./MyExample Hello, world! a: ndarray([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], shape = (3, 3), rank = 2) b: ndarray([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], shape = (3, 3), rank = 2) c: ndarray([[15, 18, 21], [42, 54, 66], [69, 90, 111]], shape = (3, 3), rank = 2) $ ./TorchTestBridge Input: [1, 3, 10, 10] Output: [1, 3, 10, 10] ``` The first command will compile the `MyExample` program, which is a simple "Hello, world!" program that uses ChAI. The second command will compile the `TorchTestBridge` program, which tests the PyTorch interop functionality of ChAI. See `docker-dev-run.md` for instructions on how to run the examples in a Docker container. This is useful if either of these build steps fail on your system. You may also try ```bash $ cd examples/vgg $ python3 dump_weights.py $ cd ../.. $ make vgg $ ./vgg images/frog.jpg VGG Example Directory: .../ChAI/examples/vgg Loading labels from .../ChAI/examples/vgg/imagenet/LOC_synset_mapping.txt Loaded 1000 labels. Constructing VGG16 model. Constructed VGG16 model. Loading VGG16 model weights. Loaded VGG16 model. Read file: .../ChAI/build/images/frog.jpg with ext: jpg imageData shape: (3, 720, 1280) normalizedImageData shape: (3, 720, 1280) inputImage shape: (size = 3, sizes = 3 720 1280) For 'images/frog.jpg' the top 5 predictions are: 0: label=tree frog, tree-frog; confidence=27% 1: label=tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui; confidence=8.2% 2: label=leafhopper; confidence=4.5% 3: label=leaf beetle, chrysomelid; confidence=1.9% 4: label=weevil; confidence=1.5% ```
2 parents 5263a9f + 3a90551 commit b5dbff2

63 files changed

Lines changed: 1724 additions & 546 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
presentation
2+
libtorch_static
3+
libtorch
4+
target
5+
docs
6+
dependencies
7+
build
8+
cmake-build-debug
9+
.git
10+
learning
11+
.cache

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ libtorch/
4545
build-release/
4646
libtorch_static/
4747
examples/vgg/**/*.pt
48+
segmentation_models.pytorch/
49+
.idea/
50+
cmake-build-debug/
51+
libtorch_old/

CMakeLists.txt

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.31 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
22
# project(MyProject LANGUAGES CXX)
33

44
include(CMakePrintHelpers)
@@ -15,7 +15,7 @@ set(PROJECT_CACHE_DIR "${PROJECT_ROOT_DIR}/.cache")
1515
set(LIBTORCH_DIR "${PROJECT_ROOT_DIR}/libtorch")
1616
# set(LIBTORCH_STATIC_INSTALL_DIR "${PROJECT_ROOT_DIR}/libtorch_static")
1717
set(BRIDGE_DIR "${PROJECT_ROOT_DIR}/bridge")
18-
18+
set(Torch_DIR "${PROJECT_ROOT_DIR}/libtorch")
1919

2020
find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
2121
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
@@ -38,62 +38,16 @@ download_libtorch(
3838
DESTINATION ${LIBTORCH_DIR}
3939
)
4040

41-
include(FetchContent)
42-
include(ExternalProject)
43-
44-
45-
# if(NOT EXISTS "${LIBTORCH_STATIC_INSTALL_DIR}/lib/libtorch.a")
46-
# ExternalProject_Add(
47-
# pytorch
48-
# GIT_REPOSITORY https://github.com/pytorch/pytorch.git
49-
# GIT_TAG v2.6.0 # Example: specify a particular release
50-
# UPDATE_COMMAND "" # Don’t auto-run 'git pull'
51-
# PATCH_COMMAND "" # No custom patch step
52-
53-
# # DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/pytorch-download" # Where to download the repo
54-
55-
# # We need all PyTorch submodules. By default, ExternalProject won't do submodule init.
56-
# # So we can do that in a separate step if we want a full build. For a minimal CPU build,
57-
# # you might not need them all, but let's be safe:
58-
# STEP_TARGETS clone
59-
# # After 'clone', run "git submodule update --init --recursive"
60-
# # to fetch all submodules.
61-
# # We can use a little trick with COMMAND.
62-
# # PATCH_COMMAND "git submodule update --init --recursive"
63-
64-
# # CMAKE_ARGS
65-
# # -DBUILD_SHARED_LIBS=OFF # Build static libraries
66-
# # -DBUILD_PYTHON=OFF # Don’t build Python bindings
67-
# # -DBUILD_TEST=OFF # Don’t build tests
68-
# # -DUSE_CUDA=OFF # Disable CUDA
69-
# # -DUSE_CUDNN=OFF # Disable cuDNN
70-
# # -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
71-
# # # -DBUILD_BINARY=ON
72-
# # # -DUSE_DISTRIBUTED=ON
73-
# # # -DBUILD_STATIC_RUNTIME_BENCHMARK=ON
74-
# # # -DBUILD_LITE_INTERPRETER=ON
75-
# # # -DUSE_STATIC_MKL=ON
76-
# # # -DSTATIC_DISPATCH_BACKEND=ON
77-
# # # -DCAFFE2_USE_MSVC_STATIC_RUNTIME=ON
78-
# # # -DUSE_DISTRIBUTED=ON
79-
# # # -DCMAKE_BUILD_TYPE=Release
80-
# # -DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR}
81-
# # # -DCMAKE_POLICY_VERSION_MINIMUM=3.5
82-
# CMAKE_ARGS
83-
# -DBUILD_SHARED_LIBS=OFF # Build static libraries
84-
# -DBUILD_PYTHON=OFF # Don’t build Python bindings
85-
# -DBUILD_TEST=OFF # Don’t build tests
86-
# -DUSE_CUDA=OFF # Disable CUDA
87-
# -DUSE_CUDNN=OFF # Disable cuDNN
88-
# -DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
89-
# -DCMAKE_BUILD_TYPE=Release
90-
# # -DSTATIC_DISPATCH_BACKEND=ON
91-
# -DCMAKE_INSTALL_PREFIX=${LIBTORCH_STATIC_INSTALL_DIR}
92-
# -DCMAKE_POLICY_VERSION_MINIMUM=3.5
93-
94-
# INSTALL_DIR ${LIBTORCH_STATIC_INSTALL_DIR} # Where to install
95-
# )
96-
# endif()
41+
list(APPEND CMAKE_PREFIX_PATH "${LIBTORCH_DIR}/share/cmake")
42+
find_package(Torch REQUIRED)
43+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++23 -lm -ldl ${TORCH_CXX_FLAGS}")
44+
45+
46+
cmake_print_variables(TORCH_LIBRARIES)
47+
cmake_print_variables(TORCH_INCLUDE_DIRS)
48+
cmake_print_variables(TORCH_INSTALL_PREFIX)
49+
cmake_print_variables(TORCH_CXX_FLAGS)
50+
cmake_print_variables(TORCH_LIBRARY)
9751

9852

9953
file(GLOB CHAI_LIB_FILES "${PROJECT_ROOT_DIR}/lib/*.chpl")
@@ -151,18 +105,32 @@ function(watch)
151105
)
152106
endfunction()
153107

108+
109+
154110
# cmake_print_variables(CHAI_LIB_FILES)
155111

156-
add_library(bridge STATIC ${BRIDGE_DIR}/include/bridge.h ${BRIDGE_DIR}/lib/bridge.cpp)
112+
add_library(bridge OBJECT ${BRIDGE_DIR}/include/bridge.h ${BRIDGE_DIR}/lib/bridge.cpp)
113+
114+
target_link_libraries(
115+
bridge
116+
PRIVATE
117+
${TORCH_LIBRARIES}
118+
# ${LIBTORCH_DIR}/lib/libtorch.so
119+
# ${LIBTORCH_DIR}/lib/libtorch_cpu.so
120+
# ${LIBTORCH_DIR}/lib/libc10.so
121+
# ${LIBTORCH_DIR}/lib/libtorch_global_deps.so
122+
)
157123

158124
target_include_directories(
159125
bridge
160126
PRIVATE
161127
${BRIDGE_DIR}/include
162128
${LIBTORCH_DIR}/include
163129
${LIBTORCH_DIR}/include/torch/csrc/api/include
130+
# ${BRIDGE_DIR}/util
164131
)
165132

133+
166134
set(BRIDGE_OBJECT_FILES $<TARGET_OBJECTS:bridge>)
167135

168136

@@ -204,13 +172,19 @@ foreach(lib_name IN LISTS LIBTORCH_ALL_LIBS)
204172
list(APPEND LIBTORCH_LIBS_LINKER_ARGS "-l${lib_name_short}")
205173
endforeach()
206174

207-
if(LINUX)
208-
set(LIBTORCH_LINKER_ARGS
209-
"-ltorch"
210-
"-ltorch_cpu"
211-
"-lc10"
212-
)
213-
endif()
175+
cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
176+
# cmake_print_variables(${BRIDGE_OBJECT_FILES})
177+
# cmake_print_variables(BRIDGE_OBJECT_FILES)
178+
179+
180+
set(LIBTORCH_LIBS_LINKER_ARGS
181+
"-ltorch"
182+
# "-ltorch_cpu"
183+
# "-lc10"
184+
# "-ltorch_global_deps"
185+
)
186+
cmake_print_variables(LIBTORCH_LIBS_LINKER_ARGS)
187+
214188

215189
add_executable(TorchBridge ${BRIDGE_DIR}/lib/Bridge.chpl)
216190
add_dependencies(TorchBridge bridge)
@@ -220,10 +194,10 @@ target_link_options(TorchBridge
220194
${BRIDGE_DIR}/include/bridge.h
221195
${BRIDGE_OBJECT_FILES}
222196
-L ${LIBTORCH_DIR}/lib
223-
# "-ltorch"
224-
# "-ltorch_cpu"
225-
# "-lc10"
226-
# "-ltorch_global_deps"
197+
"-ltorch"
198+
"-ltorch_cpu"
199+
"-lc10"
200+
"-ltorch_global_deps"
227201
${LIBTORCH_LIBS_LINKER_ARGS}
228202
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
229203
)
@@ -293,5 +267,33 @@ add_custom_command(
293267

294268
# ./vgg images/frog.jpg
295269

270+
# add_subdirectory(bridge)
271+
296272
add_subdirectory(examples)
297273
add_subdirectory("test")
274+
add_subdirectory(demos)
275+
276+
277+
278+
add_executable(MPSTest
279+
"${PROJECT_ROOT_DIR}/test/tiny/mps.cpp"
280+
)
281+
282+
target_include_directories(MPSTest
283+
PRIVATE
284+
${LIBTORCH_DIR}/include
285+
${LIBTORCH_DIR}/include/torch/csrc/api/include
286+
)
287+
288+
target_link_directories(MPSTest
289+
PRIVATE
290+
${LIBTORCH_DIR}/lib
291+
)
292+
293+
target_link_libraries(MPSTest
294+
PRIVATE
295+
-ltorch
296+
-ltorch_cpu
297+
-lc10
298+
-ltorch_global_deps
299+
)

Dockerfile

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,79 @@
1-
FROM ubuntu:22.04
1+
# ────────────────────────────────
2+
# 1. Build stage
3+
# ────────────────────────────────
4+
FROM ubuntu:24.04 AS builder
25

36

4-
# RUN apt update > /dev/null && apt install -y apt-utils && apt upgrade -y > /dev/null
57

8+
ENV DEBIAN_FRONTEND=noninteractive
69

710
COPY deb /
811

9-
RUN export DEBIAN_FRONTEND=noninteractive && \
10-
apt update > /dev/null && \
11-
apt upgrade -y > /dev/null && \
12-
apt install -y apt-utils > /dev/null && \
13-
apt install -y python3-pip > /dev/null && \
14-
(apt install -y ./chapel-2.3.0-1.ubuntu22.arm64.deb > /dev/null) || (apt install -y ./chapel-2.3.0-1.ubuntu22.amd64.deb > /dev/null)
12+
# -------- toolchain & helpers
13+
RUN apt-get update && apt-get install -y --no-install-recommends \
14+
build-essential g++ cmake ninja-build \
15+
git curl wget ca-certificates unzip \
16+
python3 python3-pip libssl-dev \
17+
apt-utils \
18+
&& ((apt-get install -y --no-install-recommends ./chapel-2.4.0-1.ubuntu24.arm64.deb) || (apt-get install -y --no-install-recommends ./chapel-2.4.0-1.ubuntu24.amd64.deb)) \
19+
&& rm -rf /var/lib/apt/lists/*
1520

21+
# && (apt-get install -y --no-install-recommends ./chapel-2.4.0-1.ubuntu24.arm64.deb) || (apt install -y ./chapel-2.4.0-1.ubuntu24.amd64.deb)
1622

23+
# -------- LibTorch (change CUDA tag for GPU builds)
24+
# ARG TORCH_VERSION=2.2.0 # ← keep in sync with your sources
25+
# ARG TORCH_VARIANT=cpu # cpu | cu118 | cu121 …
26+
# RUN mkdir -p /opt \
27+
# && cd /opt \
28+
# && wget -q --show-progress \
29+
# https://download.pytorch.org/libtorch/${TORCH_VARIANT}/libtorch-cxx11-abi-shared-with-deps-${TORCH_VERSION}%2B${TORCH_VARIANT}.zip \
30+
# && unzip libtorch-cxx11-abi-shared-with-deps-${TORCH_VERSION}+${TORCH_VARIANT}.zip \
31+
# && rm libtorch-cxx11-abi-shared-with-deps-${TORCH_VERSION}+${TORCH_VARIANT}.zip
1732

18-
RUN pip3 install numpy > /dev/null
1933

20-
RUN pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu > /dev/null
34+
RUN mkdir -p /app \
35+
&& cd /app \
36+
&& wget -q --show-progress \
37+
https://download.pytorch.org/libtorch/nightly/cpu/libtorch-cxx11-abi-shared-with-deps-latest.zip \
38+
&& unzip libtorch-cxx11-abi-shared-with-deps-latest.zip \
39+
&& mkdir -p /app/.cache \
40+
&& mv /app/libtorch-cxx11-abi-shared-with-deps-latest.zip /app/.cache/libtorch_cache.zip
2141

2242

23-
COPY lib /lib
24-
COPY examples /examples
25-
COPY test /test
43+
ENV Torch_DIR=/app/libtorch
44+
ENV CMAKE_PREFIX_PATH=/app/libtorch/libtorch/share/cmake
45+
# lets CMake find TorchConfig.cmake
2646

27-
# Run tests
28-
RUN cd test/correspondence && python3 correspondence.py
29-
RUN cd test/correspondence && python3 correspondence.py --print-compiler-errors
3047

48+
# -------- build your project
49+
WORKDIR /app
50+
COPY . /app
51+
52+
# (uncomment if you have submodules)
53+
# RUN git submodule update --init --recursive
54+
55+
RUN ls
56+
57+
RUN cd /app \
58+
&& mkdir -p build \
59+
&& cd build \
60+
&& cmake -DCMAKE_BUILD_TYPE=Debug .. \
61+
&& make MyExample
62+
63+
# RUN mkdir build \
64+
# && cd build \
65+
# && cmake -DCMAKE_BUILD_TYPE=Debug .. \
66+
# && make TinyLayerTest -j$(nproc)
67+
68+
#RUN cd build && ./TinyLayerTest
69+
70+
# && cmake -DCMAKE_PREFIX_PATH=/opt/libtorch -DCMAKE_BUILD_TYPE=Release .. \
71+
72+
# configure-and-build
73+
# RUN cmake -S . -B build -GNinja -DCMAKE_BUILD_TYPE=Release \
74+
# && cmake --build build --target all -j$(nproc)
75+
76+
# RUN cmake -S . -B build -GNinja -DCMAKE_BUILD_TYPE=Release
77+
78+
# RUN cmake --build build --target all -j$(nproc)
3179

32-
# Build instructions
33-
# force (last resort): docker system prune --all --force
34-
# docker builder prune --all
35-
# docker build --no-cache -t chapel-deb .
36-
# docker build -t chapel-deb .
37-
# docker container run -it chapel-deb bash

Dockerfile.testing

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
FROM ubuntu:24.04
2+
3+
4+
# RUN apt update > /dev/null && apt install -y apt-utils && apt upgrade -y > /dev/null
5+
6+
7+
COPY deb /
8+
9+
RUN export DEBIAN_FRONTEND=noninteractive && \
10+
apt update > /dev/null && \
11+
apt upgrade -y > /dev/null && \
12+
apt install -y apt-utils > /dev/null && \
13+
apt install -y python3-pip > /dev/null && \
14+
(apt install -y ./chapel-2.4.0-1.ubuntu25.arm64.deb > /dev/null) || (apt install -y ./chapel-2.4.0-1.ubuntu25.amd64.deb > /dev/null)
15+
16+
17+
18+
RUN pip3 install numpy > /dev/null
19+
20+
RUN pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu > /dev/null
21+
22+
23+
COPY lib /lib
24+
COPY examples /examples
25+
COPY test /test
26+
27+
# Run tests
28+
RUN cd test/correspondence && python3 correspondence.py
29+
RUN cd test/correspondence && python3 correspondence.py --print-compiler-errors
30+
31+
32+
# Build instructions
33+
# force (last resort): docker system prune --all --force
34+
# docker builder prune --all
35+
# docker build --no-cache -t chapel-deb .
36+
# docker build -t chapel-deb .
37+
# docker container run -it chapel-deb bash

0 commit comments

Comments
 (0)