-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[CANN] Simplify the environment variable setting for GGML_CANN_MEM_POOL and GGML_CANN_ASYNC_MODE #13104
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
Merged
Merged
[CANN] Simplify the environment variable setting for GGML_CANN_MEM_POOL and GGML_CANN_ASYNC_MODE #13104
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
327a274
Simplify the environment variable setting to specify the memory pool …
bachelor-dou 30a1dc4
Adjust the GGML_CANN_ASYNC_MODE setting to accept yes, enable, 1, or …
bachelor-dou ed225c0
update
bachelor-dou 8cf4d1e
fix CI
bachelor-dou 66a4e16
update
bachelor-dou e720075
delete whitespace
bachelor-dou 8c3df5e
fix according to review
bachelor-dou f66e152
update CANN.md
bachelor-dou cb7c13d
update CANN.md
bachelor-dou 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ | |
#include <mutex> | ||
#include <queue> | ||
#include <chrono> | ||
#include <unordered_set> | ||
#include <optional> | ||
|
||
#include "ggml-impl.h" | ||
#include "ggml-backend-impl.h" | ||
|
@@ -92,6 +94,26 @@ int32_t ggml_cann_get_device() { | |
return id; | ||
} | ||
|
||
/** | ||
* @brief Get the value of the specified environment variable (name). | ||
* if not empty, return a std::string object | ||
*/ | ||
std::optional<std::string> get_env(const std::string& name) { | ||
const char* val = std::getenv(name.c_str()); | ||
if (!val) return std::nullopt; | ||
std::string res = std::string(val); | ||
std::transform(res.begin(), res.end(), res.begin(), ::tolower); | ||
return res; | ||
} | ||
|
||
/** | ||
* @brief Verify whether the environment variable is a valid value. | ||
*/ | ||
bool parse_bool(const std::string& value) { | ||
std::unordered_set<std::string> valid_values = {"on", "1", "yes", "y", "enable", "true"}; | ||
return valid_values.find(value) != valid_values.end(); | ||
} | ||
|
||
/** | ||
* @brief Initialize the CANN device information. | ||
* | ||
|
@@ -213,7 +235,7 @@ struct ggml_cann_pool_buf_prio : public ggml_cann_pool { | |
* @param device The device ID to associate with this buffer pool. | ||
*/ | ||
explicit ggml_cann_pool_buf_prio(int device) : device(device) { | ||
disable_clean = getenv("GGML_CANN_DISABLE_BUF_POOL_CLEAN") != nullptr; | ||
disable_clean = parse_bool(get_env("GGML_CANN_DISABLE_BUF_POOL_CLEAN").value_or("")); | ||
} | ||
|
||
/** | ||
|
@@ -409,7 +431,7 @@ struct ggml_cann_pool_buf : public ggml_cann_pool { | |
* @param device The device ID to associate with this buffer pool. | ||
*/ | ||
explicit ggml_cann_pool_buf(int device) : device(device) { | ||
disable_clean = getenv("GGML_CANN_DISABLE_BUF_POOL_CLEAN") != nullptr; | ||
disable_clean = parse_bool(get_env("GGML_CANN_DISABLE_BUF_POOL_CLEAN").value_or("")); | ||
} | ||
|
||
/** | ||
|
@@ -730,16 +752,18 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool { | |
*/ | ||
std::unique_ptr<ggml_cann_pool> ggml_backend_cann_context::new_pool_for_device( | ||
int device) { | ||
bool disable_vmm = (getenv("GGML_CANN_DISABLE_VMM_POOL") != nullptr); | ||
if (!disable_vmm && ggml_cann_info().devices[device].vmm) { | ||
GGML_LOG_INFO("%s: device %d use vmm pool\n", __func__, device); | ||
return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_vmm(device)); | ||
} | ||
bool enable_buf_prio = (getenv("GGML_CANN_ENABLE_BUF_PRIO_POOL") != nullptr); | ||
if (enable_buf_prio) { | ||
std::string mem_pool_type = get_env("GGML_CANN_MEM_POOL").value_or(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use get_env function |
||
|
||
if (mem_pool_type == "prio") { | ||
GGML_LOG_INFO("%s: device %d use buffer pool with priority queue\n", __func__, device); | ||
return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf_prio(device)); | ||
} | ||
|
||
if (ggml_cann_info().devices[device].vmm && mem_pool_type != "leg") { | ||
GGML_LOG_INFO("%s: device %d use vmm pool\n", __func__, device); | ||
return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_vmm(device)); | ||
} | ||
|
||
GGML_LOG_INFO("%s: device %d use buffer pool\n", __func__, device); | ||
return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf(device)); | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not empty -> if defined