CoolFace
Modelpublic

TigreGotico/indictrans2-en-indic-dist-200M-onnx

sourceHugging Facemitupdated 2mo agoView on Hugging Face
0likes103downloads
Model Card

indictrans2-en-indic-dist-200M-onnx

ONNX export of `ai4bharat/indictrans2-en-indic-dist-200M`, the IndicTrans2 translation model from AI4Bharat / IIT Madras.

Direction: English into 22 Indic languages. src_lang is always eng_Latn.

Files

FilePurpose
encoder_model.onnxEncoder
decoder_model.onnxDecoder, first step, no cache
decoder_with_past_model.onnxDecoder, later steps, with KV cache
int8/The same three graphs, int8 dynamic quantization
model.SRC, model.TGT, dict.SRC.json, dict.TGT.jsonSentencePiece models and vocabularies (source and target sides are separate)
configuration_indictrans.py, modeling_indictrans.py, tokenization_indictrans.pyCustom architecture and tokenizer code, loaded with trust_remote_code=True

Sizes: fp32 1.87 GB, int8 472 MB.

Preprocessing is mandatory

IndicTrans2 does not take raw text. You must run IndicProcessor from IndicTransToolkit first:

bash
pip install IndicTransToolkit optimum[onnxruntime] transformers sentencepiece

IndicProcessor.preprocess_batch normalises the script, applies Indic-specific punctuation and numeral handling, protects entities, and prefixes the two language tags. Skipping it gives silently wrong output — the model still produces fluent-looking text, but in the wrong language or with corrupted content.

postprocess_batch reverses the entity protection. It is also required.

Language tags

The tags are plain text prefixed to the source sentence, in the order <src_lang> <tgt_lang> <sentence>. IndicProcessor adds them for you; you only pass src_lang= and tgt_lang=.

For example, ip.preprocess_batch(["This is a test."], src_lang="eng_Latn", tgt_lang="hin_Deva") produces "eng_Latn hin_Deva This is a test .".

The tags are <iso639-3>_<ISO 15924 script>. Languages with two scripts have two tags. Supported tags:

TagLanguage
asm_BengAssamese
ben_BengBengali
brx_DevaBodo
doi_DevaDogri
gom_DevaKonkani
guj_GujrGujarati
hin_DevaHindi
kan_KndaKannada
kas_ArabKashmiri (Arabic)
kas_DevaKashmiri (Devanagari)
mai_DevaMaithili
mal_MlymMalayalam
mar_DevaMarathi
mni_BengManipuri (Bengali)
mni_MteiManipuri (Meitei)
npi_DevaNepali
ory_OryaOdia
pan_GuruPunjabi
san_DevaSanskrit
sat_OlckSantali
snd_ArabSindhi (Arabic)
snd_DevaSindhi (Devanagari)
tam_TamlTamil
tel_TeluTelugu
urd_ArabUrdu
eng_LatnEnglish

Usage

python
import torch
from transformers import AutoTokenizer
from optimum.onnxruntime import ORTModelForSeq2SeqLM
from IndicTransToolkit.processor import IndicProcessor

REPO = "TigreGotico/indictrans2-en-indic-dist-200M-onnx"

tokenizer = AutoTokenizer.from_pretrained(REPO, trust_remote_code=True)
model = ORTModelForSeq2SeqLM.from_pretrained(REPO, trust_remote_code=True, use_cache=True)
ip = IndicProcessor(inference=True)          # REQUIRED - see "Preprocessing"

sentences = ["The weather is nice today and the children are playing outside.", "I would like a cup of tea with a little sugar."]
src_lang, tgt_lang = "eng_Latn", "hin_Deva"

batch = ip.preprocess_batch(sentences, src_lang=src_lang, tgt_lang=tgt_lang)
enc = tokenizer(batch, return_tensors="pt", padding=True, truncation=True, max_length=256)

with torch.inference_mode():
    out = model.generate(**enc, num_beams=4, max_new_tokens=64, use_cache=True)

decoded = tokenizer.batch_decode(out, skip_special_tokens=True, src=False)
print(ip.postprocess_batch(decoded, lang=tgt_lang))

To use the quantized graphs, pass subfolder="int8".

Sample output

DirectionInputOutput
eng_Latn → hin_DevaThe weather is nice today and the children are playing outside.आज मौसम अच्छा है और बच्चे बाहर खेल रहे हैं।
eng_Latn → tam_TamlI would like a cup of tea with a little sugar.எனக்கு கொஞ்சம் சர்க்கரையுடன் ஒரு கப் தேநீர் வேண்டும்.
eng_Latn → ben_BengThe train to Chennai leaves from platform number three at six in the morning.চেন্নাই যাওয়ার ট্রেনটি তিন নম্বর প্ল্যাটফর্ম থেকে সকাল ছয়টায় ছেড়ে যায়।

Parity against the PyTorch original

10 sentences over 5 Indic languages (Hindi, Tamil, Bengali, Marathi, Malayalam), num_beams=4, max_new_tokens=64, greedy string comparison against AutoModelForSeq2SeqLM.from_pretrained(..., trust_remote_code=True).

PrecisionExact match
fp32100%
int860%

Limits

Maximum sequence length is 256 tokens on both sides. The sinusoidal position table is frozen into the graph at export time, so longer inputs are not supported. Truncate with max_length=256.

Export route

Exported with optimum.exporters.onnx.onnx_export_from_model and a custom OnnxConfig registered for the IndicTrans model type. The config subclasses M2M100OnnxConfig and remaps the field names IndicTrans2 uses (encoder_embed_dim instead of d_model, encoder_vocab_size instead of vocab_size). Opset 17. Optimum validated encoder and both decoder graphs against PyTorch at atol=1e-3 during export.

optimum-cli export onnx alone does not work: it has no config for the IndicTrans architecture.

Attribution

The model is the work of AI4Bharat, IIT Madras.

bibtex
@article{gala2023indictrans2,
  title   = {IndicTrans2: Towards High-Quality and Accessible Machine Translation Models for all 22 Scheduled Indian Languages},
  author  = {Jay Gala and Pranjal A. Chitale and Raghavan AK and Varun Gumma and Sumanth Doddapaneni and Aswanth Kumar and Janki Nawale and Anupama Sujatha and Ratish Puduppully and Vivek Raghavan and Pratyush Kumar and Mitesh M. Khapra and Raj Dabre and Anoop Kunchukuttan},
  journal = {Transactions on Machine Learning Research},
  year    = {2023}
}

Licence: MIT, same as the original.