Skip to content

Commit

Permalink
test: update tests with wcf + mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbanse committed Dec 12, 2024
1 parent 1c97f63 commit 569e940
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 66 deletions.
2 changes: 1 addition & 1 deletion ecologits/_ecologits.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class _Config:
@staticmethod
def init(
providers: Optional[Union[str, list[str]]] = None,
electricity_zone: Optional[str] = "WOR",
electricity_zone: str = "WOR",
) -> None:
"""
Initialization static method. Will attempt to initialize all providers by default.
Expand Down
5 changes: 3 additions & 2 deletions ecologits/impacts/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,9 @@ def compute_llm_impacts(
else:
total_params = [model_total_parameter_count, model_total_parameter_count]

results = {}
fields = ["request_energy", "request_usage_gwp", "request_usage_adpe", "request_usage_pe", "request_usage_wcf", "request_embodied_gwp", "request_embodied_adpe", "request_embodied_pe"]
results: dict[str, Union[RangeValue, float, int]] = {}
fields = ["request_energy", "request_usage_gwp", "request_usage_adpe", "request_usage_pe",
"request_usage_wcf", "request_embodied_gwp", "request_embodied_adpe", "request_embodied_pe"]
for act_param, tot_param in zip(active_params, total_params):
res = compute_llm_impacts_dag(
model_active_parameter_count=act_param,
Expand Down
16 changes: 10 additions & 6 deletions ecologits/tracers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def llm_impacts(
model_name: str,
output_token_count: int,
request_latency: float,
electricity_zone: Optional[str] = DEFAULT_ZONE,
electricity_zone: str = DEFAULT_ZONE,
) -> Optional[Impacts]:
"""
High-level function to compute the impacts of an LLM generation request.
Expand Down Expand Up @@ -57,12 +57,16 @@ def llm_impacts(
if electricity_wue is None:
logger.warning(f"Could not find zone `{electricity_zone}` in the electricty \
WUE database (WRI), world average used instead.")
electricity_wue = electricity_wue_list.find_electricity_mix(zone=DEFAULT_ZONE)
electricity_wue = electricity_wue_list.find_electricity_wue(zone=DEFAULT_ZONE)

if electricity_mix is not None:
if_electricity_mix_adpe=electricity_mix.adpe
if_electricity_mix_pe=electricity_mix.pe
if_electricity_mix_gwp=electricity_mix.gwp

if electricity_wue is not None:
wue_off_site=electricity_wue.wue

if_electricity_mix_adpe=electricity_mix.adpe
if_electricity_mix_pe=electricity_mix.pe
if_electricity_mix_gwp=electricity_mix.gwp
wue_off_site=electricity_wue.wue
return compute_llm_impacts(
model_active_parameter_count=model_active_params,
model_total_parameter_count=model_total_params,
Expand Down
37 changes: 20 additions & 17 deletions tests/cassettes/test_tracer/test_double_init.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interactions:
host:
- api.openai.com
user-agent:
- OpenAI/Python 1.35.14
- OpenAI/Python 1.55.2
x-stainless-arch:
- arm64
x-stainless-async:
Expand All @@ -25,42 +25,45 @@ interactions:
x-stainless-os:
- MacOS
x-stainless-package-version:
- 1.35.14
- 1.55.2
x-stainless-retry-count:
- '0'
x-stainless-runtime:
- CPython
x-stainless-runtime-version:
- 3.11.8
- 3.13.0
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: !!binary |
H4sIAAAAAAAAAwAAAP//VJDLTsMwEEX3+YrB66ZqStOq2aAiIbVlwQ6QEKocZ5KaOh5jT4Cq6r+j
hPTBxos5PuN7fYgAhC5EBkJtJavamXgxmbmHF10+7++r9dfjKt01r2tc5J+rpwTFoDUo/0DFJ2uo
qHYGWZP9w8qjZGy3JrNxmk6ms3TagZoKNK1WOY5vh2nMjc8pHiXjtDe3pBUGkcFbBABw6M42oy3w
R2QwGpwmNYYgKxTZ+RKA8GTaiZAh6MDSshhcoCLLaLvYSzSGbmBJ36CkhRX8CbCnBpgKub+7Fj2W
TZBtcNsY08+P5ySGKucpDz0/z0ttddhuPMpAtn01MDnR0WME8N41bv6VEM5T7XjDtEPbLkz6wuLy
xxc47xkTS3PlzKM+ngj7wFhvSm0r9M7rrn1X4hj9AgAA//8DAHXm4Sv8AQAA
H4sIAAAAAAAAA4xSy07DMBC85ysWn1vUtJRHL6jiUiRu9IIQilx7m5g6XsveQCvUf0dOH2kFSFx8
mNkZz6z9lQEIo8UEhKokq9rb/lTj5u5j9Gxu5/MwerBPq5f19OPl+akMNBW9pKDFOyo+qC4V1d4i
G3I7WgWUjMk1vxldDfLx9VXeEjVptElWeu6PLsd9bsKC+oN8ON4rKzIKo5jAawYA8NWeKaPTuBYT
GPQOSI0xyhLF5DgEIALZhAgZo4ksHYteRypyjK6NPUNrCbjCgBcwo09Q0sEj7GSwoQaYtNzcn8oD
LpsoU3zXWLvHt8c8lkofaBH3/BFfGmdiVQSUkVy6OzJ50bLbDOCt7d2cVRE+UO25YFqhS4b5vrbo
Nv0LycTSdvjwgJ+5FRpZGhtP9iaUVBXqTtktWTba0AmRnXT+GeY3711v48r/2HeEUugZdeEDaqPO
C3djAdM//GvsuOM2sIibyFgXS+NKDD6Y9ie0T7nNvgEAAP//AwD1GB88CAMAAA==
headers:
CF-Cache-Status:
- DYNAMIC
CF-RAY:
- 8be6edf909c883dd-BRU
- 8f0e95de2dccb738-BRU
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json
Date:
- Thu, 05 Sep 2024 14:32:36 GMT
- Thu, 12 Dec 2024 15:00:41 GMT
Server:
- cloudflare
Set-Cookie:
- __cf_bm=0Dq48UgAzdubvIw15c1GKEAwDNy6IudJa9i6BfdMqOM-1725546756-1.0.1.1-hZ.GO_oFkEz1r2n03.vZzQVnHkdIMTZfs0.iA_kHA8IhVX3bFbOfM8sQ4EjzbFGnFP_TjX18_j05PXks57j4Rg;
path=/; expires=Thu, 05-Sep-24 15:02:36 GMT; domain=.api.openai.com; HttpOnly;
- __cf_bm=ao9d6nWB4HOfeLY_HD0_A8sRb0.YfYEn9R0mqOj2hKI-1734015641-1.0.1.1-kJOHNt.T1nNnB7jgCtuWrqemiGAiYRXjaUSQKZ2XoFtoGZHrY.Cu4lExem5B598juF1IA0Wd_IoNL1CTK3BjAw;
path=/; expires=Thu, 12-Dec-24 15:30:41 GMT; domain=.api.openai.com; HttpOnly;
Secure; SameSite=None
- _cfuvid=zrmwURGxG_7NS3UfKZFgFN6_HpYkYGsut0ri3BW3lSo-1725546756550-0.0.1.1-604800000;
- _cfuvid=3diB6_XnwjywmAD0RKmZCd8EhpJolgB2fRaLp_36ke8-1734015641664-0.0.1.1-604800000;
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
Transfer-Encoding:
- chunked
Expand All @@ -73,11 +76,11 @@ interactions:
openai-organization:
- user-9oylvgpazsbmej0gab9qijx2
openai-processing-ms:
- '243'
- '212'
openai-version:
- '2020-10-01'
strict-transport-security:
- max-age=15552000; includeSubDomains; preload
- max-age=31536000; includeSubDomains; preload
x-ratelimit-limit-requests:
- '10000'
x-ratelimit-limit-tokens:
Expand All @@ -91,7 +94,7 @@ interactions:
x-ratelimit-reset-tokens:
- 6ms
x-request-id:
- req_cb1479c0fce5046d25276757bc40d29b
- req_0ac1b143952a1ad7d0a4d2ae9663eb98
status:
code: 200
message: OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interactions:
host:
- api.openai.com
user-agent:
- OpenAI/Python 1.35.14
- OpenAI/Python 1.55.2
x-stainless-arch:
- arm64
x-stainless-async:
Expand All @@ -26,42 +26,45 @@ interactions:
x-stainless-os:
- MacOS
x-stainless-package-version:
- 1.35.14
- 1.55.2
x-stainless-retry-count:
- '0'
x-stainless-runtime:
- CPython
x-stainless-runtime-version:
- 3.11.8
- 3.13.0
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: !!binary |
H4sIAAAAAAAAA1SQPW/CMBCG9/yKq2eCgDalyVJRMdCpS1laVcg4l+DW8bn2RYAQ/71yCB9dPNzj
5/y+PiQAQpeiAKE2klXjTDrLdv6Nl78Gl7O5nCNOpx/b9/Ihr+hlLgbRoPU3Kj5bQ0WNM8ia7Akr
j5Ixbh1PJ1n+lI1Hjx1oqEQTtdpxej/MUm79mtLReJL15oa0wiAK+EwAAA7dGTPaEneigNHgPGkw
BFmjKC6XAIQnEydChqADS8ticIWKLKPtYi/QGLqDBW1BSQuvcBJgTy0wlXL/fCt6rNogY3DbGtPP
j5ckhmrnaR16fplX2uqwWXmUgWx8NTA50dFjAvDVNW7/lRDOU+N4xfSDNi4c94XF9Y+vMO8ZE0tz
4+RJH0+EfWBsVpW2NXrndde+K3FM/gAAAP//AwDQe9mQ/AEAAA==
H4sIAAAAAAAAA4xSQW7bMBC86xVbnu1ASuy29iXIIWlyyjkoCoEm1zQTikuQK6Ry4L8XlGVLRlqg
Fx5mdoYzS34UAMJqsQahdpJVE9z8TmO3eni5N2b//rw3++b+bvH6o6y670+lEbOsoM0rKj6prhQ1
wSFb8kdaRZSM2bX6drMoq+XXRdUTDWl0WWYCz2+ulnNu44bmZXW9HJQ7sgqTWMPPAgDgoz9zRq/x
t1hDOTshDaYkDYr1eQhARHIZETIlm1h6FrORVOQZfR/7EZ2jL/BI76Ckhyc4CqCjFpi07G6nwojb
Nskc3LfODfjhnMSRCZE2aeDP+NZ6m3Z1RJnI51sTUxA9eygAfvWN24sSIkRqAtdMb+izYTUUFuOO
R3I1cEws3URzwi/Mao0srUuThQkl1Q71qBy3K1ttaUIUk8qfs/zN+1jbevM/9iOhFAZGXYeI2qrL
vuNYxPwB/zV2XnEfWKQuMTb11nqDMUTbf4H+JQ/FHwAAAP//AwBN+YemAQMAAA==
headers:
CF-Cache-Status:
- DYNAMIC
CF-RAY:
- 8c10bbdf0b3483db-BRU
- 8f0e95e0cad9ba5d-BRU
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json
Date:
- Tue, 10 Sep 2024 16:18:26 GMT
- Thu, 12 Dec 2024 15:00:42 GMT
Server:
- cloudflare
Set-Cookie:
- __cf_bm=Gvm_KBYRBjUpgiyFKJx0aRQsBXcBJk2yN2W0Hyca_k4-1725985106-1.0.1.1-yTtLJ0yD2vGNUKJMwKVP0jvusLe09wXtqyuFLZC40vxKEuLKDWPFSrBa_.LxpXh_GTq820KHqHRNuZib5ZWduQ;
path=/; expires=Tue, 10-Sep-24 16:48:26 GMT; domain=.api.openai.com; HttpOnly;
- __cf_bm=YQp5EuQdREMqkY7UP5y4EQnZyd4z1RoQ8VdT9kWQfc0-1734015642-1.0.1.1-rsl5gvRJIVWv4XGt0P10jRFJtdxcHjtPaPbk76OqqNglij467r7B6VUAxnuqM6hVWw_EKGTjjNNTcCWDbVYa2g;
path=/; expires=Thu, 12-Dec-24 15:30:42 GMT; domain=.api.openai.com; HttpOnly;
Secure; SameSite=None
- _cfuvid=sS_7MkQkzMd8l92wE7eI0YjSjSl1QutNgtcsrB.zN9Q-1725985106637-0.0.1.1-604800000;
- _cfuvid=5b9xgoL.FixpmFAjiM2okF6qadgFyGsPylvyHk83QYo-1734015642089-0.0.1.1-604800000;
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
Transfer-Encoding:
- chunked
Expand All @@ -74,25 +77,25 @@ interactions:
openai-organization:
- user-9oylvgpazsbmej0gab9qijx2
openai-processing-ms:
- '253'
- '205'
openai-version:
- '2020-10-01'
strict-transport-security:
- max-age=15552000; includeSubDomains; preload
- max-age=31536000; includeSubDomains; preload
x-ratelimit-limit-requests:
- '10000'
x-ratelimit-limit-tokens:
- '200000'
x-ratelimit-remaining-requests:
- '9999'
- '9998'
x-ratelimit-remaining-tokens:
- '199980'
x-ratelimit-reset-requests:
- 8.64s
- 16.855s
x-ratelimit-reset-tokens:
- 6ms
x-request-id:
- req_1a8fbf25d778e625875d9c29c34e499e
- req_787134980a8fc1f17ec3c72d05afb1b4
status:
code: 200
message: OK
Expand All @@ -111,12 +114,12 @@ interactions:
content-type:
- application/json
cookie:
- __cf_bm=Gvm_KBYRBjUpgiyFKJx0aRQsBXcBJk2yN2W0Hyca_k4-1725985106-1.0.1.1-yTtLJ0yD2vGNUKJMwKVP0jvusLe09wXtqyuFLZC40vxKEuLKDWPFSrBa_.LxpXh_GTq820KHqHRNuZib5ZWduQ;
_cfuvid=sS_7MkQkzMd8l92wE7eI0YjSjSl1QutNgtcsrB.zN9Q-1725985106637-0.0.1.1-604800000
- __cf_bm=YQp5EuQdREMqkY7UP5y4EQnZyd4z1RoQ8VdT9kWQfc0-1734015642-1.0.1.1-rsl5gvRJIVWv4XGt0P10jRFJtdxcHjtPaPbk76OqqNglij467r7B6VUAxnuqM6hVWw_EKGTjjNNTcCWDbVYa2g;
_cfuvid=5b9xgoL.FixpmFAjiM2okF6qadgFyGsPylvyHk83QYo-1734015642089-0.0.1.1-604800000
host:
- api.openai.com
user-agent:
- OpenAI/Python 1.35.14
- OpenAI/Python 1.55.2
x-stainless-arch:
- arm64
x-stainless-async:
Expand All @@ -126,35 +129,38 @@ interactions:
x-stainless-os:
- MacOS
x-stainless-package-version:
- 1.35.14
- 1.55.2
x-stainless-retry-count:
- '0'
x-stainless-runtime:
- CPython
x-stainless-runtime-version:
- 3.11.8
- 3.13.0
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: !!binary |
H4sIAAAAAAAAAwAAAP//VJA9b8IwEIb3/IqrZ4IIVShkqRALnWhVhqpVhYxzBLeOz7IvLQjx3yuH
8NHFwz1+zu/rQwIgdCkKEGorWdXOpNN85xfZ/O1lic/ZbDpdLNVriWhno/H7j+hFg9ZfqPhs9RXV
ziBrsiesPErGuDV7GOaTcZ4NRi2oqUQTtcpxet/PU278mtJBNsw7c0taYRAFfCQAAIf2jBltiTtR
wKB3ntQYgqxQFJdLAMKTiRMhQ9CBpWXRu0JFltG2sedoDN3BnH5BSQtPcBJgTw0wlXL/eCt63DRB
xuC2MaabHy9JDFXO0zp0/DLfaKvDduVRBrLx1cDkREuPCcBn27j5V0I4T7XjFdM32rgw6wqL6x9f
4aRjTCzNjTNJungi7ANjvdpoW6F3Xrft2xLH5A8AAP//AwApB2hX/AEAAA==
H4sIAAAAAAAAAwAAAP//jFLBauMwFLz7K151TkriJFuaSykLJYXSy8ISWBYjSy+OtrKekJ5pTci/
L3Kc2KG70IsOM29GM086ZADCaLEGofaSVe3t9FFj+7h9LXPFi/Klyrfvu++b/OfT8sc9zsQkKaj8
g4rPqltFtbfIhtyJVgElY3Kd3y2Ws/nq2zLviJo02iSrPE8Xt6spN6Gk6Wyer3rlnozCKNbwKwMA
OHRnyug0fog1zCZnpMYYZYVifRkCEIFsQoSM0USWjsVkIBU5RtfF3qC1dAMbegclHTzDSQAtNcCk
ZfswFgbcNVGm4K6xtsePlySWKh+ojD1/wXfGmbgvAspILt0ambzo2GMG8Ltr3FyVED5Q7blgekOX
DOd9YTHseCDve46JpR1pzviVWaGRpbFxtDChpNqjHpTDdmWjDY2IbFT5c5Z/eZ9qG1d9xX4glELP
qAsfUBt13XcYC5g+4P/GLivuAovYRsa62BlXYfDBdF+ge8lj9hcAAP//AwAsu4omAQMAAA==
headers:
CF-Cache-Status:
- DYNAMIC
CF-RAY:
- 8c10bbe52ea383db-BRU
- 8f0e95e3b80fba5d-BRU
Connection:
- keep-alive
Content-Encoding:
- gzip
Content-Type:
- application/json
Date:
- Tue, 10 Sep 2024 16:18:27 GMT
- Thu, 12 Dec 2024 15:00:42 GMT
Server:
- cloudflare
Transfer-Encoding:
Expand All @@ -168,25 +174,25 @@ interactions:
openai-organization:
- user-9oylvgpazsbmej0gab9qijx2
openai-processing-ms:
- '236'
- '252'
openai-version:
- '2020-10-01'
strict-transport-security:
- max-age=15552000; includeSubDomains; preload
- max-age=31536000; includeSubDomains; preload
x-ratelimit-limit-requests:
- '10000'
x-ratelimit-limit-tokens:
- '200000'
x-ratelimit-remaining-requests:
- '9998'
- '9997'
x-ratelimit-remaining-tokens:
- '199980'
x-ratelimit-reset-requests:
- 16.602s
- 25.047s
x-ratelimit-reset-tokens:
- 6ms
x-request-id:
- req_cbcf7fe669bdc3adc64f13585f8c6dcb
- req_c44d228d60613fd17c2df8fd10f104ca
status:
code: 200
message: OK
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions tests/test_electricity_wue_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from ecologits.repositories.electricity_wue_repository import ElectricityWUERepository, ElectricityWUE


def test_create_electricity_wue_repository_default():
electricity_wue_list = ElectricityWUERepository.from_csv()
assert isinstance(electricity_wue_list, ElectricityWUERepository)
assert electricity_wue_list.find_electricity_wue(zone="BEL") is not None


def test_create_electricity_wue_repository_from_scratch():
electricity_wue_list = ElectricityWUERepository([
ElectricityWUE(
zone="wonderland",
wue=0.,
)
])
assert electricity_wue_list.find_electricity_wue(zone="wonderland") is not None


def test_find_unknown_zone():
electricity_wue_list = ElectricityWUERepository.from_csv()
assert electricity_wue_list.find_electricity_wue(zone="AAA") is None

4 changes: 4 additions & 0 deletions tests/test_llm_impacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ def test_compute_llm_impacts(model_active_parameter_count: float,
assert impacts.gwp.value > 0
assert impacts.adpe.value > 0
assert impacts.pe.value > 0
assert impacts.wcf.value > 0
assert impacts.usage.energy.value > 0
assert impacts.usage.gwp.value > 0
assert impacts.usage.adpe.value > 0
assert impacts.usage.pe.value > 0
assert impacts.usage.wcf.value > 0
assert impacts.embodied.gwp.value > 0
assert impacts.embodied.adpe.value > 0
assert impacts.embodied.pe.value > 0
Expand All @@ -49,10 +51,12 @@ def compare_impacts(impacts: Impacts, prev_impacts: Impacts, op=gt):
assert op(impacts.gwp, prev_impacts.gwp)
assert op(impacts.adpe, prev_impacts.adpe)
assert op(impacts.pe, prev_impacts.pe)
assert op(impacts.wcf, prev_impacts.wcf)
assert op(impacts.usage.energy, prev_impacts.usage.energy)
assert op(impacts.usage.gwp, prev_impacts.usage.gwp)
assert op(impacts.usage.adpe, prev_impacts.usage.adpe)
assert op(impacts.usage.pe, prev_impacts.usage.pe)
assert op(impacts.usage.wcf, prev_impacts.usage.wcf)
assert op(impacts.embodied.gwp, prev_impacts.embodied.gwp)
assert op(impacts.embodied.adpe, prev_impacts.embodied.adpe)
assert op(impacts.embodied.pe, prev_impacts.embodied.pe)
Expand Down
Loading

0 comments on commit 569e940

Please sign in to comment.