Enhanced Plugin System Architecture v2.0

Complete four-level plugin architecture with enhanced kernel plugin system (v2.0), featuring ClassLoader isolation, hot-reload support, comprehensive validation, and full engine integration.


Overview

The Gollek Inference Engine implements a comprehensive four-level plugin architecture that provides:

  • Flexibility: Hot-reload, selective deployment, ClassLoader isolation
  • Performance: Up to 30x speedup with all optimizations
  • Efficiency: 70% code reuse, minimal duplication
  • Portability: Platform-specific kernels with auto-detection
  • Extensibility: Easy to add new plugins at any level
  • Observability: Comprehensive metrics and health monitoring

What’s New in v2.0

Enhanced Kernel Plugin System

The kernel plugin system has been completely redesigned with production-ready features:

Feature v1.0 v2.0
Type Safety Basic ✅ Generic operations
Lifecycle Simple ✅ validate/initialize/health/shutdown
Isolation None ✅ ClassLoader per plugin
Hot-Reload Limited ✅ Full support
Validation Basic ✅ Comprehensive with errors/warnings
Error Handling Generic ✅ Specific exception hierarchy
Metrics Basic ✅ Comprehensive observability
Configuration Map-based ✅ Type-safe with builder
CDI Integration Manual ✅ Automatic producers
Engine Integration Partial ✅ Full integration

Architecture Improvements

┌─────────────────────────────────────────────────────────┐
│              Application Layer                          │
│  InferenceRequest → Engine                              │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│         Gollek Inference Engine                         │
│  - PluginSystemIntegrator                               │
│  - KernelPluginProducer (CDI) ← NEW                     │
│  - KernelPluginIntegration ← NEW                        │
│  - InferenceOrchestrator                                │
│  - Metrics & Monitoring                                 │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│       Level 1: Runner Plugins                           │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐  │
│  │  GGUF    │ │ Safetensor│ │   ONNX   │ │TensorRT  │  │
│  └──────────┘ └──────────┘ └──────────┘ └──────────┘  │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│       Level 2: Feature Plugins                          │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐                │
│  │  Audio   │ │  Vision  │ │   Text   │                │
│  └──────────┘ └──────────┘ └──────────┘                │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│      Level 3: Optimization Plugins                      │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐  │
│  │   FA3    │ │   FA4    │ │  Paged   │ │  Prompt  │  │
│  └──────────┘ └──────────┘ └──────────┘ └──────────┘  │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│    Level 4: Kernel Plugins (Enhanced v2.0)              │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐  │
│  │  CUDA    │ │  ROCm    │ │  Metal   │ │ DirectML │  │
│  │  Kernel  │ │  Kernel  │ │  Kernel  │ │  Kernel  │  │
│  └──────────┘ └──────────┘ └──────────┘ └──────────┘  │
│         ↑                                              │
│  ┌──────────────────────────────────────────┐          │
│  │  KernelPluginManager (v2.0)              │          │
│  │  - ClassLoader Isolation                 │          │
│  │  - Health Monitoring                     │          │
│  │  - Metrics Collection                    │          │
│  │  - Hot-Reload Support                    │          │
│  └──────────────────────────────────────────┘          │
└─────────────────────────────────────────────────────────┘

Enhanced Kernel Plugin System (v2.0)

Key Components

1. KernelPlugin SPI (Enhanced)

The core SPI interface with comprehensive lifecycle management:

public interface KernelPlugin {
    // Identity
    String id();
    String name();
    String version();
    String platform();
    
    // Lifecycle
    KernelValidationResult validate();
    void initialize(KernelContext context) throws KernelException;
    boolean isAvailable();
    boolean isHealthy();
    KernelHealth health();
    void shutdown();
    
    // Operations (Typed)
    <T> KernelResult<T> execute(
        KernelOperation operation,
        KernelContext context) throws KernelException;
    
    // Async Support
    <T> CompletionStage<KernelResult<T>> executeAsync(
        KernelOperation operation,
        KernelContext context);
    
    // Capabilities
    Set<String> supportedOperations();
    Set<String> supportedArchitectures();
    Set<String> supportedVersions();
    KernelConfig getConfig();
    Map<String, String> dependencies();
}

