Skip to content

Commit 2ef4126

Browse files
authored
Merge b3496
b3496
2 parents 46dc1fd + 398ede5 commit 2ef4126

File tree

14 files changed

+171
-27
lines changed

14 files changed

+171
-27
lines changed

.devops/nix/package.nix

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,9 @@ let
126126
++ optionals useMetalKit [ MetalKit ];
127127

128128
cudaBuildInputs = with cudaPackages; [
129-
cuda_cccl.dev # <nv/target>
130-
131-
# A temporary hack for reducing the closure size, remove once cudaPackages
132-
# have stopped using lndir: https://github.com/NixOS/nixpkgs/issues/271792
133-
cuda_cudart.dev
134-
cuda_cudart.lib
135-
cuda_cudart.static
136-
libcublas.dev
137-
libcublas.lib
138-
libcublas.static
129+
cuda_cudart
130+
cuda_cccl # <nv/target>
131+
libcublas
139132
];
140133

141134
rocmBuildInputs = with rocmPackages; [

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ set(LLAMA_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Location o
139139
# determining _precisely_ which defines are necessary for the llama-config
140140
# package.
141141
#
142-
get_directory_property(GGML_DIR_DEFINES DIRECTORY ggml/src COMPILE_DEFINITIONS)
142+
get_target_property(GGML_DIRECTORY ggml SOURCE_DIR)
143+
get_directory_property(GGML_DIR_DEFINES DIRECTORY ${GGML_DIRECTORY} COMPILE_DEFINITIONS)
143144
get_target_property(GGML_TARGET_DEFINES ggml COMPILE_DEFINITIONS)
144145
set(GGML_TRANSIENT_DEFINES ${GGML_TARGET_DEFINES} ${GGML_DIR_DEFINES})
145146
get_target_property(GGML_LINK_LIBRARIES ggml LINK_LIBRARIES)

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ggml/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ set(GGML_PUBLIC_HEADERS
207207
include/ggml-alloc.h
208208
include/ggml-backend.h
209209
include/ggml-blas.h
210+
include/ggml-cann.h
210211
include/ggml-cuda.h
211212
include/ggml.h
212213
include/ggml-kompute.h

ggml/src/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -849,11 +849,6 @@ if (GGML_CANN)
849849
${CANN_INSTALL_DIR}/acllib/include
850850
)
851851

852-
# TODO: find libs
853-
link_directories(
854-
${CANN_INSTALL_DIR}/lib64
855-
)
856-
857852
add_subdirectory(ggml-cann/kernels)
858853
list(APPEND CANN_LIBRARIES
859854
ascendcl
@@ -872,6 +867,7 @@ if (GGML_CANN)
872867

873868
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} ${CANN_LIBRARIES} )
874869
set(GGML_EXTRA_INCLUDES ${GGML_EXTRA_INCLUDES} ${CANN_INCLUDE_DIRS})
870+
set(GGML_EXTRA_LIBDIRS ${GGML_EXTRA_LIBDIRS} ${CANN_INSTALL_DIR}/lib64)
875871
list(APPEND GGML_CDEF_PUBLIC GGML_USE_CANN)
876872
endif()
877873
else()

ggml/src/ggml-quants.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6449,22 +6449,22 @@ void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * r
64496449
// compute mask for subtraction
64506450
vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
64516451
vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
6452-
vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
6452+
vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_mu(vmask_0, q3_0, q3_0, 0x4, vl);
64536453
m <<= 1;
64546454

64556455
vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
64566456
vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
6457-
vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
6457+
vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_mu(vmask_1, q3_1, q3_1, 0x4, vl);
64586458
m <<= 1;
64596459

64606460
vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
64616461
vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
6462-
vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
6462+
vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_mu(vmask_2, q3_2, q3_2, 0x4, vl);
64636463
m <<= 1;
64646464

64656465
vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
64666466
vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
6467-
vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
6467+
vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_mu(vmask_3, q3_3, q3_3, 0x4, vl);
64686468
m <<= 1;
64696469

64706470
// load Q8 and take product with Q3
@@ -7720,13 +7720,13 @@ void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * r
77207720
vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
77217721
vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
77227722
vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
7723-
vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
7723+
vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_mu(vmask_1, q5_a, q5_a, 16, vl);
77247724
m <<= 1;
77257725

77267726
vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
77277727
vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
77287728
vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
7729-
vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
7729+
vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_mu(vmask_2, q5_l, q5_l, 16, vl);
77307730
m <<= 1;
77317731

77327732
vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);

ggml/src/ggml-sycl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,6 +4108,9 @@ bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tens
41084108
case GGML_OP_ARGSORT:
41094109
func = ggml_sycl_argsort;
41104110
break;
4111+
case GGML_OP_TIMESTEP_EMBEDDING:
4112+
func = ggml_sycl_op_timestep_embedding;
4113+
break;
41114114
default:
41124115
return false;
41134116
}
@@ -5225,6 +5228,7 @@ GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, cons
52255228
case GGML_OP_UPSCALE:
52265229
case GGML_OP_PAD:
52275230
case GGML_OP_LEAKY_RELU:
5231+
case GGML_OP_TIMESTEP_EMBEDDING:
52285232
return true;
52295233
default:
52305234
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
@@ -42,6 +42,7 @@
4242
#define SYCL_IM2COL_BLOCK_SIZE 256
4343
#define SYCL_POOL2D_BLOCK_SIZE 256
4444
#define SYCL_CONV_TRANPOSE_1D_BLOCK_SIZE 256
45+
#define SYCL_TIMESTEP_EMBEDDING_BLOCK_SIZE 256
4546

4647
// dmmv = dequantize_mul_mat_vec
4748
#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+
}

0 commit comments

Comments
 (0)