File size: 9,313 Bytes
b59a5d0 |
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 |
"""
Formatting utilities for displaying benchmark data with emojis.
"""
from typing import Any, Optional
from datetime import datetime
def format_platform(platform: str) -> str:
"""Format platform with emoji."""
emoji_map = {
"node": "๐ข",
"web": "๐",
}
emoji = emoji_map.get(platform, "")
return f"{emoji} {platform}" if emoji else platform
def format_device(device: str) -> str:
"""Format device with emoji."""
emoji_map = {
"wasm": "๐ฆ",
"webgpu": "โก",
"cpu": "๐ฅ๏ธ",
"cuda": "๐ฎ",
}
emoji = emoji_map.get(device, "")
return f"{emoji} {device}" if emoji else device
def format_browser(browser: str) -> str:
"""Format browser with emoji."""
if not browser:
return ""
emoji_map = {
"chromium": "๐ต",
"chrome": "๐ต",
"firefox": "๐ฆ",
"webkit": "๐งญ",
"safari": "๐งญ",
}
emoji = emoji_map.get(browser.lower(), "")
return f"{emoji} {browser}" if emoji else browser
def format_status(status: str) -> str:
"""Format status with emoji."""
emoji_map = {
"completed": "โ
",
"failed": "โ",
"running": "๐",
"pending": "โณ",
}
emoji = emoji_map.get(status, "")
return f"{emoji} {status}" if emoji else status
def format_mode(mode: str) -> str:
"""Format mode with emoji."""
emoji_map = {
"warm": "๐ฅ",
"cold": "โ๏ธ",
}
emoji = emoji_map.get(mode, "")
return f"{emoji} {mode}" if emoji else mode
def format_headed(headed: bool) -> str:
"""Format headed mode with emoji."""
return "๐๏ธ Yes" if headed else "No"
def format_metric_ms(value: Optional[float], metric_type: str = "inference") -> str:
"""Format metric in milliseconds with performance emoji.
Args:
value: Metric value in milliseconds
metric_type: Type of metric ('load', 'inference')
Returns:
Formatted string with emoji
"""
if value is None or value == 0:
return "-"
# Different thresholds for different metric types
if metric_type == "load":
# Load time thresholds (in ms)
if value < 100:
emoji = "๐" # Very fast
elif value < 500:
emoji = "โก" # Fast
elif value < 2000:
emoji = "โ
" # Good
elif value < 5000:
emoji = "โ ๏ธ" # Slow
else:
emoji = "๐" # Very slow
else: # inference
# Inference time thresholds (in ms)
if value < 5:
emoji = "๐" # Very fast
elif value < 20:
emoji = "โก" # Fast
elif value < 50:
emoji = "โ
" # Good
elif value < 100:
emoji = "โ ๏ธ" # Slow
else:
emoji = "๐" # Very slow
return f"{emoji} {value:.1f}ms"
def format_duration(duration_s: Optional[float]) -> str:
"""Format duration with emoji."""
if duration_s is None or duration_s == 0:
return "-"
if duration_s < 5:
emoji = "๐" # Very fast
elif duration_s < 15:
emoji = "โก" # Fast
elif duration_s < 60:
emoji = "โ
" # Good
elif duration_s < 300:
emoji = "โ ๏ธ" # Slow
else:
emoji = "๐" # Very slow
return f"{emoji} {duration_s:.1f}s"
def format_memory(memory_gb: Optional[int]) -> str:
"""Format memory with emoji."""
if memory_gb is None or memory_gb == 0:
return "-"
if memory_gb >= 32:
emoji = "๐ช" # High
elif memory_gb >= 16:
emoji = "โ
" # Good
elif memory_gb >= 8:
emoji = "โ ๏ธ" # Medium
else:
emoji = "๐" # Low
return f"{emoji} {memory_gb}GB"
def format_cpu_cores(cores: Optional[int]) -> str:
"""Format CPU cores with emoji."""
if cores is None or cores == 0:
return "-"
if cores >= 16:
emoji = "๐ช" # Many
elif cores >= 8:
emoji = "โ
" # Good
elif cores >= 4:
emoji = "โ ๏ธ" # Medium
else:
emoji = "๐" # Few
return f"{emoji} {cores} cores"
def format_timestamp(timestamp: Optional[datetime]) -> str:
"""Format timestamp as datetime string.
Args:
timestamp: datetime object
Returns:
Formatted datetime string
"""
if timestamp is None:
return "-"
try:
# Format as readable datetime
return timestamp.strftime("%Y-%m-%d %H:%M:%S")
except (ValueError, AttributeError):
return str(timestamp)
def format_downloads(downloads: Optional[int]) -> str:
"""Format downloads count with emoji.
Args:
downloads: Number of downloads
Returns:
Formatted string with emoji
"""
if downloads is None or downloads == 0:
return "-"
# Format large numbers
if downloads >= 1_000_000:
formatted = f"{downloads / 1_000_000:.1f}M"
emoji = "๐ฅ" # Very popular
elif downloads >= 100_000:
formatted = f"{downloads / 1_000:.0f}k"
emoji = "โญ" # Popular
elif downloads >= 10_000:
formatted = f"{downloads / 1_000:.1f}k"
emoji = "โจ" # Well-known
elif downloads >= 1_000:
formatted = f"{downloads / 1_000:.1f}k"
emoji = "๐" # Moderate
else:
formatted = str(downloads)
emoji = "๐" # New/niche
return f"{emoji} {formatted}"
def format_likes(likes: Optional[int]) -> str:
"""Format likes count with emoji.
Args:
likes: Number of likes
Returns:
Formatted string with emoji
"""
if likes is None or likes == 0:
return "-"
# Format based on popularity
if likes >= 1000:
emoji = "๐" # Very popular
elif likes >= 100:
emoji = "โค๏ธ" # Popular
elif likes >= 50:
emoji = "๐" # Well-liked
elif likes >= 10:
emoji = "๐" # Moderate
else:
emoji = "๐ค" # Few likes
return f"{emoji} {likes}"
def format_first_timer_score(score: Optional[float]) -> str:
"""Format first-timer-friendly score with emoji.
Args:
score: First-timer score (0-100)
Returns:
Formatted string with emoji
"""
if score is None:
return "-"
# Format based on score (0-100 scale)
if score >= 80:
emoji = "โญโญโญ" # Excellent
elif score >= 60:
emoji = "โญโญ" # Good
elif score >= 40:
emoji = "โญ" # Fair
else:
emoji = "ยท" # Below average
return f"{emoji} {score:.0f}"
def apply_formatting(df_dict: dict) -> dict:
"""Apply emoji formatting to a benchmark result dictionary.
Args:
df_dict: Dictionary containing benchmark data (one row)
Returns:
Dictionary with formatted values
"""
formatted = df_dict.copy()
# Format categorical fields
if "platform" in formatted:
formatted["platform"] = format_platform(formatted["platform"])
if "device" in formatted:
formatted["device"] = format_device(formatted["device"])
if "browser" in formatted:
formatted["browser"] = format_browser(formatted["browser"])
if "status" in formatted:
formatted["status"] = format_status(formatted["status"])
if "mode" in formatted:
formatted["mode"] = format_mode(formatted["mode"])
if "headed" in formatted:
formatted["headed"] = format_headed(formatted["headed"])
# Format metrics
if "load_ms_p50" in formatted:
formatted["load_ms_p50"] = format_metric_ms(formatted["load_ms_p50"], "load")
if "load_ms_p90" in formatted:
formatted["load_ms_p90"] = format_metric_ms(formatted["load_ms_p90"], "load")
if "first_infer_ms_p50" in formatted:
formatted["first_infer_ms_p50"] = format_metric_ms(formatted["first_infer_ms_p50"], "inference")
if "first_infer_ms_p90" in formatted:
formatted["first_infer_ms_p90"] = format_metric_ms(formatted["first_infer_ms_p90"], "inference")
if "subsequent_infer_ms_p50" in formatted:
formatted["subsequent_infer_ms_p50"] = format_metric_ms(formatted["subsequent_infer_ms_p50"], "inference")
if "subsequent_infer_ms_p90" in formatted:
formatted["subsequent_infer_ms_p90"] = format_metric_ms(formatted["subsequent_infer_ms_p90"], "inference")
# Format environment info
if "memory_gb" in formatted:
formatted["memory_gb"] = format_memory(formatted["memory_gb"])
if "cpuCores" in formatted:
formatted["cpuCores"] = format_cpu_cores(formatted["cpuCores"])
if "duration_s" in formatted:
formatted["duration_s"] = format_duration(formatted["duration_s"])
# Format timestamp
if "timestamp" in formatted:
formatted["timestamp"] = format_timestamp(formatted["timestamp"])
# Format HuggingFace metadata
if "downloads" in formatted:
formatted["downloads"] = format_downloads(formatted["downloads"])
if "likes" in formatted:
formatted["likes"] = format_likes(formatted["likes"])
# Format first-timer score
if "first_timer_score" in formatted:
formatted["first_timer_score"] = format_first_timer_score(formatted["first_timer_score"])
return formatted
|