Csm 1b
CSM是由Sesame开发的1B参数语音生成模型,可通过文本和音频输入生成RVQ音频编码,支持带上下文的语音生成。
下载量 5,144
发布时间 : 3/26/2025
模型简介
基于Llama主干网络和轻量级音频解码器的语音生成模型,可输出Mimi音频编码,适用于文本转语音任务。
模型特点
上下文感知生成
支持通过历史对话音频和文本作为上下文输入,优化当前语音生成效果
高效架构设计
采用Llama主干网络结合轻量级解码器,平衡生成质量与计算效率
多模态输入
支持同时处理文本和音频输入,实现更自然的语音交互
模型能力
文本转语音生成
上下文感知语音合成
多说话人语音生成
使用案例
交互式语音应用
语音助手
为对话系统提供自然语音输出
演示案例显示可生成带情感语调的语音
内容创作
有声内容生成
将文本内容自动转换为语音
🚀 CSM 1B
CSM(对话语音模型)是由 Sesame 推出的语音生成模型,它能够根据文本和音频输入生成 RVQ 音频代码。该模型采用 Llama 作为主干架构,并配备一个较小的音频解码器,用于生成 Mimi 音频代码。
🚀 快速开始
- 2025/03/13 - 我们发布了 1B 版本的 CSM 变体。原始代码可在 GitHub 上获取:SesameAILabs/csm。
- 2025/05/07 - Transformers 开始支持 CSM。
一个经过微调的 CSM 变体为我们 博客文章 中展示的 交互式语音演示 提供了支持。此外,还提供了一个托管的 HuggingFace 空间 用于测试音频生成。
✨ 主要特性
- 基于文本和音频输入生成 RVQ 音频代码。
- 采用 Llama 主干架构和小型音频解码器。
- 支持批量推理和 CUDA 图的全图编译。
- 可使用 Transformers 的 Trainer 进行微调。
📦 安装指南
文档未提供具体安装步骤,故跳过该章节。
💻 使用示例
基础用法
import torch
from transformers import CsmForConditionalGeneration, AutoProcessor
model_id = "eustlb/csm-1b"
device = "cuda" if torch.cuda.is_available() else "cpu"
# load the model and the processor
processor = AutoProcessor.from_pretrained(model_id)
model = CsmForConditionalGeneration.from_pretrained(model_id, device_map=device)
# prepare the inputs
text = "[0]The past is just a story we tell ourselves." # `[0]` for speaker id 0
inputs = processor(text, add_special_tokens=True).to(device)
# another equivalent way to prepare the inputs
conversation = [
{"role": "0", "content": [{"type": "text", "text": "The past is just a story we tell ourselves."}]},
]
inputs = processor.apply_chat_template(
conversation,
tokenize=True,
return_dict=True,
).to(device)
# infer the model
audio = model.generate(**inputs, output_audio=True)
processor.save_audio(audio, "example_without_context.wav")
高级用法
提供上下文时使用
import torch
from transformers import CsmForConditionalGeneration, AutoProcessor
from datasets import load_dataset, Audio
model_id = "eustlb/csm-1b"
device = "cuda" if torch.cuda.is_available() else "cpu"
# load the model and the processor
processor = AutoProcessor.from_pretrained(model_id)
model = CsmForConditionalGeneration.from_pretrained(model_id, device_map=device)
# prepare the inputs
ds = load_dataset("hf-internal-testing/dailytalk-dummy", split="train")
# ensure the audio is 24kHz
ds = ds.cast_column("audio", Audio(sampling_rate=24000))
conversation = []
# 1. context
for text, audio, speaker_id in zip(ds[:4]["text"], ds[:4]["audio"], ds[:4]["speaker_id"]):
conversation.append(
{
"role": f"{speaker_id}",
"content": [{"type": "text", "text": text}, {"type": "audio", "path": audio["array"]}],
}
)
# 2. text prompt
conversation.append({"role": f"{ds[4]['speaker_id']}", "content": [{"type": "text", "text": ds[4]["text"]}]})
inputs = processor.apply_chat_template(
conversation,
tokenize=True,
return_dict=True,
).to(device)
# infer the model
audio = model.generate(**inputs, output_audio=True)
processor.save_audio(audio, "example_with_context.wav")
批量推理
import torch
from transformers import CsmForConditionalGeneration, AutoProcessor
from datasets import load_dataset, Audio
model_id = "eustlb/csm-1b"
device = "cuda" if torch.cuda.is_available() else "cpu"
# load the model and the processor
processor = AutoProcessor.from_pretrained(model_id)
model = CsmForConditionalGeneration.from_pretrained(model_id, device_map=device)
# prepare the inputs
ds = load_dataset("hf-internal-testing/dailytalk-dummy", split="train")
# ensure the audio is 24kHz
ds = ds.cast_column("audio", Audio(sampling_rate=24000))
# here a batch with two prompts
conversation = [
[
{
"role": f"{ds[0]['speaker_id']}",
"content": [
{"type": "text", "text": ds[0]["text"]},
{"type": "audio", "path": ds[0]["audio"]["array"]},
],
},
{
"role": f"{ds[1]['speaker_id']}",
"content": [
{"type": "text", "text": ds[1]["text"]},
],
},
],
[
{
"role": f"{ds[0]['speaker_id']}",
"content": [
{"type": "text", "text": ds[0]["text"]},
],
}
],
]
inputs = processor.apply_chat_template(
conversation,
tokenize=True,
return_dict=True,
).to(device)
audio = model.generate(**inputs, output_audio=True)
processor.save_audio(audio, [f"speech_batch_idx_{i}.wav" for i in range(len(audio))])
CUDA 图全图编译
import torch
import copy
from transformers import CsmForConditionalGeneration, AutoProcessor
from datasets import load_dataset
model_id = "eustlb/csm-1b"
device = "cuda"
# set logs to ensure no recompilation and graph breaks
torch._logging.set_logs(graph_breaks=True, recompiles=True, cudagraphs=True)
# load the model and the processor
processor = AutoProcessor.from_pretrained(model_id)
model = CsmForConditionalGeneration.from_pretrained(model_id, device_map=device)
# use static cache, enabling automatically torch compile with fullgraph and reduce-overhead
model.generation_config.max_length = 250 # big enough to avoid recompilation
model.generation_config.max_new_tokens = None # would take precedence over max_length
model.generation_config.cache_implementation = "static"
model.depth_decoder.generation_config.cache_implementation = "static"
# generation kwargs
gen_kwargs = {
"do_sample": False,
"depth_decoder_do_sample": False,
"temperature": 1.0,
"depth_decoder_temperature": 1.0,
}
# Define a timing decorator
class TimerContext:
def __init__(self, name="Execution"):
self.name = name
self.start_event = None
self.end_event = None
def __enter__(self):
# Use CUDA events for more accurate GPU timing
self.start_event = torch.cuda.Event(enable_timing=True)
self.end_event = torch.cuda.Event(enable_timing=True)
self.start_event.record()
return self
def __exit__(self, *args):
self.end_event.record()
torch.cuda.synchronize()
elapsed_time = self.start_event.elapsed_time(self.end_event) / 1000.0
print(f"{self.name} time: {elapsed_time:.4f} seconds")
# prepare the inputs
ds = load_dataset("hf-internal-testing/dailytalk-dummy", split="train")
conversation = [
{
"role": f"{ds[0]['speaker_id']}",
"content": [
{"type": "text", "text": ds[0]["text"]},
{"type": "audio", "path": ds[0]["audio"]["array"]},
],
},
{
"role": f"{ds[1]['speaker_id']}",
"content": [
{"type": "text", "text": ds[1]["text"]},
{"type": "audio", "path": ds[1]["audio"]["array"]},
],
},
{
"role": f"{ds[2]['speaker_id']}",
"content": [
{"type": "text", "text": ds[2]["text"]},
],
},
]
padded_inputs_1 = processor.apply_chat_template(
conversation,
tokenize=True,
return_dict=True,
).to(device)
print("\n" + "="*50)
print("First generation - compiling and recording CUDA graphs...")
with TimerContext("First generation"):
_ = model.generate(**padded_inputs_1, **gen_kwargs)
print("="*50)
print("\n" + "="*50)
print("Second generation - fast !!!")
with TimerContext("Second generation"):
_ = model.generate(**padded_inputs_1, **gen_kwargs)
print("="*50)
# now with different inputs
conversation = [
{
"role": f"{ds[0]['speaker_id']}",
"content": [
{"type": "text", "text": ds[2]["text"]},
{"type": "audio", "path": ds[2]["audio"]["array"]},
],
},
{
"role": f"{ds[1]['speaker_id']}",
"content": [
{"type": "text", "text": ds[3]["text"]},
{"type": "audio", "path": ds[3]["audio"]["array"]},
],
},
{
"role": f"{ds[2]['speaker_id']}",
"content": [
{"type": "text", "text": ds[4]["text"]},
],
},
]
padded_inputs_2 = processor.apply_chat_template(
conversation,
tokenize=True,
return_dict=True,
).to(device)
print("\n" + "="*50)
print("Generation with other inputs!")
with TimerContext("Generation with different inputs"):
_ = model.generate(**padded_inputs_2, **gen_kwargs)
print("="*50)
微调与训练
from datasets import load_dataset, Audio
from transformers import (
CsmForConditionalGeneration,
TrainingArguments,
CsmProcessor,
Trainer
)
processor = CsmProcessor.from_pretrained("eustlb/csm-1b")
model = CsmForConditionalGeneration.from_pretrained("eustlb/csm-1b")
model.train()
ds = load_dataset("eustlb/dailytalk-conversations-grouped", split="train")
ds = ds.cast_column("audio", Audio(sampling_rate=processor.feature_extractor.sampling_rate))
def data_collator(samples):
conversations = []
for sample in samples:
concatenated_audio_array = sample["audio"]["array"]
audio = [concatenated_audio_array[s: e] for s, e in sample["audio_cut_idxs"]]
conversation = []
for speaker_id, text, audio in zip(sample["speaker_ids"], sample["texts"], audio):
conversation.append({
"role": f"{speaker_id}",
"content": [
{"type": "text", "text": text},
{"type": "audio", "audio": audio}
]
})
conversations.append(conversation)
inputs = processor.apply_chat_template(
conversations,
tokenize=True,
return_dict=True,
output_labels=True,
)
return inputs
training_args = TrainingArguments(
"test-trainer",
remove_unused_columns=False,
gradient_checkpointing=True,
)
trainer = Trainer(
model,
training_args,
train_dataset=ds,
data_collator=data_collator,
)
trainer.train()
📚 详细文档
常见问题解答
- 这个模型自带语音吗? 这里开源的模型是一个基础生成模型。它能够产生各种语音,但尚未针对任何特定语音进行微调。
- 我可以和模型对话吗? CSM 是作为音频生成模型进行训练的,而不是通用的多模态大语言模型。它不能生成文本。我们建议使用单独的大语言模型进行文本生成。
- 它支持其他语言吗? 由于训练数据中存在数据污染,该模型对非英语语言有一定的处理能力,但效果可能不佳。
滥用与误用
本项目提供了一个高质量的语音生成模型,用于研究和教育目的。虽然我们鼓励负责任和合乎道德的使用,但我们明确禁止以下行为:
- 冒充或欺诈:未经他人明确同意,不得使用此模型生成模仿真实个人的语音。
- 虚假信息或欺骗:不得使用此模型创建具有欺骗性或误导性的内容,如虚假新闻或欺诈性电话。
- 非法或有害活动:不得将此模型用于任何非法、有害或恶意目的。
使用此模型即表示您同意遵守所有适用的法律和道德准则。我们不对任何滥用行为负责,并强烈谴责对该技术的不道德应用。
📄 许可证
本项目采用 Apache-2.0 许可证。
作者
Johan Schalkwyk、Ankit Kumar、Dan Lyth、Sefik Emre Eskimez、Zack Hodari、Cinjon Resnick、Ramon Sanabria、Raven Jiang 以及 Sesame 团队。
Kokoro 82M
Apache-2.0
Kokoro是一款拥有8200万参数的开源文本转语音(TTS)模型,以其轻量级架构和高音质著称,同时具备快速和成本效益高的特点。
语音合成 英语
K
hexgrad
2.0M
4,155
XTTS V2
其他
ⓍTTS是一款革命性的语音生成模型,仅需6秒音频片段即可实现跨语言音色克隆,支持17种语言。
语音合成
X
coqui
1.7M
2,630
F5 TTS
F5-TTS 是一个基于流匹配的语音合成模型,专注于流畅且忠实的语音合成,特别适用于童话讲述等场景。
语音合成
F
SWivid
851.49k
1,000
Bigvgan V2 22khz 80band 256x
MIT
BigVGAN是基于大规模训练的通用神经声码器,能够从梅尔频谱生成高质量音频波形。
语音合成
B
nvidia
503.23k
16
Speecht5 Tts
MIT
基于LibriTTS数据集微调的SpeechT5语音合成(文本转语音)模型,支持高质量的文本转语音转换。
语音合成
Transformers

S
microsoft
113.83k
760
Dia 1.6B
Apache-2.0
Dia是由Nari实验室开发的16亿参数文本转语音模型,能够直接从文本生成高度逼真的对话,支持情感和语调控制,并能生成非语言交流内容。
语音合成
Safetensors 英语
D
nari-labs
80.28k
1,380
Csm 1b
Apache-2.0
CSM是Sesame开发的10亿参数规模语音生成模型,可根据文本和音频输入生成RVQ音频编码
语音合成
Safetensors 英语
C
sesame
65.03k
1,950
Kokoro 82M V1.1 Zh
Apache-2.0
Kokoro 是一个开放权重的小型但功能强大的文本转语音(TTS)模型系列,新增了来自专业数据集的100名中文说话人数据。
语音合成
K
hexgrad
51.56k
112
Indic Parler Tts
Apache-2.0
Indic Parler-TTS 是 Parler-TTS Mini 的多语言印度语言扩展版本,支持21种语言,包括多种印度语言和英语。
语音合成
Transformers 支持多种语言

I
ai4bharat
43.59k
124
Bark
MIT
Bark是由Suno创建的基于Transformer的文本转音频模型,能生成高度逼真的多语言语音、音乐、背景噪音和简单音效。
语音合成
Transformers 支持多种语言

B
suno
35.72k
1,326
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98