This commit is contained in:
Jared Van Bortel 2023-11-09 12:11:41 -05:00
parent bca0962575
commit 233cb0741f
3 changed files with 10 additions and 18 deletions

24
convert-gguf-endian.py Normal file → Executable file
View file

@ -1,13 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import annotations from __future__ import annotations
import os
import sys import sys
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
import os
if "NO_LOCAL_GGUF" not in os.environ: if "NO_LOCAL_GGUF" not in os.environ:
sys.path.insert(1, str(Path(__file__).parent / "gguf-py")) sys.path.insert(1, str(Path(__file__).parent / "gguf-py"))
import gguf import gguf
@ -31,9 +30,7 @@ def convert_byteorder(filename: str, order: str) -> None:
file_endian = host_endian file_endian = host_endian
if order == "native": if order == "native":
order = host_endian order = host_endian
print( print(f"* Host is {host_endian.upper()} endian, GGUF file seems to be {file_endian.upper()} endian")
f"* Host is {host_endian.upper()} endian, GGUF file seems to be {file_endian.upper()} endian"
)
if file_endian == order: if file_endian == order:
print(f"* File is already {order.upper()} endian. Nothing to do.") print(f"* File is already {order.upper()} endian. Nothing to do.")
sys.exit(0) sys.exit(0)
@ -44,15 +41,11 @@ def convert_byteorder(filename: str, order: str) -> None:
gguf.GGMLQuantizationType.F16, gguf.GGMLQuantizationType.F16,
gguf.GGMLQuantizationType.Q8_0, gguf.GGMLQuantizationType.Q8_0,
): ):
raise ValueError( raise ValueError(f"Cannot handle type {tensor.tensor_type.name} for tensor {repr(tensor.name)}")
f"Cannot handle type {tensor.tensor_type.name} for tensor {repr(tensor.name)}"
)
print(f"* Preparing to convert from {file_endian.upper()} to {order.upper()}") print(f"* Preparing to convert from {file_endian.upper()} to {order.upper()}")
print("\n*** Warning *** Warning *** Warning **") print("\n*** Warning *** Warning *** Warning **")
print("* This conversion process may damage the file. Ensure you have a backup.") print("* This conversion process may damage the file. Ensure you have a backup.")
print( print("* The file will be modified immediately, so if conversion fails or is interrupted")
"* The file will be modified immediately, so if conversion fails or is interrupted"
)
print("* the file will be corrupted. If you are positive then type YES:") print("* the file will be corrupted. If you are positive then type YES:")
response = input("YES, I am sure> ") response = input("YES, I am sure> ")
if response != "YES": if response != "YES":
@ -60,15 +53,14 @@ def convert_byteorder(filename: str, order: str) -> None:
sys.exit(0) sys.exit(0)
print(f"\n* Converting fields ({len(reader.fields)})") print(f"\n* Converting fields ({len(reader.fields)})")
for idx, field in enumerate(reader.fields.values()): for idx, field in enumerate(reader.fields.values()):
print( print(f"- {idx:4}: Converting field {repr(field.name)}, part count: {len(field.parts)}")
f"- {idx:4}: Converting field {repr(field.name)}, part count: {len(field.parts)}"
)
for part in field.parts: for part in field.parts:
part.byteswap(inplace=True) part.byteswap(inplace=True)
print(f"\n* Converting tensors ({len(reader.tensors)})") print(f"\n* Converting tensors ({len(reader.tensors)})")
for idx, tensor in enumerate(reader.tensors): for idx, tensor in enumerate(reader.tensors):
print( print(
f" - {idx:4}: Converting tensor {repr(tensor.name)}, type={tensor.tensor_type.name}, elements={tensor.n_elements}... ", f" - {idx:4}: Converting tensor {repr(tensor.name)}, type={tensor.tensor_type.name}, "
f"elements={tensor.n_elements}... ",
end="", end="",
) )
tensor_type = tensor.tensor_type tensor_type = tensor.tensor_type
@ -84,7 +76,7 @@ def convert_byteorder(filename: str, order: str) -> None:
for block_num in range(n_blocks): for block_num in range(n_blocks):
block_offs = block_num * block_size block_offs = block_num * block_size
# I know I said f16, but it doesn't matter here - any simple 16 bit type works. # I know I said f16, but it doesn't matter here - any simple 16 bit type works.
delta = tensor.data[block_offs : block_offs + 2].view(dtype=np.uint16) delta = tensor.data[block_offs:block_offs + 2].view(dtype=np.uint16)
delta.byteswap(inplace=True) delta.byteswap(inplace=True)
if block_num % 100000 == 0: if block_num % 100000 == 0:
print(f"[{(n_blocks - block_num) // 1000}K]", end="") print(f"[{(n_blocks - block_num) // 1000}K]", end="")

View file

@ -2,7 +2,7 @@ from __future__ import annotations
import sys import sys
from enum import IntEnum, StrEnum, auto from enum import IntEnum, StrEnum, auto
from typing import Any, Type from typing import Any
# #
# constants # constants

View file

@ -1,4 +1,4 @@
import gguf import gguf # noqa: F401
# TODO: add tests # TODO: add tests