2. KernelPluginManager

Enhanced manager with comprehensive features:

@ApplicationScoped
public class KernelPluginManager {
    
    // Lifecycle
    void initialize();
    void shutdown();
    
    // Plugin Management
    void register(KernelPlugin kernel);
    Optional<KernelPlugin> getActiveKernel();
    List<KernelPlugin> getAllKernels();
    
    // Execution
    <T> KernelResult<T> execute(
        KernelOperation operation,
        KernelContext context);
    <T> CompletionStage<KernelResult<T>> executeAsync(
        KernelOperation operation,
        KernelContext context);
    
    // Observability
    Map<String, KernelHealth> getHealthStatus();
    KernelMetrics getMetrics();
    Map<String, Object> getStats();
    
    // Platform
    String getCurrentPlatform();
    void selectKernelForPlatform(String platform);
}

3. KernelPluginLoader

Advanced loader with ClassLoader isolation:

public class KernelPluginLoader {
    
    // Load all plugins from directory
    List<KernelPlugin> loadAll();
    
    // Load specific plugin
    KernelPlugin loadPlugin(Path jarPath);
    
    // Hot-reload
    KernelPlugin reloadPlugin(Path jarPath);
    
    // Unload
    boolean unloadPlugin(String pluginId);
    
    // Isolated ClassLoader
    class KernelPluginClassLoader extends URLClassLoader {
        // Parent-first for core packages
        // Plugin isolation
    }
}

Type-Safe Operations

KernelOperation

KernelOperation operation = KernelOperation.builder()
    .name("gemm")
    .parameter("m", 1024)
    .parameter("n", 1024)
    .parameter("k", 1024)
    .parameter("matrix_a", matrixA)
    .parameter("matrix_b", matrixB)
    .metadata("priority", "high")
    .metadata("stream", true)
    .build();

KernelContext

KernelContext context = KernelContext.builder()
    .config(kernelConfig)
    .parameter("batch_size", 4)
    .parameter("sequence_length", 4096)
    .metadata("trace_id", "abc123")
    .executionContext(execContext)
    .build();

KernelResult

KernelResult<Matrix> result = kernelManager.execute(operation, context);

// Access typed result
Matrix output = result.getData();

// Check status
if (result.isSuccess()) {
    System.out.println("Completed in " + 
        result.getDuration().toMillis() + "ms");
}

// Access metadata
Map<String, Object> metadata = result.getMetadata();

Comprehensive Validation

@Override
public KernelValidationResult validate() {
    List<String> errors = new ArrayList<>();
    List<String> warnings = new ArrayList<>();
    
    // Check hardware
    if (!isGpuAvailable()) {
        errors.add("GPU not available");
    }
    
    // Check drivers
    if (!areDriversInstalled()) {
        errors.add("GPU drivers not installed");
    }
    
    // Check compute capability
    int cap = getComputeCapability();
    if (cap < 60) {
        errors.add("Compute capability 6.0+ required");
    } else if (cap < 80) {
        warnings.add("Limited performance on CC < 8.0");
    }
    
    return KernelValidationResult.builder()
        .valid(errors.isEmpty())
        .errors(errors)
        .warnings(warnings)
        .build();
}

Exception Hierarchy

KernelException (base)
├── KernelInitializationException (unchecked)
├── KernelExecutionException (unchecked)
├── KernelNotFoundException (unchecked)
└── UnknownOperationException (unchecked)

Usage:

try {
    kernelManager.execute(operation, context);
} catch (KernelNotFoundException e) {
    // No kernel for platform
    LOG.errorf("No kernel for: %s", e.getPlatform());
} catch (UnknownOperationException e) {
    // Unsupported operation
    LOG.errorf("Unknown op: %s", e.getOperationName());
} catch (KernelExecutionException e) {
    // Execution failed
    LOG.errorf("Exec failed: %s", e.getMessage(), e);
}

Observability & Metrics

KernelMetrics

KernelMetrics metrics = kernelManager.getMetrics();

// Counters
long totalOps = metrics.getCounter("total_operations");
long errors = metrics.getCounter("total_errors");

