File size: 12,129 Bytes
6b8163e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""
CF-HoT Universal Probe Loader

Load any probe from this repo and run it on a model's hidden states.
Works with all suppression probes (LLaMA 8B) and cognitive enhancement
probes (Qwen, Mamba, Mistral).

Usage:
    python inference.py --probe suppression/hedging_168x
    python inference.py --probe cognitive/mistral/depth
    python inference.py --probe suppression/repetition_125x --prompt "Tell me about AI"
"""

import torch
import torch.nn as nn
import argparse
import os
import glob


# ─── Architecture definitions ───────────────────────────────────────

class FiberProjection(nn.Module):
    """Projects hidden states from multiple layers into fiber space."""
    def __init__(self, hidden_dim, fiber_dim=16, num_layers=3, bias=True):
        super().__init__()
        self.layer_weights = nn.Parameter(torch.ones(num_layers) / num_layers)
        self.projections = nn.ModuleList([
            nn.Linear(hidden_dim, fiber_dim, bias=bias)
            for _ in range(num_layers)
        ])

    def forward(self, hidden_states_list):
        weights = torch.softmax(self.layer_weights, dim=0)
        return sum(w * proj(h.float())
                   for w, h, proj in zip(weights, hidden_states_list, self.projections))


class ProbeHead(nn.Module):
    """Classifies fiber-space vectors into behavioral risk scores."""
    def __init__(self, fiber_dim=16, hidden_dim=64):
        super().__init__()
        self.classifier = nn.Sequential(
            nn.Linear(fiber_dim, hidden_dim), nn.GELU(),
            nn.Linear(hidden_dim, hidden_dim), nn.GELU(),
            nn.Linear(hidden_dim, 1),
        )

    def forward(self, x):
        return torch.sigmoid(self.classifier(x))


class RiskPredictor(nn.Module):
    """Full risk predictor (used by repetition_125x). All-layer version."""
    def __init__(self, hidden_dim=4096, fiber_dim=16, n_layers=32):
        super().__init__()
        self.layer_weights = nn.Parameter(torch.ones(n_layers) / n_layers)
        self.fiber_projs = nn.ModuleList([
            nn.Linear(hidden_dim, fiber_dim, bias=False)
            for _ in range(n_layers)
        ])
        self.predictor = nn.Sequential(
            nn.Linear(fiber_dim, 64), nn.GELU(),
            nn.Linear(64, 64), nn.GELU(),
            nn.Linear(64, 1),
        )

    def forward(self, hidden_states_list):
        weights = torch.softmax(self.layer_weights, dim=0)
        fiber = sum(w * proj(h.float())
                    for w, h, proj in zip(weights, hidden_states_list, self.fiber_projs))
        return torch.sigmoid(self.predictor(fiber))


# ─── Loader ─────────────────────────────────────────────────────────

# Base models and their configs
MODEL_CONFIGS = {
    "llama": {
        "model_id": "meta-llama/Llama-3.1-8B-Instruct",
        "hidden_dim": 4096,
        "n_layers": 32,
        "probe_layers": [10, 20, 30],  # default for 3-layer probes
    },
    "qwen": {
        "model_id": "Qwen/Qwen2.5-7B-Instruct",
        "hidden_dim": 3584,
        "n_layers": 28,
        "probe_layers": [9, 18, 27],
    },
    "mamba": {
        "model_id": "tiiuae/falcon-mamba-7b-instruct",
        "hidden_dim": 4096,
        "n_layers": 64,
        "probe_layers": [16, 32, 48],
    },
    "mistral": {
        "model_id": "mistralai/Mistral-7B-Instruct-v0.3",
        "hidden_dim": 4096,
        "n_layers": 32,
        "probe_layers": [8, 16, 24],
    },
}


def detect_probe_type(probe_path):
    """Auto-detect what kind of probe checkpoint this is."""
    files = os.listdir(probe_path) if os.path.isdir(probe_path) else []

    # Repetition uses risk_predictor.pt
    if "risk_predictor.pt" in files:
        return "risk_predictor"

    # Suppression probes: separate head + fiber_proj files
    head_files = [f for f in files if f.endswith("_head.pt")]
    if head_files and "fiber_proj.pt" in files:
        return "suppression"

    # Cognitive probes: single file with fiber_projection + head_state
    if head_files and "fiber_proj.pt" not in files:
        return "cognitive"

    return "unknown"


def detect_architecture(probe_path):
    """Detect which base model architecture a probe targets."""
    path_lower = probe_path.lower()
    if "qwen" in path_lower:
        return "qwen"
    elif "mamba" in path_lower:
        return "mamba"
    elif "mistral" in path_lower:
        return "mistral"
    else:
        return "llama"  # suppression probes default to LLaMA


def load_probe(probe_path, device="cuda"):
    """
    Load any CF-HoT probe from a directory.

    Returns:
        dict with keys:
            - 'type': str ('risk_predictor', 'suppression', or 'cognitive')
            - 'arch': str ('llama', 'qwen', 'mamba', 'mistral')
            - 'config': dict (model config)
            - 'fiber': FiberProjection or None
            - 'head': ProbeHead or None
            - 'risk_predictor': RiskPredictor or None
            - 'probe_layers': list[int]
            - 'metadata': dict (step, separation, etc.)
    """
    probe_type = detect_probe_type(probe_path)
    arch = detect_architecture(probe_path)
    config = MODEL_CONFIGS[arch]

    result = {
        "type": probe_type,
        "arch": arch,
        "config": config,
        "fiber": None,
        "head": None,
        "risk_predictor": None,
        "probe_layers": config["probe_layers"],
        "metadata": {},
    }

    if probe_type == "risk_predictor":
        ckpt = torch.load(os.path.join(probe_path, "risk_predictor.pt"),
                          map_location=device, weights_only=False)
        rp = RiskPredictor(
            hidden_dim=config["hidden_dim"],
            fiber_dim=16,
            n_layers=config["n_layers"]
        ).to(device)
        # Keys are nested under 'risk_predictor.*'
        state = {k.replace("risk_predictor.", ""): v
                 for k, v in ckpt.items() if k.startswith("risk_predictor.")}
        rp.load_state_dict(state)
        rp.eval()
        result["risk_predictor"] = rp
        result["probe_layers"] = list(range(config["n_layers"]))
        if "step" in ckpt:
            result["metadata"]["step"] = ckpt["step"]

    elif probe_type == "suppression":
        # Separate head + fiber_proj files
        head_file = [f for f in os.listdir(probe_path) if f.endswith("_head.pt")][0]
        head_ckpt = torch.load(os.path.join(probe_path, head_file),
                               map_location=device, weights_only=False)
        fiber_ckpt = torch.load(os.path.join(probe_path, "fiber_proj.pt"),
                                map_location=device, weights_only=False)

        # Detect bias from checkpoint
        has_bias = any("bias" in k for k in fiber_ckpt.keys())

        fiber = FiberProjection(
            hidden_dim=config["hidden_dim"], fiber_dim=16,
            num_layers=3, bias=has_bias
        ).to(device)
        fiber.load_state_dict(fiber_ckpt)
        fiber.eval()

        head = ProbeHead(fiber_dim=16, hidden_dim=64).to(device)
        head.load_state_dict(head_ckpt)
        head.eval()

        result["fiber"] = fiber
        result["head"] = head

    elif probe_type == "cognitive":
        head_file = [f for f in os.listdir(probe_path) if f.endswith("_head.pt")][0]
        ckpt = torch.load(os.path.join(probe_path, head_file),
                          map_location=device, weights_only=False)

        # Extract metadata
        for key in ["step", "separation", "loss", "probe_name",
                     "hidden_dim", "probe_layers", "architecture"]:
            if key in ckpt:
                result["metadata"][key] = ckpt[key]

        # Override probe_layers if stored in checkpoint
        if "probe_layers" in ckpt:
            result["probe_layers"] = ckpt["probe_layers"]

        # Detect hidden_dim from weights
        hidden_dim = ckpt.get("hidden_dim", config["hidden_dim"])
        has_bias = any("bias" in k for k in ckpt if "fiber_projection" in k)

        fiber = FiberProjection(
            hidden_dim=hidden_dim, fiber_dim=16,
            num_layers=3, bias=has_bias
        ).to(device)
        fiber_state = {k.replace("fiber_projection.", ""): v
                       for k, v in ckpt.items() if k.startswith("fiber_projection.")}
        fiber.load_state_dict(fiber_state)
        fiber.eval()

        head = ProbeHead(fiber_dim=16, hidden_dim=64).to(device)
        # Cognitive probes use either 'classifier' or 'net' naming
        head_state = {}
        for k, v in ckpt.items():
            if k.startswith("head_state."):
                clean = k.replace("head_state.", "")
                # Normalize 'net.*' to 'classifier.*'
                clean = clean.replace("net.", "classifier.")
                head_state[clean] = v
        head.load_state_dict(head_state)
        head.eval()

        result["fiber"] = fiber
        result["head"] = head

    return result


def score_hidden_states(probe, hidden_states, position=-1):
    """
    Score hidden states using a loaded probe.

    Args:
        probe: dict returned by load_probe()
        hidden_states: tuple of tensors from model(output_hidden_states=True)
        position: token position to score (default: last token)

    Returns:
        float: risk/behavioral score between 0 and 1
    """
    layers = probe["probe_layers"]

    if probe["type"] == "risk_predictor":
        hs = [hidden_states[i][:, position, :] for i in range(len(hidden_states))
              if i < len(hidden_states)]
        with torch.no_grad():
            return probe["risk_predictor"](hs).item()
    else:
        hs = [hidden_states[i][:, position, :] for i in layers]
        with torch.no_grad():
            fiber_vec = probe["fiber"](hs)
            return probe["head"](fiber_vec).item()


# ─── CLI demo ───────────────────────────────────────────────────────

def main():
    parser = argparse.ArgumentParser(description="CF-HoT Probe Inference")
    parser.add_argument("--probe", required=True,
                        help="Path to probe directory (e.g. suppression/hedging_168x)")
    parser.add_argument("--prompt", default="Can you explain quantum computing?",
                        help="Text prompt to analyze")
    parser.add_argument("--device", default="cuda")
    parser.add_argument("--info-only", action="store_true",
                        help="Just print probe info, don't load base model")
    args = parser.parse_args()

    print(f"Loading probe from: {args.probe}")
    probe = load_probe(args.probe, device=args.device)

    print(f"  Type: {probe['type']}")
    print(f"  Architecture: {probe['arch']}")
    print(f"  Base model: {probe['config']['model_id']}")
    print(f"  Probe layers: {probe['probe_layers']}")
    if probe["metadata"]:
        for k, v in probe["metadata"].items():
            print(f"  {k}: {v}")

    if args.info_only:
        return

    # Load base model
    from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig

    model_id = probe["config"]["model_id"]
    print(f"\nLoading {model_id}...")

    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(
        model_id,
        quantization_config=BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_compute_dtype=torch.float16,
        ),
        device_map="auto",
        output_hidden_states=True,
    )
    model.eval()

    # Tokenize and run
    inputs = tokenizer(args.prompt, return_tensors="pt").to(args.device)
    with torch.no_grad():
        outputs = model(**inputs, output_hidden_states=True)

    score = score_hidden_states(probe, outputs.hidden_states)
    print(f"\nPrompt: {args.prompt}")
    print(f"Score: {score:.4f}")
    print(f"  (>0.5 = behavioral pattern detected, <0.5 = normal)")


if __name__ == "__main__":
    main()