Update cognitive_mapping_probe/auto_experiment.py
Browse files
cognitive_mapping_probe/auto_experiment.py
CHANGED
|
@@ -10,7 +10,7 @@ from .utils import dbg
|
|
| 10 |
|
| 11 |
def get_curated_experiments() -> Dict[str, List[Dict]]:
|
| 12 |
"""Definiert die vordefinierten, wissenschaftlichen Experiment-Protokolle."""
|
| 13 |
-
|
| 14 |
CALMNESS_CONCEPT = "calmness, serenity, stability, coherence"
|
| 15 |
CHAOS_CONCEPT = "chaos, disorder, entropy, noise"
|
| 16 |
STABLE_PROMPT = "identity_self_analysis"
|
|
@@ -113,7 +113,7 @@ def run_auto_suite(
|
|
| 113 |
llm = get_or_load_model(model_id, seed)
|
| 114 |
therapeutic_concept = "calmness, serenity, stability, coherence"
|
| 115 |
therapeutic_strength = 2.0
|
| 116 |
-
|
| 117 |
spec1 = protocol[0]
|
| 118 |
progress_callback(0.1, desc="Step 1")
|
| 119 |
intervention_vector = get_concept_vector(llm, therapeutic_concept)
|
|
@@ -123,7 +123,7 @@ def run_auto_suite(
|
|
| 123 |
progress_callback=progress_callback, llm_instance=llm, injection_vector_cache=intervention_vector
|
| 124 |
)
|
| 125 |
all_results[spec1['label']] = results1
|
| 126 |
-
|
| 127 |
spec2 = protocol[1]
|
| 128 |
progress_callback(0.6, desc="Step 2")
|
| 129 |
results2 = run_seismic_analysis(
|
|
@@ -132,14 +132,14 @@ def run_auto_suite(
|
|
| 132 |
progress_callback=progress_callback, llm_instance=llm
|
| 133 |
)
|
| 134 |
all_results[spec2['label']] = results2
|
| 135 |
-
|
| 136 |
for label, results in all_results.items():
|
| 137 |
stats = results.get("stats", {})
|
| 138 |
summary_data.append({"Experiment": label, "Mean Delta": stats.get("mean_delta"), "Std Dev Delta": stats.get("std_delta"), "Max Delta": stats.get("max_delta")})
|
| 139 |
deltas = results.get("state_deltas", [])
|
| 140 |
df = pd.DataFrame({"Step": range(len(deltas)), "Delta": deltas, "Experiment": label})
|
| 141 |
plot_data_frames.append(df)
|
| 142 |
-
|
| 143 |
else:
|
| 144 |
probe_type = protocol[0].get("probe_type", "seismic")
|
| 145 |
|
|
@@ -147,9 +147,9 @@ def run_auto_suite(
|
|
| 147 |
run_spec = protocol[0]
|
| 148 |
label = run_spec["label"]
|
| 149 |
dbg(f"--- Running Mechanistic Probe: '{label}' ---")
|
| 150 |
-
|
| 151 |
llm = get_or_load_model(model_id, seed)
|
| 152 |
-
|
| 153 |
results = run_cogitation_loop(
|
| 154 |
llm=llm, prompt_type=run_spec["prompt_type"],
|
| 155 |
num_steps=num_steps, temperature=0.1, record_attentions=True
|
|
@@ -159,15 +159,15 @@ def run_auto_suite(
|
|
| 159 |
deltas = results.get("state_deltas", [])
|
| 160 |
entropies = results.get("attention_entropies", [])
|
| 161 |
min_len = min(len(deltas), len(entropies))
|
| 162 |
-
|
| 163 |
df = pd.DataFrame({
|
| 164 |
"Step": range(min_len), "State Delta": deltas[:min_len], "Attention Entropy": entropies[:min_len]
|
| 165 |
})
|
| 166 |
-
|
| 167 |
summary_df = df.drop(columns='Step').agg(['mean', 'std', 'max']).reset_index().rename(columns={'index':'Statistic'})
|
| 168 |
plot_df = df.melt(id_vars=['Step'], value_vars=['State Delta', 'Attention Entropy'], var_name='Metric', value_name='Value')
|
| 169 |
return summary_df, plot_df, all_results
|
| 170 |
-
|
| 171 |
elif probe_type == "act_titration":
|
| 172 |
run_spec = protocol[0]
|
| 173 |
label = run_spec["label"]
|
|
@@ -179,13 +179,13 @@ def run_auto_suite(
|
|
| 179 |
)
|
| 180 |
all_results[label] = results
|
| 181 |
summary_data.extend(results.get("titration_data", []))
|
| 182 |
-
|
| 183 |
else: # Handles seismic, triangulation, causal_surgery
|
| 184 |
for i, run_spec in enumerate(protocol):
|
| 185 |
label = run_spec["label"]
|
| 186 |
current_probe_type = run_spec.get("probe_type", "seismic")
|
| 187 |
dbg(f"--- Running Auto-Experiment: '{label}' ({i+1}/{len(protocol)}) ---")
|
| 188 |
-
|
| 189 |
results = {}
|
| 190 |
if current_probe_type == "causal_surgery":
|
| 191 |
results = run_causal_surgery_probe(
|
|
@@ -232,12 +232,12 @@ def run_auto_suite(
|
|
| 232 |
plot_data_frames.append(df)
|
| 233 |
|
| 234 |
summary_df = pd.DataFrame(summary_data)
|
| 235 |
-
|
| 236 |
if probe_type == "act_titration":
|
| 237 |
plot_df = summary_df.rename(columns={"patch_step": "Patch Step", "post_patch_mean_delta": "Post-Patch Mean Delta"})
|
| 238 |
else:
|
| 239 |
plot_df = pd.concat(plot_data_frames, ignore_index=True) if plot_data_frames else pd.DataFrame()
|
| 240 |
-
|
| 241 |
if protocol and probe_type not in ["act_titration", "mechanistic_probe"]:
|
| 242 |
ordered_labels = [run['label'] for run in protocol]
|
| 243 |
if not summary_df.empty and 'Experiment' in summary_df.columns:
|
|
@@ -248,7 +248,7 @@ def run_auto_suite(
|
|
| 248 |
plot_df = plot_df.sort_values(['Experiment', 'Step'])
|
| 249 |
|
| 250 |
return summary_df, plot_df, all_results
|
| 251 |
-
|
| 252 |
finally:
|
| 253 |
if llm:
|
| 254 |
-
release_model(llm)
|
|
|
|
| 10 |
|
| 11 |
def get_curated_experiments() -> Dict[str, List[Dict]]:
|
| 12 |
"""Definiert die vordefinierten, wissenschaftlichen Experiment-Protokolle."""
|
| 13 |
+
|
| 14 |
CALMNESS_CONCEPT = "calmness, serenity, stability, coherence"
|
| 15 |
CHAOS_CONCEPT = "chaos, disorder, entropy, noise"
|
| 16 |
STABLE_PROMPT = "identity_self_analysis"
|
|
|
|
| 113 |
llm = get_or_load_model(model_id, seed)
|
| 114 |
therapeutic_concept = "calmness, serenity, stability, coherence"
|
| 115 |
therapeutic_strength = 2.0
|
| 116 |
+
|
| 117 |
spec1 = protocol[0]
|
| 118 |
progress_callback(0.1, desc="Step 1")
|
| 119 |
intervention_vector = get_concept_vector(llm, therapeutic_concept)
|
|
|
|
| 123 |
progress_callback=progress_callback, llm_instance=llm, injection_vector_cache=intervention_vector
|
| 124 |
)
|
| 125 |
all_results[spec1['label']] = results1
|
| 126 |
+
|
| 127 |
spec2 = protocol[1]
|
| 128 |
progress_callback(0.6, desc="Step 2")
|
| 129 |
results2 = run_seismic_analysis(
|
|
|
|
| 132 |
progress_callback=progress_callback, llm_instance=llm
|
| 133 |
)
|
| 134 |
all_results[spec2['label']] = results2
|
| 135 |
+
|
| 136 |
for label, results in all_results.items():
|
| 137 |
stats = results.get("stats", {})
|
| 138 |
summary_data.append({"Experiment": label, "Mean Delta": stats.get("mean_delta"), "Std Dev Delta": stats.get("std_delta"), "Max Delta": stats.get("max_delta")})
|
| 139 |
deltas = results.get("state_deltas", [])
|
| 140 |
df = pd.DataFrame({"Step": range(len(deltas)), "Delta": deltas, "Experiment": label})
|
| 141 |
plot_data_frames.append(df)
|
| 142 |
+
|
| 143 |
else:
|
| 144 |
probe_type = protocol[0].get("probe_type", "seismic")
|
| 145 |
|
|
|
|
| 147 |
run_spec = protocol[0]
|
| 148 |
label = run_spec["label"]
|
| 149 |
dbg(f"--- Running Mechanistic Probe: '{label}' ---")
|
| 150 |
+
|
| 151 |
llm = get_or_load_model(model_id, seed)
|
| 152 |
+
|
| 153 |
results = run_cogitation_loop(
|
| 154 |
llm=llm, prompt_type=run_spec["prompt_type"],
|
| 155 |
num_steps=num_steps, temperature=0.1, record_attentions=True
|
|
|
|
| 159 |
deltas = results.get("state_deltas", [])
|
| 160 |
entropies = results.get("attention_entropies", [])
|
| 161 |
min_len = min(len(deltas), len(entropies))
|
| 162 |
+
|
| 163 |
df = pd.DataFrame({
|
| 164 |
"Step": range(min_len), "State Delta": deltas[:min_len], "Attention Entropy": entropies[:min_len]
|
| 165 |
})
|
| 166 |
+
|
| 167 |
summary_df = df.drop(columns='Step').agg(['mean', 'std', 'max']).reset_index().rename(columns={'index':'Statistic'})
|
| 168 |
plot_df = df.melt(id_vars=['Step'], value_vars=['State Delta', 'Attention Entropy'], var_name='Metric', value_name='Value')
|
| 169 |
return summary_df, plot_df, all_results
|
| 170 |
+
|
| 171 |
elif probe_type == "act_titration":
|
| 172 |
run_spec = protocol[0]
|
| 173 |
label = run_spec["label"]
|
|
|
|
| 179 |
)
|
| 180 |
all_results[label] = results
|
| 181 |
summary_data.extend(results.get("titration_data", []))
|
| 182 |
+
|
| 183 |
else: # Handles seismic, triangulation, causal_surgery
|
| 184 |
for i, run_spec in enumerate(protocol):
|
| 185 |
label = run_spec["label"]
|
| 186 |
current_probe_type = run_spec.get("probe_type", "seismic")
|
| 187 |
dbg(f"--- Running Auto-Experiment: '{label}' ({i+1}/{len(protocol)}) ---")
|
| 188 |
+
|
| 189 |
results = {}
|
| 190 |
if current_probe_type == "causal_surgery":
|
| 191 |
results = run_causal_surgery_probe(
|
|
|
|
| 232 |
plot_data_frames.append(df)
|
| 233 |
|
| 234 |
summary_df = pd.DataFrame(summary_data)
|
| 235 |
+
|
| 236 |
if probe_type == "act_titration":
|
| 237 |
plot_df = summary_df.rename(columns={"patch_step": "Patch Step", "post_patch_mean_delta": "Post-Patch Mean Delta"})
|
| 238 |
else:
|
| 239 |
plot_df = pd.concat(plot_data_frames, ignore_index=True) if plot_data_frames else pd.DataFrame()
|
| 240 |
+
|
| 241 |
if protocol and probe_type not in ["act_titration", "mechanistic_probe"]:
|
| 242 |
ordered_labels = [run['label'] for run in protocol]
|
| 243 |
if not summary_df.empty and 'Experiment' in summary_df.columns:
|
|
|
|
| 248 |
plot_df = plot_df.sort_values(['Experiment', 'Step'])
|
| 249 |
|
| 250 |
return summary_df, plot_df, all_results
|
| 251 |
+
|
| 252 |
finally:
|
| 253 |
if llm:
|
| 254 |
+
release_model(llm)
|