Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ if (NOT SD_USE_UPSTREAM_GGML AND (SD_CUDA OR SD_HIPBLAS OR SD_MUSA))
endif()
if (SD_VULKAN AND NOT SD_USE_UPSTREAM_GGML)
add_subdirectory(src/core/vulkan)
list(APPEND SD_BACKEND_REGISTRATIONS sd_ggml_register_vulkan_rope sd_ggml_register_vulkan_i8 sd_ggml_register_vulkan_modulate sd_ggml_register_vulkan_rms_affine)
list(APPEND SD_BACKEND_REGISTRATIONS sd_ggml_register_vulkan_rope sd_ggml_register_vulkan_i8 sd_ggml_register_vulkan_modulate sd_ggml_register_vulkan_rms_affine sd_ggml_register_vulkan_vae)
endif()
if(SD_BACKEND_REGISTRATIONS)
string(REPLACE ";" ",\n " SD_BACKEND_REGISTRATIONS "${SD_BACKEND_REGISTRATIONS}")
Expand Down
1 change: 1 addition & 0 deletions cmake/backend_extensions.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void sd_ggml_register_vulkan_rope(ggml_backend_dev_t device);
void sd_ggml_register_vulkan_i8(ggml_backend_dev_t device);
void sd_ggml_register_vulkan_rms_affine(ggml_backend_dev_t device);
void sd_ggml_register_vulkan_modulate(ggml_backend_dev_t device);
void sd_ggml_register_vulkan_vae(ggml_backend_dev_t device);

