| 1 | /* |
| 2 | Copyright (C) 2024-2025 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "grn_ctx.h" |
| 22 | |
| 23 | #include <groonga/bulk.hpp> |
| 24 | |
| 25 | #include <cmath> |
| 26 | |
| 27 | #ifdef GRN_WITH_SIMSIMD |
| 28 | # include <simsimd/simsimd.h> |
| 29 | #endif |
| 30 | |
| 31 | #ifdef GRN_WITH_XSIMD |
| 32 | # include <xsimd/xsimd.hpp> |
| 33 | #endif |
| 34 | |
| 35 | #if defined(GRN_WITH_XSIMD) && XSIMD_VERSION_MAJOR < 14 |
| 36 | namespace xsimd { |
| 37 | using common = generic; |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | namespace grn { |
| 42 | namespace distance { |
| 43 | extern bool use_simsimd; |
| 44 | extern bool use_xsimd; |
| 45 | constexpr size_t use_simd_threshold = 256; |
| 46 | |
| 47 | #ifdef GRN_WITH_SIMSIMD |
| 48 | namespace simsimd { |
| 49 | extern simsimd_capability_t capabilities; |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | #ifdef GRN_WITH_XSIMD |
| 54 | struct l2_norm { |
| 55 | template <typename Arch, typename ElementType> |
| 56 | float |
| 57 | operator()(Arch, const ElementType *vector_raw, size_t n_elements); |
| 58 | }; |
| 59 | |
| 60 | struct difference_l1_norm { |
| 61 | template <typename Arch, typename ElementType> |
| 62 | float |
| 63 | operator()(Arch, |
| 64 | const ElementType *vector_raw1, |
| 65 | const ElementType *vector_raw2, |
| 66 | size_t n_elements); |
| 67 | }; |
| 68 | |
| 69 | struct difference_l2_norm_squared { |
| 70 | template <typename Arch, typename ElementType> |
| 71 | float |
| 72 | operator()(Arch, |
| 73 | const ElementType *vector_raw1, |
| 74 | const ElementType *vector_raw2, |
| 75 | size_t n_elements); |
| 76 | }; |
| 77 | |
| 78 | struct inner_product { |
| 79 | template <typename Arch, typename ElementType> |
| 80 | float |
| 81 | operator()(Arch, |
| 82 | const ElementType *vector_raw1, |
| 83 | const ElementType *vector_raw2, |
| 84 | size_t n_elements); |
| 85 | }; |
| 86 | |
| 87 | struct cosine { |
| 88 | template <typename Arch, typename ElementType> |
| 89 | float |
| 90 | operator()(Arch, |
| 91 | const ElementType *vector_raw1, |
| 92 | const ElementType *vector_raw2, |
| 93 | size_t n_elements); |
| 94 | }; |
| 95 | #endif |
| 96 | } // namespace distance |
| 97 | } // namespace grn |
| 98 | |
| 99 | #ifdef GRN_WITH_XSIMD |
| 100 | # define GRN_INSTANTIATION_EXTERN extern |
| 101 | |
| 102 | # define GRN_INSTANTIATION_SIMSIMD_ARCH avx512 |
| 103 | # define GRN_INSTANTIATION_XSIMD_ARCH xsimd::avx512dq |
| 104 | # include "grn_distance_instantiation.hpp" |
| 105 | # undef GRN_INSTANTIATION_SIMSIMD_ARCH |
| 106 | # undef GRN_INSTANTIATION_XSIMD_ARCH |
| 107 | |
| 108 | # define GRN_INSTANTIATION_XSIMD_ARCH xsimd::avx2 |
| 109 | # include "grn_distance_instantiation.hpp" |
| 110 | # undef GRN_INSTANTIATION_XSIMD_ARCH |
| 111 | |
| 112 | # define GRN_INSTANTIATION_XSIMD_ARCH xsimd::avx |
| 113 | # include "grn_distance_instantiation.hpp" |
| 114 | # undef GRN_INSTANTIATION_XSIMD_ARCH |
| 115 | |
| 116 | # define GRN_INSTANTIATION_SIMSIMD_ARCH neon |
| 117 | # define GRN_INSTANTIATION_XSIMD_ARCH xsimd::neon64 |
| 118 | # include "grn_distance_instantiation.hpp" |
| 119 | # undef GRN_INSTANTIATION_SIMSIMD_ARCH |
| 120 | # undef GRN_INSTANTIATION_XSIMD_ARCH |
| 121 | |
| 122 | # define GRN_INSTANTIATION_SIMSIMD_ARCH serial |
| 123 | # define GRN_INSTANTIATION_XSIMD_ARCH xsimd::common |
| 124 | # include "grn_distance_instantiation.hpp" |
| 125 | # undef GRN_INSTANTIATION_SIMSIMD_ARCH |
| 126 | # undef GRN_INSTANTIATION_XSIMD_ARCH |
| 127 | |
| 128 | # undef GRN_INSTANTIATION_EXTERN |
| 129 | #endif |
| 130 | |
| 131 | namespace grn { |
| 132 | namespace distance { |
| 133 | template <typename ElementType> |
| 134 | float |
| 135 | compute_l2_norm(grn_obj *vector) |
| 136 | { |
| 137 | auto vector_raw = |
| 138 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector)); |
| 139 | auto n_elements = GRN_BULK_VSIZE(vector) / sizeof(ElementType); |
| 140 | #ifdef GRN_WITH_SIMD |
| 141 | # ifdef GRN_WITH_XSIMD |
| 142 | if (use_xsimd && |
| 143 | (sizeof(ElementType) * n_elements) >= use_simd_threshold) { |
| 144 | auto dispatched = xsimd::dispatch<xsimd::arch_list< |
| 145 | # ifdef GRN_WITH_SIMD_AVX512 |
| 146 | xsimd::avx512dq, |
| 147 | # endif |
| 148 | # ifdef GRN_WITH_SIMD_AVX2 |
| 149 | xsimd::avx2, |
| 150 | # endif |
| 151 | # ifdef GRN_WITH_SIMD_AVX |
| 152 | xsimd::avx, |
| 153 | # endif |
| 154 | # ifdef GRN_WITH_SIMD_NEON64 |
| 155 | xsimd::neon64, |
| 156 | # endif |
| 157 | xsimd::common>>(l2_norm{}); |
| 158 | return dispatched(vector_raw, n_elements); |
| 159 | } |
| 160 | # endif |
| 161 | #endif |
| 162 | float square_sum = 0; |
| 163 | for (size_t i = 0; i < n_elements; ++i) { |
| 164 | square_sum += vector_raw[i] * vector_raw[i]; |
| 165 | } |
| 166 | return std::sqrt(square_sum); |
| 167 | } |
| 168 | |
| 169 | template <typename ElementType> |
| 170 | float |
| 171 | compute_difference_l1_norm(grn_obj *vector1, grn_obj *vector2) |
| 172 | { |
| 173 | auto vector_raw1 = |
| 174 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector1)); |
| 175 | auto vector_raw2 = |
| 176 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector2)); |
| 177 | auto n_elements = GRN_BULK_VSIZE(vector1) / sizeof(ElementType); |
| 178 | #ifdef GRN_WITH_SIMD |
| 179 | # ifdef GRN_WITH_XSIMD |
| 180 | if (use_xsimd && |
| 181 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 182 | auto dispatched = xsimd::dispatch<xsimd::arch_list< |
| 183 | # ifdef GRN_WITH_SIMD_AVX512 |
| 184 | xsimd::avx512dq, |
| 185 | # endif |
| 186 | # ifdef GRN_WITH_SIMD_AVX2 |
| 187 | xsimd::avx2, |
| 188 | # endif |
| 189 | # ifdef GRN_WITH_SIMD_AVX |
| 190 | xsimd::avx, |
| 191 | # endif |
| 192 | # ifdef GRN_WITH_SIMD_NEON64 |
| 193 | xsimd::neon64, |
| 194 | # endif |
| 195 | xsimd::common>>(difference_l1_norm{}); |
| 196 | return dispatched(vector_raw1, vector_raw2, n_elements); |
| 197 | } |
| 198 | # endif |
| 199 | #endif |
| 200 | ElementType absolute_sum = 0; |
| 201 | for (size_t i = 0; i < n_elements; ++i) { |
| 202 | auto difference = vector_raw1[i] - vector_raw2[i]; |
| 203 | absolute_sum += difference * ((difference > 0) - (difference < 0)); |
| 204 | } |
| 205 | return absolute_sum; |
| 206 | } |
| 207 | |
| 208 | template <typename ElementType> |
| 209 | float |
| 210 | compute_difference_l2_norm_squared(grn_obj *vector1, grn_obj *vector2) |
| 211 | { |
| 212 | auto vector_raw1 = |
| 213 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector1)); |
| 214 | auto vector_raw2 = |
| 215 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector2)); |
| 216 | auto n_elements = GRN_BULK_VSIZE(vector1) / sizeof(ElementType); |
| 217 | #ifdef GRN_WITH_SIMD |
| 218 | # ifdef GRN_WITH_SIMSIMD |
| 219 | if (use_simsimd && |
| 220 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 221 | # ifdef GRN_WITH_SIMD_AVX512 |
| 222 | if (simsimd::capabilities & simsimd_cap_x86_avx512_k) { |
| 223 | return simsimd::compute_distance_l2_norm_squared_avx512(vector_raw1, |
| 224 | vector_raw2, |
| 225 | n_elements); |
| 226 | } |
| 227 | # endif |
| 228 | # ifdef GRN_WITH_SIMD_NEON64 |
| 229 | if (simsimd::capabilities & simsimd_cap_arm_neon_k) { |
| 230 | return simsimd::compute_distance_l2_norm_squared_neon(vector_raw1, |
| 231 | vector_raw2, |
| 232 | n_elements); |
| 233 | } |
| 234 | # endif |
| 235 | return simsimd::compute_distance_l2_norm_squared_serial(vector_raw1, |
| 236 | vector_raw2, |
| 237 | n_elements); |
| 238 | } |
| 239 | # endif |
| 240 | # ifdef GRN_WITH_XSIMD |
| 241 | if (use_xsimd && |
| 242 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 243 | auto dispatched = xsimd::dispatch<xsimd::arch_list< |
| 244 | # ifdef GRN_WITH_SIMD_AVX512 |
| 245 | xsimd::avx512dq, |
| 246 | # endif |
| 247 | # ifdef GRN_WITH_SIMD_AVX2 |
| 248 | xsimd::avx2, |
| 249 | # endif |
| 250 | # ifdef GRN_WITH_SIMD_AVX |
| 251 | xsimd::avx, |
| 252 | # endif |
| 253 | # ifdef GRN_WITH_SIMD_NEON64 |
| 254 | xsimd::neon64, |
| 255 | # endif |
| 256 | xsimd::common>>(difference_l2_norm_squared{}); |
| 257 | return dispatched(vector_raw1, vector_raw2, n_elements); |
| 258 | } |
| 259 | # endif |
| 260 | #endif |
| 261 | ElementType square_sum = 0; |
| 262 | for (size_t i = 0; i < n_elements; ++i) { |
| 263 | auto difference = vector_raw1[i] - vector_raw2[i]; |
| 264 | square_sum += difference * difference; |
| 265 | } |
| 266 | return square_sum; |
| 267 | } |
| 268 | |
| 269 | template <typename ElementType> |
| 270 | float |
| 271 | compute_distance_inner_product(grn_obj *vector1, grn_obj *vector2) |
| 272 | { |
| 273 | auto vector_raw1 = |
| 274 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector1)); |
| 275 | auto vector_raw2 = |
| 276 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector2)); |
| 277 | auto n_elements = GRN_BULK_VSIZE(vector1) / sizeof(ElementType); |
| 278 | #ifdef GRN_WITH_SIMD |
| 279 | # ifdef GRN_WITH_SIMSIMD |
| 280 | if (use_simsimd && |
| 281 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 282 | # ifdef GRN_WITH_SIMD_AVX512 |
| 283 | if (simsimd::capabilities & simsimd_cap_x86_avx512_k) { |
| 284 | return simsimd::compute_distance_inner_product_avx512(vector_raw1, |
| 285 | vector_raw2, |
| 286 | n_elements); |
| 287 | } |
| 288 | # endif |
| 289 | # ifdef GRN_WITH_SIMD_NEON64 |
| 290 | if (simsimd::capabilities & simsimd_cap_arm_neon_k) { |
| 291 | return simsimd::compute_distance_inner_product_neon(vector_raw1, |
| 292 | vector_raw2, |
| 293 | n_elements); |
| 294 | } |
| 295 | # endif |
| 296 | return simsimd::compute_distance_inner_product_serial(vector_raw1, |
| 297 | vector_raw2, |
| 298 | n_elements); |
| 299 | } |
| 300 | # endif |
| 301 | # ifdef GRN_WITH_XSIMD |
| 302 | if (use_xsimd && |
| 303 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 304 | auto dispatched = xsimd::dispatch<xsimd::arch_list< |
| 305 | # ifdef GRN_WITH_SIMD_AVX512 |
| 306 | xsimd::avx512dq, |
| 307 | # endif |
| 308 | # ifdef GRN_WITH_SIMD_AVX2 |
| 309 | xsimd::avx2, |
| 310 | # endif |
| 311 | # ifdef GRN_WITH_SIMD_AVX |
| 312 | xsimd::avx, |
| 313 | # endif |
| 314 | # ifdef GRN_WITH_SIMD_NEON64 |
| 315 | xsimd::neon64, |
| 316 | # endif |
| 317 | xsimd::common>>(inner_product{}); |
| 318 | return dispatched(vector_raw1, vector_raw2, n_elements); |
| 319 | } |
| 320 | # endif |
| 321 | #endif |
| 322 | ElementType multiplication_sum = 0; |
| 323 | for (size_t i = 0; i < n_elements; ++i) { |
| 324 | multiplication_sum += vector_raw1[i] * vector_raw2[i]; |
| 325 | } |
| 326 | return 1 - multiplication_sum; |
| 327 | } |
| 328 | |
| 329 | template <typename ElementType> |
| 330 | float |
| 331 | compute_distance_cosine(grn_obj *vector1, grn_obj *vector2) |
| 332 | { |
| 333 | auto vector_raw1 = |
| 334 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector1)); |
| 335 | auto vector_raw2 = |
| 336 | reinterpret_cast<const ElementType *>(GRN_BULK_HEAD(vector2)); |
| 337 | auto n_elements = GRN_BULK_VSIZE(vector1) / sizeof(ElementType); |
| 338 | #ifdef GRN_WITH_SIMD |
| 339 | # ifdef GRN_WITH_SIMSIMD |
| 340 | if (use_simsimd && |
| 341 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 342 | # ifdef GRN_WITH_SIMD_AVX512 |
| 343 | if (simsimd::capabilities & simsimd_cap_x86_avx512_k) { |
| 344 | return simsimd::compute_distance_cosine_avx512(vector_raw1, |
| 345 | vector_raw2, |
| 346 | n_elements); |
| 347 | } |
| 348 | # endif |
| 349 | # ifdef GRN_WITH_SIMD_NEON64 |
| 350 | if (simsimd::capabilities & simsimd_cap_arm_neon_k) { |
| 351 | return simsimd::compute_distance_cosine_neon(vector_raw1, |
| 352 | vector_raw2, |
| 353 | n_elements); |
| 354 | } |
| 355 | # endif |
| 356 | return simsimd::compute_distance_cosine_serial(vector_raw1, |
| 357 | vector_raw2, |
| 358 | n_elements); |
| 359 | } |
| 360 | # endif |
| 361 | # ifdef GRN_WITH_XSIMD |
| 362 | if (use_xsimd && |
| 363 | (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) { |
| 364 | auto dispatched = xsimd::dispatch<xsimd::arch_list< |
| 365 | # ifdef GRN_WITH_SIMD_AVX512 |
| 366 | xsimd::avx512dq, |
| 367 | # endif |
| 368 | # ifdef GRN_WITH_SIMD_AVX2 |
| 369 | xsimd::avx2, |
| 370 | # endif |
| 371 | # ifdef GRN_WITH_SIMD_AVX |
| 372 | xsimd::avx, |
| 373 | # endif |
| 374 | # ifdef GRN_WITH_SIMD_NEON64 |
| 375 | xsimd::neon64, |
| 376 | # endif |
| 377 | xsimd::common>>(cosine{}); |
| 378 | return dispatched(vector_raw1, vector_raw2, n_elements); |
| 379 | } |
| 380 | # endif |
| 381 | #endif |
| 382 | ElementType inner_product = 0; |
| 383 | ElementType square_sum1 = 0; |
| 384 | ElementType square_sum2 = 0; |
| 385 | for (size_t i = 0; i < n_elements; ++i) { |
| 386 | ElementType value1 = vector_raw1[i]; |
| 387 | ElementType value2 = vector_raw2[i]; |
| 388 | inner_product += value1 * value2; |
| 389 | square_sum1 += value1 * value1; |
| 390 | square_sum2 += value2 * value2; |
| 391 | } |
| 392 | if (numeric::is_zero(inner_product)) { |
| 393 | return 1; |
| 394 | } else { |
| 395 | return 1 - (inner_product / |
| 396 | (std::sqrt(square_sum1) * std::sqrt(square_sum2))); |
| 397 | } |
| 398 | } |
| 399 | } // namespace distance |
| 400 | } // namespace grn |