最近終于有時(shí)間看大模型相關(guān)的教程郑什,由于huggingface國內(nèi)訪問受阻侦香,因此探索了一下阿里的modelscope圾叼,并記錄常用的組件和模塊供以后學(xué)習(xí)工作復(fù)查。
0盛泡、準(zhǔn)備篇
看了一段時(shí)間的教程闷祥,關(guān)于大模型的使用主要涉及到以下的點(diǎn):
1、用什么框架加載大模型(modelscope, huggingface)傲诵;
2凯砍、如何訓(xùn)練大模型以使大模型能夠根據(jù)特定的場景/問題回答給出答案(涉及比較專業(yè)的數(shù)據(jù));
3拴竹、如何準(zhǔn)備高質(zhì)量訓(xùn)練數(shù)據(jù)及訓(xùn)練數(shù)據(jù)的類型有什么悟衩;
4、如何訓(xùn)練大模型栓拜,用什么方法對(duì)大模型進(jìn)行訓(xùn)練座泳,訓(xùn)練的本質(zhì)是什么;
5幕与、如何評(píng)估模型:怎樣才知道訓(xùn)練好的模型是否按預(yù)期輸出钳榨;
6、大模型訓(xùn)練纽门、部署及成本評(píng)估
- 安裝modelscope庫
pip install modelscope
#gpt-3的模型下載需要用到這個(gè)
pip install megatron_util -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
1薛耻、模型加載
- 以chatGLM2-6b為例:不問為什么,就只有這個(gè)模型跑得順利
from modelscope.models import Model
model = Model.from_pretrained('ZhipuAI/chatglm2-6b', device_map='auto', revision='v1.0.12')
2赏陵、數(shù)據(jù)加載
使用的modelscope模塊是MsDataset
加載本地?cái)?shù)據(jù)集饼齿、加載huggingface hub的數(shù)據(jù)集,加載modelscope的數(shù)據(jù)集等蝙搔,官方目前支持的文件格式有:csv缕溉、txt、json吃型、jsonl证鸥、pickle、png勤晚、jpeg
下面的代碼全部來自官方文檔
#加載本地?cái)?shù)據(jù)集
dataset_en = MsDataset.load("/path/to/data")
#批量加載數(shù)據(jù)集
ds = MsDataset.load('csv', data_files=[my_csv_1, my_csv_2])
# 加載modelscope上的數(shù)據(jù)集
# 以cats_and_dogs數(shù)據(jù)集為例枉层,數(shù)據(jù)集鏈接: https://modelscope.cn/datasets/tany0699/cats_and_dogs/summary
ds = MsDataset.load('cats_and_dogs', namespace='tany0699', split='train')
print(next(iter(ds)))
#加載huggingface_hub的數(shù)據(jù)集
#可以通過split參數(shù)加載部分?jǐn)?shù)據(jù)集,也可以將split參數(shù)去掉加載全部數(shù)據(jù)集
#加載指定數(shù)據(jù)集
ds_train = MsDataset.load('glue', subset_name='sst2', split='train', hub='huggingface')
#加載全部數(shù)據(jù)集
ds_train = MsDataset.load('glue', subset_name='sst2', hub='huggingface')
print(next(iter(ds_train))
3赐写、pipeline推理(模型預(yù)測(cè))
- 什么是推理任務(wù): 在深度學(xué)習(xí)中指的是模型預(yù)測(cè)這一行為鸟蜡,模型在ModelScope里面指的是一系列的任務(wù),包括數(shù)據(jù)加載挺邀,數(shù)據(jù)預(yù)處理和模型推導(dǎo)任務(wù)等
from modelscope.pipelines import pipeline
#推理
word_segmentation = pipeline('word-segmentation',
model=word_seg_model, preprocessor=tokenizer)
#批量推理
word_segmentation = pipeline('word-segmentation',
model=word_seg_model, preprocessor=tokenizer, batch_size=2)
inputs = "XXX,XXX"
print(word_segmentation(inputs))
4揉忘、模型訓(xùn)練
框架:
SWIFT
跳座, 這個(gè)框架里面包含了LoRA訓(xùn)練法等等,根據(jù)Modelscope的官方說法就是一行代碼實(shí)現(xiàn)"XXXX"方法的訓(xùn)練泣矛,下面知乎的文檔中有少許過時(shí)疲眷,下面貼出截止到2023年10月18號(hào)版本的代碼。原來的兩個(gè)參數(shù)改變了:replace_modules-->target_modules您朽, rank-->merge_weights
咪橙。另外swift的utils模塊中也取消了show_freeze_layers的功能安裝相關(guān)的庫
pip install ms-swift -U
- Swift框架下的LoRA訓(xùn)練代碼
LORA_TARGET_MODULES = ['query_key_value']
LORA_RANK = 8
LORA_ALPHA = 32
LORA_DROPOUT_P = 0.1
lora_config = LoRAConfig(
target_modules=LORA_TARGET_MODULES,
merge_weights=LORA_RANK,
lora_alpha=LORA_ALPHA,
lora_dropout=LORA_DROPOUT_P
)
logger.info(f'lora_config: {lora_config}')
Swift.prepare_model(model, lora_config) #加載配置
- ps:官方的手冊(cè)非常詳細(xì)且說人話,更多功能參考官方文檔虚倒。Learning through making對(duì)于我來說真的非常必要美侦,邊學(xué)邊做才能真正學(xué)會(huì)。
- 吐槽:官方在知乎8月份掛的代碼是跑不通的魂奥,小白心塞菠剩,建議直接到github看
參考
modelscope手冊(cè)
用modelscope訓(xùn)練chatGLM
Swift推理readme手冊(cè)