Skip to main content

CUDA kernels for machine learning systems optimization

Project description

kernel-craft Python API

CUDA convolution kernels for ML training-time optimization, exposed to Python with numpy and PyTorch support.

Installation

Option 1: pip (recommended for users)

pip install kernel-craft

Requires:

  • Python 3.11 - 3.12
  • numpy >= 1.20
  • CUDA runtime (for GPU execution)

Option 2: Build with Python (recommended for distribution)

cd src/python
python -m build

The .so file will be at src/python/build/kernel_craft_python.cpython-*.so.

Option 3: Build with CMake

cd /path/to/kernel-craft
mkdir build && cd build
cmake ..
make kernel_craft_python

The module will be at src/python/build/kernel_craft_python.cpython-*.so.

Usage

import sys
sys.path.insert(0, 'src/python/build")

import kernel_craft_python as kc
import numpy as np

# Input: 2D float32 numpy array
input = np.random.randn(256, 256).astype(np.float32)
kernel = np.random.randn(3, 3).astype(np.float32)

# Naive convolution
out = kc.conv_naive(input, kernel)  # -> np.ndarray

# Tiled convolution with configurable tile size
out = kc.conv_tiled(input, kernel, tile_w=8, tile_h=8)  # -> np.ndarray

# Phase 10: Inference kernels
# INT8 quantized convolution
input_scale = kc.compute_quantization_scale(input)
kernel_scale = kc.compute_quantization_scale(kernel)
out_int8 = kc.conv_int8_naive(input, kernel, input_scale, kernel_scale, 1.0)

# Batch Normalization folding (pre-compute for inference)
conv_weights = np.random.rand(64, 3, 3, 3).astype(np.float32)
conv_bias = np.random.rand(64).astype(np.float32)
bn_mean = np.random.rand(64).astype(np.float32)
bn_variance = np.random.rand(64).astype(np.float32) + 0.01
bn_gamma = np.random.rand(64).astype(np.float32) + 0.5
bn_beta = np.random.rand(64).astype(np.float32) - 0.05
folded_weights, folded_bias = kc.bn_folding(
    conv_weights, conv_bias, bn_mean, bn_variance, bn_gamma, bn_beta
)

# Fused Conv+ReLU
out_relu = kc.conv_relu(input, kernel, tiled=True)

Version

import kernel_craft
print(kernel_craft.__version__)  # "0.1.1"

Or via the module directly:

import kernel_craft_python as kc
print(kc.__version__)  # "0.1.1"

PyTorch Tensors

import torch
import kernel_craft_python as kc

# Input: 2D float32 PyTorch tensor on CUDA
input = torch.rand(256, 256, dtype=torch.float32, device='cuda')
kernel = torch.rand(3, 3, dtype=torch.float32, device='cuda')

# Naive convolution
out = kc.conv_naive(input, kernel)  # -> torch.Tensor on GPU

# Tiled convolution
out = kc.conv_tiled(input, kernel, tile_w=16, tile_h=16)  # -> torch.Tensor on GPU

API Reference

Function Input Type Output Type Description
conv_naive(input, kernel) np.ndarray or Tensor np.ndarray or Tensor Baseline convolution
conv_tiled(input, kernel, tile_w, tile_h) np.ndarray or Tensor np.ndarray or Tensor Tiled convolution
conv_int8_naive(input, kernel, input_scale, kernel_scale, output_scale) np.ndarray np.ndarray INT8 quantized
bn_folding(conv_weights, conv_bias, bn_mean, bn_variance, bn_gamma, bn_beta, epsilon) np.ndarray tuple(np.ndarray, np.ndarray) BN folding
conv_relu(input, kernel, tiled) np.ndarray np.ndarray Fused Conv+ReLU
compute_quantization_scale(data) np.ndarray float INT8 scale

Parameters

  • input: Input image (2D, float32)
  • kernel: Convolution kernel (2D, float32, odd dimension)
  • tile_w: Tile width for tiled convolution (default: 8)
  • tile_h: Tile height for tiled convolution (default: 8)
  • input_scale: Scale factor for INT8 quantization
  • kernel_scale: Scale factor for INT8 quantization
  • output_scale: Scale factor for INT8 dequantization
  • conv_weights: 4D array [C_out, C_in, K_h, K_w]
  • conv_bias: 1D array [C_out] or None
  • bn_mean, bn_variance, bn_gamma, bn_beta: 1D arrays [C_out]
  • epsilon: Small constant for numerical stability (default: 1e-5)
  • tiled: Use tiled implementation for Conv+ReLU (default: False)

Supported Tile Sizes

  • 8x8 (default, best overall performance)
  • 16x16
  • 32x32

Error Handling

All functions raise RuntimeError with descriptive messages for:

  • Invalid input dimensions (must be 2D)
  • Invalid kernel dimensions (must be 2D, square, odd-sized)
  • Invalid dtype (must be float32)
  • CUDA errors (kernel launch failures, memory errors)

Quick Start with pip

import kernel_craft
import numpy as np

input = np.random.randn(256, 256).astype(np.float32)
kernel = np.random.randn(3, 3).astype(np.float32)

# Works with both import styles
out = kernel_craft.conv_naive(input, kernel)  # Recommended

# Or with the internal module name
import kernel_craft_python as kc
out = kc.conv_tiled(input, kernel, tile_w=8, tile_h=8)

Publishing to PyPI

# Build package
cd src/python
python -m build

# Upload to TestPyPI
twine upload --repository testpypi dist/*

# Upload to PyPI
twine upload dist/*

Requirements

  • Python 3.11 - 3.12
  • numpy >= 1.20
  • CUDA Toolkit (for building, not for installed .so)

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

kernel_craft-0.1.6.tar.gz (127.5 kB view details)

Uploaded Source

Built Distribution

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

kernel_craft-0.1.6-py3-none-any.whl (117.7 kB view details)

Uploaded Python 3

File details

Details for the file kernel_craft-0.1.6.tar.gz.

File metadata

  • Download URL: kernel_craft-0.1.6.tar.gz
  • Upload date:
  • Size: 127.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for kernel_craft-0.1.6.tar.gz
Algorithm Hash digest
SHA256 fd10e4b300ff3e295406a6bd1fc2d0c72d2aebb2f74d6eb90bb2140040582a8d
MD5 40e8712dece1b751b7b2f490408f3a1b
BLAKE2b-256 aac1f039cea50e0d2e72804e5b92874e7d73e57fa3d275945a72acd9799757d8

See more details on using hashes here.

File details

Details for the file kernel_craft-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: kernel_craft-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 117.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for kernel_craft-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0d838755012daf1bdbaaaeef0bd9983cee7c9cd82d1f16a83d3bdfdbcbcbed0e
MD5 4de3ff348d3f5dd7665df0047f84f7b4
BLAKE2b-256 798aea01e4fd487b9873cfc1ad2ae0f2e2af323b5b124a1a1d6ee66646f4526e

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