Abe Mbise | 4bd2cb8 | 2017-09-27 18:39:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_FIXED_POINT_CL_TARGET |
| 25 | #define ARM_COMPUTE_TEST_FIXED_POINT_CL_TARGET |
| 26 | |
| 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 28 | |
| 29 | #include "tests/Globals.h" |
| 30 | #include "tests/Types.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | namespace |
| 39 | { |
| 40 | template <typename TensorType, typename AccessorType, typename T> |
| 41 | void compute_target_impl(const TensorShape &shape, DataType dt, FixedPointOp op, int fixed_point_position, TensorType &src, TensorType &dst) |
| 42 | { |
| 43 | std::string fixed_point_operation_kernel; |
| 44 | #ifndef EMBEDDED_KERNELS |
| 45 | std::cout << "EMBEDDED_KERNELS NOT DEFINED" << std::endl; |
| 46 | |
| 47 | fixed_point_operation_kernel += "#include \"fixed_point.h\"\n"; |
| 48 | #endif /* EMBEDDED_KERNELS */ |
| 49 | fixed_point_operation_kernel += |
| 50 | "__kernel void fixed_point_operation_qs8( \n" |
| 51 | " __global char* src, \n" |
| 52 | " __global char* dst) \n" |
| 53 | "{ \n" |
| 54 | " char16 in = vload16(0, src + get_global_id(0) * 16); \n" |
| 55 | " if(FIXED_POINT_OP == 0) \n" |
| 56 | " { \n" |
| 57 | " vstore16(EXP_OP_EXPAND(in, DATA_TYPE, 16, FIXED_POINT_POS), 0, dst + get_global_id(0) * 16); \n" |
| 58 | " } \n" |
| 59 | " else if(FIXED_POINT_OP == 1) \n" |
| 60 | " { \n" |
| 61 | " vstore16(INVSQRT_OP_EXPAND(in, DATA_TYPE, 16, FIXED_POINT_POS), 0, dst + get_global_id(0) * 16); \n" |
| 62 | " } \n" |
| 63 | " else \n" |
| 64 | " { \n" |
| 65 | " vstore16(LOG_OP_EXPAND(in, DATA_TYPE, 16, FIXED_POINT_POS), 0, dst + get_global_id(0) * 16); \n" |
| 66 | " } \n" |
| 67 | "} \n" |
| 68 | "\n"; |
| 69 | |
| 70 | // Set build options |
| 71 | std::string build_opts = "-DFIXED_POINT_POS=" + support::cpp11::to_string(fixed_point_position); |
| 72 | build_opts += " -DDATA_TYPE=qs8"; |
| 73 | |
| 74 | // Fill tensors. |
| 75 | int min = 0; |
| 76 | int max = 0; |
| 77 | switch(op) |
| 78 | { |
| 79 | case(FixedPointOp::EXP): |
| 80 | min = -(1 << (fixed_point_position - 1)); |
| 81 | max = (1 << (fixed_point_position - 1)); |
| 82 | build_opts += " -DFIXED_POINT_OP=0"; |
| 83 | break; |
| 84 | case(FixedPointOp::INV_SQRT): |
| 85 | min = 1; |
| 86 | max = (dt == DataType::QS8) ? 0x7F : 0x7FFF; |
| 87 | build_opts += " -DFIXED_POINT_OP=1"; |
| 88 | break; |
| 89 | case(FixedPointOp::LOG): |
| 90 | min = (1 << (fixed_point_position - 1)); |
| 91 | max = (dt == DataType::QS8) ? 0x3F : 0x3FFF; |
| 92 | build_opts += " -DFIXED_POINT_OP=2"; |
| 93 | break; |
| 94 | default: |
| 95 | ARM_COMPUTE_ERROR("Fixed point operation not supported"); |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | std::uniform_int_distribution<> distribution(min, max); |
| 100 | library->fill(AccessorType(src), distribution, 0); |
| 101 | |
| 102 | std::vector<std::string> sources; |
| 103 | |
| 104 | #ifndef EMBEDDED_KERNELS |
| 105 | build_opts += " -I" + CLKernelLibrary::get().get_kernel_path(); |
| 106 | #else /* EMBEDDED_KERNELS */ |
| 107 | sources.push_back(CLKernelLibrary::get().get_program_source("fixed_point.h")); |
| 108 | #endif /* EMBEDDED_KERNELS */ |
| 109 | |
| 110 | sources.push_back(fixed_point_operation_kernel); |
| 111 | |
| 112 | // Create program |
| 113 | ::cl::Program program(sources); |
| 114 | |
| 115 | // Build program |
| 116 | program.build(build_opts.c_str()); |
| 117 | |
| 118 | ::cl::Kernel kernel(program, "fixed_point_operation_qs8", nullptr); |
| 119 | |
| 120 | unsigned int idx = 0; |
| 121 | kernel.setArg(idx++, src.cl_buffer()); |
| 122 | kernel.setArg(idx++, dst.cl_buffer()); |
| 123 | |
| 124 | ::cl::NDRange gws(shape[0] / 16, 1, 1); |
| 125 | CLScheduler::get().queue().enqueueNDRangeKernel(kernel, 0, gws); |
| 126 | } |
| 127 | } // namespace |
| 128 | } // namespace validation |
| 129 | } // namespace test |
| 130 | } // namespace arm_compute |
| 131 | #endif /* ARM_COMPUTE_TEST_FIXED_POINT_TARGET */ |