#ifndef DBMS_VECTOR_ONNX_INTERNAL_HPP
|
#define DBMS_VECTOR_ONNX_INTERNAL_HPP
|
|
/*
|
* DBMS_VECTOR local ONNX inference core (provider 'database').
|
*
|
* Wraps the ONNX Runtime C API. The module is compiled both with and without
|
* --with-onnxruntime: without the flag every call degrades gracefully (the
|
* provider dispatch in dbms_vector.cpp raises the deferred-inference error
|
* instead), so builds without the runtime keep working unchanged.
|
*/
|
|
#include <string>
|
#include <vector>
|
|
extern "C"
|
{
|
|
#include "postgres.h"
|
#include "utils/jsonb.h"
|
|
}
|
|
/// True when this server was built with the ONNX runtime (--with-onnxruntime).
|
bool dbms_vector_onnx_available();
|
|
/*
|
* Validate ONNX model bytes with the runtime before they are stored:
|
* the protobuf must parse (an OrtSession must initialize from the bytes) and
|
* the model must satisfy the embedding contract - a string input (the
|
* tokenizer fused into the graph, Oracle 23ai parity) and a float output.
|
* Input/output selection honors the Oracle metadata mapping
|
* {"input":{"<graph input>":["DATA"]},"embeddingOutput":"<graph output>"}.
|
* Raises ereport ERROR on any violation; a no-op without the runtime.
|
*/
|
void dbms_vector_onnx_validate(bytea *model_data, Jsonb *metadata);
|
|
/*
|
* Run the embedding model over a batch of texts. A fresh session is created
|
* per call (the one-shot path for unpinned models) and destroyed before
|
* returning or raising. Returns one vector text form "[%.8g,...]" per
|
* input.
|
*/
|
std::vector<std::string> dbms_vector_onnx_run(const bytea *model_data,
|
Jsonb *metadata,
|
const std::vector<std::string> &inputs);
|
|
/*
|
* Cache-aware run for the provider 'database' path: when the model is
|
* pinned in this backend's session cache with the same owner, name and
|
* content digest, the cached session runs the batch (no per-call session
|
* creation); otherwise the call degrades to the one-shot path. Cache
|
* entries older than the shared generation counter (bumped by
|
* DROP_ONNX_MODEL) are discarded first. Without the runtime this is
|
* never reached (the provider dispatch raises first).
|
*/
|
std::vector<std::string> dbms_vector_onnx_run_cached(
|
const char *owner, const char *model_name, const bytea *model_data,
|
Jsonb *metadata, const std::vector<std::string> &inputs);
|
|
/*
|
* INMEMORY_ONNX_MODEL: create the session of a stored model once and keep
|
* it in this backend's session cache for the lifetime of the backend (the
|
* pin). Raises when the runtime is absent (same flavor error as the
|
* provider dispatch). The params argument of the SQL surface is accepted
|
* and ignored.
|
*/
|
void dbms_vector_onnx_pin(const char *owner, const char *model_name,
|
const bytea *model_data, Jsonb *metadata);
|
|
/*
|
* Bump the cluster-wide model generation counter; every backend discards
|
* its cached sessions once it observes the new value. Called by
|
* DROP_ONNX_MODEL. Cheap and runtime-independent.
|
*/
|
void dbms_vector_onnx_bump_generation();
|
|
#endif // DBMS_VECTOR_ONNX_INTERNAL_HPP
|