Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ In your command prompt use:
```codecarbon monitor```
The package will track your emissions independently from your code.

### Detecting your hardware 🔍

In your command prompt use:
```codecarbon detect```
The package will detect and print your hardware information (RAM, CPU, GPU).

### In your Python code 🐍
```python
from codecarbon import track_emissions
Expand Down
25 changes: 25 additions & 0 deletions codecarbon/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,31 @@ def signal_handler(signum, frame):
raise e


@codecarbon.command("detect", short_help="Detect hardware and print information.")
def detect():
"""
Detects hardware and prints information without running any measurements.
"""
print("Detecting hardware...")
tracker = EmissionsTracker(save_to_file=False)
hardware_info = tracker.get_detected_hardware()

print("\nDetected Hardware and System Information:")
print(f"- Available RAM: {hardware_info['ram_total_size']:.3f} GB")
print(
f"- CPU count: {hardware_info['cpu_count']} thread(s) in {hardware_info['cpu_physical_count']} physical CPU(s)"
)
print(f"- CPU model: {hardware_info['cpu_model']}")
print(f"- GPU count: {hardware_info['gpu_count']}")

gpu_model_str = hardware_info["gpu_model"]
if hardware_info.get("gpu_ids"):
gpu_model_str += (
f" BUT only tracking these GPU ids : {hardware_info['gpu_ids']}"
)
print(f"- GPU model: {gpu_model_str}")


def questionary_prompt(prompt, list_options, default):
value = questionary.select(
prompt,
Expand Down
35 changes: 27 additions & 8 deletions codecarbon/emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,30 @@ def __init__(
self._active_task_emissions_at_start: Optional[EmissionsData] = None

# Tracking mode detection
ressource_tracker = ResourceTracker(self)
ressource_tracker.set_CPU_GPU_ram_tracking()
self._hardware = []
resource_tracker = ResourceTracker(self)
resource_tracker.set_CPU_GPU_ram_tracking()

self._conf["hardware"] = list(map(lambda x: x.description(), self._hardware))

logger.info(">>> Tracker's metadata:")
logger.info(f" Platform system: {self._conf.get('os')}")
logger.info(f" Python version: {self._conf.get('python_version')}")
logger.info(f" CodeCarbon version: {self._conf.get('codecarbon_version')}")
logger.info(f" Available RAM : {self._conf.get('ram_total_size'):.3f} GB")

hardware_info = self.get_detected_hardware()
logger.info(f" Available RAM : {hardware_info['ram_total_size']:.3f} GB")
logger.info(
f" CPU count: {self._conf.get('cpu_count')} thread(s) in {self._conf.get('cpu_physical_count')} physical CPU(s)"
f" CPU count: {hardware_info['cpu_count']} thread(s) in {hardware_info['cpu_physical_count']} physical CPU(s)"
)
logger.info(f" CPU model: {self._conf.get('cpu_model')}")
logger.info(f" GPU count: {self._conf.get('gpu_count')}")
logger.info(f" CPU model: {hardware_info['cpu_model']}")
logger.info(f" GPU count: {hardware_info['gpu_count']}")
if self._gpu_ids:
logger.info(
f" GPU model: {self._conf.get('gpu_model')} BUT only tracking these GPU ids : {self._conf.get('gpu_ids')}"
f" GPU model: {hardware_info['gpu_model']} BUT only tracking these GPU ids : {hardware_info['gpu_ids']}"
)
else:
logger.info(f" GPU model: {self._conf.get('gpu_model')}")
logger.info(f" GPU model: {hardware_info['gpu_model']}")

# Run `self._measure_power_and_energy` every `measure_power_secs` seconds in a
# background thread
Expand Down Expand Up @@ -469,6 +472,22 @@ def _init_output_methods(self, *, api_key: str = None):
if self._save_to_logfire:
self._output_handlers.append(LogfireOutput())

def get_detected_hardware(self) -> Dict[str, Any]:
"""
Get the detected hardware.
:return: A dictionary containing hardware data.
"""
hardware_info = {
"ram_total_size": self._conf.get("ram_total_size"),
"cpu_count": self._conf.get("cpu_count"),
"cpu_physical_count": self._conf.get("cpu_physical_count"),
"cpu_model": self._conf.get("cpu_model"),
"gpu_count": self._conf.get("gpu_count"),
"gpu_model": self._conf.get("gpu_model"),
"gpu_ids": self._conf.get("gpu_ids"),
}
return hardware_info

def service_shutdown(self, signum, frame):
logger.warning("service_shutdown - Caught signal %d" % signum)
self.stop()
Expand Down
25 changes: 10 additions & 15 deletions docs/_sources/installation.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
Installing CodeCarbon
=====================

Create a virtual environment using `conda` for easier management of dependencies and packages.
For installing conda, follow the instructions on the
`official conda website <https://docs.conda.io/projects/conda/en/latest/user-guide/install>`__

.. code-block:: bash

conda create --name codecarbon
conda activate codecarbon

From PyPi repository
--------------------