// Operation stats
OperationStats stats = metrics.getOperationStats("gemm");
System.out.printf("GEMM: count=%d, avg=%.2fms, min=%dms, max=%dms, success=%.2f%%\n",
    stats.getCount(),
    stats.getAverageDuration(),
    stats.getMinDuration(),
    stats.getMaxDuration(),
    stats.getSuccessRate() * 100);

// Full export
Map<String, Object> metricsMap = metrics.toMap();

Health Monitoring

Map<String, KernelHealth> health = kernelManager.getHealthStatus();
health.forEach((platform, h) -> {
    if (h.isHealthy()) {
        System.out.println(platform + ": ✓ " + h.getMessage());
        System.out.println("  Details: " + h.getDetails());
    } else {
        System.out.println(platform + ": ✗ " + h.getMessage());
    }
});

Engine Integration

CDI Integration

The kernel plugin system is fully integrated with CDI for automatic dependency injection:

@ApplicationScoped
public class MyInferenceService {
    
    @Inject
    KernelPluginManager kernelManager;  // Auto-injected
    
    @Inject
    KernelPluginIntegration kernelIntegration;  // Auto-injected
    
    public void execute() {
        KernelOperation op = KernelOperation.builder()
            .name("gemm")
            .parameter("m", 1024)
            .build();
        
        KernelResult<Matrix> result = kernelManager.execute(
            op, KernelContext.empty());
    }
}

PluginSystemIntegrator

Enhanced integrator with kernel plugin support:

@ApplicationScoped
public class PluginSystemIntegrator {
    
    @Inject
    KernelPluginProducer kernelPluginProducer;
    
    @PostConstruct
    public void initialize() {
        // Initialize in order
        initializeKernelPlugins();    // Level 4 (Enhanced)
        initializeOptimizationPlugins(); // Level 3
        initializeFeaturePlugins();   // Level 2
        initializeRunnerPlugins();    // Level 1
        
        logPluginStatus();
    }
    
    private void initializeKernelPlugins() {
        // Initialize via producer
        kernelPluginProducer.initialize();
        
        KernelPluginManager manager = 
            kernelPluginProducer.getKernelManager();
        
        // Log details
        LOG.infof("Platform: %s", manager.getCurrentPlatform());
        LOG.infof("Kernels: %d", manager.getAllKernels().size());
        LOG.infof("Health: %s", 
            manager.getActiveKernel()
                .map(k -> k.isHealthy() ? "✓" : "✗")
                .orElse("✗"));
    }
}

KernelPluginProducer

CDI producer for automatic injection:

@ApplicationScoped
public class KernelPluginProducer {
    
    @Produces
    @Singleton
    public KernelPluginManager produceKernelPluginManager() {
        if (!initialized) {
            initialize();
        }
        return kernelPluginManager;
    }
    
    @Produces
    @Singleton
    public KernelPluginIntegration produceKernelPluginIntegration() {
        if (!initialized) {
            initialize();
        }
        return kernelIntegration;
    }
}

Configuration

Complete Configuration Example

gollek:
  engine:
    plugin-system:
      enabled: true
      auto-detect: true

  # Level 4: Kernel Plugins (Enhanced)
  kernels:
    auto-detect: true
    plugin-directory: ~/.gollek/plugins/kernels
    cuda:
      enabled: true
      device_id: 0
      memory_fraction: 0.9
      allow_growth: true
      compute_mode: default
      timeout_ms: 300000
    rocm:
      enabled: false
    metal:
      enabled: false
    directml:
      enabled: false

  # Level 3: Optimization Plugins
  optimizations:
    flash-attention-3:
      enabled: true
      tile_size: 128
    paged-attention:
      enabled: true
      block_size: 16

  # Level 2: Feature Plugins
  features:
    audio:
      enabled: true
    vision:
      enabled: false
    text:
      enabled: true

  # Level 1: Runner Plugins
  runners:
    gguf-runner:
      enabled: true
      n_gpu_layers: -1
      n_ctx: 4096

System Properties

# Plugin directory
-Dgollek.plugin.directory=~/.gollek/plugins

