Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/examples/gpt-2/convert-cerebras-to-ggml.py # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / examples / gpt-2 / convert-cerebras-to-ggml.py` 183 lines · 6319 bytes Raw Blame History 1 # Convert Cerebras models to ggml format 2 # 3 # ref: https://www.cerebras.net/blog/cerebras-gpt-a-family-of-open-compute-efficient-large-language-models/ 4 # 5 6 import sys 7 import struct 8 import json 9 import torch 10 import numpy as np 11 import re 12 13 from transformers import AutoModelForCausalLM 14 15 # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py 16 def bytes_to_unicode(): 17 """ 18 Returns list of utf-8 byte and a corresponding list of unicode strings. 19 The reversible bpe codes work on unicode strings. 20 This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. 21 When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. 22 This is a signficant percentage of your normal, say, 32K bpe vocab. 23 To avoid that, we want lookup tables between utf-8 bytes and unicode strings. 24 And avoids mapping to whitespace/control characters the bpe code barfs on. 25 """ 26 bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) 27 cs = bs[:] 28 n = 0 29 for b in range(2**8): 30 if b not in bs: 31 bs.append(b) 32 cs.append(2**8+n) 33 n += 1 34 cs = [chr(n) for n in cs] 35 return dict(zip(bs, cs)) 36 37 if len(sys.argv) < 2: 38 print("Usage: convert-cerebras-to-ggml.py dir-model [use-f32]\n") 39 sys.exit(1) 40 41 # output in the same directory as the model 42 dir_model = sys.argv[1] 43 fname_out = sys.argv[1] + "/ggml-model-f16.bin" 44 45 with open(dir_model + "/vocab.json", "r", encoding="utf-8") as f: 46 encoder = json.load(f) 47 48 with open(dir_model + "/config.json", "r", encoding="utf-8") as f: 49 hparams = json.load(f) 50 51 # use 16-bit or 32-bit floats 52 use_f16 = True 53 if len(sys.argv) > 2: 54 use_f16 = False 55 fname_out = sys.argv[1] + "/ggml-model-f32.bin" 56 57 model = AutoModelForCausalLM.from_pretrained(dir_model, low_cpu_mem_usage=True) 58 #print (model) 59 60 list_vars = model.state_dict() 61 #print (list_vars) 62 63 print(hparams) 64 65 fout = open(fname_out, "wb") 66 67 fout.write(struct.pack("i", 0x67676d6c)) # magic: ggml in hex 68 fout.write(struct.pack("i", hparams["vocab_size"])) 69 fout.write(struct.pack("i", hparams["n_positions"])) 70 fout.write(struct.pack("i", hparams["n_embd"])) 71 fout.write(struct.pack("i", hparams["n_head"])) 72 fout.write(struct.pack("i", hparams["n_layer"])) 73 fout.write(struct.pack("i", use_f16)) 74 75 byte_encoder = bytes_to_unicode() 76 byte_decoder = {v:k for k, v in byte_encoder.items()} 77 78 fout.write(struct.pack("i", len(encoder))) 79 80 for key in encoder: 81 text = bytearray([byte_decoder[c] for c in key]) 82 fout.write(struct.pack("i", len(text))) 83 fout.write(text) 84 85 for name in list_vars.keys(): 86 data = list_vars[name].squeeze().numpy() 87 print("Processing variable: " + name + " with shape: ", data.shape) 88 89 # rename headers to keep compatibility 90 if name == "transformer.ln_f.weight": 91 name = "model/ln_f/g" 92 elif name == "transformer.ln_f.bias": 93 name = "model/ln_f/b" 94 elif name == "transformer.wte.weight": 95 name = "model/wte" 96 elif name == "transformer.wpe.weight": 97 name = "model/wpe" 98 elif name == "lm_head.weight": 99 name = "model/lm_head" 100 elif re.match(r"transformer.h\.\d+\.ln_1\.weight", name): 101 i = re.findall("\d+", name)[0] 102 name = f"model/h{i}/ln_1/g" 103 elif re.match(r"transformer.h\.\d+\.ln_1\.bias", name): 104 i = re.findall("\d+", name)[0] 105 name = f"model/h{i}/ln_1/b" 106 elif re.match(r"transformer.h\.\d+\.attn\.c_attn\.weight", name): 107 i = re.findall("\d+", name)[0] 108 name = f"model/h{i}/attn/c_attn/w" 109 elif re.match(r"transformer.h\.\d+\.attn\.c_attn\.bias", name): 110 i = re.findall("\d+", name)[0] 111 name = f"model/h{i}/attn/c_attn/b" 112 elif re.match(r"transformer.h\.\d+\.attn\.c_proj\.weight", name): 113 i = re.findall("\d+", name)[0] 114 name = f"model/h{i}/attn/c_proj/w" 115 elif re.match(r"transformer.h.\d+.attn.c_proj.bias", name): 116 i = re.findall("\d+", name)[0] 117 name = f"model/h{i}/attn/c_proj/b" 118 elif re.match(r"transformer.h.\d+.ln_2.weight", name): 119 i = re.findall("\d+", name)[0] 120 name = f"model/h{i}/ln_2/g" 121 elif re.match(r"transformer.h.\d+.ln_2.bias", name): 122 i = re.findall("\d+", name)[0] 123 name = f"model/h{i}/ln_2/b" 124 elif re.match(r"transformer.h.\d+.mlp.c_fc.weight", name): 125 i = re.findall("\d+", name)[0] 126 name = f"model/h{i}/mlp/c_fc/w" 127 elif re.match(r"transformer.h.\d+.mlp.c_fc.bias", name): 128 i = re.findall("\d+", name)[0] 129 name = f"model/h{i}/mlp/c_fc/b" 130 elif re.match(r"transformer.h.\d+.mlp.c_proj.weight", name): 131 i = re.findall("\d+", name)[0] 132 name = f"model/h{i}/mlp/c_proj/w" 133 elif re.match(r"transformer.h.\d+.mlp.c_proj.bias", name): 134 i = re.findall("\d+", name)[0] 135 name = f"model/h{i}/mlp/c_proj/b" 136 else: 137 print("Unrecognized variable name. %s", name) 138 139 # we don't need these 140 if name.endswith("attn.masked_bias") or name.endswith(".attn.bias"): 141 print(" Skipping variable: " + name) 142 continue 143 144 n_dims = len(data.shape); 145 146 # ftype == 0 -> float32, ftype == 1 -> float16 147 ftype = 0; 148 if use_f16: 149 if (name == "model/wte" or name == "model/lm_head" or name[-2:] == "/g" or name[-2:] == "/w") and n_dims == 2: 150 print(" Converting to float16") 151 data = data.astype(np.float16) 152 ftype = 1 153 else: 154 print(" Converting to float32") 155 data = data.astype(np.float32) 156 ftype = 0 157 158 # for efficiency - transpose the projection matrices 159 # "model/h.*/attn/c_attn/w" 160 # "model/h.*/attn/c_proj/w" 161 # "model/h.*/mlp/c_fc/w" 162 # "model/h.*/mlp/c_proj/w" 163 if name[-14:] == "/attn/c_attn/w" or \ 164 name[-14:] == "/attn/c_proj/w" or \ 165 name[-11:] == "/mlp/c_fc/w" or \ 166 name[-13:] == "/mlp/c_proj/w": 167 print(" Transposing") 168 data = data.transpose() 169 170 # header 171 str = name.encode('utf-8') 172 fout.write(struct.pack("iii", n_dims, len(str), ftype)) 173 for i in range(n_dims): 174 fout.write(struct.pack("i", data.shape[n_dims - 1 - i])) 175 fout.write(str); 176 177 # data 178 data.tofile(fout) 179 180 fout.close() 181 182 print("Done. Output file: " + fname_out) 183 print("")