def switch(condition: Task, cases: Dict[Any, Task]) -> None: """ Adds a SWITCH to a workflow. The condition task is evaluated and the result is compared to the keys of the cases dictionary. The task corresponding to the matching key is run; all other tasks are skipped. Any tasks downstream of the skipped tasks are also skipped unless they set `skip_on_upstream_skip=False`. Args: - condition (Task): a task whose result forms the condition for the switch - cases (Dict[Any, Task]): a dict representing the "case" statements of the switch. The value of the `condition` task will be compared to the keys of this dict, and the matching task will be executed. Raises: - PrefectWarning: if any of the tasks in "cases" have upstream dependencies, then this task will warn that those upstream tasks may run whether or not the switch condition matches their branch. The most common cause of this is passing a list of tasks as one of the cases, which adds the `List` task to the switch condition but leaves the tasks themselves upstream. """ with prefect.tags("switch"): for value, task in cases.items(): task = prefect.utilities.tasks.as_task(task) match_condition = CompareValue(value=value).bind(value=condition) task.set_dependencies(upstream_tasks=[match_condition])
def switch(condition: Task, cases: Dict[Any, Task], mapped: bool = False) -> None: """ Adds a SWITCH to a workflow. The condition task is evaluated and the result is compared to the keys of the cases dictionary. The task corresponding to the matching key is run; all other tasks are skipped. Any tasks downstream of the skipped tasks are also skipped unless they set `skip_on_upstream_skip=False`. Example: ```python @task def condition(): return "b" # returning 'b' will take the b_branch @task def a_branch(): return "A Branch" @task def b_branch(): return "B Branch" with Flow("switch-flow") as flow: switch(condition, dict(a=a_branch, b=b_branch)) ``` Args: - condition (Task): a task whose result forms the condition for the switch - cases (Dict[Any, Task]): a dict representing the "case" statements of the switch. The value of the `condition` task will be compared to the keys of this dict, and the matching task will be executed. - mapped (bool, optional): If true, the `switch` operation will be mapped over the arguments instead of applied directly. Defaults to `False`. Raises: - PrefectWarning: if any of the tasks in "cases" have upstream dependencies, then this task will warn that those upstream tasks may run whether or not the switch condition matches their branch. The most common cause of this is passing a list of tasks as one of the cases, which adds the `List` task to the switch condition but leaves the tasks themselves upstream. """ with prefect.tags("switch"): for value, task in cases.items(): task = prefect.utilities.tasks.as_task(task) match_condition = CompareValue(value=value).bind(value=condition, mapped=mapped) task.set_dependencies(upstream_tasks=[match_condition], mapped=mapped)
def switch(condition: Task, cases: Dict[Any, Task]) -> None: """ Adds a SWITCH to a workflow. The condition task is evaluated and the result is compared to the keys of the cases dictionary. The task corresponding to the matching key is run; all other tasks are skipped. Any tasks downstream of the skipped tasks are also skipped unless they set `skip_on_upstream_skip=False`. Args: - condition (Task): a task whose result forms the condition for the switch - cases (Dict[Any, Task]): a dict representing the "case" statements of the switch. The value of the `condition` task will be compared to the keys of this dict, and the matching task will be executed. Raises: - PrefectWarning: if any of the tasks in "cases" have upstream dependencies, then this task will warn that those upstream tasks may run whether or not the switch condition matches their branch. The most common cause of this is passing a list of tasks as one of the cases, which adds the `List` task to the switch condition but leaves the tasks themselves upstream. """ with prefect.tags("switch"): for value, task in cases.items(): task = prefect.utilities.tasks.as_task(task) active_flow = prefect.context.get("flow", None) if ( active_flow and task in active_flow.tasks and active_flow.upstream_tasks(task) ): # TODO link this warning to a more complete example in the docs. warnings.warn( "One of the tasks passed to the switch condition has upstream " "dependencies: {}. Those upstream tasks could run even if the " "switch condition fails, which might cause unexpected " "results.".format(task), prefect.utilities.exceptions.PrefectWarning, ) match_condition = CompareValue(value=value).bind(value=condition) task.set_dependencies(upstream_tasks=[match_condition])
target="{task_name}-{task_tags}.pkl", checkpoint=True, result=LocalResult(dir=f"./cache/datasets/rico/"), ) prepare_rico_task = PrepareRicoScaPair() prepare_rico_layout_lm_task = PrepareLayoutLMPairTask() layout_lm_trainer_task = LayoutLMPair() INSTRUCTION_TYPE = [2] # where: 0 and 3 - Lexical Matching # 1 - Spatial (Relative to screen) # 2 - Spatial (Relative to other elements) with Flow("Running the Transformers for Pair Classification") as flow1: with tags("train"): train_input = prepare_rico_task(train_path, type_instructions=INSTRUCTION_TYPE) train_dataset = prepare_rico_layout_lm_task(train_input["data"]) with tags("dev"): dev_input = prepare_rico_task(dev_path, type_instructions=INSTRUCTION_TYPE) dev_dataset = prepare_rico_layout_lm_task(dev_input["data"]) with tags("test"): test_input = prepare_rico_task(test_path, type_instructions=INSTRUCTION_TYPE) test_dataset = prepare_rico_layout_lm_task(test_input["data"]) layout_lm_trainer_task( train_dataset=train_dataset, dev_dataset=dev_dataset, test_dataset=test_dataset,