| name | huggingface-transformers-nlp |
| metadata | {"category":"NLP Audio and Speech AI"} |
| description | Build, fine-tune, and deploy enterprise NLP solutions using Hugging Face transformers, datasets, accelerate, and peft. Triggers when implementing QLoRA / LoRA fine-tuning, sequence classification, token classification (NER), SFTTrainer, DPO, vLLM serving, FlashAttention-2, or model quantization (BitsAndBytes, AWQ, GPTQ). |
| compatibility | Python (>= 3.9), PyTorch (>= 2.1), transformers (>= 4.38.0), peft, datasets, accelerate, vLLM |
Hugging Face Transformers & Enterprise NLP
Production patterns for parameter-efficient LLM fine-tuning (QLoRA), sequence classification, named entity recognition (NER), and high-throughput inference serving.
1. NLP Architecture & Fine-Tuning Workflow
+-------------------+ +----------------------------------+ +---------------------------+
| Raw Text Dataset | ---> | HF Datasets & Tokenizer | ---> | 4-bit Quantized Base Model|
| (JSONL / Parquet) | | (Dynamic Padding & Chunking) | | (BitsAndBytes NF4 Config) |
+-------------------+ +----------------------------------+ +---------------------------+
|
v
+-------------------+ +----------------------------------+ +---------------------------+
| Deployed LLM API | <--- | Merge LoRA Adapters | <--- | PEFT LoRA Fine-Tuning |
| (vLLM / TGI Engine| | (Base Model + Adapter Weights) | | (SFTTrainer / Accelerate) |
+-------------------+ +----------------------------------+ +---------------------------+
2. QLoRA Parameter-Efficient LLM Fine-Tuning (qlora_finetune.py)
Memory-efficient 4-bit quantized fine-tuning using peft, bitsandbytes, and trl's SFTTrainer.
import os
import torch
from datasets import load_dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
TrainingArguments
)
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from trl import SFTTrainer
def train_qlora_llm():
MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
OUTPUT_DIR = "./qlora_llama3_output"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True
)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True,
attn_implementation="flash_attention_2"
)
model = prepare_model_for_kbit_training(model)
peft_config = LoraConfig(
r=16,
lora_alpha=32,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM"
)
dataset = load_dataset(, data_files={: })
training_args = TrainingArguments(
output_dir=OUTPUT_DIR,
per_device_train_batch_size=,
gradient_accumulation_steps=,
learning_rate=,
logging_steps=,
num_train_epochs=,
optim=,
fp16=,
bf16=,
max_grad_norm=,
warmup_ratio=,
lr_scheduler_type=,
save_strategy=,
report_to=
)
trainer = SFTTrainer(
model=model,
train_dataset=dataset[],
peft_config=peft_config,
dataset_text_field=,
max_seq_length=,
tokenizer=tokenizer,
args=training_args
)
trainer.train()
trainer.model.save_pretrained(os.path.join(OUTPUT_DIR, ))
tokenizer.save_pretrained(os.path.join(OUTPUT_DIR, ))
()
__name__ == :
train_qlora_llm()
3. Sequence Classification & Named Entity Recognition (NER) (ner_pipeline.py)
A production pipeline for extracting domain entities (e.g. PER, ORG, LOC, MEDICINE) using RoBERTa.
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
class NamedEntityExtractor:
def __init__(self, model_checkpoint: str = "dslim/bert-base-NER"):
self.tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
self.model = AutoModelForTokenClassification.from_pretrained(model_checkpoint)
self.nlp_pipeline = pipeline(
"ner",
model=self.model,
tokenizer=self.tokenizer,
aggregation_strategy="simple",
device=0 if torch.cuda.is_available() else -1
)
def extract_entities(self, text: str) -> list[dict]:
entities = self.nlp_pipeline(text)
formatted = []
for ent in entities:
formatted.append({
"entity_group": ent["entity_group"],
"word": ent["word"],
"score": round(float(ent["score"]), 4),
"start": ent["start"],
"end": ent["end"]
})
formatted
__name__ == :
extractor = NamedEntityExtractor()
results = extractor.extract_entities()
(results)
4. High-Throughput LLM Serving via vLLM (serve_vllm.py)
Deploy fine-tuned or open-source LLMs using vLLM for PagedAttention key-value cache optimization.
from vllm import LLM, SamplingParams
def run_vllm_batch_inference():
prompts = [
"System: You are an AI legal expert.\nUser: Summarize the non-disclosure agreement obligations.\nAssistant:",
"System: You are an AI code reviewer.\nUser: Explain Python memory management GIL.\nAssistant:"
]
llm = LLM(
model="meta-llama/Meta-Llama-3-8B-Instruct",
tensor_parallel_size=1,
gpu_memory_utilization=0.90,
max_model_len=4096
)
sampling_params = SamplingParams(
temperature=0.7,
top_p=0.95,
max_tokens=512
)
outputs = llm.generate(prompts, sampling_params)
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}")
print(f"Generated: {generated_text!r}\n")
if __name__ == "__main__":
run_vllm_batch_inference()
5. Best Practices & Hardware Optimization
- FlashAttention-2: Always pass
attn_implementation="flash_attention_2" during model loading on Ampere/Hopper GPUs to reduce attention memory complexity from $O(N^2)$ to $O(N)$.
- Gradient Accumulation: Match hardware VRAM constraints by lowering batch size (
per_device_train_batch_size=2) and scaling up gradient_accumulation_steps=8.
- Model Merging: Merge LoRA adapter weights into base model weights using
model.merge_and_unload() before serving via vLLM engines to avoid runtime adapter attachment latency.