ModelScope社区
ModelScope 是一个一站式 AI 开发社区,提供各种主流模型。支持推理训练,ModelScope 也提供免费GPU 进行模型测试,在线一键部署,简单易用。今天分享一下,如何通过容器的方式在本地快速搭建并使用 ModelScope 中的模型。
- 安装 Docker GPU 插件
- 启动镜像
- 下载模型
安装 Docker GPU 插件
在 Ubuntu 下的安装方法,如果需要其他系统的安装方式,请到官网查看。
- 安装 APT 源
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
- 更新列表
sudo apt-get update
- 安装 Nvidia 容器插件
sudo apt-get install -y nvidia-container-toolkit
- 配置容器运行时,下面的命令会修改 daemon.json,从而让容器支持 GPU
nvidia-ctk runtime configure --runtime=docker
- 重启容器服务
systemctl restart docker
启动ModelScope 镜像
这两个参数是重点 --runtime=nvidia --gpus all,代表启动容器时需要支持 GPU。容器的版本可以在官网进行查看,ModelScope
## -p 对外打开notebook 端口
## 映射本地模型目录和 Notebook 目录
docker run --rm --runtime=nvidia --gpus all -p 8890:8888 \
-v /opt/modelscope/models:/mnt/workspace/.cache \
-v /opt/modelscope/notebook:/notebook \
-it registry.cn-beijing.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda12.1.0-py310-torch2.1.2-tf2.14.0-1.13.1 /bin/bash
启动 Notebook
jupyter lab --ip=0.0.0.0 --allow-root --notebook-dir=/notebook --no-browser
查看 CUDA 信息
运行 pytorch 程序查看 cuda 信息
import torch
# Check if CUDA is available
print("CUDA Available: ", torch.cuda.is_available())
# Get the number of CUDA devices
print("Number of CUDA Devices: ", torch.cuda.device_count())
# Get the name of each CUDA device
for i in range(torch.cuda.device_count()):
print("CUDA Device #", i, ": ", torch.cuda.get_device_name(i))
# Get current device index, and other properties
if torch.cuda.is_available():
current_device = torch.cuda.current_device()
print("Current CUDA Device index: ", current_device)
print("Current CUDA Device: ", torch.cuda.get_device_name(current_device))
# Get additional device properties
device_properties = torch.cuda.get_device_properties(current_device)
print("Device Properties: ", device_properties)
测试一个简单模型
运行以下代码,如果成功返回,证明 ModelScope 安装成功。
from modelscope.pipelines import pipeline
word_segmentation = pipeline('word-segmentation')
input_str = '今天天气不错,适合出去游玩'
print(word_segmentation(input_str))
总结
ModelScope 提供了非常易用的接口,三行代码就可以推理,支持本地部署,上手简单。如果想通过本地快速部署模型并验证应用场景,ModelScope 是一个不错的选择,国内 HuggingFace 很慢,HuggingFace 上的主流大模型在ModelScope 都可以找到,而且还可以方便的进行调优,ModelScope 也同时开源 Swift LLM训练、调优框架,可以训练私有大模型。