快捷方式

MemoryMappedTensor

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) 張量列表

參見 torch.chunk()

類方法 empty(*size, dtype=None, device=None, filename=None)
類方法 empty(shape, *, dtype=None, device=None, filename=None)

建立一個內容為空的張量,具有指定的形狀、資料型別和檔名。

引數:

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

關鍵字引數:
  • dtypetorch.dtype)– 張量的資料型別。

  • devicetorch.device)– 張量的裝置。僅接受 None“cpu”,任何其他裝置將引發異常。

  • filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

  • existsok布林值可選)– 是否允許覆蓋現有檔案。預設為 False

類方法 empty_like(input, *, filename=None)

建立一個內容為空但具有與輸入張量相同形狀和資料型別的張量。

引數:

inputtorch.Tensor)– 用作示例的張量。

關鍵字引數:

filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

類方法 empty_nested(*args, **kwargs)

建立一個內容為空的張量,具有指定的形狀、資料型別和檔名。

引數:

shape巢狀形狀)– 張量的形狀。

關鍵字引數:
  • dtypetorch.dtype)– 張量的資料型別。

  • devicetorch.device)– 張量的裝置。僅接受 None“cpu”,任何其他裝置將引發異常。

  • filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

  • existsok布林值可選)– 是否允許覆蓋現有檔案。預設為 False

屬性 filename

張量的檔名(如果存在)。

否則會引發異常。

類方法 from_filename(filename, dtype, shape, index=None)

從給定檔名載入 MemoryMappedTensor。

引數:
  • filename路徑等效值)– 檔案的路徑。

  • dtypetorch.dtype)– 張量的資料型別。

  • shapetorch.Sizetorch.Tensor)– 張量的形狀。如果提供張量,則假定該張量是 nested_tensor 例項。

  • indextorch 相容的索引型別)– 用於構建張量的索引。

類方法 from_handler(handler, dtype, shape, index=None)

從給定控制代碼載入 MemoryMappedTensor。

引數:
  • handler相容的檔案控制代碼)– 張量的控制代碼。

  • dtypetorch.dtype)– 張量的資料型別。

  • shapetorch.Sizetorch.Tensor)– 張量的形狀。如果提供張量,則假定該張量是 nested_tensor 例項。

  • indextorch 相容的索引型別,可選)– 用於構建張量的索引。

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

建立一個與另一個張量內容相同的 MemoryMappedTensor。

如果輸入張量已經是 MemoryMappedTensor,並且 filename 引數為 None 或兩個路徑匹配,則返回原始張量。在所有其他情況下,會生成一個新的 MemoryMappedTensor

引數:

inputtorch.Tensor)– 其內容必須複製到 MemoryMappedTensor 的張量。

關鍵字引數:
  • filename檔案路徑)– 應儲存張量的檔案路徑。如果未提供,則改用檔案控制代碼。

  • existsok布林值可選)– 如果為 True,則檔案將覆蓋現有檔案。預設為 False

  • copy_existing布林值可選)– 如果為 True 且提供的輸入是具有關聯檔名的 MemoryMappedTensor,則允許將內容複製到新位置。否則,將引發異常。此行為旨在防止無意中在磁碟上覆制資料。

  • copy_data布林值可選)– 如果為 True,則張量的內容將複製到儲存中。預設為 True

  • shapetorch.Sizetorch.Tensor)– 一個用於覆蓋張量形狀的形狀。如果傳入張量,它必須表示巢狀張量的巢狀形狀。

類方法 full(*size, fill_value, dtype=None, device=None, filename=None)
類方法 full(shape, *, fill_value, dtype=None, device=None, filename=None)

建立一個內容為 fill_value 指定的單值張量,具有指定的形狀、資料型別和檔名。

引數:

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

關鍵字引數:
  • fill_value浮點數等效值)– 張量的內容。

  • dtypetorch.dtype)– 張量的資料型別。

  • devicetorch.device)– 張量的裝置。僅接受 None“cpu”,任何其他裝置將引發異常。

  • filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

  • existsok布林值可選)– 是否允許覆蓋現有檔案。預設為 False

類方法 full_like(input, fill_value, *, filename=None)

建立一個內容由 fill_value 引數指示,但具有與輸入張量相同形狀和資料型別的單值張量。

引數:
  • inputtorch.Tensor)– 用作示例的張量。

  • fill_value浮點數等效值)– 張量的內容。

關鍵字引數:

filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

類方法 ones(*size, dtype=None, device=None, filename=None)
類方法 ones(shape, *, dtype=None, device=None, filename=None)

建立一個內容全部為 1 的張量,具有指定的形狀、資料型別和檔名。

引數:

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

關鍵字引數:
  • dtypetorch.dtype)– 張量的資料型別。

  • devicetorch.device)– 張量的裝置。僅接受 None“cpu”,任何其他裝置將引發異常。

  • filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

  • existsok布林值可選)– 是否允許覆蓋現有檔案。預設為 False

類方法 ones_like(input, *, filename=None)

建立一個內容全部為 1,但具有與輸入張量相同形狀和資料型別的張量。

引數:

inputtorch.Tensor)– 用作示例的張量。

關鍵字引數:

filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

unbind(dim=0) seq

參見 torch.unbind()

類方法 zeros(*size, dtype=None, device=None, filename=None)
類方法 zeros(shape, *, dtype=None, device=None, filename=None)

建立一個內容全部為 0 的張量,具有指定的形狀、資料型別和檔名。

引數:

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

關鍵字引數:
  • dtypetorch.dtype)– 張量的資料型別。

  • devicetorch.device)– 張量的裝置。僅接受 None“cpu”,任何其他裝置將引發異常。

  • filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

  • existsok布林值可選)– 是否允許覆蓋現有檔案。預設為 False

類方法 zeros_like(input, *, filename=None)

建立一個內容全部為 0,但具有與輸入張量相同形狀和資料型別的張量。

引數:

inputtorch.Tensor)– 用作示例的張量。

關鍵字引數:

filename路徑等效值)– 檔案的路徑(如果存在)。如果未提供,則使用控制代碼。

文件

訪問 PyTorch 的完整開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源