Lora sequence classification

From wenmin-wu/ds-skills · View interactive version

Lora sequence classification is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Load a pretrained LLM with LoRA adapter via PEFT for memory-efficient fine-tuned sequence classification

How to use this skill

Skill file

LoRA Sequence Classification

Overview

Fine-tuning a full LLM (7B+ params) for classification is expensive. LoRA (Low-Rank Adaptation) freezes the base model and trains small rank-decomposed weight matrices (~0.1% of params). Load the base model for sequence classification, then apply a trained LoRA adapter via PEFT. Enables 7-9B model inference on a single GPU with fp16/bf16.

Quick Start

import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from peft import PeftModel

# Load base model with classification head
model = AutoModelForSequenceClassification.from_pretrained(
    "google/gemma-2-9b-it",
    num_labels=n_classes,
    torch_dtype=torch.bfloat16,
    device_map="auto",
)

# Apply LoRA adapter
model = PeftModel.from_pretrained(model, "path/to/lora-adapter")
model.eval()

# Inference
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-9b-it")
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
    logits = model(**inputs.to(model.device)).logits
probs = torch.softmax(logits, dim=-1)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.