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
20 changes: 11 additions & 9 deletions docs/guides/int8_convrot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
132 changes: 85 additions & 47 deletions src/core/cuda/requantize.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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];
constexpr int block_size = SD_REQUANTIZE_I8_BLOCK_SIZE;

// 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_assert(sizeof(Q8Block) == 34);

static __device__ __forceinline__ int8_t requantize_q8_value(float value) {
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.
Expand Down Expand Up @@ -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*) {
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];
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* 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);
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) {
launch_requantize<2>(weights, values, scales, k, n, stream);
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) {
launch_requantize<4>(weights, values, scales, k, n, stream);
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) {
launch_requantize<8>(weights, values, scales, k, n, stream);
requantize_i8_weights<type, 8><<<unsigned(n), threads, 0, stream>>>(weights, values, scales, blocks_per_channel);
} else {
launch_requantize<0>(weights, values, scales, k, n, stream);
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];
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;
}
Expand All @@ -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));
}
}
4 changes: 2 additions & 2 deletions src/core/ggml_extend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand All @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions src/core/ggml_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion src/core/ggml_extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions src/core/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading