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_pq_encoding.cppCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory145 lines (123 loc) · 4.14 KB mainBreadcrumbsfaiss/tests/test_pq_encoding.cppCopy pathTopFile metadata and controlsCodeBlame145 lines (123 loc) · 4.14 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145/* * 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 <iostream>#include <memory>#include <vector> #include <gtest/gtest.h> #include <faiss/IndexPQFastScan.h>#include <faiss/impl/ProductQuantizer.h>#include <faiss/impl/fast_scan/fast_scan.h> namespace { const std::vector<uint64_t> random_vector(size_t s) { std::vector<uint64_t> v(s, 0); for (size_t i = 0; i < s; ++i) { v[i] = rand(); } return v;} const std::vector<float> random_vector_float(size_t s) { std::vector<float> v(s, 0); for (size_t i = 0; i < s; ++i) { v[i] = rand(); } return v;} } // namespace TEST(PQEncoderGeneric, encode) { const int nsubcodes = 97; const int minbits = 1; const int maxbits = 24; const std::vector<uint64_t> values = random_vector(nsubcodes); for (int nbits = minbits; nbits <= maxbits; ++nbits) { std::cerr << "nbits = " << nbits << std::endl; const uint64_t mask = (1ull << nbits) - 1; std::unique_ptr<uint8_t[]> codes( new uint8_t[(nsubcodes * maxbits + 7) / 8]); // NOTE(hoss): Necessary scope to ensure trailing bits are flushed to // mem. { faiss::PQEncoderGeneric encoder(codes.get(), nbits); for (const auto& v : values) { encoder.encode(v & mask); } } faiss::PQDecoderGeneric decoder(codes.get(), nbits); for (int i = 0; i < nsubcodes; ++i) { uint64_t v = decoder.decode(); EXPECT_EQ(values[i] & mask, v); } }} TEST(PQEncoder8, encode) { const int nsubcodes = 100; const std::vector<uint64_t> values = random_vector(nsubcodes); const uint64_t mask = 0xFF; std::unique_ptr<uint8_t[]> codes(new uint8_t[nsubcodes]); faiss::PQEncoder8 encoder(codes.get(), 8); for (const auto& v : values) { encoder.encode(v & mask); } faiss::PQDecoder8 decoder(codes.get(), 8); for (int i = 0; i < nsubcodes; ++i) { uint64_t v = decoder.decode(); EXPECT_EQ(values[i] & mask, v); }} TEST(PQEncoder16, encode) { const int nsubcodes = 100; const std::vector<uint64_t> values = random_vector(nsubcodes); const uint64_t mask = 0xFFFF; std::unique_ptr<uint8_t[]> codes(new uint8_t[2 * nsubcodes]); faiss::PQEncoder16 encoder(codes.get(), 16); for (const auto& v : values) { encoder.encode(v & mask); } faiss::PQDecoder16 decoder(codes.get(), 16); for (int i = 0; i < nsubcodes; ++i) { uint64_t v = decoder.decode(); EXPECT_EQ(values[i] & mask, v); }} TEST(PQFastScan, set_packed_element) { int d = 20, ntotal = 1000, M = 5, nbits = 4; const std::vector<float> ds = random_vector_float(ntotal * d); faiss::IndexPQFastScan index(d, M, nbits); index.train(ntotal, ds.data()); index.add(ntotal, ds.data()); for (int j = 0; j < 10; j++) { int vector_id = rand() % ntotal; std::vector<uint8_t> old(ntotal * M); std::vector<uint8_t> code(M); for (int i = 0; i < ntotal; i++) { for (int sq = 0; sq < M; sq++) { old[i * M + sq] = faiss::pq4_get_packed_element( index.codes.data(), index.bbs, M, i, sq); } } for (int sq = 0; sq < M; sq++) { faiss::pq4_set_packed_element( index.codes.data(), ((old[vector_id * M + sq] + 3) % 16), index.bbs, M, vector_id, sq); } for (int i = 0; i < ntotal; i++) { for (int sq = 0; sq < M; sq++) { uint8_t newcode = faiss::pq4_get_packed_element( index.codes.data(), index.bbs, M, i, sq); uint8_t oldcode = old[i * M + sq]; if (i == vector_id) { EXPECT_EQ(newcode, (oldcode + 3) % 16); } else { EXPECT_EQ(newcode, oldcode); } } } }} You can’t perform that action at this time.