diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2017-08-30 19:17:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-30 19:17:17 +0200 |
commit | 6e95752054edc6bf49430ec40355d2aea691c32a (patch) | |
tree | d0027323a9f45ea4fd2a82bb029f5ba9d58adf44 /src | |
parent | da28cc5e9315729bc7f9078f1ae43ac68740ec87 (diff) | |
parent | 161fd8514d75d61045e1683e091f1851656c28da (diff) |
Merge pull request #184 from CNugteren/im_to_col
im2col
Diffstat (limited to 'src')
-rw-r--r-- | src/clblast.cpp | 37 | ||||
-rw-r--r-- | src/clblast_c.cpp | 67 | ||||
-rw-r--r-- | src/clblast_netlib_c.cpp | 90 | ||||
-rw-r--r-- | src/kernels/levelx/im2col.opencl | 78 | ||||
-rw-r--r-- | src/routines/levelx/xim2col.cpp | 91 | ||||
-rw-r--r-- | src/routines/levelx/xim2col.hpp | 44 | ||||
-rw-r--r-- | src/utilities/utilities.hpp | 25 |
7 files changed, 432 insertions, 0 deletions
diff --git a/src/clblast.cpp b/src/clblast.cpp index ca401066..9089b17c 100644 --- a/src/clblast.cpp +++ b/src/clblast.cpp @@ -72,6 +72,7 @@ // Level-x includes (non-BLAS) #include "routines/levelx/xomatcopy.hpp" +#include "routines/levelx/xim2col.hpp" #include "routines/levelx/xaxpybatched.hpp" #include "routines/levelx/xgemmbatched.hpp" @@ -2212,6 +2213,42 @@ template StatusCode PUBLIC_API Omatcopy<half>(const Layout, const Transpose, cl_mem, const size_t, const size_t, cl_command_queue*, cl_event*); +// Im2col function (non-BLAS function): SIM2COL/DIM2COL/CIM2COL/ZIM2COL/HIM2COL +template <typename T> +StatusCode Im2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + auto queue_cpp = Queue(*queue); + auto routine = Xim2col<T>(queue_cpp, event); + routine.DoIm2col(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + Buffer<T>(im_buffer), im_offset, + Buffer<T>(col_buffer), col_offset); + return StatusCode::kSuccess; + } catch (...) { return DispatchException(); } +} +template StatusCode PUBLIC_API Im2col<float>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<double>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<float2>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<double2>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); +template StatusCode PUBLIC_API Im2col<half>(const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, const size_t, + const cl_mem, const size_t, + cl_mem, const size_t, + cl_command_queue*, cl_event*); + // Batched version of AXPY: SAXPYBATCHED/DAXPYBATCHED/CAXPYBATCHED/ZAXPYBATCHED/HAXPYBATCHED template <typename T> StatusCode AxpyBatched(const size_t n, diff --git a/src/clblast_c.cpp b/src/clblast_c.cpp index d2656274..24697779 100644 --- a/src/clblast_c.cpp +++ b/src/clblast_c.cpp @@ -3515,6 +3515,73 @@ CLBlastStatusCode CLBlastHomatcopy(const CLBlastLayout layout, const CLBlastTran } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } } +// IM2COL +CLBlastStatusCode CLBlastSim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<float>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastDim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<double>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastCim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<float2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastZim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<double2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} +CLBlastStatusCode CLBlastHim2col(const size_t channels, const size_t height, const size_t width, const size_t kernel_h, const size_t kernel_w, const size_t pad_h, const size_t pad_w, const size_t stride_h, const size_t stride_w, const size_t dilation_h, const size_t dilation_w, + const cl_mem im_buffer, const size_t im_offset, + cl_mem col_buffer, const size_t col_offset, + cl_command_queue* queue, cl_event* event) { + try { + return static_cast<CLBlastStatusCode>( + clblast::Im2col<half>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer, im_offset, + col_buffer, col_offset, + queue, event) + ); + } catch (...) { return static_cast<CLBlastStatusCode>(clblast::DispatchExceptionForC()); } +} + // AXPY CLBlastStatusCode CLBlastSaxpyBatched(const size_t n, const float *alphas, diff --git a/src/clblast_netlib_c.cpp b/src/clblast_netlib_c.cpp index d3b9b5e6..7859dddf 100644 --- a/src/clblast_netlib_c.cpp +++ b/src/clblast_netlib_c.cpp @@ -4735,4 +4735,94 @@ void cblas_zomatcopy(const CLBlastLayout layout, const CLBlastTranspose a_transp b_buffer.Read(queue, b_size, reinterpret_cast<double2*>(b)); } +// IM2COL +void cblas_sim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const float* im, + float* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<float>(context, im_size); + auto col_buffer = clblast::Buffer<float>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const float*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<float*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<float>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<float*>(col)); +} +void cblas_dim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const double* im, + double* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<double>(context, im_size); + auto col_buffer = clblast::Buffer<double>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const double*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<double*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<double>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<double*>(col)); +} +void cblas_cim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const void* im, + void* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<float2>(context, im_size); + auto col_buffer = clblast::Buffer<float2>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const float2*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<float2*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<float2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<float2*>(col)); +} +void cblas_zim2col(const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, + const void* im, + void* col) { + auto device = get_device(); + auto context = clblast::Context(device); + auto queue = clblast::Queue(context, device); + const auto im_size = height * width * channels; + const auto col_size = height * width * channels; + auto im_buffer = clblast::Buffer<double2>(context, im_size); + auto col_buffer = clblast::Buffer<double2>(context, col_size); + im_buffer.Write(queue, im_size, reinterpret_cast<const double2*>(im)); + col_buffer.Write(queue, col_size, reinterpret_cast<double2*>(col)); + auto queue_cl = queue(); + auto s = clblast::Im2col<double2>(channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h, dilation_w, + im_buffer(), 0, + col_buffer(), 0, + &queue_cl); + if (s != clblast::StatusCode::kSuccess) { + throw std::runtime_error("CLBlast returned with error code " + clblast::ToString(s)); + } + col_buffer.Read(queue, col_size, reinterpret_cast<double2*>(col)); +} + // ================================================================================================= diff --git a/src/kernels/levelx/im2col.opencl b/src/kernels/levelx/im2col.opencl new file mode 100644 index 00000000..a141db41 --- /dev/null +++ b/src/kernels/levelx/im2col.opencl @@ -0,0 +1,78 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren <www.cedricnugteren.nl> +// +// This file contains the im2col kernel. +// +// ================================================================================================= + +// Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string +// literal). Comment-out this line for syntax-highlighting when developing. +R"( + +// Work-group size parameters re-used from the 'copy' kernel +#ifndef COPY_DIMX + #define COPY_DIMX 8 // Local workgroup size in the first dimension (x) +#endif +#ifndef COPY_DIMY + #define COPY_DIMY 8 // Local workgroup size in the second dimension (y) +#endif + +// ================================================================================================= + +__kernel __attribute__((reqd_work_group_size(COPY_DIMX, COPY_DIMY, 1))) +void im2col(const int input_h, const int input_w, + const int output_h, const int output_w, + const int kernel_h, const int kernel_w, + const int pad_h, const int pad_w, + const int stride_h, const int stride_w, + const int dilation_h, const int dilation_w, + const __global real* restrict im_buffer, const int im_offset, + __global real* col_buffer, const int col_offset) { + + // Thread IDs + const int w_id = get_global_id(0); // image width, max 'output_w' + const int h_id = get_global_id(1); // image height, max 'output_h' + const int c_id = get_global_id(2); // input channels + if (h_id < output_h && w_id < output_w) { + + #pragma unroll + for (int kh_id = 0; kh_id < kernel_h; ++kh_id) { // kernel height + #pragma unroll + for (int kw_id = 0; kw_id < kernel_w; ++kw_id) { // kernel width + + // Retrieves the input value + const int h_index = -pad_h + kh_id * dilation_h + stride_h * h_id; + const int w_index = -pad_w + kw_id * dilation_w + stride_w * w_id; + real val; + if (h_index >= 0 && h_index < input_h && + w_index >= 0 && w_index < input_w) { + const int input_index = w_index + input_w * (h_index + input_h * c_id); + val = im_buffer[input_index + im_offset]; + } + else { + SetToZero(val); + } + + // Sets the output value + const int kernel_index = kw_id + kernel_w * kh_id; + const int patch_index = w_id + output_w * h_id; + const int output_index = patch_index + kernel_index * output_w * output_h + + c_id * output_w * output_h * kernel_h * kernel_w; + col_buffer[output_index + col_offset] = val; + } + } + } +} + +// ================================================================================================= + +// End of the C++11 raw string literal +)" + +// ================================================================================================= diff --git a/src/routines/levelx/xim2col.cpp b/src/routines/levelx/xim2col.cpp new file mode 100644 index 00000000..51171eb5 --- /dev/null +++ b/src/routines/levelx/xim2col.cpp @@ -0,0 +1,91 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren <www.cedricnugteren.nl> +// +// This file implements the Xim2col class (see the header for information about the class). +// +// ================================================================================================= + +#include "routines/levelx/xim2col.hpp" + +#include <string> +#include <vector> + +namespace clblast { +// ================================================================================================= + +// Constructor: forwards to base class constructor +template <typename T> +Xim2col<T>::Xim2col(Queue &queue, EventPointer event, const std::string &name): + Routine(queue, event, name, {"Copy"}, PrecisionValue<T>(), {}, { +#include "../../kernels/levelx/im2col.opencl" + }) { +} + +// ================================================================================================= + +// The main routine +template <typename T> +void Xim2col<T>::DoIm2col(const size_t channels, const size_t height, const size_t width, + const size_t kernel_h, const size_t kernel_w, const size_t pad_h, + const size_t pad_w, const size_t stride_h, const size_t stride_w, + const size_t dilation_h, const size_t dilation_w, + const Buffer<T> &im_buffer, const size_t im_offset, + const Buffer<T> &col_buffer, const size_t col_offset) { + + // Makes sure all dimensions are larger than zero + if ((channels == 0) || (height == 0) || (width == 0)) { throw BLASError(StatusCode::kInvalidDimension); } + + // Sets the output height and width + const auto size_h = height + 2 * pad_h; + const auto padding_h = dilation_h * (kernel_h - 1) + 1; + const auto output_h = (size_h >= padding_h) ? (size_h - padding_h) / stride_h + 1 : 1; + const auto size_w = width + 2 * pad_w; + const auto padding_w = dilation_w * (kernel_w - 1) + 1; + const auto output_w = (size_w >= padding_w) ? (size_w - padding_w) / stride_w + 1 : 1; + + // Retrieves the Xcopy kernel from the compiled binary + auto kernel = Kernel(program_, "im2col"); + + // Sets the kernel arguments + kernel.SetArgument(0, static_cast<int>(height)); + kernel.SetArgument(1, static_cast<int>(width)); + kernel.SetArgument(2, static_cast<int>(output_h)); + kernel.SetArgument(3, static_cast<int>(output_w)); + kernel.SetArgument(4, static_cast<int>(kernel_h)); + kernel.SetArgument(5, static_cast<int>(kernel_w)); + kernel.SetArgument(6, static_cast<int>(pad_h)); + kernel.SetArgument(7, static_cast<int>(pad_w)); + kernel.SetArgument(8, static_cast<int>(stride_h)); + kernel.SetArgument(9, static_cast<int>(stride_w)); + kernel.SetArgument(10, static_cast<int>(dilation_h)); + kernel.SetArgument(11, static_cast<int>(dilation_w)); + kernel.SetArgument(12, im_buffer()); + kernel.SetArgument(13, static_cast<int>(im_offset)); + kernel.SetArgument(14, col_buffer()); + kernel.SetArgument(15, static_cast<int>(col_offset)); + + // Launches the kernel + const auto w_ceiled = Ceil(output_w, db_["COPY_DIMY"]); + const auto h_ceiled = Ceil(output_h, db_["COPY_DIMX"]); + const auto global = std::vector<size_t>{w_ceiled, h_ceiled, channels}; + const auto local = std::vector<size_t>{db_["COPY_DIMX"], db_["COPY_DIMY"], 1}; + RunKernel(kernel, queue_, device_, global, local, event_); +} + +// ================================================================================================= + +// Compiles the templated class +template class Xim2col<half>; +template class Xim2col<float>; +template class Xim2col<double>; +template class Xim2col<float2>; +template class Xim2col<double2>; + +// ================================================================================================= +} // namespace clblast diff --git a/src/routines/levelx/xim2col.hpp b/src/routines/levelx/xim2col.hpp new file mode 100644 index 00000000..4448b54e --- /dev/null +++ b/src/routines/levelx/xim2col.hpp @@ -0,0 +1,44 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren <www.cedricnugteren.nl> +// +// This file implements the Xim2col routine. The precision is implemented using a template argument. +// +// ================================================================================================= + +#ifndef CLBLAST_ROUTINES_XIM2COL_H_ +#define CLBLAST_ROUTINES_XIM2COL_H_ + +#include "routine.hpp" + +namespace clblast { +// ================================================================================================= + +// See comment at top of file for a description of the class +template <typename T> +class Xim2col: public Routine { + public: + + // Constructor + Xim2col(Queue &queue, EventPointer event, const std::string &name = "IM2COL"); + + // Templated-precision implementation of the routine + void DoIm2col(const size_t channels, const size_t height, const size_t width, + const size_t kernel_h, const size_t kernel_w, + const size_t pad_h, const size_t pad_w, + const size_t stride_h, const size_t stride_w, + const size_t dilation_h, const size_t dilation_w, + const Buffer<T> &im_buffer, const size_t im_offset, + const Buffer<T> &col_buffer, const size_t col_offset); +}; + +// ================================================================================================= +} // namespace clblast + +// CLBLAST_ROUTINES_XIM2COL_H_ +#endif diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index a9c492f3..fae69b63 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -77,6 +77,19 @@ constexpr auto kArgAlpha = "alpha"; constexpr auto kArgBeta = "beta"; constexpr auto kArgBatchCount = "batch_num"; +// Constants for im2col +constexpr auto kArgChannels = "channels"; +constexpr auto kArgHeight = "height"; +constexpr auto kArgWidth = "width"; +constexpr auto kArgKernelH = "kernelh"; +constexpr auto kArgKernelW = "kernelw"; +constexpr auto kArgPadH = "padh"; +constexpr auto kArgPadW = "padw"; +constexpr auto kArgStrideH = "strideh"; +constexpr auto kArgStrideW = "stridew"; +constexpr auto kArgDilationH = "dilationh"; +constexpr auto kArgDilationW = "dilationw"; + // The tuner-specific arguments in string form constexpr auto kArgFraction = "fraction"; constexpr auto kArgHeuristicSelection = "heuristic"; @@ -162,6 +175,18 @@ struct Arguments { size_t imax_offset = 0; T alpha = ConstantOne<T>(); T beta = ConstantOne<T>(); + // Arguments for im2col + size_t channels = 1; + size_t height = 1; + size_t width = 1; + size_t kernel_h = 3; + size_t kernel_w = 3; + size_t pad_h = 0; + size_t pad_w = 0; + size_t stride_h = 1; + size_t stride_w = 1; + size_t dilation_h = 1; + size_t dilation_w = 1; // Batch-specific arguments size_t batch_count = 1; std::vector<size_t> x_offsets; // = {0}; |