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¶
- classmethod empty(*size, dtype=None, device=None, filename=None)¶
- classmethod empty(shape, *, dtype=None, device=None, filename=None)
建立一個具有空內容、特定形狀、dtype 和檔名的張量。
- 引數:
shape (integers or torch.Size) – 張量的形狀。
- 關鍵字引數:
dtype (torch.dtype) – 張量的 dtype。
device (torch.device) – 張量的裝置。只接受 None 和 “cpu”,任何其他裝置都會引發異常。
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
existsok (bool, optional) – 是否可以覆蓋現有檔案。預設為
False。
- classmethod empty_like(input, *, filename=None)¶
建立一個沒有內容但具有與輸入張量相同形狀和 dtype 的張量。
- 引數:
input (torch.Tensor) – 用作示例的張量。
- 關鍵字引數:
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
- classmethod empty_nested(*args, **kwargs)¶
建立一個具有空內容、特定形狀、dtype 和檔名的張量。
- 引數:
shape (nested_shape) – 張量的形狀。
- 關鍵字引數:
dtype (torch.dtype) – 張量的 dtype。
device (torch.device) – 張量的裝置。只接受 None 和 “cpu”,任何其他裝置都會引發異常。
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
existsok (bool, optional) – 是否可以覆蓋現有檔案。預設為
False。
- property filename¶
張量的檔名(如果存在)。
否則引發異常。
- classmethod from_filename(filename, dtype, shape, index=None)¶
從給定檔名載入 MemoryMappedTensor。
- 引數:
filename (path or equivalent) – 檔案的路徑。
dtype (torch.dtype) – 張量的 dtype。
shape (torch.Size or torch.Tensor) – 張量的形狀。如果提供張量,則假定該張量是 nested_tensor 例項。
index (torch-compatible index type) – 用於構建張量的索引。
- classmethod from_handler(handler, dtype, shape, index=None)¶
從給定處理程式載入 MemoryMappedTensor。
- 引數:
handler (compatible file handler) – 張量的處理程式。
dtype (torch.dtype) – 張量的 dtype。
shape (torch.Size or torch.Tensor) – 張量的形狀。如果提供張量,則假定該張量是 nested_tensor 例項。
index (torch-compatible index type, optional) – 用於構建張量的索引。
- 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)¶
建立一個內容與另一個張量相同的 MemoryMappedTensor。
如果張量已經是 MemoryMappedTensor,並且 filename 引數為 None 或兩個路徑匹配,則返回原始張量。在所有其他情況下,將生成一個新的
MemoryMappedTensor。- 引數:
input (torch.Tensor) – 內容必須複製到 MemoryMappedTensor 的張量。
- 關鍵字引數:
filename (path to a file) – 張量應儲存的檔案路徑。如果未提供,則改用檔案控制代碼。
existsok (bool, optional) – 如果為
True,則檔案將覆蓋現有檔案。預設為False。copy_existing (bool, optional) – 如果為
True且提供的輸入是具有關聯檔名的 MemoryMappedTensor,則允許將內容複製到新位置。否則,將丟擲異常。此行為是為了防止無意中在磁碟上覆制資料。copy_data (bool, optional) – 如果為
True,則將複製張量的內容到儲存。預設為True。shape (torch.Size or torch.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 (integers or torch.Size) – 張量的形狀。
- 關鍵字引數:
fill_value (float or equivalent) – 張量的內容。
dtype (torch.dtype) – 張量的 dtype。
device (torch.device) – 張量的裝置。只接受 None 和 “cpu”,任何其他裝置都會引發異常。
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
existsok (bool, optional) – 是否可以覆蓋現有檔案。預設為
False。
- classmethod full_like(input, fill_value, *, filename=None)¶
建立一個具有由 fill_value 引數指示的單個內容,但具有與輸入張量相同的形狀和 dtype 的張量。
- 引數:
input (torch.Tensor) – 用作示例的張量。
fill_value (float or equivalent) – 張量的內容。
- 關鍵字引數:
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
- classmethod ones(*size, dtype=None, device=None, filename=None)¶
- classmethod ones(shape, *, dtype=None, device=None, filename=None)
建立一個具有 1 填充內容、特定形狀、dtype 和檔名的張量。
- 引數:
shape (integers or torch.Size) – 張量的形狀。
- 關鍵字引數:
dtype (torch.dtype) – 張量的 dtype。
device (torch.device) – 張量的裝置。只接受 None 和 “cpu”,任何其他裝置都會引發異常。
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
existsok (bool, optional) – 是否可以覆蓋現有檔案。預設為
False。
- classmethod ones_like(input, *, filename=None)¶
建立一個 1 填充內容的張量,但具有與輸入張量相同的形狀和 dtype。
- 引數:
input (torch.Tensor) – 用作示例的張量。
- 關鍵字引數:
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
- unbind(dim=0) seq¶
- classmethod zeros(*size, dtype=None, device=None, filename=None)¶
- classmethod zeros(shape, *, dtype=None, device=None, filename=None)
建立一個具有 0 填充內容、特定形狀、dtype 和檔名的張量。
- 引數:
shape (integers or torch.Size) – 張量的形狀。
- 關鍵字引數:
dtype (torch.dtype) – 張量的 dtype。
device (torch.device) – 張量的裝置。只接受 None 和 “cpu”,任何其他裝置都會引發異常。
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。
existsok (bool, optional) – 是否可以覆蓋現有檔案。預設為
False。
- classmethod zeros_like(input, *, filename=None)¶
建立一個 0 填充內容的張量,但具有與輸入張量相同的形狀和 dtype。
- 引數:
input (torch.Tensor) – 用作示例的張量。
- 關鍵字引數:
filename (path or equivalent) – 檔案的路徑,如果有的話。如果未提供,則使用處理程式。