快捷方式

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 的完整开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获取问题解答

查看资源