Roy Kid
06/05/2024, 7:22 PM@resolve
expand a (template) function to n
(not fixed, determined when CONFIG_AVAILABLE
) functions with different inputs, and collect them in the next function:
from hamilton.function_modifiers import resolve, ResolveAt, parameterize_sources
from hamilton.function_modifiers import source, value
@resolve(
when=ResolveAt.CONFIG_AVAILABLE,
decorate_with= lambda names: parameterize_sources(
**{
f'parallel_{name}': dict(name=name) for name in names
}
)
)
def serial(name: str) -> str:
# expand serial func to n functions
# n is not fixed
return name
@resolve(
when=ResolveAt.CONFIG_AVAILABLE,
decorate_with= lambda names: parameterize_sources(
**{
f'collect': {f"parallel_{name}": value(name) for name in names}
}
)
)
def collect(all_name: tuple[str]) -> str:
# expected input node: parallel_a, parallel_b, parallel_c
# each value of input is calculated by serial function
# serail is like a template
return ' '.join(all_name)
# above equal to:
def parallel_a:...
def parallel_b:...
def collect(parallel_a, parallel_b, ...):...
# but the number of inputs of collect is not fixed, determined when config_available
I think it could be achieved with Parallelable/Collect, but this "static & compile" style looks fancy, and it maybe more friendly to graph/UI?
in case you want to know why: A molecule construct with different segments, and each segments need to be processed(and calculated) in advanced. So a general workflow dont know how many segments(inputs) would be taken before "CONFIG_AVAILABLE". I hope I can define a template, and specialize it when it start.
Elijah Ben Izzy
06/05/2024, 7:26 PMinject
— https://hamilton.dagworks.io/en/latest/reference/decorators/inject/Elijah Ben Izzy
06/05/2024, 7:27 PMElijah Ben Izzy
06/05/2024, 7:28 PMRoy Kid
06/05/2024, 7:33 PMresolve
: However, since the code goes against one of Hamilton's primary tenets ( that all code is highly readable), we require that you enable power_user_mode.
Only because this style is not readable? I also think this method is hard to parallelize by executor?Elijah Ben Izzy
06/05/2024, 7:35 PMElijah Ben Izzy
06/05/2024, 7:35 PMgroup
— should be exactly what you want, and in the example for injectRoy Kid
06/05/2024, 7:40 PMElijah Ben Izzy
06/05/2024, 7:57 PMRoy Kid
06/05/2024, 8:39 PMElijah Ben Izzy
06/06/2024, 12:27 AM