Class: Ignis::AI::NN::RMSNorm
- Defined in:
- lib/nnw/ai/nn/rms_norm.rb
Overview
Root-mean-square layer normalization: y = gamma * x / sqrt(mean(x^2) + eps). Used by Llama, Qwen, Mistral, SmolLM, Phi. Unlike LayerNorm there is no mean-subtraction and no bias — only a learned per-feature scale (gamma).
Instance Attribute Summary collapse
-
#weight ⇒ Tensor
readonly
Gamma (scale), initialized to ones.
Attributes inherited from Module
Instance Method Summary collapse
- #forward(x) ⇒ Tensor
-
#initialize(normalized_shape, eps: 1e-6, device_id: 0) ⇒ RMSNorm
constructor
A new instance of RMSNorm.
- #to_s ⇒ String
Methods inherited from Module
#call, #eval!, #load_state_dict, #named_parameters, #num_parameters, #parameters, #state_dict, #to, #train!, #zero_grad!
Constructor Details
#initialize(normalized_shape, eps: 1e-6, device_id: 0) ⇒ RMSNorm
Returns a new instance of RMSNorm.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nnw/ai/nn/rms_norm.rb', line 18 def initialize(normalized_shape, eps: 1e-6, device_id: 0) super() @normalized_shape = normalized_shape @eps = eps weight_nv = Ignis::Shared::NvArray.new(shape: [normalized_shape], dtype: :float32, device_id: device_id) weight_nv.from_host(Array.new(normalized_shape, 1.0)) @weight = register_parameter("weight", Tensor.new(data: weight_nv, requires_grad: true)) end |
Instance Attribute Details
#weight ⇒ Tensor (readonly)
Returns gamma (scale), initialized to ones.
13 14 15 |
# File 'lib/nnw/ai/nn/rms_norm.rb', line 13 def weight @weight end |
Instance Method Details
#forward(x) ⇒ Tensor
32 33 34 |
# File 'lib/nnw/ai/nn/rms_norm.rb', line 32 def forward(x) x.rms_norm(@weight, eps: @eps) end |
#to_s ⇒ String
37 38 39 |
# File 'lib/nnw/ai/nn/rms_norm.rb', line 37 def to_s "RMSNorm(#{@normalized_shape}, eps=#{@eps})" end |