Modal lets you create writeable volumes that can be simultaneously attached to multiple Modal functions. These are helpful for use cases such as:
- Caching model checkpoints
- Storing datasets
- Keeping a shared cache for expensive computations
The modal.SharedVolume constructor
initializes an empty volume. This can be mounted within a function by providing
a mapping between mount paths and
SharedVolume objects. For example, to use a
SharedVolume to initialize a shared
shelve disk cache:
import shelve import modal volume = modal.SharedVolume() def expensive_computation(key: str): with shelve.open("/root/cache/shelve") as cache: cached_val = cache.get(key) if cached_val is not None: return cached_val # cache miss; populate value ...
The above implements basic disk caching, but be aware that
shelve does not
in the event of concurrent read/write operations. To protect against concurrent
write conflicts, the flufl.lock
package is useful. An example of that library’s usage is in the
By default, a modal.SharedVolume lives as
long as the app it’s defined in, just like any other Modal object. However in
many situations you might want to persist the cache between runs of the app. To
do this, you can use the
persist method on the
SharedVolume object. For
example, to avoid re-downloading a HuggingFace model checkpoint every time you
run your function:
import modal volume = modal.SharedVolume().persist("model-cache-vol") stub = modal.Stub() CACHE_DIR = "/cache" def run_inference(): ...
To remove a persisted shared volume, deleting all its data, you must stop the volume. This can be done via the volume’s dashboard app page or the CLI.
For example, a volume with the name
my-vol that lives in the
workspace could be stopped (i.e. deleted) by going to its dashboard page at
https://modal.com/apps/e-corp/my-vol and clicking the trash icon. Alternatively,
you can use the volume’s app ID with
modal app stop.
(Volumes are currently a specialized app type within Modal, which is why deleting a volume is done by stopping an app.)
- The Modal Podcast Transcribe uses a persisted volume to durably store raw audio, metadata, and finished transcriptions.
- News Article Summarizer uses a persisted volume to store pretrained models.