From 4320055b22ca4301d17ace8fc7e4204a98d9e1a0 Mon Sep 17 00:00:00 2001 From: KerfuffleV2 Date: Thu, 31 Aug 2023 04:01:17 -0600 Subject: [PATCH 1/5] scripts: Use local gguf when running from repo --- convert-falcon-hf-to-gguf.py | 5 ++++- convert-gptneox-hf-to-gguf.py | 4 ++++ convert-llama-ggmlv3-to-gguf.py | 6 +++++- convert.py | 6 +++++- .../convert-train-checkpoint-to-gguf.py | 8 +++++++- 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index ec786ff67c096..acd07caddb5c8 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -11,11 +11,14 @@ from pathlib import Path from typing import Any -import gguf import numpy as np import torch from transformers import AutoTokenizer # type: ignore[import] +if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): + sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +import gguf + def bytes_to_unicode(): # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index 852123d99e5be..dcad523f4d8c5 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -16,6 +16,10 @@ import torch from transformers import AutoTokenizer # type: ignore[import] +if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): + sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +import gguf + # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py diff --git a/convert-llama-ggmlv3-to-gguf.py b/convert-llama-ggmlv3-to-gguf.py index 3f39bc39e14a9..2870088c4221b 100755 --- a/convert-llama-ggmlv3-to-gguf.py +++ b/convert-llama-ggmlv3-to-gguf.py @@ -7,9 +7,13 @@ import sys from pathlib import Path -import gguf import numpy as np +import os +if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): + sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +import gguf + # Note: Does not support GGML_QKK_64 QK_K = 256 # Items here are (block size, type size) diff --git a/convert.py b/convert.py index 9a39edb994c27..ba6ead707701c 100755 --- a/convert.py +++ b/convert.py @@ -25,10 +25,14 @@ from pathlib import Path from typing import IO, TYPE_CHECKING, Any, Callable, Generator, Iterable, Literal, Sequence, TypeVar -import gguf import numpy as np from sentencepiece import SentencePieceProcessor # type: ignore[import] +import os +if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): + sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +import gguf + if TYPE_CHECKING: from typing import TypeAlias diff --git a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py index 01b3ee92a5a0c..cde4e73146dc5 100644 --- a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py +++ b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py @@ -2,13 +2,19 @@ # train-text-from-scratch checkpoint --> gguf conversion import argparse -import gguf import os import struct import sys import numpy as np from pathlib import Path +if os.environ.get('NO_LOCAL_GGUF') is None: + if Path('gguf-py', 'gguf', '__init__.py').is_file(): + sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) + elif Path('..', '..', 'gguf-py', 'gguf', '__init__.py').is_file(): + sys.path.insert(1, str(Path('..', '..', 'gguf-py', 'gguf').absolute())) +import gguf + # gguf constants LLM_KV_OPTIMIZER_TYPE = "optimizer.type" LLM_KV_OPTIMIZER_TYPE_ADAM = "adam" From 489ffb2525eedd6548758dfa305c8f40b2f014e5 Mon Sep 17 00:00:00 2001 From: KerfuffleV2 Date: Thu, 31 Aug 2023 04:06:48 -0600 Subject: [PATCH 2/5] Only I could mess up something this simple. --- convert-gptneox-hf-to-gguf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index dcad523f4d8c5..3741a19ca5968 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -11,7 +11,6 @@ from pathlib import Path from typing import Any -import gguf import numpy as np import torch from transformers import AutoTokenizer # type: ignore[import] From 4b54a7e7bc197ca73353db69b95d7d8ce2e1f2f1 Mon Sep 17 00:00:00 2001 From: KerfuffleV2 Date: Thu, 31 Aug 2023 12:31:27 -0600 Subject: [PATCH 3/5] Look for local gguf module relative to running script file --- convert-falcon-hf-to-gguf.py | 5 +++-- convert-gptneox-hf-to-gguf.py | 5 +++-- convert-llama-ggmlv3-to-gguf.py | 5 +++-- convert.py | 5 +++-- .../convert-train-checkpoint-to-gguf.py | 8 +++----- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index acd07caddb5c8..b0f670fbc1ebc 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -15,8 +15,9 @@ import torch from transformers import AutoTokenizer # type: ignore[import] -if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +# Use local gguf module if available. +if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): + sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index 3741a19ca5968..cc757b64096b6 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -15,8 +15,9 @@ import torch from transformers import AutoTokenizer # type: ignore[import] -if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +# Use local gguf module if available. +if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): + sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py diff --git a/convert-llama-ggmlv3-to-gguf.py b/convert-llama-ggmlv3-to-gguf.py index 2870088c4221b..3f08801bf8fb3 100755 --- a/convert-llama-ggmlv3-to-gguf.py +++ b/convert-llama-ggmlv3-to-gguf.py @@ -10,8 +10,9 @@ import numpy as np import os -if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +# Use local gguf module if available. +if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): + sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf # Note: Does not support GGML_QKK_64 diff --git a/convert.py b/convert.py index ba6ead707701c..b6641ea966e73 100755 --- a/convert.py +++ b/convert.py @@ -29,8 +29,9 @@ from sentencepiece import SentencePieceProcessor # type: ignore[import] import os -if os.environ.get('NO_LOCAL_GGUF') is None and Path('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) +# Use local gguf module if available. +if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): + sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf if TYPE_CHECKING: diff --git a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py index cde4e73146dc5..0cd2892a37294 100644 --- a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py +++ b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py @@ -8,11 +8,9 @@ import numpy as np from pathlib import Path -if os.environ.get('NO_LOCAL_GGUF') is None: - if Path('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path('gguf-py', 'gguf').absolute())) - elif Path('..', '..', 'gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path('..', '..', 'gguf-py', 'gguf').absolute())) +# Use local gguf module if available. +if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf', '__init__.py')).is_file(): + sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf'))) import gguf # gguf constants From 31fc2bdebe73b4de9a89cd276f8b220ede888943 Mon Sep 17 00:00:00 2001 From: KerfuffleV2 Date: Thu, 31 Aug 2023 12:37:56 -0600 Subject: [PATCH 4/5] Life is suffering. --- convert-falcon-hf-to-gguf.py | 2 +- convert-gptneox-hf-to-gguf.py | 2 +- convert-llama-ggmlv3-to-gguf.py | 2 +- convert.py | 2 +- .../train-text-from-scratch/convert-train-checkpoint-to-gguf.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index b0f670fbc1ebc..99abf078cfe90 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -16,7 +16,7 @@ from transformers import AutoTokenizer # type: ignore[import] # Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): +if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index cc757b64096b6..abbddf3fb7405 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -16,7 +16,7 @@ from transformers import AutoTokenizer # type: ignore[import] # Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): +if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf diff --git a/convert-llama-ggmlv3-to-gguf.py b/convert-llama-ggmlv3-to-gguf.py index 3f08801bf8fb3..43bc3f7657ed3 100755 --- a/convert-llama-ggmlv3-to-gguf.py +++ b/convert-llama-ggmlv3-to-gguf.py @@ -11,7 +11,7 @@ import os # Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): +if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf diff --git a/convert.py b/convert.py index b6641ea966e73..78cbb7b7ac33f 100755 --- a/convert.py +++ b/convert.py @@ -30,7 +30,7 @@ import os # Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py')).is_file(): +if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) import gguf diff --git a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py index 0cd2892a37294..46b48cb091a18 100644 --- a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py +++ b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py @@ -9,7 +9,7 @@ from pathlib import Path # Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and (Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf', '__init__.py')).is_file(): +if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf', '__init__.py').is_file(): sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf'))) import gguf From 459d3e799db20b83e380da82f1d79735cd117161 Mon Sep 17 00:00:00 2001 From: KerfuffleV2 Date: Thu, 31 Aug 2023 15:26:09 -0600 Subject: [PATCH 5/5] Just add local gguf to path unless environ forbids it --- convert-falcon-hf-to-gguf.py | 5 ++--- convert-gptneox-hf-to-gguf.py | 5 ++--- convert-llama-ggmlv3-to-gguf.py | 5 ++--- convert.py | 5 ++--- .../convert-train-checkpoint-to-gguf.py | 5 ++--- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index 99abf078cfe90..271e589728e64 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -15,9 +15,8 @@ import torch from transformers import AutoTokenizer # type: ignore[import] -# Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) +if 'NO_LOCAL_GGUF' not in os.environ: + sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) import gguf diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index abbddf3fb7405..b9c8b4607e593 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -15,9 +15,8 @@ import torch from transformers import AutoTokenizer # type: ignore[import] -# Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) +if 'NO_LOCAL_GGUF' not in os.environ: + sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) import gguf # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py diff --git a/convert-llama-ggmlv3-to-gguf.py b/convert-llama-ggmlv3-to-gguf.py index 43bc3f7657ed3..08ba0c490cd1e 100755 --- a/convert-llama-ggmlv3-to-gguf.py +++ b/convert-llama-ggmlv3-to-gguf.py @@ -10,9 +10,8 @@ import numpy as np import os -# Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) +if 'NO_LOCAL_GGUF' not in os.environ: + sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) import gguf # Note: Does not support GGML_QKK_64 diff --git a/convert.py b/convert.py index 78cbb7b7ac33f..5cc3f6e66f6bb 100755 --- a/convert.py +++ b/convert.py @@ -29,9 +29,8 @@ from sentencepiece import SentencePieceProcessor # type: ignore[import] import os -# Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('gguf-py', 'gguf'))) +if 'NO_LOCAL_GGUF' not in os.environ: + sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) import gguf if TYPE_CHECKING: diff --git a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py index 46b48cb091a18..a527d615304b8 100644 --- a/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py +++ b/examples/train-text-from-scratch/convert-train-checkpoint-to-gguf.py @@ -8,9 +8,8 @@ import numpy as np from pathlib import Path -# Use local gguf module if available. -if 'NO_LOCAL_GGUF' not in os.environ and Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf', '__init__.py').is_file(): - sys.path.insert(1, str(Path(__file__).parent.absolute().joinpath('..', '..', 'gguf-py', 'gguf'))) +if 'NO_LOCAL_GGUF' not in os.environ: + sys.path.insert(1, str(Path(__file__).parent / '..' / '..' / 'gguf-py' / 'gguf')) import gguf # gguf constants