stable-diffusion.cpp/src/core/cpu/int8.cpp

266 lines
13 KiB
C++

#include "core/cpu/registration.h"
#include "core/ggml_extensions.h"
#ifndef SD_USE_UPSTREAM_GGML
#include <algorithm>
#include <cmath>
#include <cstring>
#include <limits>
namespace sd_cpu {
struct I8Execution {
SDI8Operation op;
int64_t rows;
int8_t* quant;
float* scales;
float* output;
};
static void quantize(ggml_tensor*, int ith, int nth, void* data) {
auto& e = *static_cast<I8Execution*>(data);
const int64_t k = e.op.input->ne[0];
const int group = e.op.group_size;
const auto* input = static_cast<const float*>(e.op.input->data);
for (int64_t row = ith; row < e.rows; row += nth) {
const float* src = input + row * k;
int8_t* dst = e.quant + row * k;
float amax = 0.f, values[256];
auto visit = [&](auto fn) { if (!group) { for (int64_t i = 0; i < k; ++i) fn(i, src[i]); }
else for (int64_t base = 0; base < k; base += group) { std::memcpy(values, src + base, group * sizeof(float)); sd_ggml_rotate_convrot(values, group, group); for (int j = 0; j < group; ++j) fn(base + j, values[j]); } };
visit([&](int64_t, float value) { amax = std::max(amax, std::fabs(value)); });
const float scale = amax / 127.f, inverse = scale == 0.f ? 0.f : 1.f / scale;
e.scales[row] = scale;
if (scale == 0.f)
std::memset(dst, 0, k);
else
visit([&](int64_t i, float value) { dst[i] = static_cast<int8_t>(std::max(-127L, std::min(127L, std::lrintf(value * inverse)))); });
}
}
static void multiply(ggml_tensor*, int ith, int nth, void* data) {
auto& e = *static_cast<I8Execution*>(data);
const auto& op = e.op;
const int64_t k = op.weight->ne[0], n = op.weight->ne[1];
const auto* w = static_cast<const int8_t*>(op.weight->data);
const auto* ws = op.weight_scale ? static_cast<const float*>(op.weight_scale->data) : nullptr;
const auto* bias = op.bias ? static_cast<const float*>(op.bias->data) : nullptr;
for (int64_t index = ith; index < e.rows * n; index += nth) {
const int64_t row = index / n, column = index % n;
uint32_t sum = 0;
for (int64_t i = 0; i < k; ++i)
sum += uint32_t(int32_t(w[column * k + i]) * int32_t(e.quant[row * k + i]));
int32_t signed_sum;
std::memcpy(&signed_sum, &sum, sizeof(sum));
float value = float(signed_sum) * e.scales[row];
if (ws)
value *= ws[column];
if (bias)
value += bias[column];
e.output[index] = value;
}
}
static bool overlaps(const ggml_tensor* dst, const ggml_tensor* src) {
if (!src)
return false;
const auto a = reinterpret_cast<uintptr_t>(dst->data), b = reinterpret_cast<uintptr_t>(src->data);
return a <= b ? b - a < ggml_nbytes(dst) : a - b < ggml_nbytes(src);
}
static size_t quant_workspace(const ggml_tensor* node, const SDI8Operation& op) {
return op.input->type == GGML_TYPE_F32 ? (size_t(ggml_nelements(op.input)) + 3) / 4 * 4 + size_t(ggml_nrows(node)) * 4 : 0;
}
static bool i8_supports(ggml_backend_dev_t, const ggml_tensor* node, void*) {
SDI8Operation op;
return sd_ggml_i8_describe(node, &op);
}
static bool convrot_supports(ggml_backend_dev_t, const ggml_tensor* node, void*) {
int group;
return sd_ggml_convrot_describe(node, &group);
}
static ggml_status i8_prepare(ggml_backend_t, ggml_tensor* const* nodes, int, void**, size_t* bytes, void*) {
SDI8Operation op;
if (!sd_ggml_i8_describe(nodes[0], &op))
return GGML_STATUS_FAILED;
*bytes = quant_workspace(nodes[0], op) + ggml_nbytes(nodes[0]);
return GGML_STATUS_SUCCESS;
}
static ggml_status convrot_prepare(ggml_backend_t, ggml_tensor* const* nodes, int, void**, size_t* bytes, void*) {
*bytes = ggml_nbytes(nodes[0]);
return GGML_STATUS_SUCCESS;
}
static void destroy(void*, void*) {}
static ggml_ext_cpu_parallel_fn parallel(ggml_backend_t backend) {
return reinterpret_cast<ggml_ext_cpu_parallel_fn>(ggml_backend_reg_get_proc_address(ggml_backend_dev_backend_reg(ggml_backend_get_device(backend)), "ggml_ext_cpu_parallel_v1"));
}
static ggml_status i8_execute(const ggml_ext_execution_context* context, ggml_tensor* const* nodes, int, void*, void*) {
auto* dst = nodes[0];
I8Execution e = {};
if (!sd_ggml_i8_describe(dst, &e.op))
return GGML_STATUS_FAILED;
e.rows = ggml_nrows(dst);
auto run = parallel(context->backend);
if (!run)
return GGML_STATUS_FAILED;
char* scratch = static_cast<char*>(ggml_backend_buffer_get_base(context->workspace));
const size_t quant_size = quant_workspace(dst, e.op);
if (e.op.input->type == GGML_TYPE_F32) {
e.quant = reinterpret_cast<int8_t*>(scratch);
e.scales = reinterpret_cast<float*>(scratch + (size_t(ggml_nelements(e.op.input)) + 3) / 4 * 4);
auto status = run(context->backend, dst, quantize, &e);
if (status != GGML_STATUS_SUCCESS)
return status;
} else {
e.quant = static_cast<int8_t*>(e.op.input->data);
e.scales = e.op.packed ? reinterpret_cast<float*>(e.quant + e.op.input->ne[0] * ((e.rows + 3) / 4 * 4)) : static_cast<float*>(e.op.input_scale->data);
}
bool alias = false;
for (auto* input : dst->src)
alias |= overlaps(dst, input);
e.output = alias ? reinterpret_cast<float*>(scratch + quant_size) : static_cast<float*>(dst->data);
auto status = run(context->backend, dst, multiply, &e);
if (status == GGML_STATUS_SUCCESS && alias)
std::memcpy(dst->data, e.output, ggml_nbytes(dst));
return status;
}
static ggml_status convrot_execute(const ggml_ext_execution_context* context, ggml_tensor* const* nodes, int, void*, void*) {
auto* dst = nodes[0];
I8Execution e = {};
if (!sd_ggml_convrot_describe(dst, &e.op.group_size))
return GGML_STATUS_FAILED;
auto run = parallel(context->backend);
if (!run)
return GGML_STATUS_FAILED;
e.op.input = dst->src[0];
e.rows = ggml_nrows(e.op.input);
bool alias = overlaps(dst, e.op.input);
e.quant = static_cast<int8_t*>(alias ? ggml_backend_buffer_get_base(context->workspace) : dst->data);
const int64_t k = dst->ne[0], padded = (e.rows + 3) / 4 * 4;
e.scales = reinterpret_cast<float*>(e.quant + k * padded);
auto status = run(context->backend, dst, quantize, &e);
if (status == GGML_STATUS_SUCCESS) {
std::memset(e.quant + k * e.rows, 0, k * (padded - e.rows));
if (alias)
std::memcpy(dst->data, e.quant, ggml_nbytes(dst));
}
return status;
}
struct LoRAMergeExecution {
SDI8LoRAMerge op;
float* merged;
SDI8LoRARowStats* stats;
float* scales;
int8_t* output;
};
static void merge_lora_rows(ggml_tensor*, int ith, int nth, void* data) {
auto& e = *static_cast<LoRAMergeExecution*>(data);
const int64_t k = e.op.weight->ne[0];
const auto* base = static_cast<const int8_t*>(e.op.weight->data);
const auto* old = static_cast<const float*>(e.op.scale->data);
const auto* diff = static_cast<const float*>(e.op.diff->data);
for (int64_t row = ith; row < e.op.weight->ne[1]; row += nth) {
const int64_t offset = row * k;
float* values = e.merged + offset;
std::memcpy(values, diff + offset, k * sizeof(float));
const bool changed = std::any_of(values, values + k, [](float value) { return value != 0.f; });
sd_ggml_rotate_convrot(values, k, e.op.group);
const float scale = old[e.op.scalar_scale() ? 0 : row];
float maximum = 0.f;
for (int64_t i = 0; i < k; ++i) {
const float value = values[i] + base[offset + i] * scale;
values[i] = value;
maximum = std::max(maximum, std::isfinite(value) ? std::fabs(value) : std::numeric_limits<float>::infinity());
}
e.stats[row] = {maximum, changed};
}
}
static void quantize_lora_rows(ggml_tensor*, int ith, int nth, void* data) {
auto& e = *static_cast<LoRAMergeExecution*>(data);
const int64_t k = e.op.weight->ne[0];
const auto* base = static_cast<const int8_t*>(e.op.weight->data);
const auto* old = static_cast<const float*>(e.op.scale->data);
for (int64_t row = ith; row < e.op.weight->ne[1]; row += nth) {
const int64_t offset = row * k;
if (!e.op.scalar_scale() && !e.stats[row].changed) {
e.scales[row] = old[row];
std::memcpy(e.output + offset, base + offset, k);
continue;
}
const float scale = e.op.scalar_scale() ? e.scales[0] : e.stats[row].maximum / 127.f;
if (!e.op.scalar_scale())
e.scales[row] = scale;
for (int64_t i = offset; i < offset + k; ++i)
e.output[i] = scale == 0.f ? 0 : static_cast<int8_t>(std::clamp(std::nearbyint(e.merged[i] / scale), -127.f, 127.f));
}
}
static ggml_status lora_prepare(ggml_backend_t, ggml_tensor* const* nodes, int, void**, size_t* bytes, void*) {
SDI8LoRAMerge op;
if (!sd_ggml_i8_lora_merge_describe(nodes[0], &op))
return GGML_STATUS_FAILED;
*bytes = op.workspace_size();
return GGML_STATUS_SUCCESS;
}
static ggml_status lora_execute(const ggml_ext_execution_context* context, ggml_tensor* const* nodes, int, void*, void*) {
auto* dst = nodes[0];
SDI8LoRAMerge op;
const auto run = parallel(context->backend);
if (!run || !sd_ggml_i8_lora_merge_describe(dst, &op))
return GGML_STATUS_FAILED;
auto* scratch = static_cast<char*>(ggml_backend_buffer_get_base(context->workspace));
auto* packed = static_cast<char*>(dst->data);
auto* flags = reinterpret_cast<uint32_t*>(packed);
LoRAMergeExecution e{
op, reinterpret_cast<float*>(scratch), reinterpret_cast<SDI8LoRARowStats*>(scratch + ggml_nbytes(op.diff)),
reinterpret_cast<float*>(packed + op.scale_offset()), reinterpret_cast<int8_t*>(packed + op.weight_offset())};
auto status = run(context->backend, dst, merge_lora_rows, &e);
if (status != GGML_STATUS_SUCCESS)
return status;
*flags = 0;
float maximum = 0.f;
for (int64_t row = 0; row < op.weight->ne[1]; ++row) {
const auto& stats = e.stats[row];
if (stats.changed)
*flags |= SD_I8_LORA_CHANGED;
if (stats.changed || op.scalar_scale())
maximum = std::max(maximum, stats.maximum);
}
if (*flags && !std::isfinite(maximum))
*flags |= SD_I8_LORA_NONFINITE;
if (*flags != SD_I8_LORA_CHANGED) {
std::memcpy(e.output, op.weight->data, ggml_nbytes(op.weight));
std::memcpy(e.scales, op.scale->data, ggml_nbytes(op.scale));
return GGML_STATUS_SUCCESS;
}
if (op.scalar_scale())
e.scales[0] = maximum / 127.f;
return run(context->backend, dst, quantize_lora_rows, &e);
}
void register_i8(ggml_backend_dev_t device) {
ggml_ext_kernel_desc d = {};
d.abi_version = GGML_EXT_ABI_VERSION;
d.priority = 100;
d.destroy = destroy;
d.name = "sd.linear_i8.cpu.v1";
d.op = GGML_OP_CUSTOM;
d.custom_op = sd_ggml_i8_op();
d.supports = i8_supports;
d.prepare = i8_prepare;
d.execute = i8_execute;
GGML_ASSERT(ggml_ext_register_kernel(device, &d));
d.name = "sd.quantize_i8_convrot.cpu.v1";
d.op = GGML_OP_CUSTOM;
d.custom_op = sd_ggml_convrot_op();
d.supports = convrot_supports;
d.prepare = convrot_prepare;
d.execute = convrot_execute;
GGML_ASSERT(ggml_ext_register_kernel(device, &d));
d.name = "sd.i8_lora_merge.cpu.v1";
d.custom_op = sd_ggml_i8_lora_merge_op();
d.supports = [](ggml_backend_dev_t, const ggml_tensor* node, void*) {
SDI8LoRAMerge op;
return sd_ggml_i8_lora_merge_describe(node, &op);
};
d.prepare = lora_prepare;
d.execute = lora_execute;
GGML_ASSERT(ggml_ext_register_kernel(device, &d));
}
}
#endif