Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions candle-core/src/cpu_backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32> =
rand_distr::Uniform::new(min as f32, max as f32).map_err(Error::wrap)?;
for _i in 0..elem_count {
data.push(rng.sample::<bf16, _>(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<f32> =
rand_distr::Uniform::new(min as f32, max as f32).map_err(Error::wrap)?;
for _i in 0..elem_count {
data.push(rng.sample::<f16, _>(uniform))
let sample: f32 = normal.sample(&mut rng);
data.push(f16::from_f32(sample));
}
Ok(CpuStorage::F16(data))
}
Expand Down Expand Up @@ -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<f32> =
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<f32> =
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))
}
Expand Down
2 changes: 1 addition & 1 deletion candle-core/src/pickle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<std::path::Path>>(
path: P,
key: Option<&str>,
Expand Down
8 changes: 3 additions & 5 deletions candle-core/tests/quantized_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<f32>() - 0.5)
.map(|_| random::<f32>() - 0.5)
.collect::<Vec<_>>();
let rhs = (0..n * k)
.map(|_| rng.gen::<f32>() - 0.5)
.map(|_| random::<f32>() - 0.5)
.collect::<Vec<_>>();

let lhs = Tensor::from_vec(lhs, (m, k), device)?;
Expand Down
2 changes: 1 addition & 1 deletion candle-examples/examples/mamba-minimal/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions candle-examples/examples/metavoice/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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::<f32>()?;
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)
}
Expand Down
8 changes: 4 additions & 4 deletions candle-nn/src/loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -34,7 +34,7 @@ pub fn nll(inp: &Tensor, target: &Tensor) -> 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 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.
Expand All @@ -56,9 +56,9 @@ pub fn mse(inp: &Tensor, target: &Tensor) -> 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 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<Tensor> {
Expand Down
4 changes: 2 additions & 2 deletions candle-nn/src/var_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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())
}
}

Expand Down
4 changes: 2 additions & 2 deletions candle-transformers/src/generation/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl LogitsProcessor {
}

fn sample_multinomial(&mut self, prs: &Vec<f32>) -> Result<u32> {
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)
}
Expand Down
4 changes: 2 additions & 2 deletions candle-transformers/src/models/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions candle-transformers/src/models/flux/sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub fn get_noise(
width: usize,
device: &Device,
) -> Result<Tensor> {
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)
}

Expand Down Expand Up @@ -84,8 +84,8 @@ pub fn get_schedule(num_steps: usize, shift: Option<(usize, f64, f64)>) -> Vec<f

pub fn unpack(xs: &Tensor, height: usize, width: usize) -> Result<Tensor> {
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))
Expand Down
2 changes: 1 addition & 1 deletion candle-transformers/src/models/mamba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion candle-transformers/src/models/metavoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion candle-transformers/src/models/whisper/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub fn log_mel_spectrogram_<T: Float>(
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
};

Expand Down
2 changes: 1 addition & 1 deletion candle-wasm-examples/whisper/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn log_mel_spectrogram_<T: Float + std::fmt::Display>(
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
};

Expand Down
4 changes: 2 additions & 2 deletions candle-wasm-examples/whisper/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Decoder {
let next_token = if t > 0f64 {
let prs = softmax(&(&logits / t)?, 0)?;
let logits_v: Vec<f32> = 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<f32> = logits.to_vec1()?;
Expand Down
Loading