Skip to content

Commit 1f908b1

Browse files
committed
remove .rotary_pos_emb.inv_freq and unuse code for chatglm3 model
Signed-off-by: XingXing Qiao <qiaoxx@dingdao.com>
1 parent 5747a0c commit 1f908b1

File tree

1 file changed

+8
-75
lines changed

1 file changed

+8
-75
lines changed

convert-hf-to-gguf.py

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,85 +2507,18 @@ def set_gguf_parameters(self):
25072507
self.gguf_writer.add_rope_dimension_count(64)
25082508
self.gguf_writer.add_add_bos_token(False)
25092509

2510-
def write_tensors(self):
2511-
block_count = self.hparams["num_layers"]
2512-
tensors = dict(self.get_tensors())
2513-
tensor_map = gguf.get_tensor_name_map(self.model_arch, block_count)
2514-
has_lm_head = True
2515-
n_head = self.hparams.get("n_head", self.hparams.get("num_attention_heads"))
2516-
n_embed = self.hparams.get("hidden_size", self.hparams.get("n_embed"))
2517-
2518-
for name, data_torch in tensors.items():
2519-
if name.endswith(".rotary_pos_emb.inv_freq"):
2520-
continue
2521-
2522-
if "lm_head.weight" not in tensors.keys() and "output.weight" not in tensors.keys():
2523-
has_lm_head = False
2510+
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
2511+
if name.endswith(".rotary_pos_emb.inv_freq"):
2512+
return []
25242513

2525-
name = re.sub(r'transformer\.', '', name)
2514+
del bid # unused
25262515

2527-
old_dtype = data_torch.dtype
2516+
name = re.sub(r'transformer\.', '', name)
25282517

2529-
# convert any unsupported data types to float32
2530-
if data_torch.dtype not in (torch.float16, torch.float32):
2531-
data_torch = data_torch.to(torch.float32)
2518+
if name == "word_embeddings.weight":
2519+
assert self.tensor_names is not None
25322520

2533-
data = data_torch.squeeze().numpy()
2534-
2535-
if re.match(r"h\.\d+\.self_attention\.query_key_value\.weight", name):
2536-
# Map bloom-style qkv_linear to gpt-style qkv_linear
2537-
# bloom: https://github.com/huggingface/transformers/blob/main/src/transformers/models/bloom/modeling_bloom.py#L238-L252 # noqa
2538-
# gpt-2: https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/modeling_gpt2.py#L312 # noqa
2539-
qkv_weights = data.reshape((n_head, 3, n_embed // n_head, n_embed))
2540-
data = np.concatenate(
2541-
(
2542-
qkv_weights[:, 0, :, :].reshape((-1, n_embed)),
2543-
qkv_weights[:, 1, :, :].reshape((-1, n_embed)),
2544-
qkv_weights[:, 2, :, :].reshape((-1, n_embed)),
2545-
),
2546-
axis=0,
2547-
)
2548-
print("re-format attention.linear_qkv.weight")
2549-
elif re.match(r"h\.\d+\.self_attention\.query_key_value\.bias", name):
2550-
qkv_bias = data.reshape((n_head, 3, n_embed // n_head))
2551-
data = np.concatenate(
2552-
(
2553-
qkv_bias[:, 0, :].reshape((n_embed,)),
2554-
qkv_bias[:, 1, :].reshape((n_embed,)),
2555-
qkv_bias[:, 2, :].reshape((n_embed,)),
2556-
),
2557-
axis=0,
2558-
)
2559-
print("re-format attention.linear_qkv.bias")
2560-
2561-
# map tensor names
2562-
new_name = tensor_map.get_name(name, try_suffixes=(".weight", ".bias"))
2563-
if new_name is None:
2564-
print(f"Can not map tensor {name!r}")
2565-
sys.exit()
2566-
2567-
n_dims = len(data.shape)
2568-
data_dtype = data.dtype
2569-
2570-
# if f32 desired, convert any float16 to float32
2571-
if self.ftype == 0 and data_dtype == np.float16:
2572-
data = data.astype(np.float32)
2573-
2574-
# TODO: Why cant we use these float16 as-is? There should be not reason to store float16 as float32
2575-
if self.ftype == 1 and data_dtype == np.float16 and n_dims == 1:
2576-
data = data.astype(np.float32)
2577-
2578-
# if f16 desired, convert any float32 2-dim weight tensors to float16
2579-
if self.ftype == 1 and data_dtype == np.float32 and name.endswith(".weight") and n_dims == 2:
2580-
data = data.astype(np.float16)
2581-
2582-
print(f"=> {new_name}, shape = {data.shape}, {old_dtype} --> {data.dtype}")
2583-
2584-
self.gguf_writer.add_tensor(new_name, data)
2585-
2586-
if not has_lm_head and name == "word_embeddings.weight":
2587-
self.gguf_writer.add_tensor("output.weight", data)
2588-
print(name, f"=> output.weight, shape = {data.shape}, {old_dtype} --> {data.dtype}")
2521+
return [(self.map_tensor_name(name), data_torch)]
25892522

25902523

25912524
###### CONVERSION LOGIC ######

0 commit comments

Comments
 (0)