memory.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. """
  2. A context object for caching a function's return value each time it
  3. is called with the same input arguments.
  4. """
  5. # Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
  6. # Copyright (c) 2009 Gael Varoquaux
  7. # License: BSD Style, 3 clauses.
  8. import asyncio
  9. import datetime
  10. import functools
  11. import inspect
  12. import logging
  13. import os
  14. import pathlib
  15. import pydoc
  16. import re
  17. import textwrap
  18. import time
  19. import tokenize
  20. import traceback
  21. import warnings
  22. import weakref
  23. from . import hashing
  24. from ._store_backends import (
  25. CacheWarning, # noqa
  26. FileSystemStoreBackend,
  27. StoreBackendBase,
  28. )
  29. from .func_inspect import (
  30. filter_args,
  31. format_call,
  32. format_signature,
  33. get_func_code,
  34. get_func_name,
  35. )
  36. from .logger import Logger, format_time, pformat
  37. FIRST_LINE_TEXT = "# first line:"
  38. # TODO: The following object should have a data store object as a sub
  39. # object, and the interface to persist and query should be separated in
  40. # the data store.
  41. #
  42. # This would enable creating 'Memory' objects with a different logic for
  43. # pickling that would simply span a MemorizedFunc with the same
  44. # store (or do we want to copy it to avoid cross-talks?), for instance to
  45. # implement HDF5 pickling.
  46. # TODO: Same remark for the logger, and probably use the Python logging
  47. # mechanism.
  48. def extract_first_line(func_code):
  49. """Extract the first line information from the function code
  50. text if available.
  51. """
  52. if func_code.startswith(FIRST_LINE_TEXT):
  53. func_code = func_code.split("\n")
  54. first_line = int(func_code[0][len(FIRST_LINE_TEXT) :])
  55. func_code = "\n".join(func_code[1:])
  56. else:
  57. first_line = -1
  58. return func_code, first_line
  59. class JobLibCollisionWarning(UserWarning):
  60. """Warn that there might be a collision between names of functions."""
  61. _STORE_BACKENDS = {"local": FileSystemStoreBackend}
  62. def register_store_backend(backend_name, backend):
  63. """Extend available store backends.
  64. The Memory, MemorizeResult and MemorizeFunc objects are designed to be
  65. agnostic to the type of store used behind. By default, the local file
  66. system is used but this function gives the possibility to extend joblib's
  67. memory pattern with other types of storage such as cloud storage (S3, GCS,
  68. OpenStack, HadoopFS, etc) or blob DBs.
  69. Parameters
  70. ----------
  71. backend_name: str
  72. The name identifying the store backend being registered. For example,
  73. 'local' is used with FileSystemStoreBackend.
  74. backend: StoreBackendBase subclass
  75. The name of a class that implements the StoreBackendBase interface.
  76. """
  77. if not isinstance(backend_name, str):
  78. raise ValueError(
  79. "Store backend name should be a string, '{0}' given.".format(backend_name)
  80. )
  81. if backend is None or not issubclass(backend, StoreBackendBase):
  82. raise ValueError(
  83. "Store backend should inherit StoreBackendBase, '{0}' given.".format(
  84. backend
  85. )
  86. )
  87. _STORE_BACKENDS[backend_name] = backend
  88. def _store_backend_factory(backend, location, verbose=0, backend_options=None):
  89. """Return the correct store object for the given location."""
  90. if backend_options is None:
  91. backend_options = {}
  92. if isinstance(location, pathlib.Path):
  93. location = str(location)
  94. if isinstance(location, StoreBackendBase):
  95. return location
  96. elif isinstance(location, str):
  97. obj = None
  98. location = os.path.expanduser(location)
  99. # The location is not a local file system, we look in the
  100. # registered backends if there's one matching the given backend
  101. # name.
  102. for backend_key, backend_obj in _STORE_BACKENDS.items():
  103. if backend == backend_key:
  104. obj = backend_obj()
  105. # By default, we assume the FileSystemStoreBackend can be used if no
  106. # matching backend could be found.
  107. if obj is None:
  108. raise TypeError(
  109. "Unknown location {0} or backend {1}".format(location, backend)
  110. )
  111. # The store backend is configured with the extra named parameters,
  112. # some of them are specific to the underlying store backend.
  113. obj.configure(location, verbose=verbose, backend_options=backend_options)
  114. return obj
  115. elif location is not None:
  116. warnings.warn(
  117. "Instantiating a backend using a {} as a location is not "
  118. "supported by joblib. Returning None instead.".format(
  119. location.__class__.__name__
  120. ),
  121. UserWarning,
  122. )
  123. return None
  124. def _build_func_identifier(func):
  125. """Build a roughly unique identifier for the cached function."""
  126. modules, funcname = get_func_name(func)
  127. # We reuse historical fs-like way of building a function identifier
  128. return os.path.join(*modules, funcname)
  129. # An in-memory store to avoid looking at the disk-based function
  130. # source code to check if a function definition has changed
  131. _FUNCTION_HASHES = weakref.WeakKeyDictionary()
  132. ###############################################################################
  133. # class `MemorizedResult`
  134. ###############################################################################
  135. class MemorizedResult(Logger):
  136. """Object representing a cached value.
  137. Attributes
  138. ----------
  139. location: str
  140. The location of joblib cache. Depends on the store backend used.
  141. func: function or str
  142. function whose output is cached. The string case is intended only for
  143. instantiation based on the output of repr() on another instance.
  144. (namely eval(repr(memorized_instance)) works).
  145. argument_hash: str
  146. hash of the function arguments.
  147. backend: str
  148. Type of store backend for reading/writing cache files.
  149. Default is 'local'.
  150. mmap_mode: {None, 'r+', 'r', 'w+', 'c'}
  151. The memmapping mode used when loading from cache numpy arrays. See
  152. numpy.load for the meaning of the different values.
  153. verbose: int
  154. verbosity level (0 means no message).
  155. timestamp, metadata: string
  156. for internal use only.
  157. """
  158. def __init__(
  159. self,
  160. location,
  161. call_id,
  162. backend="local",
  163. mmap_mode=None,
  164. verbose=0,
  165. timestamp=None,
  166. metadata=None,
  167. ):
  168. Logger.__init__(self)
  169. self._call_id = call_id
  170. self.store_backend = _store_backend_factory(backend, location, verbose=verbose)
  171. self.mmap_mode = mmap_mode
  172. if metadata is not None:
  173. self.metadata = metadata
  174. else:
  175. self.metadata = self.store_backend.get_metadata(self._call_id)
  176. self.duration = self.metadata.get("duration", None)
  177. self.verbose = verbose
  178. self.timestamp = timestamp
  179. @property
  180. def func(self):
  181. return self.func_id
  182. @property
  183. def func_id(self):
  184. return self._call_id[0]
  185. @property
  186. def args_id(self):
  187. return self._call_id[1]
  188. def get(self):
  189. """Read value from cache and return it."""
  190. try:
  191. return self.store_backend.load_item(
  192. self._call_id,
  193. timestamp=self.timestamp,
  194. metadata=self.metadata,
  195. verbose=self.verbose,
  196. )
  197. except ValueError as exc:
  198. new_exc = KeyError(
  199. "Error while trying to load a MemorizedResult's value. "
  200. "It seems that this folder is corrupted : {}".format(
  201. os.path.join(self.store_backend.location, *self._call_id)
  202. )
  203. )
  204. raise new_exc from exc
  205. def clear(self):
  206. """Clear value from cache"""
  207. self.store_backend.clear_item(self._call_id)
  208. def __repr__(self):
  209. return '{}(location="{}", func="{}", args_id="{}")'.format(
  210. self.__class__.__name__, self.store_backend.location, *self._call_id
  211. )
  212. def __getstate__(self):
  213. state = self.__dict__.copy()
  214. state["timestamp"] = None
  215. return state
  216. class NotMemorizedResult(object):
  217. """Class representing an arbitrary value.
  218. This class is a replacement for MemorizedResult when there is no cache.
  219. """
  220. __slots__ = ("value", "valid")
  221. def __init__(self, value):
  222. self.value = value
  223. self.valid = True
  224. def get(self):
  225. if self.valid:
  226. return self.value
  227. else:
  228. raise KeyError("No value stored.")
  229. def clear(self):
  230. self.valid = False
  231. self.value = None
  232. def __repr__(self):
  233. if self.valid:
  234. return "{class_name}({value})".format(
  235. class_name=self.__class__.__name__, value=pformat(self.value)
  236. )
  237. else:
  238. return self.__class__.__name__ + " with no value"
  239. # __getstate__ and __setstate__ are required because of __slots__
  240. def __getstate__(self):
  241. return {"valid": self.valid, "value": self.value}
  242. def __setstate__(self, state):
  243. self.valid = state["valid"]
  244. self.value = state["value"]
  245. ###############################################################################
  246. # class `NotMemorizedFunc`
  247. ###############################################################################
  248. class NotMemorizedFunc(object):
  249. """No-op object decorating a function.
  250. This class replaces MemorizedFunc when there is no cache. It provides an
  251. identical API but does not write anything on disk.
  252. Attributes
  253. ----------
  254. func: callable
  255. Original undecorated function.
  256. """
  257. # Should be a light as possible (for speed)
  258. def __init__(self, func):
  259. self.func = func
  260. def __call__(self, *args, **kwargs):
  261. return self.func(*args, **kwargs)
  262. def call_and_shelve(self, *args, **kwargs):
  263. return NotMemorizedResult(self.func(*args, **kwargs))
  264. def __repr__(self):
  265. return "{0}(func={1})".format(self.__class__.__name__, self.func)
  266. def clear(self, warn=True):
  267. # Argument "warn" is for compatibility with MemorizedFunc.clear
  268. pass
  269. def call(self, *args, **kwargs):
  270. return self.func(*args, **kwargs), {}
  271. def check_call_in_cache(self, *args, **kwargs):
  272. return False
  273. ###############################################################################
  274. # class `AsyncNotMemorizedFunc`
  275. ###############################################################################
  276. class AsyncNotMemorizedFunc(NotMemorizedFunc):
  277. async def call_and_shelve(self, *args, **kwargs):
  278. return NotMemorizedResult(await self.func(*args, **kwargs))
  279. ###############################################################################
  280. # class `MemorizedFunc`
  281. ###############################################################################
  282. class MemorizedFunc(Logger):
  283. """Callable object decorating a function for caching its return value
  284. each time it is called.
  285. Methods are provided to inspect the cache or clean it.
  286. Attributes
  287. ----------
  288. func: callable
  289. The original, undecorated, function.
  290. location: string
  291. The location of joblib cache. Depends on the store backend used.
  292. backend: str
  293. Type of store backend for reading/writing cache files.
  294. Default is 'local', in which case the location is the path to a
  295. disk storage.
  296. ignore: list or None
  297. List of variable names to ignore when choosing whether to
  298. recompute.
  299. mmap_mode: {None, 'r+', 'r', 'w+', 'c'}
  300. The memmapping mode used when loading from cache
  301. numpy arrays. See numpy.load for the meaning of the different
  302. values.
  303. compress: boolean, or integer
  304. Whether to zip the stored data on disk. If an integer is
  305. given, it should be between 1 and 9, and sets the amount
  306. of compression. Note that compressed arrays cannot be
  307. read by memmapping.
  308. verbose: int, optional
  309. The verbosity flag, controls messages that are issued as
  310. the function is evaluated.
  311. cache_validation_callback: callable, optional
  312. Callable to check if a result in cache is valid or is to be recomputed.
  313. When the function is called with arguments for which a cache exists,
  314. the callback is called with the cache entry's metadata as its sole
  315. argument. If it returns True, the cached result is returned, else the
  316. cache for these arguments is cleared and the result is recomputed.
  317. """
  318. # ------------------------------------------------------------------------
  319. # Public interface
  320. # ------------------------------------------------------------------------
  321. def __init__(
  322. self,
  323. func,
  324. location,
  325. backend="local",
  326. ignore=None,
  327. mmap_mode=None,
  328. compress=False,
  329. verbose=1,
  330. timestamp=None,
  331. cache_validation_callback=None,
  332. ):
  333. Logger.__init__(self)
  334. self.mmap_mode = mmap_mode
  335. self.compress = compress
  336. self.func = func
  337. self.cache_validation_callback = cache_validation_callback
  338. self.func_id = _build_func_identifier(func)
  339. self.ignore = ignore if ignore is not None else []
  340. self._verbose = verbose
  341. # retrieve store object from backend type and location.
  342. self.store_backend = _store_backend_factory(
  343. backend,
  344. location,
  345. verbose=verbose,
  346. backend_options=dict(compress=compress, mmap_mode=mmap_mode),
  347. )
  348. if self.store_backend is not None:
  349. # Create func directory on demand.
  350. self.store_backend.store_cached_func_code([self.func_id])
  351. self.timestamp = timestamp if timestamp is not None else time.time()
  352. try:
  353. functools.update_wrapper(self, func)
  354. except Exception:
  355. pass # Objects like ufunc don't like that
  356. if inspect.isfunction(func):
  357. doc = pydoc.TextDoc().document(func)
  358. # Remove blank line
  359. doc = doc.replace("\n", "\n\n", 1)
  360. # Strip backspace-overprints for compatibility with autodoc
  361. doc = re.sub("\x08.", "", doc)
  362. else:
  363. # Pydoc does a poor job on other objects
  364. doc = func.__doc__
  365. self.__doc__ = "Memoized version of %s" % doc
  366. self._func_code_info = None
  367. self._func_code_id = None
  368. def _is_in_cache_and_valid(self, call_id):
  369. """Check if the function call is cached and valid for given arguments.
  370. - Compare the function code with the one from the cached function,
  371. asserting if it has changed.
  372. - Check if the function call is present in the cache.
  373. - Call `cache_validation_callback` for user define cache validation.
  374. Returns True if the function call is in cache and can be used, and
  375. returns False otherwise.
  376. """
  377. # Check if the code of the function has changed
  378. if not self._check_previous_func_code(stacklevel=4):
  379. return False
  380. # Check if this specific call is in the cache
  381. if not self.store_backend.contains_item(call_id):
  382. return False
  383. # Call the user defined cache validation callback
  384. metadata = self.store_backend.get_metadata(call_id)
  385. if (
  386. self.cache_validation_callback is not None
  387. and not self.cache_validation_callback(metadata)
  388. ):
  389. self.store_backend.clear_item(call_id)
  390. return False
  391. return True
  392. def _cached_call(self, args, kwargs, shelving):
  393. """Call wrapped function and cache result, or read cache if available.
  394. This function returns the wrapped function output or a reference to
  395. the cached result.
  396. Arguments:
  397. ----------
  398. args, kwargs: list and dict
  399. input arguments for wrapped function
  400. shelving: bool
  401. True when called via the call_and_shelve function.
  402. Returns
  403. -------
  404. output: Output of the wrapped function if shelving is false, or a
  405. MemorizedResult reference to the value if shelving is true.
  406. metadata: dict containing the metadata associated with the call.
  407. """
  408. args_id = self._get_args_id(*args, **kwargs)
  409. call_id = (self.func_id, args_id)
  410. _, func_name = get_func_name(self.func)
  411. func_info = self.store_backend.get_cached_func_info([self.func_id])
  412. location = func_info["location"]
  413. if self._verbose >= 20:
  414. logging.basicConfig(level=logging.INFO)
  415. _, signature = format_signature(self.func, *args, **kwargs)
  416. self.info(
  417. textwrap.dedent(
  418. f"""
  419. Querying {func_name} with signature
  420. {signature}.
  421. (argument hash {args_id})
  422. The store location is {location}.
  423. """
  424. )
  425. )
  426. # Compare the function code with the previous to see if the
  427. # function code has changed and check if the results are present in
  428. # the cache.
  429. if self._is_in_cache_and_valid(call_id):
  430. if shelving:
  431. return self._get_memorized_result(call_id), {}
  432. try:
  433. start_time = time.time()
  434. output = self._load_item(call_id)
  435. if self._verbose > 4:
  436. self._print_duration(
  437. time.time() - start_time, context="cache loaded "
  438. )
  439. return output, {}
  440. except Exception:
  441. # XXX: Should use an exception logger
  442. _, signature = format_signature(self.func, *args, **kwargs)
  443. self.warn(
  444. "Exception while loading results for {}\n {}".format(
  445. signature, traceback.format_exc()
  446. )
  447. )
  448. if self._verbose > 10:
  449. self.warn(
  450. f"Computing func {func_name}, argument hash {args_id} "
  451. f"in location {location}"
  452. )
  453. # Returns the output but not the metadata
  454. return self._call(call_id, args, kwargs, shelving)
  455. @property
  456. def func_code_info(self):
  457. # 3-tuple property containing: the function source code, source file,
  458. # and first line of the code inside the source file
  459. if hasattr(self.func, "__code__"):
  460. if self._func_code_id is None:
  461. self._func_code_id = id(self.func.__code__)
  462. elif id(self.func.__code__) != self._func_code_id:
  463. # Be robust to dynamic reassignments of self.func.__code__
  464. self._func_code_info = None
  465. if self._func_code_info is None:
  466. # Cache the source code of self.func . Provided that get_func_code
  467. # (which should be called once on self) gets called in the process
  468. # in which self.func was defined, this caching mechanism prevents
  469. # undesired cache clearing when the cached function is called in
  470. # an environment where the introspection utilities get_func_code
  471. # relies on do not work (typically, in joblib child processes).
  472. # See #1035 for more info
  473. # TODO (pierreglaser): do the same with get_func_name?
  474. self._func_code_info = get_func_code(self.func)
  475. return self._func_code_info
  476. def call_and_shelve(self, *args, **kwargs):
  477. """Call wrapped function, cache result and return a reference.
  478. This method returns a reference to the cached result instead of the
  479. result itself. The reference object is small and picklable, allowing
  480. to send or store it easily. Call .get() on reference object to get
  481. result.
  482. Returns
  483. -------
  484. cached_result: MemorizedResult or NotMemorizedResult
  485. reference to the value returned by the wrapped function. The
  486. class "NotMemorizedResult" is used when there is no cache
  487. activated (e.g. location=None in Memory).
  488. """
  489. # Return the wrapped output, without the metadata
  490. return self._cached_call(args, kwargs, shelving=True)[0]
  491. def __call__(self, *args, **kwargs):
  492. # Return the output, without the metadata
  493. return self._cached_call(args, kwargs, shelving=False)[0]
  494. def __getstate__(self):
  495. # Make sure self.func's source is introspected prior to being pickled -
  496. # code introspection utilities typically do not work inside child
  497. # processes
  498. _ = self.func_code_info
  499. # We don't store the timestamp when pickling, to avoid the hash
  500. # depending from it.
  501. state = self.__dict__.copy()
  502. state["timestamp"] = None
  503. # Invalidate the code id as id(obj) will be different in the child
  504. state["_func_code_id"] = None
  505. return state
  506. def check_call_in_cache(self, *args, **kwargs):
  507. """Check if the function call is cached and valid for given arguments.
  508. Does not call the function or do any work besides function inspection
  509. and argument hashing.
  510. - Compare the function code with the one from the cached function,
  511. asserting if it has changed.
  512. - Check if the function call is present in the cache.
  513. - Call `cache_validation_callback` for user define cache validation.
  514. Returns
  515. -------
  516. is_call_in_cache: bool
  517. Whether or not the function call is in cache and can be used.
  518. """
  519. call_id = (self.func_id, self._get_args_id(*args, **kwargs))
  520. return self._is_in_cache_and_valid(call_id)
  521. # ------------------------------------------------------------------------
  522. # Private interface
  523. # ------------------------------------------------------------------------
  524. def _get_args_id(self, *args, **kwargs):
  525. """Return the input parameter hash of a result."""
  526. return hashing.hash(
  527. filter_args(self.func, self.ignore, args, kwargs),
  528. coerce_mmap=self.mmap_mode is not None,
  529. )
  530. def _hash_func(self):
  531. """Hash a function to key the online cache"""
  532. func_code_h = hash(getattr(self.func, "__code__", None))
  533. return id(self.func), hash(self.func), func_code_h
  534. def _write_func_code(self, func_code, first_line):
  535. """Write the function code and the filename to a file."""
  536. # We store the first line because the filename and the function
  537. # name is not always enough to identify a function: people
  538. # sometimes have several functions named the same way in a
  539. # file. This is bad practice, but joblib should be robust to bad
  540. # practice.
  541. func_code = "%s %i\n%s" % (FIRST_LINE_TEXT, first_line, func_code)
  542. self.store_backend.store_cached_func_code([self.func_id], func_code)
  543. # Also store in the in-memory store of function hashes
  544. is_named_callable = (
  545. hasattr(self.func, "__name__") and self.func.__name__ != "<lambda>"
  546. )
  547. if is_named_callable:
  548. # Don't do this for lambda functions or strange callable
  549. # objects, as it ends up being too fragile
  550. func_hash = self._hash_func()
  551. try:
  552. _FUNCTION_HASHES[self.func] = func_hash
  553. except TypeError:
  554. # Some callable are not hashable
  555. pass
  556. def _check_previous_func_code(self, stacklevel=2):
  557. """
  558. stacklevel is the depth a which this function is called, to
  559. issue useful warnings to the user.
  560. """
  561. # First check if our function is in the in-memory store.
  562. # Using the in-memory store not only makes things faster, but it
  563. # also renders us robust to variations of the files when the
  564. # in-memory version of the code does not vary
  565. try:
  566. if self.func in _FUNCTION_HASHES:
  567. # We use as an identifier the id of the function and its
  568. # hash. This is more likely to falsely change than have hash
  569. # collisions, thus we are on the safe side.
  570. func_hash = self._hash_func()
  571. if func_hash == _FUNCTION_HASHES[self.func]:
  572. return True
  573. except TypeError:
  574. # Some callables are not hashable
  575. pass
  576. # Here, we go through some effort to be robust to dynamically
  577. # changing code and collision. We cannot inspect.getsource
  578. # because it is not reliable when using IPython's magic "%run".
  579. func_code, source_file, first_line = self.func_code_info
  580. try:
  581. old_func_code, old_first_line = extract_first_line(
  582. self.store_backend.get_cached_func_code([self.func_id])
  583. )
  584. except (IOError, OSError): # some backend can also raise OSError
  585. self._write_func_code(func_code, first_line)
  586. return False
  587. if old_func_code == func_code:
  588. return True
  589. # We have differing code, is this because we are referring to
  590. # different functions, or because the function we are referring to has
  591. # changed?
  592. _, func_name = get_func_name(
  593. self.func, resolv_alias=False, win_characters=False
  594. )
  595. if old_first_line == first_line == -1 or func_name == "<lambda>":
  596. if not first_line == -1:
  597. func_description = "{0} ({1}:{2})".format(
  598. func_name, source_file, first_line
  599. )
  600. else:
  601. func_description = func_name
  602. warnings.warn(
  603. JobLibCollisionWarning(
  604. "Cannot detect name collisions for function '{0}'".format(
  605. func_description
  606. )
  607. ),
  608. stacklevel=stacklevel,
  609. )
  610. # Fetch the code at the old location and compare it. If it is the
  611. # same than the code store, we have a collision: the code in the
  612. # file has not changed, but the name we have is pointing to a new
  613. # code block.
  614. if not old_first_line == first_line and source_file is not None:
  615. if os.path.exists(source_file):
  616. _, func_name = get_func_name(self.func, resolv_alias=False)
  617. num_lines = len(func_code.split("\n"))
  618. with tokenize.open(source_file) as f:
  619. on_disk_func_code = f.readlines()[
  620. old_first_line - 1 : old_first_line - 1 + num_lines - 1
  621. ]
  622. on_disk_func_code = "".join(on_disk_func_code)
  623. possible_collision = (
  624. on_disk_func_code.rstrip() == old_func_code.rstrip()
  625. )
  626. else:
  627. possible_collision = source_file.startswith("<doctest ")
  628. if possible_collision:
  629. warnings.warn(
  630. JobLibCollisionWarning(
  631. "Possible name collisions between functions "
  632. "'%s' (%s:%i) and '%s' (%s:%i)"
  633. % (
  634. func_name,
  635. source_file,
  636. old_first_line,
  637. func_name,
  638. source_file,
  639. first_line,
  640. )
  641. ),
  642. stacklevel=stacklevel,
  643. )
  644. # The function has changed, wipe the cache directory.
  645. # XXX: Should be using warnings, and giving stacklevel
  646. if self._verbose > 10:
  647. _, func_name = get_func_name(self.func, resolv_alias=False)
  648. self.warn(
  649. "Function {0} (identified by {1}) has changed.".format(
  650. func_name, self.func_id
  651. )
  652. )
  653. self.clear(warn=True)
  654. return False
  655. def clear(self, warn=True):
  656. """Empty the function's cache."""
  657. func_id = self.func_id
  658. if self._verbose > 0 and warn:
  659. self.warn("Clearing function cache identified by %s" % func_id)
  660. self.store_backend.clear_path(
  661. [
  662. func_id,
  663. ]
  664. )
  665. func_code, _, first_line = self.func_code_info
  666. self._write_func_code(func_code, first_line)
  667. def call(self, *args, **kwargs):
  668. """Force the execution of the function with the given arguments.
  669. The output values will be persisted, i.e., the cache will be updated
  670. with any new values.
  671. Parameters
  672. ----------
  673. *args: arguments
  674. The arguments.
  675. **kwargs: keyword arguments
  676. Keyword arguments.
  677. Returns
  678. -------
  679. output : object
  680. The output of the function call.
  681. metadata : dict
  682. The metadata associated with the call.
  683. """
  684. call_id = (self.func_id, self._get_args_id(*args, **kwargs))
  685. # Return the output and the metadata
  686. return self._call(call_id, args, kwargs)
  687. def _call(self, call_id, args, kwargs, shelving=False):
  688. # Return the output and the metadata
  689. self._before_call(args, kwargs)
  690. start_time = time.time()
  691. output = self.func(*args, **kwargs)
  692. return self._after_call(call_id, args, kwargs, shelving, output, start_time)
  693. def _before_call(self, args, kwargs):
  694. if self._verbose > 0:
  695. print(format_call(self.func, args, kwargs))
  696. def _after_call(self, call_id, args, kwargs, shelving, output, start_time):
  697. self.store_backend.dump_item(call_id, output, verbose=self._verbose)
  698. duration = time.time() - start_time
  699. if self._verbose > 0:
  700. self._print_duration(duration)
  701. metadata = self._persist_input(duration, call_id, args, kwargs)
  702. if shelving:
  703. return self._get_memorized_result(call_id, metadata), metadata
  704. if self.mmap_mode is not None:
  705. # Memmap the output at the first call to be consistent with
  706. # later calls
  707. output = self._load_item(call_id, metadata)
  708. return output, metadata
  709. def _persist_input(self, duration, call_id, args, kwargs, this_duration_limit=0.5):
  710. """Save a small summary of the call using json format in the
  711. output directory.
  712. output_dir: string
  713. directory where to write metadata.
  714. duration: float
  715. time taken by hashing input arguments, calling the wrapped
  716. function and persisting its output.
  717. args, kwargs: list and dict
  718. input arguments for wrapped function
  719. this_duration_limit: float
  720. Max execution time for this function before issuing a warning.
  721. """
  722. start_time = time.time()
  723. argument_dict = filter_args(self.func, self.ignore, args, kwargs)
  724. input_repr = dict((k, repr(v)) for k, v in argument_dict.items())
  725. # This can fail due to race-conditions with multiple
  726. # concurrent joblibs removing the file or the directory
  727. metadata = {
  728. "duration": duration,
  729. "input_args": input_repr,
  730. "time": start_time,
  731. }
  732. self.store_backend.store_metadata(call_id, metadata)
  733. this_duration = time.time() - start_time
  734. if this_duration > this_duration_limit:
  735. # This persistence should be fast. It will not be if repr() takes
  736. # time and its output is large, because json.dump will have to
  737. # write a large file. This should not be an issue with numpy arrays
  738. # for which repr() always output a short representation, but can
  739. # be with complex dictionaries. Fixing the problem should be a
  740. # matter of replacing repr() above by something smarter.
  741. warnings.warn(
  742. "Persisting input arguments took %.2fs to run."
  743. "If this happens often in your code, it can cause "
  744. "performance problems "
  745. "(results will be correct in all cases). "
  746. "The reason for this is probably some large input "
  747. "arguments for a wrapped function." % this_duration,
  748. stacklevel=5,
  749. )
  750. return metadata
  751. def _get_memorized_result(self, call_id, metadata=None):
  752. return MemorizedResult(
  753. self.store_backend,
  754. call_id,
  755. metadata=metadata,
  756. timestamp=self.timestamp,
  757. verbose=self._verbose - 1,
  758. )
  759. def _load_item(self, call_id, metadata=None):
  760. return self.store_backend.load_item(
  761. call_id, metadata=metadata, timestamp=self.timestamp, verbose=self._verbose
  762. )
  763. def _print_duration(self, duration, context=""):
  764. _, name = get_func_name(self.func)
  765. msg = f"{name} {context}- {format_time(duration)}"
  766. print(max(0, (80 - len(msg))) * "_" + msg)
  767. # ------------------------------------------------------------------------
  768. # Private `object` interface
  769. # ------------------------------------------------------------------------
  770. def __repr__(self):
  771. return "{class_name}(func={func}, location={location})".format(
  772. class_name=self.__class__.__name__,
  773. func=self.func,
  774. location=self.store_backend.location,
  775. )
  776. ###############################################################################
  777. # class `AsyncMemorizedFunc`
  778. ###############################################################################
  779. class AsyncMemorizedFunc(MemorizedFunc):
  780. async def __call__(self, *args, **kwargs):
  781. out = self._cached_call(args, kwargs, shelving=False)
  782. out = await out if asyncio.iscoroutine(out) else out
  783. return out[0] # Don't return metadata
  784. async def call_and_shelve(self, *args, **kwargs):
  785. out = self._cached_call(args, kwargs, shelving=True)
  786. out = await out if asyncio.iscoroutine(out) else out
  787. return out[0] # Don't return metadata
  788. async def call(self, *args, **kwargs):
  789. out = super().call(*args, **kwargs)
  790. return await out if asyncio.iscoroutine(out) else out
  791. async def _call(self, call_id, args, kwargs, shelving=False):
  792. self._before_call(args, kwargs)
  793. start_time = time.time()
  794. output = await self.func(*args, **kwargs)
  795. return self._after_call(call_id, args, kwargs, shelving, output, start_time)
  796. ###############################################################################
  797. # class `Memory`
  798. ###############################################################################
  799. class Memory(Logger):
  800. """A context object for caching a function's return value each time it
  801. is called with the same input arguments.
  802. All values are cached on the filesystem, in a deep directory
  803. structure.
  804. Read more in the :ref:`User Guide <memory>`.
  805. Parameters
  806. ----------
  807. location: str, pathlib.Path or None
  808. The path of the base directory to use as a data store
  809. or None. If None is given, no caching is done and
  810. the Memory object is completely transparent. This option
  811. replaces cachedir since version 0.12.
  812. backend: str, optional, default='local'
  813. Type of store backend for reading/writing cache files.
  814. The 'local' backend is using regular filesystem operations to
  815. manipulate data (open, mv, etc) in the backend.
  816. mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, optional
  817. The memmapping mode used when loading from cache
  818. numpy arrays. See numpy.load for the meaning of the
  819. arguments.
  820. compress: boolean, or integer, optional
  821. Whether to zip the stored data on disk. If an integer is
  822. given, it should be between 1 and 9, and sets the amount
  823. of compression. Note that compressed arrays cannot be
  824. read by memmapping.
  825. verbose: int, optional
  826. Verbosity flag, controls the debug messages that are issued
  827. as functions are evaluated.
  828. backend_options: dict, optional
  829. Contains a dictionary of named parameters used to configure
  830. the store backend.
  831. """
  832. # ------------------------------------------------------------------------
  833. # Public interface
  834. # ------------------------------------------------------------------------
  835. def __init__(
  836. self,
  837. location=None,
  838. backend="local",
  839. mmap_mode=None,
  840. compress=False,
  841. verbose=1,
  842. backend_options=None,
  843. ):
  844. Logger.__init__(self)
  845. self._verbose = verbose
  846. self.mmap_mode = mmap_mode
  847. self.timestamp = time.time()
  848. self.backend = backend
  849. self.compress = compress
  850. if backend_options is None:
  851. backend_options = {}
  852. self.backend_options = backend_options
  853. if compress and mmap_mode is not None:
  854. warnings.warn("Compressed results cannot be memmapped", stacklevel=2)
  855. self.location = location
  856. if isinstance(location, str):
  857. location = os.path.join(location, "joblib")
  858. self.store_backend = _store_backend_factory(
  859. backend,
  860. location,
  861. verbose=self._verbose,
  862. backend_options=dict(
  863. compress=compress, mmap_mode=mmap_mode, **backend_options
  864. ),
  865. )
  866. def cache(
  867. self,
  868. func=None,
  869. ignore=None,
  870. verbose=None,
  871. mmap_mode=False,
  872. cache_validation_callback=None,
  873. ):
  874. """Decorates the given function func to only compute its return
  875. value for input arguments not cached on disk.
  876. Parameters
  877. ----------
  878. func: callable, optional
  879. The function to be decorated
  880. ignore: list of strings
  881. A list of arguments name to ignore in the hashing
  882. verbose: integer, optional
  883. The verbosity mode of the function. By default that
  884. of the memory object is used.
  885. mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, optional
  886. The memmapping mode used when loading from cache
  887. numpy arrays. See numpy.load for the meaning of the
  888. arguments. By default that of the memory object is used.
  889. cache_validation_callback: callable, optional
  890. Callable to validate whether or not the cache is valid. When
  891. the cached function is called with arguments for which a cache
  892. exists, this callable is called with the metadata of the cached
  893. result as its sole argument. If it returns True, then the
  894. cached result is returned, else the cache for these arguments
  895. is cleared and recomputed.
  896. Returns
  897. -------
  898. decorated_func: MemorizedFunc object
  899. The returned object is a MemorizedFunc object, that is
  900. callable (behaves like a function), but offers extra
  901. methods for cache lookup and management. See the
  902. documentation for :class:`joblib.memory.MemorizedFunc`.
  903. """
  904. if cache_validation_callback is not None and not callable(
  905. cache_validation_callback
  906. ):
  907. raise ValueError(
  908. "cache_validation_callback needs to be callable. "
  909. f"Got {cache_validation_callback}."
  910. )
  911. if func is None:
  912. # Partial application, to be able to specify extra keyword
  913. # arguments in decorators
  914. return functools.partial(
  915. self.cache,
  916. ignore=ignore,
  917. mmap_mode=mmap_mode,
  918. verbose=verbose,
  919. cache_validation_callback=cache_validation_callback,
  920. )
  921. if self.store_backend is None:
  922. cls = (
  923. AsyncNotMemorizedFunc
  924. if inspect.iscoroutinefunction(func)
  925. else NotMemorizedFunc
  926. )
  927. return cls(func)
  928. if verbose is None:
  929. verbose = self._verbose
  930. if mmap_mode is False:
  931. mmap_mode = self.mmap_mode
  932. if isinstance(func, MemorizedFunc):
  933. func = func.func
  934. cls = AsyncMemorizedFunc if inspect.iscoroutinefunction(func) else MemorizedFunc
  935. return cls(
  936. func,
  937. location=self.store_backend,
  938. backend=self.backend,
  939. ignore=ignore,
  940. mmap_mode=mmap_mode,
  941. compress=self.compress,
  942. verbose=verbose,
  943. timestamp=self.timestamp,
  944. cache_validation_callback=cache_validation_callback,
  945. )
  946. def clear(self, warn=True):
  947. """Erase the complete cache directory."""
  948. if warn:
  949. self.warn("Flushing completely the cache")
  950. if self.store_backend is not None:
  951. self.store_backend.clear()
  952. # As the cache is completely clear, make sure the _FUNCTION_HASHES
  953. # cache is also reset. Else, for a function that is present in this
  954. # table, results cached after this clear will be have cache miss
  955. # as the function code is not re-written.
  956. _FUNCTION_HASHES.clear()
  957. def reduce_size(self, bytes_limit=None, items_limit=None, age_limit=None):
  958. """Remove cache elements to make the cache fit its limits.
  959. The limitation can impose that the cache size fits in ``bytes_limit``,
  960. that the number of cache items is no more than ``items_limit``, and
  961. that all files in cache are not older than ``age_limit``.
  962. Parameters
  963. ----------
  964. bytes_limit: int | str, optional
  965. Limit in bytes of the size of the cache. By default, the size of
  966. the cache is unlimited. When reducing the size of the cache,
  967. ``joblib`` keeps the most recently accessed items first. If a
  968. str is passed, it is converted to a number of bytes using units
  969. { K | M | G} for kilo, mega, giga.
  970. items_limit: int, optional
  971. Number of items to limit the cache to. By default, the number of
  972. items in the cache is unlimited. When reducing the size of the
  973. cache, ``joblib`` keeps the most recently accessed items first.
  974. age_limit: datetime.timedelta, optional
  975. Maximum age of items to limit the cache to. When reducing the size
  976. of the cache, any items last accessed more than the given length of
  977. time ago are deleted. Example: to remove files older than 5 days,
  978. use datetime.timedelta(days=5). Negative timedelta are not
  979. accepted.
  980. """
  981. if self.store_backend is None:
  982. # No cached results, this function does nothing.
  983. return
  984. if bytes_limit is None and items_limit is None and age_limit is None:
  985. # No limitation to impose, returning
  986. return
  987. # Defers the actual limits enforcing to the store backend.
  988. self.store_backend.enforce_store_limits(bytes_limit, items_limit, age_limit)
  989. def eval(self, func, *args, **kwargs):
  990. """Eval function func with arguments `*args` and `**kwargs`,
  991. in the context of the memory.
  992. This method works similarly to the builtin `apply`, except
  993. that the function is called only if the cache is not
  994. up to date.
  995. """
  996. if self.store_backend is None:
  997. return func(*args, **kwargs)
  998. return self.cache(func)(*args, **kwargs)
  999. # ------------------------------------------------------------------------
  1000. # Private `object` interface
  1001. # ------------------------------------------------------------------------
  1002. def __repr__(self):
  1003. return "{class_name}(location={location})".format(
  1004. class_name=self.__class__.__name__,
  1005. location=(
  1006. None if self.store_backend is None else self.store_backend.location
  1007. ),
  1008. )
  1009. def __getstate__(self):
  1010. """We don't store the timestamp when pickling, to avoid the hash
  1011. depending from it.
  1012. """
  1013. state = self.__dict__.copy()
  1014. state["timestamp"] = None
  1015. return state
  1016. ###############################################################################
  1017. # cache_validation_callback helpers
  1018. ###############################################################################
  1019. def expires_after(
  1020. days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0
  1021. ):
  1022. """Helper cache_validation_callback to force recompute after a duration.
  1023. Parameters
  1024. ----------
  1025. days, seconds, microseconds, milliseconds, minutes, hours, weeks: numbers
  1026. argument passed to a timedelta.
  1027. """
  1028. delta = datetime.timedelta(
  1029. days=days,
  1030. seconds=seconds,
  1031. microseconds=microseconds,
  1032. milliseconds=milliseconds,
  1033. minutes=minutes,
  1034. hours=hours,
  1035. weeks=weeks,
  1036. )
  1037. def cache_validation_callback(metadata):
  1038. computation_age = time.time() - metadata["time"]
  1039. return computation_age < delta.total_seconds()
  1040. return cache_validation_callback