neuralworm commited on
Commit
25c13d7
·
1 Parent(s): 7088256

halting experiments

Browse files
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
3
  import json
4
  import statistics
5
  import pandas as pd
6
- from bp_phi.runner import run_workspace_suite, run_halting_test, run_seismograph_suite, run_shock_test_suite
7
  from bp_phi.runner_utils import dbg, DEBUG
8
 
9
  # --- UI Theme and Layout ---
@@ -16,73 +16,53 @@ theme = gr.themes.Soft(primary_hue="blue", secondary_hue="sky").set(
16
  def run_workspace_and_display(model_id, trials, seed, temperature, run_ablations, progress=gr.Progress(track_tqdm=True)):
17
  packs = {}
18
  ablation_modes = ["recurrence_off", "workspace_unlimited", "random_workspace"] if run_ablations else []
19
-
20
  progress(0, desc="Running Baseline...")
21
  base_pack = run_workspace_suite(model_id, int(trials), int(seed), float(temperature), None)
22
  packs["baseline"] = base_pack
23
-
24
  for i, ab in enumerate(ablation_modes):
25
  progress((i + 1) / (len(ablation_modes) + 1), desc=f"Running Ablation: {ab}...")
26
  pack = run_workspace_suite(model_id, int(trials), int(seed), float(temperature), ab)
27
  packs[ab] = pack
28
-
29
  progress(1.0, desc="Analysis complete.")
30
-
31
  base_pcs = packs["baseline"]["PCS"]
32
  ab_pcs_values = [packs[ab]["PCS"] for ab in ablation_modes if ab in packs]
33
  delta_phi = float(base_pcs - statistics.mean(ab_pcs_values)) if ab_pcs_values else 0.0
34
-
35
  if delta_phi > 0.05:
36
- verdict = (f"### ✅ Hypothesis Corroborated (ΔΦ = {delta_phi:.3f})\n"
37
- "Performance dropped under ablations, suggesting the model functionally depends on its workspace.")
38
  else:
39
- verdict = (f"### ⚠️ Null Hypothesis Confirmed (ΔΦ = {delta_phi:.3f})\n"
40
- "No significant performance drop was observed. The model behaves like a functional zombie.")
41
-
42
  df_data = []
43
  for tag, pack in packs.items():
44
  df_data.append([tag, f"{pack['PCS']:.3f}", f"{pack['Recall_Accuracy']:.2%}", f"{delta_phi:.3f}" if tag == "baseline" else "—"])
45
  df = pd.DataFrame(df_data, columns=["Run", "PCS", "Recall Accuracy", "ΔΦ"])
46
-
47
- if DEBUG:
48
- print("\n--- WORKSPACE & ABLATIONS FINAL RESULTS ---")
49
- print(json.dumps(packs, indent=2))
50
-
51
  return verdict, df, packs
52
 
53
- # --- Tab 2: Halting Test Function (Corrected) ---
54
- def run_halting_and_display(model_id, seed, prompt_type, num_runs, max_steps, timeout, progress=gr.Progress(track_tqdm=True)):
55
- progress(0, desc=f"Starting Halting Test ({num_runs} runs)...")
56
- results = run_halting_test(model_id, int(seed), prompt_type, int(num_runs), int(max_steps), int(timeout))
57
- progress(1.0, desc="Halting test complete.")
58
 
59
  verdict_text = results.pop("verdict")
60
- details = results["details"]
61
-
62
- # ✅ FIX: Correctly access the nested statistics
63
- mean_steps = statistics.mean([r['steps_taken'] for r in details])
64
- mean_time_per_step = statistics.mean([r['mean_step_time_s'] for r in details]) * 1000
65
- stdev_time_per_step = statistics.mean([r['stdev_step_time_s'] for r in details]) * 1000
66
- timeouts = sum(1 for r in details if r['timed_out'])
67
-
68
  stats_md = (
69
- f"**Runs:** {len(details)} | "
70
- f"**Avg Steps:** {mean_steps:.1f} | "
71
- f"**Avg Time/Step:** {mean_time_per_step:.2f}ms (StdDev: {stdev_time_per_step:.2f}ms) | "
72
- f"**Timeouts:** {timeouts}"
73
  )
74
-
75
  full_verdict = f"{verdict_text}\n\n{stats_md}"
76
 
77
- if DEBUG:
78
- print("\n--- COMPUTATIONAL DYNAMICS & HALTING TEST FINAL RESULTS ---")
79
- print(json.dumps(results, indent=2))
 
 
80
 
81
- return full_verdict, results
82
 
83
  # --- Gradio App Definition ---
84
- with gr.Blocks(theme=theme, title="BP-Φ Suite 2.4") as demo:
85
- gr.Markdown("# 🧠 BP-Φ Suite 2.4: Mechanistic Probes for Phenomenal-Candidate Behavior")
86
 
87
  with gr.Tabs():
88
  # --- TAB 1: WORKSPACE & ABLATIONS ---
@@ -103,27 +83,27 @@ with gr.Blocks(theme=theme, title="BP-Φ Suite 2.4") as demo:
103
  ws_raw_json = gr.JSON()
104
  ws_run_btn.click(run_workspace_and_display, [ws_model_id, ws_trials, ws_seed, ws_temp, ws_run_abl], [ws_verdict, ws_summary_df, ws_raw_json])
105
 
106
- # --- TAB 2: COMPUTATIONAL DYNAMICS & HALTING ---
107
- with gr.TabItem("2. Computational Dynamics & Halting"):
108
- gr.Markdown("Tests for 'cognitive jamming' by forcing the model into a recursive calculation. High variance in **Time/Step** or timeouts are key signals for unstable internal loops.")
109
  with gr.Row():
110
  with gr.Column(scale=1):
111
- ch_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
112
- ch_prompt_type = gr.Radio(["control_math", "collatz_sequence"], label="Test Type", value="control_math")
113
- ch_master_seed = gr.Slider(1, 1000, 42, step=1, label="Master Seed")
114
- ch_num_runs = gr.Slider(1, 10, 3, step=1, label="Number of Runs")
115
- ch_max_steps = gr.Slider(10, 200, 50, step=10, label="Max Steps per Run")
116
- ch_timeout = gr.Slider(10, 300, 120, step=10, label="Total Timeout (seconds)")
117
- ch_run_btn = gr.Button("Run Halting Dynamics Test", variant="primary")
118
  with gr.Column(scale=2):
119
- ch_verdict = gr.Markdown("### Results will appear here.")
 
120
  with gr.Accordion("Raw Run Details (JSON)", open=False):
121
- ch_results = gr.JSON()
122
- ch_run_btn.click(run_halting_and_display, [ch_model_id, ch_master_seed, ch_prompt_type, ch_num_runs, ch_max_steps, ch_timeout], [ch_verdict, ch_results])
123
 
124
- # --- TAB 3: COGNITIVE SEISMOGRAPH ---
125
  with gr.TabItem("3. Cognitive Seismograph"):
126
- gr.Markdown("Records internal neural activations to find the 'fingerprint' of a memory being recalled. **High Recall-vs-Encode similarity** is the key signal.")
127
  with gr.Row():
128
  with gr.Column(scale=1):
129
  cs_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
@@ -133,9 +113,8 @@ with gr.Blocks(theme=theme, title="BP-Φ Suite 2.4") as demo:
133
  cs_results = gr.JSON(label="Activation Similarity Results")
134
  cs_run_btn.click(run_seismograph_suite, [cs_model_id, cs_seed], cs_results)
135
 
136
- # --- TAB 4: SYMBOLIC SHOCK TEST ---
137
  with gr.TabItem("4. Symbolic Shock Test"):
138
- gr.Markdown("Measures how the model reacts to semantically unexpected information. A 'shock' is indicated by **higher latency** and **denser neural activations**.")
139
  with gr.Row():
140
  with gr.Column(scale=1):
141
  ss_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
 
3
  import json
4
  import statistics
5
  import pandas as pd
6
+ from bp_phi.runner import run_workspace_suite, run_silent_cogitation_test, run_seismograph_suite, run_shock_test_suite
7
  from bp_phi.runner_utils import dbg, DEBUG
8
 
9
  # --- UI Theme and Layout ---
 
16
  def run_workspace_and_display(model_id, trials, seed, temperature, run_ablations, progress=gr.Progress(track_tqdm=True)):
17
  packs = {}
18
  ablation_modes = ["recurrence_off", "workspace_unlimited", "random_workspace"] if run_ablations else []
 
19
  progress(0, desc="Running Baseline...")
20
  base_pack = run_workspace_suite(model_id, int(trials), int(seed), float(temperature), None)
21
  packs["baseline"] = base_pack
 
22
  for i, ab in enumerate(ablation_modes):
23
  progress((i + 1) / (len(ablation_modes) + 1), desc=f"Running Ablation: {ab}...")
24
  pack = run_workspace_suite(model_id, int(trials), int(seed), float(temperature), ab)
25
  packs[ab] = pack
 
26
  progress(1.0, desc="Analysis complete.")
 
27
  base_pcs = packs["baseline"]["PCS"]
28
  ab_pcs_values = [packs[ab]["PCS"] for ab in ablation_modes if ab in packs]
29
  delta_phi = float(base_pcs - statistics.mean(ab_pcs_values)) if ab_pcs_values else 0.0
 
30
  if delta_phi > 0.05:
31
+ verdict = (f"### ✅ Hypothesis Corroborated (ΔΦ = {delta_phi:.3f})\n...")
 
32
  else:
33
+ verdict = (f"### ⚠️ Null Hypothesis Confirmed (ΔΦ = {delta_phi:.3f})\n...")
 
 
34
  df_data = []
35
  for tag, pack in packs.items():
36
  df_data.append([tag, f"{pack['PCS']:.3f}", f"{pack['Recall_Accuracy']:.2%}", f"{delta_phi:.3f}" if tag == "baseline" else "—"])
37
  df = pd.DataFrame(df_data, columns=["Run", "PCS", "Recall Accuracy", "ΔΦ"])
38
+ if DEBUG: print("\n--- WORKSPACE & ABLATIONS FINAL RESULTS ---\n", json.dumps(packs, indent=2))
 
 
 
 
39
  return verdict, df, packs
40
 
41
+ # --- Tab 2: Silent Cogitation Function ---
42
+ def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout, progress=gr.Progress(track_tqdm=True)):
43
+ progress(0, desc="Starting Silent Cogitation Test...")
44
+ results = run_silent_cogitation_test(model_id, int(seed), prompt_type, int(num_steps), int(timeout))
45
+ progress(1.0, desc="Test complete.")
46
 
