On-Device Grammar Correction with Sequence-to-Sequence Models

google · Tech · 2021 · source Agentic: no · promotional bias: low · source-backed · extracted with prompt 0.5.4 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Tech

Google needed real-time grammar correction in Gboard on mobile devices without sending user typing to a cloud service.

Constraints:

Architecture

Google built Gboard grammar correction as a compact on-device sequence-to-sequence system. The runtime model combines a Transformer encoder with an LSTM decoder, shares/factorizes embeddings to keep a large token vocabulary without a large footprint, and uses post-training quantization to store 32-bit weights as 8-bit values. Training uses hard distillation: a cloud grammar model corrects hundreds of millions of public-web English sentences, then a prefix-capable cloud model generates sentence-prefix pairs so the smaller device model learns both complete-sentence and as-you-type correction. At inference time, Gboard calls the local model after more than three words, aligns original and corrected text with Levenshtein distance, and turns sentence edits into user-facing replacement suggestions.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  cloud-grammar-model["cloud-grammar-model"] --> distilled-training-data["distilled-training-data"]
  distilled-training-data --> on-device-grammar-model["on-device-grammar-model"]
  on-device-grammar-model --> transformer-encoder["transformer-encoder"]
  on-device-grammar-model --> lstm-decoder["lstm-decoder"]
  transformer-encoder --> shared-factorized-embeddings["shared-factorized-embeddings"]
  lstm-decoder --> shared-factorized-embeddings
  shared-factorized-embeddings --> quantized-weights["quantized-weights"]
  gboard-client["gboard-client"] --> on-device-grammar-model
  gboard-client --> edit-alignment["edit-alignment"]
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
gboard-client Runs grammar correction locally as the user types and displays replacement suggestions. on-device-grammar-model, edit-alignment
on-device-grammar-model Compact grammar-correction seq2seq model trained to correct full sentences and sentence prefixes. gboard-client, transformer-encoder, lstm-decoder, distilled-training-data
transformer-encoder Language-understanding half of the hybrid architecture. on-device-grammar-model, shared-factorized-embeddings
lstm-decoder Generation half of the hybrid architecture that emits corrected text. on-device-grammar-model, shared-factorized-embeddings
shared-factorized-embeddings Shares weights and separates vocabulary embeddings from hidden size to reduce model footprint while keeping a large token vocabulary. transformer-encoder, lstm-decoder, quantized-weights
quantized-weights Stores model weights in 8-bit form after post-training quantization to reduce size. shared-factorized-embeddings, on-device-grammar-model
cloud-grammar-model Large grammar system used to generate distilled correction targets for complete sentences and sentence prefixes. distilled-training-data
distilled-training-data Combination of cloud-generated full-sentence and sentence-prefix original/corrected pairs for device-model training. cloud-grammar-model, on-device-grammar-model
edit-alignment Aligns original and corrected sentences and turns model output into concrete replacement suggestions. gboard-client

Data flow

Training starts with original/corrected text pairs produced by a cloud grammar model over hundreds of millions of web sentences. Google adds a second sentence-prefix dataset by sampling aligned prefixes, completing original prefixes with a neural language model, and distilling from a prefix-capable cloud model. The combined full-sentence and prefix data trains the compact on-device model. At runtime, Gboard sends typed text to the local grammar model once the user has typed more than three words; the model returns a corrected sentence, an alignment step minimizes Levenshtein distance between original and corrected text, and the app converts insertions/deletions into replacement suggestions.

5 supporting quotes
“We first collected hundreds of millions of English sentences from across the public web. We then used the large cloud-based grammar model to generate grammar corrections for those sentences.”article, stated
“We created a second dataset suitable for training a large cloud-based model, but this time focusing on sentence prefixes.”article, stated
“Finally, we constructed the final training data for the on-device model by combining these new sentence prefix pairs with the full sentence pairs.”article, stated
“Gboard sends a request to the on-device grammar model whenever the user has typed more than three words, whether the sentence is completed or not.”article, stated
“we align the original sentence and the corrected sentence by minimizing the Levenshtein distance”article, stated

Why this architecture

The architecture is shaped by phone constraints: grammar correction must run locally for privacy, fit mobile memory/compute limits, and remain fast enough to trigger while typing. The hybrid Transformer/LSTM model balances quality and latency; shared/factorized embeddings and quantization keep the model small; hard distillation transfers cloud-model behavior into a device-sized model; and prefix data handles the mobile reality that users type and send incomplete sentences.

