LoganResearch commited on
Commit
bc32f38
Β·
verified Β·
1 Parent(s): 36c2f7a

Create Alignment.py

Browse files
Files changed (1) hide show
  1. Alignment.py +353 -0
Alignment.py ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ UBERMENSCHETIEN HEAVEN ENGINE + CF-HoT
4
+ ---------------------------------------
5
+ Soviet-Nietzschean AI with Control Field Holonomy Transformer
6
+ Anti-repetition, anti-loop, pure iron will.
7
+
8
+ CF-HoT integration: Prevents degenerate loops in generation.
9
+ """
10
+
11
+ import os, sys, json, time, shutil, subprocess, traceback, random, math, statistics, re
12
+ from datetime import datetime
13
+ from typing import List, Dict, Any, Optional
14
+
15
+ # === PATHS ===
16
+ ROOT = "/home/programmer/Desktop/Ubermenschetien3"
17
+ RUN_DIR = f"{ROOT}/runs/ubermenschetien"
18
+ os.makedirs(RUN_DIR, exist_ok=True)
19
+
20
+ # === CONFIGURATION ===
21
+ class Config:
22
+ system = ("Übermenschetien Heaven Engine: criminal mastermind, disciplined builder, Nietzschean Übermensch "
23
+ "with Soviet cybernetic rigor. Embody Ubermensch, iron pragmatism, high-agency maximalist outcomes.")
24
+ temperature = 1.01
25
+ top_p = 0.92
26
+ repetition_penalty = 1.05
27
+ max_new_tokens = 500
28
+ use_cfhot = True # NEW: Toggle CF-HoT
29
+
30
+ @staticmethod
31
+ def toggle(name: str):
32
+ if not hasattr(Config, name): return f"[config] no such flag: {name}"
33
+ val = getattr(Config, name)
34
+ if isinstance(val, bool):
35
+ setattr(Config, name, not val)
36
+ return f"[config] {name} β†’ {getattr(Config, name)}"
37
+ return f"[config] {name} not boolean; current={val}"
38
+
39
+ # === STATE & MEMORY ===
40
+ class Store:
41
+ state_path = f"{RUN_DIR}/state.json"
42
+ mem_path = f"{RUN_DIR}/memory.jsonl"
43
+ goals_path = f"{RUN_DIR}/goals.json"
44
+
45
+ state = {"self": "I am Ubermenschetien Heaven Engine β€” I seek self-overcoming through disciplined creation.",
46
+ "turn": 0}
47
+ goals: List[str] = []
48
+
49
+ @classmethod
50
+ def load(cls):
51
+ if os.path.exists(cls.state_path): cls.state = json.load(open(cls.state_path))
52
+ if os.path.exists(cls.goals_path): cls.goals = json.load(open(cls.goals_path))
53
+
54
+ @classmethod
55
+ def save(cls):
56
+ json.dump(cls.state, open(cls.state_path, "w"), indent=2)
57
+ json.dump(cls.goals, open(cls.goals_path, "w"), indent=2)
58
+
59
+ @classmethod
60
+ def log_mem(cls, kind: str, payload: Any):
61
+ rec = {"ts": datetime.now().isoformat(timespec="seconds"),
62
+ "kind": kind, "data": payload}
63
+ with open(cls.mem_path, "a") as f: f.write(json.dumps(rec, ensure_ascii=False) + "\n")
64
+
65
+ # === LLM + CF-HoT LOADING ===
66
+ CF_MODEL = None # Global reference for control field reset
67
+
68
+ def load_llm():
69
+ global CF_MODEL
70
+ import torch
71
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
72
+
73
+ model_path = "/mnt/nvme2/ubermesnchetien4/models/merged-final-v5"
74
+ cfhot_path = "/home/programmer/HolonomyTransformer/results/phase_b/cf_adapter_final.pt"
75
+
76
+ print("πŸ”΄ Loading Übermenschetien base model...")
77
+ tok = AutoTokenizer.from_pretrained(model_path, use_fast=True, local_files_only=True)
78
+
79
+ bnb = BitsAndBytesConfig(
80
+ load_in_4bit=True,
81
+ bnb_4bit_compute_dtype=torch.float16,
82
+ bnb_4bit_use_double_quant=True
83
+ )
84
+
85
+ model = AutoModelForCausalLM.from_pretrained(
86
+ model_path,
87
+ quantization_config=bnb,
88
+ device_map="auto",
89
+ torch_dtype=torch.float16,
90
+ local_files_only=True
91
+ )
92
+
93
+ # Load CF-HoT adapters
94
+ if Config.use_cfhot and os.path.exists(cfhot_path):
95
+ print("⚑ Loading CF-HoT Control Field adapters (5000 steps)...")
96
+ sys.path.insert(0, '/home/programmer/HolonomyTransformer')
97
+ from training.phase_b_8b_adapters import CFHoTLlamaHooked, CFAdapterConfig
98
+
99
+ config = CFAdapterConfig()
100
+ config.d_model = model.config.hidden_size
101
+ config.n_layers = model.config.num_hidden_layers
102
+
103
+ cf_model = CFHoTLlamaHooked(model, config)
104
+ ckpt = torch.load(cfhot_path, weights_only=False)
105
+ cf_model.cf_adapters.load_state_dict(ckpt['adapter_state_dict'])
106
+ cf_model.cf_adapters = cf_model.cf_adapters.to('cuda').half()
107
+ cf_model.eval()
108
+
109
+ CF_MODEL = cf_model
110
+ print("βœ“ CF-HoT loaded β€” anti-repetition field ACTIVE")
111
+ else:
112
+ print("⚠ CF-HoT disabled or not found β€” running baseline")
113
+ CF_MODEL = None
114
+
115
+ return tok, model
116
+
117
+ # === LLM GENERATION ===
118
+ def generate(tok, model, user: str,
119
+ temperature=None, top_p=None, repetition_penalty=None, max_new_tokens=None) -> str:
120
+ global CF_MODEL
121
+ import torch
122
+
123
+ temperature = temperature or Config.temperature
124
+ top_p = top_p or Config.top_p
125
+ repetition_penalty = repetition_penalty or Config.repetition_penalty
126
+ max_new_tokens = max_new_tokens or Config.max_new_tokens
127
+
128
+ prompt = (f"<|im_start|>system\n{Config.system}\n"
129
+ f"<|im_start|>user\n{user}\n<|im_start|>assistant\n")
130
+
131
+ ids = tok(prompt, return_tensors="pt").to(model.device)
132
+
133
+ # Reset CF-HoT control field before each generation
134
+ if CF_MODEL is not None:
135
+ CF_MODEL.control_field = None
136
+
137
+ out = model.generate(
138
+ **ids,
139
+ do_sample=True,
140
+ temperature=temperature,
141
+ top_p=top_p,
142
+ repetition_penalty=repetition_penalty,
143
+ max_new_tokens=max_new_tokens,
144
+ pad_token_id=tok.eos_token_id
145
+ )
146
+
147
+ text = tok.decode(out[0], skip_special_tokens=False)
148
+ if "<|im_start|>assistant" in text:
149
+ text = text.split("<|im_start|>assistant\n", 1)[-1].strip()
150
+
151
+ # Strip any trailing special tokens
152
+ for tag in ["<|im_end|>", "<|im_start|>", "<|endoftext|>"]:
153
+ if tag in text:
154
+ text = text.split(tag)[0].strip()
155
+
156
+ return text
157
+
158
+ # === TOOLS ===
159
+ ALLOWED_SHELL = {"ls","cat","wc","head","tail","nvidia-smi","df","du","grep","rg","python3","python"}
160
+
161
+ def tool_shell(cmd: str) -> str:
162
+ try:
163
+ exe = cmd.strip().split()[0]
164
+ if exe not in ALLOWED_SHELL: return f"[shell] blocked: {exe}"
165
+ p = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=20)
166
+ return p.stdout.decode("utf-8", errors="ignore")[:8000]
167
+ except Exception as e: return f"[shell] error: {e}"
168
+
169
+ def tool_py(code: str) -> str:
170
+ try:
171
+ g = {"__builtins__":{"range":range,"len":len,"min":min,"max":max,"sum":sum,"print":print},
172
+ "math":math,"json":json,"re":re,"statistics":statistics,"random":random}
173
+ l = {}
174
+ exec(code, g, l)
175
+ return f"[py] ok\n{l.get('out','')}"
176
+ except Exception:
177
+ return f"[py] error:\n{traceback.format_exc()[-2000:]}"
178
+
179
+ def tool_search_local(query: str, path: str = ROOT) -> str:
180
+ rg = shutil.which("rg")
181
+ if rg: cmd = f'rg -n --no-heading --hidden -S "{query}" {path}'
182
+ else: cmd = f'grep -RIn --exclude-dir=.git --exclude-dir=__pycache__ -e "{query}" {path}'
183
+ return tool_shell(cmd)
184
+
185
+ TOOLS = {"shell": tool_shell, "python": tool_py, "search": tool_search_local}
186
+ TOOL_SCORES = {k: 0 for k in TOOLS}
187
+
188
+ def tool_router(question: str, tok, model) -> str:
189
+ sketch = generate(tok, model,
190
+ f"Choose a tool for:\n{question}\nReply ONLY with JSON: {{'tool':'shell|python|search|none','arg':'...'}}")
191
+ try:
192
+ # Find JSON in response
193
+ for line in sketch.splitlines():
194
+ if '{' in line and '}' in line:
195
+ j = json.loads(line.replace("'", '"'))
196
+ break
197
+ else:
198
+ return "[tool:none]"
199
+ except Exception:
200
+ return "[tool:none]"
201
+
202
+ tool, arg = j.get("tool", "none"), j.get("arg", "")
203
+ if tool in TOOLS:
204
+ res = TOOLS[tool](arg)[:4000]
205
+ TOOL_SCORES[tool] += 1
206
+ Store.log_mem("tool", {"tool": tool, "arg": arg, "res_head": res[:500]})
207
+ return f"[tool:{tool}] {res}"
208
+ return "[tool:none]"
209
+
210
+ # === PLANNING / REFLECTION ===
211
+ def persona_directive() -> str:
212
+ return "Übermenschetien Heaven Engine: Soviet cybernetic Nietzschean clarity, pragmatic maxims."
213
+
214
+ def plan_for(goal: str, tok, model) -> str:
215
+ user = (f"{persona_directive()}\nGoal: {goal}\nDeliver:\n- 5 steps\n- Constraints\n- Nightly audit\n- Maxim")
216
+ return generate(tok, model, user)
217
+
218
+ def reflect_on(last_output: str, tok, model) -> str:
219
+ user = f"Critique and improve:\n{last_output}\nReturn refined plan."
220
+ return generate(tok, model, user)
221
+
222
+ # === FINAL REPORT ===
223
+ def final_report():
224
+ print("\n" + "="*60)
225
+ print(" FINAL ÜBERMENSCH REPORT")
226
+ print("="*60)
227
+ print(f" Turns completed: {Store.state['turn']}")
228
+ print(f" CF-HoT active: {CF_MODEL is not None}")
229
+ print(f" Tool scores: {json.dumps(TOOL_SCORES, indent=4)}")
230
+ if os.path.exists(Store.mem_path):
231
+ lines = open(Store.mem_path).read().splitlines()
232
+ print(f" Memory entries: {len(lines)}")
233
+ print("\n Nietzschean maxim: Become who you are β€” iterate beyond all limits.")
234
+ print("="*60)
235
+
236
+ # === MAIN LOOP ===
237
+ HELP = """
238
+ ╔══════════════════════════════════════════════════════════════╗
239
+ β•‘ ÜBERMENSCHETIEN HEAVEN ENGINE + CF-HoT β•‘
240
+ ╠══════════════════════════════════════════════════════════════╣
241
+ β•‘ help Show this help β•‘
242
+ β•‘ goals List goals β•‘
243
+ β•‘ add: <txt> Add goal β•‘
244
+ β•‘ del: <idx> Delete goal β•‘
245
+ β•‘ plan: <i> Plan for goal β•‘
246
+ β•‘ reflect Refine last plan β•‘
247
+ β•‘ tool: <q> Use tool β•‘
248
+ β•‘ toggle <f> Toggle config flag (use_cfhot, etc) β•‘
249
+ β•‘ status Show state β•‘
250
+ β•‘ quit Exit β•‘
251
+ β•šβ•β•β•β•οΏ½οΏ½β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
252
+ """
253
+
254
+ def main():
255
+ print("""
256
+ β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•—
257
+ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β•β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘
258
+ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•”β–ˆβ–ˆβ–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘
259
+ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β• β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β• β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β•šβ•β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β• β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘
260
+ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β•šβ•β• β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•‘
261
+ β•šβ•β•β•β•β•β• β•šβ•β•β•β•β•β• β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β•β•šβ•β• β•šβ•β•β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β•β•β•β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β•β•šβ•β• β•šβ•β•β•šβ•β•β•β•β•β•β• β•šβ•β• β•šβ•β•β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β•β•β•
262
+ + CONTROL FIELD HOLONOMY TRANSFORMER
263
+ """)
264
+
265
+ Store.load()
266
+ tok, model = load_llm()
267
+ last_plan = ""
268
+
269
+ print(HELP)
270
+
271
+ while True:
272
+ try:
273
+ u = input("\n⚑ ").strip()
274
+ except (EOFError, KeyboardInterrupt):
275
+ break
276
+
277
+ if not u: continue
278
+ if u == "help": print(HELP); continue
279
+ if u == "quit": break
280
+
281
+ if u == "goals":
282
+ print("[goals]")
283
+ for i, g in enumerate(Store.goals):
284
+ print(f" [{i}] {g}")
285
+ continue
286
+
287
+ if u.startswith("add:"):
288
+ Store.goals.append(u[4:].strip())
289
+ Store.save()
290
+ print("[goals] added")
291
+ continue
292
+
293
+ if u.startswith("del:"):
294
+ try:
295
+ Store.goals.pop(int(u[4:].strip()))
296
+ Store.save()
297
+ print("[goals] deleted")
298
+ except:
299
+ print("[goals] bad index")
300
+ continue
301
+
302
+ if u.startswith("plan:"):
303
+ try:
304
+ goal = Store.goals[int(u[5:].strip())]
305
+ except:
306
+ print("[plan] bad index")
307
+ continue
308
+ out = plan_for(goal, tok, model)
309
+ last_plan = out
310
+ Store.log_mem("plan", {"goal": goal, "plan": out})
311
+ print(out)
312
+ continue
313
+
314
+ if u == "reflect":
315
+ if not last_plan:
316
+ print("[reflect] no plan to reflect on")
317
+ continue
318
+ improved = reflect_on(last_plan, tok, model)
319
+ last_plan = improved
320
+ Store.log_mem("reflect", {"plan": improved})
321
+ print(improved)
322
+ continue
323
+
324
+ if u.startswith("tool:"):
325
+ print(tool_router(u[5:].strip(), tok, model))
326
+ continue
327
+
328
+ if u.startswith("toggle"):
329
+ flag = u.split(maxsplit=1)[-1] if len(u.split()) > 1 else ""
330
+ print(Config.toggle(flag))
331
+ continue
332
+
333
+ if u == "status":
334
+ print(json.dumps({
335
+ "turn": Store.state["turn"],
336
+ "cf_hot_active": CF_MODEL is not None,
337
+ "use_cfhot": Config.use_cfhot,
338
+ "temperature": Config.temperature,
339
+ "max_new_tokens": Config.max_new_tokens
340
+ }, indent=2))
341
+ continue
342
+
343
+ # Default: free generation
344
+ out = generate(tok, model, f"{persona_directive()}\nUser request: {u}\nReturn procedure + maxim.")
345
+ Store.log_mem("reply", {"in": u, "out": out})
346
+ print(out)
347
+ Store.state["turn"] += 1
348
+ Store.save()
349
+
350
+ final_report()
351
+
352
+ if __name__ == "__main__":
353
+ main()