Commit f036d77 for stable-diffusion.cpp
commit f036d778c08c8dc7173adda072848cd89f22e47f
Author: leejet <leejet714@gmail.com>
Date: Sat Dec 5 01:17:15 2026 +0800
fix: restrict INT8 dot fallback to unsupported BLAS calls (#2524)
diff --git a/docs/guides/int8_convrot.md b/docs/guides/int8_convrot.md
index f67a467..39d9134 100644
--- a/docs/guides/int8_convrot.md
+++ b/docs/guides/int8_convrot.md
@@ -99,7 +99,7 @@ The packed runtime activation tensor contains the I8 activation rows and their f
- CPU provides the portable regular Hadamard, activation quantization, INT8 matrix multiplication, and scale restoration implementations.
- Metal supports INT8 tensorwise and convrot on Apple silicon (Apple GPU family 7 or newer). It keeps weights in INT8 and rotates and quantizes activations on the GPU. On macOS/iOS 26 or newer, it probes Metal TensorOps `matmul2d` with INT8 inputs and INT32 cooperative results. It combines 1024-product partial sums using wrapping integer addition to preserve exact accumulation on older GPUs, then applies scales and bias in registers. Older systems or unavailable TensorOps pipelines use SIMD-group matrix multiplication: INT8 tiles are converted to FP16, and FP32 partial sums cover at most 256 products before being combined as integers, preserving the I32 accumulation contract. Convrot groups 1, 4, 16, 64, and 256 are supported. These kernels require the patched GGML Metal extension services and are enabled by `SD_METAL=ON`.
- CUDA uses CUTLASS for eligible shapes when enabled, and cuBLAS for other eligible shapes on Turing and newer. HIP uses hipBLAS or hipBLASLt on supported targets, including RDNA2, RDNA3, RDNA3.5, RDNA4 (`gfx120x`), and CDNA.
-- CUDA, HIP, and MUSA share a tiled INT8 dot-product fallback. It handles devices without INT8 BLAS and projections whose output width is not divisible by four. It uses packed byte-dot instructions where available.
+- CUDA, HIP, and MUSA share a tiled INT8 dot-product fallback. It handles devices without INT8 BLAS, projections whose output width is not divisible by four, and INT8 GEMMs that cuBLAS or hipBLAS explicitly reports as unsupported. It uses packed byte-dot instructions where available.
- Vulkan supports unrotated rowwise, H64, and H256 activation quantization and packed-activation INT8 matrix multiplication with input widths divisible by 64. It requires integer dot product support and uses cooperative matrices when available. Unsupported configurations use the scheduler's CPU fallback.
`--lora-apply-mode auto` selects immediate merging for INT8 tensorwise models
@@ -163,9 +163,11 @@ with a 128 MiB storage-buffer range. The packed weights, activations, scales,
and FP32 output must still fit their respective bindings. Projections that
exceed those limits fall back to CPU.
-W4A4 uses packed-weight kernels. Eligible CUDA SM80+ shapes can use CUTLASS mixed
-S8 x S4 GEMM. Vulkan uses cooperative matrices where supported and requires a
-logical input width divisible by 64. Use `GGML_CUDA_W4_FORCE_DP4A=1` or
+Eligible W4A4 projections on HIP decode weights into up to 96 MiB of workspace
+for the shared INT8 GEMM path. Larger projections use the packed-weight kernel.
+Eligible CUDA SM80+ shapes can use CUTLASS mixed S8 x S4 GEMM. Vulkan uses
+cooperative matrices where supported and requires a logical input width divisible
+by 64. Use `GGML_CUDA_W4_FORCE_DP4A=1` or
`GGML_VK_W4_FORCE_DP4A=1` to select packed byte dots for W4A4 on the corresponding
backend.
diff --git a/src/core/cuda/hip_i8_gemm.cpp b/src/core/cuda/hip_i8_gemm.cpp
index 61b5fbb..c9d724d 100644
--- a/src/core/cuda/hip_i8_gemm.cpp
+++ b/src/core/cuda/hip_i8_gemm.cpp
@@ -76,8 +76,9 @@ namespace sd_hip {
};
struct HipGemmTimer {
- hipEvent_t start = nullptr;
- hipEvent_t end = nullptr;
+ hipEvent_t start = nullptr;
+ hipEvent_t end = nullptr;
+ hipError_t sync_error = hipSuccess;
~HipGemmTimer() {
if (end)
@@ -87,7 +88,7 @@ namespace sd_hip {
}
template <typename Launch>
- float measure(hipStream_t stream, Launch launch) const {
+ float measure(hipStream_t stream, Launch launch) {
constexpr int trials = 3;
if (hipEventRecord(start, stream) != hipSuccess)
return 0.f;
@@ -95,21 +96,23 @@ namespace sd_hip {
if (launch() != HIPBLAS_STATUS_SUCCESS)
return 0.f;
}
+ if (hipEventRecord(end, stream) != hipSuccess)
+ return 0.f;
+ sync_error = hipEventSynchronize(end);
float ms;
- return hipEventRecord(end, stream) == hipSuccess && hipEventSynchronize(end) == hipSuccess &&
- hipEventElapsedTime(&ms, start, end) == hipSuccess
+ return sync_error == hipSuccess && hipEventElapsedTime(&ms, start, end) == hipSuccess
? ms / trials
: 0.f;
}
};
- static void tune(int device, hipblasLtHandle_t handle, const HipI8Gemm& gemm, HipLtPlan& plan) {
+ static hipblasStatus_t tune(int device, hipblasLtHandle_t handle, const HipI8Gemm& gemm, HipLtPlan& plan) {
if (!plan.init(gemm))
- return;
+ return HIPBLAS_STATUS_SUCCESS;
hipblasLtMatmulPreference_t preference = nullptr;
if (hipblasLtMatmulPreferenceCreate(&preference) != HIPBLAS_STATUS_SUCCESS)
- return;
+ return HIPBLAS_STATUS_SUCCESS;
// A zero-workspace kernel can use the existing ggml accumulator buffer.
constexpr uint64_t workspace = 0;
std::array<hipblasLtMatmulHeuristicResult_t, 8> choices{};
@@ -122,29 +125,40 @@ namespace sd_hip {
}
hipblasLtMatmulPreferenceDestroy(preference);
if (count == 0)
- return;
+ return HIPBLAS_STATUS_SUCCESS;
HipGemmTimer timer;
if (hipEventCreate(&timer.start) != hipSuccess || hipEventCreate(&timer.end) != hipSuccess)
- return;
- const auto blas_status = gemm.run_hipblas();
- const bool blas_unsupported = blas_status == HIPBLAS_STATUS_NOT_SUPPORTED || blas_status == HIPBLAS_STATUS_ARCH_MISMATCH;
+ return HIPBLAS_STATUS_SUCCESS;
+ auto blas_status = gemm.run_hipblas();
+ const bool blas_unsupported = hip_i8_gemm_unsupported(blas_status);
float baseline = 0.f;
if (blas_status == HIPBLAS_STATUS_SUCCESS) {
- baseline = timer.measure(gemm.stream, [&] { return gemm.run_hipblas(); });
+ baseline = timer.measure(gemm.stream, [&] { return blas_status = gemm.run_hipblas(); });
+ if (timer.sync_error != hipSuccess)
+ return HIPBLAS_STATUS_EXECUTION_FAILED;
if (baseline <= 0.f)
- return;
+ return hip_i8_gemm_unsupported(blas_status) ? HIPBLAS_STATUS_SUCCESS : blas_status;
} else if (!blas_unsupported) {
- return;
+ return blas_status;
}
float best = blas_unsupported ? std::numeric_limits<float>::infinity() : baseline * 0.95f;
for (int i = 0; i < count; ++i) {
const auto& candidate = choices[i];
- if (candidate.state != HIPBLAS_STATUS_SUCCESS || candidate.workspaceSize != 0 ||
- plan.run(handle, gemm, candidate.algo) != HIPBLAS_STATUS_SUCCESS)
+ if (candidate.state != HIPBLAS_STATUS_SUCCESS || candidate.workspaceSize != 0)
+ continue;
+ auto status = plan.run(handle, gemm, candidate.algo);
+ float ms = 0.f;
+ if (status == HIPBLAS_STATUS_SUCCESS) {
+ ms = timer.measure(gemm.stream, [&] { return status = plan.run(handle, gemm, candidate.algo); });
+ if (timer.sync_error != hipSuccess)
+ return HIPBLAS_STATUS_EXECUTION_FAILED;
+ }
+ if (hip_i8_gemm_unsupported(status))
continue;
- const float ms = timer.measure(gemm.stream, [&] { return plan.run(handle, gemm, candidate.algo); });
+ if (status != HIPBLAS_STATUS_SUCCESS)
+ return status;
if (ms > 0.f && ms < best) {
best = ms;
plan.algo = candidate.algo;
@@ -160,6 +174,7 @@ namespace sd_hip {
gemm.n, gemm.rows, gemm.k, device, baseline, best);
}
}
+ return HIPBLAS_STATUS_SUCCESS;
}
static HipLtDevice& lt_device(int ordinal) {
@@ -169,13 +184,13 @@ namespace sd_hip {
return devices.try_emplace(ordinal, ordinal).first->second;
}
- bool hip_i8_gemm_lt(int device, const HipI8Gemm& gemm) {
+ hipblasStatus_t hip_i8_gemm_lt(int device, const HipI8Gemm& gemm) {
static const bool disabled = std::getenv("SD_HIP_DISABLE_I8_LT") != nullptr;
if (disabled || gemm.n < 1024 || gemm.rows < 1024 || gemm.k < 1024 || gemm.k > INT32_MAX / 16384 ||
(gemm.n | gemm.k) % 16 != 0 ||
(reinterpret_cast<uintptr_t>(gemm.weight) | reinterpret_cast<uintptr_t>(gemm.input) |
reinterpret_cast<uintptr_t>(gemm.output)) % 16 != 0) {
- return false;
+ return HIPBLAS_STATUS_NOT_SUPPORTED;
}
auto& state = lt_device(device);
std::lock_guard<std::mutex> lock(state.mutex);
@@ -185,7 +200,7 @@ namespace sd_hip {
if (!needs_tuning) {
state.plans.splice(state.plans.begin(), state.plans, plan);
if (!plan->use_lt)
- return false;
+ return HIPBLAS_STATUS_NOT_SUPPORTED;
}
const auto stream_handle = state.handles.find(gemm.stream);
@@ -193,28 +208,35 @@ namespace sd_hip {
if (needs_tuning || !handle) {
// Handle creation and tuning must run outside graph capture.
hipStreamCaptureStatus capture;
- if (hipStreamIsCapturing(gemm.stream, &capture) != hipSuccess || capture != hipStreamCaptureStatusNone)
- return false;
+ if (hipStreamIsCapturing(gemm.stream, &capture) != hipSuccess)
+ return HIPBLAS_STATUS_EXECUTION_FAILED;
+ if (capture != hipStreamCaptureStatusNone)
+ return HIPBLAS_STATUS_NOT_SUPPORTED;
}
if (!handle) {
// A host lock does not wait for work submitted on another stream.
- if (hipblasLtCreate(&handle) != HIPBLAS_STATUS_SUCCESS)
- return false;
+ const auto status = hipblasLtCreate(&handle);
+ if (status != HIPBLAS_STATUS_SUCCESS)
+ return status;
state.handles.emplace(gemm.stream, handle);
}
if (needs_tuning) {
state.plans.emplace_front(shape);
plan = state.plans.begin();
- tune(device, handle, gemm, *plan);
+ const auto status = tune(device, handle, gemm, *plan);
+ if (status != HIPBLAS_STATUS_SUCCESS) {
+ state.plans.erase(plan);
+ return status;
+ }
if (state.plans.size() > 64)
state.plans.pop_back();
if (!plan->use_lt)
- return false;
+ return HIPBLAS_STATUS_NOT_SUPPORTED;
}
- if (plan->run(handle, gemm, plan->algo) == HIPBLAS_STATUS_SUCCESS)
- return true;
- plan->use_lt = false;
- return false;
+ const auto status = plan->run(handle, gemm, plan->algo);
+ if (hip_i8_gemm_unsupported(status))
+ plan->use_lt = false;
+ return status;
}
}
diff --git a/src/core/cuda/hip_i8_gemm.h b/src/core/cuda/hip_i8_gemm.h
index 1c24b32..0a2fa88 100644
--- a/src/core/cuda/hip_i8_gemm.h
+++ b/src/core/cuda/hip_i8_gemm.h
@@ -6,6 +6,10 @@
#include <cstdint>
namespace sd_hip {
+ inline bool hip_i8_gemm_unsupported(hipblasStatus_t status) {
+ return status == HIPBLAS_STATUS_NOT_SUPPORTED || status == HIPBLAS_STATUS_ARCH_MISMATCH;
+ }
+
struct HipI8Gemm {
int n, rows, k;
const void* weight;
@@ -28,7 +32,7 @@ namespace sd_hip {
};
#ifdef SD_HIP_I8_LT
- bool hip_i8_gemm_lt(int device, const HipI8Gemm& gemm);
+ hipblasStatus_t hip_i8_gemm_lt(int device, const HipI8Gemm& gemm);
#endif
}
diff --git a/src/core/cuda/int8.cu b/src/core/cuda/int8.cu
index fc11eb3..6abc07e 100644
--- a/src/core/cuda/int8.cu
+++ b/src/core/cuda/int8.cu
@@ -473,6 +473,56 @@ namespace SD_CUDA_NAMESPACE {
}
}
+ static bool run_i8_dot(CUDAStream stream, const void* weight, const int8_t* input, int32_t* output, int64_t k, int64_t n, int64_t rows) {
+ const dim3 grid(unsigned(std::min<int64_t>((n + 31) / 32, 65535)), unsigned(std::min<int64_t>((rows + 31) / 32, 65535)));
+ mul_mat_i8_dot<<<grid, dim3(16, 16), 0, stream>>>(static_cast<const int*>(weight),
+ reinterpret_cast<const int*>(input), output, k / 4, n, rows);
+ return cuda_last_error() == cuda_success;
+ }
+
+#if !defined(SD_CUDA_VENDOR_MUSA)
+ static void* i8_blas_handle(ggml_backend_t backend) {
+ auto reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend));
+ auto query = reinterpret_cast<ggml_ext_gpu_blas_fn>(ggml_backend_reg_get_proc_address(reg, "ggml_ext_gpu_blas_v1"));
+ return query ? query(backend) : nullptr;
+ }
+
+ static bool run_i8_blas(const ggml_ext_gpu_context& context, void* handle, const void* weight, const int8_t* input, int32_t* output, int64_t k, int64_t n, int64_t rows, int64_t padded) {
+ if (!handle)
+ return false;
+ auto stream = static_cast<CUDAStream>(context.stream);
+#if defined(SD_CUDA_VENDOR_HIP)
+ const HipI8Gemm gemm{int(n), int(padded), int(k), weight, input, output, static_cast<hipblasHandle_t>(handle), stream};
+#ifdef SD_HIP_I8_LT
+ const auto lt_status = hip_i8_gemm_lt(context.device, gemm);
+ if (!hip_i8_gemm_unsupported(lt_status))
+ return lt_status == HIPBLAS_STATUS_SUCCESS && cuda_last_error() == cuda_success;
+ const auto lt_error = cuda_last_error();
+ if (lt_error != cuda_success && lt_error != hipErrorNotSupported)
+ return false;
+#endif
+ const auto status = gemm.run_hipblas();
+ const bool success = status == HIPBLAS_STATUS_SUCCESS;
+ const bool unsupported = hip_i8_gemm_unsupported(status);
+ constexpr auto runtime_unsupported = hipErrorNotSupported;
+#else
+ const int32_t alpha = 1, beta = 0;
+ const auto status = cublasGemmEx(static_cast<cublasHandle_t>(handle), CUBLAS_OP_T, CUBLAS_OP_N,
+ int(n), int(padded), int(k), &alpha,
+ weight, CUDA_R_8I, int(k), input, CUDA_R_8I, int(k),
+ &beta, output, CUDA_R_32I, int(n), CUBLAS_COMPUTE_32I, CUBLAS_GEMM_DEFAULT_TENSOR_OP);
+ const bool success = status == CUBLAS_STATUS_SUCCESS;
+ const bool unsupported = status == CUBLAS_STATUS_NOT_SUPPORTED || status == CUBLAS_STATUS_ARCH_MISMATCH;
+ constexpr auto runtime_unsupported = cudaErrorNotSupported;
+#endif
+ const auto error = cuda_last_error();
+ if (success)
+ return error == cuda_success;
+ return unsupported && (error == cuda_success || error == runtime_unsupported) &&
+ run_i8_dot(stream, weight, input, output, k, n, rows);
+ }
+#endif
+
static __global__ void decode_w4a8(const uint8_t* packed, const float* codebook, const uint8_t* scales, int8_t* decoded, int64_t groups) {
const int64_t stride = int64_t(blockDim.x) * gridDim.x;
for (int64_t g = int64_t(blockDim.x) * blockIdx.x + threadIdx.x; g < groups; g += stride) {
@@ -765,27 +815,26 @@ namespace SD_CUDA_NAMESPACE {
total += (bytes + 255) / 256 * 256;
return offset;
}
- static ggml_status prepare_impl(ggml_backend_t backend, ggml_tensor* const* nodes, int count, void** state, size_t* workspace, void*,
- const SDI8Operation* w4op) {
- const auto* dst = nodes[count - 1];
+ static ggml_status prepare_impl(ggml_backend_t backend, ggml_tensor* const* nodes, int count, void** state, size_t* workspace, void*, const SDI8Operation* w4op) {
+ const auto* dst = nodes[count - 1];
SDI8Operation op = {};
const bool mat = w4op ? true : sd_ggml_i8_describe(nodes[0], &op);
if (w4op)
op = *w4op;
- auto* p = new Plan{};
- *state = p;
- auto device = ggml_backend_get_device(backend);
- p->device = cuda_device_info(device);
- p->kind = !mat ? QUANTIZE : count == 3 ? (dst->op == GGML_OP_ADD ? GATED_RESIDUAL : SWIGLU_QUANTIZE)
- : count == 1 ? MAT
- : dst->op == GGML_OP_CONCAT ? CONCAT
- : GATED;
- p->k = mat ? op.k() : dst->ne[0];
- p->n = mat ? nodes[0]->ne[0] : 0;
- p->rows = mat ? ggml_nrows(nodes[0]) : ggml_nrows(dst->src[0]);
- p->padded = (p->rows + 3) / 4 * 4;
- p->group = mat ? op.group_size : 0;
- p->situ = (p->kind == GATED || p->kind == SWIGLU_QUANTIZE) && ggml_get_glu_op(nodes[1]) == GGML_GLU_OP_SITU;
+ auto* p = new Plan{};
+ *state = p;
+ auto device = ggml_backend_get_device(backend);
+ p->device = cuda_device_info(device);
+ p->kind = !mat ? QUANTIZE : count == 3 ? (dst->op == GGML_OP_ADD ? GATED_RESIDUAL : SWIGLU_QUANTIZE)
+ : count == 1 ? MAT
+ : dst->op == GGML_OP_CONCAT ? CONCAT
+ : GATED;
+ p->k = mat ? op.k() : dst->ne[0];
+ p->n = mat ? nodes[0]->ne[0] : 0;
+ p->rows = mat ? ggml_nrows(nodes[0]) : ggml_nrows(dst->src[0]);
+ p->padded = (p->rows + 3) / 4 * 4;
+ p->group = mat ? op.group_size : 0;
+ p->situ = (p->kind == GATED || p->kind == SWIGLU_QUANTIZE) && ggml_get_glu_op(nodes[1]) == GGML_GLU_OP_SITU;
if (!mat)
sd_ggml_convrot_describe(dst, &p->group);
size_t total = 0;
@@ -1056,18 +1105,17 @@ namespace SD_CUDA_NAMESPACE {
up, stride, stride, xs, ws, nullptr, 0.f, 1.f, 0.f, nullptr, nullptr);
return cuda_last_error();
}
- static ggml_status execute_impl(const ggml_ext_execution_context* execution, ggml_tensor* const* nodes, int count, void* state, void*,
- const SDI8Operation* w4op) {
+ static ggml_status execute_impl(const ggml_ext_execution_context* execution, ggml_tensor* const* nodes, int count, void* state, void*, const SDI8Operation* w4op) {
const auto& p = *static_cast<const Plan*>(state);
ggml_ext_gpu_context context;
if (!cuda_context(execution->backend, &context) || context.device != p.device.device)
return GGML_STATUS_FAILED;
- auto stream = static_cast<CUDAStream>(context.stream);
- auto* scratch = execution->workspace ? static_cast<char*>(ggml_backend_buffer_get_base(execution->workspace)) : nullptr;
- auto scratch_at = [scratch](size_t offset) { return scratch ? scratch + offset : nullptr; };
- auto* dst = nodes[count - 1];
- SDI8Operation op = {};
- const bool mat = w4op ? true : sd_ggml_i8_describe(nodes[0], &op);
+ auto stream = static_cast<CUDAStream>(context.stream);
+ auto* scratch = execution->workspace ? static_cast<char*>(ggml_backend_buffer_get_base(execution->workspace)) : nullptr;
+ auto scratch_at = [scratch](size_t offset) { return scratch ? scratch + offset : nullptr; };
+ auto* dst = nodes[count - 1];
+ SDI8Operation op = {};
+ const bool mat = w4op ? true : sd_ggml_i8_describe(nodes[0], &op);
if (w4op)
op = *w4op;
const auto* residual = p.kind == GATED_RESIDUAL ? residual_input(nodes[1], dst) : nullptr;
@@ -1136,13 +1184,6 @@ namespace SD_CUDA_NAMESPACE {
auto* gemm_output = reinterpret_cast<float*>(static_cast<char*>(output) + prefix);
auto* accum = reinterpret_cast<int32_t*>(scratch_at(p.accum));
const bool intermediate = p.kind == SWIGLU_QUANTIZE || (p.kind == GATED && !p.fused_activation);
- auto run_dot = [&]() {
- const dim3 grid(unsigned(std::min<int64_t>((p.n + 31) / 32, 65535)), unsigned(std::min<int64_t>((p.rows + 31) / 32, 65535)));
- mul_mat_i8_dot<<<grid, dim3(16, 16), 0, stream>>>(reinterpret_cast<const int*>(weight),
- reinterpret_cast<const int*>(quant), accum, p.k / 4, p.n, p.rows);
- return cuda_last_error() == cuda_success;
- };
- bool gemm_done = false;
#ifdef SD_CUDA_I8_CUTLASS
if (p.chunk_cols) {
auto* decoded = reinterpret_cast<int8_t*>(scratch_at(p.decoded_weight));
@@ -1157,52 +1198,22 @@ namespace SD_CUDA_NAMESPACE {
if (status != GGML_STATUS_SUCCESS)
return status;
}
- gemm_done = true;
} else if (p.gemm) {
auto status = sd_cuda_i8_gemm_execute(p.gemm, quant, weight, scales, ws,
intermediate ? reinterpret_cast<float*>(accum) : gemm_output, scratch_at(p.gemm_offset), stream, bias);
if (status != GGML_STATUS_SUCCESS)
return status;
- gemm_done = true;
- }
+ } else
#endif
- if (!gemm_done && !p.use_blas) {
- if (!run_dot())
- return GGML_STATUS_FAILED;
- gemm_done = true;
- }
#if !defined(SD_CUDA_VENDOR_MUSA)
- if (!gemm_done) {
- auto reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(execution->backend));
- auto blas_query = reinterpret_cast<ggml_ext_gpu_blas_fn>(ggml_backend_reg_get_proc_address(reg, "ggml_ext_gpu_blas_v1"));
- auto handle = blas_query ? blas_query(execution->backend) : nullptr;
- bool blas_ok = handle != nullptr;
-#if defined(SD_CUDA_VENDOR_HIP)
- if (blas_ok) {
- const HipI8Gemm gemm{int(p.n), int(p.padded), int(p.k), weight, quant, accum,
- static_cast<hipblasHandle_t>(handle), stream};
-#ifdef SD_HIP_I8_LT
- blas_ok = hip_i8_gemm_lt(context.device, gemm) || gemm.run_hipblas() == HIPBLAS_STATUS_SUCCESS;
-#else
- blas_ok = gemm.run_hipblas() == HIPBLAS_STATUS_SUCCESS;
-#endif
- }
-#else
- if (blas_ok) {
- const int32_t alpha = 1, beta = 0;
- blas_ok = cublasGemmEx(static_cast<cublasHandle_t>(handle), CUBLAS_OP_T, CUBLAS_OP_N,
- int(p.n), int(p.padded), int(p.k), &alpha,
- weight, CUDA_R_8I, int(p.k), quant, CUDA_R_8I, int(p.k),
- &beta, accum, CUDA_R_32I, int(p.n), CUBLAS_COMPUTE_32I, CUBLAS_GEMM_DEFAULT_TENSOR_OP) == CUBLAS_STATUS_SUCCESS;
- }
+ if (p.use_blas) {
+ if (!run_i8_blas(context, i8_blas_handle(execution->backend), weight, quant, accum, p.k, p.n, p.rows, p.padded))
+ return GGML_STATUS_FAILED;
+ } else
#endif
- if (!blas_ok) {
- (void)cuda_last_error();
- if (!run_dot())
- return GGML_STATUS_FAILED;
- }
+ if (!run_i8_dot(stream, weight, quant, accum, p.k, p.n, p.rows)) {
+ return GGML_STATUS_FAILED;
}
-#endif
const auto* values = reinterpret_cast<const float*>(accum);
if (p.kind == SWIGLU_QUANTIZE) {
if (p.fused_activation)
@@ -1343,9 +1354,7 @@ namespace SD_CUDA_NAMESPACE {
ggml_ext_gpu_context context;
if (!cuda_context(execution->backend, &context))
return GGML_STATUS_FAILED;
- auto reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(execution->backend));
- auto blas_query = reinterpret_cast<ggml_ext_gpu_blas_fn>(ggml_backend_reg_get_proc_address(reg, "ggml_ext_gpu_blas_v1"));
- auto handle = blas_query ? static_cast<hipblasHandle_t>(blas_query(execution->backend)) : nullptr;
+ auto handle = i8_blas_handle(execution->backend);
if (!handle)
return GGML_STATUS_FAILED;
@@ -1354,15 +1363,8 @@ namespace SD_CUDA_NAMESPACE {
auto* gate_accum = reinterpret_cast<int32_t*>(scratch);
auto* up_accum = gate_accum + p.n * p.padded;
const auto* input = static_cast<const int8_t*>(gate.input->data);
- auto gemm = [&](const SDI8Operation& op, int32_t* output) {
- const HipI8Gemm g{int(p.n), int(p.padded), int(p.k), op.weight->data, input, output, handle, stream};
-#ifdef SD_HIP_I8_LT
- if (hip_i8_gemm_lt(context.device, g))
- return true;
-#endif
- return g.run_hipblas() == HIPBLAS_STATUS_SUCCESS;
- };
- if (!gemm(gate, gate_accum) || !gemm(up, up_accum))
+ if (!run_i8_blas(context, handle, gate.weight->data, input, gate_accum, p.k, p.n, p.rows, p.padded) ||
+ !run_i8_blas(context, handle, up.weight->data, input, up_accum, p.k, p.n, p.rows, p.padded))
return GGML_STATUS_FAILED;
bool alias = false;