INNER CODE UNIT · Go
d_v
takara-ai/go-attention · attention/attention.go:175
d_v := len(values[0]) // Dimension of value vectors
// Determine key dimension (d_k) safely
d_k := len(query)
if n > 0 && len(keys[0]) != d_k {
// Check the first key's dimension against the query dimension
return nil, nil, fmt.Errorf("query dimension (%d) must match key dimension (%d)", d_k, len(keys[0]))
}
// Compute attention scores
scores := make(Vector, n)
// Pre-calculate scaling factor only if d_k > 0 to avoid division by zero or sqrt of zero
scale := 1.0
if d_k > 0 {
scale = 1.0 / math.Sqrt(float64(d_k))
} // If d_k is 0, scale remains 1.0, dot product will likely be 0 unless vectors are empty.