|
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -**극한 KV 캐시 압축을 내장한 LLM 추론 엔진. 외부 의존성 없음. 순수 C.** |
| 5 | +**순수 C LLM 추론 엔진. 47 tok/s. 외부 의존성 없음.** |
6 | 6 |
|
7 | | -모델 로드, 텍스트 생성, KV 캐시 압축 — 하나의 바이너리, Python 불필요. |
| 7 | +로드 → 생성 → 끝. Python 없이. GPU 없이. 바이너리 하나로. |
8 | 8 |
|
9 | 9 | []() |
10 | 10 | []() |
11 | 11 | []() |
12 | | -[]() |
| 12 | +[]() |
13 | 13 |
|
14 | | ---- |
15 | | - |
16 | | -## 한눈에 보기 |
17 | | - |
18 | | -| | PyTorch | TurboQuant.cpp | |
19 | | -|---|---|---| |
20 | | -| **CPU 속도** | 0.8 tok/s | **18 tok/s** (23배) | |
21 | | -| **GPU 속도** | 10 tok/s (MPS) | **18 tok/s (CPU만으로!)** | |
22 | | -| **모델 로딩** | ~3초 | **< 0.3초** (TQM mmap) | |
23 | | -| **가중치 메모리** | 1.7 GB (BF16) | **270 MB** (Q4) | |
24 | | -| **KV 캐시** | FP16 (전체 크기) | **7.5배 압축** (4-bit) | |
25 | | -| **의존성** | PyTorch + transformers | **0개** (순수 C) | |
26 | | - |
27 | | -> Qwen3.5-0.8B, Apple Silicon 기준. CPU만으로 PyTorch GPU보다 빠름. |
| 14 | +``` |
| 15 | +PyTorch CPU: 0.8 tok/s |
| 16 | +PyTorch GPU: 10 tok/s |
| 17 | +TurboQuant CPU: 47 tok/s ← 59배 빠름, GPU 불필요 |
| 18 | +``` |
28 | 19 |
|
29 | 20 | --- |
30 | 21 |
|
31 | | -## 실행하기 |
| 22 | +## 30초 빠른 시작 |
32 | 23 |
|
33 | 24 | ```bash |
34 | 25 | git clone https://github.com/quantumaikr/TurboQuant.cpp && cd TurboQuant.cpp |
35 | 26 | cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build -j$(nproc) |
36 | 27 |
|
37 | | -# Step 1: 모델 변환 (1회, 자동 감지) |
| 28 | +# 모델 변환 (1회, HuggingFace 캐시 자동 감지) |
38 | 29 | ./build/tq_convert |
39 | 30 |
|
40 | | -# Step 2: 추론 (즉시 로딩, 토크나이저 내장) |
41 | | -./build/tq_run model.tqm -p "What is AI?" -j 4 |
| 31 | +# 실행 |
| 32 | +./build/tq_run model.tqm -p "What is deep learning?" -j 4 |
42 | 33 | ``` |
43 | 34 |
|
44 | 35 | ``` |
45 | | -Prompt: What is AI? |
| 36 | +Prompt: What is deep learning? |
46 | 37 | --- |
47 | | -Artificial intelligence (AI) is a field of computer science that focuses |
48 | | -on creating systems capable of performing tasks that typically require |
49 | | -human intelligence... |
| 38 | +Deep learning is a field of artificial intelligence and machine learning |
| 39 | +that uses artificial neural networks to learn complex patterns... |
50 | 40 | --- |
51 | | -50 tokens in 2.7s (18.3 tok/s, 4 threads, kv=uniform_4b) |
52 | | -``` |
53 | | - |
54 | | -### Python |
55 | | - |
56 | | -```python |
57 | | -from turboquant import TurboQuant |
58 | | -tq = TurboQuant("cpu") |
59 | | -compressed = tq.quantize_keys(keys, TurboQuant.UNIFORM_4B) # 7.5배 압축 |
60 | | -scores = tq.attention(query, compressed, seq_len, dim, TurboQuant.UNIFORM_4B) |
| 41 | +100 tokens in 2.1s (46.9 tok/s, 4 threads, weights=Q4, kv=uniform_4b) |
61 | 42 | ``` |
62 | 43 |
|
63 | 44 | --- |
64 | 45 |
|
65 | | -## 왜 빠른가 |
| 46 | +## 왜 TurboQuant인가? |
| 47 | + |
| 48 | +| | PyTorch | TurboQuant.cpp | |
| 49 | +|---|---|---| |
| 50 | +| **속도** | 0.8 tok/s | **47 tok/s** (59배) | |
| 51 | +| **로딩** | 3초 | **0.3초** (mmap) | |
| 52 | +| **가중치 메모리** | 1.7 GB | **270 MB** (Q4) | |
| 53 | +| **KV 캐시** | 전체 크기 | **7.5배 압축** | |
| 54 | +| **의존성** | PyTorch, transformers | **없음** | |
| 55 | +| **바이너리** | ~2 GB 설치 | **~1 MB** | |
| 56 | +| **품질** | 기준 | **코사인 0.999** (PyTorch 대비) | |
66 | 57 |
|
67 | | -### 1. 자체 추론 엔진 |
| 58 | +--- |
68 | 59 |
|
69 | | -래퍼가 아닌 순수 C 추론 엔진: |
| 60 | +## 구성 요소 |
70 | 61 |
|
71 | 62 | ``` |
72 | | -모델 로딩 safetensors (mmap, BF16→FP32 스트리밍) |
73 | | -토크나이저 HuggingFace BPE (248K 어휘) |
74 | | -Forward Pass DeltaNet + Self-Attention (Qwen3.5 하이브리드) |
75 | | -KV 캐시 TurboQuant 양자화 (4-bit, 자동 압축) |
76 | | -Attention 정수 Q4×Q8 (FP32 대비 2.9배 빠름) |
77 | | -가중치 Q8 양자화 (-q 플래그, 메모리 4배 절약) |
78 | | -생성 Top-p 샘플링, 스트리밍 출력 |
| 63 | +┌─────────────────────────────────────────────────────┐ |
| 64 | +│ tq_convert │ |
| 65 | +│ safetensors → TQM (사전 양자화, mmap 가능) │ |
| 66 | +├─────────────────────────────────────────────────────┤ |
| 67 | +│ tq_run │ |
| 68 | +│ TQM → mmap 로드 → forward → 토큰 스트리밍 │ |
| 69 | +│ │ |
| 70 | +│ ┌─── Forward Pass ────────────────────────────┐ │ |
| 71 | +│ │ DeltaNet (18 레이어, 순환) │ │ |
| 72 | +│ │ Self-Attention (6 레이어, GQA + RoPE) │ │ |
| 73 | +│ │ SwiGLU FFN (전체 24 레이어) │ │ |
| 74 | +│ │ KV 캐시: TurboQuant Q4 양자화 │ │ |
| 75 | +│ │ Attention: 정수 Q4×Q8 (FP32 대비 2.9배) │ │ |
| 76 | +│ └─────────────────────────────────────────────┘ │ |
| 77 | +│ │ |
| 78 | +│ Q4 가중치 ── NEON matmul ── 멀티스레드 │ |
| 79 | +└─────────────────────────────────────────────────────┘ |
79 | 80 | ``` |
80 | 81 |
|
81 | | -### 2. 정수 도메인 Attention |
82 | | - |
83 | | -양자화 데이터에서 직접 attention 계산 — 역양자화 없음: |
| 82 | +### 5가지 최적화 |
84 | 83 |
|
85 | | -``` |
86 | | -FP32 attention: 22.8 μs (기준) |
87 | | -Q4×Q8 정수: 7.8 μs (2.9배 빠름, ARM vdotq_s32) |
88 | | -``` |
| 84 | +| # | 기법 | 효과 | |
| 85 | +|---|------|------| |
| 86 | +| 1 | **Q4 가중치** — 4-bit, 8배 작음 | 2배 빠름 | |
| 87 | +| 2 | **TQM 포맷** — 사전 양자화 mmap | 10배 빠른 로딩 | |
| 88 | +| 3 | **정수 attention** — Q4×Q8, ARM vdotq_s32 | 2.9배 빠름 | |
| 89 | +| 4 | **멀티스레드 matmul** — pthread, NEON | 1.6배 빠름 | |
| 90 | +| 5 | **스트리밍 BF16** — 임베딩 온디맨드 | 메모리 6배 절약 | |
89 | 91 |
|
90 | | -### 3. Q8 가중치 양자화 |
| 92 | +### 실제 모델 검증 |
91 | 93 |
|
92 | | -가중치 4배 압축, 품질 손실 무시: |
| 94 | +[Qwen3.5-0.8B](https://huggingface.co/Qwen/Qwen3.5-0.8B) — 실제 추론, 합성 아님: |
93 | 95 |
|
94 | 96 | ``` |
95 | | -./build/tq_run model.safetensors -p "1+1=" -q |
96 | | -→ "2" (정확, 2.1 GB 대신 533 MB) |
| 97 | +"1+1=" → "2" ✓ |
| 98 | +"The capital of France is" → "Paris" ✓ |
| 99 | +"What is deep learning?" → 정확한 문단 ✓ |
| 100 | +PyTorch 대비 logits 코사인 → 0.999 ✓ |
97 | 101 | ``` |
98 | 102 |
|
99 | 103 | --- |
100 | 104 |
|
101 | | -## 실제 모델 검증 |
| 105 | +## 시퀀스 길이별 속도 |
102 | 106 |
|
103 | | -[Qwen3.5-0.8B](https://huggingface.co/Qwen/Qwen3.5-0.8B)로 검증 — 합성이 아닌 실제 추론: |
104 | | - |
105 | | -| 테스트 | 결과 | |
106 | | -|--------|------| |
107 | | -| "1+1=" | **2** ✓ | |
108 | | -| "The capital of France is" | **Paris** ✓ | |
109 | | -| "The capital of Japan is" | **Tokyo** ✓ | |
110 | | -| "What is deep learning?" | 정확한 문단 ✓ | |
111 | | -| PyTorch 대비 logits 코사인 | **0.999** | |
112 | | - |
113 | | -### KV 캐시 품질 |
114 | | - |
115 | | -| 타입 | 압축률 | 품질 (코사인) | 등급 | |
116 | | -|------|--------|-------------|------| |
117 | | -| **uniform_4b** | 7.5x | 0.994 | **A+** | |
118 | | -| **mixed_4b8** | 6.4x | 0.994 | **A+** | |
119 | | -| uniform_2b | 14.2x | 0.953 | A | |
| 107 | +``` |
| 108 | +토큰 수 속도 비고 |
| 109 | +────── ───────── ────────────────── |
| 110 | +10 12 tok/s 첫 토큰 지연 포함 |
| 111 | +30 41 tok/s ← 40 tok/s 돌파 |
| 112 | +50 44 tok/s |
| 113 | +100 47 tok/s ← 정상 속도 |
| 114 | +200 48 tok/s ← 최대 |
| 115 | +``` |
120 | 116 |
|
121 | 117 | --- |
122 | 118 |
|
123 | | -## CLI 사용법 |
| 119 | +## CLI |
124 | 120 |
|
125 | 121 | ```bash |
126 | | -# 기본 추론 |
127 | | -./build/tq_run MODEL -t TOKENIZER -p "프롬프트" -n 100 |
128 | | - |
129 | | -# 옵션 |
130 | | --j 4 # 스레드 수 (기본: 4) |
131 | | --q # Q8 가중치 양자화 (메모리 4배 절약) |
132 | | --k uniform_4b # KV 캐시 타입 |
133 | | --T 0.7 # temperature |
134 | | --P 0.9 # top-p |
135 | | ---info # 모델 정보 표시 |
| 122 | +# 변환 (1회) |
| 123 | +./build/tq_convert # 자동 감지 |
| 124 | + |
| 125 | +# 추론 |
| 126 | +./build/tq_run model.tqm -p "Hello" # 토크나이저 내장 |
| 127 | +./build/tq_run model.tqm -p "Hello" -j 4 -n 200 -T 0.7 |
| 128 | + |
| 129 | +# Python CLI |
| 130 | +python3 tools/tq info # 양자화 타입 |
| 131 | +python3 tools/tq +memory llama-3.2-3b 65536 |
| 132 | +python3 tools/tq_chat.py "What is AI?" # 네이티브 엔진 + KV 분석 |
136 | 133 | ``` |
137 | 134 |
|
138 | | -### Python CLI |
| 135 | +### Python API |
139 | 136 |
|
140 | | -```bash |
141 | | -python3 tools/tq info # 양자화 타입 정보 |
142 | | -python3 tools/tq bench # 성능 벤치마크 |
143 | | -python3 tools/tq +memory llama-3.2-3b 65536 # 메모리 계산 |
144 | | -python3 tools/tq +memory qwen3.5-0.8b 131072 --json # JSON 출력 |
| 137 | +```python |
| 138 | +from turboquant import TurboQuant |
| 139 | +tq = TurboQuant("cpu") |
| 140 | +compressed = tq.quantize_keys(keys, TurboQuant.UNIFORM_4B) # 7.5배 압축 |
| 141 | +scores = tq.attention(query, compressed, seq_len, dim, TurboQuant.UNIFORM_4B) |
145 | 142 | ``` |
146 | 143 |
|
147 | 144 | --- |
148 | 145 |
|
149 | 146 | ## 문서 |
150 | 147 |
|
151 | | -| 문서 | 설명 | |
| 148 | +| 문서 | 내용 | |
152 | 149 | |------|------| |
153 | | -| **[시작 가이드](docs/getting-started.md)** | 빌드, 실행, 통합 | |
154 | | -| [아키텍처](docs/architecture.md) | 엔진 설계, 타입 시스템 | |
155 | | -| [Qwen3.5 검증](docs/qwen35_validation_results.md) | 실제 모델 A/B 결과 | |
| 150 | +| **[시작 가이드](docs/getting-started.md)** | 빌드, 변환, 실행, 통합 | |
| 151 | +| [아키텍처](docs/architecture.md) | 엔진 설계, 4-layer 스택 | |
| 152 | +| [Qwen3.5 결과](docs/qwen35_validation_results.md) | 실제 모델 A/B 테스트 | |
| 153 | +| [변경 이력](CHANGELOG.md) | 전체 버전 히스토리 | |
156 | 154 | | [통합 가이드](docs/integration_guide.md) | llama.cpp, vLLM, Python | |
157 | | -| [변경 이력](CHANGELOG.md) | 릴리즈 노트 | |
158 | 155 |
|
159 | 156 | --- |
160 | 157 |
|
161 | | -## 기술 요약 |
| 158 | +## 기술 상세 |
162 | 159 |
|
163 | | -- **자체 추론 엔진** — 모델 로드, 토큰화, forward, 생성을 순수 C로 |
| 160 | +- **8,500줄 이상의 C** — 완전한 추론 엔진, 래퍼 아님 |
164 | 161 | - **8개 양자화 타입** — Uniform, Mixed Precision, PolarQuant, QJL, TurboQuant |
165 | | -- **Q8 가중치** — 메모리 4배 절약, NEON 최적화 matmul |
166 | | -- **정수 attention** — Q4×Q8, ARM `vdotq_s32` |
167 | | -- **멀티스레드** — pthread matmul, 설정 가능한 스레드 수 |
168 | | -- **하이브리드 모델** — DeltaNet (순환) + Self-Attention (Qwen3.5) |
169 | | -- **RHT** — Random Hadamard Transform, MSE 3.9배 감소 |
170 | | -- **K/V 비대칭** — 키/값 독립 비트 할당 |
171 | | -- **외부 의존성 제로** — 순수 C11, libc/libm만 |
172 | | -- **70+ 테스트** — 19 C++ 스위트 + 22 Python, ASan/UBSan/TSan 클린 |
| 162 | +- **TQM 포맷** — 사전 양자화 바이너리, mmap 즉시 로딩 |
| 163 | +- **DeltaNet + Self-Attention** — Qwen3.5 하이브리드 아키텍처 순수 C |
| 164 | +- **BPE 토크나이저** — HuggingFace 호환 (248K 어휘, TQM 내장) |
| 165 | +- **Q4×Q8 정수 attention** — ARM vdotq_s32, float 역양자화 없음 |
| 166 | +- **멀티스레드** — pthread matmul + NEON, 설정 가능 |
| 167 | +- **반복 방지** — repetition penalty로 퇴화 방지 |
| 168 | +- **20 테스트 스위트, 70+ 테스트** — ASan + UBSan + TSan 클린 |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## 여정 |
| 173 | + |
| 174 | +``` |
| 175 | +1일차 오전: 빈 디렉토리 |
| 176 | +1일차 오후: KV 캐시 압축 라이브러리 (8개 타입, A/B 테스트) |
| 177 | +1일차 저녁: 완전한 추론 엔진 (모델 로드 → 텍스트 생성) |
| 178 | +1일차 밤: 47 tok/s, Q4 가중치, TQM 즉시 로딩 |
| 179 | +
|
| 180 | +C 코드: 8,500줄 이상 |
| 181 | +테스트: 20개 스위트 (70+ 테스트) |
| 182 | +커밋: 52개 |
| 183 | +속도: 0.8 → 47 tok/s (59배 개선) |
| 184 | +``` |
173 | 185 |
|
174 | 186 | --- |
175 | 187 |
|
176 | 188 | ## 참고 논문 |
177 | 189 |
|
178 | | -- **TurboQuant** — [arXiv:2504.19874](https://arxiv.org/abs/2504.19874) (ICLR 2026) |
179 | | -- **QJL** — [arXiv:2406.03482](https://arxiv.org/abs/2406.03482) (AAAI 2025) |
180 | | -- **PolarQuant** — [arXiv:2502.02617](https://arxiv.org/abs/2502.02617) (AISTATS 2026) |
| 190 | +- [TurboQuant](https://arxiv.org/abs/2504.19874) (ICLR 2026) — KV 캐시 압축 |
| 191 | +- [QJL](https://arxiv.org/abs/2406.03482) (AAAI 2025) — 1비트 양자화 JL 변환 |
| 192 | +- [PolarQuant](https://arxiv.org/abs/2502.02617) (AISTATS 2026) — 극좌표 양자화 |
| 193 | + |
| 194 | +아키텍처: [llama.cpp](https://github.com/ggerganov/llama.cpp), [vLLM](https://github.com/vllm-project/vllm), [ONNX](https://github.com/onnx/onnx) 참조. |
181 | 195 |
|
182 | 196 | --- |
183 | 197 |
|
184 | | -**개발사: [QuantumAI Inc.](https://quantumai.kr)** | [hi@quantumai.kr](mailto:hi@quantumai.kr) |
| 198 | +**[QuantumAI Inc.](https://quantumai.kr)** | [hi@quantumai.kr](mailto:hi@quantumai.kr) |
0 commit comments