File size: 17,358 Bytes
d8a562e e9f53f0 5ce3bc6 85e37e1 9f104ef 85e37e1 5ce3bc6 d23d1fc 85e37e1 d23d1fc 85e37e1 d23d1fc 85e37e1 d23d1fc 85e37e1 f236326 56b18b9 85e37e1 5ce3bc6 e1dae5e 85e37e1 5ce3bc6 85e37e1 bb4818f 85e37e1 bb4818f 85e37e1 bb4818f 85e37e1 7ef1ddb bb4818f 85e37e1 9f104ef 85e37e1 1c2951f 85e37e1 1c2951f 85e37e1 96f9f10 85e37e1 0ebc358 85e37e1 bb4818f 85e37e1 e1dae5e d23d1fc e1dae5e d23d1fc e1dae5e 85e37e1 2abdf04 d23d1fc 85e37e1 d23d1fc 85e37e1 2abdf04 d23d1fc 85e37e1 d23d1fc 85e37e1 d23d1fc 5d2d2fe 85e37e1 bb4818f e1dae5e f236326 85e37e1 f236326 85e37e1 f236326 e1dae5e 85e37e1 f236326 e1dae5e 85e37e1 f236326 85e37e1 f236326 e1dae5e f236326 85e37e1 9a75d39 85e37e1 5d2d2fe 85e37e1 5d2d2fe 85e37e1 5d2d2fe 85e37e1 9c47a26 85e37e1 5ce3bc6 85e37e1 e1dae5e |
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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
import gradio as gr
from ecologits.tracers.utils import compute_llm_impacts, _avg
from ecologits.impacts.llm import compute_llm_impacts as compute_llm_impacts_expert
from ecologits.impacts.llm import IF_ELECTRICITY_MIX_GWP, IF_ELECTRICITY_MIX_ADPE, IF_ELECTRICITY_MIX_PE
from ecologits.model_repository import models
from src.assets import custom_css
from src.electricity_mix import COUNTRY_CODES, find_electricity_mix
from src.content import (
HERO_TEXT,
ABOUT_TEXT,
CITATION_LABEL,
CITATION_TEXT,
LICENCE_TEXT, METHODOLOGY_TEXT
)
from src.constants import (
PROVIDERS,
OPENAI_MODELS,
ANTHROPIC_MODELS,
COHERE_MODELS,
META_MODELS,
MISTRALAI_MODELS,
PROMPTS,
CLOSED_SOURCE_MODELS,
MODELS,
)
from src.utils import (
format_impacts,
format_energy_eq_physical_activity,
PhysicalActivity,
format_energy_eq_electric_vehicle,
format_gwp_eq_streaming, format_energy_eq_electricity_production, EnergyProduction,
format_gwp_eq_airplane_paris_nyc, format_energy_eq_electricity_consumption_ireland
)
CUSTOM = "Custom"
def model_list(provider: str) -> gr.Dropdown:
if provider == "openai":
return gr.Dropdown(
OPENAI_MODELS,
label="Model",
value=OPENAI_MODELS[0][1],
filterable=True,
)
elif provider == "anthropic":
return gr.Dropdown(
ANTHROPIC_MODELS,
label="Model",
value=ANTHROPIC_MODELS[0][1],
filterable=True,
)
elif provider == "cohere":
return gr.Dropdown(
COHERE_MODELS,
label="Model",
value=COHERE_MODELS[0][1],
filterable=True,
)
elif provider == "huggingface_hub/meta":
return gr.Dropdown(
META_MODELS,
label="Model",
value=META_MODELS[0][1],
filterable=True,
)
elif provider == "mistralai":
return gr.Dropdown(
MISTRALAI_MODELS,
label="Model",
value=MISTRALAI_MODELS[0][1],
filterable=True,
)
def custom():
return CUSTOM
def model_active_params_fn(model_name: str, n_param: float):
if model_name == CUSTOM:
return n_param
provider, model_name = model_name.split('/', 1)
model = models.find_model(provider=provider, model_name=model_name)
return model.active_parameters or _avg(model.active_parameters_range)
def model_total_params_fn(model_name: str, n_param: float):
if model_name == CUSTOM:
return n_param
provider, model_name = model_name.split('/', 1)
model = models.find_model(provider=provider, model_name=model_name)
return model.total_parameters or _avg(model.total_parameters_range)
def mix_fn(country_code: str, mix_adpe: float, mix_pe: float, mix_gwp: float):
if country_code == CUSTOM:
return mix_adpe, mix_pe, mix_gwp
return find_electricity_mix(country_code)
with gr.Blocks(css=custom_css) as demo:
gr.Markdown(HERO_TEXT)
with gr.Tab("🧮 Calculator"):
with gr.Row():
gr.Markdown("# Estimate the environmental impacts of LLM inference")
with gr.Row():
input_provider = gr.Dropdown(
PROVIDERS,
label="Provider",
value=PROVIDERS[0][1],
filterable=True,
)
input_model = gr.Dropdown(
OPENAI_MODELS,
label="Model",
value=OPENAI_MODELS[0][1],
filterable=True,
)
input_provider.change(model_list, input_provider, input_model)
input_prompt = gr.Dropdown(
PROMPTS,
label="Example prompt",
value=400,
)
@gr.render(inputs=[input_provider, input_model, input_prompt])
def render_simple(provider, model, prompt):
if provider.startswith("huggingface_hub"):
provider = provider.split("/")[0]
if models.find_model(provider, model) is not None:
impacts = compute_llm_impacts(
provider=provider,
model_name=model,
output_token_count=prompt,
request_latency=100000
)
impacts = format_impacts(impacts)
# Inference impacts
with gr.Blocks():
if f"{provider}/{model}" in CLOSED_SOURCE_MODELS:
with gr.Row():
gr.Markdown("""<p> ⚠️ You've selected a closed-source model; be aware that the impact's
precision is low due to a lack of transparency from the provider. (see FAQ in About)
</p>""", elem_classes="warning-box")
with gr.Row():
gr.Markdown("""
## Environmental impacts
To understand how the environmental impacts are computed go to the 📖 Methodology tab.
""")
with gr.Row():
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">⚡️ Energy</h2>
$$ \Large {impacts.energy.magnitude:.3g} \ \large {impacts.energy.units} $$
<p align="center"><i>Evaluates the electricity consumption<i></p><br>
""")
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">🌍️ GHG Emissions</h2>
$$ \Large {impacts.gwp.magnitude:.3g} \ \large {impacts.gwp.units} $$
<p align="center"><i>Evaluates the effect on global warming<i></p><br>
""")
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">🪨 Abiotic Resources</h2>
$$ \Large {impacts.adpe.magnitude:.3g} \ \large {impacts.adpe.units} $$
<p align="center"><i>Evaluates the use of metals and minerals<i></p><br>
""")
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">⛽️ Primary Energy</h2>
$$ \Large {impacts.pe.magnitude:.3g} \ \large {impacts.pe.units} $$
<p align="center"><i>Evaluates the use of energy resources<i></p><br>
""")
# Impacts equivalents
with gr.Blocks():
with gr.Row():
gr.Markdown("""
---
## That's equivalent to...
Making this request to the LLM is equivalent to the following actions.
""")
with gr.Row():
physical_activity, distance = format_energy_eq_physical_activity(impacts.energy)
if physical_activity == PhysicalActivity.WALKING:
physical_activity = "🚶 " + physical_activity.capitalize()
if physical_activity == PhysicalActivity.RUNNING:
physical_activity = "🏃 " + physical_activity.capitalize()
with gr.Column(scale=1, min_width=300):
gr.Markdown(f"""
<h2 align="center">{physical_activity} $$ \Large {distance.magnitude:.3g}\ {distance.units} $$ </h2>
<p align="center"><i>Based on energy consumption<i></p><br>
""", latex_delimiters=[{"left": "$$", "right": "$$", "display": False}])
ev_eq = format_energy_eq_electric_vehicle(impacts.energy)
with gr.Column(scale=1, min_width=300):
gr.Markdown(f"""
<h2 align="center">🔋 Electric Vehicle $$ \Large {ev_eq.magnitude:.3g}\ {ev_eq.units} $$ </h2>
<p align="center"><i>Based on energy consumption<i></p><br>
""", latex_delimiters=[{"left": "$$", "right": "$$", "display": False}])
streaming_eq = format_gwp_eq_streaming(impacts.gwp)
with gr.Column(scale=1, min_width=300):
gr.Markdown(f"""
<h2 align="center">⏯️ Streaming $$ \Large {streaming_eq.magnitude:.3g}\ {streaming_eq.units} $$ </h2>
<p align="center"><i>Based on GHG emissions<i></p><br>
""", latex_delimiters=[{"left": "$$", "right": "$$", "display": False}])
# Bigger scale impacts equivalent
with gr.Blocks():
with gr.Row():
gr.Markdown("""
## What if 1% of the planet does this request everyday for 1 year?
If this use case is largely deployed around the world the equivalent impacts would be. (The
impacts of this request x 1% of 8 billion people x 365 days in a year.)
""")
with gr.Row():
electricity_production, count = format_energy_eq_electricity_production(impacts.energy)
if electricity_production == EnergyProduction.NUCLEAR:
emoji = "☢️"
name = "Nuclear power plants"
if electricity_production == EnergyProduction.WIND:
emoji = "💨️ "
name = "Wind turbines"
with gr.Column(scale=1, min_width=300):
gr.Markdown(f"""
<h2 align="center">{emoji} $$ \Large {count:.0f} $$ {name} <span style="font-size: 12px">(yearly)</span></h2>
<p align="center"><i>Based on electricity consumption<i></p><br>
""", latex_delimiters=[{"left": "$$", "right": "$$", "display": False}])
ireland_count = format_energy_eq_electricity_consumption_ireland(impacts.energy)
with gr.Column(scale=1, min_width=300):
gr.Markdown(f"""
<h2 align="center">🇮🇪 $$ \Large {ireland_count:.2g} $$ x Ireland <span style="font-size: 12px">(yearly ⚡️ cons.)</span></h2>
<p align="center"><i>Based on electricity consumption<i></p><br>
""", latex_delimiters=[{"left": "$$", "right": "$$", "display": False}])
paris_nyc_airplane = format_gwp_eq_airplane_paris_nyc(impacts.gwp)
with gr.Column(scale=1, min_width=300):
gr.Markdown(f"""
<h2 align="center">✈️ $$ \Large {paris_nyc_airplane:,.0f} $$ Paris ↔ NYC </h2>
<p align="center"><i>Based on GHG emissions<i></p><br>
""", latex_delimiters=[{"left": "$$", "right": "$$", "display": False}])
with gr.Tab("🤓 Expert Mode"):
with gr.Row():
gr.Markdown("# 🤓 Expert mode")
model = gr.Dropdown(
MODELS + [CUSTOM],
label="Model name",
value="openai/gpt-3.5-turbo",
filterable=True,
interactive=True
)
input_model_active_params = gr.Number(
label="Number of billions of active parameters",
value=45.0,
interactive=True
)
input_model_total_params = gr.Number(
label="Number of billions of total parameters",
value=45.0,
interactive=True
)
model.change(fn=model_active_params_fn,
inputs=[model, input_model_active_params],
outputs=[input_model_active_params])
model.change(fn=model_total_params_fn,
inputs=[model, input_model_total_params],
outputs=[input_model_total_params])
input_model_active_params.input(fn=custom, outputs=[model])
input_model_total_params.input(fn=custom, outputs=[model])
input_tokens = gr.Number(
label="Output tokens",
value=100
)
mix = gr.Dropdown(
COUNTRY_CODES + [CUSTOM],
label="Location",
value="WOR",
filterable=True,
interactive=True
)
input_mix_gwp = gr.Number(
label="Electricity mix - GHG emissions [kgCO2eq / kWh]",
value=IF_ELECTRICITY_MIX_GWP,
interactive=True
)
input_mix_adpe = gr.Number(
label="Electricity mix - Abiotic resources [kgSbeq / kWh]",
value=IF_ELECTRICITY_MIX_ADPE,
interactive=True
)
input_mix_pe = gr.Number(
label="Electricity mix - Primary energy [MJ / kWh]",
value=IF_ELECTRICITY_MIX_PE,
interactive=True
)
mix.change(fn=mix_fn,
inputs=[mix, input_mix_adpe, input_mix_pe, input_mix_gwp],
outputs=[input_mix_adpe, input_mix_pe, input_mix_gwp])
input_mix_gwp.input(fn=custom, outputs=mix)
input_mix_adpe.input(fn=custom, outputs=mix)
input_mix_pe.input(fn=custom, outputs=mix)
@gr.render(inputs=[
input_model_active_params,
input_model_total_params,
input_tokens,
input_mix_gwp,
input_mix_adpe,
input_mix_pe
])
def render_expert(
model_active_params,
model_total_params,
tokens,
mix_gwp,
mix_adpe,
mix_pe
):
impacts = compute_llm_impacts_expert(
model_active_parameter_count=model_active_params,
model_total_parameter_count=model_total_params,
output_token_count=tokens,
request_latency=100000,
if_electricity_mix_gwp=mix_gwp,
if_electricity_mix_adpe=mix_adpe,
if_electricity_mix_pe=mix_pe
)
impacts = format_impacts(impacts)
with gr.Blocks():
with gr.Row():
gr.Markdown("## Environmental impacts")
with gr.Row():
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">⚡️ Energy</h2>
$$ \Large {impacts.energy.magnitude:.3g} \ \large {impacts.energy.units} $$
<p align="center"><i>Evaluates the electricity consumption<i></p><br>
""")
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">🌍️ GHG Emissions</h2>
$$ \Large {impacts.gwp.magnitude:.3g} \ \large {impacts.gwp.units} $$
<p align="center"><i>Evaluates the effect on global warming<i></p><br>
""")
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">🪨 Abiotic Resources</h2>
$$ \Large {impacts.adpe.magnitude:.3g} \ \large {impacts.adpe.units} $$
<p align="center"><i>Evaluates the use of metals and minerals<i></p><br>
""")
with gr.Column(scale=1, min_width=220):
gr.Markdown(f"""
<h2 align="center">⛽️ Primary Energy</h2>
$$ \Large {impacts.pe.magnitude:.3g} \ \large {impacts.pe.units} $$
<p align="center"><i>Evaluates the use of energy resources<i></p><br>
""")
with gr.Tab("📖 Methodology"):
gr.Markdown(METHODOLOGY_TEXT,
elem_classes="descriptive-text",
latex_delimiters=[
{"left": "$$", "right": "$$", "display": True},
{"left": "$", "right": "$", "display": False}
])
with gr.Tab("ℹ️ About"):
gr.Markdown(ABOUT_TEXT, elem_classes="descriptive-text",)
with gr.Accordion("📚 Citation", open=False):
gr.Textbox(
value=CITATION_TEXT,
label=CITATION_LABEL,
interactive=False,
show_copy_button=True,
lines=len(CITATION_TEXT.split('\n')),
)
# License
gr.Markdown(LICENCE_TEXT)
if __name__ == '__main__':
demo.launch()
|