Skip to content

Commit 4b0e2ce

Browse files
committed
replace ifdefs on whisper
1 parent 40e92bb commit 4b0e2ce

3 files changed

Lines changed: 73 additions & 66 deletions

File tree

kcpp_backend.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
# include "rocblas/rocblas.h"
1717
#endif
1818

19+
#if defined(GGML_USE_METAL)
20+
# include "ggml-metal.h"
21+
#endif
22+
1923
static std::string to_lowercase(const char* ptr) {
2024
std::string s = (ptr == nullptr) ? "" : ptr;
2125
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
@@ -50,6 +54,19 @@ static ggml_backend_dev_t get_ggml_main_device(void)
5054
return dev;
5155
}
5256

57+
ggml_backend_dev_t kcpp_backend_get_device(int index)
58+
{
59+
if (index < 0) {
60+
if (index == -1) {
61+
return get_ggml_main_device();
62+
} else {
63+
return ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
64+
}
65+
} else {
66+
return ggml_backend_dev_get((size_t)index);
67+
}
68+
}
69+
5370
// this is similar to sd_backend_is, except:
5471
// - if no backend is provided, checks the first ggml device (should be equivalent to a compile-time check)
5572
// - tests a |-separated list of device names
@@ -105,6 +122,15 @@ int kcpp_backend_check(const char* name, ggml_backend_t backend)
105122
return has_any_prefix(lo_dev_name, name, '|');
106123
}
107124

125+
bool kcpp_backend_metal_supports_family(ggml_backend_t backend, int family)
126+
{
127+
#if defined(GGML_USE_METAL)
128+
return ggml_backend_metal_supports_family(backend, family);
129+
#else
130+
return false;
131+
#endif
132+
}
133+
108134
void kcpp_backend_cuda_ggmlv2_transform_tensor(ggml_v2_tensor * tensor)
109135
{
110136
#if defined(GGML_USE_CUDA)

kcpp_backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// checks if the provided backend (or the first one) matches a |-separated name list
1717
int kcpp_backend_check(const char* name_list, ggml_backend_t backend = nullptr);
1818

19+
ggml_backend_dev_t kcpp_backend_get_device(int index);
1920

2021
// per-backend aux functions
2122

@@ -36,3 +37,5 @@ void kcpp_backend_cuda_set_mul_mat_q(int use_mmq);
3637

3738
void kcpp_backend_hip_initialize();
3839

40+
bool kcpp_backend_metal_supports_family(ggml_backend_t backend, int family);
41+

otherarch/whispercpp/whisper.cpp

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,7 @@
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
239227
enum 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) {
12311215
static 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(
19021860
static 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(
21472110
static 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

65936566
WHISPER_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

Comments
 (0)