test-thread-safety.cpp 5.2 KB

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