Zhipu AI/Dense

glm-4-9b

chatTool Use
9.4B
Parameters
128K
Context length
6
Benchmarks
4
Quantizations
198K
HF downloads
Architecture
Dense
Released
2024-06-04
Layers
40
KV Heads
2
Head Dim
128
Family
glm

GLM-4-9B-Chat

Read this in English.

2024/11/25, 我们建议使用从 transformers>=4.46.0 开始,使用 glm-4-9b-chat-hf 以减少后续 transformers 升级导致的兼容性问题。

2024/08/12, 本仓库代码已更新并使用 transformers>=4.44.0, 请及时更新依赖。

2024/07/24,我们发布了与长文本相关的最新技术解读,关注 这里 查看我们在训练 GLM-4-9B 开源模型中关于长文本技术的技术报告

模型介绍

GLM-4-9B 是智谱 AI 推出的最新一代预训练模型 GLM-4 系列中的开源版本。 在语义、数学、推理、代码和知识等多方面的数据集测评中,GLM-4-9B 及其人类偏好对齐的版本 GLM-4-9B-Chat 均表现出较高的性能。 除了能进行多轮对话,GLM-4-9B-Chat 还具备网页浏览、代码执行、自定义工具调用(Function Call)和长文本推理(支持最大 128K 上下文)等高级功能。 本代模型增加了多语言支持,支持包括日语,韩语,德语在内的 26 种语言。我们还推出了支持 1M 上下文长度(约 200 万中文字符)的模型。

评测结果

我们在一些经典任务上对 GLM-4-9B-Chat 模型进行了评测,并得到了如下的结果:

ModelAlignBench-v2MT-BenchIFEvalMMLUC-EvalGSM8KMATHHumanEvalNCB
Llama-3-8B-Instruct5.128.0068.5868.451.379.630.062.224.7
ChatGLM3-6B3.975.5028.166.469.072.325.758.511.3
GLM-4-9B-Chat6.618.3569.072.475.679.650.671.832.2

长文本

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

needle

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

leaderboard

多语言能力

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

DatasetLlama-3-8B-InstructGLM-4-9B-ChatLanguages
M-MMLU49.656.6all
FLORES25.028.8ru, es, de, fr, it, pt, pl, ja, nl, ar, tr, cs, vi, fa, hu, el, ro, sv, uk, fi, ko, da, bg, no
MGSM54.065.3zh, en, bn, de, es, fr, ja, ru, sw, te, th
XWinograd61.773.1zh, en, fr, jp, ru, pt
XStoryCloze84.790.7zh, en, ar, es, eu, hi, id, my, ru, sw, te
XCOPA73.380.1zh, et, ht, id, it, qu, sw, ta, th, tr, vi

工具调用能力

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

ModelOverall Acc.AST SummaryExec SummaryRelevance
Llama-3-8B-Instruct58.8859.2570.0145.83
gpt-4-turbo-2024-04-0981.2482.1478.6188.75
ChatGLM3-6B57.8862.1869.785.42
GLM-4-9B-Chat81.0080.2684.4087.92

本仓库是 GLM-4-9B-Chat 的模型仓库,支持128K上下文长度。

运行模型

更多推理代码和依赖信息,请访问我们的 github

请严格按照依赖安装,否则无法正常运行。

使用 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

# GLM-4-9B-Chat
# 如果遇见 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)

协议

GLM-4 模型的权重的使用则需要遵循 LICENSE

引用

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

@misc{glm2024chatglm,
      title={ChatGLM: A Family of Large Language Models from GLM-130B to GLM-4 All Tools}, 
      author={Team GLM and Aohan Zeng and Bin Xu and Bowen Wang and Chenhui Zhang and Da Yin and Diego Rojas and Guanyu Feng and Hanlin Zhao and Hanyu Lai and Hao Yu and Hongning Wang and Jiadai Sun and Jiajie Zhang and Jiale Cheng and Jiayi Gui and Jie Tang and Jing Zhang and Juanzi Li and Lei Zhao and Lindong Wu and Lucen Zhong and Mingdao Liu and Minlie Huang and Peng Zhang and Qinkai Zheng and Rui Lu and Shuaiqi Duan and Shudan Zhang and Shulin Cao and Shuxun Yang and Weng Lam Tam and Wenyi Zhao and Xiao Liu and Xiao Xia and Xiaohan Zhang and Xiaotao Gu and Xin Lv and Xinghan Liu and Xinyi Liu and Xinyue Yang and Xixuan Song and Xunkai Zhang and Yifan An and Yifan Xu and Yilin Niu and Yuantao Yang and Yueyan Li and Yushi Bai and Yuxiao Dong and Zehan Qi and Zhaoyu Wang and Zhen Yang and Zhengxiao Du and Zhenyu Hou and Zihan Wang},
      year={2024},
      eprint={2406.12793},
      archivePrefix={arXiv},
      primaryClass={id='cs.CL' full_name='Computation and Language' is_active=True alt_name='cmp-lg' in_archive='cs' is_general=False description='Covers natural language processing. Roughly includes material in ACM Subject Class I.2.7. Note that work on artificial languages (programming languages, logics, formal systems) that does not explicitly address natural-language issues broadly construed (natural-language processing, computational linguistics, speech, text retrieval, etc.) is not appropriate for this area.'}
}

