forked from linkong/planet
123 lines
4.8 KiB
Python
123 lines
4.8 KiB
Python
"""Versioned, source-backed annual observations for compute comparisons."""
|
|
|
|
from copy import deepcopy
|
|
from datetime import date
|
|
|
|
from app.schemas.compute_configuration import ComputeConfiguration
|
|
from app.services.compute_catalog import (
|
|
CATALOG,
|
|
DEFAULT_PARTICIPANTS,
|
|
PARTICIPANTS,
|
|
participant_members,
|
|
)
|
|
from app.services.compute_configuration import DEFAULT_CONFIGURATION
|
|
|
|
|
|
def _participant_value(
|
|
members: tuple[str, ...], observations: dict[str, float | int], mode: str
|
|
) -> tuple[float | int | None, dict]:
|
|
covered = [member for member in members if member in observations]
|
|
missing = [member for member in members if member not in observations]
|
|
coverage = {
|
|
"status": "complete" if not missing else "incomplete",
|
|
"required_members": list(members),
|
|
"covered_members": covered,
|
|
"missing_members": missing,
|
|
}
|
|
if missing:
|
|
return None, coverage
|
|
if mode == "bilateral_share":
|
|
if (
|
|
not set(members).issubset({"CN", "US"})
|
|
or not {"CN", "US"}.issubset(observations)
|
|
or observations["US"] + observations["CN"] <= 0
|
|
):
|
|
coverage.update(
|
|
{
|
|
"status": "incompatible",
|
|
"reason": "bilateral_share_requires_CN_and_US_observations",
|
|
}
|
|
)
|
|
return None, coverage
|
|
return (
|
|
round(
|
|
sum(observations[member] for member in members)
|
|
/ (observations["US"] + observations["CN"])
|
|
* 100,
|
|
2,
|
|
),
|
|
coverage,
|
|
)
|
|
return sum(observations[member] for member in members), coverage
|
|
|
|
|
|
def compute_history(
|
|
countries: list[str],
|
|
mode: str = "global_share",
|
|
config: ComputeConfiguration | None = None,
|
|
) -> dict:
|
|
"""Aggregate only fully covered, same-year compatible observations."""
|
|
config = config if config is not None else DEFAULT_CONFIGURATION
|
|
metric = "global_share" if mode == "bilateral_share" else mode
|
|
annual = {row.year: row for row in config.observations if row.metric == metric}
|
|
gaps = {row.year: row for row in config.innovation}
|
|
observed_years = [row.year for row in config.observations] + list(gaps)
|
|
years = tuple(
|
|
range(
|
|
min(observed_years, default=date.today().year),
|
|
max([date.today().year, *observed_years]) + 1,
|
|
)
|
|
)
|
|
members_by_participant = participant_members(countries)
|
|
rows = []
|
|
for year in years:
|
|
record = annual.get(year)
|
|
observations = record.values if record else {}
|
|
values, coverage = {}, {}
|
|
for code, members in members_by_participant.items():
|
|
values[code], coverage[code] = _participant_value(members, observations, mode)
|
|
evidence_id = record.source_id if record else None
|
|
rows.append(
|
|
{"year": year, "values": values, "coverage": coverage, "evidence_id": evidence_id}
|
|
)
|
|
|
|
selected_members = {member for members in members_by_participant.values() for member in members}
|
|
has_innovation_scope = {"CN", "US"}.issubset(selected_members)
|
|
return {
|
|
"version": f"compute-history-{config.reviewed_at}",
|
|
"provenance": "curated_reports",
|
|
"reviewed_at": config.reviewed_at.isoformat(),
|
|
"time_basis": "retrospective",
|
|
"years": years,
|
|
"mode": mode,
|
|
"unit": "EFlop/s (FP32)" if mode == "absolute" else "%",
|
|
"denominator": {"global_share": "global", "bilateral_share": "US+CN", "absolute": None}[
|
|
mode
|
|
],
|
|
"catalog": deepcopy(CATALOG),
|
|
"default_participants": list(DEFAULT_PARTICIPANTS),
|
|
"countries": [deepcopy(PARTICIPANTS[code]) for code in countries],
|
|
"compute": rows,
|
|
"innovation": [
|
|
{
|
|
"year": year,
|
|
"value": gaps[year].value if year in gaps and has_innovation_scope else None,
|
|
"evidence_id": gaps[year].source_id
|
|
if year in gaps and has_innovation_scope
|
|
else None,
|
|
}
|
|
for year in years
|
|
],
|
|
"evidence": {
|
|
source.id: source.model_dump(mode="json", exclude={"id"}) for source in config.sources
|
|
},
|
|
"innovation_scope": ["US", "CN"],
|
|
"limitations": [
|
|
"公开设施样本不代表全国总算力;FP64、FP16 与 FP32 不可直接相加。",
|
|
"区域值只在全部成员有同年、同指标、可比较观测时汇总;缺失成员时返回 null。",
|
|
"全球份额不从中美或其他局部成员反推剩余地区。",
|
|
"年度资料按已配置报告引用展示,观测年与报告年份分列;跨年方法可比性仍需核验。",
|
|
"创新分差为美国减中国;只有分差,不反推单国得分,不证明算力与创新的因果关系。",
|
|
],
|
|
}
|