百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

多模态/实时语音交互:LLaMA-Omni不打断

ztj100 2025-02-11 14:27 66 浏览 0 评论

blog地址:

https://github.com/ictnlp/LLaMA-Omni--

code地址:

https://github.com/ictnlp/LLaMA-Omni--

paper地址:

https://arxiv.org/abs/2409.06666

模型下载
https://huggingface.co/ICTNLP/Llama-3.1-8B-Omni--https://modelscope.cn/models/ICTNLP/Llama-3.1-8B-Omni--


0 简介

像GPT-4o这样的模型通过语音实现与大型语言模型(LLMs)的实时交互,相较于传统的基于文本的交互方式显著提升了用户体验。然而目前仍缺乏如何基于开源LLMs构建语音交互模型的探索。为此提出一种名为LLaMA-Omni的创新模型架构,旨在实现低延迟、高质量的语音交互。LLaMA-Omni集成了预训练的语音编码器、语音适配器、LLM和流式语音解码器,消除了语音转录的需求,能够直接从语音指令中生成文本和语音响应,且延迟极低。模型基于最新的Llama-3.1-8B-Instruct模型构建,为了将模型与语音交互场景对齐,构建了一个名为InstructS2S-200K的数据集,其中包含20万个语音指令及相应的语音响应。实验结果表明,与以往的语音语言模型相比,LLaMA-Omni在内容和风格上均提供了更优的响应,其响应延迟低至226毫秒。此外,训练LLaMA-Omni仅需不到3天的时间,使用4个GPU,为未来高效开发语音语言模型铺平了道路。

LLaMA-Omni是一个基于Llama-3.1-8B-Instruct的语音语言模型。 它支持低延迟和高质量的语音交互,根据语音指令同时生成文本和语音响应。

亮点

  • 基于Llama-3.1-8B-Instruct构建,确保高质量的响应。
  • 低延迟语音交互,延迟低至226 ms。
  • 同时生成文本和语音响应。
  • ?仅使用4个GPU,在不到3天的时间内完成训练。

1 模型性能

实验结果表明,与之前的语音语言模型相比,LLaMA-Omni在内容和风格上都提供了更优的响应,响应延迟低至226毫秒。

2 模型结构

仅4块GPU、不到3天训练出「开源版GPT-4o」:LLaMA-Omni模型通过整合语音编码器、语音适配器、LLM和流式语音解码器,实现从语音指令直接生成文本和语音响应,无需转录为文本。

模型采用两阶段训练策略::先训练语音适配器和LLM生成文本响应,再训练语音解码器生成语音响应,整体训练在4块GPU上完成,耗时约65小时,LLaMA-Omni在实验中显示出较低的响应延迟(226ms)和高质量的语音输出,优于其他语音-语言模型,如SpeechGPT。

3 结论

本文提出一种创新的模型架构LLaMA-Omni,它使基于LLMs的低延迟和高质量的语音交互成为可能。LLaMA-Omni 基于最新的 Llama-3.1-8B-Instruct 模型,增加了语音编码器用于语音理解,以及一个可以同时生成文本和语音响应的流式语音解码器。为了使模型适应语音交互场景,构建了一个包含 200K 条语音指令及其语音响应的语音指令数据集 InstructS2S-200K。实验结果表明,与之前的语音语言模型相比,LLaMA-Omni 在内容和风格上提供了更优的响应,响应延迟低至 226 毫秒。此外训练LLaMA-Omni在4个GPU上不到3天的时间,使基于最新 LLMs 快速开发语音交互模型成为可能。探索增强生成语音响应的表现力和改进实时交互能力。

4 快速使用与体验

Local Inference

To run inference locally, please organize the speech instruction files according to the format in the omni_speech/infer/examples directory, then refer to the following script.

bash omni_speech/infer/run.sh omni_speech/infer/examples
import os
import time
import subprocess
import json
import soundfile as sf
import torch

from cog import BasePredictor, Input, Path, BaseModel
from fairseq import utils as fairseq_utils
from fairseq.models.text_to_speech.vocoder import CodeHiFiGANVocoder

