Skip to content

Conversation

ngxson
Copy link
Collaborator

@ngxson ngxson commented Sep 5, 2025

Ref original webgpu PR: #14978

Example command:

# install emscripten: brew install emscripten

emcmake cmake -B build-wasm -DGGML_WEBGPU=ON -DLLAMA_CURL=OFF -DGGML_WEBGPU_DEBUG=ON
cmake --build build-wasm --target test-backend-ops

@github-actions github-actions bot added the ggml changes relating to the ggml tensor library for machine learning label Sep 5, 2025
@github-actions github-actions bot added the build Compilation issues label Sep 6, 2025
@github-actions github-actions bot added the testing Everything test related label Sep 6, 2025
@@ -148,3 +148,6 @@ poetry.toml
/run-vim.sh
/run-chat.sh
.ccache/

# emscripten
a.out.*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just a.out* ?

@ngxson
Copy link
Collaborator Author

ngxson commented Sep 6, 2025

@ggerganov @slaren Quick question, I'm building test-backend-ops on wasm without multi-thread support for simplification. However, seems like ggml-cpu now always requires a threadpool, which makes multi-thread a requirement.

So I'm wondering, is there any ways to completely disable threadpool?

Edit: I'm referring to this code:

if (threadpool == NULL) {
//GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
disposable_threadpool = true;
struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads);
threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);
} else {

@ggerganov
Copy link
Member

I think a threadpool is currently required - don't think there is an easy workaround. @max-krasnyansky any thoughts?

@github-actions github-actions bot added the script Script related label Sep 7, 2025
@ngxson
Copy link
Collaborator Author

ngxson commented Sep 8, 2025

I think a threadpool is currently required - don't think there is an easy workaround. @max-krasnyansky any thoughts?

Hmm ok that means both wllama and whisper.cpp single-thread wasm builds are currently broken. Having single-thread support would be nice, but it's not urgent though.

@ggerganov
Copy link
Member

Yes, we should support to launch a single-thread compute without invoking synchronization primitives and spawning threads so that thread-less WASM works. Shouldn't be hard to implement.

Looking at the implementation, I think almost everything is inplace for that. Where does the single-thread WASM fail when you call ggml compute with n_threads == 1?

@ngxson
Copy link
Collaborator Author

ngxson commented Sep 8, 2025

It currently fails at this line:

GGML_ASSERT(rc == 0);

I don't have the stack trace due to some difficulty debugging in-browser, but it's very likely invoked by this line:

threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);

Where we try to create a threadpool of one single thread

@ngxson
Copy link
Collaborator Author

ngxson commented Sep 8, 2025

Also just want to note that atomic ops like atomic_store_explicit are not available in single-thread build, so this should requires some extra adaptations

@ggerganov
Copy link
Member

It currently fails at this line:

GGML_ASSERT(rc == 0);

I don't have the stack trace due to some difficulty debugging in-browser, but it's very likely invoked by this line:

threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);

Where we try to create a threadpool of one single thread

To enter that loop, it would mean that tpp->nthreads is larger than 1, because the loop stars from 1:

    for (int j = 1; j < tpp->n_threads; j++) {
        ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter);

        int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]);
        GGML_ASSERT(rc == 0);
    }

Could the calling program be using n_threads > 1 instead of n_threads == 1?

@ngxson
Copy link
Collaborator Author

ngxson commented Sep 8, 2025

Yeah you're right, the n_thread for CPU is not correctly set for test-backend-ops. I added it in bf9d14c and it's now working as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Compilation issues ggml changes relating to the ggml tensor library for machine learning script Script related testing Everything test related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants