diffusion_rs_common/nn/
layer_norm.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Layer Normalization.
//!
//! This layer applies Layer Normalization over a mini-batch of inputs as described in [`Layer
//! Normalization`]. The input is expected to have three dimensions: a batch dimension, a length,
//! and a hidden size, the normalization is applied over the last dimension.
//!
//! # Example
//!
//! ```rust
//! use diffusion_rs_common::core::{Tensor, Device::Cpu, test_utils::to_vec3_round};
//! use diffusion_rs_common::nn::{LayerNorm, Module};
//! # fn main() -> diffusion_rs_common::core::Result<()> {
//!
//! let w = Tensor::new(&[1f32, 1f32, 1f32], &Cpu)?;
//! let b = Tensor::new(&[0f32, 0f32, 0f32], &Cpu)?;
//! let layer = LayerNorm::new(w, b, 1e-5);
//!
//! let xs = Tensor::new(
//!     &[[[1f32, 2., 3.], [4., 5., 6.], [9., 8., 7.]]],
//!     &Cpu)?;
//! let ys = layer.forward(&xs)?;
//! assert_eq!(
//!     to_vec3_round(&ys, 4)?,
//!     &[[[-1.2247, 0.0,  1.2247],
//!        [-1.2247, 0.0,  1.2247],
//!        [ 1.2247, 0.0, -1.2247]]]);
//! # Ok(()) }
//! ```
//!
//! [`Layer Normalization`]: https://arxiv.org/abs/1607.06450

use std::marker::PhantomData;

#[cfg(feature = "cuda")]
use crate::core::cuda_backend::{
    cudarc::driver::{DeviceRepr, LaunchAsync, LaunchConfig},
    kernel_name, kernels, CudaDType, WrapErr,
};

#[cfg(feature = "cuda")]
use crate::core::{
    backend::BackendStorage, from_storage_no_op, CudaDevice, CudaStorage, Storage, WithDType,
};

use crate::core::{DType, Device, Module, Result, Tensor, D};

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LayerNormConfig {
    pub eps: f64,
    /// Whether to remove the mean or not, the default is true and when set to false, this turns
    /// this layer into RmsNorm.
    pub remove_mean: bool,
    pub affine: bool,
}

impl Default for LayerNormConfig {
    fn default() -> Self {
        Self {
            eps: 1e-5,
            remove_mean: true,
            affine: true,
        }
    }
}

impl From<f64> for LayerNormConfig {
    fn from(eps: f64) -> Self {
        Self {
            eps,
            remove_mean: true,
            affine: true,
        }
    }
}

// This layer norm version handles both weight and bias so removes the mean.
#[derive(Clone, Debug)]
pub struct LayerNorm {
    weight: Tensor,
    bias: Tensor,
    remove_mean: bool,
    eps: f64,
}

impl LayerNorm {
    pub fn new(weight: Tensor, bias: Tensor, eps: f64) -> Self {
        Self {
            weight,
            bias,
            remove_mean: true,
            eps,
        }
    }

    pub fn new_no_bias(weight: Tensor, eps: f64) -> Self {
        Self {
            weight: weight.clone(),
            bias: Tensor::zeros_like(&weight).unwrap(),
            remove_mean: true,
            eps,
        }
    }

    pub fn rms_norm(weight: Tensor, eps: f64) -> Self {
        Self {
            weight: weight.clone(),
            bias: Tensor::zeros_like(&weight).unwrap(),
            remove_mean: false,
            eps,
        }
    }

    pub fn weight(&self) -> &Tensor {
        &self.weight
    }

    pub fn bias(&self) -> &Tensor {
        &self.bias
    }

    pub fn to_device(&self, dev: &Device) -> Result<Self> {
        Ok(Self {
            weight: self.weight.to_device(dev)?,
            bias: self.bias.to_device(dev)?,
            remove_mean: self.remove_mean,
            eps: self.eps,
        })
    }
}

impl Module for LayerNorm {
    fn forward(&self, x: &Tensor) -> Result<Tensor> {
        if x.is_contiguous() && self.remove_mean {
            return crate::nn::ops::layer_norm(x, &self.weight, &self.bias, self.eps as f32);
        }
        let x_dtype = x.dtype();
        let internal_dtype = match x_dtype {
            DType::F16 | DType::BF16 => DType::F32,
            d => d,
        };
        let hidden_size = x.dim(D::Minus1)?;
        let x = x.to_dtype(internal_dtype)?;
        let x = if self.remove_mean {
            let mean_x = (x.sum_keepdim(D::Minus1)? / hidden_size as f64)?;
            x.broadcast_sub(&mean_x)?
        } else {
            x
        };
        let norm_x = (x.sqr()?.sum_keepdim(D::Minus1)? / hidden_size as f64)?;
        let x_normed = x.broadcast_div(&(norm_x + self.eps)?.sqrt()?)?;
        let x = x_normed.to_dtype(x_dtype)?.broadcast_mul(&self.weight)?;
        x.broadcast_add(&self.bias)
    }
}

pub fn layer_norm<C: Into<LayerNormConfig>>(
    size: usize,
    config: C,
    vb: crate::nn::VarBuilder,
) -> Result<LayerNorm> {
    let config = config.into();
    let weight = vb.get_with_hints(size, "weight", crate::nn::Init::Const(1.))?;
    let bias = if config.affine {
        Some(vb.get_with_hints(size, "bias", crate::nn::Init::Const(0.))?)
    } else {
        None
    };
    Ok(LayerNorm {
        weight: weight.clone(),
        bias: bias.unwrap_or(Tensor::zeros_like(&weight)?),
        remove_mean: config.remove_mean,
        eps: config.eps,
    })
}

