Skip to content

Commit d0b4569

Browse files
committed
fix conflict
1 parent 47e1de7 commit d0b4569

5 files changed

Lines changed: 150 additions & 4 deletions

File tree

docs/ops.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ Legend:
7373
| MUL_MAT_ID || 🟡 ||| 🟡 | 🟡 ||| 🟡 | 🟡 ||
7474
| NEG |||| 🟡 ||||||||
7575
| NORM |||||||| 🟡 ||||
76-
| OPT_STEP_ADAMW ||||||| |||||
77-
| OPT_STEP_SGD ||||||| |||||
76+
| OPT_STEP_ADAMW ||||||| |||||
77+
| OPT_STEP_SGD ||||||| |||||
7878
| OUT_PROD | 🟡 | 🟡 | 🟡 | 🟡 ||| 🟡 |||| 🟡 |
7979
| PAD || 🟡 || 🟡 | 🟡 | 🟡 | 🟡 |||||
8080
| PAD_REFLECT_1D ||||||||||||

docs/ops/SYCL.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16752,8 +16752,8 @@ zjy 2
1675216752
"SYCL0","CROSS_ENTROPY_LOSS","type=f32,ne=[30000,1,1,1]","support","1","yes","SYCL"
1675316753
"SYCL0","CROSS_ENTROPY_LOSS_BACK","type=f32,ne=[10,5,4,3]","support","1","yes","SYCL"
1675416754
"SYCL0","CROSS_ENTROPY_LOSS_BACK","type=f32,ne=[30000,1,1,1]","support","1","yes","SYCL"
16755-
"SYCL0","OPT_STEP_ADAMW","type=f32,ne=[10,5,4,3]","support","0","no","SYCL"
16756-
"SYCL0","OPT_STEP_SGD","type=f32,ne=[10,5,4,3]","support","0","no","SYCL"
16755+
"SYCL0","OPT_STEP_ADAMW","type=f32,ne=[10,5,4,3]","support","1","yes","SYCL"
16756+
"SYCL0","OPT_STEP_SGD","type=f32,ne=[10,5,4,3]","support","1","yes","SYCL"
1675716757
"SYCL0","GATED_DELTA_NET","type=f32,head_count=32,head_size=128,n_seq_tokens=1,n_seqs=1,v_repeat=1,permuted=0,kda=0,K=1","support","1","yes","SYCL"
1675816758
"SYCL0","GATED_DELTA_NET","type=f32,head_count=32,head_size=16,n_seq_tokens=1,n_seqs=1,v_repeat=1,permuted=0,kda=0,K=1","support","1","yes","SYCL"
1675916759
"SYCL0","GATED_DELTA_NET","type=f32,head_count=32,head_size=16,n_seq_tokens=1,n_seqs=1,v_repeat=1,permuted=1,kda=1,K=1","support","1","yes","SYCL"

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include "ggml-sycl/fill.hpp"
7272
#include "ggml-sycl/cumsum.hpp"
7373
#include "ggml-sycl/diag.hpp"
74+
#include "ggml-sycl/opt-step.hpp"
7475
#include "ggml-sycl/solve_tri.hpp"
7576
#include "ggml-sycl/gated_delta_net.hpp"
7677
#include "ggml-sycl/pool.hpp"
@@ -5141,6 +5142,12 @@ static bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct gg
51415142
case GGML_OP_GATED_DELTA_NET:
51425143
ggml_sycl_gated_delta_net(ctx, dst);
51435144
break;
5145+
case GGML_OP_OPT_STEP_ADAMW:
5146+
ggml_sycl_opt_step_adamw(ctx, dst);
5147+
break;
5148+
case GGML_OP_OPT_STEP_SGD:
5149+
ggml_sycl_opt_step_sgd(ctx, dst);
5150+
break;
51445151
case GGML_OP_SSM_CONV:
51455152
ggml_sycl_ssm_conv(ctx, dst);
51465153
break;
@@ -5893,6 +5900,8 @@ static bool do_ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, cons
58935900
case GGML_OP_RWKV_WKV7:
58945901
case GGML_OP_GATED_LINEAR_ATTN:
58955902
case GGML_OP_GATED_DELTA_NET:
5903+
case GGML_OP_OPT_STEP_ADAMW:
5904+
case GGML_OP_OPT_STEP_SGD:
58965905
return true;
58975906
case GGML_OP_SSM_CONV:
58985907
return op->type == GGML_TYPE_F32 &&

