All python tests pass
This commit is contained in:
parent
4c4d073b90
commit
91d3b9a223
11 changed files with 158 additions and 169 deletions
|
|
@ -54,17 +54,20 @@ PYBIND11_MODULE(kp, m) {
|
|||
|
||||
py::class_<kp::OpBase, std::shared_ptr<kp::OpBase>>(m, "OpBase");
|
||||
|
||||
py::class_<kp::OpTensorSyncDevice, std::shared_ptr<kp::OpTensorSyncDevice>>(m, "OpTensorSyncDevice")
|
||||
py::class_<kp::OpTensorSyncDevice, std::shared_ptr<kp::OpTensorSyncDevice>>(m, "OpTensorSyncDevice", py::base<kp::OpBase>())
|
||||
.def(py::init<const std::vector<std::shared_ptr<kp::Tensor>>&>());
|
||||
|
||||
py::class_<kp::OpTensorSyncLocal, std::shared_ptr<kp::OpTensorSyncLocal>>(m, "OpTensorSyncLocal")
|
||||
py::class_<kp::OpTensorSyncLocal, std::shared_ptr<kp::OpTensorSyncLocal>>(m, "OpTensorSyncLocal", py::base<kp::OpBase>())
|
||||
.def(py::init<const std::vector<std::shared_ptr<kp::Tensor>>&>());
|
||||
|
||||
py::class_<kp::OpTensorCopy, std::shared_ptr<kp::OpTensorCopy>>(m, "OpTensorCopy")
|
||||
py::class_<kp::OpTensorCopy, std::shared_ptr<kp::OpTensorCopy>>(m, "OpTensorCopy", py::base<kp::OpBase>())
|
||||
.def(py::init<const std::vector<std::shared_ptr<kp::Tensor>>&>());
|
||||
|
||||
py::class_<kp::OpAlgoDispatch, std::shared_ptr<kp::OpAlgoDispatch>>(m, "OpAlgoDispatch")
|
||||
.def(py::init<const std::shared_ptr<kp::Algorithm>&, bool>());
|
||||
py::class_<kp::OpAlgoDispatch, std::shared_ptr<kp::OpAlgoDispatch>>(m, "OpAlgoDispatch", py::base<kp::OpBase>())
|
||||
.def(py::init<const std::shared_ptr<kp::Algorithm>&>());
|
||||
|
||||
py::class_<kp::OpMult, std::shared_ptr<kp::OpMult>>(m, "OpMult", py::base<kp::OpBase>())
|
||||
.def(py::init<const std::vector<std::shared_ptr<kp::Tensor>>&,const std::shared_ptr<kp::Algorithm>&>());
|
||||
|
||||
py::class_<kp::Algorithm, std::shared_ptr<kp::Algorithm>>(m, "Algorithm")
|
||||
.def("get_tensors", &kp::Algorithm::getTensors)
|
||||
|
|
@ -112,8 +115,7 @@ PYBIND11_MODULE(kp, m) {
|
|||
.def("__len__", &kp::Tensor::size, "Retrieves the size of the Tensor data as per the local Tensor memory.")
|
||||
.def("tensor_type", &kp::Tensor::tensorType, "Retreves the memory type of the tensor.")
|
||||
.def("is_init", &kp::Tensor::isInit, "Checks whether the tensor GPU memory has been initialised.")
|
||||
.def("map_data_from_host", &kp::Tensor::mapDataFromHostMemory, "Maps data into GPU memory from tensor local data.")
|
||||
.def("map_data_into_host", &kp::Tensor::mapDataIntoHostMemory, "Maps data from GPU memory into tensor local data.");
|
||||
.def("destroy", &kp::Tensor::destroy, "Destroy tensor GPU resources.");
|
||||
|
||||
py::class_<kp::Sequence, std::shared_ptr<kp::Sequence>>(m, "Sequence")
|
||||
.def("record", [](kp::Sequence& self, std::shared_ptr<kp::OpBase> op) { return self.record(op); })
|
||||
|
|
@ -147,15 +149,17 @@ PYBIND11_MODULE(kp, m) {
|
|||
.def("algorithm", [](kp::Manager& self,
|
||||
const std::vector<std::shared_ptr<kp::Tensor>>& tensors,
|
||||
const py::bytes& spirv,
|
||||
const kp::Workgroup& workgroup = {},
|
||||
const kp::Constants& spec_consts = {},
|
||||
const kp::Constants& push_consts = {}) {
|
||||
const kp::Workgroup& workgroup,
|
||||
const kp::Constants& spec_consts,
|
||||
const kp::Constants& push_consts) {
|
||||
py::buffer_info info(py::buffer(spirv).request());
|
||||
const char *data = reinterpret_cast<const char *>(info.ptr);
|
||||
size_t length = static_cast<size_t>(info.size);
|
||||
std::vector<uint32_t> spirvVec((uint32_t*)data, (uint32_t*)(data + length));
|
||||
return self.algorithm(tensors, spirvVec, workgroup, spec_consts, push_consts);
|
||||
});
|
||||
},
|
||||
"Algorithm initialisation function",
|
||||
py::arg("tensors"), py::arg("spirv"), py::arg("workgroup") = kp::Workgroup(), py::arg("spec_consts") = kp::Constants(), py::arg("push_consts") = kp::Constants());
|
||||
|
||||
#ifdef VERSION_INFO
|
||||
m.attr("__version__") = VERSION_INFO;
|
||||
|
|
|
|||
|
|
@ -9,29 +9,26 @@ def test_array_multiplication():
|
|||
mgr = kp.Manager()
|
||||
|
||||
# 2. Create Kompute Tensors to hold data
|
||||
tensor_in_a = kp.Tensor([2, 2, 2])
|
||||
tensor_in_b = kp.Tensor([1, 2, 3])
|
||||
tensor_out = kp.Tensor([0, 0, 0])
|
||||
tensor_in_a = mgr.tensor([2, 2, 2])
|
||||
tensor_in_b = mgr.tensor([1, 2, 3])
|
||||
tensor_out = mgr.tensor([0, 0, 0])
|
||||
|
||||
# 3. Initialise the Kompute Tensors in the GPU
|
||||
mgr.rebuild([tensor_in_a, tensor_in_b, tensor_out])
|
||||
params = [tensor_in_a, tensor_in_b, tensor_out]
|
||||
|
||||
# 4. Define the multiplication shader code to run on the GPU
|
||||
@ps.python2shader
|
||||
def compute_shader_multiply(index=("input", "GlobalInvocationId", ps.ivec3),
|
||||
def compute_mult(index=("input", "GlobalInvocationId", ps.ivec3),
|
||||
data1=("buffer", 0, ps.Array(ps.f32)),
|
||||
data2=("buffer", 1, ps.Array(ps.f32)),
|
||||
data3=("buffer", 2, ps.Array(ps.f32))):
|
||||
i = index.x
|
||||
data3[i] = data1[i] * data2[i]
|
||||
|
||||
# 5. Run shader code against our previously defined tensors
|
||||
mgr.eval_algo_data_def(
|
||||
[tensor_in_a, tensor_in_b, tensor_out],
|
||||
compute_shader_multiply.to_spirv())
|
||||
|
||||
# 6. Sync tensor data from GPU back to local
|
||||
mgr.eval_tensor_sync_local_def([tensor_out])
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(mgr.algorithm(params, compute_mult.to_spirv())))
|
||||
.record(kp.OpTensorSyncLocal([tensor_out]))
|
||||
.eval())
|
||||
|
||||
assert tensor_out.data() == [2.0, 4.0, 6.0]
|
||||
assert np.all(tensor_out.numpy() == [2.0, 4.0, 6.0])
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import pyshader as ps
|
|||
|
||||
DIRNAME = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
kp_log = logging.getLogger("kp")
|
||||
|
||||
# TODO: Add example with file
|
||||
#def test_opalgobase_file():
|
||||
# """
|
||||
|
|
@ -62,9 +64,9 @@ void main()
|
|||
algo = mgr.algorithm(params, spirv)
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncLocal(params))
|
||||
.record(kp.OpAlgoDispatch(algo))
|
||||
.record(kp.OpTensorSyncDevice(params))
|
||||
.record(kp.OpAlgoDispatch(algo))
|
||||
.record(kp.OpTensorSyncLocal(params))
|
||||
.eval())
|
||||
|
||||
assert tensor_out.data() == [2.0, 4.0, 6.0]
|
||||
|
|
@ -102,9 +104,9 @@ def test_sequence():
|
|||
|
||||
sq = mgr.sequence()
|
||||
|
||||
sq.record(kp.OpTensorSyncLocal(params))
|
||||
sq.record(kp.OpAlgoDispatch(algo))
|
||||
sq.record(kp.OpTensorSyncDevice(params))
|
||||
sq.record(kp.OpAlgoDispatch(algo))
|
||||
sq.record(kp.OpTensorSyncLocal(params))
|
||||
|
||||
sq.eval()
|
||||
|
||||
|
|
@ -141,16 +143,14 @@ def test_workgroup():
|
|||
data1[i] = f32(gl_idx.x)
|
||||
data2[i] = f32(gl_idx.y)
|
||||
|
||||
algo = mgr.algorithm([tensor_a, tensor_b], compute_shader_wg.to_spirv(), (16,8,1), [], [])
|
||||
algo = mgr.algorithm([tensor_a, tensor_b], compute_shader_wg.to_spirv(), (16,8,1))
|
||||
|
||||
(mgr.sequence()
|
||||
.record(kp.OpTensorSyncDevice([tensor_a, tensor_b]))
|
||||
.record(kp.OpAlgoDispatch(algo))
|
||||
.record(kp.OpAlgoTensorSyncLocal([tensor_a, tensor_b]))
|
||||
.record(kp.OpTensorSyncLocal([tensor_a, tensor_b]))
|
||||
.eval())
|
||||
|
||||
assert sq.is_init() == False
|
||||
|
||||
print(tensor_a.numpy())
|
||||
print(tensor_b.numpy())
|
||||
|
||||
|
|
|
|||
|
|
@ -46,45 +46,39 @@ def test_logistic_regression():
|
|||
mgr = kp.Manager(0)
|
||||
|
||||
# First we create input and ouput tensors for shader
|
||||
tensor_x_i = kp.Tensor([0.0, 1.0, 1.0, 1.0, 1.0])
|
||||
tensor_x_j = kp.Tensor([0.0, 0.0, 0.0, 1.0, 1.0])
|
||||
tensor_x_i = mgr.tensor([0.0, 1.0, 1.0, 1.0, 1.0])
|
||||
tensor_x_j = mgr.tensor([0.0, 0.0, 0.0, 1.0, 1.0])
|
||||
|
||||
tensor_y = kp.Tensor([0.0, 0.0, 0.0, 1.0, 1.0])
|
||||
tensor_y = mgr.tensor([0.0, 0.0, 0.0, 1.0, 1.0])
|
||||
|
||||
tensor_w_in = kp.Tensor([0.001, 0.001])
|
||||
tensor_w_out_i = kp.Tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
tensor_w_out_j = kp.Tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
tensor_w_in = mgr.tensor([0.001, 0.001])
|
||||
tensor_w_out_i = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
tensor_w_out_j = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
|
||||
tensor_b_in = kp.Tensor([0.0])
|
||||
tensor_b_out = kp.Tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
tensor_b_in = mgr.tensor([0.0])
|
||||
tensor_b_out = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
|
||||
tensor_l_out = kp.Tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
tensor_l_out = mgr.tensor([0.0, 0.0, 0.0, 0.0, 0.0])
|
||||
|
||||
tensor_m = kp.Tensor([ tensor_y.size() ])
|
||||
tensor_m = mgr.tensor([ tensor_y.size() ])
|
||||
|
||||
# We store them in an array for easier interaction
|
||||
params = [tensor_x_i, tensor_x_j, tensor_y, tensor_w_in, tensor_w_out_i,
|
||||
tensor_w_out_j, tensor_b_in, tensor_b_out, tensor_l_out, tensor_m]
|
||||
|
||||
mgr.rebuild(params)
|
||||
mgr.sequence().eval(kp.OpTensorSyncDevice(params))
|
||||
|
||||
# Create a managed sequence
|
||||
sq = mgr.sequence()
|
||||
|
||||
# Clear previous operations and begin recording for new operations
|
||||
sq.begin()
|
||||
|
||||
# Record operation to sync memory from local to GPU memory
|
||||
sq.record_tensor_sync_device([tensor_w_in, tensor_b_in])
|
||||
sq.record(kp.OpTensorSyncDevice([tensor_w_in, tensor_b_in]))
|
||||
|
||||
# Record operation to execute GPU shader against all our parameters
|
||||
sq.record_algo_data(params, compute_shader.to_spirv())
|
||||
sq.record(kp.OpAlgoDispatch(mgr.algorithm(params, compute_shader.to_spirv())))
|
||||
|
||||
# Record operation to sync memory from GPU to local memory
|
||||
sq.record_tensor_sync_local([tensor_w_out_i, tensor_w_out_j, tensor_b_out, tensor_l_out])
|
||||
|
||||
# Stop recording operations
|
||||
sq.end()
|
||||
sq.record(kp.OpTensorSyncLocal([tensor_w_out_i, tensor_w_out_j, tensor_b_out, tensor_l_out]))
|
||||
|
||||
ITERATIONS = 100
|
||||
learning_rate = 0.1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue