Added types tests
This commit is contained in:
parent
8abb2313d0
commit
df0dfd351f
3 changed files with 207 additions and 22 deletions
|
|
@ -9,27 +9,6 @@ DIRNAME = os.path.dirname(os.path.abspath(__file__))
|
|||
|
||||
kp_log = logging.getLogger("kp")
|
||||
|
||||
# TODO: Add example with file
|
||||
#def test_opalgobase_file():
|
||||
# """
|
||||
# Test basic OpMult operation
|
||||
# """
|
||||
#
|
||||
# tensor_in_a = kp.Tensor([2, 2, 2])
|
||||
# tensor_in_b = kp.Tensor([1, 2, 3])
|
||||
# tensor_out = kp.Tensor([0, 0, 0])
|
||||
#
|
||||
# mgr = kp.Manager()
|
||||
# mgr.rebuild([tensor_in_a, tensor_in_b, tensor_out])
|
||||
#
|
||||
# shader_path = os.path.join(DIRNAME, "../../shaders/glsl/opmult.comp.spv")
|
||||
#
|
||||
# mgr.eval_algo_file_def([tensor_in_a, tensor_in_b, tensor_out], shader_path)
|
||||
#
|
||||
# mgr.eval_tensor_sync_local_def([tensor_out])
|
||||
#
|
||||
# assert tensor_out.data() == [2.0, 4.0, 6.0]
|
||||
|
||||
def test_end_to_end():
|
||||
|
||||
mgr = kp.Manager()
|
||||
|
|
|
|||
206
python/test/test_tensor_types.py
Normal file
206
python/test/test_tensor_types.py
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
import pyshader as ps
|
||||
import os
|
||||
import pytest
|
||||
import kp
|
||||
import numpy as np
|
||||
|
||||
|
||||
def test_type_float():
|
||||
|
||||
shader = """
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer tensorLhs {float valuesLhs[];};
|
||||
layout(set = 0, binding = 1) buffer tensorRhs {float valuesRhs[];};
|
||||
layout(set = 0, binding = 2) buffer tensorOutput { float valuesOutput[];};
|
||||
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
valuesOutput[index] = valuesLhs[index] * valuesRhs[index];
|
||||
}
|
||||
"""
|
||||
|
||||
spirv = kp.Shader.compile_source(shader)
|
||||
|
||||
arr_in_a = np.array([123., 153., 231.], dtype=np.float32)
|
||||
arr_in_b = np.array([9482, 1208, 1238], dtype=np.float32)
|
||||
arr_out = np.array([0, 0, 0], dtype=np.float32)
|
||||
|
||||
mgr = kp.Manager()
|
||||
|
||||
tensor_in_a = mgr.tensor(arr_in_a)
|
||||
tensor_in_b = mgr.tensor(arr_in_b)
|
||||
tensor_out = mgr.tensor(arr_out)
|
||||
|
||||
params = [tensor_in_a, tensor_in_b, tensor_out]
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(mgr.algorithm(params, spirv)))
|
||||
.record(kp.OpTensorSyncLocal([tensor_out]))
|
||||
.eval())
|
||||
|
||||
assert np.all(tensor_out.data() == arr_in_a * arr_in_b)
|
||||
|
||||
|
||||
def test_type_float_double_incorrect():
|
||||
|
||||
shader = """
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer tensorLhs {float valuesLhs[];};
|
||||
layout(set = 0, binding = 1) buffer tensorRhs {float valuesRhs[];};
|
||||
layout(set = 0, binding = 2) buffer tensorOutput { float valuesOutput[];};
|
||||
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
valuesOutput[index] = valuesLhs[index] * valuesRhs[index];
|
||||
}
|
||||
"""
|
||||
|
||||
spirv = kp.Shader.compile_source(shader)
|
||||
|
||||
arr_in_a = np.array([123., 153., 231.], dtype=np.float32)
|
||||
arr_in_b = np.array([9482, 1208, 1238], dtype=np.uint32)
|
||||
arr_out = np.array([0, 0, 0], dtype=np.float32)
|
||||
|
||||
mgr = kp.Manager()
|
||||
|
||||
tensor_in_a = mgr.tensor_t(arr_in_a)
|
||||
tensor_in_b = mgr.tensor_t(arr_in_b)
|
||||
tensor_out = mgr.tensor_t(arr_out)
|
||||
|
||||
params = [tensor_in_a, tensor_in_b, tensor_out]
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(mgr.algorithm(params, spirv)))
|
||||
.record(kp.OpTensorSyncLocal([tensor_out]))
|
||||
.eval())
|
||||
|
||||
assert np.all(tensor_out.data() != arr_in_a * arr_in_b)
|
||||
|
||||
@pytest.mark.skipif("swiftshader" in os.environ.get("VK_ICD_FILENAMES"),
|
||||
reason="Swiftshader doesn't support double")
|
||||
def test_type_double():
|
||||
|
||||
shader = """
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer tensorLhs { double valuesLhs[]; };
|
||||
layout(set = 0, binding = 1) buffer tensorRhs { double valuesRhs[]; };
|
||||
layout(set = 0, binding = 2) buffer tensorOutput { double valuesOutput[]; };
|
||||
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
valuesOutput[index] = valuesLhs[index] * valuesRhs[index];
|
||||
}
|
||||
"""
|
||||
|
||||
spirv = kp.Shader.compile_source(shader)
|
||||
|
||||
arr_in_a = np.array([123., 153., 231.], dtype=np.float64)
|
||||
arr_in_b = np.array([9482, 1208, 1238], dtype=np.float64)
|
||||
arr_out = np.array([0, 0, 0], dtype=np.float64)
|
||||
|
||||
mgr = kp.Manager()
|
||||
|
||||
tensor_in_a = mgr.tensor_t(arr_in_a)
|
||||
tensor_in_b = mgr.tensor_t(arr_in_b)
|
||||
tensor_out = mgr.tensor_t(arr_out)
|
||||
|
||||
params = [tensor_in_a, tensor_in_b, tensor_out]
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(mgr.algorithm(params, spirv)))
|
||||
.record(kp.OpTensorSyncLocal([tensor_out]))
|
||||
.eval())
|
||||
|
||||
print(f"Dtype value {tensor_out.data().dtype}")
|
||||
|
||||
assert np.all(tensor_out.data() == arr_in_a * arr_in_b)
|
||||
|
||||
def test_type_int():
|
||||
|
||||
shader = """
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer tensorLhs { int valuesLhs[]; };
|
||||
layout(set = 0, binding = 1) buffer tensorRhs { int valuesRhs[]; };
|
||||
layout(set = 0, binding = 2) buffer tensorOutput { int valuesOutput[]; };
|
||||
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
valuesOutput[index] = valuesLhs[index] * valuesRhs[index];
|
||||
}
|
||||
"""
|
||||
|
||||
spirv = kp.Shader.compile_source(shader)
|
||||
|
||||
arr_in_a = np.array([123, 153, 231], dtype=np.int32)
|
||||
arr_in_b = np.array([9482, 1208, 1238], dtype=np.int32)
|
||||
arr_out = np.array([0, 0, 0], dtype=np.int32)
|
||||
|
||||
mgr = kp.Manager()
|
||||
|
||||
tensor_in_a = mgr.tensor_t(arr_in_a)
|
||||
tensor_in_b = mgr.tensor_t(arr_in_b)
|
||||
tensor_out = mgr.tensor_t(arr_out)
|
||||
|
||||
params = [tensor_in_a, tensor_in_b, tensor_out]
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(mgr.algorithm(params, spirv)))
|
||||
.record(kp.OpTensorSyncLocal([tensor_out]))
|
||||
.eval())
|
||||
|
||||
print(f"Dtype value {tensor_out.data().dtype}")
|
||||
|
||||
assert np.all(tensor_out.data() == arr_in_a * arr_in_b)
|
||||
|
||||
def test_type_unsigned_int():
|
||||
|
||||
shader = """
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer tensorLhs { uint valuesLhs[]; };
|
||||
layout(set = 0, binding = 1) buffer tensorRhs { uint valuesRhs[]; };
|
||||
layout(set = 0, binding = 2) buffer tensorOutput { uint valuesOutput[]; };
|
||||
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
void main()
|
||||
{
|
||||
uint index = gl_GlobalInvocationID.x;
|
||||
valuesOutput[index] = valuesLhs[index] * valuesRhs[index];
|
||||
}
|
||||
"""
|
||||
|
||||
spirv = kp.Shader.compile_source(shader)
|
||||
|
||||
arr_in_a = np.array([123, 153, 231], dtype=np.uint32)
|
||||
arr_in_b = np.array([9482, 1208, 1238], dtype=np.uint32)
|
||||
arr_out = np.array([0, 0, 0], dtype=np.uint32)
|
||||
|
||||
mgr = kp.Manager()
|
||||
|
||||
tensor_in_a = mgr.tensor_t(arr_in_a)
|
||||
tensor_in_b = mgr.tensor_t(arr_in_b)
|
||||
tensor_out = mgr.tensor_t(arr_out)
|
||||
|
||||
params = [tensor_in_a, tensor_in_b, tensor_out]
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(mgr.algorithm(params, spirv)))
|
||||
.record(kp.OpTensorSyncLocal([tensor_out]))
|
||||
.eval())
|
||||
|
||||
print(f"Dtype value {tensor_out.data().dtype}")
|
||||
|
||||
assert np.all(tensor_out.data() == arr_in_a * arr_in_b)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue