Updated to use all uint32_t to avoid ambiguity on passing strings

This commit is contained in:
Alejandro Saucedo 2021-02-20 18:09:02 +00:00
parent 5bc3ac9c06
commit 56d9a3a933
18 changed files with 65 additions and 68 deletions

View file

@ -19,6 +19,7 @@ jobs:
- name: configure-cpp
run: |
cmake -Bbuild/ \
-DCMAKE_BUILD_TYPE=Debug \
-DKOMPUTE_OPT_INSTALL=0 \
-DKOMPUTE_OPT_REPO_SUBMODULE_BUILD=1 \
-DKOMPUTE_OPT_BUILD_TESTS=1 \

View file

@ -78,7 +78,7 @@ int main() {
// 3. Run operation with string shader synchronously
mgr.evalOpDefault<kp::OpAlgoBase>(
{ tensorInA, tensorInB, tensorOut },
std::vector<char>(shaderString.begin(), shaderString.end()));
std::vector<uint32_t>(shaderString.begin(), shaderString.end()));
// 4. Map results back from GPU memory to print the results
mgr.evalOpDefault<kp::OpTensorSyncLocal>({ tensorInA, tensorInB, tensorOut });

View file

@ -54,11 +54,11 @@ void KomputeModelML::train(std::vector<float> yData, std::vector<float> xIData,
#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<char>(LR_SHADER.begin(), LR_SHADER.end()));
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<char>(
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

View file

@ -40,11 +40,11 @@ int main()
mgr.evalOpDefault<kp::OpAlgoBase>(
{ tensorInA, tensorInB, tensorOut },
std::vector<char>(shader.begin(), shader.end()));
std::vector<uint32_t>(shader.begin(), shader.end()));
#else
mgr.evalOpDefault<kp::OpAlgoBase>(
{ tensorInA, tensorInB, tensorOut },
std::vector<char>(
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));

View file

@ -49,7 +49,7 @@ int main()
params, "shaders/glsl/logistic_regression.comp");
#else
sq->record<kp::OpAlgoBase>(
params, std::vector<char>(
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));

View file

