| 44 | |
| 45 | |
| 46 | def positional_encoding(position, d_model_size, dtype): |
| 47 | # create the sinusoidal pattern for the positional encoding |
| 48 | angle_rads = angle_defn( |
| 49 | torch.arange(position, dtype=dtype).unsqueeze(1), |
| 50 | torch.arange(d_model_size, dtype=dtype).unsqueeze(0), |
| 51 | d_model_size, |
| 52 | ) |
| 53 | |
| 54 | sines = torch.sin(angle_rads[:, 0::2]) |
| 55 | cosines = torch.cos(angle_rads[:, 1::2]) |
| 56 | |
| 57 | pos_encoding = torch.cat([sines, cosines], dim=-1) |
| 58 | return pos_encoding |
| 59 | |
| 60 | |
| 61 | def scaled_dot_product_attention(q, k, v, mask, attention_mask=None, head_mask=None): |