ruvector/crates/ruvector-postgres/docs/GNN_QUICK_REFERENCE.md
rUv 073ce73612
feat(postgres): Add 53 SQL function definitions for all advanced modules (#46)
* feat(postgres): Add 7 advanced AI modules to ruvector-postgres

Comprehensive implementation of advanced AI capabilities:

## New Modules (23,541 lines of code)

### 1. Self-Learning / ReasoningBank (`src/learning/`)
- Trajectory tracking for query optimization
- Pattern extraction using K-means clustering
- ReasoningBank for pattern storage and matching
- Adaptive search parameter optimization

### 2. Attention Mechanisms (`src/attention/`)
- Scaled dot-product attention (core)
- Multi-head attention with parallel heads
- Flash Attention v2 (memory-efficient)
- 10 attention types with PostgresEnum support

### 3. GNN Layers (`src/gnn/`)
- Message passing framework
- GCN (Graph Convolutional Network)
- GraphSAGE with mean/max aggregation
- Configurable aggregation methods

### 4. Hyperbolic Embeddings (`src/hyperbolic/`)
- Poincaré ball model
- Lorentz hyperboloid model
- Hyperbolic distance metrics
- Möbius operations

### 5. Sparse Vectors (`src/sparse/`)
- COO format sparse vector type
- Efficient sparse-sparse distance functions
- BM25/SPLADE compatible
- Top-k pruning operations

### 6. Graph Operations & Cypher (`src/graph/`)
- Property graph storage (nodes/edges)
- BFS, DFS, Dijkstra traversal
- Cypher query parser (AST-based)
- Query executor with pattern matching

### 7. Tiny Dancer Routing (`src/routing/`)
- FastGRNN neural network
- Agent registry with capabilities
- Multi-objective routing optimization
- Cost/latency/quality balancing

## Docker Infrastructure
- Dockerfile with pgrx 0.12.6 and PostgreSQL 16
- docker-compose.yml with test runner
- Initialization SQL with test tables
- Shell scripts for dev/test/benchmark

## Feature Flags
- `learning`, `attention`, `gnn`, `hyperbolic`
- `sparse`, `graph`, `routing`
- `ai-complete` and `graph-complete` bundles

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(docker): Copy entire workspace for pgrx build

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(docker): Build standalone crate without workspace

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: Update README to enhance clarity and structure

* fix(postgres): Resolve compilation errors and Docker build issues

- Fix simsimd Option/Result type mismatch in scaled_dot.rs
- Fix f32/f64 type conversions in poincare.rs and lorentz.rs
- Fix AVX512 missing wrapper functions by using AVX2 fallback
- Fix Vec<Vec<f32>> to JsonB for pgrx pg_extern compatibility
- Fix DashMap get() to get_mut() for mutable access
- Fix router.rs dereference for best_score comparison
- Update Dockerfile to copy pre-written SQL file for pgrx
- Simplify init.sql to use correct function names
- Add postgres-cli npm package for CLI tooling

All changes tested successfully in Docker with:
- Extension loads with AVX2 SIMD support (8 floats/op)
- Distance functions verified working
- PostgreSQL 16 container runs successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: Add ruvLLM examples and enhanced postgres-cli

Added from claude/ruvector-lfm2-llm-01YS5Tc7i64PyYCLecT9L1dN branch:
- examples/ruvLLM: Complete LLM inference system with SIMD optimization
  - Pretraining, benchmarking, and optimization system
  - Real SIMD-optimized CPU inference engine
  - Comprehensive SOTA benchmark suite
  - Attention mechanisms, memory management, router

Enhanced postgres-cli with full ruvector-postgres integration:
- Sparse vector operations (BM25, top-k, prune, conversions)
- Hyperbolic geometry (Poincare, Lorentz, Mobius operations)
- Agent routing (Tiny Dancer system)
- Vector quantization (binary, scalar, product)
- Enhanced graph and learning commands

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(postgres-cli): Use native ruvector type instead of pgvector

- Change createVectorTable to use ruvector type (native RuVector extension)
- Add dimensions column for metadata since ruvector is variable-length
- Update index creation to use simple btree (HNSW/IVFFlat TBD)
- Tested against Docker container with ruvector extension

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(postgres): Add 53 SQL function definitions for all advanced modules

Enable all advanced PostgreSQL extension functions by adding their SQL
definitions to the extension file. This exposes all Rust #[pg_extern]
functions to PostgreSQL.

## New SQL Functions (53 total)

### Hyperbolic Geometry (8 functions)
- ruvector_poincare_distance, ruvector_lorentz_distance
- ruvector_mobius_add, ruvector_exp_map, ruvector_log_map
- ruvector_poincare_to_lorentz, ruvector_lorentz_to_poincare
- ruvector_minkowski_dot

### Sparse Vectors (14 functions)
- ruvector_sparse_create, ruvector_sparse_from_dense
- ruvector_sparse_dot, ruvector_sparse_cosine, ruvector_sparse_l2_distance
- ruvector_sparse_add, ruvector_sparse_scale, ruvector_sparse_to_dense
- ruvector_sparse_nnz, ruvector_sparse_dim
- ruvector_bm25_score, ruvector_tf_idf, ruvector_sparse_normalize
- ruvector_sparse_topk

### GNN - Graph Neural Networks (5 functions)
- ruvector_gnn_gcn_layer, ruvector_gnn_graphsage_layer
- ruvector_gnn_gat_layer, ruvector_gnn_message_pass
- ruvector_gnn_aggregate

### Routing/Agents - "Tiny Dancer" (11 functions)
- ruvector_route_query, ruvector_route_with_context
- ruvector_calculate_agent_affinity, ruvector_select_best_agent
- ruvector_multi_agent_route, ruvector_create_agent_embedding
- ruvector_get_routing_stats, ruvector_register_agent
- ruvector_update_agent_performance, ruvector_adaptive_route
- ruvector_fastgrnn_forward

### Learning/ReasoningBank (7 functions)
- ruvector_record_trajectory, ruvector_get_verdict
- ruvector_distill_memory, ruvector_adaptive_search
- ruvector_learning_feedback, ruvector_get_learning_patterns
- ruvector_optimize_search_params

### Graph/Cypher (8 functions)
- ruvector_graph_create_node, ruvector_graph_create_edge
- ruvector_graph_get_neighbors, ruvector_graph_shortest_path
- ruvector_graph_pagerank, ruvector_cypher_query
- ruvector_graph_traverse, ruvector_graph_similarity_search

## CLI Updates
- Enabled hyperbolic geometry commands in postgres-cli
- Added vector distance and normalize commands
- Enhanced client with connection pooling and retry logic

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 22:49:29 -05:00

7.5 KiB

GNN Quick Reference Guide

SQL Functions

1. GCN Forward Pass

ruvector_gcn_forward(
    embeddings FLOAT[][],  -- Node embeddings [num_nodes x in_dim]
    src INT[],             -- Source node indices
    dst INT[],             -- Destination node indices
    weights FLOAT[],       -- Edge weights (optional)
    out_dim INT            -- Output dimension
) RETURNS FLOAT[][]        -- Updated embeddings [num_nodes x out_dim]

Example:

SELECT ruvector_gcn_forward(
    ARRAY[ARRAY[1.0, 2.0], ARRAY[3.0, 4.0]],
    ARRAY[0],
    ARRAY[1],
    NULL,
    8
);

2. GNN Aggregation

ruvector_gnn_aggregate(
    messages FLOAT[][],    -- Neighbor messages
    method TEXT            -- 'sum', 'mean', or 'max'
) RETURNS FLOAT[]          -- Aggregated message

Example:

SELECT ruvector_gnn_aggregate(
    ARRAY[ARRAY[1.0, 2.0], ARRAY[3.0, 4.0]],
    'mean'
);
-- Returns: [2.0, 3.0]

3. GraphSAGE Forward Pass

ruvector_graphsage_forward(
    embeddings FLOAT[][],  -- Node embeddings
    src INT[],             -- Source node indices
    dst INT[],             -- Destination node indices
    out_dim INT,           -- Output dimension
    num_samples INT        -- Neighbors to sample per node
) RETURNS FLOAT[][]        -- Updated embeddings

Example:

SELECT ruvector_graphsage_forward(
    node_embeddings,
    edge_src,
    edge_dst,
    64,
    10
)
FROM my_graph;

4. Multi-Hop Message Passing

ruvector_message_pass(
    node_table TEXT,       -- Table with node features
    edge_table TEXT,       -- Table with edges
    embedding_col TEXT,    -- Column name for embeddings
    hops INT,              -- Number of hops
    layer_type TEXT        -- 'gcn' or 'sage'
) RETURNS TEXT             -- Description of operation

Example:

SELECT ruvector_message_pass(
    'nodes',
    'edges',
    'embedding',
    3,
    'gcn'
);

5. Batch GNN Processing

ruvector_gnn_batch_forward(
    embeddings_batch FLOAT[][],   -- Batch of embeddings
    edge_indices_batch INT[],     -- Flattened edge indices
    graph_sizes INT[],            -- Nodes per graph
    layer_type TEXT,              -- 'gcn' or 'sage'
    out_dim INT                   -- Output dimension
) RETURNS FLOAT[][]               -- Batch of results

Common Patterns

Pattern 1: Node Classification

-- Create node embeddings table
CREATE TABLE node_embeddings (
    node_id INT PRIMARY KEY,
    embedding FLOAT[]
);

-- Create edge table
CREATE TABLE edges (
    src INT,
    dst INT,
    weight FLOAT DEFAULT 1.0
);

-- Apply GCN
WITH gcn_output AS (
    SELECT ruvector_gcn_forward(
        ARRAY_AGG(embedding ORDER BY node_id),
        ARRAY_AGG(src ORDER BY edge_id),
        ARRAY_AGG(dst ORDER BY edge_id),
        ARRAY_AGG(weight ORDER BY edge_id),
        128
    ) as updated_embeddings
    FROM node_embeddings
    CROSS JOIN edges
)
SELECT * FROM gcn_output;
-- Compute edge embeddings using node embeddings
WITH node_features AS (
    SELECT ruvector_graphsage_forward(
        embeddings,
        sources,
        targets,
        64,
        10
    ) as new_embeddings
    FROM graph_data
),
edge_features AS (
    SELECT
        e.src,
        e.dst,
        nf.new_embeddings[e.src] || nf.new_embeddings[e.dst] as edge_embedding
    FROM edges e
    CROSS JOIN node_features nf
)
SELECT * FROM edge_features;

Pattern 3: Graph Classification

-- Aggregate node embeddings to graph embedding
WITH node_embeddings AS (
    SELECT
        graph_id,
        ruvector_gcn_forward(
            ARRAY_AGG(features),
            ARRAY_AGG(src),
            ARRAY_AGG(dst),
            NULL,
            128
        ) as embeddings
    FROM graphs
    GROUP BY graph_id
),
graph_embeddings AS (
    SELECT
        graph_id,
        ruvector_gnn_aggregate(embeddings, 'mean') as graph_embedding
    FROM node_embeddings
)
SELECT * FROM graph_embeddings;

Aggregation Methods

Method Formula Use Case
sum Σ messages Counting, accumulation
mean (Σ messages) / n Averaging features
max max(messages) Feature selection

Layer Types

GCN (Graph Convolutional Network)

When to use:

  • Transductive learning (fixed graph)
  • Homophilic graphs (similar nodes connected)
  • Need interpretable aggregation

Characteristics:

  • Degree normalization
  • All neighbors considered
  • Memory efficient

GraphSAGE

When to use:

  • Inductive learning (new nodes)
  • Large graphs (need sampling)
  • Heterogeneous graphs

Characteristics:

  • Neighbor sampling
  • Separate self/neighbor weights
  • L2 normalization

Performance Tips

  1. Use Sampling for Large Graphs:

    -- Instead of all neighbors
    SELECT ruvector_graphsage_forward(..., 10);  -- Sample 10 neighbors
    
  2. Batch Processing:

    -- Process multiple graphs at once
    SELECT ruvector_gnn_batch_forward(...);
    
  3. Index Edges:

    CREATE INDEX idx_edges_src ON edges(src);
    CREATE INDEX idx_edges_dst ON edges(dst);
    
  4. Materialize Intermediate Results:

    CREATE MATERIALIZED VIEW layer1_output AS
    SELECT ruvector_gcn_forward(...);
    

Typical Dimensions

Layer Input Dim Output Dim Hidden Dim
Layer 1 Raw features (varies) 128-256 -
Layer 2 128-256 64-128 -
Layer 3 64-128 32-64 -
Output 32-64 # classes -

Error Handling

-- Check for empty inputs
SELECT CASE
    WHEN ARRAY_LENGTH(embeddings, 1) = 0
    THEN NULL
    ELSE ruvector_gcn_forward(embeddings, src, dst, NULL, 64)
END;

-- Handle disconnected nodes
-- (automatically handled - returns original features)

Integration with PostgreSQL

Create Extension

CREATE EXTENSION ruvector;

Check Version

SELECT ruvector_version();

View Available Functions

\df ruvector_*

Complete Example

-- 1. Create tables
CREATE TABLE papers (
    paper_id INT PRIMARY KEY,
    features FLOAT[],
    label INT
);

CREATE TABLE citations (
    citing INT,
    cited INT,
    FOREIGN KEY (citing) REFERENCES papers(paper_id),
    FOREIGN KEY (cited) REFERENCES papers(paper_id)
);

-- 2. Load data
INSERT INTO papers VALUES
    (1, ARRAY[0.1, 0.2, 0.3], 0),
    (2, ARRAY[0.4, 0.5, 0.6], 1),
    (3, ARRAY[0.7, 0.8, 0.9], 0);

INSERT INTO citations VALUES
    (1, 2),
    (2, 3),
    (3, 1);

-- 3. Apply 2-layer GCN
WITH layer1 AS (
    SELECT ruvector_gcn_forward(
        ARRAY_AGG(features ORDER BY paper_id),
        ARRAY_AGG(citing ORDER BY citing, cited),
        ARRAY_AGG(cited ORDER BY citing, cited),
        NULL,
        128
    ) as h1
    FROM papers
    CROSS JOIN citations
),
layer2 AS (
    SELECT ruvector_gcn_forward(
        h1,
        ARRAY_AGG(citing ORDER BY citing, cited),
        ARRAY_AGG(cited ORDER BY citing, cited),
        NULL,
        64
    ) as h2
    FROM layer1
    CROSS JOIN citations
)
SELECT * FROM layer2;

Troubleshooting

Issue: Dimension Mismatch

-- Check input dimensions
SELECT ARRAY_LENGTH(features, 1) FROM papers LIMIT 1;

Issue: Out of Memory

-- Use GraphSAGE with sampling
SELECT ruvector_graphsage_forward(..., 10);  -- Limit neighbors

Issue: Slow Performance

-- Create indexes
CREATE INDEX ON edges(src, dst);

-- Use parallel queries
SET max_parallel_workers_per_gather = 4;

Quick Start: Copy the "Complete Example" above to get started immediately!