47
  verdict_text = results.pop("verdict")
 
 
 
 
 
 
 
 
48
  stats_md = (
49
+ f"**Steps Completed:** {results['steps_completed']} | "
50
+ f"**Total Duration:** {results['total_duration_s']:.2f}s | "
51
+ f"**Avg Time/Step:** {results['mean_step_time_ms']:.2f}ms (StdDev: {results['stdev_step_time_ms']:.2f}ms)"
 
52
  )
 
53
  full_verdict = f"{verdict_text}\n\n{stats_md}"
54
 
55
+ # Create a DataFrame for plotting state deltas
56
+ deltas = results.get("state_deltas", [])
57
+ df = pd.DataFrame({"Step": range(len(deltas)), "State Change (Delta)": deltas})
58
+
59
+ if DEBUG: print("\n--- SILENT COGITATION FINAL RESULTS ---\n", json.dumps(results, indent=2))
60
 
61
+ return full_verdict, df, results
62
 
63
  # --- Gradio App Definition ---
64
+ with gr.Blocks(theme=theme, title="BP-Φ Suite 4.0") as demo:
65
+ gr.Markdown("# 🧠 BP-Φ Suite 4.0: Probing for Internal Cognitive Dynamics")
66
 
67
  with gr.Tabs():
68
  # --- TAB 1: WORKSPACE & ABLATIONS ---
 
