AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


清华智谱最新GLM4 –9B模型很惊艳
发布日期:2024-06-07 06:17:11 浏览次数: 3785


编者:清华智谱GLM4-9B很惊艳。对比之前介绍的Cohere Aya 23B,以及Llama3 -8B很有特点。其中有一点与加拿大Cohere Aya类似就是支持的语言到达26种,除了中文英文,更包括东亚日文,韩文及俄语、法语、德语、西班牙语、阿拉伯语等。这为中国企业全球化和一带一路的出海数字化支持提供了最坚实的新质生产力引擎。上下文长度达到1M,训练数据量达到10T.而且具有多模态支持能力,所以眼前很一亮,可以用夏日的大模型市场惊鸿一瞥来描述,值得深入试用和推荐。

目录

1关于GLM4-9B

2 9B模型介绍

3模型列表

4评测结果

4.1基座模型典型任务

4.2长文本

5多语言能力

6工具调用能力

7多模态能力

8快速调用

8.1硬件配置和系统要求

8.2基础功能调用

8.3使用以下方法快速调用 GLM-4-9B-Chat 语言模型

8.4使用以下方法快速调用 GLM-4V-9B 多模态模型

8.5完整项目列表

8.6友情链接

8.7协议

8.8引用

9附录 高效微调架构框架

1  关于GLM4-9B

GLM-4系列:开源多语言多模态对话模型

开源许可:Apache-2.0 license

Stars 1.8k stars(Hugging Face


2  GLM4-9B模型介绍

GLM-4-9B是智谱 AI 推出的最新一代预训练模型 GLM-4 系列中的开源版本。 在语义、数学、推理、代码和知识等多方面的数据集测评中, GLM-4-9B 及其人类偏好对齐的版本 GLM-4-9B-Chat 均表现出超越 Llama-3-8B 的卓越性能。除了能进行多轮对话,GLM-4-9B-Chat 还具备网页浏览、代码执行、自定义工具调用(Function Call)和长文本推理(支持最大 128K 上下文)等高级功能。本代模型增加了多语言支持,支持包括日语,韩语,德语在内的 26 种语言。我们还推出了支持 1M 上下文长度(约 200 万中文字符)的 GLM-4-9B-Chat-1M 模型和基于 GLM-4-9B 的多模态模型 GLM-4V-9BGLM-4V-9B 具备 1120 * 1120 高分辨率下的中英双语多轮对话能力,在中英文综合能力、感知推理、文字识别、图表理解等多方面多模态评测中,GLM-4V-9B 表现出超越 GPT-4-turbo-2024-04-09Gemini 1.0 ProQwen-VL-Max  Claude 3 Opus 的卓越性能。

3 模型列表

4 评测结果

对话模型典型任务

4.1 基座模型典型任务

由于 GLM-4-9B 在预训练过程中加入了部分数学、推理、代码相关的 instruction 数据,

所以将 Llama-3-8B-Instruct 也列入比较范围。

4.2长文本

 1M 的上下文长度下进行大海捞针实验,结果如下:

 LongBench-Chat 上对长文本能力进行了进一步评测,结果如下:

5  多语言能力

在六个多语言数据集上对 GLM-4-9B-Chat  Llama-3-8B-Instruct 进行了测试,测试结果及数据集对应选取语言如下表

6  工具调用能力

我们在 Berkeley Function Calling Leaderboard 上进行了测试并得到了以下结果:

7  多模态能力

GLM-4V-9B是一个多模态语言模型,具备视觉理解能力,其相关经典任务的评测结果如下:

8  快速调用

8.1  硬件配置和系统要求

 demo 中,你将体验到如何使用 GLM-4-9B 开源模型进行基本的任务。

请严格按照文档的步骤进行操作,以避免不必要的错误。

设备和依赖检查

相关推理测试数据

本文档的数据均在以下硬件环境测试,实际运行环境需求和运行占用的显存略有不同,请以实际运行环境为准。

测试硬件信息:

  • OS: Ubuntu 22.04

  • Memory: 512GB

  • Python: 3.12.3

  • CUDA Version: 12.3

  • GPU Driver: 535.104.05

  • GPU: NVIDIA A100-SXM4-80GB * 8

相关推理的压力测试数据如下:

所有测试均在单张GPU上进行测试,所有显存消耗都按照峰值左右进行测算

GLM-4-9B-Chat

精度

显存占用

Prefilling

Decode Speed

Remarks

BF16

19 GB

0.2s

27.8 tokens/s

输入长度为 1000

BF16

21 GB

0.8s

31.8 tokens/s

输入长度为 8000

BF16

28 GB

4.3s

14.4 tokens/s

输入长度为 32000

BF16

58 GB

38.1s

3.4 tokens/s

输入长度为 128000

精度

显存占用

Prefilling

Decode Speed

Remarks

INT4

8 GB

0.2s

23.3 tokens/s

输入长度为 1000

INT4

10 GB

0.8s

23.4 tokens/s

输入长度为 8000

INT4

17 GB

4.3s

14.6 tokens/s

输入长度为 32000

GLM-4-9B-Chat-1M

精度

显存占用

Prefilling

Decode Speed

Remarks

BF16

75 GB

98.4s

2.3 tokens/s

输入长度为 200000

如果您的输入超过200K,我们建议您使用vLLM后端进行多卡推理,以获得更好的性能。

GLM-4V-9B

精度

显存占用

Prefilling

Decode Speed

Remarks

BF16

28 GB

0.1s

33.4 tokens/s

输入长度为 1000

BF16

33 GB

0.7s

39.2 tokens/s

输入长度为 8000

精度

显存占用

Prefilling

Decode Speed

Remarks

INT4

10 GB

0.1s

28.7 tokens/s

输入长度为 1000

INT4

15 GB

0.8s

24.2 tokens/s

输入长度为 8000

最低硬件要求

如果您希望运行官方提供的最基础代码 (transformers 后端) 您需要:

  • Python >= 3.10

  • 内存不少于 32 GB

如果您希望运行官方提供的本文件夹的所有代码,您还需要:

  • Linux 操作系统 (Debian 系列最佳)

  • 大于 8GB 显存的,支持 CUDA 或者 ROCM 并且支持 BF16 推理的 GPU 设备。(FP16 精度无法训练,推理有小概率出现问题)

安装依赖

pip install -r requirements.txt

8.2 基础功能调用

除非特殊说明,本文件夹所有 demo 并不支持 Function Call 和 All Tools 等进阶用法

使用 transformers 后端代码

  • 使用命令行与 GLM-4-9B 模型进行对话。

python trans_cli_demo.py # GLM-4-9B-Chat

python trans_cli_vision_demo.py # GLM-4V- 

使用 Gradio 网页端与 GLM-4-9B-Chat 模型进行对话。

python trans_web_demo.py

使用 Batch 推理。

python cli_batch_request_demo.py使用vLLM 后端代码
  • 使用命令行与 GLM-4-9B-Chat 模型进行对话。

python vllm_cli_demo.py

自行构建服务端,并使用 OpenAI API 的请求格式与 GLM-4-9B-Chat 模型进行对话。本 demo 支持Function Call 和 All Tools功能。

启动服务端:

python openai_api_server.py

客户端请求:

python openai_api_request.py

压力测试

用户可以在自己的设备上使用本代码测试模型在 transformers后端的生成速度:

python trans_stress_test.py

8.3 快速调用 GLM-4-9B-Chat 语言模型

使用 transformers 后端进行推理:

import torch

from transformers import AutoModelForCausalLM, AutoTokenizer

device = "cuda"

tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-4-9b-chat", trust_remote_code=True)

query = "你好"

inputs = tokenizer.apply_chat_template([{"role": "user", "content": query}],

                                       add_generation_prompt=True,

                                       tokenize=True,

                                       return_tensors="pt",

                                       return_dict=True

                                       )

inputs = inputs.to(device)

model = AutoModelForCausalLM.from_pretrained(

    "THUDM/glm-4-9b-chat",

    torch_dtype=torch.bfloat16,

    low_cpu_mem_usage=True,

    trust_remote_code=True

).to(device).eval()

gen_kwargs = {"max_length": 2500, "do_sample": True, "top_k": 1}

with torch.no_grad():

    outputs = model.generate(**inputs, **gen_kwargs)

    outputs = outputs[:, inputs['input_ids'].shape[1]:]

    print(tokenizer.decode(outputs[0], skip_special_tokens=True))使用 vLLM 后端进行推理:
from transformers import AutoTokenizer

from vllm import LLM, SamplingParams

# GLM-4-9B-Chat-1M

# max_model_len, tp_size = 1048576, 4

# 如果遇见 OOM 现象,建议减少max_model_len,或者增加tp_size

max_model_len, tp_size = 131072, 1

model_name = "THUDM/glm-4-9b-chat"

prompt = [{"role": "user", "content": "你好"}]

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

llm = LLM(

    model=model_name,

    tensor_parallel_size=tp_size,

    max_model_len=max_model_len,

    trust_remote_code=True,

    enforce_eager=True,

    # GLM-4-9B-Chat-1M 如果遇见 OOM 现象,建议开启下述参数

    # enable_chunked_prefill=True,

    # max_num_batched_tokens=8192

)

stop_token_ids = [151329, 151336, 151338]

sampling_params = SamplingParams(temperature=0.95, max_tokens=1024, stop_token_ids=stop_token_ids)

inputs = tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)

outputs = llm.generate(prompts=inputs, sampling_params=sampling_params)

print(outputs[0].outputs[0].text)

8.4  快速调用 GLM-4V-9B 多模态模型

使用 transformers 后端进行推理:

import torch

from PIL import Image

from transformers import AutoModelForCausalLM, AutoTokenizer

device = "cuda"

tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-4v-9b", trust_remote_code=True)

query = '描述这张图片'

image = Image.open("your image").convert('RGB')

inputs = tokenizer.apply_chat_template([{"role": "user", "image": image, "content": query}],

                                       add_generation_prompt=True, tokenize=True, return_tensors="pt",

                                       return_dict=True)  # chat mode

inputs = inputs.to(device)

model = AutoModelForCausalLM.from_pretrained(

    "THUDM/glm-4v-9b",

    torch_dtype=torch.bfloat16,

    low_cpu_mem_usage=True,

    trust_remote_code=True

).to(device).eval()

gen_kwargs = {"max_length": 2500, "do_sample": True, "top_k": 1}

with torch.no_grad():

    outputs = model.generate(**inputs, **gen_kwargs)

    outputs = outputs[:, inputs['input_ids'].shape[1]:]

    print(tokenizer.decode(outputs[0])注意: GLM-4V-9B 暂不支持使用 vLLM 方式调用。

8.5  完整项目列表

如果你想更进一步了解 GLM-4-9B 系列开源模型,本开源仓库通过以下内容为开发者提供基础的 GLM-4-9B的使用和开发代码

·base在这里包含了

    • 使用 transformers  vLLM 后端的交互代码

    • OpenAI API 后端交互代码

    • Batch 推理代码

·composite_demo在这里包含了

    • GLM-4-9B-Chat 以及 GLM-4V-9B 开源模型的完整功能演示代码,包含了 All Tools 能力、长文档解读和多模态能力的展示。

·fintune_demo在这里包含了

    • PEFT (LORA, P-Tuning) 微调代码

    • SFT 微调代码

8.6   友情链接

  • LLaMA-Factory

  • 高效开源微调框架,已支持 GLM-4-9B-Chat 语言模型微调。

8.7  开源协议

·GLM-4 模型的权重的使用则需要遵循 模型协议。

·本开源仓库的代码则遵循 Apache 2.0 协议。

请您严格遵循开源协议。

8.8  引用

如果你觉得我们的工作有帮助的话,请考虑引用下列论文。

@inproceedings{zeng2022glm,

  title={{GLM-130B:} An Open Bilingual Pre-trained Model},

  author={Zeng, Aohan and Liu, Xiao and Du, Zhengxiao and Wang, Zihan and Lai, Hanyu and Ding, Ming and Yang, Zhuoyi and Xu, Yifan and Zheng, Wendi and Xia, Xiao and others},

  booktitle={The Eleventh International Conference on Learning Representations,

                  {ICLR} 2023, Kigali, Rwanda, May 1-5, 2023},

  year= {2023},

}@inproceedings{du2022glm,

  title={GLM: General Language Model Pretraining with Autoregressive Blank Infilling},

  author={Du, Zhengxiao and Qian, Yujie and Liu, Xiao and Ding, Ming and Qiu, Jiezhong and Yang, Zhilin and Tang, Jie},

  booktitle={Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)},

  pages={320--335},

  year={2022}

}title={CogVLM: Visual Expert for Pretrained Language Models},

      author={Weihan Wang and Qingsong Lv and Wenmeng Yu and Wenyi Hong and Ji Qi and Yan Wang and Junhui Ji and Zhuoyi Yang and Lei Zhao and Xixuan Song and Jiazheng Xu and Bin Xu and Juanzi Li and Yuxiao Dong and Ming Ding and Jie Tang},

      year={2023},

      eprint={2311.03079},

      archivePrefix={arXiv},

      primaryClass={cs.CV}

}

