快捷方式

LazyMemmapStorage

class torchrl.data.replay_buffers.LazyMemmapStorage(max_size: int, *, scratch_dir=None, device: device = 'cpu', ndim: int = 1, existsok: bool = False, compilable: bool = False)[source]

用於張量 (tensors) 和 tensordicts 的記憶體對映儲存。

引數:

max_size (int) – 儲存大小,即緩衝區中儲存的最大元素數量。

關鍵字引數:
  • scratch_dir (strpath) – 記憶體對映張量將寫入的目錄。

  • device (torch.device, 可選) – 取樣張量將被儲存和傳送到的裝置。預設值為 torch.device("cpu")。如果提供 None,裝置將從傳入的第一批資料中自動獲取。預設情況下不啟用此功能,以避免資料被錯誤地放置在 GPU 上,導致 OOM 問題。

  • ndim (int, 可選) – 計算儲存大小時要考慮的維度數量。例如,形狀為 [3, 4] 的儲存,如果 ndim=1,容量為 3;如果 ndim=2,容量為 12。預設值為 1

  • existsok (bool, 可選) – 如果任何張量已存在於磁碟上,是否應該引發錯誤。預設值為 True。如果為 False,張量將按原樣開啟,不會被覆蓋。

注意

在檢查點 LazyMemmapStorage 時,可以提供與儲存已儲存位置相同的路徑,以避免執行長時間的資料複製,因為資料已儲存在磁碟上。這僅在使用預設的 TensorStorageCheckpointer 檢查點器時有效。示例

>>> from tensordict import TensorDict
>>> from torchrl.data import TensorStorage, LazyMemmapStorage, ReplayBuffer
>>> import tempfile
>>> from pathlib import Path
>>> import time
>>> td = TensorDict(a=0, b=1).expand(1000).clone()
>>> # We pass a path that is <main_ckpt_dir>/storage to LazyMemmapStorage
>>> rb_memmap = ReplayBuffer(storage=LazyMemmapStorage(10_000_000, scratch_dir="dump/storage"))
>>> rb_memmap.extend(td);
>>> # Checkpointing in `dump` is a zero-copy, as the data is already in `dump/storage`
>>> rb_memmap.dumps(Path("./dump"))

示例

>>> data = TensorDict({
...     "some data": torch.randn(10, 11),
...     ("some", "nested", "data"): torch.randn(10, 11, 12),
... }, batch_size=[10, 11])
>>> storage = LazyMemmapStorage(100)
>>> storage.set(range(10), data)
>>> len(storage)  # only the first dimension is considered as indexable
10
>>> storage.get(0)
TensorDict(
    fields={
        some data: MemoryMappedTensor(shape=torch.Size([11]), device=cpu, dtype=torch.float32, is_shared=False),
        some: TensorDict(
            fields={
                nested: TensorDict(
                    fields={
                        data: MemoryMappedTensor(shape=torch.Size([11, 12]), device=cpu, dtype=torch.float32, is_shared=False)},
                    batch_size=torch.Size([11]),
                    device=cpu,
                    is_shared=False)},
            batch_size=torch.Size([11]),
            device=cpu,
            is_shared=False)},
    batch_size=torch.Size([11]),
    device=cpu,
    is_shared=False)

此類也支援 tensorclass 資料。

示例

>>> from tensordict import tensorclass
>>> @tensorclass
... class MyClass:
...     foo: torch.Tensor
...     bar: torch.Tensor
>>> data = MyClass(foo=torch.randn(10, 11), bar=torch.randn(10, 11, 12), batch_size=[10, 11])
>>> storage = LazyMemmapStorage(10)
>>> storage.set(range(10), data)
>>> storage.get(0)
MyClass(
    bar=MemoryMappedTensor(shape=torch.Size([11, 12]), device=cpu, dtype=torch.float32, is_shared=False),
    foo=MemoryMappedTensor(shape=torch.Size([11]), device=cpu, dtype=torch.float32, is_shared=False),
    batch_size=torch.Size([11]),
    device=cpu,
    is_shared=False)
attach(buffer: Any) None

此函式將取樣器附加到此儲存。

從該儲存讀取的緩衝區必須透過呼叫此方法作為附加實體包含在內。這保證了當儲存中的資料發生變化時,即使儲存與其他緩衝區(例如,優先順序取樣器)共享,元件也會意識到這些變化。

引數:

buffer – 從此儲存讀取的物件。

dump(*args, **kwargs)

dumps() 的別名。

load(*args, **kwargs)

loads() 的別名。

save(*args, **kwargs)

dumps() 的別名。

文件

訪問 PyTorch 的完整開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源