Skip to main content

KPU (Knowledge Processing Unit) Simulator Python API with native C++ backend

Project description

KPU Python Package

High-level Python API for the KPU (Knowledge Processing Unit) simulator - a multi-fidelity neural network accelerator simulator with decorator-based compilation and comprehensive quantization support.

Installation

pip install stillwater-kpu

Optional dependencies:

pip install stillwater-kpu[torch]     # PyTorch integration (torch.compile backend)
pip install stillwater-kpu[bfloat16]  # Native bfloat16 support via ml_dtypes

Quick Start

import kpu
import numpy as np

# Define a neural network with @kpu.compile
@kpu.compile
def mlp(x, w1, w2):
    h = kpu.relu(x @ w1)
    return h @ w2

# Create tensors
x = kpu.Tensor(np.random.randn(32, 784).astype(np.float32))
w1 = kpu.Tensor(np.random.randn(784, 128).astype(np.float32))
w2 = kpu.Tensor(np.random.randn(128, 10).astype(np.float32))

# Execute
result = mlp(x, w1, w2)
print(result.shape)  # (32, 10)

Key Features

Multi-Fidelity Simulation

  • BEHAVIORAL: Computes actual values for functional correctness
  • TRANSACTIONAL: Statistical timing model for performance estimation
  • CYCLE_ACCURATE: Full timing simulation with C++ backend

Comprehensive Operator Support

  • Matrix: matmul, linear
  • Convolution: conv2d with stride/padding
  • Attention: scaled_dot_product_attention, multi_head_attention
  • Pooling: max_pool2d, avg_pool2d, adaptive_avg_pool2d
  • Activation: relu, gelu, silu, sigmoid, tanh, softmax
  • Normalization: layer_norm, batch_norm2d
  • Elementwise: exp, log, sqrt, +, -, *, /
  • Shape: reshape, transpose, concat, flatten

Quantization Support (v0.7.x)

Full quantization infrastructure for simulating low-precision inference:

Type Bits Memory Reduction
FP16 16 2x
BF16 16 2x
INT8 8 4x
FP8 (E4M3/E5M2) 8 4x
INT4 4 8x
FP4 4 8x
# INT8 quantization
from kpu import quantize, dequantize, compute_scale_zero_point

scale, zp = compute_scale_zero_point(weights)
w_int8 = quantize(weights, scale, zp)

# Calibration for post-training quantization
from kpu import CalibrationObserver, CalibrationMethod

observer = CalibrationObserver(method=CalibrationMethod.PERCENTILE)
for batch in calibration_data:
    observer.observe(activations)
params = observer.compute_params()

Kernel Fusion

Automatic fusion of common patterns for reduced memory traffic:

  • MatMul + Bias + ReLU/GELU/SiLU
  • Conv2D + BatchNorm + Activation
@kpu.compile(optimize=True)  # Fusion enabled by default
def fused_layer(x, w, b):
    return kpu.relu(x @ w + b)  # Fused into single operation

PyTorch Integration

Use KPU as a torch.compile backend:

import torch
model = torch.compile(my_model, backend="kpu")
output = model(input)

# With timing statistics
model = torch.compile(my_model, backend="kpu_transactional")
stats = kpu.get_torch_compile_stats()
print(f"Estimated cycles: {stats.cycles}")

Simulation Modes

import kpu

# Functional simulation (default)
kpu.set_fidelity(kpu.BEHAVIORAL)

# Performance estimation
kpu.set_fidelity(kpu.TRANSACTIONAL)
kpu.set_clock_frequency(1.0)  # 1 GHz

# Execute and get timing
result = model(input)
stats = model.get_stats()
print(f"Cycles: {stats.cycles}, GFLOPS: {stats.gflops:.1f}")

Architecture

Python Code with @kpu.compile
        ↓
    Tracing (build OpGraph)
        ↓
    Fusion Optimization (optional)
        ↓
    DFX IR Emission
        ↓
    Runtime Execution
    ├── BEHAVIORAL (pure Python, computes values)
    ├── TRANSACTIONAL (C++ bindings, statistical timing)
    └── CYCLE_ACCURATE (C++ bindings, full timing)

Examples

# CNN for image classification
@kpu.compile
def cnn(x, conv_w, fc_w):
    h = kpu.relu(kpu.conv2d(x, conv_w, padding=1))
    h = kpu.max_pool2d(h, kernel_size=2)
    h = h.reshape(h.shape[0], -1)
    return h @ fc_w

# Transformer attention
@kpu.compile
def attention(q, k, v):
    return kpu.scaled_dot_product_attention(q, k, v)

# Quantized inference
from kpu import int4_linear, calibrate_percentile

params = calibrate_percentile(weights)
output = int4_linear(x, weights, params)

Links

License

Apache-2.0

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

stillwater_kpu-0.8.0.tar.gz (174.6 kB view details)

Uploaded Source

Built Distributions

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

stillwater_kpu-0.8.0-cp312-cp312-win_amd64.whl (389.0 kB view details)

Uploaded CPython 3.12Windows x86-64

