AgentSkillsCN

ruvector-gnn-wasm

面向浏览器与边缘推理的 WebAssembly 图神经网络层,支持 GraphConv、GATLayer、SAGEConv 以及 GINConv。适用于在浏览器中运行 GNN 推理、将图模型部署至边缘设备、构建客户端侧知识图嵌入,或为 Web 应用程序引入 WASM 加速的图学习功能。

SKILL.md
--- frontmatter
name: "ruvector-gnn-wasm"
description: "WebAssembly Graph Neural Network layers for browser and edge inference with GraphConv, GATLayer, SAGEConv, and GINConv. Use when running GNN inference in browsers, deploying graph models to edge devices, building client-side knowledge graph embeddings, or adding WASM-accelerated graph learning to web applications."

@ruvector/gnn-wasm

WebAssembly-compiled Graph Neural Network layers providing browser-native and edge-compatible inference for GraphConv, GATLayer, SAGEConv, and GINConv with near-native performance and zero server dependency.

Quick Reference

TaskCode
Installnpx @ruvector/gnn-wasm@latest
Import (Node)import { WasmGNN, WasmGraphConv } from '@ruvector/gnn-wasm';
Import (Browser)import init, { WasmGNN } from '@ruvector/gnn-wasm'; await init();
Createconst gnn = new WasmGNN({ layers: ['gcn', 'gat'] });
Forwardconst out = gnn.forward(nodeFeatures, edgeIndex);
Embedconst emb = gnn.embed(graph);

Installation

Install: npx @ruvector/gnn-wasm@latest See Installation Guide for the full ecosystem.

Key API

WasmGNN

Main WASM-accelerated GNN inference engine.

Node.js:

typescript
import { WasmGNN } from '@ruvector/gnn-wasm';

const gnn = new WasmGNN({
  layers: ['gcn', 'gat', 'sage'],
  hiddenDim: 64,
  outputDim: 32,
});

Browser:

typescript
import init, { WasmGNN } from '@ruvector/gnn-wasm';

await init(); // Initialize WASM module

const gnn = new WasmGNN({
  layers: ['gcn', 'gat', 'sage'],
  hiddenDim: 64,
  outputDim: 32,
});

Constructor Options:

OptionTypeDefaultDescription
layersstring[]['gcn']Layer types: 'gcn', 'gat', 'sage', 'gin'
hiddenDimnumber64Hidden dimension size
outputDimnumber32Output embedding dimension
numHeadsnumber4Attention heads (GAT layers)
dropoutnumber0.0Dropout rate (inference only, typically 0)
activationstring'relu'Activation: 'relu', 'elu', 'silu', 'gelu'
normalizebooleantrueApply layer normalization
simdbooleantrueUse WASM SIMD instructions

Methods:

MethodReturnsDescription
forward(features, edgeIndex)Float32ArrayForward pass through all layers
embed(graph)Float32ArrayGenerate graph-level embedding
nodeEmbed(graph)Float32Array[]Per-node embeddings
loadWeights(weights)voidLoad pretrained weights
getMemoryUsage()MemoryInfoWASM memory usage stats
dispose()voidFree WASM memory

WasmGraphConv

Single WASM-compiled graph convolution layer.

typescript
import init, { WasmGraphConv } from '@ruvector/gnn-wasm';
await init();

const conv = new WasmGraphConv({ inDim: 128, outDim: 64 });
const output = conv.forward(nodeFeatures, edgeIndex);

Constructor Options:

OptionTypeDefaultDescription
inDimnumberrequiredInput feature dimension
outDimnumberrequiredOutput feature dimension
aggrstring'mean'Aggregation: 'mean', 'sum', 'max'
biasbooleantrueInclude bias term

WasmGATLayer

WASM-compiled Graph Attention layer.

typescript
import init, { WasmGATLayer } from '@ruvector/gnn-wasm';
await init();

const gat = new WasmGATLayer({ inDim: 128, outDim: 64, heads: 8 });
const output = gat.forward(nodeFeatures, edgeIndex);

Constructor Options:

OptionTypeDefaultDescription
inDimnumberrequiredInput feature dimension
outDimnumberrequiredOutput feature dimension
headsnumber4Number of attention heads
concatbooleantrueConcatenate heads (false = average)
negativeSlopenumber0.2LeakyReLU negative slope

Common Patterns

Browser Graph Visualization

typescript
import init, { WasmGNN } from '@ruvector/gnn-wasm';

await init();

const gnn = new WasmGNN({ layers: ['gcn', 'gcn'], outputDim: 2 });
gnn.loadWeights(await fetch('/model/weights.bin').then(r => r.arrayBuffer()));

const embeddings = gnn.nodeEmbed({ features, edgeIndex });
// Use 2D embeddings for visualization layout
embeddings.forEach((emb, i) => {
  nodes[i].x = emb[0] * scale;
  nodes[i].y = emb[1] * scale;
});

Edge Device Inference

typescript
import { WasmGNN } from '@ruvector/gnn-wasm';

const gnn = new WasmGNN({
  layers: ['sage'],
  hiddenDim: 32,
  outputDim: 16,
  simd: true,
});

gnn.loadWeights(modelBuffer);
const prediction = gnn.forward(sensorFeatures, sensorGraph);
console.log(`Memory: ${gnn.getMemoryUsage().heapUsed} bytes`);

Bundler Configuration (Vite)

typescript
// vite.config.ts
export default {
  optimizeDeps: {
    exclude: ['@ruvector/gnn-wasm'],
  },
  plugins: [wasmPlugin()],
};

RAN DDD Context

Bounded Context: RANO Optimization

References