@@ -10,40 +10,58 @@ namespace SD_CUDA_NAMESPACE {
1010 using i8 ::blas_supported;
1111 using i8 ::warp_reduce_max;
1212
13- // Match the GGML Q8_0 block layout checked by requantize_q8_supports().
14- struct Q8Block {
15- static constexpr int size = 32 ;
16- static constexpr int pairs = size / 2 ;
17- half scale;
18- uint16_t values[pairs];
13+ constexpr int block_size = SD_REQUANTIZE_I8_BLOCK_SIZE ;
14+
15+ // GGML stores FP16 parameters, then the Q5 high bits, then the quants.
16+ template <ggml_type type>
17+ struct QuantBlock {
18+ static constexpr int bits = type == GGML_TYPE_Q8_0 ? 8 : (type == GGML_TYPE_Q5_0 || type == GGML_TYPE_Q5_1 ? 5 : 4 );
19+ static constexpr bool affine = type == GGML_TYPE_Q4_1 || type == GGML_TYPE_Q5_1 ;
20+ static constexpr int high_words = bits == 5 ? 2 : 0 ;
21+ half params[affine ? 2 : 1 ];
22+ uint16_t payload[(bits == 8 ? block_size / 2 : block_size / 4 ) + high_words];
23+
24+ __device__ __forceinline__ float decode (int lane) const {
25+ const float scale = __half2float (params[0 ]);
26+ if constexpr (bits == 8 ) {
27+ const int8_t value = int8_t (payload[lane / 2 ] >> (8 * (lane % 2 )));
28+ return float (value) * scale;
29+ } else {
30+ const int low_lane = lane % 16 ;
31+ const uint16_t packed = payload[high_words + low_lane / 2 ];
32+ int value = (packed >> (8 * (low_lane % 2 ) + 4 * (lane / 16 ))) & 15 ;
33+ if constexpr (bits == 5 )
34+ value |= ((payload[lane / 16 ] >> low_lane) & 1 ) << 4 ;
35+ if constexpr (affine)
36+ return float (value) * scale + __half2float (params[1 ]);
37+ return float (value - (1 << (bits - 1 ))) * scale;
38+ }
39+ }
1940 };
20- static_assert (sizeof (Q8Block) == 34 );
2141
22- static __device__ __forceinline__ int8_t requantize_q8_value (float value) {
42+ static __device__ __forceinline__ int8_t requantize_i8_value (float value) {
2343 return int8_t (__float2int_rn (fminf (127 .f , fmaxf (-127 .f , value))));
2444 }
2545
26- static __device__ __forceinline__ void load_rotated_group (const Q8Block* blocks, int lane, float (&values)[8]) {
46+ template <ggml_type type>
47+ static __device__ __forceinline__ void load_rotated_group (const QuantBlock<type>* blocks, int lane, float (&values)[8]) {
2748#pragma unroll
28- for (int j = 0 ; j < 8 ; ++j) {
29- const auto & block = blocks[j];
30- const int8_t value = int8_t (block.values [lane / 2 ] >> (8 * (lane % 2 )));
31- values[j] = (float (value) * __half2float (block.scale )) * (1 .0f / 16 .0f );
32- }
49+ for (int j = 0 ; j < 8 ; ++j)
50+ values[j] = blocks[j].decode (lane) * (1 .0f / 16 .0f );
3351 sd_convrot::rotate_h256 (values, lane);
3452 }
3553
3654 constexpr int requantize_warps = 8 ;
37- constexpr int blocks_per_group = 256 / Q8Block::size ;
55+ constexpr int blocks_per_group = 256 / block_size ;
3856 constexpr int blocks_per_round = requantize_warps * blocks_per_group;
39- constexpr int weights_per_round = blocks_per_round * Q8Block::size ;
57+ constexpr int weights_per_round = blocks_per_round * block_size ;
4058
41- template <int cached_groups>
42- static __global__ void requantize_q8_weights (const Q8Block * weights, int8_t * decoded, float * scales, int64_t blocks_per_channel) {
59+ template <ggml_type type, int cached_groups>
60+ static __global__ void requantize_i8_weights (const QuantBlock<type> * weights, int8_t * decoded, float * scales, int64_t blocks_per_channel) {
4361 const int lane = threadIdx .x ;
4462 const int warp = threadIdx .y ;
4563 const auto * blocks = weights + int64_t (blockIdx .x ) * blocks_per_channel;
46- auto * output = decoded + int64_t (blockIdx .x ) * blocks_per_channel * Q8Block::size ;
64+ auto * output = decoded + int64_t (blockIdx .x ) * blocks_per_channel * block_size ;
4765
4866 __shared__ float maxima[requantize_warps];
4967 // Keep a bounded number of rotated groups in registers across the scale reduction.
@@ -98,46 +116,66 @@ namespace SD_CUDA_NAMESPACE {
98116 load_rotated_group (blocks + block, lane, values);
99117#pragma unroll
100118 for (int j = 0 ; j < 8 ; ++j) {
101- const float value = cached_groups > 0 ? retained[slot][j] : values[j];
102- output[(block + j) * Q8Block::size + lane] = requantize_q8_value (value * inverse);
119+ const float value = cached_groups > 0 ? retained[slot][j] : values[j];
120+ output[(block + j) * block_size + lane] = requantize_i8_value (value * inverse);
103121 }
104122 }
105123 }
106124
107- template <int cached_groups>
108- static void launch_requantize (const Q8Block* weights, int8_t * values, float * scales, int64_t k, int64_t n, CUDAStream stream) {
109- requantize_q8_weights<cached_groups><<<unsigned (n), dim3 (Q8Block::size, requantize_warps), 0 , stream>>> (
110- weights, values, scales, k / Q8Block::size);
111- }
112-
113- static bool requantize_q8_supports (ggml_backend_dev_t device, const ggml_tensor* node, void *) {
125+ static bool requantize_i8_supports (ggml_backend_dev_t device, const ggml_tensor* node, void *) {
114126 const auto * weight = node->src [0 ];
115127 const int64_t k = weight->ne [0 ], n = weight->ne [1 ];
116128 const auto info = cuda_device_info (device);
117- return info.device >= 0 && k <= INT_MAX / 16384 && n <= INT_MAX && aligned (weight, alignof (Q8Block)) &&
118- ggml_blck_size (GGML_TYPE_Q8_0 ) == Q8Block::size && ggml_type_size (GGML_TYPE_Q8_0 ) == sizeof (Q8Block) &&
129+ return info.device >= 0 && k <= INT_MAX / 16384 && n <= INT_MAX && aligned (weight, alignof (uint16_t )) &&
119130 (cuda_kind != GGML_EXT_GPU_CUDA || blas_supported (device, info, n));
120131 }
121132
122- static ggml_status requantize_q8_execute (const ggml_ext_execution_context* execution, ggml_tensor* const * nodes, int , void *, void *) {
123- ggml_ext_gpu_context context;
124- if (!cuda_context (execution->backend , &context))
125- return GGML_STATUS_FAILED ;
126- auto * dst = nodes[0 ];
127- const auto * weight = dst->src [0 ];
133+ template <ggml_type type>
134+ static void launch_requantize (const ggml_tensor* weight, int8_t * values, CUDAStream stream) {
135+ using Block = QuantBlock<type>;
136+ static_assert (sizeof (Block) == sd_ggml_requantize_i8_block_bytes (type) && alignof (Block) == alignof (uint16_t ));
128137 const int64_t k = weight->ne [0 ], n = weight->ne [1 ];
129- auto * values = static_cast <int8_t *>(dst->data );
130138 auto * scales = reinterpret_cast <float *>(values + k * n);
131- const auto * weights = static_cast <const Q8Block*>(weight->data );
132- const auto stream = static_cast <CUDAStream>(context.stream );
139+ const auto * weights = static_cast <const Block*>(weight->data );
140+ const dim3 threads (block_size, requantize_warps);
141+ const int64_t blocks_per_channel = k / block_size;
133142 if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 2 * weights_per_round) {
134- launch_requantize< 2 >(weights, values, scales, k, n, stream );
143+ requantize_i8_weights<type, 2 > <<< unsigned (n), threads, 0 , stream>>> (weights, values, scales, blocks_per_channel );
135144 } else if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 4 * weights_per_round) {
136- launch_requantize< 4 >(weights, values, scales, k, n, stream );
145+ requantize_i8_weights<type, 4 > <<< unsigned (n), threads, 0 , stream>>> (weights, values, scales, blocks_per_channel );
137146 } else if (cuda_kind == GGML_EXT_GPU_CUDA && k <= 8 * weights_per_round) {
138- launch_requantize< 8 >(weights, values, scales, k, n, stream );
147+ requantize_i8_weights<type, 8 > <<< unsigned (n), threads, 0 , stream>>> (weights, values, scales, blocks_per_channel );
139148 } else {
140- launch_requantize<0 >(weights, values, scales, k, n, stream);
149+ requantize_i8_weights<type, 0 ><<<unsigned (n), threads, 0 , stream>>> (weights, values, scales, blocks_per_channel);
150+ }
151+ }
152+
153+ static ggml_status requantize_i8_execute (const ggml_ext_execution_context* execution, ggml_tensor* const * nodes, int , void *, void *) {
154+ ggml_ext_gpu_context context;
155+ if (!cuda_context (execution->backend , &context))
156+ return GGML_STATUS_FAILED ;
157+ auto * dst = nodes[0 ];
158+ const auto * weight = dst->src [0 ];
159+ auto * values = static_cast <int8_t *>(dst->data );
160+ const auto stream = static_cast <CUDAStream>(context.stream );
161+ switch (weight->type ) {
162+ case GGML_TYPE_Q4_0 :
163+ launch_requantize<GGML_TYPE_Q4_0 >(weight, values, stream);
164+ break ;
165+ case GGML_TYPE_Q4_1 :
166+ launch_requantize<GGML_TYPE_Q4_1 >(weight, values, stream);
167+ break ;
168+ case GGML_TYPE_Q5_0 :
169+ launch_requantize<GGML_TYPE_Q5_0 >(weight, values, stream);
170+ break ;
171+ case GGML_TYPE_Q5_1 :
172+ launch_requantize<GGML_TYPE_Q5_1 >(weight, values, stream);
173+ break ;
174+ case GGML_TYPE_Q8_0 :
175+ launch_requantize<GGML_TYPE_Q8_0 >(weight, values, stream);
176+ break ;
177+ default :
178+ return GGML_STATUS_FAILED ;
141179 }
142180 return cuda_last_error () == cuda_success ? GGML_STATUS_SUCCESS : GGML_STATUS_FAILED ;
143181 }
@@ -147,10 +185,10 @@ namespace SD_CUDA_NAMESPACE {
147185 ggml_ext_kernel_desc desc = {};
148186 desc.abi_version = GGML_EXT_ABI_VERSION ;
149187 desc.op = GGML_OP_CUSTOM ;
150- desc.name = " sd.requantize_q8 ." SD_CUDA_NAME " .v1" ;
151- desc.custom_op = sd_ggml_requantize_q8_op ();
152- desc.supports = requantize_q8_supports ;
153- desc.execute = requantize_q8_execute ;
188+ desc.name = " sd.requantize_i8 ." SD_CUDA_NAME " .v1" ;
189+ desc.custom_op = sd_ggml_requantize_i8_op ();
190+ desc.supports = requantize_i8_supports ;
191+ desc.execute = requantize_i8_execute ;
154192 GGML_ASSERT (ggml_ext_register_kernel_ex (device, &desc, GGML_EXT_KERNEL_CAPTURE_SAFE | GGML_EXT_KERNEL_REPLAY_REUSABLE ));
155193 }
156194}
0 commit comments