Viet-Hoa Do | 1df9f6e | 2023-07-24 17:57:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | |
| 25 | #ifndef CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARETENSORTEST_H |
| 26 | #define CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARETENSORTEST_H |
| 27 | |
| 28 | #include "ckw/Error.h" |
| 29 | #include "ckw/Kernel.h" |
| 30 | #include "ckw/KernelArgument.h" |
| 31 | #include "ckw/TensorInfo.h" |
| 32 | #include "ckw/types/TensorComponentType.h" |
| 33 | #include "ckw/types/TensorDataLayout.h" |
| 34 | #include "src/cl/CLKernelWriter.h" |
| 35 | #include "validation/tests/common/Common.h" |
| 36 | |
| 37 | namespace ckw |
| 38 | { |
| 39 | |
| 40 | class CLKernelWriterDeclareTensorTest : public ITest |
| 41 | { |
| 42 | public: |
| 43 | CLKernelWriterDeclareTensorTest() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | std::string name() override |
| 48 | { |
| 49 | return "CLKernelWriterDeclareTensorTest"; |
| 50 | } |
| 51 | |
| 52 | bool run() override |
| 53 | { |
| 54 | auto all_tests_passed = true; |
| 55 | |
| 56 | CLKernelWriter writer; |
| 57 | |
| 58 | auto src = writer.declare_tensor_argument("src", TensorInfo(DataType::Fp32, TensorShape{ 2, 3, 4, 5 }, TensorDataLayout::Nhwc, 0)); |
| 59 | auto dst = writer.declare_tensor_argument("dst", TensorInfo(DataType::Fp32, TensorShape{ 6, 7, 8, 9 }, TensorDataLayout::Nhwc, 1)); |
| 60 | |
| 61 | auto src_dim0 = src.dim0(); |
| 62 | auto src_stride2 = src.stride2(); |
| 63 | auto src_offset_element = src.offset_first_element_in_bytes(); |
| 64 | |
| 65 | auto dst_dim1 = dst.dim0(); |
| 66 | |
| 67 | auto src_dim0_again = src.dim0(); |
| 68 | |
| 69 | CKW_UNUSED(src_dim0, src_stride2, src_offset_element, dst_dim1, src_dim0_again); |
| 70 | |
| 71 | const auto kernel = writer.emit_kernel("test_kernel"); |
| 72 | |
| 73 | const std::string expected_code = |
| 74 | "__kernel void test_kernel\n" |
| 75 | "(\n" |
| 76 | "int G0__src_dim0,\n" |
| 77 | "int G0__src_stride2,\n" |
| 78 | "int G0__src_offset_first_element,\n" |
| 79 | "int G0__dst_dim0\n" |
| 80 | ")\n" |
| 81 | "{\n" |
| 82 | "}\n"; |
| 83 | |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 84 | std::string actual_code = kernel->source_code(); |
| 85 | |
| 86 | std::size_t pos = actual_code.find("__kernel"); |
| 87 | |
| 88 | if (pos != std::string::npos) |
| 89 | { |
| 90 | // Remove text before "__kernel" |
| 91 | actual_code = actual_code.substr(pos); |
| 92 | } |
Viet-Hoa Do | 1df9f6e | 2023-07-24 17:57:12 +0100 | [diff] [blame] | 93 | |
| 94 | int test_id = 0; |
| 95 | VALIDATE_TEST(kernel->arguments().size() == 4, all_tests_passed, test_id++); |
| 96 | test_tensor_component_argument(kernel->arguments()[0], 0, TensorComponentType::Dim0, all_tests_passed, test_id); |
| 97 | test_tensor_component_argument(kernel->arguments()[1], 0, TensorComponentType::Stride2, all_tests_passed, test_id); |
| 98 | test_tensor_component_argument(kernel->arguments()[2], 0, TensorComponentType::OffsetFirstElement, all_tests_passed, test_id); |
| 99 | test_tensor_component_argument(kernel->arguments()[3], 1, TensorComponentType::Dim0, all_tests_passed, test_id); |
| 100 | VALIDATE_TEST(actual_code == expected_code, all_tests_passed, test_id++); |
| 101 | |
| 102 | return all_tests_passed; |
| 103 | } |
| 104 | |
| 105 | void test_tensor_component_argument(const KernelArgument &arg, int32_t tensor_id, TensorComponentType component_type, bool &all_tests_passed, int &test_id) |
| 106 | { |
| 107 | VALIDATE_TEST(arg.type() == KernelArgument::Type::TensorComponent, all_tests_passed, test_id++); |
| 108 | VALIDATE_TEST(arg.id() == tensor_id, all_tests_passed, test_id++); |
| 109 | VALIDATE_TEST(arg.tensor_component_type() == component_type, all_tests_passed, test_id++); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | } // namespace ckw |
| 114 | |
| 115 | #endif // CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARETENSORTEST_H |