Skip to content

Commit 5942bf9

Browse files
committed
Revert "Revert musa: Upgrade MUSA SDK version to rc4.0.1 and use mudnn::Unary"
This reverts commit 2359c09.
1 parent e114aed commit 5942bf9

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

ggml/src/ggml-cuda/cpy.cu

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "cpy.cuh"
22
#include "dequantize.cuh"
3+
#ifdef GGML_USE_MUSA
4+
#include "ggml-musa/mudnn.cuh"
5+
#endif // GGML_USE_MUSA
36

47
typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
58

@@ -676,7 +679,14 @@ void ggml_cuda_cpy(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, gg
676679
#endif
677680
if (src0->type == src1->type && ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) {
678681
GGML_ASSERT(ggml_nbytes(src0) == ggml_nbytes(src1));
679-
CUDA_CHECK(cudaMemcpyAsync(src1_ddc, src0_ddc, ggml_nbytes(src0), cudaMemcpyDeviceToDevice, main_stream));
682+
#ifdef GGML_USE_MUSA
683+
if (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) {
684+
CUDA_CHECK(mudnnMemcpyAsync(ctx, src1, src0));
685+
} else
686+
#endif // GGML_USE_MUSA
687+
{
688+
CUDA_CHECK(cudaMemcpyAsync(src1_ddc, src0_ddc, ggml_nbytes(src0), cudaMemcpyDeviceToDevice, main_stream));
689+
}
680690
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
681691
ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream, dest_ptrs_d, graph_cpynode_index);
682692
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_BF16) {

ggml/src/ggml-cuda/fattn-mma-f16.cuh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ static __device__ __forceinline__ void flash_attn_ext_f16_iter(
775775
GGML_UNUSED(stride_mask); GGML_UNUSED(jt); GGML_UNUSED(tile_K);
776776
GGML_UNUSED(tile_V); GGML_UNUSED(tile_mask); GGML_UNUSED(Q_B);
777777
GGML_UNUSED(VKQ_C); GGML_UNUSED(KQ_max); GGML_UNUSED(KQ_rowsum);
778-
GGML_UNUSED(kb0);
778+
GGML_UNUSED(kb0); GGML_UNUSED(tile_Q);
779779
NO_DEVICE_CODE;
780780
#endif // NEW_MMA_AVAILABLE
781781
}

ggml/src/ggml-musa/mudnn.cu

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <mutex>
2+
#include <mudnn.h>
3+
4+
#include "mudnn.cuh"
5+
6+
namespace mudnn = musa::dnn;
7+
8+
// Returns a human-readable error string for mudnn::Status
9+
const char* mudnnGetErrorString(mudnn::Status err) {
10+
switch (err) {
11+
case mudnn::Status::SUCCESS:
12+
return "Success";
13+
case mudnn::Status::INVALID_PARAMETER:
14+
return "Invalid parameter";
15+
case mudnn::Status::NOT_INITIALIZED:
16+
return "Not initialized";
17+
case mudnn::Status::ALLOC_FAILED:
18+
return "Allocation failed";
19+
case mudnn::Status::NOT_SUPPORTED:
20+
return "Not supported";
21+
case mudnn::Status::INTERNAL_ERROR:
22+
return "Internal error";
23+
case mudnn::Status::ARCH_MISMATCH:
24+
return "Architecture mismatch";
25+
case mudnn::Status::EXECUTION_FAILED:
26+
return "Execution failed";
27+
default:
28+
return "Unknown mudnn status";
29+
}
30+
}
31+
32+
// Error checking macro for MUDNN calls
33+
#define MUDNN_CHECK(err) CUDA_CHECK_GEN(err, mudnn::Status::SUCCESS, mudnnGetErrorString)
34+
35+
namespace {
36+
// Thread-safe cache for mudnn::Handle objects per device
37+
std::unordered_map<int, std::unique_ptr<mudnn::Handle>> handle_cache;
38+
std::mutex handle_cache_mutex;
39+
40+
mudnn::Handle* get_cached_handle(int device_id) {
41+
std::lock_guard<std::mutex> lock(handle_cache_mutex);
42+
auto it = handle_cache.find(device_id);
43+
if (it != handle_cache.end()) {
44+
return it->second.get();
45+
}
46+
auto handle = std::make_unique<mudnn::Handle>(device_id);
47+
mudnn::Handle* handle_ptr = handle.get();
48+
handle_cache[device_id] = std::move(handle);
49+
return handle_ptr;
50+
}
51+
}
52+
53+
// Extracts dimensions and strides from a ggml_tensor
54+
int get_ggml_dims_and_strides(const ggml_tensor* tensor,
55+
std::vector<int64_t>& dims,
56+
std::vector<int64_t>& strides) {
57+
const int ndims = ggml_n_dims(tensor);
58+
const size_t element_size = ggml_element_size(tensor);
59+
60+
dims.resize(ndims);
61+
strides.resize(ndims);
62+
63+
for (int i = 0; i < ndims; ++i) {
64+
dims[i] = tensor->ne[i];
65+
strides[i] = tensor->nb[i] / static_cast<int64_t>(element_size);
66+
}
67+
return ndims;
68+
}
69+
70+
// Converts ggml_type to mudnn::Tensor::Type
71+
mudnn::Tensor::Type ggml_type_to_mudnn_type(ggml_type type) {
72+
switch (type) {
73+
case GGML_TYPE_F32:
74+
return mudnn::Tensor::Type::FLOAT;
75+
case GGML_TYPE_F16:
76+
return mudnn::Tensor::Type::HALF;
77+
78+
// TODO: Add support for other types
79+
80+
default:
81+
MUDNN_CHECK(mudnn::Status::NOT_SUPPORTED);
82+
}
83+
84+
return mudnn::Tensor::Type::FLOAT; // Default fallback
85+
}
86+
87+
// Asynchronous memory copy using mudnn::Unary::IDENTITY
88+
musaError_t mudnnMemcpyAsync(ggml_backend_cuda_context& ctx, const ggml_tensor* dst, const ggml_tensor* src) {
89+
mudnn::Tensor tensor_dst, tensor_src;
90+
91+
MUDNN_CHECK(tensor_dst.SetType(ggml_type_to_mudnn_type(dst->type)));
92+
MUDNN_CHECK(tensor_src.SetType(ggml_type_to_mudnn_type(src->type)));
93+
94+
std::vector<int64_t> dims, strides;
95+
const int ndims = get_ggml_dims_and_strides(src, dims, strides);
96+
97+
MUDNN_CHECK(tensor_dst.SetNdInfo(ndims, dims.data(), strides.data()));
98+
MUDNN_CHECK(tensor_src.SetNdInfo(ndims, dims.data(), strides.data()));
99+
MUDNN_CHECK(tensor_dst.SetAddr(dst->data));
100+
MUDNN_CHECK(tensor_src.SetAddr(src->data));
101+
102+
mudnn::Unary op;
103+
MUDNN_CHECK(op.SetMode(mudnn::Unary::Mode::IDENTITY));
104+
MUDNN_CHECK(op.SetAlpha(0.0f));
105+
MUDNN_CHECK(op.SetBeta(0.0f));
106+
107+
mudnn::Handle* handle = get_cached_handle(ctx.device);
108+
MUDNN_CHECK(handle->SetStream(ctx.stream()));
109+
MUDNN_CHECK(op.Run(*handle, tensor_dst, tensor_src));
110+
111+
return musaSuccess;
112+
}

ggml/src/ggml-musa/mudnn.cuh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include "../include/ggml.h"
4+
#include "../ggml-cuda/common.cuh"
5+
6+
// Asynchronously copies data from src tensor to dst tensor using the provided context.
7+
// Returns a musaError_t indicating success or failure.
8+
musaError_t mudnnMemcpyAsync(
9+
ggml_backend_cuda_context &ctx,
10+
const ggml_tensor *dst,
11+
const ggml_tensor *src
12+
);

0 commit comments

Comments
 (0)