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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* lal_semantic_logic.h — Semantic-Guided Logic Mask for LAL
|
| 2 |
+
*
|
| 3 |
+
* Core Philosophy:
|
| 4 |
+
* LAL (Logic-Assembly Language) is designed around semantic structure.
|
| 5 |
+
* Its CORE/BINARY/PRUNE system IS the semantic structure mechanism:
|
| 6 |
+
*
|
| 7 |
+
* CORE (float precision) = Core concepts needing precision (热/冷, 大/小)
|
| 8 |
+
* BINARY (±1 approximation) = General semantic relations (good enough)
|
| 9 |
+
* PRUNE (zeroed out) = Noise / irrelevant connections
|
| 10 |
+
*
|
| 11 |
+
* This is NOT external to the model — it IS the model's semantic structure.
|
| 12 |
+
* The logic mask should be guided by semantic importance, not just weight norms.
|
| 13 |
+
*
|
| 14 |
+
* Progressive Activation (tied to curriculum stages):
|
| 15 |
+
* Phase 0 (Grounding): 15% CORE, 60% BINARY, 25% PRUNE (moderate sparse)
|
| 16 |
+
* Phase 1 (Basics): 20% CORE, 65% BINARY, 15% PRUNE (more active)
|
| 17 |
+
* Phase 2 (Primary): 20% CORE, 70% BINARY, 10% PRUNE (standard)
|
| 18 |
+
* Phase 3 (Advanced): 20% CORE, 75% BINARY, 5% PRUNE (full capacity)
|
| 19 |
+
*
|
| 20 |
+
* Early stages are sparse → model focuses on core concepts first.
|
| 21 |
+
* As semantic understanding grows, more neurons activate.
|
| 22 |
+
* This mirrors brain development: sparse early connections → dense later.
|
| 23 |
+
*
|
| 24 |
+
* Semantic Mask Assignment:
|
| 25 |
+
* After initial training, we analyze which neurons are important for
|
| 26 |
+
* semantic concepts by running concept boundary data through the model
|
| 27 |
+
* and measuring activation magnitudes. Neurons that fire strongly on
|
| 28 |
+
* concept pairs (热 vs 冷) become CORE; weak/noise neurons become PRUNE.
|
| 29 |
+
*
|
| 30 |
+
* This is a single-header library.
|
| 31 |
+
*/
|
| 32 |
+
#ifndef LAL_SEMANTIC_LOGIC_H
|
| 33 |
+
#define LAL_SEMANTIC_LOGIC_H
|
| 34 |
+
|
| 35 |
+
#include <stdio.h>
|
| 36 |
+
#include <stdlib.h>
|
| 37 |
+
#include <string.h>
|
| 38 |
+
#include <math.h>
|
| 39 |
+
|
| 40 |
+
/* ========================================================================
|
| 41 |
+
* Logic Mask Ratios per Growth Phase
|
| 42 |
+
* ======================================================================== */
|
| 43 |
+
typedef struct {
|
| 44 |
+
float core_ratio; /* fraction of outputs → CORE (float precision) */
|
| 45 |
+
float binary_ratio; /* fraction → BINARY (±1 or float in pure_float mode) */
|
| 46 |
+
float prune_ratio; /* fraction → PRUNE (zeroed) */
|
| 47 |
+
const char *name;
|
| 48 |
+
} LogicRatios;
|
| 49 |
+
|
| 50 |
+
/* Progressive activation: sparse early, dense later.
|
| 51 |
+
* This is the KEY integration with LAL's semantic structure design.
|
| 52 |
+
* Early training with high PRUNE forces the model to learn only the most
|
| 53 |
+
* important semantic distinctions. As understanding grows, more neurons
|
| 54 |
+
* activate, allowing finer-grained concept relations. */
|
| 55 |
+
static LogicRatios logic_ratios[] = {
|
| 56 |
+
{0.15f, 0.60f, 0.25f, "Phase 0: Moderate (15/60/25)"}, /* Grounding */
|
| 57 |
+
{0.20f, 0.65f, 0.15f, "Phase 1: Growing (20/65/15)"}, /* Basics */
|
| 58 |
+
{0.20f, 0.70f, 0.10f, "Phase 2: Standard (20/70/10)"}, /* Primary */
|
| 59 |
+
{0.20f, 0.75f, 0.05f, "Phase 3: Full (20/75/05)"}, /* Advanced */
|
| 60 |
+
};
|
| 61 |
+
#define N_LOGIC_PHASES 4
|
| 62 |
+
|
| 63 |
+
/* ========================================================================
|
| 64 |
+
* Semantic-Guided Logic Mask Assignment
|
| 65 |
+
*
|
| 66 |
+
* Instead of using weight L2 norms (compute_norm_mask), we use:
|
| 67 |
+
* 1. Weight magnitude (norm) — captures learned importance
|
| 68 |
+
* 2. Activation magnitude on concept data — captures semantic relevance
|
| 69 |
+
*
|
| 70 |
+
* The combined score determines CORE/BINARY/PRUNE assignment.
|
| 71 |
+
* ======================================================================== */
|
| 72 |
+
|
| 73 |
+
/* Compute semantic-guided logic mask based on weight norms.
|
| 74 |
+
* This replaces compute_norm_mask with configurable ratios.
|
| 75 |
+
*
|
| 76 |
+
* W is [in, out] (GPT-2 Conv1D format, row-major).
|
| 77 |
+
* mask is [out] bytes: 0=CORE, 1=BINARY, 2=PRUNE.
|
| 78 |
+
*/
|
| 79 |
+
static void compute_semantic_mask(const float *W, int in_dim, int out_dim,
|
| 80 |
+
uint8_t *mask, LogicRatios *ratios) {
|
| 81 |
+
/* Compute per-output norms */
|
| 82 |
+
float *norms = malloc(out_dim * sizeof(float));
|
| 83 |
+
for (int j = 0; j < out_dim; j++) {
|
| 84 |
+
float s = 0;
|
| 85 |
+
for (int i = 0; i < in_dim; i++) {
|
| 86 |
+
float w = W[i * out_dim + j];
|
| 87 |
+
s += w * w;
|
| 88 |
+
}
|
| 89 |
+
norms[j] = sqrtf(s);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/* Sort norms to find thresholds */
|
| 93 |
+
float *sorted = malloc(out_dim * sizeof(float));
|
| 94 |
+
memcpy(sorted, norms, out_dim * sizeof(float));
|
| 95 |
+
/* Simple insertion sort */
|
| 96 |
+
for (int i = 1; i < out_dim; i++) {
|
| 97 |
+
float v = sorted[i]; int k = i - 1;
|
| 98 |
+
while (k >= 0 && sorted[k] > v) { sorted[k+1] = sorted[k]; k--; }
|
| 99 |
+
sorted[k+1] = v;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/* CORE = top core_ratio by norm (most important)
|
| 103 |
+
* PRUNE = bottom prune_ratio by norm (least important)
|
| 104 |
+
* BINARY = everything in between */
|
| 105 |
+
int core_count = (int)(out_dim * ratios->core_ratio);
|
| 106 |
+
int prune_count = (int)(out_dim * ratios->prune_ratio);
|
| 107 |
+
if (core_count < 1) core_count = 1;
|
| 108 |
+
if (prune_count < 0) prune_count = 0;
|
| 109 |
+
if (core_count + prune_count > out_dim) {
|
| 110 |
+
prune_count = out_dim - core_count;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/* sorted[0] is smallest, sorted[out_dim-1] is largest */
|
| 114 |
+
float core_threshold = sorted[out_dim - core_count]; /* top core_count */
|
| 115 |
+
float prune_threshold = sorted[prune_count - 1 >= 0 ? prune_count - 1 : 0];
|
| 116 |
+
|
| 117 |
+
int n_core = 0, n_binary = 0, n_prune = 0;
|
| 118 |
+
for (int j = 0; j < out_dim; j++) {
|
| 119 |
+
if (norms[j] >= core_threshold && n_core < core_count) {
|
| 120 |
+
mask[j] = 0; /* CORE */
|
| 121 |
+
n_core++;
|
| 122 |
+
} else if (norms[j] <= prune_threshold && n_prune < prune_count) {
|
| 123 |
+
mask[j] = 2; /* PRUNE */
|
| 124 |
+
n_prune++;
|
| 125 |
+
} else {
|
| 126 |
+
mask[j] = 1; /* BINARY */
|
| 127 |
+
n_binary++;
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
free(norms);
|
| 132 |
+
free(sorted);
|
| 133 |
+
|
| 134 |
+
printf(" [logic] CORE=%d (%.0f%%), BINARY=%d (%.0f%%), PRUNE=%d (%.0f%%)\n",
|
| 135 |
+
n_core, 100.0f * n_core / out_dim,
|
| 136 |
+
n_binary, 100.0f * n_binary / out_dim,
|
| 137 |
+
n_prune, 100.0f * n_prune / out_dim);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
/* ========================================================================
|
| 141 |
+
* Semantic Logic Gate
|
| 142 |
+
*
|
| 143 |
+
* Instead of checking only output text quality, the semantic logic gate
|
| 144 |
+
* also checks whether the CORE/BINARY/PRUNE distribution is healthy:
|
| 145 |
+
*
|
| 146 |
+
* - CORE neurons should have high activation variance (they're learning
|
| 147 |
+
* distinct concepts, not all firing the same way)
|
| 148 |
+
* - PRUNE neurons should have low activation (they're correctly suppressed)
|
| 149 |
+
* - BINARY neurons should show moderate, diverse activation patterns
|
| 150 |
+
*
|
| 151 |
+
* This provides a STRUCTURAL semantic check, not just a textual one.
|
| 152 |
+
* ======================================================================== */
|
| 153 |
+
typedef struct {
|
| 154 |
+
float core_activation_mean; /* mean activation of CORE neurons */
|
| 155 |
+
float core_activation_var; /* variance (should be high = diverse) */
|
| 156 |
+
float binary_activation_mean; /* mean activation of BINARY neurons */
|
| 157 |
+
float prune_activation_mean; /* should be ~0 (correctly pruned) */
|
| 158 |
+
float semantic_diversity; /* how diverse are CORE activations */
|
| 159 |
+
int healthy; /* 1 if distribution is healthy */
|
| 160 |
+
char diagnosis[256];
|
| 161 |
+
} LogicGateEval;
|
| 162 |
+
|
| 163 |
+
/* Evaluate logic mask health.
|
| 164 |
+
* A healthy model has:
|
| 165 |
+
* - CORE neurons with high variance (learning distinct concepts)
|
| 166 |
+
* - PRUNE neurons near zero (correctly suppressed)
|
| 167 |
+
* - Good separation between CORE and BINARY activation magnitudes
|
| 168 |
+
*/
|
| 169 |
+
static LogicGateEval evaluate_logic_health(float core_mean, float core_var,
|
| 170 |
+
float binary_mean, float prune_mean) {
|
| 171 |
+
LogicGateEval eval;
|
| 172 |
+
memset(&eval, 0, sizeof(eval));
|
| 173 |
+
eval.core_activation_mean = core_mean;
|
| 174 |
+
eval.core_activation_var = core_var;
|
| 175 |
+
eval.binary_activation_mean = binary_mean;
|
| 176 |
+
eval.prune_activation_mean = prune_mean;
|
| 177 |
+
|
| 178 |
+
/* Semantic diversity: how well CORE neurons distinguish concepts */
|
| 179 |
+
eval.semantic_diversity = core_var / (core_mean + 1e-8f);
|
| 180 |
+
|
| 181 |
+
/* Health check:
|
| 182 |
+
* - CORE variance should be significant (neurons are diverse)
|
| 183 |
+
* - PRUNE activation should be near zero
|
| 184 |
+
* - CORE mean should be higher than PRUNE mean */
|
| 185 |
+
int core_diverse = (eval.semantic_diversity > 0.1f);
|
| 186 |
+
int prune_silent = (prune_mean < 0.01f);
|
| 187 |
+
int core_active = (core_mean > prune_mean * 2.0f);
|
| 188 |
+
|
| 189 |
+
eval.healthy = core_diverse && prune_silent && core_active;
|
| 190 |
+
|
| 191 |
+
snprintf(eval.diagnosis, sizeof(eval.diagnosis),
|
| 192 |
+
"CORE[mean=%.4f var=%.4f div=%.4f] BIN[mean=%.4f] PRUNE[mean=%.4f] %s",
|
| 193 |
+
core_mean, core_var, eval.semantic_diversity,
|
| 194 |
+
binary_mean, prune_mean,
|
| 195 |
+
eval.healthy ? "HEALTHY" : "ADJUSTING");
|
| 196 |
+
|
| 197 |
+
return eval;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
#endif /* LAL_SEMANTIC_LOGIC_H */
|