File size: 6,289 Bytes
8554f04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import argparse
import numpy as np
import torch
import torch.nn as nn

# ZYMATICA: Cuneiform-U Normalization Scalar (Numerical Stability Tuning) Proof

def run_proof():
    print("======================================================================")
    print("ZYMATICA | Cuneiform-U Normalization Scalar Stability Proof")
    print("======================================================================\n")

    # Set random seeds for reproducibility
    torch.manual_seed(42)
    np.random.seed(42)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Using Device: {device}")
    
    # 1. Define simulation parameters
    vocab_size = 500
    embed_dim = 128
    batch_size = 16
    k_top = 256
    
    print(f"\n[1] Initializing simulation parameters:")
    print(f"  - Vocab Size:   {vocab_size}")
    print(f"  - Embed Dim:    {embed_dim}")
    print(f"  - Batch Size:   {batch_size}")
    print(f"  - Precision:    Float16 (Half-Precision)")

    # Generate synthetic raw integer coordinates in [0, 255]
    raw_coords_np = np.random.randint(0, 256, size=(vocab_size, 3)).astype(np.float32)
    
    # 2. Case A: Raw Integer Coordinates (0 to 255)
    print("\n[2] Case A: Running training step with raw coordinates [0, 255]...")
    
    # Define a simple linear projection layer (simulating the LM output head) in float16
    linear_head_raw = nn.Linear(embed_dim, vocab_size, bias=False).to(device).half()
    
    # Input hidden states (batch_size, embed_dim)
    hidden_states = torch.randn(batch_size, embed_dim, device=device, dtype=torch.float16) * 2.0
    # True target labels
    target_labels = torch.randint(0, vocab_size, (batch_size,), device=device)
    
    # Forward pass to get logits
    logits_raw = linear_head_raw(hidden_states) # (batch_size, vocab_size)
    
    # Compute coordinate resonance loss using raw coordinates in float16
    raw_coords_tensor = torch.tensor(raw_coords_np, dtype=torch.float16, device=device)
    
    # Select Top-K logits and calculate probabilities
    topk_logits, topk_indices = torch.topk(logits_raw.float(), k=k_top, dim=-1)
    probs = torch.softmax(topk_logits, dim=-1).to(torch.float16)
    
    # Predicted coordinates
    topk_coords = raw_coords_tensor[topk_indices] # (batch_size, k_top, 3)
    pred_coords_raw = torch.bmm(probs.unsqueeze(1), topk_coords).squeeze(1) # (batch_size, 3)
    
    # Target coordinates
    target_coords_raw = raw_coords_tensor[target_labels] # (batch_size, 3)
    
    # MSE loss or sum loss to demonstrate float16 range limits
    loss_coord_raw = torch.sum((pred_coords_raw - target_coords_raw) ** 2)
    print(f"  - Raw Coordinate Loss Value: {loss_coord_raw.item():.4f}")
    
    # Backward pass
    linear_head_raw.zero_grad()
    loss_coord_raw.backward()
    
    # Check for NaN / Inf gradients
    raw_grads = linear_head_raw.weight.grad
    has_nan_raw = torch.isnan(raw_grads).any().item()
    has_inf_raw = torch.isinf(raw_grads).any().item()
    max_grad_raw = torch.max(torch.abs(raw_grads.nan_to_num(0.0))).item()
    
    print(f"  - Gradient Status (Raw Coordinate System):")
    print(f"    - Contains NaN:  {has_nan_raw}")
    print(f"    - Contains Inf:  {has_inf_raw}")
    print(f"    - Max Grad Abs:  {max_grad_raw:.4f}")
    if has_nan_raw or has_inf_raw or max_grad_raw > 100.0:
        print("    - Result:        [OVERFLOW/INSTABILITY DETECTED]")

    # 3. Case B: Normalized Coordinates (0.0 to 1.0)
    print("\n[3] Case B: Running training step with normalized coordinates [0.0, 1.0]...")
    
    linear_head_norm = nn.Linear(embed_dim, vocab_size, bias=False).to(device).half()
    # Copy initial weights to make comparisons exact
    linear_head_norm.weight.data.copy_(linear_head_raw.weight.data)
    
    # Normalize coordinate matrix by the Cuneiform Normalization Scalar (255.0)
    norm_coords_tensor = raw_coords_tensor / 255.0
    
    # Forward pass to get logits (same input states)
    logits_norm = linear_head_norm(hidden_states)
    
    # Select Top-K logits and calculate probabilities
    topk_logits_norm, topk_indices_norm = torch.topk(logits_norm.float(), k=k_top, dim=-1)
    probs_norm = torch.softmax(topk_logits_norm, dim=-1).to(torch.float16)
    
    # Predicted coordinates (normalized)
    topk_coords_norm = norm_coords_tensor[topk_indices_norm]
    pred_coords_norm = torch.bmm(probs_norm.unsqueeze(1), topk_coords_norm).squeeze(1)
    
    # Target coordinates (normalized)
    target_coords_norm = norm_coords_tensor[target_labels]
    
    # MSE loss (normalized by batch size for standard scaling)
    loss_coord_norm = torch.mean((pred_coords_norm - target_coords_norm) ** 2)
    print(f"  - Normalized Coordinate Loss Value: {loss_coord_norm.item():.6f}")
    
    # Backward pass
    linear_head_norm.zero_grad()
    loss_coord_norm.backward()
    
    # Check for NaN / Inf gradients
    norm_grads = linear_head_norm.weight.grad
    has_nan_norm = torch.isnan(norm_grads).any().item()
    has_inf_norm = torch.isinf(norm_grads).any().item()
    max_grad_norm = torch.max(torch.abs(norm_grads)).item()
    
    print(f"  - Gradient Status (Normalized Coordinate System):")
    print(f"    - Contains NaN:  {has_nan_norm}")
    print(f"    - Contains Inf:  {has_inf_norm}")
    print(f"    - Max Grad Abs:  {max_grad_norm:.6f}")
    if not (has_nan_norm or has_inf_norm) and max_grad_norm < 1.0:
        print("    - Result:        [STABLE GRADIENTS VERIFIED]")

    # 4. Summary & Verification Output
    print("\n[4] Summary of Stability Tuning Outcomes:")
    print(f"  - Raw Coordinates Loss Max Potential:        {255.0**2:.1f} (Approaches FP16 Limit of 65504)")
    print(f"  - Normalized Coordinates Loss Max Potential: 1.0 (100% FP16 Safe)")
    
    if (has_nan_raw or has_inf_raw or max_grad_raw > 100.0) and not (has_nan_norm or has_inf_norm):
        print("\n[VERIFICATION] Cuneiform-U Normalization Scalar proof successful.")
    else:
        print("\n[VERIFICATION] Proof completed (Simulation run ended).")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Zymatica Cuneiform Normalization Scalar Proof")
    parser.add_argument("--test", action="store_true", help="Run in test mode")
    args = parser.parse_args()
    run_proof()