Contrastive Instruction Tuning

Tianyi Yan, Fei Wang, James Y. Huang, Wenxuan Zhou, Fan Yin, Aram Galstyan, Wenpeng Yin, Muhao Chen

Introduction

Large language models (LLMs) have demonstrated remarkable capabilities in following human instructions. However, their performance often remains brittle; a slight change in phrasing, a typo, or the use of a synonym can lead to significantly different or even incorrect outputs. This vulnerability to textual variations poses a major challenge for the reliability of LLMs in real-world applications. To address this, we present Contrastive Instruction Tuning (COIN), a novel training methodology that employs contrastive learning to align the internal representations of semantically equivalent instructions, thereby fostering greater model robustness and consistency.

The Problem: Brittle Instruction Following

Standard instruction tuning typically focuses on maximizing the likelihood of a target output given a specific input prompt. While effective for teaching task execution, this approach does not explicitly teach the model that different linguistic expressions can represent the same underlying intent. Consequently, if a user asks a model to "Identify the sentiment of this sentence" versus "Please evaluate the sentiment of the following text," the model may succeed on one and fail on the other. This inconsistency limits the trustworthiness of LLMs, as they lack a fundamental understanding of semantic invariance across prompt perturbations.

The COIN Methodology

COIN introduces a contrastive learning objective integrated with standard autoregressive language modeling. The goal is to ensure that the model maps varied but semantically identical instructions to similar regions in its representation space.

1. Positive and Hard Negative Sampling

To implement contrastive learning effectively, the choice of pairs is critical:

  • Positive Samples (Semantically Equivalent): For every training instance, COIN generates a positive pair by paraphrasing the instruction while keeping the input data and target output constant.
  • Hard Negative Samples (Semantically Different): Unlike standard contrastive learning that uses random batch samples as negatives, COIN utilizes "near-OOD" (Out-of-Distribution) samples. It pairs the original instruction with a different input and a different output label. This forces the model to distinguish between the instruction's structure and the specific context/intent of the task.

2. Representation Alignment and Loss Function

COIN extracts the hidden state of the decoder's last token to serve as the representation for the instruction-instance pair. It then applies an InfoNCE loss to minimize the distance between positive pairs in the embedding space while maximizing the distance from hard negatives.

To ensure that this auxiliary task does not degrade the model's generative capabilities, COIN dynamically scales the contrastive loss to match the magnitude of the standard cross-entropy generation loss, governed by a hyperparameter ($\lambda$).

Experimental Setup

Training Data

The authors utilized a subset of the FLAN Collection, selecting 25 datasets (52k entries). This was augmented by generating 104k contrastive instructions through paraphrasing, creating a robust dataset for contrastive alignment.

Evaluation Framework

The model, based on the Alpaca/LLaMA architecture, was evaluated using the PromptBench benchmark across 10 GLUE datasets. Robustness was tested against four distinct types of perturbations:

  1. Character-level: Typos and misspellings.
  2. Word-level: Synonym substitutions.
  3. Sentence-level: Appending distracting or random strings.
  4. Semantic-level: General paraphrasing.

Key Insights and Results

Improved Accuracy and Robustness

COIN demonstrated a clear advantage over standard instruction tuning, achieving an average accuracy boost of +2.5% on unseen, perturbed instructions. The most significant gains were observed in the face of word-level noise (synonym swaps), where accuracy improved by up to +4.4%.

Task-Specific Performance

The method proved particularly effective for tasks requiring deep linguistic understanding. Performance on Paraphrase Identification increased by +5.4%, and Grammar Correctness saw an improvement of +6.3%. These results suggest that contrastive loss helps the model better grasp nuanced syntactic and semantic relationships.

Consistency and Variance Reduction

Beyond raw accuracy, COIN significantly improved the reliability of model outputs. The standard deviation of performance across different prompt variations dropped by 1.2% to 2.5%. This reduction in variance indicates that the model's internal representations are more stable, leading to more predictable behavior regardless of how a user phrases a prompt.

Representation Analysis

UMAP visualizations of the model's hidden states confirmed the underlying mechanism: COIN physically shifts the representations of perturbed prompts closer to their "clean" counterparts. By seeing these variations as internally identical, the model processes them with consistent logic.

Conclusion and Future Work

Contrastive Instruction Tuning (COIN) offers a powerful solution to the brittleness of modern LLMs. By explicitly training models to recognize semantic equivalence through hard negative sampling and representation alignment, COIN enhances both accuracy and consistency.

Future research may explore expanding this contrastive framework to other prompt elements, such as system prompts and few-shot examples, or applying these principles to multimodal models to ensure robustness across vision and language tasks.

Summary updated

BibTeX
@inproceedings{yan2024contrastive,
  author = {Tianyi Yan and Fei Wang and James Y. Huang and Wenxuan Zhou and Fan Yin and Aram Galstyan and Wenpeng Yin and Muhao Chen},
  title = {{Contrastive Instruction Tuning}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.613},
}
Figure