Skip to content

issue/349 - Support GLM4 model#370

Merged
wooway777 merged 1 commit into
InfiniTensor:mainfrom
rubik-hua:glm4
May 13, 2026
Merged

issue/349 - Support GLM4 model#370
wooway777 merged 1 commit into
InfiniTensor:mainfrom
rubik-hua:glm4

Conversation

@rubik-hua
Copy link
Copy Markdown

@rubik-hua rubik-hua commented May 12, 2026

根据#352 这个PR里面的检视意见以及本次PR之committer的检视意见,进行了重构
建议参考一下修改点
1、新增模型不应该修改已有模型代码,不要修改llama_legacy文件中的代码。
2、删除config_factory.cpp和rank_worker.cpp的改动
3、参考已有代码实现(非llama_legacy文件夹),mlp model causual_lm 应该可以使用现有的模块。
4、在glm4文件添加如下文件 glm4_decoder_layer.cpp/hpp + glm4_for_causal_lm.cpp/hpp。
5、csrc/models/glm4/glm4_for_causal_lm.cpp中,需要定义一个自己的Glm4ForCausalLM类,不要使用nfinilm::models::llama::LlamaForCausalLM。
6、RoPE类型问题:csrc/layers/rotary_embedding/rotary_embedding.cpp中get_rope函数的功能,在这个函数中处理GPT_J类型和"partial_rotary_factor"超参数。
7、使用字典模式设计weights remap
8、同时支持atention_static和attention_paged
9、中文注释改成英文注释
10、using Glm4ForCausalLM = infinilm::layers::causal_lm_templates::TextCausalLM;复用已有模块
11、删除reset_cache重载

1、模型test_infer.py测试截图:
命令:python examples/test_infer.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/
image
2、推理服务启动
命令:python python/infinilm/server/inference_server.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/
9935ba401a8cf8fa2231e3859dbd0fd5
客户端命令:python scripts/test_perf.py --verbose
客户端部分输出截图:
f86a09cfbd5c50da66238ebf02c4f7ae
98d11eebab0edb1277c98440870cb57f
92bc919e118dae545126d76cfee63a90
f2d15507f83601b54089455da652b5e4
2aa026aa1dcf8e1420154d6fd21be30f

另外由于修改了csrc/layers/rotary_embedding/rotary_embedding.cpp中的代码,algo默认参数为infinicore::nn::RoPE::Algo algo = infinicore::nn::RoPE::Algo::GPT_NEOX,逻辑跟原来一样。
跑两个之前的ok的模型进行验证:
b89e6d56b60f1964e0e77bec054bc1d7
image

根据检视意见修改后在进行验证,加上enable_paged
测试命令:python examples/test_infer.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn
输出:
image

模型服务测试:python python/infinilm/server/inference_server.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn
image

客户端部分输出截图:
image

image

tp并行测试,命令:CUDA_VISIBLE_DEVICES=0,1 python examples/test_infer.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn --tp=2 --prompt="山东最高的山是?"
输出:
image
命令:CUDA_VISIBLE_DEVICES=0,1 python examples/test_infer.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn --tp=1 --prompt="山东最高的山是?"
输出:
image

tp并行推理服务启动命令:CUDA_VISIBLE_DEVICES=0,1 python python/infinilm/server/inference_server.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn --tp=2
image
image

@rubik-hua rubik-hua requested a review from a team May 12, 2026 08:58
Comment thread csrc/models/glm4/glm4_attention_layer.hpp Outdated
Comment thread csrc/models/glm4/glm4_attention.hpp
Comment thread csrc/models/glm4/glm4_for_causal_lm.hpp Outdated
Comment thread csrc/models/glm4/glm4_for_causal_lm.cpp Outdated
Comment thread csrc/models/glm4/glm4_for_causal_lm.hpp Outdated
Comment thread python/infinilm/modeling_utils.py Outdated
Comment thread python/infinilm/modeling_utils.py Outdated
Comment thread csrc/models/glm4/glm4_attention.cpp
Comment thread csrc/models/glm4/glm4_allocate_kv_cache_tensors.cpp Outdated
Comment thread csrc/models/glm4/glm4_for_causal_lm.cpp Outdated
Comment thread csrc/layers/rotary_embedding/rotary_embedding.hpp Outdated
Comment thread csrc/models/glm4/glm4_allocate_kv_cache_tensors.cpp Outdated
@pengcheng888
Copy link
Copy Markdown
Collaborator

