Masked Language Modeling (MLM) is a pre-training objective where a percentage of input tokens are randomly replaced with a special [MASK] token, and the model is trained to predict the original vocabulary ID of the hidden words based on their bidirectional context. Unlike autoregressive models that only see preceding text, MLM allows the model to condition its predictions on both the left and right surrounding words simultaneously, building a deep, holistic representation of syntax and semantics.
Glossary
Masked Language Modeling (MLM)

What is Masked Language Modeling (MLM)?
Masked Language Modeling is the foundational self-supervised pre-training objective for encoder-based architectures, teaching models to develop a bidirectional, contextual understanding of language by predicting intentionally hidden tokens within an input sequence.
This technique, popularized by BERT, is critical for legal artificial intelligence because it forces the model to learn the functional relationships between legal concepts rather than memorizing sequences. By masking key statutory terms or party names, the model must infer meaning from the surrounding contractual or regulatory context, enabling superior performance on downstream tasks like legal text classification, entity recognition, and clause extraction.
Key Characteristics of MLM
Masked Language Modeling is a self-supervised pre-training objective that teaches encoder models to develop a deep, bidirectional understanding of legal syntax by predicting intentionally hidden words from their surrounding context.
The Cloze Task Mechanism
MLM randomly masks a percentage of input tokens (typically 15%) and forces the model to predict the original vocabulary ID of the hidden word. Unlike Causal Language Modeling (CLM) , which only sees left context, MLM allows the model to attend to tokens both before and after the mask. This bidirectional attention is critical for understanding complex legal syntax where the meaning of a term like 'consideration' depends on its full contractual context.
Bidirectional Context Fusion
The core architectural advantage of MLM is its ability to fuse left and right context into a single representation. For a masked statute like 'The [MASK] shall have the authority to adjudicate,' the model simultaneously weighs 'The' and 'shall have the authority' to predict 'court' or 'tribunal.' This deep contextualization enables Domain-Adaptive Pre-Training (DAPT) to produce superior legal embeddings for downstream tasks like Legal Text Summarization and Citation Network Analysis.
Masking Strategies for Legal Text
Standard random masking is often suboptimal for legal corpora. Advanced strategies include:
- Whole Word Masking: Masking entire words like 'jurisdiction' instead of subword pieces, preventing the model from cheating via partial tokens.
- Citation Masking: Replacing legal citations (e.g., '347 U.S. 483') with a special [CITE] token, forcing the model to learn the rhetorical function of authority rather than memorizing case strings.
- Span Masking: Masking contiguous sequences of tokens to teach the model to reconstruct longer legal phrases and clauses.
Encoder-Only Architecture Foundation
MLM is the defining pre-training objective for encoder-only transformer architectures like BERT, RoBERTa, and Legal-BERT. These models are not designed for text generation but excel at natural language understanding. In the legal domain, an MLM-pretrained encoder produces contextualized embeddings that power Contract Clause Extraction, Legal Argument Mining, and semantic similarity systems. The resulting [CLS] token representation serves as a dense summary of an entire legal document.
Training Signal Density
A key efficiency consideration is that MLM only generates a training signal from the 15% of tokens that are masked. The remaining 85% of tokens are ignored in the loss calculation, making MLM less sample-efficient per epoch than Causal Language Modeling (CLM) . To compensate, legal models like CaseLaw-BERT are typically trained for more epochs on carefully stratified Legal Data Mix corpora. The trade-off is accepted because the resulting bidirectional representations are far more powerful for classification and extraction tasks.
Evaluation via Legal Perplexity
The primary intrinsic metric for an MLM-trained legal model is Legal Perplexity on a held-out corpus of statutes and contracts. A lower perplexity score indicates the model has internalized the statistical patterns of legal language. For example, a model achieving a perplexity of 3.2 on the EDGAR contracts corpus has a much stronger grasp of transactional syntax than a general-domain model scoring 8.7. This metric directly correlates with downstream performance on tasks from the LexGLUE benchmark.
Frequently Asked Questions
Explore the mechanics, applications, and nuances of Masked Language Modeling (MLM), the foundational pre-training objective behind powerful legal language understanding models like BERT.
Masked Language Modeling (MLM) is a self-supervised pre-training objective where a percentage of input tokens are randomly replaced with a special [MASK] token, and the model is trained to predict the original vocabulary ID of the masked token by analyzing its bidirectional context. Unlike autoregressive models that only look at previous tokens, an MLM model simultaneously considers both the left and right context to fill in the blank. For example, in the sentence 'The court held that the [MASK] was liable,' the model uses the surrounding words to predict 'defendant.' This forces the model to develop a deep, contextualized understanding of syntax, semantics, and even factual world knowledge, making it exceptionally powerful for natural language understanding (NLU) tasks like legal document classification and entity extraction. The standard masking rate is typically 15%, with 80% of those masked tokens replaced by [MASK], 10% by a random token, and 10% left unchanged to bias the model towards realistic representations.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Understanding Masked Language Modeling requires familiarity with the core pre-training objectives, tokenization strategies, and architectural components that define modern encoder-based legal language models.
Causal Language Modeling (CLM)
The autoregressive counterpart to MLM, where the model predicts the next token in a sequence based solely on preceding context. Unlike MLM's bidirectional attention, CLM uses a unidirectional (left-to-right) attention mask, making it the foundation for generative models like GPT-4. In legal AI, CLM excels at text generation tasks such as drafting clauses and summarization, while MLM-based encoders are preferred for understanding tasks like classification and entity extraction.
Subword Tokenization
Algorithms like Byte-Pair Encoding (BPE) and SentencePiece that split text into frequent subword units, balancing vocabulary size against coverage of rare terms. For legal MLM pre-training, a domain-specific tokenizer is critical—it prevents splitting terms like 'res judicata' or '28 U.S.C. § 1332' into meaningless fragments. A well-trained legal tokenizer directly reduces the out-of-vocabulary rate and improves the model's ability to learn coherent representations of statutory language.
Domain-Adaptive Pre-Training (DAPT)
The process of continuing to train a foundation model on a large, unlabeled domain-specific corpus using the same MLM objective. For legal models, this means taking a BERT-base checkpoint and continuing pre-training on millions of case law documents and contracts. DAPT adapts the model's internal representations to legal syntax and semantics without requiring labeled data, dramatically improving downstream performance on tasks like contract clause extraction and case outcome prediction.
Citation Masking
A specialized pre-processing strategy where legal citations are replaced with special tokens during MLM pre-training. Instead of predicting '347 U.S. 483', the model sees [CITE] and must learn the contextual function of the authority—why a case is cited—rather than memorizing specific citation strings. This technique prevents the model from hallucinating plausible but incorrect citations and encourages deeper reasoning about precedential relationships.
Legal Perplexity
An intrinsic evaluation metric measuring how surprised a language model is by a held-out legal text. A lower perplexity indicates the model has better internalized the statistical patterns of legal language. For MLM-based models, perplexity is computed over the masked tokens only. While useful for monitoring pre-training progress, low perplexity does not guarantee good downstream performance—a model may perfectly predict legal syntax while failing at complex reasoning tasks.
FlashAttention
An IO-aware exact attention algorithm that dramatically accelerates the self-attention mechanism central to MLM architectures. By optimizing memory reads and writes between GPU high-bandwidth memory and on-chip SRAM, FlashAttention reduces the quadratic memory complexity of attention. This makes it feasible to pre-train legal encoders on longer sequence lengths, enabling models to process entire multi-page contracts or judicial opinions in a single forward pass without truncation.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us