mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 11:11:21 +00:00
feat: support Q4 and Q5 weights in INT8 linear layers (#2452)
Some checks are pending
CI / windows-latest-cmake (cuda12, -DSD_CUDA=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES='61;70;75;80;86;89;90;100;120' -DCMAKE_CUDA_FL… (push) Waiting to run
CI / windows-latest-cmake (vulkan, -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / windows-latest-rocm (push) Waiting to run
CI / ubuntu-latest-rocm (10.1.0, x64, gfx900;gfx906;gfx908;gfx90a;gfx90c;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Waiting to run
CI / ubuntu-latest-cmake (push) Waiting to run
CI / ubuntu-latest-cmake-vulkan (push) Waiting to run
CI / Build and push container images (push) Waiting to run
CI / macOS-latest-cmake (push) Waiting to run
CI / windows-latest-cmake (cpu, -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_LIBS=ON -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / release (push) Blocked by required conditions
Some checks are pending
CI / windows-latest-cmake (cuda12, -DSD_CUDA=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES='61;70;75;80;86;89;90;100;120' -DCMAKE_CUDA_FL… (push) Waiting to run
CI / windows-latest-cmake (vulkan, -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / windows-latest-rocm (push) Waiting to run
CI / ubuntu-latest-rocm (10.1.0, x64, gfx900;gfx906;gfx908;gfx90a;gfx90c;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Waiting to run
CI / ubuntu-latest-cmake (push) Waiting to run
CI / ubuntu-latest-cmake-vulkan (push) Waiting to run
CI / Build and push container images (push) Waiting to run
CI / macOS-latest-cmake (push) Waiting to run
CI / windows-latest-cmake (cpu, -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_LIBS=ON -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / release (push) Blocked by required conditions
This commit is contained in:
parent
3771ef249c
commit
d68f2883c9
8 changed files with 207 additions and 88 deletions
|
|
@ -166,12 +166,14 @@ these formats because they require the application extension API.
|
|||
|
||||
## Runtime requantization to INT8
|
||||
|
||||
Use `--linear-type i8` to run eligible Q8_0 linear layers through INT8 GEMM on
|
||||
CUDA, HIP, MUSA, and Vulkan. A bare mode applies to all models in the context,
|
||||
including diffusion models and text encoders loaded with `--llm`. The default
|
||||
is `auto`, which currently behaves like `raw`. With `--linear-type raw`, each
|
||||
layer uses its original execution path. Native INT8 weights use their INT8
|
||||
path in all three modes.
|
||||
Use `--linear-type i8` to run eligible Q4_0, Q4_1, Q5_0, Q5_1, and Q8_0 linear
|
||||
layers through INT8 GEMM on CUDA, HIP, MUSA, and Vulkan. A bare mode applies to
|
||||
all models in the context, including diffusion models and text encoders loaded
|
||||
with `--llm`. The default is `auto`, which currently behaves like `raw`. With
|
||||
`--linear-type raw`, each layer uses its original execution path. Native INT8
|
||||
weights use their INT8 path in all three modes. For floating-point checkpoints,
|
||||
use `--type q4_0` (or another supported weight type) to quantize eligible weights
|
||||
at load time.
|
||||
|
||||
Use module assignments to apply a mode selectively. For example,
|
||||
`--linear-type dit=i8` selects runtime conversion for the diffusion module.
|
||||
|
|
@ -189,14 +191,14 @@ dot products and compatible storage buffer sizes and alignment. The C API option
|
|||
use `sd_dgl_ctx_set(ctx, "linear_type", "dit=i8")` or pass
|
||||
`--linear-type dit=i8` when running the script from the CLI.
|
||||
|
||||
At runtime, sd.cpp dequantizes the Q8_0 blocks, rotates the weights, and
|
||||
At runtime, sd.cpp dequantizes the weight blocks, rotates the weights, and
|
||||
requantizes them to INT8 with one scale per output channel. It applies the
|
||||
same rotation to the activations before quantizing each input row. The
|
||||
converted matrix uses temporary GPU memory and feeds the standard INT8 kernels
|
||||
and their fusions. The checkpoint file stays in its original format.
|
||||
Requantization changes numerical results, and conversion overhead depends on
|
||||
matrix shape. Row-split models and unsupported layers use the standard Q8_0
|
||||
path.
|
||||
matrix shape. Row-split models and unsupported layers use their original
|
||||
execution paths.
|
||||
|
||||
## Example
|
||||
|
||||
|
|
|
|||
|
|
@ -10,40 +10,58 @@ namespace SD_CUDA_NAMESPACE {
|
|||
using i8::blas_supported;
|
||||
using i8::warp_reduce_max;
|
||||
|
||||
// Match the GGML Q8_0 block layout checked by requantize_q8_supports().
|
||||
struct Q8Block {
|
||||
static constexpr int size = 32;
|
||||
static constexpr int pairs = size / 2;
|
||||
half scale;
|
||||
uint16_t values[pairs];
|
||||
};
|
||||
static_assert(sizeof(Q8Block) == 34);
|
||||
constexpr int block_size = SD_REQUANTIZE_I8_BLOCK_SIZE;
|
||||
|
||||
static __device__ __forceinline__ int8_t requantize_q8_value(float value) {
|
||||
// GGML stores FP16 parameters, then the Q5 high bits, then the quants.
|
||||
template <ggml_type type>
|
||||
struct QuantBlock {
|
||||
static constexpr int bits = type == GGML_TYPE_Q8_0 ? 8 : (type == GGML_TYPE_Q5_0 || type == GGML_TYPE_Q5_1 ? 5 : 4);
|
||||
static constexpr bool affine = type == GGML_TYPE_Q4_1 || type == GGML_TYPE_Q5_1;
|
||||
static constexpr int high_words = bits == 5 ? 2 : 0;
|
||||
half params[affine ? 2 : 1];
|
||||
uint16_t payload[(bits == 8 ? block_size / 2 : block_size / 4) + high_words];
|
||||
|
||||
__device__ __forceinline__ float decode(int lane) const {
|
||||
const float scale = __half2float(params[0]);
|
||||
if constexpr (bits == 8) {
|
||||
const int8_t value = int8_t(payload[lane / 2] >> (8 * (lane % 2)));
|
||||
return float(value) * scale;
|
||||
} else {
|
||||
const int low_lane = lane % 16;
|
||||
const uint16_t packed = payload[high_words + low_lane / 2];
|
||||
int value = (packed >> (8 * (low_lane % 2) + 4 * (lane / 16))) & 15;
|
||||
if constexpr (bits == 5)
|
||||
value |= ((payload[lane / 16] >> low_lane) & 1) << 4;
|
||||
if constexpr (affine)
|
||||
return float(value) * scale + __half2float(params[1]);
|
||||
return float(value - (1 << (bits - 1))) * scale;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static __device__ __forceinline__ int8_t requantize_i8_value(float value) {
|
||||
return int8_t(__float2int_rn(fminf(127.f, fmaxf(-127.f, value))));
|
||||
}
|
||||
|
||||
static __device__ __forceinline__ void load_rotated_group(const Q8Block* blocks, int lane, float (&values)[8]) {
|
||||
template <ggml_type type>
|
||||
static __device__ __forceinline__ void load_rotated_group(const QuantBlock<type>* blocks, int lane, float (&values)[8]) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
const auto& block = blocks[j];
|
||||
const int8_t value = int8_t(block.values[lane / 2] >> (8 * (lane % 2)));
|
||||
values[j] = (float(value) * __half2float(block.scale)) * (1.0f / 16.0f);
|
||||
}
|
||||
for (int j = 0; j < 8; ++j)
|
||||
values[j] = blocks[j].decode(lane) * (1.0f / 16.0f);
|
||||
sd_convrot::rotate_h256(values, lane);
|
||||
}
|
||||
|
||||
constexpr int requantize_warps = 8;
|
||||
constexpr int blocks_per_group = 256 / Q8Block::size;
|
||||
constexpr int blocks_per_group = 256 / block_size;
|
||||
constexpr int blocks_per_round = requantize_warps * blocks_per_group;
|
||||
constexpr int weights_per_round = blocks_per_round * Q8Block::size;
|
||||
constexpr int weights_per_round = blocks_per_round * block_size;
|
||||
|
||||
template <int cached_groups>
|
||||
static __global__ void requantize_q8_weights(const Q8Block* weights, int8_t* decoded, float* scales, int64_t blocks_per_channel) {
|
||||
template <ggml_type type, int cached_groups>
|
||||
static __global__ void requantize_i8_weights(const QuantBlock<type>* weights, int8_t* decoded, float* scales, int64_t blocks_per_channel) {
|
||||
const int lane = threadIdx.x;
|
||||
const int warp = threadIdx.y;
|
||||
const auto* blocks = weights + int64_t(blockIdx.x) * blocks_per_channel;
|
||||
auto* output = decoded + int64_t(blockIdx.x) * blocks_per_channel * Q8Block::size;
|
||||
auto* output = decoded + int64_t(blockIdx.x) * blocks_per_channel * block_size;
|
||||
|
||||
__shared__ float maxima[requantize_warps];
|
||||
// Keep a bounded number of rotated groups in registers across the scale reduction.
|
||||
|
|
@ -98,46 +116,66 @@ namespace SD_CUDA_NAMESPACE {
|
|||
load_rotated_group(blocks + block, lane, values);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
const float value = cached_groups > 0 ? retained[slot][j] : values[j];
|
||||
output[(block + j) * Q8Block::size + lane] = requantize_q8_value(value * inverse);
|
||||
const float value = cached_groups > 0 ? retained[slot][j] : values[j];
|
||||
output[(block + j) * block_size + lane] = requantize_i8_value(value * inverse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <int cached_groups>
|
||||
static void launch_requantize(const Q8Block* weights, int8_t* values, float* scales, int64_t k, int64_t n, CUDAStream stream) {
|
||||
requantize_q8_weights<cached_groups><<<unsigned(n), dim3(Q8Block::size, requantize_warps), 0, stream>>>(
|
||||
weights, values, scales, k / Q8Block::size);
|
||||
}
|
||||
|
||||
static bool requantize_q8_supports(ggml_backend_dev_t device, const ggml_tensor* node, void*) {
|
||||
static bool requantize_i8_supports(ggml_backend_dev_t device, const ggml_tensor* node, void*) {
|
||||
const auto* weight = node->src[0];
|
||||
const int64_t k = weight->ne[0], n = weight->ne[1];
|
||||
const auto info = cuda_device_info(device);
|
||||
return info.device >= 0 && k <= INT_MAX / 16384 && n <= INT_MAX && aligned(weight, alignof(Q8Block)) &&
|
||||
ggml_blck_size(GGML_TYPE_Q8_0) == Q8Block::size && ggml_type_size(GGML_TYPE_Q8_0) == sizeof(Q8Block) &&
|
||||
return info.device >= 0 && k <= INT_MAX / 16384 && n <= INT_MAX && aligned(weight, alignof(uint16_t)) &&
|
||||
(cuda_kind != GGML_EXT_GPU_CUDA || blas_supported(device, info, n));
|
||||
}
|
||||
|
||||
static ggml_status requantize_q8_execute(const ggml_ext_execution_context* execution, ggml_tensor* const* nodes, int, void*, void*) {
|
||||
template <ggml_type type>
|
||||
static void launch_requantize(const ggml_tensor* weight, int8_t* values, CUDAStream stream) {
|
||||
using Block = QuantBlock<type>;
|
||||
static_assert(sizeof(Block) == sd_ggml_requantize_i8_block_bytes(type) && alignof(Block) == alignof(uint16_t));
|
||||
const int64_t k = weight->ne[0], n = weight->ne[1];
|
||||
auto* scales = reinterpret_cast<float*>(values + k * n);
|
||||
const auto* weights = static_cast<const Block*>(weight->data);
|
||||
const dim3 threads(block_size, requantize_warps);
|
||||
const int64_t blocks_per_channel = k / block_size;
|
||||
if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 2 * weights_per_round) {
|
||||
requantize_i8_weights<type, 2><<<unsigned(n), threads, 0, stream>>>(weights, values, scales, blocks_per_channel);
|
||||
} else if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 4 * weights_per_round) {
|
||||
requantize_i8_weights<type, 4><<<unsigned(n), threads, 0, stream>>>(weights, values, scales, blocks_per_channel);
|
||||
} else if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 8 * weights_per_round) {
|
||||
requantize_i8_weights<type, 8><<<unsigned(n), threads, 0, stream>>>(weights, values, scales, blocks_per_channel);
|
||||
} else {
|
||||
requantize_i8_weights<type, 0><<<unsigned(n), threads, 0, stream>>>(weights, values, scales, blocks_per_channel);
|
||||
}
|
||||
}
|
||||
|
||||
static ggml_status requantize_i8_execute(const ggml_ext_execution_context* execution, ggml_tensor* const* nodes, int, void*, void*) {
|
||||
ggml_ext_gpu_context context;
|
||||
if (!cuda_context(execution->backend, &context))
|
||||
return GGML_STATUS_FAILED;
|
||||
auto* dst = nodes[0];
|
||||
const auto* weight = dst->src[0];
|
||||
const int64_t k = weight->ne[0], n = weight->ne[1];
|
||||
auto* values = static_cast<int8_t*>(dst->data);
|
||||
auto* scales = reinterpret_cast<float*>(values + k * n);
|
||||
const auto* weights = static_cast<const Q8Block*>(weight->data);
|
||||
const auto stream = static_cast<CUDAStream>(context.stream);
|
||||
if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 2 * weights_per_round) {
|
||||
launch_requantize<2>(weights, values, scales, k, n, stream);
|
||||
} else if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 4 * weights_per_round) {
|
||||
launch_requantize<4>(weights, values, scales, k, n, stream);
|
||||
} else if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 8 * weights_per_round) {
|
||||
launch_requantize<8>(weights, values, scales, k, n, stream);
|
||||
} else {
|
||||
launch_requantize<0>(weights, values, scales, k, n, stream);
|
||||
auto* values = static_cast<int8_t*>(dst->data);
|
||||
const auto stream = static_cast<CUDAStream>(context.stream);
|
||||
switch (weight->type) {
|
||||
case GGML_TYPE_Q4_0:
|
||||
launch_requantize<GGML_TYPE_Q4_0>(weight, values, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q4_1:
|
||||
launch_requantize<GGML_TYPE_Q4_1>(weight, values, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q5_0:
|
||||
launch_requantize<GGML_TYPE_Q5_0>(weight, values, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q5_1:
|
||||
launch_requantize<GGML_TYPE_Q5_1>(weight, values, stream);
|
||||
break;
|
||||
case GGML_TYPE_Q8_0:
|
||||
launch_requantize<GGML_TYPE_Q8_0>(weight, values, stream);
|
||||
break;
|
||||
default:
|
||||
return GGML_STATUS_FAILED;
|
||||
}
|
||||
return cuda_last_error() == cuda_success ? GGML_STATUS_SUCCESS : GGML_STATUS_FAILED;
|
||||
}
|
||||
|
|
@ -147,10 +185,10 @@ namespace SD_CUDA_NAMESPACE {
|
|||
ggml_ext_kernel_desc desc = {};
|
||||
desc.abi_version = GGML_EXT_ABI_VERSION;
|
||||
desc.op = GGML_OP_CUSTOM;
|
||||
desc.name = "sd.requantize_q8." SD_CUDA_NAME ".v1";
|
||||
desc.custom_op = sd_ggml_requantize_q8_op();
|
||||
desc.supports = requantize_q8_supports;
|
||||
desc.execute = requantize_q8_execute;
|
||||
desc.name = "sd.requantize_i8." SD_CUDA_NAME ".v1";
|
||||
desc.custom_op = sd_ggml_requantize_i8_op();
|
||||
desc.supports = requantize_i8_supports;
|
||||
desc.execute = requantize_i8_execute;
|
||||
GGML_ASSERT(ggml_ext_register_kernel_ex(device, &desc, GGML_EXT_KERNEL_CAPTURE_SAFE | GGML_EXT_KERNEL_REPLAY_REUSABLE));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -217,7 +217,7 @@ ggml_tensor* ggml_ext_linear(GGMLRunnerContext* runner_ctx,
|
|||
const auto mul_mat = [&](ggml_tensor* input) {
|
||||
#ifndef SD_USE_UPSTREAM_GGML
|
||||
// The INT8 path accumulates in INT32 and rescales in FP32.
|
||||
if (runner_ctx->requantize_to_i8 && w->type == GGML_TYPE_Q8_0 && input->type == GGML_TYPE_F32 &&
|
||||
if (runner_ctx->requantize_to_i8 && sd_ggml_requantize_i8_block_bytes(w->type) && input->type == GGML_TYPE_F32 &&
|
||||
(sd_backend_is(runner_ctx->backend, "CUDA") ||
|
||||
sd_backend_is(runner_ctx->backend, "ROCm") ||
|
||||
sd_backend_is(runner_ctx->backend, "MUSA") ||
|
||||
|
|
@ -227,7 +227,7 @@ ggml_tensor* ggml_ext_linear(GGMLRunnerContext* runner_ctx,
|
|||
if (quantized && ggml_backend_supports_op(runner_ctx->backend, quantized)) {
|
||||
// Make the conversion depend on the activation so decoded weights only live near their GEMM.
|
||||
ggml_tensor* inputs[] = {w, quantized};
|
||||
auto* converted = ggml_ext_op_create(ctx, sd_ggml_requantize_q8_op(), inputs, 2, nullptr);
|
||||
auto* converted = ggml_ext_op_create(ctx, sd_ggml_requantize_i8_op(), inputs, 2, nullptr);
|
||||
if (converted && ggml_backend_supports_op(runner_ctx->backend, converted)) {
|
||||
const int64_t k = w->ne[0], n = w->ne[1];
|
||||
auto* weight = ggml_view_2d(ctx, converted, k, n, k, 0);
|
||||
|
|
|
|||
|
|
@ -252,10 +252,13 @@ static bool i8_infer(ggml_tensor* const* inputs, int count, const void* attribut
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool requantize_q8_infer(ggml_tensor* const* inputs, int count, const void*, ggml_ext_tensor_desc* output) {
|
||||
if (count != 2 || !inputs[0] || !inputs[1] || inputs[0]->type != GGML_TYPE_Q8_0 || inputs[1]->type != GGML_TYPE_I8)
|
||||
static bool requantize_i8_infer(ggml_tensor* const* inputs, int count, const void*, ggml_ext_tensor_desc* output) {
|
||||
if (count != 2 || !inputs[0] || !inputs[1] || inputs[1]->type != GGML_TYPE_I8)
|
||||
return false;
|
||||
const auto* weight = inputs[0];
|
||||
const size_t block_bytes = sd_ggml_requantize_i8_block_bytes(weight->type);
|
||||
if (!block_bytes || ggml_blck_size(weight->type) != SD_REQUANTIZE_I8_BLOCK_SIZE || ggml_type_size(weight->type) != block_bytes)
|
||||
return false;
|
||||
const auto* weight = inputs[0];
|
||||
const int64_t k = weight->ne[0], n = weight->ne[1];
|
||||
constexpr int64_t scale_bytes = sizeof(float);
|
||||
int group;
|
||||
|
|
@ -290,9 +293,9 @@ ggml_ext_op_t sd_ggml_i8_op() {
|
|||
return op;
|
||||
}
|
||||
|
||||
ggml_ext_op_t sd_ggml_requantize_q8_op() {
|
||||
ggml_ext_op_t sd_ggml_requantize_i8_op() {
|
||||
static const auto op = [] {
|
||||
const ggml_ext_op_desc desc = {GGML_EXT_ABI_VERSION, "sd.requantize_q8.v1", 0, requantize_q8_infer};
|
||||
const ggml_ext_op_desc desc = {GGML_EXT_ABI_VERSION, "sd.requantize_i8.v1", 0, requantize_i8_infer};
|
||||
const auto result = ggml_ext_register_op(&desc);
|
||||
GGML_ASSERT(result);
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,26 @@ struct SDI8Operation {
|
|||
bool sd_ggml_valid_convrot_group(int group);
|
||||
|
||||
ggml_ext_op_t sd_ggml_i8_op();
|
||||
ggml_ext_op_t sd_ggml_requantize_q8_op();
|
||||
ggml_ext_op_t sd_ggml_requantize_i8_op();
|
||||
|
||||
constexpr int SD_REQUANTIZE_I8_BLOCK_SIZE = 32;
|
||||
|
||||
constexpr size_t sd_ggml_requantize_i8_block_bytes(ggml_type type) {
|
||||
switch (type) {
|
||||
case GGML_TYPE_Q4_0:
|
||||
return 18;
|
||||
case GGML_TYPE_Q4_1:
|
||||
return 20;
|
||||
case GGML_TYPE_Q5_0:
|
||||
return 22;
|
||||
case GGML_TYPE_Q5_1:
|
||||
return 24;
|
||||
case GGML_TYPE_Q8_0:
|
||||
return 34;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ggml_ext_op_t sd_ggml_convrot_op();
|
||||
|
||||
|
|
|
|||
|
|
@ -55,6 +55,10 @@ sd_vulkan_shader(rms_rope_cast rms_rope_apply ON -DCAST_F16)
|
|||
sd_vulkan_shader(quantize_i8_convrot quantize_i8_convrot ${SD_VK_DOT} -O)
|
||||
sd_vulkan_shader(quantize_i8_convrot_h64 quantize_i8_convrot_h64 ${SD_VK_DOT} -O -DGROUP_SIZE=64)
|
||||
sd_vulkan_shader(quantize_i8_rowwise pack_i8_rows ${SD_VK_DOT} -O)
|
||||
sd_vulkan_shader(requantize_q4_0 pack_i8_rows ${SD_VK_DOT} -O -DQUANT_BITS=4 -DQUANT_AFFINE=0)
|
||||
sd_vulkan_shader(requantize_q4_1 pack_i8_rows ${SD_VK_DOT} -O -DQUANT_BITS=4 -DQUANT_AFFINE=1)
|
||||
sd_vulkan_shader(requantize_q5_0 pack_i8_rows ${SD_VK_DOT} -O -DQUANT_BITS=5 -DQUANT_AFFINE=0)
|
||||
sd_vulkan_shader(requantize_q5_1 pack_i8_rows ${SD_VK_DOT} -O -DQUANT_BITS=5 -DQUANT_AFFINE=1)
|
||||
sd_vulkan_shader(requantize_q8 pack_i8_rows ${SD_VK_DOT} -O -DQ8_0=1)
|
||||
sd_vulkan_shader(quantize_i8_rowwise_subgroup pack_i8_rows ${SD_VK_DOT} -O -DSUBGROUP_MAX=1)
|
||||
sd_vulkan_shader(requantize_q8_subgroup pack_i8_rows ${SD_VK_DOT} -O -DQ8_0=1 -DQ8_HALFWORDS=1 -DSUBGROUP_MAX=1)
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@
|
|||
#include "quantize_i8_convrot_subgroup.h"
|
||||
#include "quantize_i8_rowwise.h"
|
||||
#include "quantize_i8_rowwise_subgroup.h"
|
||||
#include "requantize_q4_0.h"
|
||||
#include "requantize_q4_1.h"
|
||||
#include "requantize_q5_0.h"
|
||||
#include "requantize_q5_1.h"
|
||||
#include "requantize_q8.h"
|
||||
#include "requantize_q8_register.h"
|
||||
#include "requantize_q8_subgroup.h"
|
||||
|
|
@ -549,21 +553,43 @@ namespace sd_vulkan_i8 {
|
|||
}
|
||||
static ggml_status pack_rows(const ggml_ext_execution_context* e, const Device& d, const ggml_tensor* output) {
|
||||
const auto* input = output->src[0];
|
||||
const bool requantize = ggml_is_quantized(input->type);
|
||||
const bool q8 = input->type == GGML_TYPE_Q8_0;
|
||||
const uint32_t words = uint32_t(input->ne[0] / 4);
|
||||
const uint32_t rows = uint32_t(ggml_nrows(input));
|
||||
const PackParameters pc = {words, rows, words * (q8 ? rows : GGML_PAD(rows, 4))};
|
||||
const PackParameters pc = {words, rows, words * (requantize ? rows : GGML_PAD(rows, 4))};
|
||||
ggml_ext_vulkan_binding dst;
|
||||
bool alias;
|
||||
if (!output_binding(e, d, output, {{input}}, dst, alias))
|
||||
return GGML_STATUS_FAILED;
|
||||
const ggml_ext_vulkan_binding bindings[] = {{input}, dst};
|
||||
const uint32_t groups[] = {std::min(rows, d.info.max_group_count[0]), 1, 1};
|
||||
auto pipeline = q8 ? SD_VK_PIPELINE(requantize_q8, 2, PackParameters) : SD_VK_PIPELINE(quantize_i8_rowwise, 2, PackParameters);
|
||||
constexpr uint32_t words_per_round = 8 * 64;
|
||||
const uint32_t cached_groups = words <= 2 * words_per_round ? 2 : words <= 4 * words_per_round ? 4
|
||||
: 8;
|
||||
if (d.subgroup && (!q8 || d.info.float16)) {
|
||||
auto pipeline = SD_VK_PIPELINE(quantize_i8_rowwise, 2, PackParameters);
|
||||
switch (input->type) {
|
||||
case GGML_TYPE_Q4_0:
|
||||
pipeline = SD_VK_PIPELINE(requantize_q4_0, 2, PackParameters);
|
||||
break;
|
||||
case GGML_TYPE_Q4_1:
|
||||
pipeline = SD_VK_PIPELINE(requantize_q4_1, 2, PackParameters);
|
||||
break;
|
||||
case GGML_TYPE_Q5_0:
|
||||
pipeline = SD_VK_PIPELINE(requantize_q5_0, 2, PackParameters);
|
||||
break;
|
||||
case GGML_TYPE_Q5_1:
|
||||
pipeline = SD_VK_PIPELINE(requantize_q5_1, 2, PackParameters);
|
||||
break;
|
||||
case GGML_TYPE_Q8_0:
|
||||
pipeline = SD_VK_PIPELINE(requantize_q8, 2, PackParameters);
|
||||
break;
|
||||
case GGML_TYPE_F32:
|
||||
break;
|
||||
default:
|
||||
return GGML_STATUS_FAILED;
|
||||
}
|
||||
constexpr uint32_t words_per_round = 8 * 64;
|
||||
const uint32_t cached_groups = words <= 2 * words_per_round ? 2 : words <= 4 * words_per_round ? 4
|
||||
: 8;
|
||||
if (d.subgroup && (!requantize || (q8 && d.info.float16))) {
|
||||
if (q8 && words <= cached_groups * words_per_round && d.info.max_group_size[0] >= 256) {
|
||||
pipeline = SD_VK_PIPELINE(requantize_q8_register, 2, PackParameters);
|
||||
pipeline.constants = &cached_groups;
|
||||
|
|
@ -862,7 +888,7 @@ namespace sd_vulkan_i8 {
|
|||
static bool can_pack_rows(const Device& d, const ggml_tensor* n) {
|
||||
constexpr uint32_t threads = 128;
|
||||
const auto* input = n->src[0];
|
||||
const size_t alignment = input->type == GGML_TYPE_Q8_0 ? sizeof(uint32_t) : 4 * sizeof(float);
|
||||
const size_t alignment = ggml_is_quantized(input->type) ? sizeof(uint32_t) : 4 * sizeof(float);
|
||||
return bounded(d, n) && bounded(d, input) &&
|
||||
aligned(n, sizeof(uint32_t)) && aligned(input, alignment) &&
|
||||
ggml_nbytes(n) <= UINT32_MAX && ggml_nbytes(input) <= UINT32_MAX &&
|
||||
|
|
@ -875,7 +901,6 @@ namespace sd_vulkan_i8 {
|
|||
constexpr uint32_t rotation_bytes = 2 * 128 * 4 * sizeof(float);
|
||||
constexpr uint32_t reduction_bytes = 128 * sizeof(float);
|
||||
return k % 256 == 0 && k <= INT_MAX / 16384 &&
|
||||
ggml_blck_size(GGML_TYPE_Q8_0) == 32 && ggml_type_size(GGML_TYPE_Q8_0) == 34 &&
|
||||
device_info(dev, d) && d.compute.shared_memory >= rotation_bytes + reduction_bytes && can_pack_rows(d, n);
|
||||
}
|
||||
static bool quant1_supports(ggml_backend_dev_t dev, const ggml_tensor* n, void*) {
|
||||
|
|
@ -995,7 +1020,7 @@ namespace sd_vulkan_i8 {
|
|||
return GGML_STATUS_FAILED;
|
||||
if (ggml_ext_op_get(nodes[0]) == sd_ggml_w4_op())
|
||||
return multiply_w4(e, d, nodes[0]);
|
||||
if (ggml_ext_op_get(nodes[0]) == sd_ggml_requantize_q8_op())
|
||||
if (ggml_ext_op_get(nodes[0]) == sd_ggml_requantize_i8_op())
|
||||
return pack_rows(e, d, nodes[0]);
|
||||
int group;
|
||||
if (sd_ggml_convrot_describe(nodes[count - 1], &group))
|
||||
|
|
@ -1063,8 +1088,8 @@ void sd_ggml_register_vulkan_i8(ggml_backend_dev_t device) {
|
|||
d.prepare = prepare;
|
||||
d.destroy = destroy;
|
||||
d.execute = execute;
|
||||
d.name = "sd.requantize_q8.vulkan.v1";
|
||||
d.custom_op = sd_ggml_requantize_q8_op();
|
||||
d.name = "sd.requantize_i8.vulkan.v1";
|
||||
d.custom_op = sd_ggml_requantize_i8_op();
|
||||
d.supports = requantize_supports;
|
||||
GGML_ASSERT(ggml_ext_register_kernel(device, &d));
|
||||
d.name = "sd.quantize_i8_convrot.vulkan.v1";
|
||||
|
|
|
|||
|
|
@ -24,7 +24,11 @@ layout(push_constant) uniform Parameters {
|
|||
}
|
||||
p;
|
||||
|
||||
#ifdef Q8_0
|
||||
#if defined(Q8_0) || defined(QUANT_BITS)
|
||||
#define ROTATE_WEIGHTS
|
||||
#endif
|
||||
|
||||
#ifdef ROTATE_WEIGHTS
|
||||
layout(binding = 0, std430) readonly buffer Source {
|
||||
#ifdef Q8_HALFWORDS
|
||||
uint16_t source_halfwords[];
|
||||
|
|
@ -48,6 +52,14 @@ shared float maxima[gl_WorkGroupSize.x / 32];
|
|||
shared float maxima[gl_WorkGroupSize.x];
|
||||
#endif
|
||||
|
||||
#if defined(ROTATE_WEIGHTS) && !defined(Q8_HALFWORDS)
|
||||
uint load_u32(uint byte_offset) {
|
||||
const uint index = byte_offset / 4;
|
||||
const uint word = source_words[index];
|
||||
return (byte_offset & 2) == 0 ? word : (word >> 16) | (source_words[index + 1] << 16);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef Q8_0
|
||||
ivec4 load_q8(const uint row, const uint word, out float scale) {
|
||||
// Each GGML block stores a half scale followed by 32 signed bytes.
|
||||
|
|
@ -60,11 +72,7 @@ ivec4 load_q8(const uint row, const uint word, out float scale) {
|
|||
#else
|
||||
const uint block_byte = block * 34;
|
||||
const uint scale_bits = source_words[block_byte / 4] >> ((block_byte & 2) * 8);
|
||||
const uint value_byte = block_byte + 2 + (word % 8) * 4;
|
||||
uint values = source_words[value_byte / 4];
|
||||
if ((value_byte & 2) != 0) {
|
||||
values = (values >> 16) | (source_words[value_byte / 4 + 1] << 16);
|
||||
}
|
||||
const uint values = load_u32(block_byte + 2 + (word % 8) * 4);
|
||||
#endif
|
||||
scale = unpackHalf2x16(scale_bits).x;
|
||||
const int bytes = int(values);
|
||||
|
|
@ -78,12 +86,32 @@ vec4 load_values(const uint row, const uint word) {
|
|||
float scale;
|
||||
const ivec4 values = load_q8(row, word, scale);
|
||||
return scale * vec4(values);
|
||||
#elif defined(QUANT_BITS)
|
||||
const uint param_bytes = QUANT_AFFINE == 1 ? 4u : 2u;
|
||||
const uint high_bytes = QUANT_BITS == 5 ? 4u : 0u;
|
||||
const uint block_bytes = param_bytes + high_bytes + 16u;
|
||||
const uint block_byte = (row * (p.words_per_row / 8) + word / 8) * block_bytes;
|
||||
const uint block_word = word % 8;
|
||||
const vec2 params = unpackHalf2x16(source_words[block_byte / 4] >> ((block_byte & 2) * 8));
|
||||
// The two halves of the block share the same 16 bytes of low nibbles.
|
||||
const uint low = load_u32(block_byte + param_bytes + high_bytes + (block_word % 4) * 4) >> (4 * (block_word / 4));
|
||||
const uvec4 lanes = uvec4(0, 1, 2, 3);
|
||||
uvec4 values = (uvec4(low) >> (lanes * 8)) & 15u;
|
||||
#if QUANT_BITS == 5
|
||||
const uint high = load_u32(block_byte + param_bytes) >> (block_word * 4);
|
||||
values |= ((uvec4(high) >> lanes) & 1u) << 4;
|
||||
#endif
|
||||
#if QUANT_AFFINE == 1
|
||||
return params.x * vec4(values) + params.y;
|
||||
#else
|
||||
return params.x * vec4(ivec4(values) - (1 << (QUANT_BITS - 1)));
|
||||
#endif
|
||||
#else
|
||||
return source_values[row * p.words_per_row + word];
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef Q8_0
|
||||
#ifdef ROTATE_WEIGHTS
|
||||
vec4 rotate_word(vec4 x) {
|
||||
return vec4(x.x + x.y + x.z - x.w, x.x + x.y - x.z + x.w,
|
||||
x.x - x.y + x.z + x.w, -x.x + x.y + x.z + x.w);
|
||||
|
|
@ -124,7 +152,7 @@ void rotate_subgroup(uint row, uint word, out vec4 lower, out vec4 upper) {
|
|||
lower = rotate_four(a, b, c, d, lane / 16);
|
||||
upper = rotate_four(a, b, c, d, 2 + lane / 16);
|
||||
}
|
||||
#elif defined(Q8_0)
|
||||
#elif defined(ROTATE_WEIGHTS)
|
||||
shared vec4 rotation[2][gl_WorkGroupSize.x];
|
||||
|
||||
vec4 rotate_group(const uint row, const uint word) {
|
||||
|
|
@ -168,7 +196,7 @@ void pack_row(const uint row) {
|
|||
const vec4 peak = max(abs(lower[slot]), abs(upper[slot]));
|
||||
maximum = max(maximum, max(max(peak.x, peak.y), max(peak.z, peak.w)));
|
||||
}
|
||||
#elif defined(Q8_0)
|
||||
#elif defined(ROTATE_WEIGHTS)
|
||||
for (uint first = 0; first < p.words_per_row; first += gl_WorkGroupSize.x) {
|
||||
const vec4 values = abs(rotate_group(row, first + tid));
|
||||
maximum = max(maximum, max(max(values.x, values.y), max(values.z, values.w)));
|
||||
|
|
@ -207,7 +235,7 @@ void pack_row(const uint row) {
|
|||
if (tid == 0) {
|
||||
packed[p.scale_offset + row] = floatBitsToUint(scale);
|
||||
}
|
||||
#ifdef Q8_0
|
||||
#ifdef ROTATE_WEIGHTS
|
||||
const float inverse = maxima[0] == 0.0 ? 0.0 : 127.0 / maxima[0];
|
||||
#else
|
||||
const float inverse = scale == 0.0 ? 0.0 : 1.0 / scale;
|
||||
|
|
@ -223,7 +251,7 @@ void pack_row(const uint row) {
|
|||
pack_word(row, word + 32, upper[slot], inverse);
|
||||
}
|
||||
#else
|
||||
#ifdef Q8_0
|
||||
#ifdef ROTATE_WEIGHTS
|
||||
for (uint first = 0; first < p.words_per_row; first += gl_WorkGroupSize.x) {
|
||||
const uint word = first + tid;
|
||||
const vec4 values = rotate_group(row, word);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue