What is the author explaining in the following:
To illustrate this batched matrix multiplication, suppose we
have the following tensor:
a = torch.tensor([[[[0.2745, 0.6584, 0.2775, 0.8573], #1
[0.8993, 0.0390, 0.9268, 0.7388],
[0.7179, 0.7058, 0.9156, 0.4340]],
[[0.0772, 0.3565, 0.1479, 0.5331],
[0.4066, 0.2318, 0.4545, 0.9737],
[0.4606, 0.5159, 0.4220, 0.5786]]]])
#1 The shape of this tensor is (b, num_heads, num_tokens, head_dim)
= (1, 2, 3, 4).
Now we perform a batched matrix multiplication between the
tensor itself and a view of the tensor where we transposed
the last two dimensions, num_tokens and head_dim:
print(a @ a.transpose(2, 3))
The result is
tensor([[[[1.3208, 1.1631, 1.2879],
[1.1631, 2.2150, 1.8424],
[1.2879, 1.8424, 2.0402]],
[[0.4391, 0.7003, 0.5903],
[0.7003, 1.3737, 1.0620],
[0.5903, 1.0620, 0.9912]]]])
In this case, the matrix multiplication implementation in
PyTorch handles the four-dimensional input tensor so that
the matrix multiplication is carried out between the two last
dimensions (num_tokens, head_dim) and then repeated for the
individual heads.
For instance, the preceding becomes a more compact way to
compute the matrix multiplication for each head separately:
first_head = a[0, 0, :, :]
first_res = first_head @ first_head.T
print("First head:\n", first_res)
second_head = a[0, 1, :, :]
second_res = second_head @ second_head.T
print("\nSecond head:\n", second_res)
The results are exactly the same results as those we
obtained when using the batched matrix multiplication
print(a @ a.transpose(2, 3)):
First head:
tensor([[1.3208, 1.1631, 1.2879],
[1.1631, 2.2150, 1.8424],
[1.2879, 1.8424, 2.0402]])
Second head:
tensor([[0.4391, 0.7003, 0.5903],
[0.7003, 1.3737, 1.0620],
[0.5903, 1.0620, 0.9912]])