Skip to content

Commit b43ebde

Browse files
authored
convert : partially revert PR ggml-org#4818 (ggml-org#5041)
1 parent 97c1549 commit b43ebde

6 files changed

+241
-433
lines changed

convert-hf-to-gguf.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from enum import IntEnum
1212
from pathlib import Path
13-
from typing import TYPE_CHECKING, Any, ContextManager, Iterator, cast, Optional
13+
from typing import TYPE_CHECKING, Any, ContextManager, Iterator, cast
1414

1515
import numpy as np
1616
import torch
@@ -487,7 +487,8 @@ def write_tensors(self):
487487
# map tensor names
488488
if "scales" in name:
489489
new_name = tensor_map.get_name(name, try_suffixes=(".weight", ".bias", ".scales"))
490-
new_name = new_name.replace("scales", "act.scales")
490+
if new_name is not None:
491+
new_name = new_name.replace("scales", "act.scales")
491492
else:
492493
new_name = tensor_map.get_name(name, try_suffixes=(".weight", ".bias"))
493494
if new_name is None:
@@ -904,7 +905,7 @@ def token_bytes_to_string(b):
904905
return ''.join([byte_encoder[ord(char)] for char in b.decode('latin-1')])
905906

906907
@staticmethod
907-
def bpe(mergeable_ranks: dict[bytes, int], token: bytes, max_rank: Optional[int] = None) -> list[bytes]:
908+
def bpe(mergeable_ranks: dict[bytes, int], token: bytes, max_rank: int | None = None) -> list[bytes]:
908909
parts = [bytes([b]) for b in token]
909910
while True:
910911
min_idx = None
@@ -1285,7 +1286,7 @@ def main() -> None:
12851286

12861287
if args.awq_path:
12871288
sys.path.insert(1, str(Path(__file__).parent / 'awq-py'))
1288-
from awq.apply_awq import add_scale_weights
1289+
from awq.apply_awq import add_scale_weights # type: ignore[import-not-found]
12891290
tmp_model_path = args.model / "weighted_model"
12901291
dir_model = tmp_model_path
12911292
if tmp_model_path.is_dir():

convert-llama-ggml-to-gguf.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
from __future__ import annotations
33

44
import argparse
5+
import os
56
import struct
67
import sys
78
from enum import IntEnum
89
from pathlib import Path
910

1011
import numpy as np
1112

12-
import os
1313
if 'NO_LOCAL_GGUF' not in os.environ:
1414
sys.path.insert(1, str(Path(__file__).parent / 'gguf-py'))
1515
import gguf
@@ -371,15 +371,11 @@ def handle_metadata(cfg, hp):
371371
params = convert.Params.loadOriginalParamsJson(fakemodel, orig_config_path)
372372
else:
373373
raise ValueError('Unable to load metadata')
374-
vocab = convert.load_vocab(
375-
cfg.vocab_dir if cfg.vocab_dir is not None else cfg.model_metadata_dir,
376-
cfg.vocabtype)
377-
# FIXME: Respect cfg.vocab_dir?
378-
svocab = gguf.SpecialVocab(cfg.model_metadata_dir,
379-
load_merges = cfg.vocabtype == 'bpe',
380-
n_vocab = vocab.vocab_size)
374+
vocab_path = Path(cfg.vocab_dir if cfg.vocab_dir is not None else cfg.model_metadata_dir)
375+
vocab_factory = convert.VocabFactory(vocab_path)
376+
vocab, special_vocab = vocab_factory.load_vocab(cfg.vocabtype, cfg.model_metadata_dir)
381377
convert.check_vocab_size(params, vocab)
382-
return (params, vocab, svocab)
378+
return params, vocab, special_vocab
383379

384380

385381
def handle_args():

convert-lora-to-ggml.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
import os
66
import struct
77
import sys
8+
from pathlib import Path
89
from typing import Any, BinaryIO, Sequence
910

1011
import numpy as np
1112
import torch
1213

13-
from pathlib import Path
1414
if 'NO_LOCAL_GGUF' not in os.environ:
1515
sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf'))
1616
import gguf
1717

18-
1918
NUMPY_TYPE_TO_FTYPE: dict[str, int] = {"float32": 0, "float16": 1}
2019

2120

convert-persimmon-to-gguf.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python3
2-
import torch
2+
import argparse
33
import os
4-
from pprint import pprint
54
import sys
6-
import argparse
75
from pathlib import Path
6+
from pprint import pprint
7+
8+
import torch
89
from sentencepiece import SentencePieceProcessor
10+
911
if 'NO_LOCAL_GGUF' not in os.environ:
1012
sys.path.insert(1, str(Path(__file__).parent / 'gguf-py'))
1113
import gguf
@@ -69,7 +71,7 @@ def main():
6971
persimmon_model = torch.load(args.ckpt_path)
7072
hparams = persimmon_model['args']
7173
pprint(hparams)
72-
tensors = {}
74+
tensors: dict[str, torch.Tensor] = {}
7375
_flatten_dict(persimmon_model['model'], tensors, None)
7476

7577
arch = gguf.MODEL_ARCH.PERSIMMON

0 commit comments

Comments
 (0)