okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1// src/prefill/ggml_cuda_host.cu - prompt-speed plan step 2b: the host-side symbols of llama.cpp's ggml-cuda that its MMQ 2// and quantize code reference, for the MMQ kernels compiled into strata_mmq without the rest of ggml-cuda.cu. 3#include "common.cuh" 4 5#define DPCT_PROFILING_ENABLED 6#include <sycl/sycl.hpp> 7#include <dpct/dpct.hpp> 8#include <cstdio> 9#include <cstdlib> 10#include <mutex> 11#include <vector> 12 13[[noreturn ]]void ggml_cuda_error (const char * stmt ,const char * func ,const char * file ,int line ,const char * msg ) { 14 std::fprintf (stderr ,"ggml-cuda (strata mmq): %s: %s\n in %s at %s:%d\n" ,msg ,stmt ,func ,file ,line ); 15 std::abort (); 16} 17 18int ggml_cuda_get_device () { 19int id = 0 ; 20CUDA_CHECK (DPCT_CHECK_ERROR (id = dpct::get_current_device_id ())); 21return id ; 22} 23 24const ggml_cuda_device_info & ggml_cuda_info () { 25static ggml_cuda_device_info info = [] { 26ggml_cuda_device_info in = {}; 27int n = 0 ; 28if (DPCT_CHECK_ERROR (n = dpct::device_count ())!= 0 )n = 0 ; 29n = n > GGML_CUDA_MAX_DEVICES ?GGML_CUDA_MAX_DEVICES :n ; 30in .device_count = n ; 31in .physical_device_count = n ; 32for (int id = 0 ;id < n ;++ id ) { 33 dpct::device_info prop ; 34CUDA_CHECK ( 35DPCT_CHECK_ERROR (dpct::get_device (id ).get_device_info (prop ))); 36auto & d = in .devices [id ]; 37/* 38DPCT1005: The SYCL device version is different from CUDA Compute 39Compatibility. You may need to rewrite this code. 40*/ 41d .cc = 42100 * prop .get_major_version ()+ 10 * prop .get_minor_version (); 43d .nsm (prop .set_max_compute_units ); 44/* 45DPCT1019: local_mem_size in SYCL is not a complete equivalent of 46sharedMemPerBlock in CUDA. You may need to adjust the code. 47*/ 48d .smpb (prop .set_local_mem_size ); 49/* 50DPCT1019: local_mem_size in SYCL is not a complete equivalent of 51sharedMemPerBlockOptin in CUDA. You may need to adjust the code. 52*/ 53d .smpbo (prop .set_local_mem_size ); 54/* 55DPCT1006: SYCL does not provide a standard API to differentiate 56between integrated and discrete GPU devices. 57*/ 58d .integrated = prop .get_integrated ()!= 0 ; 59d .vmm = false; 60d .total_vram (prop .set_global_mem_size ); 61d .warp_size (prop .set_max_sub_group_size ); 62d .supports_cooperative_launch = prop .cooperativeLaunch != 0 ; 63d .physical_device = id ; 64d .physical_share_count = 1 ; 65d .virtual_index = 0 ; 66 } 67return in ; 68 }(); 69return info ; 70} 71 72namespace { 73// Buffers are kept and reused: MMQ asks for the same few sizes every launch (its stream-k fixup tiles). 74struct CachingPool :ggml_cuda_pool { 75struct Buf {void * p ;size_t size ;bool used ; }; 76 std::vector < Buf > bufs ; 77 std::mutex mu ; 78void * alloc (size_t size ,size_t * actual_size )override { 79 std::lock_guard < std::mutex > lk (mu ); 80for (auto & b :bufs ) 81if (!b .used && b .size >=size ) {b .used = true;* actual_size = b .size ;return b .p ; } 82void * p = nullptr ; 83CUDA_CHECK (DPCT_CHECK_ERROR ( 84p = (void * )sycl::malloc_device (size , dpct::get_in_order_queue ()))); 85bufs .push_back ({p ,size , true}); 86* actual_size = size ; 87return p ; 88 } 89void free (void * ptr ,size_t )override { 90 std::lock_guard < std::mutex > lk (mu ); 91for (auto & b :bufs ) 92if (b .p == ptr ) {b .used = false;return ; } 93 } 94 ~CachingPool ()override { 95for (auto & b :bufs )cudaFree (b .p ); 96 } 97}; 98}// namespace 99 100std::unique_ptr < ggml_cuda_pool > ggml_backend_cuda_context::new_pool_for_device (int ,int ) { 101return std::unique_ptr < ggml_cuda_pool > (new CachingPool ()); 102} 103 104ggml_backend_cuda_context::~ggml_backend_cuda_context () {}