Skip to content

Commit

Permalink
Add: Benchmark for SciPy
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Oct 4, 2023
1 parent d8acf83 commit 5c65142
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 1 deletion.
177 changes: 177 additions & 0 deletions python/benchmark.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import simsimd as simd\n",
"from scipy.spatial.distance import cosine, sqeuclidean"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"count = 1000 # Common number of documents in a batch\n",
"ndim = 1536 # OpenAI Ada v2 embeddings API returns 1536-dim vectors\n",
"A = np.random.randn(count, ndim).astype(np.float32)\n",
"B = np.random.randn(count, ndim).astype(np.float32)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.35 ms ± 1.59 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -n 1 -r 10\n",
"result_np = [sqeuclidean(A[i], B[i]) for i in range(count)]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"418 µs ± 8.86 µs per loop (mean ± std. dev. of 10 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -n 1 -r 10\n",
"result_simd = simd.sqeuclidean(A, B)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"A = np.random.randn(count, ndim).astype(np.float16)\n",
"B = np.random.randn(count, ndim).astype(np.float16)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11.4 ms ± 387 µs per loop (mean ± std. dev. of 10 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -n 1 -r 10\n",
"result_np = [sqeuclidean(A[i], B[i]) for i in range(count)]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"442 µs ± 9.22 µs per loop (mean ± std. dev. of 10 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -n 1 -r 10\n",
"result_simd = simd.sqeuclidean(A, B)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"A = np.random.randint(-100, 100, (count, ndim), np.int8)\n",
"B = np.random.randint(-100, 100, (count, ndim), np.int8)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.41 ms ± 1.27 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -n 1 -r 10\n",
"result_np = [sqeuclidean(A[i], B[i]) for i in range(count)]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"241 µs ± 3.79 µs per loop (mean ± std. dev. of 10 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -n 1 -r 10\n",
"result_simd = simd.sqeuclidean(A, B)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_cosine(ndim, dtype):


@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float32])
@pytest.mark.parametrize("dtype", [np.float32, np.float16])
def test_batch(ndim, dtype):
"""Compares the simd.simd.sqeuclidean() function with scipy.spatial.distance.sqeuclidean() for a batch of vectors, measuring the accuracy error for f16, and f32 types."""

Expand Down

0 comments on commit 5c65142

Please sign in to comment.