ggml/src/ggml-sycl/opt-step.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "opt-step.hpp"
2+
3+
#define SYCL_OPT_STEP_BLOCK_SIZE 256
4+
5+
template <typename T>
6+
static void opt_step_adamw_f32_kernel(
7+
T * __restrict__ x,
8+
const T * __restrict__ g,
9+
T * __restrict__ g_m,
10+
T * __restrict__ g_v,
11+
const T * __restrict__ pars,
12+
const int64_t k,
13+
const sycl::nd_item<1> & item) {
14+
15+
const int64_t i = (int64_t) item.get_global_id(0);
16+
if (i >= k) {
17+
return;
18+
}
19+
20+
const float alpha = pars[0];
21+
const float beta1 = pars[1];
22+
const float beta2 = pars[2];
23+
const float eps = pars[3];
24+
const float wd = pars[4];
25+
const float beta1h = pars[5];
26+
const float beta2h = pars[6];
27+
28+
const float gi = g[i];
29+
const float gmi = g_m[i] * beta1 + gi * (1.0f - beta1);
30+
const float gvi = g_v[i] * beta2 + gi * gi * (1.0f - beta2);
31+
32+
g_m[i] = gmi;
33+
g_v[i] = gvi;
34+
35+
const float mh = gmi * beta1h;
36+
const float vh = sycl::sqrt(gvi * beta2h) + eps;
37+
38+
x[i] = x[i] * (1.0f - alpha * wd) - alpha * mh / vh;
39+
}
40+
41+
template <typename T>
42+
static void opt_step_sgd_f32_kernel(
43+
T * __restrict__ x,
44+
const T * __restrict__ g,
45+
const T * __restrict__ pars,
46+
const int64_t k,
47+
const sycl::nd_item<1> & item) {
48+
49+
const int64_t i = (int64_t) item.get_global_id(0);
50+
if (i >= k) {
51+
return;
52+
}
53+
54+
x[i] = x[i] * (1.0f - pars[0] * pars[1]) - pars[0] * g[i];
55+
}
56+
57+
void ggml_sycl_opt_step_adamw(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
58+
scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/5);
59+
60+
const ggml_tensor * src0 = dst->src[0];
61+
const ggml_tensor * src0_grad = dst->src[1];
62+
const ggml_tensor * src0_grad_m = dst->src[2];
63+
const ggml_tensor * src0_grad_v = dst->src[3];
64+
const ggml_tensor * adamw_params = dst->src[4];
65+
66+
GGML_ASSERT(src0->type == GGML_TYPE_F32);
67+
GGML_ASSERT(src0_grad->type == GGML_TYPE_F32);
68+
GGML_ASSERT(src0_grad_m->type == GGML_TYPE_F32);
69+
GGML_ASSERT(src0_grad_v->type == GGML_TYPE_F32);
70+
GGML_ASSERT(adamw_params->type == GGML_TYPE_F32);
71+
GGML_ASSERT(ggml_is_contiguous(src0));
72+
GGML_ASSERT(ggml_is_contiguous(src0_grad));
73+
GGML_ASSERT(ggml_is_contiguous(src0_grad_m));
74+
GGML_ASSERT(ggml_is_contiguous(src0_grad_v));
75+
GGML_ASSERT(ggml_is_contiguous(adamw_params));
76+
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
77+
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m));
78+
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v));
79+
GGML_ASSERT(ggml_nelements(adamw_params) == 7);
80+
81+
dpct::queue_ptr stream = ctx.stream();
82+
SYCL_CHECK(ggml_sycl_set_device(ctx.device));
83+
84+
float * src0_d = (float *) src0->data;
85+
const float * src0_grad_d = (const float *) src0_grad->data;
86+
float * src0_grad_m_d = (float *) src0_grad_m->data;
87+
float * src0_grad_v_d = (float *) src0_grad_v->data;
88+
const float * adamw_params_d = (const float *) adamw_params->data;
89+
90+
const int64_t ne = ggml_nelements(src0);
91+
const int64_t num_blocks = (ne + SYCL_OPT_STEP_BLOCK_SIZE - 1) / SYCL_OPT_STEP_BLOCK_SIZE;
92+
93+
stream->parallel_for(
94+
sycl::nd_range<1>(num_blocks * SYCL_OPT_STEP_BLOCK_SIZE, SYCL_OPT_STEP_BLOCK_SIZE),
95+
[=](sycl::nd_item<1> item) {
96+
opt_step_adamw_f32_kernel(src0_d, src0_grad_d, src0_grad_m_d, src0_grad_v_d, adamw_params_d, ne, item);
97+
});
98+
}
99+
100+
void ggml_sycl_opt_step_sgd(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
101+
scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/3);
102+
103+
const ggml_tensor * src0 = dst->src[0];
104+
const ggml_tensor * src0_grad = dst->src[1];
105+
const ggml_tensor * sgd_params = dst->src[2];
106+
107+
GGML_ASSERT(src0->type == GGML_TYPE_F32);
108+
GGML_ASSERT(src0_grad->type == GGML_TYPE_F32);
109+
GGML_ASSERT(sgd_params->type == GGML_TYPE_F32);
110+
GGML_ASSERT(ggml_is_contiguous(src0));
111+
GGML_ASSERT(ggml_is_contiguous(src0_grad));
112+
GGML_ASSERT(ggml_is_contiguous(sgd_params));
113+
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
114+
GGML_ASSERT(ggml_nelements(sgd_params) == 2);
115+
116+
dpct::queue_ptr stream = ctx.stream();
117+
SYCL_CHECK(ggml_sycl_set_device(ctx.device));
118+
119+
float * src0_d = (float *) src0->data;
120+
const float * src0_grad_d = (const float *) src0_grad->data;
121+
const float * sgd_params_d = (const float *) sgd_params->data;
122+
123+
const int64_t ne = ggml_nelements(src0);
124+
const int64_t num_blocks = (ne + SYCL_OPT_STEP_BLOCK_SIZE - 1) / SYCL_OPT_STEP_BLOCK_SIZE;
125+
126+
stream->parallel_for(
127+
sycl::nd_range<1>(num_blocks * SYCL_OPT_STEP_BLOCK_SIZE, SYCL_OPT_STEP_BLOCK_SIZE),
128+
[=](sycl::nd_item<1> item) {
129+
opt_step_sgd_f32_kernel(src0_d, src0_grad_d, sgd_params_d, ne, item);
130+
});
131+
}

ggml/src/ggml-sycl/opt-step.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include "common.hpp"
4+
5+
void ggml_sycl_opt_step_adamw(ggml_backend_sycl_context & ctx, ggml_tensor * dst);
6+
void ggml_sycl_opt_step_sgd(ggml_backend_sycl_context & ctx, ggml_tensor * dst);

0 commit comments

Comments
 (0)