1616#include "hvx-dump.h"
1717#include "hvx-arith.h"
1818#include "hvx-reduce.h"
19+ #include "hvx-copy.h"
1920
2021#define GGML_COMMON_DECL_C
2122#include "ggml-common.h"
@@ -1413,7 +1414,7 @@ static void hvx_mv_id_tiled(unsigned int nth, unsigned int ith, void * data) {
14131414
14141415 for (uint32_t ie1 = 0 ; ie1 < n_aids ; ++ ie1 ) { // for each expert
14151416 const int32_t eid = * (const int32_t * ) ((const uint8_t * ) src2 -> data + ie1 * src2 -> nb [0 ]);
1416- if (eid == -1 ) {
1417+ if (eid < 0 ) {
14171418 continue ;
14181419 }
14191420 assert (eid >= 0 && eid < (int32_t ) n_ids );
@@ -4111,7 +4112,7 @@ static int hvx_mm_matmul_id_nx(
41114112 return HTP_STATUS_OK ;
41124113}
41134114
4114- static inline void scan_expert_ids_n (
4115+ static __attribute__(( noinline )) uint32_t scan_expert_ids_n (
41154116 const struct htp_tensor * ids ,
41164117 const uint32_t n_ids ,
41174118 uint32_t n_as ,
@@ -4121,47 +4122,28 @@ static inline void scan_expert_ids_n(
41214122) {
41224123 const size_t ids_nb1 = ids -> nb [1 ];
41234124 const uint8_t * ids_data = (const uint8_t * ) ids -> data ;
4125+ uint32_t n_skipped = 0 ;
41244126
41254127 for (uint32_t iid1 = 0 ; iid1 < ids -> ne [1 ]; ++ iid1 ) {
41264128 const int32_t * row_ptr = (const int32_t * ) (ids_data + iid1 * ids_nb1 );
41274129 for (uint32_t id = 0 ; id < n_ids ; ++ id ) {
41284130 const int32_t i02 = row_ptr [id ];
4129- if (i02 == -1 ) {
4131+ if (i02 < 0 ) {
4132+ n_skipped ++ ;
41304133 continue ;
41314134 }
4132- assert (i02 >= 0 && i02 < n_as );
4135+ assert (i02 >= 0 && i02 < ( int32_t ) n_as );
41334136
41344137 if (matrix_rows ) {
41354138 matrix_rows [i02 * mapping_stride + counts [i02 ]] = (struct mmid_row_mapping ) { id , iid1 };
41364139 }
41374140 counts [i02 ] += 1 ;
41384141 }
41394142 }
4143+ return n_skipped ;
41404144}
41414145
4142- // the matmuls only write the rows that an expert owns, so the rows of the skipped slots are zeroed here
4143- static inline void zero_skipped_dst_rows (
4144- const struct htp_tensor * ids ,
4145- uint32_t n_ids ,
4146- const struct htp_tensor * dst ,
4147- uint32_t ne0 ,
4148- size_t nb1 ,
4149- size_t nb2
4150- ) {
4151- const uint8_t * ids_data = (const uint8_t * ) ids -> data ;
4152-
4153- for (uint32_t iid1 = 0 ; iid1 < ids -> ne [1 ]; ++ iid1 ) {
4154- for (uint32_t id = 0 ; id < n_ids ; ++ id ) {
4155- const int32_t i02 = * (const int32_t * ) (ids_data + iid1 * ids -> nb [1 ] + id * ids -> nb [0 ]);
4156- if (i02 != -1 ) {
4157- continue ;
4158- }
4159- memset ((uint8_t * ) (dst -> data + id * nb1 + iid1 * nb2 ), 0 , ne0 * sizeof (float ));
4160- }
4161- }
4162- }
4163-
4164- static inline void scan_expert_ids (
4146+ static __attribute__((noinline )) uint32_t scan_expert_ids (
41654147 const struct htp_tensor * ids ,
41664148 uint32_t n_ids ,
41674149 uint32_t n_as ,
@@ -4173,81 +4155,118 @@ static inline void scan_expert_ids(
41734155
41744156 if (ids_nb0 == 4 ) {
41754157 switch (n_ids ) {
4176- case 8 : scan_expert_ids_n (ids , 8 , n_as , counts , matrix_rows , mapping_stride ); break ;
4177- case 4 : scan_expert_ids_n (ids , 4 , n_as , counts , matrix_rows , mapping_stride ); break ;
4178- case 2 : scan_expert_ids_n (ids , 2 , n_as , counts , matrix_rows , mapping_stride ); break ;
4179- default : scan_expert_ids_n (ids , n_ids , n_as , counts , matrix_rows , mapping_stride ); break ;
4158+ case 8 : return scan_expert_ids_n (ids , 8 , n_as , counts , matrix_rows , mapping_stride );
4159+ case 4 : return scan_expert_ids_n (ids , 4 , n_as , counts , matrix_rows , mapping_stride );
4160+ case 2 : return scan_expert_ids_n (ids , 2 , n_as , counts , matrix_rows , mapping_stride );
4161+ default : return scan_expert_ids_n (ids , n_ids , n_as , counts , matrix_rows , mapping_stride );
41804162 }
41814163 } else {
41824164 // Strided fallback
41834165 const size_t ids_nb1 = ids -> nb [1 ];
41844166 const uint8_t * ids_data = (const uint8_t * ) ids -> data ;
4167+ uint32_t n_skipped = 0 ;
41854168 for (uint32_t iid1 = 0 ; iid1 < ids -> ne [1 ]; ++ iid1 ) {
41864169 const int32_t * row_ptr = (const int32_t * ) (ids_data + iid1 * ids_nb1 );
41874170 for (uint32_t id = 0 ; id < n_ids ; ++ id ) {
41884171 const int32_t i02 = * (const int32_t * ) ((const uint8_t * ) row_ptr + id * ids_nb0 );
4189- if (i02 == -1 ) {
4172+ if (i02 < 0 ) {
4173+ n_skipped ++ ;
41904174 continue ;
41914175 }
4192- assert (i02 >= 0 && i02 < n_as );
4176+ assert (i02 >= 0 && i02 < ( int32_t ) n_as );
41934177
41944178 if (matrix_rows ) {
41954179 matrix_rows [i02 * mapping_stride + counts [i02 ]] = (struct mmid_row_mapping ) { id , iid1 };
41964180 }
41974181 counts [i02 ] += 1 ;
41984182 }
41994183 }
4184+ return n_skipped ;
42004185 }
42014186}
42024187
4203- int op_matmul_id (struct htp_ops_context * octx ) {
4204- htp_matmul_tensors_preamble ;
4205-
4206- const struct htp_mm_kernel_params * kparams = (const struct htp_mm_kernel_params * ) octx -> kernel_params ;
4207- struct htp_mm_context mmctx_struct = {0 };
4208- struct htp_mm_context * mmctx = & mmctx_struct ;
4209-
4210- const int status = htp_mm_init_context (octx , kparams );
4211- if (status != HTP_STATUS_OK ) {
4212- return status ;
4188+ static __attribute__((noinline )) uint32_t count_skipped_ids_single (const struct htp_tensor * ids , uint32_t n_ids ) {
4189+ const uint8_t * ids_data = (const uint8_t * ) ids -> data ;
4190+ const size_t ids_nb0 = ids -> nb [0 ];
4191+ uint32_t n_skipped = 0 ;
4192+ for (uint32_t id = 0 ; id < n_ids ; ++ id ) {
4193+ const int32_t i02 = * (const int32_t * ) (ids_data + id * ids_nb0 );
4194+ if (i02 < 0 ) {
4195+ n_skipped ++ ;
4196+ }
42134197 }
4198+ return n_skipped ;
4199+ }
42144200
4215- struct htp_thread_trace * tr = & octx -> ctx -> trace [0 ];
4216- htp_trace_event_start (tr , HTP_TRACE_EVT_INIT , 0 );
4217-
4218- mmctx -> octx = octx ;
4219- mmctx -> act = act ;
4201+ // Zero rows for skipped slots (ids < 0). Matmuls only write rows that an expert owns.
4202+ static void zero_skipped_dst_rows (
4203+ struct htp_ops_context * octx ,
4204+ const struct htp_tensor * ids ,
4205+ uint32_t n_ids ,
4206+ const struct htp_tensor * dst
4207+ ) {
4208+ const uint32_t elem_size = (dst -> type == HTP_TYPE_F16 ) ? sizeof (__fp16 ) : sizeof (float );
4209+ uint32_t col_start = 0 ;
4210+ uint32_t col_count = dst -> ne [0 ];
42204211
4221- const struct htp_tensor * restrict ids = octx -> src [2 ];
4222- if (htp_tensor_is_extended (ids ) || htp_tensor_is_extended (act ) || htp_tensor_is_extended (dst )) {
4223- return HTP_STATUS_NO_SUPPORT ;
4212+ if (octx -> ctx -> mdev .count > 1 ) {
4213+ const bool can_split = htp_tensor_can_row_partition (dst , elem_size );
4214+ const uint32_t chunk_elems = (dst -> type == HTP_TYPE_F16 ) ? 64 : 32 ;
4215+ const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition (
4216+ dst -> ne [0 ], can_split ? chunk_elems : 0 , octx -> ctx -> mdev .idx , octx -> ctx -> mdev .count , & octx -> ctx -> mdev .count_div );
4217+ col_start = range .start ;
4218+ col_count = range .count ;
42244219 }
42254220
4226- const size_t src0_row_size = nb01 ;
4227- const size_t dst_row_size = nb1 ;
4228-
4229- const size_t src0_row_size_padded = hex_round_up (src0_row_size , 128 );
4221+ if (col_count == 0 ) {
4222+ return ;
4223+ }
42304224
4231- const uint32_t src0_nrows = ne01 ; // per expert
4232- const uint32_t act_nrows = ne11 * ne12 * ne13 ;
4225+ const uint8_t * ids_data = (const uint8_t * ) ids -> data ;
4226+ const size_t ids_nb0 = ids -> nb [0 ];
4227+ const size_t ids_nb1 = ids -> nb [1 ];
4228+ const size_t dst_nb1 = dst -> nb [1 ];
4229+ const size_t dst_nb2 = dst -> nb [2 ];
4230+ const uint32_t col_offset_bytes = col_start * elem_size ;
42334231
4234- // row groups
4235- const int n_ids = ids -> ne [0 ]; // n_expert_used
4236- const int n_as = ne02 ; // n_expert
4232+ for (uint32_t iid1 = 0 ; iid1 < ids -> ne [1 ]; ++ iid1 ) {
4233+ const uint8_t * ids_row = ids_data + iid1 * ids_nb1 ;
4234+ uint8_t * dst_row_group = (uint8_t * ) dst -> data + iid1 * dst_nb2 + col_offset_bytes ;
4235+ for (uint32_t id = 0 ; id < n_ids ; ++ id ) {
4236+ const int32_t i02 = * (const int32_t * ) (ids_row + id * ids_nb0 );
4237+ if (i02 < 0 ) {
4238+ hvx_splat_u (dst_row_group + id * dst_nb1 , Q6_V_vzero (), col_count , elem_size );
4239+ }
4240+ }
4241+ }
4242+ }
42374243
4244+ static int htp_mmid_prepare_mapping (
4245+ struct htp_ops_context * octx ,
4246+ const struct htp_tensor * ids ,
4247+ uint32_t n_ids ,
4248+ uint32_t n_as ,
4249+ uint32_t act_nrows ,
4250+ uint8_t * * out_mapping_buf ,
4251+ uint32_t * * out_matrix_row_counts ,
4252+ struct mmid_row_mapping * * out_matrix_rows ,
4253+ uint32_t * out_mapping_stride ,
4254+ uint32_t * out_n_skipped
4255+ ) {
42384256 uint8_t * mapping_buf = octx -> ctx -> ddr_spad_base ;
42394257 uint32_t mapping_stride = 1 ;
42404258 uint32_t * matrix_row_counts = (uint32_t * ) mapping_buf ;
42414259 struct mmid_row_mapping * matrix_rows = NULL ;
4260+ uint32_t n_skipped = 0 ;
42424261
42434262 if (act_nrows > 1 ) {
42444263 const size_t matrix_row_counts_size = n_as * sizeof (uint32_t );
42454264 assert (octx -> ctx -> ddr_spad_size >= matrix_row_counts_size );
42464265
42474266 hex_l2fetch_block ((const void * ) ids -> data , ids -> ne [1 ] * ids -> nb [1 ]);
42484267
4249- memset (matrix_row_counts , 0 , matrix_row_counts_size );
4250- scan_expert_ids (ids , n_ids , n_as , matrix_row_counts , NULL , 0 );
4268+ hvx_splat_u (matrix_row_counts , Q6_V_vzero (), n_as , sizeof ( uint32_t ) );
4269+ n_skipped = scan_expert_ids (ids , n_ids , n_as , matrix_row_counts , NULL , 0 );
42514270
42524271 uint32_t max_count = hvx_reduce_max_i32 ((const uint8_t * ) matrix_row_counts , n_as );
42534272 mapping_stride = max_count > 0 ? max_count : 1 ;
@@ -4265,11 +4284,72 @@ int op_matmul_id(struct htp_ops_context * octx) {
42654284 matrix_row_counts = (uint32_t * ) mapping_buf ;
42664285 matrix_rows = (struct mmid_row_mapping * ) (mapping_buf + matrix_row_counts_size );
42674286
4268- memset (matrix_row_counts , 0 , n_as * sizeof (uint32_t ));
4287+ hvx_splat_u (matrix_row_counts , Q6_V_vzero () , n_as , sizeof (uint32_t ));
42694288 scan_expert_ids (ids , n_ids , n_as , matrix_row_counts , matrix_rows , mapping_stride );
4289+ } else {
4290+ n_skipped = count_skipped_ids_single (ids , n_ids );
4291+ }
4292+
4293+ * out_mapping_buf = mapping_buf ;
4294+ * out_matrix_row_counts = matrix_row_counts ;
4295+ * out_matrix_rows = matrix_rows ;
4296+ * out_mapping_stride = mapping_stride ;
4297+ * out_n_skipped = n_skipped ;
4298+
4299+ return HTP_STATUS_OK ;
4300+ }
4301+
4302+ int op_matmul_id (struct htp_ops_context * octx ) {
4303+ htp_matmul_tensors_preamble ;
4304+
4305+ const struct htp_mm_kernel_params * kparams = (const struct htp_mm_kernel_params * ) octx -> kernel_params ;
4306+ struct htp_mm_context mmctx_struct = {0 };
4307+ struct htp_mm_context * mmctx = & mmctx_struct ;
4308+
4309+ const int status = htp_mm_init_context (octx , kparams );
4310+ if (status != HTP_STATUS_OK ) {
4311+ return status ;
42704312 }
42714313
4272- zero_skipped_dst_rows (ids , n_ids , dst , ne0 , nb1 , nb2 );
4314+ struct htp_thread_trace * tr = & octx -> ctx -> trace [0 ];
4315+ htp_trace_event_start (tr , HTP_TRACE_EVT_INIT , 0 );
4316+
4317+ mmctx -> octx = octx ;
4318+ mmctx -> act = act ;
4319+
4320+ const struct htp_tensor * restrict ids = octx -> src [2 ];
4321+ if (htp_tensor_is_extended (ids ) || htp_tensor_is_extended (act ) || htp_tensor_is_extended (dst )) {
4322+ return HTP_STATUS_NO_SUPPORT ;
4323+ }
4324+
4325+ const size_t src0_row_size = nb01 ;
4326+ const size_t dst_row_size = nb1 ;
4327+
4328+ const size_t src0_row_size_padded = hex_round_up (src0_row_size , 128 );
4329+
4330+ const uint32_t src0_nrows = ne01 ; // per expert
4331+ const uint32_t act_nrows = ne11 * ne12 * ne13 ;
4332+
4333+ // row groups
4334+ const int n_ids = ids -> ne [0 ]; // n_expert_used
4335+ const int n_as = ne02 ; // n_expert
4336+
4337+ uint8_t * mapping_buf = NULL ;
4338+ uint32_t * matrix_row_counts = NULL ;
4339+ struct mmid_row_mapping * matrix_rows = NULL ;
4340+ uint32_t mapping_stride = 1 ;
4341+ uint32_t n_skipped = 0 ;
4342+
4343+ int map_status = htp_mmid_prepare_mapping (octx , ids , n_ids , n_as , act_nrows ,
4344+ & mapping_buf , & matrix_row_counts , & matrix_rows ,
4345+ & mapping_stride , & n_skipped );
4346+ if (map_status != HTP_STATUS_OK ) {
4347+ return map_status ;
4348+ }
4349+
4350+ if (n_skipped > 0 ) {
4351+ zero_skipped_dst_rows (octx , ids , n_ids , dst );
4352+ }
42734353
42744354 mmctx -> matrix_row_counts = matrix_row_counts ;
42754355 mmctx -> matrix_rows = matrix_rows ;
@@ -4360,38 +4440,26 @@ int op_matmul_id_nx(struct htp_ops_context * octx) {
43604440 const int n_ids = ids -> ne [0 ];
43614441 const int n_as = src0 -> ne [2 ];
43624442
4363- uint8_t * mapping_buf = octx -> ctx -> ddr_spad_base ;
4364- uint32_t mapping_stride = 1 ;
4365- uint32_t * matrix_row_counts = (uint32_t * ) mapping_buf ;
4443+ uint8_t * mapping_buf = NULL ;
4444+ uint32_t * matrix_row_counts = NULL ;
43664445 struct mmid_row_mapping * matrix_rows = NULL ;
4446+ uint32_t mapping_stride = 1 ;
4447+ uint32_t n_skipped = 0 ;
43674448
4368- if (act_nrows > 1 ) {
4369- const size_t matrix_row_counts_size = n_as * sizeof (uint32_t );
4370- assert (octx -> ctx -> ddr_spad_size >= matrix_row_counts_size );
4371-
4372- hex_l2fetch_block ((const void * ) ids -> data , ids -> ne [1 ] * ids -> nb [1 ]);
4373-
4374- memset (matrix_row_counts , 0 , matrix_row_counts_size );
4375- scan_expert_ids (ids , n_ids , n_as , matrix_row_counts , NULL , 0 );
4376-
4377- uint32_t max_count = hvx_reduce_max_i32 ((const uint8_t * ) matrix_row_counts , n_as );
4378- mapping_stride = max_count > 0 ? max_count : 1 ;
4379-
4380- size_t matrix_row_map_size = n_as * mapping_stride * sizeof (struct mmid_row_mapping );
4381- const size_t total_map_size = matrix_row_counts_size + matrix_row_map_size ;
4449+ int map_status = htp_mmid_prepare_mapping (octx , ids , n_ids , n_as , act_nrows ,
4450+ & mapping_buf , & matrix_row_counts , & matrix_rows ,
4451+ & mapping_stride , & n_skipped );
4452+ if (map_status != HTP_STATUS_OK ) {
4453+ return map_status ;
4454+ }
43824455
4383- if (total_map_size > octx -> ctx -> ddr_spad_size ) {
4384- mapping_buf = memalign (128 , total_map_size );
4385- if (!mapping_buf ) {
4386- return HTP_STATUS_INTERNAL_ERR ;
4456+ if (n_skipped > 0 ) {
4457+ for (uint32_t p = 0 ; p < n_weights ; ++ p ) {
4458+ const struct htp_tensor * restrict d = octx -> dsts [p ];
4459+ if (d ) {
4460+ zero_skipped_dst_rows (octx , ids , n_ids , d );
43874461 }
43884462 }
4389-
4390- matrix_row_counts = (uint32_t * ) mapping_buf ;
4391- matrix_rows = (struct mmid_row_mapping * ) (mapping_buf + matrix_row_counts_size );
4392-
4393- memset (matrix_row_counts , 0 , n_as * sizeof (uint32_t ));
4394- scan_expert_ids (ids , n_ids , n_as , matrix_row_counts , matrix_rows , mapping_stride );
43954463 }
43964464
43974465 mmctx -> matrix_row_counts = matrix_row_counts ;
0 commit comments