Kompute GPU Compute Framework

repository·master·Indexed 25 days ago

https://github.com/komputeproject/kompute

A 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.

Tokens
19.8K
Snippets
38
Records
122
Agent score
81%

What's inside Kompute

  1. Overview of Kompute

    master
    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.
  2. Explore Kompute integration with Godot Engine

    master

    This 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:

    1. Godot Project: The project.godot file used to run the visual demonstration.
    2. Custom Module: Implementation details for a Godot Custom Module.
    3. GdNative Library: Implementation details for a GdNative shared library.

    For a simpler GPU-based example, refer to the godot_examples directory.

  3. How Kompute's core abstractions work together

    master

    Kompute 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 via vk::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, including vk::Pipeline and vk::DescriptorSet resources.
    • Operation (kp::OpBase): A single step executed during a GPU submission. Operations can involve one or more kp::Tensor objects.
  4. Manage GPU memory with `kp.Tensor`

    master

    The kp.Tensor component manages data in GPU memory. It uses numpy arrays 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 0
    import 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
  5. How Kompute's core architecture works

    master

    Kompute 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.
  6. Core Python components in Kompute

    master

    The 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.OpBase and its subclasses are not directly exposed in Python. Instead, you interact with operations through kp.Manager or kp.Sequence methods.

  7. How custom operations work in Kompute

    master

    Kompute uses an extensible architecture where core components can be extended by building custom operations. All operations inherit from the kp::OpBase class.

    To create operations that include custom shader logic (requiring Compute Pipelines, DescriptorSets, etc.), you should inherit from kp::OpAlgoBase instead of the generic kp::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.

  8. Key Features of Kompute

    master

    Kompute 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.
  9. Mobile support and Android integration

    master

    Kompute 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.
  10. Understand Kompute memory management principles

    master

    Kompute 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.