Skip to content

Commit 5ade1e4

Browse files
committed
Update loading instructions
1 parent 63b1e4e commit 5ade1e4

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

ptuning/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,16 @@ model = AutoModel.from_pretrained("THUDM/chatglm-6b", config=config, trust_remot
152152
prefix_state_dict = torch.load(os.path.join(CHECKPOINT_PATH, "pytorch_model.bin"))
153153
new_prefix_state_dict = {}
154154
for k, v in prefix_state_dict.items():
155-
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
155+
if k.startswith("transformer.prefix_encoder."):
156+
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
156157
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
157158
```
158159
注意你可能需要将 `pre_seq_len` 改成你训练时的实际值。如果你是[从本地加载模型的话](https://github.com/THUDM/ChatGLM-6B#%E4%BB%8E%E6%9C%AC%E5%9C%B0%E5%8A%A0%E8%BD%BD%E6%A8%A1%E5%9E%8B),需要将 `THUDM/chatglm-6b` 改成本地的模型路径(注意不是checkpoint路径)。
159160

160161
(2) 如果需要加载的是旧 Checkpoint(包含 ChatGLM-6B 以及 PrefixEncoder 参数),或者进行的全参数微调,则直接加载整个 Checkpoint:
161162

162163
```python
163-
model = AutoModel.from_pretrained(CHECKPOINT_PATH, config=config, trust_remote_code=True)
164+
model = AutoModel.from_pretrained(CHECKPOINT_PATH, trust_remote_code=True)
164165
```
165166

166167
之后根据需求可以进行量化,也可以直接使用:

ptuning/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def main():
118118
prefix_state_dict = torch.load(os.path.join(model_args.ptuning_checkpoint, "pytorch_model.bin"))
119119
new_prefix_state_dict = {}
120120
for k, v in prefix_state_dict.items():
121-
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
121+
if k.startswith("transformer.prefix_encoder."):
122+
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
122123
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
123124
else:
124125
model = AutoModel.from_pretrained(model_args.model_name_or_path, config=config, trust_remote_code=True)

0 commit comments

Comments
 (0)