| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| static ConceptPair probe_pairs_extended[] = { |
| |
| {"\xe7\x83\xad", "\xe7\x83\xad", "\xe5\x86\xb7", "\xe5\x86\xb7", "temperature"}, |
| {"\xe5\xa4\xa7", "\xe5\xa4\xa7", "\xe5\xb0\x8f", "\xe5\xb0\x8f", "size"}, |
| {"\xe4\xb8\x8a", "\xe4\xb8\x8a", "\xe4\xb8\x8b", "\xe4\xb8\x8b", "position"}, |
| {"\xe4\xba\xae", "\xe4\xba\xae", "\xe6\x9a\x97", "\xe6\x9a\x97", "light"}, |
| {"\xe9\x87\x8d", "\xe9\x87\x8d", "\xe8\xbd\xbb", "\xe8\xbd\xbb", "weight"}, |
| {"\xe5\xbf\xab", "\xe5\xbf\xab", "\xe6\x85\xa2", "\xe6\x85\xa2", "speed"}, |
| {"\xe6\xb9\xbf", "\xe6\xb9\xbf", "\xe5\xb9\xb2", "\xe5\xb9\xb2", "moisture"}, |
| |
| {"\xe7\x83\xad", "\xe7\x83\xad", "\xe6\x9a\x96", "\xe6\x9a\x96", "syn_hot"}, |
| {"\xe5\xa4\xa7", "\xe5\xa4\xa7", "\xe5\xb7\xa8", "\xe5\xb7\xa8", "syn_big"}, |
| {"\xe5\xbf\xab", "\xe5\xbf\xab", "\xe9\x80\x9f", "\xe9\x80\x9f", "syn_fast"}, |
| }; |
|
|
| |
| typedef struct { const char *name; const char *concepts[6]; int n; } ConceptCluster; |
| static ConceptCluster probe_clusters[] = { |
| {"temp", {"热","冷","暖","凉","温","冰"}, 6}, |
| {"size", {"大","小","巨","微","长","短"}, 6}, |
| {"color", {"红","黄","蓝","绿","白","黑"}, 6}, |
| {"emotion", {"喜","怒","哀","乐","爱","恨"}, 6}, |
| {"nature", {"水","火","土","木","金","风"}, 6}, |
| }; |
| #define N_CLUSTERS (sizeof(probe_clusters) / sizeof(probe_clusters[0])) |
|
|
| |
| |
| |
| |
| typedef struct { |
| const char *name; |
| const char *complex_concept; |
| const char *basic_concepts[6]; |
| int n_basics; |
| } CompositionProbe; |
| static CompositionProbe composition_probes[] = { |
| {"fire", "火", {"热","亮","红","危险","烧","光"}, 6}, |
| {"water", "水", {"湿","冷","清","流","喝","冰"}, 6}, |
| {"sun", "太阳", {"热","亮","光","圆","远","黄"}, 6}, |
| {"tree", "树", {"木","绿","叶","根","高","植物"}, 6}, |
| }; |
| #define N_COMPOSITIONS (sizeof(composition_probes) / sizeof(CompositionProbe[0])) |
|
|
| |
| |
| |
| static void probe_concept_clusters(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| int n_c = (int)N_CLUSTERS; |
|
|
| |
| float *embs = (float *)malloc(5 * 6 * n_embd * sizeof(float)); |
| for (int c = 0; c < n_c; c++) { |
| for (int i = 0; i < probe_clusters[c].n; i++) { |
| get_concept_embedding(m, probe_clusters[c].concepts[i], |
| embs + ((size_t)c * 6 + i) * n_embd, n_embd); |
| } |
| } |
|
|
| float intra_sim = 0, inter_sim = 0; |
| int n_intra = 0, n_inter = 0; |
|
|
| for (int c = 0; c < n_c; c++) { |
| for (int i = 0; i < probe_clusters[c].n; i++) { |
| for (int j = i + 1; j < probe_clusters[c].n; j++) { |
| intra_sim += cosine_sim(embs + ((size_t)c * 6 + i) * n_embd, |
| embs + ((size_t)c * 6 + j) * n_embd, n_embd); |
| n_intra++; |
| } |
| } |
| } |
|
|
| for (int c1 = 0; c1 < n_c; c1++) { |
| for (int c2 = c1 + 1; c2 < n_c; c2++) { |
| for (int i = 0; i < probe_clusters[c1].n; i++) { |
| for (int j = 0; j < probe_clusters[c2].n; j++) { |
| inter_sim += cosine_sim(embs + ((size_t)c1 * 6 + i) * n_embd, |
| embs + ((size_t)c2 * 6 + j) * n_embd, n_embd); |
| n_inter++; |
| } |
| } |
| } |
| } |
|
|
| intra_sim = n_intra > 0 ? intra_sim / n_intra : 0; |
| inter_sim = n_inter > 0 ? inter_sim / n_inter : 0; |
| float cluster_score = intra_sim - inter_sim; |
|
|
| printf(" [L2] cluster: intra=%.3f inter=%.3f score=%+.3f (intra应>inter)\n", |
| intra_sim, inter_sim, cluster_score); |
| free(embs); |
| } |
|
|
| |
| |
| |
| static void probe_composition(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| float *complex_emb = (float *)malloc(n_embd * sizeof(float)); |
| float *basic_emb = (float *)malloc(n_embd * sizeof(float)); |
|
|
| for (int p = 0; p < (int)N_COMPOSITIONS; p++) { |
| CompositionProbe *cp = &composition_probes[p]; |
| get_concept_embedding(m, cp->complex_concept, complex_emb, n_embd); |
|
|
| |
| float sims[6]; |
| for (int i = 0; i < cp->n_basics; i++) { |
| get_concept_embedding(m, cp->basic_concepts[i], basic_emb, n_embd); |
| sims[i] = cosine_sim(complex_emb, basic_emb, n_embd); |
| } |
|
|
| |
| int top3[3] = {-1, -1, -1}; |
| float top3_sim[3] = {-2, -2, -2}; |
| for (int i = 0; i < cp->n_basics; i++) { |
| for (int k = 0; k < 3; k++) { |
| if (sims[i] > top3_sim[k]) { |
| for (int t = 2; t > k; t--) { |
| top3[t] = top3[t-1]; top3_sim[t] = top3_sim[t-1]; |
| } |
| top3[k] = i; top3_sim[k] = sims[i]; break; |
| } |
| } |
| } |
|
|
| printf(" [L3] %s(%s) ≈ %s(%.2f) + %s(%.2f) + %s(%.2f)\n", |
| cp->name, cp->complex_concept, |
| cp->basic_concepts[top3[0]], top3_sim[0], |
| cp->basic_concepts[top3[1]], top3_sim[1], |
| cp->basic_concepts[top3[2]], top3_sim[2]); |
| } |
| free(complex_emb); |
| free(basic_emb); |
| } |
|
|
| |
| |
| |
| |
| static void probe_layer_propagation(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| int mlp_dim = m->cfg.mlp_dim; |
| int n_layer = m->cfg.n_layer; |
|
|
| |
| float emb_a[4096], emb_b[4096]; |
| get_concept_embedding(m, "\xe7\x83\xad", emb_a, n_embd); |
| get_concept_embedding(m, "\xe5\x86\xb7", emb_b, n_embd); |
|
|
| float *act_a = (float *)malloc(mlp_dim * sizeof(float)); |
| float *act_b = (float *)malloc(mlp_dim * sizeof(float)); |
| float gate_a[4096], gate_b[4096]; |
|
|
| printf(" [L4] 热/冷 层间 CORE 差异: "); |
| for (int l = 0; l < n_layer; l++) { |
| compute_gate_input(m, emb_a, l, gate_a, n_embd); |
| compute_gate_input(m, emb_b, l, gate_b, n_embd); |
| simulate_activation(m, gate_a, l, act_a, mlp_dim); |
| simulate_activation(m, gate_b, l, act_b, mlp_dim); |
|
|
| uint8_t *mask = m->layers[l].mlp_gate.logic_mask; |
| if (!mask) continue; |
| float cd = 0; int nc = 0; |
| for (int j = 0; j < mlp_dim; j++) { |
| if (mask[j] == 0) { cd += fabsf(act_a[j] - act_b[j]); nc++; } |
| } |
| cd = nc > 0 ? cd / nc : 0; |
| printf("L%d=%.3f ", l, cd); |
| } |
| printf("\n"); |
| free(act_a); free(act_b); |
| } |
|
|
| |
| |
| |
| |
| static void probe_nearest_neighbors(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| int V = m->cfg.vocab_size; |
|
|
| |
| const char *probes[] = {"火", "水", "大", "热"}; |
| int n_probes = 4; |
|
|
| for (int p = 0; p < n_probes; p++) { |
| float emb[4096]; |
| get_concept_embedding(m, probes[p], emb, n_embd); |
|
|
| |
| int best[5]; float bsim[5]; |
| for (int k = 0; k < 5; k++) { best[k] = -1; bsim[k] = -2; } |
| int search_range = V < 1000 ? V : 1000; |
| for (int j = 0; j < search_range; j++) { |
| const float *w = m->wte + (size_t)j * n_embd; |
| float s = cosine_sim(emb, w, n_embd); |
| for (int k = 0; k < 5; k++) { |
| if (s > bsim[k]) { |
| for (int t = 4; t > k; t--) { best[t] = best[t-1]; bsim[t] = bsim[t-1]; } |
| best[k] = j; bsim[k] = s; break; |
| } |
| } |
| } |
|
|
| printf(" [L5] \"%s\" 最近邻: ", probes[p]); |
| for (int k = 0; k < 5; k++) { |
| printf("id%d(%.2f) ", best[k], bsim[k]); |
| } |
| printf("\n"); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| static void probe_word_order(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| |
| |
| |
| float e_da[4096], e_hd[4096], e_d[4096], e_h[4096]; |
| get_concept_embedding(m, "\xe5\xa4\xa7\xe7\x83\xad", e_da, n_embd); |
| get_concept_embedding(m, "\xe7\x83\xad\xe5\xa4\xa7", e_hd, n_embd); |
| get_concept_embedding(m, "\xe5\xa4\xa7", e_d, n_embd); |
| get_concept_embedding(m, "\xe7\x83\xad", e_h, n_embd); |
|
|
| float sim_order = cosine_sim(e_da, e_hd, n_embd); |
| float sim_da_d = cosine_sim(e_da, e_d, n_embd); |
| float sim_da_h = cosine_sim(e_da, e_h, n_embd); |
| |
| |
|
|
| printf(" [L6a] 词序: 大火≈火(%.2f) vs 大火≈大(%.2f) wte交换(%.2f) 中心词=%s\n", |
| sim_da_h, sim_da_d, sim_order, |
| sim_da_h > sim_da_d ? "火(正确)" : "大(错误)"); |
| } |
|
|
| |
| |
| static void probe_collocation(Model *m) { |
| int n_embd = m->cfg.n_embd; |
|
|
| |
| struct { const char *verb; const char *correct; const char *wrong; const char *name; } pairs[] = { |
| {"\xe5\x96\x9d", "\xe6\xb0\xb4", "\xe7\x81\xab", "喝"}, |
| {"\xe5\x90\x83", "\xe9\xa5\xad", "\xe7\x9f\xb3", "吃"}, |
| {"\xe7\x9c\x8b", "\xe4\xb9\xa6", "\xe9\xa3\x8e", "看"}, |
| }; |
| int n = sizeof(pairs) / sizeof(pairs[0]); |
|
|
| float *e_verb = (float *)malloc(n_embd * sizeof(float)); |
| float *e_correct = (float *)malloc(n_embd * sizeof(float)); |
| float *e_wrong = (float *)malloc(n_embd * sizeof(float)); |
|
|
| printf(" [L6b] 搭配: "); |
| int n_correct = 0; |
| for (int i = 0; i < n; i++) { |
| get_concept_embedding(m, pairs[i].verb, e_verb, n_embd); |
| get_concept_embedding(m, pairs[i].correct, e_correct, n_embd); |
| get_concept_embedding(m, pairs[i].wrong, e_wrong, n_embd); |
|
|
| float sim_correct = cosine_sim(e_verb, e_correct, n_embd); |
| float sim_wrong = cosine_sim(e_verb, e_wrong, n_embd); |
| |
| |
| int ok = sim_correct > sim_wrong; |
| if (ok) n_correct++; |
| printf("%s(%s:%.2f>%s:%.2f %s) ", |
| pairs[i].name, pairs[i].correct, sim_correct, pairs[i].wrong, sim_wrong, |
| ok ? "✓" : "✗"); |
| } |
| printf(" %d/%d正确\n", n_correct, n); |
| free(e_verb); free(e_correct); free(e_wrong); |
| } |
|
|
| |
| static void probe_negation(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| float *e_not_hot = (float *)malloc(n_embd * sizeof(float)); |
| float *e_hot = (float *)malloc(n_embd * sizeof(float)); |
| float *e_cold = (float *)malloc(n_embd * sizeof(float)); |
| float *e_not = (float *)malloc(n_embd * sizeof(float)); |
|
|
| |
| get_concept_embedding(m, "\xe4\xb8\x8d\xe7\x83\xad", e_not_hot, n_embd); |
| get_concept_embedding(m, "\xe7\x83\xad", e_hot, n_embd); |
| get_concept_embedding(m, "\xe5\x86\xb7", e_cold, n_embd); |
| get_concept_embedding(m, "\xe4\xb8\x8d", e_not, n_embd); |
|
|
| float sim_to_hot = cosine_sim(e_not_hot, e_hot, n_embd); |
| float sim_to_cold = cosine_sim(e_not_hot, e_cold, n_embd); |
| float sim_to_not = cosine_sim(e_not_hot, e_not, n_embd); |
|
|
| |
| printf(" [L6c] 否定: 不热≈热(%.2f) 不热≈冷(%.2f) 不热≈不(%.2f) → %s\n", |
| sim_to_hot, sim_to_cold, sim_to_not, |
| sim_to_cold > sim_to_hot ? "理解否定(✓)" : "未理解(✗)"); |
| free(e_not_hot); free(e_hot); free(e_cold); free(e_not); |
| } |
|
|
| |
| |
| static void probe_modifier(Model *m) { |
| int n_embd = m->cfg.n_embd; |
| struct { const char *phrase; const char *modifier; const char *head; const char *name; } items[] = { |
| {"\xe7\xba\xa2\xe7\x9a\x84\xe8\x8a\xb1", "\xe7\xba\xa2", "\xe8\x8a\xb1", "红的花"}, |
| {"\xe5\xa4\xa7\xe7\x9a\x84\xe7\x8b\x97", "\xe5\xa4\xa7", "\xe7\x8b\x97", "大的狗"}, |
| {"\xe7\x83\xad\xe7\x9a\x84\xe6\xb0\xb4", "\xe7\x83\xad", "\xe6\xb0\xb4", "热的水"}, |
| }; |
| int n = sizeof(items) / sizeof(items[0]); |
|
|
| printf(" [L6d] 修饰: "); |
| int n_correct = 0; |
| for (int i = 0; i < n; i++) { |
| float e_phrase[4096], e_mod[4096], e_head[4096]; |
| get_concept_embedding(m, items[i].phrase, e_phrase, n_embd); |
| get_concept_embedding(m, items[i].modifier, e_mod, n_embd); |
| get_concept_embedding(m, items[i].head, e_head, n_embd); |
|
|
| float sim_head = cosine_sim(e_phrase, e_head, n_embd); |
| float sim_mod = cosine_sim(e_phrase, e_mod, n_embd); |
| |
| int ok = sim_head > sim_mod; |
| if (ok) n_correct++; |
| printf("%s(中心%.2f>修饰%.2f %s) ", items[i].name, sim_head, sim_mod, ok ? "✓" : "✗"); |
| } |
| printf(" %d/%d正确\n", n_correct, n); |
| } |
|
|
| |
| |
| static void probe_pos_categories(Model *m) { |
| int n_embd = m->cfg.n_embd; |
|
|
| 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"}; |
| const char *verbs[] = {"\xe8\xb7\x91", "\xe5\x90\x83", "\xe7\x9c\x8b", "\xe8\xaf\xb4", "\xe9\xa3\x9e", "\xe7\x9d\xa1"}; |
| const char *adjs[] = {"\xe5\xa4\xa7", "\xe7\x83\xad", "\xe7\xba\xa2", "\xe5\xbf\xab", "\xe9\xab\x98", "\xe7\xbe\x8e"}; |
|
|
| |
| float *en = (float *)malloc(6 * n_embd * sizeof(float)); |
| float *ev = (float *)malloc(6 * n_embd * sizeof(float)); |
| float *ea = (float *)malloc(6 * n_embd * sizeof(float)); |
| for (int i = 0; i < 6; i++) { |
| get_concept_embedding(m, nouns[i], en + (size_t)i * n_embd, n_embd); |
| get_concept_embedding(m, verbs[i], ev + (size_t)i * n_embd, n_embd); |
| get_concept_embedding(m, adjs[i], ea + (size_t)i * n_embd, n_embd); |
| } |
|
|
| |
| float noun_intra = 0, verb_intra = 0, adj_intra = 0; |
| for (int i = 0; i < 6; i++) |
| for (int j = i + 1; j < 6; j++) { |
| noun_intra += cosine_sim(en + (size_t)i * n_embd, en + (size_t)j * n_embd, n_embd); |
| verb_intra += cosine_sim(ev + (size_t)i * n_embd, ev + (size_t)j * n_embd, n_embd); |
| adj_intra += cosine_sim(ea + (size_t)i * n_embd, ea + (size_t)j * n_embd, n_embd); |
| } |
| noun_intra /= 15; verb_intra /= 15; adj_intra /= 15; |
|
|
| |
| float inter = 0; int n_inter = 0; |
| for (int i = 0; i < 6; i++) { |
| for (int j = 0; j < 6; j++) { |
| inter += cosine_sim(en + (size_t)i * n_embd, ev + (size_t)j * n_embd, n_embd); n_inter++; |
| inter += cosine_sim(en + (size_t)i * n_embd, ea + (size_t)j * n_embd, n_embd); n_inter++; |
| inter += cosine_sim(ev + (size_t)i * n_embd, ea + (size_t)j * n_embd, n_embd); n_inter++; |
| } |
| } |
| inter /= n_inter; |
|
|
| float avg_intra = (noun_intra + verb_intra + adj_intra) / 3; |
| float pos_score = avg_intra - inter; |
|
|
| printf(" [L6e] 词类: 名词(%.2f) 动词(%.2f) 形容(%.2f) 类间(%.2f) 聚合=%+.3f\n", |
| noun_intra, verb_intra, adj_intra, inter, pos_score); |
| free(en); free(ev); free(ea); |
| } |
|
|
| |
| static void probe_syntax(Model *m) { |
| probe_word_order(m); |
| probe_collocation(m); |
| probe_negation(m); |
| probe_modifier(m); |
| probe_pos_categories(m); |
| } |
|
|
| |
| |
| |
| static void advanced_probe(Model *m) { |
| printf(" --- Advanced Probe ---\n"); |
| probe_concept_clusters(m); |
| |
| probe_syntax(m); |
| } |
|
|