fix potential race condition in check_for_work

This commit is contained in:
fmz 2024-08-08 05:59:20 -07:00
parent dfa63778bd
commit 2e18f0d4c9
2 changed files with 21 additions and 16 deletions

View file

@ -180,7 +180,7 @@ static const char * sample(struct llama_sampling_context * ctx_sampling,
static struct llava_context * minicpmv_init(gpt_params * params, const std::string & fname, int &n_past){ static struct llava_context * minicpmv_init(gpt_params * params, const std::string & fname, int &n_past){
auto ctx_clip = clip_init_context(params); auto ctx_clip = clip_init_context(params);
auto embeds = llava_image_embed_make_with_filename(ctx_clip, params->n_threads, fname.c_str()); auto embeds = llava_image_embed_make_with_filename(ctx_clip, params->cpuparams.n_threads, fname.c_str());
if (!embeds) { if (!embeds) {
std::cerr << "error: failed to load image " << fname << ". Terminating\n\n"; std::cerr << "error: failed to load image " << fname << ". Terminating\n\n";
return NULL; return NULL;

View file

@ -18861,7 +18861,6 @@ void ggml_release_threadpool(struct ggml_compute_threadpool* threadpool) {
if (!threadpool->disposable) { if (!threadpool->disposable) {
ggml_mutex_lock(&threadpool->mutex); ggml_mutex_lock(&threadpool->mutex);
} }
threadpool->n_threads_cur = n_threads;
threadpool->stop = true; threadpool->stop = true;
threadpool->pause = false; threadpool->pause = false;
if (!threadpool->disposable) { if (!threadpool->disposable) {
@ -19154,21 +19153,27 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
static bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) { static bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
struct ggml_compute_threadpool * threadpool = state->threadpool; struct ggml_compute_threadpool * threadpool = state->threadpool;
do {
if (threadpool->poll) { if (threadpool->poll) {
while (!threadpool->new_work && !threadpool->stop && !threadpool->pause) { while (!((threadpool->new_work && state->ith < threadpool->n_threads_cur) ||
threadpool->stop ||
threadpool->pause
)
) {
// No new work. Yield and keep polling. // No new work. Yield and keep polling.
__cpu_relax(); __cpu_relax();
} }
} else { } else {
ggml_mutex_lock_shared(&threadpool->mutex); ggml_mutex_lock_shared(&threadpool->mutex);
while (!threadpool->new_work && !threadpool->stop && !threadpool->pause) { while (!((threadpool->new_work && state->ith < threadpool->n_threads_cur) ||
threadpool->stop ||
threadpool->pause
)
) {
// No new work. Wait for the signal. // No new work. Wait for the signal.
ggml_cond_wait(&threadpool->cond, &threadpool->mutex); ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
} }
ggml_mutex_unlock_shared(&threadpool->mutex); ggml_mutex_unlock_shared(&threadpool->mutex);
} }
} while (state->ith >= threadpool->n_threads_cur);
return threadpool->new_work; return threadpool->new_work;
} }