Metadata-Version: 2.4
Name: vulkann
Version: 0.1.0
Summary: Vendor-agnostic deep learning on pure Vulkan compute
Keywords: vulkan,deep-learning,pytorch,slang,amd,intel,gpu,transformer,mamba,quantization
Author: Vinay B
License-Expression: LGPL-3.0-only
License-File: LICENSE
License-File: LICENSE.GPLv3
License-File: NOTICE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Project-URL: Homepage, https://github.com/vinaybhargav/VulkaNN
Project-URL: Documentation, https://github.com/vinaybhargav/VulkaNN/blob/main/README.md
Project-URL: Repository, https://github.com/vinaybhargav/VulkaNN.git
Project-URL: Issues, https://github.com/vinaybhargav/VulkaNN/issues
Requires-Python: >=3.9
Requires-Dist: numpy>=1.20
Description-Content-Type: text/markdown

# VulkaNN: High-Performance Pure Vulkan Deep Learning Framework

<p align="center">
  <img src="assets/vulkann_logo.png" alt="VulkaNN Logo" width="220" />
</p>

<p align="center">
  <strong>Break free from proprietary CUDA lock-in. Train and deploy neural networks natively on any GPU via 100% Khronos Vulkan compute & Slang shaders.</strong>
</p>

<p align="center">
  <a href="#-key-features">Key Features</a> •
  <a href="#-architecture--performance-stack">Architecture</a> •
  <a href="#-quick-start">Quick Start</a> •
  <a href="#-examples--cookbook">Cookbook</a> •
  <a href="#-building--developing">Build</a> •
  <a href="#-agent--developer-resources">Agents & LLMs</a> •
  <a href="#-license">License</a>
</p>

---

## 🚀 Why VulkaNN?

Modern deep learning frameworks have locked developers into vendor-specific hardware stacks (CUDA, cuDNN, ROCm). **VulkaNN** is built from first principles on pure **Khronos Vulkan 1.2+** and **Slang** compute shaders:

- **100% Multi-Vendor Parity:** Runs seamlessly on NVIDIA, AMD, Intel (Arc & Iris Xe integrated), Apple Silicon (MoltenVK), Qualcomm Adreno, ARM Mali, and Raspberry Pi 5.
- **Zero CUDA / Zero ROCm:** Never requires proprietary driver toolchains or CUDA runtimes.
- **Zero `.numpy()` in Compute Kernels:** Pure GPU tensor math, forward/backward autograd, optimizer updates, and loss evaluations execute directly on GPU buffers.
- **PyTorch 1:1 API Parity:** Drop-in replacement for PyTorch tensors, autograd, `nn.Module`, optimizers, learning rate schedulers, mixed precision, and initialization.
- **Heterogeneous Multi-GPU:** Combine integrated GPUs (e.g. Intel Iris Xe) and discrete GPUs (e.g. NVIDIA RTX) in a single balanced training loop with dynamic latency-aware work distribution.
- **Static Graph Capture & Replay:** Near-zero CPU overhead via `@vulkann.compile` (`VulkanGraph`).
- **Modern Transformer & State-Space Models:** Built-in FlashAttention-2, Mamba (Selective State Space), Snake/SnakeBeta, SwiGLU, Rotary Embeddings, and Transformer encoders.
- **Zero-Dependency Safetensors:** Direct fast I/O without Hugging Face `safetensors` or PyTorch dependencies.

---

## ⚡ Key Features

### 1. Modern Vulkan Compute & Slang Shaders
- **Slang Compute Kernels:** All GPU operations written in Khronos Slang (`shaders/*.slang`), compiled to SPIR-V via `slangc`.
- **Subgroup Wave Intrinsics:** Workgroup reductions and softmax use `WaveActiveSum` and `WaveActiveMax` with explicit subgroup control (`VK_EXT_subgroup_size_control`, enforcing 32-lane execution) avoiding workgroup barrier stalls.
- **Hardware Float Atomics:** Fast non-conflicting gradient scatter/embedding updates using `VK_EXT_shader_atomic_float`.
- **Persistent Pipeline Cache:** Two-tier `VkPipelineCache` (in-memory + disk cache at `~/.cache/vulkann/`) eliminating runtime shader re-compilation hitching.
- **Hardware Matrix Acceleration:** Tensor cores and matrix engines accelerated via `VK_KHR_cooperative_matrix` with fallback to tiled GEMM.
- **Multi-Dtype Support:** Native compute in `float32`, `float16` (`half`), `bfloat16`, `int64`, `int32`, `int8`, `uint8`, and `bool`.

### 2. Comprehensive Deep Learning Modules (`vulkann.nn`)
- **Foundational Layers:** `Linear`, `Conv1d`, `Conv2d`, `Embedding`, `Dropout`, `Identity`.
- **Recurrent & Sequence Models:** `LSTM`, `GRU`, `RNN`, `MultiheadAttention`, `TransformerEncoder`, `TransformerEncoderLayer`.
- **State-of-the-Art Architectures:**
  - `Mamba`: Selective state-space sequence model (S6) with selective discretization.
  - `Snake` / `SnakeBeta`: Periodic activation functions preserving inductive bias for audio, signals, and time series.
  - `SwiGLU`: Swish-Gated Linear Units used in modern LLMs (LLaMA, Mistral).
  - `RotaryEmbedding` (RoPE): Relative rotary positional encoding for modern attention models.
  - `FlashAttention-2`: Memory-efficient tiled multi-head attention avoiding $O(N^2)$ VRAM materialization.
- **Normalization & Activations:** `LayerNorm`, `BatchNorm1d`, `BatchNorm2d`, `RMSNorm`, `ReLU`, `GELU`, `SiLU`, `Sigmoid`, `Tanh`, `LeakyReLU`, `Softmax`, `LogSoftmax`.
- **Loss Functions:** `CrossEntropyLoss`, `MSELoss`, `L1Loss`, `SmoothL1Loss`, `BCEWithLogitsLoss`, `NLLLoss`.
- **Weight Initializers (`vulkann.nn.init`):** `xavier_uniform_`, `xavier_normal_`, `kaiming_uniform_`, `kaiming_normal_`, `uniform_`, `normal_`, `constant_`, `zeros_`, `ones_`, `eye_`, `orthogonal_`.
- **Gradient Utilities:** `vulkann.nn.utils.clip_grad_norm_`, `clip_grad_value_`.

### 3. Autograd Engine & Differentiability
- **Tape-Based Reverse-Mode Autograd:** Dynamic computation graph tracking operations on tensors with `requires_grad=True`.
- **Custom Autograd Functions:** Define user operations via `vulkann.autograd.Function` with static `forward(ctx, ...)` and `backward(ctx, ...)`.
- **Activation Checkpointing:** Trade compute for memory using `vulkann.utils.checkpoint.checkpoint(fn, *args)`.
- **Analytical Gradient Verification:** Comprehensive `vulkann.autograd.gradcheck` comparing analytical gradients against finite differences.
- **Slang Automatic Differentiation:** Research integration with Slang `[Differentiable]` shaders.

### 4. Graph Capture & JIT Compilation (`@vulkann.compile`)
- Capture static execution graphs into **`VulkanGraph`** to eliminate per-iteration Python overhead, dynamic command pool allocations, and host-device synchronization latency:
```python
import vulkann as torch

model = MyModel().to("vk:0")
opt_model = torch.compile(model)  # Captures command buffers for replay

for x in batches:
    out = opt_model(x)  # Replays pre-recorded command buffers directly
```

