Skip to content

Commit 9112997

Browse files
authored
Merge pull request #735 from mlco2/codecarbon_v3_rc
Codecarbon version 3.0.0
2 parents a7d78ef + a5e001e commit 9112997

File tree

96 files changed

+7965
-559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+7965
-559
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ jobs:
4444
APP_NAME: cc_dashboard_prod
4545
run: |
4646
./clever-tools-latest_linux/clever link $CLEVER_APP_ID
47-
./clever-tools-latest_linux/clever deploy -f -a $APP_NAME --quiet
47+
./clever-tools-latest_linux/clever deploy -f -a $APP_NAME --quiet

.github/workflows/package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@ jobs:
9393
run: mamba install --channel file:///tmp/conda-bld --channel codecarbon codecarbon
9494
- name: Test conda package
9595
shell: bash -l {0}
96-
run: codecarbon --help
96+
run: codecarbon --help

.github/workflows/release-drafter.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ name: Release Drafter
33
on:
44
push:
55
# branches to consider in the event; optional, defaults to all
6-
branches:
7-
- master
6+
branches: [master]
87

98
jobs:
109
update_release_draft:
1110
runs-on: ubuntu-latest
1211
steps:
1312
# Drafts your next Release notes as Pull Requests are merged into "master"
14-
- uses: release-drafter/release-drafter@v5.7.0
13+
- uses: release-drafter/release-drafter@v6
1514
env:
1615
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

CONTRIBUTING.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ Here is the launch.json to be able to debug examples and tests:
250250
{
251251
"version": "0.2.0",
252252
"configurations": [
253+
253254
{
254255
"name": "Python: Current File",
255-
"type": "python",
256+
"type": "debugpy",
256257
"request": "launch",
257258
"program": "${file}",
258259
"console": "integratedTerminal",
@@ -261,13 +262,17 @@ Here is the launch.json to be able to debug examples and tests:
261262
},
262263
{
263264
"name": "PyTest: Current File",
264-
"type": "python",
265+
"type": "debugpy",
265266
"request": "launch",
266267
"module": "pytest",
267-
"args": ["${file}"],
268+
"args": [
269+
"-s",
270+
"${file}"
271+
],
268272
"console": "integratedTerminal",
269273
"justMyCode": true,
270-
"env": { "PYTHONPATH": "${workspaceRoot}" }
274+
"env": { "PYTHONPATH": "${workspaceRoot}",
275+
"CODECARBON_ALLOW_MULTIPLE_RUNS": "True" }
271276
},
272277
{
273278
"name": "PyTest: codecarbon monitor",
@@ -279,7 +284,7 @@ Here is the launch.json to be able to debug examples and tests:
279284
],
280285
"console": "integratedTerminal",
281286
"justMyCode": true,
282-
"env": { "PYTHONPATH": "${workspaceRoot}" }
287+
"env": { "PYTHONPATH": "${workspaceRoot}"}
283288
}
284289
]
285290
}
@@ -350,7 +355,7 @@ to regenerate the html files.
350355
### Release process
351356

