GLM-TTS Windows + WSL2 部署过程记录

GLM-TTS是智谱刚刚开源的一个基于Transformer的文本到语音(TTS)模型,本分享了我在Windows系统上使用WSL2(Windows Subsystem for Linux)部署GLM-TTS推理环境的完整流程。过程中大概率会碰到依赖构建失败问题,或者依赖缺少问题。可以直接跳到后面章节查看处理方式,因为那也是我碰到的问题。

系统环境

硬件环境

  • 操作系统: Windows 11 专业版 25H2
  • 内存: 32GB
  • GPU: 移动版的NVidia

软件环境

  • WSL2: 已安装并配置的WSL2环境,Ubuntu 22.04 LTS
  • Python: Python 3.12
  • CUDA: CUDA 11.8

第一步:WSL2环境配置

1.1 启用WSL2功能

以管理员身份打开PowerShell,执行以下命令:

1
2
3
4
5
6
7
8
# 启用WSL功能
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

# 启用虚拟机平台
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

# 重启计算机
Restart-Computer

1.2 安装WSL2 Linux发行版

重启后,打开Microsoft Store,搜索并安装Ubuntu 22.04 LTS。

或者使用命令行安装:

1
2
3
4
5
# 查看可用的发行版
wsl --list --online

# 安装Ubuntu 22.04
wsl --install -d Ubuntu-22.04

1.3 设置WSL2为默认版本

1
wsl --set-default-version 2

第二步:WSL2环境准备

2.1 启动WSL2并更新系统

打开Ubuntu终端,执行:

后续命令都是在wsl的shell下执行

1
2
3
4
5
# 更新系统包管理器
sudo apt update && sudo apt upgrade -y

# 安装基础工具
sudo apt install -y wget curl git vim build-essential

2.2 安装Miniconda(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
# 下载并安装Miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b

# 激活conda
source ~/miniconda3/bin/activate

# 初始化conda(可选,将conda添加到bashrc)
~/miniconda3/bin/conda init bash
source ~/.bashrc

# 验证conda安装
conda --version

2.3 安装CUDA(如使用GPU)

1
2
3
4
5
6
7
8
# 下载并安装CUDA 11.8
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run --toolkit --silent --override

# 添加CUDA到环境变量
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

第三步:GLM-TTS项目部署

3.1 克隆项目仓库

在WSL2中执行:

1
2
3
4
5
6
7
# 克隆GLM-TTS仓库
git clone https://github.com/zai-org/GLM-TTS.git
cd GLM-TTS

# 或者如果项目已经在Windows目录,可以链接到WSL2
# 假设Windows项目路径为 /mnt/e/workspace/videos/glm-tts
cd /mnt/e/workspace/videos/glm-tts

3.2 创建Conda虚拟环境

1
2
3
4
5
6
7
8
9
10
11
12
# 创建名为glm-tts的conda环境,指定Python 3.12
conda create -n glm-tts python=3.12 -y

# 激活conda环境
conda activate glm-tts

# 升级pip
pip install --upgrade pip

# 验证环境
python --version
which python

3.3 安装项目依赖

1
2
3
4
5
6
7

# 安装项目requirements
pip install -r requirements.txt

# 安装一些可能需要的额外依赖
conda install -c conda-forge ffmpeg libsndfile -y
pip install soundfile librosa

3.4 安装可选依赖(强化学习模块)

1
2
3
4
5
6
7
8
9
10
11
# 进入grpo目录
cd grpo/modules

# 克隆s3prl仓库
git clone https://github.com/s3prl/s3prl

# 克隆LaughterSegmentation仓库
git clone https://github.com/omine-me/LaughterSegmentation

# 返回项目根目录
cd ../..

第四步:模型下载

4.1 创建模型目录

1
mkdir -p ckpt

4.2 从ModelScope下载模型

1
2
3
4
5
# 安装modelscope
pip install -U modelscope

# 下载模型文件
modelscope download --model ZhipuAI/GLM-TTS --local_dir ckpt

第五步:环境验证

5.1 验证Conda环境

1
2
3
4
5
6
7
8
9
10
# 验证conda环境
conda info
conda list | grep python

# 验证PyTorch环境
python -c "import torch; print(f'PyTorch版本: {torch.__version__}'); print(f'CUDA可用: {torch.cuda.is_available()}')"

# 验证conda环境中的Python路径
which python
python --version

5.2 验证模型文件

1
2
3
4
5
6
7
8
9
# 检查模型文件是否完整
ls -la ckpt/

# 应该看到类似以下文件:
# - tokenizer.model
# - pytorch_model-00001-of-00002.bin
# - pytorch_model-00002-of-00002.bin
# - config.json
# - 等等...

第六步:运行推理测试

6.1 命令行推理测试

1
2
3
4
5
6
7
8
9
10
11
12
# 基本推理测试
python glmtts_inference.py \
--data=example_zh \
--exp_name=_test \
--use_cache

# 使用phoneme功能的推理(可选)
# python glmtts_inference.py \
# --data=example_zh \
# --exp_name=_test \
# --use_cache \
# --phoneme

6.2 启动Web界面

1
2
3
4
# 启动Gradio交互界面
python -m tools.gradio_app

# 访问 http://localhost:7860 使用Web界面

第七步:常见问题解决

7.1 内存不足问题

如果遇到内存不足错误:

1
2
3
4
5
6
# 减少批处理大小
python glmtts_inference.py --data=example_zh --batch_size=1

# 使用CPU模式(如果GPU内存不足),这会非常慢
export CUDA_VISIBLE_DEVICES=""
python glmtts_inference.py --data=example_zh

7.2 WSL2与Windows文件系统权限问题

如果项目在Windows文件系统中:

1
2
# 在WSL2中处理Windows文件时,可能需要修复权限
sudo chmod -R 755 /mnt/e/workspace/videos/glm-tts

7.3 依赖构建失败问题,或者依赖缺少问题

  1. 构建sentencepiece失败
1
2
# 直接pip安装sentencepiece
pip install sentencepiece
  1. 构建pynini失败
1
2
# 通过conda安装pynini
conda install -c conda-forge pynini=2.1.5
  1. 运行时缺少soxr库
1
2
# 安装soxr库
pip install soxr

如果还有其他同类的问题,都可以尝试直接通过pip安装,或者通过conda安装,版本可能不一致,但只要后面运行ok,应该就没问题。

如果还遇到任何问题,欢迎留言一块讨论。