Skip to content

Commit 6de8ed7

Browse files
authored
sycl : add PAD_REFLECT_D1 operator support (ggml-org#16145)
* sycl: add PAD_REFLECT_D1 operator support * docs(ops): regenerate docs/ops.md * remove trailing whitespaces * style: fix editorconfig issues — trim trailing spaces and normalize EOLs * fix: move PAD_REFLECT_1D case outside of fall-through block
1 parent 84bf3c6 commit 6de8ed7

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

docs/ops.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Legend:
7272
| OPT_STEP_SGD ||||||||||
7373
| OUT_PROD | 🟡 || 🟡 | 🟡 ||| 🟡 |||
7474
| PAD ||||||| 🟡 |||
75-
| PAD_REFLECT_1D ||||||| |||
75+
| PAD_REFLECT_1D ||||||| |||
7676
| POOL_2D || 🟡 ||||||||
7777
| REGLU ||||| 🟡 ||| 🟡 ||
7878
| RELU |||| 🟡 | 🟡 | 🟡 | 🟡 | 🟡 ||

docs/ops/SYCL.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9379,8 +9379,8 @@
93799379
"SYCL0","ACC","type=f32,ne_a=[256,17,1,1],ne_b=[256,16,1,1]","support","1","yes","SYCL"
93809380
"SYCL0","PAD","type=f32,ne_a=[512,512,1,1],pad_0=1,pad_1=1","support","1","yes","SYCL"
93819381
"SYCL0","PAD","type=f32,ne_a=[512,512,3,1],lp0=1,rp0=1,lp1=1,rp1=1,lp2=1,rp2=1,lp3=1,rp3=1,v=0","support","1","yes","SYCL"
9382-
"SYCL0","PAD_REFLECT_1D","type=f32,ne_a=[512,34,2,1],pad_0=10,pad_1=9","support","0","no","SYCL"
9383-
"SYCL0","PAD_REFLECT_1D","type=f32,ne_a=[3000,384,4,1],pad_0=10,pad_1=9","support","0","no","SYCL"
9382+
"SYCL0","PAD_REFLECT_1D","type=f32,ne_a=[3000,384,4,1],pad_0=10,pad_1=9","support","0","yes","SYCL"
9383+
"SYCL0","PAD_REFLECT_1D","type=f32,ne_a=[512,34,2,1],pad_0=10,pad_1=9","support","0","yes","SYCL"
93849384
"SYCL0","ROLL","shift0=3,shift1=-2,shift3=1,shift4=-1","support","0","no","SYCL"
93859385
"SYCL0","ARANGE","type=f32,start=0.000000,stop=10.000000,step=1.000000","support","0","no","SYCL"
93869386
"SYCL0","TIMESTEP_EMBEDDING","type=f32,ne_a=[2,1,1,1],dim=320,max_period=10000","support","1","yes","SYCL"

ggml/src/ggml-sycl/backend.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@
3737
#include "softmax.hpp"
3838
#include "tsembd.hpp"
3939
#include "wkv.hpp"
40+
#include "pad_reflect_1d.hpp"
41+
4042

4143
#endif // GGML_SYCL_BACKEND_HPP

ggml/src/ggml-sycl/ggml-sycl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,9 @@ static bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct gg
37443744
case GGML_OP_CONCAT:
37453745
ggml_sycl_op_concat(ctx, dst);
37463746
break;
3747+
case GGML_OP_PAD_REFLECT_1D:
3748+
ggml_sycl_op_pad_reflect_1d(ctx,dst);
3749+
break;
37473750
case GGML_OP_UPSCALE:
37483751
ggml_sycl_upscale(ctx, dst);
37493752
break;
@@ -4455,6 +4458,8 @@ static bool ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, const g
44554458
case GGML_OP_DIV:
44564459
case GGML_OP_REPEAT:
44574460
return true;
4461+
case GGML_OP_PAD_REFLECT_1D:
4462+
return ggml_is_contiguous(op->src[0]) && op-> type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
44584463
case GGML_OP_SQR:
44594464
case GGML_OP_SQRT:
44604465
case GGML_OP_SIN:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "pad_reflect_1d.hpp"
2+
3+
void pad_reflect_1d_f32(const float* src,float* dst,
4+
const int64_t ne0, const int64_t ne02, const int p0, const int p1,
5+
const int64_t nb0, const int64_t nb1, const int64_t nb2, const int64_t nb3,
6+
const int64_t nb00, const int64_t nb01, const int64_t nb02, const int64_t nb03,
7+
const sycl::nd_item<3> &item_ct1){
8+
9+
const int i0 = item_ct1.get_group(0) * SYCL_CONCAT_BLOCK_SIZE + item_ct1.get_local_id(0);
10+
const int i1 = item_ct1.get_group(1);
11+
const int g2 = item_ct1.get_group(2);
12+
const int i2 = g2 % ne02;
13+
const int i3 = g2 / ne02;
14+
15+
if (i0 >= p0 + ne0 + p1) return;
16+
17+
int t = i0 - p0;
18+
int period = 2 * ne0 -2;
19+
int m = t % period;
20+
m += (m < 0) * period;
21+
int center = ne0 -1;
22+
int srci0 = center - abs(center - m);
23+
24+
int offest_src = i3*nb3 + i2*nb2 + i1*nb1 + srci0*nb0;
25+
int offest_dst = i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00;
26+
dst[offest_dst] = src[offest_src];
27+
28+
}
29+
30+
void ggml_sycl_op_pad_reflect_1d(ggml_backend_sycl_context& ctx, ggml_tensor* dst){
31+
32+
const ggml_tensor * src0 = dst->src[0];
33+
queue_ptr stream = ctx.stream();
34+
35+
GGML_ASSERT(src0->type == GGML_TYPE_F32);
36+
GGML_ASSERT( dst->type == GGML_TYPE_F32);
37+
38+
const int32_t * opts = (const int32_t *) dst->op_params;
39+
const int p0 = opts[0];
40+
const int p1 = opts[1];
41+
42+
const int64_t ne0 = src0->ne[0];
43+
44+
const int64_t ne00 = dst->ne[0];
45+
const int64_t ne01 = dst->ne[1];
46+
const int64_t ne02 = dst->ne[2];
47+
const int64_t ne03 = dst->ne[3];
48+
49+
const int64_t nb00 = dst->nb[0];
50+
const int64_t nb01 = dst->nb[1];
51+
const int64_t nb02 = dst->nb[2];
52+
const int64_t nb03 = dst->nb[3];
53+
const int64_t nb0 = src0->nb[0];
54+
const int64_t nb1 = src0->nb[1];
55+
const int64_t nb2 = src0->nb[2];
56+
const int64_t nb3 = src0->nb[3];
57+
58+
int num_blocks = (ne00 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
59+
sycl::range<3> global(num_blocks * SYCL_CONCAT_BLOCK_SIZE, ne01, ne02*ne03);
60+
sycl::range<3> local(SYCL_CONCAT_BLOCK_SIZE, 1, 1);
61+
62+
stream->parallel_for(
63+
sycl::nd_range<3>(global,
64+
local),
65+
[=](sycl::nd_item<3> item_ct1) { pad_reflect_1d_f32(
66+
(const float *) src0->data, (float *) dst->data,
67+
ne0, ne02, p0, p1,
68+
nb0, nb1, nb2, nb3,
69+
nb00, nb01, nb02, nb03
70+
, item_ct1);
71+
});
72+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef GGML_SYCL_PAD_REFLECT_1D_HPP
2+
#define GGML_SYCL_PAD_REFLECT_1D_HPP
3+
4+
#include "common.hpp"
5+
6+
void ggml_sycl_op_pad_reflect_1d(ggml_backend_sycl_context& ctx, ggml_tensor* dst);
7+
8+
#endif // GGML_SYCL_PAD_REFLECT_1D_HPP

0 commit comments

Comments
 (0)