| import argparse |
| import numpy as np |
| import torch |
| import torch.nn as nn |
|
|
| |
|
|
| def run_proof(): |
| print("======================================================================") |
| print("ZYMATICA | Cuneiform-U Normalization Scalar Stability Proof") |
| print("======================================================================\n") |
|
|
| |
| torch.manual_seed(42) |
| np.random.seed(42) |
|
|
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| print(f"Using Device: {device}") |
| |
| |
| 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)") |
|
|
| |
| raw_coords_np = np.random.randint(0, 256, size=(vocab_size, 3)).astype(np.float32) |
| |
| |
| print("\n[2] Case A: Running training step with raw coordinates [0, 255]...") |
| |
| |
| linear_head_raw = nn.Linear(embed_dim, vocab_size, bias=False).to(device).half() |
| |
| |
| hidden_states = torch.randn(batch_size, embed_dim, device=device, dtype=torch.float16) * 2.0 |
| |
| target_labels = torch.randint(0, vocab_size, (batch_size,), device=device) |
| |
| |
| logits_raw = linear_head_raw(hidden_states) |
| |
| |
| raw_coords_tensor = torch.tensor(raw_coords_np, dtype=torch.float16, device=device) |
| |
| |
| topk_logits, topk_indices = torch.topk(logits_raw.float(), k=k_top, dim=-1) |
| probs = torch.softmax(topk_logits, dim=-1).to(torch.float16) |
| |
| |
| topk_coords = raw_coords_tensor[topk_indices] |
| pred_coords_raw = torch.bmm(probs.unsqueeze(1), topk_coords).squeeze(1) |
| |
| |
| target_coords_raw = raw_coords_tensor[target_labels] |
| |
| |
| loss_coord_raw = torch.sum((pred_coords_raw - target_coords_raw) ** 2) |
| print(f" - Raw Coordinate Loss Value: {loss_coord_raw.item():.4f}") |
| |
| |
| linear_head_raw.zero_grad() |
| loss_coord_raw.backward() |
| |
| |
| 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]") |
|
|
| |
| 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() |
| |
| linear_head_norm.weight.data.copy_(linear_head_raw.weight.data) |
| |
| |
| norm_coords_tensor = raw_coords_tensor / 255.0 |
| |
| |
| logits_norm = linear_head_norm(hidden_states) |
| |
| |
| 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) |
| |
| |
| topk_coords_norm = norm_coords_tensor[topk_indices_norm] |
| pred_coords_norm = torch.bmm(probs_norm.unsqueeze(1), topk_coords_norm).squeeze(1) |
| |
| |
| target_coords_norm = norm_coords_tensor[target_labels] |
| |
| |
| loss_coord_norm = torch.mean((pred_coords_norm - target_coords_norm) ** 2) |
| print(f" - Normalized Coordinate Loss Value: {loss_coord_norm.item():.6f}") |
| |
| |
| linear_head_norm.zero_grad() |
| loss_coord_norm.backward() |
| |
| |
| 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]") |
|
|
| |
| 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() |
|
|