Spaces:
Running
Running
File size: 5,673 Bytes
b1f0789 |
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 |
"""
Optimized configuration for HF Spaces with intelligent handling of large models.
This file contains recommended settings based on available hardware.
"""
import os
from typing import Dict, Any
class HFSpacesConfig:
"""Optimized configuration for different HF Spaces tiers"""
# Timeouts (in seconds)
TIMEOUT_SMALL_MODEL = 120 # Models <2B parameters
TIMEOUT_MEDIUM_MODEL = 300 # Models 2-5B parameters
TIMEOUT_LARGE_MODEL = 600 # Models >5B parameters
TIMEOUT_PING = 5 # Health checks
# Recommended memory limits (GB) per HF Spaces tier
MEMORY_LIMITS = {
"free": 16, # Free HF Spaces
"pro": 32, # HF Spaces PRO
"enterprise": 64 # HF Spaces Enterprise
}
# Recommended models per tier
RECOMMENDED_MODELS = {
"free": [
"meta-llama/Llama-3.2-1B",
"oopere/pruned40-llama-3.2-1B",
"oopere/Fair-Llama-3.2-1B",
"google/gemma-3-1b-pt",
"Qwen/Qwen3-1.7B",
],
"pro": [
"meta-llama/Llama-3.2-3B",
"meta-llama/Llama-3-8B",
],
"enterprise": [
"meta-llama/Llama-3-70B",
]
}
# Model loading configuration
MODEL_LOAD_CONFIG = {
"small": { # <2B params
"low_cpu_mem_usage": True,
"torch_dtype": "auto",
"device_map": "auto",
"timeout": TIMEOUT_SMALL_MODEL,
},
"medium": { # 2-8B params
"low_cpu_mem_usage": True,
"torch_dtype": "float16", # Reduces memory
"device_map": "auto",
"timeout": TIMEOUT_MEDIUM_MODEL,
},
"large": { # >8B params
"low_cpu_mem_usage": True,
"torch_dtype": "float16",
"device_map": "auto",
"load_in_8bit": True, # int8 quantization
"timeout": TIMEOUT_LARGE_MODEL,
}
}
@classmethod
def get_model_size_category(cls, model_name: str) -> str:
"""
Determines the model size category based on the name.
Returns:
"small", "medium", or "large"
"""
model_lower = model_name.lower()
# Detect by parameters in the name
if any(size in model_lower for size in ["1b", "1.7b", "1.5b"]):
return "small"
elif any(size in model_lower for size in ["3b", "7b", "8b"]):
return "medium"
elif any(size in model_lower for size in ["13b", "30b", "70b"]):
return "large"
# Default: small (assume the safest case)
return "small"
@classmethod
def get_timeout_for_model(cls, model_name: str) -> int:
"""Gets the recommended timeout for a model."""
size = cls.get_model_size_category(model_name)
return cls.MODEL_LOAD_CONFIG[size]["timeout"]
@classmethod
def get_load_config(cls, model_name: str) -> Dict[str, Any]:
"""Gets the optimized loading configuration for a model."""
size = cls.get_model_size_category(model_name)
return cls.MODEL_LOAD_CONFIG[size].copy()
@classmethod
def is_model_recommended(cls, model_name: str, tier: str = "free") -> bool:
"""Verifies if a model is recommended for the current tier."""
return model_name in cls.RECOMMENDED_MODELS.get(tier, [])
@classmethod
def get_memory_warning(cls, model_name: str, tier: str = "free") -> str:
"""
Generates a warning if the model may exceed memory limits.
Returns:
String with warning, or empty string if no problem
"""
if cls.is_model_recommended(model_name, tier):
return ""
size = cls.get_model_size_category(model_name)
if size == "medium" and tier == "free":
return (
"β οΈ **Warning**: This model may be too large for free HF Spaces. "
"Consider upgrading to HF Spaces PRO or using a smaller model."
)
elif size == "large" and tier in ["free", "pro"]:
return (
"β **Error**: This model is too large for your HF Spaces tier. "
"Use a smaller model or upgrade to Enterprise."
)
return ""
# Usage example:
def get_optimized_request_config(model_name: str) -> dict:
"""
Gets optimized configuration for HTTP requests based on the model.
Usage:
config = get_optimized_request_config("meta-llama/Llama-3.2-1B")
response = requests.post(url, json=payload, **config)
"""
return {
"timeout": HFSpacesConfig.get_timeout_for_model(model_name),
}
# Default configuration for general use
DEFAULT_CONFIG = {
"timeout": HFSpacesConfig.TIMEOUT_MEDIUM_MODEL,
"max_retries": 2,
"retry_delay": 5, # seconds between retries
}
if __name__ == "__main__":
# Usage examples
print("π§ Optimized configuration for HF Spaces\n")
test_models = [
"meta-llama/Llama-3.2-1B",
"meta-llama/Llama-3.2-3B",
"meta-llama/Llama-3-8B",
]
for model in test_models:
print(f"π¦ Model: {model}")
print(f" Category: {HFSpacesConfig.get_model_size_category(model)}")
print(f" Timeout: {HFSpacesConfig.get_timeout_for_model(model)}s")
print(f" Recommended (free): {HFSpacesConfig.is_model_recommended(model, 'free')}")
warning = HFSpacesConfig.get_memory_warning(model, "free")
if warning:
print(f" {warning}")
print()
|