2222namespace zvec {
2323namespace ailego {
2424
25+ // --------------------------------------------------
26+ // Dense
27+ // --------------------------------------------------
2528/* ! Squared Euclidean Distance Matrix
2629 */
2730template <typename T, size_t M, size_t N, typename = void >
@@ -48,6 +51,46 @@ struct SquaredEuclideanDistanceMatrix<
4851 }
4952};
5053
54+ template <>
55+ struct SquaredEuclideanDistanceMatrix <uint8_t , 1 , 1 > {
56+ // ! Type of value
57+ using ValueType = uint8_t ;
58+
59+ // ! Compute the distance between matrix and query
60+ static void Compute (const ValueType *m, const ValueType *q, size_t dim,
61+ float *out);
62+ };
63+
64+ template <>
65+ struct SquaredEuclideanDistanceMatrix <int8_t , 1 , 1 > {
66+ // ! Type of value
67+ using ValueType = int8_t ;
68+
69+ // ! Compute the distance between matrix and query
70+ static void Compute (const ValueType *m, const ValueType *q, size_t dim,
71+ float *out);
72+ };
73+
74+ template <>
75+ struct SquaredEuclideanDistanceMatrix <Float16, 1 , 1 > {
76+ // ! Type of value
77+ using ValueType = Float16;
78+
79+ // ! Compute the distance between matrix and query
80+ static void Compute (const ValueType *m, const ValueType *q, size_t dim,
81+ float *out);
82+ };
83+
84+ template <>
85+ struct SquaredEuclideanDistanceMatrix <float , 1 , 1 > {
86+ // ! Type of value
87+ using ValueType = float ;
88+
89+ // ! Compute the distance between matrix and query
90+ static void Compute (const ValueType *m, const ValueType *q, size_t dim,
91+ float *out);
92+ };
93+
5194/* ! Squared Euclidean Distance Matrix
5295 */
5396template <typename T, size_t M, size_t N>
@@ -353,32 +396,6 @@ struct SquaredEuclideanDistanceMatrix<uint8_t, M, 1,
353396 }
354397};
355398
356- #if !defined(__SSE4_1__)
357- /* ! Squared Euclidean Distance Matrix (INT4, M=1, N=1)
358- */
359- template <>
360- struct SquaredEuclideanDistanceMatrix <uint8_t , 1 , 1 > {
361- // ! Type of value
362- using ValueType = uint8_t ;
363-
364- // ! Compute the distance between matrix and query
365- static inline void Compute (const ValueType *m, const ValueType *q, size_t dim,
366- float *out) {
367- ailego_assert (m && q && dim && !(dim & 1 ) && out);
368-
369- float sum = 0.0 ;
370- for (size_t i = 0 ; i < (dim >> 1 ); ++i) {
371- uint8_t m_val = m[i];
372- uint8_t q_val = q[i];
373- sum +=
374- Int4SquaredDiffTable[((m_val << 4 ) & 0xf0 ) | ((q_val >> 0 ) & 0xf )] +
375- Int4SquaredDiffTable[((m_val >> 0 ) & 0xf0 ) | ((q_val >> 4 ) & 0xf )];
376- }
377- *out = sum;
378- }
379- };
380- #endif // !__SSE4_1__
381-
382399/* ! Euclidean Distance Matrix
383400 */
384401template <typename T, size_t M, size_t N,
@@ -424,76 +441,26 @@ struct EuclideanDistanceMatrix<
424441 }
425442};
426443
427- #if !defined(__SSE4_1__)
428- /* ! Euclidean Distance Matrix (INT4, M=1, N=1)
429- */
430444template <>
431445struct EuclideanDistanceMatrix <uint8_t , 1 , 1 > {
432446 // ! Type of value
433447 using ValueType = uint8_t ;
434448
435- // ! Compute the distance between matrix and query
436- static inline void Compute (const ValueType *m, const ValueType *q, size_t dim,
437- float *out) {
438- ailego_assert (m && q && dim && !(dim & 1 ) && out);
439-
440- float sum = 0.0 ;
441- for (size_t i = 0 ; i < (dim >> 1 ); ++i) {
442- uint8_t m_val = m[i];
443- uint8_t q_val = q[i];
444- sum +=
445- Int4SquaredDiffTable[((m_val << 4 ) & 0xf0 ) | ((q_val >> 0 ) & 0xf )] +
446- Int4SquaredDiffTable[((m_val >> 0 ) & 0xf0 ) | ((q_val >> 4 ) & 0xf )];
447- }
448- *out = std::sqrt (sum);
449- }
450- };
451- #endif // !__SSE4_1__
452-
453- #if defined(__SSE__) || defined(__ARM_NEON)
454- /* ! Squared Euclidean Distance Matrix (FP32, M=1, N=1)
455- */
456- template <>
457- struct SquaredEuclideanDistanceMatrix <float , 1 , 1 > {
458- // ! Type of value
459- using ValueType = float ;
460-
461- // ! Compute the distance between matrix and query
462- static void Compute (const ValueType *m, const ValueType *q, size_t dim,
463- float *out);
464- };
465- #endif // __SSE__ || __ARM_NEON
466-
467- #if defined(__SSE__) || (defined(__ARM_NEON) && (defined(__aarch64__)))
468- /* ! Euclidean Distance Matrix (FP32, M=1, N=1)
469- */
470- template <>
471- struct EuclideanDistanceMatrix <float , 1 , 1 > {
472- // ! Type of value
473- using ValueType = float ;
474-
475449 // ! Compute the distance between matrix and query
476450 static void Compute (const ValueType *m, const ValueType *q, size_t dim,
477451 float *out);
478452};
479- #endif // __SSE__ || __ARM_NEON && __aarch64__
480453
481- #if (defined(__F16C__) && defined(__AVX__)) || \
482- (defined (__ARM_NEON) && defined (__aarch64__))
483- /* ! Squared Euclidean Distance Matrix (FP16, M=1, N=1)
484- */
485454template <>
486- struct SquaredEuclideanDistanceMatrix <Float16 , 1 , 1 > {
455+ struct EuclideanDistanceMatrix < int8_t , 1 , 1 > {
487456 // ! Type of value
488- using ValueType = Float16 ;
457+ using ValueType = int8_t ;
489458
490459 // ! Compute the distance between matrix and query
491460 static void Compute (const ValueType *m, const ValueType *q, size_t dim,
492461 float *out);
493462};
494463
495- /* ! Euclidean Distance Matrix (FP16, M=1, N=1)
496- */
497464template <>
498465struct EuclideanDistanceMatrix <Float16, 1 , 1 > {
499466 // ! Type of value
@@ -503,58 +470,21 @@ struct EuclideanDistanceMatrix<Float16, 1, 1> {
503470 static void Compute (const ValueType *m, const ValueType *q, size_t dim,
504471 float *out);
505472};
506- #endif // (__F16C__ && __AVX__) || (__ARM_NEON && __aarch64__)
507473
508- #if defined(__SSE4_1__)
509- /* ! Squared Euclidean Distance Matrix (INT8, M=1, N=1)
510- */
511474template <>
512- struct SquaredEuclideanDistanceMatrix <int8_t , 1 , 1 > {
513- // ! Type of value
514- using ValueType = int8_t ;
515-
516- // ! Compute the distance between matrix and query
517- static void Compute (const ValueType *m, const ValueType *q, size_t dim,
518- float *out);
519- };
520-
521- /* ! Euclidean Distance Matrix (INT8, M=1, N=1)
522- */
523- template <>
524- struct EuclideanDistanceMatrix <int8_t , 1 , 1 > {
525- // ! Type of value
526- using ValueType = int8_t ;
527-
528- // ! Compute the distance between matrix and query
529- static void Compute (const ValueType *m, const ValueType *q, size_t dim,
530- float *out);
531- };
532-
533- /* ! Squared Euclidean Distance Matrix (INT4, M=1, N=1)
534- */
535- template <>
536- struct SquaredEuclideanDistanceMatrix <uint8_t , 1 , 1 > {
475+ struct EuclideanDistanceMatrix <float , 1 , 1 > {
537476 // ! Type of value
538- using ValueType = uint8_t ;
477+ using ValueType = float ;
539478
540479 // ! Compute the distance between matrix and query
541480 static void Compute (const ValueType *m, const ValueType *q, size_t dim,
542481 float *out);
543482};
544483
545- /* ! Euclidean Distance Matrix (INT4, M=1, N=1)
546- */
547- template <>
548- struct EuclideanDistanceMatrix <uint8_t , 1 , 1 > {
549- // ! Type of value
550- using ValueType = uint8_t ;
551-
552- // ! Compute the distance between matrix and query
553- static void Compute (const ValueType *m, const ValueType *q, size_t dim,
554- float *out);
555- };
556- #endif // __SSE4_1__
557484
485+ // --------------------------------------------------
486+ // Sparse
487+ // --------------------------------------------------
558488/* ! Squared Euclidean Distance Sparse Matrix
559489 */
560490template <typename T>
0 commit comments