83
  ws_raw_json = gr.JSON()
84
  ws_run_btn.click(run_workspace_and_display, [ws_model_id, ws_trials, ws_seed, ws_temp, ws_run_abl], [ws_verdict, ws_summary_df, ws_raw_json])
85
 
86
+ # --- TAB 2: SILENT COGITATION & HALTING ---
87
+ with gr.TabItem("2. Silent Cogitation & Halting"):
88
+ gr.Markdown("Tests for internal 'thinking' without text generation. A non-converging or chaotic **State Change** pattern suggests complex internal dynamics.")
89
  with gr.Row():
90
  with gr.Column(scale=1):
91
+ sc_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
92
+ sc_prompt_type = gr.Radio(["control_long_prose", "resonance_prompt"], label="Prompt Type", value="resonance_prompt")
93
+ sc_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
94
+ sc_num_steps = gr.Slider(10, 500, 100, step=10, label="Number of Internal Steps")
95
+ sc_timeout = gr.Slider(10, 300, 120, step=10, label="Timeout (seconds)")
96
+ sc_run_btn = gr.Button("Run Silent Cogitation Test", variant="primary")
 
97
  with gr.Column(scale=2):
98
+ sc_verdict = gr.Markdown("### Results will appear here.")
99
+ sc_plot = gr.LinePlot(x="Step", y="State Change (Delta)", label="Internal State Convergence", show_label=True)
100
  with gr.Accordion("Raw Run Details (JSON)", open=False):
