LM wrapper that returns cached results if they exist, and uses the underlying LM if not. :param lm: LM Underlying LM :param cache_db: str Path to cache db
(self, lm, cache_db)
| 880 | |
| 881 | class CachingLM: |
| 882 | def __init__(self, lm, cache_db): |
| 883 | """LM wrapper that returns cached results if they exist, and uses the underlying LM if not. |
| 884 | |
| 885 | :param lm: LM |
| 886 | Underlying LM |
| 887 | :param cache_db: str |
| 888 | Path to cache db |
| 889 | """ |
| 890 | self.lm = lm |
| 891 | self.cache_db = cache_db |
| 892 | if os.path.dirname(cache_db): |
| 893 | os.makedirs(os.path.dirname(cache_db), exist_ok=True) |
| 894 | self.dbdict = SqliteDict(cache_db, autocommit=True) |
| 895 | |
| 896 | # add hook to lm |
| 897 | lm.set_cache_hook(self.get_cache_hook()) |
| 898 | |
| 899 | def __getattr__(self, attr): |
| 900 | lm_attr = getattr(self.lm, attr) |
nothing calls this directly
no test coverage detected