352357
- Merge all PRs.
353-
- Create a PR bumping the version with `hatch run dev:bumpver update --patch`.
358+
- Create a PR bumping the version with `hatch run dev:bumpver update --patch`. For a release candidate, use `hatch run dev:bumpver update --set-version 3.0.0_rc1`.
354359
- Run `hatch run python3 .github/check_version.py` to check version consistancy.
355360
- Update the dependencies with `hatch-pip-compile --upgrade --all`.
356361
- [Build Documentation](#documentation) if needed with `hatch run docs:build`.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,9 @@ Here is a sample for BibTeX:
172172
# Contact 📝
173173

174174
Maintainers are [@vict0rsch](https://github.com/vict0rsch) [@benoit-cty](https://github.com/benoit-cty) and [@SaboniAmine](https://github.com/saboniamine). Codecarbon is developed by volunteers from [**Mila**](http://mila.quebec) and the [**DataForGoodFR**](https://twitter.com/dataforgood_fr) community alongside donated professional time of engineers at [**Comet.ml**](https://comet.ml) and [**BCG GAMMA**](https://www.bcg.com/en-nl/beyond-consulting/bcg-gamma/default).
175+
176+
## Star History
177+
178+
Comparison of the number of stars accumulated by the different Python CO2 emissions projects:
179+
[![Star History Chart](https://api.star-history.com/svg?repos=mlco2/codecarbon,lfwa/carbontracker,sb-ai-lab/Eco2AI,fvaleye/tracarbon,Breakend/experiment-impact-tracker&type=Date)](https://star-history.com/#mlco2/codecarbon&lfwa/carbontracker&sb-ai-lab/Eco2AI&fvaleye/tracarbon&Breakend/experiment-impact-tracker&Date)
180+

codecarbon/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.8.4"
1+
__version__ = "3.0.0"

codecarbon/core/api_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ def _create_run(self, experiment_id: str):
264264
gpu_count=self.conf.get("gpu_count"),
265265
gpu_model=self.conf.get("gpu_model"),
266266
# Reduce precision for Privacy
267-
longitude=round(self.conf.get("longitude"), 1),
268-
latitude=round(self.conf.get("latitude"), 1),
267+
longitude=round(self.conf.get("longitude", 0), 1),
268+
latitude=round(self.conf.get("latitude", 0), 1),
269269
region=self.conf.get("region"),
270270
provider=self.conf.get("provider"),
271271
ram_total_size=self.conf.get("ram_total_size"),

codecarbon/core/cpu.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
"""
66

77
import os
8+
import re
89
import shutil
910
import subprocess
1011
import sys
1112
from typing import Dict, Optional, Tuple
1213

1314
import pandas as pd
15+
import psutil
1416
from rapidfuzz import fuzz, process, utils
1517

1618
from codecarbon.core.rapl import RAPLFile
@@ -19,6 +21,9 @@
1921
from codecarbon.external.logger import logger
2022
from codecarbon.input import DataSource
2123

24+
# default W value per core for a CPU if no model is found in the ref csv
25+
DEFAULT_POWER_PER_CORE = 4
26+
2227

2328
def is_powergadget_available() -> bool:
2429
"""
@@ -58,6 +63,24 @@ def is_rapl_available() -> bool:
5863
return False
5964

6065

66+
def is_psutil_available():
67+
try:
68+
nice = psutil.cpu_times().nice
69+
if nice > 0.0001:
70+
return True
71+
else:
72+
logger.debug(
73+
f"is_psutil_available() : psutil.cpu_times().nice is too small : {nice} !"
74+
)
75+
return False
76+
except Exception as e:
77+
logger.debug(
78+
"Not using the psutil interface, an exception occurred while instantiating "
79+
+ f"psutil.cpu_times : {e}",
80+
)
81+
return False
82+
83+
6184
class IntelPowerGadget:
6285
"""
6386
A class to interface with Intel Power Gadget for monitoring CPU power consumption on Windows and (non-Apple Silicon) macOS.
@@ -237,7 +260,7 @@ class IntelRAPL:
237260
238261
"""
239262

240-
def __init__(self, rapl_dir="/sys/class/powercap/intel-rapl"):
263+
def __init__(self, rapl_dir="/sys/class/powercap/intel-rapl/subsystem"):
241264
self._lin_rapl_dir = rapl_dir
242265
self._system = sys.platform.lower()
243266
self._rapl_files = []
@@ -275,6 +298,8 @@ def _fetch_rapl_files(self) -> None:
275298
with open(path) as f:
276299
name = f.read().strip()
277300
# Fake the name used by Power Gadget
301+
# We ignore "core" in name as it seems to be included in "package" for Intel CPU.
302+
# TODO: Use "dram" for memory power
278303
if "package" in name:
279304
name = f"Processor Energy Delta_{i}(kWh)"
280305
i += 1
@@ -294,7 +319,7 @@ def _fetch_rapl_files(self) -> None:
294319
logger.debug("We will read Intel RAPL files at %s", rapl_file)
295320
except PermissionError as e:
296321
raise PermissionError(
297-
"Unable to read Intel RAPL files for CPU power, we will use a constant for your CPU power."
322+
"PermissionError : Unable to read Intel RAPL files for CPU power, we will use a constant for your CPU power."
298323
+ " Please view https://github.com/mlco2/codecarbon/issues/244"
299324
+ " for workarounds : %s",
300325
e,
@@ -332,8 +357,6 @@ def get_static_cpu_details(self) -> Dict:
332357
"""
333358
Return CPU details without computing them.
334359
"""
335-
logger.debug("get_static_cpu_details %s", self._cpu_details)
336-
337360
return self._cpu_details
338361

339362
def start(self) -> None:
@@ -426,6 +449,18 @@ def _get_matching_cpu(
426449
start_cpu = model_raw.find(" CPU @ ")
427450
if start_cpu > 0:
428451
model_raw = model_raw[0:start_cpu]
452+
model_raw = model_raw.replace(" CPU", "")
453+
model_raw = re.sub(r" @\s*\d+\.\d+GHz", "", model_raw)
454+
direct_match = process.extractOne(
455+
model_raw,
456+
cpu_df["Name"],
457+
processor=lambda s: s.lower(),
458+
scorer=fuzz.ratio,
459+
score_cutoff=THRESHOLD_DIRECT,
460+
)
461+
462+
if direct_match:
463+
return direct_match[0]
429464
indirect_matches = process.extract(
430465
model_raw,
431466
cpu_df["Name"],
@@ -467,10 +502,18 @@ def _main(self) -> Tuple[str, int]:
467502
+ " Please contact us.",
468503
cpu_model_detected,
469504
)
505+
if is_psutil_available():
506+
# Count thread of the CPU
507+
threads = psutil.cpu_count(logical=True)
508+
estimated_tdp = threads * DEFAULT_POWER_PER_CORE
509+
logger.warning(
510+
f"We will use the default power consumption of {DEFAULT_POWER_PER_CORE} W per thread for your {threads} CPU, so {estimated_tdp}W."
511+
)
512+
return cpu_model_detected, estimated_tdp
470513
return cpu_model_detected, None
471514
logger.warning(
472515
"We were unable to detect your CPU using the `cpuinfo` package."
473-
+ " Resorting to a default power consumption of 85W."
516+
+ " Resorting to a default power consumption."
474517
)
475518
return "Unknown", None
476519

codecarbon/core/emissions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def get_cloud_country_iso_code(self, cloud: CloudMetadata) -> str:
9090
selected = df.loc[flags]
9191
if not len(selected):
9292
raise ValueError(
93-
"Unable to find country name for "
93+
"Unable to find country ISO Code for "
9494
f"cloud_provider={cloud.provider}, "
9595
f"cloud_region={cloud.region}"
9696
)
@@ -105,7 +105,7 @@ def get_cloud_geo_region(self, cloud: CloudMetadata) -> str:
105105
selected = df.loc[flags]
106106
if not len(selected):
107107
raise ValueError(
108-
"Unable to find country name for "
108+
"Unable to find State/City name for "
109109
f"cloud_provider={cloud.provider}, "
110110
f"cloud_region={cloud.region}"
111111
)

0 commit comments

Comments
 (0)