okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// Adapted from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d: 2// ggml/src/ggml-cuda/{dsv4-hc.cu,scale.cu,unary.cu,unary.cuh}. 3// 4// MIT License 5// Copyright (c) 2023-2026 The ggml authors 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to deal 9// in the Software without restriction, including without limitation the rights 10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11// copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in all 15// copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23// SOFTWARE. 24 25#define DPCT_PROFILING_ENABLED 26#include <sycl/sycl.hpp> 27#include "strata/kprof.hpp" 28#include <dpct/dpct.hpp> 29#include "strata/sycl_queue.hpp" 30#include "strata/kernels/native_gr_postops.hpp" 31#include <cstddef> 32#include <cstdint> 33#include <limits> 34#include <stdexcept> 35#include <string> 36#include <cmath> 37 38namespace strata::kernels { 39namespace { 40constexpr int THREADS = 256 ; 41__dpct_inline__ float sigmoid (float x ) { 42return 1.0f / (1.0f + sycl::native::exp (- x )); 43} 44// ggml SCALE uses scale*x+bias, including its +0 bias. Retain this operation 45// explicitly so compile-time zero does not change signed-zero behavior. 46__dpct_inline__ float scale_zero_bias (float x ,float scale ) { 47/* 48DPCT1013: The rounding mode could not be specified and the generated 49code may have different accuracy than the original code. Verify the 50correctness. SYCL math built-in function rounding mode is aligned with 51OpenCL C 1.2 standard. 52*/ 53return sycl::fma (scale ,x ,0.0f ); 54} 55__dpct_inline__ void down_silu (float * lo ,int count ,float scale ) { 56auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 57const std::size_t i = 58 std::size_t (item_ct1 .get_group (2 ))* item_ct1 .get_local_range (2 )+ 59item_ct1 .get_local_id (2 ); 60if (i >= std::size_t (count ))return ; 61const float x = scale_zero_bias (lo [i ],scale ); 62lo [i ]= x / (1.0f + sycl::native::exp (- x )); 63} 64template < bool Fused > 65__dpct_inline__ void 66pre_gated (const float * __restrict__xn ,float * __restrict__gate , 67float * __restrict__mixed ,int n_embd ,int hc ,float scale ) { 68auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 69const std::size_t d = 70 std::size_t (item_ct1 .get_group (2 ))* item_ct1 .get_local_range (2 )+ 71item_ct1 .get_local_id (2 ); 72if (d >= std::size_t (n_embd ))return ; 73const std::size_t t = item_ct1 .get_group (1 ); 74xn += t * std::size_t (n_embd )* hc ; 75gate += t * std::size_t (n_embd )* hc ; 76mixed += t * std::size_t (n_embd ); 77float sum = 0.0f ; 78for (int c = 0 ;c < hc ;++ c ) { 79const std::size_t i = std::size_t (c )* n_embd + d ; 80const float x = xn [i ],w = sigmoid (gate [i ]); 81/* 82DPCT1013: The rounding mode could not be specified and the generated 83code may have different accuracy than the original code. Verify the 84correctness. SYCL math built-in function rounding mode is aligned with 85OpenCL C 1.2 standard. 86*/ 87const float product = x * w ; 88gate [i ]= product ; 89/* 90DPCT1013: The rounding mode could not be specified and the generated 91code may have different accuracy than the original code. Verify the 92correctness. SYCL math built-in function rounding mode is aligned with 93OpenCL C 1.2 standard. 94*/ 95if constexpr (Fused )sum = sycl::fma (x ,w ,sum ); 96/* 97DPCT1013: The rounding mode could not be specified and the generated 98code may have different accuracy than the original code. Verify the 99correctness. SYCL math built-in function rounding mode is aligned with 100OpenCL C 1.2 standard. 101*/ 102else sum = c == 0 ?product :sum + product ; 103 } 104if constexpr (Fused )mixed [d ]= scale * sum ; 105else mixed [d ]= scale_zero_bias (sum ,scale ); 106} 107__dpct_inline__ void post (const float * residual , 108const float * __restrict__block_out , 109const float * __restrict__inject ,float * output , 110int n_embd ,int hc ,float scale ) { 111auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 112const std::size_t i = 113 std::size_t (item_ct1 .get_group (2 ))* item_ct1 .get_local_range (2 )+ 114item_ct1 .get_local_id (2 ); 115if (i >= std::size_t (n_embd )* hc )return ; 116const std::size_t t = item_ct1 .get_group (1 ); 117residual += t * std::size_t (n_embd )* hc ; 118block_out += t * std::size_t (n_embd ); 119inject += t * std::size_t (hc ); 120output += t * std::size_t (n_embd )* hc ; 121const int c = int (i /n_embd ),d = int (i %n_embd ); 122const float weight = scale_zero_bias (sigmoid (scale_zero_bias (inject [c ],scale )),2.0f ); 123// Exact residual/output alias is supported; no other thread reads residual[i]. 124/* 125DPCT1013: The rounding mode could not be specified and the generated 126code may have different accuracy than the original code. Verify the 127correctness. SYCL math built-in function rounding mode is aligned with 128OpenCL C 1.2 standard. 129*/ 130output [i ]= sycl::fma (block_out [d ],weight ,residual [i ]); 131} 132void check_pointer (const void * p ) { 133if (!p || reinterpret_cast < std::uintptr_t > (p ) % alignof(float )) 134throw std::invalid_argument ("native GR postops require non-null four-byte aligned pointers" ); 135} 136void check_shape (int n ,int hc ) { 137if (n <=0 || hc <=0 || std::uint64_t (n )* hc > std::uint64_t (std::numeric_limits < int > ::max ())) 138throw std::invalid_argument ("native GR postops require positive bounded dimensions" ); 139} 140unsigned blocks (std::size_t n ) {return unsigned ((n + THREADS - 1 ) /THREADS ); } 141void check_launch () { 142/* 143DPCT1010: SYCL uses exceptions to report errors and does not use the 144error codes. The cudaGetLastError function call was replaced with 0. You 145need to rewrite this code. 146*/ 147const auto error = 0 ; 148/* 149DPCT1000: Error handling if-stmt was detected but could not be 150rewritten. 151*/ 152if (error != 0 ) 153/* 154DPCT1009: SYCL reports errors using exceptions and does not use 155error codes. Please replace the "get_error_string_dummy(...)" with a 156real error-handling function. 157*/ 158/* 159DPCT1001: The statement could not be removed. 160*/ 161throw std::runtime_error (std::string ("native GR postops launch: " )+ 162 dpct::get_error_string_dummy (error )); 163} 164}// namespace 165 166void native_gr_down_silu (float * lo ,int hc_lr ,int hc ,void * stream ) { 167check_shape (hc_lr ,hc ); 168check_pointer (lo ); 169 { 170auto exp_props = sycl::ext::oneapi::experimental::properties { 171 sycl::ext::oneapi::experimental::use_root_sync }; 172 173 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 174-> submit ([& ](sycl::handler & cgh ) { 175auto float_hc_ct2 = 1.0f /float (hc ); 176 177cgh .parallel_for < dpct_kernel_name < class down_silu_24f4fb >> ( 178 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (hc_lr ))* 179 sycl::range (1 ,1 ,THREADS ), 180 sycl::range (1 ,1 ,THREADS )), 181exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 182down_silu (lo ,hc_lr ,float_hc_ct2 ); 183 }); 184 }); 185 } 186check_launch (); 187} 188void native_gr_pre_gated (const float * xn ,float * gate ,float * mixed , 189int n_embd ,int hc ,bool fused_layer ,void * stream ) { 190native_gr_pre_gated_multi (xn ,gate ,mixed ,n_embd ,hc ,1 ,fused_layer ,stream ); 191} 192void native_gr_pre_gated_multi (const float * xn ,float * gate ,float * mixed , 193int n_embd ,int hc ,int n_tok ,bool fused_layer ,void * stream ) { 194if (n_tok <=0 ) 195throw std::invalid_argument ("native GR postops require positive n_tok" ); 196check_shape (n_embd ,hc ); 197check_pointer (xn );check_pointer (gate );check_pointer (mixed ); 198const dpct::dim3 grid {blocks (n_embd ),unsigned (n_tok ),1u }; 199if (fused_layer ) 200 { 201auto exp_props = sycl::ext::oneapi::experimental::properties { 202 sycl::ext::oneapi::experimental::use_root_sync }; 203 204 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 205-> submit ([& ](sycl::handler & cgh ) { 206auto float_hc_ct5 = 1.0f /float (hc ); 207 208cgh .parallel_for < dpct_kernel_name < class pre_gated_e6e3d1 , 209dpct_kernel_scalar < true>>> ( 210 sycl::nd_range < 3 > (grid * sycl::range (1 ,1 ,THREADS ), 211 sycl::range (1 ,1 ,THREADS )), 212exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 213pre_gated < true> (xn ,gate ,mixed ,n_embd ,hc , 214float_hc_ct5 ); 215 }); 216 }); 217 }else { 218auto exp_props = sycl::ext::oneapi::experimental::properties { 219 sycl::ext::oneapi::experimental::use_root_sync }; 220 221 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 222-> submit ([& ](sycl::handler & cgh ) { 223auto float_hc_ct5 = 1.0f /float (hc ); 224 225cgh .parallel_for < dpct_kernel_name < class pre_gated_10bc88 , 226dpct_kernel_scalar < false>>> ( 227 sycl::nd_range < 3 > (grid * sycl::range (1 ,1 ,THREADS ), 228 sycl::range (1 ,1 ,THREADS )), 229exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 230pre_gated < false> (xn ,gate ,mixed ,n_embd ,hc , 231float_hc_ct5 ); 232 }); 233 }); 234 } 235check_launch (); 236} 237void native_gr_post (const float * residual ,const float * block_out ,const float * inject , 238float * output ,int n_embd ,int hc ,void * stream ) { 239native_gr_post_multi (residual ,block_out ,inject ,output ,n_embd ,hc ,1 ,stream ); 240} 241void native_gr_post_multi (const float * residual ,const float * block_out ,const float * inject , 242float * output ,int n_embd ,int hc ,int n_tok ,void * stream ) { 243if (n_tok <=0 ) 244throw std::invalid_argument ("native GR postops require positive n_tok" ); 245check_shape (n_embd ,hc ); 246check_pointer (residual );check_pointer (block_out );check_pointer (inject );check_pointer (output ); 247const dpct::dim3 grid {blocks (std::size_t (n_embd )* hc ),unsigned (n_tok ), 2481u }; 249 { 250auto exp_props = sycl::ext::oneapi::experimental::properties { 251 sycl::ext::oneapi::experimental::use_root_sync }; 252 253 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 254-> submit ([& ](sycl::handler & cgh ) { 255auto float_hc_ct6 = 1.0f /float (hc ); 256 257cgh .parallel_for < dpct_kernel_name < class post_a7f844 >> ( 258 sycl::nd_range < 3 > (grid * sycl::range (1 ,1 ,THREADS ), 259 sycl::range (1 ,1 ,THREADS )), 260exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 261post (residual ,block_out ,inject ,output ,n_embd ,hc , 262float_hc_ct6 ); 263 }); 264 }); 265 } 266check_launch (); 267} 268}// namespace strata::kernels