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/dma-copy.h # 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 / dma-copy.h` 169 lines ยท 7101 bytes Raw Blame History 1 #ifndef HTP_DMA_COPY_H 2 #define HTP_DMA_COPY_H 3 4 // DDR<->DDR DMA copies of same-type, same-shape tensors with arbitrary strides. 5 // Used by CPY for the copy itself and by CONCAT, which is two such copies into 6 // two views of its destination. Every helper only pushes descriptors; the 7 // caller flushes the queue when it needs the data. 8 9 #include "dma-queue.h" 10 #include "hex-common.h" 11 #include "htp-tensor.h" 12 13 #include 14 #include 15 16 // Contiguous byte run, as 1d transfers of at most DMA_SAFE_CHUNK_SIZE each. 17 static inline void dma_cpy_sametype_reshape_contig(dma_queue * dma_q, 18 dma_addr_t dst, 19 dma_addr_t src0, 20 uint32_t total_bytes) { 21 if (total_bytes == 0) { 22 return; 23 } 24 25 const uint32_t max_chunk = DMA_SAFE_CHUNK_SIZE; 26 while (total_bytes > 0) { 27 const uint32_t chunk = MIN(total_bytes, max_chunk); 28 if (!dma_queue_push(dma_q, dma_make_data(dst, src0), chunk, chunk, chunk, /*nrows=*/1)) { 29 dma_queue_flush(dma_q); 30 dma_queue_push(dma_q, dma_make_data(dst, src0), chunk, chunk, chunk, /*nrows=*/1); 31 } 32 dst += chunk; 33 src0 += chunk; 34 total_bytes -= chunk; 35 } 36 } 37 38 // One 2d transfer, split at the 16-bit nrows field. 39 static inline void dma_cpy_push_2d_chunked(dma_queue * dma_q, 40 dma_addr_t dst, 41 dma_addr_t src, 42 size_t dst_stride, 43 size_t src_stride, 44 size_t row_size, 45 uint32_t nrows) { 46 if (row_size == 0 || nrows == 0) { 47 return; 48 } 49 50 while (nrows > 0) { 51 const uint32_t cur_rows = MIN(nrows, DMA_MAX_NROWS); 52 if (!dma_queue_push(dma_q, dma_make_data(dst, src), dst_stride, src_stride, row_size, cur_rows)) { 53 dma_queue_flush(dma_q); 54 dma_queue_push(dma_q, dma_make_data(dst, src), dst_stride, src_stride, row_size, cur_rows); 55 } 56 dst += cur_rows * dst_stride; 57 src += cur_rows * src_stride; 58 nrows -= cur_rows; 59 } 60 } 61 62 // Copy a range of rows [row_start, row_start + nrows) from src0 into dst: 63 // same type, same ne[], any nb[] above dim 0, dim 0 dense on both sides (nb[0] == elem_size). 64 static inline void dma_cpy_sametype_sameshape_range(dma_queue * dma_q, 65 const struct htp_tensor * dst, 66 const struct htp_tensor * src0, 67 uint32_t elem_size, 68 uint32_t row_start, 69 uint32_t nrows) { 70 if (nrows == 0) { 71 return; 72 } 73 74 const uint32_t ne00 = src0->ne[0]; 75 const uint32_t ne01 = src0->ne[1]; 76 const uint32_t ne02 = src0->ne[2]; 77 const uint32_t ne03 = src0->ne[3]; 78 79 if (ne00 == 0 || ne01 == 0 || ne02 == 0 || ne03 == 0) { 80 return; 81 } 82 83 const uint32_t nb01 = src0->nb[1]; 84 const uint32_t nb02 = src0->nb[2]; 85 const uint32_t nb03 = src0->nb[3]; 86 87 const uint32_t nb1 = dst->nb[1]; 88 const uint32_t nb2 = dst->nb[2]; 89 const uint32_t nb3 = dst->nb[3]; 90 91 const bool contiguous = htp_tensor_is_contiguous(src0, elem_size) && htp_tensor_is_contiguous(dst, elem_size); 92 93 if (contiguous) { 94 dma_cpy_sametype_reshape_contig(dma_q, 95 dst->data + (dma_addr_t) row_start * ne00 * elem_size, 96 src0->data + (dma_addr_t) row_start * ne00 * elem_size, 97 nrows * ne00 * elem_size); 98 return; 99 } 100 101 // The single-descriptor path flattens (i01,i02,i03) into one row index, so every 102 // row must sit at a constant stride: nb01 on the source, nb1 on the destination. 103 // Walk the outer dims and require each to continue that progression. A dim of 104 // extent 1 spans no rows, so it is skipped -- but its own stride must NOT then be 105 // used to justify the next dim's stride, which is what comparing nb03 against 106 // ne02*nb02 did: ggml leaves the stride of an extent-1 dim meaningless, so a view 107 // could pass the check while its rows were nowhere near that stride. 108 uint32_t exp_src = ne01 * nb01; 109 uint32_t exp_dst = ne01 * nb1; 110 bool contiguous_outer = true; 111 if (ne02 != 1) { 112 contiguous_outer = contiguous_outer && (nb02 == exp_src) && (nb2 == exp_dst); 113 } 114 exp_src *= ne02; 115 exp_dst *= ne02; 116 if (ne03 != 1) { 117 contiguous_outer = contiguous_outer && (nb03 == exp_src) && (nb3 == exp_dst); 118 } 119 120 if (contiguous_outer) { 121 dma_cpy_push_2d_chunked(dma_q, 122 dst->data + (dma_addr_t) row_start * nb1, 123 src0->data + (dma_addr_t) row_start * nb01, 124 nb1, nb01, ne00 * elem_size, nrows); 125 return; 126 } 127 128 const uint32_t ne02_ne01 = ne02 * ne01; 129 uint32_t i03 = row_start / ne02_ne01; 130 uint32_t rem = row_start - i03 * ne02_ne01; 131 uint32_t i02 = rem / ne01; 132 uint32_t i01 = rem - i02 * ne01; 133 134 dma_addr_t cur_dst = dst->data + (dma_addr_t) i01 * nb1 + (dma_addr_t) i02 * nb2 + (dma_addr_t) i03 * nb3; 135 dma_addr_t cur_src0 = src0->data + (dma_addr_t) i01 * nb01 + (dma_addr_t) i02 * nb02 + (dma_addr_t) i03 * nb03; 136 137 uint32_t r = row_start; 138 const uint32_t row_end = row_start + nrows; 139 while (r < row_end) { 140 uint32_t cur_rows = MIN(row_end - r, ne01 - i01); 141 dma_cpy_push_2d_chunked(dma_q, cur_dst, cur_src0, nb1, nb01, ne00 * elem_size, cur_rows); 142 r += cur_rows; 143 i01 += cur_rows; 144 if (i01 == ne01) { 145 i01 = 0; 146 if (++i02 == ne02) { 147 i02 = 0; 148 i03++; 149 } 150 cur_dst = dst->data + (dma_addr_t) i02 * nb2 + (dma_addr_t) i03 * nb3; 151 cur_src0 = src0->data + (dma_addr_t) i02 * nb02 + (dma_addr_t) i03 * nb03; 152 } else { 153 cur_dst += cur_rows * nb1; 154 cur_src0 += cur_rows * nb01; 155 } 156 } 157 } 158 159 // Copy src0 into dst: same type, same ne[], any nb[] above dim 0, dim 0 dense on 160 // both sides (nb[0] == elem_size). 161 static inline void dma_cpy_sametype_sameshape(dma_queue * dma_q, 162 const struct htp_tensor * dst, 163 const struct htp_tensor * src0, 164 uint32_t elem_size) { 165 const uint32_t total_rows = src0->ne[1] * src0->ne[2] * src0->ne[3]; 166 dma_cpy_sametype_sameshape_range(dma_q, dst, src0, elem_size, 0, total_rows); 167 } 168 169 #endif /* HTP_DMA_COPY_H */