-
Notifications
You must be signed in to change notification settings - Fork 13.7k
musa: Upgrade MUSA SDK version to rc4.0.1 and use mudnn::Unary::IDENTITY op to accelerate D2D memory copy #13647
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46b3f7d
musa: fix build warning (unused parameter)
yeahdongcn 11edfe9
musa: upgrade MUSA SDK version to rc4.0.1
yeahdongcn 1482871
musa: use mudnn::Unary::IDENTITY op to accelerate D2D memory copy
yeahdongcn c66ccb3
Update ggml/src/ggml-cuda/cpy.cu
yeahdongcn de631ed
musa: remove MUDNN_CHECK_GEN and use CUDA_CHECK_GEN instead in MUDNN_…
yeahdongcn 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
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
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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #include <mutex> | ||
| #include <mudnn.h> | ||
|
|
||
| #include "mudnn.cuh" | ||
|
|
||
| namespace mudnn = musa::dnn; | ||
|
|
||
| // Returns a human-readable error string for mudnn::Status | ||
| const char* mudnnGetErrorString(mudnn::Status err) { | ||
| switch (err) { | ||
| case mudnn::Status::SUCCESS: | ||
| return "Success"; | ||
| case mudnn::Status::INVALID_PARAMETER: | ||
| return "Invalid parameter"; | ||
| case mudnn::Status::NOT_INITIALIZED: | ||
| return "Not initialized"; | ||
| case mudnn::Status::ALLOC_FAILED: | ||
| return "Allocation failed"; | ||
| case mudnn::Status::NOT_SUPPORTED: | ||
| return "Not supported"; | ||
| case mudnn::Status::INTERNAL_ERROR: | ||
| return "Internal error"; | ||
| case mudnn::Status::ARCH_MISMATCH: | ||
| return "Architecture mismatch"; | ||
| case mudnn::Status::EXECUTION_FAILED: | ||
| return "Execution failed"; | ||
| default: | ||
| return "Unknown mudnn status"; | ||
| } | ||
| } | ||
|
|
||
| // Error checking macro for MUDNN calls | ||
| #define MUDNN_CHECK_GEN(err, success, error_fn) \ | ||
| do { \ | ||
| auto err_ = (err); \ | ||
| if (err_ != (success)) { \ | ||
| ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #define MUDNN_CHECK(err) MUDNN_CHECK_GEN(err, mudnn::Status::SUCCESS, mudnnGetErrorString) | ||
|
|
||
| namespace { | ||
| // Thread-safe cache for mudnn::Handle objects per device | ||
| std::unordered_map<int, std::unique_ptr<mudnn::Handle>> handle_cache; | ||
| std::mutex handle_cache_mutex; | ||
|
|
||
| mudnn::Handle* get_cached_handle(int device_id) { | ||
| std::lock_guard<std::mutex> lock(handle_cache_mutex); | ||
| auto it = handle_cache.find(device_id); | ||
| if (it != handle_cache.end()) { | ||
| return it->second.get(); | ||
| } | ||
| auto handle = std::make_unique<mudnn::Handle>(device_id); | ||
| mudnn::Handle* handle_ptr = handle.get(); | ||
| handle_cache[device_id] = std::move(handle); | ||
| return handle_ptr; | ||
| } | ||
| } | ||
|
|
||
| // Extracts dimensions and strides from a ggml_tensor | ||
| int get_ggml_dims_and_strides(const ggml_tensor* tensor, | ||
| std::vector<int64_t>& dims, | ||
| std::vector<int64_t>& strides) { | ||
| const int ndims = ggml_n_dims(tensor); | ||
| const size_t element_size = ggml_element_size(tensor); | ||
|
|
||
| dims.resize(ndims); | ||
| strides.resize(ndims); | ||
|
|
||
| for (int i = 0; i < ndims; ++i) { | ||
| dims[i] = tensor->ne[i]; | ||
| strides[i] = tensor->nb[i] / static_cast<int64_t>(element_size); | ||
| } | ||
| return ndims; | ||
| } | ||
|
|
||
| // Converts ggml_type to mudnn::Tensor::Type | ||
| mudnn::Tensor::Type ggml_type_to_mudnn_type(ggml_type type) { | ||
| switch (type) { | ||
| case GGML_TYPE_F32: | ||
| return mudnn::Tensor::Type::FLOAT; | ||
| case GGML_TYPE_F16: | ||
| return mudnn::Tensor::Type::HALF; | ||
|
|
||
| // TODO: Add support for other types | ||
|
|
||
| default: | ||
| MUDNN_CHECK(mudnn::Status::NOT_SUPPORTED); | ||
| } | ||
|
|
||
| return mudnn::Tensor::Type::FLOAT; // Default fallback | ||
| } | ||
|
|
||
| // Asynchronous memory copy using mudnn::Unary::IDENTITY | ||
| musaError_t mudnnMemcpyAsync(ggml_backend_cuda_context& ctx, const ggml_tensor* dst, const ggml_tensor* src) { | ||
| mudnn::Tensor tensor_dst, tensor_src; | ||
|
|
||
| MUDNN_CHECK(tensor_dst.SetType(ggml_type_to_mudnn_type(dst->type))); | ||
| MUDNN_CHECK(tensor_src.SetType(ggml_type_to_mudnn_type(src->type))); | ||
|
|
||
| std::vector<int64_t> dims, strides; | ||
| const int ndims = get_ggml_dims_and_strides(src, dims, strides); | ||
|
|
||
| MUDNN_CHECK(tensor_dst.SetNdInfo(ndims, dims.data(), strides.data())); | ||
| MUDNN_CHECK(tensor_src.SetNdInfo(ndims, dims.data(), strides.data())); | ||
| MUDNN_CHECK(tensor_dst.SetAddr(dst->data)); | ||
| MUDNN_CHECK(tensor_src.SetAddr(src->data)); | ||
|
|
||
| mudnn::Unary op; | ||
| MUDNN_CHECK(op.SetMode(mudnn::Unary::Mode::IDENTITY)); | ||
| MUDNN_CHECK(op.SetAlpha(0.0f)); | ||
| MUDNN_CHECK(op.SetBeta(0.0f)); | ||
|
|
||
| mudnn::Handle* handle = get_cached_handle(ctx.device); | ||
| MUDNN_CHECK(handle->SetStream(ctx.stream())); | ||
| MUDNN_CHECK(op.Run(*handle, tensor_dst, tensor_src)); | ||
|
|
||
| return musaSuccess; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #pragma once | ||
|
|
||
| #include "../include/ggml.h" | ||
| #include "../ggml-cuda/common.cuh" | ||
|
|
||
| // Asynchronously copies data from src tensor to dst tensor using the provided context. | ||
| // Returns a musaError_t indicating success or failure. | ||
| musaError_t mudnnMemcpyAsync( | ||
| ggml_backend_cuda_context &ctx, | ||
| const ggml_tensor *dst, | ||
| const ggml_tensor *src | ||
| ); |
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.
Uh oh!
There was an error while loading. Please reload this page.