blob: 9529268c9afb1577decdaa580422ba1cde81b761 [file] [log] [blame]
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +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/Error.h"
26#include "ckw/KernelWriter.h"
27#include "ckw/TensorOperand.h"
28#include "ckw/TensorTileSampler.h"
29#include "ckw/TileOperand.h"
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010030
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010031#include "common/ExampleComponentArgument.h"
32#include "common/ExampleKernelWriter.h"
33#include "common/ExampleScopedKernelWriter.h"
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010034
35#include <iostream>
36#include <vector>
37
38using namespace ckw;
39
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010040TensorTileSampler create_simple_sampler(ExampleScopedKernelWriter writer)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010041{
42 TensorTileSampler sampler;
43
44 constexpr int32_t m0 = 4;
45 constexpr int32_t n0 = 4;
46
47 auto &gid_0 = writer->declare_tile("gid_0", DataType::Int32);
48 auto &gid_1 = writer->declare_tile("gid_1", DataType::Int32);
49 auto &gid_2 = writer->declare_tile("gid_2", DataType::Int32);
50
51 auto &const_0 = writer->declare_tile("0", 0);
52
53 writer->op_get_global_id(gid_0, 0);
54 writer->op_get_global_id(gid_1, 1);
55 writer->op_get_global_id(gid_2, 2);
56
57 sampler.x(gid_0);
58 sampler.y(gid_1);
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010059 sampler.z(const_0);
60 sampler.b(gid_2);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010061
62 sampler.width(n0);
63 sampler.height(m0);
64
65 sampler.format(TensorSamplerFormat::C_WH_1);
66 sampler.address_mode_x(TensorSamplerAddressModeX::None);
67 sampler.address_mode_y(TensorSamplerAddressModeY::ClampToBorder);
68 sampler.address_mode_z(TensorSamplerAddressModeZ::Skip);
69
70 return sampler;
71}
72
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +010073void op_binary_elementwise(ExampleScopedKernelWriter writer, std::vector<ExampleComponentArgument *> operands)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +010074{
75 auto lhs = operands.at(0);
76 auto rhs = operands.at(1);
77 auto dst = operands.at(2);
78
79 // Load the LHS and RHS tile and prepare the tensor sampler.
80 if(!lhs->has_tile() && !rhs->has_tile())
81 {
82 const auto sampler = create_simple_sampler(writer);
83
84 writer->op_load_once(lhs, sampler);
85 writer->op_load_once(rhs, sampler);
86 }
87 else if(lhs->has_tile())
88 {
89 const auto &sampler = lhs->tile_sampler();
90 writer->op_load_once(rhs, sampler);
91 }
92 else
93 {
94 const auto &sampler = rhs->tile_sampler();
95 writer->op_load_once(lhs, sampler);
96 }
97
98 auto &lhs_tile = lhs->tile();
99 auto &rhs_tile = rhs->tile();
100 const auto &sampler = lhs->tile_sampler();
101
102 // Prepare the output tile.
103 if(!dst->has_tile())
104 {
105 auto &tile = writer->declare_tile("dst_tile", lhs_tile.tile_info());
106 dst->init_virtual_tensor(tile, sampler);
107 }
108
109 auto &dst_tile = dst->tile();
110
111 // Perform the operation.
Nikolaj Jensen5ff48022023-06-27 14:13:24 +0100112 writer->op_binary_expression(dst_tile, lhs_tile, BinaryOp::Add, rhs_tile);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100113}
114
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100115void op_exp(ExampleScopedKernelWriter writer, std::vector<ExampleComponentArgument *> operands)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100116{
117 auto src = operands.at(0);
118 auto dst = operands.at(1);
119
120 // Load the source tile and prepare the sampler.
121 if(!src->has_tile())
122 {
123 const auto sampler = create_simple_sampler(writer);
124 writer->op_load_once(src, sampler);
125 }
126
127 auto &src_tile = src->tile();
128 const auto &sampler = src->tile_sampler();
129
130 // Prepare the output tile.
131 if(!dst->has_tile())
132 {
133 auto &tile = writer->declare_tile("dst_tile", src_tile.tile_info());
134 dst->init_virtual_tensor(tile, sampler);
135 }
136
137 auto &dst_tile = dst->tile();
138
139 // Perform the operation.
Nikolaj Jensen5ff48022023-06-27 14:13:24 +0100140 writer->op_unary_elementwise_function(dst_tile, UnaryFunction::Exp, src_tile);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100141}
142
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100143void op_store(ExampleScopedKernelWriter writer, std::vector<ExampleComponentArgument *> operands)
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100144{
145 auto src = operands.at(0);
146 auto dst = operands.at(1);
147
148 auto &src_tile = src->tile();
149 const auto &sampler = src->tile_sampler();
150 auto &dst_tensor = dst->tensor();
151
152 writer->op_store(dst_tensor, src_tile, sampler);
153}
154
155int main()
156{
Nikolaj Jensenacea4072023-07-03 09:44:42 +0100157 Kernel kernel("example", GpuTargetLanguage::OpenCL);
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100158 ExampleKernelWriter root_writer(kernel);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100159
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100160 ExampleScopedKernelWriter writer(&root_writer);
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100161
162 const TensorInfo src0_info(DataType::Fp32, TensorShape({ 3, 10, 20, 1, 1 }), TensorDataLayout::Nhwc, 0);
163 const TensorInfo src1_info(DataType::Fp32, TensorShape({ 3, 10, 20, 1, 1 }), TensorDataLayout::Nhwc, 1);
164 const TensorInfo dst_info(DataType::Fp32, TensorShape({ 3, 10, 20, 1, 1 }), TensorDataLayout::Nhwc, 2);
165
Nikolaj Jensen5ff48022023-06-27 14:13:24 +0100166 ExampleComponentArgument src0(writer->declare_tensor_argument("src0", src0_info));
167 ExampleComponentArgument src1(writer->declare_tensor_argument("src1", src1_info));
168 ExampleComponentArgument dst(writer->declare_tensor_argument("dst", dst_info));
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100169
Viet-Hoa Doce3c48c2023-07-03 13:44:43 +0100170 ExampleComponentArgument ans;
Viet-Hoa Dobd4f6b92023-05-30 09:34:32 +0100171
172 op_binary_elementwise(writer, { &src0, &src1, &ans });
173 op_exp(writer, { &ans, &ans });
174 op_store(writer, { &ans, &dst });
175
176 const auto code = root_writer.generate_code();
177 std::cout << code;
178
179 return 0;
180}