Nikolaj Jensen | fab6c21 | 2023-06-27 14:13:24 +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 | #include "ckw/KernelWriter.h" |
| 26 | #include "../include/ckw/KernelWriterHelper.h" |
| 27 | #include "ckw/TensorTileSampler.h" |
| 28 | |
| 29 | #include <iostream> |
| 30 | |
| 31 | using namespace ckw; |
| 32 | |
| 33 | TensorTileSampler create_simple_sampler(KernelWriter& writer) |
| 34 | { |
| 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 | |
| 66 | int main() |
| 67 | { |
| 68 | Kernel kernel("test", GpuTargetLanguage::OpenCL); |
| 69 | KernelWriterHelper<KernelWriter> writer(kernel); |
| 70 | |
| 71 | 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); |
| 73 | |
| 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())); |
| 80 | auto &other = writer.declare_tile("other_tile", TileInfo(src_tensor.data_type(), sampler.height(), sampler.width())); |
| 81 | auto &dst = writer.declare_tile("dst_tile", TileInfo(src_tensor.data_type(), sampler.height(), sampler.width())); |
| 82 | |
| 83 | writer.op_load(src, src_tensor, sampler); |
| 84 | writer.op_load(other, src_tensor, sampler); |
| 85 | writer.op_load(dst, dst_tensor, sampler); |
| 86 | |
| 87 | auto test = dst ^ src ^ other; |
| 88 | auto other_test = logical_and(dst, src, other); |
| 89 | writer.op_assign(dst, logical_and(dst, src, other)); |
| 90 | writer.op_assign(dst, test); |
| 91 | writer.op_assign(dst, other_test); |
| 92 | writer.op_assign(dst, operator^(operator^(dst, src), other)); |
| 93 | |
| 94 | writer.op_if(exp(src) == dst, [&]{ |
| 95 | writer.op_binary_expression(dst, src, BinaryOp::Add, src); |
| 96 | }).op_else_if(exp(src) > dst, [&]{ |
| 97 | writer.op_binary_expression(dst, src, BinaryOp::Add, src); |
| 98 | }).op_else([&] { |
| 99 | writer.op_assign(dst, src); |
| 100 | }); |
| 101 | |
| 102 | writer.op_assign(dst, src + src * src); |
| 103 | writer.op_assign(dst, src * max(src, dst) + src); |
| 104 | writer.op_assign(dst, src * select(src, dst, src) + src); |
| 105 | |
| 106 | writer.op_assign(dst, src ^ dst); |
| 107 | writer.op_assign(dst, ~src); |
| 108 | |
| 109 | writer.op_for_loop(dst < src, dst += src, [&]{ |
| 110 | writer.op_assign(dst, src + dst); |
| 111 | }); |
| 112 | |
| 113 | writer.op_assign(dst += src); |
| 114 | writer.op_assign(dst += exp(src)); |
| 115 | |
| 116 | std::cout << "======== KERNEL ========" << std::endl; |
| 117 | std::cout << writer.generate_code() << std::endl; |
| 118 | } |