MAGNET: Augmenting Generative Decoders with Representation Learning and Infilling Capabilities

Savya Khosla, Aditi Tiwari, Kushal Kafle, Simon Jenni, Handong Zhao, John P. Collomosse, Jing Shi

Abstract

Modern Large Language Models (LLMs) primarily utilize decoder-only architectures, which excel in unidirectional text generation but struggle with tasks requiring bidirectional context, such as representation learning and text infilling. We present MAGNET (Modified Attention for Generation and Encoding of Text), a framework designed to transform standard decoder-only models into versatile multitaskers. By implementing a hybrid attention mechanism and a tripartite training strategy—comprising Masked Next Token Prediction (MNTP), Self-Supervised Contrastive Learning (SSCL), and Missing Span Generation (MSG)—MAGNET enables models like Llama-2 to function as high-quality encoders without compromising their generative capabilities or falling victim to common pitfalls like the repetition problem.

Introduction

The dominance of decoder-only architectures (e.g., GPT-4, Llama) in the current AI landscape is driven by their superior performance in autoregressive text generation. However, these models process text unidirectionally, which is inherently suboptimal for tasks that require a holistic understanding of context, such as Named Entity Recognition (NER), sentiment analysis, or semantic similarity. Historically, researchers have relied on encoder-only models like BERT for these tasks, but these models lack the ability to generate coherent long-form text.

Previous attempts to bridge this gap by adapting decoders into encoders have often resulted in a "repetition problem," where the model's generative performance degrades, causing it to loop phrases indefinitely. MAGNET addresses this trade-off, providing a unified architecture that excels at both understanding (encoding) and creating (generating/infilling) text.

Methodology

MAGNET introduces two primary innovations to the standard transformer architecture: a modified attention mechanism and a unified fine-tuning objective.

Hybrid Attention Mechanism

To support both encoding and generation, MAGNET categorizes tokens into two groups:

  1. Context Tokens: These utilize bidirectional attention, allowing each token to attend to every other context token in the sequence. This mimics the behavior of an encoder.
  2. Span Tokens: These utilize causal (unidirectional) attention, where tokens only attend to preceding tokens. This preserves the autoregressive nature required for generation.

Unified Training Objectives

MAGNET is fine-tuned using three distinct but complementary objectives:

  • Masked Next Token Prediction (MNTP): Unlike standard BERT masking (MTP), which predicts a masked token at its own position, MNTP predicts the masked token from the preceding position. This alignment with the original pre-training of decoders makes the transition to bidirectional processing more efficient.
  • Self-Supervised Contrastive Learning (SSCL): This objective aligns sentence-level representations by ensuring that a sentence and its paraphrase are mapped to similar points in the vector space, enhancing the model's utility for embedding tasks.
  • Missing Span Generation (MSG): This task trains the model to fill in missing segments of text. Crucially, MSG acts as a regularizer that prevents the model from losing its generative "muscles" during the adaptation process.

Experimental Setup

The authors evaluated the framework by fine-tuning a Llama-2-7B model. The training data included Wikitext-103 for representation and infilling tasks, and SlimPajama to ensure the retention of general world knowledge. Evaluation spanned several benchmarks:

  • Word-level tasks: CoNLL-2003 for NER and POS tagging.
  • Sentence-level tasks: Massive Text Embedding Benchmark (MTEB) and Semantic Textual Similarity (STS).
  • Infilling and Reasoning: ROC Stories and standard LLM benchmarks like MMLU and HellaSwag.

Key Results and Discussion

Superior Encoding and Embeddings

MAGNET-adapted Llama-2-7B demonstrated significant improvements over both the base model and specialized encoders. On NER tasks, MAGNET achieved a score of 98.31, outperforming BERT-Large (97.16). In the realm of Semantic Textual Similarity (STS), MAGNET reached an average score of 79.36, a substantial leap from the base Llama-2 (65.25) and the previous state-of-the-art adaptation method, LLM2Vec (75.92).

High-Quality Infilling

The model's ability to "fill in the blanks" improved dramatically. Infilling perplexity on ROC Stories dropped from 13.9 to 9.5. Human evaluations confirmed these metrics, with 62.0% of MAGNET's infills being judged as contextually appropriate compared to 53.5% for the base model.

Preservation of Generative Reasoning

A critical success of MAGNET is the mitigation of the repetition problem. By keeping the autoregressive generation objective active via MSG, the model avoids the infinite loops common in other decoder-to-encoder adaptations. Furthermore, MAGNET maintained nearly identical performance to the base Llama-2 on general reasoning benchmarks (MMLU, HellaSwag), suggesting that the additional capabilities do not come at the cost of "catastrophic forgetting."

Conclusion and Future Work

MAGNET demonstrates that representation learning and text generation are not mutually exclusive but are, in fact, synergistic. The inclusion of the infilling objective (MSG) was found to actually improve performance on word-level encoding tasks.

While MAGNET represents a significant step forward, limitations remain. There is a slight "generation tax," with next-token prediction perplexity increasing from 6.4 to 7.6 on Wikitext-103. Additionally, the model's infilling performance declines for sequences longer than 128 tokens. Future research will focus on scaling MAGNET to larger model sizes (70B+) and exploring the integration of these objectives directly into the initial pre-training phase of LLMs.

Summary updated

BibTeX
@inproceedings{khosla2025magnet,
  author = {Savya Khosla and Aditi Tiwari and Kushal Kafle and Simon Jenni and Handong Zhao and John P. Collomosse and Jing Shi},
  title = {{MAGNET: Augmenting Generative Decoders with Representation Learning and Infilling Capabilities}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure