Skip to content

Commit 7ba854b

Browse files
committed
conditionally build candle cuda support
Signed-off-by: Huamin Chen <[email protected]>
1 parent 8156b56 commit 7ba854b

File tree

6 files changed

+50
-29
lines changed

6 files changed

+50
-29
lines changed

.github/workflows/publish-crate.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ jobs:
7171
exit 1
7272
fi
7373
74-
- name: Run tests
74+
- name: Run tests (CPU-only, no CUDA)
7575
working-directory: candle-binding
76-
run: cargo test --verbose
76+
run: cargo test --no-default-features --verbose
7777

78-
- name: Check crate
78+
- name: Check crate (CPU-only, no CUDA)
7979
working-directory: candle-binding
80-
run: cargo check --verbose
80+
run: cargo check --no-default-features --verbose
8181

82-
- name: Build crate
82+
- name: Build crate (CPU-only, no CUDA)
8383
working-directory: candle-binding
84-
run: cargo build --release --verbose
84+
run: cargo build --release --no-default-features --verbose
8585

8686
- name: Dry run publish
8787
working-directory: candle-binding

.github/workflows/test-and-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ jobs:
6969
- name: Check go mod tidy
7070
run: make check-go-mod-tidy
7171

72-
- name: Build Rust library
73-
run: make rust
72+
- name: Build Rust library (CPU-only, no CUDA)
73+
run: make rust-ci
7474

7575
- name: Install HuggingFace CLI
7676
run: |

Dockerfile.extproc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@ COPY candle-binding/Cargo.loc[k] ./candle-binding/
3030
COPY tools/make/ tools/make/
3131
COPY Makefile ./
3232

33-
# Pre-build dependencies to cache them
33+
# Pre-build dependencies to cache them (CPU-only, no CUDA)
3434
RUN cd candle-binding && \
3535
mkdir -p src && \
3636
echo "fn main() {}" > src/lib.rs && \
37-
cargo build --release && \
37+
cargo build --release --no-default-features && \
3838
rm -rf src
3939

4040
# Copy source code and build
4141
COPY candle-binding/src/ ./candle-binding/src/
4242

43-
# Use Makefile to build the Rust library (rebuild with actual source code)
44-
RUN echo "Building Rust library with actual source code..." && \
43+
# Use Makefile to build the Rust library (rebuild with actual source code, CPU-only, no CUDA)
44+
RUN echo "Building Rust library with actual source code (CPU-only, no CUDA)..." && \
4545
echo "Checking source files:" && \
4646
ls -la candle-binding/src/ && \
4747
echo "Forcing clean rebuild..." && \
4848
cd candle-binding && \
4949
cargo clean && \
50-
cargo build --release && \
50+
cargo build --release --no-default-features && \
5151
echo "Checking built library:" && \
5252
find target -name "*.so" -type f && \
5353
ls -la target/release/

Dockerfile.extproc.cross

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,29 @@ COPY candle-binding/Cargo.loc[k] ./candle-binding/
7272
COPY tools/make/ tools/make/
7373
COPY Makefile ./
7474

75-
# Create a modified Makefile for cross-compilation
75+
# Create a modified Makefile for cross-compilation (CPU-only, no CUDA)
7676
RUN if [ "$TARGETARCH" = "arm64" ]; then \
77-
echo "Modifying rust.mk for ARM64 cross-compilation..."; \
78-
sed -i 's/cd candle-binding && cargo build --release/cd candle-binding \&\& cargo build --release --target aarch64-unknown-linux-gnu/' tools/make/rust.mk; \
77+
echo "Modifying rust.mk for ARM64 cross-compilation (CPU-only, no CUDA)..."; \
78+
sed -i 's/cd candle-binding && cargo build --release/cd candle-binding \&\& cargo build --release --no-default-features --target aarch64-unknown-linux-gnu/' tools/make/rust.mk; \
7979
cat tools/make/rust.mk | grep "cargo build"; \
8080
fi
8181

82-
# Pre-build dependencies to cache them
82+
# Pre-build dependencies to cache them (CPU-only, no CUDA)
8383
RUN cd candle-binding && \
8484
mkdir -p src && \
8585
echo "fn main() {}" > src/lib.rs && \
8686
if [ "$TARGETARCH" = "arm64" ]; then \
87-
cargo build --release --target aarch64-unknown-linux-gnu; \
87+
cargo build --release --no-default-features --target aarch64-unknown-linux-gnu; \
8888
else \
89-
cargo build --release; \
89+
cargo build --release --no-default-features; \
9090
fi && \
9191
rm -rf src
9292

