File size: 14,994 Bytes
3df89a1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
"""
Main KerdosAgent class that orchestrates the training and deployment process.
"""
from typing import Optional, Union, Dict, Any, List
from pathlib import Path
import torch
import logging
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
GenerationConfig
)
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
import warnings
from .trainer import Trainer
from .deployer import Deployer
from .data_processor import DataProcessor
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class KerdosAgent:
"""
Main agent class for training and deploying LLMs with custom data.
"""
def __init__(
self,
base_model: str,
training_data: Union[str, Path],
device: Optional[str] = None,
**kwargs
):
"""
Initialize the KerdosAgent.
Args:
base_model: Name or path of the base LLM model
training_data: Path to the training data
device: Device to run the model on (cuda/cpu)
**kwargs: Additional configuration parameters
"""
self.base_model = base_model
self.training_data = Path(training_data) if training_data else None
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
self.config = kwargs
logger.info(f"Initializing KerdosAgent with base model: {base_model}")
logger.info(f"Using device: {self.device}")
# Validate configuration
self._validate_config()
# Initialize components
try:
quantization_config = self._get_quantization_config()
self.model = AutoModelForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
device_map="auto",
quantization_config=quantization_config,
trust_remote_code=kwargs.get('trust_remote_code', False)
)
self.tokenizer = AutoTokenizer.from_pretrained(
base_model,
trust_remote_code=kwargs.get('trust_remote_code', False)
)
# Set pad token if not present
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
self.model.config.pad_token_id = self.model.config.eos_token_id
# Initialize other components
if self.training_data:
self.data_processor = DataProcessor(self.training_data)
else:
self.data_processor = None
self.trainer = Trainer(self.model, self.tokenizer, self.device)
self.deployer = Deployer(self.model, self.tokenizer)
logger.info("KerdosAgent initialized successfully")
except Exception as e:
logger.error(f"Error initializing KerdosAgent: {str(e)}")
raise
def train(
self,
epochs: int = 3,
batch_size: int = 4,
learning_rate: float = 2e-5,
**kwargs
) -> Dict[str, Any]:
"""
Train the model on the provided data.
Args:
epochs: Number of training epochs
batch_size: Training batch size
learning_rate: Learning rate for training
**kwargs: Additional training parameters
Returns:
Dictionary containing training metrics
"""
# Process training data
train_dataset = self.data_processor.prepare_dataset()
# Train the model
training_args = {
"epochs": epochs,
"batch_size": batch_size,
"learning_rate": learning_rate,
**kwargs
}
metrics = self.trainer.train(train_dataset, **training_args)
return metrics
def deploy(
self,
deployment_type: str = "rest",
host: str = "0.0.0.0",
port: int = 8000,
**kwargs
) -> None:
"""
Deploy the trained model.
Args:
deployment_type: Type of deployment (rest/docker/kubernetes)
host: Host address for REST API
port: Port number for REST API
**kwargs: Additional deployment parameters
"""
deployment_args = {
"deployment_type": deployment_type,
"host": host,
"port": port,
**kwargs
}
self.deployer.deploy(**deployment_args)
def save(self, output_dir: Union[str, Path]) -> None:
"""
Save the trained model and tokenizer.
Args:
output_dir: Directory to save the model
"""
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
self.model.save_pretrained(output_dir)
self.tokenizer.save_pretrained(output_dir)
def generate(
self,
prompt: str,
max_length: int = 100,
temperature: float = 0.7,
top_p: float = 0.9,
top_k: int = 50,
num_return_sequences: int = 1,
**kwargs
) -> Union[str, List[str]]:
"""
Generate text from a prompt.
Args:
prompt: Input text prompt
max_length: Maximum length of generated text
temperature: Sampling temperature
top_p: Nucleus sampling parameter
top_k: Top-k sampling parameter
num_return_sequences: Number of sequences to generate
**kwargs: Additional generation parameters
Returns:
Generated text or list of generated texts
"""
try:
logger.info(f"Generating text from prompt: {prompt[:50]}...")
# Tokenize input
inputs = self.tokenizer(
prompt,
return_tensors="pt",
padding=True,
truncation=True
).to(self.device)
# Set up generation config
generation_config = GenerationConfig(
max_length=max_length,
temperature=temperature,
top_p=top_p,
top_k=top_k,
num_return_sequences=num_return_sequences,
pad_token_id=self.tokenizer.pad_token_id,
eos_token_id=self.tokenizer.eos_token_id,
**kwargs
)
# Generate
self.model.eval()
with torch.no_grad():
outputs = self.model.generate(
**inputs,
generation_config=generation_config
)
# Decode outputs
generated_texts = [
self.tokenizer.decode(output, skip_special_tokens=True)
for output in outputs
]
logger.info(f"Generated {len(generated_texts)} sequence(s)")
return generated_texts[0] if num_return_sequences == 1 else generated_texts
except Exception as e:
logger.error(f"Error generating text: {str(e)}")
raise
def inference(
self,
texts: List[str],
batch_size: int = 8,
**kwargs
) -> List[str]:
"""
Run batch inference on multiple texts.
Args:
texts: List of input texts
batch_size: Batch size for inference
**kwargs: Additional generation parameters
Returns:
List of generated texts
"""
try:
logger.info(f"Running inference on {len(texts)} texts")
results = []
self.model.eval()
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
# Tokenize batch
inputs = self.tokenizer(
batch,
return_tensors="pt",
padding=True,
truncation=True
).to(self.device)
# Generate
with torch.no_grad():
outputs = self.model.generate(
**inputs,
pad_token_id=self.tokenizer.pad_token_id,
**kwargs
)
# Decode
batch_results = [
self.tokenizer.decode(output, skip_special_tokens=True)
for output in outputs
]
results.extend(batch_results)
logger.info(f"Inference completed for {len(results)} texts")
return results
except Exception as e:
logger.error(f"Error during inference: {str(e)}")
raise
def prepare_for_training(
self,
use_lora: bool = True,
lora_r: int = 8,
lora_alpha: int = 32,
lora_dropout: float = 0.1,
target_modules: Optional[List[str]] = None,
use_4bit: bool = False,
use_8bit: bool = False
) -> None:
"""
Prepare the model for efficient training using LoRA and/or quantization.
Args:
use_lora: Whether to use LoRA (Low-Rank Adaptation)
lora_r: LoRA rank
lora_alpha: LoRA alpha parameter
lora_dropout: LoRA dropout rate
target_modules: List of module names to apply LoRA to
use_4bit: Whether to use 4-bit quantization
use_8bit: Whether to use 8-bit quantization
"""
try:
logger.info("Preparing model for training")
# Prepare model for k-bit training if quantization is used
if use_4bit or use_8bit:
logger.info("Preparing model for k-bit training")
self.model = prepare_model_for_kbit_training(self.model)
# Apply LoRA if requested
if use_lora:
logger.info(f"Applying LoRA with r={lora_r}, alpha={lora_alpha}")
if target_modules is None:
# Default target modules for common architectures
target_modules = ["q_proj", "v_proj", "k_proj", "o_proj"]
lora_config = LoraConfig(
r=lora_r,
lora_alpha=lora_alpha,
target_modules=target_modules,
lora_dropout=lora_dropout,
bias="none",
task_type="CAUSAL_LM"
)
self.model = get_peft_model(self.model, lora_config)
self.model.print_trainable_parameters()
logger.info("Model prepared for training successfully")
except Exception as e:
logger.error(f"Error preparing model for training: {str(e)}")
raise
def _validate_config(self) -> None:
"""
Validate the agent configuration.
Raises:
ValueError: If configuration is invalid
"""
if not self.base_model:
raise ValueError("base_model must be specified")
if self.config.get('use_4bit') and self.config.get('use_8bit'):
raise ValueError("Cannot use both 4-bit and 8-bit quantization")
logger.debug("Configuration validated successfully")
def _get_quantization_config(self) -> Optional[BitsAndBytesConfig]:
"""
Get quantization configuration if requested.
Returns:
BitsAndBytesConfig or None
"""
if self.config.get('use_4bit'):
logger.info("Using 4-bit quantization")
return BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True
)
elif self.config.get('use_8bit'):
logger.info("Using 8-bit quantization")
return BitsAndBytesConfig(
load_in_8bit=True
)
return None
def get_model_info(self) -> Dict[str, Any]:
"""
Get information about the current model.
Returns:
Dictionary containing model information
"""
total_params = sum(p.numel() for p in self.model.parameters())
trainable_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
return {
"base_model": self.base_model,
"device": self.device,
"total_parameters": total_params,
"trainable_parameters": trainable_params,
"trainable_percentage": (trainable_params / total_params) * 100 if total_params > 0 else 0,
"model_dtype": str(next(self.model.parameters()).dtype),
"config": self.config
}
@classmethod
def load(cls, model_dir: Union[str, Path], **kwargs) -> "KerdosAgent":
"""
Load a trained model from disk.
Args:
model_dir: Directory containing the saved model
**kwargs: Additional initialization parameters
Returns:
Loaded KerdosAgent instance
"""
try:
model_dir = Path(model_dir)
if not model_dir.exists():
raise FileNotFoundError(f"Model directory {model_dir} does not exist")
logger.info(f"Loading model from {model_dir}")
# Create agent with loaded model
agent = cls(
base_model=str(model_dir),
training_data=None, # Not needed for loading
**kwargs
)
logger.info("Model loaded successfully")
return agent
except Exception as e:
logger.error(f"Error loading model: {str(e)}")
raise |