diff --git a/candle-core/src/cpu_backend/mod.rs b/candle-core/src/cpu_backend/mod.rs index 721127afff..7e848f1dcc 100644 --- a/candle-core/src/cpu_backend/mod.rs +++ b/candle-core/src/cpu_backend/mod.rs @@ -3553,19 +3553,21 @@ impl BackendDevice for CpuDevice { } DType::BF16 => { let mut data = Vec::with_capacity(elem_count); - let uniform = rand::distr::Uniform::new(bf16::from_f64(min), bf16::from_f64(max)) - .map_err(Error::wrap)?; + let normal: rand_distr::Uniform = + rand_distr::Uniform::new(min as f32, max as f32).map_err(Error::wrap)?; for _i in 0..elem_count { - data.push(rng.sample::(uniform)) + let sample: f32 = normal.sample(&mut rng); + data.push(bf16::from_f32(sample)); } Ok(CpuStorage::BF16(data)) } DType::F16 => { let mut data = Vec::with_capacity(elem_count); - let uniform = rand::distr::Uniform::new(f16::from_f64(min), f16::from_f64(max)) - .map_err(Error::wrap)?; + let normal: rand_distr::Uniform = + rand_distr::Uniform::new(min as f32, max as f32).map_err(Error::wrap)?; for _i in 0..elem_count { - data.push(rng.sample::(uniform)) + let sample: f32 = normal.sample(&mut rng); + data.push(f16::from_f32(sample)); } Ok(CpuStorage::F16(data)) } @@ -3610,19 +3612,21 @@ impl BackendDevice for CpuDevice { } DType::BF16 => { let mut data = Vec::with_capacity(elem_count); - let normal = rand_distr::Normal::new(bf16::from_f64(mean), bf16::from_f64(std)) - .map_err(Error::wrap)?; + let normal: rand_distr::Normal = + rand_distr::Normal::new(mean as f32, std as f32).map_err(Error::wrap)?; for _i in 0..elem_count { - data.push(normal.sample(&mut rng)) + let sample: f32 = normal.sample(&mut rng); + data.push(bf16::from_f32(sample)); } Ok(CpuStorage::BF16(data)) } DType::F16 => { let mut data = Vec::with_capacity(elem_count); - let normal = rand_distr::Normal::new(f16::from_f64(mean), f16::from_f64(std)) - .map_err(Error::wrap)?; + let normal: rand_distr::Normal = + rand_distr::Normal::new(mean as f32, std as f32).map_err(Error::wrap)?; for _i in 0..elem_count { - data.push(normal.sample(&mut rng)) + let sample: f32 = normal.sample(&mut rng); + data.push(f16::from_f32(sample)); } Ok(CpuStorage::F16(data)) } diff --git a/candle-core/src/pickle.rs b/candle-core/src/pickle.rs index b054567e30..66150d0ab1 100644 --- a/candle-core/src/pickle.rs +++ b/candle-core/src/pickle.rs @@ -792,7 +792,7 @@ impl PthTensors { /// # Arguments /// * `path` - Path to the pth file. /// * `key` - Optional key to retrieve `state_dict` from the pth file. Sometimes the pth file -/// contains multiple objects and the `state_dict` is the one we are interested in. +/// contains multiple objects and the `state_dict` is the one we are interested in. pub fn read_all_with_key>( path: P, key: Option<&str>, diff --git a/candle-core/tests/quantized_tests.rs b/candle-core/tests/quantized_tests.rs index ab3f15bcf8..6abd1e831e 100644 --- a/candle-core/tests/quantized_tests.rs +++ b/candle-core/tests/quantized_tests.rs @@ -6,7 +6,7 @@ use candle_core::{ DType, Device, IndexOp, Module, Result, Tensor, Var, }; use quantized::{k_quants, GgmlType}; -use rand::prelude::*; +use rand::{prelude::*, random}; const GGML_TEST_SIZE: usize = 32 * 128; @@ -1110,13 +1110,11 @@ fn get_random_tensors( n: usize, device: &Device, ) -> Result<(Tensor, Tensor, Tensor)> { - let mut rng = StdRng::seed_from_u64(314159265358979); - let lhs = (0..m * k) - .map(|_| rng.gen::() - 0.5) + .map(|_| random::() - 0.5) .collect::>(); let rhs = (0..n * k) - .map(|_| rng.gen::() - 0.5) + .map(|_| random::() - 0.5) .collect::>(); let lhs = Tensor::from_vec(lhs, (m, k), device)?; diff --git a/candle-examples/examples/mamba-minimal/model.rs b/candle-examples/examples/mamba-minimal/model.rs index dd1c1aabff..6738590373 100644 --- a/candle-examples/examples/mamba-minimal/model.rs +++ b/candle-examples/examples/mamba-minimal/model.rs @@ -21,7 +21,7 @@ impl Config { } fn dt_rank(&self) -> usize { - (self.d_model + 15) / 16 + self.d_model.div_ceil(16) } fn d_conv(&self) -> usize { diff --git a/candle-examples/examples/metavoice/main.rs b/candle-examples/examples/metavoice/main.rs index 7a7ec3e475..2c5049f816 100644 --- a/candle-examples/examples/metavoice/main.rs +++ b/candle-examples/examples/metavoice/main.rs @@ -6,6 +6,7 @@ extern crate accelerate_src; use anyhow::Result; use clap::Parser; +use rand::distr::Distribution; use std::io::Write; use candle_transformers::generation::LogitsProcessor; @@ -16,7 +17,7 @@ use candle_transformers::models::quantized_metavoice::transformer as qtransforme use candle::{DType, IndexOp, Tensor}; use candle_nn::VarBuilder; use hf_hub::api::sync::Api; -use rand::{distributions::Distribution, SeedableRng}; +use rand::SeedableRng; pub const ENCODEC_NTOKENS: u32 = 1024; @@ -250,7 +251,7 @@ fn main() -> Result<()> { let logits = logits.i(step)?.to_dtype(DType::F32)?; let logits = &(&logits / 1.0)?; let prs = candle_nn::ops::softmax_last_dim(logits)?.to_vec1::()?; - let distr = rand::distributions::WeightedIndex::new(prs.as_slice())?; + let distr = rand::distr::weighted::WeightedIndex::new(prs.as_slice())?; let sample = distr.sample(&mut rng) as u32; codes_.push(sample) } diff --git a/candle-nn/src/loss.rs b/candle-nn/src/loss.rs index 03e8524d6d..7fc349fa0a 100644 --- a/candle-nn/src/loss.rs +++ b/candle-nn/src/loss.rs @@ -7,7 +7,7 @@ use candle::{Result, Tensor}; /// Arguments /// /// * [inp]: The input tensor of dimensions `N, C` where `N` is the batch size and `C` the number -/// of categories. This is expected to contain log probabilities. +/// of categories. This is expected to contain log probabilities. /// * [target]: The ground truth labels as a tensor of u32 of dimension `N`. /// /// The resulting tensor is a scalar containing the average value over the batch. @@ -34,7 +34,7 @@ pub fn nll(inp: &Tensor, target: &Tensor) -> Result { /// Arguments /// /// * [inp]: The input tensor of dimensions `N, C` where `N` is the batch size and `C` the number -/// of categories. This is expected to raw logits. +/// of categories. This is expected to raw logits. /// * [target]: The ground truth labels as a tensor of u32 of dimension `N`. /// /// The resulting tensor is a scalar containing the average value over the batch. @@ -56,9 +56,9 @@ pub fn mse(inp: &Tensor, target: &Tensor) -> Result { /// Arguments /// /// * [inp]: The input tensor of dimensions `N, C` where `N` is the batch size and `C` the number -/// of categories. This is expected to raw logits. +/// of categories. This is expected to raw logits. /// * [target]: The ground truth labels as a tensor of u32 of dimension `N, C` where `N` is the batch size and `C` the number -/// of categories. +/// of categories. /// /// The resulting tensor is a scalar containing the average value over the batch. pub fn binary_cross_entropy_with_logit(inp: &Tensor, target: &Tensor) -> Result { diff --git a/candle-nn/src/var_builder.rs b/candle-nn/src/var_builder.rs index fbfb8200a8..eb07ee3962 100644 --- a/candle-nn/src/var_builder.rs +++ b/candle-nn/src/var_builder.rs @@ -423,7 +423,7 @@ impl SimpleBackend for candle::npy::NpzTensors { } fn contains_tensor(&self, name: &str) -> bool { - self.get(name).map_or(false, |v| v.is_some()) + self.get(name).is_ok_and(|v| v.is_some()) } } @@ -461,7 +461,7 @@ impl SimpleBackend for candle::pickle::PthTensors { } fn contains_tensor(&self, name: &str) -> bool { - self.get(name).map_or(false, |v| v.is_some()) + self.get(name).is_ok_and(|v| v.is_some()) } } diff --git a/candle-transformers/src/generation/mod.rs b/candle-transformers/src/generation/mod.rs index c250a1865f..a450c4d9a1 100644 --- a/candle-transformers/src/generation/mod.rs +++ b/candle-transformers/src/generation/mod.rs @@ -1,5 +1,5 @@ use candle::{DType, Error, Result, Tensor}; -use rand::{distributions::Distribution, SeedableRng}; +use rand::{distr::Distribution, SeedableRng}; #[derive(Clone, PartialEq, Debug)] pub enum Sampling { @@ -45,7 +45,7 @@ impl LogitsProcessor { } fn sample_multinomial(&mut self, prs: &Vec) -> Result { - let distr = rand::distributions::WeightedIndex::new(prs).map_err(Error::wrap)?; + let distr = rand::distr::weighted::WeightedIndex::new(prs).map_err(Error::wrap)?; let next_token = distr.sample(&mut self.rng) as u32; Ok(next_token) } diff --git a/candle-transformers/src/models/dac.rs b/candle-transformers/src/models/dac.rs index fa6c8c7120..ac4b84932a 100644 --- a/candle-transformers/src/models/dac.rs +++ b/candle-transformers/src/models/dac.rs @@ -99,7 +99,7 @@ impl EncoderBlock { let snake1 = Snake1d::new(dim / 2, vb.pp(3))?; let cfg1 = Conv1dConfig { stride, - padding: (stride + 1) / 2, + padding: stride.div_ceil(2), ..Default::default() }; let conv1 = encodec::conv1d_weight_norm(dim / 2, dim, 2 * stride, cfg1, vb.pp(4))?; @@ -191,7 +191,7 @@ impl DecoderBlock { let snake1 = Snake1d::new(in_dim, vb.pp(0))?; let cfg = ConvTranspose1dConfig { stride, - padding: (stride + 1) / 2, + padding: stride.div_ceil(2), ..Default::default() }; let conv_tr1 = encodec::conv_transpose1d_weight_norm( diff --git a/candle-transformers/src/models/flux/sampling.rs b/candle-transformers/src/models/flux/sampling.rs index f3f0eafd4b..cdfef043ed 100644 --- a/candle-transformers/src/models/flux/sampling.rs +++ b/candle-transformers/src/models/flux/sampling.rs @@ -6,8 +6,8 @@ pub fn get_noise( width: usize, device: &Device, ) -> Result { - let height = (height + 15) / 16 * 2; - let width = (width + 15) / 16 * 2; + let height = height.div_ceil(16) * 2; + let width = width.div_ceil(16) * 2; Tensor::randn(0f32, 1., (num_samples, 16, height, width), device) } @@ -84,8 +84,8 @@ pub fn get_schedule(num_steps: usize, shift: Option<(usize, f64, f64)>) -> Vec Result { let (b, _h_w, c_ph_pw) = xs.dims3()?; - let height = (height + 15) / 16; - let width = (width + 15) / 16; + let height = height.div_ceil(16); + let width = width.div_ceil(16); xs.reshape((b, height, width, c_ph_pw / 4, 2, 2))? // (b, h, w, c, ph, pw) .permute((0, 3, 1, 4, 2, 5))? // (b, c, h, ph, w, pw) .reshape((b, c_ph_pw / 4, height * 2, width * 2)) diff --git a/candle-transformers/src/models/mamba.rs b/candle-transformers/src/models/mamba.rs index 1b00230d98..1946fcf491 100644 --- a/candle-transformers/src/models/mamba.rs +++ b/candle-transformers/src/models/mamba.rs @@ -22,7 +22,7 @@ impl Config { } fn dt_rank(&self) -> usize { - (self.d_model + 15) / 16 + self.d_model.div_ceil(16) } fn d_inner(&self) -> usize { diff --git a/candle-transformers/src/models/metavoice.rs b/candle-transformers/src/models/metavoice.rs index ec382711cd..77bbd9e457 100644 --- a/candle-transformers/src/models/metavoice.rs +++ b/candle-transformers/src/models/metavoice.rs @@ -714,7 +714,7 @@ pub mod transformer { None => { let hidden_dim = self.dim * 4; let n_hidden = ((2 * hidden_dim) as f64 / 3.) as usize; - (n_hidden + 255) / 256 * 256 + n_hidden.div_ceil(256) * 256 } } } diff --git a/candle-transformers/src/models/whisper/audio.rs b/candle-transformers/src/models/whisper/audio.rs index 35f9f3df5f..c793278828 100644 --- a/candle-transformers/src/models/whisper/audio.rs +++ b/candle-transformers/src/models/whisper/audio.rs @@ -198,7 +198,7 @@ pub fn log_mel_spectrogram_( let samples = { let mut samples_padded = samples.to_vec(); let to_add = n_len * fft_step - samples.len(); - samples_padded.extend(std::iter::repeat(zero).take(to_add)); + samples_padded.extend(std::iter::repeat_n(zero, to_add)); samples_padded }; diff --git a/candle-wasm-examples/whisper/src/audio.rs b/candle-wasm-examples/whisper/src/audio.rs index b87f7df187..d3c0bb7ed6 100644 --- a/candle-wasm-examples/whisper/src/audio.rs +++ b/candle-wasm-examples/whisper/src/audio.rs @@ -177,7 +177,7 @@ fn log_mel_spectrogram_( let samples = { let mut samples_padded = samples.to_vec(); let to_add = n_len * fft_step - samples.len(); - samples_padded.extend(std::iter::repeat(zero).take(to_add)); + samples_padded.extend(std::iter::repeat_n(zero, to_add)); samples_padded }; diff --git a/candle-wasm-examples/whisper/src/worker.rs b/candle-wasm-examples/whisper/src/worker.rs index f5c09baead..4c98512da9 100644 --- a/candle-wasm-examples/whisper/src/worker.rs +++ b/candle-wasm-examples/whisper/src/worker.rs @@ -3,7 +3,7 @@ use anyhow::Error as E; use candle::{safetensors::Load, DType, Device, IndexOp, Tensor, D}; use candle_nn::{ops::softmax, VarBuilder}; pub use candle_transformers::models::whisper::{self as m, Config}; -use rand::{distributions::Distribution, rngs::StdRng, SeedableRng}; +use rand::{distr::Distribution, rngs::StdRng, SeedableRng}; use serde::{Deserialize, Serialize}; use tokenizers::Tokenizer; use wasm_bindgen::prelude::*; @@ -221,7 +221,7 @@ impl Decoder { let next_token = if t > 0f64 { let prs = softmax(&(&logits / t)?, 0)?; let logits_v: Vec = prs.to_vec1()?; - let distr = rand::distributions::WeightedIndex::new(&logits_v)?; + let distr = rand::distr::weighted::WeightedIndex::new(&logits_v)?; distr.sample(&mut self.rng) as u32 } else { let logits_v: Vec = logits.to_vec1()?;