aboutsummaryrefslogtreecommitdiff
path: root/compute_kernel_writer/src/cl/helpers/CLMemoryOpImage2dHelper.cpp
blob: f392cd89cc9e1f8123dfe153d5e032808750760f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2023 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "src/cl/helpers/CLMemoryOpImage2dHelper.h"

#include "ckw/Error.h"
#include "ckw/TensorSampler.h"
#include "ckw/types/MemoryOperation.h"
#include "ckw/types/TensorStorageType.h"

#include "src/cl/CLKernelWriter.h"
#include "src/cl/CLTensorArgument.h"
#include "src/cl/CLTile.h"
#include "src/ITensor.h"
#include "src/Tensor3dMapper.h"
#include "src/TileView.h"

namespace ckw
{
void CLMemoryOpImage2dHelper::initialize(const CLTile *x, const CLTile *z, const CLTile *b)
{
    _coord_x = x->scalar(0, 0).str;
    _coord_z = z->scalar(0, 0).str;
    _coord_b = b->scalar(0, 0).str;
}

void CLMemoryOpImage2dHelper::write_row(int32_t row_id, const std::string &coord_y)
{
    // The only check required is on Y.
    out_of_bound_initialize_y(coord_y);

    const std::string dst     = _dst.vector(row_id).str;
    const std::string sampler = to_ls_image2d_sampler();
    const std::string coord   = to_ls_image2d_address(_coord_x, coord_y, _coord_z, _coord_b);
    const std::string ls_buf  = to_ls_image2d(_op, _ls_width_full, dst, sampler, coord);

    _writer->op_write_raw_code(ls_buf + ";\n");

    out_of_bound_finalize_y();
}

void CLMemoryOpImage2dHelper::finalize()
{
}

bool CLMemoryOpImage2dHelper::validate(const CLKernelWriter   *writer,
                                       const ITensor          *tensor,
                                       const TensorSampler    *sampler,
                                       const Tensor3dMapper   *mapper,
                                       MemoryOperation         op,
                                       const TileView<CLTile> &dst)
{
    CKW_UNUSED(writer, tensor, mapper);

    if (dst.width() != 4)
    {
        return false;
    }
    if (sampler->address_mode_x() != TensorSamplerAddressModeX::None)
    {
        return false;
    }
    if (sampler->address_mode_z() != TensorSamplerAddressModeZ::None)
    {
        return false;
    }
    if (sampler->storage() != TensorStorageType::Texture2dReadOnly && op == MemoryOperation::Load)
    {
        return false;
    }
    if (sampler->storage() != TensorStorageType::Texture2dWriteOnly && op == MemoryOperation::Store)
    {
        return false;
    }
    if ((dst.data_type() != DataType::Fp32) && (dst.data_type() != DataType::Fp16))
    {
        return false;
    }
    return true;
}

void CLMemoryOpImage2dHelper::out_of_bound_initialize_y(const std::string &coord)
{
    CKW_UNUSED(coord);

    const TensorSamplerAddressModeY address_mode_y = _sampler->address_mode_y();
    switch (address_mode_y)
    {
        case TensorSamplerAddressModeY::SkipLessThanZero:
            _writer->op_write_raw_code("if(" + coord + " >= 0)\n{\n");
            break;
        case TensorSamplerAddressModeY::ClampToBorderMaxOnly:
        case TensorSamplerAddressModeY::None:
            break;
        default:
            CKW_THROW_MSG("Unsupported address mode for Y dimension");
    }
}

void CLMemoryOpImage2dHelper::out_of_bound_finalize_y()
{
    const TensorSamplerAddressModeY address_mode_y = _sampler->address_mode_y();
    switch (address_mode_y)
    {
        case TensorSamplerAddressModeY::SkipLessThanZero:
            _writer->op_write_raw_code("}\n");
            break;
        case TensorSamplerAddressModeY::ClampToBorderMaxOnly:
        case TensorSamplerAddressModeY::None:
            break;
        default:
            CKW_THROW_MSG("Unsupported address mode for Y dimension");
    }
}

std::string CLMemoryOpImage2dHelper::to_ls_image2d(MemoryOperation    op,
                                                   int32_t            vector_width,
                                                   const std::string &data,
                                                   const std::string &sampler,
                                                   const std::string &address) const
{
    CKW_UNUSED(vector_width);
    CKW_ASSERT_MSG(_dst.data_type() == DataType::Fp32 || _dst.data_type() == DataType::Fp16,
                   "Image2d only supports floating-point data type");

    const TensorStorageType tensor_storage = _sampler->storage();
    const std::string       image2d_obj    = _tensor->storage(tensor_storage).val;
    const std::string       post_fix       = _dst.data_type() == DataType::Fp32 ? "f" : "h";

    switch (op)
    {
        case MemoryOperation::Load:
            return data + " = read_image" + post_fix + "(" + image2d_obj + ", " + sampler + ", " + address + ")";
            break;
        case MemoryOperation::Store:
            return "write_image" + post_fix + "(" + image2d_obj + ", " + address + ", " + data + ")";
        default:
            CKW_THROW_MSG("Unsupported MemoryOperation");
    }
}

std::string CLMemoryOpImage2dHelper::to_ls_image2d_sampler() const
{
    const auto address_mode_y = _sampler->address_mode_y();

    switch (address_mode_y)
    {
        case TensorSamplerAddressModeY::None:
            return "CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST";
        case TensorSamplerAddressModeY::SkipLessThanZero:
        case TensorSamplerAddressModeY::ClampToBorderMaxOnly:
            return "CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST";
        default:
            CKW_THROW_MSG("Unsupported address_mode_coord");
    }
}

std::string CLMemoryOpImage2dHelper::to_ls_image2d_address(const std::string &x,
                                                           const std::string &y,
                                                           const std::string &z,
                                                           const std::string &b) const
{
    std::string coord_x = "(" + x + ") >> 2";
    std::string coord_y = "(";

    if (y != "0")
    {
        coord_y += y;
    }
    if (z != "0" && (_mapper->dim_z().str != "1"))
    {
        const std::string dim = _mapper->dim_y().str;
        coord_y += " + (";
        coord_y += z + ")";
        coord_y += " * ";
        coord_y += dim;
    }
    if (b != "0" && (_mapper->dim_batch().str != "1"))
    {
        const std::string dim0 = _mapper->dim_y().str;
        const std::string dim1 = _mapper->dim_z().str;
        coord_y += " + (";
        coord_y += b + ")";
        coord_y += " * ";
        coord_y += dim0;
        coord_y += " * ";
        coord_y += dim1;
    }
    coord_y += ")";
    return "(int2)(" + coord_x + ", " + coord_y + ")";
}

} // namespace ckw