blob: a185cce5450b0967c8e0d3eba9d01d3dcb30e8fa [file] [log] [blame]
Viet-Hoa Doe1c3b462023-07-31 17:13:34 +01001/*
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_CLKERNELWRITERCASTTEST_H
26#define CKW_VALIDATION_TESTS_CLKERNELWRITERCASTTEST_H
27
28#include "ckw/TileInfo.h"
29#include "ckw/types/ConvertPolicy.h"
30#include "ckw/types/DataType.h"
31#include "src/cl/CLKernelWriter.h"
32#include "validation/tests/common/Common.h"
33#include "validation/tests/common/KernelWriterInterceptor.h"
34
35#include <cstdint>
36#include <vector>
37
38namespace ckw
39{
40
41class CLKernelWriterCastTest : public ITest
42{
43public:
44 CLKernelWriterCastTest()
45 {
46 _tests.push_back({ 1, 1, DataType::Fp16, 1, 1, DataType::Fp32, ConvertPolicy::None, "G0__dst = convert_half(G0__src);\n" }); // Scalar.
47
48 _tests.push_back({ 1, 3, DataType::Int32, 1, 3, DataType::Fp16, ConvertPolicy::Saturate, "G0__dst = convert_int3_sat(G0__src);\n" }); // Whole vector.
49
50 _tests.push_back({ 2, 4, DataType::Uint16, 2, 4, DataType::Int8, ConvertPolicy::Saturate, "G0__dst__0 = convert_ushort4_sat(G0__src__0);\nG0__dst__1 = convert_ushort4_sat(G0__src__1);\n" }); // Whole tile.
51
52 _tests.push_back({ 2, 3, DataType::Int8, 1, 3, DataType::Uint8, ConvertPolicy::None, "G0__dst__0 = convert_char3(G0__src);\nG0__dst__1 = convert_char3(G0__src);\n" }); // Y-dimension broadcast.
53
54 _tests.push_back({ 2, 4, DataType::Fp16, 2, 1, DataType::Fp32, ConvertPolicy::None, "G0__dst__0 = (half4)convert_half(G0__src__0);\nG0__dst__1 = (half4)convert_half(G0__src__1);\n" }); // X-dimension broadcast.
55
56 _tests.push_back({ 2, 3, DataType::Fp32, 1, 1, DataType::Fp16, ConvertPolicy::None, "G0__dst__0 = (float3)convert_float(G0__src);\nG0__dst__1 = (float3)convert_float(G0__src);\n" }); // X and y dimension broadcast.
57 }
58
59 bool run() override
60 {
61 int32_t test_no = 0;
62 bool all_tests_passed = true;
63
64 for(const auto &test : _tests)
65 {
66 KernelWriterInterceptor<CLKernelWriter> writer;
67
68 auto dst = writer.declare_tile("dst", TileInfo(test.dst_data_type, test.dst_height, test.dst_width));
69 auto src = writer.declare_tile("src", TileInfo(test.src_data_type, test.src_height, test.src_width));
70
71 writer.start_capture_code();
72
73 writer.op_cast(dst, src, test.policy);
74
75 VALIDATE_TEST(writer.check_added_code(test.expected_code), all_tests_passed, test_no++);
76 }
77
78 return all_tests_passed;
79 }
80
81 std::string name() override
82 {
83 return "CLKernelWriterCastTest";
84 }
85
86private:
87 struct TestInfo
88 {
89 int32_t dst_height;
90 int32_t dst_width;
91 DataType dst_data_type;
92 int32_t src_height;
93 int32_t src_width;
94 DataType src_data_type;
95 ConvertPolicy policy;
96 std::string expected_code;
97 };
98
99 std::vector<TestInfo> _tests{};
100};
101
102} // namespace ckw
103
104#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERCASTTEST_H