File size: 6,707 Bytes
13d3ba0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
#include "ggml/ggml.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#if defined(_WIN32)
#include <windows.h>
typedef volatile LONG atomic_int;
static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
return InterlockedExchangeAdd(ptr, inc);
}
#else
#include <stdatomic.h>
#endif
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
struct ggml_context * make_ctx(void) {
struct ggml_init_params params = {
/*.mem_size =*/ 1 * 1024 * 1024,
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ false,
};
return ggml_init(params);
}
char g_userdata[] = "ggml";
atomic_int g_custom1_count = 0;
atomic_int g_custom2_count = 0;
atomic_int g_custom3_count = 0;
void custom1(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata) {
// check that the userdata is correct
assert(userdata == NULL);
assert(ggml_are_same_shape(dst, a));
atomic_fetch_add(&g_custom1_count, 1);
const float * a_data = ggml_get_data_f32(a);
float * dst_data = ggml_get_data_f32(dst);
// this assumes that the tensors are contiguous
assert(ggml_is_contiguous(dst));
assert(ggml_is_contiguous(a));
// parallelize by elements
const int ne = (int)ggml_nelements(dst);
const int dr = (ne + nth - 1) / nth;
const int ie0 = dr * ith;
const int ie1 = MIN(ie0 + dr, ne);
for (int i = ie0; i < ie1; ++i) {
dst_data[i] = a_data[i] * 2;
}
}
void custom2(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata) {
// check that the userdata is correct
assert(userdata == g_userdata);
assert(strcmp(userdata, "ggml") == 0);
assert(ggml_are_same_shape(dst, a));
assert(ggml_are_same_shape(dst, b));
atomic_fetch_add(&g_custom2_count, 1);
const float * a_data = ggml_get_data_f32(a);
const float * b_data = ggml_get_data_f32(b);
float * dst_data = ggml_get_data_f32(dst);
// parallelize by rows
const int nr = (int)ggml_nrows(dst);
// number of rows per thread
const int dr = (nr + nth - 1) / nth;
// row range for this thread
const int ir0 = dr * ith;
const int ir1 = MIN(ir0 + dr, nr);
// number of columns
const int nc = (int)dst->ne[0];
// this assumes that the tensors are contiguous
assert(ggml_is_contiguous(dst));
assert(ggml_is_contiguous(a));
assert(ggml_is_contiguous(b));
for (int ir = ir0; ir < ir1; ++ir) {
for (int ic = 0; ic < nc; ++ic) {
const int i = ir * nc + ic;
dst_data[i] = a_data[i] + b_data[i];
}
}
}
void custom3(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata) {
// check that the userdata is correct
assert(userdata == g_userdata);
assert(strcmp(userdata, "ggml") == 0);
assert(ggml_are_same_shape(dst, a));
assert(ggml_are_same_shape(dst, b));
assert(ggml_are_same_shape(dst, c));
atomic_fetch_add(&g_custom3_count, 1);
const float * a_data = ggml_get_data_f32(a);
const float * b_data = ggml_get_data_f32(b);
const float * c_data = ggml_get_data_f32(c);
float * dst_data = ggml_get_data_f32(dst);
// dont parallelize
assert(ith == 0);
// number of elements
const int ne = (int)ggml_nelements(dst);
// this assumes that the tensors are contiguous
assert(ggml_is_contiguous(dst));
assert(ggml_is_contiguous(a));
assert(ggml_is_contiguous(b));
assert(ggml_is_contiguous(c));
for (int i = 0; i < ne; ++i) {
dst_data[i] = a_data[i] + b_data[i] + c_data[i];
}
}
int main(int argc, const char** argv) {
float buf1_f32[1024];
for (int i = 0; i < 1024; ++i) {
buf1_f32[i] = (float)(i + 1);
}
float buf2_f32[1024];
for (int i = 0; i < 1024; ++i) {
buf2_f32[i] = (float)(i + 1) * 2;
}
float buf3_f32[1024];
for (int i = 0; i < 1024; ++i) {
buf3_f32[i] = (float)(i + 1) * 3;
}
// map_custom1
// 2 tasks, no userdata, parallelized by elements
{
struct ggml_context * ctx = make_ctx();
struct ggml_tensor * t = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
memcpy(t->data, buf1_f32, ggml_nbytes(t));
struct ggml_tensor * m1 = ggml_map_custom1(ctx, t, custom1, 2, NULL);
struct ggml_cgraph graph = ggml_build_forward(m1);
ggml_graph_compute_with_ctx(ctx, &graph, 4);
const float * output = ggml_get_data_f32(m1);
for (int i = 0; i < ggml_nelements(m1); ++i) {
assert(output[i] == buf1_f32[i] * 2);
}
assert(g_custom1_count == 2);
ggml_free(ctx);
}
// map_custom2
// max tasks (4), userdata, parallelized by rows
{
struct ggml_context * ctx = make_ctx();
struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
struct ggml_tensor * m2 = ggml_map_custom2(ctx, t1, t2, custom2, GGML_N_TASKS_MAX, g_userdata);
struct ggml_cgraph graph = ggml_build_forward(m2);
ggml_graph_compute_with_ctx(ctx, &graph, 4);
const float * output = ggml_get_data_f32(m2);
for (int i = 0; i < ggml_nelements(m2); ++i) {
assert(output[i] == buf1_f32[i] + buf2_f32[i]);
}
assert(g_custom2_count == 4);
ggml_free(ctx);
}
// map_custom3
// 1 task, userdata, not parallelized
{
struct ggml_context * ctx = make_ctx();
struct ggml_tensor * t1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
memcpy(t1->data, buf1_f32, ggml_nbytes(t1));
struct ggml_tensor * t2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
memcpy(t2->data, buf2_f32, ggml_nbytes(t2));
struct ggml_tensor * t3 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 2);
memcpy(t3->data, buf3_f32, ggml_nbytes(t3));
struct ggml_tensor * m3 = ggml_map_custom3(ctx, t1, t2, t3, custom3, 1, g_userdata);
struct ggml_cgraph graph = ggml_build_forward(m3);
ggml_graph_compute_with_ctx(ctx, &graph, 4);
const float * output = ggml_get_data_f32(m3);
for (int i = 0; i < ggml_nelements(m3); ++i) {
assert(output[i] == buf1_f32[i] + buf2_f32[i] + buf3_f32[i]);
}
assert(g_custom3_count == 1);
ggml_free(ctx);
}
return 0;
}
|