From 82a56b7e85c298f785fde2f6d2a63700213eff6f Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 21 Feb 2021 10:06:29 +0000 Subject: [PATCH] Updated examples to match new shader api --- .../android/android-simple/app/build.gradle | 4 +++- .../app/src/main/cpp/KomputeModelML.cpp | 12 +----------- examples/array_multiplication/src/Main.cpp | 11 +---------- .../kompute_summator/KomputeSummatorNode.cpp | 2 +- .../gdnative_shared/src/KomputeSummator.cpp | 2 +- examples/logistic_regression/src/Main.cpp | 11 +++-------- examples/python/README.md | 17 ----------------- src/Shader.cpp | 10 +++++----- src/include/kompute/Core.hpp | 6 +++--- src/include/kompute/Shader.hpp | 4 ++-- 10 files changed, 20 insertions(+), 59 deletions(-) delete mode 100644 examples/python/README.md diff --git a/examples/android/android-simple/app/build.gradle b/examples/android/android-simple/app/build.gradle index 3990324..6ddb285 100644 --- a/examples/android/android-simple/app/build.gradle +++ b/examples/android/android-simple/app/build.gradle @@ -18,8 +18,10 @@ android { arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DKOMPUTE_OPT_ANDOID_BUILD=1', + '-DKOMPUTE_OPT_REPO_SUBMODULE_BUILD=1', '-DKOMPUTE_OPT_INSTALL=0', - '-DKOMPUTE_OPT_BUILD_SINGLE_HEADER=1', + '-DKOMPUTE_OPT_ENABLE_SPDLOG=0', + '-DKOMPUTE_OPT_BUILD_SINGLE_HEADER=0', '-DKOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS=1', '-DKOMPUTE_EXTRA_CXX_FLAGS=-DKOMPUTE_VK_API_MINOR_VERSION=0' } diff --git a/examples/android/android-simple/app/src/main/cpp/KomputeModelML.cpp b/examples/android/android-simple/app/src/main/cpp/KomputeModelML.cpp index 679136b..e396570 100755 --- a/examples/android/android-simple/app/src/main/cpp/KomputeModelML.cpp +++ b/examples/android/android-simple/app/src/main/cpp/KomputeModelML.cpp @@ -51,19 +51,9 @@ void KomputeModelML::train(std::vector yData, std::vector xIData, sq->record({ wIn, bIn }); -#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING // Newer versions of Android are able to use shaderc to read raw string sq->record( - params, std::vector(LR_SHADER.begin(), LR_SHADER.end())); -#else - // Older versions of Android require the SPIRV binary directly - sq->record( - params, std::vector( - kp::shader_data::shaders_glsl_logisticregression_comp_spv, - kp::shader_data::shaders_glsl_logisticregression_comp_spv - + kp::shader_data::shaders_glsl_logisticregression_comp_spv_len - )); -#endif + params, kp::Shader::compile_source(LR_SHADER)); sq->record({ wOutI, wOutJ, bOut, lOut }); diff --git a/examples/array_multiplication/src/Main.cpp b/examples/array_multiplication/src/Main.cpp index d773886..0fb704a 100755 --- a/examples/array_multiplication/src/Main.cpp +++ b/examples/array_multiplication/src/Main.cpp @@ -18,7 +18,6 @@ int main() auto tensorInB = mgr.tensor({ 0.0, 1.0, 2.0 }); auto tensorOut = mgr.tensor({ 0.0, 0.0, 0.0 }); -#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING std::string shader(R"( // The version to use #version 450 @@ -40,15 +39,7 @@ int main() mgr.evalOpDefault( { tensorInA, tensorInB, tensorOut }, - std::vector(shader.begin(), shader.end())); -#else - mgr.evalOpDefault( - { tensorInA, tensorInB, tensorOut }, - std::vector( - kp::shader_data::shaders_glsl_opmult_comp_spv, - kp::shader_data::shaders_glsl_opmult_comp_spv - + kp::shader_data::shaders_glsl_opmult_comp_spv_len)); -#endif + kp::Shader::compile_source(shader)); mgr.evalOpDefault({tensorOut}); diff --git a/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp b/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp index 304416a..c0b6859 100644 --- a/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp +++ b/examples/godot_examples/custom_module/kompute_summator/KomputeSummatorNode.cpp @@ -61,7 +61,7 @@ void KomputeSummatorNode::_init() { // Then we run the operation with both tensors sq->record( { this->mPrimaryTensor, this->mSecondaryTensor }, - std::vector(shader.begin(), shader.end())); + kp::Shader::compile_source(shader)); // We map the result back to local sq->record( diff --git a/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp index 26a3818..feb674c 100644 --- a/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp +++ b/examples/godot_examples/gdnative_shared/src/KomputeSummator.cpp @@ -58,7 +58,7 @@ void KomputeSummator::_init() { // Then we run the operation with both tensors this->mSequence->record( { this->mPrimaryTensor, this->mSecondaryTensor }, - std::vector(shader.begin(), shader.end())); + kp::Shader::compile_source(shader)); // We map the result back to local this->mSequence->record( diff --git a/examples/logistic_regression/src/Main.cpp b/examples/logistic_regression/src/Main.cpp index 4138a0a..e342e7a 100755 --- a/examples/logistic_regression/src/Main.cpp +++ b/examples/logistic_regression/src/Main.cpp @@ -44,16 +44,11 @@ int main() sq->record({ wIn, bIn }); -#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING - sq->record( - params, "shaders/glsl/logistic_regression.comp"); -#else sq->record( params, std::vector( - kp::shader_data::shaders_glsl_logisticregression_comp_spv, - kp::shader_data::shaders_glsl_logisticregression_comp_spv - + kp::shader_data::shaders_glsl_logisticregression_comp_spv_len)); -#endif + (uint32_t*)kp::shader_data::shaders_glsl_logisticregression_comp_spv, + (uint32_t*)(kp::shader_data::shaders_glsl_logisticregression_comp_spv + + kp::shader_data::shaders_glsl_logisticregression_comp_spv_len))); sq->record({ wOutI, wOutJ, bOut, lOut }); diff --git a/examples/python/README.md b/examples/python/README.md deleted file mode 100644 index 1e2977c..0000000 --- a/examples/python/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Kompute Python Example - -This folder contains the accompanying code for the article "High Performance Python for GPU Accelerated Machine Learning in Cross-Vendor GPUs". - -The easiest way to try this example is by using the [Google Binder Notebook](https://colab.research.google.com/drive/15uQ7qMZuOyk8JcXF-3SB2R5yNFW21I4P), which will allow you to use a GPU for free and runs without much setup. - - - - - -Alternatively if you want to test the example yourself locally, you can get setup and started through the following links: - -1. Install the [Kompute Python Package](https://kompute.cc/overview/python-package.html#package-installation) -2. Run the [Array Multiplication Code](https://github.com/EthicalML/vulkan-kompute/blob/python_extensions/python/test/test_array_multiplication.py) -3. Run the [Logistic Regression Code](https://github.com/EthicalML/vulkan-kompute/blob/python_extensions/python/test/test_logistic_regression.py) - - diff --git a/src/Shader.cpp b/src/Shader.cpp index a58e0a9..3f42ee2 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -38,7 +38,7 @@ Shader::compile_sources(const std::vector& sources, if (!shader.parse(&glslang::DefaultTBuiltInResource, 100, false, messages)) { info_log = std::string(shader.getInfoLog()) + "\n" + std::string(shader.getInfoDebugLog()); - SPDLOG_ERROR(info_log); + SPDLOG_ERROR("Kompute Shader Error: {}", info_log); throw std::runtime_error(info_log); } @@ -49,7 +49,7 @@ Shader::compile_sources(const std::vector& sources, if (!program.link(messages)) { info_log = std::string(program.getInfoLog()) + "\n" + std::string(program.getInfoDebugLog()); - SPDLOG_ERROR(info_log); + SPDLOG_ERROR("Kompute Shader Error: {}", info_log); throw std::runtime_error(info_log); } @@ -57,7 +57,7 @@ Shader::compile_sources(const std::vector& sources, if (shader.getInfoLog()) { info_log += std::string(shader.getInfoLog()) + "\n" + std::string(shader.getInfoDebugLog()) + "\n"; - SPDLOG_INFO(info_log); + SPDLOG_INFO("Kompute Shader Information: {}", info_log); } glslang::TIntermediate *intermediate = program.getIntermediate(language); @@ -65,7 +65,7 @@ Shader::compile_sources(const std::vector& sources, if (!intermediate) { info_log += "Failed to get shared intermediate code.\n"; - SPDLOG_ERROR(info_log); + SPDLOG_ERROR("Kompute Shader Error: {}", info_log); throw std::runtime_error(info_log); } @@ -76,7 +76,7 @@ Shader::compile_sources(const std::vector& sources, if (shader.getInfoLog()) { info_log += logger.getAllMessages() + "\n"; - SPDLOG_DEBUG(info_log); + SPDLOG_DEBUG("Kompute Shader all result messages: {}", info_log); } // Shutdown glslang library. diff --git a/src/include/kompute/Core.hpp b/src/include/kompute/Core.hpp index 809cf53..9550d38 100644 --- a/src/include/kompute/Core.hpp +++ b/src/include/kompute/Core.hpp @@ -1,6 +1,6 @@ #pragma once -#ifdef VK_USE_PLATFORM_ANDROID_KHR +#if VK_USE_PLATFORM_ANDROID_KHR #include #include // VK_NO_PROTOTYPES required before vulkan import but after wrapper.hpp @@ -82,7 +82,7 @@ extern py::object kp_debug, kp_info, kp_warning, kp_error; #else #if defined(VK_USE_PLATFORM_ANDROID_KHR) #define SPDLOG_WARN(message, ...) \ - ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) + ((void)__android_log_print(ANDROID_LOG_WARN, KOMPUTE_LOG_TAG, message)) #elif defined(KOMPUTE_BUILD_PYTHON) #define SPDLOG_WARN(message, ...) kp_warning(message); #else @@ -96,7 +96,7 @@ extern py::object kp_debug, kp_info, kp_warning, kp_error; #else #if defined(VK_USE_PLATFORM_ANDROID_KHR) #define SPDLOG_ERROR(message, ...) \ - ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) + ((void)__android_log_print(ANDROID_LOG_ERROR, KOMPUTE_LOG_TAG, message)) #elif defined(KOMPUTE_BUILD_PYTHON) #define SPDLOG_ERROR(message, ...) kp_error(message); #else diff --git a/src/include/kompute/Shader.hpp b/src/include/kompute/Shader.hpp index f486366..0b363ae 100644 --- a/src/include/kompute/Shader.hpp +++ b/src/include/kompute/Shader.hpp @@ -4,12 +4,12 @@ #include #include -#include "Core.hpp" - #include #include #include +#include "kompute/Core.hpp" + namespace kp { /**