Skip to main content

Online cache for prompt embeddings using TensorDict memory-mapped storage

Project description

tensordict-cache

A persistent, memory-mapped cache for tensor data built on TensorDict.

Store and retrieve TensorDict objects on disk using memory-mapped files. Cached entries survive process restarts and are loaded lazily without copying data into RAM.

Installation

pip install tensordict-cache

Quick start

import torch
from tensordict import TensorDict
from tensordict_cache import TensorCache

# Create a cache (directory is created if it doesn't exist)
cache = TensorCache("./my_cache")

# Store embeddings keyed by prompt text
cache["hello world"] = TensorDict(
    {"embedding": torch.randn(768)},
    batch_size=[],
)

# Retrieve them
embedding = cache["hello world"]["embedding"]

Usage

Creating a cache

from tensordict_cache import TensorCache

# Open or create a cache directory
cache = TensorCache("/path/to/cache")

# Open without loading existing entries
cache = TensorCache("/path/to/cache", load_existing=False)

Storing entries

Keys can be strings or integers. Values must be TensorDict instances.

import torch
from tensordict import TensorDict

td = TensorDict({
    "hidden_state": torch.randn(512),
    "logits": torch.randn(10),
}, batch_size=[])

cache["my_prompt"] = td
cache[42] = td  # integer keys work too

Retrieving entries

# Dict-style access (raises KeyError if missing)
result = cache["my_prompt"]

# Safe access with default
result = cache.get("my_prompt")          # returns None if missing
result = cache.get("my_prompt", default) # returns default if missing

Checking membership and length

if "my_prompt" in cache:
    print("Hit!")

print(f"Cache has {len(cache)} entries")

Listing keys and inspecting the cache

# Keys are SHA-256 hashes of the original key
print(cache.keys())

# Human-readable representation
print(cache)
# TensorCache(prefix=/path/to/cache, n_cache=3)

Clearing the cache

# Remove all entries from memory and disk
cache.clear()

Persistence across sessions

Data is written to disk as memory-mapped files. Reopening the same directory automatically loads all previously stored entries:

# Session 1
cache = TensorCache("./my_cache")
cache["prompt_a"] = TensorDict({"v": torch.tensor(1.0)}, batch_size=[])

# Session 2 (new process)
cache = TensorCache("./my_cache")
assert "prompt_a" in cache  # still there

How it works

Each entry is stored in a subdirectory under the cache prefix:

my_cache/
  a1b2c3d4.../   # SHA-256 hash of the key
    meta.json
    *.memmap
  e5f6g7h8.../
    meta.json
    *.memmap

Under the hood, TensorCache calls TensorDict.memmap() to write each entry and TensorDict.load_memmap() to read it back. Memory-mapped storage means tensors are not loaded into RAM until accessed, keeping memory usage low even for large caches.

API reference

Method Description
TensorCache(prefix, load_existing=True) Create or open a cache at prefix
cache[key] = td Store a TensorDict under key
cache[key] Retrieve a TensorDict (raises KeyError if missing)
cache.get(key, default=None) Retrieve or return default
key in cache Check if key exists
len(cache) Number of cached entries
cache.keys() List of hashed key names
cache.clear() Remove all entries from memory and disk
cache.get_cache_size() Total cache size in bytes
cache.get_cache_size_human() Cache size as human-readable string
cache.key_to_basename(key) Get the hashed filename for a key

Requirements

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensordict_cache-0.2.0.tar.gz (69.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensordict_cache-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file tensordict_cache-0.2.0.tar.gz.

File metadata

  • Download URL: tensordict_cache-0.2.0.tar.gz
  • Upload date:
  • Size: 69.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tensordict_cache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8b07b7c83dcd55dd55238d665a5fb1c7428a29b31ac41a639dd368408a812c61
MD5 388d8d7402890a96f62169d3e1c3651e
BLAKE2b-256 a934417791483694d23769917d027b12d25a8738aefe889fb3d0105af673ed25

See more details on using hashes here.

File details

Details for the file tensordict_cache-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tensordict_cache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tensordict_cache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 073508111821d406a001faabcd57190ad1fd0a2dd2987cf68d87638cace0d959
MD5 96c8e21f5cfcf14e03d1cc9254435489
BLAKE2b-256 b83d655a501ae8a4098ae71d90c7d2dd14d0f6b98f66a8b64a92ef82d480df14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page