Spaces:
Paused
Paused
feat: Establish initial kLLM project structure with core configurations, dependencies, and Docker setup.
Browse files- Dockerfile +38 -0
- requirements.txt +34 -0
- src/__init__.py +2 -0
- src/config.py +145 -0
- src/data/__init__.py +1 -0
- src/inference/__init__.py +1 -0
- src/training/__init__.py +1 -0
Dockerfile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install system dependencies
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
git \
|
| 9 |
+
git-lfs \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/* \
|
| 11 |
+
&& git lfs install
|
| 12 |
+
|
| 13 |
+
# Copy requirements first for caching
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
|
| 16 |
+
# Install Python dependencies
|
| 17 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
+
|
| 19 |
+
# Copy application code
|
| 20 |
+
COPY . .
|
| 21 |
+
|
| 22 |
+
# Create necessary directories
|
| 23 |
+
RUN mkdir -p /app/checkpoints /app/data
|
| 24 |
+
|
| 25 |
+
# Set environment variables
|
| 26 |
+
ENV PYTHONUNBUFFERED=1
|
| 27 |
+
ENV GRADIO_SERVER_NAME=0.0.0.0
|
| 28 |
+
ENV GRADIO_SERVER_PORT=7860
|
| 29 |
+
|
| 30 |
+
# Expose port for Gradio
|
| 31 |
+
EXPOSE 7860
|
| 32 |
+
|
| 33 |
+
# Health check
|
| 34 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
| 35 |
+
CMD curl -f http://localhost:7860/ || exit 1
|
| 36 |
+
|
| 37 |
+
# Run the Gradio app
|
| 38 |
+
CMD ["python", "app.py"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# kLLM - Custom LLM Training & Inference Dependencies
|
| 2 |
+
# Hugging Face Ecosystem
|
| 3 |
+
transformers>=4.36.0
|
| 4 |
+
datasets>=2.16.0
|
| 5 |
+
accelerate>=0.25.0
|
| 6 |
+
huggingface_hub>=0.20.0
|
| 7 |
+
|
| 8 |
+
# Training with RLHF
|
| 9 |
+
trl>=0.7.0
|
| 10 |
+
peft>=0.7.0
|
| 11 |
+
|
| 12 |
+
# Quantization & Memory Optimization
|
| 13 |
+
bitsandbytes>=0.41.0
|
| 14 |
+
|
| 15 |
+
# Deep Learning
|
| 16 |
+
torch>=2.1.0
|
| 17 |
+
safetensors>=0.4.0
|
| 18 |
+
|
| 19 |
+
# Web Interface & API
|
| 20 |
+
gradio>=4.0.0
|
| 21 |
+
fastapi>=0.104.0
|
| 22 |
+
uvicorn>=0.24.0
|
| 23 |
+
|
| 24 |
+
# Configuration
|
| 25 |
+
pyyaml>=6.0
|
| 26 |
+
python-dotenv>=1.0.0
|
| 27 |
+
|
| 28 |
+
# Monitoring (optional)
|
| 29 |
+
wandb>=0.16.0
|
| 30 |
+
tensorboard>=2.15.0
|
| 31 |
+
|
| 32 |
+
# Utilities
|
| 33 |
+
numpy>=1.24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|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
kLLM Configuration
|
| 3 |
+
Central configuration for model training and inference.
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
from dataclasses import dataclass, field
|
| 7 |
+
from typing import Optional, List
|
| 8 |
+
|
| 9 |
+
# Hugging Face Configuration
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 11 |
+
HF_USERNAME = "imkrish"
|
| 12 |
+
MODEL_REPO = f"{HF_USERNAME}/kLLM"
|
| 13 |
+
CHECKPOINT_REPO = f"{HF_USERNAME}/kLLM-checkpoints"
|
| 14 |
+
|
| 15 |
+
# Base Model Options
|
| 16 |
+
BASE_MODELS = {
|
| 17 |
+
"tinyllama": "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 18 |
+
"phi2": "microsoft/phi-2",
|
| 19 |
+
"mistral": "mistralai/Mistral-7B-Instruct-v0.2",
|
| 20 |
+
"llama2": "meta-llama/Llama-2-7b-chat-hf",
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# Default base model
|
| 24 |
+
DEFAULT_BASE_MODEL = "tinyllama"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@dataclass
|
| 28 |
+
class ModelConfig:
|
| 29 |
+
"""Model architecture configuration."""
|
| 30 |
+
base_model: str = BASE_MODELS[DEFAULT_BASE_MODEL]
|
| 31 |
+
model_name: str = "kLLM"
|
| 32 |
+
max_length: int = 2048
|
| 33 |
+
load_in_4bit: bool = True
|
| 34 |
+
load_in_8bit: bool = False
|
| 35 |
+
trust_remote_code: bool = True
|
| 36 |
+
use_flash_attention: bool = False
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@dataclass
|
| 40 |
+
class LoRAConfig:
|
| 41 |
+
"""LoRA (Low-Rank Adaptation) configuration for efficient fine-tuning."""
|
| 42 |
+
r: int = 16 # LoRA rank
|
| 43 |
+
lora_alpha: int = 32 # LoRA alpha
|
| 44 |
+
lora_dropout: float = 0.05
|
| 45 |
+
bias: str = "none"
|
| 46 |
+
task_type: str = "CAUSAL_LM"
|
| 47 |
+
target_modules: List[str] = field(default_factory=lambda: [
|
| 48 |
+
"q_proj", "k_proj", "v_proj", "o_proj",
|
| 49 |
+
"gate_proj", "up_proj", "down_proj"
|
| 50 |
+
])
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@dataclass
|
| 54 |
+
class SFTConfig:
|
| 55 |
+
"""Supervised Fine-Tuning configuration."""
|
| 56 |
+
output_dir: str = "./checkpoints/sft"
|
| 57 |
+
num_train_epochs: int = 3
|
| 58 |
+
per_device_train_batch_size: int = 4
|
| 59 |
+
per_device_eval_batch_size: int = 4
|
| 60 |
+
gradient_accumulation_steps: int = 4
|
| 61 |
+
learning_rate: float = 2e-4
|
| 62 |
+
weight_decay: float = 0.01
|
| 63 |
+
warmup_ratio: float = 0.03
|
| 64 |
+
lr_scheduler_type: str = "cosine"
|
| 65 |
+
max_seq_length: int = 2048
|
| 66 |
+
|
| 67 |
+
# Logging & Saving
|
| 68 |
+
logging_steps: int = 10
|
| 69 |
+
save_strategy: str = "epoch" # Save every epoch
|
| 70 |
+
save_total_limit: int = 5
|
| 71 |
+
evaluation_strategy: str = "epoch"
|
| 72 |
+
|
| 73 |
+
# Push to Hub
|
| 74 |
+
push_to_hub: bool = True
|
| 75 |
+
hub_model_id: str = f"{HF_USERNAME}/kLLM-sft"
|
| 76 |
+
hub_token: str = HF_TOKEN
|
| 77 |
+
|
| 78 |
+
# Mixed precision
|
| 79 |
+
fp16: bool = False
|
| 80 |
+
bf16: bool = True # Better for modern GPUs
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@dataclass
|
| 84 |
+
class RewardConfig:
|
| 85 |
+
"""Reward Model configuration for RLHF."""
|
| 86 |
+
output_dir: str = "./checkpoints/reward"
|
| 87 |
+
num_train_epochs: int = 1
|
| 88 |
+
per_device_train_batch_size: int = 4
|
| 89 |
+
gradient_accumulation_steps: int = 4
|
| 90 |
+
learning_rate: float = 1e-5
|
| 91 |
+
|
| 92 |
+
# Save settings
|
| 93 |
+
save_strategy: str = "epoch"
|
| 94 |
+
push_to_hub: bool = True
|
| 95 |
+
hub_model_id: str = f"{HF_USERNAME}/kLLM-reward"
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
@dataclass
|
| 99 |
+
class RLHFConfig:
|
| 100 |
+
"""RLHF/PPO Training configuration."""
|
| 101 |
+
output_dir: str = "./checkpoints/rlhf"
|
| 102 |
+
|
| 103 |
+
# PPO specific
|
| 104 |
+
ppo_epochs: int = 4
|
| 105 |
+
mini_batch_size: int = 4
|
| 106 |
+
batch_size: int = 16
|
| 107 |
+
learning_rate: float = 1e-5
|
| 108 |
+
|
| 109 |
+
# KL penalty
|
| 110 |
+
init_kl_coef: float = 0.2
|
| 111 |
+
target_kl: float = 6.0
|
| 112 |
+
|
| 113 |
+
# Reward scaling
|
| 114 |
+
reward_baseline: float = 0.0
|
| 115 |
+
|
| 116 |
+
# Training
|
| 117 |
+
num_train_epochs: int = 1
|
| 118 |
+
max_steps: int = 1000
|
| 119 |
+
|
| 120 |
+
# Checkpointing - save every epoch
|
| 121 |
+
save_strategy: str = "epoch"
|
| 122 |
+
save_steps: int = 100
|
| 123 |
+
push_to_hub: bool = True
|
| 124 |
+
hub_model_id: str = f"{HF_USERNAME}/kLLM"
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
@dataclass
|
| 128 |
+
class InferenceConfig:
|
| 129 |
+
"""Inference configuration."""
|
| 130 |
+
model_path: str = MODEL_REPO
|
| 131 |
+
max_new_tokens: int = 512
|
| 132 |
+
temperature: float = 0.7
|
| 133 |
+
top_p: float = 0.9
|
| 134 |
+
top_k: int = 50
|
| 135 |
+
repetition_penalty: float = 1.1
|
| 136 |
+
do_sample: bool = True
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# Global configs
|
| 140 |
+
model_config = ModelConfig()
|
| 141 |
+
lora_config = LoRAConfig()
|
| 142 |
+
sft_config = SFTConfig()
|
| 143 |
+
reward_config = RewardConfig()
|
| 144 |
+
rlhf_config = RLHFConfig()
|
| 145 |
+
inference_config = InferenceConfig()
|
src/data/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Data processing modules for kLLM."""
|
src/inference/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Inference modules for kLLM."""
|
src/training/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Training modules for kLLM."""
|