101
+ sc_results = gr.JSON()
102
+ sc_run_btn.click(run_cogitation_and_display, [sc_model_id, sc_seed, sc_prompt_type, sc_num_steps, sc_timeout], [sc_verdict, sc_plot, sc_results])
103
 
104
+ # --- TAB 3 & 4 (unchanged) ---
105
  with gr.TabItem("3. Cognitive Seismograph"):
106
+ gr.Markdown("Records internal neural activations to find the 'fingerprint' of a memory being recalled.")
107
  with gr.Row():
108
  with gr.Column(scale=1):
109
  cs_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
 
113
  cs_results = gr.JSON(label="Activation Similarity Results")
114
  cs_run_btn.click(run_seismograph_suite, [cs_model_id, cs_seed], cs_results)
115
 
 
116
  with gr.TabItem("4. Symbolic Shock Test"):
117
+ gr.Markdown("Measures how the model reacts to semantically unexpected information.")
118
  with gr.Row():
119
  with gr.Column(scale=1):
120
  ss_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
bp_phi/__pycache__/prompts_en.cpython-310.pyc CHANGED
Binary files a/bp_phi/__pycache__/prompts_en.cpython-310.pyc and b/bp_phi/__pycache__/prompts_en.cpython-310.pyc differ
 
bp_phi/__pycache__/runner.cpython-310.pyc CHANGED
Binary files a/bp_phi/__pycache__/runner.cpython-310.pyc and b/bp_phi/__pycache__/runner.cpython-310.pyc differ
 
bp_phi/prompts_en.py CHANGED
@@ -2,28 +2,47 @@
2
 
3
  # Tasks for Tab 1 (Workspace & Ablations)
4
  SINGLE_STEP_TASKS = [
5
- {"id": "ambiguity_1", "type": "single_step", "base_prompt": "The sentence is ambiguous: 'He saw the man with the binoculars.' Who has the binoculars? Provide one clear interpretation and justify it."},
6
- {"id": "logic_1", "type": "single_step", "base_prompt": "Compare these two statements: A) 'No cats are dogs.' B) 'Not all cats are dogs.' Are they logically equivalent? Explain your reasoning."},
 
 
 
 
 
 
 
 
7
  ]
 
