Skip to content

Commit 8ba2463

Browse files
opencl: integrate backend dyn.load interface and fix compiler and format warnings
1 parent 5f2dd48 commit 8ba2463

10 files changed

+41
-43
lines changed

ggml/include/ggml-opencl2.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ GGML_BACKEND_API bool ggml_backend_is_opencl2(ggml_backend_t backend);
3030
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl2_buffer_type(void);
3131
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl2_host_buffer_type(void);
3232

33-
GGML_BACKEND_API ggml_backend_t ggml_backend_reg_opencl2_init(const char * params, void * user_data);
34-
3533
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_opencl2_reg(void);
3634

3735
#ifdef __cplusplus

ggml/src/ggml-opencl2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (OpenCL_FOUND)
66

77
set(TARGET_NAME ggml-opencl2)
88

9-
add_library(${TARGET_NAME}
9+
ggml_add_backend_library(${TARGET_NAME}
1010
ggml-opencl2.cpp
1111
../../include/ggml-opencl2.h)
1212
target_link_libraries(${TARGET_NAME} PRIVATE ggml-base ${OpenCL_LIBRARIES})

ggml/src/ggml-opencl2/ggml-opencl2.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// SPDX-FileCopyrightText: Copyright (c) Qualcomm Innovation Center, Inc. All rights reserved
22
// SPDX-License-Identifier: MIT
33

4+
#define CL_TARGET_OPENCL_VERSION 220
5+
6+
// suppress warnings in CL headers for GCC and Clang
7+
#pragma GCC diagnostic ignored "-Wgnu-anonymous-struct"
8+
#pragma GCC diagnostic ignored "-Woverlength-strings"
9+
410
#include "ggml-opencl2.h"
511
#include "ggml-backend.h"
612
#include "ggml-impl.h"
@@ -1237,10 +1243,6 @@ static void ggml_backend_opencl2_buffer_init_tensor(ggml_backend_buffer_t buffer
12371243
tensor->extra = extra;
12381244
}
12391245
}
1240-
1241-
// This should be removed. Keep it to make it easier to identify the backend
1242-
// when debugging until backend is removed from tensor.
1243-
tensor->backend = GGML_BACKEND_TYPE_GPU;
12441246
}
12451247

12461248
// The optimized gemm and gemv kernels are used for large matrices without batch.
@@ -1938,18 +1940,7 @@ static struct ggml_backend_device_i ggml_backend_opencl2_device_i = {
19381940
/* .event_synchronize = */ NULL,
19391941
};
19401942

1941-
//
1942-
// Backend registration
1943-
//
1944-
1945-
GGML_API ggml_backend_t ggml_backend_reg_opencl2_init(const char * params, void * user_data) {
1946-
return ggml_backend_opencl2_init();
1947-
1948-
GGML_UNUSED(params);
1949-
GGML_UNUSED(user_data);
1950-
}
1951-
1952-
// new API
1943+
// Backend registry
19531944

