From a043050243b7474e670bc3a9a590aae736744c38 Mon Sep 17 00:00:00 2001 From: yangfei Date: Mon, 15 Oct 2018 11:38:36 +0800 Subject: [PATCH] add some function --- src/common/common.h | 2 +- src/framework/cl/cl_image.h | 15 +++++++++- src/framework/executor.cpp | 2 +- src/framework/operator.cpp | 38 ++++++++++++++++--------- src/operators/kernel/cl/feed_kernel.cpp | 8 +++--- test/net/test_googlenet.cpp | 3 +- test/net/test_mobilenet_GPU.cpp | 7 +++-- 7 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/common/common.h b/src/common/common.h index c59e6b7932..ed23b543b3 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -28,4 +28,4 @@ inline double time_diff(Time t1, Time t2) { return counter.count() / 1000.0; } -} +} // namespace paddle_mobile diff --git a/src/framework/cl/cl_image.h b/src/framework/cl/cl_image.h index 5943ecc9c7..a144ccf35b 100644 --- a/src/framework/cl/cl_image.h +++ b/src/framework/cl/cl_image.h @@ -118,7 +118,20 @@ class CLImage { cl_image_format cf = {.image_channel_order = CL_RGBA, .image_channel_data_type = CL_HALF_FLOAT}; // NCHW -> [W * (C+3)/4, H * N] - DLOG << tensor_dims_; + tensor_dims_ = dim; + if (tensor_data) { + tensor_data_ = tensor_data; + } else { + int numel = 1; + for (int i = 0; i < dim.size(); i++) { + numel *= dim[i]; + } + tensor_data_ = static_cast( + paddle_mobile::memory::Alloc(sizeof(float) * numel)); + for (int i = 0; i < numel; i++) { + tensor_data_[i] = 0; + } + } size_t N, C, H, W; if (tensor_dims_.size() == 4) { N = tensor_dims_[0]; diff --git a/src/framework/executor.cpp b/src/framework/executor.cpp index 27052de2fd..274a6a1d82 100644 --- a/src/framework/executor.cpp +++ b/src/framework/executor.cpp @@ -936,7 +936,7 @@ void Executor::InitMemory() { cl_image->SetTensorData(tensorInput, ddim); delete origin_data; - paddle_mobile::memory::Free(tensorInput); + // paddle_mobile::memory::Free(tensorInput); } else { if (var_desc->Type() == framework::VARTYPE_TYPE_LOD_TENSOR) { auto cl_image = var->template GetMutable(); diff --git a/src/framework/operator.cpp b/src/framework/operator.cpp index fa04ac2e46..68ce52114c 100644 --- a/src/framework/operator.cpp +++ b/src/framework/operator.cpp @@ -72,13 +72,16 @@ void OperatorBase::Run() { if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor; } else { CLImage *cl_image = vari->template GetMutable(); - // cl_command_queue commandQueue = - // scope_->GetCLScpoe()->CommandQueue(); Tensor *tmp ; - // CLImageToTensor(cl_image,tmp,commandQueue); - // tmp->Resize(cl_image->dims()); + // cl_command_queue commandQueue = + // scope_->GetCLScpoe()->CommandQueue(); Tensor + // *tmp ; + // CLImageToTensor(cl_image,tmp,commandQueue); + // tmp->Resize(cl_image->dims()); + const float *input = cl_image->data(); if (cl_image) { - // DLOG<template GetMutable(); + if (tensor) + DLOG << type_ << " output- " << key << "=" << tensor->dims(); + } else { + CLImage *cl_image = vari->template GetMutable(); + // cl_command_queue commandQueue = + // scope_->GetCLScpoe()->CommandQueue(); Tensor *tmp ; + // CLImageToTensor(cl_image,tmp,commandQueue); + // tmp->Resize(cl_image->dims()); + if (cl_image) { + const float *output = cl_image->data(); + DLOG << type_ << " output- " << key << "=" << cl_image->dims(); + // if(output) + // DLOG<cl_helper_.KernelAt(0); cl_int status; auto output = param.Out(); - auto input = param.InputX(); - DLOG << " input: " << input; + const Tensor *input = param.InputX(); + const float *input_data = nullptr; + input_data = input->data(); - const float *input_data = input->data(); cl_mem cl_image = output->GetCLImage(); int height = output->dims()[2]; int width = output->dims()[3]; + DLOG << output->dims(); status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &input_data); status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &cl_image); status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &width); diff --git a/test/net/test_googlenet.cpp b/test/net/test_googlenet.cpp index 9e826d3a74..6e3465271b 100644 --- a/test/net/test_googlenet.cpp +++ b/test/net/test_googlenet.cpp @@ -30,7 +30,8 @@ int main() { auto time1 = time(); if (paddle_mobile.Load(g_googlenet, optimize)) { auto time2 = paddle_mobile::time(); - std::cout << "load cost :" << paddle_mobile::time_diff(time1, time2) << "ms" << std::endl; + std::cout << "load cost :" << paddle_mobile::time_diff(time1, time2) << "ms" + << std::endl; std::vector input; std::vector dims{1, 3, 224, 224}; GetInput(g_test_image_1x3x224x224, &input, dims); diff --git a/test/net/test_mobilenet_GPU.cpp b/test/net/test_mobilenet_GPU.cpp index f65e1890f3..70983e2b15 100644 --- a/test/net/test_mobilenet_GPU.cpp +++ b/test/net/test_mobilenet_GPU.cpp @@ -26,7 +26,8 @@ int main() { auto isok = paddle_mobile.Load(g_mobilenet, false); if (isok) { auto time2 = paddle_mobile::time(); - std::cout << "load cost :" << paddle_mobile::time_diff(time1, time1) << "ms" << std::endl; + std::cout << "load cost :" << paddle_mobile::time_diff(time1, time1) << "ms" + << std::endl; std::vector input; std::vector dims{1, 3, 224, 224}; @@ -48,8 +49,8 @@ int main() { } DLOG << vec_result; auto time4 = paddle_mobile::time(); - std::cout << "predict cost :" << paddle_mobile::time_diff(time3, time4) / 10 << "ms" - << std::endl; + std::cout << "predict cost :" << paddle_mobile::time_diff(time3, time4) / 10 + << "ms" << std::endl; } std::cout << "如果结果Nan请查看: test/images/g_test_image_1x3x224x224_banana " -- GitLab