import os
import warnings
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # 要使用的GPU的索引
os.environ['TRANSFORMERS_OFFLINE'] = '1' # 离线的方式加载模型
# os.environ['DWAN_DISABLED'] = 'true'
warnings.filterwarnings("ignore") # 忽略警告
import torch
from modelscope import (AutoModelForCausalLM,
BitsAndBytesConfig)
# 4bit的压缩量化(32bit --> 4bit)
_bnb_config = BitsAndBytesConfig(load_in_4bit=True, # 权重将被加载为4位
bnb_4bit_use_double_quant=True, # 双量化方案:对权重和激活进行量化
bnb_4bit_quant_type="nf4", # 量化类型为nf4:非对称4位浮点数(non-fused 4-bit floating point)
bnb_4bit_compute_dtype=torch.float32) # 进行4位计算的数据类型,计算仍然在32位浮点精度(torch.float32)上进行,有助于保持模型的性能。
# 导入通义千问的模型
_model = AutoModelForCausalLM.from_pretrained("../models/qwen/Qwen2-0.5B",
low_cpu_mem_usage=True,
quantization_config=_bnb_config)
from modelscope import AutoTokenizer
_tokenizer = AutoTokenizer.from_pretrained("../models/qwen/Qwen2-0.5B")
from transformers import pipeline
# 正确使用基础模型
pipe = pipeline("text-generation",
model=_model,
tokenizer=_tokenizer)
print(pipe(f"User: 你好 Tom。Assistant:"))
from datasets import load_dataset
_dataset = load_dataset("json", data_files="data.json", split="train")
print(_dataset)
#input_tokens : "User:你是谁?Assistant:只剩一个心脏了还能活吗?0000000"
#attention_mask : [1,1,1,0,0,0]
#_labels:[-100,-100,...,1100,15263,....]
'''
example
{
"instruction": "今天天气如何?",
"output": "天气很好"
}
name:{instruction}answer:{output}
'''
def preprocess_dataset(example):
MAX_LENGTH = 256
_input_ids, _attention_mask, _labels = [], [], []
_instruction = _tokenizer(f"User: {example['instruction']}Assistant: ", add_special_tokens=False)
_response = _tokenizer(example["output"] + _tokenizer.eos_token, add_special_tokens=False)
_input_ids = _instruction["input_ids"] + _response["input_ids"]
_attention_mask = _instruction["attention_mask"] + _response["attention_mask"]
_labels = [-100] * len(_instruction["input_ids"]) + _response["input_ids"]
if len(_input_ids) > MAX_LENGTH:
_input_ids = _input_ids[:MAX_LENGTH]
_attention_mask = _attention_mask[:MAX_LENGTH]
_labels = _labels[:MAX_LENGTH]
return {
"input_ids": _input_ids,
"attention_mask": _attention_mask,
"labels": _labels
}
_dataset = _dataset.map(preprocess_dataset, remove_columns=_dataset.column_names)
_dataset = _dataset.shuffle()
print(_dataset)
from peft import (LoraConfig,
get_peft_model,
TaskType)
# lora微调
config = LoraConfig(task_type=TaskType.CAUSAL_LM,
r=8,
target_modules="all-linear") # 所有权重都参与训练
# 把原来的模型和lora微调部分合并在一起
_model = get_peft_model(_model, config)
from transformers import (TrainingArguments,
Trainer,
DataCollatorForSeq2Seq)
_training_args = TrainingArguments(output_dir="checkpoints/qlora", # 存放训练结果
overwrite_output_dir=True,
run_name="qlora_study",
per_device_train_batch_size=2,
gradient_accumulation_steps=8, # 梯度累加
num_train_epochs=200,
save_steps=50,
logging_steps=50,
report_to="none",
optim="paged_adamw_32bit")
trainer = Trainer(
model=_model,
args=_training_args,
train_dataset=_dataset,
data_collator=DataCollatorForSeq2Seq(tokenizer=_tokenizer, padding=True),
)
trainer.train()
from transformers import pipeline
from peft import PeftModel
peft_model = PeftModel.from_pretrained(model=_model,
model_id="checkpoints/qlora/checkpoint-50")
pipe = pipeline("text-generation",
model=peft_model,
tokenizer=_tokenizer)
print(pipe(f"User: 你好?Assistant:", do_sample=True))
from transformers import pipeline
from peft import PeftModel
peft_model = PeftModel.from_pretrained(model=_model,
model_id="checkpoints/qlora/checkpoint-50")
# 保存训练好的模型
peft_model = peft_model.merge_and_unload()
peft_model.save_pretrained("best",safe_serialization=False)
import torch
from modelscope import (AutoModelForCausalLM,
BitsAndBytesConfig)
# 4bit的压缩量化(32bit --> 4bit)
_bnb_config = BitsAndBytesConfig(load_in_4bit=True, # 权重将被加载为4位
bnb_4bit_use_double_quant=True, # 双量化方案:对权重和激活进行量化
bnb_4bit_quant_type="nf4", # 量化类型为nf4:非对称4位浮点数(non-fused 4-bit floating point)
bnb_4bit_compute_dtype=torch.float32) # 进行4位计算的数据类型,计算仍然在32位浮点精度(torch.float32)上进行,有助于保持模型的性能。
# 导入通义千问的模型
_model = AutoModelForCausalLM.from_pretrained("myqwen2-0.5b",
low_cpu_mem_usage=True,
quantization_config=_bnb_config)
pipe = pipeline("text-generation",
model=_model,
tokenizer=_tokenizer)
pipe(f"User: 你好?Assistant:", do_sample=True)
大模型开发与应用2还有微调
于 2024-07-30 01:57:56 首次发布