pg_onnx
1.0.0
กราฟ LR
Subgraph P [PostgreSQL]
ทิศทาง LR
PM ((Postmaster))
PS1 [Postgres]
PS2 [postgres]
PS3 [postgres]
ตาราง [(ข้อมูลโมเดล ONNX)]]
subgraph onnx [PG_ONNX Working Worker]
ทิศทางวัณโรค
OS [OnnxRuntime-Server]
onnx1 ([onnxruntime nsession: nmodel1]))
onnx2 ([onnxruntime nsession: nmodel2])
OS -. "สร้าง/ดำเนินการเซสชัน".-O ONNX1 & ONNX2
จบ
PM -. "Fork".-O PS1 & PS2 & PS3
PM -. "ผู้ปฏิบัติงานพื้นหลังแบบไดนามิก". -o onnx
PS3 <-"การนำเข้าโมเดล"-> ตาราง
PS1 & PS2 & PS3 <-"ONNX Operation"-> onnx
จบ
c [ไคลเอนต์พยายามใช้ pg_onnx] ==> ps3
download-onnxruntime-linux.sh/usr/local/onnxruntime/usr/local/onnxruntime/lib ถึง /etc/ld.so.conf.d/onnxruntime.conf และเรียกใช้ ldconfigbrew install onnxruntimesudo apt install cmake libboost-all-dev libpq-dev postgresql-server-dev-allsudo apt install cuda-toolkit-12 libcudnn9-dev-cuda-12brew install cmake boost postgresqlgit submodule update --init --recursive เพื่ออัปเดต submodules git clone --recursive https://github.com/kibae/pg_onnx.gitcmake -B build -S . -DCMAKE_BUILD_TYPE=Release
cmake --build build --target pg_onnx --parallel
sudo cmake --install build/pg_onnxCREATE EXTENSION IF NOT EXISTS pg_onnx; SELECT pg_onnx_import_model(
' sample_model ' , -- ------------- model name
' v20230101 ' , -- ---------------- model version
PG_READ_BINARY_FILE( ' /your_model_path/model.onnx ' ):: bytea , -- model binary data
' {"cuda": true} ' ::jsonb, -- ---- options
' sample model ' -- -------------- description
);
SELECT pg_onnx_execute_session(
' sample_model ' , -- model name
' v20230101 ' , -- --- model version
' {
"x": [[1], [2], [3]],
"y": [[3], [4], [5]],
"z": [[5], [6], [7]]
} ' -- ------------- inputs
); pg_onnx_execute
--------------------------------------------------------------------------------
{"output": [[0.7488641738891602], [0.8607008457183838], [0.9725375175476074]]}
-- Create a test table
CREATE TABLE trigger_test
(
id SERIAL PRIMARY KEY ,
value1 INT ,
value2 INT ,
value3 INT ,
prediction FLOAT
);
-- Create a trigger function
CREATE OR REPLACE FUNCTION trigger_test_insert ()
RETURNS TRIGGER AS
$$
DECLARE
result jsonb;
BEGIN
result : = pg_onnx_execute_session(
' sample_model ' , ' v20230101 ' ,
JSONB_BUILD_OBJECT(
' x ' , ARRAY [[ NEW . value1 ]],
' y ' , ARRAY [[ NEW . value2 ]],
' z ' , ARRAY [[ NEW . value3 ]]));
-- output shape: float[-1,1]
-- eg: {"output": [[0.6492120623588562]]}
NEW . prediction : = result - > ' output ' - > 0 - > 0 ;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
-- Create a trigger
CREATE TRIGGER trigger_test_insert
BEFORE INSERT
ON trigger_test
FOR EACH ROW
EXECUTE PROCEDURE trigger_test_insert();