static void (*const sd_backend_extensions[])(ggml_backend_dev_t) = {
@SD_BACKEND_REGISTRATIONS@
Expand Down
8 changes: 7 additions & 1 deletion src/core/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ cmake_policy(SET CMP0079 NEW)

function(sd_cuda_sources VENDOR LANGUAGE)
set(SOURCES)
foreach(OP rope int8 requantize i8_lora_merge w4 convrot affine modulate bf16 rms_affine registration)
set(OPERATORS rope int8 requantize i8_lora_merge w4 convrot affine modulate bf16 rms_affine vae_pad vae_frame_norm registration)
if(NOT VENDOR STREQUAL "musa")
list(APPEND OPERATORS swiglu_f16)
endif()
foreach(OP IN LISTS OPERATORS)
set(SOURCE "${CMAKE_CURRENT_BINARY_DIR}/${OP}_${VENDOR}.cu")
string(TOUPPER "${VENDOR}" UPPER_VENDOR)
file(GENERATE OUTPUT "${SOURCE}" CONTENT "#define SD_CUDA_VENDOR_${UPPER_VENDOR} 1\n#include \"core/cuda/${OP}.cu\"\n")
Expand Down Expand Up @@ -58,6 +62,8 @@ if(SD_HIPBLAS)
endif()
target_sources(${SD_CUDA_TARGET} PRIVATE hip_attention.cu)
set_source_files_properties(${SD_CUDA_SOURCES} PROPERTIES COMPILE_OPTIONS "-ffast-math")
# Keep the GroupNorm reductions and SiLU division consistent with ggml-hip.
set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/vae_frame_norm_hip.cu" PROPERTIES COMPILE_OPTIONS "-fno-fast-math")
# LoRA merging must detect NaNs and infinities before requantization.
set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/i8_lora_merge_hip.cu" PROPERTIES COMPILE_OPTIONS "-fno-fast-math")
target_link_libraries(${SD_CUDA_TARGET} PRIVATE hip::host roc::hipblas)
Expand Down
10 changes: 10 additions & 0 deletions src/core/cuda/registration.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ namespace SD_CUDA_NAMESPACE {
void register_modulate(ggml_backend_dev_t device);
void register_bf16(ggml_backend_dev_t device);
void register_rms_affine(ggml_backend_dev_t device);
void register_vae_pad(ggml_backend_dev_t device);
void register_vae_frame_norm(ggml_backend_dev_t device);
#if !defined(SD_CUDA_VENDOR_MUSA)
void register_swiglu_f16(ggml_backend_dev_t device);
#endif
#if defined(SD_CUDA_VENDOR_HIP)
void register_wmma_attention(ggml_backend_dev_t device);
#endif
Expand All @@ -39,6 +44,11 @@ namespace SD_CUDA_NAMESPACE {
register_rope(device);
register_bf16(device);
register_rms_affine(device);
register_vae_pad(device);
register_vae_frame_norm(device);
#if !defined(SD_CUDA_VENDOR_MUSA)
register_swiglu_f16(device);
#endif
#if defined(SD_CUDA_VENDOR_HIP)
register_wmma_attention(device);
#endif
Expand Down
72 changes: 50 additions & 22 deletions src/core/cuda/rope.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SD_CUDA_NAMESPACE {
return v;
}

template <int D, bool interleaved, typename T, bool affine = true, int HiddenDim = 0>
template <int D, bool interleaved, typename T, bool affine = true, int HiddenDim = 0, bool vector_norm = false>
static __global__ __launch_bounds__(HiddenDim > 4096 ? 256 : HiddenDim ? HiddenDim / 4
: 128) void rms_norm_mul_rope_apply_fused(const float* x,
const float* weight,
Expand Down Expand Up @@ -75,14 +75,23 @@ namespace SD_CUDA_NAMESPACE {
: (batch * heads + head) * tokens + token;
T* out = dst + out_row * D;
float v[D / 32], sums[8] = {};
if constexpr (vector_norm) {
// Keep the same four-channel partial sums and warp reduction as native RMSNorm.
float sum = 0.f;
if (lane < D / 4) {
const auto values = reinterpret_cast<const float4*>(x_row)[lane];
sum = values.x * values.x + values.y * values.y + values.z * values.z + values.w * values.w;
}
scale = rsqrtf(sd_warp_sum(sum) / float(D) + eps);
}
#pragma unroll
for (int j = 0; j < D / 32; ++j) {
v[j] = x_row[j * 32 + lane];
if constexpr (HiddenDim == 0) {
if constexpr (HiddenDim == 0 && !vector_norm) {
sums[j] = sd_warp_sum(__fmul_rn(v[j], v[j]));
}
}
if constexpr (HiddenDim == 0) {
if constexpr (HiddenDim == 0 && !vector_norm) {
// Preserve the 256-thread RMSNorm kernel's reduction tree, including inactive warps.
const float sum = __fadd_rn(
__fadd_rn(__fadd_rn(sums[0], sums[4]), __fadd_rn(sums[2], sums[6])),
Expand Down Expand Up @@ -391,8 +400,16 @@ namespace SD_CUDA_NAMESPACE {
return hidden_rope_cast_count(g, first);
}
if (first + 1 < ggml_graph_n_nodes(g) &&
rope_non_affine_fusion_supports(ggml_graph_node(g, first), ggml_graph_node(g, first + 1)))
rope_non_affine_fusion_supports(ggml_graph_node(g, first), ggml_graph_node(g, first + 1))) {
if (first + 2 < ggml_graph_n_nodes(g)) {
const auto* rope = ggml_graph_node(g, first + 1);
const auto* cast = ggml_graph_node(g, first + 2);
if (cast->op == GGML_OP_CPY && cast->src[0] == rope && cast->src[1] == cast && !cast->view_src &&
cast->type == GGML_TYPE_F16 && ggml_is_contiguous(cast) && ggml_are_same_shape(rope, cast))
return 3;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
return 2;
}
if (first + 2 >= ggml_graph_n_nodes(g) || !rope_fusion_supports(
ggml_graph_node(g, first), ggml_graph_node(g, first + 1), ggml_graph_node(g, first + 2)))
return 0;
Expand Down Expand Up @@ -474,9 +491,8 @@ namespace SD_CUDA_NAMESPACE {

static ggml_status rope_prepare(ggml_backend_t, ggml_tensor* const* nodes, int count, void**, size_t* workspace, void*) {
const auto* dst = nodes[count - 1];
// Owning casts are fresh outputs; copies into existing storage retain view_src.
const bool fresh_output = !dst->data && !dst->buffer && !dst->view_src;
*workspace = fresh_output ? 0 : ggml_nbytes(dst);
// The allocator can reuse input storage for a terminal copy, including an owning cast.
*workspace = dst->op == GGML_OP_CPY || dst->data || dst->buffer || dst->view_src ? ggml_nbytes(dst) : 0;
return GGML_STATUS_SUCCESS;
}

Expand All @@ -487,28 +503,40 @@ namespace SD_CUDA_NAMESPACE {
return x <= y ? y - x < ggml_nbytes(a) : x - y < ggml_nbytes(b);
}

template <typename T>
static CUDAError non_affine_rope_launch(CUDAStream stream, const ggml_tensor* norm, const ggml_tensor* rope, T* output) {
const auto* x = norm->src[0];
const auto* pe = rope->src[1];
const int64_t rows = ggml_nrows(x);
const bool aligned = (reinterpret_cast<uintptr_t>(x->data) | x->nb[1] | x->nb[2] | x->nb[3]) % 16 == 0;
auto kernel = aligned ? rms_norm_mul_rope_apply_fused<64, false, T, false, 0, true>
: rms_norm_mul_rope_apply_fused<64, false, T, false>;
kernel<<<static_cast<unsigned int>((rows + 3) / 4), dim3(32, 4), 0, stream>>>(
static_cast<const float*>(x->data), nullptr, static_cast<const float*>(pe->data), output,
x->ne[1], x->ne[2], rows, x->nb[1] / 4, x->nb[2] / 4, x->nb[3] / 4, int(pe->ne[2]), param_f32(norm, 0), 1.f);
return cuda_last_error();
}

static ggml_status rope_execute(const ggml_ext_execution_context* execution, ggml_tensor* const* nodes, int count, void*, void*) {
ggml_ext_gpu_context context;
if (!cuda_context(execution->backend, &context))
return GGML_STATUS_FAILED;
auto stream = static_cast<CUDAStream>(context.stream);
const bool hidden = count >= 4 && rope_is_node(nodes[3]);
const auto* rope = nodes[hidden ? 3 : count <= 2 ? count - 1
: 2];
const auto* x = count == 1 ? rope->src[0] : nodes[0]->src[0];
auto* dst = nodes[count - 1];
const bool alias = overlaps(dst, x) || overlaps(dst, rope->src[1]) || (count > 2 && overlaps(dst, nodes[1]->src[1]));
void* output = alias ? ggml_backend_buffer_get_base(execution->workspace) : dst->data;
auto stream = static_cast<CUDAStream>(context.stream);
const bool hidden = count >= 4 && rope_is_node(nodes[3]);
const bool non_affine = count >= 2 && rope_is_node(nodes[1]);
const auto* rope = nodes[hidden ? 3 : non_affine ? 1
: count > 1 ? 2
: 0];
const auto* x = count == 1 ? rope->src[0] : nodes[0]->src[0];
auto* dst = nodes[count - 1];
const bool alias = overlaps(dst, x) || overlaps(dst, rope->src[1]) || (count > 2 && !non_affine && overlaps(dst, nodes[1]->src[1]));
void* output = alias ? ggml_backend_buffer_get_base(execution->workspace) : dst->data;
CUDAError error;
if (hidden) {
error = hidden_rope_launch(stream, nodes, output, dst->type == GGML_TYPE_F16, 1.f);
} else if (count == 2) {
const auto* pe = rope->src[1];
const int64_t rows = ggml_nrows(x);
rms_norm_mul_rope_apply_fused<64, false, float, false><<<static_cast<unsigned int>((rows + 3) / 4), dim3(32, 4), 0, stream>>>(
static_cast<const float*>(x->data), nullptr, static_cast<const float*>(pe->data), static_cast<float*>(output),
x->ne[1], x->ne[2], rows, x->nb[1] / 4, x->nb[2] / 4, x->nb[3] / 4, int(pe->ne[2]), param_f32(nodes[0], 0), 1.f);
error = cuda_last_error();
} else if (non_affine) {
error = dst->type == GGML_TYPE_F16 ? non_affine_rope_launch(stream, nodes[0], rope, static_cast<half*>(output))
: non_affine_rope_launch(stream, nodes[0], rope, static_cast<float*>(output));
} else if (count > 2) {
error = rms_rope_launch(stream, nodes, output, count > 3, count == 5 ? param_f32(nodes[3], 0) : 1.f);
} else {
Expand Down
Loading
Loading