Dịch máy nơ-ronNeural translation

Dịch máy là đoán token tiếp theo, có điều kiện là cả câu nguồn. Translation is next-token prediction, conditioned on the whole source.

Sáu bước biến “Hello, world!” thành “¡Hola, mundo!” — theo sơ đồ pipeline của Google Translate, đã đối chiếu và sửa những chỗ sai. Six steps that turn “Hello, world!” into “¡Hola, mundo!” — following the Google Translate pipeline diagram, checked and corrected where it was wrong.

Hello, world! → tokens → <2es> → encoder/decoder → beam search → ¡Hola, mundo! 1 · nhận diện1 · detect Hello, world! en .97 fr .02 2 · tách token 3 · token đích2 · tokenize 3 · target token <2es> ▁Hello, ▁world! 4 · encoder 5 · decoder từng token mộtone token at a time 6 · beam search (k = 2) <BOS> ¡Hola −0.4 Hola −0.6 Oye −2.1 ✕ ¡Hola, mundo! −0.9 Hola mundo −1.4

Bạn có cần dịch máy không?Do you need machine translation at all?

Trả lời câu này trước khi học kiến trúc. Hầu hết đội sản phẩm không cần tự dựng pipeline.Answer this before learning the architecture. Most product teams never need to build the pipeline.

Khi nào không cần gì cảWhen you need none of it

  • Chuỗi cố định (giao diện, menu, email mẫu): thuê người dịch một lần, lưu vào file i18n. Rẻ nhất về lâu dài, không có độ trễ.Fixed strings (UI, menus, email templates): hire translators once, store in i18n files. Cheapest long-term, zero latency.
  • Đã biết ngôn ngữ nguồn: bỏ bước nhận diện, hỏi người dùng một lần rồi nhớ.Known source language: skip detection, ask once and remember.
  • Nội dung phổ thông, khối lượng vừa: gọi API dịch có sẵn.General content, moderate volume: call an existing translation API.
  • Pháp lý, y tế: người dịch chuyên môn; máy chỉ làm bản nháp.Legal, medical: professional translators; the machine only drafts.

Chọn trong 30 giâyChoose in 30 seconds

  1. Nội dung cố định, ít đổi?Fixed, rarely changing content?Người dịch + file i18n.Translators + i18n files.
  2. Rủi ro cao (pháp lý, y tế)?High stakes (legal, medical)?Người dịch, máy dịch nháp.Translators, machine draft.
  3. Dữ liệu không được rời hạ tầng?Data must stay in-house?Model mở tự host: NLLB-200, OPUS-MT, TranslateGemma.Self-hosted open model: NLLB-200, OPUS-MT, TranslateGemma.
  4. Cần giữ giọng văn, ngữ cảnh dài?Need tone and long context?LLM + prompt có ngữ cảnh + kiểm tra đầu ra.LLM + context-rich prompt + output checks.
  5. Còn lạiEverything elseAPI dịch có sẵn, thêm glossary nếu có thuật ngữ riêng.Existing API, plus a glossary for custom terms.

Sáu bước, theo đúng thứ tự chạySix steps, in execution order

Đây là một chuỗi thật nên được đánh số. Màu cho biết bước đó thuộc nhóm nào.This is a real sequence, so it is numbered. Colour tells you which group a step belongs to.

01

Nhận diện ngôn ngữLanguage detection

Chuẩn bị đầu vàoInput prep

Một classifier tí hon (kiểu CLD3) đọc n-gram ký tự và trả về xác suất cho từng ngôn ngữ, trong cỡ mili-giây.A tiny classifier (CLD3-style) reads character n-grams and returns a probability per language, in about a millisecond.

Dùng choUse for

Nút “phát hiện ngôn ngữ”, định tuyến ticket, lọc dữ liệu crawl.“Detect language”, ticket routing, filtering crawled data.

ĐượcGain

Vài MB, chạy CPU, có điểm tin cậy để đặt ngưỡng.A few MB, runs on CPU, gives a confidence score to threshold.

MấtLose

Sai với chuỗi ngắn (“OK”), câu lẫn ngôn ngữ, tiếng Việt không dấu.Fails on short strings (“OK”), mixed languages, unaccented Vietnamese.

Chọn classifier tí hon là đổi độ chính xác trên câu ngắn lấy tốc độ mili-giây và chi phí gần bằng không.A tiny classifier trades accuracy on short input for millisecond speed and near-zero cost.

