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_ivfpq_codec.cppCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory85 lines (65 loc) · 2.22 KB mainBreadcrumbsfaiss/tests/test_ivfpq_codec.cppCopy pathTopFile metadata and controlsCodeBlame85 lines (65 loc) · 2.22 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485/* * 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 <random> #include <omp.h> #include <gtest/gtest.h> #include <faiss/IndexFlat.h>#include <faiss/IndexIVFPQ.h>#include <faiss/utils/distances.h> namespace { // dimension of the vectors to indexint d = 64; // size of the database we plan to indexsize_t nb = 8000; double eval_codec_error(long ncentroids, long m, const std::vector<float>& v) { faiss::IndexFlatL2 coarse_quantizer(d); faiss::IndexIVFPQ index(&coarse_quantizer, d, ncentroids, m, 8); index.pq.cp.niter = 10; // speed up train index.train(nb, v.data()); // encode and decode to compute reconstruction error std::vector<faiss::idx_t> keys(nb); std::vector<uint8_t> codes(nb * m); index.encode_multiple(nb, keys.data(), v.data(), codes.data(), true); std::vector<float> v2(nb * d); index.decode_multiple(nb, keys.data(), codes.data(), v2.data()); return faiss::fvec_L2sqr(v.data(), v2.data(), nb * d);} } // namespace bool runs_on_sandcastle() { // see discussion here https://fburl.com/qc5kpdo2 const char* sandcastle = getenv("SANDCASTLE"); if (sandcastle && !strcmp(sandcastle, "1")) { return true; } const char* tw_job_user = getenv("TW_JOB_USER"); if (tw_job_user && !strcmp(tw_job_user, "sandcastle")) { return true; } return false;} TEST(IVFPQ, codec) { std::vector<float> database(nb * d); std::mt19937 rng; std::uniform_real_distribution<> distrib; for (size_t i = 0; i < nb * d; i++) { database[i] = distrib(rng); } // limit number of threads when running on heavily parallelized test // environment if (runs_on_sandcastle()) { omp_set_num_threads(2); } double err0 = eval_codec_error(16, 8, database); // should be more accurate as there are more coarse centroids double err1 = eval_codec_error(128, 8, database); EXPECT_GT(err0, err1); // should be more accurate as there are more PQ codes double err2 = eval_codec_error(16, 16, database); EXPECT_GT(err0, err2);} You can’t perform that action at this time.