-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
830 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Cargo.lock | ||
target/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
[package] | ||
name = "inference" | ||
version = "0.1.0" | ||
version.workspace = true | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
async-trait.workspace = true | ||
candle.workspace = true | ||
candle-nn.workspace = true | ||
candle-transformers.workspace = true | ||
candle.workspace = true | ||
clap.workspace = true | ||
ed25519-consensus.workspace = true | ||
hf-hub = { workspace = true, features = ["tokio"] } | ||
image = { workspace = true } | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = "1.0.114" | ||
thiserror.workspace = true | ||
tokenizers.workspace = true | ||
tokenizers = { workspace = true, features = ["onig"] } | ||
tokio = { workspace = true, features = ["full", "tracing"] } | ||
tracing.workspace = true | ||
llama_cpp = "0.3.1" | ||
|
||
[features] | ||
cuda = ["candle/cuda", "candle-nn/cuda"] | ||
metal = ["candle/metal", "candle-nn/metal"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
pub mod stable_diffusion; | ||
pub mod token_output_stream; | ||
|
||
use std::path::PathBuf; | ||
|
||
use candle::{ | ||
utils::{cuda_is_available, metal_is_available}, | ||
Device, Tensor, | ||
}; | ||
use tracing::info; | ||
|
||
use crate::models::ModelError; | ||
|
||
pub trait CandleModel { | ||
type Fetch; | ||
type Input; | ||
fn fetch(fetch: &Self::Fetch) -> Result<(), ModelError>; | ||
fn inference(input: Self::Input) -> Result<Vec<Tensor>, ModelError>; | ||
} | ||
|
||
pub fn device() -> Result<Device, candle::Error> { | ||
if cuda_is_available() { | ||
info!("Using CUDA"); | ||
Device::new_cuda(0) | ||
} else if metal_is_available() { | ||
info!("Using Metal"); | ||
Device::new_metal(0) | ||
} else { | ||
info!("Using Cpu"); | ||
Ok(Device::Cpu) | ||
} | ||
} | ||
|
||
pub fn hub_load_safetensors( | ||
repo: &hf_hub::api::sync::ApiRepo, | ||
json_file: &str, | ||
) -> candle::Result<Vec<std::path::PathBuf>> { | ||
let json_file = repo.get(json_file).map_err(candle::Error::wrap)?; | ||
let json_file = std::fs::File::open(json_file)?; | ||
let json: serde_json::Value = | ||
serde_json::from_reader(&json_file).map_err(candle::Error::wrap)?; | ||
let weight_map = match json.get("weight_map") { | ||
None => candle::bail!("no weight map in {json_file:?}"), | ||
Some(serde_json::Value::Object(map)) => map, | ||
Some(_) => candle::bail!("weight map in {json_file:?} is not a map"), | ||
}; | ||
let mut safetensors_files = std::collections::HashSet::new(); | ||
for value in weight_map.values() { | ||
if let Some(file) = value.as_str() { | ||
safetensors_files.insert(file.to_string()); | ||
} | ||
} | ||
let safetensors_files = safetensors_files | ||
.iter() | ||
.map(|v| repo.get(v).map_err(candle::Error::wrap)) | ||
.collect::<candle::Result<Vec<_>>>()?; | ||
Ok(safetensors_files) | ||
} | ||
|
||
pub fn save_image<P: AsRef<std::path::Path>>(img: &Tensor, p: P) -> candle::Result<()> { | ||
let p = p.as_ref(); | ||
let (channel, height, width) = img.dims3()?; | ||
if channel != 3 { | ||
candle::bail!("save_image expects an input of shape (3, height, width)") | ||
} | ||
let img = img.permute((1, 2, 0))?.flatten_all()?; | ||
let pixels = img.to_vec1::<u8>()?; | ||
let image: image::ImageBuffer<image::Rgb<u8>, Vec<u8>> = | ||
match image::ImageBuffer::from_raw(width as u32, height as u32, pixels) { | ||
Some(image) => image, | ||
None => candle::bail!("error saving image {p:?}"), | ||
}; | ||
image.save(p).map_err(candle::Error::wrap)?; | ||
Ok(()) | ||
} |
Oops, something went wrong.