GLM
论文
GLM: General Language Model Pretraining with Autoregressive Blank Infilling
模型结构
GLM采用Transformer模型结构.
算法原理
GLM是一种具有自回归填充目标的通用语言模型,可以在各种自然语言理解和生成任务上进行微调。
环境配置
-v 路径、docker_name和imageID根据实际情况修改
Docker(方法一)
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk24.04-py310
docker run -it --network=host --privileged=true --name=docker_name --device=/dev/kfd --device=/dev/dri --ipc=host --shm-size=32G --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro imageID /bin/bash
cd /your_code_path/glm_pytorch
pip install -r requirements.txt
Dockerfile(方法二)
cd ./docker
docker build --no-cache -t glm:latest .
docker run -it --network=host --privileged=true --name=docker_name --device=/dev/kfd --device=/dev/dri --ipc=host --shm-size=32G --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro imageID /bin/bash
cd /your_code_path/glm_pytorch
pip install -r requirements.txt
Anaconda(方法三)
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.hpccube.com/tool/
DTK软件栈:dtk24.04
python:python3.10
torch:2.1
apex: 1.1.0+0dd7f68.abi0.dtk2404.torch2.1
deepspeed: 0.12.3+gita724046.abi0.dtk2404.torch2.1.0
Tips:以上dtk软件栈、python、torch等DCU相关工具版本需要严格一一对应
2、其他非特殊库直接按照下面步骤进行安装
pip install apex-1.1.0+das1.0+0dd7f68.abi0.dtk2404.torch2.1-cp310-cp310-manylinux2014_x86_64.whl
pip install deepspeed-0.12.3+das1.0+gita724046.abi0.dtk2404.torch2.1.0-cp310-cp310-manylinux2014_x86_64.whl
pip install -r requirements.txt
数据集
准备数据集
-
Seq2Seq CNN/Daily Mail,根据
Instructions
内容进行数据下载和处理。 -
文本填充 Yahoo,根据
Download Data
内容进行数据下载。
数据集的目录结构如下:
├── datasets
│ ├── cnn_dm
│ ├── test.source
│ ├── test.target
│ ├── train.source
│ ├── train.target
│ ├── val.source
│ └── val.target
│ └── blank_yahoo
│ ├── blank
│ ├── test.txt
│ ├── train.txt
│ └── valid.txt
训练
Tips: 如果遇到了out of memory
错误,可以尝试尝试模型并行性,将参数划分为多个dcus。以双向模型并行为例:
# 数字表示切分数量
python change_mp.py path_to_the_checkpoint 2
微调
项目提供了在一些下游任务上调优GLM的脚本。预训练模型可通过SCNet AIModels下载,当前用例默认预训练模型为:GLM-10B。
Seq2Seq(use the CNN/Daily Mail dataset)
-
修改模型配置文件、任务配置文件中的参数路径,例如模型配置文件config_tasks/model_blocklm_10B.sh、任务配置文件config_tasks/seq_cnndm_org.sh; 若替换为其他模型,如GLM-Large, 对应模型配置文件也需替换为config_tasks/model_blocklm_large.sh;
-
参考scripts/ds_finetune_seq2seq.sh中的样例步骤,修改
DATA_ROOT, CHECKPOINT_PATH, SAVE_PATH
参数为本地对应地址,将MP_SIZE
参数1
改为切分后的数量
; -
执行下面的脚本 (以CNN/Daily Mail数据集为例):
bash scripts/ds_finetune_seq2seq.sh \
config_tasks/model_blocklm_10B.sh \
config_tasks/seq_cnndm_org.sh
- summaries被写入
./runs/experiment_name/test.json.hyps
中, references被写入同一目录下的test.json.ref
中。要计算rouge,请安装file2rouge并从这里下载Stanford CoreNLP。其中,experiment_name表示训练任务名称,运行以下脚本:
bash scripts/evaluate_seq2seq.sh \
./runs/experiment_name/test.jsonl.hyps ./runs/experiment_name/test.jsonl.refs
- 如果使用自己的数据集进行训练,参考cnn数据集,将您的seq2seq数据处理成
{split}.source
和{split}.target
,每行是样本的上下文或目标,split
表示train
、val
或test
。执行下面的脚本:
bash scripts/ds_finetune_seq2seq.sh \
config_tasks/model_blocklm_10B.sh \
config_tasks/seq_customization.sh
您可以指定 config_tasks/seq_customization.sh
和 config_tasks/config_blocklm_10B_cnndm.json
中的超参数。
文本填充
-
下载Yahoo数据,参考scripts/finetune_blank.sh中的样例步骤,修改
DATA_ROOT, CHECKPOINT_PATH, SAVE_PATH
参数为当前对应的本地路径; -
在模型配置文件中的参数路径,例如模型配置文件config_tasks/model_blocklm_large.sh、任务配置文件config_tasks/seq_cnndm_org.sh;
-
执行脚本:
bash scripts/finetune_blank.sh \
config_tasks/model_blocklm_large.sh \
config_tasks/seq_blank.sh
推理
-
根据需求修改
--model_name_or_path
模型地址; -
添加环境变量
pip install -U huggingface_hub hf_transfer
export export HF_ENDPOINT=https://hf-mirror.com
- 执行:
python inference.py --model_name_or_path THUDM/glm-10b
result
精度
测试数据:智源glm_trian_data数据,使用的加速卡:K100。
device | dtype | params | acc |
---|---|---|---|
A100 | fp16 | bs=8, lr=1e-05 | 0.808 |
K100 | fp16 | bs=8, lr=1e-05 | 0.804 |
应用场景
算法类别
多轮对话
热点应用行业
家居,教育,科研