dou112 commited on
Commit
f1535d7
·
1 Parent(s): 56f0e48

CANN: Simplify the environment variable setting(#13104)

Browse files

* Simplify the environment variable setting to specify the memory pool type.

* Adjust the GGML_CANN_ASYNC_MODE setting to accept yes, enable, 1, or on (case-insensitive) as valid options.

* update

* fix CI

* update

* delete whitespace

* fix according to review

* update CANN.md

* update CANN.md

ggml/src/ggml-cann/common.h CHANGED
@@ -37,6 +37,7 @@
37
  #include <thread>
38
  #include <unistd.h>
39
  #include <functional>
 
40
 
41
  #include "../include/ggml-cann.h"
42
  #include "../include/ggml.h"
@@ -103,6 +104,9 @@ const ggml_cann_device_info& ggml_cann_info();
103
  void ggml_cann_set_device(int32_t device);
104
  int32_t ggml_cann_get_device();
105
 
 
 
 
106
  /**
107
  * @brief Abstract base class for memory pools used by CANN.
108
  */
@@ -354,7 +358,8 @@ struct ggml_backend_cann_context {
354
  : device(device), name("CANN" + std::to_string(device)), task_queue(1024, device) {
355
  ggml_cann_set_device(device);
356
  description = aclrtGetSocName();
357
- async_mode = (getenv("GGML_CANN_ASYNC_MODE") != nullptr);
 
358
  GGML_LOG_INFO("%s: device %d async operator submission is %s\n", __func__,
359
  device, async_mode ? "ON" : "OFF");
360
  }
 
37
  #include <thread>
38
  #include <unistd.h>
39
  #include <functional>
40
+ #include <optional>
41
 
42
  #include "../include/ggml-cann.h"
43
  #include "../include/ggml.h"
 
104
  void ggml_cann_set_device(int32_t device);
105
  int32_t ggml_cann_get_device();
106
 
107
+ std::optional<std::string> get_env(const std::string& name);
108
+ bool parse_bool(const std::string& value);
109
+
110
  /**
111
  * @brief Abstract base class for memory pools used by CANN.
112
  */
 
358
  : device(device), name("CANN" + std::to_string(device)), task_queue(1024, device) {
359
  ggml_cann_set_device(device);
360
  description = aclrtGetSocName();
361
+
362
+ bool async_mode = parse_bool(get_env("GGML_CANN_ASYNC_MODE").value_or(""));
363
  GGML_LOG_INFO("%s: device %d async operator submission is %s\n", __func__,
364
  device, async_mode ? "ON" : "OFF");
365
  }
ggml/src/ggml-cann/ggml-cann.cpp CHANGED
@@ -31,6 +31,8 @@
31
  #include <mutex>
32
  #include <queue>
33
  #include <chrono>
 
 
34
 
35
  #include "ggml-impl.h"
36
  #include "ggml-backend-impl.h"
@@ -93,6 +95,26 @@ int32_t ggml_cann_get_device() {
93
  return id;
94
  }
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  /**
97
  * @brief Initialize the CANN device information.
98
  *
@@ -214,7 +236,7 @@ struct ggml_cann_pool_buf_prio : public ggml_cann_pool {
214
  * @param device The device ID to associate with this buffer pool.
215
  */
216
  explicit ggml_cann_pool_buf_prio(int device) : device(device) {
217
- disable_clean = getenv("GGML_CANN_DISABLE_BUF_POOL_CLEAN") != nullptr;
218
  }
219
 
220
  /**
@@ -410,7 +432,7 @@ struct ggml_cann_pool_buf : public ggml_cann_pool {
410
  * @param device The device ID to associate with this buffer pool.
411
  */
412
  explicit ggml_cann_pool_buf(int device) : device(device) {
413
- disable_clean = getenv("GGML_CANN_DISABLE_BUF_POOL_CLEAN") != nullptr;
414
  }
415
 
416
  /**
@@ -731,16 +753,18 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
731
  */
732
  std::unique_ptr<ggml_cann_pool> ggml_backend_cann_context::new_pool_for_device(
733
  int device) {
734
- bool disable_vmm = (getenv("GGML_CANN_DISABLE_VMM_POOL") != nullptr);
735
- if (!disable_vmm && ggml_cann_info().devices[device].vmm) {
736
- GGML_LOG_INFO("%s: device %d use vmm pool\n", __func__, device);
737
- return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_vmm(device));
738
- }
739
- bool enable_buf_prio = (getenv("GGML_CANN_ENABLE_BUF_PRIO_POOL") != nullptr);
740
- if (enable_buf_prio) {
741
  GGML_LOG_INFO("%s: device %d use buffer pool with priority queue\n", __func__, device);
742
  return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf_prio(device));
743
  }
 
 
 
 
 
 
744
  GGML_LOG_INFO("%s: device %d use buffer pool\n", __func__, device);
745
  return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf(device));
746
  }
 
31
  #include <mutex>
32
  #include <queue>
33
  #include <chrono>
34
+ #include <unordered_set>
35
+ #include <optional>
36
 
37
  #include "ggml-impl.h"
38
  #include "ggml-backend-impl.h"
 
95
  return id;
96
  }
97
 
98
+ /**
99
+ * @brief Get the value of the specified environment variable (name).
100
+ * if not empty, return a std::string object
101
+ */
102
+ std::optional<std::string> get_env(const std::string& name) {
103
+ const char* val = std::getenv(name.c_str());
104
+ if (!val) return std::nullopt;
105
+ std::string res = std::string(val);
106
+ std::transform(res.begin(), res.end(), res.begin(), ::tolower);
107
+ return res;
108
+ }
109
+
110
+ /**
111
+ * @brief Verify whether the environment variable is a valid value.
112
+ */
113
+ bool parse_bool(const std::string& value) {
114
+ std::unordered_set<std::string> valid_values = {"on", "1", "yes", "y", "enable", "true"};
115
+ return valid_values.find(value) != valid_values.end();
116
+ }
117
+
118
  /**
119
  * @brief Initialize the CANN device information.
120
  *
 
236
  * @param device The device ID to associate with this buffer pool.
237
  */
238
  explicit ggml_cann_pool_buf_prio(int device) : device(device) {
239
+ disable_clean = parse_bool(get_env("GGML_CANN_DISABLE_BUF_POOL_CLEAN").value_or(""));
240
  }
241
 
242
  /**
 
432
  * @param device The device ID to associate with this buffer pool.
433
  */
434
  explicit ggml_cann_pool_buf(int device) : device(device) {
435
+ disable_clean = parse_bool(get_env("GGML_CANN_DISABLE_BUF_POOL_CLEAN").value_or(""));
436
  }
437
 
438
  /**
 
753
  */
754
  std::unique_ptr<ggml_cann_pool> ggml_backend_cann_context::new_pool_for_device(
755
  int device) {
756
+ std::string mem_pool_type = get_env("GGML_CANN_MEM_POOL").value_or("");
757
+
758
+ if (mem_pool_type == "prio") {
 
 
 
 
759
  GGML_LOG_INFO("%s: device %d use buffer pool with priority queue\n", __func__, device);
760
  return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf_prio(device));
761
  }
762
+
763
+ if (ggml_cann_info().devices[device].vmm && mem_pool_type != "leg") {
764
+ GGML_LOG_INFO("%s: device %d use vmm pool\n", __func__, device);
765
+ return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_vmm(device));
766
+ }
767
+
768
  GGML_LOG_INFO("%s: device %d use buffer pool\n", __func__, device);
769
  return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf(device));
770
  }