注意
点击 此处 下载完整的示例代码
嵌套张量入门¶
嵌套张量概括了常规密集张量的形状,允许表示参差不齐大小的数据。
对于常规张量,每个维度都是规则的并且具有大小
对于嵌套张量,并非所有维度都具有规则的大小;其中一些是参差不齐的
嵌套张量是表示各种领域中顺序数据的自然解决方案
在 NLP 中,句子可以具有可变长度,因此一批句子构成一个嵌套张量
在 CV 中,图像可以具有可变形状,因此一批图像构成一个嵌套张量
在本教程中,我们将演示嵌套张量的基本用法,并通过一个真实的示例说明它们在对不同长度的顺序数据进行操作方面的有用性。特别是,它们对于构建可以有效地对参差不齐的顺序输入进行操作的 Transformer 至关重要。下面,我们展示了一个使用嵌套张量实现多头注意力机制的示例,该示例结合使用 torch.compile
,其性能优于在带有填充的张量上进行朴素操作。
嵌套张量目前是一个原型特性,可能会发生变化。
import numpy as np
import timeit
import torch
import torch.nn.functional as F
from torch import nn
torch.manual_seed(1)
np.random.seed(1)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
嵌套张量初始化¶
从 Python 前端,可以从张量列表创建嵌套张量。我们将 nt[i] 表示为嵌套张量的第 i 个张量组件。
nt = torch.nested.nested_tensor([torch.arange(12).reshape(
2, 6), torch.arange(18).reshape(3, 6)], dtype=torch.float, device=device)
print(f"{nt=}")
通过将每个底层张量填充到相同的形状,可以将嵌套张量转换为常规张量。
padded_out_tensor = torch.nested.to_padded_tensor(nt, padding=0.0)
print(f"{padded_out_tensor=}")
所有张量都具有一个属性,用于确定它们是否嵌套;
print(f"nt is nested: {nt.is_nested}")
print(f"padded_out_tensor is nested: {padded_out_tensor.is_nested}")
通常从形状不规则的张量批次构造嵌套张量。即,假设维度 0 是批次维度。索引维度 0 将返回第一个底层张量组件。
print("First underlying tensor component:", nt[0], sep='\n')
print("last column of 2nd underlying tensor component:", nt[1, :, -1], sep='\n')
# When indexing a nestedtensor's 0th dimension, the result is a regular tensor.
print(f"First underlying tensor component is nested: {nt[0].is_nested}")
需要注意的是,维度 0 的切片尚未得到支持。这意味着目前无法构建组合底层张量组件的视图。
嵌套张量操作¶
由于嵌套张量 (nested tensors) 的每个操作都必须显式实现,因此嵌套张量的操作覆盖范围目前比普通张量窄。目前,仅涵盖索引、dropout、softmax、转置、reshape、线性、bmm 等基本操作。但是,覆盖范围正在扩展。如果您需要某些操作,请提交一个问题,以帮助我们确定覆盖范围的优先级。
reshape
reshape 操作用于更改张量的形状。其针对普通张量的完整语义可以在这里找到。对于普通张量,在指定新形状时,一个维度可以是 -1,在这种情况下,它将根据剩余维度和元素数量推断得出。
嵌套张量的语义类似,除了 -1 不再进行推断。相反,它继承旧大小(此处 nt[0]
为 2,nt[1]
为 3)。-1 是为锯齿状维度指定唯一合法的尺寸。
nt_reshaped = nt.reshape(2, -1, 2, 3)
print(f"{nt_reshaped=}")
transpose
transpose 操作用于交换张量的两个维度。其完整语义可以在这里找到。请注意,对于嵌套张量,维度 0 是特殊的;它被假定为批处理维度,因此不支持涉及嵌套张量维度 0 的转置。
nt_transposed = nt_reshaped.transpose(1, 2)
print(f"{nt_transposed=}")
其他
其他操作与普通张量的语义相同。将操作应用于嵌套张量等效于将操作应用于底层张量组件,结果也是一个嵌套张量。
nt_mm = torch.nested.nested_tensor([torch.randn((2, 3, 4)), torch.randn((2, 3, 5))], device=device)
nt3 = torch.matmul(nt_transposed, nt_mm)
print(f"Result of Matmul:\n {nt3}")
nt4 = F.dropout(nt3, 0.1)
print(f"Result of Dropout:\n {nt4}")
nt5 = F.softmax(nt4, -1)
print(f"Result of Softmax:\n {nt5}")
为什么使用嵌套张量¶
当数据是顺序数据时,每个样本通常具有不同的长度。例如,在一批句子中,每个句子都有不同的单词数量。处理不同长度序列的常用技术是手动将每个数据张量填充到相同的形状以形成一个批次。例如,我们有 2 个不同长度的句子和一个词汇表,为了将其表示为单个张量,我们用 0 填充到批次中的最大长度。
sentences = [["goodbye", "padding"],
["embrace", "nested", "tensor"]]
vocabulary = {"goodbye": 1.0, "padding": 2.0,
"embrace": 3.0, "nested": 4.0, "tensor": 5.0}
padded_sentences = torch.tensor([[1.0, 2.0, 0.0],
[3.0, 4.0, 5.0]])
nested_sentences = torch.nested.nested_tensor([torch.tensor([1.0, 2.0]),
torch.tensor([3.0, 4.0, 5.0])])
print(f"{padded_sentences=}")
print(f"{nested_sentences=}")
这种将一批数据填充到其最大长度的技术并非最佳。填充数据不需要进行计算,并且通过分配比必要更大的张量来浪费内存。此外,并非所有操作在应用于填充数据时都具有相同的语义。为了忽略填充的条目,矩阵乘法需要用 0 填充,而 softmax 需要用 -inf 填充以忽略特定条目。嵌套张量的主要目标是使用标准 PyTorch 张量 UX 简化对参差不齐数据的操作,从而无需进行低效且复杂的填充和掩码。
padded_sentences_for_softmax = torch.tensor([[1.0, 2.0, float("-inf")],
[3.0, 4.0, 5.0]])
print(F.softmax(padded_sentences_for_softmax, -1))
print(F.softmax(nested_sentences, -1))
让我们来看一个实际例子:Transformers 中使用的多头注意力组件。我们可以以这样一种方式实现它,使其可以对填充张量或嵌套张量进行操作。
class MultiHeadAttention(nn.Module):
"""
Computes multi-head attention. Supports nested or padded tensors.
Args:
E_q (int): Size of embedding dim for query
E_k (int): Size of embedding dim for key
E_v (int): Size of embedding dim for value
E_total (int): Total embedding dim of combined heads post input projection. Each head
has dim E_total // nheads
nheads (int): Number of heads
dropout_p (float, optional): Dropout probability. Default: 0.0
"""
def __init__(self, E_q: int, E_k: int, E_v: int, E_total: int,
nheads: int, dropout_p: float = 0.0):
super().__init__()
self.nheads = nheads
self.dropout_p = dropout_p
self.query_proj = nn.Linear(E_q, E_total)
self.key_proj = nn.Linear(E_k, E_total)
self.value_proj = nn.Linear(E_v, E_total)
E_out = E_q
self.out_proj = nn.Linear(E_total, E_out)
assert E_total % nheads == 0, "Embedding dim is not divisible by nheads"
self.E_head = E_total // nheads
def forward(self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor) -> torch.Tensor:
"""
Forward pass; runs the following process:
1. Apply input projection
2. Split heads and prepare for SDPA
3. Run SDPA
4. Apply output projection
Args:
query (torch.Tensor): query of shape (N, L_t, E_q)
key (torch.Tensor): key of shape (N, L_s, E_k)
value (torch.Tensor): value of shape (N, L_s, E_v)
Returns:
attn_output (torch.Tensor): output of shape (N, L_t, E_q)
"""
# Step 1. Apply input projection
# TODO: demonstrate packed projection
query = self.query_proj(query)
key = self.key_proj(key)
value = self.value_proj(value)
# Step 2. Split heads and prepare for SDPA
# reshape query, key, value to separate by head
# (N, L_t, E_total) -> (N, L_t, nheads, E_head) -> (N, nheads, L_t, E_head)
query = query.unflatten(-1, [self.nheads, self.E_head]).transpose(1, 2)
# (N, L_s, E_total) -> (N, L_s, nheads, E_head) -> (N, nheads, L_s, E_head)
key = key.unflatten(-1, [self.nheads, self.E_head]).transpose(1, 2)
# (N, L_s, E_total) -> (N, L_s, nheads, E_head) -> (N, nheads, L_s, E_head)
value = value.unflatten(-1, [self.nheads, self.E_head]).transpose(1, 2)
# Step 3. Run SDPA
# (N, nheads, L_t, E_head)
attn_output = F.scaled_dot_product_attention(
query, key, value, dropout_p=dropout_p, is_causal=True)
# (N, nheads, L_t, E_head) -> (N, L_t, nheads, E_head) -> (N, L_t, E_total)
attn_output = attn_output.transpose(1, 2).flatten(-2)
# Step 4. Apply output projection
# (N, L_t, E_total) -> (N, L_t, E_out)
attn_output = self.out_proj(attn_output)
return attn_output
根据Transformer 论文设置超参数
N = 512
E_q, E_k, E_v, E_total = 512, 512, 512, 512
E_out = E_q
nheads = 8
除了 dropout 概率:设置为 0 以进行正确性检查
dropout_p = 0.0
让我们根据 Zipf 定律生成一些逼真的虚假数据。
def zipf_sentence_lengths(alpha: float, batch_size: int) -> torch.Tensor:
# generate fake corpus by unigram Zipf distribution
# from wikitext-2 corpus, we get rank "." = 3, "!" = 386, "?" = 858
sentence_lengths = np.empty(batch_size, dtype=int)
for ibatch in range(batch_size):
sentence_lengths[ibatch] = 1
word = np.random.zipf(alpha)
while word != 3 and word != 386 and word != 858:
sentence_lengths[ibatch] += 1
word = np.random.zipf(alpha)
return torch.tensor(sentence_lengths)
创建嵌套张量批次输入
def gen_batch(N, E_q, E_k, E_v, device):
# generate semi-realistic data using Zipf distribution for sentence lengths
sentence_lengths = zipf_sentence_lengths(alpha=1.2, batch_size=N)
# Note: the torch.jagged layout is a nested tensor layout that supports a single ragged
# dimension and works with torch.compile. The batch items each have shape (B, S*, D)
# where B = batch size, S* = ragged sequence length, and D = embedding dimension.
query = torch.nested.nested_tensor([
torch.randn(l.item(), E_q, device=device)
for l in sentence_lengths
], layout=torch.jagged)
key = torch.nested.nested_tensor([
torch.randn(s.item(), E_k, device=device)
for s in sentence_lengths
], layout=torch.jagged)
value = torch.nested.nested_tensor([
torch.randn(s.item(), E_v, device=device)
for s in sentence_lengths
], layout=torch.jagged)
return query, key, value, sentence_lengths
query, key, value, sentence_lengths = gen_batch(N, E_q, E_k, E_v, device)
生成查询、键、值的填充形式以进行比较
def jagged_to_padded(jt, padding_val):
# TODO: do jagged -> padded directly when this is supported
return torch.nested.to_padded_tensor(
torch.nested.nested_tensor(list(jt.unbind())),
padding_val)
padded_query, padded_key, padded_value = (
jagged_to_padded(t, 0.0) for t in (query, key, value)
)
构建模型
mha = MultiHeadAttention(E_q, E_k, E_v, E_total, nheads, dropout_p).to(device=device)
检查正确性和性能
def benchmark(func, *args, **kwargs):
torch.cuda.synchronize()
begin = timeit.default_timer()
output = func(*args, **kwargs)
torch.cuda.synchronize()
end = timeit.default_timer()
return output, (end - begin)
output_nested, time_nested = benchmark(mha, query, key, value)
output_padded, time_padded = benchmark(mha, padded_query, padded_key, padded_value)
# padding-specific step: remove output projection bias from padded entries for fair comparison
for i, entry_length in enumerate(sentence_lengths):
output_padded[i, entry_length:] = 0.0
print("=== without torch.compile ===")
print("nested and padded calculations differ by", (jagged_to_padded(output_nested, 0.0) - output_padded).abs().max().item())
print("nested tensor multi-head attention takes", time_nested, "seconds")
print("padded tensor multi-head attention takes", time_padded, "seconds")
# warm up compile first...
compiled_mha = torch.compile(mha)
compiled_mha(query, key, value)
# ...now benchmark
compiled_output_nested, compiled_time_nested = benchmark(
compiled_mha, query, key, value)
# warm up compile first...
compiled_mha(padded_query, padded_key, padded_value)
# ...now benchmark
compiled_output_padded, compiled_time_padded = benchmark(
compiled_mha, padded_query, padded_key, padded_value)
# padding-specific step: remove output projection bias from padded entries for fair comparison
for i, entry_length in enumerate(sentence_lengths):
compiled_output_padded[i, entry_length:] = 0.0
print("=== with torch.compile ===")
print("nested and padded calculations differ by", (jagged_to_padded(compiled_output_nested, 0.0) - compiled_output_padded).abs().max().item())
print("nested tensor multi-head attention takes", compiled_time_nested, "seconds")
print("padded tensor multi-head attention takes", compiled_time_padded, "seconds")
请注意,如果没有 torch.compile
,python 子类嵌套张量的开销会使其比填充张量上的等效计算慢。但是,一旦启用 torch.compile
,对嵌套张量进行操作会带来数倍的速度提升。避免在填充上浪费计算只会随着批次中填充百分比的增加而变得更有价值。
print(f"Nested speedup: {compiled_time_padded / compiled_time_nested:.3f}")
结论¶
在本教程中,我们学习了如何使用嵌套张量执行基本操作,以及如何以避免填充计算的方式为 transformers 实现多头注意力。有关更多信息,请查看 torch.nested 命名空间的文档。
脚本的总运行时间:(0 分钟 0.000 秒)