| #ifndef OV_UNIVERSAL_KERNEL_H |
| #define OV_UNIVERSAL_KERNEL_H |
|
|
| #include <vector> |
| #include <cmath> |
| #include <algorithm> |
| #include <iostream> |
|
|
| |
| |
| |
|
|
| namespace OV_Kernel { |
|
|
| |
| struct MemoryContext { |
| std::vector<float> truth_vector; |
| float confidence; |
| bool is_active; |
| }; |
|
|
| |
| |
| |
| static void apply_attention_bias( |
| std::vector<float>& attention_scores, |
| const MemoryContext& mem, |
| int context_start_idx, |
| int context_len, |
| float strength = 10.0f |
| ) { |
| if (!mem.is_active || mem.confidence < 0.5f) return; |
|
|
| |
| for (int i = context_start_idx; i < context_start_idx + context_len; ++i) { |
| if (i < attention_scores.size()) { |
| |
| attention_scores[i] += (strength * mem.confidence); |
| } |
| } |
| } |
|
|
| |
| |
| |
| static void apply_state_correction( |
| std::vector<float>& hidden_state, |
| const MemoryContext& mem, |
| float alpha_base = 0.3f |
| ) { |
| if (!mem.is_active || mem.confidence < 0.5f) return; |
|
|
| float alpha = alpha_base * mem.confidence; |
| |
| for (size_t i = 0; i < hidden_state.size(); ++i) { |
| |
| float current = hidden_state[i]; |
| float target = mem.truth_vector[i]; |
| hidden_state[i] = ((1.0f - alpha) * current) + (alpha * target); |
| } |
| } |
|
|
| |
| |
| |
| |
| static void apply_router_bias( |
| std::vector<float>& router_logits, |
| const std::vector<int>& factual_expert_indices, |
| const MemoryContext& mem, |
| float strength = 5.0f |
| ) { |
| if (!mem.is_active || mem.confidence < 0.6f) return; |
|
|
| for (int expert_idx : factual_expert_indices) { |
| if (expert_idx < router_logits.size()) { |
| router_logits[expert_idx] += (strength * mem.confidence); |
| } |
| } |
| } |
|
|
| |
| |
| |
| static void apply_memory_overwrite( |
| std::vector<std::vector<float>>& memory_bank, |
| const MemoryContext& mem, |
| int write_head_idx |
| ) { |
| if (!mem.is_active || mem.confidence < 0.8f) return; |
|
|
| |
| if (write_head_idx < memory_bank.size()) { |
| memory_bank[write_head_idx] = mem.truth_vector; |
| } |
| } |
| } |
|
|
| #endif |
|
|