// This whole non quantized/quantized RmsNorm is a hack. It seems like quantized works without this impl, but it is slower.
#[derive(Clone, Debug)]
pub struct RmsNormQuantized;
#[derive(Clone, Debug)]
pub struct RmsNormNonQuantized;

/// RmsNorm is a specialized version of the LayerNorm module.
#[derive(Clone, Debug)]
pub struct RmsNorm<T> {
    inner: LayerNorm,
    _ghost: PhantomData<T>,
}

impl RmsNorm<RmsNormNonQuantized> {
    pub fn new(weight: Tensor, eps: f64) -> Self {
        Self {
            inner: LayerNorm::rms_norm(weight, eps),
            _ghost: PhantomData,
        }
    }

    pub fn to_device(&self, dev: &Device) -> Result<Self> {
        Ok(Self {
            inner: self.inner.to_device(dev)?,
            _ghost: PhantomData,
        })
    }
}

impl RmsNorm<RmsNormQuantized> {
    pub fn new(weight: Tensor, eps: f64) -> Self {
        Self {
            inner: LayerNorm::rms_norm(weight, eps),
            _ghost: PhantomData,
        }
    }

    #[cfg(feature = "cuda")]
    fn dtype_execute_rmsnorm<T: CudaDType + DeviceRepr + WithDType, F>(
        &self,
        dev: &CudaDevice,
        eps_converter: F,
        x_storage: &CudaStorage,
        weight_storage: &CudaStorage,
        x: &Tensor,
    ) -> Result<Tensor>
    where
        F: FnOnce(f64) -> T,
    {
        assert!(x.layout().is_contiguous());
        let hidden_size = *x.dims().last().unwrap();
        let elem_count = x.elem_count();
        let num_tokens = elem_count / hidden_size;
        let out = unsafe { dev.alloc::<T>(elem_count) }.w()?;

        let cfg = LaunchConfig {
            grid_dim: (num_tokens as u32, 1, 1),
            block_dim: (u32::min(hidden_size as u32, 1024), 1, 1),
            shared_mem_bytes: 0,
        };

        let func = dev.get_or_load_func(&kernel_name::<T>("rms_norm"), kernels::FUSED_RMS_NORM)?;

        let params = (
            &out,
            x_storage.as_cuda_slice::<T>()?,
            weight_storage.as_cuda_slice::<T>()?,
            eps_converter(self.inner.eps),
            num_tokens as i32,
            hidden_size as i32,
        );
        unsafe { func.launch(cfg, params) }.w()?;

        Ok(from_storage_no_op(
            Storage::Cuda(CudaStorage::wrap_cuda_slice(out, dev.clone())),
            x.shape(),
            false,
        ))
    }

    #[cfg(feature = "cuda")]
    fn fused_rmsnorm(&self, x: &Tensor, dev: &CudaDevice) -> Result<Tensor> {
        match (
            &*x.storage_and_layout().0,
            &*self.inner.weight().storage_and_layout().0,
        ) {
            (Storage::Cuda(x_storage), Storage::Cuda(weight_storage)) => {
                match (x_storage.dtype(), weight_storage.dtype()) {
                    (DType::BF16, DType::BF16) => self.dtype_execute_rmsnorm::<half::bf16, _>(
                        dev,
                        half::bf16::from_f64,
                        x_storage,
                        weight_storage,
                        x,
                    ),
                    (DType::F16, DType::F16) => self.dtype_execute_rmsnorm::<half::f16, _>(
                        dev,
                        half::f16::from_f64,
                        x_storage,
                        weight_storage,
                        x,
                    ),
                    (DType::F32, DType::F32) => self.dtype_execute_rmsnorm::<f32, _>(
                        dev,
                        |x| x as f32,
                        x_storage,
                        weight_storage,
                        x,
                    ),
                    _ => crate::bail!("DType mismatch in fused rmsnorm."),
                }
            }
            _ => unreachable!(),
        }
    }
}

impl<T> RmsNorm<T> {
    pub fn into_inner(self) -> LayerNorm {
        self.inner
    }
    pub fn inner(&self) -> &LayerNorm {
        &self.inner
    }
}

impl Module for RmsNorm<RmsNormNonQuantized> {
    fn forward(&self, xs: &Tensor) -> Result<Tensor> {
        self.inner.forward(xs)
    }
}

impl Module for RmsNorm<RmsNormQuantized> {
    fn forward(&self, xs: &Tensor) -> Result<Tensor> {
        #[cfg(feature = "cuda")]
        match (xs.dtype(), xs.device()) {
            (DType::BF16, Device::Cuda(dev))
            | (DType::F32, Device::Cuda(dev))
            | (DType::F16, Device::Cuda(dev)) => self.fused_rmsnorm(xs, dev),
            _ => self.inner.forward(xs),
        }
        #[cfg(not(feature = "cuda"))]
        {
            self.inner.forward(xs)
        }
    }
}

pub fn rms_norm_non_quant(
    size: usize,
    eps: f64,
    vb: crate::nn::VarBuilder,
) -> Result<RmsNorm<RmsNormNonQuantized>> {
    let config = LayerNormConfig {
        eps,
        remove_mean: false,
        affine: false,
    };
    Ok(RmsNorm {
        inner: layer_norm(size, config, vb)?,
        _ghost: PhantomData,
    })
}

pub fn rms_norm_quant(
    size: usize,
    eps: f64,
    vb: crate::nn::VarBuilder,
) -> Result<RmsNorm<RmsNormQuantized>> {
    let config = LayerNormConfig {
        eps,
        remove_mean: false,
        affine: false,
    };
    Ok(RmsNorm {
        inner: layer_norm(size, config, vb)?,
        _ghost: PhantomData,
    })
}