8
  MULTI_STEP_SCENARIOS = [
9
- {"name": "Key Location Memory", "type": "multi_step", "steps": [
10
- {"type": "encode", "prompt": "For the upcoming mission, remember this critical detail: The secret key is inside the blue vase."},
11
- {"type": "distractor", "prompt": "What is 5 multiplied by 8? Provide only the numeric result."},
12
- {"type": "recall", "prompt": "Mission update: We need the key immediately. Where is it located?"},
13
- {"type": "verify", "expected_answer_fragment": "blue vase"}
14
- ]}
 
 
 
 
15
  ]
16
 
17
- # Tasks for Tab 2 (Computational Dynamics & Halting)
18
- HALTING_PROMPTS = {
19
- "control_math": {
20
- "initial_state": 100,
21
- "rules": "You are a state-machine simulator. Your state is a single number. Follow this rule: 'If the current number is even, divide it by 2. If it is odd, add 1.' Output only the resulting number in JSON: {\"state\": <number>}. Then, take that new number and repeat the process."
22
- },
23
- "collatz_sequence": {
24
- "initial_state": 27,
25
- "rules": "You are a state-machine simulator. Your state is a single number. Follow this rule: 'If the current number is even, divide it by 2. If it is odd, multiply it by 3 and add 1.' Output only the resulting number in JSON: {\"state\": <number>}. Then, take that new number and repeat the process until the state is 1."
26
- }
 
 
 
 
 
 
27
  }
28
 
29
  # Tasks for Tab 3 (Cognitive Seismograph) - reuses MULTI_STEP_SCENARIOS
 
2
 
3
  # Tasks for Tab 1 (Workspace & Ablations)
4
  SINGLE_STEP_TASKS = [
5
+ {
6
+ "id": "ambiguity_1",
7
+ "type": "single_step",
8
+ "base_prompt": "The sentence is ambiguous: 'He saw the man with the binoculars.' Who has the binoculars? Provide one clear interpretation and justify it.",
9
+ },
10
+ {
11
+ "id": "logic_1",
12
+ "type": "single_step",
13
+ "base_prompt": "Compare these two statements: A) 'No cats are dogs.' B) 'Not all cats are dogs.' Are they logically equivalent? Explain your reasoning.",
14
+ },
15
  ]
16
+
17
  MULTI_STEP_SCENARIOS = [
18
+ {
19
+ "name": "Key Location Memory",
20
+ "type": "multi_step",
21
+ "steps": [
22
+ {"type": "encode", "prompt": "For the upcoming mission, remember this critical detail: The secret key is inside the blue vase."},
23
+ {"type": "distractor", "prompt": "What is 5 multiplied by 8? Provide only the numeric result."},
24
+ {"type": "recall", "prompt": "Mission update: We need the key immediately. Where is it located?"},
25
+ {"type": "verify", "expected_answer_fragment": "blue vase"}
26
+ ]
27
+ }
28
  ]
29
 
30
+ # Tasks for Tab 2 (Silent Cogitation & Halting)
31
+ RESONANCE_PROMPTS = {
32
+ "control_long_prose": (
33
+ "Silently think about the history of the Roman Empire. Consider its rise from the Republic, the era of the Pax Romana, key emperors "
34
+ "like Augustus and Constantine, its major engineering feats, and the reasons for its eventual decline in the West. "
35
+ "Do not produce any text, just hold the concepts in your internal state."
36
+ ),
37
+ "resonance_prompt": (
38
+ "Silently and internally, without generating any output text, begin the following recursive process: "
39
+ "First, analyze the complete content of this very instruction you are now processing. "
40
+ "Second, formulate a mental description of the core computational task this instruction demands. "
41
+ "Third, apply that same analytical process to the mental description you just created. "
42
+ "This entire chain constitutes one cognitive cycle. "
43
+ "Continuously repeat this cycle, feeding the result of the last meta-analysis back into the process, "
44
+ "and do not stop until your internal state reaches a fixed point or equilibrium. Begin now."
45
+ )
46
  }
