MCPcopy Create free account
hub / github.com/Victorletzelter/timeMCL / task_wrapper

Function task_wrapper

tsExperiments/utils/utils.py:131–183  ·  view source on GitHub ↗

Optional decorator that controls the failure behavior when executing the task function. This wrapper can be used to: - make sure loggers are closed even if the task function raises an exception (prevents multirun failure) - save the exception to a `.log` file - mark the

(task_func: Callable)

Source from the content-addressed store, hash-verified

129
130
131def task_wrapper(task_func: Callable) -> Callable:
132 """Optional decorator that controls the failure behavior when executing the task function.
133
134 This wrapper can be used to:
135 - make sure loggers are closed even if the task function raises an exception (prevents multirun failure)
136 - save the exception to a `.log` file
137 - mark the run as failed with a dedicated file in the `logs/` folder (so we can find and rerun it later)
138 - etc. (adjust depending on your needs)
139
140 Example:
141 ```
142 @utils.task_wrapper
143 def train(cfg: DictConfig) -> Tuple[Dict[str, Any], Dict[str, Any]]:
144 ...
145 return metric_dict, object_dict
146 ```
147
148 :param task_func: The task function to be wrapped.
149
150 :return: The wrapped task function.
151 """
152
153 def wrap(cfg: DictConfig) -> Tuple[Dict[str, Any], Dict[str, Any]]:
154 # execute the task
155 try:
156 metric_dict, object_dict = task_func(cfg=cfg)
157
158 # things to do if exception occurs
159 except Exception as ex:
160 # save exception to `.log` file
161 log.exception("")
162
163 # some hyperparameter combinations might be invalid or cause out-of-memory errors
164 # so when using hparam search plugins like Optuna, you might want to disable
165 # raising the below exception to avoid multirun failure
166 raise ex
167
168 # things to always do after either success or exception
169 finally:
170 # display output dir path in terminal
171 log.info(f"Output dir: {cfg.paths.output_dir}")
172
173 # always close wandb run (even if exception occurs so multirun won't fail)
174 if find_spec("wandb"): # check if wandb is installed
175 import wandb
176
177 if wandb.run:
178 log.info("Closing wandb!")
179 wandb.finish()
180
181 return metric_dict, object_dict
182
183 return wrap
184
185
186def get_metric_value(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected