char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b6b003d2c
char/aliceai-t5-moe
1#include <sycl/ext/oneapi/backend/level_zero.hpp> 2#include <sycl/sycl.hpp> 3 4#ifdef GGML_SYCL_SUPPORT_LEVEL_ZERO_API 5#include <level_zero/ze_api.h> 6#include <level_zero/zes_api.h> 7#endif 8 9#include <cstdint> 10#include <iostream> 11#include <vector> 12 13#include "base.hpp" 14#include "mem.hpp" 15 16const char * mem_api_int2str (int mem_api ) { 17if (mem_api == MEMORY_API_TYPE_SYCL ) { 18return "SYCL API" ; 19 }else if (mem_api == MEMORY_API_TYPE_LEVEL_ZERO ) { 20return "Level Zero API" ; 21 }else { 22return "Unknown" ; 23 } 24} 25 26#ifdef GGML_SYCL_SUPPORT_LEVEL_ZERO_API 27/* 28* Depend on to call zesInit(0) before any other Level Zero API calls, otherwise the Level Zero API calls may fail. 29*/ 30bool query_free_memory_by_ze (sycl::device dev ,size_t & free_bytes ,size_t & total_bytes ) { 31GGML_SYCL_DEBUG ("[SYCL] call %s: Querying free memory using Level Zero API.\n" ,__func__ ); 32 33free_bytes = 0 ; 34total_bytes = 0 ; 35 36uint32_t module_count = 0 ; 37 38#if defined(SYCL_EXT_ONEAPI_BACKEND_LEVEL_ZERO ) 39constexpr sycl::backend kL0Backend = sycl::backend::ext_oneapi_level_zero ; 40#else 41constexpr sycl::backend kL0Backend = sycl::backend::level_zero ; 42#endif 43 44try { 45 46if (dev .get_platform ().get_backend ()!= kL0Backend ) { 47GGML_SYCL_DEBUG ("Device backend is not Level Zero.\n" ); 48return false; 49 } 50 51ze_device_handle_t ze_dev = sycl::get_native < kL0Backend > (dev ); 52if (ze_dev == nullptr ) { 53GGML_SYCL_DEBUG ("Level Zero device handle is null.\n" ); 54return false; 55 } 56 57ze_result_t r = zesDeviceEnumMemoryModules (ze_dev ,& module_count ,nullptr ); 58if (r != ZE_RESULT_SUCCESS || module_count == 0 ) { 59GGML_SYCL_DEBUG ("Failed to enumerate Level Zero memory modules.\n" ); 60return false; 61 } 62 63 std::vector < zes_mem_handle_t > modules (module_count ); 64r = zesDeviceEnumMemoryModules (ze_dev ,& module_count ,modules .data ()); 65if (r != ZE_RESULT_SUCCESS || module_count == 0 ) { 66GGML_SYCL_DEBUG ("Failed to enumerate Level Zero memory modules.\n" ); 67return false; 68 } 69 70for (uint32_t i = 0 ;i < module_count ;++ i ) { 71zes_mem_state_t state = {}; 72state .stype = ZES_STRUCTURE_TYPE_MEM_STATE ; 73state .pNext = nullptr ; 74 75r = zesMemoryGetState (modules [i ],& state ); 76if (r != ZE_RESULT_SUCCESS ) { 77continue ; 78 } 79 80free_bytes += state .free ; 81total_bytes += state .size ; 82 } 83 84if (total_bytes == 0 ) { 85GGML_SYCL_DEBUG ("Level Zero memory query returned zero total bytes.\n" ); 86return false; 87 } 88return total_bytes >=free_bytes ; 89 90 }catch (const sycl::exception & e ) { 91GGML_SYCL_DEBUG ("Level Zero memory query failed: %s\n" ,e .what ()); 92return false; 93 } 94} 95#endif 96 97bool get_memory_size_by_sycl_api (sycl::device dev ,size_t & free_bytes ,size_t & total_bytes ) { 98GGML_SYCL_DEBUG ("[SYCL] call %s: Querying free memory using SYCL API.\n" ,__func__ ); 99total_bytes = dev .get_info < sycl::info::device::global_mem_size > (); 100 101#if (defined(__SYCL_COMPILER_VERSION )&& __SYCL_COMPILER_VERSION >=20221105 ) 102if (dev .has (sycl::aspect::ext_intel_free_memory )) { 103try { 104GGML_SYCL_DEBUG ("Querying free memory using SYCL aspect::ext_intel_free_memory.\n" ); 105free_bytes = dev .get_info < sycl::ext::intel::info::device::free_memory > (); 106return true; 107 }catch (const sycl::exception & ) { 108GGML_SYCL_DEBUG ( 109"Failed to query free memory using SYCL aspect::ext_intel_free_memory.\n" ); 110return false; 111 } 112 }else { 113GGML_SYCL_DEBUG ( 114"Device does not support SYCL aspect::ext_intel_free_memory.\n" ); 115 } 116#else 117GGML_SYCL_DEBUG ("SYCL Compiler version is older than 20221105.\n" ); 118#endif 119return false; 120} 121 122bool get_memory_size (sycl::device dev ,size_t & free_bytes ,size_t & total_bytes ,MemoryAPIType api_type ) { 123 124GGML_SYCL_DEBUG ("[%s]GPU Name: %s\n" ,__func__ , 125dev .get_info < sycl::info::device::name > ().c_str ()); 126GGML_SYCL_DEBUG ("[%s]GPU Vendor: %s\n" ,__func__ , 127dev .get_info < sycl::info::device::vendor > ().c_str ()); 128 129if (api_type == MEMORY_API_TYPE_LEVEL_ZERO ) { 130#ifdef GGML_SYCL_SUPPORT_LEVEL_ZERO_API 131GGML_SYCL_DEBUG ("[%s] Querying free memory using Level Zero API.\n" ,__func__ ); 132if (query_free_memory_by_ze (dev ,free_bytes ,total_bytes )) { 133return true; 134 } 135//fallback to SYCL API if Level Zero API fails 136GGML_SYCL_DEBUG ("[%s] Falling back to SYCL API for memory query.\n" ,__func__ ); 137#endif 138 } 139 140//MEMORY_API_TYPE_SYCL 141if (get_memory_size_by_sycl_api (dev ,free_bytes ,total_bytes )){ 142return true; 143 } 144 145//Todo, fallback to other methods to get free memory size, such as using OS-specific APIs (e.g., /proc/meminfo on Linux, GlobalMemoryStatusEx on Windows, etc.) 146GGML_SYCL_DEBUG ( 147"[%s] Can't get free mem size by Level Zero and SYCL API. Using total memory as free memory.\n" ,__func__ ); 148free_bytes = total_bytes ; 149 150return true; 151}