Skip to content

Commit 9039d40

Browse files
committed
Merge branch 'eso_b6110' into crokeso
2 parents 2034127 + 6b2e118 commit 9039d40

File tree

20 files changed

+594
-163
lines changed

20 files changed

+594
-163
lines changed

ggml/src/ggml-backend.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,11 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
10771077
}
10781078
}
10791079
}
1080+
// if the node is still unassigned, assign it to the first backend that supports it
1081+
for (int b = 0; b < sched->n_backends && *cur_backend_id == -1; b++) {
1082+
ggml_backend_sched_set_if_supported(sched, node, b, cur_backend_id);
1083+
}
1084+
GGML_ASSERT(*cur_backend_id != -1);
10801085
}
10811086

10821087
// pass 5: split graph, find tensors that need to be copied
@@ -1104,7 +1109,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
11041109

11051110
const int node_backend_id = tensor_backend_id(node);
11061111

1107-
assert(node_backend_id != -1); // all nodes should be assigned by now, this can happen if there is no CPU fallback
1112+
GGML_ASSERT(node_backend_id != -1); // all nodes should be assigned by now, this can happen if there is no CPU fallback
11081113

11091114
// check if we should start a new split based on the sources of the current node
11101115
bool need_new_split = false;
@@ -1162,7 +1167,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
11621167

11631168
size_t src_id = hash_id(src);
11641169
const int src_backend_id = sched->hv_tensor_backend_ids[src_id];
1165-
assert(src_backend_id != -1); // all inputs should be assigned by now
1170+
GGML_ASSERT(src_backend_id != -1); // all inputs should be assigned by now
11661171