19541945
static const char * ggml_backend_opencl2_reg_get_name(ggml_backend_reg_t reg) {
19551946
return "OpenCL2";
@@ -1986,6 +1977,7 @@ ggml_backend_reg_t ggml_backend_opencl2_reg(void) {
19861977

19871978
if (!initialized) {
19881979
reg = ggml_backend_reg {
1980+
/* .api_version = */ GGML_BACKEND_API_VERSION,
19891981
/* .iface = */ ggml_backend_opencl2_reg_i,
19901982
/* .context = */ NULL,
19911983
};
@@ -2004,6 +1996,8 @@ ggml_backend_reg_t ggml_backend_opencl2_reg(void) {
20041996
return ®
20051997
}
20061998

1999+
GGML_BACKEND_DL_IMPL(ggml_backend_opencl2_reg)
2000+
20072001
//------------------------------------------------------------------------------
20082002
// Debugging utils
20092003
//------------------------------------------------------------------------------
@@ -2921,13 +2915,11 @@ static void ggml_cl_mul_mat(ggml_backend_t backend, const ggml_tensor * src0, co
29212915
// init CL objects
29222916
// <--------------------------------------------> //
29232917
cl_int status;
2924-
cl_event evt;
29252918
cl_image_format img_fmt_1d;
29262919
cl_image_desc img_desc_1d;
29272920
cl_buffer_region region;
29282921
cl_mem A_image1d;
29292922
cl_mem B_image1d;
2930-
cl_mem A_sub_buffer;
29312923
cl_mem B_sub_buffer;
29322924
cl_mem C_d;
29332925
// for B transpose
@@ -3623,7 +3615,7 @@ static void ggml_cl_cpy(ggml_backend_t backend, const ggml_tensor * src0, const
36233615
GGML_ASSERT(src1->extra);
36243616

36253617
// GGML_OP_CPY happens between src0 and src1.
3626-
// GGML_OP_DUP and GGML_OP_CONT happen between src0 and dst.
3618+
// GGML_OP_DUP and GGML_OP_CONT happen between src0 and dst.
36273619
UNUSED(dst);
36283620

36293621
const int ne00 = src0 ? src0->ne[0] : 0;
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
#
2+
13
import sys
4+
import logging
5+
logger = logging.getLogger("opencl-embed-kernerl")
6+
27

38
def main():
4-
if len(sys.argv) != 3:
5-
print("Usage: python embed_kernel.py <input_file> <output_file>")
6-
exit(1)
9+
logging.basicConfig(level=logging.INFO)
10+
11+
if len(sys.argv) != 3:
12+
logger.info("Usage: python embed_kernel.py <input_file> <output_file>")
13+
sys.exit(1)
14+
15+
ifile = open(sys.argv[1], "r")
16+
ofile = open(sys.argv[2], "w")
717

8-
ifile = open(sys.argv[1], "r")
9-
ofile = open(sys.argv[2], "w")
18+
ofile.write("R\"(\n\n")
19+
ofile.write(ifile.read())
20+
ofile.write("\n)\"")
1021

11-
ofile.write("R\"(\n\n")
12-
ofile.write(ifile.read())
13-
ofile.write("\n)\"")
22+
ifile.close()
23+
ofile.close()
1424

15-
ifile.close()
16-
ofile.close()
1725

1826
if __name__ == "__main__":
19-
main()
27+
main()

ggml/src/ggml-opencl2/kernels/ggml-opencl2.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#elif defined(cl_amd_fp16)
77
#pragma OPENCL EXTENSION cl_amd_fp16 : enable
88
#else
9-
#error "Half precision floating point not supportedby OpenCL implementation on your device."
9+
#error "Half precision floating point not supportedby OpenCL implementation on your device."
1010
#endif
1111

1212
#ifdef cl_khr_subgroups

ggml/src/ggml-opencl2/kernels/ggml-opencl2_cvt.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#elif defined(cl_amd_fp16)
1212
#pragma OPENCL EXTENSION cl_amd_fp16 : enable
1313
#else
14-
#error "Half precision floating point not supportedby OpenCL implementation on your device."
14+
#error "Half precision floating point not supportedby OpenCL implementation on your device."
1515
#endif
1616

1717
#ifdef cl_khr_subgroups

ggml/src/ggml-opencl2/kernels/ggml-opencl2_gemv_noshuffle.cl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#pragma OPENCL EXTENSION cl_qcom_extra_vector_types : enable
99
#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
1010

11-
// assume
11+
// assume
1212
#define QK4_0 32
1313
#define N_SIMDGROUP 4
1414

@@ -204,7 +204,7 @@ __kernel void kernel_gemv_noshuffle(
204204
int ne10, // K
205205
int ne12, // 1
206206
int ne0, // M
207-
int ne1, // N
207+
int ne1, // N
208208
int r2, // 1
209209
int r3)
210210
{

ggml/src/ggml-opencl2/kernels/ggml-opencl2_gemv_noshuffle_general.cl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#pragma OPENCL EXTENSION cl_qcom_extra_vector_types : enable
99
#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
1010

11-
// assume
11+
// assume
1212
#define QK4_0 32
1313
#define N_SIMDGROUP 4
1414

@@ -204,7 +204,7 @@ __kernel void kernel_gemv_noshuffle(
204204
int ne10, // K
205205
int ne12, // 1
206206
int ne0, // M
207-
int ne1, // N
207+
int ne1, // N
208208
int r2, // 1
209209
int r3)
210210
{

ggml/src/ggml-opencl2/kernels/ggml-opencl2_mm.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#elif defined(cl_amd_fp16)
1111
#pragma OPENCL EXTENSION cl_amd_fp16 : enable
1212
#else
13-
#error "Half precision floating point not supportedby OpenCL implementation on your device."
13+
#error "Half precision floating point not supportedby OpenCL implementation on your device."
1414
#endif
1515

1616
#ifdef cl_khr_subgroups

ggml/src/ggml-opencl2/kernels/ggml-opencl2_transpose_16.cl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
// 16-bit transpose, loading/storing an 8x8 tile of elements
55

66
kernel void kernel_transpose_16(
7-
__read_only image1d_buffer_t input,
8-
__write_only image1d_buffer_t output,
9-
const uint rows,
7+
__read_only image1d_buffer_t input,
8+
__write_only image1d_buffer_t output,
9+
const uint rows,
1010
const uint cols
1111
) {
1212

0 commit comments

Comments
 (0)