FastAPI middleware: attribute per-request energy by CPU time and exclude idle power
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 30/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
調査の方向性
Start by reading codecarbon/integrations/fastapi/attribution.py, especially _settle, and the middleware introduced by #1203. Trace how async handlers and FastAPI dependant calls are currently measured before assessing the CPU-time and idle-energy design. Done means the stated conservation invariant and CI checks are covered, with the bare-metal validation documented as complete or still unavailable.
索引モデルが issue の本文から書いたものです。
説明
Is your feature request related to a problem? Please describe.
The FastAPI middleware from #1203 splits each sampling window's machine energy across the requests in flight, weighted by wall-clock overlap (codecarbon/integrations/fastapi/attribution.py, _settle). That number is an estimated share, and it is biased in four ways:
- Idle power is charged to whichever request happens to be in flight. A single 1 ms request in an otherwise idle 15 s window receives the whole window's energy.
- A request waiting on I/O is weighted the same as one using the CPU.
- In machine mode, energy used by other processes on the host is charged to requests.
- GPU energy is not tied to the request that launched the work.
A benchmark on #1203 (uvicorn, oha, all-MiniLM-L6-v2 on CPU) found the middleware's latency cost within noise, so speed is not the concern here. Accuracy is.
Describe the solution you'd like
Charge each request only the energy above idle that its own process used, split by the CPU time the request consumed:
- Per window and per component:
dynamic = max(ΔE − P_idle × width, 0). The remainder goes to anidle_kwhbucket. - Idle power: the lower of a rolling minimum of window power and a regression intercept (used only when the fit is good). In CPU load mode it is known analytically (0.1 × TDP).
- Process share of CPU dynamic energy: Δ
process_time()/ Δ busy CPU time frompsutil.cpu_times(). The rest goes toother_processes_kwh. - Per-request CPU time: async handlers are metered by wrapping the handler coroutine and summing
time.thread_time_ns()around each resumption. Sync handlers are metered by wrapping FastAPI's dependant calls that run in the threadpool. CPU time the meter cannot assign goes toprocess_unattributed_kwh. - GPU dynamic energy stays split by wall-clock overlap and is reported separately, labelled as such.
- An exact conservation invariant:
attributed + idle + other_processes + process_unattributed + unattributed == settled. - New
RequestEnergyfields:cpu_seconds,attribution_method(cpu_time/wall/mixed),quality(measuredfor RAPL/powermetrics/NVML,modeledfor load mode,none).energy_kwhbecomes the dynamic energy. It has not been released yet, so this breaks no users.
Validation, against known ground truth:
| Check | Threshold | Where |
|---|---|---|
| Conservation with fake clocks and energy | relative error < 1e-12 | CI |
cpu_seconds of a known CPU burn |
±10% | CI |
| Energy ratios of 1:2:4 ms CPU endpoints, concurrency 16 | ±10% | Linux with RAPL |
asyncio.sleep endpoint energy |
≤ 2% of the 4 ms burn | Linux with RAPL |
| Per-request energy with a CPU hog in another process | changes ≤ 15% | Linux with RAPL |
Per-request overhead budget: ≤ 30 µs p50 on Linux, ≤ 50 µs on macOS.
Describe alternatives you've considered
- Idle subtraction only, keeping wall-clock weighting. Simpler, but a request waiting on a database is still charged like one using the CPU.
- Per-route aggregates only, no per-request numbers. Avoids misreading single values, but does not improve the estimate.
- Metering child tasks (
create_task,gather) with a custom task factory. Left out for now: it can clash with a user's own factory, and theprocess_unattributed_kwhbucket shows how much CPU time goes unmetered without it.
Additional context
Known limits: CPU time is a proxy for energy, and frequency scaling, SMT and AVX add error (an estimated ±15%, not yet measured). Per-request GPU energy is not possible without instrumenting user code. Wrapping dependant calls relies on FastAPI internals.
The bare-metal checks (the last three rows of the table) have not been run yet: they need a Linux machine with readable RAPL, which GitHub runners likely do not expose.
Related: #1203 (the middleware), #1178 (earlier per-request inference proposal), #936 (several processes sharing one machine).
- 主要言語
- Python
- スター
- 1.9k
- フォーク
- 351
- 平均マージ
- 3日 12時間
- マージ済み PR(30日)
- 22
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
mlco2/codecarbon のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
mlco2/codecarbon#1460 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
CLI: get_config() returns None without [codecarbon] section; get_api_endpoint() appends api_endpoint under wrong section対応中かも @sohammishra864-wq が 38 日前に担当しました。 オープン
難易度 2/5 半日 初心者へのやさしさ 82/100
mlco2/codecarbon#1432 ·
メンテナーはふだん 1 日以内に返信
-
Update the screen shot of the dashboard対応中かも @KLGani が 13 日前に担当しました。 オープンdocumentation good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
mlco2/codecarbon#1423 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
-
OfflineEmissionsTracker silently returns a half-built object when construction fails対応中かも @davidberenstein1957 が 86 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
mlco2/codecarbon#1311 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
mlco2/codecarbon#1429 ·
メンテナーはふだん 1 日以内に返信
mlco2/codecarbon の issue をすべて見る
似ている issue
-
Remove the `pull_request_target` workflow before GitHub blocks it対応中かも @jat255 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
IBM/mcp-context-forge#7483 ·
メンテナーはふだん 2 日以内に返信
-
bug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
[Bug Report]: OpenAI 辅助 API 连通性测试因输出上限仅 1 token 误报“未知错误”対応中かも @fjfanjian が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Project-N-E-K-O/N.E.K.O#3640 ·
メンテナーはふだん 1 日以内に返信
-
author-page correction
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
acl-org/acl-anthology#10808 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信