glm_pytorch多轮对话算法模型

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)
  1. 修改模型配置文件、任务配置文件中的参数路径,例如模型配置文件config_tasks/model_blocklm_10B.sh、任务配置文件config_tasks/seq_cnndm_org.sh; 若替换为其他模型,如GLM-Large, 对应模型配置文件也需替换为config_tasks/model_blocklm_large.sh;

  2. 参考scripts/ds_finetune_seq2seq.sh中的样例步骤,修改 DATA_ROOT, CHECKPOINT_PATH, SAVE_PATH 参数为本地对应地址,将MP_SIZE参数1改为切分后的数量;

  3. 执行下面的脚本 (以CNN/Daily Mail数据集为例):

bash scripts/ds_finetune_seq2seq.sh \
    config_tasks/model_blocklm_10B.sh \
    config_tasks/seq_cnndm_org.sh
  1. 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
  1. 如果使用自己的数据集进行训练,参考cnn数据集,将您的seq2seq数据处理成 {split}.source 和 {split}.target,每行是样本的上下文或目标,split 表示 trainval 或 test。执行下面的脚本:
bash scripts/ds_finetune_seq2seq.sh \
   config_tasks/model_blocklm_10B.sh \
   config_tasks/seq_customization.sh

您可以指定 config_tasks/seq_customization.shconfig_tasks/config_blocklm_10B_cnndm.json 中的超参数。

文本填充

  1. 下载Yahoo数据,参考scripts/finetune_blank.sh中的样例步骤,修改 DATA_ROOT, CHECKPOINT_PATH, SAVE_PATH 参数为当前对应的本地路径;

  2. 在模型配置文件中的参数路径,例如模型配置文件config_tasks/model_blocklm_large.sh、任务配置文件config_tasks/seq_cnndm_org.sh;

  3. 执行脚本:

bash scripts/finetune_blank.sh \
     config_tasks/model_blocklm_large.sh \
     config_tasks/seq_blank.sh

推理

  1. 根据需求修改--model_name_or_path模型地址;

  2. 添加环境变量

pip install -U huggingface_hub hf_transfer
export export HF_ENDPOINT=https://hf-mirror.com
  1. 执行:
python inference.py --model_name_or_path THUDM/glm-10b

result

精度

测试数据:智源glm_trian_data数据,使用的加速卡:K100。

devicedtypeparamsacc
A100fp16bs=8, lr=1e-050.808
K100fp16bs=8, lr=1e-050.804

应用场景

算法类别

多轮对话

热点应用行业

家居,教育,科研

源码仓库及问题反馈

参考资料

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术瘾君子1573

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值