File size: 3,713 Bytes
421f9b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Do the cached KV latents actually DIFFER between positions?

Chain of evidence: making the softmax selective (32x score scale) changed nothing,
and disabling attention entirely changed the output but not its quality. That is what
you would see if every cached position held nearly the SAME latent — then attending
sharply and mean-pooling give the same answer, and the score scale is irrelevant.

This measures pairwise cosine between c_cache rows across positions. Near 1.0 means
attention has nothing to choose between, and the bug is in the kv_a path, not the
softmax.
"""
import sys, ctypes
sys.path.insert(0, r"E:\ABU\NEOCORTEX")
import numpy as np
import torch
from gguf import GGUFReader
from faie_kernel import load_kernel
from faie_generate import FAIEInference, KV_LORA_RANK, QK_ROPE_DIM, HIDDEN_DIM
from expert_locality import GGUFTokenizer
import faie_generate

MODEL = r"E:\ABU\MODELS\llm\glm-5.2-ream\GLM-5.2-REAM-Q2_K.gguf"
FAIE_DIR = r"E:\ABU\NEOCORTEX\faie_model"
VP = ctypes.c_void_p

reader = GGUFReader(MODEL, "r")
lib = load_kernel()
tok = GGUFTokenizer(reader)
eng = FAIEInference(reader, lib, FAIE_DIR, n_layers=78)
faie_generate.FAIEInference._moe_ffn_warm = lambda self, x, li: lib.full_moe_layer_v2(
    VP(x.data_ptr()), li, VP(self._norm_buf.data_ptr()),
    VP(self._gate_buf.data_ptr()), VP(self._mega_temp.data_ptr()))
eng.forward_single_token(1)
lib.sync_device(); torch.cuda.synchronize()

# Deliberately heterogeneous prompt so the positions SHOULD differ a lot.
IDS = tok.encode("Paris zebra 7 quantum ocean the France capital", limit=32)
print(f"prompt tokens: {len(IDS)}  ids {IDS}\n")
for _ in range(8):
    eng.reset_context()
    for t in IDS:
        eng.forward_single_token(t)
    lib.sync_device(); torch.cuda.synchronize()
    if eng.page_in_misses()[0] == 0:
        break

eng.reset_context()
for t in IDS:
    eng.forward_single_token(t)
lib.sync_device(); torch.cuda.synchronize()
T = eng.pos
print(f"cached positions = {T}\n")


def pairwise(mat):
    m = mat / (mat.norm(dim=-1, keepdim=True) + 1e-12)
    g = m @ m.T
    iu = torch.triu_indices(g.shape[0], g.shape[0], offset=1)
    v = g[iu[0], iu[1]]
    return float(v.mean()), float(v.min()), float(v.max())


print(f"{'L':>3} | {'c latent: mean':>14} {'min':>8} {'max':>8} | "
      f"{'k_rope: mean':>13} {'min':>8} {'max':>8} | {'|c| spread':>11}")
print("-" * 92)
for L in (0, 4, 8, 12, 24, 40, 60, 77):
    C = eng._c_cache[L][: T * KV_LORA_RANK].view(T, KV_LORA_RANK).float()
    K = eng._kr_cache[L][: T * QK_ROPE_DIM].view(T, QK_ROPE_DIM).float()
    cm, cn, cx = pairwise(C)
    km, kn, kx = pairwise(K)
    norms = C.norm(dim=-1)
    spread = float(norms.std() / (norms.mean() + 1e-12))
    print(f"{L:>3} | {cm:>14.5f} {cn:>8.5f} {cx:>8.5f} | "
          f"{km:>13.5f} {kn:>8.5f} {kx:>8.5f} | {spread:>11.5f}")

print("\nIf 'c latent mean' is ~0.99+, every position holds the same vector and")
print("attention CANNOT discriminate — the fault is upstream in the kv_a path.")
print("Healthy values for unrelated tokens would be roughly 0.0-0.6.")

# Same probe on the residual stream itself, to see if positions differ at all.
print("\n=== residual x per position (recomputed) ===")
xs = []
eng.reset_context()
from faie_generate import dequant_q2k_fast
for t in IDS:
    eng.forward_single_token(t)
    lib.sync_device(); torch.cuda.synchronize()
    xs.append(eng._x_buf.clone())
X = torch.stack(xs).float()
m, n, x = pairwise(X)
print(f"  final hidden state across {len(IDS)} positions: mean cos {m:.5f} "
      f"min {n:.5f} max {x:.5f}")
print("  (if ~1.0, every token produces the same hidden state -> nothing downstream")
print("   can distinguish them, and that is the whole bug)")