Skip to content

Latest commit

Β 

History

119 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

j-SVM: GPU-Accelerated Parallel SVM using JAX

⭐ If you find this project helpful, please give it a star on GitHub!

πŸ“– Project Introduction Slides

j-SVM is a dual-implementation Support Vector Machine library that provides both a pure NumPy backend (SVM/) for CPU execution and a JAX-accelerated backend (JSVM/) for GPU/TPU acceleration. The JAX backend leverages JIT compilation, automatic differentiation, and NystrΓΆm kernel approximation to scale SVMs to large datasets efficiently.


🏷️ Badges

License & Runtime

Apache 2.0 Python JAX

Platform

nVIDIA AMD_HIP Linux

Tools

NumPy uv


✨ Features

  • Dual implementation β€” Choose between NumPy (CPU) or JAX (GPU/TPU) backends with identical APIs
  • Multiple kernel support β€” Linear, RBF (via NystrΓΆm approximation), and Polynomial kernels
  • NystrΓΆm kernel approximation β€” Efficient low-rank approximation of large kernel matrices for scalable training
  • JIT-compiled forward pass β€” JAX backend uses jax.jit for accelerated computation
  • Model persistence β€” Save and load trained models via joblib
  • Clean OOP design β€” Strategy pattern for pluggable kernel functions
  • Multi-class classification β€” One-vs-One strategy via MultiSupportVectorMachine
  • Built-in dataset loaders β€” Iris, Pima Indians Diabetes, Framingham Heart Study

πŸ—οΈ Architecture

j-svm/
β”œβ”€β”€ core/                          # Base parameter definitions
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── SVMParameter.py            # SVM hyperparameter container
β”‚
β”œβ”€β”€ JSVM/                          # JAX-accelerated implementation (GPU/TPU)
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ SupportVectorMachine.py    # Binary SVM β€” JAX backend
β”‚   └── MultiSupportVectorMachine.py  # Multi-class (OvO) β€” JAX backend
β”‚
β”œβ”€β”€ SVM/                           # NumPy implementation (CPU)
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ SupportVectorMachine.py    # Binary SVM β€” NumPy backend
β”‚   └── MultiSupportVectorMachine.py  # Multi-class (OvO) β€” NumPy backend
β”‚
β”œβ”€β”€ data/                          # Dataset loading utilities
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ DataUnit.py                # Unified dataset container
β”‚   β”œβ”€β”€ IrisDataset.py             # Iris dataset loader
β”‚   └── functional.py              # Data preprocessing helpers
β”‚
β”œβ”€β”€ utils/                         # Testing utilities
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_utils.py
β”‚
β”œβ”€β”€ example/                       # Usage examples
β”‚   β”œβ”€β”€ normal_svm.py              # NumPy SVM on iris
β”‚   β”œβ”€β”€ normal_svm_jax.py          # JAX SVM on iris
β”‚   β”œβ”€β”€ normal_multi_svm.py        # NumPy multi-class SVM
β”‚   β”œβ”€β”€ normal_multi_svm_jax.py    # JAX multi-class SVM
β”‚   β”œβ”€β”€ sample_jax_code.py         # JAX primitive demos
β”‚   β”œβ”€β”€ sample_jax_code_2.py       # Advanced JAX demos
β”‚   └── data_set_all_plus.py       # All-dataset benchmark
β”‚
β”œβ”€β”€ tests/                        # Unit tests (pytest)
β”‚   β”œβ”€β”€ test_svm.py               # Binary SVM β€” NumPy backend
β”‚   β”œβ”€β”€ test_jsvm.py              # Binary SVM β€” JAX backend
β”‚   β”œβ”€β”€ test_multi_svm.py         # Multi-class SVM β€” NumPy backend
β”‚   └── test_multi_jsvm.py        # Multi-class SVM β€” JAX backend
β”‚
β”œβ”€β”€ main.py                        # Entry point
β”œβ”€β”€ run_JSVM.py                    # JAX runtime script
β”œβ”€β”€ run_SVM.py                     # NumPy runtime script
└── pyproject.toml                 # Project configuration (uv)

πŸš€ Quick Start

Installation

git clone https://github.com/KeithLin724/j-svm.git
cd j-svm
pip install uv
uv sync
uv run python -c "from JSVM import SupportVectorMachine; SupportVectorMachine.warm_up()"

Minimal Working Example (Iris Dataset)

from JSVM import SupportVectorMachine   # or: from SVM import SupportVectorMachine
from data import IrisDataset

# Warm up JAX (only needed for JAX backend)
SupportVectorMachine.warm_up()

# Load data
data = IrisDataset()

# Train
model = SupportVectorMachine(C=10, kernel_name="rbf", kernel_arg={"sigma": 2})
model.train(data.train_x, data.train_y)

# Evaluate
print(f"Accuracy: {model.acc(data.test_x, data.test_y):.2%}")

Expected output (JAX backend, RBF kernel): ~96% accuracy on Iris test set.


πŸ“– Usage

J-SVM (JAX / GPU Backend)

from JSVM import SupportVectorMachine

# Warm up JAX (compiles kernels, loads CUDA/HIP)
SupportVectorMachine.warm_up()

# Build model
model = SupportVectorMachine(C=10, kernel_name="rbf", kernel_arg={"sigma": 2})

# Train
model.train(x=data_unit.train_x, y=data_unit.train_y)

# Predict (with_sign=False returns raw scores)
predict = model(data_unit.test_x)

# Save / Load
model.save("model_jax")
model = SupportVectorMachine.load_from("model_jax")

Example code in example/normal_svm_jax.py. Run on large datasets via run_JSVM.py.

SVM (NumPy / CPU Backend)

from SVM import SupportVectorMachine

# Build model
model = SupportVectorMachine(C=10, kernel_name="rbf", kernel_arg={"sigma": 2})

# Train
model.train(x=data_unit.train_x, y=data_unit.train_y)

# Predict
predict = model(data_unit.test_x)

# Save / Load
model.save("model")
model = SupportVectorMachine.load_from("model")

Example code in example/normal_svm.py. Run on large datasets via run_SVM.py.


See TODO.md for project roadmap.


πŸ“š API Reference

SupportVectorMachine

The core binary SVM classifier, available in both SVM and JSVM modules.

Constructor

model = SupportVectorMachine(
    C: float = 1.0,                          # Regularization parameter
    kernel_name: str = "linear",             # Kernel: "linear" | "rbf" | "poly"
    kernel_arg: dict | None = None,          # Kernel args, e.g. {"sigma": 2} for RBF
    threshold: float = 1e-5,                 # Support vector threshold
    approx_scale: int = 100                  # NystrΓΆm approximation rank (RBF only)
)

Methods

Method Description
model.train(x, y) Train the SVM on data x (NΓ—D) and labels y (N,)
model(x, with_sign=False) Predict. with_sign=True returns Β±1 class labels
model.acc(x, y) Compute accuracy against ground truth
model.save(path) Persist model to disk via joblib
SupportVectorMachine.load_from(path) Load a saved model from disk

MultiSupportVectorMachine

One-vs-One multi-class wrapper. Same methods as SupportVectorMachine.


πŸ”— Reference

Datasets

  • Pima Indians Diabetes Database β€” Kaggle
  • Framingham Heart Study Dataset β€” Kaggle

Releases

Packages

Contributors

Languages