11671172
if (src->flags & GGML_TENSOR_FLAG_INPUT && sched->n_copies > 1) {
11681173
if (tensor_id_copy(src_id, src_backend_id, 0) == NULL) {

ggml/src/ggml-cpu/ggml-cpu-traits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extra_buffer_type::~extra_buffer_type() {}
1010
} // namespace ggml::cpu
1111

1212
bool ggml_cpu_extra_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * op) {
13-
for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
13+
for (auto extra : ggml_backend_cpu_get_extra_buffer_types()) {
1414
if (extra && extra->context) {
1515
auto buf_extra = (ggml::cpu::extra_buffer_type *) extra->context;
1616
auto tensor_traits = buf_extra->get_tensor_traits(op);
@@ -23,7 +23,7 @@ bool ggml_cpu_extra_compute_forward(struct ggml_compute_params * params, struct
2323
}
2424

2525
bool ggml_cpu_extra_work_size(int n_threads, const struct ggml_tensor * op, size_t * size) {
26-
for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
26+
for (auto extra : ggml_backend_cpu_get_extra_buffer_types()) {
2727
if (extra && extra->context) {
2828
auto buf_extra = (ggml::cpu::extra_buffer_type *) extra->context;
2929
auto tensor_traits = buf_extra->get_tensor_traits(op);

ggml/src/ggml-cpu/ggml-cpu-traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ class extra_buffer_type {
3333
} // namespace ggml::cpu
3434

3535
// implemented in ggml-cpu.cpp.
36-
std::vector<ggml_backend_buffer_type_t> & ggml_backend_cpu_get_extra_buffers_type();
36+
std::vector<ggml_backend_buffer_type_t> & ggml_backend_cpu_get_extra_buffer_types();
3737

3838
#endif

ggml/src/ggml-cpu/ggml-cpu.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
// ggml-backend interface
4242

43-
std::vector<ggml_backend_buffer_type_t>& ggml_backend_cpu_get_extra_buffers_type() {
43+
std::vector<ggml_backend_buffer_type_t> & ggml_backend_cpu_get_extra_buffer_types() {
4444
static std::vector<ggml_backend_buffer_type_t> bufts = []() {
4545
std::vector<ggml_backend_buffer_type_t> bufts;
4646

@@ -62,23 +62,27 @@ std::vector<ggml_backend_buffer_type_t>& ggml_backend_cpu_get_extra_buffers_type
6262
}
6363
#endif
6464

65-
bufts.push_back(NULL);
66-
6765
return bufts;
6866
}();
6967

7068
return bufts;
7169
}
7270

7371
static ggml_backend_buffer_type_t * ggml_backend_cpu_device_get_extra_buffers_type(ggml_backend_dev_t device) {
74-
return ggml_backend_cpu_get_extra_buffers_type().data();
72+
static std::vector<ggml_backend_buffer_type_t> extra_bufts = [] {
73+
std::vector<ggml_backend_buffer_type_t> bufts = ggml_backend_cpu_get_extra_buffer_types();
74+
bufts.push_back(nullptr);
75+
return bufts;
76+
}();
77+
78+
return extra_bufts.data();
7579

7680
GGML_UNUSED(device);
7781
}
7882

7983
static bool ggml_backend_cpu_is_extra_buffer_type(ggml_backend_buffer_type_t buft) {
80-
for (auto * extra : ggml_backend_cpu_get_extra_buffers_type()) {
81-
if (extra && extra == buft) {
84+
for (auto * extra : ggml_backend_cpu_get_extra_buffer_types()) {
85+
if (extra == buft) {
8286
return true;
8387
}
8488
}
@@ -402,20 +406,13 @@ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const st
402406
return true;
403407
}
404408

405-
// extra_buffer_op?
406-
for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
407-
if (extra) {
408-
auto buf_extra = (ggml::cpu::extra_buffer_type*) extra->context;
409-
if (buf_extra && buf_extra->supports_op(dev, op)) {
410-
return true;
411-
}
412-
}
413-
}
414-
415-
// the other case need host buffer.
416-
for (int i = 0; i < GGML_MAX_SRC; i++) {
417-
if (op->src[i] && op->src[i]->buffer && !ggml_backend_buft_is_host(op->src[i]->buffer->buft)) {
418-
return false;
409+
// check extra buffer types
410+
// note: only the first sources are checked for extra buffer types to reduce overhead, increase if necessary
411+
for (int i = 0; i < 4; i++) {
412+
if (op->src[i] && op->src[i]->buffer &&
413+
ggml_backend_cpu_is_extra_buffer_type(op->src[i]->buffer->buft)) {
414+
auto * buf_extra = (ggml::cpu::extra_buffer_type *) op->src[i]->buffer->buft->context;
415+
return buf_extra->supports_op(dev, op);
419416
}
420417
}
421418

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2+
3+
//------------------------------------------------------------------------------
4+
// add_id
5+
//------------------------------------------------------------------------------
6+
kernel void kernel_add_id(
7+
global char * src0,
8+
ulong offset0,
9+
global char * src1,
10+
ulong offset1,
11+
global char * src2,
12+
ulong offset2,
13+
global char * dst,
14+
ulong offsetd,
15+
ulong nb01,
16+
ulong nb02,
17+
ulong nb11,
18+
ulong nb21,
19+
int ne0,
20+
int ne1
21+
) {
22+
src0 = (global char*)((global char*)src0 + offset0);
23+
src1 = (global char*)((global char*)src1 + offset1);
24+
src2 = (global char*)((global char*)src2 + offset2);
25+
dst = (global char*)((global char*)dst + offsetd);
26+
27+
int i1 = get_group_id(0);
28+
int i2 = get_group_id(1);
29+
30+
const int i11 = *((global const int *) (src2 + i1*sizeof(int) + i2*nb21));
31+
32+
const size_t nb1 = ne0 * sizeof(float);
33+
const size_t nb2 = ne1 * nb1;
34+
35+
global float * dst_row = (global float *)((global char *)dst + i1*nb1 + i2*nb2);
36+
global float * src0_row = (global float *)((global char *)src0 + i1*nb01 + i2*nb02);
37+
global float * src1_row = (global float *)((global char *)src1 + i11*nb11);
38+
39+
for (int i0 = get_local_id(0); i0 < ne0; i0 += get_local_size(0)) {
40+
dst_row[i0] = src0_row[i0] + src1_row[i0];
41+
}
42+
}

ggml/src/ggml-opencl/kernels/glu.cl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,47 @@ kernel void kernel_swiglu_f16(
202202
}
203203
}
204204

205+
//------------------------------------------------------------------------------
206+
// swiglu_oai
207+
//------------------------------------------------------------------------------
208+
kernel void kernel_swiglu_oai(
209+
global char * src0,
210+
ulong offset0,
211+
global char * src1,
212+
ulong offset1,
213+
global char * dst,
214+
ulong offsetd,
215+
ulong nb01,
216+
ulong nb11,
217+
int ne0,
218+
ulong nb1,
219+
int ne00_off,
220+
int ne10_off,
221+
float limit,
222+
float alpha
223+
) {
224+
src0 = (global char*)((global char*)src0 + offset0);
225+
src1 = (global char*)((global char*)src1 + offset1);
226+
dst = (global char*)((global char*)dst + offsetd);
227+
228+
global float * src0_row = (global float *) ((global char *) src0 + get_group_id(0)*nb01) + ne00_off;
229+
global float * src1_row = (global float *) ((global char *) src1 + get_group_id(0)*nb11) + ne10_off;
230+
global float * dst_row = (global float *) ((global char *) dst + get_group_id(0)*nb1);
231+
232+
for (int i0 = get_local_id(0); i0 < ne0; i0 += get_local_size(0)) {
233+
float x0 = src0_row[i0];
234+
float x1 = src1_row[i0];
235+
236+
x0 = min(x0, limit);
237+
x1 = max(min(x1, limit), -limit);
238+
239+
float out_glu = x0 / (1.0f + exp(-x0 * alpha));
240+
out_glu = out_glu * (1.0f + x1);
241+
242+
dst_row[i0] = out_glu;
243+
}
244+
}
245+
205246
//------------------------------------------------------------------------------
206247
// geglu_erf
207248
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)