Expand All @@ -23,16 +14,20 @@ To install the package, run the following command in your terminal.

pip install codecarbon

From conda repository
---------------------

The package is hosted on the conda repository `here <https://anaconda.org/codecarbon/codecarbon>`__.
Using Conda environments
------------------------

To install the package, run the following command in your terminal.
If you're using Conda for environment management, you can install CodeCarbon with pip in your Conda environment:

.. code-block:: bash

conda install -c codecarbon -c conda-forge codecarbon
conda create --name codecarbon
conda activate codecarbon
pip install codecarbon

.. note::

While CodeCarbon can be used in Conda environments, we no longer maintain Conda packages. We recommend using ``pip install codecarbon`` within your Conda environment, which works seamlessly with Conda.

.. note::

Expand Down
8 changes: 8 additions & 0 deletions docs/_sources/usage.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ If you want to track the emissions of a computer without having to modify your c

You have to stop the monitoring manually with ``Ctrl+C``.

If you want to detect the hardware of your computer without starting any measurement, you can use:

.. code-block:: console

codecarbon detect

It will print the detected RAM, CPU and GPU information.

In the following example you will see how to use the CLI to monitor all the emissions of you computer and sending everything
to an API running on "localhost:8008" (Or you can start a private local API with "docker-compose up"). Using the public API with
this is not supported yet (coming soon!)
Expand Down
8 changes: 0 additions & 8 deletions docs/_static/basic.css
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,6 @@ abbr, acronym {
cursor: help;
}

.translated {
background-color: rgba(207, 255, 207, 0.2)
}

.untranslated {
background-color: rgba(255, 207, 207, 0.2)
}

/* -- code displays --------------------------------------------------------- */

