Skip to content

Commit 528c9ad

Browse files
committed
[Feature] Add Redis-backed TensorDict (RedisTensorDict)
Adds RedisTensorDict, a TensorDictBase subclass that stores tensors in a Redis instance for out-of-core storage. This enables datasets that exceed local RAM to be accessed with the familiar TensorDict interface. MVP features: - Async redis.asyncio client on a background event loop with sync wrappers - Zero-copy serialization via torch.frombuffer / untyped_storage bytes - Metadata (shape, dtype) stored in Redis Hashes for fast introspection - Redis Cluster-compatible key schema using hash tags - Pipelined batch I/O for all get/set operations - Nested TensorDict support via lightweight prefix-based views - Pickle support for multi-process usage - from_dict() factory, to_local() / to_tensordict() materialization - CI: install redis package and start redis-server before tests ghstack-source-id: 6cb2eff Pull-Request: pytorch#1567
1 parent 824e231 commit 528c9ad

6 files changed

Lines changed: 1815 additions & 1 deletion

File tree

.github/unittest/linux/scripts/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ dependencies:
2222
- ninja
2323
- numpy<2.0.0
2424
- mosaicml-streaming
25+
- redis

.github/unittest/linux/scripts/run_test.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ export TORCHDYNAMO_INLINE_INBUILT_NN_MODULES=1
2626
export TD_GET_DEFAULTS_TO_NONE=1
2727
export LIST_TO_STACK=1
2828

29+
# Start Redis server for test_redis.py (non-fatal if unavailable)
30+
if command -v redis-server &> /dev/null; then
31+
redis-server --daemonize yes --save "" --appendonly no || true
32+
else
33+
case "$(uname -s)" in
34+
Linux*)
35+
apt update -y && apt install -y redis-server && redis-server --daemonize yes --save "" --appendonly no || echo "Redis server not available, redis tests will be skipped"
36+
;;
37+
Darwin*)
38+
brew install redis 2>/dev/null && redis-server --daemonize yes --save "" --appendonly no || echo "Redis server not available, redis tests will be skipped"
39+
;;
40+
*)
41+
echo "Redis server not available on this platform, redis tests will be skipped"
42+
;;
43+
esac
44+
fi
45+
2946
coverage run -m pytest test/smoke_test.py -v --durations 20
3047
coverage run -m pytest --runslow --instafail -v --durations 20 --timeout 120
3148
coverage run -m pytest ./benchmarks --instafail -v --durations 20

.github/unittest/linux/scripts/setup_env.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ if [ "${PYTHON_VERSION}" == "3.14t" ]; then
9494
# Install test dependencies (mirrors environment.yml)
9595
pip install pybind11 numpy expecttest pyyaml hypothesis future cloudpickle \
9696
pytest pytest-benchmark pytest-cov pytest-mock pytest-instafail \
97-
pytest-rerunfailures pytest-timeout coverage h5py orjson ninja protobuf
97+
pytest-rerunfailures pytest-timeout coverage h5py orjson ninja protobuf redis
9898
# Note: mosaicml-streaming may not be available for 3.14t yet, skip if fails
9999
pip install mosaicml-streaming || echo "mosaicml-streaming not available for Python 3.14t, skipping"
100100
else

tensordict/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
)
4848
from tensordict.memmap import MemoryMappedTensor
4949
from tensordict.persistent import PersistentTensorDict
50+
from tensordict.redis import RedisTensorDict
5051
from tensordict.tensorclass import (
5152
from_dataclass,
5253
MetaData,
@@ -105,6 +106,7 @@
105106
"TensorClass",
106107
"MemoryMappedTensor",
107108
"PersistentTensorDict",
109+
"RedisTensorDict",
108110
"NestedKey",
109111
# Factory functions
110112
"from_dict",

0 commit comments

Comments
 (0)