UDF Usage
Caution: Custom distance metrics for IVF-flat search are experimental. They live under the
cuvs::neighbors::ivf_flat::experimental::udfnamespace and the associatedCUVS_METRICmacro. APIs and behavior may change without a major release.
What this feature does
You can supply your own CUDA device code that defines how distance accumulates between a query vector and database vectors inside the IVF-flat interleaved scan (the fine search over lists). Technical background on compilation and linking is in Link-time Optimization.
Available via C++ APIs
- IVF-flat — search (
search_params.metric_udf/CUVS_METRIC).
Requirements and tips
- Include
<cuvs/neighbors/ivf_flat.hpp>and define a metric withCUVS_METRIC(MyName, { ... }). Setsearch_params.metric_udfto the string returned byMyName_udf(). - Prefer the helpers when combining lanes so one body works for scalar and packed
int8_t/uint8_tas well as wider element types. - Custom UDF is not supported for fp16 (
__half/half) indices at this time; the headers enforce this with a static assertion when applicable. - The scan assumes ascending distance order for top-k selection; metrics that do not behave like a distance in that sense need careful validation.
- The first search with a new metric string may pay a one-time compilation cost; reuse the same string (and run a warmup) to benefit from the caches described in JIT Compilation.
Example
Helpers in CUVS_METRIC bodies
Inside CUVS_METRIC(MyName, { ... }) you write the body of operator()(AccT& acc, point_type x, point_type y). In scope: acc, x, y, template parameters T, AccT, Veclen, and the helpers below. The macro’s full argument list and notes live beside CUVS_METRIC in <cuvs/neighbors/ivf_flat.hpp>.
More examples: cpp/tests/neighbors/ann_ivf_flat/test_udf.cu.
Further reading
- C++ API reference: neighbors::ivf_flat
- JIT LTO architecture and IVF-flat fragments: Link-time Optimization