Skip to content

Commit b6f68f9

Browse files
fangqclaude
andcommitted
[build] add Makefile shortcuts: cuda / tensorrt / mex-* / package-*
Extends the convenience Makefile so the GPU and MEX builds collapse to a single command each. Each target handles the full chain -- fetching the right ORT prebuilt (CPU vs GPU), wiping build/ when switching variants, and invoking cmake. New targets: make cuda -> CUDA EP CLI build (re-fetches GPU ORT if needed) make tensorrt -> TRT + CUDA EP CLI build make mex-octave -> Octave MEX (build/siamex.mex) make mex-matlab -> MATLAB MEX (build/siamex.mex{a64,maca64,w64}) make mex-test -> matlab/tests/run_tests.m in Octave make package[-{cuda,tensorrt,mex}] -> stage + zip release bundles Idempotent ORT management via .ort-cpu / .ort-gpu PHONY targets that look for libonnxruntime_providers_cuda.so to decide whether the current third_party/onnxruntime/ install is the right variant; if not, wipe and re-fetch. Switching between `make` and `make cuda` on the same checkout no longer requires manual cleanup. README sections updated to lead with the make shortcut and demote the explicit cmake invocations to "equivalent to". Astyle ran across src/{siamize,siamize_mex}.cpp picking up whitespace from earlier edits; no semantic change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d6d3d8e commit b6f68f9

4 files changed

Lines changed: 189 additions & 33 deletions

File tree

Makefile

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,137 @@
11
# Convenience top-level Makefile for siamize. Wraps the CMake build and adds
2-
# code-formatting / cleanup targets. CMake is still the primary build system
3-
# (this Makefile just shells out to it).
2+
# code-formatting / cleanup / packaging targets. CMake is still the primary
3+
# build system (this Makefile just shells out to it).
44
#
55
# Targets:
6-
# make — configure + build (Release) under build/
7-
# make clean — remove build/
8-
# make pretty — format all source (astyle on C++, black on Python)
9-
# make pretty-cpp — astyle on src/*.{cpp,h}
10-
# make pretty-py — black on py/ and tools/
11-
# make test — run tests/run_regression.sh (needs models/ populated)
12-
13-
BUILD_DIR ?= build
6+
#
7+
# make configure + build CPU CLI (Release) under build/
8+
# make cuda configure + build CUDA CLI (re-fetches GPU ORT if needed)
9+
# make tensorrt configure + build TensorRT CLI (re-fetches GPU ORT if needed)
10+
#
11+
# make mex-octave build the Octave MEX (siamex.mex)
12+
# make mex-matlab build the MATLAB MEX (siamex.mex{a64,maca64,w64})
13+
# make mex-test run matlab/tests/run_tests.m in Octave (30 unit tests)
14+
#
15+
# make package stage + zip the CPU CLI bundle
16+
# make package-cuda stage + zip the CUDA CLI bundle (needs `make cuda` first)
17+
# make package-tensorrt stage + zip the TRT CLI bundle (needs `make tensorrt`)
18+
# make package-mex stage + zip the MEX bundle (needs `make mex-*`)
19+
#
20+
# make test run tests/run_regression.sh (needs models/ populated)
21+
#
22+
# make pretty astyle on C++, black on Python
23+
# make pretty-cpp / pretty-py
24+
#
25+
# make clean rm -rf build/ (keeps third_party/)
26+
# make distclean rm -rf build/ third_party/onnxruntime/
27+
#
28+
# Notes:
29+
# * The CPU and GPU ORT prebuilts share `third_party/onnxruntime/`, so
30+
# switching between `make` and `make cuda` re-fetches ORT only if the
31+
# current install doesn't have the right provider plugins.
32+
# * `make package*` runs `scripts/package.sh` which uses 7z; on macOS
33+
# you may need `brew install p7zip` if 7z isn't already present.
34+
35+
BUILD_DIR ?= build
1436
BUILD_TYPE ?= Release
37+
ORT_DIR := third_party/onnxruntime
38+
39+
# Filenames used to detect which ORT prebuilt (CPU vs GPU) is currently
40+
# installed under third_party/onnxruntime/.
41+
ORT_GPU_MARKER := $(ORT_DIR)/lib/libonnxruntime_providers_cuda.so
42+
ORT_GPU_MARKER_DLL := $(ORT_DIR)/lib/onnxruntime_providers_cuda.dll
43+
44+
.PHONY: all build cuda tensorrt mex-octave mex-matlab mex-test \
45+
package package-cuda package-tensorrt package-mex \
46+
ort-cpu ort-gpu clean distclean pretty pretty-cpp pretty-py test
1547

