okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2eba71548
main
1// src/core/copy_queue.cpp - see include/strata/copy_queue.hpp. 2#include "strata/copy_queue.hpp" 3#include "strata/sycl_queue.hpp" 4 5#include <dpct/dpct.hpp> 6#include <sycl/ext/oneapi/backend/level_zero.hpp> 7 8#if defined(__has_include ) 9#if __has_include (< level_zero /ze_api .h > ) 10#include <level_zero/ze_api.h> 11#define STRATA_HAVE_ZE_API 1 12#endif 13#endif 14 15#include <atomic> 16#include <mutex> 17#include <cstdio> 18#include <cstdlib> 19#include <cstring> 20#include <vector> 21 22#ifdef STRATA_HAVE_ZE_API 23#if defined(_WIN32 ) 24#include <windows.h> 25#else 26#include <dlfcn.h> 27#endif 28#endif 29 30namespace strata { 31namespace { 32std::atomic < bool > g_active {false}; 33 34#ifdef STRATA_HAVE_ZE_API 35struct ZeFns { 36 decltype(& zeDeviceGetCommandQueueGroupProperties )get_groups = nullptr ; 37 decltype(& zeCommandListCreateImmediate )create_imm = nullptr ; 38}; 39bool load_ze (ZeFns & f ) { 40#if defined(_WIN32 ) 41HMODULE h = LoadLibraryA ("ze_loader.dll" ); 42if (!h )return false; 43auto sym = [& ](const char * n ) {return (void * )GetProcAddress (h ,n ); }; 44#else 45void * h = dlopen ("libze_loader.so.1" ,RTLD_NOW |RTLD_NOLOAD );// already loaded by the SYCL runtime 46if (!h )h = dlopen ("libze_loader.so.1" ,RTLD_NOW ); 47if (!h )return false; 48auto sym = [& ](const char * n ) {return dlsym (h ,n ); }; 49#endif 50f .get_groups = (decltype(f .get_groups ))sym ("zeDeviceGetCommandQueueGroupProperties" ); 51f .create_imm = (decltype(f .create_imm ))sym ("zeCommandListCreateImmediate" ); 52return f .get_groups && f .create_imm ; 53} 54#endif 55}// namespace 56 57bool copy_engine_active () {return g_active .load (); } 58 59namespace { 60// one copy-engine queue per device (a layer split calls this once per card): device -> queue; a device that has none maps to nullptr 61struct DevQ { sycl::device dev ; sycl::queue * q ; }; 62std::vector < DevQ >& dev_queues () {static std::vector < DevQ > v ;return v ; } 63std::mutex & dev_mu () {static std::mutex m ;return m ; } 64}// namespace 65bool is_copy_engine_queue (const sycl::queue * q ) { 66if (q == nullptr )return false; 67 std::lock_guard < std::mutex > lk (dev_mu ()); 68for (const DevQ & d :dev_queues ())if (d .q == q )return true; 69return false; 70} 71 72 73sycl::queue * make_copy_engine_queue (const char * what ) { 74static const bool off = [] {const char * v = std::getenv ("STRATA_COPY_ENGINE" );return v && v [0 ]== '0' ; }(); 75if (off )return nullptr ; 76#ifndef STRATA_HAVE_ZE_API 77 (void )what ; 78return nullptr ; 79#else 80 std::lock_guard < std::mutex > lk (dev_mu ()); 81 sycl::device dev = dpct::get_current_device (); 82for (const DevQ & d :dev_queues ())if (d .dev == dev )return d .q ;// asked before for this card (nullptr: it has none) 83dev_queues ().push_back ({dev ,nullptr }); 84const size_t slot = dev_queues ().size ()- 1 ; 85try { 86 87if (dev .get_backend ()!= sycl::backend::ext_oneapi_level_zero )return nullptr ; 88// Battlemage only unless STRATA_COPY_ENGINE=1: on an Arc A750 (i915, JIT) the prompt path hung with it once the stager's 89// completion flag became a copy-engine memcpy (4K and 600-token prompts, 10+ minutes, GPU not hung), and the measured gain 90// there was 1.4% (a prompt there is compute bound). 91 { 92const char * force = std::getenv ("STRATA_COPY_ENGINE" ); 93if (!(force && force [0 ]== '1' )&& strata::intel_gpu_gen (dev )!= strata::IntelGpuGen ::Battlemage )return nullptr ; 94 } 95// The legacy Level Zero adapter already puts memcpy on the copy engine (and its kernel launches are slower); only 96// the v2 adapter needs this. SYCL_UR_USE_LEVEL_ZERO_V2=0 selects the legacy one. 97const char * v2 = std::getenv ("SYCL_UR_USE_LEVEL_ZERO_V2" ); 98if (v2 && v2 [0 ]== '0' )return nullptr ; 99ZeFns zf ; 100if (!load_ze (zf ))return nullptr ; 101 sycl::context ctx = dpct::get_default_context (); 102auto zctx = sycl::get_native < sycl::backend::ext_oneapi_level_zero > (ctx ); 103auto zdev = sycl::get_native < sycl::backend::ext_oneapi_level_zero > (dev ); 104uint32_t ng = 0 ; 105if (zf .get_groups (zdev ,& ng ,nullptr )!= ZE_RESULT_SUCCESS || ng == 0 )return nullptr ; 106 std::vector < ze_command_queue_group_properties_t > g (ng ); 107for (auto & x :g ) { std::memset (& x ,0 ,sizeof (x ));x .stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_GROUP_PROPERTIES ; } 108if (zf .get_groups (zdev ,& ng ,g .data ())!= ZE_RESULT_SUCCESS )return nullptr ; 109int ord = -1 ; 110for (uint32_t i = 0 ;i < ng ;++ i ) 111if ((g [i ].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COPY )&& 112 !(g [i ].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COMPUTE )&& g [i ].numQueues > 0 ) {ord = (int )i ;break ; } 113if (ord < 0 )return nullptr ; 114ze_command_queue_desc_t d ; 115 std::memset (& d ,0 ,sizeof (d )); 116d .stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC ; 117d .ordinal = (uint32_t )ord ; 118d .index = 0 ; 119d .flags = ZE_COMMAND_QUEUE_FLAG_IN_ORDER ; 120d .mode = ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS ; 121d .priority = ZE_COMMAND_QUEUE_PRIORITY_NORMAL ; 122ze_command_list_handle_t cl = nullptr ; 123if (zf .create_imm (zctx ,zdev ,& d ,& cl )!= ZE_RESULT_SUCCESS || cl == nullptr )return nullptr ; 124 sycl::backend_input_t < sycl::backend::ext_oneapi_level_zero , sycl::queue > in ( 125cl ,dev , sycl::ext::oneapi::level_zero::ownership::keep , 126 sycl::property_list {sycl::property::queue::in_order {}}); 127auto * q = new sycl::queue (sycl::make_queue < sycl::backend::ext_oneapi_level_zero > (in ,ctx )); 128dev_queues ()[slot ].q = q ; 129g_active .store (true); 130static std::atomic < bool > said {false}; 131if (!said .exchange (true)) 132 std::fprintf (stderr ,"strata: copy engine: %s copies run on the GPU's copy engine (queue group %d), not the " 133"compute engine (STRATA_COPY_ENGINE=0 turns it off)\n" ,what ,ord ); 134return q ; 135 }catch (const std::exception & e ) { 136 std::fprintf (stderr ,"strata: copy engine queue not available (%s): ordinary queue\n" ,e .what ()); 137return nullptr ; 138 } 139#endif 140} 141 142}// namespace strata