|
| 1 | +# Convert a SAM model checkpoint to a ggml compatible file |
| 2 | +# |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import code |
| 7 | +import json |
| 8 | +import torch |
| 9 | +import struct |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +if len(sys.argv) < 3: |
| 13 | + print("Usage: convert-pth-to-ggml.py file-model ftype\n") |
| 14 | + print(" ftype == 0 -> float32") |
| 15 | + print(" ftype == 1 -> float16") |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | +# output in the same directory as the model |
| 19 | +fname_model = sys.argv[1] |
| 20 | +fname_out = os.path.dirname(fname_model) + "/ggml-model.bin" |
| 21 | + |
| 22 | +# possible data types |
| 23 | +# ftype == 0 -> float32 |
| 24 | +# ftype == 1 -> float16 |
| 25 | +# |
| 26 | +# map from ftype to string |
| 27 | +ftype_str = ["f32", "f16"] |
| 28 | + |
| 29 | +ftype = 1 |
| 30 | +if len(sys.argv) > 2: |
| 31 | + ftype = int(sys.argv[2]) |
| 32 | + |
| 33 | +if ftype < 0 or ftype > 1: |
| 34 | + print("Invalid ftype: " + str(ftype)) |
| 35 | + sys.exit(1) |
| 36 | + |
| 37 | +fname_out = fname_out.replace(".bin", "-" + ftype_str[ftype] + ".bin") |
| 38 | + |
| 39 | +model = torch.load(fname_model, map_location="cpu") |
| 40 | + |
| 41 | +# TODO: determine based on model data |
| 42 | +# TODO: add decoder / prompt encoder if needed |
| 43 | +hparams = { |
| 44 | + "n_enc_state": 768, |
| 45 | + "n_enc_layers": 12, |
| 46 | + "n_enc_heads": 12, |
| 47 | + "n_enc_out_chans": 256, |
| 48 | + |
| 49 | + "n_pt_embd": 4, |
| 50 | +} |
| 51 | + |
| 52 | +print(hparams) |
| 53 | + |
| 54 | +for k, v in model.items(): |
| 55 | + print(k, v.shape) |
| 56 | + |
| 57 | +#exit() |
| 58 | +#code.interact(local=locals()) |
| 59 | + |
| 60 | +fout = open(fname_out, "wb") |
| 61 | + |
| 62 | +fout.write(struct.pack("i", 0x67676d6c)) # magic: ggml in hex |
| 63 | +fout.write(struct.pack("i", hparams["n_enc_state"])) |
| 64 | +fout.write(struct.pack("i", hparams["n_enc_layers"])) |
| 65 | +fout.write(struct.pack("i", hparams["n_enc_heads"])) |
| 66 | +fout.write(struct.pack("i", hparams["n_enc_out_chans"])) |
| 67 | +fout.write(struct.pack("i", hparams["n_pt_embd"])) |
| 68 | +fout.write(struct.pack("i", ftype)) |
| 69 | + |
| 70 | +for k, v in model.items(): |
| 71 | + name = k |
| 72 | + shape = v.shape |
| 73 | + |
| 74 | + # TODO: export only the Encoder -- after it works we will export the other stuff |
| 75 | + if name[:13] != "image_encoder" and \ |
| 76 | + name[:14] != "prompt_encoder": |
| 77 | + continue |
| 78 | + |
| 79 | + if name[:19] == "prompt_encoder.mask": |
| 80 | + continue |
| 81 | + |
| 82 | + print("Processing variable: " + name + " with shape: ", shape, " and type: ", v.dtype) |
| 83 | + |
| 84 | + #data = tf.train.load_variable(dir_model, name).squeeze() |
| 85 | + #data = v.numpy().squeeze() |
| 86 | + data = v.numpy() |
| 87 | + n_dims = len(data.shape); |
| 88 | + |
| 89 | + # for efficiency - transpose some matrices |
| 90 | + # "model/h.*/attn/c_attn/w" |
| 91 | + # "model/h.*/attn/c_proj/w" |
| 92 | + # "model/h.*/mlp/c_fc/w" |
| 93 | + # "model/h.*/mlp/c_proj/w" |
| 94 | + #if name[-14:] == "/attn/c_attn/w" or \ |
| 95 | + # name[-14:] == "/attn/c_proj/w" or \ |
| 96 | + # name[-11:] == "/mlp/c_fc/w" or \ |
| 97 | + # name[-13:] == "/mlp/c_proj/w": |
| 98 | + # print(" Transposing") |
| 99 | + # data = data.transpose() |
| 100 | + |
| 101 | + dshape = data.shape |
| 102 | + |
| 103 | + # default type is fp16 |
| 104 | + ftype_cur = 1 |
| 105 | + if ftype == 0 or n_dims == 1 or \ |
| 106 | + name == "image_encoder.pos_embed" or \ |
| 107 | + name.startswith("prompt_encoder"): |
| 108 | + print(" Converting to float32") |
| 109 | + data = data.astype(np.float32) |
| 110 | + ftype_cur = 0 |
| 111 | + else: |
| 112 | + print(" Converting to float16") |
| 113 | + data = data.astype(np.float16) |
| 114 | + |
| 115 | + # reshape the 1D bias into a 4D tensor so we can use ggml_repeat |
| 116 | + # keep it in F32 since the data is small |
| 117 | + if name == "image_encoder.patch_embed.proj.bias": |
| 118 | + data = data.reshape(1, data.shape[0], 1, 1) |
| 119 | + n_dims = len(data.shape); |
| 120 | + dshape = data.shape |
| 121 | + |
| 122 | + print(" New shape: ", dshape) |
| 123 | + |
| 124 | + # header |
| 125 | + str = name.encode('utf-8') |
| 126 | + fout.write(struct.pack("iii", n_dims, len(str), ftype_cur)) |
| 127 | + for i in range(n_dims): |
| 128 | + fout.write(struct.pack("i", dshape[n_dims - 1 - i])) |
| 129 | + fout.write(str); |
| 130 | + |
| 131 | + # data |
| 132 | + data.tofile(fout) |
| 133 | + |
| 134 | +fout.close() |
| 135 | + |
| 136 | +print("Done. Output file: " + fname_out) |
| 137 | +print("") |
0 commit comments