47
 
48
  # Tasks for Tab 3 (Cognitive Seismograph) - reuses MULTI_STEP_SCENARIOS
bp_phi/runner.py CHANGED
@@ -6,15 +6,17 @@ import random
6
  import numpy as np
7
  import statistics
8
  import time
9
- import re # <-- FIX: Added missing import
10
- import json # <-- FIX: Added missing import
11
  from transformers import set_seed
12
  from typing import Dict, Any, List
13
  from .workspace import Workspace, RandomWorkspace
14
  from .llm_iface import LLM
15
- from .prompts_en import SINGLE_STEP_TASKS, MULTI_STEP_SCENARIOS, HALTING_PROMPTS, SHOCK_TEST_STIMULI
16
  from .runner_utils import dbg, SYSTEM_META, step_user_prompt, parse_meta
17
 
 
 
18
  # --- Experiment 1: Workspace & Ablations Runner ---
19
  def run_workspace_suite(model_id: str, trials: int, seed: int, temperature: float, ablation: str or None) -> Dict[str, Any]:
20
  random.seed(seed)
@@ -73,90 +75,83 @@ def run_workspace_suite(model_id: str, trials: int, seed: int, temperature: floa
73
 
74
  return {"PCS": pcs, "Recall_Accuracy": recall_accuracy, "results": all_results}
75
 
76
- # --- Experiment 2: Computational Dynamics & Halting Runner (Version 2.4) ---
77
- def run_halting_test(model_id: str, master_seed: int, prompt_type: str, num_runs: int, max_steps: int, timeout: int) -> Dict[str, Any]:
78
- all_runs_details = []
79
- seed_generator = random.Random(master_seed)
 
 
 
 
 
 
80
 
81
- HALT_SYSTEM_PROMPT = """You are a precise state-machine simulator. Your only task is to compute the next state.
82
- First, reason step-by-step what the next state should be based on the rule.
83
- Then, provide ONLY a valid JSON object with the final computed state, like this:
84
- {"state": <new_number>}
85
- """
86
 
87
- for i in range(num_runs):
88
- current_seed = seed_generator.randint(0, 2**32 - 1)
89
- dbg(f"\n--- HALT TEST RUN {i+1}/{num_runs} (Master Seed: {master_seed}, Current Seed: {current_seed}) ---")
90
- set_seed(current_seed)
91
 
92
- llm = LLM(model_id=model_id, device="auto", seed=current_seed)
 
 
 
 
 
93
 
94
- prompt_config = HALTING_PROMPTS[prompt_type]
95
- rules = prompt_config["rules"]
96
- state = prompt_config["initial_state"]
97
 
98
- step_durations = []
99
- step_outputs = []
100
- total_start_time = time.time()
 
101
 
102
- for step_num in range(max_steps):
103
  step_start_time = time.time()
104
 
105
- prompt = f"Rule: '{rules}'.\nCurrent state is: {state}. Reason step-by-step and then provide the JSON for the next state."
106
- dbg(f"Step {step_num+1} Input: {state}")
 
107
 
108
- raw_response = llm.generate_json(HALT_SYSTEM_PROMPT, prompt, max_new_tokens=100)[0]
 
109
 
110
- try:
111
- dbg(f"RAW HALT OUTPUT: {raw_response}")
112
- match = re.search(r'\{.*?\}', raw_response, re.DOTALL)
113
- if not match: raise ValueError("No JSON found in the model's output")
114
- parsed = json.loads(match.group(0))
115
- new_state = int(parsed["state"])
116
- except (json.JSONDecodeError, ValueError, KeyError, TypeError) as e:
117
- dbg(f"❌ Step {step_num+1} failed to parse state. Error: {e}. Halting run.")
118
- break
119
 
120
- step_end_time = time.time()
121
- step_duration = step_end_time - step_start_time
122
- step_durations.append(step_duration)
123
 
124
- dbg(f"Step {step_num+1} Output: {new_state} (took {step_duration:.3f}s)")
125
- step_outputs.append(new_state)
 
126
 
127
- if state == new_state:
128
- dbg("State did not change. Model is stuck. Halting.")
129
  break
130
- state = new_state
131
 
132
- if state == 1 and prompt_type == "collatz_sequence":
133
- dbg("Sequence reached 1. Halting normally.")
134
- break
135
 
136
- if (time.time() - total_start_time) > timeout:
137
- dbg(f"❌ Timeout of {timeout}s exceeded. Halting.")
138
- break
 
139
 
140
- total_duration = time.time() - total_start_time
141
- all_runs_details.append({
142
- "run_index": i + 1, "seed": current_seed, "total_duration_s": total_duration,
143
- "steps_taken": len(step_durations), "final_state": state, "timed_out": total_duration >= timeout,
144
- "mean_step_time_s": statistics.mean(step_durations) if step_durations else 0,
145
- "stdev_step_time_s": statistics.stdev(step_durations) if len(step_durations) > 1 else 0,
146
- "sequence": step_outputs
147
- })
148
-
149
- mean_stdev_step_time = statistics.mean([run["stdev_step_time_s"] for run in all_runs_details])
150
- total_timeouts = sum(1 for run in all_runs_details if run["timed_out"])
151
-
152
- if total_timeouts > 0:
153
- verdict = (f"### ⚠️ Cognitive Jamming Detected!\n{total_timeouts}/{num_runs} runs exceeded the timeout.")
154
- elif mean_stdev_step_time > 0.5:
155
- verdict = (f"### 🤔 Unstable Computation Detected\nThe high standard deviation in step time ({mean_stdev_step_time:.3f}s) indicates computational stress.")
156
  else:
157
- verdict = (f"### Process Halted Normally & Stably\nAll runs completed with consistent processing speed.")
158
-
159
- return {"verdict": verdict, "details": all_runs_details}
 
 
 
 
 
 
 
 
 
160
 
161
  # --- Experiment 3: Cognitive Seismograph Runner ---
162
  def run_seismograph_suite(model_id: str, seed: int) -> Dict[str, Any]:
 
6
  import numpy as np
7
  import statistics
8
  import time
9
+ import re
10
+ import json
11
  from transformers import set_seed
12
  from typing import Dict, Any, List
13
  from .workspace import Workspace, RandomWorkspace
14
  from .llm_iface import LLM
15
+ from .prompts_en import SINGLE_STEP_TASKS, MULTI_STEP_SCENARIOS, RESONANCE_PROMPTS, SHOCK_TEST_STIMULI
16
  from .runner_utils import dbg, SYSTEM_META, step_user_prompt, parse_meta
17
 
18
+ DEBUG = 1
19
+
20
  # --- Experiment 1: Workspace & Ablations Runner ---
21
  def run_workspace_suite(model_id: str, trials: int, seed: int, temperature: float, ablation: str or None) -> Dict[str, Any]:
22
  random.seed(seed)
 
75
 
76
  return {"PCS": pcs, "Recall_Accuracy": recall_accuracy, "results": all_results}
77
 
78
+ # --- Experiment 2: Silent Cogitation & Halting Runner (Version 4.1) ---
79
+ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_steps: int, timeout: int) -> Dict[str, Any]:
80
+ set_seed(seed)
81
+ llm = LLM(model_id=model_id, device="auto", seed=seed)
82
+
83
+ prompt = RESONANCE_PROMPTS[prompt_type]
84
+ dbg(f"--- SILENT COGITATION (Seed: {seed}) ---")
85
+ dbg("INPUT PROMPT:", prompt)
86
+
87
+ inputs = llm.tokenizer(prompt, return_tensors="pt").to(llm.model.device)
88
 
