test-thread-safety.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. int dev_count = ggml_backend_dev_count();
  29. int gpu_dev_count = 0;
  30. for (int i = 0; i < dev_count; ++i) {
  31. auto * dev = ggml_backend_dev_get(i);
  32. if (dev && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_GPU) {
  33. gpu_dev_count++;
  34. }
  35. }
  36. const int num_models = gpu_dev_count + 1 + 1; // GPUs + 1 CPU model + 1 layer split
  37. //const int num_models = std::max(1, gpu_dev_count);
  38. const int num_contexts = std::max(1, params.n_parallel);
  39. std::vector<llama_model_ptr> models;
  40. std::vector<std::thread> threads;
  41. std::atomic<bool> failed = false;
  42. for (int m = 0; m < num_models; ++m) {
  43. auto mparams = common_model_params_to_llama(params);
  44. if (m < gpu_dev_count) {
  45. mparams.split_mode = LLAMA_SPLIT_MODE_NONE;
  46. mparams.main_gpu = m;
  47. } else if (m == gpu_dev_count) {
  48. mparams.split_mode = LLAMA_SPLIT_MODE_NONE;
  49. mparams.main_gpu = -1; // CPU model
  50. } else {
  51. mparams.split_mode = LLAMA_SPLIT_MODE_LAYER;;
  52. }
  53. llama_model * model = llama_model_load_from_file(params.model.path.c_str(), mparams);
  54. if (model == NULL) {
  55. LOG_ERR("%s: failed to load model '%s'\n", __func__, params.model.path.c_str());
  56. return 1;
  57. }
  58. models.emplace_back(model);
  59. }
  60. for (int m = 0; m < num_models; ++m) {
  61. auto * model = models[m].get();
  62. for (int c = 0; c < num_contexts; ++c) {
  63. threads.emplace_back([&, m, c, model]() {
  64. LOG_INF("Creating context %d/%d for model %d/%d\n", c + 1, num_contexts, m + 1, num_models);
  65. llama_context_ptr ctx { llama_init_from_model(model, cparams) };
  66. if (ctx == NULL) {
  67. LOG_ERR("failed to create context\n");
  68. failed.store(true);
  69. return;
  70. }
  71. std::unique_ptr<common_sampler, decltype(&common_sampler_free)> sampler { common_sampler_init(model, params.sampling), common_sampler_free };
  72. if (sampler == NULL) {
  73. LOG_ERR("failed to create sampler\n");
  74. failed.store(true);
  75. return;
  76. }
  77. llama_batch batch = {};
  78. {
  79. auto prompt = common_tokenize(ctx.get(), params.prompt, true);
  80. if (prompt.empty()) {
  81. LOG_ERR("failed to tokenize prompt\n");
  82. failed.store(true);
  83. return;
  84. }
  85. batch = llama_batch_get_one(prompt.data(), prompt.size());
  86. if (llama_decode(ctx.get(), batch)) {
  87. LOG_ERR("failed to decode prompt\n");
  88. failed.store(true);
  89. return;
  90. }
  91. }
  92. const auto * vocab = llama_model_get_vocab(model);
  93. std::string result = params.prompt;
  94. for (int i = 0; i < params.n_predict; i++) {
  95. llama_token token;
  96. if (batch.n_tokens > 0) {
  97. token = common_sampler_sample(sampler.get(), ctx.get(), batch.n_tokens - 1);
  98. } else {
  99. token = llama_vocab_bos(vocab);
  100. }
  101. result += common_token_to_piece(ctx.get(), token);
  102. if (llama_vocab_is_eog(vocab, token)) {
  103. break;
  104. }
  105. batch = llama_batch_get_one(&token, 1);
  106. if (llama_decode(ctx.get(), batch)) {
  107. LOG_ERR("Model %d/%d, Context %d/%d: failed to decode\n", m + 1, num_models, c + 1, num_contexts);
  108. failed.store(true);
  109. return;
  110. }
  111. }
  112. LOG_INF("Model %d/%d, Context %d/%d: %s\n\n", m + 1, num_models, c + 1, num_contexts, result.c_str());
  113. });
  114. }
  115. }
  116. for (auto & thread : threads) {
  117. thread.join();
  118. }
  119. if (failed) {
  120. LOG_ERR("One or more threads failed.\n");
  121. return 1;
  122. }
  123. LOG_INF("All threads finished without errors.\n");
  124. return 0;
  125. }