Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Workflow:Huggingface Peft Prompt Tuning Classification

From Leeroopedia



Knowledge Sources
Domains NLP, Fine_Tuning, Classification, Prompt_Learning
Last Updated 2026-02-07 06:00 GMT

Overview

End-to-end process for fine-tuning a sequence classification model using non-LoRA PEFT methods (Prompt Tuning, Prefix Tuning, or P-Tuning) that prepend trainable virtual tokens to the input rather than modifying model weights.

Description

This workflow demonstrates the prompt-based family of PEFT methods for text classification tasks. Unlike LoRA which injects trainable matrices into existing layers, these methods add trainable virtual tokens (soft prompts) to the input sequence. Prompt Tuning learns a continuous embedding for each virtual token. Prefix Tuning prepends trainable key-value pairs to every attention layer. P-Tuning uses a learnable prompt encoder (LSTM or MLP) to generate the virtual token embeddings. All three methods keep the entire base model frozen and train only the prompt parameters, which typically represent fewer than 0.1% of total parameters. The workflow uses Accelerate for distributed training support.

Usage

Execute this workflow when you want to adapt a pre-trained model for text classification (e.g., sentiment analysis, natural language inference, paraphrase detection) using extremely few trainable parameters. These methods are particularly well-suited when you need to deploy many task-specific adapters for a single base model, as each adapter is just a small set of virtual token embeddings. They also serve as a good baseline comparison against LoRA-based approaches.

Execution Steps

Step 1: Select and Configure PEFT Method

Choose between Prompt Tuning, Prefix Tuning, or P-Tuning based on the use case, and create the corresponding configuration. Specify the number of virtual tokens and the task type (SEQUENCE_CLASSIFICATION). For P-Tuning, configure the prompt encoder type (LSTM or MLP) and hidden size. For Prefix Tuning, the virtual tokens are prepended at every attention layer rather than just the input.

Key considerations:

  • Prompt Tuning: simplest method, learns embeddings for virtual tokens prepended to input
  • Prefix Tuning: prepends trainable key-value pairs at every attention layer (more expressive)
  • P-Tuning: uses a neural network (LSTM/MLP) to generate virtual token embeddings
  • Number of virtual tokens (typically 10-30) controls the capacity of the prompt

Step 2: Load Model and Tokenizer

Load a pre-trained model for sequence classification with the appropriate number of labels, and load the corresponding tokenizer. Initialize the Accelerator for distributed training support with DDP-specific settings (find_unused_parameters must be enabled for prompt-based methods). Wrap the model with get_peft_model to apply the selected PEFT configuration.

Key considerations:

  • Use AutoModelForSequenceClassification with the correct num_labels
  • Enable find_unused_parameters in DDP kwargs for prompt-based methods
  • The classification head is trained alongside the virtual tokens
  • FSDP auto-wrap policy can be configured for large-scale training

Step 3: Prepare Classification Dataset

Load the classification dataset (e.g., GLUE MRPC for paraphrase detection) and tokenize it with padding and truncation. Create DataLoaders for training and evaluation splits. Load the appropriate evaluation metric (e.g., accuracy, F1) from the evaluate library.

Key considerations:

  • Tokenize both sentence pairs (for tasks like NLI or paraphrase detection)
  • Pad to the maximum length in the batch or to a fixed max_length
  • The evaluation metric should match the benchmark (e.g., GLUE metrics)
  • Remove unnecessary columns from the dataset after tokenization

Step 4: Train the Prompt Parameters

Set up the optimizer (AdamW) and learning rate scheduler (linear warmup). Run a manual training loop using Accelerate: forward pass through the model, compute classification loss, backward pass, optimizer step. The virtual token parameters and classification head are the only trainable components. Track training loss across steps.

Key considerations:

  • Only virtual token embeddings and the classification head receive gradients
  • Learning rate for prompt methods is typically higher (1e-3) than for LoRA
  • Linear schedule with warmup is standard for classification tasks
  • Accelerate handles device placement and gradient synchronization

Step 5: Evaluate and Save

After each training epoch, evaluate the model on the validation set by gathering predictions across all distributed processes. Compute the evaluation metric (accuracy, F1, etc.) and log the results. After training completes, save the trained prompt parameters and tokenizer. The saved checkpoint is extremely small (often just a few KB) since it contains only the virtual token embeddings.

Key considerations:

  • Use accelerator.gather for collecting predictions across processes
  • The saved prompt checkpoint is typically a few KB (much smaller than LoRA adapters)
  • The prompt can be loaded and applied to the same base model for inference
  • Compare results across Prompt Tuning, Prefix Tuning, and P-Tuning to select the best method

Execution Diagram

GitHub URL

Workflow Repository