Skip to main content

Photonic KAN: Bridging PyTorch, KAN, and Q.ANT photonic hardware

Project description

PhotoKAN

Photonic Kolmogorov-Arnold Networks — bridging PyTorch · KAN · Q.ANT photonic hardware.

Python PyTorch Status PyPI


Why PhotoKAN?

Standard MLPs use fixed activations on nodes and linear weights on edges.
KANs invert this: learnable nonlinear functions sit on the edges, summed at nodes.

Photonic hardware is physically structured around edge-level nonlinear transforms — light through a waveguide produces exactly the kind of parametric nonlinear function a KAN edge needs. This is not an analogy; it is a direct structural match.

Published benchmarks show:

  • 43% fewer parameters vs equivalent MLPs
  • 46% fewer operations vs equivalent MLPs
  • 30x energy efficiency gain on Q.ANT NPU vs CMOS

Installation

pip install photokan          # CPU simulation (no hardware required)
pip install photokan[qpal]    # + Q.ANT NPU support
pip install photokan[llm]     # + HuggingFace / PEFT integration
pip install photokan[onnx]    # + ONNX export
pip install photokan[dev]     # + development tools

Quick Start

import torch
import photokan as pk

# Works on CPU sim if no NPU — no code changes needed
model = pk.PhotoKAN(
    layer_sizes=[4, 16, 16, 1],
    activation='sine',   # 'sine' | 'fourier' | 'spline' | 'relu'
    backend='auto',      # auto-detects NPU, falls back to CPU
)

x = torch.randn(32, 4)
y = model(x)

# Standard PyTorch training
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
loss = torch.nn.MSELoss()(y, torch.randn(32, 1))
loss.backward()
optimizer.step()

# Check what hardware is available
print(pk.available_backends())
# -> {'cpu': True, 'cuda': True, 'qpal': False}

Activation Variants

Name Formula Best for Photonic native
sine sum w*sin(f*x + p) Periodic targets, photonic deployment Yes
fourier a0 + sum [a*cos + b*sin] Multi-frequency signals Yes
spline B-spline basis Non-periodic, high precision Via LUT
relu sum w*ReLU(a*x + b) Edge inference, speed Yes

Photonic Noise Simulation

Test accuracy against realistic hardware impairments before deploying to NPU:

sim = pk.PhotonicSimulator()
sim.set_hardware_profile('npu2')   # SNR=16dB, 8-bit

results = sim.sweep_snr(model, x_test, y_test,
                         snr_range=[8, 10, 12, 14, 16, 20])
sim.plot_snr_accuracy(results)

Energy Estimation

Estimate energy consumption with Q.ANT's published efficiency numbers:

from photokan.utils import estimate_model_energy

reports = estimate_model_energy(model, batch_size=64)
# Layer 0: 8.214 uJ (CMOS) -> 0.267 uJ (Photonic), 30.8x better
# Layer 1: ...

Convolutional KAN

Use PhotoConvKAN for image and spatial workloads:

model = pk.PhotoConvKAN(
    in_channels=1, out_channels=16,
    kernel_size=3, activation='sine',
)
x = torch.randn(8, 1, 28, 28)
y = model(x)  # [8, 16, 28, 28]

LLM Integration

Replace MLP layers in HuggingFace transformers with PhotoKAN using LoRA-style adapters:

from photokan.llm import add_photo_lora, PhotoKANAttention

# Wrap a transformer's MLP layers
model = add_photo_lora(model, target_modules=["mlp"], n_basis=4)

AOT Compilation

Compile models to photonic deployment bundles (.npu):

compiler = pk.PhotonicCompiler()
program = compiler.compile(model, './my_model.npu')

# CPU validation (LUT interpreter)
y = program.run(x, backend='cpu')

# NPU inference (requires Q.PAL)
y = program.run(x, backend='qpal')

# Benchmark latency
stats = program.benchmark(x)
# -> {'mean_ms': 0.42, 'throughput_samples_per_sec': 76190}

Architecture

User PyTorch model
  |- PhotoKAN / PhotoKANLayer / PhotoConvKAN  (nn.Module, drop-in)
       |- EdgeActivations (sine / fourier / spline / relu)
            |- SimBackend -> CPU physics simulation
            |- QPALBackend -> Q.ANT NPU via Q.PAL (Phase 3)
  |- PhotonicCompiler
       |- LUTCompiler -> int8 quantised lookup tables
       |- QPALGraphBuilder -> NPU execution graph
       |- PhotonicProgram -> run on NPU or CPU LUT interpreter
  |- PhotonicSimulator -> SNR sweeps, transfer functions
  |- LLM integration (LoRA adapters, attention)

Project Status

Phase Features Status
Phase 1 Activations, SimBackend, Layers, Energy, Profiler Done
Phase 2 LUT compiler, execution graph, ONNX export, ConvKAN Done
Phase 3 Q.PAL hardware dispatch, LLM fine-tuning, arXiv paper Upcoming

Running Tests

pip install -e ".[dev]"
pytest                          # 130+ tests
pytest -k "not mnist_real"      # skip slow MNIST training
pytest --cov=photokan           # with coverage

References

  • Liu et al. (2024) -- KAN: Kolmogorov-Arnold Networks (arXiv 2404.19756)
  • Peng et al. (2024) -- Photonic KAN via RAMZI (98% MNIST, 65x energy-area reduction)
  • Reinhardt et al. (2024) -- SineKAN
  • Q.ANT NPU -- https://qant.com/photonic-computing/

PhotoKAN v0.3 -- Build the bridge. Light does the math.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

photokan-0.3.0.tar.gz (41.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

photokan-0.3.0-py3-none-any.whl (52.5 kB view details)

Uploaded Python 3

File details

Details for the file photokan-0.3.0.tar.gz.

File metadata

  • Download URL: photokan-0.3.0.tar.gz
  • Upload date:
  • Size: 41.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for photokan-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2c186f4d675a5cd277c2ce193ec00ab7e78f45198401421a1b7002eb50af9a29
MD5 d33c6d9c4737ad2f20a3dd99a9e45946
BLAKE2b-256 5261c46814c5ec07069bc368746998faab20a1c645fa8f7056d295882a0a33d0

See more details on using hashes here.

File details

Details for the file photokan-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: photokan-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 52.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for photokan-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19c6b4af0be41fac4370f88dc9277b847a6a7ad6406792bf418e60f510424fac
MD5 c42e8daceb6722e146be3341e557ad82
BLAKE2b-256 e01f65566c06849af47246f40a90639bf6559df4af490eb56bf8680aa0c212d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page