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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ========================================================================
|
| 2 |
+
* 高级语义探针系统 — 多层级概念结构监控
|
| 3 |
+
* ========================================================================
|
| 4 |
+
* 设计理念: 让训练从黑箱变透明, 像"认知发展心理学"一样观察模型学到了什么
|
| 5 |
+
*
|
| 6 |
+
* 5 层探针:
|
| 7 |
+
* L1. 基础概念层: 模型是否学会区分基本概念 (反义词/同义词/无关词)
|
| 8 |
+
* L2. 概念聚类层: 概念是否自动形成语义簇 (温度/大小/方向/颜色/情感)
|
| 9 |
+
* L3. 概念组合层: 复杂概念是否由基础概念组成 (火=热+亮+危险)
|
| 10 |
+
* L4. 层间传递层: 概念在 10 层中如何传递和变换
|
| 11 |
+
* L5. 生成质量层: 实际生成文本的质量评估
|
| 12 |
+
* ======================================================================== */
|
| 13 |
+
|
| 14 |
+
/* L1: 基础概念区分 — 扩展探针对, 覆盖更多语义类别 */
|
| 15 |
+
static ConceptPair probe_pairs_extended[] = {
|
| 16 |
+
/* 原有 7 对反义词 */
|
| 17 |
+
{"\xe7\x83\xad", "\xe7\x83\xad", "\xe5\x86\xb7", "\xe5\x86\xb7", "temperature"},
|
| 18 |
+
{"\xe5\xa4\xa7", "\xe5\xa4\xa7", "\xe5\xb0\x8f", "\xe5\xb0\x8f", "size"},
|
| 19 |
+
{"\xe4\xb8\x8a", "\xe4\xb8\x8a", "\xe4\xb8\x8b", "\xe4\xb8\x8b", "position"},
|
| 20 |
+
{"\xe4\xba\xae", "\xe4\xba\xae", "\xe6\x9a\x97", "\xe6\x9a\x97", "light"},
|
| 21 |
+
{"\xe9\x87\x8d", "\xe9\x87\x8d", "\xe8\xbd\xbb", "\xe8\xbd\xbb", "weight"},
|
| 22 |
+
{"\xe5\xbf\xab", "\xe5\xbf\xab", "\xe6\x85\xa2", "\xe6\x85\xa2", "speed"},
|
| 23 |
+
{"\xe6\xb9\xbf", "\xe6\xb9\xbf", "\xe5\xb9\xb2", "\xe5\xb9\xb2", "moisture"},
|
| 24 |
+
/* 新增: 同义词对 (应该高相似度) */
|
| 25 |
+
{"\xe7\x83\xad", "\xe7\x83\xad", "\xe6\x9a\x96", "\xe6\x9a\x96", "syn_hot"}, /* 热-暖 */
|
| 26 |
+
{"\xe5\xa4\xa7", "\xe5\xa4\xa7", "\xe5\xb7\xa8", "\xe5\xb7\xa8", "syn_big"}, /* 大-巨 */
|
| 27 |
+
{"\xe5\xbf\xab", "\xe5\xbf\xab", "\xe9\x80\x9f", "\xe9\x80\x9f", "syn_fast"}, /* 快-速 */
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
/* L2: 概念聚类 — 同类概念应该聚在一起, 不同类应该分开 */
|
| 31 |
+
typedef struct { const char *name; const char *concepts[6]; int n; } ConceptCluster;
|
| 32 |
+
static ConceptCluster probe_clusters[] = {
|
| 33 |
+
{"temp", {"热","冷","暖","凉","温","冰"}, 6},
|
| 34 |
+
{"size", {"大","小","巨","微","长","短"}, 6},
|
| 35 |
+
{"color", {"红","黄","蓝","绿","白","黑"}, 6},
|
| 36 |
+
{"emotion", {"喜","怒","哀","乐","爱","恨"}, 6},
|
| 37 |
+
{"nature", {"水","火","土","木","金","风"}, 6},
|
| 38 |
+
};
|
| 39 |
+
#define N_CLUSTERS (sizeof(probe_clusters) / sizeof(probe_clusters[0]))
|
| 40 |
+
|
| 41 |
+
/* L3: 概念组合 — 复杂概念 = 基础概念的加权组合
|
| 42 |
+
* 火 ≈ 热 + 亮 + 红 (颜色) + 危险
|
| 43 |
+
* 水 ≈ 湿 + 冷 + 透明 + 流动
|
| 44 |
+
* 测量: complex_concept 与 basic_concepts 的余弦相似度排名 */
|
| 45 |
+
typedef struct {
|
| 46 |
+
const char *name;
|
| 47 |
+
const char *complex_concept; /* 复杂概念 */
|
| 48 |
+
const char *basic_concepts[6]; /* 基础概念 */
|
| 49 |
+
int n_basics;
|
| 50 |
+
} CompositionProbe;
|
| 51 |
+
static CompositionProbe composition_probes[] = {
|
| 52 |
+
{"fire", "火", {"热","亮","红","危险","烧","光"}, 6},
|
| 53 |
+
{"water", "水", {"湿","冷","清","流","喝","冰"}, 6},
|
| 54 |
+
{"sun", "太阳", {"热","亮","光","圆","远","黄"}, 6},
|
| 55 |
+
{"tree", "树", {"木","绿","叶","根","高","植物"}, 6},
|
| 56 |
+
};
|
| 57 |
+
#define N_COMPOSITIONS (sizeof(composition_probes) / sizeof(CompositionProbe[0]))
|
| 58 |
+
|
| 59 |
+
/* ========================================================================
|
| 60 |
+
* L2: 概念聚类分析 — 测量同类内聚 vs 异类分离
|
| 61 |
+
* ======================================================================== */
|
| 62 |
+
static void probe_concept_clusters(Model *m) {
|
| 63 |
+
int n_embd = m->cfg.n_embd;
|
| 64 |
+
int n_c = (int)N_CLUSTERS;
|
| 65 |
+
|
| 66 |
+
/* 堆分配避免栈溢出 (5×6×4096×4 = 480KB) */
|
| 67 |
+
float *embs = (float *)malloc(5 * 6 * n_embd * sizeof(float));
|
| 68 |
+
for (int c = 0; c < n_c; c++) {
|
| 69 |
+
for (int i = 0; i < probe_clusters[c].n; i++) {
|
| 70 |
+
get_concept_embedding(m, probe_clusters[c].concepts[i],
|
| 71 |
+
embs + ((size_t)c * 6 + i) * n_embd, n_embd);
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
float intra_sim = 0, inter_sim = 0;
|
| 76 |
+
int n_intra = 0, n_inter = 0;
|
| 77 |
+
|
| 78 |
+
for (int c = 0; c < n_c; c++) {
|
| 79 |
+
for (int i = 0; i < probe_clusters[c].n; i++) {
|
| 80 |
+
for (int j = i + 1; j < probe_clusters[c].n; j++) {
|
| 81 |
+
intra_sim += cosine_sim(embs + ((size_t)c * 6 + i) * n_embd,
|
| 82 |
+
embs + ((size_t)c * 6 + j) * n_embd, n_embd);
|
| 83 |
+
n_intra++;
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
for (int c1 = 0; c1 < n_c; c1++) {
|
| 89 |
+
for (int c2 = c1 + 1; c2 < n_c; c2++) {
|
| 90 |
+
for (int i = 0; i < probe_clusters[c1].n; i++) {
|
| 91 |
+
for (int j = 0; j < probe_clusters[c2].n; j++) {
|
| 92 |
+
inter_sim += cosine_sim(embs + ((size_t)c1 * 6 + i) * n_embd,
|
| 93 |
+
embs + ((size_t)c2 * 6 + j) * n_embd, n_embd);
|
| 94 |
+
n_inter++;
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
intra_sim = n_intra > 0 ? intra_sim / n_intra : 0;
|
| 101 |
+
inter_sim = n_inter > 0 ? inter_sim / n_inter : 0;
|
| 102 |
+
float cluster_score = intra_sim - inter_sim; /* 越正越好 */
|
| 103 |
+
|
| 104 |
+
printf(" [L2] cluster: intra=%.3f inter=%.3f score=%+.3f (intra应>inter)\n",
|
| 105 |
+
intra_sim, inter_sim, cluster_score);
|
| 106 |
+
free(embs);
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
/* ========================================================================
|
| 110 |
+
* L3: 概念组合分析 — 复杂概念由哪些基础概念组成
|
| 111 |
+
* ======================================================================== */
|
| 112 |
+
static void probe_composition(Model *m) {
|
| 113 |
+
int n_embd = m->cfg.n_embd;
|
| 114 |
+
float *complex_emb = (float *)malloc(n_embd * sizeof(float));
|
| 115 |
+
float *basic_emb = (float *)malloc(n_embd * sizeof(float));
|
| 116 |
+
|
| 117 |
+
for (int p = 0; p < (int)N_COMPOSITIONS; p++) {
|
| 118 |
+
CompositionProbe *cp = &composition_probes[p];
|
| 119 |
+
get_concept_embedding(m, cp->complex_concept, complex_emb, n_embd);
|
| 120 |
+
|
| 121 |
+
/* 算复杂概念与每个基础概念的相似度, 排序 */
|
| 122 |
+
float sims[6];
|
| 123 |
+
for (int i = 0; i < cp->n_basics; i++) {
|
| 124 |
+
get_concept_embedding(m, cp->basic_concepts[i], basic_emb, n_embd);
|
| 125 |
+
sims[i] = cosine_sim(complex_emb, basic_emb, n_embd);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
/* 找 top-3 最相关基础概念 */
|
| 129 |
+
int top3[3] = {-1, -1, -1};
|
| 130 |
+
float top3_sim[3] = {-2, -2, -2};
|
| 131 |
+
for (int i = 0; i < cp->n_basics; i++) {
|
| 132 |
+
for (int k = 0; k < 3; k++) {
|
| 133 |
+
if (sims[i] > top3_sim[k]) {
|
| 134 |
+
for (int t = 2; t > k; t--) {
|
| 135 |
+
top3[t] = top3[t-1]; top3_sim[t] = top3_sim[t-1];
|
| 136 |
+
}
|
| 137 |
+
top3[k] = i; top3_sim[k] = sims[i]; break;
|
| 138 |
+
}
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
printf(" [L3] %s(%s) ≈ %s(%.2f) + %s(%.2f) + %s(%.2f)\n",
|
| 143 |
+
cp->name, cp->complex_concept,
|
| 144 |
+
cp->basic_concepts[top3[0]], top3_sim[0],
|
| 145 |
+
cp->basic_concepts[top3[1]], top3_sim[1],
|
| 146 |
+
cp->basic_concepts[top3[2]], top3_sim[2]);
|
| 147 |
+
}
|
| 148 |
+
free(complex_emb);
|
| 149 |
+
free(basic_emb);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
/* ========================================================================
|
| 153 |
+
* L4: 层间概念传递 — 概念在每层的激活模式
|
| 154 |
+
* ========================================================================
|
| 155 |
+
*/
|
| 156 |
+
static void probe_layer_propagation(Model *m) {
|
| 157 |
+
int n_embd = m->cfg.n_embd;
|
| 158 |
+
int mlp_dim = m->cfg.mlp_dim;
|
| 159 |
+
int n_layer = m->cfg.n_layer;
|
| 160 |
+
|
| 161 |
+
/* 取"热"和"冷"的概念, 看每层 CORE 激活差异 */
|
| 162 |
+
float emb_a[4096], emb_b[4096];
|
| 163 |
+
get_concept_embedding(m, "\xe7\x83\xad", emb_a, n_embd); /* 热 */
|
| 164 |
+
get_concept_embedding(m, "\xe5\x86\xb7", emb_b, n_embd); /* 冷 */
|
| 165 |
+
|
| 166 |
+
float *act_a = (float *)malloc(mlp_dim * sizeof(float));
|
| 167 |
+
float *act_b = (float *)malloc(mlp_dim * sizeof(float));
|
| 168 |
+
float gate_a[4096], gate_b[4096];
|
| 169 |
+
|
| 170 |
+
printf(" [L4] 热/冷 层间 CORE 差异: ");
|
| 171 |
+
for (int l = 0; l < n_layer; l++) {
|
| 172 |
+
compute_gate_input(m, emb_a, l, gate_a, n_embd);
|
| 173 |
+
compute_gate_input(m, emb_b, l, gate_b, n_embd);
|
| 174 |
+
simulate_activation(m, gate_a, l, act_a, mlp_dim);
|
| 175 |
+
simulate_activation(m, gate_b, l, act_b, mlp_dim);
|
| 176 |
+
|
| 177 |
+
uint8_t *mask = m->layers[l].mlp_gate.logic_mask;
|
| 178 |
+
if (!mask) continue;
|
| 179 |
+
float cd = 0; int nc = 0;
|
| 180 |
+
for (int j = 0; j < mlp_dim; j++) {
|
| 181 |
+
if (mask[j] == 0) { cd += fabsf(act_a[j] - act_b[j]); nc++; }
|
| 182 |
+
}
|
| 183 |
+
cd = nc > 0 ? cd / nc : 0;
|
| 184 |
+
printf("L%d=%.3f ", l, cd);
|
| 185 |
+
}
|
| 186 |
+
printf("\n");
|
| 187 |
+
free(act_a); free(act_b);
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
/* ========================================================================
|
| 191 |
+
* L5: 概念最近邻 — 模型认为哪些概念最相似
|
| 192 |
+
* ========================================================================
|
| 193 |
+
*/
|
| 194 |
+
static void probe_nearest_neighbors(Model *m) {
|
| 195 |
+
int n_embd = m->cfg.n_embd;
|
| 196 |
+
int V = m->cfg.vocab_size;
|
| 197 |
+
|
| 198 |
+
/* 探针概念: 看"火"的 top-5 最近邻是什么 */
|
| 199 |
+
const char *probes[] = {"火", "水", "大", "热"};
|
| 200 |
+
int n_probes = 4;
|
| 201 |
+
|
| 202 |
+
for (int p = 0; p < n_probes; p++) {
|
| 203 |
+
float emb[4096];
|
| 204 |
+
get_concept_embedding(m, probes[p], emb, n_embd);
|
| 205 |
+
|
| 206 |
+
/* 找 top-5 最近邻 (只搜前 1000 个 token, 避免太慢) */
|
| 207 |
+
int best[5]; float bsim[5];
|
| 208 |
+
for (int k = 0; k < 5; k++) { best[k] = -1; bsim[k] = -2; }
|
| 209 |
+
int search_range = V < 1000 ? V : 1000;
|
| 210 |
+
for (int j = 0; j < search_range; j++) {
|
| 211 |
+
const float *w = m->wte + (size_t)j * n_embd;
|
| 212 |
+
float s = cosine_sim(emb, w, n_embd);
|
| 213 |
+
for (int k = 0; k < 5; k++) {
|
| 214 |
+
if (s > bsim[k]) {
|
| 215 |
+
for (int t = 4; t > k; t--) { best[t] = best[t-1]; bsim[t] = bsim[t-1]; }
|
| 216 |
+
best[k] = j; bsim[k] = s; break;
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
printf(" [L5] \"%s\" 最近邻: ", probes[p]);
|
| 222 |
+
for (int k = 0; k < 5; k++) {
|
| 223 |
+
printf("id%d(%.2f) ", best[k], bsim[k]);
|
| 224 |
+
}
|
| 225 |
+
printf("\n");
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
/* ========================================================================
|
| 230 |
+
* L6: ��法探针 — 检测模型是否学会语法结构
|
| 231 |
+
* ========================================================================
|
| 232 |
+
* 6 个子探针:
|
| 233 |
+
* L6a 词序敏感性: "大火" vs "火大" — 词序不同意思不同, 模型应该区分
|
| 234 |
+
* L6b 搭配准确性: "喝+水" 高频搭配 vs "喝+火" 不搭配 — 模型应该偏好高频搭配
|
| 235 |
+
* L6c 语法角色: "红的苹果"(修饰) vs "苹果的红"(中心) — 模型应该区分修饰/中心
|
| 236 |
+
* L6d 句子连贯性: 正确语序 vs 打乱语序 — 模型对正确语序应该 loss 更低
|
| 237 |
+
* L6e 否定理解: "不热" vs "热" — 模型应该区分否定
|
| 238 |
+
* L6f 量词搭配: "一只猫" 正确 vs "一条猫" 错误 — 模型应该偏好正确量词
|
| 239 |
+
* ======================================================================== */
|
| 240 |
+
|
| 241 |
+
/* L6a: 词序敏感性 — 比较大小关系: "大火"(形容火势) vs "火大"(形容脾气) */
|
| 242 |
+
static void probe_word_order(Model *m) {
|
| 243 |
+
int n_embd = m->cfg.n_embd;
|
| 244 |
+
/* 大火 = 大 + 火 (wte 求和), 火大 = 火 + 大 (同样求和但顺序不同)
|
| 245 |
+
* wte 求和是交换的, 但模型 forward 不是 (attention 有位置编码)
|
| 246 |
+
* 所以看 wte 层面应该相同, forward 层面应该不同 */
|
| 247 |
+
float e_da[4096], e_hd[4096], e_d[4096], e_h[4096];
|
| 248 |
+
get_concept_embedding(m, "\xe5\xa4\xa7\xe7\x83\xad", e_da, n_embd); /* 大火 */
|
| 249 |
+
get_concept_embedding(m, "\xe7\x83\xad\xe5\xa4\xa7", e_hd, n_embd); /* 火大 */
|
| 250 |
+
get_concept_embedding(m, "\xe5\xa4\xa7", e_d, n_embd); /* 大 */
|
| 251 |
+
get_concept_embedding(m, "\xe7\x83\xad", e_h, n_embd); /* 火 */
|
| 252 |
+
|
| 253 |
+
float sim_order = cosine_sim(e_da, e_hd, n_embd); /* wte 层: 应该 ~1.0 (求和可交换) */
|
| 254 |
+
float sim_da_d = cosine_sim(e_da, e_d, n_embd); /* 大火 vs 大 */
|
| 255 |
+
float sim_da_h = cosine_sim(e_da, e_h, n_embd); /* 大火 vs 火 */
|
| 256 |
+
/* 如果大火更接近火 → 模型学到"大火"中心词是"火" */
|
| 257 |
+
/* 如果大火更接近大 → 模型还没学会中心词 */
|
| 258 |
+
|
| 259 |
+
printf(" [L6a] 词序: 大火≈火(%.2f) vs 大火≈大(%.2f) wte交换(%.2f) 中心词=%s\n",
|
| 260 |
+
sim_da_h, sim_da_d, sim_order,
|
| 261 |
+
sim_da_h > sim_da_d ? "火(正确)" : "大(错误)");
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
/* L6b: 搭配准确性 — 比较"喝水的概率" vs "喝火的概率"
|
| 265 |
+
* 用 wte 组合后的余弦相似度判断搭配合理性 */
|
| 266 |
+
static void probe_collocation(Model *m) {
|
| 267 |
+
int n_embd = m->cfg.n_embd;
|
| 268 |
+
|
| 269 |
+
/* 搭配对: (动词, 正确宾语, 错误宾语) */
|
| 270 |
+
struct { const char *verb; const char *correct; const char *wrong; const char *name; } pairs[] = {
|
| 271 |
+
{"\xe5\x96\x9d", "\xe6\xb0\xb4", "\xe7\x81\xab", "喝"},
|
| 272 |
+
{"\xe5\x90\x83", "\xe9\xa5\xad", "\xe7\x9f\xb3", "吃"},
|
| 273 |
+
{"\xe7\x9c\x8b", "\xe4\xb9\xa6", "\xe9\xa3\x8e", "看"},
|
| 274 |
+
};
|
| 275 |
+
int n = sizeof(pairs) / sizeof(pairs[0]);
|
| 276 |
+
|
| 277 |
+
float *e_verb = (float *)malloc(n_embd * sizeof(float));
|
| 278 |
+
float *e_correct = (float *)malloc(n_embd * sizeof(float));
|
| 279 |
+
float *e_wrong = (float *)malloc(n_embd * sizeof(float));
|
| 280 |
+
|
| 281 |
+
printf(" [L6b] 搭配: ");
|
| 282 |
+
int n_correct = 0;
|
| 283 |
+
for (int i = 0; i < n; i++) {
|
| 284 |
+
get_concept_embedding(m, pairs[i].verb, e_verb, n_embd);
|
| 285 |
+
get_concept_embedding(m, pairs[i].correct, e_correct, n_embd);
|
| 286 |
+
get_concept_embedding(m, pairs[i].wrong, e_wrong, n_embd);
|
| 287 |
+
|
| 288 |
+
float sim_correct = cosine_sim(e_verb, e_correct, n_embd);
|
| 289 |
+
float sim_wrong = cosine_sim(e_verb, e_wrong, n_embd);
|
| 290 |
+
/* 正确搭配: 动词和宾语应该有适度相似 (语义相关但不完全相同) */
|
| 291 |
+
/* 错误搭配: 动词和宾语应该不太相似 */
|
| 292 |
+
int ok = sim_correct > sim_wrong;
|
| 293 |
+
if (ok) n_correct++;
|
| 294 |
+
printf("%s(%s:%.2f>%s:%.2f %s) ",
|
| 295 |
+
pairs[i].name, pairs[i].correct, sim_correct, pairs[i].wrong, sim_wrong,
|
| 296 |
+
ok ? "✓" : "✗");
|
| 297 |
+
}
|
| 298 |
+
printf(" %d/%d正确\n", n_correct, n);
|
| 299 |
+
free(e_verb); free(e_correct); free(e_wrong);
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
/* L6c: 否定理解 — "不热" 应该离 "冷" 更近, 离 "热" 更远 */
|
| 303 |
+
static void probe_negation(Model *m) {
|
| 304 |
+
int n_embd = m->cfg.n_embd;
|
| 305 |
+
float *e_not_hot = (float *)malloc(n_embd * sizeof(float));
|
| 306 |
+
float *e_hot = (float *)malloc(n_embd * sizeof(float));
|
| 307 |
+
float *e_cold = (float *)malloc(n_embd * sizeof(float));
|
| 308 |
+
float *e_not = (float *)malloc(n_embd * sizeof(float));
|
| 309 |
+
|
| 310 |
+
/* 不热 = 不 + 热 */
|
| 311 |
+
get_concept_embedding(m, "\xe4\xb8\x8d\xe7\x83\xad", e_not_hot, n_embd); /* 不热 */
|
| 312 |
+
get_concept_embedding(m, "\xe7\x83\xad", e_hot, n_embd); /* 热 */
|
| 313 |
+
get_concept_embedding(m, "\xe5\x86\xb7", e_cold, n_embd); /* 冷 */
|
| 314 |
+
get_concept_embedding(m, "\xe4\xb8\x8d", e_not, n_embd); /* 不 */
|
| 315 |
+
|
| 316 |
+
float sim_to_hot = cosine_sim(e_not_hot, e_hot, n_embd);
|
| 317 |
+
float sim_to_cold = cosine_sim(e_not_hot, e_cold, n_embd);
|
| 318 |
+
float sim_to_not = cosine_sim(e_not_hot, e_not, n_embd);
|
| 319 |
+
|
| 320 |
+
/* "不热"如果学会了否定, 应该更接近"冷"而非"热" */
|
| 321 |
+
printf(" [L6c] 否定: 不热≈热(%.2f) 不热≈冷(%.2f) 不热≈不(%.2f) → %s\n",
|
| 322 |
+
sim_to_hot, sim_to_cold, sim_to_not,
|
| 323 |
+
sim_to_cold > sim_to_hot ? "理解否定(✓)" : "未理解(✗)");
|
| 324 |
+
free(e_not_hot); free(e_hot); free(e_cold); free(e_not);
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
/* L6d: 修饰关系 — "红的花" vs "花" vs "红"
|
| 328 |
+
* 修饰语+中心词组合, 应该更接近中心词 */
|
| 329 |
+
static void probe_modifier(Model *m) {
|
| 330 |
+
int n_embd = m->cfg.n_embd;
|
| 331 |
+
struct { const char *phrase; const char *modifier; const char *head; const char *name; } items[] = {
|
| 332 |
+
{"\xe7\xba\xa2\xe7\x9a\x84\xe8\x8a\xb1", "\xe7\xba\xa2", "\xe8\x8a\xb1", "红的花"}, /* 红的花, 红, 花 */
|
| 333 |
+
{"\xe5\xa4\xa7\xe7\x9a\x84\xe7\x8b\x97", "\xe5\xa4\xa7", "\xe7\x8b\x97", "大的狗"}, /* 大的狗, 大, 狗 */
|
| 334 |
+
{"\xe7\x83\xad\xe7\x9a\x84\xe6\xb0\xb4", "\xe7\x83\xad", "\xe6\xb0\xb4", "热的水"}, /* 热的水, 热, 水 */
|
| 335 |
+
};
|
| 336 |
+
int n = sizeof(items) / sizeof(items[0]);
|
| 337 |
+
|
| 338 |
+
printf(" [L6d] 修饰: ");
|
| 339 |
+
int n_correct = 0;
|
| 340 |
+
for (int i = 0; i < n; i++) {
|
| 341 |
+
float e_phrase[4096], e_mod[4096], e_head[4096];
|
| 342 |
+
get_concept_embedding(m, items[i].phrase, e_phrase, n_embd);
|
| 343 |
+
get_concept_embedding(m, items[i].modifier, e_mod, n_embd);
|
| 344 |
+
get_concept_embedding(m, items[i].head, e_head, n_embd);
|
| 345 |
+
|
| 346 |
+
float sim_head = cosine_sim(e_phrase, e_head, n_embd);
|
| 347 |
+
float sim_mod = cosine_sim(e_phrase, e_mod, n_embd);
|
| 348 |
+
/* 短语应该更接近中心词 */
|
| 349 |
+
int ok = sim_head > sim_mod;
|
| 350 |
+
if (ok) n_correct++;
|
| 351 |
+
printf("%s(中心%.2f>修饰%.2f %s) ", items[i].name, sim_head, sim_mod, ok ? "✓" : "✗");
|
| 352 |
+
}
|
| 353 |
+
printf(" %d/%d正确\n", n_correct, n);
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
/* L6e: 语法类别 — 名词 vs 动词 vs 形容词
|
| 357 |
+
* 同类词应该聚在一起, 异类应该分开 */
|
| 358 |
+
static void probe_pos_categories(Model *m) {
|
| 359 |
+
int n_embd = m->cfg.n_embd;
|
| 360 |
+
|
| 361 |
+
const char *nouns[] = {"\xe5\xa4\xaa\xe9\x98\xb3", "\xe6\xb0\xb4", "\xe6\xa0\x91", "\xe7\x8c\xab", "\xe4\xb9\xa6", "\xe7\x81\xab\xe5\xb1\xb1"};
|
| 362 |
+
const char *verbs[] = {"\xe8\xb7\x91", "\xe5\x90\x83", "\xe7\x9c\x8b", "\xe8\xaf\xb4", "\xe9\xa3\x9e", "\xe7\x9d\xa1"};
|
| 363 |
+
const char *adjs[] = {"\xe5\xa4\xa7", "\xe7\x83\xad", "\xe7\xba\xa2", "\xe5\xbf\xab", "\xe9\xab\x98", "\xe7\xbe\x8e"};
|
| 364 |
+
|
| 365 |
+
/* 堆分配避免栈溢出 (6×4096×4×3 = 288KB) */
|
| 366 |
+
float *en = (float *)malloc(6 * n_embd * sizeof(float));
|
| 367 |
+
float *ev = (float *)malloc(6 * n_embd * sizeof(float));
|
| 368 |
+
float *ea = (float *)malloc(6 * n_embd * sizeof(float));
|
| 369 |
+
for (int i = 0; i < 6; i++) {
|
| 370 |
+
get_concept_embedding(m, nouns[i], en + (size_t)i * n_embd, n_embd);
|
| 371 |
+
get_concept_embedding(m, verbs[i], ev + (size_t)i * n_embd, n_embd);
|
| 372 |
+
get_concept_embedding(m, adjs[i], ea + (size_t)i * n_embd, n_embd);
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
/* 算类内平均相似度 */
|
| 376 |
+
float noun_intra = 0, verb_intra = 0, adj_intra = 0;
|
| 377 |
+
for (int i = 0; i < 6; i++)
|
| 378 |
+
for (int j = i + 1; j < 6; j++) {
|
| 379 |
+
noun_intra += cosine_sim(en + (size_t)i * n_embd, en + (size_t)j * n_embd, n_embd);
|
| 380 |
+
verb_intra += cosine_sim(ev + (size_t)i * n_embd, ev + (size_t)j * n_embd, n_embd);
|
| 381 |
+
adj_intra += cosine_sim(ea + (size_t)i * n_embd, ea + (size_t)j * n_embd, n_embd);
|
| 382 |
+
}
|
| 383 |
+
noun_intra /= 15; verb_intra /= 15; adj_intra /= 15;
|
| 384 |
+
|
| 385 |
+
/* 算类间平均相似度 */
|
| 386 |
+
float inter = 0; int n_inter = 0;
|
| 387 |
+
for (int i = 0; i < 6; i++) {
|
| 388 |
+
for (int j = 0; j < 6; j++) {
|
| 389 |
+
inter += cosine_sim(en + (size_t)i * n_embd, ev + (size_t)j * n_embd, n_embd); n_inter++;
|
| 390 |
+
inter += cosine_sim(en + (size_t)i * n_embd, ea + (size_t)j * n_embd, n_embd); n_inter++;
|
| 391 |
+
inter += cosine_sim(ev + (size_t)i * n_embd, ea + (size_t)j * n_embd, n_embd); n_inter++;
|
| 392 |
+
}
|
| 393 |
+
}
|
| 394 |
+
inter /= n_inter;
|
| 395 |
+
|
| 396 |
+
float avg_intra = (noun_intra + verb_intra + adj_intra) / 3;
|
| 397 |
+
float pos_score = avg_intra - inter;
|
| 398 |
+
|
| 399 |
+
printf(" [L6e] 词类: 名词(%.2f) 动词(%.2f) 形容(%.2f) 类间(%.2f) 聚合=%+.3f\n",
|
| 400 |
+
noun_intra, verb_intra, adj_intra, inter, pos_score);
|
| 401 |
+
free(en); free(ev); free(ea);
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
/* L6 语法探针总入口 */
|
| 405 |
+
static void probe_syntax(Model *m) {
|
| 406 |
+
probe_word_order(m); /* L6a: 词序敏感性 */
|
| 407 |
+
probe_collocation(m); /* L6b: 搭配准确性 */
|
| 408 |
+
probe_negation(m); /* L6c: 否定理解 */
|
| 409 |
+
probe_modifier(m); /* L6d: 修饰关系 */
|
| 410 |
+
probe_pos_categories(m); /* L6e: 语法类别 */
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
/* ========================================================================
|
| 414 |
+
* 高级探针总入口
|
| 415 |
+
* ======================================================================== */
|
| 416 |
+
static void advanced_probe(Model *m) {
|
| 417 |
+
printf(" --- Advanced Probe ---\n");
|
| 418 |
+
probe_concept_clusters(m); /* L2: 概念聚类 */
|
| 419 |
+
/* L3/L4 暂时禁用 (多字节UTF-8 + compute_gate_input 在 Windows 栈限制下不稳定) */
|
| 420 |
+
probe_syntax(m); /* L6: 语法探针 */
|
| 421 |
+
}
|