Skip to content

Commit f62edfd

Browse files
authored
fix qwen2.5 lora finetune on 910A (#2137)
1 parent cba59ec commit f62edfd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2581
-28445
lines changed

examples/transformers/peft/lora/Qwen2.5-7B-Instruct-Lora.ipynb

Lines changed: 41 additions & 27153 deletions
Large diffs are not rendered by default.
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
# # 导入环境
5+
6+
# In[ ]:
7+
8+
9+
import mindnlp
10+
import mindspore
11+
12+
# mindspore.set_context(pynative_synchronize=True)
13+
from datasets import Dataset
14+
import pandas as pd
15+
from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForSeq2Seq, TrainingArguments, Trainer, GenerationConfig
16+
17+
18+
19+
# 将JSON文件转换为CSV文件
20+
df = pd.read_json('/home/lvyufeng/lvyufeng/mindnlp/examples/transformers/peft/lora/huanhuan.json')
21+
ds = Dataset.from_pandas(df)
22+
23+
24+
# In[ ]:
25+
26+
27+
ds[:3]
28+
29+
30+
# # 处理数据集
31+
32+
# In[ ]:
33+
34+
35+
tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen2.5-7B-Instruct', use_fast=False, trust_remote_code=True)
36+
tokenizer
37+
38+
39+
# In[ ]:
40+
41+
42+
def process_func(example):
43+
MAX_LENGTH = 384 # Llama分词器会将一个中文字切分为多个token,因此需要放开一些最大长度,保证数据的完整性
44+
input_ids, attention_mask, labels = [], [], []
45+
instruction = tokenizer(f"<|im_start|>system\n现在你要扮演皇帝身边的女人--甄嬛<|im_end|>\n<|im_start|>user\n{example['instruction'] + example['input']}<|im_end|>\n<|im_start|>assistant\n", add_special_tokens=False) # add_special_tokens 不在开头加 special_tokens
46+
response = tokenizer(f"{example['output']}", add_special_tokens=False)
47+
input_ids = instruction["input_ids"] + response["input_ids"] + [tokenizer.pad_token_id]
48+
attention_mask = instruction["attention_mask"] + response["attention_mask"] + [1] # 因为eos token咱们也是要关注的所以 补充为1
49+
labels = [-100] * len(instruction["input_ids"]) + response["input_ids"] + [tokenizer.pad_token_id]
50+
if len(input_ids) > MAX_LENGTH: # 做一个截断
51+
input_ids = input_ids[:MAX_LENGTH]
52+
attention_mask = attention_mask[:MAX_LENGTH]
53+
labels = labels[:MAX_LENGTH]
54+
return {
55+
"input_ids": input_ids,
56+
"attention_mask": attention_mask,
57+
"labels": labels
58+
}
59+
60+
61+
# In[ ]:
62+
63+
64+
tokenized_id = ds.map(process_func, remove_columns=ds.column_names)
65+
66+
print(len(tokenized_id))
67+
68+
# In[ ]:
69+
70+
71+
tokenizer.decode(tokenized_id[0]['input_ids'])
72+
73+
74+
# In[ ]:
75+
76+
77+
tokenizer.decode(list(filter(lambda x: x != -100, tokenized_id[1]["labels"])))
78+
79+
80+
# # 创建模型
81+
82+
# In[ ]:
83+
84+
85+
import torch
86+
87+
model = AutoModelForCausalLM.from_pretrained('Qwen/Qwen2.5-7B-Instruct', torch_dtype=torch.float16, attn_implementation='eager')
88+
# model = AutoModelForCausalLM.from_pretrained('Qwen/Qwen2.5-7B-Instruct', torch_dtype=torch.float16)
89+
model = model.npu()
90+
91+
92+
# In[ ]:
93+
94+
95+
model.enable_input_require_grads() # 开启梯度检查点时,要执行该方法
96+
97+
98+
# In[ ]:
99+
100+
101+
model.dtype
102+
103+
104+
# # lora
105+
106+
# In[ ]:
107+
108+
109+
from peft import LoraConfig, TaskType, get_peft_model
110+
111+
config = LoraConfig(
112+
task_type=TaskType.CAUSAL_LM,
113+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
114+
inference_mode=False, # 训练模式
115+
r=8, # Lora 秩
116+
lora_alpha=32, # Lora alaph,具体作用参见 Lora 原理
117+
lora_dropout=0.1# Dropout 比例
118+
)
119+
config
120+
121+
122+
# In[ ]:
123+
124+
125+
model = get_peft_model(model, config)
126+
config
127+
128+
129+
# In[ ]:
130+
131+
132+
model.print_trainable_parameters()
133+
134+
135+
# In[ ]:
136+
137+
138+
# # 配置训练参数
139+
140+
# In[ ]:
141+
142+
143+
args = TrainingArguments(
144+
output_dir="./output/Qwen2.5_instruct_lora",
145+
per_device_train_batch_size=4,
146+
gradient_accumulation_steps=4,
147+
logging_steps=10,
148+
num_train_epochs=3,
149+
save_steps=100,
150+
learning_rate=1e-4,
151+
save_on_each_node=True,
152+
# fp16=True,
153+
# gradient_checkpointing=True
154+
)
155+
156+
157+
# In[ ]:
158+
159+
160+
trainer = Trainer(
161+
model=model,
162+
args=args,
163+
train_dataset=tokenized_id,
164+
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, padding=True),
165+
)
166+
167+
168+
# In[ ]:
169+
170+
171+
trainer.accelerator.state
172+
173+
174+
# In[ ]:
175+
176+
177+
trainer.train()
178+
179+
180+
# # 合并加载模型
181+
182+
# In[ ]:
183+
184+
185+
from transformers import AutoModelForCausalLM, AutoTokenizer
186+
import torch
187+
from peft import PeftModel
188+
189+
mode_path = 'Qwen/Qwen2.5-7B-Instruct'
190+
lora_path = './output/Qwen2.5_instruct_lora/checkpoint-702' # 这里改称你的 lora 输出对应 checkpoint 地址
191+
192+
# 加载tokenizer
193+
tokenizer = AutoTokenizer.from_pretrained(mode_path, trust_remote_code=True)
194+
195+
# 加载模型
196+
model = AutoModelForCausalLM.from_pretrained(mode_path, device_map="auto",torch_dtype=torch.bfloat16, trust_remote_code=True).eval()
197+
198+
# 加载lora权重
199+
model = PeftModel.from_pretrained(model, model_id=lora_path)
200+
201+
prompt = "你是谁?"
202+
inputs = tokenizer.apply_chat_template([{"role": "user", "content": "假设你是皇帝身边的女人--甄嬛。"},{"role": "user", "content": prompt}],
203+
add_generation_prompt=True,
204+
tokenize=True,
205+
return_tensors="pt",
206+
return_dict=True
207+
).to('cuda')
208+
209+
210+
gen_kwargs = {"max_length": 2500, "do_sample": True, "top_k": 1}
211+
with torch.no_grad():
212+
outputs = model.generate(**inputs, **gen_kwargs)
213+
outputs = outputs[:, inputs['input_ids'].shape[1]:]
214+
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
215+

