fn main() {
    println("=== PHASE 1: VOICE DRILL ===")

    let n = load_model("examples/silkie_full.safetensors")
    let ns = create_neuron_stack(1, 512, 64)
    let cs = create_cascade()
    let ts = load_tokenizer("examples/silkie_tokenizer.json")
    let tp = load_topos("examples/silkie_topos.json")
    println(tp)
    let sc = load_schemas("examples/silkie_schemas.json")
    println(sc)

    let embed_w = model_get("token_embedding.weight")
    let baseline = 0.0
    let lr = 0.000003
    let total_reward = 0.0
    let step_count = 0

    for train_step in 0..50 {
        gc()
        neuron_stack_reset()
        cascade_reset()

        // Seed: Silkie prompt tokens (minimal — let HER fill in the voice)
        let prompt = tensor_from_ints(3591, 1207)
        let prompt = t_reshape(prompt, 1, 2)
        let generated = prompt
        let seq_len = 2

        // Generate 8 tokens (matched from working epoch script)
        for step in 0..8 {
            let flat = t_reshape(generated, seq_len)
            let x = embedding_lookup(embed_w, flat)
            let x = t_reshape(x, 1, seq_len, 512)
            for layer_idx in 0..8 {
                let prefix = "layers." + layer_idx
                let x = transformer_block(x,
                    model_get(prefix + ".attention.in_proj_weight"),
                    model_get(prefix + ".attention.in_proj_bias"),
                    model_get(prefix + ".attention.out_proj.weight"),
                    model_get(prefix + ".attention.out_proj.bias"),
                    model_get(prefix + ".ff.0.weight"),
                    model_get(prefix + ".ff.0.bias"),
                    model_get(prefix + ".ff.3.weight"),
                    model_get(prefix + ".ff.3.bias"),
                    model_get(prefix + ".norm1.weight"),
                    model_get(prefix + ".norm1.bias"),
                    model_get(prefix + ".norm2.weight"),
                    model_get(prefix + ".norm2.bias"), 8)
            }
            let x = layer_norm(x, model_get("layers.0.norm1.weight"), model_get("layers.0.norm1.bias"))
            let logits = matmul(x, transpose(embed_w))
            let logits_2d = t_reshape(logits, seq_len, 4100)
            let last_logits = t_slice(logits_2d, 0, seq_len - 1, seq_len)
            let masked = topk_mask(last_logits, 50)
            let probs = softmax(masked)
            let next_token = t_multinomial(probs)
            let generated = t_cat(generated, t_reshape(next_token, 1, 1))
            let seq_len = seq_len + 1
        }

        let gen_flat = t_reshape(generated, seq_len)
        let response_text = detokenize(gen_flat)

        // Reward: STYLE ONLY (Phase 1 — voice drilling)
        // No echo penalty, no relevance. Just: real words + Silkie vocabulary + coherence.
        let reward_vec = compute_reward_phase1(response_text)
        let reward = tensor_sum(t_slice(reward_vec, -1, 0, 1))

        let total_reward = total_reward + reward
        let baseline = baseline * 0.95 + reward * 0.05

        // === TRAINING PASS ===
        gc()
        neuron_stack_reset()
        cascade_reset()
        begin_tape()

        // Register ALL 8 layers QKV weights as trainable
        let qkv_0 = register_param("qkv_0", model_get("layers.0.attention.in_proj_weight"))
        let qkv_1 = register_param("qkv_1", model_get("layers.1.attention.in_proj_weight"))
        let qkv_2 = register_param("qkv_2", model_get("layers.2.attention.in_proj_weight"))
        let qkv_3 = register_param("qkv_3", model_get("layers.3.attention.in_proj_weight"))
        let qkv_4 = register_param("qkv_4", model_get("layers.4.attention.in_proj_weight"))
        let qkv_5 = register_param("qkv_5", model_get("layers.5.attention.in_proj_weight"))
        let qkv_6 = register_param("qkv_6", model_get("layers.6.attention.in_proj_weight"))
        let qkv_7 = register_param("qkv_7", model_get("layers.7.attention.in_proj_weight"))

        let flat = t_reshape(generated, seq_len)
        let x = embedding_lookup(embed_w, flat)
        let x = t_reshape(x, 1, seq_len, 512)

        let x = transformer_block_train(x, qkv_0,
            model_get("layers.0.attention.in_proj_bias"),
            model_get("layers.0.attention.out_proj.weight"),
            model_get("layers.0.attention.out_proj.bias"),
            model_get("layers.0.ff.0.weight"), model_get("layers.0.ff.0.bias"),
            model_get("layers.0.ff.3.weight"), model_get("layers.0.ff.3.bias"),
            model_get("layers.0.norm1.weight"), model_get("layers.0.norm1.bias"),
            model_get("layers.0.norm2.weight"), model_get("layers.0.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_1,
            model_get("layers.1.attention.in_proj_bias"),
            model_get("layers.1.attention.out_proj.weight"),
            model_get("layers.1.attention.out_proj.bias"),
            model_get("layers.1.ff.0.weight"), model_get("layers.1.ff.0.bias"),
            model_get("layers.1.ff.3.weight"), model_get("layers.1.ff.3.bias"),
            model_get("layers.1.norm1.weight"), model_get("layers.1.norm1.bias"),
            model_get("layers.1.norm2.weight"), model_get("layers.1.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_2,
            model_get("layers.2.attention.in_proj_bias"),
            model_get("layers.2.attention.out_proj.weight"),
            model_get("layers.2.attention.out_proj.bias"),
            model_get("layers.2.ff.0.weight"), model_get("layers.2.ff.0.bias"),
            model_get("layers.2.ff.3.weight"), model_get("layers.2.ff.3.bias"),
            model_get("layers.2.norm1.weight"), model_get("layers.2.norm1.bias"),
            model_get("layers.2.norm2.weight"), model_get("layers.2.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_3,
            model_get("layers.3.attention.in_proj_bias"),
            model_get("layers.3.attention.out_proj.weight"),
            model_get("layers.3.attention.out_proj.bias"),
            model_get("layers.3.ff.0.weight"), model_get("layers.3.ff.0.bias"),
            model_get("layers.3.ff.3.weight"), model_get("layers.3.ff.3.bias"),
            model_get("layers.3.norm1.weight"), model_get("layers.3.norm1.bias"),
            model_get("layers.3.norm2.weight"), model_get("layers.3.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_4,
            model_get("layers.4.attention.in_proj_bias"),
            model_get("layers.4.attention.out_proj.weight"),
            model_get("layers.4.attention.out_proj.bias"),
            model_get("layers.4.ff.0.weight"), model_get("layers.4.ff.0.bias"),
            model_get("layers.4.ff.3.weight"), model_get("layers.4.ff.3.bias"),
            model_get("layers.4.norm1.weight"), model_get("layers.4.norm1.bias"),
            model_get("layers.4.norm2.weight"), model_get("layers.4.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_5,
            model_get("layers.5.attention.in_proj_bias"),
            model_get("layers.5.attention.out_proj.weight"),
            model_get("layers.5.attention.out_proj.bias"),
            model_get("layers.5.ff.0.weight"), model_get("layers.5.ff.0.bias"),
            model_get("layers.5.ff.3.weight"), model_get("layers.5.ff.3.bias"),
            model_get("layers.5.norm1.weight"), model_get("layers.5.norm1.bias"),
            model_get("layers.5.norm2.weight"), model_get("layers.5.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_6,
            model_get("layers.6.attention.in_proj_bias"),
            model_get("layers.6.attention.out_proj.weight"),
            model_get("layers.6.attention.out_proj.bias"),
            model_get("layers.6.ff.0.weight"), model_get("layers.6.ff.0.bias"),
            model_get("layers.6.ff.3.weight"), model_get("layers.6.ff.3.bias"),
            model_get("layers.6.norm1.weight"), model_get("layers.6.norm1.bias"),
            model_get("layers.6.norm2.weight"), model_get("layers.6.norm2.bias"), 8)
        let x = transformer_block_train(x, qkv_7,
            model_get("layers.7.attention.in_proj_bias"),
            model_get("layers.7.attention.out_proj.weight"),
            model_get("layers.7.attention.out_proj.bias"),
            model_get("layers.7.ff.0.weight"), model_get("layers.7.ff.0.bias"),
            model_get("layers.7.ff.3.weight"), model_get("layers.7.ff.3.bias"),
            model_get("layers.7.norm1.weight"), model_get("layers.7.norm1.bias"),
            model_get("layers.7.norm2.weight"), model_get("layers.7.norm2.bias"), 8)

        let logits = t_matmul(x, transpose(embed_w))
        end_tape()

        backward(logits)

        // Update ALL 8 layers QKV
        let g0 = get_grad("qkv_0")
        model_set("layers.0.attention.in_proj_weight", sgd_step(qkv_0, g0, lr))
        let g1 = get_grad("qkv_1")
        model_set("layers.1.attention.in_proj_weight", sgd_step(qkv_1, g1, lr))
        let g2 = get_grad("qkv_2")
        model_set("layers.2.attention.in_proj_weight", sgd_step(qkv_2, g2, lr))
        let g3 = get_grad("qkv_3")
        model_set("layers.3.attention.in_proj_weight", sgd_step(qkv_3, g3, lr))
        let g4 = get_grad("qkv_4")
        model_set("layers.4.attention.in_proj_weight", sgd_step(qkv_4, g4, lr))
        let g5 = get_grad("qkv_5")
        model_set("layers.5.attention.in_proj_weight", sgd_step(qkv_5, g5, lr))
        let g6 = get_grad("qkv_6")
        model_set("layers.6.attention.in_proj_weight", sgd_step(qkv_6, g6, lr))
        let g7 = get_grad("qkv_7")
        model_set("layers.7.attention.in_proj_weight", sgd_step(qkv_7, g7, lr))


        let step_count = step_count + 1
        println("  [" + step_count + "/50] R:" + reward + " | " + response_text)
    }

    let avg_reward = total_reward / 50
    println("")
    println("=== VOICE DRILL COMPLETE ===")
    println("Avg reward: " + avg_reward)

    // Save trained weights
    let saved = save_safetensors("examples/silkie_full.safetensors")
    println("Saved: " + saved)
}
