INNER CODE UNIT · Go

bestDotProduct

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

func bestDotProduct(v1, v2 Vector) (float64, error) {
	if len(v1) != len(v2) {
		return 0, fmt.Errorf("vector dimensions mismatch: %d != %d", len(v1), len(v2))
	}
	// Loop unrolling for performance
	sum := 0.0
	n := len(v1)
	for i := 0; i < n-7; i += 8 {
		sum += v1[i]*v2[i] + v1[i+1]*v2[i+1] + v1[i+2]*v2[i+2] + v1[i+3]*v2[i+3] +
			v1[i+4]*v2[i+4] + v1[i+5]*v2[i+5] + v1[i+6]*v2[i+6] + v1[i+7]*v2[i+7]
	}
	for i := (n/8)*8; i < n; i++ {
		sum += v1[i] * v2[i]
	}
	return sum, nil
}

// Deprecated: Use DotProduct instead.

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…