omni-vram 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omni_vram-0.4.0/LICENSE +21 -0
- omni_vram-0.4.0/PKG-INFO +642 -0
- omni_vram-0.4.0/README.md +587 -0
- omni_vram-0.4.0/omni_vram.egg-info/PKG-INFO +642 -0
- omni_vram-0.4.0/omni_vram.egg-info/SOURCES.txt +25 -0
- omni_vram-0.4.0/omni_vram.egg-info/dependency_links.txt +1 -0
- omni_vram-0.4.0/omni_vram.egg-info/requires.txt +22 -0
- omni_vram-0.4.0/omni_vram.egg-info/top_level.txt +1 -0
- omni_vram-0.4.0/pyproject.toml +86 -0
- omni_vram-0.4.0/setup.cfg +4 -0
- omni_vram-0.4.0/setup.py +72 -0
- omni_vram-0.4.0/tests/test_audio_utils.py +349 -0
- omni_vram-0.4.0/tests/test_emotion_recognition.py +111 -0
- omni_vram-0.4.0/tests/test_noise_reduction.py +157 -0
- omni_vram-0.4.0/tests/test_speaker_diarization.py +176 -0
- omni_vram-0.4.0/tests/test_stream_processor.py +503 -0
- omni_vram-0.4.0/tests/test_whisper_bridge.py +432 -0
- omni_vram-0.4.0/vram_core/__init__.py +66 -0
- omni_vram-0.4.0/vram_core/api_server.py +534 -0
- omni_vram-0.4.0/vram_core/audio_utils.py +543 -0
- omni_vram-0.4.0/vram_core/config.py +304 -0
- omni_vram-0.4.0/vram_core/emotion_recognition.py +367 -0
- omni_vram-0.4.0/vram_core/noise_reduction.py +368 -0
- omni_vram-0.4.0/vram_core/speaker_diarization.py +485 -0
- omni_vram-0.4.0/vram_core/stream_processor.py +614 -0
- omni_vram-0.4.0/vram_core/streaming_asr.py +455 -0
- omni_vram-0.4.0/vram_core/whisper_bridge.py +1181 -0
omni_vram-0.4.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Liangchenxu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
omni_vram-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: omni-vram
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Real-time VRAM orchestration toolkit for voice-enabled LLM applications
|
|
5
|
+
Home-page: https://github.com/Liangchenxu/Omni-VRAM
|
|
6
|
+
Author: Liangchenxu
|
|
7
|
+
Author-email: Liangchenxu <lcx1479632@gmail.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/Liangchenxu/Omni-VRAM
|
|
10
|
+
Project-URL: Documentation, https://github.com/Liangchenxu/Omni-VRAM/tree/main/docs
|
|
11
|
+
Project-URL: Repository, https://github.com/Liangchenxu/Omni-VRAM
|
|
12
|
+
Project-URL: Issues, https://github.com/Liangchenxu/Omni-VRAM/issues
|
|
13
|
+
Project-URL: Changelog, https://github.com/Liangchenxu/Omni-VRAM/blob/main/CHANGELOG.md
|
|
14
|
+
Keywords: cuda,vram,whisper,voice,llm,kv-cache,zero-copy,real-time
|
|
15
|
+
Classifier: Development Status :: 4 - Beta
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Intended Audience :: Science/Research
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
20
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
21
|
+
Classifier: Operating System :: MacOS
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
28
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
29
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
30
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
|
|
31
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Analysis
|
|
32
|
+
Requires-Python: >=3.8
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
License-File: LICENSE
|
|
35
|
+
Requires-Dist: numpy>=1.19.0
|
|
36
|
+
Requires-Dist: soundfile>=0.10.0
|
|
37
|
+
Requires-Dist: python-dotenv>=0.19.0
|
|
38
|
+
Provides-Extra: torch
|
|
39
|
+
Requires-Dist: torch>=1.9.0; extra == "torch"
|
|
40
|
+
Provides-Extra: whisper-python
|
|
41
|
+
Requires-Dist: openai-whisper>=20230918; extra == "whisper-python"
|
|
42
|
+
Provides-Extra: mp3
|
|
43
|
+
Requires-Dist: pydub>=0.25.0; extra == "mp3"
|
|
44
|
+
Provides-Extra: audio-io
|
|
45
|
+
Requires-Dist: pyaudio>=0.2.11; extra == "audio-io"
|
|
46
|
+
Provides-Extra: dev
|
|
47
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
48
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
49
|
+
Provides-Extra: all
|
|
50
|
+
Requires-Dist: omni-vram[audio-io,dev,mp3,torch,whisper-python]; extra == "all"
|
|
51
|
+
Dynamic: author
|
|
52
|
+
Dynamic: home-page
|
|
53
|
+
Dynamic: license-file
|
|
54
|
+
Dynamic: requires-python
|
|
55
|
+
|
|
56
|
+
# Omni-VRAM: Zero-Copy CUDA Audio-to-LLM Bridge
|
|
57
|
+
### 零拷贝跨硬件语音大模型底层直通桥
|
|
58
|
+
|
|
59
|
+
[](LICENSE)
|
|
60
|
+

|
|
61
|
+

|
|
62
|
+

|
|
63
|
+
[](https://github.com/Liangchenxu/Omni-VRAM/actions/workflows/test.yml)
|
|
64
|
+
[](https://github.com/Liangchenxu/Omni-VRAM/releases)
|
|
65
|
+
|
|
66
|
+
[**English**](#english-documentation) | [**中文文档**](#chinese-documentation) | [**Docs**](docs/)
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
<a id="english-documentation"></a>
|
|
71
|
+
## 📖 Overview
|
|
72
|
+
|
|
73
|
+
**Omni-VRAM** is a high-performance, lightweight CUDA extension designed to eliminate VRAM fragmentation and memory transfer bottlenecks in real-time LLM (Large Language Model) audio applications.
|
|
74
|
+
|
|
75
|
+
Traditional Python-based audio processing pipelines and PyTorch native operations (such as `torch.cat` for KV-Cache updates) introduce significant overhead and non-deterministic latency. Omni-VRAM solves this by implementing **Operator Fusion** and **Zero-Copy Memory Injection** directly at the hardware level, enabling consumer-grade GPUs (e.g., RTX 30/40 series) to achieve sub-millisecond end-to-end latency for real-time voice agents.
|
|
76
|
+
|
|
77
|
+
### ✨ Core Features
|
|
78
|
+
|
|
79
|
+
* **Zero-Copy KV-Cache Appender:** Bypasses PyTorch's dynamic memory reallocation (`torch.cat`) by pre-allocating continuous VRAM blocks and directly injecting hardware-level token embeddings ($O(1)$ complexity).
|
|
80
|
+
* **Fused Audio Frontend:** Performs Voice Activity Detection (VAD), Pre-emphasis, and Windowing (Hann) in a single CUDA kernel execution, eliminating intermediate VRAM allocations.
|
|
81
|
+
* **Hardware-Aware Radar:** Dynamically scans GPU architecture (`sm_XX`) and SM counts at runtime to dispatch the most optimal computation strategy.
|
|
82
|
+
* **Whisper Multi-Backend:** Supports **faster-whisper** (CTranslate2, recommended), whisper.cpp CLI, OpenAI API, and legacy Python whisper with automatic fallback chain.
|
|
83
|
+
* **Real-Time Streaming ASR:** Sliding-window VAD-based speech recognition with concurrent worker threads, partial/final result callbacks, and GPU batch acceleration.
|
|
84
|
+
* **Web API Server:** FastAPI-based REST + WebSocket server for transcription — file upload, base64 input, and real-time streaming endpoint.
|
|
85
|
+
* **Stream Processing:** Chunk-based audio stream processor with built-in VAD, segment extraction, and callback-driven architecture.
|
|
86
|
+
* **Audio Format Utilities:** Automatic format detection, sample rate conversion, stereo-to-mono, normalization, and WAV encoding.
|
|
87
|
+
* **Noise Reduction:** STFT-based spectral subtraction with adaptive noise estimation — three presets (light / medium / aggressive), automatically applied in the stream processing pipeline.
|
|
88
|
+
* **Emotion Recognition:** Real-time speech emotion detection (happy / sad / angry / neutral / surprised) based on acoustic features — energy, zero-crossing rate, pitch (F0), and temporal dynamics.
|
|
89
|
+
* **Speaker Diarization:** MFCC-based speaker clustering with cosine similarity — identifies "who spoke when" without external speaker embedding models.
|
|
90
|
+
|
|
91
|
+
### 📁 Project Structure
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
Omni-VRAM/
|
|
95
|
+
├── vram_hacker.cu # CUDA kernel source (KV-Cache injection)
|
|
96
|
+
├── setup.py # Build & install script
|
|
97
|
+
├── test_run.py # Quick integration test
|
|
98
|
+
├── .env.example # Configuration template
|
|
99
|
+
│
|
|
100
|
+
├── vram_core/ # Python core library
|
|
101
|
+
│ ├── __init__.py # Package exports
|
|
102
|
+
│ ├── config.py # Configuration management (.env loader)
|
|
103
|
+
│ ├── audio_utils.py # Audio format detection & conversion
|
|
104
|
+
│ ├── whisper_bridge.py # Whisper multi-backend integration
|
|
105
|
+
│ ├── stream_processor.py # Real-time stream processor + VAD
|
|
106
|
+
│ ├── streaming_asr.py # Real-time streaming ASR engine
|
|
107
|
+
│ ├── api_server.py # FastAPI REST + WebSocket API
|
|
108
|
+
│ ├── noise_reduction.py # STFT spectral subtraction noise reduction
|
|
109
|
+
│ ├── emotion_recognition.py # Acoustic feature-based emotion recognition
|
|
110
|
+
│ └── speaker_diarization.py # MFCC speaker diarization & clustering
|
|
111
|
+
│
|
|
112
|
+
├── examples/ # Example applications
|
|
113
|
+
│ ├── realtime_voice_assistant.py # Real-time voice assistant
|
|
114
|
+
│ ├── meeting_transcriber.py # Meeting transcription & summary
|
|
115
|
+
│ ├── voice_chat_bot.py # Multi-turn voice chat bot
|
|
116
|
+
│ ├── benchmark_suite.py # Performance benchmark suite
|
|
117
|
+
│ ├── api_demo.py # API server demo client
|
|
118
|
+
│ └── test_whisper_local.py # Whisper local test script
|
|
119
|
+
│
|
|
120
|
+
├── tests/ # Unit tests
|
|
121
|
+
│ ├── test_audio_utils.py
|
|
122
|
+
│ ├── test_whisper_bridge.py
|
|
123
|
+
│ ├── test_stream_processor.py
|
|
124
|
+
│ ├── test_noise_reduction.py
|
|
125
|
+
│ ├── test_emotion_recognition.py
|
|
126
|
+
│ └── test_speaker_diarization.py
|
|
127
|
+
│
|
|
128
|
+
└── docs/ # Documentation
|
|
129
|
+
├── installation.md
|
|
130
|
+
├── quickstart.md
|
|
131
|
+
├── api_reference.md
|
|
132
|
+
├── examples.md
|
|
133
|
+
└── faq.md
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 🧩 Examples
|
|
137
|
+
|
|
138
|
+
| Example | Description | Command |
|
|
139
|
+
|---------|-------------|---------|
|
|
140
|
+
| **Real-time Voice Assistant** | Microphone → VAD → Whisper → Display, with file recording | `python examples/realtime_voice_assistant.py` |
|
|
141
|
+
| **Meeting Transcriber** | Long-form recording with silence auto-segmentation and export | `python examples/meeting_transcriber.py --output meeting.txt` |
|
|
142
|
+
| **Voice Chat Bot** | Multi-turn dialogue with history tracking and LLM-ready architecture | `python examples/voice_chat_bot.py` |
|
|
143
|
+
| **Benchmark Suite** | Performance testing for all modules with Markdown report | `python examples/benchmark_suite.py --skip-whisper` |
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## 📊 Performance Benchmarks
|
|
148
|
+
|
|
149
|
+
*Hardware: NVIDIA RTX 3060 (12GB) | Platform: Windows WDDM | CUDA: 12.1*
|
|
150
|
+
|
|
151
|
+
### 1. KV-Cache Memory Injection
|
|
152
|
+
*Task: Appending 100 updates (50 tokens each) to a 100,000-capacity KV-Cache tensor (Dimension: 4096).*
|
|
153
|
+
|
|
154
|
+
| Engine / Method | Latency | Complexity | OOM Risk |
|
|
155
|
+
| :--- | :--- | :--- | :--- |
|
|
156
|
+
| PyTorch Native (`torch.cat`) | 90.32 ms | $O(N)$ (Reallocation) | High (VRAM Fragmentation) |
|
|
157
|
+
| **Omni-VRAM (Zero-Copy)** | **8.07 ms** | **$O(1)$ (Pointer Offset)** | **None** |
|
|
158
|
+
| **Improvement** | **11.19x** | - | - |
|
|
159
|
+
|
|
160
|
+
### 2. Audio Processing Pipeline
|
|
161
|
+
| Pipeline Stage | Input Size | PyTorch / CPU Baseline | Omni-VRAM C++ Kernel | Speedup |
|
|
162
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
163
|
+
| **Concurrent VAD** | 10 Minutes (16kHz) | 9.45 ms (CPU `unfold`) | **0.33 ms** | **~28x** |
|
|
164
|
+
| **Fused Frontend** | 60 Seconds (16kHz) | 20.33 ms (VRAM Stacking)| **1.05 ms** | **~19x** |
|
|
165
|
+
|
|
166
|
+
### 3. Whisper Transcription (CPU)
|
|
167
|
+
| Model | 1s Audio | 5s Audio | 10s Audio |
|
|
168
|
+
| :--- | :--- | :--- | :--- |
|
|
169
|
+
| tiny | ~200ms | ~500ms | ~900ms |
|
|
170
|
+
| base | ~400ms | ~1200ms | ~2200ms |
|
|
171
|
+
|
|
172
|
+
> Run `python examples/benchmark_suite.py` for automated benchmarks on your hardware.
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## 🛠️ Installation
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# Clone the repository
|
|
180
|
+
git clone https://github.com/Liangchenxu/Omni-VRAM.git
|
|
181
|
+
cd Omni-VRAM
|
|
182
|
+
|
|
183
|
+
# Install all dependencies (core + audio + faster-whisper)
|
|
184
|
+
pip install -r requirements.txt
|
|
185
|
+
|
|
186
|
+
# Build and install the CUDA extension
|
|
187
|
+
# Note: Ensure NVCC and Visual Studio C++ Build Tools are properly configured.
|
|
188
|
+
python setup.py install
|
|
189
|
+
|
|
190
|
+
# (Optional) Install Web API server dependencies
|
|
191
|
+
pip install fastapi uvicorn python-multipart
|
|
192
|
+
|
|
193
|
+
# (Optional) Install whisper.cpp for local transcription
|
|
194
|
+
# See docs/installation.md for detailed instructions
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Configuration
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
# Copy the configuration template
|
|
201
|
+
cp .env.example .env
|
|
202
|
+
|
|
203
|
+
# Edit .env with your settings
|
|
204
|
+
# At minimum, set WHISPER_CPP_PATH and WHISPER_MODEL_PATH for local transcription
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
> See [docs/installation.md](docs/installation.md) for detailed installation guide.
|
|
208
|
+
|
|
209
|
+
## 💻 Quick Start
|
|
210
|
+
|
|
211
|
+
### Basic CUDA Operations
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
import torch
|
|
215
|
+
import vram_core
|
|
216
|
+
|
|
217
|
+
# 1. Hardware Initialization
|
|
218
|
+
print(vram_core.scan_hardware_dna())
|
|
219
|
+
|
|
220
|
+
# 2. Fused Audio Processing
|
|
221
|
+
audio_stream = torch.randn(960000, device='cuda', dtype=torch.float32)
|
|
222
|
+
# Performs VAD, pre-emphasis, and windowing in ~1 ms
|
|
223
|
+
is_speaking, features = vram_core.smart_audio_listen(audio_stream, threshold=0.5)
|
|
224
|
+
|
|
225
|
+
# 3. Zero-Copy LLM KV-Cache Update
|
|
226
|
+
hidden_dim = 4096
|
|
227
|
+
max_seq_len = 100000
|
|
228
|
+
# Pre-allocate VRAM once
|
|
229
|
+
kv_cache = torch.zeros((max_seq_len, hidden_dim), device='cuda', dtype=torch.float32)
|
|
230
|
+
current_pos = torch.tensor([0], device='cuda', dtype=torch.int32)
|
|
231
|
+
|
|
232
|
+
if is_speaking.item():
|
|
233
|
+
# Direct memory injection (0 reallocation overhead)
|
|
234
|
+
new_tokens = torch.randn((50, hidden_dim), device='cuda', dtype=torch.float32)
|
|
235
|
+
vram_core.append_to_kv_cache(kv_cache, new_tokens, current_pos)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Whisper Transcription
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
from vram_core import WhisperBridge, WhisperBackend
|
|
242
|
+
|
|
243
|
+
# Initialize with automatic backend detection
|
|
244
|
+
whisper = WhisperBridge(
|
|
245
|
+
backend=WhisperBackend.AUTO,
|
|
246
|
+
whisper_model="base",
|
|
247
|
+
language="zh",
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
# Transcribe an audio file
|
|
251
|
+
result = whisper.transcribe("audio.wav")
|
|
252
|
+
print(f"Text: {result.text}")
|
|
253
|
+
print(f"Confidence: {result.confidence}")
|
|
254
|
+
print(f"Duration: {result.audio_duration}s")
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Real-Time Stream Processing
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
import numpy as np
|
|
261
|
+
from vram_core import StreamProcessor, StreamConfig, WhisperBridge, WhisperBackend
|
|
262
|
+
|
|
263
|
+
# Initialize components
|
|
264
|
+
whisper = WhisperBridge(backend=WhisperBackend.AUTO, whisper_model="base")
|
|
265
|
+
config = StreamConfig(sample_rate=16000, chunk_duration_ms=100, vad_threshold=0.02)
|
|
266
|
+
processor = StreamProcessor(config=config, whisper_bridge=whisper)
|
|
267
|
+
|
|
268
|
+
# Set up callbacks
|
|
269
|
+
processor.on_transcription = lambda result: print(f"Transcribed: {result.text}")
|
|
270
|
+
|
|
271
|
+
# Feed audio chunks (e.g., from microphone)
|
|
272
|
+
audio_chunk = np.random.randn(1600).astype(np.float32)
|
|
273
|
+
processor.feed(audio_chunk)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Streaming ASR (Real-time Microphone Transcription)
|
|
277
|
+
|
|
278
|
+
```python
|
|
279
|
+
import numpy as np
|
|
280
|
+
from vram_core import WhisperBridge, WhisperBackend, StreamASR, StreamASRConfig
|
|
281
|
+
|
|
282
|
+
# Initialize whisper
|
|
283
|
+
whisper = WhisperBridge(backend=WhisperBackend.AUTO, whisper_model="base")
|
|
284
|
+
|
|
285
|
+
# Configure streaming ASR
|
|
286
|
+
config = StreamASRConfig(
|
|
287
|
+
sample_rate=16000,
|
|
288
|
+
vad_threshold=0.015,
|
|
289
|
+
language="zh",
|
|
290
|
+
)
|
|
291
|
+
asr = StreamASR(config=config, whisper_bridge=whisper)
|
|
292
|
+
|
|
293
|
+
# Set up callbacks
|
|
294
|
+
asr.on_partial_result = lambda text: print(f"[Partial] {text}")
|
|
295
|
+
asr.on_final_result = lambda result: print(f"[Final] {result.text}")
|
|
296
|
+
|
|
297
|
+
# Start and feed audio
|
|
298
|
+
asr.start()
|
|
299
|
+
audio_chunk = np.random.randn(3200).astype(np.float32) # from microphone
|
|
300
|
+
asr.feed(audio_chunk)
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Web API Server
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
# Start the API server
|
|
307
|
+
python vram_core/api_server.py --model base --language zh --port 8000
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
# Client: File upload transcription
|
|
312
|
+
import requests
|
|
313
|
+
with open("audio.wav", "rb") as f:
|
|
314
|
+
resp = requests.post("http://localhost:8000/transcribe", files={"file": f})
|
|
315
|
+
print(resp.json()["text"])
|
|
316
|
+
|
|
317
|
+
# Client: WebSocket streaming
|
|
318
|
+
import websockets, asyncio
|
|
319
|
+
async def stream():
|
|
320
|
+
async with websockets.connect("ws://localhost:8000/stream") as ws:
|
|
321
|
+
await ws.send(audio_bytes) # 16-bit PCM, 16kHz mono
|
|
322
|
+
result = await ws.recv()
|
|
323
|
+
print(result)
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
> See [docs/quickstart.md](docs/quickstart.md) for more examples.
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## ⚠️ Disclaimer & Liability Waiver
|
|
331
|
+
**Hardware Interaction Warning:** Omni-VRAM interfaces directly with physical GPU hardware at the CUDA C++ level, employing aggressive zero-copy pointer manipulation to maximize throughput.
|
|
332
|
+
While extensively tested, this software is provided **"as is"**, without warranty of any kind. The authors shall NOT be held liable for any kernel panics, system freezes, data loss, or hardware instability resulting from the use of this engine. **Use in production environments at your own risk.**
|
|
333
|
+
|
|
334
|
+
## 📜 License
|
|
335
|
+
Released under the [**MIT License**](https://opensource.org/licenses/MIT).
|
|
336
|
+
You are free to use, modify, and distribute this software in both commercial and non-commercial projects, provided that the original copyright notice and this permission notice are included.
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
<a id="chinese-documentation"></a>
|
|
342
|
+
## 📖 简介 (Overview)
|
|
343
|
+
|
|
344
|
+
**Omni-VRAM** 是一款高性能、轻量级的 CUDA 底层扩展库,专为解决大语言模型(LLM)实时语音应用中的显存碎片化与数据搬运瓶颈而设计。
|
|
345
|
+
|
|
346
|
+
传统的基于 Python 的音频处理流以及 PyTorch 原生操作(例如使用 `torch.cat` 更新 KV-Cache)会引发严重的内存重新分配开销和不可控的延迟。Omni-VRAM 通过在硬件底层实现**算子融合(Operator Fusion)**与**零拷贝内存注入(Zero-Copy Memory Injection)**,使得消费级显卡(如 RTX 30/40 系列)能够为实时语音助手提供亚毫秒级的端到端计算延迟。
|
|
347
|
+
|
|
348
|
+
### ✨ 核心特性
|
|
349
|
+
|
|
350
|
+
* **零拷贝 KV-Cache 注入器:** 完全绕过 PyTorch 的动态内存分配(`torch.cat`),通过预分配连续的物理显存块,以硬件指针偏移的方式直接写入 Token 向量($O(1)$ 时间复杂度)。
|
|
351
|
+
* **融合音频前处理核心:** 在单一 CUDA 核函数中并行完成语音活动检测(VAD)、预加重(Pre-emphasis)与汉宁窗(Hann Window)处理,彻底消除中间显存开销。
|
|
352
|
+
* **跨硬件自适应雷达:** 运行时动态扫描 GPU 架构(`sm_XX`)与流处理器簇(SM)数量,自动调度最优级别的计算策略。
|
|
353
|
+
* **Whisper 语音转写集成:** 多后端支持——**faster-whisper**(CTranslate2,推荐)、whisper.cpp 命令行、OpenAI API、Python whisper 库,自动回退链。
|
|
354
|
+
* **实时流式语音识别:** 基于滑动窗口 VAD 的流式 ASR,支持并发 Worker 线程、部分/最终结果回调、GPU 批处理加速。
|
|
355
|
+
* **Web API 服务:** 基于 FastAPI 的 REST + WebSocket 转写服务——文件上传、Base64 输入、实时流式端点。
|
|
356
|
+
* **实时流处理引擎:** 基于分块的音频流处理器,内置 VAD 检测、语音片段提取,支持回调驱动架构。
|
|
357
|
+
* **音频格式工具链:** 自动格式检测、采样率转换、立体声转单声道、归一化、WAV 编码。
|
|
358
|
+
* **噪声抑制:** 基于 STFT 的谱减法噪声抑制,自适应噪声估计——三档预设(轻度/中度/强力),自动集成到流处理管线中。
|
|
359
|
+
* **情绪识别:** 实时语音情绪检测(开心/悲伤/愤怒/中性/惊讶),基于声学特征——能量、过零率、基频(F0)及时序动态。
|
|
360
|
+
* **说话人识别:** 基于 MFCC 的说话人聚类,余弦相似度匹配——无需外部模型即可识别"谁在什么时候说话"。
|
|
361
|
+
|
|
362
|
+
### 📁 目录结构
|
|
363
|
+
|
|
364
|
+
```
|
|
365
|
+
Omni-VRAM/
|
|
366
|
+
├── vram_hacker.cu # CUDA 核函数源码(KV-Cache 注入)
|
|
367
|
+
├── setup.py # 编译安装脚本
|
|
368
|
+
├── test_run.py # 快速集成测试
|
|
369
|
+
├── .env.example # 配置模板
|
|
370
|
+
│
|
|
371
|
+
├── vram_core/ # Python 核心库
|
|
372
|
+
│ ├── __init__.py # 包导出
|
|
373
|
+
│ ├── config.py # 配置管理(.env 加载)
|
|
374
|
+
│ ├── audio_utils.py # 音频格式检测与转换
|
|
375
|
+
│ ├── whisper_bridge.py # Whisper 多后端集成
|
|
376
|
+
│ ├── stream_processor.py # 实时流处理器 + VAD
|
|
377
|
+
│ ├── streaming_asr.py # 实时流式语音识别引擎
|
|
378
|
+
│ ├── api_server.py # FastAPI REST + WebSocket API
|
|
379
|
+
│ ├── noise_reduction.py # STFT 谱减法噪声抑制
|
|
380
|
+
│ ├── emotion_recognition.py # 声学特征情绪识别
|
|
381
|
+
│ └── speaker_diarization.py # MFCC 说话人识别与聚类
|
|
382
|
+
│
|
|
383
|
+
├── examples/ # 示例应用
|
|
384
|
+
│ ├── realtime_voice_assistant.py # 实时语音助手
|
|
385
|
+
│ ├── meeting_transcriber.py # 会议录音转写与摘要
|
|
386
|
+
│ ├── voice_chat_bot.py # 多轮语音对话机器人
|
|
387
|
+
│ ├── benchmark_suite.py # 性能基准测试套件
|
|
388
|
+
│ ├── api_demo.py # API 服务演示客户端
|
|
389
|
+
│ └── test_whisper_local.py # Whisper 本地测试
|
|
390
|
+
│
|
|
391
|
+
├── tests/ # 单元测试
|
|
392
|
+
│ ├── test_audio_utils.py
|
|
393
|
+
│ ├── test_whisper_bridge.py
|
|
394
|
+
│ ├── test_stream_processor.py
|
|
395
|
+
│ ├── test_noise_reduction.py
|
|
396
|
+
│ ├── test_emotion_recognition.py
|
|
397
|
+
│ └── test_speaker_diarization.py
|
|
398
|
+
│
|
|
399
|
+
└── docs/ # 文档
|
|
400
|
+
├── installation.md
|
|
401
|
+
├── quickstart.md
|
|
402
|
+
├── api_reference.md
|
|
403
|
+
├── examples.md
|
|
404
|
+
└── faq.md
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### 🧩 示例项目
|
|
408
|
+
|
|
409
|
+
| 示例 | 说明 | 运行命令 |
|
|
410
|
+
|------|------|----------|
|
|
411
|
+
| **实时语音助手** | 麦克风 → VAD → Whisper → 显示,支持文件录制 | `python examples/realtime_voice_assistant.py` |
|
|
412
|
+
| **会议录音转写** | 长时间录音,自动静音分段,导出文字记录 | `python examples/meeting_transcriber.py --output meeting.txt` |
|
|
413
|
+
| **语音对话机器人** | 多轮对话,对话历史追踪,LLM 可接入架构 | `python examples/voice_chat_bot.py` |
|
|
414
|
+
| **性能基准测试** | 全模块性能测试,自动生成 Markdown 报告 | `python examples/benchmark_suite.py --skip-whisper` |
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
## 📊 性能基准测试 (Benchmarks)
|
|
419
|
+
|
|
420
|
+
*硬件环境: NVIDIA RTX 3060 (12GB) | 平台: Windows WDDM | CUDA 版本: 12.1*
|
|
421
|
+
|
|
422
|
+
### 1. KV-Cache 显存注入
|
|
423
|
+
*任务:在一个容量为 100,000、维度为 4096 的 KV-Cache 张量中,连续追加 100 次(每次 50 个 token)的新特征。*
|
|
424
|
+
|
|
425
|
+
| 引擎 / 方法 | 延迟 | 复杂度 | 爆显存 (OOM) 风险 |
|
|
426
|
+
| :--- | :--- | :--- | :--- |
|
|
427
|
+
| PyTorch 原生 (`torch.cat`) | 90.32 ms | $O(N)$ (显存重新分配) | 极高 (显存碎片化) |
|
|
428
|
+
| **Omni-VRAM (零拷贝)** | **8.07 ms** | **$O(1)$ (底层指针偏移)** | **无** |
|
|
429
|
+
| **性能提升** | **11.19 倍** | - | - |
|
|
430
|
+
|
|
431
|
+
### 2. 音频处理管线
|
|
432
|
+
| 管线阶段 | 输入数据规模 | PyTorch / CPU 基准线 | Omni-VRAM C++ 算子 | 加速比 |
|
|
433
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
434
|
+
| **并发 VAD 检测** | 10 分钟 (16kHz) | 9.45 ms (CPU `unfold`) | **0.33 ms** | **约 28 倍** |
|
|
435
|
+
| **融合特征提取** | 60 秒 (16kHz) | 20.33 ms (VRAM 堆叠)| **1.05 ms** | **约 19 倍** |
|
|
436
|
+
|
|
437
|
+
### 3. Whisper 语音转写 (CPU)
|
|
438
|
+
| 模型 | 1 秒音频 | 5 秒音频 | 10 秒音频 |
|
|
439
|
+
| :--- | :--- | :--- | :--- |
|
|
440
|
+
| tiny | ~200ms | ~500ms | ~900ms |
|
|
441
|
+
| base | ~400ms | ~1200ms | ~2200ms |
|
|
442
|
+
|
|
443
|
+
> 运行 `python examples/benchmark_suite.py` 在你的硬件上进行自动化基准测试。
|
|
444
|
+
|
|
445
|
+
---
|
|
446
|
+
|
|
447
|
+
## 🛠️ 安装 (Installation)
|
|
448
|
+
|
|
449
|
+
```bash
|
|
450
|
+
# 克隆项目仓库
|
|
451
|
+
git clone https://github.com/Liangchenxu/Omni-VRAM.git
|
|
452
|
+
cd Omni-VRAM
|
|
453
|
+
|
|
454
|
+
# 安装所有依赖(核心 + 音频 + faster-whisper)
|
|
455
|
+
pip install -r requirements.txt
|
|
456
|
+
|
|
457
|
+
# 编译并安装 CUDA 扩展模块
|
|
458
|
+
# 注意:请确保已正确配置 NVCC 与 Visual Studio C++ 编译工具
|
|
459
|
+
python setup.py install
|
|
460
|
+
|
|
461
|
+
# (可选) 安装 Web API 服务依赖
|
|
462
|
+
pip install fastapi uvicorn python-multipart
|
|
463
|
+
|
|
464
|
+
# (可选) 安装 whisper.cpp 用于本地语音转写
|
|
465
|
+
# 详见 docs/installation.md
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### 配置文件
|
|
469
|
+
|
|
470
|
+
```bash
|
|
471
|
+
# 复制配置模板
|
|
472
|
+
cp .env.example .env
|
|
473
|
+
|
|
474
|
+
# 编辑 .env 文件设置你的配置
|
|
475
|
+
# 至少需要设置 WHISPER_CPP_PATH 和 WHISPER_MODEL_PATH 用于本地转写
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
> 详细安装指南请参阅 [docs/installation.md](docs/installation.md)。
|
|
479
|
+
|
|
480
|
+
## 💻 快速开始 (Quick Start)
|
|
481
|
+
|
|
482
|
+
### 基本 CUDA 操作
|
|
483
|
+
|
|
484
|
+
```python
|
|
485
|
+
import torch
|
|
486
|
+
import vram_core
|
|
487
|
+
|
|
488
|
+
# 1. 硬件底层雷达初始化
|
|
489
|
+
print(vram_core.scan_hardware_dna())
|
|
490
|
+
|
|
491
|
+
# 2. 算子融合音频处理
|
|
492
|
+
audio_stream = torch.randn(960000, device='cuda', dtype=torch.float32)
|
|
493
|
+
# 1毫秒内并发完成 VAD 检测、预加重与加窗
|
|
494
|
+
is_speaking, features = vram_core.smart_audio_listen(audio_stream, threshold=0.5)
|
|
495
|
+
|
|
496
|
+
# 3. 零拷贝大模型 KV-Cache 更新
|
|
497
|
+
hidden_dim = 4096
|
|
498
|
+
max_seq_len = 100000
|
|
499
|
+
# 仅进行一次物理显存预分配
|
|
500
|
+
kv_cache = torch.zeros((max_seq_len, hidden_dim), device='cuda', dtype=torch.float32)
|
|
501
|
+
current_pos = torch.tensor([0], device='cuda', dtype=torch.int32)
|
|
502
|
+
|
|
503
|
+
if is_speaking.item():
|
|
504
|
+
# 物理级显存直通注入(0 内存重新分配开销)
|
|
505
|
+
new_tokens = torch.randn((50, hidden_dim), device='cuda', dtype=torch.float32)
|
|
506
|
+
vram_core.append_to_kv_cache(kv_cache, new_tokens, current_pos)
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
### Whisper 语音转写
|
|
510
|
+
|
|
511
|
+
```python
|
|
512
|
+
from vram_core import WhisperBridge, WhisperBackend
|
|
513
|
+
|
|
514
|
+
# 自动后端检测初始化
|
|
515
|
+
whisper = WhisperBridge(
|
|
516
|
+
backend=WhisperBackend.AUTO,
|
|
517
|
+
whisper_model="base",
|
|
518
|
+
language="zh",
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
# 转写音频文件
|
|
522
|
+
result = whisper.transcribe("audio.wav")
|
|
523
|
+
print(f"文本: {result.text}")
|
|
524
|
+
print(f"置信度: {result.confidence}")
|
|
525
|
+
print(f"时长: {result.audio_duration}秒")
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### 实时流处理
|
|
529
|
+
|
|
530
|
+
```python
|
|
531
|
+
import numpy as np
|
|
532
|
+
from vram_core import StreamProcessor, StreamConfig, WhisperBridge, WhisperBackend
|
|
533
|
+
|
|
534
|
+
# 初始化组件
|
|
535
|
+
whisper = WhisperBridge(backend=WhisperBackend.AUTO, whisper_model="base")
|
|
536
|
+
config = StreamConfig(sample_rate=16000, chunk_duration_ms=100, vad_threshold=0.02)
|
|
537
|
+
processor = StreamProcessor(config=config, whisper_bridge=whisper)
|
|
538
|
+
|
|
539
|
+
# 设置回调
|
|
540
|
+
processor.on_transcription = lambda result: print(f"转写结果: {result.text}")
|
|
541
|
+
|
|
542
|
+
# 喂入音频分块(如来自麦克风)
|
|
543
|
+
audio_chunk = np.random.randn(1600).astype(np.float32)
|
|
544
|
+
processor.feed(audio_chunk)
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
### 实时流式语音识别 (Streaming ASR)
|
|
548
|
+
|
|
549
|
+
```python
|
|
550
|
+
import numpy as np
|
|
551
|
+
from vram_core import WhisperBridge, WhisperBackend, StreamASR, StreamASRConfig
|
|
552
|
+
|
|
553
|
+
# 初始化 Whisper
|
|
554
|
+
whisper = WhisperBridge(backend=WhisperBackend.AUTO, whisper_model="base")
|
|
555
|
+
|
|
556
|
+
# 配置流式 ASR
|
|
557
|
+
config = StreamASRConfig(
|
|
558
|
+
sample_rate=16000,
|
|
559
|
+
vad_threshold=0.015,
|
|
560
|
+
language="zh",
|
|
561
|
+
)
|
|
562
|
+
asr = StreamASR(config=config, whisper_bridge=whisper)
|
|
563
|
+
|
|
564
|
+
# 设置回调
|
|
565
|
+
asr.on_partial_result = lambda text: print(f"[部分] {text}")
|
|
566
|
+
asr.on_final_result = lambda result: print(f"[最终] {result.text}")
|
|
567
|
+
|
|
568
|
+
# 启动并喂入音频
|
|
569
|
+
asr.start()
|
|
570
|
+
audio_chunk = np.random.randn(3200).astype(np.float32) # 来自麦克风
|
|
571
|
+
asr.feed(audio_chunk)
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### Web API 服务
|
|
575
|
+
|
|
576
|
+
```bash
|
|
577
|
+
# 启动 API 服务
|
|
578
|
+
python vram_core/api_server.py --model base --language zh --port 8000
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
```python
|
|
582
|
+
# 客户端:文件上传转写
|
|
583
|
+
import requests
|
|
584
|
+
with open("audio.wav", "rb") as f:
|
|
585
|
+
resp = requests.post("http://localhost:8000/transcribe", files={"file": f})
|
|
586
|
+
print(resp.json()["text"])
|
|
587
|
+
|
|
588
|
+
# 客户端:WebSocket 流式转写
|
|
589
|
+
import websockets, asyncio
|
|
590
|
+
async def stream():
|
|
591
|
+
async with websockets.connect("ws://localhost:8000/stream") as ws:
|
|
592
|
+
await ws.send(audio_bytes) # 16-bit PCM, 16kHz 单声道
|
|
593
|
+
result = await ws.recv()
|
|
594
|
+
print(result)
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
> 更多示例请参阅 [docs/quickstart.md](docs/quickstart.md)。
|
|
598
|
+
|
|
599
|
+
---
|
|
600
|
+
|
|
601
|
+
## ⚠️ 免责声明 (Disclaimer)
|
|
602
|
+
**硬件交互警告:** Omni-VRAM 在 CUDA C++ 级别直接与物理 GPU 硬件交互,并采用激进的零拷贝指针操作以压榨极限吞吐量。
|
|
603
|
+
尽管经过了测试,但本软件按**"原样 (as is)"**提供,不作任何形式的担保。对于因使用本底层引擎而导致的任何内核崩溃、系统死锁、数据丢失或硬件不稳定,作者概不负责。**在生产环境中使用本软件,请自行承担一切风险。**
|
|
604
|
+
|
|
605
|
+
## 📜 协议 (License)
|
|
606
|
+
本项目基于 [**MIT License**](https://opensource.org/licenses/MIT) 开源。
|
|
607
|
+
您可以自由地在商业或非商业项目中使用、修改和分发本软件,但前提是必须保留原始版权声明及本许可声明。
|
|
608
|
+
|
|
609
|
+
---
|
|
610
|
+
|
|
611
|
+
## 🤝 贡献指南 (Contributing)
|
|
612
|
+
|
|
613
|
+
我们欢迎任何形式的贡献!
|
|
614
|
+
|
|
615
|
+
1. **Fork** 本仓库
|
|
616
|
+
2. 创建你的特性分支:`git checkout -b feature/amazing-feature`
|
|
617
|
+
3. 提交你的修改:`git commit -m 'feat: add amazing feature'`
|
|
618
|
+
4. 推送到分支:`git push origin feature/amazing-feature`
|
|
619
|
+
5. 提交 **Pull Request**
|
|
620
|
+
|
|
621
|
+
请确保:
|
|
622
|
+
- 所有单元测试通过:`pytest tests/ -v`
|
|
623
|
+
- 新功能附带相应的测试用例
|
|
624
|
+
- 遵循项目代码风格
|
|
625
|
+
|
|
626
|
+
> 详细信息请参阅 [CHANGELOG.md](CHANGELOG.md) 了解版本历史,[docs/faq.md](docs/faq.md) 了解常见问题。
|
|
627
|
+
|
|
628
|
+
---
|
|
629
|
+
|
|
630
|
+
## ⭐ Star 历史
|
|
631
|
+
|
|
632
|
+
[](https://star-history.com/#Liangchenxu/Omni-VRAM&Date)
|
|
633
|
+
|
|
634
|
+
---
|
|
635
|
+
|
|
636
|
+
<div align="center">
|
|
637
|
+
|
|
638
|
+
**[⬆ 回到顶部](#omni-vram-zero-copy-cuda-audio-to-llm-bridge)**
|
|
639
|
+
|
|
640
|
+
Made with ❤️ by [Liangchenxu](https://github.com/Liangchenxu)
|
|
641
|
+
|
|
642
|
+
</div>
|