(args_in: list[str] | None = None)
| 1189 | |
| 1190 | |
| 1191 | def main(args_in: list[str] | None = None) -> None: |
| 1192 | output_choices = ["f32", "f16"] |
| 1193 | if np.uint32(1) == np.uint32(1).newbyteorder("<"): |
| 1194 | # We currently only support Q8_0 output on little endian systems. |
| 1195 | output_choices.append("q8_0") |
| 1196 | parser = argparse.ArgumentParser(description="Convert a LLaMa model to a GGML compatible file") |
| 1197 | parser.add_argument("--dump", action="store_true", help="don't convert, just show what's in the model") |
| 1198 | parser.add_argument("--dump-single", action="store_true", help="don't convert, just show what's in a single model file") |
| 1199 | parser.add_argument("--vocab-only", action="store_true", help="extract only the vocab") |
| 1200 | parser.add_argument("--outtype", choices=output_choices, help="output format - note: q8_0 may be very slow (default: f16 or f32 based on input)", default="f16") |
| 1201 | parser.add_argument("--vocab-dir", type=Path, help="directory containing tokenizer.model, if separate from model file") |
| 1202 | parser.add_argument("--outfile", type=Path, help="path to write to; default: based on input") |
| 1203 | parser.add_argument("--ctx", type=int, help="model training context (default: based on input)") |
| 1204 | parser.add_argument("--concurrency", type=int, help=f"concurrency used for conversion (default: {DEFAULT_CONCURRENCY})", default = DEFAULT_CONCURRENCY) |
| 1205 | parser.add_argument("--bigendian", action="store_true", help="model is executed on big endian machine") |
| 1206 | parser.add_argument("--vocabtype", choices=["spm", "bpe"], help="vocab format (default: spm)", default="spm") |
| 1207 | parser.add_argument("model", type=Path, help="directory containing model file, or model file itself (*.pth, *.pt, *.bin, *.safetensors)") |
| 1208 | parser.add_argument("sparse_predictor", type=Path, help="predictors for sparse FFN inference") |
| 1209 | |
| 1210 | args = parser.parse_args(args_in) |
| 1211 | |
| 1212 | try: |
| 1213 | with open(args.model / "config.json", "r", encoding="utf-8") as f: |
| 1214 | hf_config = json.load(f) |
| 1215 | if model_type := hf_config.get("model_type") not in ("llama", "bamboo"): |
| 1216 | # invoke another script to convert other models |
| 1217 | print(f"Model architecture {model_type} is not supported by this `convert.py`. Trying with `convert-hf-to-powerinfer-gguf.py`...") |
| 1218 | script_path = Path(__file__).resolve().parent / "convert-hf-to-powerinfer-gguf.py" |
| 1219 | subprocess.run(["python3", str(script_path.absolute())] + sys.argv[1:]) |
| 1220 | return |
| 1221 | except FileNotFoundError: |
| 1222 | print("Could not find config.json under the original model directory. ", file=sys.stderr) |
| 1223 | sys.exit(1) |
| 1224 | |
| 1225 | if args.dump_single: |
| 1226 | model_plus = lazy_load_file(args.model) |
| 1227 | do_dump_model(model_plus) |
| 1228 | return |
| 1229 | |
| 1230 | if not args.vocab_only: |
| 1231 | model_plus = load_some_model(args.model) |
| 1232 | params = Params.load(model_plus) |
| 1233 | mlp_predictor_plus = load_predictor_model(args.sparse_predictor) |
| 1234 | params.predictor_params = PredictorParams.load(mlp_predictor_plus) |
| 1235 | model_plus = merge_multifile_models([model_plus, mlp_predictor_plus]) |
| 1236 | else: |
| 1237 | model_plus = ModelPlus(model = {}, paths = [args.model / 'dummy'], format = 'none', vocab = None) |
| 1238 | params = Params.load(model_plus) |
| 1239 | |
| 1240 | if args.dump: |
| 1241 | do_dump_model(model_plus) |
| 1242 | return |
| 1243 | endianess = gguf.GGUFEndian.LITTLE |
| 1244 | if args.bigendian: |
| 1245 | endianess = gguf.GGUFEndian.BIG |
| 1246 | |
| 1247 | if params.n_ctx == -1: |
| 1248 | if args.ctx is None: |
no test coverage detected