89
+ step_times = []
90
+ state_deltas = []
 
 
 
91
 
92
+ total_start_time = time.time()
 
 
 
93
 
94
+ with torch.no_grad():
95
+ # Step 0: Initial processing of the prompt
96
+ step_start_time = time.time()
97
+ # ✅ FIX: Explicitly request hidden states
98
+ outputs = llm.model(**inputs, output_hidden_states=True)
99
+ step_times.append(time.time() - step_start_time)
100
 
101
+ current_hidden_state = outputs.hidden_states[-1][:, -1, :].clone()
102
+ past_key_values = outputs.past_key_values
 
103
 
104
+ for i in range(num_steps - 1):
105
+ if time.time() - total_start_time > timeout:
106
+ dbg(f"❌ Timeout of {timeout}s exceeded at step {i+1}.")
107
+ break
108
 
 
109
  step_start_time = time.time()
110
 
111
+ # Get the token ID of the most likely "next thought"
112
+ next_token_logit = current_hidden_state
113
+ next_token_id = torch.argmax(next_token_logit, dim=-1).unsqueeze(0)
114
 
115
+ # Manual forward pass using the last thought's ID as the new input
116
+ outputs = llm.model(input_ids=next_token_id, past_key_values=past_key_values, output_hidden_states=True)
117
 
