Skip to content

Commit 79fea96

Browse files
Merge pull request #36 from InauguralSystems/claude/eigenscript-deep-dive-013qE3Qft5PPArmn4Fi5VWUo
Claude/eigenscript deep dive 013q e3 qft5 pp armn4 fi5 vw uo
2 parents 92d8157 + 129d60d commit 79fea96

1 file changed

Lines changed: 244 additions & 31 deletions

File tree

examples/ai/introspective_llm.eigs

Lines changed: 244 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,32 @@ print of "╚══════════════════════
5757
print of ""
5858

5959
embed_matrix is random_matrix of [vocab_size, d_model]
60-
W_Q is random_matrix of [d_model, d_k]
61-
W_K is random_matrix of [d_model, d_k]
62-
W_V is random_matrix of [d_model, d_k]
63-
W_O is random_matrix of [d_k, d_model]
64-
W_ff1 is random_matrix of [d_model, d_ff]
65-
W_ff2 is random_matrix of [d_ff, d_model]
66-
67-
print of "Weight initialization complete."
60+
61+
# Layer 1 weights
62+
W_Q1 is random_matrix of [d_model, d_k]
63+
W_K1 is random_matrix of [d_model, d_k]
64+
W_V1 is random_matrix of [d_model, d_k]
65+
W_ff1_1 is random_matrix of [d_model, d_ff]
66+
W_ff2_1 is random_matrix of [d_ff, d_model]
67+
68+
# Layer 2 weights
69+
W_Q2 is random_matrix of [d_model, d_k]
70+
W_K2 is random_matrix of [d_model, d_k]
71+
W_V2 is random_matrix of [d_model, d_k]
72+
W_ff1_2 is random_matrix of [d_model, d_ff]
73+
W_ff2_2 is random_matrix of [d_ff, d_model]
74+
75+
# Output projection (hidden -> vocab logits)
76+
W_out is random_matrix of [d_model, vocab_size]
77+
78+
# Aliases for backward compatibility
79+
W_Q is W_Q1
80+
W_K is W_K1
81+
W_V is W_V1
82+
W_ff1 is W_ff1_1
83+
W_ff2 is W_ff2_1
84+
85+
print of "Weight initialization complete (2 layers)."
6886
print of ""
6987
print of ">>> INTERROGATIVE: Querying initialization state..."
7088

@@ -118,6 +136,142 @@ define introspective_attention as:
118136

119137
return output
120138

139+
140+
# Causal Introspective Attention (for autoregressive generation)
141+
# Uses causal mask to prevent attending to future tokens
142+
define causal_introspective_attention as:
143+
input_emb is arg[0]
144+
w_q is arg[1]
145+
w_k is arg[2]
146+
w_v is arg[3]
147+
attn_scale is arg[4]
148+
seq_length is arg[5]
149+
150+
Q is matmul of [input_emb, w_q]
151+
K is matmul of [input_emb, w_k]
152+
V is matmul of [input_emb, w_v]
153+
154+
K_T is transpose of K
155+
scores is matmul of [Q, K_T]
156+
scores is matrix_scale of [scores, attn_scale]
157+
158+
mask is causal_mask of seq_length
159+
scores is matrix_add of [scores, mask]
160+
161+
if oscillating:
162+
prev_scores is was is scores
163+
scores is matrix_scale of [matrix_add of [scores, prev_scores], 0.5]
164+
165+
if chaotic:
166+
scores is matrix_scale of [scores, 0.5]
167+
168+
attn_weights is softmax_matrix of scores
169+
output is matmul of [attn_weights, V]
170+
171+
return output
172+
173+
174+
define introspective_layer as:
175+
input_hidden is arg[0]
176+
w_q is arg[1]
177+
w_k is arg[2]
178+
w_v is arg[3]
179+
w_ff1 is arg[4]
180+
w_ff2 is arg[5]
181+
attn_scale is arg[6]
182+
layer_num is arg[7]
183+
184+
attn_out is introspective_attention of [input_hidden, w_q, w_k, w_v, attn_scale]
185+
layer_grad is why is attn_out
186+
187+
hidden is layer_norm_matrix of attn_out
188+
residual1 is matrix_add of [input_hidden, hidden]
189+
190+
ffn_h is matmul of [residual1, w_ff1]
191+
ffn_act is gelu_matrix of ffn_h
192+
ffn_out is matmul of [ffn_act, w_ff2]
193+
194+
output is matrix_add of [residual1, ffn_out]
195+
output is layer_norm_matrix of output
196+
197+
layer_quality is how is output
198+
layer_trend is trend is output
199+
200+
layer_converged is 0
201+
if stable:
202+
layer_converged is 1
203+
print of " Layer"
204+
print of layer_num
205+
print of "CONVERGED (stable)"
206+
207+
return [output, layer_converged]
208+
209+
210+
# Causal Introspective Layer (for generation)
211+
# Uses causal masking to prevent looking at future tokens
212+
define causal_introspective_layer as:
213+
input_hidden is arg[0]
214+
w_q is arg[1]
215+
w_k is arg[2]
216+
w_v is arg[3]
217+
w_ff1 is arg[4]
218+
w_ff2 is arg[5]
219+
attn_scale is arg[6]
220+
layer_num is arg[7]
221+
seq_length is arg[8]
222+
223+
attn_out is causal_introspective_attention of [input_hidden, w_q, w_k, w_v, attn_scale, seq_length]
224+
layer_grad is why is attn_out
225+
226+
hidden is layer_norm_matrix of attn_out
227+
residual1 is matrix_add of [input_hidden, hidden]
228+
229+
ffn_h is matmul of [residual1, w_ff1]
230+
ffn_act is gelu_matrix of ffn_h
231+
ffn_out is matmul of [ffn_act, w_ff2]
232+
233+
output is matrix_add of [residual1, ffn_out]
234+
output is layer_norm_matrix of output
235+
236+
layer_converged is 0
237+
if stable:
238+
layer_converged is 1
239+
print of " Layer"
240+
print of layer_num
241+
print of "CONVERGED (causal)"
242+
243+
return [output, layer_converged]
244+
245+
246+
# Temperature-based Token Sampling
247+
# Uses introspection to adapt temperature based on model confidence
248+
define temperature_sample as:
249+
hidden_state is arg[0]
250+
temp is arg[1]
251+
252+
logits is matmul of [hidden_state, W_out]
253+
inv_temp is 1.0 / temp
254+
scaled_logits is matrix_scale of [logits, inv_temp]
255+
probs is softmax_matrix of scaled_logits
256+
prob_list is matrix_to_list of probs
257+
last_row is prob_list[0]
258+
259+
best_idx is 0
260+
best_val is 0.0
261+
check_idx is 0
262+
263+
loop while check_idx < vocab_size:
264+
current_prob is last_row[check_idx]
265+
current_val is what is current_prob
266+
if current_val > best_val:
267+
best_val is current_val
268+
best_idx is check_idx
269+
270+
check_idx is check_idx + 1
271+
272+
return best_idx
273+
274+
121275
print of "Testing introspective attention..."
122276
print of ""
123277