pre {
Expand Down
13 changes: 8 additions & 5 deletions docs/_static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,11 @@ const Search = {
// perform the search on the required terms
searchTerms.forEach((word) => {
const files = [];
// find documents, if any, containing the query word in their text/title term indices
// use Object.hasOwnProperty to avoid mismatching against prototype properties
const arr = [
{ files: terms[word], score: Scorer.term },
{ files: titleTerms[word], score: Scorer.title },
{ files: terms.hasOwnProperty(word) ? terms[word] : undefined, score: Scorer.term },
{ files: titleTerms.hasOwnProperty(word) ? titleTerms[word] : undefined, score: Scorer.title },
];
// add support for partial matches
if (word.length > 2) {
Expand Down Expand Up @@ -547,8 +549,9 @@ const Search = {

// set score for the word in each file
recordFiles.forEach((file) => {
if (!scoreMap.has(file)) scoreMap.set(file, {});
scoreMap.get(file)[word] = record.score;
if (!scoreMap.has(file)) scoreMap.set(file, new Map());
const fileScores = scoreMap.get(file);
fileScores.set(word, record.score);
});
});

Expand Down Expand Up @@ -587,7 +590,7 @@ const Search = {
break;

// select one (max) score for the file.
const score = Math.max(...wordList.map((w) => scoreMap.get(file)[w]));
const score = Math.max(...wordList.map((w) => scoreMap.get(file).get(w)));
// add result to the result list
results.push([
docNames[file],
Expand Down
8 changes: 8 additions & 0 deletions docs/edit/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ If you want to track the emissions of a computer without having to modify your c

You have to stop the monitoring manually with ``Ctrl+C``.

If you want to detect the hardware of your computer without starting any measurement, you can use:

.. code-block:: console

codecarbon detect

It will print the detected RAM, CPU and GPU information.

In the following example you will see how to use the CLI to monitor all the emissions of you computer and sending everything
to an API running on "localhost:8008" (Or you can start a private local API with "docker-compose up"). Using the public API with
this is not supported yet (coming soon!)
Expand Down
3 changes: 1 addition & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<script src="_static/documentation_options.js?v=eb155f5e"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script src="_static/js/theme.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
Expand Down Expand Up @@ -133,7 +132,7 @@ <h1>CodeCarbon<a class="headerlink" href="#codecarbon" title="Link to this headi
<ul>
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installing CodeCarbon</a><ul>
<li class="toctree-l2"><a class="reference internal" href="installation.html#from-pypi-repository">From PyPi repository</a></li>
<li class="toctree-l2"><a class="reference internal" href="installation.html#from-conda-repository">From conda repository</a></li>
<li class="toctree-l2"><a class="reference internal" href="installation.html#using-conda-environments">Using Conda environments</a></li>
<li class="toctree-l2"><a class="reference internal" href="installation.html#dependencies">Dependencies</a></li>
</ul>
</li>
Expand Down
24 changes: 11 additions & 13 deletions docs/installation.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">Installing CodeCarbon</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#from-pypi-repository">From PyPi repository</a></li>
<li class="toctree-l2"><a class="reference internal" href="#from-conda-repository">From conda repository</a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-conda-environments">Using Conda environments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#dependencies">Dependencies</a></li>
</ul>
</li>
Expand Down Expand Up @@ -99,13 +99,6 @@

<section id="installing-codecarbon">
<span id="installation"></span><h1>Installing CodeCarbon<a class="headerlink" href="#installing-codecarbon" title="Link to this heading"></a></h1>
<p>Create a virtual environment using <cite>conda</cite> for easier management of dependencies and packages.
For installing conda, follow the instructions on the
<a class="reference external" href="https://docs.conda.io/projects/conda/en/latest/user-guide/install">official conda website</a></p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>conda<span class="w"> </span>create<span class="w"> </span>--name<span class="w"> </span>codecarbon
conda<span class="w"> </span>activate<span class="w"> </span>codecarbon
</pre></div>
</div>
<section id="from-pypi-repository">
<h2>From PyPi repository<a class="headerlink" href="#from-pypi-repository" title="Link to this heading"></a></h2>
<p>The package is hosted on the pip repository <a class="reference external" href="https://pypi.org/project/codecarbon/">here</a>.</p>
Expand All @@ -114,15 +107,20 @@ <h2>From PyPi repository<a class="headerlink" href="#from-pypi-repository" title
</pre></div>
</div>
</section>
<section id="from-conda-repository">
<h2>From conda repository<a class="headerlink" href="#from-conda-repository" title="Link to this heading"></a></h2>
<p>The package is hosted on the conda repository <a class="reference external" href="https://anaconda.org/codecarbon/codecarbon">here</a>.</p>
<p>To install the package, run the following command in your terminal.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>conda<span class="w"> </span>install<span class="w"> </span>-c<span class="w"> </span>codecarbon<span class="w"> </span>-c<span class="w"> </span>conda-forge<span class="w"> </span>codecarbon
<section id="using-conda-environments">
<h2>Using Conda environments<a class="headerlink" href="#using-conda-environments" title="Link to this heading"></a></h2>
<p>If you’re using Conda for environment management, you can install CodeCarbon with pip in your Conda environment:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>conda<span class="w"> </span>create<span class="w"> </span>--name<span class="w"> </span>codecarbon
conda<span class="w"> </span>activate<span class="w"> </span>codecarbon
pip<span class="w"> </span>install<span class="w"> </span>codecarbon
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>While CodeCarbon can be used in Conda environments, we no longer maintain Conda packages. We recommend using <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">codecarbon</span></code> within your Conda environment, which works seamlessly with Conda.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>We recommend using Python 3.8 or above.</p>
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ <h3>Command line<a class="headerlink" href="#command-line" title="Link to this h
</pre></div>
</div>
<p>You have to stop the monitoring manually with <code class="docutils literal notranslate"><span class="pre">Ctrl+C</span></code>.</p>
<p>If you want to detect the hardware of your computer without starting any measurement, you can use:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">codecarbon detect</span>
</pre></div>
</div>
<p>It will print the detected RAM, CPU and GPU information.</p>
<p>In the following example you will see how to use the CLI to monitor all the emissions of you computer and sending everything
to an API running on “localhost:8008” (Or you can start a private local API with “docker-compose up”). Using the public API with
this is not supported yet (coming soon!)</p>
Expand Down
21 changes: 21 additions & 0 deletions examples/print_hardware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from codecarbon.emissions_tracker import EmissionsTracker

if __name__ == "__main__":
tracker = EmissionsTracker(measure_power_secs=0, save_to_file=False)

print("Detected Hardware:")
hardware_info = tracker.get_detected_hardware()

print(f"- Available RAM: {hardware_info['ram_total_size']:.3f} GB")
print(
f"- CPU count: {hardware_info['cpu_count']} thread(s) in {hardware_info['cpu_physical_count']} physical CPU(s)"
)
print(f"- CPU model: {hardware_info['cpu_model']}")
print(f"- GPU count: {hardware_info['gpu_count']}")

gpu_model_str = hardware_info["gpu_model"]
if hardware_info.get("gpu_ids"):
gpu_model_str += (
f" BUT only tracking these GPU ids : {hardware_info['gpu_ids']}"
)
print(f"- GPU model: {gpu_model_str}")
19 changes: 19 additions & 0 deletions tests/test_emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,22 @@ def test_scheduler_warning_shown_when_running(
self.assertTrue(
scheduler_warning_found, "Expected scheduler warning was not found"
)

def test_get_detected_hardware(
self,
mock_cli_setup,
mock_log_values,
mocked_get_gpu_details,
mocked_env_cloud_details,
mocked_is_gpu_details_available,
):
tracker = EmissionsTracker(save_to_file=False)
hardware_info = tracker.get_detected_hardware()
self.assertIsInstance(hardware_info, dict)
self.assertIn("ram_total_size", hardware_info)
self.assertIn("cpu_count", hardware_info)
self.assertIn("cpu_physical_count", hardware_info)
self.assertIn("cpu_model", hardware_info)
self.assertIn("gpu_count", hardware_info)
self.assertIn("gpu_model", hardware_info)
self.assertIn("gpu_ids", hardware_info)
Loading