【书生大模型实战营第三期 | 基础岛第2关-8G 显存玩转书生大模型 Demo】

学习心得:8G 显存玩转书生大模型 Demo

摘要

本文是对《8G 显存玩转书生大模型 Demo》文档的学习心得。通过阅读文档,我了解了如何在有限的硬件资源下部署和使用大型语言模型,包括InternLM2-Chat-1.8B、InternLM-XComposer2-VL-1.8B和InternVL2-2B模型。文档详细介绍了开发机的创建、环境配置、模型部署以及使用不同工具进行模型服务的方法。

文章大纲

  1. 开发机的创建与环境配置

    • 选择开发机的镜像和配置
    • 环境的创建与激活
    • 必要的软件安装
  2. 模型部署

    • 使用Cli Demo部署InternLM2-Chat-1.8B模型
    • 使用Streamlit Web Demo部署InternLM2-Chat-1.8B模型
    • 使用LMDeploy部署InternLM-XComposer2-VL-1.8B和InternVL2-2B模型
  3. 工具使用

    • 介绍Streamlit和LMDeploy工具
    • 演示如何使用这些工具启动模型服务
  4. 问题解决

    • 遇到inotify watch limit reached问题的解决方法

总结

通过学习这篇文档,我认识到了在资源有限的情况下,如何高效地部署和运行大型语言模型。文档中提供的详细步骤和代码示例,让我对模型部署有了更深入的理解。特别是使用LMDeploy工具,它不仅提供了高效的推理性能,还支持量化和模型服务的轻松分发。此外,文档中对问题的解决策略也让我意识到在实际操作中遇到问题时的应对方法。总体来说,这是一次宝贵的学习经历,为我日后在AI领域的研究和实践打下了坚实的基础。

心得体会

学习这篇文档让我深刻体会到了技术文档的重要性。它不仅提供了具体的操作步骤,还涵盖了可能遇到的问题及其解决方案。这种详尽的指导对于初学者来说尤其宝贵,能够帮助他们快速上手并克服学习过程中的障碍。同时,我也认识到了持续学习和实践的重要性,因为只有通过实际操作,才能真正掌握知识并解决实际问题。

闯关任务

环境准备

# 创建环境
conda create -n demo python=3.10 -y
# 激活环境
conda activate demo
# 安装 torch
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y
# 安装其他依赖
pip install transformers==4.38
pip install sentencepiece==0.1.99
pip install einops==0.8.0
pip install protobuf==5.27.2
pip install accelerate==0.33.0
pip install streamlit==1.37.0

基础任务

创建目录&脚本

mkdir -p /root/demo
touch /root/demo/cli_demo.py

编辑脚本内容

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


model_name_or_path = "/root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True, device_map='cuda:0')
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='cuda:0')
model = model.eval()

system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
"""

messages = [(system_prompt, '')]

print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")

while True:
    input_text = input("\nUser  >>> ")
    input_text = input_text.replace(' ', '')
    if input_text == "exit":
        break

    length = 0
    for response, _ in model.stream_chat(tokenizer, input_text, messages):
        if response is not None:
            print(response[length:], flush=True, end="")
            length = len(response)

执行脚本:

python /root/demo/cli_demo.py

效果如下:
在这里插入图片描述

进阶任务

使用 LMDeploy 完成 InternLM-XComposer2-VL-1.8B 的部署

克隆代码

cd /root/demo
git clone https://github.com/InternLM/Tutorial.git

安装依赖

conda activate demo
pip install lmdeploy[all]==0.5.1
pip install timm==1.0.7

启动服务

lmdeploy serve gradio /share/new_models/Shanghai_AI_Laboratory/internlm-xcomposer2-vl-1_8b --cache-max-entry-count 0.1

端口映射

ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p 你的 ssh 端口号

效果如下
在这里插入图片描述
在这里插入图片描述

使用 LMDeploy 完成 InternVL2-2B 的部署

启动服务

conda activate demo
lmdeploy serve gradio /share/new_models/OpenGVLab/InternVL2-2B --cache-max-entry-count 0.1

效果如下
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值