评价此页

命名张量算子覆盖范围#

创建日期:2019年10月08日 | 最后更新日期:2025年06月08日

请先阅读 命名张量 以了解命名张量的介绍。

本文档是关于名称推断的参考,名称推断是一个定义命名张量如何

  1. 使用名称来提供额外的自动运行时正确性检查

  2. 将名称从输入张量传播到输出张量

以下是支持命名张量的所有操作及其相关的名称推断规则的列表。

如果您在这里未找到对您的用例有帮助的操作,请 搜索是否已提交相关问题,如果没有,请 提交一个问题

警告

命名张量 API 是实验性的,可能会发生变化。

支持的操作#

API

名称推断规则

Tensor.abs(), torch.abs()

保留输入名称

Tensor.abs_()

保留输入名称

Tensor.acos(), torch.acos()

保留输入名称

Tensor.acos_()

保留输入名称

Tensor.add(), torch.add()

统一输入名称

Tensor.add_()

统一输入名称

Tensor.addmm(), torch.addmm()

收缩维度

Tensor.addmm_()

收缩维度

Tensor.addmv(), torch.addmv()

收缩维度

Tensor.addmv_()

收缩维度

Tensor.align_as()

参见文档

Tensor.align_to()

参见文档

Tensor.all(), torch.all()

Tensor.any(), torch.any()

Tensor.asin(), torch.asin()

保留输入名称

Tensor.asin_()

保留输入名称

Tensor.atan(), torch.atan()

保留输入名称

Tensor.atan2(), torch.atan2()

统一输入名称

Tensor.atan2_()

统一输入名称

Tensor.atan_()

保留输入名称

Tensor.bernoulli(), torch.bernoulli()

保留输入名称

Tensor.bernoulli_()

Tensor.bfloat16()

保留输入名称

Tensor.bitwise_not(), torch.bitwise_not()

保留输入名称

Tensor.bitwise_not_()

Tensor.bmm(), torch.bmm()

收缩维度

Tensor.bool()

保留输入名称

Tensor.byte()

保留输入名称

torch.cat()

统一输入名称

Tensor.cauchy_()

Tensor.ceil(), torch.ceil()

保留输入名称

Tensor.ceil_()

Tensor.char()

保留输入名称

Tensor.chunk(), torch.chunk()

保留输入名称

Tensor.clamp(), torch.clamp()

保留输入名称

Tensor.clamp_()

Tensor.copy_()

输出函数和原地变体

Tensor.cos(), torch.cos()

保留输入名称

Tensor.cos_()

Tensor.cosh(), torch.cosh()

保留输入名称

Tensor.cosh_()

Tensor.acosh(), torch.acosh()

保留输入名称

Tensor.acosh_()

Tensor.cpu()

保留输入名称

Tensor.cuda()

保留输入名称

Tensor.cumprod(), torch.cumprod()

保留输入名称

Tensor.cumsum(), torch.cumsum()

保留输入名称

Tensor.data_ptr()

Tensor.deg2rad(), torch.deg2rad()

保留输入名称

Tensor.deg2rad_()

Tensor.detach(), torch.detach()

保留输入名称

Tensor.detach_()

Tensor.device, torch.device()

Tensor.digamma(), torch.digamma()

保留输入名称

Tensor.digamma_()

Tensor.dim()

Tensor.div(), torch.div()

统一输入名称

Tensor.div_()

统一输入名称

Tensor.dot(), torch.dot()

Tensor.double()

保留输入名称

Tensor.element_size()

torch.empty()

工厂函数

torch.empty_like()

工厂函数

Tensor.eq(), torch.eq()

统一输入名称

Tensor.erf(), torch.erf()

保留输入名称

Tensor.erf_()

Tensor.erfc(), torch.erfc()

保留输入名称

Tensor.erfc_()

Tensor.erfinv(), torch.erfinv()

保留输入名称

Tensor.erfinv_()

Tensor.exp(), torch.exp()

保留输入名称

Tensor.exp_()

Tensor.expand()

保留输入名称

Tensor.expm1(), torch.expm1()

保留输入名称

Tensor.expm1_()

Tensor.exponential_()

Tensor.fill_()

Tensor.flatten(), torch.flatten()

参见文档

Tensor.float()

保留输入名称

Tensor.floor(), torch.floor()

保留输入名称