mindnlp/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@
5050

5151
# set mindnlp.core to torch
5252
from .utils.torch_proxy import initialize_torch_proxy, setup_metadata_patch
53-
from .utils.safetensors_patch import setup_safetensors_patch
54-
from .core._tensor import enable_mindspore_patch
55-
56-
enable_mindspore_patch()
5753
initialize_torch_proxy()
5854
setup_metadata_patch()
55+
56+
from .utils.safetensors_patch import setup_safetensors_patch
5957
setup_safetensors_patch()
6058

6159
from . import transformers

mindnlp/core/_C/_ConvBackend.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Slow2d = None
2+
SlowTranspose2d = None
3+
SlowDilated2d = None
4+
Slow3d = None
5+
SlowDilated3d = None
6+
Empty = None
7+
CudaDepthwise2d = None
8+
CudaDepthwise3d = None
9+
Cudnn = None
10+
CudnnTranspose = None
11+
Miopen = None
12+
MiopenTranspose = None
13+
MiopenDepthwise = None
14+
Mkldnn = None
15+
MkldnnEmpty = None

mindnlp/core/__init__.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,16 @@
4141
inf = float("inf")
4242
nan = float("nan")
4343

44-
from ._dtype import *
45-
from ._tensor import Tensor, tensor, is_tensor, \
46-
LongTensor, FloatTensor, BoolTensor, HalfTensor, BFloat16Tensor, IntTensor
44+
45+
4746
from ._C import *
4847
from ._C.size import Size
49-
from .autograd import *
48+
from ._dtype import *
5049
from .ops import *
51-
from .serialization import load, save
52-
from ._bind import get_default_dtype, set_default_dtype, get_default_device, is_autocast_enabled, set_autocast_enabled, \
53-
set_autocast_dtype, get_autocast_dtype
54-
55-
from .amp import autocast, GradScaler
56-
from .func import vmap
57-
from .configs import set_pyboost
58-
59-
from . import _dynamo
60-
from . import profiler, cuda, amp, compiler, jit, version, __future__, overrides, \
61-
return_types, linalg, fx, backends, nn, fft, _jit_internal, utils, optim, testing
62-
from ._lowrank import svd_lowrank
63-
from .random import get_rng_state, initial_seed, manual_seed, seed, set_rng_state
64-
50+
from ._tensor import Tensor, tensor, is_tensor, \
51+
LongTensor, FloatTensor, BoolTensor, HalfTensor, BFloat16Tensor, IntTensor
52+
from ._tensor import enable_mindspore_patch
53+
enable_mindspore_patch()
6554

