mirror of
https://github.com/huggingface/candle.git
synced 2025-06-19 03:54:56 +00:00
60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
use candle::{safetensors::SafeTensors, DType, Device, Shape, Tensor};
|
|
use std::collections::HashMap;
|
|
|
|
pub struct VarBuilder<'a> {
|
|
safetensors: Option<(HashMap<String, usize>, Vec<SafeTensors<'a>>)>,
|
|
pub dtype: DType,
|
|
pub device: Device,
|
|
}
|
|
|
|
impl<'a> VarBuilder<'a> {
|
|
pub fn from_safetensors(
|
|
safetensors: Vec<SafeTensors<'a>>,
|
|
dtype: DType,
|
|
device: &Device,
|
|
) -> Self {
|
|
let mut routing = HashMap::new();
|
|
for (index, sf) in safetensors.iter().enumerate() {
|
|
for k in sf.names() {
|
|
routing.insert(k.to_string(), index);
|
|
}
|
|
}
|
|
Self {
|
|
safetensors: Some((routing, safetensors)),
|
|
device: device.clone(),
|
|
dtype,
|
|
}
|
|
}
|
|
|
|
pub fn zeros(dtype: DType, device: Device) -> Self {
|
|
Self {
|
|
safetensors: None,
|
|
device,
|
|
dtype,
|
|
}
|
|
}
|
|
|
|
pub fn get<S: Into<Shape>>(&self, s: S, tensor_name: &str) -> candle::Result<Tensor> {
|
|
let s: Shape = s.into();
|
|
match &self.safetensors {
|
|
None => Tensor::zeros(s, self.dtype, &self.device),
|
|
Some((routing, safetensors)) => {
|
|
// Unwrap or 0 just to let the proper error flow.
|
|
let index = routing.get(tensor_name).unwrap_or(&0);
|
|
let tensor = safetensors[*index]
|
|
.tensor(tensor_name, &self.device)?
|
|
.to_dtype(self.dtype)?;
|
|
if *tensor.shape() != s {
|
|
let msg = format!("shape mismatch for {tensor_name}");
|
|
Err(candle::Error::UnexpectedShape {
|
|
msg,
|
|
expected: s,
|
|
got: tensor.shape().clone(),
|
|
})?
|
|
}
|
|
Ok(tensor)
|
|
}
|
|
}
|
|
}
|
|
}
|