# Platform override
-Dgollek.kernel.platform=cuda

# Device selection
-Dgollek.kernel.device_id=0

# Enable metrics
-Dgollek.kernel.metrics.enabled=true

# Enable hot-reload
-Dgollek.kernel.hot_reload.enabled=true

Usage Examples

Example 1: Basic Kernel Execution

@Inject
KernelPluginManager kernelManager;

public Matrix multiply(Matrix a, Matrix b) {
    KernelOperation operation = KernelOperation.builder()
        .name("gemm")
        .parameter("matrix_a", a)
        .parameter("matrix_b", b)
        .build();
    
    KernelResult<Matrix> result = kernelManager.execute(
        operation, KernelContext.empty());
    
    return result.getData();
}

Example 2: Async Execution

@Inject
KernelPluginManager kernelManager;

public CompletionStage<Matrix> multiplyAsync(Matrix a, Matrix b) {
    KernelOperation operation = KernelOperation.builder()
        .name("gemm")
        .parameter("matrix_a", a)
        .parameter("matrix_b", b)
        .build();
    
    return kernelManager.executeAsync(operation, KernelContext.empty())
        .thenApply(KernelResult::getData);
}

Example 3: Health Monitoring

@Inject
KernelPluginManager kernelManager;

@Scheduled(every = "10s")
public void checkHealth() {
    Map<String, KernelHealth> health = kernelManager.getHealthStatus();
    health.forEach((platform, h) -> {
        if (!h.isHealthy()) {
            LOG.warnf("Unhealthy: %s - %s", platform, h.getMessage());
        }
    });
}

Example 4: Metrics Collection

@Inject
KernelPluginManager kernelManager;

public void logMetrics() {
    KernelMetrics metrics = kernelManager.getMetrics();
    
    LOG.infof("Uptime: %dms", metrics.getUptime());
    LOG.infof("Operations: %d", metrics.getCounter("total_operations"));
    LOG.infof("Errors: %d", metrics.getCounter("total_errors"));
    
    OperationStats gemmStats = metrics.getOperationStats("gemm");
    LOG.infof("GEMM: count=%d, avg=%.2fms, success=%.2f%%",
        gemmStats.getCount(),
        gemmStats.getAverageDuration(),
        gemmStats.getSuccessRate() * 100);
}

Example 5: Hot-Reload Plugin

@Inject
KernelPluginIntegration kernelIntegration;

public void reloadPlugin(Path newJar) {
    KernelPluginManager manager = 
        kernelIntegration.getKernelPluginManager();
    
    // Get loader (implementation detail)
    KernelPluginLoader loader = getPluginLoader(manager);
    
    // Reload
    try {
        loader.reloadPlugin(newJar);
        LOG.info("Plugin reloaded successfully");
    } catch (KernelException e) {
        LOG.errorf("Reload failed: %s", e.getMessage());
    }
}

Creating Custom Plugins

Custom Kernel Plugin

public class CustomKernelPlugin implements KernelPlugin {
    
    public static final String ID = "custom-kernel";
    
    @Override
    public String id() { return ID; }
    
    @Override
    public String name() { return "Custom Kernel"; }
    
    @Override
    public String version() { return "1.0.0"; }
    
    @Override
    public String platform() { return "custom"; }
    
    @Override
    public KernelValidationResult validate() {
        if (!isHardwareAvailable()) {
            return KernelValidationResult.invalid(
                "Custom hardware not available");
        }
        return KernelValidationResult.valid();
    }
    
    @Override
    public void initialize(KernelContext context) 
            throws KernelException {
        // Initialize custom hardware
        initializeHardware(context.getConfig());
    }
    
    @Override
    public boolean isAvailable() {
        return isHardwareAvailable();
    }
    
    @Override
    public <T> KernelResult<T> execute(
            KernelOperation operation,
            KernelContext context) throws KernelException {
        
        return switch (operation.getName()) {
            case "gemm" -> executeGemm(operation, context);
            case "attention" -> executeAttention(operation, context);
            default -> throw new UnknownOperationException(
                platform(), operation.getName());
        };
    }
    
