LLM inference in C/C++
C++ ggml
ggml-opencl : replace alloca() with std::vector (#29765)
Adrien Gallouëtf7b384c1e5c5
diff --git a/ggml/src/ggml-opencl/ggml-opencl.cpp b/ggml/src/ggml-opencl/ggml-opencl.cppindex 4dcc9e9574..74bddd7036 100644--- a/ggml/src/ggml-opencl/ggml-opencl.cpp+++ b/ggml/src/ggml-opencl/ggml-opencl.cpp@@ -6612,14 +6612,14 @@ static bool ggml_opencl_is_device_supported(ggml_backend_dev_t dev) {#endifsize_t ext_str_size;- clGetDeviceInfo(dev_ctx->device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_str_size);+ CL_CHECK(clGetDeviceInfo(dev_ctx->device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_str_size));- char *ext_buffer = (char *)alloca(ext_str_size + 1);- clGetDeviceInfo(dev_ctx->device, CL_DEVICE_EXTENSIONS, ext_str_size, ext_buffer, NULL);+ std::vector<char> ext_buffer(ext_str_size + 1);+ CL_CHECK(clGetDeviceInfo(dev_ctx->device, CL_DEVICE_EXTENSIONS, ext_str_size, ext_buffer.data(), NULL));ext_buffer[ext_str_size] = '\0';// Check if ext_buffer contains cl_khr_fp16- bool fp16_support = strstr(ext_buffer, "cl_khr_fp16") != NULL;+ bool fp16_support = strstr(ext_buffer.data(), "cl_khr_fp16") != NULL;if (!fp16_support) {GGML_LOG_WARN("ggml_opencl: device does not support FP16\n");return false;@@ -6627,8 +6627,8 @@ static bool ggml_opencl_is_device_supported(ggml_backend_dev_t dev) {// If OpenCL 3.0 is supported, then check for cl_khr_subgroups, which becomes// optional in OpenCL 3.0 (cl_khr_subgroup is mandatory in OpenCL 2.x)- if (opencl_c_version.major == 3 && strstr(ext_buffer, "cl_khr_subgroups") == NULL &&- strstr(ext_buffer, "cl_intel_subgroups") == NULL) {+ if (opencl_c_version.major == 3 && strstr(ext_buffer.data(), "cl_khr_subgroups") == NULL &&+ strstr(ext_buffer.data(), "cl_intel_subgroups") == NULL) {GGML_LOG_WARN("ggml_opencl: device does not support subgroups (cl_khr_subgroups or cl_intel_subgroups) ""(note that subgroups is an optional feature in OpenCL 3.0)\n");return false;@@ -6691,13 +6691,13 @@ static ggml_backend_opencl_context * ggml_cl_init(ggml_backend_dev_t dev) {// Check driver versionsize_t driver_version_str_size;- clGetDeviceInfo(device, CL_DRIVER_VERSION, 0, NULL, &driver_version_str_size);- char *driver_version = (char *)alloca(driver_version_str_size + 1);- clGetDeviceInfo(device, CL_DRIVER_VERSION, driver_version_str_size, driver_version, NULL);+ CL_CHECK(clGetDeviceInfo(device, CL_DRIVER_VERSION, 0, NULL, &driver_version_str_size));+ std::vector<char> driver_version(driver_version_str_size + 1);+ CL_CHECK(clGetDeviceInfo(device, CL_DRIVER_VERSION, driver_version_str_size, driver_version.data(), NULL));driver_version[driver_version_str_size] = '\0';- backend_ctx->driver_version = driver_version;+ backend_ctx->driver_version = driver_version.data();- backend_ctx->adreno_cl_compiler_version = get_adreno_cl_compiler_version(driver_version);+ backend_ctx->adreno_cl_compiler_version = get_adreno_cl_compiler_version(driver_version.data());backend_ctx->has_vector_subgroup_broadcast =(backend_ctx->adreno_cl_compiler_version.type == E031 && backend_ctx->adreno_cl_compiler_version.major >= 47) ||(backend_ctx->adreno_cl_compiler_version.type == DX && backend_ctx->adreno_cl_compiler_version.major >= 17);@@ -6713,33 +6713,33 @@ static ggml_backend_opencl_context * ggml_cl_init(ggml_backend_dev_t dev) {!backend_ctx->adreno_cl_compiler_version.newer_than_or_same(E031, 45, 0, 0);size_t ext_str_size;- clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_str_size);- char *ext_buffer = (char *)alloca(ext_str_size + 1);- clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ext_str_size, ext_buffer, NULL);+ CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_str_size));+ std::vector<char> ext_buffer(ext_str_size + 1);+ CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ext_str_size, ext_buffer.data(), NULL));ext_buffer[ext_str_size] = '\0'; // ensure it is null terminated// check support for qcom_subgroup_shuffle- if (strstr(ext_buffer, "cl_qcom_subgroup_shuffle") != NULL) {+ if (strstr(ext_buffer.data(), "cl_qcom_subgroup_shuffle") != NULL) {backend_ctx->has_qcom_subgroup_shuffle = true;}// Check if ext_buffer contains cl_khr_fp16- backend_ctx->fp16_support = strstr(ext_buffer, "cl_khr_fp16") != NULL;+ backend_ctx->fp16_support = strstr(ext_buffer.data(), "cl_khr_fp16") != NULL;// check Adreno large buffer support- backend_ctx->adreno_has_large_buffer = strstr(ext_buffer, "cl_qcom_large_buffer") != NULL;+ backend_ctx->adreno_has_large_buffer = strstr(ext_buffer.data(), "cl_qcom_large_buffer") != NULL;// subgroup shuffle support (N_SPLIT>1 FA kernel)- backend_ctx->has_qcom_subgroup_shuffle = strstr(ext_buffer, "cl_qcom_subgroup_shuffle") != NULL;+ backend_ctx->has_qcom_subgroup_shuffle = strstr(ext_buffer.data(), "cl_qcom_subgroup_shuffle") != NULL;backend_ctx->has_subgroup_shuffle =- strstr(ext_buffer, "cl_khr_subgroup_shuffle") != NULL ||+ strstr(ext_buffer.data(), "cl_khr_subgroup_shuffle") != NULL ||backend_ctx->has_qcom_subgroup_shuffle;// check for cl_khr_integer_dot_product// cl_qcom_dot_product8 uses signed * unsigned// while cl_khr_integer_dot_product uses signed * signed -- we stick with khr for nowbackend_ctx->has_integer_dot =- strstr(ext_buffer, "cl_khr_integer_dot_product") != NULL;+ strstr(ext_buffer.data(), "cl_khr_integer_dot_product") != NULL;cl_uint base_align_in_bits;CL_CHECK(clGetDeviceInfo(device, CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(cl_uint), &base_align_in_bits, NULL));