捷徑

MemoryMappedTensor

class tensordict.MemoryMappedTensor(source, *, dtype=None, shape=None, index=None, device=None, handler=None, filename=None)

記憶體對應張量。

支援檔名或檔案處理常式。

MemoryMappedTensor 的主要優勢在於其序列化方法,可確保張量通過佇列或 RPC 遠端呼叫傳遞,而無需任何複製。

注意

在 RPC 設定中使用時,該檔案路徑應可供兩個節點存取。 如果不是,將 MemoryMappedTensor 從一個工作者傳遞到另一個工作者的行為是未定義的。

MemoryMappedTensor 支援多種建構方法。

範例

>>> # from an existing tensor
>>> tensor = torch.randn(3)
>>> with tempfile.NamedTemporaryFile() as file:
...     memmap_tensor = MemoryMappedTensor.from_tensor(tensor, filename=file.name)
...     assert memmap_tensor.filename is not None
>>> # if no filename is passed, a handler is used
>>> tensor = torch.randn(3)
>>> memmap_tensor = MemoryMappedTensor.from_tensor(tensor, filename=file.name)
>>> assert memmap_tensor.filename is None
>>> # one can create an empty tensor too
>>> with tempfile.NamedTemporaryFile() as file:
...     memmap_tensor_empty = MemoryMappedTensor.empty_like(tensor, filename=file.name)
>>> with tempfile.NamedTemporaryFile() as file:
...     memmap_tensor_zero = MemoryMappedTensor.zeros_like(tensor, filename=file.name)
>>> with tempfile.NamedTemporaryFile() as file:
...     memmap_tensor = MemoryMappedTensor.ones_like(tensor, filename=file.name)
chunk(chunks, dim=0) List of Tensors

請參閱 torch.chunk()

classmethod empty(*size, dtype=None, device=None, filename=None)
classmethod empty(shape, *, dtype=None, device=None, filename=None)

建立一個具有空內容、特定形狀、dtype 和檔名的張量。

參數:

shape (整數torch.Size) – 張量的形狀。

關鍵字引數:
  • dtype (torch.dtype) – 張量的 dtype。

  • device ( torch.device) – tensor 的裝置。只接受 None“cpu”,任何其他裝置都會引發例外。

  • filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

  • existsok ( bool, optional) – 是否可以覆寫現有檔案。預設值為 False

classmethod empty_like(input, *, filename=None)

建立一個沒有內容,但具有與輸入 tensor 相同形狀和 dtype 的 tensor。

參數:

input ( torch.Tensor) – 要用作範例的 tensor。

關鍵字引數:

filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

classmethod empty_nested(*args, **kwargs)

建立一個具有空內容、特定形狀、dtype 和檔名的張量。

參數:

shape (nested_shape) – tensors 的形狀。

關鍵字引數:
  • dtype (torch.dtype) – 張量的 dtype。

  • device ( torch.device) – tensor 的裝置。只接受 None“cpu”,任何其他裝置都會引發例外。

  • filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

  • existsok ( bool, optional) – 是否可以覆寫現有檔案。預設值為 False

property filename

tensor 的檔案名稱 (如果有的話)。

否則會引發例外。

classmethod from_filename(filename, dtype, shape, index=None)

從給定的檔案名稱載入 MemoryMappedTensor。

參數:
  • filename (路徑等效物) – 檔案的路徑。

  • dtype (torch.dtype) – 張量的 dtype。

  • shape ( torch.Sizetorch.Tensor) – tensor 的形狀。 如果提供了 tensor,則假定該 tensor 是 nested_tensor 的實例。

  • index (torch 相容的索引類型) – 用於建立 tensor 的索引。

classmethod from_handler(handler, dtype, shape, index=None)

從給定的處理器載入 MemoryMappedTensor。

參數:
  • handler (相容的檔案處理器) – tensor 的處理器。

  • dtype (torch.dtype) – 張量的 dtype。

  • shape ( torch.Sizetorch.Tensor) – tensor 的形狀。 如果提供了 tensor,則假定該 tensor 是 nested_tensor 的實例。

  • index (torch 相容的索引類型, optional) – 用於建立 tensor 的索引。

classmethod from_tensor(input, *, filename: Optional[Union[Path, str]] = None, existsok: bool = False, copy_existing: bool = False, copy_data: bool = True, shape: Optional[Size] = None)

