aboutsummaryrefslogtreecommitdiff
path: root/src/core/experimental/dynamic_fusion/ClKernelBuildingAPI.h
blob: 15622c848de7ac7b5c87f9b878e5b55952a8e270 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 2022 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.
 */
#if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)

#ifndef ARM_COMPUTE_EXPERIMENTAL_CLKERNELBUILDINGAPI_H
#define ARM_COMPUTE_EXPERIMENTAL_CLKERNELBUILDINGAPI_H

#include "arm_compute/core/CL/CLCompileContext.h"
#include "arm_compute/core/Window.h"

namespace arm_compute
{
namespace experimental
{
namespace dynamic_fusion
{
using ArgumentID = int32_t;

static constexpr ArgumentID g_arg_placeholder = -1;

/** Verbose and explicit way to enumerate all the tensor arguments variants used by
 *  all kernel implementations. This avoids any ambiguity in what kernel arguments are passed
 */
enum class TensorArgType : int
{
    Scalar,

    Vector,

    Image,
    Image_Reinterpret_As_3D,
    Image_Export_To_ClImage2D,

    Image_3D, // 3D Tensor represented as a 2D Image + stride_z
    Image_3D_Export_To_ClImage2D,

    Tensor_3D,
    Tensor_4D
};
/** Describes all the info required to add a kernel argument at run time */
struct ClKernelArgRuntimeDescriptor
{
    ClKernelArgRuntimeDescriptor(int arg_id, TensorArgType type, bool slide_along_dimz = true)
        : arg_id{ arg_id }, tensor_arg_type{ type }, slide_along_dimz{ slide_along_dimz }
    {
    }
    ~ClKernelArgRuntimeDescriptor() = default;
    int           arg_id{ g_arg_placeholder }; // Arg ID in the blueprint
    TensorArgType tensor_arg_type{ TensorArgType::Image };
    bool          slide_along_dimz{ true };
};

using ClKernelArgList = std::vector<ClKernelArgRuntimeDescriptor>;

/** Intermediate representation of the final, complete kernel source. */
class ClKernelBlueprint
{
public:
    ClKernelBlueprint();
    ~ClKernelBlueprint();

private:
    struct Implementation;
    std::unique_ptr<Implementation> _impl;

public:
    Implementation       &impl();
    const Implementation &impl() const;
};

///// Kernel Components /////

/** Meta information about all Cl Kernel Components */
struct ClKernelComponentDescriptor
{
    int32_t version{ 1 }; /**< Operator version */
};

/** Component: Tensor Argument */
struct ClTensorDescriptor
{
    ClTensorDescriptor(const ITensorInfo *info, unsigned int dim)
        : tensor_info(info), slice_dim(dim)
    {
    }

    const ITensorInfo *tensor_info;
    unsigned int       slice_dim;
};

Status add_tensor_argument(ClKernelBlueprint &, const ClTensorDescriptor &, ArgumentID &);
Status add_tensor_intermed(ClKernelBlueprint &, ArgumentID &);

/** Component: Gemm Native */
struct GemmNativeDescriptor
{
    float             alpha{};
    float             beta{};
    unsigned int      m{};
    unsigned int      n{};
    unsigned int      k{};
    unsigned int      depth_output_gemm3d{};
    bool              reinterpret_input_as_3d{};
    bool              broadcast_bias{};
    bool              fp_mixed_precision{};
    bool              has_pad_y{};
    int               nmult_transpose1xW_width{};
    int               mult_interleave4x4_height{};
    GEMMLHSMatrixInfo lhs_info{};
    GEMMRHSMatrixInfo rhs_info{};
    int32_t           a_offset{};
    int32_t           b_offset{};
};

Status add_kcomp_gemm_native(ClKernelBlueprint &, const ClKernelComponentDescriptor &, const GemmNativeDescriptor &, ArgumentID input_id,
                             ArgumentID weights_id, ArgumentID bias_id, ArgumentID &dst_id);

/** Component: Eltwise Add */
struct EltwiseAddDescriptor
{
    ConvertPolicy convert_policy{ ConvertPolicy::SATURATE };
};
Status add_kcomp_eltwise_add(ClKernelBlueprint &, const ClKernelComponentDescriptor &, const EltwiseAddDescriptor &, ArgumentID src0_id,
                             ArgumentID src1_id, ArgumentID &dst_id);

/** Component: Activation */
struct ActivationDescriptor
{
};
Status add_kcomp_activation(ClKernelBlueprint &, const ClKernelComponentDescriptor &, const ActivationDescriptor &, ArgumentID src_id, ArgumentID &dst_id);

enum class ClippingStrategy
{
    TOP_LEFT,
    TOP_RIGHT,
    BOTTOM_LEFT,
    BOTTOM_RIGHT,
};

/** Component: Store */
struct TileDescriptor
{
    Size2D           tile_dims{};
    Size2D           boundaries{};
    ClippingStrategy clipping{ ClippingStrategy::TOP_LEFT };

    TileDescriptor()
    {
    }

    TileDescriptor(Size2D dims, const Size2D &bound, const ClippingStrategy &clip)
        : tile_dims(dims), boundaries(bound), clipping(clip)
    {
    }

    bool empty() const
    {
        return (tile_dims.area() == 0) || (boundaries.area() == 0);
    }
};

enum class StoreType
{
    VStore,
    VStorePartial,
    StoreRow,
    ConvertStoreRow,
    StoreBlock,
    ConvertStoreBlock,
    StoreRowPartial,
    StoreBlockPartial,
    StoreBlockBoundaryAware,
    StoreVectorSelect,
    TStoreIndirectWidthSelect
};

Status add_kcomp_store(ClKernelBlueprint &, const ClKernelComponentDescriptor &, ArgumentID src_id, ArgumentID dst_id, const StoreType &store_type);

///// Kernel Components /////

///// Building /////

/** Information required for kernel compilation. The build results of KernelBlueprint */
struct ClKernelCode
{
    std::string     name{};          /**< Kernel name */
    std::string     code{};          /**< Kernel source code */
    std::string     config_id{};     /**< Generated from blueprint based on complex component */
    CLBuildOptions  build_options{}; /**< Kernel build options */
    Window          window{};        /**< Execution window */
    ClKernelArgList arguments{};     /**< Kernel argument specficiations */

    bool operator==(const ClKernelCode &other) const
    {
        return name == other.name && code == other.code && build_options == other.build_options;
    }
};

/** GPU information for building the @ref ClKernelCode */
struct GpuInfo
{
    GPUTarget target{ GPUTarget::UNKNOWN };
};

/** All information required for building the @ref ClKernelCode */
struct ClCodeBuilderContext
{
    GpuInfo gpu_info{};
};

Status set_tile_info(ClKernelBlueprint &, const TileDescriptor &);

/** Build final kernel source from KernelBlueprint */
Status build(ClKernelCode &code, const ClCodeBuilderContext &, ClKernelBlueprint &);

///// Building /////

///// Tuning /////
struct ClExecutionDescriptor
{
    cl::NDRange suggested_lws{}; /**< Suggested local work-group size for optimal performance if not zero */
    cl::NDRange gws{};           /**< Global work-group to be used */
};

Status tune_static(ClExecutionDescriptor &, const ClKernelCode &);

///// Tuning /////

} // namespace dynamic_fusion
} // namespace experimental
} // namespace arm_compute
#endif //ARM_COMPUTE_EXPERIMENTAL_CLKERNELBUILDINGAPI_H

#endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)