Skip to content

Commit 92d0b75

Browse files
authored
convert : fix python 3.8 support, modernize type annotations (ggml-org#2916)
* convert : fix python 3.8 support * convert : sort imports * convert : fix required parameters in convert-llama-ggmlv3-to-gguf * convert : fix mypy errors in convert-llama-ggmlv3-to-gguf * convert : use PEP 585 generics and PEP 604 unions Now that we have `from __future__ import annotations`, we can use this modern syntax in Python 3.7 instead of restricting support to Python 3.9 or 3.10 respectively. * gguf.py : a tuple is already a tuple * add mypy.ini * convert : add necessary `type: ignore` comments * gguf-py: bump version
1 parent 8afe228 commit 92d0b75

10 files changed

+188
-163
lines changed

convert-falcon-hf-to-gguf.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#!/usr/bin/env python3
22
# HF falcon--> gguf conversion
33

4-
import gguf
4+
from __future__ import annotations
5+
6+
import argparse
7+
import json
58
import os
6-
import sys
79
import struct
8-
import json
10+
import sys
11+
from pathlib import Path
12+
from typing import Any
13+
14+
import gguf
915
import numpy as np
1016
import torch
11-
import argparse
17+
from transformers import AutoTokenizer # type: ignore[import]
1218

13-
from typing import Any, List
14-
from pathlib import Path
15-
from transformers import AutoTokenizer
1619

1720
def bytes_to_unicode():
1821
# ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py
@@ -114,9 +117,9 @@ def parse_args() -> argparse.Namespace:
114117

115118
print("gguf: get tokenizer metadata")
116119

117-
tokens: List[bytearray] = []
118-
scores: List[float] = []
119-
toktypes: List[int] = []
120+
tokens: list[bytearray] = []
121+
scores: list[float] = []
122+
toktypes: list[int] = []
120123

121124
tokenizer_json_file = dir_model / 'tokenizer.json'
122125
if not tokenizer_json_file.is_file():

convert-gptneox-hf-to-gguf.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#!/usr/bin/env python3
22
# HF gptneox--> gguf conversion
33

4-
import gguf
4+
from __future__ import annotations
5+
6+
import argparse
7+
import json
58
import os
6-
import sys
79
import struct
8-
import json
10+
import sys
11+
from pathlib import Path
12+
from typing import Any
13+
14+
import gguf
915
import numpy as np
1016
import torch
11-
import argparse
12-
13-
from typing import Any, List
14-
from pathlib import Path
15-
from transformers import AutoTokenizer
17+
from transformers import AutoTokenizer # type: ignore[import]
1618

1719
# ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py
1820

@@ -112,7 +114,7 @@ def parse_args() -> argparse.Namespace:
112114

113115
print("gguf: get tokenizer metadata")
114116

115-
tokens: List[bytearray] = []
117+
tokens: list[bytearray] = []
116118

117119
tokenizer_json_file = dir_model / 'tokenizer.json'
118120
if not tokenizer_json_file.is_file():

convert-llama-7b-pth-to-gguf.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
# Only models with a single datafile are supported, like 7B
44
# HF files required in the model dir: config.json tokenizer_config.json tokenizer.json tokenizer.model
55

6-
import gguf
6+
from __future__ import annotations
7+
8+
import argparse
9+
import json
710
import os
8-
import sys
911
import struct
10-
import json
12+
import sys
13+
from pathlib import Path
14+
from typing import TYPE_CHECKING, Any
15+
16+
import gguf
1117
import numpy as np
1218
import torch
13-
import argparse
19+
from sentencepiece import SentencePieceProcessor # type: ignore[import]
1420

15-
from typing import Any, List, TypeAlias
16-
from pathlib import Path
17-
from sentencepiece import SentencePieceProcessor
21+
if TYPE_CHECKING:
22+
from typing import TypeAlias
1823

19-
#NDArray = np.ndarray[Any, Any]
20-
# compatible with python < 3.9
21-
NDArray: 'TypeAlias' = 'np.ndarray[Any, Any]'
24+
NDArray: TypeAlias = 'np.ndarray[Any, Any]'
2225

2326

2427
def count_model_parts(dir_model: Path) -> int:
@@ -129,9 +132,9 @@ def parse_args() -> argparse.Namespace:
129132

130133
print("gguf: get tokenizer metadata")
131134

132-
tokens: List[bytes] = []
133-
scores: List[float] = []
134-
toktypes: List[int] = []
135+
tokens: list[bytes] = []
136+
scores: list[float] = []
137+
toktypes: list[int] = []
135138

136139
tokenizer_model_file = dir_model / 'tokenizer.model'
137140
if not tokenizer_model_file.is_file():

convert-llama-ggmlv3-to-gguf.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/env python3
2-
import sys, struct, math, argparse
3-
from pathlib import Path
2+
from __future__ import annotations
43

5-
import numpy as np
4+
import argparse
5+
import math
6+
import struct
7+
import sys
8+
from pathlib import Path
69

710
import gguf
11+
import numpy as np
812

913
# Note: Does not support GGML_QKK_64
1014
QK_K = 256
@@ -72,7 +76,7 @@ def load(self, data, offset, n_vocab):
7276
class Tensor:
7377
def __init__(self):
7478
self.name = None
75-
self.dims = ()
79+
self.dims: tuple[int, ...] = ()
7680
self.dtype = None
7781
self.start_offset = 0
7882
self.len_bytes = np.int64(0)
@@ -119,7 +123,7 @@ def load(self, data, offset):
119123
offset += hp.load(data, offset)
120124
vocab = Vocab()
121125
offset += vocab.load(data, offset, hp.n_vocab)
122-
tensors = []
126+
tensors: list[Tensor] = []
123127
tensor_map = {}
124128
while offset < len(data):
125129
tensor = Tensor()
@@ -305,8 +309,8 @@ def handle_metadata(cfg, hp):
305309

306310
def handle_args():
307311
parser = argparse.ArgumentParser(description = 'Convert GGMLv3 models to GGUF')
308-
parser.add_argument('--input', '-i', type = Path, help = 'Input GGMLv3 filename')
309-
parser.add_argument('--output', '-o', type = Path, help ='Output GGUF filename')
312+
parser.add_argument('--input', '-i', type = Path, required = True, help = 'Input GGMLv3 filename')
313+
parser.add_argument('--output', '-o', type = Path, required = True, help ='Output GGUF filename')
310314
parser.add_argument('--name', help = 'Set model name')
311315
parser.add_argument('--desc', help = 'Set model description')
312316
parser.add_argument('--gqa', type = int, default = 1, help = 'grouped-query attention factor (use 8 for LLaMA2 70B)')

convert-llama-hf-to-gguf.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#!/usr/bin/env python3
22
# HF llama --> gguf conversion
33

4-
import gguf
4+
from __future__ import annotations
5+
6+
import argparse
7+
import json
58
import os
6-
import sys
79
import struct
8-
import json
10+
import sys
11+
from pathlib import Path
12+
from typing import TYPE_CHECKING, Any
13+
14+
import gguf
915
import numpy as np
1016
import torch
11-
import argparse
17+
from sentencepiece import SentencePieceProcessor # type: ignore[import]
1218

13-
from typing import Any, List, Optional, TypeAlias
14-
from pathlib import Path
15-
from sentencepiece import SentencePieceProcessor
19+
if TYPE_CHECKING:
20+
from typing import TypeAlias
1621

17-
#NDArray = np.ndarray[Any, Any]
18-
# compatible with python < 3.9
19-
NDArray: 'TypeAlias' = 'np.ndarray[Any, Any]'
22+
NDArray: TypeAlias = 'np.ndarray[Any, Any]'
2023

2124
# reverse HF permute back to original pth layout
2225
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py
2326

2427

25-
def reverse_hf_permute(weights: NDArray, n_head: int, n_kv_head: Optional[int] = None) -> NDArray:
28+
def reverse_hf_permute(weights: NDArray, n_head: int, n_kv_head: int | None = None) -> NDArray:
2629
if n_kv_head is not None and n_head != n_kv_head:
2730
n_head //= n_kv_head
2831

@@ -136,9 +139,9 @@ def parse_args() -> argparse.Namespace:
136139

137140
print("gguf: get tokenizer metadata")
138141

139-
tokens: List[bytes] = []
140-
scores: List[float] = []
141-
toktypes: List[int] = []
142+
tokens: list[bytes] = []
143+
scores: list[float] = []
144+
toktypes: list[int] = []
142145

143146
tokenizer_model_file = dir_model / 'tokenizer.model'
144147
if not tokenizer_model_file.is_file():

convert-lora-to-ggml.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env python3
2+
from __future__ import annotations
3+
24
import json
35
import os
46
import re
57
import struct
68
import sys
7-
from typing import Any, Dict, Sequence, BinaryIO
9+
from typing import Any, BinaryIO, Sequence
810

911
import numpy as np
1012
import torch
1113

12-
NUMPY_TYPE_TO_FTYPE: Dict[str, int] = {"float32": 0, "float16": 1}
14+
NUMPY_TYPE_TO_FTYPE: dict[str, int] = {"float32": 0, "float16": 1}
1315

1416

1517
HF_SUBLAYER_TO_GGML = {
@@ -46,7 +48,7 @@ def translate_tensor_name(t: str) -> str:
4648
sys.exit(1)
4749

4850

49-
def write_file_header(fout: BinaryIO, params: Dict[str, Any]) -> None:
51+
def write_file_header(fout: BinaryIO, params: dict[str, Any]) -> None:
5052
fout.write(b"ggla"[::-1]) # magic (ggml lora)
5153
fout.write(struct.pack("i", 1)) # file version
5254
fout.write(struct.pack("i", params["r"]))

0 commit comments

Comments
 (0)