opencl: integrate backend dyn.load interface and fix compiler and format warnings

This commit is contained in:
Max Krasnyansky 2024-11-27 18:09:05 -08:00
parent c1af4b72b7
commit 8ad0bb30df
10 changed files with 41 additions and 43 deletions

View file

@ -30,8 +30,6 @@ GGML_BACKEND_API bool ggml_backend_is_opencl2(ggml_backend_t backend);
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl2_buffer_type(void); GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl2_buffer_type(void);
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl2_host_buffer_type(void); GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl2_host_buffer_type(void);
GGML_BACKEND_API ggml_backend_t ggml_backend_reg_opencl2_init(const char * params, void * user_data);
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_opencl2_reg(void); GGML_BACKEND_API ggml_backend_reg_t ggml_backend_opencl2_reg(void);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -6,7 +6,7 @@ if (OpenCL_FOUND)
set(TARGET_NAME ggml-opencl2) set(TARGET_NAME ggml-opencl2)
add_library(${TARGET_NAME} ggml_add_backend_library(${TARGET_NAME}
ggml-opencl2.cpp ggml-opencl2.cpp
../../include/ggml-opencl2.h) ../../include/ggml-opencl2.h)
target_link_libraries(${TARGET_NAME} PRIVATE ggml-base ${OpenCL_LIBRARIES}) target_link_libraries(${TARGET_NAME} PRIVATE ggml-base ${OpenCL_LIBRARIES})

View file

@ -1,6 +1,12 @@
// SPDX-FileCopyrightText: Copyright (c) Qualcomm Innovation Center, Inc. All rights reserved // SPDX-FileCopyrightText: Copyright (c) Qualcomm Innovation Center, Inc. All rights reserved
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
#define CL_TARGET_OPENCL_VERSION 220
// suppress warnings in CL headers for GCC and Clang
#pragma GCC diagnostic ignored "-Wgnu-anonymous-struct"
#pragma GCC diagnostic ignored "-Woverlength-strings"
#include "ggml-opencl2.h" #include "ggml-opencl2.h"
#include "ggml-backend.h" #include "ggml-backend.h"
#include "ggml-impl.h" #include "ggml-impl.h"
@ -1237,10 +1243,6 @@ static void ggml_backend_opencl2_buffer_init_tensor(ggml_backend_buffer_t buffer
tensor->extra = extra; tensor->extra = extra;
} }
} }
// This should be removed. Keep it to make it easier to identify the backend
// when debugging until backend is removed from tensor.
tensor->backend = GGML_BACKEND_TYPE_GPU;
} }
// The optimized gemm and gemv kernels are used for large matrices without batch. // 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 = {
/* .event_synchronize = */ NULL, /* .event_synchronize = */ NULL,
}; };
// // Backend registry
// Backend registration
//
GGML_API ggml_backend_t ggml_backend_reg_opencl2_init(const char * params, void * user_data) {
return ggml_backend_opencl2_init();
GGML_UNUSED(params);
GGML_UNUSED(user_data);
}
// new API
static const char * ggml_backend_opencl2_reg_get_name(ggml_backend_reg_t reg) { static const char * ggml_backend_opencl2_reg_get_name(ggml_backend_reg_t reg) {
return "OpenCL2"; return "OpenCL2";
@ -1986,6 +1977,7 @@ ggml_backend_reg_t ggml_backend_opencl2_reg(void) {
if (!initialized) { if (!initialized) {
reg = ggml_backend_reg { reg = ggml_backend_reg {
/* .api_version = */ GGML_BACKEND_API_VERSION,
/* .iface = */ ggml_backend_opencl2_reg_i, /* .iface = */ ggml_backend_opencl2_reg_i,
/* .context = */ NULL, /* .context = */ NULL,
}; };
@ -2004,6 +1996,8 @@ ggml_backend_reg_t ggml_backend_opencl2_reg(void) {
return ® return ®
} }
GGML_BACKEND_DL_IMPL(ggml_backend_opencl2_reg)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Debugging utils // Debugging utils
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -2921,13 +2915,11 @@ static void ggml_cl_mul_mat(ggml_backend_t backend, const ggml_tensor * src0, co
// init CL objects // init CL objects
// <--------------------------------------------> // // <--------------------------------------------> //
cl_int status; cl_int status;
cl_event evt;
cl_image_format img_fmt_1d; cl_image_format img_fmt_1d;
cl_image_desc img_desc_1d; cl_image_desc img_desc_1d;
cl_buffer_region region; cl_buffer_region region;
cl_mem A_image1d; cl_mem A_image1d;
cl_mem B_image1d; cl_mem B_image1d;
cl_mem A_sub_buffer;
cl_mem B_sub_buffer; cl_mem B_sub_buffer;
cl_mem C_d; cl_mem C_d;
// for B transpose // for B transpose

View file

@ -1,9 +1,16 @@
#
import sys import sys
import logging
logger = logging.getLogger("opencl-embed-kernerl")
def main(): def main():
logging.basicConfig(level=logging.INFO)
if len(sys.argv) != 3: if len(sys.argv) != 3:
print("Usage: python embed_kernel.py <input_file> <output_file>") logger.info("Usage: python embed_kernel.py <input_file> <output_file>")
exit(1) sys.exit(1)
ifile = open(sys.argv[1], "r") ifile = open(sys.argv[1], "r")
ofile = open(sys.argv[2], "w") ofile = open(sys.argv[2], "w")
@ -15,5 +22,6 @@ def main():
ifile.close() ifile.close()
ofile.close() ofile.close()
if __name__ == "__main__": if __name__ == "__main__":
main() main()