mirror of
https://github.com/huggingface/candle.git
synced 2025-06-16 18:48:51 +00:00
Fixes for the stable diffusion example. (#342)
* Fixes for the stable diffusion example. * Bugfix. * Another fix. * Fix for group-norm. * More fixes to get SD to work.
This commit is contained in:
@ -59,17 +59,21 @@ impl GroupNorm {
|
||||
let x = x.broadcast_sub(&mean_x)?;
|
||||
let norm_x = (x.sqr()?.sum_keepdim(2)? / hidden_size as f64)?;
|
||||
let x_normed = x.broadcast_div(&(norm_x + self.eps)?.sqrt()?)?;
|
||||
let mut w_dims = vec![1; x_shape.len()];
|
||||
w_dims[1] = n_channels;
|
||||
let weight = self.weight.reshape(w_dims.clone())?;
|
||||
let bias = self.bias.reshape(w_dims)?;
|
||||
x_normed
|
||||
.to_dtype(x_dtype)?
|
||||
.broadcast_mul(&self.weight)?
|
||||
.broadcast_add(&self.bias)?
|
||||
.reshape(x_shape)
|
||||
.reshape(x_shape)?
|
||||
.broadcast_mul(&weight)?
|
||||
.broadcast_add(&bias)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn group_norm(
|
||||
num_channels: usize,
|
||||
num_groups: usize,
|
||||
num_channels: usize,
|
||||
eps: f64,
|
||||
vb: crate::VarBuilder,
|
||||
) -> Result<GroupNorm> {
|
||||
|
@ -30,8 +30,8 @@ use test_utils::to_vec3_round;
|
||||
#[test]
|
||||
fn group_norm() -> Result<()> {
|
||||
let device = &Device::Cpu;
|
||||
let w = Tensor::new(&[1f32], device)?;
|
||||
let b = Tensor::new(&[0f32], device)?;
|
||||
let w = Tensor::from_vec(vec![1f32; 6], 6, device)?;
|
||||
let b = Tensor::from_vec(vec![0f32; 6], 6, device)?;
|
||||
let gn2 = GroupNorm::new(w.clone(), b.clone(), 6, 2, 1e-5)?;
|
||||
let gn3 = GroupNorm::new(w, b, 6, 3, 1e-5)?;
|
||||
|
||||
|
Reference in New Issue
Block a user