### 5. Heterogeneous Multi-GPU (`DataParallel`)
- Concurrently train models across **any combination of GPUs** (e.g. `vk:0` Intel Iris Xe + `vk:1` NVIDIA RTX).
- **Closed-Loop EMA Latency Balancer:** Continuously monitors step execution latencies on each device and automatically adjusts batch shard splits proportionally to real-time throughput.
- **Small-Batch Bypass:** Automatically avoids dispatch overhead by executing sub-device batches on the primary GPU.
- **Zero-Copy Parameter Broadcast & Async All-Reduce:** Non-blocking parameter replication and gradient reduction.

### 6. Mixed Precision & Quantization
- **Automatic Mixed Precision (AMP):** Context-managed `vulkann.amp.autocast("vulkan", dtype=torch.float16)` combined with `vulkann.amp.GradScaler` to prevent underflow.
- **Post-Training Quantization (`vulkann.quantize`):**
  - In-place Int8 symmetric per-channel weight quantization (`quantize_dynamic_int8`).
  - In-place Int4 packed weight quantization (`quantize_dynamic_int4`) for 75% memory footprint reduction on resource-constrained devices.

### 7. Ecosystem & Standard Math Parity
- **Einstein Summation:** High-performance `vulkann.einsum(equation, *operands)`.
- **Linear Algebra (`vulkann.linalg`):** Matrix norm, vector norm, trace, determinant, inverse, solve, dot, inner, outer products.
- **Fast Fourier Transforms (`vulkann.fft`):** 1D/2D real and complex FFTs (`fft`, `ifft`, `rfft`, `irfft`, `fft2`, `ifft2`).
- **Optimization & Schedulers (`vulkann.optim`):**
  - Optimizers: `Adam`, `AdamW`, `SGD` (with momentum & Nesterov), `RMSprop`, `Adagrad`.
  - Schedulers: `StepLR`, `MultiStepLR`, `ExponentialLR`, `CosineAnnealingLR`, `LinearLR`, `ConstantLR`.
- **Zero-Dependency Safetensors:** Fast binary weight serialization via `torch.save_safetensors` and `torch.load_safetensors`.

---

## 🧱 Architecture & Performance Stack

```
┌────────────────────────────────────────────────────────────────────────┐
│                        User Deep Learning Code                         │
│       import vulkann as torch  /  import vulkann.nn as nn            │
├───────────────────────────────────┬────────────────────────────────────┤
│   High-Level Neural Architectures │   Ecosystem & Training Utilities   │
│   • FlashAttention-2, RoPE        │   • AMP Autocast & GradScaler      │
│   • Mamba (Selective S6)          │   • Optimizers & LR Schedulers     │
│   • Snake / SnakeBeta Periodic    │   • Weight Initializers (nn.init)  │
│   • SwiGLU, RMSNorm, LayerNorm    │   • Safetensors Zero-Copy I/O      │
│   • LSTM, GRU, TransformerEncoder │   • einsum, linalg, fft            │
├───────────────────────────────────┴────────────────────────────────────┤
│                    Autograd & Graph Execution Engine                   │
│   • Tape-based Autograd with custom autograd.Function & Checkpointing  │
│   • Static Graph Compilation: @vulkann.compile (VulkanGraph Replay)   │
│   • Dynamic Heterogeneous Multi-GPU DataParallel (EMA Latency-Balanced)│
├────────────────────────────────────────────────────────────────────────┤
│                       C++ Core Engine (vulkann_core)                  │
│   • Zero-copy Strided Tensor Views & Storage Slicing                   │
│   • Asynchronous Command Submission & Staging Memory Manager           │
│   • Persistent Two-Tier Disk VkPipelineCache (~/.cache/vulkann/)      │
│   • Extensions: VK_EXT_subgroup_size_control, atomic_float, coopmat    │
├────────────────────────────────────────────────────────────────────────┤
│                     Khronos Slang Compute Shaders                      │
│   • WaveActiveSum / WaveActiveMax Subgroup Shuffles (Zero Barriers)    │
│   • Tiled GEMM & Cooperative Matrix Hardware Math                      │
│   • Elementwise, Reductions, Fused Kernels, Quantized Dequant-GEMM     │
├────────────────────────────────────────────────────────────────────────┤
│                      Khronos Vulkan 1.2+ Drivers                       │
│    NVIDIA RTX / AMD Radeon / Intel Arc & Xe / Apple Silicon / Adreno   │
└────────────────────────────────────────────────────────────────────────┘
```