Dấu hiệu cần:You need it when: người dùng không chọn ngôn ngữ nguồn.the user didn't pick a source language.

02

Tách tokenTokenization

Chuẩn bị đầu vàoInput prep

Cắt văn bản thành subword và đổi thành ID. Google dùng WordPiece / SentencePiece; dấu ▁ đánh dấu khoảng trắng phía trước.Splits text into subwords and maps them to IDs. Google uses WordPiece / SentencePiece; ▁ marks a preceding space.

Dùng choUse for

Mọi model ngôn ngữ; tên riêng, lỗi chính tả, ngôn ngữ không có dấu cách.Every language model; names, typos, languages without spaces.

ĐượcGain

32k–64k token phủ mọi chuỗi; không còn từ ngoài từ điển.32k–64k tokens cover any string; no out-of-vocabulary words.

MấtLose

Ngôn ngữ ít dữ liệu bị chẻ vụn, tốn token; đổi tokenizer là phải huấn luyện lại.Low-resource languages get fragmented; changing tokenizer means retraining.

Chọn subword là đổi chuỗi token dài hơn lấy khả năng biểu diễn mọi từ, kể cả từ chưa từng thấy.Subwords trade longer token sequences for the ability to represent any word, even unseen ones.

Bẫy hay gặp:Common trap: tiếng Việt dạng NFD (copy từ PDF, macOS) cho token khác NFC. Chuẩn hoá NFC trước.Vietnamese in NFD (from PDFs, macOS) tokenizes differently from NFC. Normalize to NFC first.

03

Token ngôn ngữ đíchTarget language token

Chuẩn bị đầu vàoInput prep

Một token như <2es> bảo model dịch sang ngôn ngữ nào. Kiến trúc giữ nguyên, chỉ dữ liệu thay đổi (Johnson và cộng sự, 2017).A token like <2es> tells the model which language to produce. Same architecture, only the data changes (Johnson et al., 2017).

Dùng choUse for

Phục vụ nhiều cặp bằng một model; giúp ngôn ngữ ít dữ liệu; zero-shot.Many pairs from one model; helps low-resource languages; zero-shot.

ĐượcGain

Một model thay cho N×(N−1) model song ngữ.One model replaces N×(N−1) bilingual ones.

MấtLose

Cặp lớn giảm chất lượng nhẹ; zero-shot hay ra sai ngôn ngữ (off-target).Slight drop on large pairs; zero-shot often goes off-target.

Chọn model đa ngôn ngữ là đổi một ít chất lượng ở cặp giàu dữ liệu lấy việc vận hành một model thay vì hàng trăm.One multilingual model trades a little quality on rich pairs for running one model instead of hundreds.

Lưu ý:Note: vị trí token tuỳ model — NLLB đặt ở đầu decoder (forced_bos_token_id), không phải đầu câu nguồn.placement varies — NLLB puts it at the decoder start (forced_bos_token_id), not before the source.

04

Encoder

Mạng nơ-ronNeural model

Đọc cả câu nguồn một lượt. Self-attention cho mỗi token nhìn mọi token khác, nên “bank” trong “river bank” và “bank account” ra hai vector khác nhau.Reads the whole source in one pass. Self-attention lets every token see every other, so “bank” in “river bank” and “bank account” get different vectors.

Dùng choUse for

Dịch, tóm tắt, phân loại — khi đầu vào có sẵn đủ từ đầu.Translation, summarization, classification — when the full input is known up front.

ĐượcGain

Song song hoá; nối trực tiếp mọi cặp token. Theo Google, phần lớn mức tăng chất lượng đến từ encoder.Parallel; every token pair directly connected. Per Google, most of the quality gain comes from the encoder.

MấtLose

Chi phí tăng theo bình phương độ dài; giới hạn độ dài cứng.Cost grows with length squared; hard length limit.

Chọn self-attention là đổi chi phí bậc hai theo độ dài câu lấy xử lý song song và tầm nhìn toàn câu.Self-attention trades quadratic cost in sentence length for parallelism and a whole-sentence view.

Mẹo:Tip: encoder chạy một lần mỗi câu; kết quả được dùng lại ở mọi bước decoder và mọi nhánh beam.the encoder runs once per sentence; its output is reused at every decoder step and in every beam.

05

Decoder

Mạng nơ-ronNeural model