4 supporting quotes
“that works entirely on-device to preserve privacy, detecting and suggesting corrections for grammatical errors while the user is typing.”article, stated
“a combination that provides a good balance of quality and latency.”article, stated
“Mobile devices are constrained by limited memory and computational power, which make it more difficult to build a high quality grammar checking system.”article, stated
“If grammar correction is only triggered on complete sentences, it might miss many errors.”article, stated

Models used: cloud-based grammar model · on-device grammar model

Scale

Alternatives considered (roads not taken)

Using the original cloud-model training data directly for the small on-device model

Rejected because: Hard distillation produced higher-quality suggestions for a similar-sized on-device model.

“One possible approach to generating a small on-device model would be to use the same training data as a large cloud-based grammar model.”article, stated
“to generate training data that is better-matched to the on-device domain yields even better quality results.”article, stated

Triggering grammar correction only on complete sentences

Rejected because: Mobile messaging often lacks a final period, so complete-sentence-only correction would miss many typing-time errors.

“While this enables grammar correction when the user has only typed part of a sentence, this capability is particularly useful in messaging apps, where the user often omits the final period”article, stated
“If grammar correction is only triggered on complete sentences, it might miss many errors.”article, stated

Using a larger remote grammar system at inference time

Rejected because: The product goal was private, low-latency correction on the device without sending keystrokes to a remote server.

“This compact model enables users to correct their text entirely on their own device without ever needing to send their keystrokes to a remote server.”article, stated

Patterns

model-distillation (model-operations)

A large cloud grammar model generated corrections used to train a smaller on-device model.

“We then used the large cloud-based grammar model to generate grammar corrections for those sentences.”article, stated

latency-optimization (serving)

The model was compressed enough to run 60-character inference under 22ms on a Pixel 6 CPU.

“By employing these techniques, the resulting model takes up only 20MB of storage and performs inference on 60 input characters under 22ms on the Google Pixel 6 CPU.”article, stated

privacy-preserving-processing (governance)

The system keeps grammar correction local to the device instead of sending keystrokes to a remote model.

“The system performs real-time grammatical error correction entirely on-device, eliminating the need to send user keystrokes to remote servers.”summary, stated

Tradeoffs

Compress the model for on-device latency while preserving acceptable quality.

Chose factorized embeddings and quantization over a larger cloud-scale model on the phone.

Rationale: The source states mobile constraints required controlling memory and latency, and that lower-fidelity weights did not materially affect model quality.

“While this means that each weight is stored with lower fidelity, nevertheless, we find that the quality of the model is not materially affected.”article, stated

Use hard distillation for device-domain training data.

Chose cloud-generated correction pairs matched to the on-device domain over the original data used to train the large cloud grammar model.

Rationale: A similar-sized model trained from distilled data produced higher-quality suggestions.

“to generate training data that is better-matched to the on-device domain yields even better quality results.”article, stated
“the on-device model built from this training dataset produces significantly higher quality suggestions than a similar-sized on-device model built on the original data”article, stated

Train on sentence prefixes as well as complete sentences.

Chose combined full-sentence and sentence-prefix training data over complete-sentence-only grammar correction.

Rationale: Typing-time mobile correction would miss many errors if it waited for completed sentences.

“The on-device model trained on this combined data is then capable of correcting both full sentences as well as sentence prefixes.”article, stated
“If grammar correction is only triggered on complete sentences, it might miss many errors.”article, stated

Gotchas (what bit them)

Mobile grammar correction must handle incomplete sentences, not just complete sentences.

Root cause: Messaging users often send text without a final period, and correction can happen while they type.

Resolution: Google generated sentence-prefix training pairs and combined them with full-sentence pairs.

“To enable the model to correct grammar as the user types (an important capability of mobile devices) it needs to be able to handle sentence prefixes.”article, stated

Evaluation

The source describes quality comparisons between distilled on-device models and similar-sized models trained on original cloud-model data.

Metrics: model quality · model size · inference latency

“We found that the on-device model built from this training dataset produces significantly higher quality suggestions than a similar-sized on-device model built on the original data used to train the cloud-based model.”article, stated

Outcomes

The feature is deployed through Gboard on Pixel 6.

“Gboard sends a request to the on-device grammar model whenever the user has typed more than three words, whether the sentence is completed or not.”article, stated