forked from etalab-ia/evalap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_eco.py
More file actions
146 lines (112 loc) · 5.41 KB
/
utils_eco.py
File metadata and controls
146 lines (112 loc) · 5.41 KB
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
from pathlib import Path
import toml
from ecologits.tracers.utils import compute_llm_impacts, electricity_mixes
from evalap.logger import logger
DEFAULT_PARAMS = {"params": 100, "active_params": 100, "total_params": 100}
def load_models_info() -> dict:
config_path = Path("evalap/config/models-extra-info.toml")
with open(config_path, "r", encoding="utf-8") as f:
config = toml.load(f)
return config
def get_model_name_from_path(full_name: str) -> str:
return full_name.split("/")[-1].lower()
def estimate_model_params(model_name: str) -> dict:
"""Estimate model parameters based on its name and known patterns."""
name_lower = model_name.lower()
# Size estimation patterns
size_patterns = {"mini": 3, "small": 7, "medium": 35, "large": 70, "xl": 200, "xxl": 400}
# Mixture of Experts patterns
moe_patterns = ["moe", "mixture", "sparse"]
# Total parameters estimation
total_params = DEFAULT_PARAMS["total_params"]
for pattern, size in size_patterns.items():
if pattern in name_lower:
total_params = size
break
# Active parameters estimation
active_params = total_params
if any(pattern in name_lower for pattern in moe_patterns):
active_params = total_params // 4 # MoE models typically use 1/4 of total parameters
return {
"params": total_params,
"total_params": total_params,
"active_params": active_params,
"estimated": True,
}
def build_model_extra_info(model_name: str, models_info_params: dict) -> dict:
"""Build model information dictionary with default values for missing parameters."""
std_name = get_model_name_from_path(model_name)
logger.debug(f"Processing model: {std_name}")
# Case-insensitive search in TOML keys
model = None
for key in models_info_params.keys():
if key.lower() == std_name:
model = models_info_params[key]
break
if model is None:
logger.debug(f"Model {std_name} not found in models-extra-info.toml. Estimating parameters...")
model = estimate_model_params(std_name)
model["id"] = std_name.lower()
else:
model = model.copy()
model["id"] = model.get("id", std_name).lower()
model["estimated"] = False
# Handle size parameters
if not any(model.get(key) for key in ("friendly_size", "params", "total_params")):
model["params"] = DEFAULT_PARAMS["params"]
# Map friendly sizes to parameter counts
PARAMS_SIZE_MAP = {"XS": 3, "S": 7, "M": 35, "L": 70, "XL": 200}
model["params"] = model.get(
"total_params", PARAMS_SIZE_MAP.get(model.get("friendly_size"), DEFAULT_PARAMS["params"])
)
# if quantization, divide by 2
if model.get("quantization") == "q8":
model["active_params"] = model.get("active_params", model["params"]) // 2
model["total_params"] = model.get("total_params", model["params"]) // 2
else:
model["active_params"] = model.get("active_params", model["params"])
model["total_params"] = model.get("total_params", model["params"])
# Calculate required RAM based on quantization
if model.get("quantization") == "q8":
model["required_ram"] = model["params"] * 2 # q8 quantization uses 2 bytes per parameter
else:
model["required_ram"] = model["params"] # Default: 1 byte per parameter
logger.debug(f"Model info: {model}")
return model
def impact_carbon(model_name: str, model_url: str, token_count: int, request_latency: float) -> dict:
"""Calculate carbon impact of a model inference."""
logger.debug(f"model_name : {model_name}")
logger.debug(f"model_url : {model_url}")
logger.debug(f"token_count : {token_count}")
models_info = load_models_info()
model_data = build_model_extra_info(model_name, models_info)
# Validate input parameters
if not isinstance(token_count, (int, float)) or token_count < 0:
raise ValueError("token_count must be a positive number")
if not isinstance(request_latency, (int, float)) or request_latency < 0:
raise ValueError("request_latency must be a positive number")
# Get model parameters, always using DEFAULT_PARAMS as fallback
mapc = model_data.get("active_params", model_data.get("params", DEFAULT_PARAMS["active_params"]))
matpc = model_data.get("total_params", model_data.get("params", DEFAULT_PARAMS["total_params"]))
# Determine electricity mix zone
if not isinstance(model_url, str):
raise ValueError("model_url must be a string")
# Use French electricity mix for Albert models, world average for others
electricity_mix_zone = "FRA" if "albert" in model_url.lower() else "WOR"
electricity_mix = electricity_mixes.find_electricity_mix(zone=electricity_mix_zone)
if not electricity_mix:
raise ValueError(f"electricity zone {electricity_mix_zone} not found")
# Calculate carbon impact using Ecologits
impacts = compute_llm_impacts(
model_active_parameter_count=mapc,
model_total_parameter_count=matpc,
output_token_count=token_count,
if_electricity_mix_adpe=electricity_mix.adpe,
if_electricity_mix_pe=electricity_mix.pe,
if_electricity_mix_gwp=electricity_mix.gwp,
request_latency=request_latency,
)
# Convert to dict and add estimation flag
impacts_dict = impacts.model_dump()
impacts_dict["estimated"] = model_data.get("estimated", False)
return impacts_dict