char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
e2eb39e81
char/aliceai-t5-moe
1#!/usr/bin/env python3 2 3import sys 4import numpy as np 5from pathlib import Path 6import os 7 8# Add utils directory to path for direct script execution 9sys .path .insert (0 ,str (Path (__file__ ).parent .parent / "utils" )) 10from common import get_model_name_from_env_path ,compare_tokens ,exit_with_warning # type: ignore[import-not-found, ty:unresolved-import] 11 12def quick_logits_check (pytorch_file ,llamacpp_file ): 13"""Lightweight sanity check before NMSE""" 14 15try : 16pytorch_logits = np .fromfile (pytorch_file ,dtype = np .float32 ) 17llamacpp_logits = np .fromfile (llamacpp_file ,dtype = np .float32 ) 18except Exception as e : 19f"❌ NOK: Failed to load files - { e } " ) 20return False 21 22# Check shapes match 23if pytorch_logits .shape != llamacpp_logits .shape : 24f"❌ NOK: Shape mismatch - PyTorch: { pytorch_logits . shape } , llama.cpp: { llamacpp_logits . shape } " ) 25return False 26 27# Calculate key metrics 28diff = pytorch_logits - llamacpp_logits 29abs_diff = np .abs (diff ) 30max_diff = np .max (abs_diff ) 31 32# Get top 10 predictions from both models 33pytorch_top10 = np .argsort (pytorch_logits )[- 10 :][::- 1 ] 34llamacpp_top10 = np .argsort (llamacpp_logits )[- 10 :][::- 1 ] 35f"Top 10 PyTorch logits: { pytorch_logits [ pytorch_top10 ] } " ) 36f"Top 10 llama.cpp logits: { llamacpp_logits [ llamacpp_top10 ] } " ) 37f"Max absolute difference: { max_diff :.4f } " ) 38 39return True 40 41def main (): 42model_path = os .environ .get ('MODEL_PATH' ) 43model_name = get_model_name_from_env_path ('MODEL_PATH' ) 44data_dir = Path ("data" ) 45pytorch_file = data_dir / f"pytorch- { model_name } .bin" 46 47llamacpp_model_name = get_model_name_from_env_path ('CONVERTED_MODEL' ) 48f"Using converted model: { llamacpp_model_name } " ) 49llamacpp_file = data_dir / f"llamacpp- { llamacpp_model_name } .bin" 50 51if not pytorch_file .exists (): 52f"Error: PyTorch logits file not found: { pytorch_file } " ) 53"Please run scripts/run-org-model.sh first to generate this file." ) 54sys .exit (1 ) 55 56if not llamacpp_file .exists (): 57f"Error: llama.cpp logits file not found: { llamacpp_file } " ) 58"Please run scripts/run-converted-model.sh first to generate this file." ) 59sys .exit (1 ) 60 61"Checked all required files were found. Proceeding...\n" ) 62 63# Verify tokens as they are a prerequisite for logits comparison. 64"🔍 Token Comparison Check" ) 65"=" * 40 ) 66if not compare_tokens (f"pytorch- { model_name } " ,f"llamacpp- { llamacpp_model_name } " ): 67exit_with_warning ("\n❌ Token mismatch detected" ,model_path ) 68"🔍 GGML Model Validation for model " ,model_name ) 71"=" * 40 ) 72f"PyTorch logits : { pytorch_file } " ) 73f"llama.cpp logits: { llamacpp_file } " ) 74success = quick_logits_check (pytorch_file ,llamacpp_file ) 77 78# Exit with appropriate code 79if success : 80"✅ OK: Lightweight model check successful!" ) 81" Ok to proceed with NMSE check..." ) 82sys .exit (0 ) 83else : 84exit_with_warning (f"❌ NOK: Top 10 predictions don't match - generation will differ" ,model_path ) 85 86if __name__ == "__main__" : 87main ()