stillwater_kpu-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (399.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

stillwater_kpu-0.8.0-cp312-cp312-macosx_10_15_x86_64.whl (363.1 kB view details)

Uploaded CPython 3.12macOS 10.15+ x86-64

stillwater_kpu-0.8.0-cp311-cp311-win_amd64.whl (387.2 kB view details)

Uploaded CPython 3.11Windows x86-64

stillwater_kpu-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (399.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

stillwater_kpu-0.8.0-cp311-cp311-macosx_10_15_x86_64.whl (361.2 kB view details)

Uploaded CPython 3.11macOS 10.15+ x86-64

stillwater_kpu-0.8.0-cp310-cp310-win_amd64.whl (386.6 kB view details)

Uploaded CPython 3.10Windows x86-64

stillwater_kpu-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (398.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

stillwater_kpu-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl (359.7 kB view details)

Uploaded CPython 3.10macOS 10.15+ x86-64

stillwater_kpu-0.8.0-cp39-cp39-win_amd64.whl (390.3 kB view details)

Uploaded CPython 3.9Windows x86-64

stillwater_kpu-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (398.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

stillwater_kpu-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl (359.8 kB view details)

Uploaded CPython 3.9macOS 10.15+ x86-64

File details

Details for the file stillwater_kpu-0.8.0.tar.gz.

File metadata

  • Download URL: stillwater_kpu-0.8.0.tar.gz
  • Upload date:
  • Size: 174.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stillwater_kpu-0.8.0.tar.gz
Algorithm Hash digest
SHA256 f7c80f1fc2111d6454db74876a796e570604b3385362f1ca552040820447b376
MD5 67543b1f7c5ac2eab5a70e37d8174d2b
BLAKE2b-256 d6f560ddc12acce218e4012956fd097dbe422891db16d25166996920483154d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0.tar.gz:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 84bb9b0140a0d99f3f189e9b35dfb7fbd4cb2486b91b66e25e13f7d8e59e54d9
MD5 6b7cec758bde33c4a0d67f4e1310a113
BLAKE2b-256 9cabcf0588883241afbbc621cb7ad2951e7599d3ee5f9e9d19ff01608dd86a0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp312-cp312-win_amd64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5eb09dc77f1410462c50b75ab2d8ce729db97f198ef8016907bb52dcc4afccc3
MD5 47b4eaf484a82c09c4a04577e2ede3bf
BLAKE2b-256 619fe7a769ef5a15a9d1f39518adbdd776ae707109d58ed407290a134dde90de

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp312-cp312-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 f4fe36f0c215bffe2b910f9f849200a903f546d1597f80ae21cc3df16907e95a
MD5 e15f6366a076d4200092a54ba8627516
BLAKE2b-256 46f16e4525996dad750a806e71c5b1c9fe81bbad93d5c9df22d398b35fbc47e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp312-cp312-macosx_10_15_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 81510912d612ceaee966be968d3aef28e2aa2061f1e9e47cb4bcd19a19bdc99c
MD5 ea199558d5d5e00c4026db1bee1ec276
BLAKE2b-256 a5aac5a1ba81cc899b968ece8d57abbe2b583804d308c20baa143ba765f61d78

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp311-cp311-win_amd64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a6d8d0041454c5df12b5d98dec80cdcf027491e272a7e9ed763bb97b7852c596
MD5 6f27fc1142eff4431edb571ce16dd8b0
BLAKE2b-256 dae8e9c70598b56ad05e0ec39273b59d1d5029f5273470048b5a1cab7cb43a8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp311-cp311-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 740ec959ff5b2fd11537733322408c064f839139b2ab167196dc719149279765
MD5 204052a75ebe5d78176e0b374f5e30f9
BLAKE2b-256 ea7a4db250e8b2c372519f77748b10c1a67f7f5c98577899747452aa4aea4af8

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp311-cp311-macosx_10_15_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7084a3178fc14926be4935d4bb69addfb442e4bc751bc372fd7bc6b9b0ab4918
MD5 740a983d03f9fb635e7d9f2997901b85
BLAKE2b-256 9019b4314365446629d00a79a544540c00a09057f8b4db9b0585d0b347069079

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp310-cp310-win_amd64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e3e058638ec7457a377a6a98f6f4ec4a4d6d55ffeb3b679aa0fb8bca504dbd3e
MD5 81de1280c1f7444e3499f9931b4e8b48
BLAKE2b-256 f1fd630e2ab116e0e22520bd788bcf178e4ded7b2bab61db0a4b3741d30484d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 51d4dab8726688eaeee32ddd9f10cf6f218c827ab317365f4c73b346095f15b4
MD5 60899a4cb235638c7f8cbfd7a2c47e1c
BLAKE2b-256 7560c8770a123485ca0bef8c2d77cc4e157ce5f885e5cb49ca48adb777181fa8

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp310-cp310-macosx_10_15_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e5743c28aa5db36b5cff0f42007373bd73f1de1885461223560f962e5d2af750
MD5 10e9b5fa910075a20a89abf5615dbe87
BLAKE2b-256 ca5b917188b27ab5dd1dbe0d2da23b3bb8424b6ea603bfc81444f43f42f51867

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp39-cp39-win_amd64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c8a330348fc047228aca10756043e8c8d736b237877ddb4fdf083a67b2f9d98a
MD5 246423469e1253f0830953c1a5f8e3f8
BLAKE2b-256 4de344a15481e338f519647608481373bdba6a14e9b689cc9ece2060b8a67f52

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stillwater_kpu-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for stillwater_kpu-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 3f8305cd9090f1529b751fb527edce36717f2be7045065fb1488fd8c99d16733
MD5 61b84806c994bb0f8f5eb62797dde8ae
BLAKE2b-256 68d509c68533b59d01569c262327decdd1dcf200c6381ca0b945d60bca9f4dca

See more details on using hashes here.

Provenance

The following attestation bundles were made for stillwater_kpu-0.8.0-cp39-cp39-macosx_10_15_x86_64.whl:

Publisher: python-publish.yml on stillwater-sc/kpu-sim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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