Title: I recently found myself computing the similarity between lots of very high dimensional vectors (i.e., sentence embedding vectors from LLMs), and I wanted to try some more powerful measures of similarity&#x2F;dependency than just Cosine similarity, which seems to be the default for everything nowadays because of its computational efficiency.<p>There are many other more involved measures that can detect more subtle relationships, but the problem is that some of them are quite slow to compute, especially if you&#x27;re trying to do it in Python. For my favorite measure of statistical dependency, Hoeffding&#x27;s D, that&#x27;s true even if you use Numpy. Since I recently learned Rust and wanted to learn how to make Python packages using Rust, I put together this new library that I call Fast Vector Similarity.<p>I was blown away by the performance of Rust and the quality of the tooling while making this. And even though it required a lot of fussing with Github Actions, I was also really impressed with just how easy it was to make a Python library using Rust that could be automatically compiled into wheels for every combination of platform (Linux, Windows, Mac) and Python Version (3.8 through 3.11) and uploaded to PyPi, all triggered by a commit to the repo and handled by Github&#x27;s servers-- and all for free if you&#x27;re working on a public repo!<p>Anyway, this library can easily be installed to try out using `pip install fast_vector_similarity`, and you can see some simple demo Python code in the readme to show how to use it.<p>Aside from exposing some very high performance implementations of some very nice similarity measures, I also included the ability to get robust estimates of these measures using the Bootstrap method. Basically, if you have two very high dimensional vectors, instead of using the entire vector to measure similarity, you can take the same random subset of indices from both vectors and compute the similarity of just those elements. Then you repeat the process hundreds or thousands of times and look at the robust average (i.e., throw away the results outside the 25th percentile to 75th percentile and average the remaining ones, to reduce the impact of outliers) and standard deviation of the results. Obviously this is very demanding of performance, but it&#x27;s still reasonable if you&#x27;re not trying to compute it for too many vectors.<p>Everything is designed to fully saturate the performance of multi-core machines by extensive use of broadcasting&#x2F;vectorization and the use of paralell processing via the Rayon library. I was really impressed with how easy and low-overhead it is to make highly parallelized code in Rust, especially compared to coming from Python, where you have to jump through a lot of hoops to use multiprocessing and there is a ton of overhead.<p>Anyway, please let me know what you think. I&#x27;m looking to add more measures of similarity if I can find ones that can be efficiently computed (I already gave up on including HSIC because I couldn&#x27;t get it to go fast enough, even using BLAS&#x2F;LAPACK).
Upvote: