Skip to content

Commit e1256c6

Browse files
zhentaoyuNeo Zhang
authored and
Neo Zhang
committed
[SYCL] Add TIMESTEP_EMBEDDING OP (ggml-org#8707)
Signed-off-by: zhentaoyu <zhentao.yu@intel.com>
1 parent e896300 commit e1256c6

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

ggml/src/ggml-sycl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3953,6 +3953,9 @@ bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tens
39533953
case GGML_OP_ARGSORT:
39543954
func = ggml_sycl_argsort;
39553955
break;
3956+
case GGML_OP_TIMESTEP_EMBEDDING:
3957+
func = ggml_sycl_op_timestep_embedding;
3958+
break;
39563959
default:
39573960
return false;
39583961
}
@@ -5077,6 +5080,7 @@ GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, cons
50775080
case GGML_OP_UPSCALE:
50785081
case GGML_OP_PAD:
50795082
case GGML_OP_LEAKY_RELU:
5083+
case GGML_OP_TIMESTEP_EMBEDDING:
50805084
return true;
50815085
default:
50825086
return false;

ggml/src/ggml-sycl/backend.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
#include "rope.hpp"
2525
#include "norm.hpp"
2626
#include "softmax.hpp"
27+
#include "tsembd.hpp"
2728

2829
#endif // GGML_SYCL_BACKEND_HPP

ggml/src/ggml-sycl/presets.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#define SYCL_IM2COL_BLOCK_SIZE 256
4545
#define SYCL_POOL2D_BLOCK_SIZE 256
4646
#define SYCL_CONV_TRANPOSE_1D_BLOCK_SIZE 256
47+
#define SYCL_TIMESTEP_EMBEDDING_BLOCK_SIZE 256
4748

4849
// dmmv = dequantize_mul_mat_vec
4950
#ifndef GGML_SYCL_DMMV_X

ggml/src/ggml-sycl/tsembd.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// MIT license
3+
// Copyright (C) 2024 Intel Corporation
4+
// SPDX-License-Identifier: MIT
5+
//
6+
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
13+
#include "tsembd.hpp"
14+
15+
static void timestep_embedding_f32(
16+
const float * timesteps, float * dst, const int nb1,
17+
const int dim, const int max_period, const sycl::nd_item<3> &item_ct1) {
18+
// item_ct1.get_group(1)(blockIDx.y): idx of timesteps->ne[0]
19+
// item_ct1.get_group(2) (blockIDx.x): idx of ((dim + 1) / 2) / BLOCK_SIZE
20+
int i = item_ct1.get_group(1);
21+
int j = item_ct1.get_local_id(2) + item_ct1.get_group(2) * item_ct1.get_local_range(2);
22+
float * embed_data = (float *)((char *)dst + i*nb1);
23+
24+
if (dim % 2 != 0 && j == ((dim + 1) / 2)) {
25+
embed_data[dim] = 0.f;
26+
}
27+
28+
int half = dim / 2;
29+
if (j >= half) {
30+
return;
31+
}
32+
33+
float timestep = timesteps[i];
34+
float freq = (float)sycl::native::exp(-(sycl::log((float)max_period)) * j / half);
35+
float arg = timestep * freq;
36+
embed_data[j] = sycl::cos(arg);
37+
embed_data[j + half] = sycl::sin(arg);
38+
}
39+
40+
static void timestep_embedding_f32_sycl(
41+
const float * x, float * dst, const int ne00, const int nb1,
42+
const int dim, const int max_period, const queue_ptr& stream) {
43+
// As the kernel returns when thread.idx is larger than dim/2, the half_ceil does not need to pad
44+
int half_ceil = dim / 2;
45+
int num_blocks = (half_ceil + SYCL_TIMESTEP_EMBEDDING_BLOCK_SIZE - 1) / SYCL_TIMESTEP_EMBEDDING_BLOCK_SIZE;
46+
sycl::range<3> block_dims(1, 1, SYCL_TIMESTEP_EMBEDDING_BLOCK_SIZE);
47+
sycl::range<3> gridDim(1, ne00, num_blocks);
48+
stream->parallel_for(
49+
sycl::nd_range<3>(
50+
gridDim * block_dims, block_dims),
51+
[=](sycl::nd_item<3> item_ct1) {
52+
timestep_embedding_f32(
53+
x, dst, nb1, dim, max_period, item_ct1
54+
);
55+
});
56+
}
57+
58+
void ggml_sycl_op_timestep_embedding(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
59+
const ggml_tensor *src1, ggml_tensor * dst) {
60+
const float * src0_d = (const float *)src0->data;
61+
float * dst_d = (float *)dst->data;
62+
dpct::queue_ptr stream = ctx.stream();
63+
64+
GGML_ASSERT(src0->type == GGML_TYPE_F32);
65+
GGML_ASSERT(dst->type == GGML_TYPE_F32);
66+
67+
const int dim = dst->op_params[0];
68+
const int max_period = dst->op_params[1];
69+
70+
timestep_embedding_f32_sycl(src0_d, dst_d, src0->ne[0], dst->nb[1], dim, max_period, stream);
71+
}

ggml/src/ggml-sycl/tsembd.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// MIT license
3+
// Copyright (C) 2024 Intel Corporation
4+
// SPDX-License-Identifier: MIT
5+
//
6+
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
13+
#ifndef GGML_SYCL_TSEMBD_HPP
14+
#define GGML_SYCL_TSEMBD_HPP
15+
16+
#include "common.hpp"
17+
18+
void ggml_sycl_op_timestep_embedding(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
19+
const ggml_tensor *src1, ggml_tensor * dst);
20+
21+
#endif // GGML_SYCL_TSEMBD_HPP

0 commit comments

Comments
 (0)