Created
July 1, 2019 08:32
-
-
Save mdouze/bfa06e7dc0869f0c0495928aab25800f to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import faiss" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"8" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": { | |
"bento_obj_id": "139847443308032" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"faiss.get_num_gpus()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def my_index_cpu_to_gpu_multiple(resources, index, co=None, gpu_nos=None):\n", | |
" vres = faiss.GpuResourcesVector()\n", | |
" vdev = faiss.IntVector()\n", | |
" if gpu_nos is None: \n", | |
" gpu_nos = range(len(resources))\n", | |
" for i, res in zip(gpu_nos, resources):\n", | |
" vdev.push_back(i)\n", | |
" vres.push_back(res)\n", | |
" index = faiss.index_cpu_to_gpu_multiple(vres, vdev, index, co)\n", | |
" index.referenced_objects = resources\n", | |
" return index" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Test with 20k vectors " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"x = faiss.rand((20000, 512))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 136 ms, sys: 541 ms, total: 677 ms\n", | |
"Wall time: 581 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"gpu_nos = [1,2,3]\n", | |
"resources = [faiss.StandardGpuResources() for i in gpu_nos]\n", | |
"index_gpu = my_index_cpu_to_gpu_multiple(resources, faiss.IndexFlatL2(512), gpu_nos=gpu_nos)\n", | |
"index_gpu.add(x)\n", | |
"D, I = index_gpu.search(x, 128)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 71.5 ms, sys: 338 ms, total: 409 ms\n", | |
"Wall time: 408 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"gpu_nos = [2]\n", | |
"resources = [faiss.StandardGpuResources() for i in gpu_nos]\n", | |
"index_gpu = my_index_cpu_to_gpu_multiple(resources, faiss.IndexFlatL2(512), gpu_nos=gpu_nos)\n", | |
"index_gpu.add(x)\n", | |
"D, I = index_gpu.search(x, 128)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Test with 200k vectors " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"x = faiss.rand((200000, 512))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 3.7 s, sys: 2.55 s, total: 6.25 s\n", | |
"Wall time: 2.45 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"gpu_nos = [1,2,3]\n", | |
"resources = [faiss.StandardGpuResources() for i in gpu_nos]\n", | |
"index_gpu = my_index_cpu_to_gpu_multiple(resources, faiss.IndexFlatL2(512), gpu_nos=gpu_nos)\n", | |
"index_gpu.add(x)\n", | |
"D, I = index_gpu.search(x, 128)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 3.39 s, sys: 1.99 s, total: 5.38 s\n", | |
"Wall time: 5.37 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"gpu_nos = [2]\n", | |
"resources = [faiss.StandardGpuResources() for i in gpu_nos]\n", | |
"index_gpu = my_index_cpu_to_gpu_multiple(resources, faiss.IndexFlatL2(512), gpu_nos=gpu_nos)\n", | |
"index_gpu.add(x)\n", | |
"D, I = index_gpu.search(x, 128)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"bento_stylesheets": { | |
"bento/extensions/flow/main.css": true, | |
"bento/extensions/kernel_selector/main.css": true, | |
"bento/extensions/kernel_ui/main.css": true, | |
"bento/extensions/new_kernel/main.css": true, | |
"bento/extensions/system_usage/main.css": true, | |
"bento/extensions/theme/main.css": true | |
}, | |
"kernelspec": { | |
"display_name": "pytorch", | |
"language": "python", | |
"name": "bento_kernel_pytorch" | |
}, | |
"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.6.3rc1+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment