termux-bitnet 1.0.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.
- termux_bitnet-1.0.0/CMakeLists.txt +71 -0
- termux_bitnet-1.0.0/LICENSE +17 -0
- termux_bitnet-1.0.0/MANIFEST.in +6 -0
- termux_bitnet-1.0.0/PKG-INFO +215 -0
- termux_bitnet-1.0.0/README.md +180 -0
- termux_bitnet-1.0.0/include/termux_bitnet.h +145 -0
- termux_bitnet-1.0.0/pyproject.toml +52 -0
- termux_bitnet-1.0.0/setup.cfg +4 -0
- termux_bitnet-1.0.0/setup.py +52 -0
- termux_bitnet-1.0.0/src/c_api.cpp +27 -0
- termux_bitnet-1.0.0/src/ggml_bitnet_mad.cpp +317 -0
- termux_bitnet-1.0.0/src/llama_bitnet_core.cpp +385 -0
- termux_bitnet-1.0.0/src/llama_bitnet_core.h +52 -0
- termux_bitnet-1.0.0/src/main.cpp +127 -0
- termux_bitnet-1.0.0/termux_bitnet/__init__.py +19 -0
- termux_bitnet-1.0.0/termux_bitnet/cli.py +222 -0
- termux_bitnet-1.0.0/termux_bitnet/config.py +41 -0
- termux_bitnet-1.0.0/termux_bitnet/downloader.py +122 -0
- termux_bitnet-1.0.0/termux_bitnet/engine.py +223 -0
- termux_bitnet-1.0.0/termux_bitnet/hardware.py +93 -0
- termux_bitnet-1.0.0/termux_bitnet/server.py +187 -0
- termux_bitnet-1.0.0/termux_bitnet.egg-info/PKG-INFO +215 -0
- termux_bitnet-1.0.0/termux_bitnet.egg-info/SOURCES.txt +26 -0
- termux_bitnet-1.0.0/termux_bitnet.egg-info/dependency_links.txt +1 -0
- termux_bitnet-1.0.0/termux_bitnet.egg-info/entry_points.txt +2 -0
- termux_bitnet-1.0.0/termux_bitnet.egg-info/requires.txt +10 -0
- termux_bitnet-1.0.0/termux_bitnet.egg-info/top_level.txt +1 -0
- termux_bitnet-1.0.0/tests/test_engine.py +127 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.14)
|
|
2
|
+
project(termux_bitnet LANGUAGES C CXX)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_C_STANDARD 11)
|
|
5
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
6
|
+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
|
7
|
+
|
|
8
|
+
# Optimization Options
|
|
9
|
+
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
|
|
10
|
+
option(GGML_NEON "Enable ARM NEON SIMD optimizations" ON)
|
|
11
|
+
option(GGML_ARM_DOTPROD "Enable ARM Dot Product extension" AUTO)
|
|
12
|
+
option(BUILD_CLI "Build native standalone CLI" ON)
|
|
13
|
+
|
|
14
|
+
# Compiler Optimization Flags
|
|
15
|
+
if(MSVC)
|
|
16
|
+
add_compile_options(/O2 /W3 /utf-8)
|
|
17
|
+
else()
|
|
18
|
+
add_compile_options(-O3 -Wall -Wextra -Wno-unused-parameter)
|
|
19
|
+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
|
|
20
|
+
if(GGML_NEON)
|
|
21
|
+
add_definitions(-D__ARM_NEON)
|
|
22
|
+
if(GGML_ARM_DOTPROD)
|
|
23
|
+
add_compile_options(-march=armv8.2-a+dotprod+fp16)
|
|
24
|
+
add_definitions(-D__ARM_FEATURE_DOTPROD=1)
|
|
25
|
+
else()
|
|
26
|
+
add_compile_options(-march=armv8-a+simd+fp16)
|
|
27
|
+
endif()
|
|
28
|
+
endif()
|
|
29
|
+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
|
|
30
|
+
# x86 Fallback / Development environment
|
|
31
|
+
if(NOT MSVC)
|
|
32
|
+
add_compile_options(-mavx2 -mfma)
|
|
33
|
+
endif()
|
|
34
|
+
endif()
|
|
35
|
+
endif()
|
|
36
|
+
|
|
37
|
+
# Include Directories
|
|
38
|
+
include_directories(
|
|
39
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include
|
|
40
|
+
${CMAKE_CURRENT_SOURCE_DIR}/src
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Core Sources
|
|
44
|
+
set(BITNET_CORE_SOURCES
|
|
45
|
+
src/ggml_bitnet_mad.cpp
|
|
46
|
+
src/llama_bitnet_core.cpp
|
|
47
|
+
src/c_api.cpp
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Shared Library for Python SDK & C/C++ Embedding
|
|
51
|
+
add_library(termux_bitnet SHARED ${BITNET_CORE_SOURCES})
|
|
52
|
+
target_include_directories(termux_bitnet PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
53
|
+
|
|
54
|
+
if(UNIX AND NOT APPLE)
|
|
55
|
+
target_link_libraries(termux_bitnet PRIVATE m pthread dl)
|
|
56
|
+
endif()
|
|
57
|
+
|
|
58
|
+
# Native CLI Application
|
|
59
|
+
if(BUILD_CLI)
|
|
60
|
+
add_executable(termux-bitnet-cli src/main.cpp)
|
|
61
|
+
target_link_libraries(termux-bitnet-cli PRIVATE termux_bitnet)
|
|
62
|
+
target_include_directories(termux-bitnet-cli PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
63
|
+
endif()
|
|
64
|
+
|
|
65
|
+
# Install Directives
|
|
66
|
+
install(TARGETS termux_bitnet
|
|
67
|
+
LIBRARY DESTINATION lib
|
|
68
|
+
ARCHIVE DESTINATION lib
|
|
69
|
+
RUNTIME DESTINATION bin
|
|
70
|
+
)
|
|
71
|
+
install(FILES include/termux_bitnet.h DESTINATION include)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2026 uno-km (https://github.com/uno-km)
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the " License\);
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an \AS IS\ BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: termux-bitnet
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: High-performance 1.58-bit BitNet inference engine, Python SDK & CLI optimized for Android Termux and ARM64 architecture.
|
|
5
|
+
Author-email: uno-km <unokim.dev@gmail.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/uno-km/termux-bitnet
|
|
8
|
+
Project-URL: Documentation, https://uno-km.github.io/termux-bitnet/
|
|
9
|
+
Project-URL: Repository, https://github.com/uno-km/termux-bitnet.git
|
|
10
|
+
Project-URL: Issues, https://github.com/uno-km/termux-bitnet/issues
|
|
11
|
+
Keywords: bitnet,1-bit-llm,termux,arm64,neon,dotprod,edge-ai,on-device-ai
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: C++
|
|
22
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
23
|
+
Classifier: Operating System :: Android
|
|
24
|
+
Requires-Python: >=3.8
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: tqdm>=4.64.0
|
|
28
|
+
Requires-Dist: requests>=2.28.0
|
|
29
|
+
Provides-Extra: server
|
|
30
|
+
Requires-Dist: uvicorn>=0.20.0; extra == "server"
|
|
31
|
+
Requires-Dist: fastapi>=0.95.0; extra == "server"
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest-benchmark>=4.0.0; extra == "dev"
|
|
35
|
+
|
|
36
|
+
# termux-bitnet
|
|
37
|
+
|
|
38
|
+
> **Single C++ Core & Multi-Language Thin Gateways (Python SDK + Node.js npm) for 1.58-bit (i2_s) BitNet On-Device Inference on Android Termux & ARM64.**
|
|
39
|
+
|
|
40
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
41
|
+
[]()
|
|
42
|
+
[]()
|
|
43
|
+
[]()
|
|
44
|
+
[]()
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 1. 아키텍처 철학: "Single C++ Core, Dual Thin Gateways"
|
|
49
|
+
|
|
50
|
+
`termux-bitnet`은 **"연산과 텐서 제어의 모든 핵심(Heavy Lifting)은 오직 순수 C++ 단 한 곳에서만 수행하고, Python(`pip`)과 Node.js(`npm`)는 제로 오버헤드로 C++ 엔진에 진입하는 경량 입구(Thin Gateway / FFI Boundary) 역할만 수행한다"**는 글로벌 표준 오픈소스 AI 엔진 설계 원칙을 철저히 준수합니다.
|
|
51
|
+
|
|
52
|
+
```mermaid
|
|
53
|
+
graph TD
|
|
54
|
+
subgraph Gateways ["Multi-Language Thin Gateways (Lightweight Entry Points)"]
|
|
55
|
+
G1["Python Gateway<br/><code>pip install termux-bitnet</code><br/>(ctypes Zero-Copy FFI)"]
|
|
56
|
+
G2["Node.js / TS Gateway<br/><code>npm install termux-bitnet</code><br/>(Native CLI / IPC)"]
|
|
57
|
+
G3["Native CLI<br/><code>termux-bitnet-cli</code>"]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
subgraph Boundary ["Strict C ABI Boundary (include/termux_bitnet.h)"]
|
|
61
|
+
ABI["bitnet_init() | bitnet_eval() | bitnet_generate_stream() | bitnet_free()"]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
subgraph Core ["Single High-Performance C++ Core (libtermux_bitnet.so)"]
|
|
65
|
+
K1["ARM64 NEON + DotProd Accel (vdotq_s32)"]
|
|
66
|
+
K2["ARM64 NEON + FMA Fallback (vmlal_s8)"]
|
|
67
|
+
K3["QK=128 32-Stride Interleaved Scalar Fallback"]
|
|
68
|
+
KV["KV Cache & Top-P / Temperature Sampler"]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
G1 --> ABI
|
|
72
|
+
G2 --> ABI
|
|
73
|
+
G3 --> ABI
|
|
74
|
+
ABI --> Core
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 2. 검증된 비트넷 모델 레지스트리 (Verified Model Registry)
|
|
82
|
+
|
|
83
|
+
`termux-bitnet`은 Hugging Face 상의 공식/커뮤니티 1.58-bit GGUF 모델을 지원하며, 내장 다운로더(`download`)를 통해 원터치로 캐싱 및 구동할 수 있습니다:
|
|
84
|
+
|
|
85
|
+
| Alias | 원본 저장소 및 모델 파일 | 파라미터 / 용량 | 특징 |
|
|
86
|
+
|---|---|---|---|
|
|
87
|
+
| `bitnet-2b` | `microsoft/bitnet-b1.58-2B-4T-gguf` | 2.4B / **1.13 GB** | Microsoft 공식 1.58-bit 플래그십 (모바일 권장) |
|
|
88
|
+
| `bitnet-large` | `RichardErkhov/1bitLLM_-_bitnet_b1_58-large-gguf` | 0.7B / **404 MB** | 저사양 모바일/Termux 기기용 초경량 엔진 |
|
|
89
|
+
| `bitnet-3b` | `Green-Sky/bitnet_b1_58-3B-GGUF` | 3.3B / **730 MB** | 대용량 고정밀 온디바이스 모델 |
|
|
90
|
+
| `bitnet-3b-q4` | `RichardErkhov/1bitLLM_-_bitnet_b1_58-3B-gguf` | 3.3B / **1.83 GB** | Q4 양자화 고성능 3B 모델 |
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
# 모델 원터치 다운로드 (이어받기 지원)
|
|
94
|
+
termux-bitnet download bitnet-2b
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 3. 빠른 시작 (Quick Start)
|
|
100
|
+
|
|
101
|
+
### 3.1 Python Gateway (`pip`)
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
# 설치
|
|
105
|
+
pip install termux-bitnet
|
|
106
|
+
|
|
107
|
+
# 실행 (풀 파라미터 제어)
|
|
108
|
+
termux-bitnet run -m ~/.cache/termux-bitnet/models/bitnet-2b-ggml-model-i2_s.gguf \
|
|
109
|
+
-p "The capital of France is" \
|
|
110
|
+
-t 8 -c 2048 -n 128 --temp 0.7 --top-p 0.95 --top-k 40 --repeat-penalty 1.15
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from termux_bitnet import BitNetEngine, BitNetConfig
|
|
115
|
+
|
|
116
|
+
config = BitNetConfig(
|
|
117
|
+
model_path="~/.cache/termux-bitnet/models/bitnet-2b-ggml-model-i2_s.gguf",
|
|
118
|
+
n_threads=8,
|
|
119
|
+
temperature=0.7,
|
|
120
|
+
top_p=0.95,
|
|
121
|
+
top_k=40,
|
|
122
|
+
min_p=0.05,
|
|
123
|
+
repeat_penalty=1.15,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
with BitNetEngine(config) as engine:
|
|
127
|
+
for token in engine.generate_stream("Write a Python palindrome check:"):
|
|
128
|
+
print(token, end="", flush=True)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
### 3.2 Node.js Gateway (`npm`)
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
# 설치
|
|
137
|
+
npm install termux-bitnet
|
|
138
|
+
|
|
139
|
+
# CLI 실행
|
|
140
|
+
npx termux-bitnet run -p "Explain harmonic mean in one sentence" -t 8 --temp 0.7 --top-p 0.95
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```javascript
|
|
144
|
+
const { createEngine } = require('termux-bitnet');
|
|
145
|
+
|
|
146
|
+
async function main() {
|
|
147
|
+
const engine = createEngine({
|
|
148
|
+
threads: 8,
|
|
149
|
+
temperature: 0.7,
|
|
150
|
+
topP: 0.95,
|
|
151
|
+
topK: 40,
|
|
152
|
+
repeatPenalty: 1.15,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
await engine.generateStream('Question: Explain harmonic mean:', 128, (token) => {
|
|
156
|
+
process.stdout.write(token);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
main();
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## 4. 파라미터 매트릭스 (Full Parameter Matrix)
|
|
166
|
+
|
|
167
|
+
| CLI Flag | Python (`BitNetConfig`) | Node.js (`BitNetOptions`) | C ABI (`bitnet_params_t`) | 기본값 | 설명 |
|
|
168
|
+
|---|---|---|---|---|---|
|
|
169
|
+
| `-m, --model` | `model_path` | `modelPath` | `model_path` | `""` | GGUF 모델 파일 경로 |
|
|
170
|
+
| `-p, --prompt` | `prompt` | `prompt` | `prompt` | `""` | 입력 프롬프트 텍스트 |
|
|
171
|
+
| `-t, --threads` | `n_threads` | `threads` | `n_threads` | `cores` | CPU 워커 스레드 수 |
|
|
172
|
+
| `-c, --ctx-size` | `n_ctx` | `contextSize` | `n_ctx` | `2048` | KV Cache 컨텍스트 윈도우 크기 |
|
|
173
|
+
| `-b, --batch-size`| `n_batch` | `batchSize` | `n_batch` | `512` | 프롬프트 평가 배치 크기 |
|
|
174
|
+
| `-n, --n-predict` | `n_predict` | `maxTokens` | `n_predict` | `128` | 최대 생성 토큰 수 |
|
|
175
|
+
| `--temp` | `temperature` | `temperature` | `temperature` | `0.7` | Softmax 온도 (0.0=Greedy) |
|
|
176
|
+
| `--top-p` | `top_p` | `topP` | `top_p` | `0.95` | Nucleus Top-P 샘플링 |
|
|
177
|
+
| `--top-k` | `top_k` | `topK` | `top_k` | `40` | Top-K 샘플링 컷오프 |
|
|
178
|
+
| `--min-p` | `min_p` | `minP` | `min_p` | `0.05` | Min-P 상대 확률 컷오프 |
|
|
179
|
+
| `--repeat-penalty`| `repeat_penalty` | `repeatPenalty` | `repeat_penalty` | `1.15` | 반복 토큰 억제 계수 |
|
|
180
|
+
| `-s, --seed` | `seed` | `seed` | `seed` | `0` | 난수 시드 (0=무작위) |
|
|
181
|
+
| `--system-prompt` | `system_prompt` | `systemPrompt` | `system_prompt` | `""` | 시스템 프롬프트 접두사 |
|
|
182
|
+
| `-r, --stop` | `stop_tokens` | `stopTokens` | `stop_tokens` | `""` | 생성 중단 토큰 목록 |
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## 5. C ABI 직접 임베딩 (C/C++)
|
|
187
|
+
|
|
188
|
+
```c
|
|
189
|
+
#include "termux_bitnet.h"
|
|
190
|
+
#include <stdio.h>
|
|
191
|
+
|
|
192
|
+
int main() {
|
|
193
|
+
bitnet_params_t params = bitnet_default_params();
|
|
194
|
+
params.temperature = 0.7f;
|
|
195
|
+
params.top_p = 0.95f;
|
|
196
|
+
params.top_k = 40;
|
|
197
|
+
bitnet_context_t ctx = bitnet_init(¶ms);
|
|
198
|
+
|
|
199
|
+
bitnet_generate_stream(ctx, "The capital of France is", 64,
|
|
200
|
+
[](const char* token, int32_t id, void* u) {
|
|
201
|
+
printf("%s", token);
|
|
202
|
+
return true;
|
|
203
|
+
}, NULL);
|
|
204
|
+
|
|
205
|
+
bitnet_free(ctx);
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## 6. 라이선스 (License)
|
|
213
|
+
|
|
214
|
+
Apache License 2.0. Copyright (c) 2026 uno-km.
|
|
215
|
+
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# termux-bitnet
|
|
2
|
+
|
|
3
|
+
> **Single C++ Core & Multi-Language Thin Gateways (Python SDK + Node.js npm) for 1.58-bit (i2_s) BitNet On-Device Inference on Android Termux & ARM64.**
|
|
4
|
+
|
|
5
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
6
|
+
[]()
|
|
7
|
+
[]()
|
|
8
|
+
[]()
|
|
9
|
+
[]()
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 1. 아키텍처 철학: "Single C++ Core, Dual Thin Gateways"
|
|
14
|
+
|
|
15
|
+
`termux-bitnet`은 **"연산과 텐서 제어의 모든 핵심(Heavy Lifting)은 오직 순수 C++ 단 한 곳에서만 수행하고, Python(`pip`)과 Node.js(`npm`)는 제로 오버헤드로 C++ 엔진에 진입하는 경량 입구(Thin Gateway / FFI Boundary) 역할만 수행한다"**는 글로벌 표준 오픈소스 AI 엔진 설계 원칙을 철저히 준수합니다.
|
|
16
|
+
|
|
17
|
+
```mermaid
|
|
18
|
+
graph TD
|
|
19
|
+
subgraph Gateways ["Multi-Language Thin Gateways (Lightweight Entry Points)"]
|
|
20
|
+
G1["Python Gateway<br/><code>pip install termux-bitnet</code><br/>(ctypes Zero-Copy FFI)"]
|
|
21
|
+
G2["Node.js / TS Gateway<br/><code>npm install termux-bitnet</code><br/>(Native CLI / IPC)"]
|
|
22
|
+
G3["Native CLI<br/><code>termux-bitnet-cli</code>"]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
subgraph Boundary ["Strict C ABI Boundary (include/termux_bitnet.h)"]
|
|
26
|
+
ABI["bitnet_init() | bitnet_eval() | bitnet_generate_stream() | bitnet_free()"]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
subgraph Core ["Single High-Performance C++ Core (libtermux_bitnet.so)"]
|
|
30
|
+
K1["ARM64 NEON + DotProd Accel (vdotq_s32)"]
|
|
31
|
+
K2["ARM64 NEON + FMA Fallback (vmlal_s8)"]
|
|
32
|
+
K3["QK=128 32-Stride Interleaved Scalar Fallback"]
|
|
33
|
+
KV["KV Cache & Top-P / Temperature Sampler"]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
G1 --> ABI
|
|
37
|
+
G2 --> ABI
|
|
38
|
+
G3 --> ABI
|
|
39
|
+
ABI --> Core
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## 2. 검증된 비트넷 모델 레지스트리 (Verified Model Registry)
|
|
47
|
+
|
|
48
|
+
`termux-bitnet`은 Hugging Face 상의 공식/커뮤니티 1.58-bit GGUF 모델을 지원하며, 내장 다운로더(`download`)를 통해 원터치로 캐싱 및 구동할 수 있습니다:
|
|
49
|
+
|
|
50
|
+
| Alias | 원본 저장소 및 모델 파일 | 파라미터 / 용량 | 특징 |
|
|
51
|
+
|---|---|---|---|
|
|
52
|
+
| `bitnet-2b` | `microsoft/bitnet-b1.58-2B-4T-gguf` | 2.4B / **1.13 GB** | Microsoft 공식 1.58-bit 플래그십 (모바일 권장) |
|
|
53
|
+
| `bitnet-large` | `RichardErkhov/1bitLLM_-_bitnet_b1_58-large-gguf` | 0.7B / **404 MB** | 저사양 모바일/Termux 기기용 초경량 엔진 |
|
|
54
|
+
| `bitnet-3b` | `Green-Sky/bitnet_b1_58-3B-GGUF` | 3.3B / **730 MB** | 대용량 고정밀 온디바이스 모델 |
|
|
55
|
+
| `bitnet-3b-q4` | `RichardErkhov/1bitLLM_-_bitnet_b1_58-3B-gguf` | 3.3B / **1.83 GB** | Q4 양자화 고성능 3B 모델 |
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# 모델 원터치 다운로드 (이어받기 지원)
|
|
59
|
+
termux-bitnet download bitnet-2b
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 3. 빠른 시작 (Quick Start)
|
|
65
|
+
|
|
66
|
+
### 3.1 Python Gateway (`pip`)
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# 설치
|
|
70
|
+
pip install termux-bitnet
|
|
71
|
+
|
|
72
|
+
# 실행 (풀 파라미터 제어)
|
|
73
|
+
termux-bitnet run -m ~/.cache/termux-bitnet/models/bitnet-2b-ggml-model-i2_s.gguf \
|
|
74
|
+
-p "The capital of France is" \
|
|
75
|
+
-t 8 -c 2048 -n 128 --temp 0.7 --top-p 0.95 --top-k 40 --repeat-penalty 1.15
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from termux_bitnet import BitNetEngine, BitNetConfig
|
|
80
|
+
|
|
81
|
+
config = BitNetConfig(
|
|
82
|
+
model_path="~/.cache/termux-bitnet/models/bitnet-2b-ggml-model-i2_s.gguf",
|
|
83
|
+
n_threads=8,
|
|
84
|
+
temperature=0.7,
|
|
85
|
+
top_p=0.95,
|
|
86
|
+
top_k=40,
|
|
87
|
+
min_p=0.05,
|
|
88
|
+
repeat_penalty=1.15,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
with BitNetEngine(config) as engine:
|
|
92
|
+
for token in engine.generate_stream("Write a Python palindrome check:"):
|
|
93
|
+
print(token, end="", flush=True)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### 3.2 Node.js Gateway (`npm`)
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# 설치
|
|
102
|
+
npm install termux-bitnet
|
|
103
|
+
|
|
104
|
+
# CLI 실행
|
|
105
|
+
npx termux-bitnet run -p "Explain harmonic mean in one sentence" -t 8 --temp 0.7 --top-p 0.95
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
const { createEngine } = require('termux-bitnet');
|
|
110
|
+
|
|
111
|
+
async function main() {
|
|
112
|
+
const engine = createEngine({
|
|
113
|
+
threads: 8,
|
|
114
|
+
temperature: 0.7,
|
|
115
|
+
topP: 0.95,
|
|
116
|
+
topK: 40,
|
|
117
|
+
repeatPenalty: 1.15,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
await engine.generateStream('Question: Explain harmonic mean:', 128, (token) => {
|
|
121
|
+
process.stdout.write(token);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
main();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 4. 파라미터 매트릭스 (Full Parameter Matrix)
|
|
131
|
+
|
|
132
|
+
| CLI Flag | Python (`BitNetConfig`) | Node.js (`BitNetOptions`) | C ABI (`bitnet_params_t`) | 기본값 | 설명 |
|
|
133
|
+
|---|---|---|---|---|---|
|
|
134
|
+
| `-m, --model` | `model_path` | `modelPath` | `model_path` | `""` | GGUF 모델 파일 경로 |
|
|
135
|
+
| `-p, --prompt` | `prompt` | `prompt` | `prompt` | `""` | 입력 프롬프트 텍스트 |
|
|
136
|
+
| `-t, --threads` | `n_threads` | `threads` | `n_threads` | `cores` | CPU 워커 스레드 수 |
|
|
137
|
+
| `-c, --ctx-size` | `n_ctx` | `contextSize` | `n_ctx` | `2048` | KV Cache 컨텍스트 윈도우 크기 |
|
|
138
|
+
| `-b, --batch-size`| `n_batch` | `batchSize` | `n_batch` | `512` | 프롬프트 평가 배치 크기 |
|
|
139
|
+
| `-n, --n-predict` | `n_predict` | `maxTokens` | `n_predict` | `128` | 최대 생성 토큰 수 |
|
|
140
|
+
| `--temp` | `temperature` | `temperature` | `temperature` | `0.7` | Softmax 온도 (0.0=Greedy) |
|
|
141
|
+
| `--top-p` | `top_p` | `topP` | `top_p` | `0.95` | Nucleus Top-P 샘플링 |
|
|
142
|
+
| `--top-k` | `top_k` | `topK` | `top_k` | `40` | Top-K 샘플링 컷오프 |
|
|
143
|
+
| `--min-p` | `min_p` | `minP` | `min_p` | `0.05` | Min-P 상대 확률 컷오프 |
|
|
144
|
+
| `--repeat-penalty`| `repeat_penalty` | `repeatPenalty` | `repeat_penalty` | `1.15` | 반복 토큰 억제 계수 |
|
|
145
|
+
| `-s, --seed` | `seed` | `seed` | `seed` | `0` | 난수 시드 (0=무작위) |
|
|
146
|
+
| `--system-prompt` | `system_prompt` | `systemPrompt` | `system_prompt` | `""` | 시스템 프롬프트 접두사 |
|
|
147
|
+
| `-r, --stop` | `stop_tokens` | `stopTokens` | `stop_tokens` | `""` | 생성 중단 토큰 목록 |
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## 5. C ABI 직접 임베딩 (C/C++)
|
|
152
|
+
|
|
153
|
+
```c
|
|
154
|
+
#include "termux_bitnet.h"
|
|
155
|
+
#include <stdio.h>
|
|
156
|
+
|
|
157
|
+
int main() {
|
|
158
|
+
bitnet_params_t params = bitnet_default_params();
|
|
159
|
+
params.temperature = 0.7f;
|
|
160
|
+
params.top_p = 0.95f;
|
|
161
|
+
params.top_k = 40;
|
|
162
|
+
bitnet_context_t ctx = bitnet_init(¶ms);
|
|
163
|
+
|
|
164
|
+
bitnet_generate_stream(ctx, "The capital of France is", 64,
|
|
165
|
+
[](const char* token, int32_t id, void* u) {
|
|
166
|
+
printf("%s", token);
|
|
167
|
+
return true;
|
|
168
|
+
}, NULL);
|
|
169
|
+
|
|
170
|
+
bitnet_free(ctx);
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## 6. 라이선스 (License)
|
|
178
|
+
|
|
179
|
+
Apache License 2.0. Copyright (c) 2026 uno-km.
|
|
180
|
+
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file termux_bitnet.h
|
|
3
|
+
* @brief C ABI Interface for BitNet 1.58-bit (i2_s) On-Device Inference Engine.
|
|
4
|
+
* @author uno-km (https://github.com/uno-km)
|
|
5
|
+
* @license Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#ifndef TERMUX_BITNET_H
|
|
9
|
+
#define TERMUX_BITNET_H
|
|
10
|
+
|
|
11
|
+
#include <stdint.h>
|
|
12
|
+
#include <stddef.h>
|
|
13
|
+
#include <stdbool.h>
|
|
14
|
+
|
|
15
|
+
#ifdef __cplusplus
|
|
16
|
+
extern "C" {
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#if defined(_WIN32)
|
|
20
|
+
#if defined(TERMUX_BITNET_EXPORTS)
|
|
21
|
+
#define BITNET_API __declspec(dllexport)
|
|
22
|
+
#else
|
|
23
|
+
#define BITNET_API __declspec(dllimport)
|
|
24
|
+
#endif
|
|
25
|
+
#else
|
|
26
|
+
#define BITNET_API __attribute__((visibility("default")))
|
|
27
|
+
#endif
|
|
28
|
+
|
|
29
|
+
/** Opaque handle to the BitNet execution context. */
|
|
30
|
+
typedef struct bitnet_context* bitnet_context_t;
|
|
31
|
+
|
|
32
|
+
/** Inference configuration and sampling hyperparameters. */
|
|
33
|
+
typedef struct {
|
|
34
|
+
const char* model_path; /**< Absolute or relative path to .gguf model */
|
|
35
|
+
const char* system_prompt; /**< System prompt prefix */
|
|
36
|
+
const char* stop_tokens; /**< Comma-separated stop sequences (e.g. "<|end|>,</s>") */
|
|
37
|
+
int32_t n_threads; /**< CPU worker threads (default: hardware core count) */
|
|
38
|
+
int32_t n_ctx; /**< KV Cache context window length (default: 2048) */
|
|
39
|
+
int32_t n_batch; /**< Prompt evaluation logical batch size (default: 512) */
|
|
40
|
+
int32_t n_ubatch; /**< Physical micro-batch size (default: 512) */
|
|
41
|
+
int32_t n_predict; /**< Maximum tokens to generate (default: 128) */
|
|
42
|
+
int32_t top_k; /**< Top-K sampling cutoff (0 = disabled, default: 40) */
|
|
43
|
+
int32_t repeat_last_n; /**< Number of previous tokens to consider for penalty (default: 64) */
|
|
44
|
+
int32_t n_gpu_layers; /**< Number of layers to offload to GPU/NPU (default: 0) */
|
|
45
|
+
uint32_t seed; /**< RNG seed for deterministic sampling (0 = random) */
|
|
46
|
+
float temperature; /**< Softmax temperature (0.0 = deterministic greedy) */
|
|
47
|
+
float top_p; /**< Nucleus sampling threshold (default: 0.95) */
|
|
48
|
+
float min_p; /**< Min-P sampling cutoff relative to max prob (default: 0.05) */
|
|
49
|
+
float typical_p; /**< Locally typical sampling threshold (default: 1.0) */
|
|
50
|
+
float repeat_penalty; /**< Repetition penalty coefficient (default: 1.15) */
|
|
51
|
+
float frequency_penalty; /**< Frequency penalty coefficient (default: 0.0) */
|
|
52
|
+
float presence_penalty; /**< Presence penalty coefficient (default: 0.0) */
|
|
53
|
+
bool flash_attn; /**< Enable flash attention acceleration (default: false) */
|
|
54
|
+
bool verbose; /**< Enable verbose diagnostic logs */
|
|
55
|
+
} bitnet_params_t;
|
|
56
|
+
|
|
57
|
+
/** Token generation callback for streaming responses. Return false to abort early. */
|
|
58
|
+
typedef bool (*bitnet_stream_cb)(const char* token_str, int32_t token_id, void* user_data);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @brief Initialize default hyperparameters.
|
|
62
|
+
*/
|
|
63
|
+
BITNET_API bitnet_params_t bitnet_default_params(void);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @brief Load a 1.58-bit GGUF model and initialize the execution engine.
|
|
67
|
+
* @param params Pointer to engine parameters.
|
|
68
|
+
* @return Context handle on success, NULL on failure.
|
|
69
|
+
*/
|
|
70
|
+
BITNET_API bitnet_context_t bitnet_init(const bitnet_params_t* params);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @brief Release all memory, KV cache, and model weights associated with the context.
|
|
74
|
+
* @param ctx Context handle.
|
|
75
|
+
*/
|
|
76
|
+
BITNET_API void bitnet_free(bitnet_context_t ctx);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @brief Tokenize an input string into token IDs.
|
|
80
|
+
* @param ctx Context handle.
|
|
81
|
+
* @param text UTF-8 input string.
|
|
82
|
+
* @param tokens Output buffer for token IDs.
|
|
83
|
+
* @param max_tokens Maximum size of the output buffer.
|
|
84
|
+
* @return Number of tokens generated, or negative error code.
|
|
85
|
+
*/
|
|
86
|
+
BITNET_API int32_t bitnet_tokenize(bitnet_context_t ctx, const char* text, int32_t* tokens, int32_t max_tokens);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @brief Convert a token ID to its UTF-8 string representation.
|
|
90
|
+
* @param ctx Context handle.
|
|
91
|
+
* @param token Token ID.
|
|
92
|
+
* @param buf Output string buffer.
|
|
93
|
+
* @param buf_len Buffer capacity.
|
|
94
|
+
* @return Number of characters written.
|
|
95
|
+
*/
|
|
96
|
+
BITNET_API int32_t bitnet_token_to_str(bitnet_context_t ctx, int32_t token, char* buf, int32_t buf_len);
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @brief Perform forward evaluation of a token sequence into the KV cache.
|
|
100
|
+
* @param ctx Context handle.
|
|
101
|
+
* @param tokens Array of token IDs.
|
|
102
|
+
* @param n_tokens Number of tokens to evaluate.
|
|
103
|
+
* @return 0 on success, non-zero on error.
|
|
104
|
+
*/
|
|
105
|
+
BITNET_API int32_t bitnet_eval(bitnet_context_t ctx, const int32_t* tokens, int32_t n_tokens);
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @brief Sample the next token from current logits using configured temperature & top_p.
|
|
109
|
+
* @param ctx Context handle.
|
|
110
|
+
* @return Sampled token ID.
|
|
111
|
+
*/
|
|
112
|
+
BITNET_API int32_t bitnet_sample(bitnet_context_t ctx);
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @brief High-level streaming text generation interface.
|
|
116
|
+
* @param ctx Context handle.
|
|
117
|
+
* @param prompt Input prompt text.
|
|
118
|
+
* @param max_new_tokens Maximum number of tokens to generate.
|
|
119
|
+
* @param callback Callback invoked on each generated token piece.
|
|
120
|
+
* @param user_data User-defined pointer passed to callback.
|
|
121
|
+
* @return Total number of tokens generated.
|
|
122
|
+
*/
|
|
123
|
+
BITNET_API int32_t bitnet_generate_stream(bitnet_context_t ctx, const char* prompt, int32_t max_new_tokens, bitnet_stream_cb callback, void* user_data);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @brief Inspect runtime hardware features and acceleration modes.
|
|
127
|
+
* @param buf Output buffer.
|
|
128
|
+
* @param buf_len Buffer capacity.
|
|
129
|
+
*/
|
|
130
|
+
BITNET_API void bitnet_get_hardware_info(char* buf, int32_t buf_len);
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @brief Retrieve performance metrics of the last generation.
|
|
134
|
+
* @param ctx Context handle.
|
|
135
|
+
* @param prompt_eval_ms Output pointer for TTFT / prompt eval time in ms.
|
|
136
|
+
* @param eval_ms Output pointer for token generation eval time in ms.
|
|
137
|
+
* @param tokens_per_sec Output pointer for generation tokens/sec.
|
|
138
|
+
*/
|
|
139
|
+
BITNET_API void bitnet_get_perf_stats(bitnet_context_t ctx, double* prompt_eval_ms, double* eval_ms, double* tokens_per_sec);
|
|
140
|
+
|
|
141
|
+
#ifdef __cplusplus
|
|
142
|
+
}
|
|
143
|
+
#endif
|
|
144
|
+
|
|
145
|
+
#endif /* TERMUX_BITNET_H */
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0.0", "wheel", "cmake>=3.20.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "termux-bitnet"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "High-performance 1.58-bit BitNet inference engine, Python SDK & CLI optimized for Android Termux and ARM64 architecture."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "uno-km", email = "unokim.dev@gmail.com" }
|
|
12
|
+
]
|
|
13
|
+
license = { text = "Apache-2.0" }
|
|
14
|
+
keywords = ["bitnet", "1-bit-llm", "termux", "arm64", "neon", "dotprod", "edge-ai", "on-device-ai"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
19
|
+
"License :: OSI Approved :: Apache Software License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: C++",
|
|
26
|
+
"Operating System :: POSIX :: Linux",
|
|
27
|
+
"Operating System :: Android",
|
|
28
|
+
]
|
|
29
|
+
requires-python = ">=3.8"
|
|
30
|
+
dependencies = [
|
|
31
|
+
"tqdm>=4.64.0",
|
|
32
|
+
"requests>=2.28.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
server = [
|
|
37
|
+
"uvicorn>=0.20.0",
|
|
38
|
+
"fastapi>=0.95.0",
|
|
39
|
+
]
|
|
40
|
+
dev = [
|
|
41
|
+
"pytest>=7.0.0",
|
|
42
|
+
"pytest-benchmark>=4.0.0",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.scripts]
|
|
46
|
+
termux-bitnet = "termux_bitnet.cli:main"
|
|
47
|
+
|
|
48
|
+
[project.urls]
|
|
49
|
+
Homepage = "https://github.com/uno-km/termux-bitnet"
|
|
50
|
+
Documentation = "https://uno-km.github.io/termux-bitnet/"
|
|
51
|
+
Repository = "https://github.com/uno-km/termux-bitnet.git"
|
|
52
|
+
Issues = "https://github.com/uno-km/termux-bitnet/issues"
|