建立一個與另一個 tensor 具有相同內容的 MemoryMappedTensor。

如果 tensor 已經是 MemoryMappedTensor,則當 filename 參數為 None 或兩個路徑匹配時,會傳回原始 tensor。 在所有其他情況下,都會產生一個新的 MemoryMappedTensor

參數:

input ( torch.Tensor) – 必須將其內容複製到 MemoryMappedTensor 上的 tensor。

關鍵字引數:
  • filename (檔案的路徑) – 應儲存 tensor 的檔案路徑。 如果未提供,則改為使用檔案處理器。

  • existsok (bool, optional) – 如果 True,檔案會覆寫現有檔案。預設值為 False

  • copy_existing (bool, optional) – 如果 True 且提供的輸入是一個具有相關檔案名稱的 MemoryMappedTensor,則允許將內容複製到新位置。否則,會拋出例外。此行為的存在是為了防止不小心在磁碟上複製資料。

  • copy_data (bool, optional) – 如果 True,張量的內容將複製到儲存空間。預設值為 True

  • shape (torch.Sizetorch.Tensor) – 用於覆寫張量形狀的形狀。如果傳遞張量,則它必須表示巢狀張量的巢狀形狀。

classmethod full(*size, fill_value, dtype=None, device=None, filename=None)
classmethod full(shape, *, fill_value, dtype=None, device=None, filename=None)

建立具有單一內容(由 fill_value 指定)、特定形狀、dtype 和檔案名稱的張量。

參數:

shape (整數torch.Size) – 張量的形狀。

關鍵字引數:
  • fill_value (floatequivalent) – 張量的內容。

  • dtype (torch.dtype) – 張量的 dtype。

  • device ( torch.device) – tensor 的裝置。只接受 None“cpu”,任何其他裝置都會引發例外。

  • filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

  • existsok ( bool, optional) – 是否可以覆寫現有檔案。預設值為 False

classmethod full_like(input, fill_value, *, filename=None)

建立具有單一內容(由 fill_value 參數指示)的張量,但具有與輸入張量相同的形狀和 dtype。

參數:
  • input ( torch.Tensor) – 要用作範例的 tensor。

  • fill_value (floatequivalent) – 張量的內容。

關鍵字引數:

filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

classmethod ones(*size, dtype=None, device=None, filename=None)
classmethod ones(shape, *, dtype=None, device=None, filename=None)

建立具有 1 填充內容、特定形狀、dtype 和檔案名稱的張量。

參數:

shape (整數torch.Size) – 張量的形狀。

關鍵字引數:
  • dtype (torch.dtype) – 張量的 dtype。

  • device ( torch.device) – tensor 的裝置。只接受 None“cpu”,任何其他裝置都會引發例外。

  • filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

  • existsok ( bool, optional) – 是否可以覆寫現有檔案。預設值為 False

classmethod ones_like(input, *, filename=None)

建立具有 1 填充內容的張量,但具有與輸入張量相同的形狀和 dtype。

參數:

input ( torch.Tensor) – 要用作範例的 tensor。

關鍵字引數:

filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

unbind(dim=0) seq

請參閱 torch.unbind()

classmethod zeros(*size, dtype=None, device=None, filename=None)
classmethod zeros(shape, *, dtype=None, device=None, filename=None)

建立具有 0 填充內容、特定形狀、dtype 和檔案名稱的張量。

參數:

shape (整數torch.Size) – 張量的形狀。

關鍵字引數:
  • dtype (torch.dtype) – 張量的 dtype。

  • device ( torch.device) – tensor 的裝置。只接受 None“cpu”,任何其他裝置都會引發例外。

  • filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

  • existsok ( bool, optional) – 是否可以覆寫現有檔案。預設值為 False

classmethod zeros_like(input, *, filename=None)

建立一個內容全為 0 的張量,但形狀和 dtype 與輸入張量相同。

參數:

input ( torch.Tensor) – 要用作範例的 tensor。

關鍵字引數:

filename (路徑等效物) – 檔案的路徑 (如果有的話)。如果未提供,則會使用處理器。

文件

存取 PyTorch 的完整開發者文件

檢視文件

教學

取得針對初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並取得您問題的解答

檢視資源