@@ -139,6 +139,41 @@ class Identity : public UnaryBlock {
139139 }
140140};
141141
142+ // Normalized regular Hadamard transform (H/gs^0.5) applied independently to each
143+ // contiguous group of group_size elements along ne0, expressed as a butterfly of
144+ // view/add/sub/concat stages so it runs on every backend without a dedicated op.
145+ __STATIC_INLINE__ ggml_tensor* ggml_regular_hadamard_f32 (ggml_context* ctx, ggml_tensor* x, int group_size) {
146+ GGML_ASSERT (x->type == GGML_TYPE_F32 );
147+ GGML_ASSERT (ggml_is_contiguous (x));
148+ GGML_ASSERT (group_size > 1 && (group_size & (group_size - 1 )) == 0 );
149+ GGML_ASSERT (x->ne [0 ] % group_size == 0 );
150+ const int64_t gs = group_size;
151+ const int64_t n_rows = ggml_nelements (x) / x->ne [0 ];
152+ const int64_t m = x->ne [0 ] / gs; // independent Hadamard groups per row
153+ ggml_tensor* y = ggml_reshape_3d (ctx, x, gs, m, n_rows);
154+ for (int64_t b = gs; b > 1 ; b /= 2 ) {
155+ const int64_t h = b / 2 ;
156+ const int64_t blocks_per_group = (gs / b) * m;
157+ if (ggml_nelements (y) != h * 2 * blocks_per_group * n_rows || !ggml_is_contiguous (y)) {
158+ fprintf (stderr, " hadamard stage pre-check failed: b=%lld h=%lld y=[%lld,%lld,%lld,%lld] n_rows=%lld\n " ,
159+ (long long )b, (long long )h, (long long )y->ne [0 ], (long long )y->ne [1 ], (long long )y->ne [2 ],
160+ (long long )y->ne [3 ], (long long )n_rows);
161+ GGML_ABORT (" ggml_regular_hadamard_f32 stage shape mismatch" );
162+ }
163+ ggml_tensor* v = ggml_reshape_4d (ctx, y, h, 2 , blocks_per_group, n_rows);
164+ const size_t nb1 = ggml_row_size (GGML_TYPE_F32 , h);
165+ const size_t nb2 = ggml_row_size (GGML_TYPE_F32 , 2 * h);
166+ const size_t nb3 = ggml_row_size (GGML_TYPE_F32 , 2 * h * blocks_per_group);
167+ ggml_tensor* top = ggml_view_4d (ctx, v, h, 1 , blocks_per_group, n_rows, nb1, nb2, nb3, 0 );
168+ ggml_tensor* bottom = ggml_view_4d (ctx, v, h, 1 , blocks_per_group, n_rows, nb1, nb2, nb3, h * sizeof (float ));
169+ ggml_tensor* sum = ggml_add (ctx, top, bottom);
170+ ggml_tensor* diff = ggml_sub (ctx, top, bottom);
171+ y = ggml_concat (ctx, sum, diff, 0 );
172+ }
173+ y = ggml_scale (ctx, y, 1 .0f / sqrtf ((float )gs));
174+ return ggml_reshape_4d (ctx, y, x->ne [0 ], x->ne [1 ], x->ne [2 ], x->ne [3 ]);
175+ }
176+
142177class Linear : public UnaryBlock {
143178protected:
144179 int64_t in_features;
@@ -149,6 +184,8 @@ class Linear : public UnaryBlock {
149184 bool has_weight_scale = false ;
150185 bool int8_convrot = false ;
151186 int int8_convrot_group_size = 0 ;
187+ int w4_convrot_kind = W4_CONVROT_NONE ;
188+ int w4_convrot_group_size = 0 ;
152189 float scale;
153190 std::string prefix;
154191
@@ -157,19 +194,28 @@ class Linear : public UnaryBlock {
157194 has_weight_scale = false ;
158195 int8_convrot = false ;
159196 int8_convrot_group_size = 0 ;
197+ w4_convrot_kind = W4_CONVROT_NONE ;
198+ w4_convrot_group_size = 0 ;
199+ auto weight_storage = tensor_storage_map.find (prefix + " weight" );
200+ const bool is_int8_tensorwise = weight_storage != tensor_storage_map.end () && weight_storage->second .is_int8_tensorwise ;
201+ const bool is_w4_convrot = weight_storage != tensor_storage_map.end () && weight_storage->second .w4_convrot_kind != W4_CONVROT_NONE ;
160202 enum ggml_type wtype = get_type (prefix + " weight" , tensor_storage_map, GGML_TYPE_F32 );
161203 if (in_features % ggml_blck_size (wtype) != 0 || force_f32) {
162204 wtype = GGML_TYPE_F32 ;
163205 }
164- params[" weight" ] = ggml_new_tensor_2d (ctx, wtype, in_features, out_features);
206+ if (is_w4_convrot) {
207+ // packed nibbles: file tensor is [N, K/2] I8; the graph consumes the
208+ // same bytes as [K/2, N] (ggml dims are reversed vs file layout)
209+ GGML_ASSERT (in_features % 2 == 0 );
210+ params[" weight" ] = ggml_new_tensor_2d (ctx, GGML_TYPE_I8 , in_features / 2 , out_features);
211+ } else {
212+ params[" weight" ] = ggml_new_tensor_2d (ctx, wtype, in_features, out_features);
213+ }
165214 if (bias) {
166- enum ggml_type wtype = GGML_TYPE_F32 ;
167- params[" bias" ] = ggml_new_tensor_1d (ctx, wtype, out_features);
215+ params[" bias" ] = ggml_new_tensor_1d (ctx, GGML_TYPE_F32 , out_features);
168216 }
169- auto weight_storage = tensor_storage_map.find (prefix + " weight" );
170- const bool is_int8_tensorwise = weight_storage != tensor_storage_map.end () && weight_storage->second .is_int8_tensorwise ;
171217 auto weight_scale_storage = tensor_storage_map.find (prefix + " weight_scale" );
172- if (weight_scale_storage != tensor_storage_map.end ()) {
218+ if (weight_scale_storage != tensor_storage_map.end () && !is_w4_convrot ) {
173219 const int64_t scale_nelements = weight_scale_storage->second .nelements ();
174220 GGML_ASSERT (scale_nelements == 1 || scale_nelements == out_features);
175221 params[" weight_scale" ] = ggml_new_tensor_1d (ctx, GGML_TYPE_F32 , scale_nelements);
@@ -181,6 +227,31 @@ class Linear : public UnaryBlock {
181227 int8_convrot = weight_storage->second .int8_convrot ;
182228 int8_convrot_group_size = weight_storage->second .int8_convrot_group_size ;
183229 }
230+ if (is_w4_convrot) {
231+ w4_convrot_kind = weight_storage->second .w4_convrot_kind ;
232+ w4_convrot_group_size = weight_storage->second .w4_convrot_group_size ;
233+ if (w4_convrot_kind == W4_CONVROT_W4A4 ) {
234+ // row scales load straight from the weight_scale companion
235+ GGML_ASSERT (weight_scale_storage != tensor_storage_map.end ());
236+ const int64_t scale_nelements = weight_scale_storage->second .nelements ();
237+ GGML_ASSERT (scale_nelements == out_features);
238+ params[" weight_scale" ] = ggml_new_tensor_1d (ctx, GGML_TYPE_F32 , scale_nelements);
239+ has_weight_scale = true ;
240+ } else {
241+ GGML_ASSERT (w4_convrot_kind == W4_CONVROT_W4A8 );
242+ GGML_ASSERT (in_features % 16 == 0 );
243+ auto codebook_storage = tensor_storage_map.find (prefix + " weight_codebook" );
244+ auto s_channel_storage = tensor_storage_map.find (prefix + " weight_s_channel" );
245+ auto s_rel_storage = tensor_storage_map.find (prefix + " weight_s_rel" );
246+ GGML_ASSERT (codebook_storage != tensor_storage_map.end () && codebook_storage->second .nelements () == 16 );
247+ GGML_ASSERT (s_channel_storage != tensor_storage_map.end () && s_channel_storage->second .nelements () == out_features);
248+ GGML_ASSERT (s_rel_storage != tensor_storage_map.end () && s_rel_storage->second .is_f8_e4m3 &&
249+ s_rel_storage->second .nelements () == out_features * (in_features / 16 ));
250+ params[" weight_codebook" ] = ggml_new_tensor_1d (ctx, GGML_TYPE_F32 , 16 );
251+ params[" weight_s_channel" ] = ggml_new_tensor_1d (ctx, GGML_TYPE_F32 , out_features);
252+ params[" weight_s_rel" ] = ggml_new_tensor_2d (ctx, GGML_TYPE_F8_E4M3 , in_features / 16 , out_features);
253+ }
254+ }
184255 }
185256
186257public:
@@ -235,7 +306,7 @@ class Linear : public UnaryBlock {
235306 }
236307 ggml_tensor* linear_bias = has_weight_scale ? nullptr : b;
237308 ggml_tensor* out = nullptr ;
238- if (w->type == GGML_TYPE_I8 ) {
309+ if (w->type == GGML_TYPE_I8 && w4_convrot_kind == W4_CONVROT_NONE ) {
239310 if (x->type != GGML_TYPE_F32 ) {
240311 x = ggml_ext_cast_f32 (ctx->ggml_ctx , ctx->backend , x);
241312 }
@@ -280,6 +351,61 @@ class Linear : public UnaryBlock {
280351 }
281352 return out;
282353 }
354+ if (w4_convrot_kind != W4_CONVROT_NONE ) {
355+ // packed-w4 kernel path: fused Hadamard+int8 activation quant, then
356+ // the w4 mul_mat chain (mirrors the int8 branch above)
357+ ggml_tensor* lora_input = x;
358+ if (x->type != GGML_TYPE_F32 ) {
359+ x = ggml_ext_cast_f32 (ctx->ggml_ctx , ctx->backend , x);
360+ }
361+ if (!ggml_is_contiguous (x)) {
362+ x = ggml_cont (ctx->ggml_ctx , x);
363+ }
364+ if (ctx->weight_adapter && b != nullptr ) {
365+ b = ctx->weight_adapter ->patch_weight (ctx->ggml_ctx , ctx->backend , b, prefix + " bias" );
366+ }
367+ // quantize only at unit scale: with scale != 1 the wrapper
368+ // pre-scales the F32 input and the w4 kernels quantize inline
369+ if (scale == 1 .f ) {
370+ const auto cache_key = std::make_pair (x, w4_convrot_group_size);
371+ auto cached = ctx->int8_convrot_cache .find (cache_key);
372+ if (cached == ctx->int8_convrot_cache .end ()) {
373+ x = ggml_quantize_i8_convrot (ctx->ggml_ctx , x, w4_convrot_group_size);
374+ ctx->int8_convrot_cache .emplace (cache_key, x);
375+ } else {
376+ x = cached->second ;
377+ }
378+ }
379+ ggml_tensor* weight_scales = nullptr ;
380+ ggml_tensor* s_channel = nullptr ;
381+ ggml_tensor* s_rel = nullptr ;
382+ if (w4_convrot_kind == W4_CONVROT_W4A4 ) {
383+ weight_scales = params[" weight_scale" ];
384+ } else {
385+ // the kernel folds codebook x s_channel into its per-group LUT
386+ weight_scales = params[" weight_codebook" ];
387+ s_channel = params[" weight_s_channel" ];
388+ s_rel = params[" weight_s_rel" ];
389+ }
390+ out = ggml_ext_linear_w4_convrot (ctx->ggml_ctx , x, w, weight_scales, s_channel, s_rel, b,
391+ w4_convrot_kind, w4_convrot_group_size, scale);
392+ if (ctx->weight_adapter ) {
393+ // LoRA was trained on unrotated activations; apply it separately
394+ WeightAdapter::ForwardParams forward_params;
395+ forward_params.op_type = WeightAdapter::ForwardParams::op_type_t ::OP_LINEAR ;
396+ forward_params.linear .force_prec_f32 = force_prec_f32;
397+ forward_params.linear .scale = scale;
398+ out = ctx->weight_adapter ->add_lora_to_output (ctx->ggml_ctx ,
399+ ctx->backend ,
400+ lora_input,
401+ w,
402+ out,
403+ prefix,
404+ forward_params);
405+ return out;
406+ }
407+ return out;
408+ }
283409 if (has_weight_scale) {
284410 out = ggml_ext_linear (ctx->ggml_ctx , x, w, nullptr , force_prec_f32, scale);
285411 out = ggml_mul (ctx->ggml_ctx , out, weight_scale);
0 commit comments