1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #pragma once
10
11 #include <executorch/backends/vulkan/runtime/api/api.h>
12
13 #define CL_TARGET_OPENCL_VERSION 200
14 #define CL_HPP_TARGET_OPENCL_VERSION CL_TARGET_OPENCL_VERSION
15 #include <CL/opencl.hpp>
16
17 using namespace vkcompute;
18 using namespace api;
19
20 #define QP context()->querypool()
21
benchmark_on_gpu(std::string shader_id,uint32_t niter,std::function<void ()> encode_kernel)22 auto benchmark_on_gpu(
23 std::string shader_id,
24 uint32_t niter,
25 std::function<void()> encode_kernel) {
26 auto fence = context()->fences().get_fence();
27
28 for (int i = 0; i < niter; ++i) {
29 encode_kernel();
30 };
31
32 context()->submit_cmd_to_gpu(fence.get_submit_handle());
33 fence.wait();
34 QP.extract_results();
35 uint64_t count = QP.get_mean_shader_ns(shader_id);
36 QP.reset_state();
37 context()->flush();
38
39 return count / 1000.f;
40 }
41
ensure_min_niter(double min_time_us,uint32_t & niter,std::function<double ()> run)42 void ensure_min_niter(
43 double min_time_us,
44 uint32_t& niter,
45 std::function<double()> run) {
46 const uint32_t DEFAULT_NITER = 100;
47 niter = DEFAULT_NITER;
48 for (uint32_t i = 0; i < 100; ++i) {
49 double t = run();
50 if (t > min_time_us * 0.99) {
51 return;
52 }
53 niter = uint32_t(niter * min_time_us / t);
54 }
55 }
56
whd_to_nchw(std::vector<int64_t> sizes)57 std::vector<int64_t> whd_to_nchw(std::vector<int64_t> sizes) {
58 const int64_t W = sizes[0];
59 const int64_t H = sizes[1];
60 const int64_t D = sizes[2];
61
62 // Channels-packed: {W, H, D} = {W, H, (C / 4) * N}
63 return {1, D * 4, H, W};
64 }
65
get_cl_platform_id()66 cl_platform_id get_cl_platform_id() {
67 cl_uint nplatform_id;
68 clGetPlatformIDs(0, nullptr, &nplatform_id);
69 std::vector<cl_platform_id> platform_ids;
70 platform_ids.resize(nplatform_id);
71 clGetPlatformIDs(nplatform_id, platform_ids.data(), nullptr);
72 return platform_ids[0];
73 }
74
get_cl_dev_id(cl_platform_id platform_id)75 cl_device_id get_cl_dev_id(cl_platform_id platform_id) {
76 cl_uint ndev_id;
77 clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 0, nullptr, &ndev_id);
78 std::vector<cl_device_id> dev_ids;
79 dev_ids.resize(ndev_id);
80 clGetDeviceIDs(
81 platform_id, CL_DEVICE_TYPE_ALL, ndev_id, dev_ids.data(), nullptr);
82 return dev_ids[0];
83 }
84
get_cl_device()85 cl::Device get_cl_device() {
86 auto platform_id = get_cl_platform_id();
87 auto dev_id = get_cl_dev_id(platform_id);
88 cl::Device dev(dev_id);
89 return dev;
90 }
91