Kompute GPU Compute Framework
repository·master·Indexed 25 days ago
https://github.com/komputeproject/komputeA cross-vendor GPU compute framework built on Vulkan for high-performance tasks such as machine learning and mobile acceleration. It supports C++ and Python and provides examples for Android, Raspberry Pi 4, and integration with the Godot game engine via custom modules or GDNative.
What's inside Kompute
- Kompute is a general-purpose GPU compute framework designed for cross-vendor graphics cards, including AMD, Qualcomm, and NVIDIA. It is built on a 'Bring-your-own-Vulkan' (BYOV) design, allowing it to integrate seamlessly with existing Vulkan applications. The framework is optimized for high-performance, asynchronous, and parallel processing using GPU family queues, making it suitable for machine learning, mobile development (via Android NDK), and game development.
Explore Kompute integration with Godot Engine
masterThis example demonstrates how to integrate Kompute for GPU-accelerated machine learning within the Godot game engine. It is based on the technical guide "Supercharging Game Development with GPU Accelerated Machine Learning".
The example is composed of three main parts:
- Godot Project: The
project.godotfile used to run the visual demonstration. - Custom Module: Implementation details for a Godot Custom Module.
- GdNative Library: Implementation details for a GdNative shared library.
For a simpler GPU-based example, refer to the
godot_examplesdirectory.- Godot Project: The
Access Python package documentation
masterFor detailed information regarding the Kompute Python package, including API references and usage guides, refer to the documentation located at/docs/overviewwithin the repository.How Kompute's core abstractions work together
masterKompute is built on a hierarchy of abstractions that manage the relationship between CPU (Host) and GPU (Device) memory and execution:
- Manager (
kp::Manager): The high-level entry point that simplifies interaction with GPU sequences and operations. - Sequence (
kp::Sequence): A batch of operations executed on a specific GPU queue. Sequences can be executed synchronously or asynchronously and are coordinated viavk::Fence. - Tensor (
kp::Tensor): The atomic unit of data in Kompute, used to handle both Host and GPU Device data. - Algorithm (
kp::Algorithm): Encapsulates the components required for shader execution, includingvk::Pipelineandvk::DescriptorSetresources. - Operation (
kp::OpBase): A single step executed during a GPU submission. Operations can involve one or morekp::Tensorobjects.
- Manager (
Manage GPU memory with `kp.Tensor`
masterThe
kp.Tensorcomponent manages data in GPU memory. It usesnumpyarrays to wrap GPU memory, providing a primary interface to the GPU.Memory Management Lifecycle:
- Memory is managed by Kompute and persists until the Python object's reference count reaches zero or
destroy()is explicitly called. - Warning on
.data(): Calling.data()on a tensor returns a numpy array that adds an extra reference count to the underlying resources. The resources will not be destroyed until this numpy array object is also destroyed.
Example of memory lifecycle behavior:
import kp m = kp.Manager() t = m.tensor([1,2,3]) # td holds a reference to the underlying tensor memory td = t.data() del t # td is still valid because of the extra refcount from .data() assert td.base.is_init() == True m.destroy() # Frees all memory inside tensors # After manager destruction, the underlying resource is no longer initialized assert td.base.is_init() == False del td # Now the tensor destructor is called as refcount reaches 0import kp m = kp.Manager() t = m.tensor([1,2,3]) td = t.data() del t assert td.base.is_init() == True m.destroy() assert td.base.is_init() == False del td- Memory is managed by Kompute and persists until the Python object's reference count reaches zero or
Handle data with Kompute Tensor
masterThekp.Tensoris the atomic unit in Kompute. It is used to manage and transfer data between the Host (CPU) and the GPU Device memory.How Kompute's core architecture works
masterKompute is organized into several key abstractions that manage the lifecycle of GPU computations:
- Kompute Manager: The central orchestrator. It is responsible for creating and managing the device (GPU) and all child components like tensors and algorithms.
- Kompute Tensor: Structured data used in GPU operations. Tensors hold the actual data that is passed to and from the GPU.
- Kompute Algorithm: An abstraction for the logic (defined by a shader) that is executed on the GPU. It binds together tensors and constants.
- Kompute Sequence: A container for a batch of operations. Instead of executing commands one by one, you record them into a sequence and then dispatch the entire batch to the GPU for efficiency.
- Kompute Operation: The base class for all individual tasks (like dispatching an algorithm or syncing memory) that can be recorded into a sequence.
Core Python components in Kompute
masterThe Kompute Python package is built around three primary classes:
kp.Manager: Manages high-level GPU and Kompute resources.kp.Sequence: Contains a set of recorded operations that can be reused.kp.Tensor: The core data component used to manage GPU and host data for operations.
Note that
kp.OpBaseand its subclasses are not directly exposed in Python. Instead, you interact with operations throughkp.Managerorkp.Sequencemethods.How custom operations work in Kompute
masterKompute uses an extensible architecture where core components can be extended by building custom operations. All operations inherit from the
kp::OpBaseclass.To create operations that include custom shader logic (requiring Compute Pipelines, DescriptorSets, etc.), you should inherit from
kp::OpAlgoBaseinstead of the generickp::OpBase.Understanding the lifecycle of an operation is critical because functions are called in a specific sequence during execution. This sequence ensures that resources are correctly initialized, commands are recorded to the Vulkan command buffer, and data is synchronized between host and device memory.
Key Features of Kompute
masterKompute provides several core capabilities for GPU acceleration:
- Multi-language Support: A flexible Python module for ease of use and a C++ SDK for high-performance optimizations.
- Asynchronous & Parallel Processing: Leverages GPU family queues for non-blocking operations.
- Mobile Compatibility: Mobile-enabled with support for Android NDK across multiple architectures.
- Vulkan Integration: Uses a BYOV (Bring-your-own-Vulkan) design to coexist with other Vulkan-based applications.
- Explicit Memory Management: Provides explicit control over memory ownership and management between the GPU and the host.
Mobile support and Android integration
masterKompute is optimized for mobile environments, specifically Android.
- Dynamic Loading: The build system supports dynamic loading of the Vulkan shared library for Android.
- Android NDK: There is a working Android NDK wrapper for the C++ headers.
- Integration: You can run end-to-end examples in Android Studio using the provided Android NDK mobile Kompute ML application code.
Understand Kompute memory management principles
masterKompute follows a hierarchical, acyclic ownership model for memory management. Key principles include:
- Optional Management: Kompute only manages memory for resources it creates. If you provide your own Vulkan resources, Kompute will not manage their lifecycle.
- Single Top Manager: A top-level manager (e.g.,
kp::Manager) acts as the primary owner of GPU resources and ensures all resources it owns are released when the manager is destroyed. - Weak Pointer Safety: The manager uses weak pointers to track resources. If a resource created outside of Kompute's management is destroyed, the manager ensures it is released safely without attempting to manage it.
- Resource Lifecycle: Once a resource is destroyed, it cannot be recreated. Resources can only be rebuilt if they have not been destroyed.
- BYOV (Bring Your Own VulkanSDK): Kompute is designed to work alongside existing Vulkan SDK-enabled applications. You can initialize Kompute components using existing Vulkan resources rather than letting Kompute create them from scratch.