CoolFace
Modelpublic

sourize/phi2-memory-deeptalks

sourceHugging Facemitupdated 1y agoView on Hugging Face
1likes5downloads
Model Card

phi2-memory-deeptalks

A LoRA adapter for the Phi-2 language model, fine-tuned on short conversational snippets to provide short-term memory in dialogue. This adapter enables your assistant to recall and leverage the last few user/assistant turnsβ€”without full fine-tuning of the 2.7 B-parameter base model.

<p align="center"> <a href="https://huggingface.co/spaces/sourize/DeepTalks"> πŸ”— Live Demo on Hugging Face Spaces </a> </p> <p align="center"> ⏳ It takes time to generate responses since it's running on the CPU free tier </p>


πŸš€ Overview

phi2-memory-deeptalks injects lightweight, low-rank corrections into the attention and MLP layers of microsoft/phi-2.

  • β€”Size: ~6 M trainable parameters (β‰ˆ 0.2 % of the base model)
  • β€”Base: Phi-2 (2.7 B parameters)
  • β€”Adapter: Low-Rank Adaptation (LoRA) via the PEFT library

πŸ“¦ Model Details

Architecture & Adapter Configuration

  • β€”Base model: microsoft/phi-2 (causal-LM)
  • β€”LoRA rank (r): 4
  • β€”Modules wrapped:
  • β€”Attention projections: q_proj, k_proj, v_proj, dense
  • β€”MLP layers: fc1, fc2
  • β€”LoRA hyperparameters:
  • β€”lora_alpha: 32
  • β€”lora_dropout: 0.05
  • β€”Trainable params: ~5.9 M

Training Data & Preprocessing

  • β€”Dataset: HyperThink-Mini 50 K (7 % used)
  • β€”Prompt format:
text
  ### Human:
  <user message>

  ### Assistant:
  <assistant response>
  • β€”Tokenization: Truncated/padded to 256 tokens, labels = input_ids
  • β€”Optimizer: AdamW (PyTorch), FP16 on GPU
  • β€”Batching: per_device_train_batch_size=1 + gradient_accumulation_steps=8
  • β€”Epochs: 3
  • β€”Checkpointing: Save every 500 steps; final adapter weights in adapter_model.safetensors

🎯 Evaluation

  • β€”Training loss (step 500): ~1.08
  • β€”Validation loss: ~1.10
  • β€”Qualitative:
  • β€”Improved recall of the last 2–4 turns in dialogue
  • β€”Maintains base Phi-2 fluency on general language

πŸ”§ Usage

Load the adapter into your Phi-2 model with just a few lines:

python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel, LoraConfig

# 1) Load base
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", padding_side="left")
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")

# 2) Apply LoRA adapter
peft_config = LoraConfig.from_pretrained("sourize/phi2-memory-deeptalks")
model = PeftModel.from_pretrained(model, peft_config)

# 3) (Optional) Resize embeddings
model.base_model.resize_token_embeddings(len(tokenizer))

# 4) Generate
prompt = "### Human:\nHello, how are you?\n\n### Assistant:"
inputs = tokenizer(prompt, return_tensors="pt")
output = model.generate(**inputs, max_new_tokens=64)
print(tokenizer.decode(output[0], skip_special_tokens=True))

βš™οΈ Inference & Deployment

  • β€”Preferred: GPU (NVIDIA-CUDA) for sub-second latency
  • β€”CPU-only: ~7–10 min per response (large model!)
  • β€”Hugging Face Inference API:
bash
  curl -X POST \
    -H "Authorization: Bearer $HF_TOKEN" \
    -H "Content-Type: application/json" \
    https://api-inference.huggingface.co/pipeline/text-generation/sourize/phi2-memory-deeptalks \
    -d '{
      "inputs": "Hello, how are you?",
      "parameters": {
        "max_new_tokens": 64,
        "do_sample": true,
        "temperature": 0.7,
        "top_p": 0.9,
        "return_full_text": false
      }
    }'

πŸ’‘ Use Cases & Limitations

  • β€”Ideal for:
  • β€”Short back-and-forth chats (2–4 turns)
  • β€”Chatbots that need to β€œremember” very recent context
  • β€”Not suited for:
  • β€”Long-term memory or document-level retrieval
  • β€”High-volume production on CPU (too slow)

πŸ“– Further Reading


πŸ”– Citation

bibtex
@misc{sourize_phi2_memory_deeptalks,
  title        = {phi2-memory-lora: LoRA adapter for Phi-2 with short-term conversational memory},
  author       = {Sourish},
  year         = {2025},
  howpublished = {\url{https://huggingface.co/sourize/phi2-memory-deeptalks}},
  license      = {MIT}
}

Questions or feedback? Please open an issue on the [repository](https://huggingface.co/sourize/phi2-memory-deeptalks).