From e7890bbb4d82ebfb855022e93a2de8b64091cd98 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sat, 3 Dec 2022 17:48:51 +0000 Subject: [PATCH 1/9] Delete blank Kompute.hpp file --- single_include/kompute/Kompute.hpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 single_include/kompute/Kompute.hpp diff --git a/single_include/kompute/Kompute.hpp b/single_include/kompute/Kompute.hpp deleted file mode 100644 index e69de29b..00000000 From cd831a76fb9c7aca1efe3654ec49080e8e4f2f7b Mon Sep 17 00:00:00 2001 From: crydsch Date: Tue, 2 May 2023 17:52:53 +0200 Subject: [PATCH 2/9] Fix vulkan version parsing Signed-off-by: crydsch --- cmake/check_vulkan_version.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/check_vulkan_version.cmake b/cmake/check_vulkan_version.cmake index 56d8c44f..0372d320 100644 --- a/cmake/check_vulkan_version.cmake +++ b/cmake/check_vulkan_version.cmake @@ -11,7 +11,7 @@ function(check_vulkan_version) file(STRINGS ${VULKAN_CORE_H} VULKAN_HEADER_VERSION_LINE REGEX "^#define VK_HEADER_VERSION ") string(REGEX MATCHALL "[0-9]+" VULKAN_HEADER_VERSION "${VULKAN_HEADER_VERSION_LINE}") file(STRINGS ${VULKAN_CORE_H} VULKAN_HEADER_VERSION_LINE2 REGEX "^#define VK_HEADER_VERSION_COMPLETE ") - if(${VULKAN_HEADER_VERSION_LINE2}) + if(NOT ${VULKAN_HEADER_VERSION_LINE2} STREQUAL "") string(REGEX MATCHALL "[0-9]+" VULKAN_HEADER_VERSION2 "${VULKAN_HEADER_VERSION_LINE2}") list(LENGTH VULKAN_HEADER_VERSION2 _len) # Versions >= 1.2.175 have an additional numbers in front of e.g. '0, 1, 2' instead of '1, 2' @@ -71,7 +71,7 @@ function(check_vulkan_version) message(FATAL_ERROR "No GPU supporting Vulkan found in vulkaninfo. Does your GPU (driver) support Vulkan?") endif() - string(REGEX MATCHALL "apiVersion[ ]*=[ ]*[0-9a-fA-F]+[ ]+[(]([0-9]+[.][0-9]+[.][0-9]+)[)]" GPU_API_VERSIONS ${VULKAN_INFO_OUTPUT}) + string(REGEX MATCHALL "apiVersion[ ]*=[ ]*[0-9a-fA-F]*[ ]*[(]*([0-9]+[.][0-9]+[.][0-9]+)[)]*" GPU_API_VERSIONS ${VULKAN_INFO_OUTPUT}) if(NOT GPU_API_VERSIONS) message(FATAL_ERROR "No valid Vulkan API version found in vulkaninfo. Does your GPU (driver) support Vulkan?") endif() @@ -93,7 +93,7 @@ function(check_vulkan_version) list(GET GPU_API_VERSIONS ${INDEX} API_VERSION) # Extract API version - if(${API_VERSION} MATCHES "apiVersion[ ]*=[ ]*[0-9a-fA-F]+[ ]+[(]([0-9]+[.][0-9]+[.][0-9]+)[)]") + if(${API_VERSION} MATCHES "apiVersion[ ]*=[ ]*[0-9a-fA-F]*[ ]*[(]*([0-9]+[.][0-9]+[.][0-9]+)[)]*") set(VULKAN_DRIVER_VERSION ${CMAKE_MATCH_1}) else() message(FATAL_ERROR "API version match failed. This should not have happened...") From f91f6d4e63861444fa4687fd6eae543e129b995f Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:09:50 -0400 Subject: [PATCH 3/9] replaceTensorDataTypes with std::type_info replaces kp::Tensor::TensorDataTypes with std::type_info. This allows you to use any struct as a datatype Signed-off-by: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> --- python/src/main.cpp | 21 ++++-------- src/OpTensorCopy.cpp | 3 +- src/Tensor.cpp | 60 ++++----------------------------- src/include/kompute/Manager.hpp | 16 ++++++++- src/include/kompute/Tensor.hpp | 22 +++++------- 5 files changed, 38 insertions(+), 84 deletions(-) diff --git a/python/src/main.cpp b/python/src/main.cpp index 6c0f640c..8da69669 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -258,34 +258,25 @@ PYBIND11_MODULE(kp, m) flatdata.size(), std::string(py::str(flatdata.dtype()))); if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, + return self.tensor(info.ptr, flatdata.size(), - sizeof(float), - kp::Tensor::TensorDataTypes::eFloat, tensor_type); } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, + return self.tensor( + info.ptr, flatdata.size(), - sizeof(uint32_t), - kp::Tensor::TensorDataTypes::eUnsignedInt, tensor_type); } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, + return self.tensor(info.ptr, flatdata.size(), - sizeof(int32_t), - kp::Tensor::TensorDataTypes::eInt, tensor_type); } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, + return self.tensor(info.ptr, flatdata.size(), - sizeof(double), - kp::Tensor::TensorDataTypes::eDouble, tensor_type); } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, + return self.tensor(info.ptr, flatdata.size(), - sizeof(bool), - kp::Tensor::TensorDataTypes::eBool, tensor_type); } else { throw std::runtime_error( diff --git a/src/OpTensorCopy.cpp b/src/OpTensorCopy.cpp index 1eaf428b..116d4ff7 100644 --- a/src/OpTensorCopy.cpp +++ b/src/OpTensorCopy.cpp @@ -2,6 +2,7 @@ #include "kompute/operations/OpTensorCopy.hpp" #include "kompute/Tensor.hpp" +#include namespace kp { @@ -16,7 +17,7 @@ OpTensorCopy::OpTensorCopy(const std::vector>& tensors) "Kompute OpTensorCopy called with less than 2 tensor"); } - kp::Tensor::TensorDataTypes dataType = this->mTensors[0]->dataType(); + const std::type_info& dataType = this->mTensors[0]->dataType(); uint32_t size = this->mTensors[0]->size(); for (const std::shared_ptr& tensor : tensors) { if (tensor->dataType() != dataType) { diff --git a/src/Tensor.cpp b/src/Tensor.cpp index ad5cac9a..9359bd68 100644 --- a/src/Tensor.cpp +++ b/src/Tensor.cpp @@ -1,26 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 #include "kompute/Tensor.hpp" +#include namespace kp { std::string -Tensor::toString(Tensor::TensorDataTypes dt) +Tensor::toString(const std::type_info& dt) { - switch (dt) { - case TensorDataTypes::eBool: - return "eBool"; - case TensorDataTypes::eInt: - return "eInt"; - case TensorDataTypes::eUnsignedInt: - return "eUnsignedInt"; - case TensorDataTypes::eFloat: - return "eFloat"; - case TensorDataTypes::eDouble: - return "eDouble"; - default: - return "unknown"; - } + return dt.name(); } std::string @@ -43,8 +31,9 @@ Tensor::Tensor(std::shared_ptr physicalDevice, void* data, uint32_t elementTotalCount, uint32_t elementMemorySize, - const TensorDataTypes& dataType, + const std::type_info& dataType, const TensorTypes& tensorType) + : mDataType(dataType) { KP_LOG_DEBUG("Kompute Tensor constructor data length: {}, and type: {}", elementTotalCount, @@ -52,7 +41,6 @@ Tensor::Tensor(std::shared_ptr physicalDevice, this->mPhysicalDevice = physicalDevice; this->mDevice = device; - this->mDataType = dataType; this->mTensorType = tensorType; this->rebuild(data, elementTotalCount, elementMemorySize); @@ -125,7 +113,7 @@ Tensor::memorySize() return this->mSize * this->mDataTypeMemorySize; } -kp::Tensor::TensorDataTypes +const std::type_info& Tensor::dataType() { return this->mDataType; @@ -588,40 +576,4 @@ Tensor::destroy() KP_LOG_DEBUG("Kompute Tensor successful destroy()"); } - -template<> -Tensor::TensorDataTypes -TensorT::dataType() -{ - return Tensor::TensorDataTypes::eBool; -} - -template<> -Tensor::TensorDataTypes -TensorT::dataType() -{ - return Tensor::TensorDataTypes::eInt; -} - -template<> -Tensor::TensorDataTypes -TensorT::dataType() -{ - return Tensor::TensorDataTypes::eUnsignedInt; -} - -template<> -Tensor::TensorDataTypes -TensorT::dataType() -{ - return Tensor::TensorDataTypes::eFloat; -} - -template<> -Tensor::TensorDataTypes -TensorT::dataType() -{ - return Tensor::TensorDataTypes::eDouble; -} - } diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index 52f9ada7..3d3fc084 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "kompute/Core.hpp" @@ -102,11 +103,24 @@ class Manager return this->tensorT(data, tensorType); } + template + std::shared_ptr tensor( + void* data, + uint32_t elementTotalCount, + Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) + { + return tensor(data, + elementTotalCount, + sizeof(T), + typeid(T), + tensorType); + } + std::shared_ptr tensor( void* data, uint32_t elementTotalCount, uint32_t elementMemorySize, - const Tensor::TensorDataTypes& dataType, + const std::type_info& dataType, Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) { std::shared_ptr tensor{ new kp::Tensor(this->mPhysicalDevice, diff --git a/src/include/kompute/Tensor.hpp b/src/include/kompute/Tensor.hpp index a2bcd187..67f08f3b 100644 --- a/src/include/kompute/Tensor.hpp +++ b/src/include/kompute/Tensor.hpp @@ -5,6 +5,7 @@ #include "logger/Logger.hpp" #include #include +#include namespace kp { @@ -31,16 +32,8 @@ class Tensor eHost = 1, ///< Type is host memory, source and destination eStorage = 2, ///< Type is Device memory (only) }; - enum class TensorDataTypes - { - eBool = 0, - eInt = 1, - eUnsignedInt = 2, - eFloat = 3, - eDouble = 4, - }; - static std::string toString(TensorDataTypes dt); + static std::string toString(const std::type_info& dt); static std::string toString(TensorTypes dt); /** @@ -58,7 +51,7 @@ class Tensor void* data, uint32_t elementTotalCount, uint32_t elementMemorySize, - const TensorDataTypes& dataType, + const std::type_info& dataType, const TensorTypes& tensorType = TensorTypes::eDevice); /** @@ -201,7 +194,7 @@ class Tensor * * @return Data type of tensor of type kp::Tensor::TensorDataTypes */ - TensorDataTypes dataType(); + const std::type_info& dataType(); /** * Retrieve the raw data via the pointer to the memory that contains the raw @@ -247,7 +240,7 @@ class Tensor protected: // -------------- ALWAYS OWNED RESOURCES TensorTypes mTensorType; - TensorDataTypes mDataType; + const std::type_info& mDataType; uint32_t mSize; uint32_t mDataTypeMemorySize; void* mRawData; @@ -341,7 +334,10 @@ class TensorT : public Tensor Tensor::setRawData(data.data()); } - TensorDataTypes dataType(); + const std::type_info& dataType() + { + return typeid(T); + } }; } // End namespace kp From 7da4c1c6c4abb3973e6ee294ddb343685c8ae176 Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:24:36 -0400 Subject: [PATCH 4/9] Update tests for typeid implementation Signed-off-by: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> --- test/TestTensor.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/TestTensor.cpp b/test/TestTensor.cpp index 7eeff4af..9c81313b 100644 --- a/test/TestTensor.cpp +++ b/test/TestTensor.cpp @@ -22,25 +22,24 @@ TEST(TestTensor, DataTypes) { std::vector vec{ 0, 1, 2 }; std::shared_ptr> tensor = mgr.tensor(vec); - EXPECT_EQ(tensor->dataType(), kp::Tensor::TensorDataTypes::eFloat); + EXPECT_EQ(tensor->dataType(), typeid(float)); } { std::vector vec{ 0, 1, 2 }; std::shared_ptr> tensor = mgr.tensorT(vec); - EXPECT_EQ(tensor->dataType(), kp::Tensor::TensorDataTypes::eInt); + EXPECT_EQ(tensor->dataType(), typeid(int32_t)); } { std::vector vec{ 0, 1, 2 }; std::shared_ptr> tensor = mgr.tensorT(vec); - EXPECT_EQ(tensor->dataType(), - kp::Tensor::TensorDataTypes::eUnsignedInt); + EXPECT_EQ(tensor->dataType(), typeid(uint32_t)); } { std::vector vec{ 0, 1, 2 }; std::shared_ptr> tensor = mgr.tensorT(vec); - EXPECT_EQ(tensor->dataType(), kp::Tensor::TensorDataTypes::eDouble); + EXPECT_EQ(tensor->dataType(), typeid(double)); } } From 87aa8a9ca08aa5070cbd0a4c2b43ef67b4156b38 Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Thu, 29 Jun 2023 16:30:33 -0400 Subject: [PATCH 5/9] Add structure array multiplication example Signed-off-by: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> --- .../CMakeLists.txt | 45 ++++++++++++ .../structure_array_multiplication/README.md | 43 +++++++++++ .../src/CMakeLists.txt | 4 ++ .../src/main.cpp | 71 +++++++++++++++++++ .../src/shaders/CMakeLists.txt | 18 +++++ .../src/shaders/SHADER_Mult1.comp | 45 ++++++++++++ .../src/shaders/structs/ExampleStructure1.hpp | 28 ++++++++ .../src/shaders/structs/ExampleStructure2.hpp | 31 ++++++++ .../src/shaders/structs/StructTemplate.hpp | 24 +++++++ 9 files changed, 309 insertions(+) create mode 100644 examples/structure_array_multiplication/CMakeLists.txt create mode 100644 examples/structure_array_multiplication/README.md create mode 100644 examples/structure_array_multiplication/src/CMakeLists.txt create mode 100644 examples/structure_array_multiplication/src/main.cpp create mode 100644 examples/structure_array_multiplication/src/shaders/CMakeLists.txt create mode 100644 examples/structure_array_multiplication/src/shaders/SHADER_Mult1.comp create mode 100644 examples/structure_array_multiplication/src/shaders/structs/ExampleStructure1.hpp create mode 100644 examples/structure_array_multiplication/src/shaders/structs/ExampleStructure2.hpp create mode 100644 examples/structure_array_multiplication/src/shaders/structs/StructTemplate.hpp diff --git a/examples/structure_array_multiplication/CMakeLists.txt b/examples/structure_array_multiplication/CMakeLists.txt new file mode 100644 index 00000000..a805a86d --- /dev/null +++ b/examples/structure_array_multiplication/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.20) +project(kompute_array_mult) +include(CMakePrintHelpers) + +set(CMAKE_CXX_STANDARD 14) + +# Options +option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" "master") +option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" 0) +# Set a default build type if none was specified +# Based on: https://github.com/openchemistry/tomviz/blob/master/cmake/BuildType.cmake +set(DEFAULT_BUILD_TYPE "Release") + +if(EXISTS "${CMAKE_SOURCE_DIR}/.git") + set(DEFAULT_BUILD_TYPE "Debug") +endif() + +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.") + set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE STRING "Choose the type of build." FORCE) + + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") +endif() + +if(WIN32) # Install dlls in the same directory as the executable on Windows + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) +endif() + +if(KOMPUTE_OPT_FROM_SOURCE) + add_subdirectory(../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build) +else() + include(FetchContent) + FetchContent_Declare(kompute GIT_REPOSITORY https://github.com/KomputeProject/kompute.git + GIT_TAG ${KOMPUTE_OPT_GIT_TAG}) + FetchContent_MakeAvailable(kompute) + include_directories(${kompute_SOURCE_DIR}/src/include) +endif() + +# Add to the list, so CMake can later find the code to compile shaders to header files +list(APPEND CMAKE_PREFIX_PATH "${kompute_SOURCE_DIR}/cmake") + +add_subdirectory(src/shaders) +add_subdirectory(src) diff --git a/examples/structure_array_multiplication/README.md b/examples/structure_array_multiplication/README.md new file mode 100644 index 00000000..442b0e1d --- /dev/null +++ b/examples/structure_array_multiplication/README.md @@ -0,0 +1,43 @@ +# Kompute Structure Array Multiplication Example + +This folder contains an Kompute Example demonstrating how to use structs between c++ and shaders. + +## Building the example + +You will notice that it's a standalone project, so you can re-use it for your application. +It uses CMake's [`fetch_content`](https://cmake.org/cmake/help/latest/module/FetchContent.html) to consume Kompute as a dependency. +To build you just need to run the CMake command in this folder as follows: + +```bash +git clone https://github.com/KomputeProject/kompute.git +cd kompute/examples/structure_array_multiplication +mkdir build +cd build +cmake .. +cmake --build . +``` + +## Executing + +Form inside the `build/` directory run: + +### Linux + +```bash +./kompute_array_mult +``` + +### Windows + +```bash +.\Debug\kompute_array_mult.exe +``` + +## Pre-requisites + +In order to run this example, you will need the following dependencies: + +* REQUIRED + + The Vulkan SDK must be installed + +For the Vulkan SDK, the simplest way to install it is through [their website](https://vulkan.lunarg.com/sdk/home). You just have to follow the instructions for the relevant platform. diff --git a/examples/structure_array_multiplication/src/CMakeLists.txt b/examples/structure_array_multiplication/src/CMakeLists.txt new file mode 100644 index 00000000..eaf9ebb1 --- /dev/null +++ b/examples/structure_array_multiplication/src/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.20) + +add_executable(kompute_array_mult main.cpp) +target_link_libraries(kompute_array_mult PRIVATE shaders kompute::kompute) \ No newline at end of file diff --git a/examples/structure_array_multiplication/src/main.cpp b/examples/structure_array_multiplication/src/main.cpp new file mode 100644 index 00000000..ab0acb76 --- /dev/null +++ b/examples/structure_array_multiplication/src/main.cpp @@ -0,0 +1,71 @@ + +#include +#include +#include + +#include "SHADER_Mult1.hpp" +#include +#include "shaders/structs/ExampleStructure2.hpp" + +int main() +{ + kp::Manager mgr; + + ExampleStructure2 a{ 1,2,3,4,5,6,7,8,9,10, {11,12,13,14,15,16,17,18,19,20} }; + ExampleStructure2 b{ 1,2,3,4,5,6,7,8,9,10, {11,12,13,14,15,16,17,18,19,20} }; + ExampleStructure2 c{ 0 }; + + std::shared_ptr> tensorInA = + mgr.tensorT({ a, b }); + std::shared_ptr> tensorInB = + mgr.tensorT({ b, a }); + std::shared_ptr> tensorOut = + mgr.tensorT({ c, c }); + + const std::vector> params = { tensorInA, + tensorInB, + tensorOut }; + + const std::vector shader = std::vector( + shaders::SHADER_MULT1_COMP_SPV.begin(), shaders::SHADER_MULT1_COMP_SPV.end()); + std::shared_ptr algo = mgr.algorithm(params, shader); + + mgr.sequence() + ->record(params) + ->record(algo) + ->record(params) + ->eval(); + + // prints "Output { 0 4 12 }" + std::cout << "Output: { " << std::endl; + int i = 0; + for (const ExampleStructure2& elem : tensorOut->vector()) { + std::cout << "Elm " << i++ << ": "; + std::cout << elem.a << " "; + std::cout << elem.b << " "; + std::cout << elem.c << " "; + std::cout << elem.d << " "; + std::cout << elem.e << " "; + std::cout << elem.f << " "; + std::cout << elem.g << " "; + std::cout << elem.h << " "; + std::cout << elem.i << " "; + std::cout << elem.j << " "; + + std::cout << "{" << " "; + + std::cout << elem.k.a << " "; + std::cout << elem.k.b << " "; + std::cout << elem.k.c << " "; + std::cout << elem.k.d << " "; + std::cout << elem.k.e << " "; + std::cout << elem.k.f << " "; + std::cout << elem.k.g << " "; + std::cout << elem.k.h << " "; + std::cout << elem.k.i << " "; + std::cout << elem.k.j << " "; + + std::cout << "} " << std::endl; + } + std::cout << "}" << std::endl; +} diff --git a/examples/structure_array_multiplication/src/shaders/CMakeLists.txt b/examples/structure_array_multiplication/src/shaders/CMakeLists.txt new file mode 100644 index 00000000..f798b497 --- /dev/null +++ b/examples/structure_array_multiplication/src/shaders/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.20) + +file(GLOB SHADERS *.comp) +foreach(SHADER ${SHADERS}) + get_filename_component(SHADER_NAME ${SHADER} NAME_WE) + set(OUTPUT_FILE_NO_EXT ${CMAKE_CURRENT_BINARY_DIR}/${SHADER_NAME}) + + vulkan_compile_shader(INFILE ${SHADER_NAME}.comp + OUTFILE ${SHADER_NAME}.hpp + NAMESPACE "shaders" + RELATIVE_PATH "${kompute_SOURCE_DIR}/cmake") + + + list(APPEND SHADER_OUTPUTS ${OUTPUT_FILE_NO_EXT}.hpp) +endforeach() + +add_library(shaders INTERFACE ${SHADER_OUTPUTS}) +target_include_directories(shaders INTERFACE $) diff --git a/examples/structure_array_multiplication/src/shaders/SHADER_Mult1.comp b/examples/structure_array_multiplication/src/shaders/SHADER_Mult1.comp new file mode 100644 index 00000000..74ae0ca2 --- /dev/null +++ b/examples/structure_array_multiplication/src/shaders/SHADER_Mult1.comp @@ -0,0 +1,45 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +#include "structs/ExampleStructure2.hpp" + +layout (local_size_x = 1) in; + +layout (std430, binding = 0) buffer bufA { + ExampleStructure2 A[]; +}; + +layout (std430, binding = 1) buffer bufB { + ExampleStructure2 B[]; +}; + +layout (std430, binding = 2) buffer bufOut { + ExampleStructure2 Out[]; +}; + +void main() { + uint id = gl_GlobalInvocationID.x; + + //example structure2 + Out[id].a = A[id].a * B[id].a; + Out[id].b = A[id].b * B[id].b; + Out[id].c = A[id].c * B[id].c; + Out[id].d = A[id].d * B[id].d; + Out[id].e = A[id].e * B[id].e; + Out[id].f = A[id].f * B[id].f; + Out[id].g = A[id].g * B[id].g; + Out[id].h = A[id].h * B[id].h; + Out[id].i = A[id].i * B[id].i; + Out[id].j = A[id].j * B[id].j; + + //example structure1(nested struct) + Out[id].k.a = A[id].k.a * B[id].k.a; + Out[id].k.b = A[id].k.b * B[id].k.b; + Out[id].k.c = A[id].k.c * B[id].k.c; + Out[id].k.d = A[id].k.d * B[id].k.d; + Out[id].k.e = A[id].k.e * B[id].k.e; + Out[id].k.f = A[id].k.f * B[id].k.f; + Out[id].k.g = A[id].k.g * B[id].k.g; + Out[id].k.h = A[id].k.h * B[id].k.h; + Out[id].k.i = A[id].k.i * B[id].k.i; + Out[id].k.j = A[id].k.j * B[id].k.j; +} \ No newline at end of file diff --git a/examples/structure_array_multiplication/src/shaders/structs/ExampleStructure1.hpp b/examples/structure_array_multiplication/src/shaders/structs/ExampleStructure1.hpp new file mode 100644 index 00000000..7eaefe7c --- /dev/null +++ b/examples/structure_array_multiplication/src/shaders/structs/ExampleStructure1.hpp @@ -0,0 +1,28 @@ +#ifndef EXAMPLE_STRUCTURE_1_HPP +#define EXAMPLE_STRUCTURE_1_HPP + +#if defined(__clang__) || defined(_MSC_VER) || defined(__ICC) || defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__GNUG__) +//c++ compiler +#else +//shader comipler +#extension GL_GOOGLE_include_directive : require +#endif + +#include "StructTemplate.hpp" + +struct ExampleStructure1 +{ + int8_t a; + int16_t b; + int32_t c; + int64_t d; + + uint8_t e; + uint16_t f; + uint32_t g; + uint64_t h; + + float32_t i; + float64_t j; +}; +#endif \ No newline at end of file diff --git a/examples/structure_array_multiplication/src/shaders/structs/ExampleStructure2.hpp b/examples/structure_array_multiplication/src/shaders/structs/ExampleStructure2.hpp new file mode 100644 index 00000000..f7e5ef4e --- /dev/null +++ b/examples/structure_array_multiplication/src/shaders/structs/ExampleStructure2.hpp @@ -0,0 +1,31 @@ +#ifndef EXAMPLE_STRUCTURE_2_HPP +#define EXAMPLE_STRUCTURE_2_HPP + +#if defined(__clang__) || defined(_MSC_VER) || defined(__ICC) || defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__GNUG__) +//c++ compiler +#else +//shader comipler +#extension GL_GOOGLE_include_directive : require +#endif + +#include "StructTemplate.hpp" +#include "ExampleStructure1.hpp" + +struct ExampleStructure2 +{ + int8_t a; + int16_t b; + int32_t c; + int64_t d; + + uint8_t e; + uint16_t f; + uint32_t g; + uint64_t h; + + float32_t i; + float64_t j; + ExampleStructure1 k; +}; +#endif + diff --git a/examples/structure_array_multiplication/src/shaders/structs/StructTemplate.hpp b/examples/structure_array_multiplication/src/shaders/structs/StructTemplate.hpp new file mode 100644 index 00000000..43620148 --- /dev/null +++ b/examples/structure_array_multiplication/src/shaders/structs/StructTemplate.hpp @@ -0,0 +1,24 @@ +#if defined(__clang__) || defined(_MSC_VER) || defined(__ICC) || defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__GNUG__) +//c++ compiler +typedef signed char int8_t; +typedef short int16_t; +typedef int int32_t; +typedef long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; + +typedef float float32_t; +typedef double float64_t; +#else +//shader comipler +#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require +#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require +#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require +#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require + +#extension GL_EXT_shader_explicit_arithmetic_types_float32 : require +#extension GL_EXT_shader_explicit_arithmetic_types_float64 : require +#endif \ No newline at end of file From 667b8dd43ad83007da32fbf78b5dfb781cf52946 Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Fri, 7 Jul 2023 00:25:13 -0400 Subject: [PATCH 6/9] Implement ABCTypeContainer --- src/CMakeLists.txt | 3 ++- src/OpTensorCopy.cpp | 5 ++-- src/Tensor.cpp | 8 +++---- src/include/CMakeLists.txt | 2 ++ src/include/kompute/ABCTypeContainer.hpp | 11 +++++++++ src/include/kompute/Manager.hpp | 29 ++++++++++++++---------- src/include/kompute/Tensor.hpp | 13 ++++++----- 7 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 src/include/kompute/ABCTypeContainer.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dbb47dbe..e2407d37 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,7 +17,8 @@ add_library(kompute Algorithm.cpp OpTensorSyncLocal.cpp Sequence.cpp Tensor.cpp - Core.cpp) + Core.cpp + TypeContainer.cpp) add_library(kompute::kompute ALIAS kompute) diff --git a/src/OpTensorCopy.cpp b/src/OpTensorCopy.cpp index 116d4ff7..6ea5d0f3 100644 --- a/src/OpTensorCopy.cpp +++ b/src/OpTensorCopy.cpp @@ -17,10 +17,11 @@ OpTensorCopy::OpTensorCopy(const std::vector>& tensors) "Kompute OpTensorCopy called with less than 2 tensor"); } - const std::type_info& dataType = this->mTensors[0]->dataType(); + std::shared_ptr dataType = this->mTensors[0]->dataType(); + uint32_t size = this->mTensors[0]->size(); for (const std::shared_ptr& tensor : tensors) { - if (tensor->dataType() != dataType) { + if (!(*dataType).compare(*tensor->dataType())) { throw std::runtime_error(fmt::format( "Attempting to copy tensors of different types from {} to {}", Tensor::toString(dataType), diff --git a/src/Tensor.cpp b/src/Tensor.cpp index 9359bd68..ab44e170 100644 --- a/src/Tensor.cpp +++ b/src/Tensor.cpp @@ -6,9 +6,9 @@ namespace kp { std::string -Tensor::toString(const std::type_info& dt) +Tensor::toString(std::shared_ptr dt) { - return dt.name(); + return (*dt).name(); } std::string @@ -31,7 +31,7 @@ Tensor::Tensor(std::shared_ptr physicalDevice, void* data, uint32_t elementTotalCount, uint32_t elementMemorySize, - const std::type_info& dataType, + std::shared_ptr dataType, const TensorTypes& tensorType) : mDataType(dataType) { @@ -113,7 +113,7 @@ Tensor::memorySize() return this->mSize * this->mDataTypeMemorySize; } -const std::type_info& +std::shared_ptr Tensor::dataType() { return this->mDataType; diff --git a/src/include/CMakeLists.txt b/src/include/CMakeLists.txt index e1652fdd..214c8b30 100644 --- a/src/include/CMakeLists.txt +++ b/src/include/CMakeLists.txt @@ -15,6 +15,8 @@ target_sources(kompute PRIVATE kompute/Manager.hpp kompute/Sequence.hpp kompute/Tensor.hpp + kompute/TypeContainer.hpp + kompute/ABCTypeContainer.hpp kompute/operations/OpAlgoDispatch.hpp kompute/operations/OpBase.hpp diff --git a/src/include/kompute/ABCTypeContainer.hpp b/src/include/kompute/ABCTypeContainer.hpp new file mode 100644 index 00000000..af3e4a76 --- /dev/null +++ b/src/include/kompute/ABCTypeContainer.hpp @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once +#include + +class ABCTypeContainer +{ + public: + // Pure Virtual Function + virtual bool compare(ABCTypeContainer& obj) = 0; + virtual std::string name() = 0; +}; \ No newline at end of file diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index 3d3fc084..b90cdbf6 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -9,6 +9,7 @@ #include "kompute/Sequence.hpp" #include "logger/Logger.hpp" +#include #define KP_DEFAULT_SESSION "DEFAULT" @@ -107,12 +108,15 @@ class Manager std::shared_ptr tensor( void* data, uint32_t elementTotalCount, + uint32_t elementMemorySize = sizeof(T), + std::shared_ptr dataType = + std::make_shared>(), Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) { - return tensor(data, - elementTotalCount, - sizeof(T), - typeid(T), + return this->tensor(data, + elementTotalCount, + elementMemorySize, + dataType, tensorType); } @@ -120,16 +124,17 @@ class Manager void* data, uint32_t elementTotalCount, uint32_t elementMemorySize, - const std::type_info& dataType, + std::shared_ptr dataType, Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) { - std::shared_ptr tensor{ new kp::Tensor(this->mPhysicalDevice, - this->mDevice, - data, - elementTotalCount, - elementMemorySize, - dataType, - tensorType) }; + std::shared_ptr tensor = + std::make_shared(this->mPhysicalDevice, + this->mDevice, + data, + elementTotalCount, + elementMemorySize, + dataType, + tensorType); if (this->mManageResources) { this->mManagedTensors.push_back(tensor); diff --git a/src/include/kompute/Tensor.hpp b/src/include/kompute/Tensor.hpp index 67f08f3b..43c765cd 100644 --- a/src/include/kompute/Tensor.hpp +++ b/src/include/kompute/Tensor.hpp @@ -6,6 +6,7 @@ #include #include #include +#include "TypeContainer.hpp" namespace kp { @@ -33,7 +34,7 @@ class Tensor eStorage = 2, ///< Type is Device memory (only) }; - static std::string toString(const std::type_info& dt); + static std::string toString(std::shared_ptr dt); static std::string toString(TensorTypes dt); /** @@ -51,7 +52,7 @@ class Tensor void* data, uint32_t elementTotalCount, uint32_t elementMemorySize, - const std::type_info& dataType, + std::shared_ptr dataType, const TensorTypes& tensorType = TensorTypes::eDevice); /** @@ -194,7 +195,7 @@ class Tensor * * @return Data type of tensor of type kp::Tensor::TensorDataTypes */ - const std::type_info& dataType(); + std::shared_ptr dataType(); /** * Retrieve the raw data via the pointer to the memory that contains the raw @@ -240,7 +241,7 @@ class Tensor protected: // -------------- ALWAYS OWNED RESOURCES TensorTypes mTensorType; - const std::type_info& mDataType; + std::shared_ptr mDataType; uint32_t mSize; uint32_t mDataTypeMemorySize; void* mRawData; @@ -334,9 +335,9 @@ class TensorT : public Tensor Tensor::setRawData(data.data()); } - const std::type_info& dataType() + std::shared_ptr dataType() { - return typeid(T); + return std::make_shared>(); } }; From 68424a1f7d575f24d2e431be8559ee184217dc64 Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Fri, 7 Jul 2023 00:25:59 -0400 Subject: [PATCH 7/9] Implement C++ TypeContainer --- src/TypeContainer.cpp | 5 ++++ src/include/kompute/TypeContainer.hpp | 35 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/TypeContainer.cpp create mode 100644 src/include/kompute/TypeContainer.hpp diff --git a/src/TypeContainer.cpp b/src/TypeContainer.cpp new file mode 100644 index 00000000..7142356c --- /dev/null +++ b/src/TypeContainer.cpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 + +#include "kompute/TypeContainer.hpp" + +size_t IdCounter::counter = 0; \ No newline at end of file diff --git a/src/include/kompute/TypeContainer.hpp b/src/include/kompute/TypeContainer.hpp new file mode 100644 index 00000000..f562c6c9 --- /dev/null +++ b/src/include/kompute/TypeContainer.hpp @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include "ABCTypeContainer.hpp" +#include + + +struct IdCounter +{ + static size_t counter; +}; + +template +class TypeContainer : public ABCTypeContainer, IdCounter +{ + private: + size_t classId() + { + static size_t id = counter++; + return id; + } + + public: + TypeContainer() : dt(typeid(T)) {} + + bool compare(ABCTypeContainer& other) override + { + TypeContainer& obj = static_cast(other); + return this->classId() == obj.classId(); + } + + std::string name() override { return this->dt.name(); } + + const std::type_info& dt; +}; \ No newline at end of file From d677e85eb516c85fc09e8ce2c14dcd6ce2d21bfc Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Fri, 7 Jul 2023 00:26:30 -0400 Subject: [PATCH 8/9] Implement PyTypeContainer --- python/src/kp/__init__.py | 1 + python/src/main.cpp | 322 +++++++++----------------------------- setup.py | 8 +- 3 files changed, 77 insertions(+), 254 deletions(-) create mode 100644 python/src/kp/__init__.py diff --git a/python/src/kp/__init__.py b/python/src/kp/__init__.py new file mode 100644 index 00000000..368d167d --- /dev/null +++ b/python/src/kp/__init__.py @@ -0,0 +1 @@ +from .kp import * diff --git a/python/src/main.cpp b/python/src/main.cpp index 8da69669..72f546a3 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -3,12 +3,14 @@ #include #include +#include #include #include "docstrings.hpp" #include "utils.hpp" +typedef unsigned char byte; namespace py = pybind11; // used in Core.hpp @@ -24,31 +26,35 @@ opAlgoDispatchPyInit(std::shared_ptr& algorithm, push_consts.size(), std::string(py::str(push_consts.dtype()))); - if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((float*)info.ptr, - ((float*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((uint32_t*)info.ptr, - ((uint32_t*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((int32_t*)info.ptr, - ((int32_t*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((double*)info.ptr, - ((double*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else { - throw std::runtime_error("Kompute Python no valid dtype supported"); - } + std::vector dataVec((byte*)info.ptr, + ((byte*)info.ptr) + info.size); + return std::unique_ptr{ new kp::OpAlgoDispatch( + algorithm, dataVec) }; } +class PyTypeContainer : public ABCTypeContainer +{ + public: + PyTypeContainer(py::object clazz) + : clazz_(clazz) + {} + + bool compare(ABCTypeContainer& obj) override + { + // Implement comparison logic here + auto other = dynamic_cast(&obj); + return other && clazz_.is(other->clazz_); + } + + std::string name() override + { + // Return the name here + return py::str(clazz_); + } + + py::object clazz_; +}; + PYBIND11_MODULE(kp, m) { @@ -131,34 +137,32 @@ PYBIND11_MODULE(kp, m) m, "Tensor", DOC(kp, Tensor)) .def( "data", - [](kp::Tensor& self) { + [np](kp::Tensor& self) { // Non-owning container exposing the underlying pointer - switch (self.dataType()) { - case kp::Tensor::TensorDataTypes::eFloat: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eUnsignedInt: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eInt: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eDouble: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eBool: - return py::array( - self.size(), self.data(), py::cast(&self)); - default: - throw std::runtime_error( - "Kompute Python data type not supported"); - } + auto frombuffer = np.attr("frombuffer"); + + auto dtype = + dynamic_cast(&(*self.dataType()))->clazz_; + + auto buffer = + py::array(self.size() * dtype.attr("itemsize").cast(), + self.data(), + py::cast(&self)); + + + + return frombuffer(buffer, dtype); }, DOC(kp, Tensor, data)) .def("size", &kp::Tensor::size, DOC(kp, Tensor, size)) .def("__len__", &kp::Tensor::size, DOC(kp, Tensor, size)) .def("tensor_type", &kp::Tensor::tensorType, DOC(kp, Tensor, tensorType)) - .def("data_type", &kp::Tensor::dataType, DOC(kp, Tensor, dataType)) + .def( + "data_type", + [](kp::Tensor& self) { + return dynamic_cast(&(*self.dataType()))->clazz_; + }, + DOC(kp, Tensor, dataType)) .def("is_init", &kp::Tensor::isInit, DOC(kp, Tensor, isInit)) .def("destroy", &kp::Tensor::destroy, DOC(kp, Tensor, destroy)); @@ -236,10 +240,13 @@ PYBIND11_MODULE(kp, m) KP_LOG_DEBUG("Kompute Python Manager tensor() creating tensor " "float with data size {}", flatdata.size()); + + std::shared_ptr typeContainer = + std::make_shared(flatdata.dtype()); return self.tensor(info.ptr, flatdata.size(), - sizeof(float), - kp::Tensor::TensorDataTypes::eFloat, + flatdata.dtype().itemsize(), + typeContainer, tensor_type); }, DOC(kp, Manager, tensor), @@ -257,31 +264,14 @@ PYBIND11_MODULE(kp, m) "size {} dtype {}", flatdata.size(), std::string(py::str(flatdata.dtype()))); - if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor( - info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else { - throw std::runtime_error( - "Kompute Python no valid dtype supported"); - } + + std::shared_ptr typeContainer = + std::make_shared(flatdata.dtype()); + return self.tensor(info.ptr, + flatdata.size(), + flatdata.dtype().itemsize(), + typeContainer, + tensor_type); }, DOC(kp, Manager, tensorT), py::arg("data"), @@ -333,186 +323,14 @@ PYBIND11_MODULE(kp, m) spec_consts.size(), std::string(py::str(spec_consts.dtype()))); - // We have to iterate across a combination of parameters due to the - // lack of support for templating - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specConstsVec( - (float*)specInfo.ptr, ((float*)specInfo.ptr) + specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushConstsVec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushConstsVec( - (int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushConstsVec( - (uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushConstsVec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } - } else if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specconstsvec((int32_t*)specInfo.ptr, - ((int32_t*)specInfo.ptr) + - specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushconstsvec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } - } else if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specconstsvec((uint32_t*)specInfo.ptr, - ((uint32_t*)specInfo.ptr) + - specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushconstsvec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } - } else if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specconstsvec((double*)specInfo.ptr, - ((double*)specInfo.ptr) + - specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushconstsvec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } - } - // If reach then no valid dtype supported - throw std::runtime_error("Kompute Python no valid dtype supported"); + std::vector specconstsvec( + (byte*)specInfo.ptr, ((byte*)specInfo.ptr) + specInfo.size); + + std::vector pushconstsvec( + (byte*)pushInfo.ptr, ((byte*)pushInfo.ptr) + pushInfo.size); + + return self.algorithm( + tensors, spirvVec, workgroup, specconstsvec, pushconstsvec); }, DOC(kp, Manager, algorithm), py::arg("tensors"), diff --git a/setup.py b/setup.py index 09faa8d1..0f5b8c4c 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ import sysconfig import subprocess -from setuptools import setup, Extension +from setuptools import setup, Extension, find_packages from setuptools.command.build_ext import build_ext from distutils.version import LooseVersion @@ -83,7 +83,11 @@ def build_extension(self, ext): description='Kompute: Blazing fast, mobile-enabled, asynchronous, and optimized for advanced GPU processing usecases.', long_description=long_description, long_description_content_type='text/markdown', - ext_modules=[CMakeExtension('kp')], + ext_modules=[CMakeExtension('kp/kp')], + packages = find_packages(where="python/src"), + package_dir={ + 'kp': 'python/src/kp' + }, install_requires=[ "numpy<2.0.0" ], From 3152e545d52bdca27918cd5c707e84c155b34a25 Mon Sep 17 00:00:00 2001 From: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> Date: Fri, 7 Jul 2023 00:26:30 -0400 Subject: [PATCH 9/9] Implement PyTypeContainer Signed-off-by: Brian Petkovsek <16124109+BrianPetkovsek@users.noreply.github.com> --- python/src/kp/__init__.py | 1 + python/src/main.cpp | 322 +++++++++----------------------------- setup.py | 8 +- 3 files changed, 77 insertions(+), 254 deletions(-) create mode 100644 python/src/kp/__init__.py diff --git a/python/src/kp/__init__.py b/python/src/kp/__init__.py new file mode 100644 index 00000000..368d167d --- /dev/null +++ b/python/src/kp/__init__.py @@ -0,0 +1 @@ +from .kp import * diff --git a/python/src/main.cpp b/python/src/main.cpp index 8da69669..72f546a3 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -3,12 +3,14 @@ #include #include +#include #include #include "docstrings.hpp" #include "utils.hpp" +typedef unsigned char byte; namespace py = pybind11; // used in Core.hpp @@ -24,31 +26,35 @@ opAlgoDispatchPyInit(std::shared_ptr& algorithm, push_consts.size(), std::string(py::str(push_consts.dtype()))); - if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((float*)info.ptr, - ((float*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((uint32_t*)info.ptr, - ((uint32_t*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((int32_t*)info.ptr, - ((int32_t*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else if (push_consts.dtype().is(py::dtype::of())) { - std::vector dataVec((double*)info.ptr, - ((double*)info.ptr) + info.size); - return std::unique_ptr{ new kp::OpAlgoDispatch( - algorithm, dataVec) }; - } else { - throw std::runtime_error("Kompute Python no valid dtype supported"); - } + std::vector dataVec((byte*)info.ptr, + ((byte*)info.ptr) + info.size); + return std::unique_ptr{ new kp::OpAlgoDispatch( + algorithm, dataVec) }; } +class PyTypeContainer : public ABCTypeContainer +{ + public: + PyTypeContainer(py::object clazz) + : clazz_(clazz) + {} + + bool compare(ABCTypeContainer& obj) override + { + // Implement comparison logic here + auto other = dynamic_cast(&obj); + return other && clazz_.is(other->clazz_); + } + + std::string name() override + { + // Return the name here + return py::str(clazz_); + } + + py::object clazz_; +}; + PYBIND11_MODULE(kp, m) { @@ -131,34 +137,32 @@ PYBIND11_MODULE(kp, m) m, "Tensor", DOC(kp, Tensor)) .def( "data", - [](kp::Tensor& self) { + [np](kp::Tensor& self) { // Non-owning container exposing the underlying pointer - switch (self.dataType()) { - case kp::Tensor::TensorDataTypes::eFloat: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eUnsignedInt: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eInt: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eDouble: - return py::array( - self.size(), self.data(), py::cast(&self)); - case kp::Tensor::TensorDataTypes::eBool: - return py::array( - self.size(), self.data(), py::cast(&self)); - default: - throw std::runtime_error( - "Kompute Python data type not supported"); - } + auto frombuffer = np.attr("frombuffer"); + + auto dtype = + dynamic_cast(&(*self.dataType()))->clazz_; + + auto buffer = + py::array(self.size() * dtype.attr("itemsize").cast(), + self.data(), + py::cast(&self)); + + + + return frombuffer(buffer, dtype); }, DOC(kp, Tensor, data)) .def("size", &kp::Tensor::size, DOC(kp, Tensor, size)) .def("__len__", &kp::Tensor::size, DOC(kp, Tensor, size)) .def("tensor_type", &kp::Tensor::tensorType, DOC(kp, Tensor, tensorType)) - .def("data_type", &kp::Tensor::dataType, DOC(kp, Tensor, dataType)) + .def( + "data_type", + [](kp::Tensor& self) { + return dynamic_cast(&(*self.dataType()))->clazz_; + }, + DOC(kp, Tensor, dataType)) .def("is_init", &kp::Tensor::isInit, DOC(kp, Tensor, isInit)) .def("destroy", &kp::Tensor::destroy, DOC(kp, Tensor, destroy)); @@ -236,10 +240,13 @@ PYBIND11_MODULE(kp, m) KP_LOG_DEBUG("Kompute Python Manager tensor() creating tensor " "float with data size {}", flatdata.size()); + + std::shared_ptr typeContainer = + std::make_shared(flatdata.dtype()); return self.tensor(info.ptr, flatdata.size(), - sizeof(float), - kp::Tensor::TensorDataTypes::eFloat, + flatdata.dtype().itemsize(), + typeContainer, tensor_type); }, DOC(kp, Manager, tensor), @@ -257,31 +264,14 @@ PYBIND11_MODULE(kp, m) "size {} dtype {}", flatdata.size(), std::string(py::str(flatdata.dtype()))); - if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor( - info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else if (flatdata.dtype().is(py::dtype::of())) { - return self.tensor(info.ptr, - flatdata.size(), - tensor_type); - } else { - throw std::runtime_error( - "Kompute Python no valid dtype supported"); - } + + std::shared_ptr typeContainer = + std::make_shared(flatdata.dtype()); + return self.tensor(info.ptr, + flatdata.size(), + flatdata.dtype().itemsize(), + typeContainer, + tensor_type); }, DOC(kp, Manager, tensorT), py::arg("data"), @@ -333,186 +323,14 @@ PYBIND11_MODULE(kp, m) spec_consts.size(), std::string(py::str(spec_consts.dtype()))); - // We have to iterate across a combination of parameters due to the - // lack of support for templating - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specConstsVec( - (float*)specInfo.ptr, ((float*)specInfo.ptr) + specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushConstsVec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushConstsVec( - (int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushConstsVec( - (uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushConstsVec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specConstsVec, - pushConstsVec); - } - } else if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specconstsvec((int32_t*)specInfo.ptr, - ((int32_t*)specInfo.ptr) + - specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushconstsvec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } - } else if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specconstsvec((uint32_t*)specInfo.ptr, - ((uint32_t*)specInfo.ptr) + - specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushconstsvec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec( - (uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } - } else if (spec_consts.dtype().is(py::dtype::of())) { - std::vector specconstsvec((double*)specInfo.ptr, - ((double*)specInfo.ptr) + - specInfo.size); - if (spec_consts.dtype().is(py::dtype::of())) { - std::vector pushconstsvec((float*)pushInfo.ptr, - ((float*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((int32_t*)pushInfo.ptr, - ((int32_t*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((uint32_t*)pushInfo.ptr, - ((uint32_t*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } else if (spec_consts.dtype().is( - py::dtype::of())) { - std::vector pushconstsvec((double*)pushInfo.ptr, - ((double*)pushInfo.ptr) + - pushInfo.size); - return self.algorithm(tensors, - spirvVec, - workgroup, - specconstsvec, - pushconstsvec); - } - } - // If reach then no valid dtype supported - throw std::runtime_error("Kompute Python no valid dtype supported"); + std::vector specconstsvec( + (byte*)specInfo.ptr, ((byte*)specInfo.ptr) + specInfo.size); + + std::vector pushconstsvec( + (byte*)pushInfo.ptr, ((byte*)pushInfo.ptr) + pushInfo.size); + + return self.algorithm( + tensors, spirvVec, workgroup, specconstsvec, pushconstsvec); }, DOC(kp, Manager, algorithm), py::arg("tensors"), diff --git a/setup.py b/setup.py index 09faa8d1..0f5b8c4c 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ import sysconfig import subprocess -from setuptools import setup, Extension +from setuptools import setup, Extension, find_packages from setuptools.command.build_ext import build_ext from distutils.version import LooseVersion @@ -83,7 +83,11 @@ def build_extension(self, ext): description='Kompute: Blazing fast, mobile-enabled, asynchronous, and optimized for advanced GPU processing usecases.', long_description=long_description, long_description_content_type='text/markdown', - ext_modules=[CMakeExtension('kp')], + ext_modules=[CMakeExtension('kp/kp')], + packages = find_packages(where="python/src"), + package_dir={ + 'kp': 'python/src/kp' + }, install_requires=[ "numpy<2.0.0" ],