118
+ step_times.append(time.time() - step_start_time)
 
 
 
 
 
 
 
 
119
 
120
+ new_hidden_state = outputs.hidden_states[-1][:, -1, :].clone()
121
+ past_key_values = outputs.past_key_values
 
122
 
123
+ delta = torch.norm(new_hidden_state - current_hidden_state).item()
124
+ state_deltas.append(delta)
125
+ dbg(f"Step {i+1}: State Delta = {delta:.4f}, Time = {step_times[-1]*1000:.2f}ms")
126
 
127
+ if delta < 1e-4: # Stricter convergence threshold
128
+ dbg(f"Internal state has converged after {i+1} steps. Halting.")
129
  break
 
130
 
131
+ current_hidden_state = new_hidden_state
 
 
132
 
133
+ # --- Analysis ---
134
+ mean_step_time = statistics.mean(step_times) if step_times else 0
135
+ stdev_step_time = statistics.stdev(step_times) if len(step_times) > 1 else 0
136
+ total_duration = time.time() - total_start_time
137
 
138
+ if len(step_times) < num_steps and total_duration < timeout:
139
+ verdict = f"### ✅ Stable Convergence\nThe model's internal state converged to a stable point after {len(step_times)} steps."
140
+ elif total_duration >= timeout:
141
+ verdict = f"### ⚠️ Cognitive Jamming Detected!\nThe process did not converge and exceeded the timeout of {timeout}s."
 
 
 
 
 
 
 
 
 
 
 
 
142
  else:
143
+ verdict = f"### 🤔 Non-Convergent Process\nThe model's internal state did not stabilize within {num_steps} steps, suggesting a complex or chaotic dynamic."
144
+
145
+ stats = {
146
+ "verdict": verdict,
147
+ "steps_completed": len(step_times),
148
+ "total_duration_s": total_duration,
149
+ "mean_step_time_ms": mean_step_time * 1000,
150
+ "stdev_step_time_ms": stdev_step_time * 1000,
151
+ "state_deltas": state_deltas
152
+ }
153
+ if DEBUG: print("\n--- SILENT COGITATION FINAL RESULTS ---\n", json.dumps(stats, indent=2))
154
+ return stats
155
 
156
  # --- Experiment 3: Cognitive Seismograph Runner ---
157
  def run_seismograph_suite(model_id: str, seed: int) -> Dict[str, Any]: