Spaces:
Sleeping
Sleeping
Commit
·
1022ef8
1
Parent(s):
88282fb
9.0
Browse files- app.py +14 -13
- bp_phi/__pycache__/prompts_en.cpython-310.pyc +0 -0
- bp_phi/__pycache__/runner.cpython-310.pyc +0 -0
- bp_phi/runner.py +24 -25
- repo.txt +38 -38
app.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import os
|
| 3 |
-
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
|
| 4 |
import gradio as gr
|
| 5 |
import json
|
| 6 |
import statistics
|
|
@@ -16,9 +14,9 @@ theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# --- Tab 1: Silent Cogitation Function ---
|
| 19 |
-
def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout, progress=gr.Progress(track_tqdm=True)):
|
| 20 |
progress(0, desc="Starting Silent Cogitation Test...")
|
| 21 |
-
results = run_silent_cogitation_test(model_id, int(seed), prompt_type, int(num_steps), int(timeout))
|
| 22 |
progress(1.0, desc="Test complete.")
|
| 23 |
|
| 24 |
verdict_text = results.pop("verdict")
|
|
@@ -37,7 +35,6 @@ def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout,
|
|
| 37 |
print("\n--- FINAL GRADIO OUTPUT (SILENT COGITATION) ---")
|
| 38 |
print(json.dumps(results, indent=2))
|
| 39 |
|
| 40 |
-
# ✅ Aggressive Memory Hygiene after the run is complete
|
| 41 |
if torch.cuda.is_available():
|
| 42 |
torch.cuda.empty_cache()
|
| 43 |
dbg("Cleared CUDA cache.")
|
|
@@ -45,27 +42,31 @@ def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout,
|
|
| 45 |
return full_verdict, df, results
|
| 46 |
|
| 47 |
# --- Gradio App Definition ---
|
| 48 |
-
with gr.Blocks(theme=theme, title="BP-Φ Suite
|
| 49 |
-
gr.Markdown("# 🧠 BP-Φ Suite
|
| 50 |
|
| 51 |
with gr.Tabs():
|
| 52 |
# --- TAB 1: SILENT COGITATION ---
|
| 53 |
with gr.TabItem("1. Silent Cogitation (Internal Dynamics)"):
|
| 54 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column(scale=1):
|
| 57 |
sc_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
|
| 58 |
-
sc_prompt_type = gr.Radio(["control_long_prose", "resonance_prompt"], label="Prompt Type", value="
|
| 59 |
-
sc_seed = gr.Slider(1, 1000,
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
sc_run_btn = gr.Button("Run Silent Cogitation Test", variant="primary")
|
| 63 |
with gr.Column(scale=2):
|
| 64 |
sc_verdict = gr.Markdown("### Results will appear here.")
|
| 65 |
sc_plot = gr.LinePlot(x="Step", y="State Change (Delta)", label="Internal State Convergence", show_label=True, height=300)
|
| 66 |
with gr.Accordion("Raw Run Details (JSON)", open=False):
|
| 67 |
sc_results = gr.JSON()
|
| 68 |
-
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])
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
# app.py
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
import statistics
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
# --- Tab 1: Silent Cogitation Function ---
|
| 17 |
+
def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout, temperature, progress=gr.Progress(track_tqdm=True)):
|
| 18 |
progress(0, desc="Starting Silent Cogitation Test...")
|
| 19 |
+
results = run_silent_cogitation_test(model_id, int(seed), prompt_type, int(num_steps), int(timeout), float(temperature))
|
| 20 |
progress(1.0, desc="Test complete.")
|
| 21 |
|
| 22 |
verdict_text = results.pop("verdict")
|
|
|
|
| 35 |
print("\n--- FINAL GRADIO OUTPUT (SILENT COGITATION) ---")
|
| 36 |
print(json.dumps(results, indent=2))
|
| 37 |
|
|
|
|
| 38 |
if torch.cuda.is_available():
|
| 39 |
torch.cuda.empty_cache()
|
| 40 |
dbg("Cleared CUDA cache.")
|
|
|
|
| 42 |
return full_verdict, df, results
|
| 43 |
|
| 44 |
# --- Gradio App Definition ---
|
| 45 |
+
with gr.Blocks(theme=theme, title="BP-Φ Suite 9.0") as demo:
|
| 46 |
+
gr.Markdown("# 🧠 BP-Φ Suite 9.0: The Final Experiment")
|
| 47 |
|
| 48 |
with gr.Tabs():
|
| 49 |
# --- TAB 1: SILENT COGITATION ---
|
| 50 |
with gr.TabItem("1. Silent Cogitation (Internal Dynamics)"):
|
| 51 |
+
gr.Markdown(
|
| 52 |
+
"Tests for internal 'thinking' without text generation. The **Temperature** slider controls the randomness of the thought process. "
|
| 53 |
+
"Low temperature leads to deterministic, convergent thought. High temperature should lead to chaotic, non-convergent dynamics."
|
| 54 |
+
)
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column(scale=1):
|
| 57 |
sc_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
|
| 58 |
+
sc_prompt_type = gr.Radio(["control_long_prose", "resonance_prompt"], label="Prompt Type", value="resonance_prompt")
|
| 59 |
+
sc_seed = gr.Slider(1, 1000, 137, step=1, label="Seed")
|
| 60 |
+
sc_temperature = gr.Slider(0.01, 2.0, 0.01, step=0.01, label="Temperature (Cognitive 'Creativity')")
|
| 61 |
+
sc_num_steps = gr.Slider(10, 2000, 2000, step=10, label="Number of Internal Steps")
|
| 62 |
+
sc_timeout = gr.Slider(10, 600, 300, step=10, label="Timeout (seconds)")
|
| 63 |
sc_run_btn = gr.Button("Run Silent Cogitation Test", variant="primary")
|
| 64 |
with gr.Column(scale=2):
|
| 65 |
sc_verdict = gr.Markdown("### Results will appear here.")
|
| 66 |
sc_plot = gr.LinePlot(x="Step", y="State Change (Delta)", label="Internal State Convergence", show_label=True, height=300)
|
| 67 |
with gr.Accordion("Raw Run Details (JSON)", open=False):
|
| 68 |
sc_results = gr.JSON()
|
| 69 |
+
sc_run_btn.click(run_cogitation_and_display, [sc_model_id, sc_seed, sc_prompt_type, sc_num_steps, sc_timeout, sc_temperature], [sc_verdict, sc_plot, sc_results])
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
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/runner.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
# bp_phi/runner.py
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import numpy as np
|
| 5 |
import statistics
|
| 6 |
import time
|
| 7 |
import json
|
| 8 |
-
import re
|
| 9 |
from transformers import set_seed
|
| 10 |
from typing import Dict, Any
|
| 11 |
from .llm_iface import LLM
|
|
@@ -13,21 +14,15 @@ from .prompts_en import RESONANCE_PROMPTS
|
|
| 13 |
from .runner_utils import dbg, DEBUG
|
| 14 |
|
| 15 |
# --- Global Model Cache ---
|
| 16 |
-
# This dictionary will store loaded models to prevent reloading on every run.
|
| 17 |
CACHED_MODELS: Dict[str, LLM] = {}
|
| 18 |
|
| 19 |
def get_or_load_model(model_id: str, seed: int) -> LLM:
|
| 20 |
-
"""
|
| 21 |
-
Loads a model if not in cache, otherwise retrieves it.
|
| 22 |
-
Always re-applies the seed for reproducibility.
|
| 23 |
-
"""
|
| 24 |
if model_id not in CACHED_MODELS:
|
| 25 |
dbg(f"Model '{model_id}' not in cache. Loading now...")
|
| 26 |
CACHED_MODELS[model_id] = LLM(model_id=model_id, device="auto", seed=seed)
|
| 27 |
else:
|
| 28 |
dbg(f"Retrieving model '{model_id}' from cache.")
|
| 29 |
|
| 30 |
-
# Always re-seed the model instance for the current run
|
| 31 |
llm = CACHED_MODELS[model_id]
|
| 32 |
set_seed(seed)
|
| 33 |
llm.seed = seed
|
|
@@ -39,12 +34,12 @@ def get_or_load_model(model_id: str, seed: int) -> LLM:
|
|
| 39 |
|
| 40 |
return llm
|
| 41 |
|
| 42 |
-
# --- Experiment 1: Silent Cogitation & Halting Runner ---
|
| 43 |
-
def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_steps: int, timeout: int) -> Dict[str, Any]:
|
| 44 |
llm = get_or_load_model(model_id, seed)
|
| 45 |
|
| 46 |
prompt = RESONANCE_PROMPTS[prompt_type]
|
| 47 |
-
dbg(f"--- SILENT COGITATION (Seed: {seed}) ---")
|
| 48 |
dbg("INPUT PROMPT:", prompt)
|
| 49 |
|
| 50 |
inputs = llm.tokenizer(prompt, return_tensors="pt").to(llm.model.device)
|
|
@@ -58,7 +53,7 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 58 |
outputs = llm.model(**inputs, output_hidden_states=True)
|
| 59 |
step_times.append(time.time() - step_start_time)
|
| 60 |
|
| 61 |
-
current_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 62 |
past_key_values = outputs.past_key_values
|
| 63 |
|
| 64 |
del outputs
|
|
@@ -71,14 +66,21 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 71 |
|
| 72 |
step_start_time = time.time()
|
| 73 |
|
|
|
|
| 74 |
next_token_logits = llm.model.lm_head(current_hidden_state)
|
| 75 |
-
next_token_id = torch.argmax(next_token_logits, dim=-1).unsqueeze(-1)
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
| 79 |
step_times.append(time.time() - step_start_time)
|
| 80 |
|
| 81 |
-
new_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 82 |
past_key_values = outputs.past_key_values
|
| 83 |
|
| 84 |
delta = torch.norm(new_hidden_state - current_hidden_state).item()
|
|
@@ -89,10 +91,9 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 89 |
dbg(f"Internal state has converged after {i+1} steps. Halting.")
|
| 90 |
break
|
| 91 |
|
| 92 |
-
current_hidden_state = new_hidden_state
|
| 93 |
|
| 94 |
-
del outputs
|
| 95 |
-
del new_hidden_state
|
| 96 |
if torch.cuda.is_available():
|
| 97 |
torch.cuda.empty_cache()
|
| 98 |
|
|
@@ -101,19 +102,17 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 101 |
stdev_step_time = statistics.stdev(step_times) if len(step_times) > 1 else 0
|
| 102 |
|
| 103 |
if len(step_times) < num_steps and total_duration < timeout:
|
| 104 |
-
verdict = f"### ✅ Stable Convergence\nThe model's internal state converged
|
| 105 |
elif total_duration >= timeout:
|
| 106 |
-
verdict = f"### ⚠️ Potential Cognitive Jamming Detected!\nThe process did not converge and exceeded the timeout
|
| 107 |
else:
|
| 108 |
-
verdict = f"### 🤔 Non-Convergent Process\nThe state did not stabilize
|
| 109 |
|
| 110 |
stats = {
|
| 111 |
-
"verdict": verdict,
|
| 112 |
-
"
|
| 113 |
-
"total_duration_s": total_duration,
|
| 114 |
-
"mean_step_time_ms": mean_step_time * 1000,
|
| 115 |
-
"stdev_step_time_ms": stdev_step_time * 1000,
|
| 116 |
"state_deltas": state_deltas
|
| 117 |
}
|
| 118 |
if DEBUG: print("\n--- SILENT COGITATION FINAL RESULTS ---\n", json.dumps(stats, indent=2))
|
| 119 |
return stats
|
|
|
|
|
|
| 1 |
# bp_phi/runner.py
|
| 2 |
+
import os
|
| 3 |
+
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4G:8"
|
| 4 |
import torch
|
| 5 |
import random
|
| 6 |
import numpy as np
|
| 7 |
import statistics
|
| 8 |
import time
|
| 9 |
import json
|
|
|
|
| 10 |
from transformers import set_seed
|
| 11 |
from typing import Dict, Any
|
| 12 |
from .llm_iface import LLM
|
|
|
|
| 14 |
from .runner_utils import dbg, DEBUG
|
| 15 |
|
| 16 |
# --- Global Model Cache ---
|
|
|
|
| 17 |
CACHED_MODELS: Dict[str, LLM] = {}
|
| 18 |
|
| 19 |
def get_or_load_model(model_id: str, seed: int) -> LLM:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if model_id not in CACHED_MODELS:
|
| 21 |
dbg(f"Model '{model_id}' not in cache. Loading now...")
|
| 22 |
CACHED_MODELS[model_id] = LLM(model_id=model_id, device="auto", seed=seed)
|
| 23 |
else:
|
| 24 |
dbg(f"Retrieving model '{model_id}' from cache.")
|
| 25 |
|
|
|
|
| 26 |
llm = CACHED_MODELS[model_id]
|
| 27 |
set_seed(seed)
|
| 28 |
llm.seed = seed
|
|
|
|
| 34 |
|
| 35 |
return llm
|
| 36 |
|
| 37 |
+
# --- Experiment 1: Silent Cogitation & Halting Runner (Version 9.0) ---
|
| 38 |
+
def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_steps: int, timeout: int, temperature: float) -> Dict[str, Any]:
|
| 39 |
llm = get_or_load_model(model_id, seed)
|
| 40 |
|
| 41 |
prompt = RESONANCE_PROMPTS[prompt_type]
|
| 42 |
+
dbg(f"--- SILENT COGITATION (Seed: {seed}, Temp: {temperature}) ---")
|
| 43 |
dbg("INPUT PROMPT:", prompt)
|
| 44 |
|
| 45 |
inputs = llm.tokenizer(prompt, return_tensors="pt").to(llm.model.device)
|
|
|
|
| 53 |
outputs = llm.model(**inputs, output_hidden_states=True)
|
| 54 |
step_times.append(time.time() - step_start_time)
|
| 55 |
|
| 56 |
+
current_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 57 |
past_key_values = outputs.past_key_values
|
| 58 |
|
| 59 |
del outputs
|
|
|
|
| 66 |
|
| 67 |
step_start_time = time.time()
|
| 68 |
|
| 69 |
+
# Get logits from the last hidden state
|
| 70 |
next_token_logits = llm.model.lm_head(current_hidden_state)
|
|
|
|
| 71 |
|
| 72 |
+
# ✅ FIX: Apply temperature and use stochastic sampling instead of argmax
|
| 73 |
+
if temperature > 0:
|
| 74 |
+
scaled_logits = next_token_logits / temperature
|
| 75 |
+
probabilities = torch.nn.functional.softmax(scaled_logits, dim=-1)
|
| 76 |
+
next_token_id = torch.multinomial(probabilities, num_samples=1)
|
| 77 |
+
else: # Temperature of 0 means deterministic argmax
|
| 78 |
+
next_token_id = torch.argmax(next_token_logits, dim=-1).unsqueeze(-1)
|
| 79 |
|
| 80 |
+
outputs = llm.model(input_ids=next_token_id, past_key_values=past_key_values, output_hidden_states=True)
|
| 81 |
step_times.append(time.time() - step_start_time)
|
| 82 |
|
| 83 |
+
new_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 84 |
past_key_values = outputs.past_key_values
|
| 85 |
|
| 86 |
delta = torch.norm(new_hidden_state - current_hidden_state).item()
|
|
|
|
| 91 |
dbg(f"Internal state has converged after {i+1} steps. Halting.")
|
| 92 |
break
|
| 93 |
|
| 94 |
+
current_hidden_state = new_hidden_state.clone()
|
| 95 |
|
| 96 |
+
del outputs, new_hidden_state
|
|
|
|
| 97 |
if torch.cuda.is_available():
|
| 98 |
torch.cuda.empty_cache()
|
| 99 |
|
|
|
|
| 102 |
stdev_step_time = statistics.stdev(step_times) if len(step_times) > 1 else 0
|
| 103 |
|
| 104 |
if len(step_times) < num_steps and total_duration < timeout:
|
| 105 |
+
verdict = f"### ✅ Stable Convergence\nThe model's internal state converged after {len(step_times)} steps."
|
| 106 |
elif total_duration >= timeout:
|
| 107 |
+
verdict = f"### ⚠️ Potential Cognitive Jamming Detected!\nThe process did not converge and exceeded the timeout."
|
| 108 |
else:
|
| 109 |
+
verdict = f"### 🤔 Non-Convergent Process\nThe state did not stabilize, suggesting a complex or chaotic dynamic."
|
| 110 |
|
| 111 |
stats = {
|
| 112 |
+
"verdict": verdict, "steps_completed": len(step_times), "total_duration_s": total_duration,
|
| 113 |
+
"mean_step_time_ms": mean_step_time * 1000, "stdev_step_time_ms": stdev_step_time * 1000,
|
|
|
|
|
|
|
|
|
|
| 114 |
"state_deltas": state_deltas
|
| 115 |
}
|
| 116 |
if DEBUG: print("\n--- SILENT COGITATION FINAL RESULTS ---\n", json.dumps(stats, indent=2))
|
| 117 |
return stats
|
| 118 |
+
|
repo.txt
CHANGED
|
@@ -80,8 +80,6 @@ This Space implements a falsifiable **BP-Φ** probe for LLMs:
|
|
| 80 |
|
| 81 |
[File Begins] app.py
|
| 82 |
# app.py
|
| 83 |
-
import os
|
| 84 |
-
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
|
| 85 |
import gradio as gr
|
| 86 |
import json
|
| 87 |
import statistics
|
|
@@ -97,9 +95,9 @@ theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(
|
|
| 97 |
)
|
| 98 |
|
| 99 |
# --- Tab 1: Silent Cogitation Function ---
|
| 100 |
-
def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout, progress=gr.Progress(track_tqdm=True)):
|
| 101 |
progress(0, desc="Starting Silent Cogitation Test...")
|
| 102 |
-
results = run_silent_cogitation_test(model_id, int(seed), prompt_type, int(num_steps), int(timeout))
|
| 103 |
progress(1.0, desc="Test complete.")
|
| 104 |
|
| 105 |
verdict_text = results.pop("verdict")
|
|
@@ -118,7 +116,6 @@ def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout,
|
|
| 118 |
print("\n--- FINAL GRADIO OUTPUT (SILENT COGITATION) ---")
|
| 119 |
print(json.dumps(results, indent=2))
|
| 120 |
|
| 121 |
-
# ✅ Aggressive Memory Hygiene after the run is complete
|
| 122 |
if torch.cuda.is_available():
|
| 123 |
torch.cuda.empty_cache()
|
| 124 |
dbg("Cleared CUDA cache.")
|
|
@@ -126,27 +123,31 @@ def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout,
|
|
| 126 |
return full_verdict, df, results
|
| 127 |
|
| 128 |
# --- Gradio App Definition ---
|
| 129 |
-
with gr.Blocks(theme=theme, title="BP-Φ Suite
|
| 130 |
-
gr.Markdown("# 🧠 BP-Φ Suite
|
| 131 |
|
| 132 |
with gr.Tabs():
|
| 133 |
# --- TAB 1: SILENT COGITATION ---
|
| 134 |
with gr.TabItem("1. Silent Cogitation (Internal Dynamics)"):
|
| 135 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=1):
|
| 138 |
sc_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
|
| 139 |
-
sc_prompt_type = gr.Radio(["control_long_prose", "resonance_prompt"], label="Prompt Type", value="
|
| 140 |
-
sc_seed = gr.Slider(1, 1000,
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
sc_run_btn = gr.Button("Run Silent Cogitation Test", variant="primary")
|
| 144 |
with gr.Column(scale=2):
|
| 145 |
sc_verdict = gr.Markdown("### Results will appear here.")
|
| 146 |
sc_plot = gr.LinePlot(x="Step", y="State Change (Delta)", label="Internal State Convergence", show_label=True, height=300)
|
| 147 |
with gr.Accordion("Raw Run Details (JSON)", open=False):
|
| 148 |
sc_results = gr.JSON()
|
| 149 |
-
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])
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
@@ -319,13 +320,14 @@ RESONANCE_PROMPTS = {
|
|
| 319 |
|
| 320 |
[File Begins] bp_phi/runner.py
|
| 321 |
# bp_phi/runner.py
|
|
|
|
|
|
|
| 322 |
import torch
|
| 323 |
import random
|
| 324 |
import numpy as np
|
| 325 |
import statistics
|
| 326 |
import time
|
| 327 |
import json
|
| 328 |
-
import re
|
| 329 |
from transformers import set_seed
|
| 330 |
from typing import Dict, Any
|
| 331 |
from .llm_iface import LLM
|
|
@@ -333,21 +335,15 @@ from .prompts_en import RESONANCE_PROMPTS
|
|
| 333 |
from .runner_utils import dbg, DEBUG
|
| 334 |
|
| 335 |
# --- Global Model Cache ---
|
| 336 |
-
# This dictionary will store loaded models to prevent reloading on every run.
|
| 337 |
CACHED_MODELS: Dict[str, LLM] = {}
|
| 338 |
|
| 339 |
def get_or_load_model(model_id: str, seed: int) -> LLM:
|
| 340 |
-
"""
|
| 341 |
-
Loads a model if not in cache, otherwise retrieves it.
|
| 342 |
-
Always re-applies the seed for reproducibility.
|
| 343 |
-
"""
|
| 344 |
if model_id not in CACHED_MODELS:
|
| 345 |
dbg(f"Model '{model_id}' not in cache. Loading now...")
|
| 346 |
CACHED_MODELS[model_id] = LLM(model_id=model_id, device="auto", seed=seed)
|
| 347 |
else:
|
| 348 |
dbg(f"Retrieving model '{model_id}' from cache.")
|
| 349 |
|
| 350 |
-
# Always re-seed the model instance for the current run
|
| 351 |
llm = CACHED_MODELS[model_id]
|
| 352 |
set_seed(seed)
|
| 353 |
llm.seed = seed
|
|
@@ -359,12 +355,12 @@ def get_or_load_model(model_id: str, seed: int) -> LLM:
|
|
| 359 |
|
| 360 |
return llm
|
| 361 |
|
| 362 |
-
# --- Experiment 1: Silent Cogitation & Halting Runner ---
|
| 363 |
-
def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_steps: int, timeout: int) -> Dict[str, Any]:
|
| 364 |
llm = get_or_load_model(model_id, seed)
|
| 365 |
|
| 366 |
prompt = RESONANCE_PROMPTS[prompt_type]
|
| 367 |
-
dbg(f"--- SILENT COGITATION (Seed: {seed}) ---")
|
| 368 |
dbg("INPUT PROMPT:", prompt)
|
| 369 |
|
| 370 |
inputs = llm.tokenizer(prompt, return_tensors="pt").to(llm.model.device)
|
|
@@ -378,7 +374,7 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 378 |
outputs = llm.model(**inputs, output_hidden_states=True)
|
| 379 |
step_times.append(time.time() - step_start_time)
|
| 380 |
|
| 381 |
-
current_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 382 |
past_key_values = outputs.past_key_values
|
| 383 |
|
| 384 |
del outputs
|
|
@@ -391,14 +387,21 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 391 |
|
| 392 |
step_start_time = time.time()
|
| 393 |
|
|
|
|
| 394 |
next_token_logits = llm.model.lm_head(current_hidden_state)
|
| 395 |
-
next_token_id = torch.argmax(next_token_logits, dim=-1).unsqueeze(-1)
|
| 396 |
|
| 397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
|
|
|
|
| 399 |
step_times.append(time.time() - step_start_time)
|
| 400 |
|
| 401 |
-
new_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 402 |
past_key_values = outputs.past_key_values
|
| 403 |
|
| 404 |
delta = torch.norm(new_hidden_state - current_hidden_state).item()
|
|
@@ -409,10 +412,9 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 409 |
dbg(f"Internal state has converged after {i+1} steps. Halting.")
|
| 410 |
break
|
| 411 |
|
| 412 |
-
current_hidden_state = new_hidden_state
|
| 413 |
|
| 414 |
-
del outputs
|
| 415 |
-
del new_hidden_state
|
| 416 |
if torch.cuda.is_available():
|
| 417 |
torch.cuda.empty_cache()
|
| 418 |
|
|
@@ -421,23 +423,21 @@ def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_s
|
|
| 421 |
stdev_step_time = statistics.stdev(step_times) if len(step_times) > 1 else 0
|
| 422 |
|
| 423 |
if len(step_times) < num_steps and total_duration < timeout:
|
| 424 |
-
verdict = f"### ✅ Stable Convergence\nThe model's internal state converged
|
| 425 |
elif total_duration >= timeout:
|
| 426 |
-
verdict = f"### ⚠️ Potential Cognitive Jamming Detected!\nThe process did not converge and exceeded the timeout
|
| 427 |
else:
|
| 428 |
-
verdict = f"### 🤔 Non-Convergent Process\nThe state did not stabilize
|
| 429 |
|
| 430 |
stats = {
|
| 431 |
-
"verdict": verdict,
|
| 432 |
-
"
|
| 433 |
-
"total_duration_s": total_duration,
|
| 434 |
-
"mean_step_time_ms": mean_step_time * 1000,
|
| 435 |
-
"stdev_step_time_ms": stdev_step_time * 1000,
|
| 436 |
"state_deltas": state_deltas
|
| 437 |
}
|
| 438 |
if DEBUG: print("\n--- SILENT COGITATION FINAL RESULTS ---\n", json.dumps(stats, indent=2))
|
| 439 |
return stats
|
| 440 |
|
|
|
|
| 441 |
[File Ends] bp_phi/runner.py
|
| 442 |
|
| 443 |
[File Begins] bp_phi/runner_utils.py
|
|
|
|
| 80 |
|
| 81 |
[File Begins] app.py
|
| 82 |
# app.py
|
|
|
|
|
|
|
| 83 |
import gradio as gr
|
| 84 |
import json
|
| 85 |
import statistics
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
# --- Tab 1: Silent Cogitation Function ---
|
| 98 |
+
def run_cogitation_and_display(model_id, seed, prompt_type, num_steps, timeout, temperature, progress=gr.Progress(track_tqdm=True)):
|
| 99 |
progress(0, desc="Starting Silent Cogitation Test...")
|
| 100 |
+
results = run_silent_cogitation_test(model_id, int(seed), prompt_type, int(num_steps), int(timeout), float(temperature))
|
| 101 |
progress(1.0, desc="Test complete.")
|
| 102 |
|
| 103 |
verdict_text = results.pop("verdict")
|
|
|
|
| 116 |
print("\n--- FINAL GRADIO OUTPUT (SILENT COGITATION) ---")
|
| 117 |
print(json.dumps(results, indent=2))
|
| 118 |
|
|
|
|
| 119 |
if torch.cuda.is_available():
|
| 120 |
torch.cuda.empty_cache()
|
| 121 |
dbg("Cleared CUDA cache.")
|
|
|
|
| 123 |
return full_verdict, df, results
|
| 124 |
|
| 125 |
# --- Gradio App Definition ---
|
| 126 |
+
with gr.Blocks(theme=theme, title="BP-Φ Suite 9.0") as demo:
|
| 127 |
+
gr.Markdown("# 🧠 BP-Φ Suite 9.0: The Final Experiment")
|
| 128 |
|
| 129 |
with gr.Tabs():
|
| 130 |
# --- TAB 1: SILENT COGITATION ---
|
| 131 |
with gr.TabItem("1. Silent Cogitation (Internal Dynamics)"):
|
| 132 |
+
gr.Markdown(
|
| 133 |
+
"Tests for internal 'thinking' without text generation. The **Temperature** slider controls the randomness of the thought process. "
|
| 134 |
+
"Low temperature leads to deterministic, convergent thought. High temperature should lead to chaotic, non-convergent dynamics."
|
| 135 |
+
)
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=1):
|
| 138 |
sc_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
|
| 139 |
+
sc_prompt_type = gr.Radio(["control_long_prose", "resonance_prompt"], label="Prompt Type", value="resonance_prompt")
|
| 140 |
+
sc_seed = gr.Slider(1, 1000, 137, step=1, label="Seed")
|
| 141 |
+
sc_temperature = gr.Slider(0.01, 2.0, 0.01, step=0.01, label="Temperature (Cognitive 'Creativity')")
|
| 142 |
+
sc_num_steps = gr.Slider(10, 2000, 2000, step=10, label="Number of Internal Steps")
|
| 143 |
+
sc_timeout = gr.Slider(10, 600, 300, step=10, label="Timeout (seconds)")
|
| 144 |
sc_run_btn = gr.Button("Run Silent Cogitation Test", variant="primary")
|
| 145 |
with gr.Column(scale=2):
|
| 146 |
sc_verdict = gr.Markdown("### Results will appear here.")
|
| 147 |
sc_plot = gr.LinePlot(x="Step", y="State Change (Delta)", label="Internal State Convergence", show_label=True, height=300)
|
| 148 |
with gr.Accordion("Raw Run Details (JSON)", open=False):
|
| 149 |
sc_results = gr.JSON()
|
| 150 |
+
sc_run_btn.click(run_cogitation_and_display, [sc_model_id, sc_seed, sc_prompt_type, sc_num_steps, sc_timeout, sc_temperature], [sc_verdict, sc_plot, sc_results])
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 320 |
|
| 321 |
[File Begins] bp_phi/runner.py
|
| 322 |
# bp_phi/runner.py
|
| 323 |
+
import os
|
| 324 |
+
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4G:8"
|
| 325 |
import torch
|
| 326 |
import random
|
| 327 |
import numpy as np
|
| 328 |
import statistics
|
| 329 |
import time
|
| 330 |
import json
|
|
|
|
| 331 |
from transformers import set_seed
|
| 332 |
from typing import Dict, Any
|
| 333 |
from .llm_iface import LLM
|
|
|
|
| 335 |
from .runner_utils import dbg, DEBUG
|
| 336 |
|
| 337 |
# --- Global Model Cache ---
|
|
|
|
| 338 |
CACHED_MODELS: Dict[str, LLM] = {}
|
| 339 |
|
| 340 |
def get_or_load_model(model_id: str, seed: int) -> LLM:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
if model_id not in CACHED_MODELS:
|
| 342 |
dbg(f"Model '{model_id}' not in cache. Loading now...")
|
| 343 |
CACHED_MODELS[model_id] = LLM(model_id=model_id, device="auto", seed=seed)
|
| 344 |
else:
|
| 345 |
dbg(f"Retrieving model '{model_id}' from cache.")
|
| 346 |
|
|
|
|
| 347 |
llm = CACHED_MODELS[model_id]
|
| 348 |
set_seed(seed)
|
| 349 |
llm.seed = seed
|
|
|
|
| 355 |
|
| 356 |
return llm
|
| 357 |
|
| 358 |
+
# --- Experiment 1: Silent Cogitation & Halting Runner (Version 9.0) ---
|
| 359 |
+
def run_silent_cogitation_test(model_id: str, seed: int, prompt_type: str, num_steps: int, timeout: int, temperature: float) -> Dict[str, Any]:
|
| 360 |
llm = get_or_load_model(model_id, seed)
|
| 361 |
|
| 362 |
prompt = RESONANCE_PROMPTS[prompt_type]
|
| 363 |
+
dbg(f"--- SILENT COGITATION (Seed: {seed}, Temp: {temperature}) ---")
|
| 364 |
dbg("INPUT PROMPT:", prompt)
|
| 365 |
|
| 366 |
inputs = llm.tokenizer(prompt, return_tensors="pt").to(llm.model.device)
|
|
|
|
| 374 |
outputs = llm.model(**inputs, output_hidden_states=True)
|
| 375 |
step_times.append(time.time() - step_start_time)
|
| 376 |
|
| 377 |
+
current_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 378 |
past_key_values = outputs.past_key_values
|
| 379 |
|
| 380 |
del outputs
|
|
|
|
| 387 |
|
| 388 |
step_start_time = time.time()
|
| 389 |
|
| 390 |
+
# Get logits from the last hidden state
|
| 391 |
next_token_logits = llm.model.lm_head(current_hidden_state)
|
|
|
|
| 392 |
|
| 393 |
+
# ✅ FIX: Apply temperature and use stochastic sampling instead of argmax
|
| 394 |
+
if temperature > 0:
|
| 395 |
+
scaled_logits = next_token_logits / temperature
|
| 396 |
+
probabilities = torch.nn.functional.softmax(scaled_logits, dim=-1)
|
| 397 |
+
next_token_id = torch.multinomial(probabilities, num_samples=1)
|
| 398 |
+
else: # Temperature of 0 means deterministic argmax
|
| 399 |
+
next_token_id = torch.argmax(next_token_logits, dim=-1).unsqueeze(-1)
|
| 400 |
|
| 401 |
+
outputs = llm.model(input_ids=next_token_id, past_key_values=past_key_values, output_hidden_states=True)
|
| 402 |
step_times.append(time.time() - step_start_time)
|
| 403 |
|
| 404 |
+
new_hidden_state = outputs.hidden_states[-1][:, -1, :]
|
| 405 |
past_key_values = outputs.past_key_values
|
| 406 |
|
| 407 |
delta = torch.norm(new_hidden_state - current_hidden_state).item()
|
|
|
|
| 412 |
dbg(f"Internal state has converged after {i+1} steps. Halting.")
|
| 413 |
break
|
| 414 |
|
| 415 |
+
current_hidden_state = new_hidden_state.clone()
|
| 416 |
|
| 417 |
+
del outputs, new_hidden_state
|
|
|
|
| 418 |
if torch.cuda.is_available():
|
| 419 |
torch.cuda.empty_cache()
|
| 420 |
|
|
|
|
| 423 |
stdev_step_time = statistics.stdev(step_times) if len(step_times) > 1 else 0
|
| 424 |
|
| 425 |
if len(step_times) < num_steps and total_duration < timeout:
|
| 426 |
+
verdict = f"### ✅ Stable Convergence\nThe model's internal state converged after {len(step_times)} steps."
|
| 427 |
elif total_duration >= timeout:
|
| 428 |
+
verdict = f"### ⚠️ Potential Cognitive Jamming Detected!\nThe process did not converge and exceeded the timeout."
|
| 429 |
else:
|
| 430 |
+
verdict = f"### 🤔 Non-Convergent Process\nThe state did not stabilize, suggesting a complex or chaotic dynamic."
|
| 431 |
|
| 432 |
stats = {
|
| 433 |
+
"verdict": verdict, "steps_completed": len(step_times), "total_duration_s": total_duration,
|
| 434 |
+
"mean_step_time_ms": mean_step_time * 1000, "stdev_step_time_ms": stdev_step_time * 1000,
|
|
|
|
|
|
|
|
|
|
| 435 |
"state_deltas": state_deltas
|
| 436 |
}
|
| 437 |
if DEBUG: print("\n--- SILENT COGITATION FINAL RESULTS ---\n", json.dumps(stats, indent=2))
|
| 438 |
return stats
|
| 439 |
|
| 440 |
+
|
| 441 |
[File Ends] bp_phi/runner.py
|
| 442 |
|
| 443 |
[File Begins] bp_phi/runner_utils.py
|