|
| 1 | +#!/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import subprocess as sp |
| 6 | +import glob |
| 7 | + |
| 8 | +parser = argparse.ArgumentParser() |
| 9 | +group = parser.add_mutually_exclusive_group() |
| 10 | +group.add_argument("--run", "-r", action='store_true', help="Run a model previously converted into ggml") |
| 11 | +group.add_argument("--convert", "-c", action='store_true', help="Convert a llama model into ggml") |
| 12 | +group.add_argument("--quantize", "-q", action='store_true', help="Optimize with quantization process ggml") |
| 13 | +group.add_argument("--all-in-one", "-a", action='store_true', help="Execute --convert & --quantize") |
| 14 | +parser.add_argument("model", type=str, help="Directory containing model file, or model file itself (*.pth, *.pt, *.bin)") |
| 15 | +parser.add_argument("--quant-method", "-Q", type=str, help="Chosen quantization method.", default="q4_0") |
| 16 | + |
| 17 | +known_args, unknown_args = parser.parse_known_args() |
| 18 | + |
| 19 | +model_path = known_args.model |
| 20 | +converted_models = glob.glob(os.path.join(model_path, 'ggml-model-*.bin')) |
| 21 | +quantized_models = glob.glob(os.path.join(model_path, f'ggml-model-{known_args.quant_method}.bin')) |
| 22 | + |
| 23 | +if known_args.convert: |
| 24 | + sp.run(['python3', './convert.py', model_path] + unknown_args, check=True) |
| 25 | + |
| 26 | +if known_args.run: |
| 27 | + sp.run(['./main', '-m', model_path] + unknown_args, check=True) |
| 28 | + |
| 29 | +if known_args.quantize: |
| 30 | + if not converted_models: |
| 31 | + print(f"No models ready for quantization found in {model_path}") |
| 32 | + exit(1) |
| 33 | + sp.run(['./quantize', converted_models[0], known_args.quant_method] + unknown_args, check=True) |
| 34 | + |
| 35 | +if known_args.all_in_one: |
| 36 | + if not converted_models: |
| 37 | + sp.run(['python3', './convert.py', model_path], check=True) |
| 38 | + converted_models = glob.glob(os.path.join(model_path, 'ggml-model-*.bin')) |
| 39 | + else: |
| 40 | + print( |
| 41 | + f"Converted models found {converted_models}! No need to convert.") |
| 42 | + if not quantized_models: |
| 43 | + sp.run(['./quantize', converted_models[0], known_args.quant_method] + unknown_args, check=True) |
| 44 | + else: |
| 45 | + print( |
| 46 | + f"Quantized models found {quantized_models}! No need to quantize.") |
| 47 | +exit() |
0 commit comments