44#include " coreml/whisper-encoder.h"
55#endif
66
7- #ifdef GGML_USE_METAL
8- #include " ggml-metal.h"
9- #endif
10-
11- #ifdef GGML_USE_CUDA
12- #include " ggml-cuda.h"
13- #endif
14-
15- #ifdef GGML_USE_VULKAN
16- #include " ggml-vulkan.h"
17- #endif
18-
19- #ifdef GGML_USE_SYCL
20- #include " ggml-sycl.h"
21- #endif
7+ #include " kcpp_backend.h"
228
239#ifdef WHISPER_USE_OPENVINO
2410#include " openvino/whisper-openvino-encoder.h"
@@ -209,7 +195,8 @@ static bool ggml_graph_compute_helper(
209195// and X_1 and Y_1 are the remaining views. X_1 and Y_1 end up being small matrices that can be processed with more
210196// general-purpose kernels
211197//
212- static struct ggml_tensor * ggml_mul_mat_pad (struct ggml_context * ctx, struct ggml_tensor * x, struct ggml_tensor * y, int pad = 32 ) {
198+ static struct ggml_tensor * ggml_mul_mat_pad (struct ggml_context * ctx, struct ggml_tensor * x, struct ggml_tensor * y) {
199+ const int pad = 32 ;
213200 // use padding only if dimension 0 is at least 8 times larger than the padding
214201 // else we won't get much benefit from the optimization
215202 const int n_pad_req = 8 ;
@@ -229,11 +216,12 @@ static struct ggml_tensor * ggml_mul_mat_pad(struct ggml_context * ctx, struct g
229216 ggml_mul_mat (ctx, x_1, y_1));
230217}
231218
219+ static struct ggml_tensor * ggml_mul_mat_original (struct ggml_context * ctx, struct ggml_tensor * x, struct ggml_tensor * y) {
220+ return ggml_mul_mat (ctx, x, y);
221+ }
222+
232223// TODO: check if other platforms can benefit from this optimization
233224// TODO: CUDA is currently broken - seems ggml_mul_mat does not handle views correctly
234- #if defined(GGML_USE_METAL)
235- #define ggml_mul_mat ggml_mul_mat_pad
236- #endif
237225
238226// available whisper models
239227enum e_model {
@@ -1082,17 +1070,13 @@ static uint32_t whisper_kv_cache_get_padding(const struct whisper_context & wctx
10821070 return 1u ;
10831071 }
10841072
1085- #ifdef GGML_USE_METAL
1086- if (ggml_backend_is_metal (wctx.backend )) {
1073+ if (kcpp_backend_check (" mtl" , wctx.backend )) {
10871074 return 32u ;
10881075 }
1089- #endif
10901076
1091- #ifdef GGML_USE_CUDA
1092- if (ggml_backend_is_cuda (wctx.backend )) {
1077+ if (kcpp_backend_check (KCPP_BACKENDS_USE_CUDA , wctx.backend )) {
10931078 return 256u ;
10941079 }
1095- #endif
10961080
10971081 return 1u ;
10981082}
@@ -1231,50 +1215,24 @@ static size_t aheads_masks_nbytes(struct whisper_aheads_masks & aheads_masks) {
12311215static ggml_backend_t whisper_backend_init (const whisper_context_params & params) {
12321216 ggml_backend_t backend_gpu = NULL ;
12331217
1234- // initialize the backends
1235- #ifdef GGML_USE_CUDA
1236- if (params.use_gpu ) {
1237- WHISPER_LOG_INFO (" %s: using CUDA backend\n " , __func__);
1238- backend_gpu = ggml_backend_cuda_init (params.gpu_device );
1239- if (!backend_gpu) {
1240- WHISPER_LOG_ERROR (" %s: ggml_backend_cuda_init() failed\n " , __func__);
1241- }
1242- }
1243- #endif
1244-
1245- #ifdef GGML_USE_METAL
1246- if (params.use_gpu ) {
1247- WHISPER_LOG_INFO (" %s: using Metal backend\n " , __func__);
1248- backend_gpu = ggml_backend_metal_init ();
1249- if (!backend_gpu) {
1250- WHISPER_LOG_ERROR (" %s: ggml_backend_metal_init() failed\n " , __func__);
1251- } else if (!ggml_backend_metal_supports_family (backend_gpu, 7 )) {
1252- WHISPER_LOG_ERROR (" %s: Metal GPU does not support family 7 - falling back to CPU\n " , __func__);
1253- ggml_backend_free (backend_gpu);
1254- backend_gpu = NULL ;
1255- }
1256- }
1257- #endif
1258-
1259- #ifdef GGML_USE_SYCL
1260- if (params.use_gpu ) {
1261- WHISPER_LOG_INFO (" %s: using SYCL backend\n " , __func__);
1262- backend_gpu = ggml_backend_sycl_init (params.gpu_device );
1263- if (!backend_gpu) {
1264- WHISPER_LOG_ERROR (" %s: ggml_backend_sycl_init() failed\n " , __func__);
1265- }
1266- }
1267- #endif
1268-
1269- #ifdef GGML_USE_VULKAN
12701218 if (params.use_gpu ) {
1271- WHISPER_LOG_INFO (" %s: using Vulkan backend\n " , __func__);
1272- backend_gpu = ggml_backend_vk_init (params.gpu_device );
1273- if (!backend_gpu) {
1274- WHISPER_LOG_ERROR (" %s: ggml_backend_vk_init() failed\n " , __func__);
1219+ auto device = kcpp_backend_get_device (params.gpu_device );
1220+ if (!device) {
1221+ WHISPER_LOG_ERROR (" %s: couldn't get device %d\n " , __func__, params.gpu_device );
1222+ } else {
1223+ WHISPER_LOG_INFO (" %s: using backend %s\n " , __func__, ggml_backend_dev_name (device));
1224+ backend_gpu = ggml_backend_dev_init (device, nullptr );
1225+ if (!backend_gpu) {
1226+ WHISPER_LOG_ERROR (" %s: ggml_backend_dev_init() failed\n " , __func__);
1227+ } else {
1228+ if (kcpp_backend_check (" mtl" , backend_gpu) && !kcpp_backend_metal_supports_family (backend_gpu, 7 )) {
1229+ WHISPER_LOG_ERROR (" %s: Metal GPU does not support family 7 - falling back to CPU\n " , __func__);
1230+ ggml_backend_free (backend_gpu);
1231+ backend_gpu = NULL ;
1232+ }
1233+ }
12751234 }
12761235 }
1277- #endif
12781236
12791237 if (backend_gpu) {
12801238 return backend_gpu;
@@ -1902,6 +1860,11 @@ static struct ggml_cgraph * whisper_build_graph_conv(
19021860static struct ggml_cgraph * whisper_build_graph_encoder (
19031861 whisper_context & wctx,
19041862 whisper_state & wstate) {
1863+
1864+ auto ggml_mul_mat = kcpp_backend_check (" mtl" , wctx.backend )
1865+ ? ggml_mul_mat_pad
1866+ : ggml_mul_mat_original;
1867+
19051868 const auto & model = wctx.model ;
19061869 const auto & hparams = model.hparams ;
19071870
@@ -2147,6 +2110,11 @@ static struct ggml_cgraph * whisper_build_graph_encoder(
21472110static struct ggml_cgraph * whisper_build_graph_cross (
21482111 whisper_context & wctx,
21492112 whisper_state & wstate) {
2113+
2114+ auto ggml_mul_mat = kcpp_backend_check (" mtl" , wctx.backend )
2115+ ? ggml_mul_mat_pad
2116+ : ggml_mul_mat_original;
2117+
21502118 const auto & model = wctx.model ;
21512119 const auto & hparams = model.hparams ;
21522120
@@ -2334,6 +2302,11 @@ static struct ggml_cgraph * whisper_build_graph_decoder(
23342302 const whisper_batch & batch,
23352303 bool save_alignment_heads_QKs,
23362304 bool worst_case) {
2305+
2306+ auto ggml_mul_mat = kcpp_backend_check (" mtl" , wctx.backend )
2307+ ? ggml_mul_mat_pad
2308+ : ggml_mul_mat_original;
2309+
23372310 const auto & model = wctx.model ;
23382311 const auto & hparams = model.hparams ;
23392312
@@ -6591,6 +6564,11 @@ WHISPER_API int whisper_bench_ggml_mul_mat(int n_threads) {
65916564}
65926565
65936566WHISPER_API const char * whisper_bench_ggml_mul_mat_str (int n_threads) {
6567+
6568+ auto ggml_mul_mat = kcpp_backend_check (" mtl" )
6569+ ? ggml_mul_mat_pad
6570+ : ggml_mul_mat_original;
6571+
65946572 static std::string s;
65956573 s = " " ;
65966574 char strbuf[256 ];
0 commit comments