---

## ⚡ Quick Start

### 1. Drop-In Replacement for PyTorch

VulkaNN is designed to mirror PyTorch semantics:

```python
import vulkann as torch
import vulkann.nn as nn
import vulkann.optim as optim

# Detect Vulkan GPU or fallback to CPU
device = "vk:0" if torch.vulkan.is_available() else "cpu"
print(f"Running on {device} ({torch.vulkan.get_device_name(0)})")


# Define an MLP
class MLP(nn.Module):

    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(784, 256),
            nn.LayerNorm(256),
            nn.GELU(),
            nn.Linear(256, 10),
        )

    def forward(self, x):
        return self.net(x)


model = MLP().to(device)
optimizer = optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-2)
criterion = nn.CrossEntropyLoss()

# Training step
x = torch.randn(64, 784, device=device)
y = torch.randint(0, 10, (64,), dtype=torch.int64, device=device)

optimizer.zero_grad()
loss = criterion(model(x), y)
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()

print(f"Step Loss: {loss.item():.4f}")
```

---

## 📖 Examples & Cookbook

VulkaNN ships with canonical, runnable examples in the [`examples/`](examples/) directory:

| Script | Title | Description |
| :--- | :--- | :--- |
| [`examples/01_quickstart_mlp.py`](examples/01_quickstart_mlp.py) | **Quickstart MLP** | Tensor creation, MLP training, autograd, optimizer, and loss evaluation. |
| [`examples/02_autograd_and_gradcheck.py`](examples/02_autograd_and_gradcheck.py) | **Autograd & Gradcheck** | Custom `autograd.Function`, activation checkpointing, and finite-difference validation. |
| [`examples/03_transformer_flash_attention.py`](examples/03_transformer_flash_attention.py) | **Modern Transformer** | FlashAttention-2, SwiGLU, RoPE, RMSNorm, and mixed-precision AMP training. |
| [`examples/04_mamba_and_snake.py`](examples/04_mamba_and_snake.py) | **Mamba & Snake** | Selective State Space sequence modeling and periodic Snake activations. |
| [`examples/05_multigpu_dataparallel.py`](examples/05_multigpu_dataparallel.py) | **Heterogeneous Multi-GPU** | Concurrent training across mixed vendor GPUs with auto-balancing and safetensors I/O. |
| [`examples/06_quantized_inference.py`](examples/06_quantized_inference.py) | **Quantized Inference** | In-place Int8 and Int4 weight quantization with cosine similarity verification. |

### Highlight: Modern Transformer with FlashAttention & AMP

```python
import vulkann as torch
import vulkann.nn as nn
from vulkann.amp import GradScaler, autocast


class ModernTransformerBlock(nn.Module):

    def __init__(self, dim, num_heads):
        super().__init__()
        self.norm1 = nn.RMSNorm(dim)
        self.attn = nn.FlashAttention(embed_dim=dim, num_heads=num_heads)
        self.norm2 = nn.RMSNorm(dim)
        self.mlp = nn.Sequential(
            nn.Linear(dim, dim * 4),
            nn.SwiGLU(),
            nn.Linear(dim * 2, dim),
        )

    def forward(self, x):
        x = x + self.attn(self.norm1(x))
        x = x + self.mlp(self.norm2(x))
        return x


model = ModernTransformerBlock(dim=256, num_heads=8).to("vk:0")
scaler = GradScaler("vulkan")
x = torch.randn(8, 128, 256, device="vk:0")

# Mixed-precision forward and backward pass
with autocast("vulkan", dtype=torch.float16):
    out = model(x)
    loss = out.mean()

scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
```