Viết câu đích từng token một. Masked self-attention chỉ nhìn phần đã viết; cross-attention hỏi encoder “phần nào của câu nguồn liên quan lúc này”.Writes the target one token at a time. Masked self-attention sees only what's written; cross-attention asks the encoder “which part of the source matters now”.

Dùng choUse for

Sinh chuỗi có độ dài không biết trước: dịch, tóm tắt, chú thích ảnh.Output of unknown length: translation, summarization, captioning.

ĐượcGain

Giữ ngữ pháp, hoà hợp, đảo trật tự từ tự do; có xác suất sẵn cho beam search.Keeps grammar and agreement, free reordering; probabilities ready for beam search.

MấtLose

40 token = 40 lượt chạy; lỗi đầu lan xuống cuối; có thể bịa hoặc bỏ sót.40 tokens = 40 passes; early errors propagate; can hallucinate or omit.

Chọn giải mã tự hồi quy là đổi tốc độ lấy việc mỗi token được quyết định dựa trên toàn bộ những gì đã viết.Autoregressive decoding trades speed for each token being chosen in light of everything already written.

Thực tế:In practice: decoder là nút thắt độ trễ — năm 2020 Google dùng decoder RNN cho production vì nhanh hơn mà gần như không kém.the decoder is the latency bottleneck — in 2020 Google used an RNN decoder in production because it was faster with almost no loss.

06

Beam search

Tìm kiếm đầu raOutput search

Giữ song song k câu dở dang có tổng log-xác suất cao nhất, loại phần còn lại. Tất định — không lấy mẫu như ảnh gốc ghi.Keeps the k partial sentences with the highest total log-probability and prunes the rest. Deterministic — it does not sample, despite what the diagram says.

Dùng choUse for

Dịch, nhận dạng giọng nói, tạo danh sách n-best để xếp hạng lại.Translation, speech recognition, n-best lists for reranking.

ĐượcGain

Thoát bẫy của greedy; beam 4–5 cải thiện ổn định; dễ tái hiện lỗi.Escapes the greedy trap; beam 4–5 gives steady gains; reproducible.

MấtLose

Thiên vị câu ngắn nếu không có length penalty; beam quá lớn làm chất lượng giảm.Favours short output without a length penalty; very large beams hurt quality.

Chọn beam rộng hơn là đổi độ trễ lấy khả năng thoát lựa chọn cục bộ tồi — chỉ tới một ngưỡng.A wider beam trades latency for escaping bad local choices — only up to a point.

Bước bị thiếu sau đó:Missing step after it: detokenize — ghép subword, đổi ▁ thành khoảng trắng, trả lại thẻ HTML và placeholder.detokenize — join subwords, turn ▁ into spaces, restore HTML tags and placeholders.

Ảnh gốc sai hoặc giản lược ở đâuWhere the original diagram is wrong or simplified

Sơ đồ là mô hình dạy học về NMT kinh điển, không phải bản vẽ hệ thống production hiện tại.The diagram is a teaching model of classic NMT, not a blueprint of today's production system.

Beam search lấy mẫu nhiều câuBeam search samples completions

Beam search tất định, giữ top-k theo log-xác suất. Lấy mẫu là chiến lược khác.Beam search is deterministic, keeping the top k by log-probability. Sampling is a different strategy.

Token đầu tiên là “!” (0.97)The first token is “!” (0.97)

Với “¡Hola, mundo!”, token đầu hợp lý là “¡” (chấm than ngược).For “¡Hola, mundo!”, the sensible first token is “¡” (inverted exclamation).

Model phục vụ mọi cặp ngôn ngữThe model serves every language pair

Model đa ngôn ngữ phục vụ được nhiều cặp, nhưng zero-shot kém hơn rõ; không có bằng chứng production dùng đúng một model.A multilingual model can serve many pairs, but zero-shot is clearly weaker; no evidence production uses exactly one model.

Encoder và decoder Transformer chuẩnStandard Transformer encoder and decoder

2020: encoder Transformer + decoder RNN. Từ 12/2025: Gemini cho khoảng 20 ngôn ngữ, bắt đầu ở Mỹ và Ấn Độ.2020: Transformer encoder + RNN decoder. Since Dec 2025: Gemini for about 20 languages, starting in the US and India.

BPE tokenizer, ký hiệu “_Hel”BPE tokenizer, “_Hel” notation