Tensor.floor_()

Tensor.frac(), torch.frac()

保留输入名称

Tensor.frac_()

Tensor.ge(), torch.ge()

统一输入名称

Tensor.get_device(), torch.get_device()

Tensor.grad

Tensor.gt(), torch.gt()

统一输入名称

Tensor.half()

保留输入名称

Tensor.has_names()

参见文档

Tensor.index_fill(), torch.index_fill()

保留输入名称

Tensor.index_fill_()

Tensor.int()

保留输入名称

Tensor.is_contiguous()

Tensor.is_cuda

Tensor.is_floating_point(), torch.is_floating_point()

Tensor.is_leaf

Tensor.is_pinned()

Tensor.is_shared()

Tensor.is_signed(), torch.is_signed()

Tensor.is_sparse

如果张量使用稀疏 CSR 存储布局,则为 True,否则为 False

torch.is_tensor()

Tensor.item()

Tensor.itemsize

Tensor.kthvalue(), torch.kthvalue()

移除维度

Tensor.le(), torch.le()

统一输入名称

Tensor.log(), torch.log()

保留输入名称

Tensor.log10(), torch.log10()

保留输入名称

Tensor.log10_()

Tensor.log1p(), torch.log1p()

保留输入名称

Tensor.log1p_()

Tensor.log2(), torch.log2()

保留输入名称

Tensor.log2_()

Tensor.log_()

Tensor.log_normal_()

Tensor.logical_not(), torch.logical_not()

保留输入名称

Tensor.logical_not_()

Tensor.logsumexp(), torch.logsumexp()

移除维度

Tensor.long()

保留输入名称

Tensor.lt(), torch.lt()

统一输入名称

torch.manual_seed()

Tensor.masked_fill(), torch.masked_fill()

保留输入名称

Tensor.masked_fill_()

Tensor.masked_select(), torch.masked_select()

将掩码对齐到输入,然后统一输入张量的名称

Tensor.matmul(), torch.matmul()

收缩维度

Tensor.mean(), torch.mean()

移除维度

Tensor.median(), torch.median()

移除维度

Tensor.nanmedian(), torch.nanmedian()

移除维度

Tensor.mm(), torch.mm()

收缩维度

Tensor.mode(), torch.mode()

移除维度

Tensor.mul(), torch.mul()

统一输入名称

Tensor.mul_()

统一输入名称

Tensor.mv(), torch.mv()

收缩维度

Tensor.names

参见文档

Tensor.narrow(), torch.narrow()

保留输入名称

Tensor.nbytes

Tensor.ndim

Tensor.ndimension()

Tensor.ne(), torch.ne()

统一输入名称

Tensor.neg(), torch.neg()

保留输入名称

Tensor.neg_()

torch.normal()

保留输入名称

Tensor.normal_()

Tensor.numel(), torch.numel()

torch.ones()

工厂函数

Tensor.pow(), torch.pow()

统一输入名称

Tensor.pow_()

Tensor.prod(), torch.prod()

移除维度

Tensor.rad2deg(), torch.rad2deg()

保留输入名称

Tensor.rad2deg_()

torch.rand()

工厂函数

torch.rand()

工厂函数

torch.randn()

工厂函数

torch.randn()

工厂函数

Tensor.random_()

Tensor.reciprocal(), torch.reciprocal()

保留输入名称

Tensor.reciprocal_()

Tensor.refine_names()

参见文档

Tensor.register_hook()

Tensor.register_post_accumulate_grad_hook()

Tensor.rename()

参见文档

Tensor.rename_()

参见文档

Tensor.requires_grad

Tensor.requires_grad_()

Tensor.resize_()

仅允许不改变形状的重置

Tensor.resize_as_()

仅允许不改变形状的重置

Tensor.round(), torch.round()

保留输入名称

Tensor.round_()

Tensor.rsqrt(), torch.rsqrt()

保留输入名称

Tensor.rsqrt_()

Tensor.select(), torch.select()

移除维度

Tensor.short()

保留输入名称

Tensor.sigmoid(), torch.sigmoid()

保留输入名称

Tensor.sigmoid_()

Tensor.sign(), torch.sign()

保留输入名称

Tensor.sign_()

Tensor.sgn(), torch.sgn()

保留输入名称

Tensor.sgn_()

Tensor.sin(), torch.sin()

保留输入名称

Tensor.sin_()

Tensor.sinh(), torch.sinh()

保留输入名称

Tensor.sinh_()

Tensor.asinh(), torch.asinh()

保留输入名称

Tensor.asinh_()

Tensor.size()

Tensor.softmax(), torch.softmax()

保留输入名称

Tensor.split(), torch.split()

保留输入名称

Tensor.sqrt(), torch.sqrt()

保留输入名称

Tensor.sqrt_()

Tensor.squeeze(), torch.squeeze()

移除维度

Tensor.std(), torch.std()

移除维度

torch.std_mean()

移除维度

Tensor.stride()

Tensor.sub(), torch.sub()

统一输入名称

Tensor.sub_()

统一输入名称

Tensor.sum(), torch.sum()

移除维度

Tensor.tan(), torch.tan()

保留输入名称

Tensor.tan_()

Tensor.tanh(), torch.tanh()

保留输入名称

Tensor.tanh_()

Tensor.atanh(), torch.atanh()

保留输入名称

Tensor.atanh_()

torch.tensor()

工厂函数

Tensor.to()

保留输入名称

Tensor.topk(), torch.topk()

移除维度

Tensor.transpose(), torch.transpose()

置换维度

Tensor.trunc(), torch.trunc()

保留输入名称

Tensor.trunc_()

Tensor.type()

Tensor.type_as()

保留输入名称

Tensor.unbind(), torch.unbind()

移除维度

Tensor.unflatten()

参见文档

Tensor.uniform_()

Tensor.var(), torch.var()

移除维度

torch.var_mean()

移除维度

Tensor.zero_()

torch.zeros()

工厂函数

保留输入名称#

所有逐点一元函数都遵循此规则,以及一些其他一元函数。

  • 检查名称:无

  • 传播名称:输入张量的名称将传播到输出。

>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.abs().names
('N', 'C')

移除维度#

所有归约操作,如sum(),通过归约期望的维度来移除维度。其他操作如select()squeeze()也会移除维度。

在可以将整数维度索引传递给操作符的地方,也可以传递维度名称。接受维度索引列表的函数也可以接受维度名称列表。

  • 检查名称:如果dimdims作为名称列表传入,则检查这些名称是否存在于self中。

  • 传播名称:如果输入张量中由dimdims指定的维度不存在于输出张量中,则这些维度的相应名称不会出现在output.names中。

>>> x = torch.randn(1, 3, 3, 3, names=('N', 'C', 'H', 'W'))
>>> x.squeeze('N').names
('C', 'H', 'W')

>>> x = torch.randn(3, 3, 3, 3, names=('N', 'C', 'H', 'W'))
>>> x.sum(['N', 'C']).names
('H', 'W')

# Reduction ops with keepdim=True don't actually remove dimensions.
>>> x = torch.randn(3, 3, 3, 3, names=('N', 'C', 'H', 'W'))
>>> x.sum(['N', 'C'], keepdim=True).names
('N', 'C', 'H', 'W')

统一来自输入的名称#

所有二元算术运算都遵循此规则。执行广播的操作仍然从右侧按位置进行广播,以保持与无名称张量的兼容性。要通过名称显式广播,请使用Tensor.align_as()

  • 检查名称:所有名称必须从右侧按位置匹配。即,在tensor + other中,对于i(-min(tensor.dim(), other.dim()) + 1, -1]中,match(tensor.names[i], other.names[i])必须为真。

  • 检查名称:此外,所有命名维度必须从右侧对齐。在匹配过程中,如果我们匹配一个命名维度A和一个无名维度None,则A不得出现在具有无名维度的张量中。

  • 传播名称:从两个张量中从右侧统一名称对,以生成输出名称。

例如,

# tensor: Tensor[   N, None]
# other:  Tensor[None,    C]
>>> tensor = torch.randn(3, 3, names=('N', None))
>>> other = torch.randn(3, 3, names=(None, 'C'))
>>> (tensor + other).names
('N', 'C')

检查名称

  • match(tensor.names[-1], other.names[-1])True

  • match(tensor.names[-2], tensor.names[-2])True

  • 因为我们在tensor中匹配了None'C',所以检查以确保'C'不存在于tensor中(它不存在)。

  • 检查以确保'N'不存在于other中(它不存在)。

