Skip to content
Closed
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
146 changes: 92 additions & 54 deletions src/ggml-cuda/ggml-cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ typedef void (*ggml_cuda_op_mul_mat_t)(
const int64_t src1_padded_row_size, cudaStream_t stream);


#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
#if !defined(GGML_USE_MUSA)
__global__ void quantize_rowwise_i8_cuda(const float * src, int8_t * dst, float * scales, int64_t k, int64_t rows) {
const int64_t row = blockIdx.x;
const int tid = threadIdx.x;
Expand Down Expand Up @@ -1377,12 +1377,14 @@ __global__ void quantize_rowwise_i8_cuda(const float * src, int8_t * dst, float
}
}

template <int group_size>
__global__ void quantize_rowwise_i8_convrot_cuda(
const float * src, int8_t * dst, float * scales,
int64_t k, int64_t rows, int64_t dst_stride) {
constexpr int group_size = 256;
constexpr int group_threads = group_size / 4;
constexpr int groups_per_wave = 1024 / group_threads;
// H4 carries a 1/2 normalization, applied log4(group_size) times.
constexpr float hadamard_norm = group_size == 256 ? (1.0f / 16.0f) : (1.0f / 8.0f);

const int64_t row = blockIdx.x;
const int tid = threadIdx.x;
Expand All @@ -1396,7 +1398,7 @@ __global__ void quantize_rowwise_i8_convrot_cuda(
int8_t * dst_row = dst + row * dst_stride;

for (int64_t i = tid; i < k; i += blockDim.x) {
values[i] = src_row[i] * (1.0f / 16.0f);
values[i] = src_row[i] * hadamard_norm;
}
__syncthreads();

Expand Down Expand Up @@ -1466,27 +1468,32 @@ __global__ void quantize_rowwise_i8_convrot_cuda(
}
}

template <int group_size>
__global__ void convrot_group_amax_i8_cuda(
const float * src, float * partial_max, int64_t k, int64_t groups, int64_t total_groups) {
constexpr int group_threads = group_size / 4;
// H4 carries a 1/2 normalization, applied log4(group_size) times.
constexpr float hadamard_norm = group_size == 256 ? (1.0f / 16.0f) : (1.0f / 8.0f);

const int64_t index = blockIdx.x;
const int tid = threadIdx.x;
if (index >= total_groups) {
return;
}

__shared__ float values[256];
__shared__ float maxima[2];
__shared__ float values[group_size];
__shared__ float maxima[group_threads];
const int64_t row = index / groups;
const int64_t group = index - row * groups;
const float * input = src + row * k + group * 256;
values[tid] = input[tid] * (1.0f / 16.0f);
values[tid + 64] = input[tid + 64] * (1.0f / 16.0f);
values[tid + 128] = input[tid + 128] * (1.0f / 16.0f);
values[tid + 192] = input[tid + 192] * (1.0f / 16.0f);
const float * input = src + row * k + group * group_size;
#pragma unroll
for (int j = 0; j < 4; ++j) {
values[tid + j * group_threads] = input[tid + j * group_threads] * hadamard_norm;
}
__syncthreads();

#pragma unroll
for (int stride = 1; stride < 256; stride *= 4) {
for (int stride = 1; stride < group_size; stride *= 4) {
const int base = (tid / stride) * 4 * stride + tid % stride;
const int i0 = base;
const int i1 = i0 + stride;
Expand All @@ -1503,18 +1510,21 @@ __global__ void convrot_group_amax_i8_cuda(
__syncthreads();
}

float value = fmaxf(fabsf(values[tid]), fabsf(values[tid + 64]));
value = fmaxf(value, fabsf(values[tid + 128]));
value = fmaxf(value, fabsf(values[tid + 192]));
for (int offset = 16; offset > 0; offset >>= 1) {
value = fmaxf(value, __shfl_down_sync(0xffffffff, value, offset));
}
if ((tid & 31) == 0) {
maxima[tid >> 5] = value;
float value = 0.0f;
#pragma unroll
for (int j = 0; j < 4; ++j) {
value = fmaxf(value, fabsf(values[tid + j * group_threads]));
}
maxima[tid] = value;
__syncthreads();
for (int stride = group_threads / 2; stride > 0; stride >>= 1) {
if (tid < stride) {
maxima[tid] = fmaxf(maxima[tid], maxima[tid + stride]);
}
__syncthreads();
}
if (tid == 0) {
partial_max[index] = fmaxf(maxima[0], maxima[1]);
partial_max[index] = maxima[0];
}
}

Expand Down Expand Up @@ -1544,28 +1554,33 @@ __global__ void reduce_convrot_row_amax_i8_cuda(
}
}

template <int group_size>
__global__ void convrot_group_quantize_i8_cuda(
const float * src, int8_t * dst, const float * scales,
int64_t k, int64_t groups, int64_t total_groups, int64_t dst_stride) {
constexpr int group_threads = group_size / 4;
// H4 carries a 1/2 normalization, applied log4(group_size) times.
constexpr float hadamard_norm = group_size == 256 ? (1.0f / 16.0f) : (1.0f / 8.0f);

const int64_t index = blockIdx.x;
const int tid = threadIdx.x;
if (index >= total_groups) {
return;
}

__shared__ float values[256];
__shared__ float values[group_size];
const int64_t row = index / groups;
const int64_t group = index - row * groups;
const float * input = src + row * k + group * 256;
int8_t * output = dst + row * dst_stride + group * 256;
values[tid] = input[tid] * (1.0f / 16.0f);
values[tid + 64] = input[tid + 64] * (1.0f / 16.0f);
values[tid + 128] = input[tid + 128] * (1.0f / 16.0f);
values[tid + 192] = input[tid + 192] * (1.0f / 16.0f);
const float * input = src + row * k + group * group_size;
int8_t * output = dst + row * dst_stride + group * group_size;
#pragma unroll
for (int j = 0; j < 4; ++j) {
values[tid + j * group_threads] = input[tid + j * group_threads] * hadamard_norm;
}
__syncthreads();

#pragma unroll
for (int stride = 1; stride < 256; stride *= 4) {
for (int stride = 1; stride < group_size; stride *= 4) {
const int base = (tid / stride) * 4 * stride + tid % stride;
const int i0 = base;
const int i1 = i0 + stride;
Expand All @@ -1584,12 +1599,12 @@ __global__ void convrot_group_quantize_i8_cuda(

const float row_scale = scales[row];
const float inv_scale = row_scale == 0.0f ? 0.0f : 1.0f / row_scale;
const int indices[4] = { tid, tid + 64, tid + 128, tid + 192 };
#pragma unroll
for (int i = 0; i < 4; ++i) {
int value = row_scale == 0.0f ? 0 : __float2int_rn(values[indices[i]] * inv_scale);
for (int j = 0; j < 4; ++j) {
const int element = tid + j * group_threads;
int value = row_scale == 0.0f ? 0 : __float2int_rn(values[element] * inv_scale);
value = max(-127, min(127, value));
output[indices[i]] = (int8_t)value;
output[element] = (int8_t)value;
}
if (group == 0 && tid == 0 && dst_stride != k) {
*((float *)(dst + row * dst_stride + k)) = row_scale;
Expand Down Expand Up @@ -1623,7 +1638,8 @@ static void ggml_cuda_quantize_i8_convrot(
int64_t k,
int64_t rows,
int64_t rows_padded,
int64_t dst_stride) {
int64_t dst_stride,
int group_size) {
cudaStream_t stream = ctx.stream();
if (rows_padded > rows) {
CUDA_CHECK(cudaMemsetAsync(
Expand All @@ -1633,26 +1649,43 @@ static void ggml_cuda_quantize_i8_convrot(
const size_t nbytes_shared = (size_t)k * sizeof(float);
const size_t smpbo = ggml_cuda_info().devices[ggml_cuda_get_device()].smpbo;
if (nbytes_shared + 128 <= smpbo) {
CUDA_SET_SHARED_MEMORY_LIMIT(quantize_rowwise_i8_convrot_cuda, smpbo - 128);
quantize_rowwise_i8_convrot_cuda<<<rows, 1024, nbytes_shared, stream>>>(
src, dst, scales, k, rows, dst_stride);
if (group_size == 64) {
CUDA_SET_SHARED_MEMORY_LIMIT(quantize_rowwise_i8_convrot_cuda<64>, smpbo - 128);
quantize_rowwise_i8_convrot_cuda<64><<<rows, 1024, nbytes_shared, stream>>>(
src, dst, scales, k, rows, dst_stride);
} else {
CUDA_SET_SHARED_MEMORY_LIMIT(quantize_rowwise_i8_convrot_cuda<256>, smpbo - 128);
quantize_rowwise_i8_convrot_cuda<256><<<rows, 1024, nbytes_shared, stream>>>(
src, dst, scales, k, rows, dst_stride);
}
return;
}

const int64_t groups = k / 256;
const int64_t groups = k / group_size;
const int64_t total_groups = rows * groups;
const int group_threads = group_size / 4;
ggml_cuda_pool_alloc<float> partial_max(ctx.pool(), total_groups);
ggml_cuda_pool_alloc<float> packed_scales(ctx.pool());
float * scales_d = scales;
if (scales_d == nullptr) {
scales_d = packed_scales.alloc(rows);
}
convrot_group_amax_i8_cuda<<<total_groups, 64, 0, stream>>>(
src, partial_max.get(), k, groups, total_groups);
if (group_size == 64) {
convrot_group_amax_i8_cuda<64><<<total_groups, group_threads, 0, stream>>>(
src, partial_max.get(), k, groups, total_groups);
} else {
convrot_group_amax_i8_cuda<256><<<total_groups, group_threads, 0, stream>>>(
src, partial_max.get(), k, groups, total_groups);
}
reduce_convrot_row_amax_i8_cuda<<<rows, 256, 0, stream>>>(
partial_max.get(), scales_d, groups, rows);
convrot_group_quantize_i8_cuda<<<total_groups, 64, 0, stream>>>(
src, dst, scales_d, k, groups, total_groups, dst_stride);
if (group_size == 64) {
convrot_group_quantize_i8_cuda<64><<<total_groups, group_threads, 0, stream>>>(
src, dst, scales_d, k, groups, total_groups, dst_stride);
} else {
convrot_group_quantize_i8_cuda<256><<<total_groups, group_threads, 0, stream>>>(
src, dst, scales_d, k, groups, total_groups, dst_stride);
}
}

static bool ggml_cuda_op_quantize_i8_convrot(
Expand All @@ -1665,7 +1698,7 @@ static bool ggml_cuda_op_quantize_i8_convrot(
const int64_t scale_rows = (rows * (int64_t)sizeof(float) + src->ne[0] - 1) / src->ne[0];
if (src->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_I8 ||
!ggml_is_contiguous(src) || !ggml_is_contiguous(dst) ||
group_size != 256 || src->ne[0] % group_size != 0 ||
(group_size != 64 && group_size != 256) || src->ne[0] % group_size != 0 ||
dst->ne[0] != src->ne[0] || dst->ne[1] != rows_padded + scale_rows) {
return false;
}
Expand All @@ -1679,7 +1712,8 @@ static bool ggml_cuda_op_quantize_i8_convrot(
src->ne[0],
rows,
rows_padded,
src->ne[0]);
src->ne[0],
group_size);
return true;
}

Expand Down Expand Up @@ -1707,7 +1741,7 @@ static void ggml_cuda_mul_mat_i8(
GGML_ASSERT(src1->ne[0] == k);
GGML_ASSERT(!prequantized || (src1->op == GGML_OP_QUANTIZE_I8_CONVROT &&
src1->ne[1] == rows_padded + scale_rows));
GGML_ASSERT(convrot_group_size == 0 || convrot_group_size == 256);
GGML_ASSERT(convrot_group_size == 0 || convrot_group_size == 64 || convrot_group_size == 256);

ggml_cuda_pool_alloc<int8_t> qdata(ctx.pool());
ggml_cuda_pool_alloc<float> scales(ctx.pool());
Expand All @@ -1727,7 +1761,7 @@ static void ggml_cuda_mul_mat_i8(
(const float *)src1->data, qdata_d, scales_d, k, rows);
} else {
ggml_cuda_quantize_i8_convrot(
ctx, (const float *)src1->data, qdata_d, scales_d, k, rows, rows_padded, k);
ctx, (const float *)src1->data, qdata_d, scales_d, k, rows, rows_padded, k, convrot_group_size);
}

const float * weight_scales = dst->src[2] != nullptr ? (const float *)dst->src[2]->data : nullptr;
Expand Down Expand Up @@ -2237,10 +2271,10 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
}

if (src0->type == GGML_TYPE_I8) {
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
#if !defined(GGML_USE_MUSA)
ggml_cuda_mul_mat_i8(ctx, src0, src1, dst);
#else
GGML_ABORT("INT8 tensorwise matmul is only implemented for CUDA");
GGML_ABORT("INT8 tensorwise matmul is only implemented for CUDA and HIP");
#endif
return;
}
Expand Down Expand Up @@ -2646,10 +2680,10 @@ static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct gg
ggml_cuda_mul_mat_id(ctx, dst);
break;
case GGML_OP_QUANTIZE_I8_CONVROT:
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
#if !defined(GGML_USE_MUSA)
GGML_ASSERT(ggml_cuda_op_quantize_i8_convrot(ctx, dst->src[0], dst, ggml_get_op_params_i32(dst, 0)));
#else
GGML_ABORT("INT8 convrot quantization is only implemented for CUDA");
GGML_ABORT("INT8 convrot quantization is only implemented for CUDA and HIP");
#endif
break;
case GGML_OP_OUT_PROD:
Expand Down Expand Up @@ -5236,7 +5270,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
return false; // TODO this could in principle be implemented though currently there is no use case.
}
if (a->type == GGML_TYPE_I8) {
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
#if !defined(GGML_USE_MUSA)
const ggml_tensor * weight_scale = op->src[2];
const ggml_tensor * bias = op->src[3];
const int convrot_group_size = ggml_get_op_params_i32(op, 2);
Expand All @@ -5259,8 +5293,12 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
(bias == nullptr ||
(bias->type == GGML_TYPE_F32 && ggml_is_contiguous(bias) &&
ggml_nelements(bias) == a->ne[1])) &&
(convrot_group_size == 0 || (convrot_group_size == 256 && a->ne[0] % 256 == 0)) &&
turing_mma_available(ggml_cuda_info().devices[dev_ctx->device].cc);
(convrot_group_size == 0 ||
((convrot_group_size == 64 || convrot_group_size == 256) &&
a->ne[0] % convrot_group_size == 0)) &&
(turing_mma_available(ggml_cuda_info().devices[dev_ctx->device].cc) ||
amd_wmma_available(ggml_cuda_info().devices[dev_ctx->device].cc) ||
amd_mfma_available(ggml_cuda_info().devices[dev_ctx->device].cc));
#else
return false;
#endif
Expand Down Expand Up @@ -5640,7 +5678,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
case GGML_OP_LIGHTNING_INDEXER:
return ggml_cuda_lightning_indexer_supported(dev_ctx->device, op);
case GGML_OP_QUANTIZE_I8_CONVROT:
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
#if !defined(GGML_USE_MUSA)
{
const ggml_tensor * src = op->src[0];
const int group_size = ggml_get_op_params_i32(op, 0);
Expand All @@ -5651,7 +5689,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
: 0;
return src != nullptr && src->type == GGML_TYPE_F32 && op->type == GGML_TYPE_I8 &&
ggml_is_contiguous(src) && ggml_is_contiguous(op) &&
group_size == 256 && src->ne[0] % group_size == 0 &&
(group_size == 64 || group_size == 256) && src->ne[0] % group_size == 0 &&
op->ne[0] == src->ne[0] && op->ne[1] == rows_padded + scale_rows;
}
#else
Expand Down
5 changes: 5 additions & 0 deletions src/ggml-cuda/vendors/hip.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define CUDA_R_16F HIPBLAS_R_16F
#define CUDA_R_16BF HIPBLAS_R_16B
#define CUDA_R_32F HIPBLAS_R_32F
#define CUDA_R_8I HIPBLAS_R_8I
#define CUDA_R_32I HIPBLAS_R_32I
#define CUBLAS_SIDE_RIGHT HIPBLAS_SIDE_RIGHT
#define CUBLAS_FILL_MODE_UPPER HIPBLAS_FILL_MODE_UPPER
#define CUBLAS_DIAG_NON_UNIT HIPBLAS_DIAG_NON_UNIT
Expand All @@ -32,6 +34,8 @@
#define __shfl_sync(mask, var, laneMask, width) __shfl(var, laneMask, width)
#define __shfl_up_sync(mask, var, laneMask, width) __shfl_up(var, laneMask, width)
#define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
// Variadic: call sites may omit the trailing width argument.
#define __shfl_down_sync(mask, ...) __shfl_down(__VA_ARGS__)
#define __all_sync(mask, var) __all(var)
#define __any_sync(mask, var) __any(var)
#define cublasStrsmBatched hipblasStrsmBatched
Expand Down Expand Up @@ -159,6 +163,7 @@
#if HIP_VERSION >= 60500000
#define CUBLAS_COMPUTE_16F HIPBLAS_COMPUTE_16F
#define CUBLAS_COMPUTE_32F HIPBLAS_COMPUTE_32F
#define CUBLAS_COMPUTE_32I HIPBLAS_COMPUTE_32I
#define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_COMPUTE_32F_FAST_16F
#define cublasComputeType_t hipblasComputeType_t
#define cudaDataType_t hipDataType
Expand Down
Loading