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
<p>To save and reload Annoy indices from disk, we need to register a loading function into <b>knncolle</b>'s <code>load_prebuilt_registry()</code>. This is a little complicated as we must decide on which combinations of types and distances we want to deal with. In the example below, we only consider the obvious distance metrics and the defaults for the internal Annoy types, though more combinations could also be supported at the cost of larger binaries and longer compile times.</p>
<divclass="line"><spanclass="keywordflow">throw</span> std::runtime_error(<spanclass="stringliteral">"unexpected type for the Annoy index"</span>);</div>
<p>Helper function to define a <code><aclass="elRef" href="https://knncolle.github.io/knncolle/namespaceknncolle.html#a0574e57c9150cb0bfe1da0e7eff6aa01">knncolle::LoadPrebuiltFunction</a></code> for Annoy in <code><aclass="elRef" href="https://knncolle.github.io/knncolle/namespaceknncolle.html#a2157dec9e895be903278fc6903c793e0">knncolle::load_prebuilt_raw()</a></code>.</p>
262
-
<p>To load an Annoy index from disk, users are expected to define and register an Annoy-specific <code><aclass="elRef" href="https://knncolle.github.io/knncolle/namespaceknncolle.html#a0574e57c9150cb0bfe1da0e7eff6aa01">knncolle::LoadPrebuiltFunction</a></code>. In this function, users should call <code><aclass="el" href="#a9eff16d6db0a482607ada4d00b00c3d1">load_annoy_prebuilt_types()</a></code> to figure out the saved index's <code>AnnoyDistance_</code>, <code>AnnoyIndex</code> and <code>AnnoyData_</code>. Then, they should call <code><aclass="el" href="#a7869865e7f1417bd6fdcad52a7a66303">load_annoy_prebuilt()</a></code> with the appropriate types to return a pointer to a <code><aclass="elRef" href="https://knncolle.github.io/knncolle/classknncolle_1_1Prebuilt.html">knncolle::Prebuilt</a></code> object. This user-defined function should be registered in <code>load_prebuilt_registry()</code> with the key in <code>knncolle_annoy::save_name</code>.</p>
262
+
<p>To load an Annoy index from disk, users are expected to define and register an Annoy-specific <code><aclass="elRef" href="https://knncolle.github.io/knncolle/namespaceknncolle.html#a0574e57c9150cb0bfe1da0e7eff6aa01">knncolle::LoadPrebuiltFunction</a></code>. In this function, users should call <code><aclass="el" href="#a9eff16d6db0a482607ada4d00b00c3d1">load_annoy_prebuilt_types()</a></code> to figure out the saved index's <code>AnnoyDistance_</code>, <code>AnnoyIndex</code> and <code>AnnoyData_</code>. Then, they should call <code><aclass="el" href="#a7869865e7f1417bd6fdcad52a7a66303">load_annoy_prebuilt()</a></code> with the appropriate types to return a pointer to a <code><aclass="elRef" href="https://knncolle.github.io/knncolle/classknncolle_1_1Prebuilt.html">knncolle::Prebuilt</a></code> object. This user-defined function should be registered in <code>load_prebuilt_registry()</code> with the key in <code>knncolle_annoy::annoy_prebuilt_save_name</code>.</p>
263
263
<p>We do not define a default function for loading Annoy indices as there are too many possible combinations of types. Instead, the user is responsible for deciding which combinations of types should be handled. This avoids binary bloat from repeated instantiations of the Annoy template classes, if the user's application only deals with a certain subset of combinations.</p>
264
264
<p>For unknown types or distances, users can set <code><aclass="el" href="#a8525f55e793e3e117e404d324b519799">custom_save_for_annoy_index()</a></code>, <code><aclass="el" href="#a8c1f7d1200d1be4a1a3403bd9a0f91e1">custom_save_for_annoy_data()</a></code> and/or <code><aclass="el" href="#afbfa92fc904c2ed19bb8f51b50403881">custom_save_for_annoy_distance()</a></code>. Each custom function saves additional information about its type to disk during a <code><aclass="elRef" href="https://knncolle.github.io/knncolle/classknncolle_1_1Prebuilt.html#add21dc74e08a6ad495c6e241fa3d5968">knncolle::Prebuilt::save()</a></code> call. That information can then be parsed in the user-defined <code><aclass="elRef" href="https://knncolle.github.io/knncolle/namespaceknncolle.html#a0574e57c9150cb0bfe1da0e7eff6aa01">knncolle::LoadPrebuiltFunction</a></code> to recover an Annoy index with the appropriate template types.</p>
0 commit comments