-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8acf83
commit 5c65142
Showing
2 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters