Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} facebookresearch / faiss Public Notifications You must be signed in to change notification settings Fork 4.4k Star 40.3k Code Issues 222 Pull requests 57 Discussions Actions Projects Wiki Security and quality 0 Insights Additional navigation options Code Issues Pull requests Discussions Actions Projects Wiki Security and quality Insights FilesExpand file tree mainBreadcrumbsfaiss/tests/test_dealloc_invlists.cppCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory171 lines (128 loc) · 4.28 KB mainBreadcrumbsfaiss/tests/test_dealloc_invlists.cppCopy pathTopFile metadata and controlsCodeBlame171 lines (128 loc) · 4.28 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #include <cstdio>#include <cstdlib> #include <memory>#include <random>#include <vector> #include <gtest/gtest.h> #include <faiss/AutoTune.h>#include <faiss/IVFlib.h>#include <faiss/IndexIVF.h>#include <faiss/index_factory.h> using namespace faiss; namespace { // dimension of the vectors to indexint d = 32; // nb of training vectorssize_t nt = 5000; // size of the database points per window stepsize_t nb = 1000; // nb of queriessize_t nq = 200; std::mt19937 rng; std::vector<float> make_data(size_t n) { std::vector<float> database(n * d); std::uniform_real_distribution<> distrib; for (size_t i = 0; i < n * d; i++) { database[i] = distrib(rng); } return database;} std::unique_ptr<Index> make_trained_index(const char* index_type) { auto index = std::unique_ptr<Index>(index_factory(d, index_type)); auto xt = make_data(nt * d); index->train(nt, xt.data()); ParameterSpace().set_index_parameter(index.get(), "nprobe", 4); return index;} std::vector<idx_t> search_index(Index* index, const float* xq) { int k = 10; std::vector<idx_t> I(k * nq); std::vector<float> D(k * nq); index->search(nq, xq, k, D.data(), I.data()); return I;} /************************************************************* * Test functions for a given index type *************************************************************/ struct EncapsulateInvertedLists : InvertedLists { const InvertedLists* il; explicit EncapsulateInvertedLists(const InvertedLists* il_in) : InvertedLists(il_in->nlist, il_in->code_size), il(il_in) {} static void* memdup(const void* m, size_t size) { if (size == 0) { return nullptr; } return memcpy(malloc(size), m, size); } size_t list_size(size_t list_no) const override { return il->list_size(list_no); } const uint8_t* get_codes(size_t list_no) const override { return (uint8_t*)memdup( il->get_codes(list_no), list_size(list_no) * code_size); } const idx_t* get_ids(size_t list_no) const override { return (idx_t*)memdup( il->get_ids(list_no), list_size(list_no) * sizeof(idx_t)); } void release_codes(size_t, const uint8_t* codes) const override { free((void*)codes); } void release_ids(size_t, const idx_t* ids) const override { free((void*)ids); } const uint8_t* get_single_code(size_t list_no, size_t offset) const override { return (uint8_t*)memdup( il->get_single_code(list_no, offset), code_size); } size_t add_entries(size_t, size_t, const idx_t*, const uint8_t*) override { assert(false && "not implemented"); return 0; } void update_entries(size_t, size_t, size_t, const idx_t*, const uint8_t*) override { assert(false && "not implemented"); } void resize(size_t, size_t) override { assert(false && "not implemented"); } ~EncapsulateInvertedLists() override {}}; int test_dealloc_invlists(const char* index_key) { std::unique_ptr<Index> index = make_trained_index(index_key); IndexIVF* index_ivf = ivflib::extract_index_ivf(index.get()); auto xb = make_data(nb * d); index->add(nb, xb.data()); auto xq = make_data(nq * d); auto ref_res = search_index(index.get(), xq.data()); EncapsulateInvertedLists eil(index_ivf->invlists); index_ivf->own_invlists = false; index_ivf->replace_invlists(&eil, false); // TEST: this could crash or leak mem auto new_res = search_index(index.get(), xq.data()); // delete explicitly delete eil.il; // just to make sure EXPECT_EQ(ref_res, new_res); return 0;} } // anonymous namespace /************************************************************* * Test entry points *************************************************************/ TEST(TestIvlistDealloc, IVFFlat) { test_dealloc_invlists("IVF32,Flat");} TEST(TestIvlistDealloc, IVFSQ) { test_dealloc_invlists("IVF32,SQ8");} TEST(TestIvlistDealloc, IVFPQ) { test_dealloc_invlists("IVF32,PQ4np");} You can’t perform that action at this time.