Quantizations & VRAM

Q4_K_M4.5 bpw
5.8 GB
VRAM required
94%
Quality
Q6_K6.5 bpw
8.1 GB
VRAM required
97%
Quality
Q8_08 bpw
9.9 GB
VRAM required
100%
Quality
FP1616 bpw
19.3 GB
VRAM required
100%
Quality

Benchmarks (6)

IFEval65.1
BBH20.7
MMLU-PRO19.7
MATH8.5
GPQA7.0
MUSR2.2

Run with Ollama

$ollama run glm:9b

GPUs that can run this model

At Q4_K_M quantization. Sorted by minimum VRAM.

NVIDIA RTX 3050 6GB
6 GB VRAM • 168 GB/s
NVIDIA
$169
Intel Arc A380
6 GB VRAM • 186 GB/s
INTEL
$129
NVIDIA RTX 2060 6GB
6 GB VRAM • 336 GB/s
NVIDIA
$150
NVIDIA GTX 1660 SUPER
6 GB VRAM • 336 GB/s
NVIDIA
$150
NVIDIA GTX 1660 Ti
6 GB VRAM • 288 GB/s
NVIDIA
$140
NVIDIA GTX 1060 6GB
6 GB VRAM • 192 GB/s
NVIDIA
$80
NVIDIA Tesla C2070
6 GB VRAM • 143 GB/s
NVIDIA
NVIDIA Tesla C2075
6 GB VRAM • 150 GB/s
NVIDIA
NVIDIA Tesla C2090
6 GB VRAM • 177 GB/s
NVIDIA
NVIDIA Tesla M2070
6 GB VRAM • 150 GB/s
NVIDIA
NVIDIA Tesla M2070-Q
6 GB VRAM • 150 GB/s
NVIDIA
NVIDIA Tesla M2075
6 GB VRAM • 150 GB/s
NVIDIA
NVIDIA Tesla M2090
6 GB VRAM • 177 GB/s
NVIDIA
NVIDIA Tesla X2070
6 GB VRAM • 177 GB/s
NVIDIA
NVIDIA Tesla X2090
6 GB VRAM • 177 GB/s
NVIDIA
NVIDIA Tesla K20X
6 GB VRAM • 250 GB/s
NVIDIA
NVIDIA Tesla K20Xm
6 GB VRAM • 250 GB/s
NVIDIA
NVIDIA GeForce GTX 1060 6 GB
6 GB VRAM • 192 GB/s
NVIDIA
NVIDIA GeForce GTX 1060 6 GB 9Gbps
6 GB VRAM • 217 GB/s
NVIDIA
NVIDIA GeForce GTX 1060 6 GB GDDR5X
6 GB VRAM • 192 GB/s
NVIDIA
NVIDIA GeForce GTX 1060 6 GB GP104
6 GB VRAM • 192 GB/s
NVIDIA
NVIDIA GeForce GTX 1060 6 GB Rev. 2
6 GB VRAM • 192 GB/s
NVIDIA
NVIDIA GeForce GTX 1660
6 GB VRAM • 192 GB/s
NVIDIA
NVIDIA GeForce GTX 1660 SUPER
6 GB VRAM • 336 GB/s
NVIDIA
NVIDIA GeForce GTX 1660 Ti
6 GB VRAM • 288 GB/s
NVIDIA
NVIDIA GeForce RTX 2060
6 GB VRAM • 336 GB/s
NVIDIA
$140
NVIDIA GeForce RTX 2060 TU104
6 GB VRAM • 336 GB/s
NVIDIA
$140
AMD Radeon RX 5600 OEM
6 GB VRAM • 288 GB/s
AMD
AMD Radeon RX 5600 XT
6 GB VRAM • 288 GB/s
AMD
$90
AMD Radeon RX 5600M
6 GB VRAM • 288 GB/s
AMD

Find the best GPU for glm-4-9b

Build Hardware for glm-4-9b