Skip to content

Commit

Permalink
chore(tracer): remove dead code in tracer (#40)
Browse files Browse the repository at this point in the history
* chore(tracer): remove dead code in tracer

* fix(tests): fix import
  • Loading branch information
tito authored Dec 31, 2024
1 parent 1497a9e commit f56f06c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 49 deletions.
48 changes: 1 addition & 47 deletions scope3ai/api/tracer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List, Optional
from .typesgen import ImpactResponse, ModeledRow, ImpactMetrics
from .typesgen import ImpactResponse, ModeledRow


class Tracer:
Expand Down Expand Up @@ -58,52 +58,6 @@ def get_all_rows(self) -> List[ModeledRow]:
all_rows.extend(child.get_all_rows())
return all_rows

def _sum_modeled_rows(self, rows: List[ModeledRow]) -> ModeledRow:
if not rows:
raise Exception("No rows to sum")
result = ModeledRow(
inference_impact=self._sum_impact_metrics(
[row.inference_impact for row in rows]
),
training_impact=self._sum_impact_metrics(
[row.training_impact for row in rows]
),
fine_tuning_impact=self._sum_impact_metrics(
[row.fine_tuning_impact for row in rows]
),
total_impact=self._sum_impact_metrics([row.total_impact for row in rows]),
)
return result

def _sum_impact_metrics(self, metrics: List[ImpactMetrics]) -> ImpactMetrics:
# Initialize totals
total_usage_energy_wh = 0.0
total_usage_emissions_gco2e = 0.0
total_usage_water_ml = 0.0
total_embodied_emissions_gco2e = 0.0
total_embodied_water_ml = 0.0

# Aggregate values
for metric in metrics:
if not isinstance(metric, ImpactMetrics):
raise TypeError(
"All items in the list must be instances of ImpactMetrics."
)
total_usage_energy_wh += metric.usage_energy_wh
total_usage_emissions_gco2e += metric.usage_emissions_gco2e
total_usage_water_ml += metric.usage_water_ml
total_embodied_emissions_gco2e += metric.embodied_emissions_gco2e
total_embodied_water_ml += metric.embodied_water_ml

# Return a new instance with summed values
return ImpactMetrics(
usage_energy_wh=total_usage_energy_wh,
usage_emissions_gco2e=total_usage_emissions_gco2e,
usage_water_ml=total_usage_water_ml,
embodied_emissions_gco2e=total_embodied_emissions_gco2e,
embodied_water_ml=total_embodied_water_ml,
)

def _link_parent(self, parent: Optional["Tracer"]) -> None:
if parent and (self not in parent.children):
parent.children.append(self)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


def test_tracer_linear():
from scope3ai.api.tracer import Tracer, ModeledRow, ImpactMetrics
from scope3ai.api.tracer import Tracer
from scope3ai.api.types import ModeledRow, ImpactMetrics

tracer = Tracer()
assert tracer is not None
Expand Down Expand Up @@ -36,7 +37,8 @@ def test_tracer_linear():


def test_tracer_nested():
from scope3ai.api.tracer import Tracer, ModeledRow, ImpactMetrics
from scope3ai.api.tracer import Tracer
from scope3ai.api.types import ModeledRow, ImpactMetrics

tracer = Tracer()
tracer2 = Tracer()
Expand Down

0 comments on commit f56f06c

Please sign in to comment.