9393
# Copy source code and build
9494
COPY candle-binding/src/ ./candle-binding/src/
9595

96-
# Build with cross-compilation (rebuild with actual source code)
97-
RUN echo "Building Rust library with actual source code..." && \
96+
# Build with cross-compilation (rebuild with actual source code, CPU-only, no CUDA)
97+
RUN echo "Building Rust library with actual source code (CPU-only, no CUDA)..." && \
9898
echo "Current directory: $(pwd)" && \
9999
echo "TARGETARCH: $TARGETARCH" && \
100100
ls -la candle-binding/src/ && \
@@ -107,9 +107,9 @@ RUN echo "Building Rust library with actual source code..." && \
107107
export CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc; \
108108
export CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++; \
109109
export AR_aarch64_unknown_linux_gnu=aarch64-linux-gnu-ar; \
110-
cargo build --release --target aarch64-unknown-linux-gnu; \
110+
cargo build --release --no-default-features --target aarch64-unknown-linux-gnu; \
111111
else \
112-
cargo build --release --target x86_64-unknown-linux-gnu; \
112+
cargo build --release --no-default-features --target x86_64-unknown-linux-gnu; \
113113
fi && \
114114
echo "Checking built library..." && \
115115
find target -name "*.so" -type f

candle-binding/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ license = "MIT OR Apache-2.0"
99
name = "candle_semantic_router"
1010
crate-type = ["staticlib", "cdylib"]
1111

12+
[features]
13+
default = ["cuda"]
14+
cuda = ["candle-core/cuda", "candle-nn/cuda", "candle-transformers/cuda"]
15+
1216
[dependencies]
1317
anyhow = { version = "1", features = ["backtrace"] }
14-
candle-core = { version = "0.8.4", features = ["cuda"] }
15-
candle-nn = { version = "0.8.4", features = ["cuda"] }
16-
candle-transformers = { version = "0.8.4", features = ["cuda"] }
18+
candle-core = "0.8.4"
19+
candle-nn = "0.8.4"
20+
candle-transformers = "0.8.4"
1721
tokenizers = { version = "0.21.0", features = ["http"] }
1822
hf-hub = "0.4.1"
1923
safetensors = "0.4.1"

tools/make/rust.mk

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ test-jailbreak-classifier: rust ## Test jailbreak classifier with candle-binding
2828
@export LD_LIBRARY_PATH=${PWD}/candle-binding/target/release && \
2929
cd src/training/prompt_guard_fine_tuning && CGO_ENABLED=1 go run jailbreak_classifier_verifier.go
3030

31-
# Build the Rust library
32-
rust: ## Ensure Rust is installed and build the Rust library
31+
# Build the Rust library (with CUDA by default)
32+
rust: ## Ensure Rust is installed and build the Rust library with CUDA support
3333
@$(LOG_TARGET)
3434
@bash -c 'if ! command -v rustc >/dev/null 2>&1; then \
3535
echo "rustc not found, installing..."; \
@@ -42,5 +42,22 @@ rust: ## Ensure Rust is installed and build the Rust library
4242
if ! command -v cargo >/dev/null 2>&1; then \
4343
echo "Error: cargo not found in PATH" && exit 1; \
4444
fi && \
45-
echo "Building Rust library..." && \
45+
echo "Building Rust library with CUDA support..." && \
4646
cd candle-binding && cargo build --release'
47+
48+
# Build the Rust library without CUDA (for CI/CD environments)
49+
rust-ci: ## Build the Rust library without CUDA support (for GitHub Actions/CI)
50+
@$(LOG_TARGET)
51+
@bash -c 'if ! command -v rustc >/dev/null 2>&1; then \
52+
echo "rustc not found, installing..."; \
53+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y; \
54+
fi && \
55+
if [ -f "$$HOME/.cargo/env" ]; then \
56+
echo "Loading Rust environment from $$HOME/.cargo/env..." && \
57+
. $$HOME/.cargo/env; \
58+
fi && \
59+
if ! command -v cargo >/dev/null 2>&1; then \
60+
echo "Error: cargo not found in PATH" && exit 1; \
61+
fi && \
62+
echo "Building Rust library without CUDA (CPU-only)..." && \
63+
cd candle-binding && cargo build --release --no-default-features'

0 commit comments

Comments
 (0)