INNER CODE UNIT · Go

BestDotProductAttention

takara-ai/go-attention · attention/attention.go:150

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 
        }
		// If both keys and values are empty (or values have zero dimension), return error or nil vectors
		return nil, nil, fmt.Errorf("empty keys and values provided")
	}

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…