Skip to content

Commit

Permalink
Merge pull request #222 from ashvardanian/main-tests
Browse files Browse the repository at this point in the history
Hot Patches for Dot-Products
  • Loading branch information
ashvardanian authored Nov 1, 2024
2 parents fd584c6 + 4fd3d24 commit 08e90aa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 37 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Pre-Release

on:
push:
branches: ["main-dev"]
branches: ["main-*"]
pull_request:
branches: ["main-dev"]
branches: ["main-*"]

env:
BUILD_TYPE: Release
Expand Down Expand Up @@ -43,6 +43,26 @@ jobs:
include/simsimd/simsimd.h:^#define SIMSIMD_VERSION_PATCH (\d+)
dry-run: "true"

test_c:
name: Test C
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- run: git submodule update --init --recursive

- name: Install dependencies
run: |
sudo apt update
sudo apt install -y cmake build-essential libjemalloc-dev libomp-dev libopenblas-dev
- name: Build locally
run: |
cmake -D CMAKE_BUILD_TYPE=Release -D SIMSIMD_BUILD_TESTS=1 -D SIMSIMD_BUILD_BENCHMARKS=1 -D SIMSIMD_BUILD_BENCHMARKS_WITH_CBLAS=1 -B build_release
cmake --build build_release --config Release
- name: Test
run: build_release/simsimd_test_run_time

