Hard Prompts Made Easy: Gradient-Based Discrete Optimization for Prompt Tuning and Discovery

Yuxin Wen, Neel Jain, John Kirchenbauer, Micah Goldblum, Jonas Geiping, Tom Goldstein

Abstract

Prompt engineering has traditionally been a manual, iterative process characterized by trial and error. While "soft prompts" (continuous vectors) allow for efficient gradient-based optimization, they lack interpretability and portability across different models. This paper introduces PEZ (Hard Prompts made EaZy), a method that enables gradient-based optimization for "hard prompts"—discrete, human-readable text strings. By projecting continuous embeddings onto the discrete token space during optimization, PEZ discovers concise and effective prompts for image generation, style transfer, and language classification that are more portable than previous continuous alternatives.

Introduction

In the current landscape of Large Language Models (LLMs) and Diffusion models, steering model behavior is typically achieved through two distinct methods. Hard Prompts consist of actual strings of text (e.g., "a cat in a hat"). They are interpretable and portable across models but are difficult to optimize systematically. Conversely, Soft Prompts utilize continuous mathematical vectors. While these can be optimized perfectly using gradients, they are essentially "black boxes"—unreadable by humans and incompatible with different model architectures or text-only APIs.

The central challenge addressed by this research is bridging the gap: utilizing the efficiency of gradient-based calculus to discover discrete words. Previous attempts, such as AutoPrompt, were computationally prohibitive due to their reliance on greedy searches or the evaluation of thousands of candidates per step. PEZ offers a more efficient alternative inspired by techniques in neural network quantization.

The PEZ Algorithm

The PEZ algorithm is designed to navigate the discrete space of a model's vocabulary using continuous optimization. The process follows a specific iterative cycle:

  1. Continuous Iterates: The algorithm maintains a set of continuous embeddings, similar to a standard soft prompt.
  2. Forward Projection: In each optimization step, these continuous vectors are projected to their nearest neighbors in the model's vocabulary, effectively turning them into "hard" tokens.
  3. Gradient Update: The loss function is calculated based on these discrete tokens. Crucially, the resulting gradient update is applied back to the underlying continuous vectors.
  4. Convergence: After a set number of iterations, the continuous vectors settle near specific discrete tokens, which are then extracted as the final hard prompt.

This approach allows the optimization process to "explore" the discrete word space with the speed and efficiency of gradient descent.

Experimental Evaluation

The researchers evaluated PEZ across diverse domains to test its versatility:

Image Generation and Inversion

Using datasets such as LAION, MS COCO, Celeb-A, and Lexica.art, the authors tested PEZ's ability to perform "image inversion"—finding the specific text prompt that would generate an existing image. PEZ achieved CLIP scores (measuring image-text alignment) comparable to the popular "CLIP Interrogator" but with significantly higher efficiency, using only 8 tokens compared to the standard 77.

Language Classification

To test NLP performance, PEZ was applied to sentiment analysis (SST-2, Amazon Polarity) and news classification (AGNEWS). The goal was to see if optimized prompts could transform a frozen LLM into a high-performing classifier. In few-shot settings (with only 2 examples per class), PEZ discovered prompts that achieved over 70% accuracy on news classification.

Key Insights

Portability and Distillation

A primary advantage of PEZ is portability. Unlike soft prompts, which are tied to a specific model's internal architecture, hard prompts discovered by PEZ on a small model (e.g., GPT-2) can be successfully transferred to much larger models (e.g., OPT-6.7B). Additionally, PEZ acts as a tool for prompt distillation, taking long, descriptive human prompts and condensing them into 3 or 4 "power tokens" that produce nearly identical results, suggesting that models often prioritize specific semantic anchors over grammatical structure.

The Nature of Optimized Tokens

While the resulting prompts consist of real words, they are often non-grammatical or "gibberish" to human readers. For example, a prompt for a "milky way campfire" might include the sub-word token ntvmilkyway. These tokens often capture semantic nuances that standard English words miss, providing a window into how the model internalizes concepts.

Safety and Adversarial Use

The research also highlighted potential risks. PEZ can be used to bypass safety filters by finding "synonym" prompts for banned content. In one experiment, the authors successfully generated the "Afghan Girl" photo even when the word "Afghan" was blacklisted, by optimizing for tokens that triggered the same visual representation. This suggests that safety measures must evolve from simple word-based filters to more robust feature-based detection.

Conclusion and Future Work

PEZ demonstrates that discrete prompt discovery does not have to be a manual or computationally expensive task. By projecting continuous gradients onto discrete vocabularies, PEZ provides a powerful tool for model steering that maintains both interpretability and portability. Future work will likely focus on improving the fluency of these prompts without sacrificing performance and exploring the mathematical geometry of embedding spaces to further refine the optimization process.

Summary updated

BibTeX
@inproceedings{wen2023hard,
  author = {Yuxin Wen and Neel Jain and John Kirchenbauer and Micah Goldblum and Jonas Geiping and Tom Goldstein},
  title = {{Hard Prompts Made Easy: Gradient-Based Discrete Optimization for Prompt Tuning and Discovery}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=VOstHxDdsN},
}
Figure