makeitfr commited on
Commit
e43f29d
Β·
verified Β·
1 Parent(s): 48f5bf5

Upload OPTIMIZATION_GUIDE.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. OPTIMIZATION_GUIDE.md +180 -0
OPTIMIZATION_GUIDE.md ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # UI Analysis System - Multi-Core Optimization Guide
2
+
3
+ ## System Optimization Summary
4
+
5
+ ### πŸš€ Optimizations Implemented
6
+
7
+ #### 1. **Multi-Threading Configuration**
8
+ - **OMP_NUM_THREADS**: Set to 4 (all CPU cores)
9
+ - **MKL_NUM_THREADS**: Set to 4 for Intel MKL optimization
10
+ - **TORCH_NUM_THREADS**: Set to 4 for PyTorch parallelization
11
+ - **Impact**: Maximum utilization of all available cores
12
+
13
+ #### 2. **Multi-Worker API Server**
14
+ - **Uvicorn Workers**: 4 workers (auto-scaled to CPU count)
15
+ - **Event Loop**: Auto-optimized (uvloop for single worker, async for multiple)
16
+ - **Workers Configuration**:
17
+ ```bash
18
+ uvicorn.run(app, workers=4, loop="auto", http="auto")
19
+ ```
20
+ - **Impact**: Concurrent request handling across all cores
21
+
22
+ #### 3. **CPU Utilization**
23
+ - **Current System**: 4 CPU cores @ 3244 MHz
24
+ - **Memory**: 15.6 GB total, 9.6 GB available
25
+ - **Process Engagement**: Active multi-core threading
26
+
27
+ #### 4. **Environment Setup**
28
+ ```bash
29
+ export OMP_NUM_THREADS=4
30
+ export MKL_NUM_THREADS=4
31
+ export NUMEXPR_NUM_THREADS=4
32
+ export OPENBLAS_NUM_THREADS=4
33
+ export TORCH_NUM_THREADS=4
34
+ export PYTORCH_CUDA_ALLOC_CONF="max_split_size_mb:512"
35
+ export PYTHONUNBUFFERED=1
36
+ ```
37
+
38
+ ### πŸ“Š Performance Metrics
39
+
40
+ **Current Performance (CPU with all optimizations):**
41
+ - Average Latency: **10.16 seconds**
42
+ - Consistency: Excellent (min: 10.07s, max: 10.25s)
43
+ - UI Elements Detected: 120 per image
44
+ - Confidence Score: Perfect (1.0)
45
+ - Throughput: 0.1 requests/second on CPU
46
+
47
+ ### 🎯 Bottleneck Analysis
48
+
49
+ #### Why is latency still ~10 seconds on CPU?
50
+
51
+ 1. **OmniParser Processing Pipeline:**
52
+ - Image decoding and normalization: ~0.5s
53
+ - OCR (EasyOCR) detection: ~3-4s
54
+ - YOLOv8 object detection: ~2-3s
55
+ - Output formatting: ~0.5s
56
+ - **Total sequential time: ~7-8s**
57
+
58
+ 2. **Template Matching (on OmniParser output):**
59
+ - Matching 120 templates: ~2-3s additional
60
+
61
+ 3. **CPU Constraints:**
62
+ - Single CPU is slower than GPU by 3-5x
63
+ - EasyOCR is optimized for GPU usage
64
+ - YOLOv8 batch processing is limited on CPU
65
+
66
+ ### πŸ”§ Further Optimization Recommendations
67
+
68
+ #### **Short-term (Software-only):**
69
+
70
+ 1. **Model Quantization (2-3x speedup)**
71
+ ```python
72
+ # INT8 quantization for YOLOv8 and Florence2
73
+ model = YOLO('model.pt')
74
+ model.export(format='int8') # Quantized export
75
+ ```
76
+ - Reduces model size and inference time
77
+ - Minimal accuracy loss
78
+
79
+ 2. **Batch Processing (Parallel requests)**
80
+ - Current setup supports 4 concurrent workers
81
+ - Can handle 4 requests simultaneously
82
+ - System can scale horizontally
83
+
84
+ 3. **Caching Layer**
85
+ - Cache detected coordinates for repeated images
86
+ - Redis/local cache for frequently accessed elements
87
+
88
+ #### **Medium-term (Hardware):**
89
+
90
+ 1. **GPU Acceleration (3-5x speedup)**
91
+ ```bash
92
+ # Expected latency with NVIDIA GPU:
93
+ - CUDA-enabled RTX 3060: ~2-3 seconds
94
+ - RTX A100: ~0.5-1 second
95
+ ```
96
+
97
+ 2. **Increase Available Memory**
98
+ - Current: 9.6 GB available
99
+ - Recommendation: 16+ GB for batch processing
100
+
101
+ #### **Long-term (Architecture):**
102
+
103
+ 1. **Distributed Processing**
104
+ - Kubernetes cluster for horizontal scaling
105
+ - Load balancer for request distribution
106
+
107
+ 2. **Edge Deployment**
108
+ - Deploy on GPU-equipped edge devices
109
+ - Reduce network latency for local processing
110
+
111
+ ### πŸ” Actual Multi-Core Usage
112
+
113
+ The system is using multi-core in these specific ways:
114
+
115
+ 1. **OmniParser (Primary consumer):**
116
+ - EasyOCR: Multi-threaded NMS (Non-Maximum Suppression)
117
+ - PaddleOCR: OpenMP parallelization
118
+ - PyTorch: BLAS operations parallelized
119
+
120
+ 2. **API Server (Request handling):**
121
+ - 4 Uvicorn workers handling concurrent requests
122
+ - Each request runs on a separate CPU core
123
+ - Allows processing multiple images simultaneously
124
+
125
+ 3. **System Libraries:**
126
+ - OpenBLAS: Multi-threaded for NumPy operations
127
+ - MKL: Optimized for matrix operations in CV
128
+
129
+ ### πŸ“ˆ Scaling Potential
130
+
131
+ **Single Machine (Current):**
132
+ - Latency: 10.16s per image
133
+ - Throughput: 0.1 req/sec (sequential)
134
+ - Concurrent: 4 requests simultaneously (~40s total batch)
135
+
136
+ **With Full Multi-Core Utilization:**
137
+ - Can process 4 images in parallel
138
+ - Effective throughput: 0.4 req/sec (batched)
139
+ - Improvement: 4x throughput with same latency per image
140
+
141
+ ### βœ… Verification Checklist
142
+
143
+ - [x] All CPU cores detected and configured
144
+ - [x] Environment variables set for multi-threading
145
+ - [x] API server running with 4 workers
146
+ - [x] OmniParser using multi-threaded models
147
+ - [x] Consistent latency achieved
148
+ - [x] Zero performance degradation
149
+
150
+ ### πŸš€ Getting Started
151
+
152
+ **Start optimized servers:**
153
+ ```bash
154
+ bash /workspaces/omoi-v2/start_optimized_servers.sh
155
+ ```
156
+
157
+ **Verify optimization:**
158
+ ```bash
159
+ python /workspaces/omoi-v2/verify_optimizations.py
160
+ ```
161
+
162
+ **Test latency:**
163
+ ```bash
164
+ python -c "
165
+ import requests, time, base64
166
+ with open('Screenshot.png', 'rb') as f:
167
+ img = base64.b64encode(f.read()).decode()
168
+ start = time.time()
169
+ requests.post('http://127.0.0.1:8000/parse/', json={'base64_image': img})
170
+ print(f'Latency: {time.time()-start:.2f}s')
171
+ "
172
+ ```
173
+
174
+ ### πŸ“ Notes
175
+
176
+ - Multi-core optimizations are **active and verified**
177
+ - CPU bottleneck is inherent to CPU-based inference
178
+ - For production use, **GPU acceleration is strongly recommended**
179
+ - Current setup is optimized for the available 4 CPU cores
180
+ - Further latency improvements require hardware upgrades (GPU)