@ -118,13 +118,13 @@ namespace kp {
class Shader {
public:
static std::vector<char> compile_sources(
static std::vector<uint32_t> compile_sources(
const std::vector<std::string>& sources,
const std::vector<std::string>& files = {},
const std::string& entryPoint = "main",
std::vector<std::pair<std::string,std::string>> definitions = {});
static std::vector<char> compile_source(
static std::vector<uint32_t> compile_source(
const std::string& source,
const std::string& entryPoint = "main",
std::vector<std::pair<std::string,std::string>> definitions = {});
@ -1683,7 +1683,7 @@ public:
* @specalizationInstalces The specialization parameters to pass to the function
* processing
*/
void init(const std::vector<char>& shaderFileData,
void init(const std::vector<uint32_t>& shaderFileData,
std::vector<std::shared_ptr<Tensor>> tensorParams);
/**
@ -1727,7 +1727,7 @@ private:
Constants mSpecializationConstants;
// Create util functions
void createShaderModule(const std::vector<char>& shaderFileData);
void createShaderModule(const std::vector<uint32_t>& shaderFileData);
void createPipeline();
// Parameters
@ -1808,7 +1808,7 @@ class OpAlgoBase : public OpBase
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer,
std::vector<std::shared_ptr<Tensor>>& tensors,
const std::vector<char>& shaderDataRaw,
const std::vector<uint32_t>& shaderDataRaw,
const Workgroup& komputeWorkgroup = {},
const Constants& specializationConstants = {});
@ -1860,9 +1860,9 @@ class OpAlgoBase : public OpBase
Workgroup mKomputeWorkgroup;
std::string mShaderFilePath; ///< Optional member variable which can be provided for the OpAlgoBase to find the data automatically and load for processing
std::vector<char> mShaderDataRaw; ///< Optional member variable which can be provided to contain either the raw shader content or the spirv binary content
std::vector<uint32_t> mShaderDataRaw; ///< Optional member variable which can be provided to contain either the raw shader content or the spirv binary content
virtual std::vector<char> fetchSpirvBinaryData();
virtual std::vector<uint32_t> fetchSpirvBinaryData();
};
} // End namespace kp
@ -1985,7 +1985,7 @@ class OpMult : public OpAlgoBase
SPDLOG_DEBUG("Kompute OpMult constructor with params");
#ifndef RELEASE
this->mShaderFilePath = "shaders/glsl/opmult.comp";
this->mShaderFilePath = "shaders/glsl/opmult.comp.spv";
#endif
}
@ -1994,15 +1994,15 @@ class OpMult : public OpAlgoBase
* If RELEASE=1 it will be using the static version of the shader which is
* loaded using this file directly. Otherwise it should not override the function.
*/
std::vector<char> fetchSpirvBinaryData() override
std::vector<uint32_t> fetchSpirvBinaryData() override
{
SPDLOG_WARN(
"Kompute OpMult Running shaders directly from header");
return std::vector<char>(
shader_data::shaders_glsl_opmult_comp_spv,
shader_data::shaders_glsl_opmult_comp_spv +
kp::shader_data::shaders_glsl_opmult_comp_spv_len);
return std::vector<uint32_t>(
(uint32_t*)shader_data::shaders_glsl_opmult_comp_spv,
(uint32_t*)(shader_data::shaders_glsl_opmult_comp_spv +
kp::shader_data::shaders_glsl_opmult_comp_spv_len));
}
#endif

View file

@ -108,7 +108,7 @@ Algorithm::~Algorithm()
}
void
Algorithm::init(const std::vector<char>& shaderFileData,
Algorithm::init(const std::vector<uint32_t>& shaderFileData,
std::vector<std::shared_ptr<Tensor>> tensorParams)
{
SPDLOG_DEBUG("Kompute Algorithm init started");
@ -206,14 +206,14 @@ Algorithm::createParameters(std::vector<std::shared_ptr<Tensor>>& tensorParams)
}
void
Algorithm::createShaderModule(const std::vector<char>& shaderFileData)
Algorithm::createShaderModule(const std::vector<uint32_t>& shaderFileData)
{
SPDLOG_DEBUG("Kompute Algorithm createShaderModule started");
vk::ShaderModuleCreateInfo shaderModuleInfo(
vk::ShaderModuleCreateFlags(),
shaderFileData.size(),
(uint32_t*)shaderFileData.data());
sizeof(uint32_t) * shaderFileData.size(),
shaderFileData.data());
SPDLOG_DEBUG("Kompute Algorithm Creating shader module. ShaderFileSize: {}",
shaderFileData.size());

View file

@ -61,7 +61,7 @@ OpAlgoBase::OpAlgoBase(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer,
std::vector<std::shared_ptr<Tensor>>& tensors,
const std::vector<char>& shaderDataRaw,
const std::vector<uint32_t>& shaderDataRaw,
const Workgroup& komputeWorkgroup,
const Constants& specializationConstants)
: OpAlgoBase(physicalDevice, device, commandBuffer, tensors, komputeWorkgroup, specializationConstants)
@ -98,7 +98,7 @@ OpAlgoBase::init()
SPDLOG_DEBUG("Kompute OpAlgoBase fetching spirv data");
std::vector<char> shaderFileData = this->fetchSpirvBinaryData();
std::vector<uint32_t> shaderFileData = this->fetchSpirvBinaryData();
SPDLOG_DEBUG("Kompute OpAlgoBase Initialising algorithm component");
@ -137,7 +137,7 @@ OpAlgoBase::postEval()
SPDLOG_DEBUG("Kompute OpAlgoBase postSubmit called");
}
std::vector<char>
std::vector<uint32_t>
OpAlgoBase::fetchSpirvBinaryData()
{
SPDLOG_DEBUG("Kompute OpAlgoBase Running fetchSpirvBinaryData");
@ -162,7 +162,7 @@ OpAlgoBase::fetchSpirvBinaryData()
SPDLOG_WARN("Kompute OpAlgoBase fetched {} bytes", shaderFileSize);
return std::vector<char>(shaderDataRaw, shaderDataRaw + shaderFileSize);
return std::vector<uint32_t>((uint32_t*)shaderDataRaw, (uint32_t*)(shaderDataRaw + shaderFileSize));
} else if (this->mShaderDataRaw.size()) {
SPDLOG_DEBUG("Kompute OpAlgoBase Reading data from data provided");
return this->mShaderDataRaw;

View file

@ -67,7 +67,7 @@ OpAlgoLhsRhsOut::init()
SPDLOG_DEBUG("Kompute OpAlgoLhsRhsOut fetching spirv data");
std::vector<char> shaderFileData = this->fetchSpirvBinaryData();
std::vector<uint32_t> shaderFileData = this->fetchSpirvBinaryData();
SPDLOG_DEBUG("Kompute OpAlgoLhsRhsOut Initialising algorithm component");

View file

@ -3,7 +3,7 @@
namespace kp {
std::vector<char>
std::vector<uint32_t>
Shader::compile_sources(const std::vector<std::string>& sources,
const std::vector<std::string>& files,
const std::string& entryPoint,
@ -81,10 +81,10 @@ Shader::compile_sources(const std::vector<std::string>& sources,
// Shutdown glslang library.
glslang::FinalizeProcess();
return std::vector<char>((char*)spirv.data(), (char*)(spirv.data()+spirv.size()) );
return spirv;
}
std::vector<char>
std::vector<uint32_t>
Shader::compile_source(const std::string& source,
const std::string& entryPoint,
std::vector<std::pair<std::string,std::string>> definitions) {

View file

@ -39,7 +39,7 @@ public:
* @specalizationInstalces The specialization parameters to pass to the function
* processing
*/
void init(const std::vector<char>& shaderFileData,
void init(const std::vector<uint32_t>& shaderFileData,
std::vector<std::shared_ptr<Tensor>> tensorParams);
/**
@ -83,7 +83,7 @@ private:
Constants mSpecializationConstants;
// Create util functions
void createShaderModule(const std::vector<char>& shaderFileData);
void createShaderModule(const std::vector<uint32_t>& shaderFileData);
void createPipeline();
// Parameters

View file

@ -13,13 +13,13 @@ namespace kp {
class Shader {
public:
static std::vector<char> compile_sources(
static std::vector<uint32_t> compile_sources(
const std::vector<std::string>& sources,
const std::vector<std::string>& files = {},
const std::string& entryPoint = "main",
std::vector<std::pair<std::string,std::string>> definitions = {});
static std::vector<char> compile_source(
static std::vector<uint32_t> compile_source(
const std::string& source,
const std::string& entryPoint = "main",
std::vector<std::pair<std::string,std::string>> definitions = {});

View file

@ -82,7 +82,7 @@ class OpAlgoBase : public OpBase
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer,
std::vector<std::shared_ptr<Tensor>>& tensors,
const std::vector<char>& shaderDataRaw,
const std::vector<uint32_t>& shaderDataRaw,
const Workgroup& komputeWorkgroup = {},
const Constants& specializationConstants = {});
@ -135,9 +135,9 @@ class OpAlgoBase : public OpBase
Workgroup mKomputeWorkgroup;
std::string mShaderFilePath; ///< Optional member variable which can be provided for the OpAlgoBase to find the data automatically and load for processing
std::vector<char> mShaderDataRaw; ///< Optional member variable which can be provided to contain either the raw shader content or the spirv binary content
std::vector<uint32_t> mShaderDataRaw; ///< Optional member variable which can be provided to contain either the raw shader content or the spirv binary content
virtual std::vector<char> fetchSpirvBinaryData();
virtual std::vector<uint32_t> fetchSpirvBinaryData();
};
} // End namespace kp

View file

@ -50,7 +50,7 @@ class OpMult : public OpAlgoBase
SPDLOG_DEBUG("Kompute OpMult constructor with params");
#ifndef RELEASE
this->mShaderFilePath = "shaders/glsl/opmult.comp";
this->mShaderFilePath = "shaders/glsl/opmult.comp.spv";
#endif
}
@ -59,15 +59,15 @@ class OpMult : public OpAlgoBase
* If RELEASE=1 it will be using the static version of the shader which is
* loaded using this file directly. Otherwise it should not override the function.
*/
std::vector<char> fetchSpirvBinaryData() override
std::vector<uint32_t> fetchSpirvBinaryData() override
{
SPDLOG_WARN(
"Kompute OpMult Running shaders directly from header");
return std::vector<char>(
shader_data::shaders_glsl_opmult_comp_spv,
shader_data::shaders_glsl_opmult_comp_spv +
kp::shader_data::shaders_glsl_opmult_comp_spv_len);
return std::vector<uint32_t>(
(uint32_t*)shader_data::shaders_glsl_opmult_comp_spv,
(uint32_t*)(shader_data::shaders_glsl_opmult_comp_spv +
kp::shader_data::shaders_glsl_opmult_comp_spv_len));
}
#endif

View file

@ -151,6 +151,8 @@ TEST(TestAsyncOperations, TestManagerAsyncExecution)
mgr.rebuild({ tensorA, tensorB });
std::vector<uint32_t> result = kp::Shader::compile_source(shader);
mgr.evalOpAsync<kp::OpAlgoBase>(
{ tensorA }, "asyncOne", kp::Shader::compile_source(shader));

View file

@ -43,10 +43,10 @@ TEST(TestLogisticRegression, TestMainLogisticRegression)
sq->record<kp::OpAlgoBase>(
params,
std::vector<char>(
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),
std::vector<uint32_t>(
(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)),
kp::Workgroup(), kp::Constants({5.0}));
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });
@ -81,7 +81,7 @@ TEST(TestLogisticRegression, TestMainLogisticRegression)
bIn->data()[0]);
}
TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy)
TEST(TestLogisticRegression, TestMainLogisticRegressionManualCopy)
{
uint32_t ITERATIONS = 100;
@ -120,19 +120,13 @@ TEST(TestLogisticRegressionAlgorithm, TestMainLogisticRegressionManualCopy)
// Record op algo base
sq->begin();
#ifdef KOMPUTE_SHADER_FROM_STRING
sq->record<kp::OpAlgoBase>(
params, "test/shaders/glsl/test_logistic_regression.comp.spv",
kp::Workgroup(), kp::Algorithm::SpecializationContainer{{(uint32_t)5}});
#else
sq->record<kp::OpAlgoBase>(
params,
std::vector<char>(
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),
kp::Workgroup(), kp::Constants({5.0}));
#endif
std::vector<uint32_t>(
(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)),
kp::Workgroup(), kp::Constants({5.0}));
sq->record<kp::OpTensorSyncLocal>({ wOutI, wOutJ, bOut, lOut });

View file

@ -47,11 +47,11 @@ TEST(TestOpAlgoBase, ShaderCompiledDataFromConstructor)
mgr.evalOpDefault<kp::OpAlgoBase>(
{ tensorA, tensorB },
std::vector<char>(
kp::shader_data::test_shaders_glsl_test_op_custom_shader_comp_spv,
kp::shader_data::test_shaders_glsl_test_op_custom_shader_comp_spv +
std::vector<uint32_t>(
(uint32_t*)kp::shader_data::test_shaders_glsl_test_op_custom_shader_comp_spv,
(uint32_t*)(kp::shader_data::test_shaders_glsl_test_op_custom_shader_comp_spv +
kp::shader_data::
test_shaders_glsl_test_op_custom_shader_comp_spv_len));
test_shaders_glsl_test_op_custom_shader_comp_spv_len)));
mgr.evalOpDefault<kp::OpTensorSyncLocal>({ tensorA, tensorB });

View file

@ -25,10 +25,10 @@ TEST(TestWorkgroup, TestSimpleWorkgroup)
sq->begin();
sq->record<kp::OpAlgoBase>(
{ tensorA, tensorB },
std::vector<char>(
kp::shader_data::test_shaders_glsl_test_workgroup_comp_spv,
kp::shader_data::test_shaders_glsl_test_workgroup_comp_spv +
kp::shader_data::test_shaders_glsl_test_workgroup_comp_spv_len),
std::vector<uint32_t>(
(uint32_t*)kp::shader_data::test_shaders_glsl_test_workgroup_comp_spv,
(uint32_t*)(kp::shader_data::test_shaders_glsl_test_workgroup_comp_spv +
kp::shader_data::test_shaders_glsl_test_workgroup_comp_spv_len)),
workgroup);
sq->end();