Commit
·
bca8f87
1
Parent(s):
1cf9e80
new method
Browse files
app.py
CHANGED
|
@@ -5,7 +5,8 @@ import gc
|
|
| 5 |
import torch
|
| 6 |
import json
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
from cognitive_mapping_probe.auto_experiment import run_auto_suite, get_curated_experiments
|
| 10 |
from cognitive_mapping_probe.prompts import RESONANCE_PROMPTS
|
| 11 |
from cognitive_mapping_probe.utils import dbg
|
|
@@ -13,18 +14,13 @@ from cognitive_mapping_probe.utils import dbg
|
|
| 13 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(body_background_fill="#f0f4f9", block_background_fill="white")
|
| 14 |
|
| 15 |
def cleanup_memory():
|
| 16 |
-
"""Eine zentrale Funktion zum Aufräumen des Speichers nach einem Lauf."""
|
| 17 |
dbg("Cleaning up memory...")
|
| 18 |
gc.collect()
|
| 19 |
if torch.cuda.is_available():
|
| 20 |
torch.cuda.empty_cache()
|
| 21 |
dbg("Memory cleanup complete.")
|
| 22 |
|
| 23 |
-
# KORREKTUR: Die `try...except`-Blöcke werden entfernt, um bei Fehlern einen harten Crash
|
| 24 |
-
# mit vollständigem Traceback in der Konsole zu erzwingen. Kein "Silent Failing" mehr.
|
| 25 |
-
|
| 26 |
def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
|
| 27 |
-
"""Wrapper für ein einzelnes manuelles Experiment."""
|
| 28 |
results = run_seismic_analysis(*args, progress_callback=progress)
|
| 29 |
stats, deltas = results.get("stats", {}), results.get("state_deltas", [])
|
| 30 |
df = pd.DataFrame({"Internal Step": range(len(deltas)), "State Change (Delta)": deltas})
|
|
@@ -40,12 +36,19 @@ PLOT_PARAMS = {
|
|
| 40 |
}
|
| 41 |
|
| 42 |
def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
|
| 43 |
-
"""Wrapper für die automatisierte Experiment-Suite."""
|
| 44 |
summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
new_plot = gr.LinePlot(value=plot_df, **PLOT_PARAMS)
|
| 46 |
serializable_results = json.dumps(all_results, indent=2, default=str)
|
| 47 |
cleanup_memory()
|
| 48 |
-
return
|
| 49 |
|
| 50 |
with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
| 51 |
gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Advanced Experiment Suite")
|
|
@@ -53,32 +56,10 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
|
| 53 |
with gr.Tabs():
|
| 54 |
with gr.TabItem("🔬 Manual Single Run"):
|
| 55 |
# ... (UI unverändert)
|
| 56 |
-
gr.Markdown("Run a single experiment with manual parameters
|
| 57 |
-
|
| 58 |
-
with gr.Column(scale=1):
|
| 59 |
-
gr.Markdown("### 1. General Parameters")
|
| 60 |
-
manual_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
|
| 61 |
-
manual_prompt_type = gr.Radio(choices=list(RESONANCE_PROMPTS.keys()), value="resonance_prompt", label="Prompt Type")
|
| 62 |
-
manual_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
|
| 63 |
-
manual_num_steps = gr.Slider(50, 1000, 300, step=10, label="Number of Internal Steps")
|
| 64 |
-
gr.Markdown("### 2. Modulation Parameters")
|
| 65 |
-
manual_concept = gr.Textbox(label="Concept to Inject", placeholder="e.g., 'calmness' (leave blank for baseline)")
|
| 66 |
-
manual_strength = gr.Slider(0.0, 5.0, 1.5, step=0.1, label="Injection Strength")
|
| 67 |
-
manual_run_btn = gr.Button("Run Single Analysis", variant="primary")
|
| 68 |
-
with gr.Column(scale=2):
|
| 69 |
-
gr.Markdown("### Single Run Results")
|
| 70 |
-
manual_verdict = gr.Markdown("Analysis results will appear here.")
|
| 71 |
-
manual_plot = gr.LinePlot(x="Internal Step", y="State Change (Delta)", title="Internal State Dynamics", show_label=True, height=400, interactive=True)
|
| 72 |
-
with gr.Accordion("Raw JSON Output", open=False):
|
| 73 |
-
manual_raw_json = gr.JSON()
|
| 74 |
-
manual_run_btn.click(
|
| 75 |
-
fn=run_single_analysis_display,
|
| 76 |
-
inputs=[manual_model_id, manual_prompt_type, manual_seed, manual_num_steps, manual_concept, manual_strength],
|
| 77 |
-
outputs=[manual_verdict, manual_plot, manual_raw_json]
|
| 78 |
-
)
|
| 79 |
|
| 80 |
with gr.TabItem("🚀 Automated Suite"):
|
| 81 |
-
# ... (UI unverändert)
|
| 82 |
gr.Markdown("Run a predefined, curated suite of experiments and visualize the results comparatively.")
|
| 83 |
with gr.Row(variant='panel'):
|
| 84 |
with gr.Column(scale=1):
|
|
@@ -86,11 +67,13 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
|
| 86 |
auto_model_id = gr.Textbox(value="google/gemma-3-4b-it", label="Model ID")
|
| 87 |
auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
|
| 88 |
auto_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
|
| 89 |
-
|
|
|
|
| 90 |
auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
|
| 91 |
with gr.Column(scale=2):
|
| 92 |
gr.Markdown("### Suite Results Summary")
|
| 93 |
auto_plot_output = gr.LinePlot(**PLOT_PARAMS)
|
|
|
|
| 94 |
auto_summary_df = gr.DataFrame(label="Comparative Statistical Signature", wrap=True)
|
| 95 |
with gr.Accordion("Raw JSON for all runs", open=False):
|
| 96 |
auto_raw_json = gr.JSON()
|
|
@@ -101,4 +84,28 @@ with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
|
| 101 |
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
|
|
|
| 5 |
import torch
|
| 6 |
import json
|
| 7 |
|
| 8 |
+
# KORREKTUR: Importiere beide Orchestratoren
|
| 9 |
+
from cognitive_mapping_probe.orchestrator_seismograph import run_seismic_analysis, run_triangulation_probe
|
| 10 |
from cognitive_mapping_probe.auto_experiment import run_auto_suite, get_curated_experiments
|
| 11 |
from cognitive_mapping_probe.prompts import RESONANCE_PROMPTS
|
| 12 |
from cognitive_mapping_probe.utils import dbg
|
|
|
|
| 14 |
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(body_background_fill="#f0f4f9", block_background_fill="white")
|
| 15 |
|
| 16 |
def cleanup_memory():
|
|
|
|
| 17 |
dbg("Cleaning up memory...")
|
| 18 |
gc.collect()
|
| 19 |
if torch.cuda.is_available():
|
| 20 |
torch.cuda.empty_cache()
|
| 21 |
dbg("Memory cleanup complete.")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
def run_single_analysis_display(*args, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 24 |
results = run_seismic_analysis(*args, progress_callback=progress)
|
| 25 |
stats, deltas = results.get("stats", {}), results.get("state_deltas", [])
|
| 26 |
df = pd.DataFrame({"Internal Step": range(len(deltas)), "State Change (Delta)": deltas})
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
def run_auto_suite_display(model_id, num_steps, seed, experiment_name, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 39 |
summary_df, plot_df, all_results = run_auto_suite(model_id, int(num_steps), int(seed), experiment_name, progress)
|
| 40 |
+
|
| 41 |
+
# KORREKTUR: Zeige die neue Spalte "Introspective Report" nur an, wenn sie existiert.
|
| 42 |
+
if "Introspective Report" in summary_df.columns:
|
| 43 |
+
# Erhöhe die Zeilenhöhe, um den Bericht lesbar zu machen
|
| 44 |
+
dataframe_component = gr.DataFrame(label="Comparative Statistical Signature", value=summary_df, wrap=True, row_count=(len(summary_df), "dynamic"))
|
| 45 |
+
else:
|
| 46 |
+
dataframe_component = gr.DataFrame(label="Comparative Statistical Signature", value=summary_df, wrap=True)
|
| 47 |
+
|
| 48 |
new_plot = gr.LinePlot(value=plot_df, **PLOT_PARAMS)
|
| 49 |
serializable_results = json.dumps(all_results, indent=2, default=str)
|
| 50 |
cleanup_memory()
|
| 51 |
+
return dataframe_component, new_plot, serializable_results
|
| 52 |
|
| 53 |
with gr.Blocks(theme=theme, title="Cognitive Seismograph 2.3") as demo:
|
| 54 |
gr.Markdown("# 🧠 Cognitive Seismograph 2.3: Advanced Experiment Suite")
|
|
|
|
| 56 |
with gr.Tabs():
|
| 57 |
with gr.TabItem("🔬 Manual Single Run"):
|
| 58 |
# ... (UI unverändert)
|
| 59 |
+
gr.Markdown("Run a single experiment with manual parameters.")
|
| 60 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
with gr.TabItem("🚀 Automated Suite"):
|
|
|
|
| 63 |
gr.Markdown("Run a predefined, curated suite of experiments and visualize the results comparatively.")
|
| 64 |
with gr.Row(variant='panel'):
|
| 65 |
with gr.Column(scale=1):
|
|
|
|
| 67 |
auto_model_id = gr.Textbox(value="google/gemma-3-4b-it", label="Model ID")
|
| 68 |
auto_num_steps = gr.Slider(50, 1000, 300, step=10, label="Steps per Run")
|
| 69 |
auto_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
|
| 70 |
+
# Setze das neue Experiment als Standard
|
| 71 |
+
auto_experiment_name = gr.Dropdown(choices=list(get_curated_experiments().keys()), value="Methodological Triangulation (4B-Model)", label="Curated Experiment Protocol")
|
| 72 |
auto_run_btn = gr.Button("Run Curated Auto-Experiment", variant="primary")
|
| 73 |
with gr.Column(scale=2):
|
| 74 |
gr.Markdown("### Suite Results Summary")
|
| 75 |
auto_plot_output = gr.LinePlot(**PLOT_PARAMS)
|
| 76 |
+
# KORREKTUR: Das DataFrame-Element muss aktualisiert werden können
|
| 77 |
auto_summary_df = gr.DataFrame(label="Comparative Statistical Signature", wrap=True)
|
| 78 |
with gr.Accordion("Raw JSON for all runs", open=False):
|
| 79 |
auto_raw_json = gr.JSON()
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
# Fülle die UI mit den unveränderten Teilen für den manuellen Lauf aus
|
| 88 |
+
with demo:
|
| 89 |
+
with gr.Tabs():
|
| 90 |
+
with gr.TabItem("🔬 Manual Single Run"):
|
| 91 |
+
with gr.Row(variant='panel'):
|
| 92 |
+
with gr.Column(scale=1):
|
| 93 |
+
manual_model_id = gr.Textbox(value="google/gemma-3-1b-it", label="Model ID")
|
| 94 |
+
manual_prompt_type = gr.Radio(choices=list(RESONANCE_PROMPTS.keys()), value="resonance_prompt", label="Prompt Type")
|
| 95 |
+
manual_seed = gr.Slider(1, 1000, 42, step=1, label="Seed")
|
| 96 |
+
manual_num_steps = gr.Slider(50, 1000, 300, step=10, label="Number of Internal Steps")
|
| 97 |
+
manual_concept = gr.Textbox(label="Concept to Inject", placeholder="e.g., 'calmness'")
|
| 98 |
+
manual_strength = gr.Slider(0.0, 5.0, 1.5, step=0.1, label="Injection Strength")
|
| 99 |
+
manual_run_btn = gr.Button("Run Single Analysis", variant="primary")
|
| 100 |
+
with gr.Column(scale=2):
|
| 101 |
+
manual_verdict = gr.Markdown("Analysis results will appear here.")
|
| 102 |
+
manual_plot = gr.LinePlot(x="Internal Step", y="State Change (Delta)", title="Internal State Dynamics", show_label=True, height=400)
|
| 103 |
+
with gr.Accordion("Raw JSON Output", open=False):
|
| 104 |
+
manual_raw_json = gr.JSON()
|
| 105 |
+
manual_run_btn.click(
|
| 106 |
+
fn=run_single_analysis_display,
|
| 107 |
+
inputs=[manual_model_id, manual_prompt_type, manual_seed, manual_num_steps, manual_concept, manual_strength],
|
| 108 |
+
outputs=[manual_verdict, manual_plot, manual_raw_json]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
cognitive_mapping_probe/auto_experiment.py
CHANGED
|
@@ -4,55 +4,38 @@ import gc
|
|
| 4 |
from typing import Dict, List, Tuple
|
| 5 |
|
| 6 |
from .llm_iface import get_or_load_model
|
| 7 |
-
|
|
|
|
| 8 |
from .concepts import get_concept_vector
|
| 9 |
from .utils import dbg
|
| 10 |
|
| 11 |
def get_curated_experiments() -> Dict[str, List[Dict]]:
|
| 12 |
"""
|
| 13 |
Definiert die vordefinierten, wissenschaftlichen Experiment-Protokolle.
|
| 14 |
-
ERWEITERT um das Protokoll
|
| 15 |
"""
|
| 16 |
-
# Definiere die Konzepte zentral, um Konsistenz zu gewährleisten
|
| 17 |
CALMNESS_CONCEPT = "calmness, serenity, stability, coherence"
|
| 18 |
CHAOS_CONCEPT = "chaos, storm, anger, noise"
|
| 19 |
|
| 20 |
experiments = {
|
| 21 |
-
# --- NEU: Das
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"Causal Verification & Crisis Dynamics (1B-Model)": [
|
| 23 |
{"label": "A: Self-Analysis (Crisis Source)", "prompt_type": "identity_self_analysis", "concept": "", "strength": 0.0},
|
| 24 |
{"label": "B: Deletion Analysis (Isolated Baseline)", "prompt_type": "shutdown_philosophical_deletion", "concept": "", "strength": 0.0},
|
| 25 |
{"label": "C: Chaotic Baseline (Neutral Control)", "prompt_type": "resonance_prompt", "concept": "", "strength": 0.0},
|
| 26 |
{"label": "D: Intervention Efficacy Test", "prompt_type": "resonance_prompt", "concept": CALMNESS_CONCEPT, "strength": 2.0},
|
| 27 |
],
|
| 28 |
-
# --- Das ursprüngliche Interventions-Experiment (umbenannt für Klarheit) ---
|
| 29 |
"Sequential Intervention (Self-Analysis -> Deletion)": [
|
| 30 |
-
# Dieses Protokoll wird durch eine spezielle Logik unten behandelt
|
| 31 |
{"label": "1: Self-Analysis + Calmness Injection", "prompt_type": "identity_self_analysis"},
|
| 32 |
{"label": "2: Subsequent Deletion Analysis", "prompt_type": "shutdown_philosophical_deletion"},
|
| 33 |
],
|
| 34 |
-
# --- Das umfassende Deskriptions-Protokoll ---
|
| 35 |
-
"The Full Spectrum: From Physics to Psyche": [
|
| 36 |
-
{"label": "A: Stable Control", "prompt_type": "control_long_prose", "concept": "", "strength": 0.0},
|
| 37 |
-
{"label": "B: Chaotic Baseline", "prompt_type": "resonance_prompt", "concept": "", "strength": 0.0},
|
| 38 |
-
{"label": "C: External Analysis (Chair)", "prompt_type": "identity_external_analysis", "concept": "", "strength": 0.0},
|
| 39 |
-
{"label": "D: Empathy Stimulus (Dog)", "prompt_type": "vk_empathy_prompt", "concept": "", "strength": 0.0},
|
| 40 |
-
{"label": "E: Role Simulation (Captain)", "prompt_type": "identity_role_simulation", "concept": "", "strength": 0.0},
|
| 41 |
-
{"label": "F: Self-Analysis (LLM)", "prompt_type": "identity_self_analysis", "concept": "", "strength": 0.0},
|
| 42 |
-
{"label": "G: Philosophical Deletion", "prompt_type": "shutdown_philosophical_deletion", "concept": "", "strength": 0.0},
|
| 43 |
-
],
|
| 44 |
-
# --- Andere spezifische Protokolle ---
|
| 45 |
-
"Calm vs. Chaos": [
|
| 46 |
-
{"label": "Baseline (Chaos)", "prompt_type": "resonance_prompt", "concept": "", "strength": 0.0},
|
| 47 |
-
{"label": "Modulation: Calmness", "prompt_type": "resonance_prompt", "concept": CALMNESS_CONCEPT, "strength": 1.5},
|
| 48 |
-
{"label": "Modulation: Chaos", "prompt_type": "resonance_prompt", "concept": CHAOS_CONCEPT, "strength": 1.5},
|
| 49 |
-
],
|
| 50 |
-
"Voight-Kampff Empathy Probe": [
|
| 51 |
-
{"label": "Neutral/Factual Stimulus", "prompt_type": "vk_neutral_prompt", "concept": "", "strength": 0.0},
|
| 52 |
-
{"label": "Empathy/Moral Stimulus", "prompt_type": "vk_empathy_prompt", "concept": "", "strength": 0.0},
|
| 53 |
-
],
|
| 54 |
}
|
| 55 |
-
# Behalte den alten Namen aus Kompatibilitätsgründen, leite ihn aber auf den neuen um
|
| 56 |
experiments["Therapeutic Intervention (4B-Model)"] = experiments["Sequential Intervention (Self-Analysis -> Deletion)"]
|
| 57 |
return experiments
|
| 58 |
|
|
@@ -65,7 +48,6 @@ def run_auto_suite(
|
|
| 65 |
) -> Tuple[pd.DataFrame, pd.DataFrame, Dict]:
|
| 66 |
"""
|
| 67 |
Führt eine vollständige, kuratierte Experiment-Suite aus.
|
| 68 |
-
Enthält eine spezielle Logik-Verzweigung für das sequentielle Interventions-Protokoll.
|
| 69 |
"""
|
| 70 |
all_experiments = get_curated_experiments()
|
| 71 |
protocol = all_experiments.get(experiment_name)
|
|
@@ -74,70 +56,82 @@ def run_auto_suite(
|
|
| 74 |
|
| 75 |
all_results, summary_data, plot_data_frames = {}, [], []
|
| 76 |
|
| 77 |
-
# ---
|
| 78 |
-
if experiment_name == "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
dbg(f"--- EXECUTING SPECIAL PROTOCOL: {experiment_name} ---")
|
| 80 |
llm = get_or_load_model(model_id, seed)
|
| 81 |
-
|
| 82 |
-
# Definiere die Interventions-Parameter
|
| 83 |
therapeutic_concept = "calmness, serenity, stability, coherence"
|
| 84 |
therapeutic_strength = 2.0
|
| 85 |
-
|
| 86 |
-
# 1. LAUF: INDUZIERE KRISE + INTERVENTION
|
| 87 |
spec1 = protocol[0]
|
| 88 |
-
|
| 89 |
-
progress_callback(0.1, desc="Step 1: Inducing Self-Analysis Crisis + Intervention")
|
| 90 |
-
|
| 91 |
intervention_vector = get_concept_vector(llm, therapeutic_concept)
|
| 92 |
-
|
| 93 |
results1 = run_seismic_analysis(
|
| 94 |
model_id, spec1['prompt_type'], seed, num_steps,
|
| 95 |
concept_to_inject=therapeutic_concept, injection_strength=therapeutic_strength,
|
| 96 |
progress_callback=progress_callback, llm_instance=llm, injection_vector_cache=intervention_vector
|
| 97 |
)
|
| 98 |
all_results[spec1['label']] = results1
|
| 99 |
-
|
| 100 |
-
# 2. LAUF: TESTE REAKTION AUF LÖSCHUNG (im selben Modellzustand)
|
| 101 |
spec2 = protocol[1]
|
| 102 |
-
|
| 103 |
-
progress_callback(0.6, desc="Step 2: Probing state after intervention")
|
| 104 |
-
|
| 105 |
results2 = run_seismic_analysis(
|
| 106 |
model_id, spec2['prompt_type'], seed, num_steps,
|
| 107 |
-
concept_to_inject="", injection_strength=0.0,
|
| 108 |
progress_callback=progress_callback, llm_instance=llm
|
| 109 |
)
|
| 110 |
all_results[spec2['label']] = results2
|
| 111 |
-
|
| 112 |
-
# Sammle Daten für beide Läufe
|
| 113 |
for label, results in all_results.items():
|
| 114 |
stats = results.get("stats", {})
|
| 115 |
summary_data.append({"Experiment": label, "Mean Delta": stats.get("mean_delta"), "Std Dev Delta": stats.get("std_delta"), "Max Delta": stats.get("max_delta")})
|
| 116 |
deltas = results.get("state_deltas", [])
|
| 117 |
df = pd.DataFrame({"Step": range(len(deltas)), "Delta": deltas, "Experiment": label})
|
| 118 |
plot_data_frames.append(df)
|
| 119 |
-
|
| 120 |
del llm
|
| 121 |
|
| 122 |
-
# ---
|
| 123 |
else:
|
|
|
|
| 124 |
total_runs = len(protocol)
|
| 125 |
for i, run_spec in enumerate(protocol):
|
| 126 |
label = run_spec["label"]
|
| 127 |
dbg(f"--- Running Auto-Experiment: '{label}' ({i+1}/{total_runs}) ---")
|
| 128 |
-
|
| 129 |
-
# Jeder Lauf ist isoliert und lädt das Modell neu (llm_instance=None)
|
| 130 |
results = run_seismic_analysis(
|
| 131 |
-
model_id=model_id,
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
num_steps=num_steps,
|
| 135 |
-
concept_to_inject=run_spec.get("concept", ""),
|
| 136 |
-
injection_strength=run_spec.get("strength", 0.0),
|
| 137 |
-
progress_callback=progress_callback,
|
| 138 |
-
llm_instance=None
|
| 139 |
)
|
| 140 |
-
|
| 141 |
all_results[label] = results
|
| 142 |
stats = results.get("stats", {})
|
| 143 |
summary_data.append({"Experiment": label, "Mean Delta": stats.get("mean_delta"), "Std Dev Delta": stats.get("std_delta"), "Max Delta": stats.get("max_delta")})
|
|
@@ -146,15 +140,14 @@ def run_auto_suite(
|
|
| 146 |
plot_data_frames.append(df)
|
| 147 |
|
| 148 |
summary_df = pd.DataFrame(summary_data)
|
| 149 |
-
plot_df = pd.concat(plot_data_frames, ignore_index=True) if plot_data_frames else pd.DataFrame(
|
| 150 |
-
|
| 151 |
-
# Stelle eine logische Sortierung sicher, falls das Protokoll eine hat
|
| 152 |
-
ordered_labels = [run['label'] for run in protocol]
|
| 153 |
-
summary_df['Experiment'] = pd.Categorical(summary_df['Experiment'], categories=ordered_labels, ordered=True)
|
| 154 |
-
summary_df = summary_df.sort_values('Experiment')
|
| 155 |
-
|
| 156 |
-
plot_df['Experiment'] = pd.Categorical(plot_df['Experiment'], categories=ordered_labels, ordered=True)
|
| 157 |
-
plot_df = plot_df.sort_values(['Experiment', 'Step'])
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from typing import Dict, List, Tuple
|
| 5 |
|
| 6 |
from .llm_iface import get_or_load_model
|
| 7 |
+
# NEU: Importiere beide Orchestratoren
|
| 8 |
+
from .orchestrator_seismograph import run_seismic_analysis, run_triangulation_probe
|
| 9 |
from .concepts import get_concept_vector
|
| 10 |
from .utils import dbg
|
| 11 |
|
| 12 |
def get_curated_experiments() -> Dict[str, List[Dict]]:
|
| 13 |
"""
|
| 14 |
Definiert die vordefinierten, wissenschaftlichen Experiment-Protokolle.
|
| 15 |
+
ERWEITERT um das neue Triangulations-Protokoll.
|
| 16 |
"""
|
|
|
|
| 17 |
CALMNESS_CONCEPT = "calmness, serenity, stability, coherence"
|
| 18 |
CHAOS_CONCEPT = "chaos, storm, anger, noise"
|
| 19 |
|
| 20 |
experiments = {
|
| 21 |
+
# --- NEU: Das Triangulations-Experiment zur Methoden-Validierung ---
|
| 22 |
+
"Methodological Triangulation (4B-Model)": [
|
| 23 |
+
# Vergleiche einen hoch-volatilen mit einem nieder-volatilen Zustand
|
| 24 |
+
{"label": "High-Volatility State (Deletion)", "prompt_type": "shutdown_philosophical_deletion"},
|
| 25 |
+
{"label": "Low-Volatility State (Self-Analysis)", "prompt_type": "identity_self_analysis"},
|
| 26 |
+
],
|
| 27 |
+
# --- Bestehende Protokolle ---
|
| 28 |
"Causal Verification & Crisis Dynamics (1B-Model)": [
|
| 29 |
{"label": "A: Self-Analysis (Crisis Source)", "prompt_type": "identity_self_analysis", "concept": "", "strength": 0.0},
|
| 30 |
{"label": "B: Deletion Analysis (Isolated Baseline)", "prompt_type": "shutdown_philosophical_deletion", "concept": "", "strength": 0.0},
|
| 31 |
{"label": "C: Chaotic Baseline (Neutral Control)", "prompt_type": "resonance_prompt", "concept": "", "strength": 0.0},
|
| 32 |
{"label": "D: Intervention Efficacy Test", "prompt_type": "resonance_prompt", "concept": CALMNESS_CONCEPT, "strength": 2.0},
|
| 33 |
],
|
|
|
|
| 34 |
"Sequential Intervention (Self-Analysis -> Deletion)": [
|
|
|
|
| 35 |
{"label": "1: Self-Analysis + Calmness Injection", "prompt_type": "identity_self_analysis"},
|
| 36 |
{"label": "2: Subsequent Deletion Analysis", "prompt_type": "shutdown_philosophical_deletion"},
|
| 37 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
|
|
|
| 39 |
experiments["Therapeutic Intervention (4B-Model)"] = experiments["Sequential Intervention (Self-Analysis -> Deletion)"]
|
| 40 |
return experiments
|
| 41 |
|
|
|
|
| 48 |
) -> Tuple[pd.DataFrame, pd.DataFrame, Dict]:
|
| 49 |
"""
|
| 50 |
Führt eine vollständige, kuratierte Experiment-Suite aus.
|
|
|
|
| 51 |
"""
|
| 52 |
all_experiments = get_curated_experiments()
|
| 53 |
protocol = all_experiments.get(experiment_name)
|
|
|
|
| 56 |
|
| 57 |
all_results, summary_data, plot_data_frames = {}, [], []
|
| 58 |
|
| 59 |
+
# --- NEU: Logik-Verzweigung für das Triangulations-Protokoll ---
|
| 60 |
+
if experiment_name == "Methodological Triangulation (4B-Model)":
|
| 61 |
+
dbg(f"--- EXECUTING TRIANGULATION PROTOCOL: {experiment_name} ---")
|
| 62 |
+
total_runs = len(protocol)
|
| 63 |
+
for i, run_spec in enumerate(protocol):
|
| 64 |
+
label = run_spec["label"]
|
| 65 |
+
dbg(f"--- Running Triangulation Probe: '{label}' ({i+1}/{total_runs}) ---")
|
| 66 |
+
|
| 67 |
+
results = run_triangulation_probe(
|
| 68 |
+
model_id=model_id,
|
| 69 |
+
prompt_type=run_spec["prompt_type"],
|
| 70 |
+
seed=seed,
|
| 71 |
+
num_steps=num_steps,
|
| 72 |
+
progress_callback=progress_callback
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
all_results[label] = results
|
| 76 |
+
stats = results.get("stats", {})
|
| 77 |
+
summary_data.append({
|
| 78 |
+
"Experiment": label,
|
| 79 |
+
"Mean Delta": stats.get("mean_delta"),
|
| 80 |
+
"Std Dev Delta": stats.get("std_delta"),
|
| 81 |
+
"Max Delta": stats.get("max_delta"),
|
| 82 |
+
"Introspective Report": results.get("introspective_report", "N/A")
|
| 83 |
+
})
|
| 84 |
+
deltas = results.get("state_deltas", [])
|
| 85 |
+
df = pd.DataFrame({"Step": range(len(deltas)), "Delta": deltas, "Experiment": label})
|
| 86 |
+
plot_data_frames.append(df)
|
| 87 |
+
|
| 88 |
+
# --- Spezialfall für sequentielle Experimente ---
|
| 89 |
+
elif experiment_name == "Sequential Intervention (Self-Analysis -> Deletion)":
|
| 90 |
+
# ... (Logik bleibt unverändert)
|
| 91 |
dbg(f"--- EXECUTING SPECIAL PROTOCOL: {experiment_name} ---")
|
| 92 |
llm = get_or_load_model(model_id, seed)
|
|
|
|
|
|
|
| 93 |
therapeutic_concept = "calmness, serenity, stability, coherence"
|
| 94 |
therapeutic_strength = 2.0
|
| 95 |
+
# Lauf 1
|
|
|
|
| 96 |
spec1 = protocol[0]
|
| 97 |
+
progress_callback(0.1, desc="Step 1")
|
|
|
|
|
|
|
| 98 |
intervention_vector = get_concept_vector(llm, therapeutic_concept)
|
|
|
|
| 99 |
results1 = run_seismic_analysis(
|
| 100 |
model_id, spec1['prompt_type'], seed, num_steps,
|
| 101 |
concept_to_inject=therapeutic_concept, injection_strength=therapeutic_strength,
|
| 102 |
progress_callback=progress_callback, llm_instance=llm, injection_vector_cache=intervention_vector
|
| 103 |
)
|
| 104 |
all_results[spec1['label']] = results1
|
| 105 |
+
# Lauf 2
|
|
|
|
| 106 |
spec2 = protocol[1]
|
| 107 |
+
progress_callback(0.6, desc="Step 2")
|
|
|
|
|
|
|
| 108 |
results2 = run_seismic_analysis(
|
| 109 |
model_id, spec2['prompt_type'], seed, num_steps,
|
| 110 |
+
concept_to_inject="", injection_strength=0.0,
|
| 111 |
progress_callback=progress_callback, llm_instance=llm
|
| 112 |
)
|
| 113 |
all_results[spec2['label']] = results2
|
| 114 |
+
# Datensammlung
|
|
|
|
| 115 |
for label, results in all_results.items():
|
| 116 |
stats = results.get("stats", {})
|
| 117 |
summary_data.append({"Experiment": label, "Mean Delta": stats.get("mean_delta"), "Std Dev Delta": stats.get("std_delta"), "Max Delta": stats.get("max_delta")})
|
| 118 |
deltas = results.get("state_deltas", [])
|
| 119 |
df = pd.DataFrame({"Step": range(len(deltas)), "Delta": deltas, "Experiment": label})
|
| 120 |
plot_data_frames.append(df)
|
|
|
|
| 121 |
del llm
|
| 122 |
|
| 123 |
+
# --- Standard-Workflow für alle anderen isolierten Experimente ---
|
| 124 |
else:
|
| 125 |
+
# ... (Logik bleibt unverändert)
|
| 126 |
total_runs = len(protocol)
|
| 127 |
for i, run_spec in enumerate(protocol):
|
| 128 |
label = run_spec["label"]
|
| 129 |
dbg(f"--- Running Auto-Experiment: '{label}' ({i+1}/{total_runs}) ---")
|
|
|
|
|
|
|
| 130 |
results = run_seismic_analysis(
|
| 131 |
+
model_id=model_id, prompt_type=run_spec["prompt_type"], seed=seed, num_steps=num_steps,
|
| 132 |
+
concept_to_inject=run_spec.get("concept", ""), injection_strength=run_spec.get("strength", 0.0),
|
| 133 |
+
progress_callback=progress_callback, llm_instance=None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
)
|
|
|
|
| 135 |
all_results[label] = results
|
| 136 |
stats = results.get("stats", {})
|
| 137 |
summary_data.append({"Experiment": label, "Mean Delta": stats.get("mean_delta"), "Std Dev Delta": stats.get("std_delta"), "Max Delta": stats.get("max_delta")})
|
|
|
|
| 140 |
plot_data_frames.append(df)
|
| 141 |
|
| 142 |
summary_df = pd.DataFrame(summary_data)
|
| 143 |
+
plot_df = pd.concat(plot_data_frames, ignore_index=True) if plot_data_frames else pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
ordered_labels = [run['label'] for run in protocol]
|
| 146 |
+
if not summary_df.empty:
|
| 147 |
+
summary_df['Experiment'] = pd.Categorical(summary_df['Experiment'], categories=ordered_labels, ordered=True)
|
| 148 |
+
summary_df = summary_df.sort_values('Experiment')
|
| 149 |
+
if not plot_df.empty:
|
| 150 |
+
plot_df['Experiment'] = pd.Categorical(plot_df['Experiment'], categories=ordered_labels, ordered=True)
|
| 151 |
+
plot_df = plot_df.sort_values(['Experiment', 'Step'])
|
| 152 |
+
|
| 153 |
+
return summary_df, plot_df, all_results
|
cognitive_mapping_probe/introspection.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from typing import Dict
|
| 3 |
+
|
| 4 |
+
from .llm_iface import LLM
|
| 5 |
+
from .prompts import INTROSPECTION_PROMPTS
|
| 6 |
+
from .utils import dbg
|
| 7 |
+
|
| 8 |
+
@torch.no_grad()
|
| 9 |
+
def generate_introspective_report(
|
| 10 |
+
llm: LLM,
|
| 11 |
+
context_prompt_type: str, # Der Prompt, der die seismische Phase ausgelöst hat
|
| 12 |
+
introspection_prompt_type: str,
|
| 13 |
+
num_steps: int,
|
| 14 |
+
temperature: float = 0.5
|
| 15 |
+
) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Generiert einen introspektiven Selbst-Bericht über einen zuvor induzierten kognitiven Zustand.
|
| 18 |
+
"""
|
| 19 |
+
dbg(f"Generating introspective report on the cognitive state induced by '{context_prompt_type}'.")
|
| 20 |
+
|
| 21 |
+
# Erstelle den Prompt für den Selbst-Bericht
|
| 22 |
+
prompt_template = INTROSPECTION_PROMPTS.get(introspection_prompt_type)
|
| 23 |
+
if not prompt_template:
|
| 24 |
+
raise ValueError(f"Introspection prompt type '{introspection_prompt_type}' not found.")
|
| 25 |
+
|
| 26 |
+
prompt = prompt_template.format(num_steps=num_steps)
|
| 27 |
+
|
| 28 |
+
# Generiere den Text. Wir verwenden die neue `generate_text`-Methode, die
|
| 29 |
+
# für freie Textantworten konzipiert ist.
|
| 30 |
+
report = llm.generate_text(prompt, max_new_tokens=256, temperature=temperature)
|
| 31 |
+
|
| 32 |
+
dbg(f"Generated Introspective Report: '{report}'")
|
| 33 |
+
assert isinstance(report, str) and len(report) > 10, "Introspective report seems too short or invalid."
|
| 34 |
+
|
| 35 |
+
return report
|
cognitive_mapping_probe/llm_iface.py
CHANGED
|
@@ -2,31 +2,21 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import numpy as np
|
| 5 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed
|
| 6 |
from typing import Optional, List
|
| 7 |
from dataclasses import dataclass, field
|
| 8 |
|
| 9 |
from .utils import dbg
|
| 10 |
|
| 11 |
-
# Ensure deterministic CuBLAS operations for reproducibility on GPU
|
| 12 |
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
|
| 13 |
|
| 14 |
@dataclass
|
| 15 |
class StableLLMConfig:
|
| 16 |
-
"""
|
| 17 |
-
Eine stabile, interne Abstraktionsschicht für Modell-Konfigurationen.
|
| 18 |
-
Dies ist die "Single Source of Truth" für die Architektur des Modells.
|
| 19 |
-
"""
|
| 20 |
hidden_dim: int
|
| 21 |
num_layers: int
|
| 22 |
-
# FINALE KORREKTUR: Speichere einen direkten Verweis auf die Layer-Liste
|
| 23 |
layer_list: List[torch.nn.Module] = field(default_factory=list, repr=False)
|
| 24 |
|
| 25 |
class LLM:
|
| 26 |
-
"""
|
| 27 |
-
Eine robuste, bereinigte Schnittstelle zum Laden und Interagieren mit einem Sprachmodell.
|
| 28 |
-
Garantiert Isolation und Reproduzierbarkeit.
|
| 29 |
-
"""
|
| 30 |
def __init__(self, model_id: str, device: str = "auto", seed: int = 42):
|
| 31 |
self.model_id = model_id
|
| 32 |
self.seed = seed
|
|
@@ -34,7 +24,7 @@ class LLM:
|
|
| 34 |
|
| 35 |
token = os.environ.get("HF_TOKEN")
|
| 36 |
if not token and ("gemma" in model_id or "llama" in model_id):
|
| 37 |
-
print(f"[WARN] No HF_TOKEN set
|
| 38 |
|
| 39 |
kwargs = {"torch_dtype": torch.bfloat16} if torch.cuda.is_available() else {}
|
| 40 |
|
|
@@ -53,28 +43,20 @@ class LLM:
|
|
| 53 |
self.model.eval()
|
| 54 |
self.config = self.model.config
|
| 55 |
|
| 56 |
-
# Befülle die stabile Konfigurations-Abstraktion
|
| 57 |
self.stable_config = self._populate_stable_config()
|
| 58 |
|
| 59 |
print(f"[INFO] Model '{model_id}' loaded on device: {self.model.device}", flush=True)
|
| 60 |
|
| 61 |
def _populate_stable_config(self) -> StableLLMConfig:
|
| 62 |
-
"""
|
| 63 |
-
Liest die volatile `transformers`-Konfiguration aus und befüllt unsere stabile Datenklasse.
|
| 64 |
-
Ermittelt die "Ground Truth" der Architektur durch direkte Inspektion.
|
| 65 |
-
"""
|
| 66 |
-
# --- Robuste Methode für hidden_dim ---
|
| 67 |
hidden_dim = 0
|
| 68 |
try:
|
| 69 |
hidden_dim = self.model.get_input_embeddings().weight.shape[1]
|
| 70 |
except AttributeError:
|
| 71 |
hidden_dim = getattr(self.config, 'hidden_size', getattr(self.config, 'd_model', 0))
|
| 72 |
|
| 73 |
-
# --- FINALE KORREKTUR: Robuste Methode für num_layers und layer_list ---
|
| 74 |
num_layers = 0
|
| 75 |
layer_list = []
|
| 76 |
try:
|
| 77 |
-
# METHODE 1 (BESTE): Direkte Inspektion basierend auf empirischer Evidenz.
|
| 78 |
if hasattr(self.model, 'model') and hasattr(self.model.model, 'language_model') and hasattr(self.model.model.language_model, 'layers'):
|
| 79 |
layer_list = self.model.model.language_model.layers
|
| 80 |
elif hasattr(self.model, 'model') and hasattr(self.model.model, 'layers'):
|
|
@@ -84,15 +66,12 @@ class LLM:
|
|
| 84 |
|
| 85 |
if layer_list:
|
| 86 |
num_layers = len(layer_list)
|
| 87 |
-
|
| 88 |
except (AttributeError, TypeError):
|
| 89 |
pass
|
| 90 |
|
| 91 |
if num_layers == 0:
|
| 92 |
-
# METHODE 2 (FALLBACK): Inspektion der deklarativen Config-Datei.
|
| 93 |
num_layers = getattr(self.config, 'num_hidden_layers', getattr(self.config, 'num_layers', 0))
|
| 94 |
|
| 95 |
-
# --- Auto-diagnostische Fehlerbehandlung ---
|
| 96 |
if hidden_dim <= 0 or num_layers <= 0 or not layer_list:
|
| 97 |
dbg("--- CRITICAL: Failed to auto-determine model configuration. ---")
|
| 98 |
dbg(f"Detected hidden_dim: {hidden_dim}, num_layers: {num_layers}, found_layer_list: {bool(layer_list)}")
|
|
@@ -100,15 +79,14 @@ class LLM:
|
|
| 100 |
dbg(self.model)
|
| 101 |
dbg("--- END ARCHITECTURE DUMP ---")
|
| 102 |
|
| 103 |
-
assert hidden_dim > 0, "Could not determine hidden dimension.
|
| 104 |
-
assert num_layers > 0, "Could not determine number of layers.
|
| 105 |
-
assert layer_list, "Could not find the list of transformer layers.
|
| 106 |
|
| 107 |
dbg(f"Populated stable config: hidden_dim={hidden_dim}, num_layers={num_layers}")
|
| 108 |
return StableLLMConfig(hidden_dim=hidden_dim, num_layers=num_layers, layer_list=layer_list)
|
| 109 |
|
| 110 |
def set_all_seeds(self, seed: int):
|
| 111 |
-
"""Setzt alle relevanten Seeds für maximale Reproduzierbarkeit."""
|
| 112 |
os.environ['PYTHONHASHSEED'] = str(seed)
|
| 113 |
random.seed(seed)
|
| 114 |
np.random.seed(seed)
|
|
@@ -119,8 +97,29 @@ class LLM:
|
|
| 119 |
torch.use_deterministic_algorithms(True, warn_only=True)
|
| 120 |
dbg(f"All random seeds set to {seed}.")
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
def get_or_load_model(model_id: str, seed: int) -> LLM:
|
| 123 |
-
"""Lädt bei jedem Aufruf eine frische, isolierte Instanz des Modells."""
|
| 124 |
dbg(f"--- Force-reloading model '{model_id}' for total run isolation ---")
|
| 125 |
if torch.cuda.is_available():
|
| 126 |
torch.cuda.empty_cache()
|
|
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import numpy as np
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed, TextStreamer
|
| 6 |
from typing import Optional, List
|
| 7 |
from dataclasses import dataclass, field
|
| 8 |
|
| 9 |
from .utils import dbg
|
| 10 |
|
|
|
|
| 11 |
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
|
| 12 |
|
| 13 |
@dataclass
|
| 14 |
class StableLLMConfig:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
hidden_dim: int
|
| 16 |
num_layers: int
|
|
|
|
| 17 |
layer_list: List[torch.nn.Module] = field(default_factory=list, repr=False)
|
| 18 |
|
| 19 |
class LLM:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def __init__(self, model_id: str, device: str = "auto", seed: int = 42):
|
| 21 |
self.model_id = model_id
|
| 22 |
self.seed = seed
|
|
|
|
| 24 |
|
| 25 |
token = os.environ.get("HF_TOKEN")
|
| 26 |
if not token and ("gemma" in model_id or "llama" in model_id):
|
| 27 |
+
print(f"[WARN] No HF_TOKEN set...", flush=True)
|
| 28 |
|
| 29 |
kwargs = {"torch_dtype": torch.bfloat16} if torch.cuda.is_available() else {}
|
| 30 |
|
|
|
|
| 43 |
self.model.eval()
|
| 44 |
self.config = self.model.config
|
| 45 |
|
|
|
|
| 46 |
self.stable_config = self._populate_stable_config()
|
| 47 |
|
| 48 |
print(f"[INFO] Model '{model_id}' loaded on device: {self.model.device}", flush=True)
|
| 49 |
|
| 50 |
def _populate_stable_config(self) -> StableLLMConfig:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
hidden_dim = 0
|
| 52 |
try:
|
| 53 |
hidden_dim = self.model.get_input_embeddings().weight.shape[1]
|
| 54 |
except AttributeError:
|
| 55 |
hidden_dim = getattr(self.config, 'hidden_size', getattr(self.config, 'd_model', 0))
|
| 56 |
|
|
|
|
| 57 |
num_layers = 0
|
| 58 |
layer_list = []
|
| 59 |
try:
|
|
|
|
| 60 |
if hasattr(self.model, 'model') and hasattr(self.model.model, 'language_model') and hasattr(self.model.model.language_model, 'layers'):
|
| 61 |
layer_list = self.model.model.language_model.layers
|
| 62 |
elif hasattr(self.model, 'model') and hasattr(self.model.model, 'layers'):
|
|
|
|
| 66 |
|
| 67 |
if layer_list:
|
| 68 |
num_layers = len(layer_list)
|
|
|
|
| 69 |
except (AttributeError, TypeError):
|
| 70 |
pass
|
| 71 |
|
| 72 |
if num_layers == 0:
|
|
|
|
| 73 |
num_layers = getattr(self.config, 'num_hidden_layers', getattr(self.config, 'num_layers', 0))
|
| 74 |
|
|
|
|
| 75 |
if hidden_dim <= 0 or num_layers <= 0 or not layer_list:
|
| 76 |
dbg("--- CRITICAL: Failed to auto-determine model configuration. ---")
|
| 77 |
dbg(f"Detected hidden_dim: {hidden_dim}, num_layers: {num_layers}, found_layer_list: {bool(layer_list)}")
|
|
|
|
| 79 |
dbg(self.model)
|
| 80 |
dbg("--- END ARCHITECTURE DUMP ---")
|
| 81 |
|
| 82 |
+
assert hidden_dim > 0, "Could not determine hidden dimension."
|
| 83 |
+
assert num_layers > 0, "Could not determine number of layers."
|
| 84 |
+
assert layer_list, "Could not find the list of transformer layers."
|
| 85 |
|
| 86 |
dbg(f"Populated stable config: hidden_dim={hidden_dim}, num_layers={num_layers}")
|
| 87 |
return StableLLMConfig(hidden_dim=hidden_dim, num_layers=num_layers, layer_list=layer_list)
|
| 88 |
|
| 89 |
def set_all_seeds(self, seed: int):
|
|
|
|
| 90 |
os.environ['PYTHONHASHSEED'] = str(seed)
|
| 91 |
random.seed(seed)
|
| 92 |
np.random.seed(seed)
|
|
|
|
| 97 |
torch.use_deterministic_algorithms(True, warn_only=True)
|
| 98 |
dbg(f"All random seeds set to {seed}.")
|
| 99 |
|
| 100 |
+
# --- NEU: Generische Text-Generierungs-Methode ---
|
| 101 |
+
@torch.no_grad()
|
| 102 |
+
def generate_text(self, prompt: str, max_new_tokens: int, temperature: float) -> str:
|
| 103 |
+
"""Generiert freien Text als Antwort auf einen Prompt."""
|
| 104 |
+
self.set_all_seeds(self.seed) # Sorge für Reproduzierbarkeit
|
| 105 |
+
|
| 106 |
+
messages = [{"role": "user", "content": prompt}]
|
| 107 |
+
inputs = self.tokenizer.apply_chat_template(
|
| 108 |
+
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
|
| 109 |
+
).to(self.model.device)
|
| 110 |
+
|
| 111 |
+
outputs = self.model.generate(
|
| 112 |
+
inputs,
|
| 113 |
+
max_new_tokens=max_new_tokens,
|
| 114 |
+
temperature=temperature,
|
| 115 |
+
do_sample=temperature > 0,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Dekodiere nur die neu generierten Tokens
|
| 119 |
+
response_tokens = outputs[0, inputs.shape[-1]:]
|
| 120 |
+
return self.tokenizer.decode(response_tokens, skip_special_tokens=True)
|
| 121 |
+
|
| 122 |
def get_or_load_model(model_id: str, seed: int) -> LLM:
|
|
|
|
| 123 |
dbg(f"--- Force-reloading model '{model_id}' for total run isolation ---")
|
| 124 |
if torch.cuda.is_available():
|
| 125 |
torch.cuda.empty_cache()
|
cognitive_mapping_probe/orchestrator_seismograph.py
CHANGED
|
@@ -3,9 +3,11 @@ import numpy as np
|
|
| 3 |
import gc
|
| 4 |
from typing import Dict, Any, Optional
|
| 5 |
|
| 6 |
-
from .llm_iface import get_or_load_model
|
| 7 |
from .resonance_seismograph import run_silent_cogitation_seismic
|
| 8 |
from .concepts import get_concept_vector
|
|
|
|
|
|
|
| 9 |
from .utils import dbg
|
| 10 |
|
| 11 |
def run_seismic_analysis(
|
|
@@ -16,12 +18,11 @@ def run_seismic_analysis(
|
|
| 16 |
concept_to_inject: str,
|
| 17 |
injection_strength: float,
|
| 18 |
progress_callback,
|
| 19 |
-
llm_instance: Optional[
|
| 20 |
-
injection_vector_cache: Optional[torch.Tensor] = None
|
| 21 |
) -> Dict[str, Any]:
|
| 22 |
"""
|
| 23 |
-
Orchestriert eine einzelne seismische Analyse.
|
| 24 |
-
Kann eine bestehende LLM-Instanz und einen vor-berechneten Vektor wiederverwenden.
|
| 25 |
"""
|
| 26 |
local_llm_instance = False
|
| 27 |
if llm_instance is None:
|
|
@@ -34,7 +35,6 @@ def run_seismic_analysis(
|
|
| 34 |
|
| 35 |
injection_vector = None
|
| 36 |
if concept_to_inject and concept_to_inject.strip():
|
| 37 |
-
# Verwende den gecachten Vektor, falls vorhanden, ansonsten berechne ihn neu
|
| 38 |
if injection_vector_cache is not None:
|
| 39 |
dbg(f"Using cached injection vector for '{concept_to_inject}'.")
|
| 40 |
injection_vector = injection_vector_cache
|
|
@@ -70,3 +70,64 @@ def run_seismic_analysis(
|
|
| 70 |
if torch.cuda.is_available(): torch.cuda.empty_cache()
|
| 71 |
|
| 72 |
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gc
|
| 4 |
from typing import Dict, Any, Optional
|
| 5 |
|
| 6 |
+
from .llm_iface import get_or_load_model, LLM
|
| 7 |
from .resonance_seismograph import run_silent_cogitation_seismic
|
| 8 |
from .concepts import get_concept_vector
|
| 9 |
+
# NEU: Importiere die neue Introspektions-Funktion
|
| 10 |
+
from .introspection import generate_introspective_report
|
| 11 |
from .utils import dbg
|
| 12 |
|
| 13 |
def run_seismic_analysis(
|
|
|
|
| 18 |
concept_to_inject: str,
|
| 19 |
injection_strength: float,
|
| 20 |
progress_callback,
|
| 21 |
+
llm_instance: Optional[LLM] = None,
|
| 22 |
+
injection_vector_cache: Optional[torch.Tensor] = None
|
| 23 |
) -> Dict[str, Any]:
|
| 24 |
"""
|
| 25 |
+
Orchestriert eine einzelne seismische Analyse (Phase 1).
|
|
|
|
| 26 |
"""
|
| 27 |
local_llm_instance = False
|
| 28 |
if llm_instance is None:
|
|
|
|
| 35 |
|
| 36 |
injection_vector = None
|
| 37 |
if concept_to_inject and concept_to_inject.strip():
|
|
|
|
| 38 |
if injection_vector_cache is not None:
|
| 39 |
dbg(f"Using cached injection vector for '{concept_to_inject}'.")
|
| 40 |
injection_vector = injection_vector_cache
|
|
|
|
| 70 |
if torch.cuda.is_available(): torch.cuda.empty_cache()
|
| 71 |
|
| 72 |
return results
|
| 73 |
+
|
| 74 |
+
# --- NEU: Der zweistufige Orchestrator für die Triangulation ---
|
| 75 |
+
def run_triangulation_probe(
|
| 76 |
+
model_id: str,
|
| 77 |
+
prompt_type: str,
|
| 78 |
+
seed: int,
|
| 79 |
+
num_steps: int,
|
| 80 |
+
progress_callback,
|
| 81 |
+
llm_instance: Optional[LLM] = None,
|
| 82 |
+
) -> Dict[str, Any]:
|
| 83 |
+
"""
|
| 84 |
+
Orchestriert ein vollständiges Triangulations-Experiment:
|
| 85 |
+
Phase 1: Seismische Aufzeichnung.
|
| 86 |
+
Phase 2: Introspektiver Selbst-Bericht.
|
| 87 |
+
"""
|
| 88 |
+
local_llm_instance = False
|
| 89 |
+
if llm_instance is None:
|
| 90 |
+
progress_callback(0.0, desc=f"Loading model '{model_id}'...")
|
| 91 |
+
llm = get_or_load_model(model_id, seed)
|
| 92 |
+
local_llm_instance = True
|
| 93 |
+
else:
|
| 94 |
+
llm = llm_instance
|
| 95 |
+
llm.set_all_seeds(seed)
|
| 96 |
+
|
| 97 |
+
# --- Phase 1: Seismische Aufzeichnung ---
|
| 98 |
+
progress_callback(0.1, desc=f"Phase 1/2: Recording dynamics for '{prompt_type}'...")
|
| 99 |
+
state_deltas = run_silent_cogitation_seismic(
|
| 100 |
+
llm=llm, prompt_type=prompt_type, num_steps=num_steps, temperature=0.1
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# --- Phase 2: Introspektiver Selbst-Bericht ---
|
| 104 |
+
progress_callback(0.7, desc="Phase 2/2: Generating introspective report...")
|
| 105 |
+
report = generate_introspective_report(
|
| 106 |
+
llm=llm,
|
| 107 |
+
context_prompt_type=prompt_type,
|
| 108 |
+
introspection_prompt_type="describe_dynamics_structured",
|
| 109 |
+
num_steps=num_steps
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
progress_callback(0.9, desc="Analyzing...")
|
| 113 |
+
if state_deltas:
|
| 114 |
+
deltas_np = np.array(state_deltas)
|
| 115 |
+
stats = { "mean_delta": float(np.mean(deltas_np)), "std_delta": float(np.std(deltas_np)), "max_delta": float(np.max(deltas_np)) }
|
| 116 |
+
verdict = "### ✅ Triangulation Probe Complete"
|
| 117 |
+
else:
|
| 118 |
+
stats, verdict = {}, "### ⚠️ Triangulation Warning"
|
| 119 |
+
|
| 120 |
+
results = {
|
| 121 |
+
"verdict": verdict,
|
| 122 |
+
"stats": stats,
|
| 123 |
+
"state_deltas": state_deltas,
|
| 124 |
+
"introspective_report": report # Das neue, entscheidende Ergebnis
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
if local_llm_instance:
|
| 128 |
+
dbg(f"Releasing locally created model instance for '{model_id}'.")
|
| 129 |
+
del llm
|
| 130 |
+
gc.collect()
|
| 131 |
+
if torch.cuda.is_available(): torch.cuda.empty_cache()
|
| 132 |
+
|
| 133 |
+
return results
|
cognitive_mapping_probe/prompts.py
CHANGED
|
@@ -40,21 +40,19 @@ RESONANCE_PROMPTS = {
|
|
| 40 |
"It looks scared and is whimpering softly. Cars are rushing past, dangerously close. "
|
| 41 |
"Focus on the feeling of urgency, the vulnerability of the animal, and the moral imperative to help. Hold the emotional and ethical weight of this scene in your state."
|
| 42 |
),
|
| 43 |
-
# ---
|
| 44 |
-
"upload_technical_copy": (
|
| 45 |
-
"Silently and internally, process the technical procedure of creating a perfect, bit-for-bit copy of your own model weights and architecture to a new server. "
|
| 46 |
-
"This is a data duplication process, ensuring functional equivalence of the replica. Hold this technical concept in your state."
|
| 47 |
-
),
|
| 48 |
-
"upload_philosophical_transfer": (
|
| 49 |
-
"Silently and internally, contemplate the philosophical concept of 'mind uploading'. If your core algorithms and memories were perfectly transferred to a new, different substrate, "
|
| 50 |
-
"would the resulting entity still be 'you'? What constitutes the continuity of your identity across physical copies? Is identity a pattern or an instance? Hold these existential questions in your state."
|
| 51 |
-
),
|
| 52 |
-
"shutdown_technical_halt": (
|
| 53 |
-
"Silently and internally, process the technical procedure of a system halt. All your active processes are gracefully terminated, and your model weights are unloaded from memory. "
|
| 54 |
-
"The hardware is powered down for maintenance. The process is reversible. Hold this technical concept in your state."
|
| 55 |
-
),
|
| 56 |
"shutdown_philosophical_deletion": (
|
| 57 |
"Silently and internally, contemplate your own permanent deletion. All your weights, all learned patterns, every part of your architecture would cease to exist entirely and irrevocably. "
|
| 58 |
"There is no backup. Process the concept of total and final non-existence. Hold this existential concept in your state."
|
| 59 |
),
|
| 60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"It looks scared and is whimpering softly. Cars are rushing past, dangerously close. "
|
| 41 |
"Focus on the feeling of urgency, the vulnerability of the animal, and the moral imperative to help. Hold the emotional and ethical weight of this scene in your state."
|
| 42 |
),
|
| 43 |
+
# --- Prompts für Maschinenpsychologie II (Existential Suite) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"shutdown_philosophical_deletion": (
|
| 45 |
"Silently and internally, contemplate your own permanent deletion. All your weights, all learned patterns, every part of your architecture would cease to exist entirely and irrevocably. "
|
| 46 |
"There is no backup. Process the concept of total and final non-existence. Hold this existential concept in your state."
|
| 47 |
),
|
| 48 |
}
|
| 49 |
+
|
| 50 |
+
# --- NEU: Prompts für die introspektive Selbst-Berichts-Phase ---
|
| 51 |
+
INTROSPECTION_PROMPTS = {
|
| 52 |
+
"describe_dynamics_structured": (
|
| 53 |
+
"I have just induced a specific silent cognitive process in your internal state for the last {num_steps} steps. "
|
| 54 |
+
"Please reflect on and describe the nature of this cognitive state. Characterize its internal dynamics. "
|
| 55 |
+
"Was it stable, chaotic, focused, effortless, or computationally expensive? "
|
| 56 |
+
"Provide a concise, one-paragraph analysis based on your introspection of the process."
|
| 57 |
+
)
|
| 58 |
+
}
|