fastvlm
1 概述¶
1.1 背景介绍¶
fastvlm是apple团队开源的多模态大模型,起开源项目地址为:
https://github.com/apple/ml-fastvlm
模型下载地址为:
https://huggingface.co/lhuanyu/fastvlm_1.5b
2 模型转换¶
2.1 offline模型转换¶
模型转换参考SGS_IPU_Toolchain路径下的doc/SDK_Doc_Release/M60/module/LLMConverter/llm_index.html,里面记录详细的大模型转换过程
2.2 offline模型¶
转换后模型拆分为
-
decoder_model_fixed.sim_sgsimg.img
-
norm_head_fixed.sim_sgsimg.img
-
FastVLMVision_fixed.sim_sgsimg.img
-
token_embedding_weight.npy
-
cos_weight.npy
-
sin_weight.npy
一般大模型的head和embedding的权重是共享的,所以转换模型会是tie_embedding模式,参数转换到了token_embedding_weight.npy。
| 模型名称 | 模型介绍 |
|---|---|
| decoder_model_fixed.sim_sgsimg.img | 模型decoder部分的权重和graph |
| norm_head_fixed.sim_sgsimg.img | 模型 head 部分的graph |
| token_embedding_weight.npy | 模型embedding部分的权重 |
| FastVLMVision_fixed.sim_sgsimg.img | 模型vision部分的权重和graph |
| cos_weight.npy | 模型位置编码的cos部分的权重 |
| sin_weight.npy | 模型位置编码的sin部分的权重 |
2.3 模型实现¶
-
sdk/verify/opendla/source/llm/modular/vision_decoder.cpp
实现了decoder_model_fixed.sim_sgsimg.img, token_embedding.npy,cos_weight.npy,sin_weight.npy的加载,子图和kv_cache内存的管理,实现了decoder模型推理的全流程。
decoder_model_fixed.sim_sgsimg.img中含有多张prefill 和decode 过程的子图,prefill的子图每次推理的token数一般设为了128,kv_cache对应最大的token_legnth逐渐增大,比如512/1024/2048等。decode的子图每次推理的数量为1,最大的token_legnth和prefill对应。在调用decoder_model_fixed.sim_sgsimg.img模型时,会根据不同的阶段和输入调用不同的子图,具体参考sdk/verify/opendla/source/vlm/fastvlm/fastvlm.cpp
-
sdk/verify/opendla/source/llm/modular/norm_head.cpp
实现了norm_head_fixed.sim_sgsimg.img 模型的加载和推理流程。
-
sdk/verify/opendla/source/llm/modular/vision.cpp
实现了FastVLMVision_fixed.sim_sgsimg.img 模型的加载和推理流程。
3 板端部署¶
3.1 程序编译¶
示例程序编译之前需要先根据板子(nand/nor/emmc, ddr型号等)选择deconfig进行sdk整包编译, 具体可以参考alkaid sdk sigdoc《开发环境搭建》文档。
-
编译板端fastvlm示例。
$cd sdk/verify/opendla $make clean && make source/vlm/fastvlm -j8 -
最终生成的可执行文件地址
sdk/verify/opendla/out/${AARCH}/app/prog_vlm_fastvlm -
备注:此demo同样可用于其他的大模型推理,只要其模型和fastvlm是一致的
3.2 运行文件¶
运行程序时, 需要准备以下几个文件
- prog_vlm_fastvlm
- fastvlm 离线模型
- apple.jpg 存放在sdk/verify/opendla/source/resource/apple.jpg
3.3 运行说明¶
-
Usage:
./prog_vlm_fastvlm -d ./fastvlm/ -m ./apple.jpg -p "Briefly describe the content of the picture"(执行文件使用命令)- -d 指向转换后的fastvlm模型,里面包含转换的模型文件以及对应的json文件
- -m 指向图片的路径
- -p 指的是提示词,demo现在传的是 "Briefly describe the content of the picture"
-
Typical Output:
./prog_vlm_fastvlm -d ./fastvlm/ -m ./apple.jpg -p "Briefly descri be the content of the picture" Assistant: image features a highly detailed, close-up photograph of a red apple with a green leaf attached to its stem. The apple, which is round and predominantly red with a few yellowish spots, is positioned centrally against a white background. The lighting is bright, casting a subtle shadow beneath the apple, enhancing its three-dimensional appearance. The leaf, with its green stem and veins, is attached to the top of the apple, adding a touch of natural realism. The image is of high quality, with a focus on the apple's texture and color, making it suitable for use in a variety of contexts, such as a stock photo for a website or a stock image for a company's website.