Luke Phillips
09/18/2024, 4:40 PMdr = Driver({}, *modules)
and I expect to repeatedly be calling
dr.execute(final_vars=['output'], inputs={...})
as part of my application (repeatedly engineering an output in an online setting) -- is there a recommended way to do this in Hamilton if I particularly care about speed of execution, other than just calling dr.execute in this fashion?
There is definitely some overhead vs just parsing the graph manually, for example, I'll put a dummy example of some slow-down in the threadLuke Phillips
09/18/2024, 4:42 PMimport numpy as np
def a(x: float) -> np.ndarray:
return np.random.normal(size=1)
def b(a: np.ndarray) -> np.ndarray:
return np.power(a, 2)
def c(b: np.ndarray) -> np.ndarray:
return np.convolve(b, np.ones(32), 'valid') / 32
build.py
import time
from hamilton import base
from hamilton.driver import Driver
from src import features
dict_builder = base.DictResult()
adapter = base.SimplePythonGraphAdapter(dict_builder)
dr = Driver({}, features, adapter=adapter)
start_time = time.time()
dr.execute(['c'], inputs={'x': 0.})
end_time = time.time()
print(f"DAG executed on run 1 in {end_time - start_time:.9f} seconds")
start_time = time.time()
dr.execute(['c'], inputs={'x': 0.})
end_time = time.time()
print(f"DAG executed on run 2 in {end_time - start_time:.9f} seconds")
from src.timings.features import a, b, c
start_time = time.time()
output = {}
output['a'] = a(0.)
output['b'] = b(output['a'])
output['c'] = c(output['b'])
end_time = time.time()
print(f"DAG executed manually in {end_time - start_time:.9f} seconds")
Gives me the output:
DAG executed in 0.000430107 seconds
DAG executed in 0.000244141 seconds
DAG executed manually in 0.000026941 seconds
for example. I'm also curious as to why execution time halves on the second run.
This is obviously pretty contrived, but I am also experiencing slowdown in a more complicated & realistic pipeline -- around 2xStefan Krawczyk
09/18/2024, 5:26 PMLuke Phillips
09/18/2024, 5:28 PMStefan Krawczyk
09/18/2024, 5:40 PMStefan Krawczyk
09/18/2024, 5:41 PMStefan Krawczyk
09/18/2024, 5:43 PMElijah Ben Izzy
09/18/2024, 6:02 PMDAG executed on run 1 in 0.000396013 seconds
DAG executed on run 2 in 0.000221014 seconds
DAG executed manually in 0.000022650 secondsElijah Ben Izzy
09/18/2024, 6:03 PMtime.sleep(1) to each function, it removes all the differences (makes sense, high variance):
DAG executed on run 1 in 3.011426926 seconds
DAG executed on run 2 in 3.014846087 seconds
DAG executed manually in 3.015386105 secondsElijah Ben Izzy
09/18/2024, 6:06 PMnumpy, the difference shortens significantly:
DAG executed on run 1 in 0.000269175 seconds
DAG executed on run 2 in 0.000249147 seconds
DAG executed manually in 0.000003099 seconds
Seeing a 20% speedup the second time still, on a few runs. SOme are closer to 30-50%. Very highly variant.Elijah Ben Izzy
09/18/2024, 6:15 PMLuke Phillips
09/18/2024, 6:31 PM