16-
.PHONY: all build clean pretty pretty-cpp pretty-py test
48+
# ---- CLI builds -------------------------------------------------------------
1749

1850
all: build
1951

20-
build:
52+
build: ort-cpu
2153
cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE)
2254
cmake --build $(BUILD_DIR) --config $(BUILD_TYPE) --parallel
2355

56+
cuda: ort-gpu
57+
cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSIAMIZE_GPU=cuda
58+
cmake --build $(BUILD_DIR) --config $(BUILD_TYPE) --parallel
59+
@echo
60+
@echo "[make cuda] built $(BUILD_DIR)/siamize with CUDA EP."
61+
@echo "Runtime: see README.md > 'Optional: NVIDIA GPU build' for the"
62+
@echo "LD_LIBRARY_PATH one-liner that exposes CUDA/cuDNN libs to ORT."
63+
64+
tensorrt: ort-gpu
65+
cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSIAMIZE_GPU=tensorrt
66+
cmake --build $(BUILD_DIR) --config $(BUILD_TYPE) --parallel
67+
@echo
68+
@echo "[make tensorrt] built $(BUILD_DIR)/siamize with TensorRT + CUDA EPs."
69+
70+
# ---- ORT prebuilt management ------------------------------------------------
71+
72+
# CPU ORT: fetch only if no CPU-only install is present. If a GPU build is
73+
# currently installed, wipe build/ + ORT and refetch.
74+
ort-cpu:
75+
@if [ -d $(ORT_DIR) ] && [ ! -e $(ORT_GPU_MARKER) ] && [ ! -e $(ORT_GPU_MARKER_DLL) ]; then \
76+
echo "[ort] CPU ORT already installed under $(ORT_DIR)"; \
77+
else \
78+
echo "[ort] (re)installing CPU ORT prebuilt"; \
79+
rm -rf $(ORT_DIR) $(BUILD_DIR); \
80+
scripts/fetch_deps.sh; \
81+
fi
82+
83+
# GPU ORT: fetch only if the CUDA provider plugin isn't already present.
84+
ort-gpu:
85+
@if [ -e $(ORT_GPU_MARKER) ] || [ -e $(ORT_GPU_MARKER_DLL) ]; then \
86+
echo "[ort] GPU ORT already installed under $(ORT_DIR)"; \
87+
else \
88+
echo "[ort] (re)installing GPU ORT prebuilt"; \
89+
rm -rf $(ORT_DIR) $(BUILD_DIR); \
90+
ORT_BUILD=gpu scripts/fetch_deps.sh; \
91+
fi
92+
93+
# ---- MEX builds -------------------------------------------------------------
94+
95+
mex-octave: ort-cpu
96+
cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSIAMIZE_BUILD_OCTAVE_MEX=ON
97+
cmake --build $(BUILD_DIR) --config $(BUILD_TYPE) --parallel
98+
@echo "[make mex-octave] built $(BUILD_DIR)/siamex.mex"
99+
100+
mex-matlab: ort-cpu
101+
cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DSIAMIZE_BUILD_MATLAB_MEX=ON
102+
cmake --build $(BUILD_DIR) --config $(BUILD_TYPE) --parallel
103+
@echo "[make mex-matlab] built $(BUILD_DIR)/siamex.mex<a64|maca64|w64>"
104+
105+
mex-test:
106+
octave-cli --no-gui --eval "cd matlab/tests; run_tests('--exit')"
107+
108+
# ---- Packaging --------------------------------------------------------------
109+
110+
# Each package-* target stages files under dist/<name>/ and zips to
111+
# <name>.zip in the repo root via scripts/package.sh.
112+
113+
package:
114+
scripts/package.sh cpu siamize-cpu
115+
116+
package-cuda:
117+
scripts/package.sh cuda siamize-cuda
118+
119+
package-tensorrt:
120+
scripts/package.sh tensorrt siamize-tensorrt
121+
122+
package-mex:
123+
scripts/package.sh mex siamex
124+
125+
# ---- Misc -------------------------------------------------------------------
126+
127+
test:
128+
tests/run_regression.sh
129+
24130
clean:
25-
rm -rf $(BUILD_DIR)
131+
rm -rf $(BUILD_DIR) dist/ siamize-cpu.zip siamize-cuda.zip siamize-tensorrt.zip siamex.zip
132+
133+
distclean: clean
134+
rm -rf $(ORT_DIR)
26135

27136
pretty: pretty-cpp pretty-py
28137

