# Feature: Model Lifecycle Management

## Description

Manages the complete lifecycle of ML models including loading, caching, warmup, and unloading. Handles all four models (SuperPoint, LightGlue, DINOv2, LiteSAM) with support for multiple formats (TensorRT, ONNX, PyTorch).

## Component APIs Implemented

### `load_model(model_name: str, model_format: str) -> bool`
- Loads model in specified format
- Checks if model already loaded (cache hit)
- Initializes inference engine
- Triggers warmup
- Caches for reuse

### `warmup_model(model_name: str) -> bool`
- Warms up model with dummy input
- Initializes CUDA kernels
- Pre-allocates GPU memory
- Ensures first real inference is fast

## External Tools and Services

- **TensorRT**: Loading TensorRT engine files
- **ONNX Runtime**: Loading ONNX models
- **PyTorch**: Loading PyTorch model weights (optional)
- **CUDA**: GPU memory allocation

## Internal Methods

| Method | Purpose |
|--------|---------|
| `_check_model_cache(model_name)` | Check if model already loaded |
| `_load_tensorrt_engine(path)` | Load TensorRT engine from file |
| `_load_onnx_model(path)` | Load ONNX model from file |
| `_allocate_gpu_memory(model)` | Allocate GPU memory for model |
| `_create_dummy_input(model_name)` | Create appropriate dummy input for warmup |
| `_cache_model(model_name, engine)` | Store loaded model in cache |

## Unit Tests

| Test | Description | Expected Result |
|------|-------------|-----------------|
| UT-16.01-01 | Load TensorRT model | Model loaded, returns True |
| UT-16.01-02 | Load ONNX model | Model loaded, returns True |
| UT-16.01-03 | Load already cached model | Returns True immediately (no reload) |
| UT-16.01-04 | Load invalid model name | Returns False, logs error |
| UT-16.01-05 | Load invalid model path | Returns False, logs error |
| UT-16.01-06 | Warmup SuperPoint | CUDA kernels initialized |
| UT-16.01-07 | Warmup LightGlue | CUDA kernels initialized |
| UT-16.01-08 | Warmup DINOv2 | CUDA kernels initialized |
| UT-16.01-09 | Warmup LiteSAM | CUDA kernels initialized |
| UT-16.01-10 | Warmup unloaded model | Returns False |

## Integration Tests

| Test | Description | Expected Result |
|------|-------------|-----------------|
| IT-16.01-01 | Load all 4 models sequentially | All models loaded successfully |
| IT-16.01-02 | Load + warmup cycle for each model | All models ready for inference |
| IT-16.01-03 | GPU memory allocation after loading all models | ~4GB GPU memory used |
| IT-16.01-04 | First inference after warmup | Latency within target range |

