Skip to content

Instantly share code, notes, and snippets.

@mdouze
Created March 18, 2020 06:49
Show Gist options
  • Save mdouze/95b4bb74dbb4c7e2387efc14486e23ba to your computer and use it in GitHub Desktop.
Save mdouze/95b4bb74dbb4c7e2387efc14486e23ba to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading faiss with AVX2 support.\n"
]
}
],
"source": [
"import faiss"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"fname = '....'\n",
"idx = faiss.read_index(fname)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Explore a loaded index"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.IndexPreTransform; proxy of <Swig Object of type 'faiss::IndexPreTransform *' at 0x7fc0c46a7ea0> >"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So it's an `IndexPreTransform` that wraps another index. To see what's inside, use `downcast_index`."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.IndexIVFScalarQuantizer; proxy of <Swig Object of type 'faiss::IndexIVFScalarQuantizer *' at 0x7fc0bc08c3c0> >"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(idx.index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An IVFScalarQuantizer. How many centroids? Is it trained? What does the quantizer look like? "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1048576"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(idx.index).nlist"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(idx.index).is_trained"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.Index; proxy of <Swig Object of type 'faiss::Index *' at 0x7fc0bc08c2d0> >"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(idx.index).quantizer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obviously it's an index. But what type of index? "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.IndexFlat; proxy of <Swig Object of type 'faiss::IndexFlat *' at 0x7fc0bc11eb10> >"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(faiss.downcast_index(idx.index).quantizer)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Moving to GPU"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"co = faiss.GpuMultipleClonerOptions()\n",
"co.shard = True\n",
"idx_gpu = faiss.index_cpu_to_all_gpus(idx, co)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.IndexPreTransform; proxy of <Swig Object of type 'faiss::IndexPreTransform *' at 0x7fc0bc0921e0> >"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idx_gpu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again behind a PreTransform (on CPU). Let's see what's inside that."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.IndexShards; proxy of <Swig Object of type 'faiss::IndexShardsTemplate< faiss::Index > *' at 0x7fc0bc08c540> >"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(idx_gpu.index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are the shards. What does each shard contain? "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.Index; proxy of <Swig Object of type 'faiss::Index *' at 0x7fc0bc0920c0> >"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(idx_gpu.index).at(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's downcast once more "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<faiss.swigfaiss_avx2.GpuIndexIVFScalarQuantizer; proxy of <Swig Object of type 'faiss::gpu::GpuIndexIVFScalarQuantizer *' at 0x7fc041d9dab0> >"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(faiss.downcast_index(idx_gpu.index).at(0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So here is the GPU Scalar Quantizer. How many inverted lists? "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1048576"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"faiss.downcast_index(faiss.downcast_index(idx_gpu.index).at(0)).nlist"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment