-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from shtopane/kk/detach-config-jax-econpizza
Update config to split `jax` and `econpizza` cache
- Loading branch information
Showing
2 changed files
with
52 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,41 @@ | ||
"""Configuration object""" | ||
|
||
import os | ||
import jax | ||
|
||
class EconPizzaConfig(dict): | ||
def __init__(self, *args, **kwargs): | ||
super(EconPizzaConfig, self).__init__(*args, **kwargs) | ||
self._enable_persistent_cache = False | ||
self._econpizza_cache_folder = "__econpizza_cache__" | ||
self._jax_cache_folder = "__jax_cache__" | ||
|
||
@property | ||
def enable_persistent_cache(self): | ||
return self._enable_persistent_cache | ||
self.__dict__ = self | ||
self.enable_jax_persistent_cache = False | ||
self.jax_cache_folder = "__jax_cache__" | ||
|
||
@enable_persistent_cache.setter | ||
def enable_persistent_cache(self, value): | ||
self._enable_persistent_cache = value | ||
self.setup_persistent_cache() | ||
self._setup_persistent_cache_map = { | ||
"enable_jax_persistent_cache": self.setup_persistent_cache_jax | ||
} | ||
|
||
@property | ||
def jax_cache_folder(self): | ||
return self._jax_cache_folder | ||
|
||
@jax_cache_folder.setter | ||
def jax_cache_folder(self, value): | ||
self._jax_cache_folder = value | ||
def __setitem__(self, key, value): | ||
return self.update(key, value) | ||
|
||
@property | ||
def econpizza_cache_folder(self): | ||
return self._econpizza_cache_folder | ||
|
||
@econpizza_cache_folder.setter | ||
def econpizza_cache_folder(self, value): | ||
self._econpizza_cache_folder = value | ||
|
||
def update(self, key, value): | ||
"""Updates the attribute, and if it's related to caching, calls the appropriate setup function.""" | ||
if hasattr(self, key): | ||
setattr(self, key, value) | ||
if key in self._setup_persistent_cache_map and value: | ||
self._setup_persistent_cache_map[key]() | ||
else: | ||
raise AttributeError(f"'EconPizzaConfig' object has no attribute '{key}'") | ||
|
||
def _create_cache_dir(self, folder_name: str): | ||
cwd = os.getcwd() | ||
folder_path = os.path.join(cwd, folder_name) | ||
os.makedirs(folder_path, exist_ok=True) | ||
|
||
return folder_path | ||
|
||
def setup_persistent_cache(self): | ||
"""Create folders for JAX and EconPizza cache. | ||
By default, they are created in callee working directory. | ||
""" | ||
if self.enable_persistent_cache == True: | ||
if not os.path.exists(self.econpizza_cache_folder): | ||
folder_path_pizza = self._create_cache_dir(self.econpizza_cache_folder) | ||
self.econpizza_cache_folder = folder_path_pizza | ||
else: | ||
folder_path_pizza = self.econpizza_cache_folder | ||
|
||
# Jax cache is enabled by the used via JAX API. In this case we should not set another folder | ||
if jax.config.jax_compilation_cache_dir is None: | ||
folder_path_jax = self._create_cache_dir(self.jax_cache_folder) | ||
jax.config.update("jax_compilation_cache_dir", folder_path_jax) | ||
jax.config.update("jax_persistent_cache_min_entry_size_bytes", -1) | ||
jax.config.update("jax_persistent_cache_min_compile_time_secs", 0) | ||
self.jax_cache_folder = folder_path_jax | ||
|
||
def __repr__(self): | ||
properties = { | ||
k.lstrip("_"): v for k, v in self.__dict__.items() if k.startswith("_") | ||
} | ||
return f"{properties}" | ||
|
||
def __str__(self): | ||
return self.__repr__() | ||
|
||
def setup_persistent_cache_jax(self): | ||
"""Setup JAX persistent cache if enabled.""" | ||
if jax.config.jax_compilation_cache_dir is None and not os.path.exists(self.jax_cache_folder): | ||
folder_path_jax = self._create_cache_dir(self.jax_cache_folder) | ||
jax.config.update("jax_compilation_cache_dir", folder_path_jax) | ||
jax.config.update("jax_persistent_cache_min_compile_time_secs", 0) | ||
self.jax_cache_folder = folder_path_jax | ||
|
||
config = EconPizzaConfig() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters