Update README.md
Browse files
README.md
CHANGED
|
@@ -7,515 +7,114 @@ tags:
|
|
| 7 |
- translation
|
| 8 |
- machine-translation
|
| 9 |
- english-to-vietnamese
|
| 10 |
-
- vietnamese
|
| 11 |
- onnx
|
| 12 |
- seq2seq
|
| 13 |
- bart
|
| 14 |
pipeline_tag: translation
|
|
|
|
|
|
|
|
|
|
| 15 |
---
|
| 16 |
|
| 17 |
# AnViMt
|
| 18 |
|
| 19 |
-
**AnViMt**
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
> **Vi** = Vietnamese
|
| 25 |
-
> **Mt** = Machine Translation
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# Model files
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
```
|
| 49 |
AnViMt/
|
| 50 |
-
├── encoder_model.onnx
|
| 51 |
-
├── decoder_model.onnx
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
Encoder nhận câu tiếng Anh đã được tokenize.
|
| 57 |
-
|
| 58 |
-
English text
|
| 59 |
-
↓
|
| 60 |
-
Tokenizer
|
| 61 |
-
↓
|
| 62 |
-
Token IDs
|
| 63 |
-
↓
|
| 64 |
-
encoder_model.onnx
|
| 65 |
-
↓
|
| 66 |
-
Encoder hidden states
|
| 67 |
-
|
| 68 |
-
Encoder xử lý toàn bộ câu nguồn và tạo hidden states dùng cho decoder.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
---
|
| 72 |
-
|
| 73 |
-
decoder_model.onnx
|
| 74 |
-
|
| 75 |
-
Decoder sinh câu tiếng Việt theo từng token.
|
| 76 |
-
|
| 77 |
-
Encoder hidden states
|
| 78 |
-
+
|
| 79 |
-
Previous generated tokens
|
| 80 |
-
↓
|
| 81 |
-
decoder_model.onnx
|
| 82 |
-
↓
|
| 83 |
-
Next token logits
|
| 84 |
-
↓
|
| 85 |
-
Select next token
|
| 86 |
-
↓
|
| 87 |
-
Repeat until EOS
|
| 88 |
-
|
| 89 |
-
Quá trình dịch là autoregressive generation.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
---
|
| 93 |
-
|
| 94 |
-
spm.model
|
| 95 |
-
|
| 96 |
-
Tokenizer SentencePiece.
|
| 97 |
-
|
| 98 |
-
File này chịu trách nhiệm:
|
| 99 |
-
|
| 100 |
-
Text
|
| 101 |
-
↓
|
| 102 |
-
SentencePiece
|
| 103 |
-
↓
|
| 104 |
-
Token IDs
|
| 105 |
-
↓
|
| 106 |
-
ONNX model
|
| 107 |
-
↓
|
| 108 |
-
Generated token IDs
|
| 109 |
-
↓
|
| 110 |
-
SentencePiece decode
|
| 111 |
-
↓
|
| 112 |
-
Vietnamese translation
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
---
|
| 116 |
-
|
| 117 |
-
Architecture
|
| 118 |
-
|
| 119 |
-
AnViMt sử dụng kiến trúc Transformer Seq2Seq encoder-decoder.
|
| 120 |
-
|
| 121 |
-
SOURCE
|
| 122 |
-
|
| 123 |
-
English sentence
|
| 124 |
-
│
|
| 125 |
-
▼
|
| 126 |
-
SentencePiece
|
| 127 |
-
│
|
| 128 |
-
▼
|
| 129 |
-
Token IDs
|
| 130 |
-
│
|
| 131 |
-
▼
|
| 132 |
-
┌─────────────────────────┐
|
| 133 |
-
│ ENCODER │
|
| 134 |
-
│ encoder_model.onnx │
|
| 135 |
-
└─────────────────────────┘
|
| 136 |
-
│
|
| 137 |
-
▼
|
| 138 |
-
Hidden states
|
| 139 |
-
│
|
| 140 |
-
▼
|
| 141 |
-
┌─────────────────────────┐
|
| 142 |
-
│ DECODER │
|
| 143 |
-
│ decoder_model.onnx │
|
| 144 |
-
└─────────────────────────┘
|
| 145 |
-
│
|
| 146 |
-
▼
|
| 147 |
-
Output logits
|
| 148 |
-
│
|
| 149 |
-
▼
|
| 150 |
-
Next token
|
| 151 |
-
│
|
| 152 |
-
└───────┐
|
| 153 |
-
│
|
| 154 |
-
Repeat
|
| 155 |
-
│
|
| 156 |
-
▼
|
| 157 |
-
Vietnamese text
|
| 158 |
-
|
| 159 |
-
Model thuộc nhóm encoder-decoder neural machine translation.
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
---
|
| 163 |
-
|
| 164 |
-
Parameters
|
| 165 |
-
|
| 166 |
-
AnViMt được export từ checkpoint fine-tuned sang ONNX.
|
| 167 |
-
|
| 168 |
-
Các tham số của model được lưu trực tiếp bên trong:
|
| 169 |
-
|
| 170 |
-
encoder_model.onnx
|
| 171 |
-
decoder_model.onnx
|
| 172 |
-
|
| 173 |
-
Runtime package ONNX không yêu cầu:
|
| 174 |
-
|
| 175 |
-
config.json
|
| 176 |
-
|
| 177 |
-
Hugging Face Transformers
|
| 178 |
-
|
| 179 |
-
PyTorch
|
| 180 |
-
|
| 181 |
-
Safetensors
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
Inference chỉ cần:
|
| 185 |
-
|
| 186 |
-
onnxruntime
|
| 187 |
-
numpy
|
| 188 |
-
sentencepiece
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
---
|
| 192 |
|
| 193 |
-
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
| 198 |
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
pip install onnxruntime numpy sentencepiece
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
---
|
| 207 |
-
|
| 208 |
-
Quick Start
|
| 209 |
-
|
| 210 |
-
Đặt 3 file:
|
| 211 |
|
|
|
|
| 212 |
project/
|
| 213 |
├── main.py
|
| 214 |
├── encoder_model.onnx
|
| 215 |
├── decoder_model.onnx
|
| 216 |
-
└── spm.model
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
)
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
# Tokenize input
|
| 257 |
-
input_ids = sp.encode(
|
| 258 |
-
text,
|
| 259 |
-
out_type=int
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
-
input_ids = np.array(
|
| 263 |
-
[input_ids],
|
| 264 |
-
dtype=np.int64
|
| 265 |
-
)
|
| 266 |
-
|
| 267 |
-
attention_mask = np.ones_like(
|
| 268 |
-
input_ids,
|
| 269 |
-
dtype=np.int64
|
| 270 |
-
)
|
| 271 |
-
|
| 272 |
-
# Encoder
|
| 273 |
-
encoder_outputs = encoder.run(
|
| 274 |
-
None,
|
| 275 |
-
{
|
| 276 |
-
encoder.get_inputs()[0].name: input_ids,
|
| 277 |
-
encoder.get_inputs()[1].name: attention_mask,
|
| 278 |
-
}
|
| 279 |
-
)
|
| 280 |
-
|
| 281 |
-
encoder_hidden_states = encoder_outputs[0]
|
| 282 |
-
|
| 283 |
-
# Decoder start token
|
| 284 |
-
decoder_ids = np.array(
|
| 285 |
-
[[sp.bos_id()]],
|
| 286 |
-
dtype=np.int64
|
| 287 |
-
)
|
| 288 |
-
|
| 289 |
-
# Generate
|
| 290 |
-
for _ in range(max_length):
|
| 291 |
-
|
| 292 |
-
decoder_inputs = {}
|
| 293 |
-
|
| 294 |
-
for inp in decoder.get_inputs():
|
| 295 |
-
|
| 296 |
-
name = inp.name
|
| 297 |
-
|
| 298 |
-
if "input_ids" in name:
|
| 299 |
-
decoder_inputs[name] = decoder_ids
|
| 300 |
-
|
| 301 |
-
elif "encoder_hidden_states" in name:
|
| 302 |
-
decoder_inputs[name] = encoder_hidden_states
|
| 303 |
-
|
| 304 |
-
elif "attention_mask" in name:
|
| 305 |
-
decoder_inputs[name] = attention_mask
|
| 306 |
-
|
| 307 |
-
outputs = decoder.run(
|
| 308 |
-
None,
|
| 309 |
-
decoder_inputs
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
-
logits = outputs[0]
|
| 313 |
-
|
| 314 |
-
next_token = np.argmax(
|
| 315 |
-
logits[:, -1, :],
|
| 316 |
-
axis=-1
|
| 317 |
-
)
|
| 318 |
-
|
| 319 |
-
decoder_ids = np.concatenate(
|
| 320 |
-
[
|
| 321 |
-
decoder_ids,
|
| 322 |
-
next_token[:, None]
|
| 323 |
-
],
|
| 324 |
-
axis=1
|
| 325 |
-
)
|
| 326 |
-
|
| 327 |
-
if int(next_token[0]) == sp.eos_id():
|
| 328 |
-
break
|
| 329 |
-
|
| 330 |
-
tokens = decoder_ids[0].tolist()
|
| 331 |
-
|
| 332 |
-
# Remove special tokens
|
| 333 |
-
tokens = [
|
| 334 |
-
x for x in tokens
|
| 335 |
-
if x not in {
|
| 336 |
-
sp.bos_id(),
|
| 337 |
-
sp.eos_id(),
|
| 338 |
-
sp.pad_id()
|
| 339 |
-
}
|
| 340 |
-
]
|
| 341 |
-
|
| 342 |
-
return sp.decode(tokens)
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
if __name__ == "__main__":
|
| 346 |
-
|
| 347 |
-
while True:
|
| 348 |
-
|
| 349 |
-
text = input("\nEN > ").strip()
|
| 350 |
-
|
| 351 |
-
if not text:
|
| 352 |
-
continue
|
| 353 |
-
|
| 354 |
-
if text.lower() in {
|
| 355 |
-
"exit",
|
| 356 |
-
"quit",
|
| 357 |
-
"q"
|
| 358 |
-
}:
|
| 359 |
-
break
|
| 360 |
-
|
| 361 |
-
result = translate(text)
|
| 362 |
-
|
| 363 |
-
print("VI >", result)
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
---
|
| 367 |
-
|
| 368 |
-
Example
|
| 369 |
-
|
| 370 |
-
Input:
|
| 371 |
-
|
| 372 |
-
The system automatically restarts the failed service after detecting an unexpected process crash.
|
| 373 |
-
|
| 374 |
-
Output:
|
| 375 |
-
|
| 376 |
-
Hệ thống tự động khởi động lại dịch vụ gặp lỗi sau khi phát hiện tiến trình bị sập bất ngờ.
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
---
|
| 380 |
-
|
| 381 |
-
Another example
|
| 382 |
-
|
| 383 |
-
Input:
|
| 384 |
-
|
| 385 |
-
The application uses asynchronous I/O so network requests do not block the main execution thread.
|
| 386 |
-
|
| 387 |
-
Expected translation style:
|
| 388 |
-
|
| 389 |
-
Ứng dụng sử dụng I/O bất đồng bộ để các yêu cầu mạng không chặn luồng thực thi chính.
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
---
|
| 393 |
-
|
| 394 |
-
Technical examples
|
| 395 |
-
|
| 396 |
-
Input:
|
| 397 |
-
|
| 398 |
-
The kernel schedules CPU time according to process priority and current system load.
|
| 399 |
-
|
| 400 |
-
The database transaction was rolled back because a constraint violation occurred during the update.
|
| 401 |
-
|
| 402 |
-
Gradient accumulation allows the model to simulate a larger effective batch size when GPU memory is limited.
|
| 403 |
-
|
| 404 |
-
The inference engine reuses cached key-value states to avoid recomputing previous attention activations.
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
---
|
| 408 |
-
|
| 409 |
-
Conversation examples
|
| 410 |
-
|
| 411 |
-
Input:
|
| 412 |
-
|
| 413 |
-
Hey bro, can you help me fix this problem? I have been stuck on it for hours.
|
| 414 |
-
|
| 415 |
-
Input:
|
| 416 |
-
|
| 417 |
-
Don't worry, I'll take a look at the logs and see what caused the crash.
|
| 418 |
-
|
| 419 |
-
Input:
|
| 420 |
-
|
| 421 |
-
Could you please restart the service and check whether the error still occurs?
|
| 422 |
-
|
| 423 |
-
AnViMt được train thêm dữ liệu hội thoại để hỗ trợ cách diễn đạt tự nhiên và nhiều phong cách xưng hô tiếng Việt.
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
---
|
| 427 |
-
|
| 428 |
-
Runtime dependencies
|
| 429 |
-
|
| 430 |
-
AnViMt ONNX runtime chỉ sử dụng 3 thư viện Python:
|
| 431 |
-
|
| 432 |
-
onnxruntime
|
| 433 |
-
numpy
|
| 434 |
-
sentencepiece
|
| 435 |
-
|
| 436 |
-
Library Purpose
|
| 437 |
-
|
| 438 |
-
onnxruntime Chạy encoder và decoder ONNX
|
| 439 |
-
numpy Xử lý tensor / token IDs
|
| 440 |
-
sentencepiece Tokenization và decoding
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
Không cần:
|
| 444 |
-
|
| 445 |
-
torch
|
| 446 |
-
transformers
|
| 447 |
-
optimum
|
| 448 |
-
tensorflow
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
---
|
| 452 |
-
|
| 453 |
-
Supported environment
|
| 454 |
-
|
| 455 |
-
AnViMt có thể chạy trên:
|
| 456 |
-
|
| 457 |
-
Linux
|
| 458 |
-
|
| 459 |
-
Windows
|
| 460 |
-
|
| 461 |
-
macOS
|
| 462 |
-
|
| 463 |
-
Android Termux
|
| 464 |
-
|
| 465 |
-
CPU environments
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
Tùy phiên bản ONNX Runtime, có thể sử dụng execution provider khác nếu môi trường hỗ trợ.
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
---
|
| 472 |
-
|
| 473 |
-
Limitations
|
| 474 |
-
|
| 475 |
-
Model tập trung vào English → Vietnamese.
|
| 476 |
-
|
| 477 |
-
Chất lượng có thể giảm với câu cực dài hoặc cấu trúc hiếm.
|
| 478 |
-
|
| 479 |
-
Một số thuật ngữ chuyên ngành mới có thể được giữ nguyên tiếng Anh.
|
| 480 |
-
|
| 481 |
-
Tên riêng và thuật ngữ kỹ thuật có thể cần hậu kiểm.
|
| 482 |
-
|
| 483 |
-
Bản ONNX runtime này ưu tiên inference đơn giản và nhẹ.
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
---
|
| 488 |
-
|
| 489 |
-
License
|
| 490 |
-
|
| 491 |
-
This model is released under:
|
| 492 |
-
|
| 493 |
-
CC BY-NC 4.0
|
| 494 |
-
|
| 495 |
-
You are allowed to:
|
| 496 |
-
|
| 497 |
-
Download
|
| 498 |
-
|
| 499 |
-
Use
|
| 500 |
-
|
| 501 |
-
Share
|
| 502 |
-
|
| 503 |
-
Modify
|
| 504 |
-
|
| 505 |
-
Fine-tune
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
You are not allowed to use this model for commercial purposes without permission.
|
| 509 |
-
|
| 510 |
-
Commercial use includes using the model to generate revenue, provide paid services, or integrate it into commercial products.
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
---
|
| 514 |
-
|
| 515 |
-
Author
|
| 516 |
-
|
| 517 |
-
Phitran21
|
| 518 |
-
|
| 519 |
-
Model: AnViMt
|
| 520 |
-
|
| 521 |
-
English → Vietnamese Machine Translation
|
|
|
|
| 7 |
- translation
|
| 8 |
- machine-translation
|
| 9 |
- english-to-vietnamese
|
|
|
|
| 10 |
- onnx
|
| 11 |
- seq2seq
|
| 12 |
- bart
|
| 13 |
pipeline_tag: translation
|
| 14 |
+
widget:
|
| 15 |
+
- text: "Machine translation is useful for multilingual applications."
|
| 16 |
+
example_title: "EN → VI"
|
| 17 |
---
|
| 18 |
|
| 19 |
# AnViMt
|
| 20 |
|
| 21 |
+
**AnViMt** — **An**=English **Vi**=Vietnamese **Mt**=Machine Translation — dịch **EN → VI** cho văn bản thường + kỹ thuật/hệ thống/lập trình/hội thoại.
|
| 22 |
|
| 23 |
+
Export sang **ONNX** để chạy chỉ với `onnxruntime + sentencepiece + numpy` (không cần `torch/transformers`).
|
| 24 |
|
| 25 |
+
## Architecture
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
**Base:** `transformers.BartForConditionalGeneration` train từ đầu, không phải pretrained BART.
|
| 28 |
|
| 29 |
+
- `vocab_size=32000` (SentencePiece BPE, `byte_fallback=True`)
|
| 30 |
+
- `d_model=512` | `encoder_layers=6` | `decoder_layers=6`
|
| 31 |
+
- `encoder_ffn_dim=2048` | `decoder_ffn_dim=2048`
|
| 32 |
+
- `encoder_attention_heads=8` | `decoder_attention_heads=8`
|
| 33 |
+
- `max_position_embeddings=256` | `dropout 0.1`
|
| 34 |
+
- `tie_word_embeddings=True` | `is_encoder_decoder=True`
|
| 35 |
+
- `pad_token_id=0` (`<pad>`) | `unk_token_id=1` (`<unk>`) | `bos_token_id=2` (`<s>`) | `eos_token_id=3` (`</s>`) | `decoder_start_token_id=2`
|
| 36 |
+
- **Params:** ~60.79M
|
| 37 |
|
| 38 |
+
```
|
| 39 |
+
EN text
|
| 40 |
+
↓ spm.model [2] + BPE ids + [3] (pad 0, bos 2, eos 3, max 256)
|
| 41 |
+
↓
|
| 42 |
+
encoder_model.onnx (input_ids, attention_mask → encoder_hidden_states [batch, seq, 512])
|
| 43 |
+
↓
|
| 44 |
+
decoder_model.onnx / decoder_with_past_model.onnx
|
| 45 |
+
(decoder_input_ids [2] + encoder_hidden_states + encoder_attention_mask + past_key_values → logits)
|
| 46 |
+
↓ autoregressive greedy (argmax) đến eos 3
|
| 47 |
+
↓ sp.decode → VI text
|
| 48 |
+
```
|
| 49 |
|
| 50 |
+
## Files
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
Export bằng `optimum` `task=seq2seq-lm` `opset=14`:
|
| 53 |
|
| 54 |
+
```
|
| 55 |
AnViMt/
|
| 56 |
+
├── encoder_model.onnx # 136M
|
| 57 |
+
├── decoder_model.onnx # 223M
|
| 58 |
+
├── decoder_with_past_model.onnx # KV cache (tùy chọn, tăng tốc)
|
| 59 |
+
├── spm.model # 752K, vocab 32000
|
| 60 |
+
└── spm.vocab
|
| 61 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
Runtime tối thiểu chỉ cần `encoder_model.onnx` + `decoder_model.onnx` + `spm.model`. `decoder_with_past_model.onnx` dùng khi muốn KV-cache.
|
| 64 |
|
| 65 |
+
Không cần: `config.json`, `pytorch_model.bin`, `safetensors`, `torch`, `transformers`.
|
| 66 |
|
| 67 |
+
## Installation
|
| 68 |
|
| 69 |
+
```bash
|
| 70 |
+
pip install onnxruntime sentencepiece numpy
|
| 71 |
+
# Termux: apt install python3-onnxruntime python3-sentencepiece python3-numpy
|
| 72 |
+
```
|
| 73 |
|
| 74 |
+
## Quick Start
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
```
|
| 77 |
project/
|
| 78 |
├── main.py
|
| 79 |
├── encoder_model.onnx
|
| 80 |
├── decoder_model.onnx
|
| 81 |
+
└── spm.model # (decoder_with_past_model.onnx nếu có)
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
`main.py` thực tế (đã test, xử lý đúng BOS/EOS/pad):
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
import numpy as np, onnxruntime as ort, sentencepiece as spm
|
| 88 |
+
sp = spm.SentencePieceProcessor(); sp.load("spm.model")
|
| 89 |
+
enc = ort.InferenceSession("encoder_model.onnx", providers=["CPUExecutionProvider"])
|
| 90 |
+
dec = ort.InferenceSession("decoder_model.onnx", providers=["CPUExecutionProvider"])
|
| 91 |
+
BOS, EOS, PAD = 2, 3, 0
|
| 92 |
+
def translate(text, max_len=256):
|
| 93 |
+
ids = [BOS] + sp.encode(text, out_type=int)[:254] + [EOS]
|
| 94 |
+
input_ids = np.array([ids], dtype=np.int64)
|
| 95 |
+
attn = np.ones_like(input_ids)
|
| 96 |
+
enc_hs = enc.run(None, {"input_ids": input_ids, "attention_mask": attn})[0]
|
| 97 |
+
gen = [BOS]
|
| 98 |
+
for _ in range(max_len):
|
| 99 |
+
dec_ids = np.array([gen], dtype=np.int64)
|
| 100 |
+
logits = dec.run(None, {"input_ids": dec_ids, "encoder_hidden_states": enc_hs, "encoder_attention_mask": attn})[0]
|
| 101 |
+
nxt = int(np.argmax(logits[0, -1]))
|
| 102 |
+
gen.append(nxt)
|
| 103 |
+
if nxt == EOS: break
|
| 104 |
+
out = [x for x in gen[1:] if x not in (BOS, EOS, PAD)]
|
| 105 |
+
if EOS in out: out = out[:out.index(EOS)]
|
| 106 |
+
return sp.decode(out)
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
## Training Data
|
| 110 |
+
|
| 111 |
+
Pretrain 1.13M EN-VI (PhoMT + TED + TECH) → finetune 200k TECH → finetune 27k v3 (hội thoại + kỹ thuật). Tokenizer BPE 32k train trên 400k pairs reservoir sampled, `nmt_nfkc`, `split_digits`, `byte_fallback`.
|
| 112 |
+
|
| 113 |
+
## Limitations
|
| 114 |
+
|
| 115 |
+
EN → VI only. Câu >254 BPE tokens bị cắt. Thuật ngữ mới có thể giữ nguyên EN.
|
| 116 |
+
|
| 117 |
+
## License
|
| 118 |
+
|
| 119 |
+
CC BY-NC 4.0 — non-commercial.
|
| 120 |
+
Author: Phitran21
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|