Loading...
Implement a single transformer block forward pass.
A transformer block consists of:
For simplicity:
Input:
Output: Output matrix after one transformer block, rounded to 4 decimal places.
2 2 1 0 0 1
[ 0.9998 -0.9998] [-0.9998 0.9998]
Attention(x) simplifies to x⋅xT normalized by the square root of the sequence length (2). For the input matrix X=[1001], Attention(x) becomes [1001].x and Attention(x): x=var+1e−5x+Attention(x)−mean. Since x+Attention(x)=[2002], mean=1, and var=0, x becomes [2−12−1]/0+1e−5≈[1001], but due to the specifics of the layer norm calculation, it actually results in [1001].