Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ailego/math/euclidean_distance_matrix_fp16_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ float SquaredEuclideanDistanceFp16NEON(const Float16 *lhs, const Float16 *rhs,
size_t size);
#endif

#if defined(__riscv_zvfh)
float SquaredEuclideanDistanceRVV(const Float16 *lhs, const Float16 *rhs,
size_t size);
#endif

#if defined(__AVX512FP16__)
float SquaredEuclideanDistanceFp16AVX512FP16(const Float16 *lhs,
const Float16 *rhs, size_t size);
Expand All @@ -48,6 +53,8 @@ void SquaredEuclideanDistanceMatrix<Float16, 1, 1>::Compute(const ValueType *m,
float *out) {
#if defined(__ARM_NEON)
*out = SquaredEuclideanDistanceFp16NEON(m, q, dim);
#elif defined(__riscv_zvfh)
*out = SquaredEuclideanDistanceRVV(m, q, dim);
#else
#if defined(__AVX512FP16__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512_FP16) {
Expand Down
58 changes: 58 additions & 0 deletions src/ailego/math/euclidean_distance_matrix_fp16_rvv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2025-present the zvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <zvec/ailego/internal/platform.h>
#include "euclidean_distance_matrix.h"

namespace zvec {
namespace ailego {

#if defined(__riscv_zvfh)
namespace {

static inline float SquaredEuclideanDistanceRVVImpl(const Float16 *lhs,
const Float16 *rhs,
size_t size) {
const _Float16 *lhs_fp16 = reinterpret_cast<const _Float16 *>(lhs);
const _Float16 *rhs_fp16 = reinterpret_cast<const _Float16 *>(rhs);
const size_t vlmax = __riscv_vsetvlmax_e16m4();
vfloat32m8_t v_sum = __riscv_vfmv_v_f_f32m8(0.0f, vlmax);

while (size != 0) {
const size_t vl = __riscv_vsetvl_e16m4(size);
vfloat16m4_t v_lhs = __riscv_vle16_v_f16m4(lhs_fp16, vl);
vfloat16m4_t v_rhs = __riscv_vle16_v_f16m4(rhs_fp16, vl);
vfloat32m8_t v_diff = __riscv_vfwsub_vv_f32m8(v_lhs, v_rhs, vl);
v_sum = __riscv_vfmacc_vv_f32m8_tu(v_sum, v_diff, v_diff, vl);
lhs_fp16 += vl;
rhs_fp16 += vl;
size -= vl;
}

vfloat32m1_t v_zero = __riscv_vfmv_v_f_f32m1(0.0f, 1);
vfloat32m1_t v_red = __riscv_vfredusum_vs_f32m8_f32m1(v_sum, v_zero, vlmax);
return __riscv_vfmv_f_s_f32m1_f32(v_red);
}

} // namespace

float SquaredEuclideanDistanceRVV(const Float16 *lhs, const Float16 *rhs,
size_t size) {
return SquaredEuclideanDistanceRVVImpl(lhs, rhs, size);
}

#endif // __riscv_zvfh

} // namespace ailego
} // namespace zvec
7 changes: 7 additions & 0 deletions src/ailego/math/euclidean_distance_matrix_fp32_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ void SquaredEuclideanDistanceFp32NEON(const float *lhs, const float *rhs,
size_t size, float *out);
#endif

#if defined(__riscv_vector)
float SquaredEuclideanDistanceRVV(const float *lhs, const float *rhs,
size_t size);
#endif

#if defined(__AVX512F__)
float SquaredEuclideanDistanceFp32AVX512(const float *lhs, const float *rhs,
size_t size);
Expand Down Expand Up @@ -51,6 +56,8 @@ void SquaredEuclideanDistanceMatrix<float, 1, 1>::Compute(const ValueType *m,
float *out) {
#if defined(__ARM_NEON)
SquaredEuclideanDistanceFp32NEON(m, q, dim, out);
#elif defined(__riscv_vector)
*out = SquaredEuclideanDistanceRVV(m, q, dim);
#else
#if defined(__AVX512F__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512F) {
Expand Down
58 changes: 58 additions & 0 deletions src/ailego/math/euclidean_distance_matrix_fp32_rvv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2025-present the zvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <zvec/ailego/internal/platform.h>
#include "distance_matrix_euclidean_utility.i"
#include "euclidean_distance_matrix.h"

namespace zvec {
namespace ailego {

#if defined(__riscv_vector)
namespace {

static inline float SquaredEuclideanDistanceRVVImpl(const float *lhs,
const float *rhs,
size_t size) {
const size_t vlmax = __riscv_vsetvlmax_e32m8();
vfloat32m8_t v_sum = __riscv_vfmv_v_f_f32m8(0.0f, vlmax);

while (size != 0) {
size_t vl = __riscv_vsetvl_e32m8(size);
vfloat32m8_t v_lhs = __riscv_vle32_v_f32m8(lhs, vl);
vfloat32m8_t v_rhs = __riscv_vle32_v_f32m8(rhs, vl);
vfloat32m8_t v_d = __riscv_vfsub_vv_f32m8(v_lhs, v_rhs, vl);
v_sum = __riscv_vfmacc_vv_f32m8_tu(v_sum, v_d, v_d, vl);
lhs += vl;
rhs += vl;
size -= vl;
}

vfloat32m1_t v_zero = __riscv_vfmv_v_f_f32m1(0.0f, 1);
vfloat32m1_t v_red = __riscv_vfredusum_vs_f32m8_f32m1(v_sum, v_zero, vlmax);
return __riscv_vfmv_f_s_f32m1_f32(v_red);
}

} // namespace

//! Squared Euclidean Distance
float SquaredEuclideanDistanceRVV(const float *lhs, const float *rhs,
size_t size) {
return SquaredEuclideanDistanceRVVImpl(lhs, rhs, size);
}

#endif // __riscv_vector

} // namespace ailego
} // namespace zvec
9 changes: 9 additions & 0 deletions src/ailego/math/euclidean_distance_matrix_int8_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
namespace zvec {
namespace ailego {

#if defined(__riscv_vector)
float SquaredEuclideanDistanceRVV(const int8_t *lhs, const int8_t *rhs,
size_t size);
#endif

#if defined(__AVX2__)
float SquaredEuclideanDistanceInt8AVX2(const int8_t *lhs, const int8_t *rhs,
size_t size);
Expand All @@ -36,6 +41,9 @@ void SquaredEuclideanDistanceMatrix<int8_t, 1, 1>::Compute(const ValueType *m,
const ValueType *q,
size_t dim,
float *out) {
#if defined(__riscv_vector)
*out = SquaredEuclideanDistanceRVV(m, q, dim);
#else
#if defined(__AVX2__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX2) {
*out = SquaredEuclideanDistanceInt8AVX2(m, q, dim);
Expand All @@ -51,6 +59,7 @@ void SquaredEuclideanDistanceMatrix<int8_t, 1, 1>::Compute(const ValueType *m,
#endif

*out = SquaredEuclideanDistanceInt8Scalar(m, q, dim);
#endif // __riscv_vector
}

//! Compute the distance between matrix and query (INT8, M=1, N=1)
Expand Down
56 changes: 56 additions & 0 deletions src/ailego/math/euclidean_distance_matrix_int8_rvv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2025-present the zvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <zvec/ailego/internal/platform.h>
#include "euclidean_distance_matrix.h"

namespace zvec {
namespace ailego {

#if defined(__riscv_vector)
namespace {

static inline float SquaredEuclideanDistanceRVVImpl(const int8_t *lhs,
const int8_t *rhs,
size_t size) {
const size_t vlmax = __riscv_vsetvlmax_e8m2();
vint32m8_t v_sum = __riscv_vmv_v_x_i32m8(0, vlmax);

while (size != 0) {
const size_t vl = __riscv_vsetvl_e8m2(size);
vint8m2_t v_lhs = __riscv_vle8_v_i8m2(lhs, vl);
vint8m2_t v_rhs = __riscv_vle8_v_i8m2(rhs, vl);
vint16m4_t v_d = __riscv_vwsub_vv_i16m4(v_lhs, v_rhs, vl);
v_sum = __riscv_vwmacc_vv_i32m8_tu(v_sum, v_d, v_d, vl);
lhs += vl;
rhs += vl;
size -= vl;
}

vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, 1);
vint32m1_t v_red = __riscv_vredsum_vs_i32m8_i32m1(v_sum, v_zero, vlmax);
return static_cast<float>(__riscv_vmv_x_s_i32m1_i32(v_red));
}

} // namespace

float SquaredEuclideanDistanceRVV(const int8_t *lhs, const int8_t *rhs,
size_t size) {
return SquaredEuclideanDistanceRVVImpl(lhs, rhs, size);
}

#endif // __riscv_vector

} // namespace ailego
} // namespace zvec
9 changes: 9 additions & 0 deletions src/ailego/math/inner_product_matrix_fp16_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ float MinusInnerProductFp16NEON(const Float16 *lhs, const Float16 *rhs,
size_t size);
#endif

#if defined(__riscv_zvfh)
float InnerProductRVV(const Float16 *lhs, const Float16 *rhs, size_t size);
float MinusInnerProductRVV(const Float16 *lhs, const Float16 *rhs, size_t size);
#endif

#if defined(__AVX__)
float InnerProductFp16AVX(const Float16 *lhs, const Float16 *rhs, size_t size);
float MinusInnerProductFp16AVX(const Float16 *lhs, const Float16 *rhs,
Expand Down Expand Up @@ -58,6 +63,8 @@ void InnerProductMatrix<Float16, 1, 1>::Compute(const ValueType *m,
float *out) {
#if defined(__ARM_NEON)
*out = InnerProductFp16NEON(m, q, dim);
#elif defined(__riscv_zvfh)
*out = InnerProductRVV(m, q, dim);
#else
#if defined(__AVX512FP16__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512_FP16) {
Expand Down Expand Up @@ -88,6 +95,8 @@ void MinusInnerProductMatrix<Float16, 1, 1>::Compute(const ValueType *m,
size_t dim, float *out) {
#if defined(__ARM_NEON)
*out = MinusInnerProductFp16NEON(m, q, dim);
#elif defined(__riscv_zvfh)
*out = MinusInnerProductRVV(m, q, dim);
#else
#if defined(__AVX512FP16__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512_FP16) {
Expand Down
60 changes: 60 additions & 0 deletions src/ailego/math/inner_product_matrix_fp16_rvv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2025-present the zvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <zvec/ailego/internal/platform.h>
#include "inner_product_matrix.h"

namespace zvec {
namespace ailego {

#if defined(__riscv_zvfh)
namespace {

static inline float InnerProductRVVImpl(const Float16 *lhs, const Float16 *rhs,
size_t size) {
const _Float16 *lhs_fp16 = reinterpret_cast<const _Float16 *>(lhs);
const _Float16 *rhs_fp16 = reinterpret_cast<const _Float16 *>(rhs);
const size_t vlmax = __riscv_vsetvlmax_e16m4();
vfloat32m8_t v_sum = __riscv_vfmv_v_f_f32m8(0.0f, vlmax);

while (size != 0) {
const size_t vl = __riscv_vsetvl_e16m4(size);
vfloat16m4_t v_lhs = __riscv_vle16_v_f16m4(lhs_fp16, vl);
vfloat16m4_t v_rhs = __riscv_vle16_v_f16m4(rhs_fp16, vl);
v_sum = __riscv_vfwmacc_vv_f32m8_tu(v_sum, v_lhs, v_rhs, vl);
lhs_fp16 += vl;
rhs_fp16 += vl;
size -= vl;
}

vfloat32m1_t v_zero = __riscv_vfmv_v_f_f32m1(0.0f, 1);
vfloat32m1_t v_red = __riscv_vfredusum_vs_f32m8_f32m1(v_sum, v_zero, vlmax);
return __riscv_vfmv_f_s_f32m1_f32(v_red);
}

} // namespace

float InnerProductRVV(const Float16 *lhs, const Float16 *rhs, size_t size) {
return InnerProductRVVImpl(lhs, rhs, size);
}

float MinusInnerProductRVV(const Float16 *lhs, const Float16 *rhs,
size_t size) {
return -InnerProductRVVImpl(lhs, rhs, size);
}

#endif // __riscv_zvfh

} // namespace ailego
} // namespace zvec
9 changes: 9 additions & 0 deletions src/ailego/math/inner_product_matrix_fp32_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ float MinusInnerProductFp32NEON(const float *lhs, const float *rhs,
size_t size);
#endif

#if defined(__riscv_vector)
float InnerProductRVV(const float *lhs, const float *rhs, size_t size);
float MinusInnerProductRVV(const float *lhs, const float *rhs, size_t size);
#endif

#if defined(__AVX512F__)
float InnerProductFp32AVX512(const float *lhs, const float *rhs, size_t size);
float MinusInnerProductFp32AVX512(const float *lhs, const float *rhs,
Expand All @@ -51,6 +56,8 @@ void InnerProductMatrix<float, 1, 1>::Compute(const float *m, const float *q,
size_t dim, float *out) {
#if defined(__ARM_NEON)
*out = InnerProductFp32NEON(m, q, dim);
#elif defined(__riscv_vector)
*out = InnerProductRVV(m, q, dim);
#else
#if defined(__AVX512F__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512F) {
Expand Down Expand Up @@ -82,6 +89,8 @@ void MinusInnerProductMatrix<float, 1, 1>::Compute(const float *m,
float *out) {
#if defined(__ARM_NEON)
*out = MinusInnerProductFp32NEON(m, q, dim);
#elif defined(__riscv_vector)
*out = MinusInnerProductRVV(m, q, dim);
#else
#if defined(__AVX512F__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512F) {
Expand Down
Loading
Loading