命名张量算子覆盖率¶
请先阅读命名张量,了解命名张量的介绍。
本文档是名称推理的参考,该过程定义了如何 命名张量:
使用名称提供额外的自动运行时正确性检查
将名称从输入张量传播到输出张量
以下是命名张量支持的所有操作的列表 及其关联的名称推理规则。
如果您在此处没有看到操作,但它对您的使用案例有所帮助,请搜索是否已提交问题,如果没有,请提交一个问题。
警告
命名的张量 API 是实验性的,可能会发生变化。
应用程序接口 |
名称推理规则 |
---|---|
查看文档 |
|
查看文档 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
|
没有 |
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
查看文档 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
|
查看文档 |
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
将蒙版与输入对齐,然后unifies_names_from_input_tensors |
|
查看文档 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
|
没有 |
没有 |
|
没有 |
|
查看文档 |
|
没有 |
|
没有 |
|
查看文档 |
|
查看文档 |
|
没有 |
|
没有 |
|
仅允许不改变形状的调整大小 |
|
仅允许不改变形状的调整大小 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
没有 |
|
查看文档 |
|
没有 |
|
没有 |
|
保留输入名称¶
所有逐点一元函数以及其他一些一元函数都遵循此规则。
检查名称:无
Propagate names:输入张量的名称将传播到输出。
>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.abs().names
('N', 'C')
删除维度¶
所有缩减操作(如通过缩减来删除维度)
超过所需的尺寸。其他操作,例如
和
remove 维度。
只要可以将整数维度索引传递给运算符,也可以将 维度名称。采用维度索引列表的函数也可以采用 维度名称列表。
检查名称:如果 或 作为名称列表传入, 检查这些名称是否存在于 中。
dim
dims
self
Propagate names:如果输入张量的维度由输出张量指定或不存在,则相应的名称 的 中未显示在 中。
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')
统一输入中的名称¶
所有二进制算术运算都遵循此规则。仍然广播的操作
从右侧进行位置广播,以保持与 unnamed 的兼容性
张。要按名称执行显式广播,请使用 .
检查名称:所有名称必须从右侧开始位置匹配。即,对于 IN 中的所有内容,都必须为 true。
tensor + other
match(tensor.names[i], other.names[i])
i
(-min(tensor.dim(), other.dim()) + 1, -1]
检查名称:此外,所有命名尺寸必须从右侧对齐。 在匹配过程中,如果我们将命名维度与 unnamed dimension 匹配,则不得出现在具有 unnamed 维度的张量中。
A
None
A
Propagate names:将两个张量从右侧到的名称对统一到 生成输出名称。
例如
# 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
检查以确保 中不存在 (it does not)。
'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.
排列维度¶
某些操作(如 )会改变维度的顺序。维度名称
附加到各个维度,因此它们也会被置换。
如果运算符采用 positional index ,它也能够采用维度
name 设置为 .dim
dim
检查名称:如果作为名称传递,请检查它是否存在于张量中。
dim
传播名称:以与维度相同的方式排列维度名称 正在排列。
>>> x = torch.randn(3, 3, names=('N', 'C'))
>>> x.transpose('N', 'C').names
('C', 'N')
Contracts away 暗淡¶
矩阵乘法函数遵循 this 的一些变体。让我们先看一遍,然后推广批量矩阵乘法的规则。
为: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')
从本质上讲,矩阵乘法在两个维度上执行点积, 折叠它们。当两个张量进行矩阵相乘时,收缩维度 disappear 的 intent 值,并且不会显示在 output tensor 中。
,
以类似的方式工作:名称推理不会
检查 Input names 并删除 .product 中涉及的维度:
>>> 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
检查名称:检查输入的批量维度是否对齐且可广播。 请参阅 统一输入中的名称 ,了解输入对齐的含义。
Propagate names:通过统一批处理维度并删除 合同规定的尺寸: 。
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')
工厂功能¶
Factory 函数现在采用一个关联名称的新参数
与每个维度。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')