Python Bindings and Frontend
This component provides Python bindings via pybind11 to build SDFGs with Python.
Additionally, this component provides the @native decorator, which automatically
converts Python functions into SDFGs and codegens them for the available target.
- Bindings: Build SDFGs programmatically with Python
- AST Parser: Automatically compile Python/NumPy code to optimized native code
- Targets: Support for CPU (sequential, OpenMP), CUDA GPUs, and other accelerators
Build from Sources
To build the Python component from sources run pip on the component's directory:
pip install -e python/
Requirements
Python Dependencies
- Python >= 3.11
- NumPy >= 1.19.0
System Dependencies
The following system dependencies must be installed (same as core components):
sudo apt-get install -y libgmp-dev libzstd-dev
sudo apt-get install -y nlohmann-json3-dev
sudo apt-get install -y libboost-graph-dev libboost-graph1.74.0
sudo apt-get install -y libisl-dev
Compiler Requirements
Clang/LLVM 19 is required for code generation. Install it with:
# Ubuntu/Debian
sudo apt-get install -y clang-19 llvm-19
For CUDA support, you also need the NVIDIA CUDA Toolkit installed.
Usage
The @native Decorator
The @native decorator is the primary way to use the Python frontend. It automatically:
- Parses the Python function's AST
- Converts it to an SDFG representation
- Applies optimizations based on the target
- Compiles to native code
- Executes and returns results
Basic Example
from docc.python import native
import numpy as np
@native
def vector_add(A, B, C, N):
for i in range(N):
C[i] = A[i] + B[i]
# Usage
N = 1024
A = np.random.rand(N).astype(np.float64)
B = np.random.rand(N).astype(np.float64)
C = np.zeros(N, dtype=np.float64)
vector_add(A, B, C, N) # JIT compiles and executes
Compilation Targets
The @native decorator accepts a target parameter to specify the code generation backend:
target="none" (Default)
No scheduling or optimization is applied. The SDFG is compiled as-is without parallelization. Useful for debugging or when you want to manually control the generated code.
@native(target="none")
def simple_loop(A, B, N):
for i in range(N):
B[i] = A[i] * 2.0
target="sequential"
Generates optimized sequential code with SIMD vectorization.
import math
@native(target="sequential")
def vectorized_sin(A, B):
for i in range(A.shape[0]):
B[i] = math.sin(A[i])
N = 128
A = np.random.rand(N).astype(np.float64)
B = np.zeros(N, dtype=np.float64)
vectorized_sin(A, B)
target="openmp"
Generates parallel code using OpenMP. Suitable for multi-core CPUs. Loops are automatically parallelized with appropriate scheduling.
@native(target="openmp", category="desktop")
def parallel_add(A, B, C, N):
for i in range(N):
C[i] = A[i] + B[i]
# Executes in parallel across CPU cores
N = 1000000
A = np.random.rand(N).astype(np.float64)
B = np.random.rand(N).astype(np.float64)
C = np.zeros(N, dtype=np.float64)
parallel_add(A, B, C, N)
target="cuda"
Generates CUDA code for NVIDIA GPUs. Loops are mapped to GPU thread blocks and threads.
@native(target="cuda", category="server")
def gpu_add(A, B, C, N):
for i in range(N):
C[i] = A[i] + B[i]
# Executes on GPU (data is automatically transferred)
N = 1024
A = np.random.rand(N).astype(np.float64)
B = np.random.rand(N).astype(np.float64)
C = np.zeros(N, dtype=np.float64)
gpu_add(A, B, C, N)
The category Parameter
The category parameter provides hints to the scheduler about the target hardware:
"edge""desktop""server"
@native(target="openmp", category="desktop")
def cpu_kernel(A, B):
...
@native(target="cuda", category="server")
def gpu_kernel(A, B):
...
Advanced: Manual Compilation
For more control, you can manually compile and cache the SDFG:
@native(target="openmp")
def my_kernel(A, B, C, N):
for i in range(N):
C[i] = A[i] + B[i]
# Pre-compile with sample arguments
compiled = my_kernel.compile(A, B, C, N)
# Reuse compiled version
result = compiled(A, B, C, N)
# Access the underlying SDFG
sdfg = my_kernel.last_sdfg
License
This component is part of docc and is published under the BSD-3-Clause license. See LICENSE for details.
Release files for docc-compiler 0.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 134.4 MB
Release files / docc_compiler-0.8.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | docc_compiler-0.8.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.14 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
360c5006311e12f037617a350e72882573eb1390d52cfbc2a91def5f62e1bd96
|
|
BLAKE2b-256 checksum How to use checksums |
2580aea4e113cdc391811174cdafbe21906ee9631125e8ffc683844951940cbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
| Download URL | docc_compiler-0.8.0-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 14.0 MB |
| Tags | CPython 3.14 Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
744cbe00ab3c050160f8905060bcb5091f384d04ba557428ea0dd362ed94b804
|
|
BLAKE2b-256 checksum How to use checksums |
e24a3e9520065a9850101ac12edd0c72453f003cd948a15e2d0c7e82ac8bdb6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp314-cp314-macosx_14_0_arm64.whl
| Download URL | docc_compiler-0.8.0-cp314-cp314-macosx_14_0_arm64.whl |
|---|---|
| Size | 4.8 MB |
| Tags | CPython 3.14 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
04181c5551fd30b0ddb21c9ec1c2b902c6354429c77d75a7c9f21b42761a6625
|
|
BLAKE2b-256 checksum How to use checksums |
8ae6af03f7304e05bc2e903102dfdd59c767aed8600b9578f90c34fe0cc2c1c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | docc_compiler-0.8.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
fe4f629f0d4e4266d880b6fc4b74bf6b7a866371452b6593644104251e42c205
|
|
BLAKE2b-256 checksum How to use checksums |
4f52615fb7929f111c7a76c594889d7c1392e0583ab33edd9586f3f60779f1d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
| Download URL | docc_compiler-0.8.0-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 14.0 MB |
| Tags | CPython 3.13 Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
6499c3ef975b3fa15261e6678ec32b0160a32cedc912fa3bf4d59145f9bdd1df
|
|
BLAKE2b-256 checksum How to use checksums |
6c83c97437bc556a5f58fd353a394b042881e5efdfe8c923f1daa09173cd394f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp313-cp313-macosx_14_0_arm64.whl
| Download URL | docc_compiler-0.8.0-cp313-cp313-macosx_14_0_arm64.whl |
|---|---|
| Size | 4.8 MB |
| Tags | CPython 3.13 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
6378e905de12495e34908946ff9fd9d8dfe428b93f428166500d15cdbcf65ab8
|
|
BLAKE2b-256 checksum How to use checksums |
5ea28a7c0b28e667f267446a3e4c022cded11085583e98df7e3a36d2945b8bd0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | docc_compiler-0.8.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
243a181da0d9fb440e444762da3aa700fc085ef215f94f9fd73c6aa6f16acdb5
|
|
BLAKE2b-256 checksum How to use checksums |
4a6abcf3ce1940544c892c2a4e563e5f00671c5b1703941cdb8319d1e99259b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
| Download URL | docc_compiler-0.8.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 14.0 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
9bfe61f54256f78fa007a60af5b82f1d8f8f2ae3e15210e30a94fc4f34504746
|
|
BLAKE2b-256 checksum How to use checksums |
4403ca431eb071a27ee955271e0993eeda9291235eb700677af81732c1b9015a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp312-cp312-macosx_14_0_arm64.whl
| Download URL | docc_compiler-0.8.0-cp312-cp312-macosx_14_0_arm64.whl |
|---|---|
| Size | 4.8 MB |
| Tags | CPython 3.12 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
921268089458fdf89e9dfcc4792111ca89046f6532b59ffb7f9bc74acfbf8653
|
|
BLAKE2b-256 checksum How to use checksums |
5fc520a106bd79c9d53196cdb708ae578ea4e426e6bed70658837a5a2c5b5bb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | docc_compiler-0.8.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 14.8 MB |
| Tags | CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
f9fecca02d25613dc75d20f86003b0ab9dd9319f9444c56fd55726e667cd0ce4
|
|
BLAKE2b-256 checksum How to use checksums |
fe5f742e659d4f1997bc13988623bee70b2874143099378969fb45cbedd77d58
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
| Download URL | docc_compiler-0.8.0-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 14.0 MB |
| Tags | CPython 3.11 Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
78f8256411b34d19d34677ff1ff7f57f139f942b5578e6a181b14519b6df7c18
|
|
BLAKE2b-256 checksum How to use checksums |
10e2491ffda1528b03f68584395e28687e366f2618eaa99c3b781c18c53e8a49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|
Release files / docc_compiler-0.8.0-cp311-cp311-macosx_14_0_arm64.whl
| Download URL | docc_compiler-0.8.0-cp311-cp311-macosx_14_0_arm64.whl |
|---|---|
| Size | 4.8 MB |
| Tags | CPython 3.11 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
531c913b976708709857d6f87a3c86334ee82be20c0c0fc41857c3db99a7903b
|
|
BLAKE2b-256 checksum How to use checksums |
eac600928293d08ea0be27a000f64c91807575d1a8d6113b5c471b3c5031827c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.14
|