Skip to content

Commit 335acd2

Browse files
authored
fix convert-lora-to-ggml.py (#2738)
1 parent 5290c38 commit 335acd2

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

convert-lora-to-ggml.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
import sys
77
from typing import Any, Dict, Sequence, TextIO
88

9+
import numpy as np
910
import torch
1011

11-
from convert import DATA_TYPE_TO_FTYPE, NUMPY_TYPE_TO_DATA_TYPE, DataType
12+
NUMPY_TYPE_TO_FTYPE: Dict[str, int] = {"float32": 0, "float16": 1}
13+
1214

1315
HF_SUBLAYER_TO_GGML = {
14-
"self_attn.q_proj": "attention.wq",
15-
"self_attn.k_proj": "attention.wk",
16-
"self_attn.v_proj": "attention.wv",
17-
"self_attn.o_proj": "attention.wo",
18-
"mlp.gate_proj": "feed_forward.w1",
19-
"mlp.down_proj": "feed_forward.w2",
20-
"mlp.up_proj": "feed_forward.w3",
21-
"input_layernorm": "attention_norm",
16+
"self_attn.q_proj": "attn_q",
17+
"self_attn.k_proj": "attn_k",
18+
"self_attn.v_proj": "attn_v",
19+
"self_attn.o_proj": "attn_output",
20+
"mlp.gate_proj": "ffn_gate",
21+
"mlp.down_proj": "ffn_down",
22+
"mlp.up_proj": "ffn_up",
23+
"input_layernorm": "attn_norm",
2224
"post_attention_layernorm": "ffn_norm",
23-
# "norm": "norm",
24-
# "embed_tokens": "tok_embeddings",
25-
# "lm_head": "output",
2625
}
2726

2827

@@ -39,7 +38,7 @@ def translate_tensor_name(t: str) -> str:
3938
sys.exit(1)
4039

4140
output_string = (
42-
f"layers.{nn}.{HF_SUBLAYER_TO_GGML[sub_layer]}.weight.lora{lora_type}"
41+
f"blk.{nn}.{HF_SUBLAYER_TO_GGML[sub_layer]}.weight.lora{lora_type}"
4342
)
4443
return output_string
4544
else:
@@ -54,20 +53,22 @@ def write_file_header(fout: TextIO, params: Dict[str, Any]) -> None:
5453
# https://opendelta.readthedocs.io/en/latest/modules/deltas.html says that `lora_alpha` is an int
5554
# but some models ship a float value instead
5655
# let's convert to int, but fail if lossless conversion is not possible
57-
assert int(params["lora_alpha"]) == params["lora_alpha"], "cannot convert float to int losslessly"
56+
assert (
57+
int(params["lora_alpha"]) == params["lora_alpha"]
58+
), "cannot convert float to int losslessly"
5859
fout.write(struct.pack("i", int(params["lora_alpha"])))
5960

6061

6162
def write_tensor_header(
62-
self, name: str, shape: Sequence[int], data_type: DataType
63+
self, name: str, shape: Sequence[int], data_type: np.dtype
6364
) -> None:
6465
sname = name.encode("utf-8")
6566
fout.write(
6667
struct.pack(
6768
"iii",
6869
len(shape),
6970
len(sname),
70-
DATA_TYPE_TO_FTYPE[NUMPY_TYPE_TO_DATA_TYPE[data_type]],
71+
NUMPY_TYPE_TO_FTYPE[data_type.name],
7172
)
7273
)
7374
fout.write(struct.pack("i" * len(shape), *shape[::-1]))

0 commit comments

Comments
 (0)