Move wasm single-thread logic out of test-backend-ops for cpu backend

This commit is contained in:
Reese Levine 2025-11-14 09:53:44 -08:00
parent 2abcdd6379
commit 5bcd57722f
3 changed files with 13 additions and 4 deletions

View File

@ -225,7 +225,13 @@
# define GGML_MAX_NAME 64
#endif
// For single-thread WASM builds, only use 1 thread
#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
#define GGML_DEFAULT_N_THREADS 4
#else
#define GGML_DEFAULT_N_THREADS 1
#endif
#define GGML_DEFAULT_GRAPH_SIZE 2048
#if UINTPTR_MAX == 0xFFFFFFFF

View File

@ -246,8 +246,11 @@ bool ggml_backend_is_cpu(ggml_backend_t backend) {
void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
// For single-thread WASM builds, do not allow changing the number of threads
#if !defined(_EMSCRIPTEN_) || defined(__EMSCRIPTEN_PTHREADS__)
struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
ctx->n_threads = n_threads;
#endif
}
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
}
static void * ggml_backend_cpu_get_proc_address(ggml_backend_reg_t reg, const char * name) {
// For single-thread WASM builds, do not expose a set_n_threads function
#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
if (strcmp(name, "ggml_backend_set_n_threads") == 0) {
ggml_backend_set_n_threads_t fct = ggml_backend_cpu_set_n_threads;
return (void *)fct;
}
#endif
if (strcmp(name, "ggml_backend_dev_get_extra_bufts") == 0) {
ggml_backend_dev_get_extra_bufts_t fct = ggml_backend_cpu_device_get_extra_buffers_type;
return (void *)fct;

View File

@ -19,7 +19,6 @@
#include <ggml-alloc.h>
#include <ggml-backend.h>
#include <ggml-cpp.h>
#include <ggml-cpu.h>
#include <algorithm>
#include <array>
@ -7905,9 +7904,6 @@ static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op
return false;
}
// TODO: find a better way to set the number of threads for the CPU backend
ggml_backend_cpu_set_n_threads(backend_cpu, N_THREADS);
size_t n_ok = 0;
size_t tests_run = 0;
std::vector<std::string> failed_tests;