feat: add SCI report output method - #1361
davidberenstein1957 wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1361 +/- ##
==========================================
+ Coverage 91.43% 91.85% +0.41%
==========================================
Files 49 50 +1
Lines 5057 5253 +196
==========================================
+ Hits 4624 4825 +201
+ Misses 433 428 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
3fd5690 to
43edc9f
Compare
080791e to
a21d8e3
Compare
Emit an ISO/IEC 21031 Software Carbon Intensity report alongside the existing output methods. E and I come from the measured run; R and M are user declarations and are reported as undeclared rather than guessed, so a report never invents numbers the run cannot support. Embodied carbon (M) is apportioned across tasks by each task's share of the run duration. Handing every task the same declared M made a 5-task run report the device's full embodied carbon 5 times. The split is exhaustive: per-task figures sum back to the run-level report, and `M_source` records the share applied. Per-task SCI is reported as `sciShare`, not as a per-unit rate. Failure handling is narrow rather than broad: an unreadable `sci_context_file` degrades instead of failing the run, while unrelated exceptions while writing a report are no longer swallowed. Context dataclasses are built straight from the JSON keys, with the documented camelCase `gCO2e` key normalised so existing context files keep working. Docs cover the formula, the term declarations, the config keys and a sample report, and note that `output_methods = csv,sci` alone can never produce a non-null sci unless the context file hardcodes `functionalUnit.count`. The constructor, `from_file` and context-file walkthrough live in `examples/sci_output.py`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
a21d8e3 to
9474fda
Compare
Verdict: ✅ Approve after 1 doc fix and 1 robustness fix
Must fix:
Should fix:
Nits:
|
… M split - from_file raises ValueError when the context file is not a JSON object, so the tracker degrades instead of losing every output handler - docs sample sci value corrected to 0.00845 - M is split by the tasks' summed duration; docs and docstring now say so - task reports show pue null instead of a misleading 1 - SCIOutput copies the FunctionalUnit it is given - document that a relative sci_context_file resolves against the CWD Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Made the changes in 89d2897: non-object context file now raises ValueError (tracker degrades, test added), sample SCI fixed to 0.00845, M-split docs now say tasks' summed duration, task pue is null, FunctionalUnit is copied, relative path documented. |
Description
Adds an SCI (Software Carbon Intensity, ISO/IEC 21031:2024) output method, following the same pattern as the existing BoAmps exporter.
codecarbon/output_methods/sci.pyholdsFunctionalUnit/EmbodiedDeclarationdataclasses for the user-suppliedRandMterms, a puremap_emissions_to_sci(data, ...)mapping function (Iis derived asemissions * 1000 / energy_consumedrather than recomputed, so the report agrees with the CSV by construction), andSCIOutput(BaseOutput), which writessci_report_<run_id>.jsonandsci_report_tasks_<run_id>.json.OutputMethod.SCIis registered in_init_output_methods, reading an optionalsci_context_filefrom the usual config hierarchy. WhenRis undeclared the report still writes withsci: nulland astatusfield explaining why, instead of dividing by zero. WhenMis undeclared,M_gCO2e: 0withM_source: "not declared". A follow-up commit apportions the declaredMacross per-task reports by each task's share of summed duration, instead of charging every task the device's full embodied carbon.Related Issue
Fixes #1353
Motivation and Context
CodeCarbon had no SCI output method. Users who need an ISO/IEC 21031-aligned report had no supported path, and any naive implementation risks inventing numbers (e.g. assuming
R = 1or guessing embodied carbon) that a compliance report cannot support.How Has This Been Tested?
uv run pytest tests/test_sci_output.py -q— 14 tests covering the formula, provenance assembly, the undeclared-Rand undeclared-Mbranches, zero-energy, the handler and its task path, and context-file loading (valid, missing, malformed). Plustest_task_out_apportions_embodied_by_duration(unequal durations: asserts a 10/30 split of a declared 40 gCO2e and that shares sum back to 40).black --checkandruff checkwere run scoped to the touched files.Screenshots (if appropriate):
N/A
Types of changes
AI Usage Disclosure
Checklist:
User-facing surface
sci.set_functional_unit_count(n)for the common case where the count is only known at the end;SCIOutput.from_file("sci_context.json")for the declarative path;output_methods = csv,sciplussci_context_filein.codecarbon.config. Docs: a new SCI section indocs/reference/output.md, a row indocs/how-to/examples.md, and a runnableexamples/sci_output.py. No new dependencies.Deliberately left out
Any automatic embodied-carbon estimation (no
EmbodiedProfiletracker parameter, no bundled hardware table, no cloud per-instance figures) — CodeCarbon has no defensible manufacturing data, and an uncitedMin a compliance report is worse than a blank one. Also out: any change to whatemissionsmeans, multiple simultaneous functional units, and schema validation on write (there is no stable machine-readable SCI schema yet).