建议将中文注释修改为简洁的英文注释,因为个别平台对中文显示不全

Comment thread csrc/models/glm4/glm4_attention.cpp
Comment thread csrc/models/glm4/glm4_attention.hpp
Copy link
Copy Markdown
Collaborator

@wooway777 wooway777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

麻烦再看一眼,当前实现不支持分布式

python examples/test_infer.py --device nvidia --model=/data-aisoft/mechdancer/models/GLM-4-9B-0414/ --enable-paged-attn --tp=2 --prompt="介绍你自己"

python examples/test_infer.py --device nvidia --model=/data-aisoft/mechdancer/models/GLM-4-9B-0414/ --tp=2 --prompt="介绍你自己"

@rubik-hua
Copy link
Copy Markdown
Author

求助下,容器环境下,我试了DeepSeek-R1-Distill-Qwen-7B和Meta-Llama-3-8B-Instruct tp也是报错,能看出来啥原因吗,正常能跑通情况下我才好验证新增的模型对tp的支持。
image
image
我看代码:
image
image

@pengcheng888
Copy link
Copy Markdown
Collaborator

求助下,容器环境下,我试了DeepSeek-R1-Distill-Qwen-7B和Meta-Llama-3-8B-Instruct tp也是报错,能看出来啥原因吗,正常能跑通情况下我才好验证新增的模型对tp的支持。 image image 我看代码: image image

请重新编译InfiniCore. 编译时指定需要编译通信库。 xmake f --nv-gpu=true --cuda=$CUDA_HOME --ccl=y -cv

@pengcheng888
Copy link
Copy Markdown
Collaborator

添加 --ccl=y 选项

根据InfiniTensor#352 这个PR里面的检视意见以及本次PR之committer的检视意见,进行了重构
建议参考一下修改点
1、新增模型不应该修改已有模型代码,不要修改llama_legacy文件中的代码。
2、删除config_factory.cpp和rank_worker.cpp的改动
3、参考已有代码实现(非llama_legacy文件夹),mlp model causual_lm 应该可以使用现有的模块。
4、在glm4文件添加如下文件 glm4_decoder_layer.cpp/hpp + glm4_for_causal_lm.cpp/hpp。
5、csrc/models/glm4/glm4_for_causal_lm.cpp中,需要定义一个自己的Glm4ForCausalLM类,不要使用nfinilm::models::llama::LlamaForCausalLM。
6、RoPE类型问题:csrc/layers/rotary_embedding/rotary_embedding.cpp中get_rope函数的功能,在这个函数中处理GPT_J类型和"partial_rotary_factor"超参数。
7、使用字典模式设计weights remap
8、同时支持atention_static和attention_paged
9、中文注释改成英文注释
10、using Glm4ForCausalLM = infinilm::layers::causal_lm_templates::TextCausalLM;复用已有模块
11、删除reset_cache重载
12、验证tp并行
@rubik-hua
Copy link
Copy Markdown
Author

非常感谢两位committer,我找到问题了,修改了两行代码,重新提交上去了,forward输出维度写错了
image

验证截图在最上面的commnt中也更新了:
CUDA_VISIBLE_DEVICES=0,1 python examples/test_infer.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn --tp=2 --prompt="山东最高的山是?"
image

CUDA_VISIBLE_DEVICES=0,1 python examples/test_infer.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn --tp=1 --prompt="山东最高的山是?"
image

CUDA_VISIBLE_DEVICES=0,1 python python/infinilm/server/inference_server.py --device nvidia --model=/data/rubik/models/GLM-4-9B-0414/ --enable-paged-attn --tp=2
image

image

@rubik-hua rubik-hua requested a review from wooway777 May 13, 2026 09:47
@wooway777 wooway777 merged commit 5194d03 into InfiniTensor:main May 13, 2026
@wooway777
Copy link
Copy Markdown
Collaborator

感谢华老师!

@rubik-hua rubik-hua deleted the glm4 branch May 14, 2026 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants