Falcon-H1-0.5B-Rust / tools /compare_hf.py
Angshul's picture
Release Falcon-H1 Rust CUDA implementation
5973eb4 verified
Raw History Blame Contribute Delete
1.72 kB
"""Compare Rust token-ID /v1/logits with the official 0.5B Hugging Face model.
Run a Rust --checkpoint server first. This parity check is deliberately a report:
there is no unsupported numerical-parity claim until the numbers are inspected.
"""
import argparse
import json
import urllib.request
import numpy as np
import torch
from transformers import AutoModelForCausalLM
def main():
p = argparse.ArgumentParser()
p.add_argument('--repo', default='tiiuae/Falcon-H1-0.5B-Instruct')
p.add_argument('--api', default='http://127.0.0.1:8080/v1/logits')
p.add_argument('--ids', nargs='+', type=int, default=[1, 452, 1223])
p.add_argument('--device', default='cuda' if torch.cuda.is_available() else 'cpu')
args = p.parse_args()
model = AutoModelForCausalLM.from_pretrained(args.repo, torch_dtype=torch.float32).eval().to(args.device)
with torch.no_grad():
y = model(input_ids=torch.tensor([args.ids], dtype=torch.long, device=args.device)).logits[0, -1, :].float().cpu().numpy()
req = urllib.request.Request(args.api, data=json.dumps({'input_ids': args.ids}).encode(), headers={'Content-Type':'application/json'})
with urllib.request.urlopen(req, timeout=600) as resp:
z = np.asarray(json.load(resp)['logits'], dtype='float32')
if y.shape != z.shape:
raise ValueError(f'vocab shape mismatch: HF {y.shape}, Rust {z.shape}')
print('reference_top1', int(np.argmax(y)))
print('rust_top1', int(np.argmax(z)))
print('max_abs_error', float(np.max(np.abs(y-z))))
print('mean_abs_error', float(np.mean(np.abs(y-z))))
print('cosine_similarity', float(np.dot(y,z) / (np.linalg.norm(y)*np.linalg.norm(z))))
if __name__ == '__main__':
main()