|
| 1 | +// thread safety test |
| 2 | +// - Loads a copy of the same model on each GPU, plus a copy on the CPU |
| 3 | +// - Creates n_parallel (--parallel) contexts per model |
| 4 | +// - Runs inference in parallel on each context |
| 5 | + |
| 6 | +#include <thread> |
| 7 | +#include <vector> |
| 8 | +#include <atomic> |
| 9 | +#include "llama.h" |
| 10 | +#include "arg.h" |
| 11 | +#include "common.h" |
| 12 | +#include "log.h" |
| 13 | +#include "sampling.h" |
| 14 | + |
| 15 | +int main(int argc, char ** argv) { |
| 16 | + common_params params; |
| 17 | + |
| 18 | + if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) { |
| 19 | + return 1; |
| 20 | + } |
| 21 | + |
| 22 | + common_init(); |
| 23 | + |
| 24 | + llama_backend_init(); |
| 25 | + llama_numa_init(params.numa); |
| 26 | + |
| 27 | + LOG_INF("%s\n", common_params_get_system_info(params).c_str()); |
| 28 | + |
| 29 | + //llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) { |
| 30 | + // if (level == GGML_LOG_LEVEL_ERROR) { |
| 31 | + // common_log_add(common_log_main(), level, "%s", text); |
| 32 | + // } |
| 33 | + //}, NULL); |
| 34 | + |
| 35 | + auto cparams = common_context_params_to_llama(params); |
| 36 | + |
| 37 | + int dev_count = ggml_backend_dev_count(); |
| 38 | + int gpu_dev_count = 0; |
| 39 | + for (int i = 0; i < dev_count; ++i) { |
| 40 | + auto * dev = ggml_backend_dev_get(i); |
| 41 | + if (dev && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU) { |
| 42 | + gpu_dev_count++; |
| 43 | + } |
| 44 | + } |
| 45 | + const int num_models = gpu_dev_count + 1 + 1; // GPUs + 1 CPU model + 1 layer split |
| 46 | + //const int num_models = std::max(1, gpu_dev_count); |
| 47 | + const int num_contexts = std::max(1, params.n_parallel); |
| 48 | + |
| 49 | + std::vector<llama_model_ptr> models; |
| 50 | + std::vector<std::thread> threads; |
| 51 | + std::atomic<bool> failed = false; |
| 52 | + |
| 53 | + for (int m = 0; m < num_models; ++m) { |
| 54 | + auto mparams = common_model_params_to_llama(params); |
| 55 | + |
| 56 | + if (m < gpu_dev_count) { |
| 57 | + mparams.split_mode = LLAMA_SPLIT_MODE_NONE; |
| 58 | + mparams.main_gpu = m; |
| 59 | + } else if (m == gpu_dev_count) { |
| 60 | + mparams.split_mode = LLAMA_SPLIT_MODE_NONE; |
| 61 | + mparams.main_gpu = -1; // CPU model |
| 62 | + } else { |
| 63 | + mparams.split_mode = LLAMA_SPLIT_MODE_LAYER;; |
| 64 | + } |
| 65 | + |
| 66 | + llama_model * model = llama_model_load_from_file(params.model.path.c_str(), mparams); |
| 67 | + if (model == NULL) { |
| 68 | + LOG_ERR("%s: failed to load model '%s'\n", __func__, params.model.path.c_str()); |
| 69 | + return 1; |
| 70 | + } |
| 71 | + |
| 72 | + models.emplace_back(model); |
| 73 | + } |
| 74 | + |
| 75 | + for (int m = 0; m < num_models; ++m) { |
| 76 | + auto * model = models[m].get(); |
| 77 | + for (int c = 0; c < num_contexts; ++c) { |
| 78 | + threads.emplace_back([&, m, c, model]() { |
| 79 | + LOG_INF("Creating context %d/%d for model %d/%d\n", c + 1, num_contexts, m + 1, num_models); |
| 80 | + |
| 81 | + llama_context_ptr ctx { llama_init_from_model(model, cparams) }; |
| 82 | + if (ctx == NULL) { |
| 83 | + LOG_ERR("failed to create context\n"); |
| 84 | + failed.store(true); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + std::unique_ptr<common_sampler, decltype(&common_sampler_free)> sampler { common_sampler_init(model, params.sampling), common_sampler_free }; |
| 89 | + if (sampler == NULL) { |
| 90 | + LOG_ERR("failed to create sampler\n"); |
| 91 | + failed.store(true); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + llama_batch batch = {}; |
| 96 | + { |
| 97 | + auto prompt = common_tokenize(ctx.get(), params.prompt, true); |
| 98 | + if (prompt.empty()) { |
| 99 | + LOG_ERR("failed to tokenize prompt\n"); |
| 100 | + failed.store(true); |
| 101 | + return; |
| 102 | + } |
| 103 | + batch = llama_batch_get_one(prompt.data(), prompt.size()); |
| 104 | + if (llama_decode(ctx.get(), batch)) { |
| 105 | + LOG_ERR("failed to decode prompt\n"); |
| 106 | + failed.store(true); |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + const auto * vocab = llama_model_get_vocab(model); |
| 112 | + std::string result = params.prompt; |
| 113 | + |
| 114 | + for (int i = 0; i < params.n_predict; i++) { |
| 115 | + llama_token token; |
| 116 | + if (batch.n_tokens > 0) { |
| 117 | + token = common_sampler_sample(sampler.get(), ctx.get(), batch.n_tokens - 1); |
| 118 | + } else { |
| 119 | + token = llama_vocab_bos(vocab); |
| 120 | + } |
| 121 | + |
| 122 | + result += common_token_to_piece(ctx.get(), token); |
| 123 | + |
| 124 | + if (llama_vocab_is_eog(vocab, token)) { |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + batch = llama_batch_get_one(&token, 1); |
| 129 | + if (llama_decode(ctx.get(), batch)) { |
| 130 | + LOG_ERR("Model %d/%d, Context %d/%d: failed to decode\n", m + 1, num_models, c + 1, num_contexts); |
| 131 | + failed.store(true); |
| 132 | + return; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + LOG_INF("Model %d/%d, Context %d/%d: %s\n\n", m + 1, num_models, c + 1, num_contexts, result.c_str()); |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + for (auto & thread : threads) { |
| 142 | + thread.join(); |
| 143 | + } |
| 144 | + |
| 145 | + if (failed) { |
| 146 | + LOG_ERR("One or more threads failed.\n"); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + |
| 150 | + LOG_INF("All threads finished without errors.\n"); |
| 151 | + return 0; |
| 152 | +} |
0 commit comments