{ "cells": [ { "cell_type": "code", "execution_count": 21, "id": "7da40592-4c80-4775-b965-e758f844393a", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "import faiss\n", "from faiss import write_index, read_index\n", "from datasets import load_dataset" ] }, { "cell_type": "code", "execution_count": 2, "id": "1049edf4-724e-48f2-95ba-45a957a37761", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Found cached dataset json (/home/jue@together.xyz/.cache/huggingface/datasets/json/default-708d018dd07be607/0.0.0/8bb11242116d547c741b2e8a1f18598ffdd40a1d4f2a2872c7a28b697434bc96)\n" ] } ], "source": [ "data = load_dataset('json', data_files='/var/cr01_data/danfu_data/rp_data_raw/wikipedia/wiki.jsonl', split='train')" ] }, { "cell_type": "code", "execution_count": 3, "id": "1ec75e11-566f-4568-8056-77909204453c", "metadata": {}, "outputs": [], "source": [ "embs = np.memmap('../indexed_data/wiki_embs.mmp', shape=(len(data), 768), dtype=np.float16, mode='r')" ] }, { "cell_type": "code", "execution_count": 6, "id": "ff2724af-1834-4560-9cb7-bf507f111404", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(29834171, 768)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "embs.shape" ] }, { "cell_type": "code", "execution_count": 7, "id": "2159bbfc-0339-4c45-bce9-6722ff70fb71", "metadata": {}, "outputs": [], "source": [ "m = 8\n", "ncells = 1000\n", "d = embs.shape[-1]\n", "quantizer = faiss.IndexFlatIP(d)\n", "index = faiss.IndexIVFPQ(quantizer, d, ncells, m, 8)" ] }, { "cell_type": "code", "execution_count": 8, "id": "7420e87e-e6cb-4071-ad35-0d962d4bf13f", "metadata": {}, "outputs": [], "source": [ "index.train(embs[0:10_000_000:10])" ] }, { "cell_type": "code", "execution_count": 9, "id": "f46987a6-3c20-4e6a-bc2d-3c7320da7fcf", "metadata": {}, "outputs": [], "source": [ "index.add(embs)" ] }, { "cell_type": "code", "execution_count": 19, "id": "e0cca6aa-c880-47b1-8b51-59e5d7bccf8c", "metadata": {}, "outputs": [], "source": [ "_, ids = index.search(embs[100][None], k=5)\n", "assert ids[0,0] == 100" ] }, { "cell_type": "code", "execution_count": 22, "id": "a5225c5f-79d0-4121-b858-575469e4c7b2", "metadata": {}, "outputs": [], "source": [ "write_index(index, '../indexed_data/wiki.index')" ] }, { "cell_type": "code", "execution_count": 23, "id": "b58c7f72-9127-4002-b803-874bb5e2d298", "metadata": {}, "outputs": [], "source": [ "index = read_index('../indexed_data/wiki.index')" ] }, { "cell_type": "code", "execution_count": 24, "id": "c93a91d6-883c-4c5f-8b34-52b6a8871d60", "metadata": {}, "outputs": [], "source": [ "_, ids = index.search(embs[100][None], k=5)\n", "assert ids[0,0] == 100" ] }, { "cell_type": "code", "execution_count": null, "id": "02a68288-f6e4-4fff-8975-d76bcffd26d1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "nebula-fav2", "language": "python", "name": "nebula-fav2" }, "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" } }, "nbformat": 4, "nbformat_minor": 5 }