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