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-zdnn/common.hpp # 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-zdnn / common.hpp` 193 lines ยท 6389 bytes Raw Blame History 1 #ifndef GGML_ZDNN_COMMON_HPP 2 #define GGML_ZDNN_COMMON_HPP 3 4 #include "ggml.h" 5 #include "ggml-impl.h" 6 7 #include "zdnn.h" 8 9 #include "scratch.hpp" 10 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 19 #define GGML_ZDNN_NAME "zDNN" 20 #define GGML_ZDNN_VERSION ZDNN_VERNUM 21 22 #define ZDNN_CHECK(stmt) \ 23 do { \ 24 zdnn_status status = (stmt); \ 25 GGML_ASSERT(status == ZDNN_OK); \ 26 } while (0); 27 28 struct ggml_backend_zdnn_device_context { 29 int zdnn_device; 30 int zdnn_device_ref_count; 31 32 bool has_parmblkformat_0; 33 bool has_parmblkformat_1; // checks for z17 34 35 size_t max_size; 36 37 int32_t n_threads; // CPU threads 38 39 char name[128]; 40 }; 41 42 struct ggml_backend_zdnn_buffer { 43 void * data; 44 ggml_backend_zdnn_buffer * extra; // for bias, etc. 45 size_t size; 46 47 zdnn_tensor_desc pre_tfm_desc; 48 zdnn_tensor_desc tfm_desc; 49 zdnn_ztensor ztensor; 50 51 std::vector scales; // per-row scales for quantized weights 52 std::vector channel_scales; // per-input-channel scales for quantized weights, applied to the inputs 53 54 char name[GGML_MAX_NAME]; 55 }; 56 57 struct ggml_backend_zdnn_buffer_context { 58 void * all_data; 59 size_t all_size; 60 bool owned; 61 62 int n_buffers; 63 std::vector> buffers; 64 }; 65 66 struct ggml_backend_zdnn_context { 67 int device; 68 int32_t n_threads; // for the CPU work around zDNN calls 69 ggml_cgraph * gf; 70 71 // zero bias for stacked quantized matmuls, keyed by (n_stacks, ne0) 72 std::map, std::unique_ptr> zero_bias; 73 74 ggml_zdnn_pool pool; // scratch memory of the ops 75 }; 76 77 // 78 // Scratch buffers 79 // 80 81 // the ztensor uses the memory of scratch, so scratch must outlive it 82 static inline void ggml_zdnn_init_scratch_ztensor(ggml_backend_zdnn_context * ctx, 83 zdnn_tensor_desc * pre_tfm_desc, 84 zdnn_tensor_desc * tfm_desc, 85 zdnn_ztensor * ztensor, 86 ggml_zdnn_pool_alloc & scratch) { 87 88 const uint64_t size = zdnn_getsize_ztensor(tfm_desc); 89 90 zdnn_init_quantized_ztensor(pre_tfm_desc, tfm_desc, 1.0f, 0.0f, ztensor); 91 ztensor->buffer = scratch.alloc(ctx->pool, size); 92 ztensor->buffer_size = size; 93 } 94 95 // same as ggml_zdnn_init_scratch_ztensor, for a DLFLOAT16 ztensor 96 static inline void ggml_zdnn_init_scratch_ztensor_dlf16(ggml_backend_zdnn_context * ctx, 97 zdnn_tensor_desc * pre_tfm_desc, 98 zdnn_tensor_desc * tfm_desc, 99 zdnn_ztensor * ztensor, 100 ggml_zdnn_pool_alloc & scratch) { 101 102 const uint64_t size = zdnn_getsize_ztensor(tfm_desc); 103 104 zdnn_init_ztensor(pre_tfm_desc, tfm_desc, ztensor); 105 ztensor->buffer = scratch.alloc(ctx->pool, size); 106 ztensor->buffer_size = size; 107 } 108 109 // 110 // Stacked ztensors 111 // 112 113 // view of the stacks [s0, s0 + n) of a stacked ztensor, the stacks are the outermost dim of its buffer 114 // the view descs must outlive the view 115 static inline zdnn_ztensor ggml_zdnn_stack_view(zdnn_tensor_desc * pre_tfm_desc, 116 zdnn_tensor_desc * tfm_desc, 117 const zdnn_ztensor & src, 118 int64_t n_stacks, 119 int64_t s0, 120 int64_t n) { 121 122 GGML_ASSERT(src.buffer_size % n_stacks == 0); 123 const uint64_t stack_bytes = src.buffer_size / n_stacks; 124 125 *pre_tfm_desc = *src.pre_transformed_desc; 126 *tfm_desc = *src.transformed_desc; 127 128 switch (pre_tfm_desc->layout) { 129 case ZDNN_3DS: 130 { 131 pre_tfm_desc->dim3 = n; 132 } break; 133 case ZDNN_2DS: 134 { 135 pre_tfm_desc->dim2 = n; 136 } break; 137 default: 138 GGML_ABORT("%s: unsupported stacked layout", __func__); 139 } 140 141 tfm_desc->dim4 = n; 142 GGML_ASSERT(zdnn_getsize_ztensor(tfm_desc) == n*stack_bytes); 143 144 zdnn_ztensor view = src; 145 view.pre_transformed_desc = pre_tfm_desc; 146 view.transformed_desc = tfm_desc; 147 view.buffer = (char *) src.buffer + s0*stack_bytes; 148 view.buffer_size = n * stack_bytes; 149 return view; 150 } 151 152 // zero bias of n_stacks stacks, created once per shape as it never changes 153 static inline const zdnn_ztensor & ggml_zdnn_zero_bias(ggml_backend_zdnn_context * ctx, int64_t n_stacks, int64_t ne0) { 154 std::unique_ptr & bias = ctx->zero_bias[{ n_stacks, ne0 }]; 155 156 if (!bias) { 157 bias = std::make_unique(); 158 159 std::vector zeros(n_stacks * ne0, 0.0f); 160 161 zdnn_init_pre_transformed_desc(ZDNN_2DS, FP32, &bias->pre_tfm_desc, n_stacks, ne0); 162 ZDNN_CHECK(zdnn_generate_transformed_desc(&bias->pre_tfm_desc, &bias->tfm_desc)); 163 ZDNN_CHECK(zdnn_init_ztensor_with_malloc(&bias->pre_tfm_desc, &bias->tfm_desc, &bias->ztensor)); 164 ZDNN_CHECK(zdnn_transform_ztensor(&bias->ztensor, zeros.data())); 165 } 166 167 return bias->ztensor; 168 } 169 170 // 171 // Threading 172 // 173 174 // split [0, n) into n_threads ranges, the calling thread runs the first range 175 template 176 static inline void ggml_zdnn_parallel_for(int32_t n_threads, int64_t n, const F & f) { 177 n_threads = (int32_t)std::max(1, std::min(n_threads, n)); 178 179 std::vector> tasks; 180 for (int32_t ith = 1; ith < n_threads; ith++) { 181 const int64_t start = (ith + 0) * n / n_threads; 182 const int64_t end = (ith + 1) * n / n_threads; 183 tasks.push_back(std::async(std::launch::async, [&f, start, end]() { f(start, end); })); 184 } 185 186 f(0, n/n_threads); 187 188 for (auto & task : tasks) { 189 task.get(); 190 } 191 } 192 193 #endif // GGML_ZDNN_COMMON_HPP