    @Override
    public Set<String> supportedOperations() {
        return Set.of("gemm", "attention", "layer_norm");
    }
    
    @Override
    public Set<String> supportedArchitectures() {
        return Set.of("custom_v1", "custom_v2");
    }
    
    @Override
    public Set<String> supportedVersions() {
        return Set.of("1.0", "2.0");
    }
}

Service Provider Configuration

Create src/main/resources/META-INF/services/tech.kayys.gollek.plugin.kernel.KernelPlugin:

com.example.CustomKernelPlugin

JAR Manifest

In pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Plugin-Id>custom-kernel</Plugin-Id>
                        <Plugin-Type>kernel</Plugin-Type>
                        <Plugin-Version>${project.version}</Plugin-Version>
                        <Platform>custom</Platform>
                        <Plugin-Class>com.example.CustomKernelPlugin</Plugin-Class>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Performance

Complete Stack Performance (H100)

Configuration Tokens/s VRAM Speedup
CPU Only (Baseline) 20 16 GB 1.0x
+ CUDA Kernel 100 16 GB 5.0x
+ FlashAttention-3 280 16 GB 14.0x
+ PagedAttention 350 13 GB 17.5x
+ Prompt Cache* 500 18 GB 25.0x
All Combined 600 14 GB 30.0x

*For cached prompts

Overhead Analysis

Feature Overhead Notes
ClassLoader Isolation +5-10ms One-time load
Metrics Collection <1% Async aggregation
Health Checks <0.1ms Cached results
Adapter Pattern Negligible Direct delegation
Validation +1-2ms Pre-initialization

Monitoring

Plugin Status API

# Get plugin system status
curl http://localhost:8080/api/v1/plugins/status

# Response:
{
  "initialized": true,
  "fully_operational": true,
  "levels": {
    "kernel": {
      "enabled": true,
      "active_platform": "cuda",
      "total_kernels": 1,
      "health": "healthy"
    },
    "optimization": {
      "enabled": true,
      "active_optimizations": ["flash-attention-3"]
    },
    "feature": {
      "enabled": true,
      "active_features": ["text"]
    },
    "runner": {
      "enabled": true,
      "active_runners": ["gguf-runner"]
    }
  }
}

Health API

# Get kernel health
curl http://localhost:8080/api/v1/kernels/health

# Response:
{
  "cuda": {
    "healthy": true,
    "available": true,
    "details": {
      "device_count": 1,
      "device_name": "NVIDIA H100",
      "memory_fraction": 0.9
    }
  }
}

Metrics API

# Get kernel metrics
curl http://localhost:8080/api/v1/kernels/metrics

# Response:
{
  "uptime_ms": 3600000,
  "total_operations": 10000,
  "total_errors": 5,
  "operations": {
    "gemm": {
      "count": 5000,
      "avg_duration_ms": 2.3,
      "success_rate": 0.999
    }
  }
}

Troubleshooting

Plugin Not Loading

Symptom: Plugin not appearing in getAllKernels()

Solutions:

  1. Check JAR manifest has Plugin-Type: kernel
  2. Verify ServiceLoader configuration
  3. Check plugin directory: ls ~/.gollek/plugins/kernels/
  4. Review logs: tail -f ~/.gollek/logs/gollek.log | grep "Loading"

Validation Failing

Symptom: KernelValidationResult returns errors

Solutions:

  1. Check hardware present (nvidia-smi)
  2. Verify drivers installed
  3. Check library dependencies (CUDA, ROCm)
  4. Review errors: result.getErrors()

CDI Injection Failing

Symptom: NullPointerException when injecting KernelPluginManager

Solutions:

  1. Ensure PluginSystemIntegrator.initialize() called
  2. Check KernelPluginProducer is initialized
  3. Verify CDI bean discovery enabled
  4. Check logs for initialization errors

ClassLoader Issues

Symptom: ClassNotFoundException

Solutions:

  1. Ensure dependencies in plugin JAR
  2. Check parent-first package config
  3. Verify no conflicting library versions
  4. Review ClassLoader isolation

Resources


Back to Architecture   GPU Runtime Docs   Optimization Plugins   Runner Plugins