命名张量操作符覆盖范围#
创建于:2019 年 10 月 08 日 | 最后更新于:2025 年 06 月 08 日
请先阅读命名张量以了解命名张量的介绍。
本文档是关于*名称推断*的参考,该过程定义了命名张量如何:
使用名称提供额外的自动运行时正确性检查
将名称从输入张量传播到输出张量
以下是支持命名张量的所有操作及其相关的名称推断规则列表。
如果您在此处未看到某个操作,但它对您的用例有帮助,请搜索是否已提交问题,如果没有,请提交一个。
警告
命名张量 API 仍处于实验阶段,可能会有所更改。
API |
名称推断规则 |
---|---|
参见文档 |
|
参见文档 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
无 |
|
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
参见文档 |
|
无 |
|
无 |
|
|
无 |
无 |
|
|
参见文档 |
|
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
无 |
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
|
无 |
|
将掩码与输入对齐,然后统一输入张量的名称 |
|
参见文档 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
|
无 |
无 |
|
无 |
|
参见文档 |
|
无 |
|
无 |
|
参见文档 |
|
参见文档 |
|
无 |
|
无 |
|
仅允许不改变形状的调整大小 |
|
仅允许不改变形状的调整大小 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
无 |
|
参见文档 |
|
无 |
|
无 |
|
保留输入名称#
所有逐点一元函数以及其他一些一元函数都遵循此规则。
检查名称:无
传播名称:输入张量的名称传播到输出。
>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.abs().names
('N', 'C')
移除维度#
所有约简操作,如 sum()
,通过对所需维度进行约简来移除维度。其他操作,如 select()
和 squeeze()
,也会移除维度。
凡是可以在操作符中传递整数维度索引的地方,也可以传递维度名称。接受维度索引列表的函数也可以接受维度名称列表。
检查名称:如果
dim
或dims
作为名称列表传入,请检查这些名称是否存在于self
中。传播名称:如果输入张量中由
dim
或dims
指定的维度不存在于输出张量中,则这些维度的相应名称不会出现在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
中,对于(-min(tensor.dim(), other.dim()) + 1, -1]
中的所有i
,match(tensor.names[i], other.names[i])
必须为True
。检查名称:此外,所有命名维度必须从右侧对齐。在匹配过程中,如果我们将命名维度
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() >= 2
和 other.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')
最后,有许多矩阵乘法函数的融合 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')