Note
Go to the end to download the full example code.
Add a variable computed with a formula#
The logs rarely contain every metric needed to compare experiments.
Argument formulas of data_cube.DataCube adds new columns
when the data is loaded. Every formula is a function receiving a
pandas.DataFrame (the data loaded so far) and returning a series.
The new columns are values: they can be shown in views and aggregated
like any other value.
Data#
Every row is one model exported with one exporter on one day. The latency of the exported model and the latency of the original model (the baseline) are measured, as well as the peak memory. One export failed (no latency) and one exported model is slower than the baseline.
import io
import textwrap
import numpy as np
import pandas
import matplotlib.pyplot as plt
from data_cube import DataCube, CubeViewDef
pandas.set_option("display.width", 250)
pandas.set_option("display.max_columns", 30)
df = pandas.read_csv(io.StringIO(textwrap.dedent("""
date,model_name,exporter,time_latency,time_baseline,mem_peak
2025/01/01,phi3,export,0.10,0.15,1200
2025/01/01,phi3,dynamo,0.12,0.15,1100
2025/01/01,phi4,export,0.20,0.26,2300
2025/01/01,phi4,dynamo,,0.26,
2025/01/01,llama,export,0.31,0.35,4100
2025/01/01,llama,dynamo,0.29,0.35,3900
2025/01/02,phi3,export,0.09,0.15,1210
2025/01/02,phi3,dynamo,0.11,0.15,1090
2025/01/02,phi4,export,0.21,0.26,2280
2025/01/02,phi4,dynamo,0.19,0.26,2250
2025/01/02,llama,export,0.38,0.35,4150
2025/01/02,llama,dynamo,0.28,0.35,3870
""")))
Formulas#
Three variables are added:
speedup: the baseline latency divided by the latency,speedup_increase: the relative gain in percent, it reusesspeedup, formulas are applied in the order of the dictionary and every formula sees the columns added by the previous ones,faster: 1 if the exported model is faster than the baseline, 0 if it is not, missing if the export failed.
def faster(data: pandas.DataFrame) -> pandas.Series:
res = (data["time_latency"] < data["time_baseline"]).astype(float)
res[data["time_latency"].isna()] = np.nan
return res
cube = DataCube(
df,
time="date",
keys=["model_name", "exporter"],
values=["time_.*", "mem_.*"],
formulas={
"speedup": lambda d: d["time_baseline"] / d["time_latency"],
"speedup_increase": lambda d: (d["speedup"] - 1) * 100,
"faster": faster,
},
).load()
print(cube.values)
print(cube.data[["date", "model_name", "exporter", "speedup", "speedup_increase", "faster"]])
['mem_peak', 'time_baseline', 'time_latency', 'speedup', 'speedup_increase', 'faster']
date model_name exporter speedup speedup_increase faster
0 2025-01-01 phi3 export 1.500000 50.000000 1.0
1 2025-01-01 phi3 dynamo 1.250000 25.000000 1.0
2 2025-01-01 phi4 export 1.300000 30.000000 1.0
3 2025-01-01 phi4 dynamo NaN NaN NaN
4 2025-01-01 llama export 1.129032 12.903226 1.0
5 2025-01-01 llama dynamo 1.206897 20.689655 1.0
6 2025-01-02 phi3 export 1.666667 66.666667 1.0
7 2025-01-02 phi3 dynamo 1.363636 36.363636 1.0
8 2025-01-02 phi4 export 1.238095 23.809524 1.0
9 2025-01-02 phi4 dynamo 1.368421 36.842105 1.0
10 2025-01-02 llama export 0.921053 -7.894737 0.0
11 2025-01-02 llama dynamo 1.250000 25.000000 1.0
The new variables are described like the others.
print(cube.describe())
dtype missing kind count min max mean sum n_values values
name
date datetime64[ns] 0 time 12 2025-01-01 00:00:00 2025-01-02 00:00:00 NaN NaN 2 2025-01-01 00:00:00 - 2025-01-02 00:00:00
model_name object 0 keys 12 NaN NaN NaN NaN 3 llama,phi3,phi4
exporter object 0 keys 12 NaN NaN NaN NaN 2 dynamo,export
time_latency float64 1 values 11 0.09 0.38 0.207273 2.280000 11 NaN
time_baseline float64 0 values 12 0.15 0.35 0.253333 3.040000 3 NaN
mem_peak float64 1 values 11 1090.0 4150.0 2495.454545 27450.000000 11 NaN
speedup float64 1 values 11 0.9210526315789473 1.6666666666666667 1.290346 14.193801 11 NaN
speedup_increase float64 1 values 11 -7.8947368421052655 66.66666666666667 29.034552 319.380076 11 NaN
faster float64 1 values 11 0.0 1.0 0.909091 10.000000 2 NaN
A view on a formula#
The models are the rows, the exporters and dates are the columns.
view = cube.view(CubeViewDef(key_index=["model_name"], values=["speedup_increase"]))
print(view)
METRICS speedup_increase
exporter dynamo export
date 2025-01-01 2025-01-02 2025-01-01 2025-01-02
model_name
llama 20.689655 25.000000 12.903226 -7.894737
phi3 25.000000 36.363636 50.000000 66.666667
phi4 NaN 36.842105 30.000000 23.809524
Aggregation#
The speedup is averaged over the models and faster is summed:
it counts the models faster than the baseline.
agg = cube.view(
CubeViewDef(
key_index=["exporter"],
values=["speedup", "faster"],
key_agg=["model_name"],
agg_args=lambda name: "sum" if name == "faster" else "mean",
)
)
print(agg)
date 2025-01-01 2025-01-02
exporter METRICS
dynamo faster 2.000000 3.000000
speedup 1.228448 1.327352
export faster 3.000000 2.000000
speedup 1.309677 1.275272
Plot#
ts = cube.view(CubeViewDef(key_index=["model_name", "exporter"], values=["speedup"]))
ax = ts.T.droplevel(0).plot(title="speedup over time", marker="o")
ax.set_ylabel("baseline / latency")
plt.tight_layout()

Total running time of the script: (0 minutes 0.159 seconds)