9  附录 高效微调架构框架

     

9.1  调大型语言模型可能很容易,因为......

选择路径:

1.合作:https://colab.research.google.com/drive/1eRTPn37ltBbYsISy9Aw2NuI2Aq5CQrD9?usp=sharing

2.PAI-DSW:https://gallery.pai-ml.com/#/preview/deepLearning/nlp/llama_factory

3.本地机:请参考使用情况

9.2  特征

1.              多种型号:LLaMALLaVAMistralMixtral-MoEQwenYiGemmaBaichuanChatGLMPhi 等。

2.              综合方法:(连续)预训练、(多模态)监督微调、奖励建模、PPODPOKTOORPO等。

3.              可扩展资源32 位全调谐、16 位冻结调谐、16  LoRA  2/4/8  QLoRA,通过AQLM/AWQ/GPTQ/LLM.int8

4.              高级算法GaLoreBAdamDoRALongLoRALLaMA ProMixture-of-DepthsLoRA+LoftQ  Agent 调谐。

5.              实用技巧FlashAttention-2UnslothRoPE 缩放、NEFTune  rsLoRA

6.              实验监视器LlamaBoardTensorBoardWandbMLflow 等。

7.              更快的推理OpenAI 风格的 APIGradio UI  CLI 以及 vLLM worker

9.3  基准

 ChatGLM  P-Tuning 相比,LLaMA Factory  LoRA 调优提供了高达 3.7 的训练速度,并在广告文本生成任务上获得了更好的 Rouge 分数。通过利用 4 位量化技术,LLaMA Factory  QLoRA 进一步提高了 GPU 内存的效率。

9.4 Changelog

[24/06/05] 我们支持对 GLM-4-9B/GLM-4-9B-Chat 型号进行微调。

[24/05/26] 我们支持 SimPO 算法进行偏好学习。有关用法,请参阅示例 

[24/05/20] 我们支持对 PaliGemma  系列模型进行微调。请注意,PaliGemma 模型是预先训练的模型,您需要使用 gemma  模板对其进行微调才能完成聊天。

完整的更新日志

9.5 已经支持 Models

模型

模型尺寸

模版

Baichuan2

7B/13B

baichuan2

BLOOM

560M/1.1B/1.7B/3B/7.1B/176B

-

BLOOMZ

560M/1.1B/1.7B/3B/7.1B/176B

-

ChatGLM3

6B

chatglm3

Command-R

35B/104B

cohere

DeepSeek (MoE)

7B/16B/67B/236B

deepseek

Falcon

7B/11B/40B/180B

falcon

Gemma/CodeGemma

2B/7B

gemma

GLM4

9B

glm4

InternLM2

7B/20B

intern2

LLaMA

7B/13B/33B/65B

-

LLaMA-2

7B/13B/70B

llama2

LLaMA-3

8B/70B

llama3

LLaVA-1.5

7B/13B

vicuna

Mistral/Mixtral

7B/8x7B/8x22B

mistral

OLMo

1B/7B

-

PaliGemma

3B

gemma

Phi-1.5/2

1.3B/2.7B

-

Phi-3

4B/7B/14B

phi

Qwen

1.8B/7B/14B/72B

qwen

Qwen1.5 (Code/MoE)

0.5B/1.8B/4B/7B/14B/32B/72B/110B

qwen

StarCoder2

3B/7B/15B

-

XVERSE

7B/13B/65B

xverse

Yi (1/1.5)

6B/9B/34B

yi

Yi-VL

6B/34B

yi_vl

Yuan

2B/51B/102B

yuan

Note

