Skip to content

feat: add Apple M2/M3/M4 chip TDP support#1256

Open
williamacostalora wants to merge 4 commits into
mlco2:masterfrom
williamacostalora:fix-issue-758
Open

feat: add Apple M2/M3/M4 chip TDP support#1256
williamacostalora wants to merge 4 commits into
mlco2:masterfrom
williamacostalora:fix-issue-758

Conversation

@williamacostalora

@williamacostalora williamacostalora commented Jun 22, 2026

Copy link
Copy Markdown

Description

Adds TDP (Thermal Design Power) entries for Apple M2, M3, and M4 chip variants to cpu_power.csv, and adds unit tests covering correct TDP lookup for all new entries plus a fallback test for unrecognized future Apple chips.

Related Issue

Fixes #758

Motivation and Context

CodeCarbon's CPU power detection only had an entry for Apple M1 in its TDP lookup table. Users on M2, M3, or M4 Macs received a "We saw that you have a Apple M3 Pro but we don't know it" warning and fell back to a generic estimate of 4W per CPU thread, producing inaccurate emissions data for a large and growing portion of ML practitioners on Apple Silicon.

How Has This Been Tested?

  • Reproduced the bug by mocking detect_cpu_model() to return "Apple M3 Pro", confirming the existing fallback warning and incorrect 32W estimate (vs. ~18W actual spec)
  • Added 12 new rows to cpu_power.csv covering M2, M2 Pro, M2 Max, M2 Ultra, M3, M3 Pro, M3 Max, M3 Ultra, M4, M4 Pro, M4 Max, M4 Ultra
  • Added 4 new unit tests to tests/test_cpu.py:
    • Correct TDP lookup for M2 series
    • Correct TDP lookup for M3 series
    • Correct TDP lookup for M4 series
    • Graceful fallback for an unrecognized future chip (e.g. "Apple M5 Pro") confirms no crash, falls back to existing default behavior
  • Ran full tests/test_cpu.py suite: 43 passed, 2 skipped (skips are pre-existing, platform-specific, unrelated to this change)
  • Rebased on latest upstream/master to confirm no conflicts with recent changes

Note on TDP value sourcing

TDP values are sourced from NotebookCheck and CPU Monkey, matching the
table @makoeppel shared in this issue thread (since Apple does not
publish official TDP specs):

  • M2 (22W), M2 Pro (35W), M2 Max (50W), M2 Ultra (105W) — midpoints of
    the ranges in the issue's sourced table
  • M3 (22W), M3 Pro (35W), M3 Max (50W) — direct values from the sourced
    table
  • M3 Ultra, M4 Pro, M4 Max, M4 Ultra had no source found in the issue
    thread, so these are extrapolated proportionally from the equivalent
    M3-generation values (M3 Ultra: 100W, M4 Pro: 35W, M4 Max: 55W,
    M4 Ultra: 110W). Happy to adjust if better sources are available, or
    to leave these out of scope for a follow-up if preferred.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

AI Usage Disclosure

  • ⭐ AI-assisted. Car analogy: you drive the car, AI helps you find your way.

I used Claude as a pair-programming assistant throughout: it helped me locate the relevant files, explained the existing fuzzy-matching lookup logic in TDP._get_matching_cpu(), and walked me through correct unittest.mock.patch usage (including a gotcha around patching codecarbon.core.cpu.detect_cpu_model rather than codecarbon.core.util.detect_cpu_model). I wrote, ran, and reviewed every command and test myself, sourced the actual Apple Silicon TDP values independently, and made all decisions about test structure and scope.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Fixes mlco2#758 - Apple Silicon chips from M2 onwards were not recognized
by CodeCarbon's TDP lookup table, causing users to fall back to a
constant default power estimate instead of a real measurement.

Added TDP entries for:
- Apple M2, M2 Pro, M2 Max, M2 Ultra
- Apple M3, M3 Pro, M3 Max, M3 Ultra
- Apple M4, M4 Pro, M4 Max, M4 Ultra

Added unit tests to verify correct TDP lookup for all new chip variants.
Adds a regression test confirming that chip variants not yet in
cpu_power.csv (e.g. a future Apple M5) still resolve to the existing
default per-thread power estimate instead of raising an error.
@williamacostalora williamacostalora marked this pull request as ready for review June 29, 2026 02:53
@williamacostalora williamacostalora requested a review from a team as a code owner June 29, 2026 02:53
Updates M2/M3/M4 TDP estimates to align with the sources cited in
issue mlco2#758 (NotebookCheck, CPU Monkey). Previous values were rough
estimates that didn't match the maintainer-reviewed table in the
issue thread.

Values without a direct source (M3 Ultra, M4 Pro/Max/Ultra) are
extrapolated proportionally from the equivalent M3 generation chips
and noted as such in the PR description, since Apple does not
publish official TDP specs.
@williamacostalora

Copy link
Copy Markdown
Author

Hi! I've updated the TDP values in this PR to match the sourced table @makoeppel shared in the issue thread, rather than the rough estimates I originally used. Also added a note in the description explaining where each value came from and flagging which ones (M3 Ultra, M4 Pro/Max/Ultra) had no direct source and were extrapolated. Let me know if you'd prefer a different approach for those, or if it makes sense to scope them out for now. Thanks for your time!

@benoit-cty benoit-cty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot !
Sorry for the delay.

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.39%. Comparing base (aea5d20) to head (7d97afe).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1256   +/-   ##
=======================================
  Coverage   89.39%   89.39%           
=======================================
  Files          47       47           
  Lines        4565     4565           
=======================================
  Hits         4081     4081           
  Misses        484      484           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@williamacostalora

Copy link
Copy Markdown
Author

@benoit-cty! Really appreciate it. Let me know if there's anything else needed before merging.

@williamacostalora

Copy link
Copy Markdown
Author

Hi @benoit-cty ! The branch update triggered a new workflow approval request. Could you approve the CI workflows when you get a chance so the pre-commit check can run? Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better support for Apple Silicon chips

2 participants