mirror of
https://github.com/nomic-ai/kompute.git
synced 2026-05-11 08:59:59 +00:00
Updated examples to match new shader api
This commit is contained in:
@@ -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'
|
||||
}
|
||||
|
||||
@@ -51,19 +51,9 @@ void KomputeModelML::train(std::vector<float> yData, std::vector<float> xIData,
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
|
||||
#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING
|
||||
// Newer versions of Android are able to use shaderc to read raw string
|
||||
sq->record<kp::OpAlgoBase>(
|
||||
params, std::vector<uint32_t>(LR_SHADER.begin(), LR_SHADER.end()));
|
||||
#else
|
||||
// Older versions of Android require the SPIRV binary directly
|
||||
sq->record<kp::OpAlgoBase>(
|
||||
params, std::vector<uint32_t>(
|
||||
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<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
|
||||
@@ -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<kp::OpAlgoBase>(
|
||||
{ tensorInA, tensorInB, tensorOut },
|
||||
std::vector<uint32_t>(shader.begin(), shader.end()));
|
||||
#else
|
||||
mgr.evalOpDefault<kp::OpAlgoBase>(
|
||||
{ tensorInA, tensorInB, tensorOut },
|
||||
std::vector<uint32_t>(
|
||||
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<kp::OpTensorSyncLocal>({tensorOut});
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ void KomputeSummatorNode::_init() {
|
||||
// Then we run the operation with both tensors
|
||||
sq->record<kp::OpAlgoBase>(
|
||||
{ this->mPrimaryTensor, this->mSecondaryTensor },
|
||||
std::vector<char>(shader.begin(), shader.end()));
|
||||
kp::Shader::compile_source(shader));
|
||||
|
||||
// We map the result back to local
|
||||
sq->record<kp::OpTensorSyncLocal>(
|
||||
|
||||
@@ -58,7 +58,7 @@ void KomputeSummator::_init() {
|
||||
// Then we run the operation with both tensors
|
||||
this->mSequence->record<kp::OpAlgoBase>(
|
||||
{ this->mPrimaryTensor, this->mSecondaryTensor },
|
||||
std::vector<char>(shader.begin(), shader.end()));
|
||||
kp::Shader::compile_source(shader));
|
||||
|
||||
// We map the result back to local
|
||||
this->mSequence->record<kp::OpTensorSyncLocal>(
|
||||
|
||||
@@ -44,16 +44,11 @@ int main()
|
||||
|
||||
sq->record<kp::OpTensorSyncDevice>({ wIn, bIn });
|
||||
|
||||
#ifdef KOMPUTE_ANDROID_SHADER_FROM_STRING
|
||||
sq->record<kp::OpAlgoBase>(
|
||||
params, "shaders/glsl/logistic_regression.comp");
|
||||
#else
|
||||
sq->record<kp::OpAlgoBase>(
|
||||
params, std::vector<uint32_t>(
|
||||
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<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
<a href="https://colab.research.google.com/drive/15uQ7qMZuOyk8JcXF-3SB2R5yNFW21I4P">
|
||||
<img src="https://raw.githubusercontent.com/EthicalML/vulkan-kompute/python_extensions/docs/images/binder-python.jpg">
|
||||
</a>
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ Shader::compile_sources(const std::vector<std::string>& 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<std::string>& 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<std::string>& 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<std::string>& 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<std::string>& 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.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef VK_USE_PLATFORM_ANDROID_KHR
|
||||
#if VK_USE_PLATFORM_ANDROID_KHR
|
||||
#include <android/log.h>
|
||||
#include <kompute_vk_ndk_wrapper.hpp>
|
||||
// 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
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "Core.hpp"
|
||||
|
||||
#include <glslang/Public/ShaderLang.h>
|
||||
#include <StandAlone/ResourceLimits.h>
|
||||
#include <SPIRV/GlslangToSpv.h>
|
||||
|
||||
#include "kompute/Core.hpp"
|
||||
|
||||
namespace kp {
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user