From 8abb2313d0d08010226a83100c3fdda5bcb2a89f Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sun, 7 Mar 2021 12:16:25 +0000 Subject: [PATCH] Updated python and cpp end to end test and readme to show support for different types on tensor --- README.md | 30 +++++++++++++++++------------ python/src/main.cpp | 5 ++++- python/test/test_kompute.py | 13 +++++++------ test/TestMultipleAlgoExecutions.cpp | 18 +++++++++-------- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 41596cb00..7a7375a6a 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,13 @@ void kompute(const std::string& shader) { kp::Manager mgr; // 2. Create and initialise Kompute Tensors through manager + + // Default tensor constructor simplifies creation of float values auto tensorInA = mgr.tensor({ 2., 2., 2. }); auto tensorInB = mgr.tensor({ 1., 2., 3. }); - auto tensorOutA = mgr.tensor({ 0., 0., 0. }); - auto tensorOutB = mgr.tensor({ 0., 0., 0. }); + // Explicit type constructor supports uint32, int32, double, float and bool + auto tensorOutA = mgr.tensorT({ 0, 0, 0 }); + auto tensorOutB = mgr.tensorT({ 0, 0, 0 }); std::vector> params = {tensorInA, tensorInB, tensorOutA, tensorOutB}; @@ -109,8 +112,8 @@ int main() { // The input tensors bind index is relative to index in parameter passed layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; }; layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; }; - layout(set = 0, binding = 2) buffer buf_out_a { float out_a[]; }; - layout(set = 0, binding = 3) buffer buf_out_b { float out_b[]; }; + layout(set = 0, binding = 2) buffer buf_out_a { uint out_a[]; }; + layout(set = 0, binding = 3) buffer buf_out_b { uint out_b[]; }; // Kompute supports push constants updated on dispatch layout(push_constant) uniform PushConstants { @@ -122,8 +125,8 @@ int main() { void main() { uint index = gl_GlobalInvocationID.x; - out_a[index] += in_a[index] * in_b[index]; - out_b[index] += const_one * push_const.val; + out_a[index] += uint( in_a[index] * in_b[index] ); + out_b[index] += uint( const_one * push_const.val ); } )"); @@ -144,10 +147,13 @@ def kompute(shader): mgr = kp.Manager() # 2. Create and initialise Kompute Tensors through manager + + # Default tensor constructor simplifies creation of float values tensor_in_a = mgr.tensor([2, 2, 2]) tensor_in_b = mgr.tensor([1, 2, 3]) - tensor_out_a = mgr.tensor([0, 0, 0]) - tensor_out_b = mgr.tensor([0, 0, 0]) + # Explicit type constructor supports uint32, int32, double, float and bool + tensor_out_a = mgr.tensor_t(np.array([0, 0, 0], dtype=np.uint32)) + tensor_out_b = mgr.tensor_t(np.array([0, 0, 0], dtype=np.uint32)) params = [tensor_in_a, tensor_in_b, tensor_out_a, tensor_out_b] @@ -194,8 +200,8 @@ if __name__ == "__main__": // The input tensors bind index is relative to index in parameter passed layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; }; layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; }; - layout(set = 0, binding = 2) buffer buf_out_a { float out_a[]; }; - layout(set = 0, binding = 3) buffer buf_out_b { float out_b[]; }; + layout(set = 0, binding = 2) buffer buf_out_a { uint out_a[]; }; + layout(set = 0, binding = 3) buffer buf_out_b { uint out_b[]; }; // Kompute supports push constants updated on dispatch layout(push_constant) uniform PushConstants { @@ -207,8 +213,8 @@ if __name__ == "__main__": void main() { uint index = gl_GlobalInvocationID.x; - out_a[index] += in_a[index] * in_b[index]; - out_b[index] += const_one * push_const.val; + out_a[index] += uint( in_a[index] * in_b[index] ); + out_b[index] += uint( const_one * push_const.val ); } """ diff --git a/python/src/main.cpp b/python/src/main.cpp index a82cd160d..495d0ed0c 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -173,6 +173,7 @@ PYBIND11_MODULE(kp, m) { kp::Tensor::TensorTypes tensor_type) { const py::array_t& flatdata = np.attr("ravel")(data); const py::buffer_info info = flatdata.request(); + KP_LOG_DEBUG("Kompute Python Manager tensor() creating tensor float with data size {}", flatdata.size()); return self.tensor( info.ptr, flatdata.size(), @@ -186,8 +187,10 @@ PYBIND11_MODULE(kp, m) { const py::array& data, kp::Tensor::TensorTypes tensor_type) { // TODO: Suppport strides in numpy format - const py::array_t& flatdata = np.attr("ravel")(data); + const py::array& flatdata = np.attr("ravel")(data); const py::buffer_info info = flatdata.request(); + KP_LOG_DEBUG("Kompute Python Manager creating tensor_T with data size {} dtype {}", + flatdata.size(), std::string(py::str(flatdata.dtype()))); if (flatdata.dtype() == py::dtype::of()) { return self.tensor( info.ptr, flatdata.size(), sizeof(float), kp::Tensor::TensorDataTypes::eFloat, tensor_type); diff --git a/python/test/test_kompute.py b/python/test/test_kompute.py index 47887930a..736768053 100644 --- a/python/test/test_kompute.py +++ b/python/test/test_kompute.py @@ -36,8 +36,9 @@ def test_end_to_end(): tensor_in_a = mgr.tensor([2, 2, 2]) tensor_in_b = mgr.tensor([1, 2, 3]) - tensor_out_a = mgr.tensor([0, 0, 0]) - tensor_out_b = mgr.tensor([0, 0, 0]) + # Explicit type constructor supports int, in32, double, float and int + tensor_out_a = mgr.tensor_t(np.array([0, 0, 0], dtype=np.uint32)) + tensor_out_b = mgr.tensor_t(np.array([0, 0, 0], dtype=np.uint32)) params = [tensor_in_a, tensor_in_b, tensor_out_a, tensor_out_b] @@ -49,8 +50,8 @@ def test_end_to_end(): // The input tensors bind index is relative to index in parameter passed layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; }; layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; }; - layout(set = 0, binding = 2) buffer buf_out_a { float out_a[]; }; - layout(set = 0, binding = 3) buffer buf_out_b { float out_b[]; }; + layout(set = 0, binding = 2) buffer buf_out_a { uint out_a[]; }; + layout(set = 0, binding = 3) buffer buf_out_b { uint out_b[]; }; // Kompute supports push constants updated on dispatch layout(push_constant) uniform PushConstants { @@ -62,8 +63,8 @@ def test_end_to_end(): void main() { uint index = gl_GlobalInvocationID.x; - out_a[index] += in_a[index] * in_b[index]; - out_b[index] += const_one * push_const.val; + out_a[index] += uint( in_a[index] * in_b[index] ); + out_b[index] += uint( const_one * push_const.val ); } """ diff --git a/test/TestMultipleAlgoExecutions.cpp b/test/TestMultipleAlgoExecutions.cpp index effc75227..f9e066f47 100644 --- a/test/TestMultipleAlgoExecutions.cpp +++ b/test/TestMultipleAlgoExecutions.cpp @@ -8,10 +8,12 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) kp::Manager mgr; + // Default tensor constructor simplifies creation of float values auto tensorInA = mgr.tensor({ 2., 2., 2. }); auto tensorInB = mgr.tensor({ 1., 2., 3. }); - auto tensorOutA = mgr.tensor({ 0., 0., 0. }); - auto tensorOutB = mgr.tensor({ 0., 0., 0. }); + // Explicit type constructor supports int, in32, double, float and int + auto tensorOutA = mgr.tensorT({ 0, 0, 0 }); + auto tensorOutB = mgr.tensorT({ 0, 0, 0 }); std::string shader = (R"( #version 450 @@ -21,8 +23,8 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) // The input tensors bind index is relative to index in parameter passed layout(set = 0, binding = 0) buffer buf_in_a { float in_a[]; }; layout(set = 0, binding = 1) buffer buf_in_b { float in_b[]; }; - layout(set = 0, binding = 2) buffer buf_out_a { float out_a[]; }; - layout(set = 0, binding = 3) buffer buf_out_b { float out_b[]; }; + layout(set = 0, binding = 2) buffer buf_out_a { uint out_a[]; }; + layout(set = 0, binding = 3) buffer buf_out_b { uint out_b[]; }; // Kompute supports push constants updated on dispatch layout(push_constant) uniform PushConstants { @@ -34,8 +36,8 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) void main() { uint index = gl_GlobalInvocationID.x; - out_a[index] += in_a[index] * in_b[index]; - out_b[index] += const_one * push_const.val; + out_a[index] += uint( in_a[index] * in_b[index] ); + out_b[index] += uint( const_one * push_const.val ); } )"); @@ -64,8 +66,8 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) sq->evalAwait(); - EXPECT_EQ(tensorOutA->vector(), std::vector({ 4, 8, 12 })); - EXPECT_EQ(tensorOutB->vector(), std::vector({ 10, 10, 10 })); + EXPECT_EQ(tensorOutA->vector(), std::vector({ 4, 8, 12 })); + EXPECT_EQ(tensorOutB->vector(), std::vector({ 10, 10, 10 })); } TEST(TestMultipleAlgoExecutions, SingleSequenceRecord)