INNER CODE UNIT · Go
DotProductAttention
takara-ai/go-attention · attention/attention.go:145
func DotProductAttention(query Vector, keys, values Matrix) (Vector, AttentionWeights, error) {
return bestDotProductAttention(query, keys, values)
}
// BestDotProductAttention is an alias for DotProductAttention
func BestDotProductAttention(query Vector, keys, values Matrix) (Vector, AttentionWeights, error) {
return DotProductAttention(query, keys, values)
}
// bestDotProductAttention is the single, canonical implementation
func bestDotProductAttention(query Vector, keys, values Matrix) (Vector, AttentionWeights, error) {
n := len(keys)
if n == 0 {
// If keys are empty, check if values exist to determine output dimension d_v
if len(values) > 0 && len(values[0]) > 0 {
d_v := len(values[0])
// Return empty weights and a zero vector of the correct value dimension
return make(Vector, d_v), Vector{}, nil