@@ -178,11 +332,31 @@ should_continue is 1
178332

179333
loop while epoch < max_epochs:
180334
if should_continue > 0:
181-
hidden is introspective_attention of [input_embeddings, W_Q, W_K, W_V, scale]
182-
hidden is layer_norm_matrix of hidden
183-
ffn_hidden is matmul of [hidden, W_ff1]
184-
ffn_act is gelu_matrix of ffn_hidden
185-
output is matmul of [ffn_act, W_ff2]
335+
hidden is input_embeddings
336+
total_layers_converged is 0
337+
338+
# Layer 1
339+
layer1_result is introspective_layer of [hidden, W_Q1, W_K1, W_V1, W_ff1_1, W_ff2_1, scale, 1]
340+
hidden is layer1_result[0]
341+
layer1_conv is layer1_result[1]
342+
total_layers_converged is total_layers_converged + layer1_conv
343+
344+
# Layer 2 (only if Layer 1 didn't converge - adaptive depth)
345+
if layer1_conv < 1:
346+
layer2_result is introspective_layer of [hidden, W_Q2, W_K2, W_V2, W_ff1_2, W_ff2_2, scale, 2]
347+
hidden is layer2_result[0]
348+
layer2_conv is layer2_result[1]
349+
total_layers_converged is total_layers_converged + layer2_conv
350+
else:
351+
print of " [EARLY EXIT] Skipping Layer 2 (Layer 1 converged)"
352+
353+
output is hidden
354+
355+
# ═══════════════════════════════════════════════════════════════════
356+
# COMPUTE ERROR AND LOSS
357+
# ═══════════════════════════════════════════════════════════════════
358+
359+
error is matrix_add of [output, matrix_scale of [target_output, -1.0]]
186360

187361
prev_loss is current_loss
188362
current_loss is compute_loss of [output, target_output]
@@ -191,6 +365,10 @@ loop while epoch < max_epochs:
191365
loss_delta is change is current_loss
192366
loss_trend is trend is current_loss
193367

368+
# ═══════════════════════════════════════════════════════════════════
369+
# PREDICATE-DRIVEN LEARNING RATE ADAPTATION
370+
# ═══════════════════════════════════════════════════════════════════
371+
194372
effective_lr is learning_rate
195373

196374
if oscillating:
@@ -204,16 +382,48 @@ loop while epoch < max_epochs:
204382
if improving:
205383
effective_lr is learning_rate * 1.1
206384

207-
update_scale is 1.0 - (effective_lr * 0.01)
208-
W_ff1 is matrix_scale of [W_ff1, update_scale]
209-
W_ff2 is matrix_scale of [W_ff2, update_scale]
385+
if chaotic:
386+
effective_lr is learning_rate * 0.3
387+
print of " [CHAOTIC] Drastically reducing learning rate"
388+
389+
# Simple gradient-informed weight update
390+
# Use 'what is' to extract scalars for safe arithmetic
391+
lr_scalar is what is effective_lr
392+
393+
# Determine update scale based on predicates
394+
update_scale is 0.999
395+
if improving:
396+
update_scale is 0.998
397+
if diverging:
398+
update_scale is 1.001
399+
if oscillating:
400+
update_scale is 0.9995
401+
402+
# Apply scaled updates to all layer weights
403+
# Layer 1
404+
W_Q1 is matrix_scale of [W_Q1, update_scale]
405+
W_K1 is matrix_scale of [W_K1, update_scale]
406+
W_V1 is matrix_scale of [W_V1, update_scale]
407+
W_ff1_1 is matrix_scale of [W_ff1_1, update_scale]
408+
W_ff2_1 is matrix_scale of [W_ff2_1, update_scale]
409+
410+
# Layer 2
411+
W_Q2 is matrix_scale of [W_Q2, update_scale]
412+
W_K2 is matrix_scale of [W_K2, update_scale]
413+
W_V2 is matrix_scale of [W_V2, update_scale]
414+
W_ff1_2 is matrix_scale of [W_ff1_2, update_scale]
415+
W_ff2_2 is matrix_scale of [W_ff2_2, update_scale]
210416

211417
print of "Epoch:"
212418
print of epoch
213419
print of " Loss:"
214420
print of current_loss
215421
print of " Trend:"
216422
print of loss_trend
423+
print of " Layers converged:"
424+
print of total_layers_converged
425+
print of " Update scale:"
426+
print of update_scale
217427
print of " Framework Strength:"
218428
print of framework_strength
219429

@@ -263,27 +473,27 @@ define introspective_generate as:
263473
pos_enc is sinusoidal_pe of [current_len, d_model]
264474
hidden is matrix_add of [current_emb, pos_enc]
265475

266-
hidden is introspective_attention of [hidden, W_Q, W_K, W_V, scale]
267-
hidden is layer_norm_matrix of hidden
268-
269-
ffn_h is matmul of [hidden, W_ff1]
270-
ffn_a is gelu_matrix of ffn_h
271-
logits is matmul of [ffn_a, W_ff2]
272-
273-
logit_list is matrix_to_list of logits
274-
first_row is logit_list[0]
275-
next_token_raw is first_row[0]
476+
# Multi-layer forward with CAUSAL MASKING and adaptive depth
477+
layer1_out is causal_introspective_layer of [hidden, W_Q1, W_K1, W_V1, W_ff1_1, W_ff2_1, scale, 1, current_len]
478+
hidden is layer1_out[0]
479+
l1_conv is layer1_out[1]
276480

277-
scaled_raw is next_token_raw * 10
278-
rounded_raw is round of scaled_raw
279-
next_token is abs of rounded_raw
280-
if next_token > 9:
281-
next_token is next_token % 10
481+
# Only run layer 2 if layer 1 didn't converge
482+
layers_used is 1
483+
if l1_conv < 1:
484+
layer2_out is causal_introspective_layer of [hidden, W_Q2, W_K2, W_V2, W_ff1_2, W_ff2_2, scale, 2, current_len]
485+
hidden is layer2_out[0]
486+
layers_used is 2
282487

488+
next_token is temperature_sample of [hidden, temperature]
283489
hidden_trend is trend is hidden
284490

285491
print of " Generated token:"
286492
print of next_token
493+
print of " Layers used:"
494+
print of layers_used
495+
print of " Temperature:"
496+
print of temperature
287497
print of " Hidden trend:"
288498
print of hidden_trend
289499
print of " Framework Strength:"
@@ -408,6 +618,9 @@ print of "Key innovations demonstrated:"
408618
print of " 1. INTERROGATIVES: why/how/what/when for gradient-free updates"
409619
print of " 2. TEMPORALS: was/change/trend for history-aware adaptation"
410620
print of " 3. PREDICATES: converged/stable/oscillating for self-termination"
621+
print of " 4. MULTI-LAYER: Per-layer convergence with adaptive depth"
622+
print of " 5. TEMPERATURE: Predicate-adaptive sampling (oscillating/chaotic)"
623+
print of " 6. CAUSAL MASKING: Autoregressive generation with proper masking"
411624
print of ""
412625
print of "This model KNOWS when it has learned enough and when to stop generating."
413626
print of ""

0 commit comments

Comments
 (0)