Adaptive Pipeline Walkthrough¶
This documentation walks through the adaptive capabilities of the IMPRESS pipeline framework, focusing specifically on how pipelines can dynamically spawn child pipelines during execution. For a conceptual overview of the underlying protocol, see Architecture; for real-world adaptive workflows, see Examples.
To watch this mechanism run instead of reading about it — including what changing the spawn threshold does to the pipeline tree — see Run It In Colab.
Adaptive Pipeline Components¶
1. Pipeline Configuration for Adaptivity¶
The DummyProteinPipeline class is configured to support adaptive behavior through several key attributes:
def __init__(self, name: str, flow: Any, generation: int = 1,
parent_name: str = 'root', max_generations: int = 3, **kwargs):
self.iter_seqs: str = 'MKFLVLACGT'
self.generation: int = generation # Track generation level
self.parent_name: str = parent_name # Track parent pipeline
self.max_generations: int = max_generations # Limit recursion depth
super().__init__(name, flow, **kwargs)
ImpressManager builds each pipeline as
type(name=..., flow=..., **config, **kwargs): the keys of a setup's
config dict arrive as keyword arguments, so the constructor declares them
as keywords. A configs: dict parameter would always be empty.
Key adaptive attributes:
- generation: Tracks the current generation level of the pipeline
- parent_name: Maintains lineage information
- max_generations: Prevents infinite recursion by setting a maximum depth
2. Adaptive Execution Point¶
The adaptive behavior is triggered at a specific point in the pipeline execution:
async def run(self) -> None:
# ... other pipeline steps ...
await self.run_adaptive_step(wait=True) # Critical adaptive execution point
# ... remaining pipeline steps ...
run_adaptive_step(wait=True):
- This method flags the pipeline for an adaptive step; it does not call the
adaptive function itself. The manager sees the flag on its next poll and
awaits the pipeline's declared adaptive_fn task.
- wait=True blocks the pipeline until that task has finished; wait=False
returns at once.
- Child requests made by the adaptive function are picked up and submitted
by the manager on a later poll, not at this call.
3. Adaptive Strategy Function¶
The core adaptive logic is implemented in the adaptive_optimization_strategy function:
async def adaptive_optimization_strategy(pipeline: DummyProteinPipeline) -> None:
# Decision logic: stop if max generations reached OR random condition (50% chance)
if pipeline.generation >= pipeline.max_generations or random.random() >= 0.5:
return # No child pipeline created
# Generate unique name for child pipeline
new_name = f"{pipeline.name}_g{pipeline.generation + 1}"
# Configure child pipeline
new_config = {
'name': new_name,
'type': type(pipeline), # Same pipeline class
'config': {
'generation': pipeline.generation + 1, # Increment generation
'parent_name': pipeline.name, # Set parent reference
'max_generations': pipeline.max_generations,
},
'adaptive_fn': adaptive_task # the DECLARED task, not the bare function
}
# Submit child pipeline to manager
pipeline.submit_child_pipeline_request(new_config)
Key adaptive mechanisms:
- Conditional spawning: Uses generation limits and random conditions to decide whether to create children
- Configuration inheritance: Child pipelines inherit configuration with incremented generation
- Recursive adaptivity: Child pipelines use the same adaptive function, enabling multi-generational spawning
- Pipeline submission: Uses
submit_child_pipeline_request()to register new pipelines with the manager
4. Manager Setup with Adaptive Function¶
The pipeline manager is configured to support adaptive behavior:
async def run() -> None:
global adaptive_task
execution_backend = await LocalExecutionBackend(ThreadPoolExecutor())
flow = await WorkflowEngine.create(backend=execution_backend)
manager: ImpressManager = ImpressManager(flow)
# Declare where the callback runs; the manager only awaits it. This
# engine's one backend is already a thread pool, so no name is needed.
adaptive_task = flow.function_task(adaptive_optimization_strategy)
pipeline_setups = [PipelineSetup(name=f'p{i}',
type=DummyProteinPipeline,
adaptive_fn=adaptive_task) # a declared engine task
for i in range(1, 4)]
try:
await manager.start(pipeline_setups=pipeline_setups)
finally:
await flow.shutdown()
Critical setup elements:
- adaptive_fn must be an engine task, declared by the caller with
flow.function_task(...). The manager awaits it and chooses nothing; a bare
function raises TypeError when the PipelineSetup is constructed, before
any pipeline starts. The backend must be a thread pool, since the callback
mutates the live pipeline object.
- A child config must carry that same declared task, which is why
adaptive_task is module-level here: the recursion passes the task on, not
the raw coroutine. Passing adaptive_optimization_strategy instead of
adaptive_task is refused by name — declaring a callback marks the bare
function too, so the two are easy to confuse and worth checking for.
- The manager handles the lifecycle of dynamically created child pipelines
Adaptive Execution Flow¶
- Initial pipelines (
p1,p2,p3) start execution - Each pipeline runs its tasks until reaching
run_adaptive_step() - Adaptive function evaluates conditions (generation limit, random chance)
- If conditions are met, child pipeline configuration is created
- Child pipeline is submitted to the manager via
submit_child_pipeline_request() - Manager spawns the child pipeline and adds it to the execution pool
- Child pipelines repeat the process, potentially creating their own children
Key Adaptive Features¶
- Dynamic pipeline creation: Pipelines can spawn children during runtime
- Configurable conditions: Adaptive logic can be based on any runtime state
- Generation tracking: Built-in support for tracking pipeline lineage
- Recursive adaptivity: Child pipelines can also be adaptive
- Manager integration: Seamless integration with the IMPRESS execution manager