File size: 1,307 Bytes
26d5b81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef NEUROFLOW_RMS_NORM_HPP
#define NEUROFLOW_RMS_NORM_HPP

#include <cstddef>
#include "tensor.hpp"

#ifdef USE_CUDA
#include <cuda_runtime.h>
#endif

namespace neuroflow {

class RMSNorm {
public:
    size_t dim_;
    float eps_;
    Tensor weight_;

    struct Cache {
        Tensor input;
        Tensor rms;
        Tensor normalized;
    };
    Cache cache_;

    RMSNorm(size_t dim, float eps = 1e-5f);
    Tensor forward(const Tensor& x);

    struct Gradients {
        Tensor weight_grad;
        Tensor input_grad;
    };

    Gradients backward(const Tensor& output_grad);
};

#ifdef USE_CUDA
void launch_rms_norm_forward(float* out, const float* input, const float* weight,

                              float* rms, float* normalized, size_t batch, size_t dim,

                              float eps, cudaStream_t stream);
void launch_rms_norm_backward(float* input_grad, float* weight_grad,

                               const float* output_grad, const float* input,

                               const float* rms, const float* normalized,

                               const float* weight, size_t batch, size_t dim,

                               cudaStream_t stream);
#endif

} // namespace neuroflow

#endif // NEUROFLOW_RMS_NORM_HPP