Caching Utilities¶
fusion_bench.utils.cache_utils
¶
cache_to_disk(file_path)
¶
A decorator to cache the result of a function to a file. If the file exists, the result is loaded from the file. Otherwise, the function is executed and the result is saved to the file.
deprecated
This function is deprecated. Use cache_with_joblib
instead for better
caching capabilities including automatic cache invalidation, better object
handling, and memory efficiency.
Example usage¶
@cache_to_disk("path_to_file.pkl")
def some_function(*args: Any, **kwargs: Any) -> Any:
# Function implementation
return "some result"
Parameters:
-
file_path
(str
) –The path to the file where the result should be cached.
Returns:
-
Callable
(Callable
) –The decorated function.
Source code in fusion_bench/utils/cache_utils.py
cache_with_joblib(cache_dir=None, verbose=0)
¶
A decorator to cache the result of a function using joblib.Memory. This provides more advanced caching capabilities compared to cache_to_disk, including: - Automatic cache invalidation when function arguments change - Better handling of numpy arrays and other complex objects - Memory-efficient storage - Optional verbose output for cache hits/misses
Example usage¶
@cache_with_joblib("./cache", verbose=1)
def expensive_computation(x: int, y: str) -> Any:
# Function implementation
return complex_result
# Or with default settings:
@cache_with_joblib()
def another_function(x: int) -> int:
return x * 2
Parameters:
-
cache_dir
(Union[str, Path]
, default:None
) –The directory where cache files should be stored. If
None
, a default directoryoutputs/cache
will be used. -
verbose
(int
, default:0
) –Verbosity level for joblib.Memory (0=silent, 1=basic, 2++=verbose).
Returns:
-
Callable
(Callable
) –A decorator function that can be applied to functions.