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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* Copyright (c) 2019-2021, 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.
*/
#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
#error "This example needs to be built with -DARM_COMPUTE_CL"
#endif /* ARM_COMPUTE_CL */
#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/KernelDescriptors.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/utils/misc/ShapeCalculator.h"
#include "arm_compute/runtime/CL/CLScheduler.h"
#include "arm_compute/runtime/CL/CLTuner.h"
#include "examples/gemm_tuner/CommonGemmExampleOptions.h"
#include "examples/gemm_tuner/GemmTunerHelpers.h"
#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedKernel.h"
#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
#include "tests/CL/Helper.h"
#include "utils/command_line/CommandLineOptions.h"
#include "utils/command_line/CommandLineParser.h"
#include "utils/Utils.h"
#include <cstdlib>
using namespace arm_compute;
using namespace arm_compute::opencl::kernels;
using namespace utils;
using namespace arm_compute::misc::shape_calculator;
using namespace gemm_tuner;
namespace
{
/** Structure holding all tunable gemm configs specific to this example/strategy */
struct GemmConfigs
{
size_t m0{4}; /**< Number of rows processed by the matrix multiplication */
size_t n0{4}; /**< Number of columns processed by the matrix multiplication */
size_t k0{4}; /**< Number of partial accumulations performed by the matrix multiplication */
size_t v0{1}; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
size_t h0{1}; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
bool interleave_lhs{true}; /**< Interleave lhs matrix */
bool transpose_lhs{true}; /**< Transpose lhs matrix. */
bool interleave_rhs{true}; /**< Interleave rhs matrix */
bool transpose_rhs{true}; /**< Transpose rhs matrix. */
bool export_to_cl_image_rhs{true}; /**< Export rhs matrix to cl_image. */
};
/** Formatted output of the GemmConfigs type
*
* @param[out] os Output stream.
* @param[in] configs Tunable configurations to output
*
* @return Modified output stream.
*/
::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
{
std::string false_str = std::string("false");
std::string true_str = std::string("true");
os << "m0 : " << configs.m0 << std::endl;
os << "n0 : " << configs.n0 << std::endl;
os << "k0 : " << configs.k0 << std::endl;
os << "v0 : " << configs.v0 << std::endl;
os << "h0 : " << configs.h0 << std::endl;
os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl;
os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl;
os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
os << "export_to_cl_image_rhs : " << (configs.export_to_cl_image_rhs ? true_str : false_str) << std::endl;
return os;
}
/** Command line options for gemm configs */
class GemmConfigOptions
{
public:
/** Constructor
*
* @param[in,out] parser A parser on which "parse()" hasn't been called yet.
*/
GemmConfigOptions(CommandLineParser &parser)
: m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)),
h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)),
interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)),
export_to_cl_image_rhs(parser.add_positional_option<SimpleOption<size_t>>("export_to_cl_image_rhs", 1))
{
m0->set_help("Number of rows processed by the matrix multiplication");
n0->set_help("Number of columns processed by the matrix multiplication");
k0->set_help("Number of partial accumulations performed by the matrix multiplication");
v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row");
h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)");
interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
// FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
// transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
// 2 variants (both transposed and none transposed)
transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do "
"transpose lhs matrix (0)");
export_to_cl_image_rhs->set_help(
"Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)");
}
/** Prevent instances of this class from being copied (As this class contains pointers) */
GemmConfigOptions(const GemmConfigOptions &) = delete;
/** Prevent instances of this class from being copied (As this class contains pointers) */
GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
/** Allow instances of this class to be moved */
GemmConfigOptions(GemmConfigOptions &&) = default;
/** Allow instances of this class to be moved */
GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
/** Default destructor */
~GemmConfigOptions() = default;
SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */
SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */
SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
// FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
// transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
// 2 variants (both transposed and none transposed)
SimpleOption<size_t> *
transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */
SimpleOption<size_t> *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/
};
/** Consumes the gemm configuration options and creates a structure containing all information
*
* @param[in] options Options to consume
*
* @return Structure containing the gemm configurations
*/
GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
{
GemmConfigs configs;
configs.m0 = options.m0->value();
configs.n0 = options.n0->value();
configs.k0 = options.k0->value();
configs.v0 = options.v0->value();
configs.h0 = options.h0->value();
configs.interleave_lhs = options.interleave_lhs->value() != 0;
// FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
// transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
// 2 variants (both transposed and none transposed)
configs.transpose_lhs = options.transpose_rhs->value() == 0;
configs.interleave_rhs = options.interleave_rhs->value() != 0;
configs.transpose_rhs = options.transpose_rhs->value() != 0;
configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0;
return configs;
}
} // namespace
// Create function for ClGemmReshapeLhsMatrixKernel
using CLGEMMReshapeLHSMatrix = test::CLSynthetizeOperator<ClGemmReshapeLhsMatrixKernel>;
// Create function for ClGemmMatrixMultiplyReshapedKernel
using CLGEMMMatrixMultiplyReshaped = test::CLSynthetizeOperator<ClGemmMatrixMultiplyReshapedKernel>;
class CLGEMMMatrixMultiplyReshapedExample : public Example
{
public:
bool do_setup(int argc, char **argv) override
{
// Default parameters
const float alpha = 1.0f;
const float beta = 0.0f;
const ActivationLayerInfo act_info = ActivationLayerInfo();
CommonGemmExampleParams params;
GemmConfigs configs;
// Set up command line parser and options
CommandLineParser parser;
CommonGemmExampleOptions param_options(parser);
GemmConfigOptions config_options(parser);
// Parse command line options
parser.parse(argc, argv);
if (param_options.help->is_set() && param_options.help->value())
{
// Print help message
parser.print_help(argv[0]);
return false;
}
if (!parser.validate())
{
// Invalid arguments. Use default parameters and configs
std::cerr << "Invalid arguments." << std::endl;
parser.print_help(argv[0]);
std::cerr << "Falling back to default parameters and configs" << std::endl;
}
else
{
// Get parameters and configs from command-line options
params = consume_common_gemm_example_parameters(param_options);
configs = consume_gemm_configs(config_options);
}
// Print gemm parameters and configurations
std::cout << "Gemm parameters:" << std::endl;
std::cout << params << std::endl;
std::cout << "Gemm configurations:" << std::endl;
std::cout << configs << std::endl;
tuner.set_tuner_mode(params.tuner_mode);
CLScheduler::get().default_init(&tuner);
lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
GEMMLHSMatrixInfo lhs_info;
lhs_info.m0 = configs.m0;
lhs_info.k0 = configs.k0;
lhs_info.v0 = configs.v0;
lhs_info.interleave = configs.interleave_lhs;
lhs_info.transpose = configs.transpose_lhs;
GEMMRHSMatrixInfo rhs_info;
rhs_info.n0 = configs.n0;
rhs_info.k0 = configs.k0;
rhs_info.h0 = configs.h0;
rhs_info.interleave = configs.interleave_rhs;
rhs_info.transpose = configs.transpose_rhs;
rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs;
GEMMKernelInfo kernel_info;
kernel_info.m = params.M;
kernel_info.n = params.N;
kernel_info.k = params.K;
kernel_info.depth_output_gemm3d = 0;
kernel_info.reinterpret_input_as_3d = false;
kernel_info.broadcast_bias = true;
kernel_info.activation_info = act_info;
if (rhs_info.h0 == 0)
{
rhs_info.h0 = std::max(kernel_info.n / rhs_info.n0, 1U);
}
// Initialise lhs_reshaped tensor info
lhs_reshaped.allocator()->init(
TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type));
// Initialise rhs_reshaped tensor info
rhs_reshaped.allocator()->init(
TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
if (rhs_info.export_to_cl_image)
{
if (!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
{
std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
return false;
}
}
// Validate argments
Status status{};
status = reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, kernel_info.reinterpret_input_as_3d);
if (!status)
{
// Unsupported arguments
std::cerr << "Unsupported arguments." << std::endl;
std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
return false;
}
status = gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info,
rhs_info, kernel_info);
if (!status)
{
// Unsupported arguments
std::cerr << "Unsupported arguments." << std::endl;
std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
return false;
}
// Configure reshape lhs function
reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info);
// Configure function
gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info,
rhs_info, kernel_info);
// Allocate tensors
lhs.allocator()->allocate();
rhs.allocator()->allocate();
lhs_reshaped.allocator()->allocate();
rhs_reshaped.allocator()->allocate();
bias.allocator()->allocate();
dst.allocator()->allocate();
return true;
}
void do_run() override
{
// Execute the functions
ITensorPack reshape_lsh_pack({{ACL_SRC, &lhs}, {ACL_DST, &lhs_reshaped}});
reshape_lhs.run(reshape_lsh_pack);
ITensorPack gemm_pack(
{{ACL_SRC_0, &lhs_reshaped}, {ACL_SRC_1, &rhs_reshaped}, {ACL_SRC_2, &bias}, {ACL_DST, &dst}});
gemm.run(gemm_pack);
// Make sure all the OpenCL jobs are done executing:
CLScheduler::get().sync();
}
void do_teardown() override
{
}
private:
CLTensor lhs{};
CLTensor rhs{};
CLTensor lhs_reshaped{};
CLTensor rhs_reshaped{};
CLTensor bias{};
CLTensor dst{};
CLTuner tuner{};
CLGEMMReshapeLHSMatrix reshape_lhs{};
CLGEMMMatrixMultiplyReshaped gemm{};
};
/** Main program for gemm reshaped test
*
* @param[in] argc Number of arguments
* @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0, [optional] v0, [optional] h0, [optional] interleave_lhs, [optional] interleave_rhs, [optional] transpose_rhs, [optional] export_to_cl_image )
*/
int main(int argc, char **argv)
{
return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv);
}
|