Skip to content

Commit 5bcd577

Browse files
committed
Move wasm single-thread logic out of test-backend-ops for cpu backend
1 parent 2abcdd6 commit 5bcd577

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

ggml/include/ggml.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,13 @@
225225
# define GGML_MAX_NAME 64
226226
#endif
227227

228+
// For single-thread WASM builds, only use 1 thread
229+
#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
228230
#define GGML_DEFAULT_N_THREADS 4
231+
#else
232+
#define GGML_DEFAULT_N_THREADS 1
233+
#endif
234+
229235
#define GGML_DEFAULT_GRAPH_SIZE 2048
230236

231237
#if UINTPTR_MAX == 0xFFFFFFFF

ggml/src/ggml-cpu/ggml-cpu.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,11 @@ bool ggml_backend_is_cpu(ggml_backend_t backend) {
246246
void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
247247
GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
248248

249+
// For single-thread WASM builds, do not allow changing the number of threads
250+
#if !defined(_EMSCRIPTEN_) || defined(__EMSCRIPTEN_PTHREADS__)
249251
struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
250252
ctx->n_threads = n_threads;
253+
#endif
251254
}
252255

253256
void ggml_backend_cpu_set_threadpool(ggml_backend_t backend_cpu, ggml_threadpool_t threadpool) {
@@ -622,10 +625,14 @@ static ggml_backend_feature * ggml_backend_cpu_get_features(ggml_backend_reg_t r
622625
}
623626

624627
static void * ggml_backend_cpu_get_proc_address(ggml_backend_reg_t reg, const char * name) {
628+
629+
// For single-thread WASM builds, do not expose a set_n_threads function
630+
#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
625631
if (strcmp(name, "ggml_backend_set_n_threads") == 0) {
626632
ggml_backend_set_n_threads_t fct = ggml_backend_cpu_set_n_threads;
627633
return (void *)fct;
628634
}
635+
#endif
629636
if (strcmp(name, "ggml_backend_dev_get_extra_bufts") == 0) {
630637
ggml_backend_dev_get_extra_bufts_t fct = ggml_backend_cpu_device_get_extra_buffers_type;
631638
return (void *)fct;

tests/test-backend-ops.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <ggml-alloc.h>
2020
#include <ggml-backend.h>
2121
#include <ggml-cpp.h>
22-
#include <ggml-cpu.h>
2322

2423
#include <algorithm>
2524
#include <array>
@@ -7905,9 +7904,6 @@ static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op
79057904
return false;
79067905
}
79077906

7908-
// TODO: find a better way to set the number of threads for the CPU backend
7909-
ggml_backend_cpu_set_n_threads(backend_cpu, N_THREADS);
7910-
79117907
size_t n_ok = 0;
79127908
size_t tests_run = 0;
79137909
std::vector<std::string> failed_tests;

0 commit comments

Comments
 (0)