Skip to content

Commit c2291d8

Browse files
jan-janssenclaude
andauthored
Warn when init_function is combined with cache_directory (#1085)
The cache key is computed from the submitted function and its arguments only, so it does not reflect the state set by the init_function. Functions which depend on this state can therefore return stale results from the cache. Until this is fixed, warn the user when both options are set. See #1084 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fef1804 commit c2291d8

7 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎src/executorlib/executor/flux.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from executorlib.standalone.inputcheck import (
55
check_command_line_argument_lst,
66
check_init_function,
7+
check_init_function_with_cache,
78
check_log_obj_size,
89
check_oversubscribe,
910
check_plot_dependency_graph,
@@ -580,6 +581,9 @@ def create_flux_executor(
580581
executor_kwargs["hostname_localhost"] = hostname_localhost
581582
executor_kwargs["log_obj_size"] = log_obj_size
582583
check_init_function(block_allocation=block_allocation, init_function=init_function)
584+
check_init_function_with_cache(
585+
init_function=init_function, cache_directory=cache_directory
586+
)
583587
check_pmi(backend="flux_allocation", pmi=pmi_mode)
584588
check_oversubscribe(
585589
oversubscribe=executor_kwargs.get("openmpi_oversubscribe", False)

‎src/executorlib/executor/single.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
check_command_line_argument_lst,
66
check_gpus_per_worker,
77
check_init_function,
8+
check_init_function_with_cache,
89
check_plot_dependency_graph,
910
check_refresh_rate,
1011
check_restart_limit,
@@ -511,6 +512,9 @@ def create_single_node_executor(
511512
executor_kwargs["log_obj_size"] = log_obj_size
512513

513514
check_init_function(block_allocation=block_allocation, init_function=init_function)
515+
check_init_function_with_cache(
516+
init_function=init_function, cache_directory=cache_directory
517+
)
514518
check_gpus_per_worker(gpus_per_worker=executor_kwargs.get("gpus_per_core", 0))
515519
check_command_line_argument_lst(
516520
command_line_argument_lst=executor_kwargs.get("slurm_cmd_args", [])

‎src/executorlib/executor/slurm.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from executorlib.executor.base import BaseExecutor
44
from executorlib.standalone.inputcheck import (
55
check_init_function,
6+
check_init_function_with_cache,
67
check_log_obj_size,
78
check_plot_dependency_graph,
89
check_refresh_rate,
@@ -552,6 +553,9 @@ def create_slurm_executor(
552553
executor_kwargs["log_obj_size"] = log_obj_size
553554
executor_kwargs["pmi_mode"] = pmi_mode
554555
check_init_function(block_allocation=block_allocation, init_function=init_function)
556+
check_init_function_with_cache(
557+
init_function=init_function, cache_directory=cache_directory
558+
)
555559
check_wait_on_shutdown(wait_on_shutdown=wait)
556560
if block_allocation:
557561
executor_kwargs["init_function"] = init_function

‎src/executorlib/standalone/inputcheck.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ def check_init_function(
138138
raise ValueError("")
139139

140140

141+
def check_init_function_with_cache(
142+
init_function: Optional[Callable], cache_directory: Optional[str]
143+
) -> None:
144+
"""
145+
Check if init_function and cache_directory are both set, and raise a warning if they are. The cache key is
146+
computed from the submitted function and its arguments only, so it does not reflect the state set by the
147+
init_function, which can lead to stale results being returned from the cache.
148+
"""
149+
if init_function is not None and cache_directory is not None:
150+
warn(
151+
"The init_function and the cache_directory are incompatible: the cache key does not include the "
152+
"state set by the init_function, so functions which depend on this state may return stale results "
153+
"from the cache. See https://github.com/pyiron/executorlib/issues/1084",
154+
stacklevel=2,
155+
)
156+
157+
141158
def check_max_workers_and_cores(
142159
max_workers: Optional[int], max_cores: Optional[int]
143160
) -> None:

‎src/executorlib/task_scheduler/interactive/spawner_pysqa.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from pysqa import QueueAdapter
77

88
from executorlib.standalone.command_pysqa import pysqa_execute_command, pysqa_terminate
9-
from executorlib.standalone.inputcheck import validate_number_of_cores
9+
from executorlib.standalone.inputcheck import (
10+
check_init_function_with_cache,
11+
validate_number_of_cores,
12+
)
1013
from executorlib.standalone.interactive.spawner import (
1114
BaseSpawner,
1215
set_current_directory_in_environment,
@@ -233,6 +236,9 @@ def create_pysqa_block_allocation_scheduler(
233236
backend: Optional[str] = None,
234237
validator: Callable = validate_resource_dict,
235238
):
239+
check_init_function_with_cache(
240+
init_function=init_function, cache_directory=cache_directory
241+
)
236242
if executor_kwargs is None:
237243
executor_kwargs = {}
238244
cores_per_worker = executor_kwargs.get("cores", 1)

‎tests/unit/executor/test_single_cache.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ def sum_with_wait(a, b):
2323
return a + b
2424

2525

26+
def init_counter():
27+
return {"counter": [0]}
28+
29+
30+
def increment(counter):
31+
counter[0] += 1
32+
return counter[0]
33+
34+
2635
class AddClass:
2736
def __call__(self, a, b):
2837
return a+b
@@ -103,7 +112,23 @@ def test_cache_error_file(self):
103112
self.assertTrue(os.path.exists(error_out))
104113
os.remove(error_out)
105114

115+
def test_cache_init_function_warning(self):
116+
cache_directory = os.path.abspath("cache_init_function")
117+
with self.assertWarns(UserWarning) as cm:
118+
with SingleNodeExecutor(
119+
max_workers=1,
120+
block_allocation=True,
121+
init_function=init_counter,
122+
cache_directory=cache_directory,
123+
) as exe:
124+
cloudpickle_register(ind=1)
125+
result_lst = [exe.submit(increment).result() for _ in range(3)]
126+
self.assertIn("init_function", str(cm.warning))
127+
# Documents the incompatibility from https://github.com/pyiron/executorlib/issues/1084
128+
self.assertEqual(result_lst, [1, 1, 1])
129+
106130
def tearDown(self):
107131
shutil.rmtree("executorlib_cache", ignore_errors=True)
108132
shutil.rmtree("cache_error", ignore_errors=True)
109133
shutil.rmtree("cache_duplicate", ignore_errors=True)
134+
shutil.rmtree("cache_init_function", ignore_errors=True)

‎tests/unit/standalone/test_inputcheck.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import warnings
23

34
from executorlib.standalone.inputcheck import (
45
check_wait_on_shutdown,
@@ -7,6 +8,7 @@
78
check_oversubscribe,
89
check_executor,
910
check_init_function,
11+
check_init_function_with_cache,
1012
check_nested_flux_executor,
1113
check_flux_log_files,
1214
check_pmi,
@@ -50,6 +52,18 @@ def test_check_init_function(self):
5052
with self.assertRaises(ValueError):
5153
check_init_function(init_function=1, block_allocation=False)
5254

55+
def test_check_init_function_with_cache(self):
56+
with self.assertWarns(UserWarning):
57+
check_init_function_with_cache(
58+
init_function=lambda: {}, cache_directory="executorlib_cache"
59+
)
60+
61+
def test_check_init_function_with_cache_no_warning(self):
62+
with warnings.catch_warnings():
63+
warnings.simplefilter("error")
64+
check_init_function_with_cache(init_function=None, cache_directory="executorlib_cache")
65+
check_init_function_with_cache(init_function=lambda: {}, cache_directory=None)
66+
5367
def test_check_refresh_rate(self):
5468
with self.assertRaises(ValueError):
5569
check_refresh_rate(refresh_rate=1)

0 commit comments

Comments
 (0)