Spaces:
Paused
Paused
File size: 13,002 Bytes
28fa644 1cf1484 28fa644 1cf1484 28fa644 1cf1484 28fa644 1cf1484 28fa644 |
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 |
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from typing import Optional, Union, AsyncGenerator
import torch
import logging
from pathlib import Path
from litgpt.api import LLM
import json
import asyncio
# Set up logging
logger = logging.getLogger(__name__)
# Create router instance
router = APIRouter()
# Global variable to store the LLM instance
llm_instance = None
class InitializeRequest(BaseModel):
"""
Configuration for model initialization including model path
"""
mode: str = "cpu"
precision: Optional[str] = None
quantize: Optional[str] = None
gpu_count: Union[str, int] = "auto"
model_path: str
class GenerateRequest(BaseModel):
prompt: str
max_new_tokens: int = 50
temperature: float = 1.0
top_k: Optional[int] = None
top_p: float = 1.0
return_as_token_ids: bool = False
stream: bool = False
# A Pydantic model for the streaming generation request
class StreamGenerateRequest(BaseModel):
prompt: str
max_new_tokens: int = 50
temperature: float = 1.0
top_k: Optional[int] = None
top_p: float = 1.0
class InitializeCustomRequest(BaseModel):
"""
Configuration for custom model initialization using from_pretrained
"""
mode: str = "cpu"
precision: Optional[str] = None
quantize: Optional[str] = None
gpu_count: Union[str, int] = "auto"
folder_path: str # Path to the model folder relative to checkpoints
model_filename: str # Name of the model file (e.g., "lit_model.pth")
config_filename: str = "config.json" # Default config filename
tokenizer_filename: Optional[str] = "tokenizer.json" # Optional tokenizer filename
@router.post("/initialize/custom")
async def initialize_custom_model(request: InitializeCustomRequest):
"""
Initialize a custom model using from_pretrained method.
This is for models that are already downloaded and stored in the checkpoints directory.
"""
global llm_instance
try:
# Get the project root directory and construct paths
project_root = Path(__file__).parent
checkpoints_dir = project_root / "checkpoints"
model_dir = checkpoints_dir / request.folder_path
logger.info(f"Loading custom model from directory: {model_dir}")
# Verify that all required files exist
model_path = model_dir / request.model_filename
config_path = model_dir / request.config_filename
if not model_path.exists():
raise HTTPException(
status_code=400,
detail=f"Model file not found: {request.model_filename}"
)
if not config_path.exists():
raise HTTPException(
status_code=400,
detail=f"Config file not found: {request.config_filename}"
)
# Check for tokenizer if specified
tokenizer_path = None
if request.tokenizer_filename:
tokenizer_path = model_dir / request.tokenizer_filename
if not tokenizer_path.exists():
raise HTTPException(
status_code=400,
detail=f"Tokenizer file not found: {request.tokenizer_filename}"
)
# Load the model using from_pretrained
llm_instance = LLM.from_pretrained(
path=str(model_dir),
model_file=request.model_filename,
config_file=request.config_filename,
tokenizer_file=request.tokenizer_filename if request.tokenizer_filename else None,
distribute=None if request.precision or request.quantize else "auto"
)
# If manual distribution is needed
if request.precision or request.quantize:
llm_instance.distribute(
accelerator="cuda" if request.mode == "gpu" else "cpu",
devices=request.gpu_count,
precision=request.precision,
quantize=request.quantize
)
# Log success and memory stats
logger.info(
f"Custom model initialized successfully with config:\n"
f"Mode: {request.mode}\n"
f"Precision: {request.precision}\n"
f"Quantize: {request.quantize}\n"
f"GPU Count: {request.gpu_count}\n"
f"Model Directory: {model_dir}\n"
f"Model File: {request.model_filename}\n"
f"Config File: {request.config_filename}\n"
f"Tokenizer File: {request.tokenizer_filename}\n"
f"Current GPU Memory: {torch.cuda.memory_allocated()/1024**3:.2f}GB allocated, "
f"{torch.cuda.memory_reserved()/1024**3:.2f}GB reserved"
)
return {
"success": True,
"message": "Custom model initialized successfully",
"model_info": {
"folder": str(model_dir),
"model_file": request.model_filename,
"config_file": request.config_filename,
"tokenizer_file": request.tokenizer_filename
}
}
except Exception as e:
logger.error(f"Error initializing custom model: {str(e)}")
# Print detailed memory statistics on failure
logger.error(f"GPU Memory Stats:\n"
f"Allocated: {torch.cuda.memory_allocated()/1024**3:.2f}GB\n"
f"Reserved: {torch.cuda.memory_reserved()/1024**3:.2f}GB\n"
f"Max Allocated: {torch.cuda.max_memory_allocated()/1024**3:.2f}GB")
raise HTTPException(status_code=500, detail=f"Error initializing custom model: {str(e)}")
# Endpoint for streaming generation
@router.post("/generate/stream")
async def generate_stream(request: StreamGenerateRequest):
"""
Generate text using the initialized model with streaming response.
Returns a StreamingResponse that yields JSON-formatted chunks of text.
"""
global llm_instance
if llm_instance is None:
raise HTTPException(
status_code=400,
detail="Model not initialized. Call /initialize first."
)
async def event_generator() -> AsyncGenerator[str, None]:
try:
# Start the generation with streaming enabled
async for token in llm_instance.generate(
prompt=request.prompt,
max_new_tokens=request.max_new_tokens,
temperature=request.temperature,
top_k=request.top_k,
top_p=request.top_p,
stream=True # Enable streaming
):
# Create a JSON response for each token
chunk = {
"token": token,
"metadata": {
"prompt": request.prompt,
"is_finished": False
}
}
# Format as SSE data
yield f"data: {json.dumps(chunk)}\n\n"
# Small delay to prevent overwhelming the client
await asyncio.sleep(0.01)
# Send final message indicating completion
final_chunk = {
"token": "",
"metadata": {
"prompt": request.prompt,
"is_finished": True
}
}
yield f"data: {json.dumps(final_chunk)}\n\n"
except Exception as e:
logger.error(f"Error in stream generation: {str(e)}")
error_chunk = {
"error": str(e),
"metadata": {
"prompt": request.prompt,
"is_finished": True
}
}
yield f"data: {json.dumps(error_chunk)}\n\n"
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
}
)
@router.get("/")
async def root():
"""Root endpoint to verify service is running"""
return {
"status": "running",
"service": "LLM Engine",
"endpoints": {
"initialize": "/initialize",
"generate": "/generate",
"health": "/health"
}
}
@router.post("/initialize")
async def initialize_model(request: InitializeRequest):
"""
Initialize the LLM model with specified configuration.
"""
global llm_instance
try:
# Get the project root directory (where main.py is located)
project_root = Path(__file__).parent
checkpoints_dir = project_root / "checkpoints"
logger.info(f"Checkpoint dir is: {checkpoints_dir}")
# For LitGPT downloaded models, path includes organization
if "/" in request.model_path:
# e.g., "mistralai/Mistral-7B-Instruct-v0.3"
org, model_name = request.model_path.split("/")
model_path = str(checkpoints_dir / org / model_name)
else:
# Fallback for direct model paths
model_path = str(checkpoints_dir / request.model_path)
logger.info(f"Using model path: {model_path}")
# Load the model
logger.info("Loading model")
llm_instance = LLM.load(
model=model_path,
distribute=None if request.precision or request.quantize else "auto"
)
logger.info("Done loading model")
# If manual distribution is needed
logger.info("Distributing model")
if request.precision or request.quantize:
llm_instance.distribute(
accelerator="cuda" if request.mode == "gpu" else "cpu",
devices=request.gpu_count,
precision=request.precision,
quantize=request.quantize
)
logger.info("Done distributing model")
logger.info(
f"Model initialized successfully with config:\n"
f"Mode: {request.mode}\n"
f"Precision: {request.precision}\n"
f"Quantize: {request.quantize}\n"
f"GPU Count: {request.gpu_count}\n"
f"Model Path: {model_path}\n"
f"Current GPU Memory: {torch.cuda.memory_allocated()/1024**3:.2f}GB allocated, "
f"{torch.cuda.memory_reserved()/1024**3:.2f}GB reserved"
)
return {"success": True, "message": "Model initialized successfully"}
except Exception as e:
logger.error(f"Error initializing model: {str(e)}")
# Print detailed memory statistics on failure
logger.error(f"GPU Memory Stats:\n"
f"Allocated: {torch.cuda.memory_allocated()/1024**3:.2f}GB\n"
f"Reserved: {torch.cuda.memory_reserved()/1024**3:.2f}GB\n"
f"Max Allocated: {torch.cuda.max_memory_allocated()/1024**3:.2f}GB")
raise HTTPException(status_code=500, detail=f"Error initializing model: {str(e)}")
@router.post("/generate")
async def generate(request: GenerateRequest):
"""
Generate text using the initialized model.
"""
global llm_instance
if llm_instance is None:
raise HTTPException(status_code=400, detail="Model not initialized. Call /initialize first.")
try:
if request.stream:
raise HTTPException(
status_code=400,
detail="Streaming is not currently supported through the API"
)
generated_text = llm_instance.generate(
prompt=request.prompt,
max_new_tokens=request.max_new_tokens,
temperature=request.temperature,
top_k=request.top_k,
top_p=request.top_p,
return_as_token_ids=request.return_as_token_ids,
stream=False # Force stream to False for now
)
response = {
"generated_text": generated_text if not request.return_as_token_ids else generated_text.tolist(),
"metadata": {
"prompt": request.prompt,
"max_new_tokens": request.max_new_tokens,
"temperature": request.temperature,
"top_k": request.top_k,
"top_p": request.top_p
}
}
return response
except Exception as e:
logger.error(f"Error generating text: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error generating text: {str(e)}")
@router.get("/health")
async def health_check():
"""
Check if the service is running and model is loaded.
"""
global llm_instance
status = {
"status": "healthy",
"model_loaded": llm_instance is not None,
}
if llm_instance is not None:
logger.info(f"llm_instance is: {llm_instance}")
status["model_info"] = {
"model_path": llm_instance.config.name,
"device": str(next(llm_instance.model.parameters()).device)
}
return status |