char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c05e8c993
char/aliceai-t5-moe
1#!/usr/bin/env python3 2from __future__import annotations 3 4import uuid 5import hashlib 6 7import logging 8import argparse 9import os 10import sys 11from pathlib import Path 12 13from tqdm import tqdm 14 15# Necessary to load the local gguf package 16if "NO_LOCAL_GGUF" not in os .environ and (Path (__file__ ).parent .parent .parent .parent / 'gguf-py' ).exists (): 17sys .path .insert (0 ,str (Path (__file__ ).parent .parent .parent )) 18 19from gguf import GGUFReader # noqa: E402 20 21 22logger = logging .getLogger ("gguf-hash" ) 23 24# UUID_NAMESPACE_LLAMA_CPP = uuid.uuid5(uuid.NAMESPACE_URL, 'en.wikipedia.org/wiki/Llama.cpp') 25UUID_NAMESPACE_LLAMA_CPP = uuid .UUID ('ef001206-dadc-5f6d-a15f-3359e577d4e5' ) 26 27 28# For more information about what field.parts and field.data represent, 29# please see the comments in the modify_gguf.py example. 30def gguf_hash (reader :GGUFReader ,filename :str ,disable_progress_bar :bool ,no_layer :bool )-> None : 31sha1 = hashlib .sha1 () 32sha256 = hashlib .sha256 () 33uuidv5_sha1 = hashlib .sha1 () 34uuidv5_sha1 .update (UUID_NAMESPACE_LLAMA_CPP .bytes ) 35 36# Total Weight Calculation For Progress Bar 37total_weights = 0 38for n ,tensor in enumerate (reader .tensors ,1 ): 39 40# We don't need these 41if tensor .name .endswith ((".attention.masked_bias" ,".attention.bias" ,".rotary_emb.inv_freq" )): 42continue 43 44# Calculate Tensor Volume 45sum_weights_in_tensor = 1 46for dim in tensor .shape : 47sum_weights_in_tensor *= dim 48total_weights += sum_weights_in_tensor 49 50# Hash Progress Bar 51bar = tqdm (desc = "Hashing" ,total = total_weights ,unit = "weights" ,unit_scale = True ,disable = disable_progress_bar ) 52 53# Hashing Process 54for tensor in reader .tensors : 55 56# We don't need these 57if tensor .name .endswith ((".attention.masked_bias" ,".attention.bias" ,".rotary_emb.inv_freq" )): 58continue 59 60# Progressbar 61sum_weights_in_tensor = 1 62for dim in tensor .shape : 63sum_weights_in_tensor *= dim 64bar .update (sum_weights_in_tensor ) 65 66if not no_layer : 67 68sha1_layer = hashlib .sha1 () 69sha1_layer .update (tensor .data .data ) 70"sha1 {0} {1}:{2}" .format (sha1_layer .hexdigest (),filename ,tensor .name ))# noqa: NP100 71 72sha256_layer = hashlib .sha256 () 73sha256_layer .update (tensor .data .data ) 74"sha256 {0} {1}:{2}" .format (sha256_layer .hexdigest (),filename ,tensor .name ))# noqa: NP100 75 76sha1 .update (tensor .data .data ) 77sha256 .update (tensor .data .data ) 78uuidv5_sha1 .update (tensor .data .data ) 79 80# Flush Hash Progress Bar 81bar .close () 82 83# Display Hash Output 84"sha1 {0} {1}" .format (sha1 .hexdigest (),filename ))# noqa: NP100 85"sha256 {0} {1}" .format (sha256 .hexdigest (),filename ))# noqa: NP100 86"uuid {0} {1}" .format (uuid .UUID (bytes = uuidv5_sha1 .digest ()[:16 ],version = 5 ),filename ))# noqa: NP100 87 88 89def main ()-> None : 90parser = argparse .ArgumentParser (description = "Dump GGUF file metadata" ) 91parser .add_argument ("model" ,type = str ,help = "GGUF format model filename" ) 92parser .add_argument ("--no-layer" ,action = "store_true" ,help = "exclude per layer hash" ) 93parser .add_argument ("--verbose" ,action = "store_true" ,help = "increase output verbosity" ) 94parser .add_argument ("--progressbar" ,action = "store_true" ,help = "enable progressbar" ) 95args = parser .parse_args (None if len (sys .argv )> 1 else ["--help" ]) 96logging .basicConfig (level = logging .DEBUG if args .verbose else logging .INFO ) 97reader = GGUFReader (args .model ,'r' ) 98gguf_hash (reader ,args .model ,not args .progressbar ,args .no_layer ) 99 100 101if __name__ == '__main__' : 102main ()