Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-hexagon/htp/allreduce-ops.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-hexagon / htp / allreduce-ops.c` 475 lines ยท 38090 bytes Raw Blame History 1 #pragma clang diagnostic ignored "-Wunused-variable" 2 #pragma clang diagnostic ignored "-Wunused-function" 3 #pragma clang diagnostic ignored "-Wunused-but-set-variable" 4 5 #include 6 #include 7 #include 8 #include 9 #include 10 11 #define GGML_COMMON_DECL_C 12 #include "ggml-common.h" 13 #include "htp-ctx.h" 14 #include "htp-ops.h" 15 #include "hvx-utils.h" 16 #include "htp-tensor.h" 17 #include "dma-queue.h" 18 #include "hex-profile.h" 19 #include "allreduce-ops.h" 20 #include "htp-fence.h" 21 22 struct htp_allreduce_context { 23 struct htp_ops_context * octx; 24 uint32_t n_ranks; 25 uint32_t n_dsts; 26 uint32_t nelem; 27 uint32_t ne0; 28 uint32_t ne1; 29 uint32_t row_size_aligned; 30 uint32_t rank_elem_start; 31 uint32_t rank_nelem; 32 uint32_t elems_per_thread; 33 uint32_t block_elems; 34 uint32_t vtcm_size_per_thread; 35 bool is_row_bcast; 36 uint8_t * src_spad_base[HTP_ALLREDUCE_MAX_RANKS]; 37 uint8_t * dst_spad_base; 38 uint8_t * res_spad_base; 39 }; 40 41 #define DEFINE_ALLREDUCE_THREAD_DMA_1D(SUFFIX, TYPE, HVX_ADD_FN, HAS_ADD) \ 42 static void allreduce_thread_dma_1d_##SUFFIX(unsigned int nth, unsigned int ith, void * data) { \ 43 struct htp_allreduce_context * actx = (struct htp_allreduce_context *) data; \ 44 struct htp_ops_context * octx = actx->octx; \ 45 \ 46 const uint32_t n_ranks = actx->n_ranks; \ 47 const uint32_t n_dsts = actx->n_dsts; \ 48 const uint32_t block_elems = actx->block_elems; \ 49 \ 50 const uint32_t dr = actx->elems_per_thread; \ 51 const uint32_t ir0 = actx->rank_elem_start + dr * ith; \ 52 const uint32_t ir1 = MIN(ir0 + dr, actx->rank_elem_start + actx->rank_nelem); \ 53 if (ir0 >= ir1) return; \ 54 \ 55 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; \ 56 dma_queue * dma_q = octx->ctx->dma[ith]; \ 57 \ 58 const size_t vtcm_thread_offset = ith * actx->vtcm_size_per_thread; \ 59 uint8_t * dst_spad_base = actx->dst_spad_base + vtcm_thread_offset; \ 60 uint8_t * res_spad_base = HAS_ADD ? (actx->res_spad_base + vtcm_thread_offset) : NULL; \ 61 \ 62 const size_t spad_half = actx->vtcm_size_per_thread / 2; \ 63 uint32_t ir_prefetch = ir0; \ 64 int spad_idx = 0; \ 65 \ 66 for (int k = 0; k < 2 && ir_prefetch < ir1; k++) { \ 67 uint32_t cur_elems = MIN(block_elems, ir1 - ir_prefetch); \ 68 size_t cur_bytes = cur_elems * sizeof(TYPE); \ 69 uint8_t * d_spad = dst_spad_base + spad_idx * spad_half; \ 70 for (uint32_t d = 0; d < n_dsts; d++) { \ 71 dma_addr_t d_ddr = octx->dsts[d]->data + ir_prefetch * sizeof(TYPE); \ 72 dma_queue_push(dma_q, dma_make_data(d_ddr, d_spad), cur_bytes, cur_bytes, cur_bytes, 0); \ 73 } \ 74 for (uint32_t s = 0; s < n_ranks; s++) { \ 75 uint8_t * s_spad = actx->src_spad_base[s] + vtcm_thread_offset + spad_idx * spad_half; \ 76 const dma_addr_t s_ddr = octx->src[s]->data + ir_prefetch * sizeof(TYPE); \ 77 dma_queue_push(dma_q, dma_make_data(s_spad, s_ddr), cur_bytes, cur_bytes, cur_bytes, 1); \ 78 } \ 79 if (HAS_ADD) { \ 80 uint8_t * r_spad = res_spad_base + spad_idx * spad_half; \ 81 const dma_addr_t r_ddr = octx->src[2 * n_ranks]->data + ir_prefetch * sizeof(TYPE); \ 82 dma_queue_push(dma_q, dma_make_data(r_spad, r_ddr), cur_bytes, cur_bytes, cur_bytes, 1); \ 83 } \ 84 ir_prefetch += cur_elems; \ 85 spad_idx ^= 1; \ 86 } \ 87 \ 88 int comp_spad_idx = 0; \ 89 for (uint32_t ir = ir0; ir < ir1; ) { \ 90 uint32_t cur_elems = MIN(block_elems, ir1 - ir); \ 91 size_t cur_bytes = cur_elems * sizeof(TYPE); \ 92 for (uint32_t d = 0; d < n_dsts; d++) { \ 93 dma_queue_pop(dma_q); \ 94 } \ 95 for (uint32_t s = 0; s < n_ranks; s++) { \ 96 dma_queue_pop(dma_q); \ 97 } \ 98 if (HAS_ADD) { \ 99 dma_queue_pop(dma_q); \ 100 } \ 101 uint8_t * d_spad = dst_spad_base + comp_spad_idx * spad_half; \ 102 uint8_t * s0_spad = actx->src_spad_base[0] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 103 uint8_t * s1_spad = actx->src_spad_base[1] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 104 uint8_t * r_spad = HAS_ADD ? (res_spad_base + comp_spad_idx * spad_half) : NULL; \ 105 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); \ 106 HVX_ADD_FN(d_spad, s0_spad, s1_spad, cur_elems); \ 107 for (uint32_t s = 2; s < n_ranks; s++) { \ 108 uint8_t * ss_spad = actx->src_spad_base[s] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 109 HVX_ADD_FN(d_spad, d_spad, ss_spad, cur_elems); \ 110 } \ 111 if (HAS_ADD) { \ 112 HVX_ADD_FN(d_spad, d_spad, r_spad, cur_elems); \ 113 } \ 114 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); \ 115 for (uint32_t d = 0; d < n_dsts; d++) { \ 116 dma_addr_t d_ddr = octx->dsts[d]->data + ir * sizeof(TYPE); \ 117 dma_queue_push(dma_q, dma_make_data(d_ddr, d_spad), cur_bytes, cur_bytes, cur_bytes, 1); \ 118 } \ 119 if (ir_prefetch < ir1) { \ 120 uint32_t next_elems = MIN(block_elems, ir1 - ir_prefetch); \ 121 size_t next_bytes = next_elems * sizeof(TYPE); \ 122 for (uint32_t s = 0; s < n_ranks; s++) { \ 123 uint8_t * s_spad = actx->src_spad_base[s] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 124 const dma_addr_t s_next = octx->src[s]->data + ir_prefetch * sizeof(TYPE); \ 125 dma_queue_push(dma_q, dma_make_data(s_spad, s_next), next_bytes, next_bytes, next_bytes, 1); \ 126 } \ 127 if (HAS_ADD) { \ 128 uint8_t * r_spad_next = res_spad_base + comp_spad_idx * spad_half; \ 129 const dma_addr_t r_next = octx->src[2 * n_ranks]->data + ir_prefetch * sizeof(TYPE); \ 130 dma_queue_push(dma_q, dma_make_data(r_spad_next, r_next), next_bytes, next_bytes, next_bytes, 1); \ 131 } \ 132 ir_prefetch += next_elems; \ 133 } \ 134 comp_spad_idx ^= 1; \ 135 ir += cur_elems; \ 136 } \ 137 dma_queue_flush(dma_q); \ 138 } 139 140 DEFINE_ALLREDUCE_THREAD_DMA_1D(f16, __fp16, hvx_add_f16_aaa, 0) 141 DEFINE_ALLREDUCE_THREAD_DMA_1D(f32, float, hvx_add_f32_aaa, 0) 142 DEFINE_ALLREDUCE_THREAD_DMA_1D(add_f16, __fp16, hvx_add_f16_aaa, 1) 143 DEFINE_ALLREDUCE_THREAD_DMA_1D(add_f32, float, hvx_add_f32_aaa, 1) 144 145 #define DEFINE_ALLREDUCE_THREAD_DMA_2D(SUFFIX, TYPE, HVX_ADD_FN, HAS_ADD, IS_ROW_BCAST) \ 146 static void allreduce_thread_dma_2d_##SUFFIX(unsigned int nth, unsigned int ith, void * data) { \ 147 struct htp_allreduce_context * actx = (struct htp_allreduce_context *) data; \ 148 struct htp_ops_context * octx = actx->octx; \ 149 \ 150 const uint32_t n_ranks = actx->n_ranks; \ 151 const uint32_t n_dsts = actx->n_dsts; \ 152 const uint32_t ne0 = actx->ne0; \ 153 const uint32_t block_rows = actx->block_elems; \ 154 const uint32_t row_size_aligned = actx->row_size_aligned; \ 155 const uint32_t row_bytes = ne0 * sizeof(TYPE); \ 156 \ 157 const uint32_t dr = actx->elems_per_thread; \ 158 const uint32_t r0 = actx->rank_elem_start + dr * ith; \ 159 const uint32_t r1 = MIN(r0 + dr, actx->rank_elem_start + actx->rank_nelem); \ 160 if (r0 >= r1) return; \ 161 \ 162 struct htp_thread_trace * tr = &octx->ctx->trace[ith]; \ 163 dma_queue * dma_q = octx->ctx->dma[ith]; \ 164 \ 165 const size_t vtcm_thread_offset = ith * actx->vtcm_size_per_thread; \ 166 uint8_t * dst_spad_base = actx->dst_spad_base + vtcm_thread_offset; \ 167 uint8_t * res_spad_base = HAS_ADD ? (IS_ROW_BCAST ? actx->res_spad_base : (actx->res_spad_base + vtcm_thread_offset)) : NULL; \ 168 \ 169 const size_t spad_half = actx->vtcm_size_per_thread / 2; \ 170 uint32_t r_prefetch = r0; \ 171 int spad_idx = 0; \ 172 \ 173 for (int k = 0; k < 2 && r_prefetch < r1; k++) { \ 174 uint32_t cur_rows = MIN(block_rows, r1 - r_prefetch); \ 175 uint8_t * d_spad = dst_spad_base + spad_idx * spad_half; \ 176 for (uint32_t d = 0; d < n_dsts; d++) { \ 177 dma_addr_t d_ddr = octx->dsts[d]->data + r_prefetch * octx->dsts[d]->nb[1]; \ 178 dma_queue_push(dma_q, dma_make_data(d_ddr, d_spad), octx->dsts[d]->nb[1], row_size_aligned, row_bytes, 0); \ 179 } \ 180 for (uint32_t s = 0; s < n_ranks; s++) { \ 181 uint8_t * s_spad = actx->src_spad_base[s] + vtcm_thread_offset + spad_idx * spad_half; \ 182 const dma_addr_t s_ddr = octx->src[s]->data + r_prefetch * octx->src[s]->nb[1]; \ 183 dma_queue_push(dma_q, dma_make_data(s_spad, s_ddr), row_size_aligned, octx->src[s]->nb[1], row_bytes, cur_rows); \ 184 } \ 185 if (HAS_ADD && !IS_ROW_BCAST) { \ 186 uint8_t * r_spad = res_spad_base + spad_idx * spad_half; \ 187 const dma_addr_t r_ddr = octx->src[2 * n_ranks]->data + r_prefetch * octx->src[2 * n_ranks]->nb[1]; \ 188 dma_queue_push(dma_q, dma_make_data(r_spad, r_ddr), row_size_aligned, octx->src[2 * n_ranks]->nb[1], row_bytes, cur_rows); \ 189 } \ 190 r_prefetch += cur_rows; \ 191 spad_idx ^= 1; \ 192 } \ 193 \ 194 int comp_spad_idx = 0; \ 195 for (uint32_t r = r0; r < r1; ) { \ 196 uint32_t cur_rows = MIN(block_rows, r1 - r); \ 197 for (uint32_t d = 0; d < n_dsts; d++) { \ 198 dma_queue_pop(dma_q); \ 199 } \ 200 for (uint32_t s = 0; s < n_ranks; s++) { \ 201 dma_queue_pop(dma_q); \ 202 } \ 203 if (HAS_ADD && !IS_ROW_BCAST) { \ 204 dma_queue_pop(dma_q); \ 205 } \ 206 uint8_t * d_spad = dst_spad_base + comp_spad_idx * spad_half; \ 207 uint8_t * s0_spad = actx->src_spad_base[0] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 208 uint8_t * s1_spad = actx->src_spad_base[1] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 209 uint8_t * r_spad = (HAS_ADD && !IS_ROW_BCAST) ? (res_spad_base + comp_spad_idx * spad_half) : NULL; \ 210 htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); \ 211 for (uint32_t row = 0; row < cur_rows; row++) { \ 212 uint8_t * d_row = d_spad + row * row_size_aligned; \ 213 const uint8_t * s0_row = s0_spad + row * row_size_aligned; \ 214 const uint8_t * s1_row = s1_spad + row * row_size_aligned; \ 215 HVX_ADD_FN(d_row, s0_row, s1_row, ne0); \ 216 for (uint32_t s = 2; s < n_ranks; s++) { \ 217 const uint8_t * ss_row = actx->src_spad_base[s] + vtcm_thread_offset + comp_spad_idx * spad_half + row * row_size_aligned; \ 218 HVX_ADD_FN(d_row, d_row, ss_row, ne0); \ 219 } \ 220 if (HAS_ADD) { \ 221 const uint8_t * res_row = IS_ROW_BCAST ? res_spad_base : (r_spad + row * row_size_aligned); \ 222 HVX_ADD_FN(d_row, d_row, res_row, ne0); \ 223 } \ 224 } \ 225 htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r); \ 226 for (uint32_t d = 0; d < n_dsts; d++) { \ 227 dma_addr_t d_ddr = octx->dsts[d]->data + r * octx->dsts[d]->nb[1]; \ 228 dma_queue_push(dma_q, dma_make_data(d_ddr, d_spad), octx->dsts[d]->nb[1], row_size_aligned, row_bytes, cur_rows); \ 229 } \ 230 if (r_prefetch < r1) { \ 231 uint32_t next_rows = MIN(block_rows, r1 - r_prefetch); \ 232 for (uint32_t s = 0; s < n_ranks; s++) { \ 233 uint8_t * s_spad = actx->src_spad_base[s] + vtcm_thread_offset + comp_spad_idx * spad_half; \ 234 const dma_addr_t s_next = octx->src[s]->data + r_prefetch * octx->src[s]->nb[1]; \ 235 dma_queue_push(dma_q, dma_make_data(s_spad, s_next), row_size_aligned, octx->src[s]->nb[1], row_bytes, next_rows); \ 236 } \ 237 if (HAS_ADD && !IS_ROW_BCAST) { \ 238 uint8_t * r_spad_next = res_spad_base + comp_spad_idx * spad_half; \ 239 const dma_addr_t r_next = octx->src[2 * n_ranks]->data + r_prefetch * octx->src[2 * n_ranks]->nb[1]; \ 240 dma_queue_push(dma_q, dma_make_data(r_spad_next, r_next), row_size_aligned, octx->src[2 * n_ranks]->nb[1], row_bytes, next_rows); \ 241 } \ 242 r_prefetch += next_rows; \ 243 } \ 244 comp_spad_idx ^= 1; \ 245 r += cur_rows; \ 246 } \ 247 dma_queue_flush(dma_q); \ 248 } 249 250 DEFINE_ALLREDUCE_THREAD_DMA_2D(f16, __fp16, hvx_add_f16_aaa, 0, 0) 251 DEFINE_ALLREDUCE_THREAD_DMA_2D(f32, float, hvx_add_f32_aaa, 0, 0) 252 DEFINE_ALLREDUCE_THREAD_DMA_2D(add_f16, __fp16, hvx_add_f16_aaa, 1, 0) 253 DEFINE_ALLREDUCE_THREAD_DMA_2D(add_f32, float, hvx_add_f32_aaa, 1, 0) 254 DEFINE_ALLREDUCE_THREAD_DMA_2D(add_bcast_f16, __fp16, hvx_add_f16_aaa, 1, 1) 255 DEFINE_ALLREDUCE_THREAD_DMA_2D(add_bcast_f32, float, hvx_add_f32_aaa, 1, 1) 256 257 static int validate_allreduce( 258 struct htp_ops_context * octx, 259 const struct htp_allreduce_kernel_params * kparams, 260 uint32_t n_ranks 261 ) { 262 if (kparams->n_threads == 0 || (uint32_t) kparams->n_threads > octx->ctx->n_threads) { 263 return HTP_STATUS_INVAL_PARAMS; 264 } 265 octx->n_threads = (uint32_t) kparams->n_threads; 266 267 if (kparams->vtcm_size_per_thread <= 0 || kparams->vtcm_size <= 0) { 268 return HTP_STATUS_INVAL_PARAMS; 269 } 270 271 const bool has_add = (octx->op == HTP_OP_ALLREDUCE_ADD); 272 const size_t n_vtcm_buffers = htp_allreduce_vtcm_buffer_count( 273 n_ranks, octx->n_threads, has_add, kparams->is_row_bcast != 0); 274 const size_t vtcm_size = n_vtcm_buffers * (size_t) kparams->vtcm_size_per_thread; 275 if (vtcm_size != (size_t) kparams->vtcm_size) { 276 return HTP_STATUS_INVAL_PARAMS; 277 } 278 if (vtcm_size > octx->ctx->vtcm_size) { 279 return HTP_STATUS_VTCM_TOO_SMALL; 280 } 281 282 if (octx->dst->type != HTP_TYPE_F16 && octx->dst->type != HTP_TYPE_F32) { 283 return HTP_STATUS_NO_SUPPORT; 284 } 285 286 return HTP_STATUS_OK; 287 } 288 289 int op_allreduce(struct htp_ops_context * octx) { 290 if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx > 0) { 291 return HTP_STATUS_OK; 292 } 293 294 const struct htp_allreduce_kernel_params * kparams = (const struct htp_allreduce_kernel_params *) octx->kernel_params; 295 const struct htp_tensor * dst = octx->dst; 296 297 const uint32_t rank = (uint32_t) kparams->rank; 298 const uint32_t n_ranks = (uint32_t) kparams->n_ranks; 299 300 if (n_ranks < 2 || n_ranks > HTP_ALLREDUCE_MAX_RANKS || rank >= n_ranks) { 301 return HTP_STATUS_INVAL_PARAMS; 302 } 303 304 const uint32_t fence_seq_entry = (uint32_t) octx->op_params[0]; 305 const uint32_t fence_seq_exit = (uint32_t) octx->op_params[1]; 306 307 const struct htp_tensor * my_sync = octx->src[n_ranks + rank]; 308 atomic_uint * my_fence = (atomic_uint *) (uintptr_t) my_sync->data; 309 310 const int status = validate_allreduce(octx, kparams, n_ranks); 311 if (status != HTP_STATUS_OK) { 312 if (status == HTP_STATUS_NO_SUPPORT) { 313 FARF(ERROR, "ggml-hex: allreduce unsupported type %d : rank %u\n", dst->type, rank); 314 } 315 htp_fence_write(my_fence, fence_seq_exit, status); 316 return status; 317 } 318 319 const bool has_add = (octx->op == HTP_OP_ALLREDUCE_ADD); 320 const uint32_t nelem = dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3]; 321 const int32_t mode = kparams->mode; 322 323 // 1. Entry Barrier: Synchronize all ranks before reading 324 struct htp_thread_trace * tr0 = &octx->ctx->trace[0]; 325 htp_trace_event_start(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_entry); 326 327 htp_fence_write(my_fence, fence_seq_entry, octx->status); 328 329 for (uint32_t j = 0; j < n_ranks; j++) { 330 if (j == rank) continue; 331 const struct htp_tensor * peer_sync = octx->src[n_ranks + j]; 332 atomic_uint * peer_fence = (atomic_uint *) (uintptr_t) peer_sync->data; 333 uint64_t spins = 0; 334 while (1) { 335 uint32_t peer_seq; 336 uint32_t peer_status; 337 htp_fence_read(peer_fence, &peer_seq, &peer_status); 338 if ((int32_t)(peer_seq - fence_seq_entry) >= 0) { 339 if (peer_status > HTP_STATUS_OK) { 340 FARF(ERROR, "ggml-hex: allreduce entry peer %u failed with status %u\n", j, peer_status); 341 htp_fence_write(my_fence, fence_seq_exit, peer_status); 342 htp_trace_event_stop(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_entry); 343 return peer_status; 344 } 345 break; 346 } 347 if (++spins > HTP_FENCE_TIMEOUT) { 348 FARF(ERROR, "ggml-hex: allreduce entry fence-wait TIMEOUT : rank %u waiting on %u fence %p seq 0x%x peer-seq 0x%x\n", 349 rank, j, peer_fence, fence_seq_entry, peer_seq); 350 htp_fence_write(my_fence, fence_seq_exit, HTP_STATUS_INTERNAL_ERR); 351 htp_trace_event_stop(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_entry); 352 return HTP_STATUS_INTERNAL_ERR; 353 } 354 hex_pause(); 355 } 356 } 357 asm volatile ("syncht" : : : "memory"); 358 359 htp_trace_event_stop(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_entry); 360 361 // 2. Multi-threaded Reduction across assigned rank chunk 362 if (nelem > 0) { 363 const uint32_t n_threads = (uint32_t) kparams->n_threads; 364 const uint32_t block_elems = (uint32_t) kparams->block_elems; 365 const uint32_t elems_per_thread = (uint32_t) kparams->elems_per_thread; 366 const uint32_t vtcm_size_per_thread = (uint32_t) kparams->vtcm_size_per_thread; 367 368 struct htp_allreduce_context actx; 369 actx.octx = octx; 370 actx.n_ranks = n_ranks; 371 actx.n_dsts = (uint32_t) kparams->n_dsts ? (uint32_t) kparams->n_dsts : n_ranks; 372 actx.nelem = nelem; 373 actx.ne0 = (uint32_t) kparams->ne0; 374 actx.ne1 = (uint32_t) kparams->ne1; 375 actx.row_size_aligned = (uint32_t) kparams->row_size_aligned; 376 actx.rank_elem_start = (uint32_t) kparams->rank_elem_start; 377 actx.rank_nelem = (uint32_t) kparams->rank_nelem; 378 actx.elems_per_thread = elems_per_thread; 379 actx.block_elems = block_elems; 380 actx.vtcm_size_per_thread = vtcm_size_per_thread; 381 actx.is_row_bcast = (kparams->is_row_bcast != 0); 382 383 work_queue_func_t reduce_fun = NULL; 384 switch (kparams->kernel_type) { 385 case HTP_ALLREDUCE_KERNEL_DMA_1D: 386 if (has_add) { 387 reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_1d_add_f16 : allreduce_thread_dma_1d_add_f32; 388 } else { 389 reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_1d_f16 : allreduce_thread_dma_1d_f32; 390 } 391 break; 392 case HTP_ALLREDUCE_KERNEL_DMA_2D: 393 if (has_add) { 394 if (kparams->is_row_bcast) { 395 reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_2d_add_bcast_f16 : allreduce_thread_dma_2d_add_bcast_f32; 396 } else { 397 reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_2d_add_f16 : allreduce_thread_dma_2d_add_f32; 398 } 399 } else { 400 reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_2d_f16 : allreduce_thread_dma_2d_f32; 401 } 402 break; 403 default: 404 FARF(ERROR, "ggml-hex: allreduce unsupported kernel %d : rank %u\n", kparams->kernel_type, rank); 405 htp_fence_write(my_fence, fence_seq_exit, HTP_STATUS_NO_SUPPORT); 406 return HTP_STATUS_NO_SUPPORT; 407 } 408 409 uint8_t * vtcm_ptr = (uint8_t *) octx->ctx->vtcm_base; 410 for (uint32_t s = 0; s < n_ranks; s++) { 411 actx.src_spad_base[s] = vtcm_ptr; 412 vtcm_ptr += n_threads * vtcm_size_per_thread; 413 } 414 actx.dst_spad_base = vtcm_ptr; 415 vtcm_ptr += n_threads * vtcm_size_per_thread; 416 if (has_add) { 417 actx.res_spad_base = vtcm_ptr; 418 vtcm_ptr += (actx.is_row_bcast ? 1 : n_threads) * vtcm_size_per_thread; 419 } 420 421 if (has_add && actx.is_row_bcast) { 422 const dma_addr_t r_ddr = octx->src[2 * n_ranks]->data; 423 const uint32_t row_bytes = actx.ne0 * (dst->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float)); 424 dma_queue * dma_q = octx->ctx->dma[0]; 425 dma_queue_push(dma_q, dma_make_data(actx.res_spad_base, r_ddr), actx.row_size_aligned, 0, row_bytes, 1); 426 dma_queue_pop(dma_q); 427 } 428 429 work_queue_run(octx->ctx->work_queue, reduce_fun, &actx, n_threads); 430 } 431 432 // 4. Exit Barrier: Synchronize all ranks after writing 433 htp_trace_event_start(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_exit); 434 435 // drain fan-out DMA writes before exit barrier 436 if (mode == HTP_ALLREDUCE_SHARDED_FANOUT) { 437 asm volatile ("syncht" : : : "memory"); 438 } 439 440 htp_fence_write(my_fence, fence_seq_exit, octx->status); 441 442 for (uint32_t j = 0; j < n_ranks; j++) { 443 if (j == rank) continue; 444 const struct htp_tensor * peer_sync = octx->src[n_ranks + j]; 445 atomic_uint * peer_fence = (atomic_uint *) (uintptr_t) peer_sync->data; 446 uint64_t spins = 0; 447 while (1) { 448 uint32_t peer_seq; 449 uint32_t peer_status; 450 htp_fence_read(peer_fence, &peer_seq, &peer_status); 451 if ((int32_t)(peer_seq - fence_seq_exit) >= 0) { 452 if (peer_status > HTP_STATUS_OK) { 453 FARF(ERROR, "ggml-hex: allreduce exit peer %u failed with status %u\n", j, peer_status); 454 htp_fence_write(my_fence, fence_seq_exit, peer_status); 455 htp_trace_event_stop(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_exit); 456 return peer_status; 457 } 458 break; 459 } 460 if (++spins > HTP_FENCE_TIMEOUT) { 461 FARF(ERROR, "ggml-hex: allreduce exit fence-wait TIMEOUT : rank %u waiting on %u fence %p seq 0x%x peer-seq 0x%x\n", 462 rank, j, peer_fence, fence_seq_exit, peer_seq); 463 htp_fence_write(my_fence, fence_seq_exit, HTP_STATUS_INTERNAL_ERR); 464 htp_trace_event_stop(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_exit); 465 return HTP_STATUS_INTERNAL_ERR; 466 } 467 hex_pause(); 468 } 469 } 470 asm volatile ("syncht" : : : "memory"); 471 472 htp_trace_event_stop(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_exit); 473 474 return octx->status; 475 }