enhance: compile go assembly with build constraints for different arch (#29515)

issue #28657 
follow https://pkg.go.dev/cmd/go#hdr-Build_constraints to compile go
assembly with different cpu arch

Signed-off-by: chasingegg <chao.gao@zilliz.com>
This commit is contained in:
Gao 2023-12-28 15:40:47 +08:00 committed by GitHub
parent 5474bce9d2
commit 8a4c0d4a3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 37 deletions

View File

@ -1,4 +1,4 @@
// Code generated by command: go run ip.go -out ip.s -stubs ip_stub.go. DO NOT EDIT.
// Code generated by command: go run ip.go -out ip_amd64.s -stubs ip_stub_amd64.go. DO NOT EDIT.
#include "textflag.h"

View File

@ -1,6 +0,0 @@
// Code generated by command: go run ip.go -out ip.s -stubs ip_stub.go. DO NOT EDIT.
package asm
// inner product between x and y
func IP(x []float32, y []float32) float32

View File

@ -0,0 +1,6 @@
// Code generated by command: go run ip.go -out ip_amd64.s -stubs ip_stub_amd64.go. DO NOT EDIT.
package asm
// inner product between x and y
func IP(x []float32, y []float32) float32

View File

@ -1,4 +1,4 @@
// Code generated by command: go run l2.go -out l2.s -stubs l2_stub.go. DO NOT EDIT.
// Code generated by command: go run l2.go -out l2_amd64.s -stubs l2_stub_amd64.go. DO NOT EDIT.
#include "textflag.h"

View File

@ -1,6 +0,0 @@
// Code generated by command: go run l2.go -out l2.s -stubs l2_stub.go. DO NOT EDIT.
package asm
// squared l2 between x and y
func L2(x []float32, y []float32) float32

View File

@ -0,0 +1,6 @@
// Code generated by command: go run l2.go -out l2_amd64.s -stubs l2_stub_amd64.go. DO NOT EDIT.
package asm
// squared l2 between x and y
func L2(x []float32, y []float32) float32

View File

@ -6,10 +6,6 @@ import (
"sync"
"github.com/cockroachdb/errors"
"golang.org/x/sys/cpu"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/util/distance/asm"
)
/**
@ -58,27 +54,11 @@ func CosineImplPure(a []float32, b []float32) float32 {
}
var (
L2Impl func(a []float32, b []float32) float32
IPImpl func(a []float32, b []float32) float32
CosineImpl func(a []float32, b []float32) float32
L2Impl func(a []float32, b []float32) float32 = IPImplPure
IPImpl func(a []float32, b []float32) float32 = L2ImplPure
CosineImpl func(a []float32, b []float32) float32 = CosineImplPure
)
func init() {
if cpu.X86.HasAVX2 {
log.Info("Hook avx for go simd distance computation")
IPImpl = asm.IP
L2Impl = asm.L2
CosineImpl = func(a []float32, b []float32) float32 {
return asm.IP(a, b) / float32(math.Sqrt(float64(asm.IP(a, a))*float64((asm.IP(b, b)))))
}
} else {
log.Info("Use pure go distance computation")
IPImpl = IPImplPure
L2Impl = L2ImplPure
CosineImpl = CosineImplPure
}
}
// ValidateMetricType returns metric text or error
func ValidateMetricType(metric string) (string, error) {
if metric == "" {

View File

@ -0,0 +1,21 @@
package distance
import (
"math"
"golang.org/x/sys/cpu"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/util/distance/asm"
)
func init() {
if cpu.X86.HasAVX2 {
log.Info("Hook avx for go simd distance computation")
IPImpl = asm.IP
L2Impl = asm.L2
CosineImpl = func(a []float32, b []float32) float32 {
return asm.IP(a, b) / float32(math.Sqrt(float64(asm.IP(a, a))*float64((asm.IP(b, b)))))
}
}
}