Skip to content

A straightforward method for training your LLM, from downloading data to generating text.

Update attention.py

TianyiQ9acf1f3a8b18
diff --git a/src/models/attention.py b/src/models/attention.py
index c0f9c8f..c57ccd7 100644
--- a/src/models/attention.py
+++ b/src/models/attention.py
@@ -43,9 +43,10 @@ class Head(nn.Module):
torch.Tensor: Output tensor after applying attention.
"""
B, T, C = x.shape
+ head_size = self.key.out_features
k = self.key(x) # (B, T, head_size)
q = self.query(x) # (B, T, head_size)
- scale_factor = 1 / math.sqrt(C)
+ scale_factor = 1 / math.sqrt(head_size)
# Calculate attention weights: (B, T, head_size) @ (B, head_size, T) -> (B, T, T)
attn_weights = q @ k.transpose(-2, -1) * scale_factor
# Apply causal masking
@@ -107,4 +108,4 @@ if __name__ == '__main__':
output_tensor = multihead_attn(input_tensor)
print("MultiHeadAttention Input Shape:", input_tensor.shape)
- print("MultiHeadAttention Output Shape:", output_tensor.shape)
\ No newline at end of file
+ print("MultiHeadAttention Output Shape:", output_tensor.shape)