zhentaoyu commited on
Commit
52eea23
·
1 Parent(s): 6989631

Add `TIMESTEP_EMBEDDING` OP (llama/8707)

Browse files

Signed-off-by: zhentaoyu <[email protected]>

ggml/src/ggml-sycl.cpp CHANGED
@@ -4108,6 +4108,9 @@ bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tens
4108
  case GGML_OP_ARGSORT:
4109
  func = ggml_sycl_argsort;
4110
  break;
 
 
 
4111
  default:
4112
  return false;
4113
  }
@@ -5225,6 +5228,7 @@ GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, cons
5225
  case GGML_OP_UPSCALE:
5226
  case GGML_OP_PAD:
5227
  case GGML_OP_LEAKY_RELU:
 
5228
  return true;
5229
  default:
5230
  return false;
 
4108
  case GGML_OP_ARGSORT:
4109
  func = ggml_sycl_argsort;
4110
  break;
4111
+ case GGML_OP_TIMESTEP_EMBEDDING:
4112
+ func = ggml_sycl_op_timestep_embedding;
4113
+ break;
4114
  default:
4115
  return false;
4116
  }
 
5228
  case GGML_OP_UPSCALE:
5229
  case GGML_OP_PAD:
5230
  case GGML_OP_LEAKY_RELU:
5231
+ case GGML_OP_TIMESTEP_EMBEDDING:
5232
  return true;
5233
  default:
5234
  return false;
ggml/src/ggml-sycl/backend.hpp CHANGED
@@ -24,5 +24,6 @@
24
  #include "rope.hpp"
25
  #include "norm.hpp"
26
  #include "softmax.hpp"
 
27
 
28
  #endif // GGML_SYCL_BACKEND_HPP
 
24
  #include "rope.hpp"
25
  #include "norm.hpp"
26
  #include "softmax.hpp"
27
+ #include "tsembd.hpp"
28
 
29
  #endif // GGML_SYCL_BACKEND_HPP
ggml/src/ggml-sycl/presets.hpp CHANGED
@@ -42,6 +42,7 @@
42
  #define SYCL_IM2COL_BLOCK_SIZE 256
43
  #define SYCL_POOL2D_BLOCK_SIZE 256
44
  #define SYCL_CONV_TRANPOSE_1D_BLOCK_SIZE 256
 
45
 
46
  // dmmv = dequantize_mul_mat_vec
47
  #ifndef GGML_SYCL_DMMV_X
 
42
  #define SYCL_IM2COL_BLOCK_SIZE 256
43
  #define SYCL_POOL2D_BLOCK_SIZE 256
44
  #define SYCL_CONV_TRANPOSE_1D_BLOCK_SIZE 256
45
+ #define SYCL_TIMESTEP_EMBEDDING_BLOCK_SIZE 256
46
 
47
  // dmmv = dequantize_mul_mat_vec
48
  #ifndef GGML_SYCL_DMMV_X
ggml/src/ggml-sycl/tsembd.cpp ADDED
@@ -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 ADDED
@@ -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