最后,输出名称使用[unify('N', None), unify(None, 'C')] = ['N', 'C']计算得到。

更多示例

# Dimensions don't match from the right:
# tensor: Tensor[N, C]
# other:  Tensor[   N]
>>> tensor = torch.randn(3, 3, names=('N', 'C'))
>>> other = torch.randn(3, names=('N',))
>>> (tensor + other).names
RuntimeError: Error when attempting to broadcast dims ['N', 'C'] and dims
['N']: dim 'C' and dim 'N' are at the same position from the right but do
not match.

# Dimensions aren't aligned when matching tensor.names[-1] and other.names[-1]:
# tensor: Tensor[N, None]
# other:  Tensor[      N]
>>> tensor = torch.randn(3, 3, names=('N', None))
>>> other = torch.randn(3, names=('N',))
>>> (tensor + other).names
RuntimeError: Misaligned dims when attempting to broadcast dims ['N'] and
dims ['N', None]: dim 'N' appears in a different position from the right
across both lists.

注意

在最后两个示例中,都可以通过名称对齐张量然后执行加法。使用Tensor.align_as()按名称对齐张量,或使用Tensor.align_to()将张量对齐到自定义维度顺序。

置换维度#

一些操作,如Tensor.t(),会置换维度的顺序。维度名称附加到单个维度上,因此它们也会被置换。

如果操作符接受位置索引dim,它也可以接受维度名称作为dim

  • 检查名称:如果dim作为名称传入,则检查它是否存在于张量中。

  • 传播名称:以与被置换维度相同的方式置换维度名称。

>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.transpose('N', 'C').names
('C', 'N')

收缩维度#

矩阵乘法函数遵循此规则的某种变体。我们先来看torch.mm(),然后再推广批次矩阵乘法的规则。

对于torch.mm(tensor, other)

  • 检查名称:无

  • 传播名称:结果名称为(tensor.names[-2], other.names[-1])

>>> x = torch.randn(3, 3, names=('N', 'D'))
>>> y = torch.randn(3, 3, names=('in', 'out'))
>>> x.mm(y).names
('N', 'out')

本质上,矩阵乘法会对两个维度执行点积,将其折叠。当两个张量进行矩阵乘法时,收缩的维度会消失,不会出现在输出张量中。

torch.mv(), torch.dot()的工作方式类似:名称推断不检查输入名称,并移除参与点积的维度。

>>> x = torch.randn(3, 3, names=('N', 'D'))
>>> y = torch.randn(3, names=('something',))
>>> x.mv(y).names
('N',)

现在,让我们看看torch.matmul(tensor, other)。假设tensor.dim() >= 2other.dim() >= 2

  • 检查名称:检查输入的批次维度是否已对齐且可广播。有关输入对齐的含义,请参阅统一来自输入的名称

  • 传播名称:结果名称通过统一批次维度并移除收缩的维度来获得:unify(tensor.names[:-2], other.names[:-2]) + (tensor.names[-2], other.names[-1])

示例

# Batch matrix multiply of matrices Tensor['C', 'D'] and Tensor['E', 'F'].
# 'A', 'B' are batch dimensions.
>>> x = torch.randn(3, 3, 3, 3, names=('A', 'B', 'C', 'D'))
>>> y = torch.randn(3, 3, 3, names=('B', 'E', 'F'))
>>> torch.matmul(x, y).names
('A', 'B', 'C', 'F')

最后,许多matmul函数都有融合的add版本。例如,addmm()addmv()。这些被视为组合了mm()的名称推断和add()的名称推断。

工厂函数#

工厂函数现在接受一个新的names参数,该参数将名称与每个维度关联起来。

>>> torch.zeros(2, 3, names=('N', 'C'))
tensor([[0., 0., 0.],
        [0., 0., 0.]], names=('N', 'C'))

out函数和原地变体#

指定为out=的张量具有以下行为:

  • 如果它没有命名维度,那么从操作计算出的名称将被传播到它。

  • 如果它有任何命名维度,那么从操作计算出的名称必须与现有名称完全相等。否则,操作将报错。

所有原地方法都会修改输入,使其名称等于从名称推断计算出的名称。例如:

>>> x = torch.randn(3, 3)
>>> y = torch.randn(3, 3, names=('N', 'C'))
>>> x.names
(None, None)

>>> x += y
>>> x.names
('N', 'C')