Cache#
Modules cache the data they allocate during the forward pass (e.g. the output arrays, per input shape and data type), so that it is allocated only once and reused (e.g. by CUDA graphs) in subsequent calls.
The cached data lives in a global cache, organized in sub-caches (one per module). A module acquires its sub-cache
when it is created, which is exposed via the module’s cache property. A sub-cache is released (and its data freed)
when the module is garbage-collected, and it is cleared when the module is moved to another device.
Example#
import warp as wp
from warp_nn import nn
from warp_nn.utils import cache
relu = nn.ReLU()
input = wp.ones((2, 4), dtype=wp.float32)
output = relu(input)
assert relu(input) is output # the output array is cached
# modules fetch (or create, on first access) their cached data through their sub-cache
buffer = relu.cache.get(("buffer", input.shape), lambda: wp.empty_like(input))
assert relu.cache.get(("buffer", input.shape), lambda: wp.empty_like(input)) is buffer
# disable the cache process-wide (the data is created at every call)
cache.set_enabled(False)
assert relu(input) is not output
cache.set_enabled(True)
# remove the cached data of a module, or of all the modules
cache.clear(relu)
cache.clear()
Warning
CUDA graphs reference the cached arrays by address. Arrays allocated while the cache is disabled (at every call), and arrays removed by clearing the cache (or by moving a module to another device), are freed, so they must not be referenced by CUDA graphs that are replayed afterwards.
API#
- class warp_nn.utils.cache.Cache[source]#
Sub-cache of the global cache, owned by a single object.
Instances are created by
acquire(), and should not be instantiated directly.- get( ) T[source]#
Fetch the cached data for the given key, or create (and cache) it with the factory if it is not cached.
If the cache is disabled (see
is_enabled()), the cached data is bypassed: the factory is called at every invocation, and its result is returned without being cached.Warning
When the cache is disabled, new data (e.g. arrays) is created at every invocation, so it must not be referenced by CUDA graphs that outlive the invocation.
- Parameters:
key – Key of the cached data (e.g. the input shape and data type).
factory – Function (without arguments) that creates the data.
- Returns:
The cached (or created) data.
- warp_nn.utils.cache.acquire(owner: object) Cache[source]#
Acquire the sub-cache of an owner, registering it in the global cache on first acquisition.
The sub-cache is identified by the owner’s identity (
id()), so successive acquisitions for the same owner return the same sub-cache. The sub-cache is released from the global cache (and its cached data freed) when the owner is garbage-collected.- Parameters:
owner – Object owning the sub-cache (e.g. a module). It must support weak references.
- Returns:
The sub-cache of the owner.
- Raises:
TypeError – If the owner does not support weak references.
- warp_nn.utils.cache.clear(owner: object | None = None) None[source]#
Remove the cached data of an owner, or of all the owners.
The sub-caches stay registered (i.e. sub-caches already acquired remain valid), only their data is removed.
Warning
The removed data (e.g. arrays) is freed, so it must not be referenced by CUDA graphs captured before clearing.
- Parameters:
owner – Object whose cached data is removed. If
None, the cached data of all the owners is removed.
- warp_nn.utils.cache.set_enabled(enabled: bool) None[source]#
Enable or disable the cache process-wide.
While disabled, the cached data is bypassed (see
Cache.get()). Data already cached is kept.- Parameters:
enabled – Whether to enable (true) or disable (false) the cache.