diff options
author | laurent <laurent.mazare@gmail.com> | 2023-07-03 08:37:46 +0100 |
---|---|---|
committer | laurent <laurent.mazare@gmail.com> | 2023-07-03 08:37:46 +0100 |
commit | cf2789fb819049cb33a52d73b84a5810cc27cc97 (patch) | |
tree | d2944472bcc6557de5b712aeda280bb476adadf3 /candle-core/examples/llama/weights.rs | |
parent | 9e419641fb5594435ea8f0abd04547db0991c2b2 (diff) | |
download | candle-cf2789fb819049cb33a52d73b84a5810cc27cc97.tar.gz candle-cf2789fb819049cb33a52d73b84a5810cc27cc97.tar.bz2 candle-cf2789fb819049cb33a52d73b84a5810cc27cc97.zip |
Move some safetensors bits in the candle-core crate.
Diffstat (limited to 'candle-core/examples/llama/weights.rs')
-rw-r--r-- | candle-core/examples/llama/weights.rs | 33 |
1 files changed, 2 insertions, 31 deletions
diff --git a/candle-core/examples/llama/weights.rs b/candle-core/examples/llama/weights.rs index 5eff8e21..4ad9b391 100644 --- a/candle-core/examples/llama/weights.rs +++ b/candle-core/examples/llama/weights.rs @@ -1,38 +1,10 @@ use super::*; use candle::{Device, Result, Tensor}; -use half::f16; use memmap2::MmapOptions; -use safetensors::{ - tensor::{Dtype, TensorView}, - SafeTensors, -}; +use safetensors::SafeTensors; use std::fs::File; use std::path::PathBuf; -fn convert(view: TensorView<'_>, device: &Device) -> Result<Tensor> { - match view.dtype() { - Dtype::F16 => { - let v = view.data(); - if (v.as_ptr() as usize) % 2 == 0 { - // SAFETY This is safe because we just checked that this - // was correctly aligned. - let data: &[f16] = - unsafe { std::slice::from_raw_parts(v.as_ptr() as *const f16, v.len() / 2) }; - Tensor::from_slice(data, view.shape(), device)?.to_dtype(DTYPE) - } else { - let mut c = Vec::with_capacity(v.len() / 2); - let mut i = 0; - while i < v.len() { - c.push(f16::from_le_bytes([v[i], v[i + 1]])); - i += 2; - } - Tensor::from_slice(&c, view.shape(), device)?.to_dtype(DTYPE) - } - } - dt => todo!("Unhandled dtype {dt:?}"), - } -} - pub struct VarBuilder<'a> { routing: HashMap<String, usize>, safetensors: Vec<SafeTensors<'a>>, @@ -59,8 +31,7 @@ impl<'a> VarBuilder<'a> { // Unwrap or 0 just to let the proper error flow. let index = self.routing.get(tensor_name).unwrap_or(&0); let view = self.safetensors[*index].tensor(tensor_name).unwrap(); - let tensor = convert(view, &self.device)?; - Ok(tensor) + candle::safetensors::convert(view, &self.device)?.to_dtype(DTYPE) } } |