默认模块用于 lora_target 参数,您可以使用 lora_target: all 来指定所有可用的模块,以便更好地收敛。

对于基本模型,可以从默认羊驼骆马等中选择模板参数。但请确保指导/聊天模型使用相应的模板。

请记住  在训练和推理中使用 SAME 模板。

请参阅 constants.py ,了解我们支持的型号的完整列表。

您还可以向 template.py 添加自定义聊天模板。

9.6 支持的训练方法

9.7 准备数据集


预训练数据集监督微调数据集首选项数据集


某些数据集在使用前需要确认,因此我们建议您使用这些命令使用您的 Hugging Face 帐户登录。

pip install --upgrade huggingface_hub

huggingface-cli login

9.8 配置需求

硬件需求

估算

9.9试用安装指南

安装

重要:安装是强制性的。

git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git

cd LLaMA-Factory

pip install -e .[torch,metrics]

可用的额外依赖项:torch、torch_npu、metrics、deepspeed、bitsandbytes、vllm、galore、badam、gptq、awq、aqlm、qwen、modelscope、quality

Use pip install --no-deps -e . to resolve package conflicts.


对于Windows用户对于Ascend NPU用户

数据准备

请参考 data/README.md  查看数据集文件格式的详细信息。您可以在 HuggingFace/ModelScope 中心使用数据集,也可以将数据集加载到本地磁盘中。

注意

请更新数据/dataset_info.json以使用您的自定义数据集。

快速入门

使用以下 3 个命令分别运行 Llama3-8B-Instruct 模型的 LoRA 微调、推理和合并。


CUDA_VISIBLE_DEVICES=0 llamafactory-cli train examples/lora_single_gpu/llama3_lora_sft.yaml

CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat examples/inference/llama3_lora_sft.yaml

CUDA_VISIBLE_DEVICES=0 llamafactory-cli export examples/merge_lora/llama3_lora_sft.yamlSee examples/README.md for advanced usage (including distributed training).Use llamafactory-cli
help to show help information.

使用 LLaMA 板 GUI 进行微调(由 Gradio 提供支持)

使用本地环境

CUDA_VISIBLE_DEVICES=0 GRADIO_SHARE=1 llamafactory-cli webui
Use Docker

docker build -f ./Dockerfile -t llama-factory:latest .

docker run --gpus=all \

    -v ./hf_cache:/root/.cache/huggingface/ \

    -v ./data:/app/data \

    -v ./output:/app/output \

    -e CUDA_VISIBLE_DEVICES=0 \

    -p 7860:7860 \

    --shm-size 16G \

    --name llama_factory \

    -d llama-factory:latest

使用Docker Compose

docker compose -f ./docker-compose.yml up -d

9.10魔搭 Hub下载

如果在从 Hugging Face 下载模型和数据集时遇到问题,可以使用 魔搭。

export USE_MODELSCOPE_HUB=1 # `set USE_MODELSCOPE_HUB=1` for WindowsTrain the model by specifying a model ID of the ModelScope Hub as themodel_name_or_path. You can find a full list of model IDs at ModelScope Hub, e.g., LLM-Research/Meta-Llama-3-8B-Instruct.

1.1     使用 LLaMA Factory 的项目

如果您有需要合并的项目,请通过电子邮件联系或创建拉取请求。

点击显示

此存储库根据Apache-2.0 许可证获得许可。

请按照型号许可证使用相应的型号权重:

Baichuan2 / BLOOM / ChatGLM3 / Command-R / DeepSeek / Falcon / Gemma / GLM4 / InternLM2 / LLaMA / LLaMA-2 (LLaVA-1.5) / LLaMA-3 / Mistral / OLMo / Phi-1.5/2 / Phi-3 / Qwen / StarCoder2 / XVERSE / Yi / Yi-1.5 / Yuan

9.11 引用

如果这项工作有帮助,请引用为:

@article{zheng2024llamafactory,

  title={LlamaFactory: Unified Efficient Fine-Tuning of 100+ Language Models},

  author={Yaowei Zheng and Richong Zhang and Junhao Zhang and Yanhan Ye and Zheyan Luo and Yongqiang Ma},

  journal={arXiv preprint arXiv:2403.13372},

  year={2024},

  url={http://arxiv.org/abs/2403.13372}

}


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询