|
Download README.md from Netra-Lab/Netra-NMT: direct link, hf CLI and curl.
- Browser
- Download file 8.07 kB
-
https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/README.md
- Command line
-
hf download hf://Netra-Lab/Netra-NMT/README.md
-
curl -L -o README.md https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/README.md
8.07 kB
| license: mit | |
| language: | |
| - en | |
| - km | |
| library_name: onnx | |
| pipeline_tag: translation | |
| datasets: | |
| - Netra-Lab/bilingual-en-km | |
| - Netra-Lab/khmer-english-pairs-raw | |
| - lyfeyvutha/nllb-en-km-316K | |
| - KrorngAI/ParaCrawl-English-Khmer-v2 | |
| - SeyhaLite/Translate-English-Khmer-All | |
| metrics: | |
| - sacrebleu | |
| - chrf | |
| - ter | |
| - comet | |
| - bertscore | |
| tags: | |
| - translation | |
| - khmer | |
| - english | |
| - machine-translation | |
| - onnx | |
| - netra-lab | |
| <p align="center"> | |
| <img src="https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/assets/header.png" width="260" alt="Netra Lab" /> | |
| </p> | |
| <hr> | |
| <p align="center"> | |
| <a href="https://github.com/netra-ai-lab/Netra-NMT"><b>GitHub</b></a> | | |
| <a href="https://github.com/netra-ai-lab/Netra-NMT/pkgs/container/netra-nmt"><b>Docker Image</b></a> | | |
| <a href="https://pypi.org/project/netra-nmt/"><b>PyPI</b></a> | |
| </p> | |
| # Netra NMT | |
| **English or Khmer text in, the other language out.** Netra NMT is a **77M-parameter** English↔Khmer translation model: one encoder-decoder transformer for both directions, trained from scratch on 220 million tokens of parallel text. It runs on ONNX Runtime with the `netra-nmt` package, on a CPU, without PyTorch. | |
| ## Results | |
| ALT test split (`mutiyama/alt`, 1,018 pairs), greedy decoding; spBLEU with the FLORES-200 tokenizer, higher is better. | |
| | Model | Params | EN → KM | KM → EN | | |
| |---|---:|---:|---:| | |
| | NLLB-350M (EN-KM) | 350M | 18.89 | 0.26 | | |
| | NLLB-200 distilled 600M | 600M | 9.24 | 31.28 | | |
| | NLLB-200 distilled 1.3B | 1.3B | 16.62 | 35.15 | | |
| | MADLAD-400 | 3B | 16.19 | 36.13 | | |
| | Google Translate | — | 28.93 | 34.86 | | |
| | **Netra NMT** | **77M** | **25.83** | **19.47** | | |
| - **Where it wins:** English→Khmer, the best spBLEU after Google Translate and the best TER of all systems (60.47), and on CPU 65.6 ms per sentence greedy against 550.8 for NLLB-350M. | |
| - **Where it loses:** Khmer→English, 15.7 spBLEU behind NLLB-200 1.3B. | |
| <p align="center"><img src="https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/assets/metrics_benchmark.png" width="100%" alt="spBLEU, chrF++, TER, COMET and BERTScore on ALT"></p> | |
| *spBLEU, chrF++, TER, COMET (wmt22-comet-da) and BERTScore F1 (xlm-roberta-base) on ALT, both directions.* | |
| <p align="center"><img src="https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/assets/speed_benchmark.png" width="100%" alt="Speed against NLLB on CPU and GPU"></p> | |
| *English→Khmer, median ms per sentence over 20 sentences, Intel i7-13700KF (16 threads) and RTX 3060, PyTorch.* | |
| ## Architecture | |
| <p align="center"><img src="https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/assets/model_architecture.png" width="100%" alt="Netra-NMT translating I love my country into Khmer: a 12-layer encoder and a 2-layer decoder, one layer of each opened up"></p> | |
| - **Shape:** 12 encoder layers, 2 decoder layers (deep encoder, shallow decoder), d_model 512, 8 heads, FFN 2048, vocabulary 32K. | |
| - **Components:** rotary position embeddings, RMSNorm (pre-norm), SwiGLU, one embedding table shared by the encoder, the decoder and the tied output head, a KV cache. | |
| - **Input:** a target-language marker (`<2km>` or `<2en>`) then the source. English sources are NFC-normalised and lowercased; Khmer is word-segmented with khmercut. The package does this for you. | |
| ### Decoding | |
| The encoder runs once per sentence; the 2-layer decoder then runs one step per token, reusing its self-attention keys and values from a KV cache. Beam search stops as soon as no live beam can still beat the best finished one. Scores only fall as a beam grows, so this gives exactly the output of full-length beam search (1,018 of 1,018 ALT pairs identical, both directions), far sooner. | |
| | English→Khmer, i7-13700KF CPU | Greedy ms/sent ↓ | Beam-5 ms/sent ↓ | | |
| |---|---:|---:| | |
| | netra-nmt 0.1.0 (PyTorch, full-length beam) | 62.6 | 701.1 | | |
| | **netra-nmt 1.0.0 (ONNX Runtime, early stop)** | **39.1** | **63.5** | | |
| *Median over 20 sentences. The package generates the same tokens as the PyTorch model on the ALT test set, greedy and beam-5, both directions.* | |
| ## Try the app | |
| The Docker image bundles the model with a web app and a REST API (`POST /api/translate`), and runs offline. A [demo Space](https://huggingface.co/spaces/Darayut/Netra-NMT) runs the same model. | |
| ```bash | |
| docker run --rm -p 8000:8000 ghcr.io/netra-ai-lab/netra-nmt:latest | |
| # then open http://localhost:8000 | |
| ``` | |
| <p align="center"><img src="https://huggingface.co/Netra-Lab/Netra-NMT/resolve/main/assets/web_app.png" width="100%" alt="The Netra NMT web app translating two English sentences about a scholarship program into Khmer with beam search"></p> | |
| *English→Khmer with beam search, in version 1.0.0 of the Docker image on the CPU.* | |
| ## Usage | |
| ```bash | |
| pip install "netra-nmt[cpu]" # or [gpu] | |
| ``` | |
| ```python | |
| from netra_nmt import translate | |
| translate("Hello, how are you?") # English -> Khmer | |
| translate("ខ្ញុំស្រលាញ់ប្រទេសកម្ពុជា។", direction="km2en") # -> "I love Cambodia." | |
| ``` | |
| ```bash | |
| netra-nmt --text "Hello, how are you?" --mode beam | |
| netra-nmt serve # web app + REST API (extra: server) | |
| ``` | |
| The package downloads `model/` from this repo once, at the commit it was tested with. Translate one sentence at a time: sources are cut at 256 tokens. | |
| ## Files | |
| | File | Contents | | |
| |---|---| | |
| | `model/netra-model.json` | settings: graphs, decoder shape, special ids, language markers, sha256 of every file | | |
| | `model/encoder.onnx` | tokens → cross-attention keys/values for every decoder layer (136 MB) | | |
| | `model/decoder_step.onnx` | one decoding step with the self-attention KV cache (52 MB) | | |
| | `model/spm_32k.model` | the SentencePiece tokenizer | | |
| | `pytorch/model.safetensors` | fp16 weights for fine-tuning (`lm_head` tied to the embeddings, not stored) | | |
| | `pytorch/config.json`, `pytorch/spm_32k.model` | architecture and tokenizer for the PyTorch model | | |
| | `assets/` | the figures on this card | | |
| To fine-tune from `pytorch/`: `bash training/finetune.sh my_pairs.tsv` in the GitHub repo. | |
| ## Training data and test set | |
| About 2.4M unique pairs after deduplication and length filtering, used in both directions (4.2M examples), from four sources: | |
| - [Netra-Lab/khmer-english-pairs-raw](https://huggingface.co/datasets/Netra-Lab/khmer-english-pairs-raw): 200K synthetic pairs (legal, literary, governmental), made for this project; | |
| - [lyfeyvutha/nllb-en-km-316K](https://huggingface.co/datasets/lyfeyvutha/nllb-en-km-316K): 316K synthetic pairs; | |
| - [KrorngAI/ParaCrawl-English-Khmer-v2](https://huggingface.co/datasets/KrorngAI/ParaCrawl-English-Khmer-v2): 1.5M web-crawled pairs; | |
| - [SeyhaLite/Translate-English-Khmer-All](https://huggingface.co/datasets/SeyhaLite/Translate-English-Khmer-All): 366K pairs. | |
| The cleaned, combined set is [Netra-Lab/bilingual-en-km](https://huggingface.co/datasets/Netra-Lab/bilingual-en-km). The model was tested on the test split of [mutiyama/alt](https://huggingface.co/datasets/mutiyama/alt) (Wikinews), and was not fine-tuned on it. | |
| ## Learn more | |
| | | | | |
| |---|---| | |
| | [README](https://github.com/netra-ai-lab/Netra-NMT#readme) | The project overview: data, architecture, evaluation, install, usage and limitations | | |
| | [Results](https://github.com/netra-ai-lab/Netra-NMT/blob/main/docs/RESULTS.md) | Every measured number: quality, speed (PyTorch and ONNX Runtime), the parity check | | |
| | [Architecture](https://github.com/netra-ai-lab/Netra-NMT/blob/main/docs/ARCHITECTURE.md) | The package, the two ONNX graphs and the model folder format | | |
| | [Training](https://github.com/netra-ai-lab/Netra-NMT/blob/main/training/README.md) | Fine-tuning on your own pairs, the full recipe, evaluation and export | | |
| | [Issues](https://github.com/netra-ai-lab/Netra-NMT/issues) | Bug reports and questions | | |
| ## License and attribution | |
| - **Weights:** MIT, trained from scratch (no pretrained weights). | |
| - **Training data:** see the [dataset card](https://huggingface.co/datasets/Netra-Lab/bilingual-en-km) and its sources; check their terms before commercial use. | |
| - **Evaluation data:** ALT (NICT), CC BY 4.0. | |