-
Notifications
You must be signed in to change notification settings - Fork 12.1k
llama : add thread safety test #14035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
slaren
wants to merge
12
commits into
master
Choose a base branch
from
sl/thread-safety-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e14d9d8
llama : add thread safety test
slaren bf45300
use smaller stories15M-q4_0.gguf model
slaren b046f0c
test
slaren 169774a
llamafile : remove global state
ggerganov 8ef4b95
cont : reuse current_chunk from ggml_threadpool
ggerganov 03da6c8
cont : memory order relaxed
ggerganov 00ad177
cleanup
slaren 292c4e7
move context creation to the threads to test it too
slaren 2158fd0
load all models first
slaren 29020e6
Merge remote-tracking branch 'origin/master' into sl/thread-safety-test
slaren 9381f4e
disable vulkan tests
slaren b839192
Merge remote-tracking branch 'origin/master' into sl/thread-safety-test
slaren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// thread safety test | ||
// - Loads a copy of the same model on each GPU, plus a copy on the CPU | ||
// - Creates n_parallel (--parallel) contexts per model | ||
// - Runs inference in parallel on each context | ||
|
||
#include <thread> | ||
#include <vector> | ||
#include <atomic> | ||
#include "llama.h" | ||
#include "arg.h" | ||
#include "common.h" | ||
#include "log.h" | ||
#include "sampling.h" | ||
|
||
int main(int argc, char ** argv) { | ||
common_params params; | ||
|
||
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) { | ||
return 1; | ||
} | ||
|
||
common_init(); | ||
|
||
llama_backend_init(); | ||
llama_numa_init(params.numa); | ||
|
||
LOG_INF("%s\n", common_params_get_system_info(params).c_str()); | ||
|
||
//llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) { | ||
// if (level == GGML_LOG_LEVEL_ERROR) { | ||
// common_log_add(common_log_main(), level, "%s", text); | ||
// } | ||
//}, NULL); | ||
|
||
auto mparams = common_model_params_to_llama(params); | ||
auto cparams = common_context_params_to_llama(params); | ||
|
||
int dev_count = ggml_backend_dev_count(); | ||
int gpu_dev_count = 0; | ||
for (int i = 0; i < dev_count; ++i) { | ||
auto * dev = ggml_backend_dev_get(i); | ||
if (dev && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU) { | ||
gpu_dev_count++; | ||
} | ||
} | ||
const int num_models = gpu_dev_count + 1; // GPUs + 1 CPU model | ||
//const int num_models = std::max(1, gpu_dev_count); | ||
const int num_contexts = std::max(1, params.n_parallel); | ||
|
||
struct model_context { | ||
llama_model_ptr model; | ||
std::vector<llama_context_ptr> contexts; | ||
std::vector<std::unique_ptr<common_sampler, decltype(&common_sampler_free)>> samplers; | ||
}; | ||
|
||
std::vector<model_context> models; | ||
std::vector<std::thread> threads; | ||
std::atomic<bool> failed = false; | ||
|
||
for (int m = 0; m < num_models; ++m) { | ||
model_context this_model; | ||
|
||
mparams.split_mode = LLAMA_SPLIT_MODE_NONE; | ||
mparams.main_gpu = m < gpu_dev_count ? m : -1; | ||
|
||
llama_model * model = llama_model_load_from_file(params.model.path.c_str(), mparams); | ||
if (model == NULL) { | ||
LOG_ERR("%s: failed to load model '%s'\n", __func__, params.model.path.c_str()); | ||
return 1; | ||
} | ||
|
||
this_model.model.reset(model); | ||
|
||
for (int c = 0; c < num_contexts; ++c) { | ||
LOG_INF("Creating context %d/%d for model %d/%d\n", c + 1, num_contexts, m + 1, num_models); | ||
llama_context * ctx = llama_init_from_model(model, cparams); | ||
if (ctx == NULL) { | ||
LOG_ERR("%s: failed to create context\n", __func__); | ||
return 1; | ||
} | ||
this_model.contexts.emplace_back(ctx); | ||
|
||
common_sampler * sampler = common_sampler_init(model, params.sampling); | ||
if (sampler == NULL) { | ||
LOG_ERR("%s: failed to create sampler\n", __func__); | ||
return 1; | ||
} | ||
this_model.samplers.emplace_back(sampler, common_sampler_free); | ||
|
||
threads.emplace_back([model, ctx, sampler, ¶ms, &failed, m, c, num_models, num_contexts]() { | ||
llama_batch batch = {}; | ||
{ | ||
auto prompt = common_tokenize(ctx, params.prompt, true); | ||
if (prompt.empty()) { | ||
LOG_ERR("failed to tokenize prompt\n"); | ||
failed.store(true); | ||
return; | ||
} | ||
batch = llama_batch_get_one(prompt.data(), prompt.size()); | ||
if (llama_decode(ctx, batch)) { | ||
LOG_ERR("failed to decode prompt\n"); | ||
failed.store(true); | ||
return; | ||
} | ||
} | ||
|
||
const auto * vocab = llama_model_get_vocab(model); | ||
std::string result = params.prompt; | ||
|
||
for (int i = 0; i < params.n_predict; i++) { | ||
llama_token token; | ||
if (batch.n_tokens > 0) { | ||
token = common_sampler_sample(sampler, ctx, batch.n_tokens - 1); | ||
} else { | ||
token = llama_vocab_bos(vocab); | ||
} | ||
|
||
if (llama_vocab_is_eog(vocab, token)) { | ||
break; | ||
} | ||
result += common_token_to_piece(ctx, token); | ||
|
||
batch = llama_batch_get_one(&token, 1); | ||
if (llama_decode(ctx, batch)) { | ||
LOG_ERR("failed to decode\n"); | ||
failed.store(true); | ||
return; | ||
} | ||
} | ||
|
||
LOG_INF("Model %d/%d, Context %d/%d: Result: '%s'\n", m + 1, num_models, c + 1, num_contexts, result.c_str()); | ||
}); | ||
|
||
} | ||
|
||
models.emplace_back(std::move(this_model)); | ||
} | ||
|
||
for (auto & thread : threads) { | ||
thread.join(); | ||
} | ||
|
||
if (failed) { | ||
LOG_ERR("One or more threads failed.\n"); | ||
return 1; | ||
} | ||
|
||
LOG_INF("All threads completed successfully.\n"); | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.