Skip to content

Commit b2d6285

Browse files
committed
Initial implementation of SVS Runtime package
1 parent b3558f0 commit b2d6285

12 files changed

+1714
-0
lines changed

bindings/cpp/CMakeLists.txt

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Copyright (C) 2023 Intel Corporation
2+
#
3+
# This software and the related documents are Intel copyrighted materials,
4+
# and your use of them is governed by the express license under which they
5+
# were provided to you ("License"). Unless the License provides otherwise,
6+
# you may not use, modify, copy, publish, distribute, disclose or transmit
7+
# this software or the related documents without Intel's prior written
8+
# permission.
9+
#
10+
# This software and the related documents are provided as is, with no
11+
# express or implied warranties, other than those that are expressly stated
12+
# in the License.
13+
14+
cmake_minimum_required(VERSION 3.21)
15+
project(svs_runtime VERSION 0.0.10 LANGUAGES CXX)
16+
set(TARGET_NAME svs_runtime)
17+
18+
set(SVS_RUNTIME_HEADERS
19+
include/IndexSVSImplDefs.h
20+
include/IndexSVSFlatImpl.h
21+
include/IndexSVSVamanaImpl.h
22+
)
23+
24+
set(SVS_RUNTIME_SOURCES
25+
src/IndexSVSImplUtils.h
26+
src/IndexSVSFlatImpl.cpp
27+
src/IndexSVSVamanaImpl.cpp
28+
)
29+
30+
option(SVS_RUNTIME_ENABLE_LVQ "Enable compilation of SVS runtime with LVQ support" ON)
31+
if (SVS_RUNTIME_ENABLE_LVQ)
32+
message(STATUS "SVS runtime will be built with LVQ support")
33+
list(APPEND SVS_RUNTIME_HEADERS
34+
include/IndexSVSVamanaLVQImpl.h
35+
)
36+
list(APPEND SVS_RUNTIME_SOURCES
37+
src/IndexSVSVamanaLVQImpl.cpp
38+
)
39+
else()
40+
message(STATUS "SVS runtime will be built without LVQ support")
41+
endif()
42+
43+
option(SVS_RUNTIME_ENABLE_LEANVEC "Enable compilation of SVS runtime with LeanVec support" ON)
44+
if (SVS_RUNTIME_ENABLE_LEANVEC)
45+
message(STATUS "SVS runtime will be built with LeanVec support")
46+
list(APPEND SVS_RUNTIME_HEADERS
47+
include/IndexSVSVamanaLeanVecImpl.h
48+
)
49+
list(APPEND SVS_RUNTIME_SOURCES
50+
src/IndexSVSVamanaLeanVecImpl.cpp
51+
)
52+
else()
53+
message(STATUS "SVS runtime will be built without LeanVec support")
54+
endif()
55+
56+
find_package(OpenMP REQUIRED)
57+
58+
add_library(${TARGET_NAME} SHARED
59+
${SVS_RUNTIME_HEADERS}
60+
${SVS_RUNTIME_SOURCES}
61+
)
62+
63+
target_link_libraries(${TARGET_NAME} PUBLIC
64+
OpenMP::OpenMP_CXX
65+
)
66+
67+
target_include_directories(${TARGET_NAME} PRIVATE
68+
${CMAKE_CURRENT_SOURCE_DIR}/include
69+
)
70+
71+
target_compile_options(${TARGET_NAME} PRIVATE
72+
-DSVS_ENABLE_OMP=1
73+
-fvisibility=hidden
74+
)
75+
76+
target_compile_features(${TARGET_NAME} INTERFACE cxx_std_20)
77+
set_target_properties(${TARGET_NAME} PROPERTIES PUBLIC_HEADER "${SVS_RUNTIME_HEADERS}")
78+
set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD 20)
79+
set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON)
80+
set_target_properties(${TARGET_NAME} PROPERTIES CXX_EXTENSIONS OFF)
81+
set_target_properties(${TARGET_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} )
82+
83+
if ((SVS_RUNTIME_ENABLE_LVQ OR SVS_RUNTIME_ENABLE_LEANVEC))
84+
if(TARGET svs::svs_static_library)
85+
target_link_libraries(${TARGET_NAME} PRIVATE
86+
svs::svs
87+
svs::svs_static_library
88+
svs_compile_options
89+
svs_x86_options_base
90+
)
91+
else()
92+
set(SVS_URL "https://github.com/intel/ScalableVectorSearch/releases/download/v1.0.0-dev/svs-shared-library-1.0.0-NIGHTLY-20251017-faiss.tar.gz"
93+
CACHE STRING "URL to download SVS shared library")
94+
include(FetchContent)
95+
FetchContent_Declare(
96+
svs
97+
URL ${SVS_URL}
98+
)
99+
FetchContent_MakeAvailable(svs)
100+
list(APPEND CMAKE_PREFIX_PATH "${svs_SOURCE_DIR}")
101+
find_package(svs REQUIRED)
102+
target_link_libraries(${TARGET_NAME} PRIVATE
103+
svs::svs
104+
svs::svs_compile_options
105+
svs::svs_static_library
106+
)
107+
endif()
108+
else()
109+
# Include the SVS library directly if needed.
110+
if (NOT TARGET svs::svs)
111+
add_subdirectory("../.." "${CMAKE_CURRENT_BINARY_DIR}/svs")
112+
endif()
113+
target_link_libraries(${TARGET_NAME} PRIVATE
114+
svs::svs
115+
svs_compile_options
116+
svs_x86_options_base
117+
)
118+
endif()
119+
120+
# installing
121+
include(GNUInstallDirs)
122+
123+
set(SVS_RUNTIME_EXPORT_NAME ${TARGET_NAME})
124+
set(VERSION_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/${SVS_RUNTIME_EXPORT_NAME}ConfigVersion.cmake")
125+
set(SVS_RUNTIME_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/svs_runtime)
126+
set(SVS_RUNTIME_COMPONENT_NAME "Runtime")
127+
128+
install(TARGETS ${TARGET_NAME}
129+
EXPORT ${SVS_RUNTIME_EXPORT_NAME}
130+
COMPONENT ${SVS_RUNTIME_COMPONENT_NAME}
131+
LIBRARY DESTINATION lib
132+
PUBLIC_HEADER DESTINATION include/svs/runtime
133+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
134+
)
135+
136+
install(DIRECTORY include/svs/runtime
137+
COMPONENT ${SVS_RUNTIME_COMPONENT_NAME}
138+
DESTINATION include/svs
139+
FILES_MATCHING PATTERN "*.h"
140+
)
141+
142+
install(EXPORT ${SVS_RUNTIME_EXPORT_NAME}
143+
COMPONENT ${SVS_RUNTIME_COMPONENT_NAME}
144+
NAMESPACE svs::
145+
DESTINATION ${SVS_RUNTIME_CONFIG_INSTALL_DIR}
146+
)
147+
148+
include(CMakePackageConfigHelpers)
149+
configure_package_config_file(
150+
"${CMAKE_CURRENT_LIST_DIR}/runtimeConfig.cmake.in"
151+
"${CMAKE_CURRENT_BINARY_DIR}/${SVS_RUNTIME_EXPORT_NAME}Config.cmake"
152+
INSTALL_DESTINATION "${SVS_RUNTIME_CONFIG_INSTALL_DIR}"
153+
)
154+
155+
# Don't make compatibility guarantees until we reach a compatibility milestone.
156+
write_basic_package_version_file(
157+
${VERSION_CONFIG}
158+
VERSION ${PROJECT_VERSION}
159+
COMPATIBILITY ExactVersion
160+
)
161+
162+
install(FILES
163+
"${CMAKE_CURRENT_BINARY_DIR}/${SVS_RUNTIME_EXPORT_NAME}Config.cmake"
164+
"${VERSION_CONFIG}"
165+
COMPONENT ${SVS_RUNTIME_COMPONENT_NAME}
166+
DESTINATION "${SVS_RUNTIME_CONFIG_INSTALL_DIR}"
167+
)
168+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2025 Intel Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "IndexSVSImplDefs.h"
18+
#include <istream>
19+
#include <memory>
20+
#include <ostream>
21+
22+
namespace svs {
23+
class Flat;
24+
25+
namespace runtime {
26+
class SVS_RUNTIME_API IndexSVSFlatImpl {
27+
public:
28+
static IndexSVSFlatImpl* build(size_t dim, MetricType metric) noexcept;
29+
static void destroy(IndexSVSFlatImpl* impl) noexcept;
30+
31+
Status add(size_t n, const float* x) noexcept;
32+
void reset() noexcept;
33+
34+
Status search(
35+
size_t n, const float* x, size_t k, float* distances, size_t* labels
36+
) const noexcept;
37+
38+
Status serialize(std::ostream& out) const noexcept;
39+
40+
Status deserialize(std::istream& in) noexcept;
41+
42+
private:
43+
IndexSVSFlatImpl(size_t dim, MetricType metric);
44+
~IndexSVSFlatImpl();
45+
Status init_impl(size_t n, const float* x) noexcept;
46+
47+
MetricType metric_type_;
48+
size_t dim_;
49+
std::unique_ptr<svs::Flat> impl{nullptr};
50+
};
51+
} // namespace runtime
52+
} // namespace svs
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2025 Intel Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <cstdint>
20+
#include <span>
21+
22+
#ifdef svs_runtime_EXPORTS
23+
#define SVS_RUNTIME_API __attribute__((visibility("default")))
24+
#define SVS_RUNTIME_API_INTERFACE // reserved for future use
25+
#else
26+
#define SVS_RUNTIME_API
27+
#define SVS_RUNTIME_API_INTERFACE // reserved for future use
28+
#endif
29+
30+
namespace svs {
31+
namespace runtime {
32+
enum class MetricType { L2, INNER_PRODUCT };
33+
34+
enum class ErrorCode {
35+
SUCCESS = 0,
36+
UNKNOWN_ERROR = 1,
37+
INVALID_ARGUMENT = 2,
38+
NOT_IMPLEMENTED = 3,
39+
NOT_INITIALIZED = 4
40+
};
41+
42+
struct Status {
43+
ErrorCode code = ErrorCode::SUCCESS;
44+
const char* message = nullptr;
45+
constexpr bool ok() const { return code == ErrorCode::SUCCESS; }
46+
};
47+
48+
constexpr Status Status_Ok{ErrorCode::SUCCESS, nullptr};
49+
50+
struct SVS_RUNTIME_API_INTERFACE IDFilter {
51+
virtual bool is_member(size_t id) const = 0;
52+
virtual ~IDFilter() = default;
53+
54+
// Helper method to allow using IDFilter instances as callable objects
55+
bool operator()(size_t id) const { return this->is_member(id); }
56+
};
57+
58+
struct SearchResultsStorage {
59+
std::span<int64_t> labels; // faiss::idx_t is int64_t
60+
std::span<float> distances;
61+
};
62+
63+
struct SVS_RUNTIME_API_INTERFACE ResultsAllocator {
64+
virtual SearchResultsStorage allocate(std::span<size_t> result_counts) const = 0;
65+
virtual ~ResultsAllocator() = default;
66+
67+
// Helper method to allow using ResultsAllocator instances as callable objects
68+
SearchResultsStorage operator()(std::span<size_t> result_counts) const {
69+
return this->allocate(result_counts);
70+
}
71+
};
72+
73+
} // namespace runtime
74+
} // namespace svs

0 commit comments

Comments
 (0)