Skip to content

Commit 9fe6232

Browse files
authored
Fix single file binary builder to only run when env var is set (#3126)
1 parent 41fa5f1 commit 9fe6232

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

candle-examples/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tokenizers = { workspace = true, features = ["onig"] }
4141
cpal = { version = "0.15.2", optional = true }
4242
pdf2image = { version = "0.1.2", optional = true }
4343
tekken-rs = { version = "0.1.1", optional = true }
44-
bert-single-file-binary-builder = { path = "bert_single_file_binary_builder", optional = true }
44+
single-file-binary-builder = { path = "single-file-binary-builder", optional = true }
4545

4646
[dev-dependencies]
4747
anyhow = { workspace = true }
@@ -92,7 +92,7 @@ mimi = ["cpal", "symphonia", "rubato"]
9292
snac = ["cpal", "symphonia", "rubato"]
9393
depth_anything_v2 = ["palette", "enterpolation"]
9494
tekken = ["tekken-rs"]
95-
bert-single-file-binary-builder = ["dep:bert-single-file-binary-builder"]
95+
single-file-binary-builder = ["dep:single-file-binary-builder"]
9696

9797
[[example]]
9898
name = "llama_multiprocess"
@@ -160,4 +160,4 @@ required-features = ["symphonia"]
160160

161161
[[example]]
162162
name = "bert_single_file_binary"
163-
required-features = ["bert-single-file-binary-builder"]
163+
required-features = ["single-file-binary-builder"]

candle-examples/examples/bert_single_file_binary/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
This is an adapted version of the Candle Bert example to inline (embed) the model files into the binary to create a single file binary.
44

5-
**Note: the bert-single-file-binary-builder feature is required `--features="bert-single-file-binary-builder"`.**
5+
**Note: the single-file-binary-builder feature is required `--features="single-file-binary-builder"`.**
66

77
### Limitations
88

9-
1. Because the model files must be available at compile time, a special build step is needed. See the [bert-single-file-binary-builder crate](../../bert_single_file_binary_builder/)
10-
2. The model id and revision is hardcoded
11-
3. Since the [`include_bytes!`](https://doc.rust-lang.org/std/macro.include_bytes.html) marco is project relative and requires the argument must be a string literal, it is easier to download the files into the examples dir than navigate the hub cache dir snapshots.
9+
1. Because the model files must be available at compile time, a special build step is needed. See the [single-file-binary-builder crate](../../single_file_binary_builder/)
10+
2. Since the [`include_bytes!`](https://doc.rust-lang.org/std/macro.include_bytes.html) marco is project relative and requires the argument must be a string literal, it is easier to download the files into the examples dir than navigate the hub cache dir snapshots.
1211

1312
## Running the example
1413

1514
```bash
1615
cd path/to/candle/candle-examples
17-
cargo build --example bert_single_file_binary --release --features="bert-single-file-binary-builder"
16+
CANDLE_SINGLE_FILE_BINARY_BUILDER_URL="https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/c9745ed1d9f207416be6d2e6f8de32d1f16199bf" cargo build --example bert_single_file_binary --release --features="single-file-binary-builder"
1817
../target/release/examples/bert_single_file_binary --prompt "Here is a test sentence"
1918
```
2019

candle-examples/examples/bert_single_file_binary/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct Args {
3939
approximate_gelu: bool,
4040
}
4141

42+
// Remember to set env variable before running.
43+
// Use specific commit vs main to reduce chance of URL breaking later from directory layout changes, etc.
44+
// CANDLE_SINGLE_FILE_BINARY_BUILDER_URL="https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/c9745ed1d9f207416be6d2e6f8de32d1f16199bf"
45+
// cargo run --example bert_single_file_binary
4246
fn main() -> Result<()> {
4347
use tracing_chrome::ChromeLayerBuilder;
4448
use tracing_subscriber::prelude::*;
@@ -171,13 +175,11 @@ fn main() -> Result<()> {
171175
}
172176

173177
pub fn build_model_and_tokenizer_from_bytes(device: &Device) -> Result<(BertModel, Tokenizer)> {
174-
let config_data = include_bytes!("../../bert_single_file_binary_builder/files/config.json");
178+
let config_data = include_bytes!("../../single-file-binary-builder/files/config.json");
175179

176-
let tokenizer_data =
177-
include_bytes!("../../bert_single_file_binary_builder/files/tokenizer.json");
180+
let tokenizer_data = include_bytes!("../../single-file-binary-builder/files/tokenizer.json");
178181

179-
let weights_data =
180-
include_bytes!("../../bert_single_file_binary_builder/files/model.safetensors");
182+
let weights_data = include_bytes!("../../single-file-binary-builder/files/model.safetensors");
181183

182184
let config_string = std::str::from_utf8(config_data)?;
183185
let config: BertConfig = serde_json::from_str(config_string)?;

candle-examples/bert_single_file_binary_builder/Cargo.toml renamed to candle-examples/single-file-binary-builder/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "bert-single-file-binary-builder"
2+
name = "single-file-binary-builder"
33
version.workspace = true
44
edition.workspace = true
55
description.workspace = true
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# candle_bert_single_file_binary_builder
1+
# candle_single_file_binary_builder
22

33
This crate provides and isolates the necessary build steps to fetch the model files for the [`bert_single_file_binary` example](../examples/bert_single_file_binary/). See [https://github.com/huggingface/candle/pull/3104#issuecomment-3369276760](https://github.com/huggingface/candle/pull/3104#issuecomment-3369276760) for background.
44

55
### Limitations
66

77
1. Because the model files must be available at compile time, a special build step is needed
8-
2. The model id and revision is hardcoded
9-
3. The model files are downloaded from directly Hugging Face at compile time for simplicity sake, not using the hf-hub library
8+
2. The model files are downloaded from directly Hugging Face at compile time for simplicity sake, not using the hf-hub library
109
1. Since the file paths must be known at compile time it is easier to download the files into the example dir than navigate the hub cache dir snapshots.

candle-examples/bert_single_file_binary_builder/build.rs renamed to candle-examples/single-file-binary-builder/build.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ use std::{
77
use anyhow::{Context, Result};
88

99
fn main() -> Result<()> {
10-
println!("cargo:rerun-if-changed=build.rs");
10+
let base_url = core::option_env!("CANDLE_SINGLE_FILE_BINARY_BUILDER_URL");
11+
if base_url.is_none() {
12+
return Ok(());
13+
}
14+
let base_url = base_url.unwrap();
1115

12-
// Use specific commit vs main to reduce chance of URL breaking later from directory layout changes, etc.
13-
let base_url = "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/c9745ed1d9f207416be6d2e6f8de32d1f16199bf";
16+
println!("cargo::rerun-if-changed=build.rs");
1417

1518
let example_name = "bert-single-file-binary-builder";
1619
let dest_path = Path::new("files");
@@ -22,26 +25,26 @@ fn main() -> Result<()> {
2225

2326
if all_files_exist {
2427
println!(
25-
"cargo:warning=All {} files already exist, skipping download",
28+
"cargo::warning=All {} files already exist, skipping download",
2629
example_name
2730
);
2831
return Ok(());
2932
}
3033

31-
println!("cargo:warning=Downloading {} files...", example_name);
34+
println!("cargo::warning=Downloading {} files...", example_name);
3235

3336
fs::create_dir_all(dest_path).context("Failed to create destination directory")?;
3437

3538
for filename in &files {
3639
let dest_file = dest_path.join(filename);
3740

3841
if dest_file.exists() {
39-
println!("cargo:warning=File already exists, skipping: {}", filename);
42+
println!("cargo::warning=File already exists, skipping: {}", filename);
4043
continue;
4144
}
4245

4346
let url = format!("{}/{}", base_url, filename);
44-
println!("cargo:warning=Downloading {} from {}...", filename, url);
47+
println!("cargo::warning=Downloading {} from {}...", filename, url);
4548

4649
let response = ureq::get(&url)
4750
.call()
@@ -63,13 +66,13 @@ fn main() -> Result<()> {
6366
copy(&mut reader, &mut file).context(format!("Failed to write {}", filename))?;
6467

6568
println!(
66-
"cargo:warning=Downloaded {} ({} bytes)",
69+
"cargo::warning=Downloaded {} ({} bytes)",
6770
filename, bytes_written
6871
);
6972
}
7073

7174
println!(
72-
"cargo:warning=All {} files downloaded successfully",
75+
"cargo::warning=All {} files downloaded successfully",
7376
example_name
7477
);
7578

0 commit comments

Comments
 (0)