test_python:
name: Test Python ${{ matrix.python-version }} on ${{ matrix.architecture }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
4 changes: 4 additions & 0 deletions include/simsimd/simsimd.h
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,10 @@ SIMSIMD_DYNAMIC int simsimd_uses_sierra(void);
* @note The dot product is zero if and only if the two vectors are orthogonal.
* @note Defined only for floating-point and integer data types.
*/
SIMSIMD_DYNAMIC void simsimd_dot_i8(simsimd_i8_t const *a, simsimd_i8_t const *b, simsimd_size_t n,
simsimd_distance_t *d);
SIMSIMD_DYNAMIC void simsimd_dot_u8(simsimd_u8_t const *a, simsimd_u8_t const *b, simsimd_size_t n,
simsimd_distance_t *d);
SIMSIMD_DYNAMIC void simsimd_dot_f16(simsimd_f16_t const *a, simsimd_f16_t const *b, simsimd_size_t n,
simsimd_distance_t *d);
SIMSIMD_DYNAMIC void simsimd_dot_bf16(simsimd_bf16_t const *a, simsimd_bf16_t const *b, simsimd_size_t n,
Expand Down
74 changes: 39 additions & 35 deletions scripts/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,59 +91,63 @@ void test_distance_from_itself(void) {
simsimd_f16_t f16s[1536];
simsimd_bf16_t bf16s[1536];
simsimd_i8_t i8s[1536];
simsimd_b8_t b8s[1536 / 8]; // 8 bits per word
simsimd_distance_t distance;
simsimd_u8_t u8s[1536];
simsimd_b8_t b8s[1536 / 8]; // 8 bits per word
simsimd_distance_t distance[2]; // For complex dot-products we need two values

// Cosine distance between two vectors
simsimd_cos_i8(i8s, i8s, 1536, &distance);
simsimd_cos_f16(f16s, f16s, 1536, &distance);
simsimd_cos_bf16(bf16s, bf16s, 1536, &distance);
simsimd_cos_f32(f32s, f32s, 1536, &distance);
simsimd_cos_f64(f64s, f64s, 1536, &distance);
simsimd_cos_i8(i8s, i8s, 1536, &distance[0]);
simsimd_cos_u8(u8s, u8s, 1536, &distance[0]);
simsimd_cos_f16(f16s, f16s, 1536, &distance[0]);
simsimd_cos_bf16(bf16s, bf16s, 1536, &distance[0]);
simsimd_cos_f32(f32s, f32s, 1536, &distance[0]);
simsimd_cos_f64(f64s, f64s, 1536, &distance[0]);

// Euclidean distance between two vectors
simsimd_l2sq_i8(i8s, i8s, 1536, &distance);
simsimd_l2sq_f16(f16s, f16s, 1536, &distance);
simsimd_l2sq_bf16(bf16s, bf16s, 1536, &distance);
simsimd_l2sq_f32(f32s, f32s, 1536, &distance);
simsimd_l2sq_f64(f64s, f64s, 1536, &distance);
simsimd_l2sq_i8(i8s, i8s, 1536, &distance[0]);
simsimd_l2sq_u8(u8s, u8s, 1536, &distance[0]);
simsimd_l2sq_f16(f16s, f16s, 1536, &distance[0]);
simsimd_l2sq_bf16(bf16s, bf16s, 1536, &distance[0]);
simsimd_l2sq_f32(f32s, f32s, 1536, &distance[0]);
simsimd_l2sq_f64(f64s, f64s, 1536, &distance[0]);

// Inner product between two vectors
simsimd_dot_f16(f16s, f16s, 1536, &distance);
simsimd_dot_bf16(bf16s, bf16s, 1536, &distance);
simsimd_dot_f32(f32s, f32s, 1536, &distance);
simsimd_dot_f64(f64s, f64s, 1536, &distance);
simsimd_dot_i8(i8s, i8s, 1536, &distance[0]);
simsimd_dot_u8(u8s, u8s, 1536, &distance[0]);
simsimd_dot_f16(f16s, f16s, 1536, &distance[0]);
simsimd_dot_bf16(bf16s, bf16s, 1536, &distance[0]);
simsimd_dot_f32(f32s, f32s, 1536, &distance[0]);
simsimd_dot_f64(f64s, f64s, 1536, &distance[0]);

// Complex inner product between two vectors
simsimd_dot_f16c(f16s, f16s, 1536, &distance);
simsimd_dot_bf16c(bf16s, bf16s, 1536, &distance);
// simsimd_dot_bf16c(bf16s, bf16s, 1536, &distance);
simsimd_dot_f32c(f32s, f32s, 1536, &distance);
simsimd_dot_f64c(f64s, f64s, 1536, &distance);
simsimd_dot_f16c(f16s, f16s, 1536, &distance[0]);
simsimd_dot_bf16c(bf16s, bf16s, 1536, &distance[0]);
simsimd_dot_f32c(f32s, f32s, 1536, &distance[0]);
simsimd_dot_f64c(f64s, f64s, 1536, &distance[0]);

// Complex conjugate inner product between two vectors
simsimd_vdot_f16c(f16s, f16s, 1536, &distance);
simsimd_vdot_bf16c(bf16s, bf16s, 1536, &distance);
simsimd_vdot_f32c(f32s, f32s, 1536, &distance);
simsimd_vdot_f64c(f64s, f64s, 1536, &distance);
simsimd_vdot_f16c(f16s, f16s, 1536, &distance[0]);
simsimd_vdot_bf16c(bf16s, bf16s, 1536, &distance[0]);
simsimd_vdot_f32c(f32s, f32s, 1536, &distance[0]);
simsimd_vdot_f64c(f64s, f64s, 1536, &distance[0]);

// Hamming distance between two vectors
simsimd_hamming_b8(b8s, b8s, 1536 / 8, &distance);
simsimd_hamming_b8(b8s, b8s, 1536 / 8, &distance[0]);

// Jaccard distance between two vectors
simsimd_jaccard_b8(b8s, b8s, 1536 / 8, &distance);
simsimd_jaccard_b8(b8s, b8s, 1536 / 8, &distance[0]);

// Jensen-Shannon divergence between two vectors
simsimd_js_f16(f16s, f16s, 1536, &distance);
simsimd_js_bf16(bf16s, bf16s, 1536, &distance);
simsimd_js_f32(f32s, f32s, 1536, &distance);
simsimd_js_f64(f64s, f64s, 1536, &distance);
simsimd_js_f16(f16s, f16s, 1536, &distance[0]);
simsimd_js_bf16(bf16s, bf16s, 1536, &distance[0]);
simsimd_js_f32(f32s, f32s, 1536, &distance[0]);
simsimd_js_f64(f64s, f64s, 1536, &distance[0]);

// Kullback-Leibler divergence between two vectors
simsimd_kl_f16(f16s, f16s, 1536, &distance);
simsimd_kl_bf16(bf16s, bf16s, 1536, &distance);
simsimd_kl_f32(f32s, f32s, 1536, &distance);
simsimd_kl_f64(f64s, f64s, 1536, &distance);
simsimd_kl_f16(f16s, f16s, 1536, &distance[0]);
simsimd_kl_bf16(bf16s, bf16s, 1536, &distance[0]);
simsimd_kl_f32(f32s, f32s, 1536, &distance[0]);
simsimd_kl_f64(f64s, f64s, 1536, &distance[0]);
}

int main(int argc, char **argv) {
Expand Down

0 comments on commit 08e90aa

Please sign in to comment.