blob: b7d146bdee005096a45ecb744fbd029967bdfe08 [file] [log] [blame]
Gunes Bayir038fe912023-08-11 12:50:31 +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#include "src/cl/helpers/CLMemoryOpImage2dHelper.h"
25
26#include "ckw/Error.h"
27#include "ckw/TensorSampler.h"
28#include "ckw/types/MemoryOperation.h"
29#include "ckw/types/TensorStorageType.h"
30
Gunes Bayir038fe912023-08-11 12:50:31 +010031#include "src/cl/CLKernelWriter.h"
32#include "src/cl/CLTensorArgument.h"
33#include "src/cl/CLTile.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034#include "src/ITensor.h"
35#include "src/Tensor3dMapper.h"
Gunes Bayir038fe912023-08-11 12:50:31 +010036
37namespace ckw
38{
39void CLMemoryOpImage2dHelper::initialize(const CLTile *dst, const CLTile *x, const CLTile *z, const CLTile *b)
40{
41 CKW_ASSERT(validate(_writer, _tensor, _sampler, _mapper.get(), _op, dst));
42
43 _dst = dst;
44 _ls_width_full = dst->info().width();
45 _coord_x = x->scalar(0, 0).str;
46 _coord_z = z->scalar(0, 0).str;
47 _coord_b = b->scalar(0, 0).str;
48}
49
50void CLMemoryOpImage2dHelper::write_row(int32_t row_id, const std::string &coord_y)
51{
52 // The only check required is on Y.
53 out_of_bound_initialize_y(coord_y);
54
55 const std::string dst = _dst->vector(row_id).str;
56 const std::string sampler = to_ls_image2d_sampler();
57 const std::string coord = to_ls_image2d_address(_coord_x, coord_y, _coord_z, _coord_b);
58 const std::string ls_buf = to_ls_image2d(_op, _ls_width_full, dst, sampler, coord);
59
60 _writer->op_write_raw_code(ls_buf + ";\n");
61
62 out_of_bound_finalize_y();
63}
64
65void CLMemoryOpImage2dHelper::finalize()
66{
67}
68
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069bool CLMemoryOpImage2dHelper::validate(const CLKernelWriter *writer,
70 const ITensor *tensor,
71 const TensorSampler *sampler,
72 const Tensor3dMapper *mapper,
73 MemoryOperation op,
74 const CLTile *dst)
Gunes Bayir038fe912023-08-11 12:50:31 +010075{
76 CKW_UNUSED(writer, tensor, mapper);
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (dst->info().width() != 4)
Gunes Bayir038fe912023-08-11 12:50:31 +010079 {
80 return false;
81 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 if (sampler->address_mode_x() != TensorSamplerAddressModeX::None)
Gunes Bayir038fe912023-08-11 12:50:31 +010083 {
84 return false;
85 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 if (sampler->address_mode_z() != TensorSamplerAddressModeZ::None)
Gunes Bayir038fe912023-08-11 12:50:31 +010087 {
88 return false;
89 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 if (sampler->storage() != TensorStorageType::Texture2dReadOnly && op == MemoryOperation::Load)
Gunes Bayir038fe912023-08-11 12:50:31 +010091 {
92 return false;
93 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (sampler->storage() != TensorStorageType::Texture2dWriteOnly && op == MemoryOperation::Store)
Gunes Bayir038fe912023-08-11 12:50:31 +010095 {
96 return false;
97 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 if ((dst->info().data_type() != DataType::Fp32) && (dst->info().data_type() != DataType::Fp16))
Gunes Bayir038fe912023-08-11 12:50:31 +010099 {
100 return false;
101 }
102 return true;
103}
104
105void CLMemoryOpImage2dHelper::out_of_bound_initialize_y(const std::string &coord)
106{
107 CKW_UNUSED(coord);
108
109 const TensorSamplerAddressModeY address_mode_y = _sampler->address_mode_y();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 switch (address_mode_y)
Gunes Bayir038fe912023-08-11 12:50:31 +0100111 {
Gunes Bayird5f9a1c2023-08-17 11:04:02 +0100112 case TensorSamplerAddressModeY::SkipLessThanZero:
113 _writer->op_write_raw_code("if(" + coord + " >= 0)\n{\n");
114 break;
Gunes Bayir038fe912023-08-11 12:50:31 +0100115 case TensorSamplerAddressModeY::ClampToBorderMaxOnly:
116 case TensorSamplerAddressModeY::None:
117 break;
118 default:
119 CKW_THROW_MSG("Unsupported address mode for Y dimension");
120 }
121}
122
123void CLMemoryOpImage2dHelper::out_of_bound_finalize_y()
124{
125 const TensorSamplerAddressModeY address_mode_y = _sampler->address_mode_y();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 switch (address_mode_y)
Gunes Bayir038fe912023-08-11 12:50:31 +0100127 {
Gunes Bayird5f9a1c2023-08-17 11:04:02 +0100128 case TensorSamplerAddressModeY::SkipLessThanZero:
129 _writer->op_write_raw_code("}\n");
130 break;
Gunes Bayir038fe912023-08-11 12:50:31 +0100131 case TensorSamplerAddressModeY::ClampToBorderMaxOnly:
132 case TensorSamplerAddressModeY::None:
133 break;
134 default:
135 CKW_THROW_MSG("Unsupported address mode for Y dimension");
136 }
137}
138
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100139std::string CLMemoryOpImage2dHelper::to_ls_image2d(MemoryOperation op,
140 int32_t vector_width,
141 const std::string &data,
142 const std::string &sampler,
143 const std::string &address) const
Gunes Bayir038fe912023-08-11 12:50:31 +0100144{
145 CKW_UNUSED(vector_width);
146
147 const TensorStorageType tensor_storage = _sampler->storage();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 const std::string image2d_obj = _tensor->storage(tensor_storage).val;
149 const std::string post_fix = _dst->info().data_type() == DataType::Fp32 ? "f" : "h";
Gunes Bayir038fe912023-08-11 12:50:31 +0100150
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 switch (op)
Gunes Bayir038fe912023-08-11 12:50:31 +0100152 {
153 case MemoryOperation::Load:
154 return data + " = read_image" + post_fix + "(" + image2d_obj + ", " + sampler + ", " + address + ")";
155 break;
156 case MemoryOperation::Store:
157 return "write_image" + post_fix + "(" + image2d_obj + ", " + address + ", " + data + ")";
158 default:
159 CKW_THROW_MSG("Unsupported MemoryOperation");
160 }
161}
162
163std::string CLMemoryOpImage2dHelper::to_ls_image2d_sampler() const
164{
165 const auto address_mode_y = _sampler->address_mode_y();
166
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 switch (address_mode_y)
Gunes Bayir038fe912023-08-11 12:50:31 +0100168 {
169 case TensorSamplerAddressModeY::None:
170 return "CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST";
Gunes Bayird5f9a1c2023-08-17 11:04:02 +0100171 case TensorSamplerAddressModeY::SkipLessThanZero:
Gunes Bayir038fe912023-08-11 12:50:31 +0100172 case TensorSamplerAddressModeY::ClampToBorderMaxOnly:
173 return "CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST";
174 default:
175 CKW_THROW_MSG("Unsupported address_mode_coord");
176 }
177}
178
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179std::string CLMemoryOpImage2dHelper::to_ls_image2d_address(const std::string &x,
180 const std::string &y,
181 const std::string &z,
Gunes Bayir038fe912023-08-11 12:50:31 +0100182 const std::string &b) const
183{
184 std::string coord_x = "(" + x + ") >> 2";
185 std::string coord_y = "(";
186
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 if (y != "0")
Gunes Bayir038fe912023-08-11 12:50:31 +0100188 {
189 coord_y += y;
190 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 if (z != "0" && (_mapper->dim_z().str != "1"))
Gunes Bayir038fe912023-08-11 12:50:31 +0100192 {
193 const std::string dim = _mapper->dim_y().str;
194 coord_y += " + (";
195 coord_y += z + ")";
196 coord_y += " * ";
197 coord_y += dim;
198 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100199 if (b != "0" && (_mapper->dim_batch().str != "1"))
Gunes Bayir038fe912023-08-11 12:50:31 +0100200 {
201 const std::string dim0 = _mapper->dim_y().str;
202 const std::string dim1 = _mapper->dim_z().str;
203 coord_y += " + (";
204 coord_y += b + ")";
205 coord_y += " * ";
206 coord_y += dim0;
207 coord_y += " * ";
208 coord_y += dim1;
209 }
210 coord_y += ")";
211 return "(int2)(" + coord_x + ", " + coord_y + ")";
212}
213
214} // namespace ckw