Skip to content

Commit 747aa7b

Browse files
committed
refactor: optimize GGUF imports
- optimize imports in GGUF conversion utilities - rename gguf library modules - update .gitignore and build workflow
1 parent 3804da0 commit 747aa7b

17 files changed

+605
-762
lines changed

Diff for: .github/workflows/build.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ jobs:
6161
if: matrix.os == 'windows-latest'
6262
run: |
6363
$distPath = if ("${{ github.event.inputs.build_type }}" -eq "RELEASE") { "build\release\dist" } else { "build\dev\dist" }
64-
New-Item -ItemType Directory -Force -Path "$distPath\src\gguf-py"
65-
Copy-Item -Path "src\gguf-py\*" -Destination "$distPath\src\gguf-py" -Recurse
64+
New-Item -ItemType Directory -Force -Path "$distPath\src\gguf"
65+
Copy-Item -Path "src\gguf\*" -Destination "$distPath\src\gguf" -Recurse
6666
Copy-Item -Path "src\convert_hf_to_gguf.py" -Destination "$distPath\src"
6767
Copy-Item -Path "src\convert_lora_to_gguf.py" -Destination "$distPath\src"
6868
Copy-Item -Path "src\convert_lora_to_ggml.py" -Destination "$distPath\src"
@@ -72,8 +72,8 @@ jobs:
7272
if: matrix.os != 'windows-latest'
7373
run: |
7474
distPath=$(if [ "${{ github.event.inputs.build_type }}" = "RELEASE" ]; then echo "build/release/dist"; else echo "build/dev/dist"; fi)
75-
mkdir -p $distPath/src/gguf-py
76-
cp -R src/gguf-py/* $distPath/src/gguf-py/
75+
mkdir -p $distPath/src/gguf
76+
cp -R src/gguf/* $distPath/src/gguf/
7777
cp src/convert_hf_to_gguf.py $distPath/src/
7878
cp src/convert_lora_to_gguf.py $distPath/src/
7979
cp src/convert_lora_to_ggml.py $distPath/src/

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ __pycache__/
2020
!src/
2121
src/*
2222
!src/*.py
23+
!src/gguf
24+
src/gguf/*
25+
!src/gguf/*.py
2326

2427
# Allow docs folder and its .py files
2528
!docs/

Diff for: src/AutoGGUF.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import importlib
22
import json
33
import shutil
4-
import urllib.request
54
import urllib.error
5+
import urllib.request
66
from datetime import datetime
77
from functools import partial, wraps
88
from typing import Any, Dict, List, Tuple
@@ -24,10 +24,10 @@
2424
from error_handling import handle_error, show_error
2525
from imports_and_globals import (
2626
ensure_directory,
27+
load_dotenv,
2728
open_file_safe,
2829
resource_path,
2930
show_about,
30-
load_dotenv,
3131
)
3232

3333

@@ -41,21 +41,18 @@ def wrapper(self, *args, **kwargs):
4141

4242
# Length check
4343
if len(value) > 1024:
44-
show_error(f"{field} exceeds maximum length")
44+
show_error(self.logger, f"{field} exceeds maximum length")
4545

4646
# Normalize path
4747
normalized_path = os.path.normpath(value)
4848

4949
# Check for path traversal attempts
5050
if ".." in normalized_path:
51-
show_error(f"Invalid path in {field}")
51+
show_error(self.logger, f"Invalid path in {field}")
5252

5353
# Disallow control characters and null bytes
5454
if re.search(r"[\x00-\x1f\x7f]", value):
55-
show_error(f"Invalid characters in {field}")
56-
57-
# Update the field with normalized path
58-
getattr(self, field).setText(normalized_path)
55+
show_error(self.logger, f"Invalid characters in {field}")
5956

6057
return func(self, *args, **kwargs)
6158

Diff for: src/convert_hf_to_gguf.py

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
if TYPE_CHECKING:
3131
from torch import Tensor
3232

33-
if "NO_LOCAL_GGUF" not in os.environ:
34-
sys.path.insert(1, str(Path(__file__).parent / "gguf-py"))
3533
import gguf
3634

3735
logger = logging.getLogger("hf-to-gguf")

Diff for: src/convert_lora_to_ggml.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
from __future__ import annotations
22

3-
import logging
43
import json
4+
import logging
55
import os
66
import struct
77
import sys
8-
from pathlib import Path
9-
from typing import Any, BinaryIO, Sequence
8+
from typing import BinaryIO
109

1110
import numpy as np
1211
import torch
1312

14-
if "NO_LOCAL_GGUF" not in os.environ:
15-
sys.path.insert(1, str(Path(__file__).parent / "gguf-py" / "gguf"))
16-
import gguf
13+
from gguf.constants import *
14+
from gguf.tensor_mapping import *
1715

1816
logging.basicConfig(level=logging.DEBUG)
1917
logger = logging.getLogger("lora-to-gguf")
@@ -51,19 +49,14 @@ def write_tensor_header(
5149
fout.seek((fout.tell() + 31) & -32)
5250

5351

54-
def pyinstaller_include():
55-
# PyInstaller import
56-
pass
57-
58-
5952
if __name__ == "__main__":
6053
if len(sys.argv) < 2:
6154
logger.info(f"Usage: python {sys.argv[0]} <path> <output_path> [arch]")
6255
logger.info(
6356
"Path must contain HuggingFace PEFT LoRA files 'adapter_config.json' and 'adapter_model.bin'"
6457
)
6558
logger.info(
66-
f"Arch must be one of {list(gguf.MODEL_ARCH_NAMES.values())} (default: llama)"
59+
f"Arch must be one of {list(MODEL_ARCH_NAMES.values())} (default: llama)"
6760
)
6861
sys.exit(1)
6962

@@ -82,14 +75,14 @@ def pyinstaller_include():
8275

8376
arch_name = sys.argv[3] if len(sys.argv) == 4 else "llama"
8477

85-
if arch_name not in gguf.MODEL_ARCH_NAMES.values():
78+
if arch_name not in MODEL_ARCH_NAMES.values():
8679
logger.error(f"Error: unsupported architecture {arch_name}")
8780
sys.exit(1)
8881

89-
arch = list(gguf.MODEL_ARCH_NAMES.keys())[
90-
list(gguf.MODEL_ARCH_NAMES.values()).index(arch_name)
82+
arch = list(MODEL_ARCH_NAMES.keys())[
83+
list(MODEL_ARCH_NAMES.values()).index(arch_name)
9184
]
92-
name_map = gguf.TensorNameMap(arch, 500)
85+
name_map = TensorNameMap(arch, 500)
9386

9487
with open(input_json, "r") as f:
9588
params = json.load(f)

Diff for: src/convert_lora_to_gguf.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
if TYPE_CHECKING:
2525
from torch import Tensor
2626

27-
if "NO_LOCAL_GGUF" not in os.environ:
28-
sys.path.insert(1, str(Path(__file__).parent / "gguf-py"))
29-
import gguf
27+
from gguf.constants import *
3028

3129
from convert_hf_to_gguf import LazyTorchTensor, Model
3230

Diff for: src/gguf-py/gguf/gguf.py

-15
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)