@@ -54,6 +163,3 @@ pretty-cpp:
54163
# Python formatting via black (PEP 8 conformant, default 88-col line length).
55164
pretty-py:
56165
black py/ tools/
57-
58-
test:
59-
tests/run_regression.sh

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ scripts/fetch_weights.sh # downloads the 5 fp16 .onnx folds (~1.35 GB) into
9090
CPU-only (default):
9191

9292
```bash
93-
cmake -S . -B build
94-
cmake --build build -j
93+
make # convenience target -- wraps cmake configure + build
94+
# or, equivalently:
95+
cmake -S . -B build && cmake --build build -j
9596
```
9697

9798
This produces `build/siamize`. `libonnxruntime.so.1` is located by RPATH:
@@ -103,14 +104,16 @@ checkout without setting `LD_LIBRARY_PATH`.
103104
#### Optional: NVIDIA GPU build (CUDA Execution Provider)
104105

105106
```bash
106-
# Re-fetch ORT with CUDA provider plugins.
107-
rm -rf third_party/onnxruntime
107+
make cuda # re-fetches GPU ORT prebuilt (only if needed) + configures + builds
108+
```
109+
110+
That's the convenience shortcut. The equivalent explicit form:
111+
112+
```bash
113+
rm -rf third_party/onnxruntime build
108114
ORT_BUILD=gpu scripts/fetch_deps.sh # default = CUDA 12.x build
109115
# or, if your NVIDIA driver is CUDA 13:
110116
# ORT_BUILD=gpu ORT_CUDA=13 scripts/fetch_deps.sh
111-
112-
# Configure with the CUDA backend enabled.
113-
rm -rf build
114117
cmake -S . -B build -DSIAMIZE_GPU=cuda
115118
cmake --build build -j
116119
```
@@ -145,8 +148,9 @@ time. It's an opt-in build:
145148

146149
```bash
147150
# Build with TRT enabled (gpu ORT prebuilt also has the TRT provider plugin).
148-
cmake -S . -B build -DSIAMIZE_GPU=tensorrt
149-
cmake --build build -j
151+
make tensorrt
152+
# equivalent explicit form:
153+
# cmake -S . -B build -DSIAMIZE_GPU=tensorrt && cmake --build build -j
150154

151155
# Install the matching TensorRT Python wheel (ships libnvinfer + per-SM
152156
# builder resources). Pin it to your CUDA runtime version.
@@ -227,12 +231,14 @@ share the `siamize_core` C++ sources).
227231

228232
```bash
229233
# Octave (Linux/macOS):
230-
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DSIAMIZE_BUILD_OCTAVE_MEX=ON
231-
cmake --build build -j # -> build/siamex.mex
234+
make mex-octave # -> build/siamex.mex
232235

233236
# MATLAB (Linux/macOS/Windows):
234-
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DSIAMIZE_BUILD_MATLAB_MEX=ON
235-
cmake --build build -j # -> build/siamex.mexa64 / .mexmaca64 / .mexw64
237+
make mex-matlab # -> build/siamex.mexa64 / .mexmaca64 / .mexw64
238+
239+
# Equivalent explicit forms:
240+
# cmake -S . -B build -DSIAMIZE_BUILD_OCTAVE_MEX=ON && cmake --build build -j
241+
# cmake -S . -B build -DSIAMIZE_BUILD_MATLAB_MEX=ON && cmake --build build -j
236242
```
237243

238244
The bundled jsonlab submodule (`matlab/jsonlab/`) provides
@@ -282,7 +288,8 @@ MEX and the CLI binary. Full reference: [`matlab/README.md`](matlab/README.md).
282288
### Tests
283289

284290
```bash
285-
octave-cli --no-gui --eval "cd matlab/tests; run_tests('--exit')"
291+
make mex-test
292+
# equivalent: octave-cli --no-gui --eval "cd matlab/tests; run_tests('--exit')"
286293
```
287294

288295
30 unit tests that stub the underlying MEX so they run in under a

src/siamize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ int main(int argc, char** argv) {
263263
// exception re-raises so the user sees the explicit failure they
264264
// asked for.
265265
LogitsVolume logits;
266+
266267
try {
267268
logits = siam::sliding_window(
268269
resampled, model_paths, patch, num_classes,
@@ -288,6 +289,7 @@ int main(int argc, char** argv) {
288289
throw;
289290
}
290291
}
292+
291293
resampled = Volume{}; // free
292294

293295
// resample logits back to cropped (pre-resample) shape, per channel, with trilinear.

0 commit comments

Comments
 (0)