### Highlight: Heterogeneous Multi-GPU Training

Train seamlessly across an integrated Intel GPU and a discrete NVIDIA/AMD GPU simultaneously:

```python
import vulkann as torch
import vulkann.nn as nn

model = BigNet()
# Automatically probes devices and balances batch shards via closed-loop latency feedback
parallel_model = nn.DataParallel(model, device_ids=["all"], balance="auto")

for x_batch, y_batch in dataloader:
    optimizer.zero_grad()
    parallel_model.zero_grad()

    # Forward pass executes concurrently across all GPUs
    outputs = parallel_model(x_batch)
    loss = criterion(outputs, y_batch)
    loss.backward()

    # Async all-reduce gradients to master parameters
    parallel_model.reduce_gradients()
    optimizer.step()
```

### Highlight: Zero-Dependency Safetensors Serialization

Save and load models at line speed without third-party libraries:

```python
import vulkann as torch

# Save weights to disk
torch.save_safetensors(model.state_dict(), "model.safetensors")

# Load weights directly to target GPU
state_dict = torch.load_safetensors("model.safetensors", device="vk:0")
model.load_state_dict(state_dict)
```

---

## 🛠️ Building & Developing

### Prerequisites
- **C++20 Compiler**: GCC $\ge 11$ or Clang $\ge 14$
- **CMake**: $\ge 3.18$
- **Vulkan Loader & Headers**: `libvulkan-dev` / `vulkan-headers` ($\ge 1.2$)
- **Slang Compiler**: `slangc` $\ge 2024.1$ (from LunarG Vulkan SDK or package manager)
- **Python**: $\ge 3.8$ with `numpy`, `pytest`, `ruff`

### Build from Source

```bash
# 1. Fetch dependencies and Vulkan headers
scripts/fetch_deps.sh

# 2. Configure and build C++ backend + Slang SPIR-V shaders
cmake -S . -B build -DPython_EXECUTABLE=$(which python3)
cmake --build build -j$(nproc)

# 3. Run unit tests
PYTHONPATH=. pytest
```

### Zero-Warning & Correctness Gate
VulkaNN enforces a strict **zero-warning** policy across all C++, Slang, and Python code:

```bash
scripts/check.sh
```

- Enforces `-Werror` in C++ with Allman style via `.clang-format`.
- Enforces strict formatting and linting via `ruff format` and `ruff check`.
- Every operator undergoes finite-difference `torch.gradcheck` verification across CPU, integrated GPUs, and discrete GPUs.

---

## 🤖 Agent & Developer Resources

VulkaNN includes native discoverability files for AI coding agents, autonomous workflows, and LLMs:

- **[`AGENTS.md`](AGENTS.md):** Architectural patterns, constraints (no CUDA, no `.numpy()` in compute kernels, Allman C++ style), and standard idioms.
- **[`llms.txt`](llms.txt):** Structured project overview, core APIs, and cookbook links for LLM context injection.
- **[`llms-full.txt`](llms-full.txt):** Full comprehensive API surface reference including tensor math, neural layers, optimizers, and Vulkan driver flags.

---

## 📜 License
**GNU Lesser General Public License v3.0 (`LGPL-3.0-only`)** — see [LICENSE](LICENSE) and [LICENSE.GPLv3](LICENSE.GPLv3).

- **Free to use in research, commercial, and proprietary applications:** You can `import vulkann` in closed-source applications, cloud services, and training pipelines without having to open-source your models or application code.
- **Anti-plagiarism copyleft on VulkaNN internals:** Any modification to VulkaNN's Slang shaders, C++ backend, or Python source, or any derivative library incorporating VulkaNN code, must be released under LGPLv3 (or GPLv3) with full source code and attribution.
