Metadata-Version: 2.4
Name: photokan
Version: 0.3.0
Summary: Photonic KAN: Bridging PyTorch, KAN, and Q.ANT photonic hardware
Author-email: Koneti <koneti@gmail.com>
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.1.0
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Requires-Dist: sympy>=1.12
Requires-Dist: matplotlib>=3.7
Provides-Extra: qpal
Requires-Dist: qpal; extra == "qpal"
Provides-Extra: llm
Requires-Dist: transformers>=4.40; extra == "llm"
Requires-Dist: peft; extra == "llm"
Provides-Extra: onnx
Requires-Dist: onnxscript; extra == "onnx"
Requires-Dist: onnxruntime; extra == "onnx"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Dynamic: license-file

# PhotoKAN

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

[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://python.org)
[![PyTorch](https://img.shields.io/badge/pytorch-2.1%2B-orange)](https://pytorch.org)
[![Status](https://img.shields.io/badge/status-alpha-yellow)](https://github.com)
[![PyPI](https://img.shields.io/pypi/v/photokan)](https://pypi.org/project/photokan/)

---

## 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

```bash
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

```python
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:

```python
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:

```python
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:

```python
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:

```python
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`):

```python
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

```bash
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.*
