codybum commited on
Commit
42905ab
·
verified ·
1 Parent(s): cccec22

Upload testing/convert_gpt2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. testing/convert_gpt2.py +33 -0
testing/convert_gpt2.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Correct HF->GGUF f16 conversion for the rebuilt v4 ByteLevel tokenizer:
2
+ force pre='gpt-2' (verified full token parity) instead of the wrong 'sarvam-moe'.
3
+ Keeps the BYTE-retype patch for <0xXX> byte-fallback tokens."""
4
+ import os, sys, re, gguf
5
+ LL = "/workspace/llm/llama.cpp"
6
+ SRC = "/workspace/llm/kos-v4-conv"
7
+ OUT = "/workspace/llm/kos-v4-gguf/kosv4-f16.gguf"
8
+ sys.path.insert(0, LL); os.chdir(LL)
9
+ from conversion.base import TextModel
10
+
11
+ def _pre(self, tokenizer):
12
+ return "gpt-2"
13
+ TextModel.get_vocab_base_pre = _pre
14
+
15
+ _byte = re.compile(r"^<0x[0-9A-Fa-f]{2}>$")
16
+ _orig_base = TextModel.get_vocab_base
17
+ def _base_patched(self):
18
+ tokens, toktypes, tokpre = _orig_base(self)
19
+ n = 0
20
+ for i, t in enumerate(tokens):
21
+ if _byte.match(t) and toktypes[i] != gguf.TokenType.BYTE:
22
+ toktypes[i] = gguf.TokenType.BYTE; n += 1
23
+ if n: print(f"[patch] retyped {n} byte-fallback tokens as BYTE", file=sys.stderr)
24
+ return tokens, toktypes, tokpre
25
+ TextModel.get_vocab_base = _base_patched
26
+
27
+ import convert_hf_to_gguf as conv
28
+ sys.argv = ["convert_hf_to_gguf.py", "--outfile", OUT, "--outtype", "f16", SRC]
29
+ try:
30
+ conv.main()
31
+ except SystemExit as e:
32
+ if e.code not in (None, 0): raise
33
+ print("[done] wrote", OUT)