6655
def _has_compatible_shallow_copy_type(tensor, other):
6756
"""
@@ -137,4 +126,26 @@ def typename(obj: _Any, /) -> str:
137126
return f"{module}.{qualname}"
138127

139128

129+
def _nnpack_available():
130+
return False
131+
132+
133+
from .autograd import *
134+
from .serialization import load, save
135+
from ._bind import get_default_dtype, set_default_dtype, get_default_device, is_autocast_enabled, set_autocast_enabled, \
136+
set_autocast_dtype, get_autocast_dtype
137+
138+
from .amp import autocast, GradScaler
139+
from .func import vmap
140+
from .configs import set_pyboost
141+
from .storage import UntypedStorage, Storage, TypedStorage
142+
143+
from . import _dynamo
144+
from . import profiler, cuda, amp, compiler, jit, version, __future__, overrides, \
145+
return_types, linalg, fx, backends, nn, fft, _jit_internal, utils, optim, testing
146+
from ._lowrank import svd_lowrank
147+
from .random import get_rng_state, initial_seed, manual_seed, seed, set_rng_state
148+
149+
150+
140151
__version__ = 'test_version_no_value'

mindnlp/core/_dtype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def __gt__(self, other):
4949
float8_e4m3fnuz = None
5050
float8_e5m2fnuz = None
5151
complex32 = None
52-
cfloat = complex32
53-
cdouble = complex64
52+
cfloat = complex64
53+
cdouble = complex128
5454

5555
uint1 = None
5656
uint2 = None
File renamed without changes.

mindnlp/core/_prims/ascend.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,4 @@ def one_hot_ext(tensor, num_classes):
238238
return pyboost_inner_prim.one_hot_ext_impl(tensor, num_classes, on_value, off_value, -1)
239239

240240
__all__.append('one_hot_ext')
241+

mindnlp/core/_prims/cpu.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,18 @@ def bitwise_right_shift(input, other):
194194
return bitwise_right_shift_op(input, other)
195195

196196
__all__.append('bitwise_right_shift')
197+
198+
embedding_op = ops.Gather().set_device('CPU')
199+
def embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq):
200+
return embedding_op(weight, input, 0)
201+
202+
__all__.append('embedding')
203+
204+
205+
def randn(size, seed, offset, dtype):
206+
rand_op = ops.StandardNormal()
207+
output = rand_op(size)
208+
return output
209+
210+
__all__.append('randn')
211+

mindnlp/core/_tensor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class StubTensor: pass
1616
except:
1717
from mindspore._c_expression import Tensor as Tensor_
1818

19+
from mindnlp import core
1920
from . import ops, _dtype
2021
from ._bind import get_device_in_context, device_, get_default_dtype
2122
from ._utils import _rebuild_tensor_v2
@@ -2509,6 +2510,9 @@ def backward(self):
25092510
def log_softmax(self, dim):
25102511
return ops.log_softmax(self, dim)
25112512

2513+
def char(self):
2514+
return self.to(core.int8)
2515+
25122516
@property
25132517
def is_nested(self):
25142518
return False

0 commit comments

Comments
 (0)