from omni_speech.model.builder import load_pretrained_model
from omni_speech.utils import disable_torch_init
from omni_speech.infer.infer import create_data_loader, ctc_postprocess


MODEL_CACHE = "models"
MODEL_URL = (
    f"https://weights.replicate.delivery/default/ictnlp/LLaMA-Omni/{MODEL_CACHE}.tar"
)
os.environ["HF_DATASETS_OFFLINE"] = "1"
os.environ["TRANSFORMERS_OFFLINE"] = "1"
os.environ["HF_HOME"] = MODEL_CACHE
os.environ["TORCH_HOME"] = MODEL_CACHE
os.environ["HF_DATASETS_CACHE"] = MODEL_CACHE
os.environ["TRANSFORMERS_CACHE"] = MODEL_CACHE
os.environ["HUGGINGFACE_HUB_CACHE"] = MODEL_CACHE


class ModelOutput(BaseModel):
    audio: Path
    text: str


def download_weights(url, dest):
    start = time.time()
    print("downloading url: ", url)
    print("downloading to: ", dest)
    subprocess.check_call(["pget", "-x", url, dest], close_fds=False)
    print("downloading took: ", time.time() - start)


class Predictor(BasePredictor):
    def setup(self) -> None:
        """Load the model into memory to make running multiple predictions efficient"""

        if not os.path.exists(MODEL_CACHE):
            download_weights(MODEL_URL, MODEL_CACHE)

        # Model
        disable_torch_init()
        self.tokenizer, self.model, _ = load_pretrained_model(
            f"{MODEL_CACHE}/Llama-3.1-8B-Omni", model_base=None, s2s=True
        )

        with open(f"{MODEL_CACHE}/vocoder/config.json") as f:
            vocoder_cfg = json.load(f)
        self.vocoder = CodeHiFiGANVocoder(
            f"{MODEL_CACHE}/vocoder/g_00500000", vocoder_cfg
        ).cuda()

    def predict(
        self,
        input_audio: Path = Input(description="Input audio"),
        prompt: str = Input(
            default="Please directly answer the questions in the user's speech"
        ),
        temperature: float = Input(
            description="Controls randomness. Lower values make the model more deterministic, higher values make it more random.",
            default=0.0,
            ge=0.0,
            le=1.0,
        ),
        top_p: float = Input(
            description="Controls diversity of the output. Valid when temperature > 0. Lower values make the output more focused, higher values make it more diverse.",
            default=0.0,
            ge=0.0,
            le=1.0,
        ),
        max_new_tokens: int = Input(
            description="Maximum number of tokens to generate", default=256, ge=1
        ),
    ) -> ModelOutput:
        """Run a single prediction on the model"""

        questions = [
            {
                "speech": str(input_audio),
                "conversations": [{"from": "human", "value": f"\n{prompt}"}],
            }
        ]

        data_loader = create_data_loader(
            questions,
            self.tokenizer,
            self.model.config,
            input_type="mel",
            mel_size=128,
            conv_mode="llama_3",
        )

        (input_ids, speech_tensor, speech_length) = next(iter(data_loader))

        input_ids = input_ids.to(device="cuda", non_blocking=True)
        speech_tensor = speech_tensor.to(
            dtype=torch.float16, device="cuda", non_blocking=True
        )
        speech_length = speech_length.to(device="cuda", non_blocking=True)

        with torch.inference_mode():
            output_ids, output_units = self.model.generate(
                input_ids,
                speech=speech_tensor,
                speech_lengths=speech_length,
                do_sample=True if temperature > 0 else False,
                temperature=temperature,
                top_p=top_p if temperature > 0 else None,
                num_beams=1,
                max_new_tokens=max_new_tokens,
                use_cache=True,
                pad_token_id=128004,
                streaming_unit_gen=False,
            )

        prediction = self.tokenizer.batch_decode(output_ids, skip_special_tokens=True)[
            0
        ].strip()

        output_units = ctc_postprocess(
            output_units, blank=self.model.config.unit_vocab_size
        )

        print(prediction)

        print(f"output_units: {output_units}")
        print(type(output_units))

        output_units = [(list(map(int, output_units.strip().split())))]

        x = {
            "code": torch.LongTensor(output_units[0]).view(1, -1),
        }

        x = fairseq_utils.move_to_cuda(x)
        wav = self.vocoder(x, True)

        out_path = "/tmp/out.wav"

        sf.write(
            out_path,
            wav.detach().cpu().numpy(),
            16000,
        )

        return ModelOutput(audio=Path(out_path), text=prediction)


