blob: 8623afbf50a8ea813da87725360e9fdc11fdf81a [file] [log] [blame]
Nikolaj Jensenfab6c212023-06-27 14:13:24 +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#include "ckw/KernelWriter.h"
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010026#include "ckw/TensorTileSampler.h"
27
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "../include/ckw/KernelWriterHelper.h"
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010029#include <iostream>
30
31using namespace ckw;
32
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033TensorTileSampler create_simple_sampler(KernelWriter &writer)
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010034{
35 TensorTileSampler sampler;
36
37 constexpr int32_t m0 = 1;
38 constexpr int32_t n0 = 1;
39
40 auto &gid_0 = writer.declare_tile("gid_0", DataType::Int32);
41 auto &gid_1 = writer.declare_tile("gid_1", DataType::Int32);
42 auto &gid_2 = writer.declare_tile("gid_2", DataType::Int32);
43
44 auto &const_0 = writer.declare_tile("0", 0);
45
46 writer.op_get_global_id(gid_0, 0);
47 writer.op_get_global_id(gid_1, 1);
48 writer.op_get_global_id(gid_2, 2);
49
50 sampler.x(gid_0);
51 sampler.y(gid_1);
52 sampler.z(gid_2);
53 sampler.b(const_0);
54
55 sampler.width(n0);
56 sampler.height(m0);
57
58 sampler.format(TensorSamplerFormat::C_WH_1);
59 sampler.address_mode_x(TensorSamplerAddressModeX::None);
60 sampler.address_mode_y(TensorSamplerAddressModeY::ClampToBorder);
61 sampler.address_mode_z(TensorSamplerAddressModeZ::Skip);
62
63 return sampler;
64}
65
66int main()
67{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 Kernel kernel("test", GpuTargetLanguage::OpenCL);
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010069 KernelWriterHelper<KernelWriter> writer(kernel);
70
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 const TensorInfo src_info(DataType::Fp32, TensorShape({1, 1, 1, 1, 1}), TensorDataLayout::Nhwc, 0);
72 const TensorInfo dst_info(DataType::Fp32, TensorShape({1, 1, 1, 1, 1}), TensorDataLayout::Nhwc, 1);
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010073
74 auto &src_tensor = writer.declare_tensor_argument("src", src_info);
75 auto &dst_tensor = writer.declare_tensor_argument("dst", dst_info);
76
77 const auto sampler = create_simple_sampler(writer);
78
79 auto &src = writer.declare_tile("src_tile", TileInfo(src_tensor.data_type(), sampler.height(), sampler.width()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 auto &other =
81 writer.declare_tile("other_tile", TileInfo(src_tensor.data_type(), sampler.height(), sampler.width()));
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010082 auto &dst = writer.declare_tile("dst_tile", TileInfo(src_tensor.data_type(), sampler.height(), sampler.width()));
83
84 writer.op_load(src, src_tensor, sampler);
85 writer.op_load(other, src_tensor, sampler);
86 writer.op_load(dst, dst_tensor, sampler);
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 auto test = dst ^ src ^ other;
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010089 auto other_test = logical_and(dst, src, other);
90 writer.op_assign(dst, logical_and(dst, src, other));
91 writer.op_assign(dst, test);
92 writer.op_assign(dst, other_test);
93 writer.op_assign(dst, operator^(operator^(dst, src), other));
94
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 writer.op_if(exp(src) == dst, [&] { writer.op_binary_expression(dst, src, BinaryOp::Add, src); })
96 .op_else_if(exp(src) > dst, [&] { writer.op_binary_expression(dst, src, BinaryOp::Add, src); })
97 .op_else([&] { writer.op_assign(dst, src); });
Nikolaj Jensenfab6c212023-06-27 14:13:24 +010098
99 writer.op_assign(dst, src + src * src);
100 writer.op_assign(dst, src * max(src, dst) + src);
101 writer.op_assign(dst, src * select(src, dst, src) + src);
102
103 writer.op_assign(dst, src ^ dst);
104 writer.op_assign(dst, ~src);
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 writer.op_for_loop(dst < src, dst += src, [&] { writer.op_assign(dst, src + dst); });
Nikolaj Jensenfab6c212023-06-27 14:13:24 +0100107
108 writer.op_assign(dst += src);
109 writer.op_assign(dst += exp(src));
110
111 std::cout << "======== KERNEL ========" << std::endl;
112 std::cout << writer.generate_code() << std::endl;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113}