WordPiece / SentencePiece; ký hiệu đúng là “▁Hel”. ID chỉ minh hoạ.WordPiece / SentencePiece; the real marker is “▁Hel”. IDs are illustrative.

Núm vặn ở từng bướcThe knob at each step

Các bước nối tiếp nhau, không thay thế nhau. Thứ bạn thật sự chọn là vặn mỗi núm tới đâu.The steps are a chain, not alternatives. What you really choose is how far to turn each knob.

BướcStep Núm vặnKnob Vặn lên thì đượcTurn up to gain Vặn lên thì mấtTurn up and lose Latency
Nhận diệnDetectNgưỡng tin cậyConfidence thresholdÍt nhận saiFewer misdetectionsNhiều câu phải fallbackMore fallbacksKhông đáng kểNegligible
Tách tokenTokenizeKích thước từ điểnVocab sizeChuỗi ngắn hơnShorter sequencesEmbedding nặng, token hiếm học kémHeavier embeddings, rare tokens learned poorlyGiảm nhẹSlight decrease
Token đíchTarget tokenSố ngôn ngữ / modelLanguages per modelÍt model, ngôn ngữ nhỏ tốt lênFewer models, small languages improveCặp lớn giảm, off-targetLarge pairs drop, off-targetKhông đổiUnchanged
EncoderSố lớp, độ dài tối đaLayers, max lengthHiểu câu tốt hơnBetter understandingBộ nhớ, chi phí bậc haiMemory, quadratic costTăng vừa (chạy 1 lần)Moderate (runs once)
DecoderSố lớp, kiểu decoderLayers, decoder typeCâu trôi chảy hơnMore fluent outputChậm hơn × độ dài câuSlower × sentence lengthTăng mạnhLarge
Beam searchBeam size kThoát lựa chọn cục bộ tồiEscape bad local choicesLatency ×k; quá lớn thì kém điLatency ×k; too large gets worseTăng tuyến tínhLinear

Anti-patterns

Những lỗi xuất hiện ở hầu hết lần tích hợp dịch máy đầu tiên.Mistakes that show up in almost every first machine-translation integration.

Sai lầmMistakeVì sao saiWhy it's wrongSửa thế nàoFix
Beam càng lớn càng tốtBigger beam is always betterQuá ngưỡng thì câu cụt hoặc rỗng; độ trễ ×kPast a point outputs get short or empty; latency ×kBeam 4–5 + length penalty, chỉnh trên tập devBeam 4–5 + length penalty, tuned on dev
Tin nhận diện với vài từTrusting detection on a few words“OK”, “Hello” có ở hàng chục ngôn ngữ“OK”, “Hello” exist in dozens of languagesNgưỡng tin cậy + ngôn ngữ người dùng đã chọnThreshold + the user's chosen language
Bỏ chuẩn hoá UnicodeSkipping Unicode normalizationNFD và NFC ra token khác nhauNFD and NFC tokenize differentlyChuẩn hoá NFC ở đầu pipelineNormalize to NFC up front
Dịch cả placeholder, thẻ HTMLTranslating placeholders and tags{name} bị dịch, UI vỡ{name} gets translated, UI breaksChe bằng token giữ chỗ, trả lại sauMask with sentinels, restore after
Dịch từng câu tách rờiIsolated sentence-by-sentenceMất đại từ, thuật ngữ thiếu nhất quánLost pronouns, inconsistent termsGửi kèm ngữ cảnh, dùng glossaryPass context, use a glossary
Xác suất cao = đúngHigh probability = correctModel có thể tự tin mà vẫn bịaThe model can be confident and still inventModel ước lượng chất lượng (QE) + kiểm tra ngườiQuality estimation model + human spot checks
Chỉ đo bằng BLEUMeasuring only with BLEUTương quan kém với đánh giá của ngườiCorrelates poorly with human judgementCOMET, MetricX, chrF + đánh giá ngườiCOMET, MetricX, chrF + human review

3 câu cần nhớ3 things to remember

  1. Encoder đọc cả câu nguồn một lần; decoder viết câu đích từng token một.The encoder reads the source once; the decoder writes the target one token at a time.
  2. Subword để không bao giờ gặp từ lạ, token đích để một model phục vụ nhiều cặp ngôn ngữ.Subwords so no word is ever unknown; a target token so one model serves many pairs.
  3. Beam search tìm câu có xác suất cao, không tìm câu đúng.Beam search finds a probable sentence, not a correct one.