参考文献:

https://zhuanlan.zhihu.com/p/720048515

Acknowledgements

  • LLaVA: The codebase we built upon.
  • SLAM-LLM: We borrow some code about speech encoder and speech adaptor.

《完》

相关推荐

sharding-jdbc实现`分库分表`与`读写分离`

一、前言本文将基于以下环境整合...

三分钟了解mysql中主键、外键、非空、唯一、默认约束是什么

在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据存储的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的。每一行代表一条唯一的记录,每一列代表记录中的一个域。...

MySQL8行级锁_mysql如何加行级锁

MySQL8行级锁版本:8.0.34基本概念...

mysql使用小技巧_mysql使用入门

1、MySQL中有许多很实用的函数,好好利用它们可以省去很多时间:group_concat()将取到的值用逗号连接,可以这么用:selectgroup_concat(distinctid)fr...

MySQL/MariaDB中如何支持全部的Unicode?

永远不要在MySQL中使用utf8,并且始终使用utf8mb4。utf8mb4介绍MySQL/MariaDB中,utf8字符集并不是对Unicode的真正实现,即不是真正的UTF-8编码,因...

聊聊 MySQL Server 可执行注释,你懂了吗?

前言MySQLServer当前支持如下3种注释风格:...

MySQL系列-源码编译安装(v5.7.34)

一、系统环境要求...

MySQL的锁就锁住我啦!与腾讯大佬的技术交谈,是我小看它了

对酒当歌,人生几何!朝朝暮暮,唯有己脱。苦苦寻觅找工作之间,殊不知今日之事乃我心之痛,难道是我不配拥有工作嘛。自面试后他所谓的等待都过去一段时日,可惜在下京东上的小金库都要见低啦。每每想到不由心中一...

MySQL字符问题_mysql中字符串的位置

中文写入乱码问题:我输入的中文编码是urf8的,建的库是urf8的,但是插入mysql总是乱码,一堆"???????????????????????"我用的是ibatis,终于找到原因了,我是这么解决...

深圳尚学堂:mysql基本sql语句大全(三)

数据开发-经典1.按姓氏笔画排序:Select*FromTableNameOrderByCustomerNameCollateChinese_PRC_Stroke_ci_as//从少...

MySQL进行行级锁的?一会next-key锁,一会间隙锁,一会记录锁?

大家好,是不是很多人都对MySQL加行级锁的规则搞的迷迷糊糊,一会是next-key锁,一会是间隙锁,一会又是记录锁。坦白说,确实还挺复杂的,但是好在我找点了点规律,也知道如何如何用命令分析加...

一文讲清怎么利用Python Django实现Excel数据表的导入导出功能

摘要:Python作为一门简单易学且功能强大的编程语言,广受程序员、数据分析师和AI工程师的青睐。本文系统讲解了如何使用Python的Django框架结合openpyxl库实现Excel...

用DataX实现两个MySQL实例间的数据同步

DataXDataX使用Java实现。如果可以实现数据库实例之间准实时的...

MySQL数据库知识_mysql数据库基础知识

MySQL是一种关系型数据库管理系统;那废话不多说,直接上自己以前学习整理文档:查看数据库命令:(1).查看存储过程状态:showprocedurestatus;(2).显示系统变量:show...

如何为MySQL中的JSON字段设置索引

背景MySQL在2015年中发布的5.7.8版本中首次引入了JSON数据类型。自此,它成了一种逃离严格列定义的方式,可以存储各种形状和大小的JSON文档,例如审计日志、配置信息、第三方数据包、用户自定...

取消回复欢迎 发表评论: