Add checks for F16.gguf and imatrix.dat, as well make HF model removal optional by default.
Browse files---
@ABX-AI
---
- Checks for F16.gguf before initial conversion.
- Checks for imatrix.dat in the GGUF dir before generation to make it more convenient for use with user supplied imatrix data.
- Now asks for user if they want to remove the original downloaded HF repo, defaulting to 'no'.
4 unmodified lines5678910116 unmodified lines1819202122232420 unmodified lines4546474849505119 unmodified lines717273747576774 unmodified lines8283848586878889909192939495969798991001011021 unmodified line1041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371384 unmodified linesimport shutilfrom huggingface_hub import snapshot_download# Clone or update the llama.cpp repository with shallow cloningdef clone_or_update_llama_cpp():print("Preparing...")base_dir = os.path.dirname(os.path.abspath(__file__))6 unmodified linesos.chdir(base_dir)print("The 'llama.cpp' repository is ready.")# Cownload and extract the latest release of llama.cppdef download_llama_release():base_dir = os.path.dirname(os.path.abspath(__file__))dl_dir = os.path.join(base_dir, "bin", "dl")20 unmodified lineselse:print("Failed to fetch the latest release information.")# Download and extract cudart if necessarydef download_cudart_if_necessary(latest_release_tag):base_dir = os.path.dirname(os.path.abspath(__file__))cudart_dl_dir = os.path.join(base_dir, "bin", "dl")19 unmodified lineselse:print("Failed to download the cudart release file.")# Collect user input and download the specified model repositorydef download_model_repo():base_dir = os.path.dirname(os.path.abspath(__file__))models_dir = os.path.join(base_dir, "models")4 unmodified linesmodel_name = model_id.split("/")[-1]model_dir = os.path.join(models_dir, model_name)# Download the model repository if it doesn't existif not os.path.exists(model_dir):revision = input("Enter the revision (branch, tag, or commit) to download (default: main): ") or "main"print("Downloading model repository...")snapshot_download(repo_id=model_id, local_dir=model_dir, revision=revision)print("Model repository downloaded successfully.")else:print("Model already exists.")# Convert the downloaded model to GGUF F16 format and generate imatrix.datconvert_model_to_gguf_f16(base_dir, model_dir, model_name)# Convert the downloaded model to GGUF F16 formatdef convert_model_to_gguf_f16(base_dir, model_dir, model_name):convert_script = os.path.join(base_dir, "llama.cpp", "convert.py")gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")gguf_model_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")1 unmodified lineif not os.path.exists(gguf_dir):os.makedirs(gguf_dir)# Execute the conversion command if F16 file doesn't existif not os.path.exists(gguf_model_path):subprocess.run(["python", convert_script, model_dir, "--outfile", gguf_model_path, "--outtype", "f16"])# Delete the original model directoryshutil.rmtree(model_dir)print(f"Original model directory '{model_dir}' deleted.")# Execute the imatrix command if imatrix.dat doesn't existimatrix_exe = os.path.join(base_dir, "bin", "imatrix.exe")imatrix_output = os.path.join(gguf_dir, "imatrix.dat")imatrix_txt = os.path.join(base_dir, "imatrix", "imatrix.txt")if not os.path.exists(imatrix_output):subprocess.run([imatrix_exe, "-m", gguf_model_path, "-f", imatrix_txt, "-ngl", "13"])# Move the imatrix.dat file to the GGUF foldershutil.move("imatrix.dat", gguf_dir)print("imatrix.dat generated successfully.")# Quantize the modelsquantize_models(base_dir, model_name)# Qantize models with different optionsdef quantize_models(base_dir, model_name):gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")f16_gguf_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")quantization_options = ["Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS", "Q5_K_M","Q5_K_S", "Q6_K", "Q8_0", "IQ3_M", "IQ3_S", "IQ3_XS", "IQ3_XXS"]for quant_option in quantization_options:4 unmodified lines5678910116 unmodified lines1819202122232420 unmodified lines4546474849505119 unmodified lines717273747576774 unmodified lines8283848586878889909192939495969798991001011021031041051061071081091101111121 unmodified line1141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601614 unmodified linesimport shutilfrom huggingface_hub import snapshot_download# Clone or update the llama.cpp repository with --depth 1def clone_or_update_llama_cpp():print("Preparing...")base_dir = os.path.dirname(os.path.abspath(__file__))6 unmodified linesos.chdir(base_dir)print("The 'llama.cpp' repository is ready.")# Download and extract the latest release of llama.cpp Windows binariesdef download_llama_release():base_dir = os.path.dirname(os.path.abspath(__file__))dl_dir = os.path.join(base_dir, "bin", "dl")20 unmodified lineselse:print("Failed to fetch the latest release information.")# Download and extract the Cuda .dll resources if they aren't present in the bin folderdef download_cudart_if_necessary(latest_release_tag):base_dir = os.path.dirname(os.path.abspath(__file__))cudart_dl_dir = os.path.join(base_dir, "bin", "dl")19 unmodified lineselse:print("Failed to download the cudart release file.")# Ask for user input to download or fetch from cache the specified model repository if it doesn't existdef download_model_repo():base_dir = os.path.dirname(os.path.abspath(__file__))models_dir = os.path.join(base_dir, "models")4 unmodified linesmodel_name = model_id.split("/")[-1]model_dir = os.path.join(models_dir, model_name)# Check if the model repository already existsif os.path.exists(model_dir):print("Model repository already exists. Using existing repository.")# If the model already exists, prompt the user if they want to delete the model directorydelete_model_dir = input("Remove HF model folder after converting original model to GGUF? (yes/no) (default: no): ").strip().lower()# Convert the existing model to GGUF F16 format and generate imatrix.datconvert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir)else:revision = input("Enter the revision (branch, tag, or commit) to download (default: main): ") or "main"# Ask the user if they want to remove the HF model folder after conversiondelete_model_dir = input("Remove HF model folder after converting original model to GGUF? (yes/no) (default: no): ").strip().lower()print("Downloading model repository...")snapshot_download(repo_id=model_id, local_dir=model_dir, revision=revision)print("Model repository downloaded successfully.")# Convert the downloaded model to GGUF F16 format and generate imatrix.datconvert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir)# Convert the downloaded model to GGUF F16 formatdef convert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir):convert_script = os.path.join(base_dir, "llama.cpp", "convert.py")gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")gguf_model_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")1 unmodified lineif not os.path.exists(gguf_dir):os.makedirs(gguf_dir)# Check if F16 file already existsif not os.path.exists(gguf_model_path):# Execute the conversion commandsubprocess.run(["python", convert_script, model_dir, "--outfile", gguf_model_path, "--outtype", "f16"])# Delete the original model directory under conditionsif delete_model_dir == 'yes' or delete_model_dir == 'y':shutil.rmtree(model_dir)print(f"Original model directory '{model_dir}' deleted.")else:print(f"Original model directory '{model_dir}' was not deleted. You can remove it manually.")# Check if imatrix.dat exists within gguf_dirimatrix_exe = os.path.join(base_dir, "bin", "imatrix.exe")imatrix_output = os.path.join(gguf_dir, "imatrix.dat")imatrix_txt = os.path.join(base_dir, "imatrix", "imatrix.txt")if not os.path.exists(imatrix_output):# Execute the imatrix commandsubprocess.run([imatrix_exe, "-m", gguf_model_path, "-f", imatrix_txt, "-ngl", "13"], cwd=gguf_dir)# Move the imatrix.dat file to the GGUF foldershutil.move(os.path.join(gguf_dir, "imatrix.dat"), gguf_dir)print("imatrix.dat generated successfully.")else:print("Skipping imatrix generation as imatrix.dat already exists.")else:print("Skipping model conversion as F16 file already exists.")# Quantize the modelsquantize_models(base_dir, model_name)# Quantize models with different optionsdef quantize_models(base_dir, model_name):gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")f16_gguf_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")quantization_options = ["IQ3_M", "IQ3_XXS","Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS","Q5_K_M", "Q5_K_S","Q6_K","Q8_0"]for quant_option in quantization_options:
gguf-imat.py+44-21