okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- blas_gemm_utils.hpp ----------------------*- C++ -*----------------==// 2// 3// Copyright (C) Intel Corporation 4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5// See https://llvm.org/LICENSE.txt for license information. 6// 7//===----------------------------------------------------------------------===// 8// This file contains the implementation of GEneral Matrix Multiplication by 9// using oneDNN. More datatype combinations and the epilogue can be supported by 10// oneDNN, which is not available in blas_utils.hpp using oneMKL. 11//===----------------------------------------------------------------------===// 12 13#ifndef __DPCT_BLAS_GEMM_UTILS_HPP__ 14#define __DPCT_BLAS_GEMM_UTILS_HPP__ 15 16#include "compat_service.hpp" 17#include "blas_utils.hpp" 18#include "dnnl_utils.hpp" 19 20namespace dpct::blas_gemm::experimental { 21enum class order_t : std::uint8_t { 22col , 23row , 24col32 , 25col4_4r2_8c , 26col32_2r_4r4 27}; 28}// namespace dpct::blas_gemm::experimental 29 30#include "detail/blas_gemm_utils_detail.hpp" 31 32namespace dpct::blas_gemm::experimental { 33enum class pointer_mode_t { 34host , 35device , 36device_vector , 37alpha_device_vector_beta_zero , 38alpha_device_vector_beta_host 39}; 40enum class epilogue_t { 41nop = 1 , 42relu , 43bias , 44gelu , 45gelu_bias , 46gelu_aux , 47gelu_aux_bias , 48dgelu , 49bgradb 50}; 51 52class descriptor ; 53using descriptor_ptr = descriptor * ; 54class matrix_layout_t ; 55using matrix_layout_ptr = matrix_layout_t * ; 56class matmul_desc_t ; 57using matmul_desc_ptr = matmul_desc_t * ; 58class transform_desc_t ; 59using transform_desc_ptr = transform_desc_t * ; 60 61class descriptor { 62public : 63descriptor () {} 64void init (sycl::queue * q_ptr ) { 65_engine = ::dnnl::sycl_interop::make_engine (q_ptr -> get_device (), 66q_ptr -> get_context ()); 67_engine_stream = ::dnnl::sycl_interop::make_stream (_engine ,* q_ptr ); 68 } 69 ::dnnl::engine get_engine ()const noexcept {return _engine ; } 70 ::dnnl::stream get_engine_stream ()const noexcept {return _engine_stream ; }; 71 72private : 73 ::dnnl::engine _engine ; 74 ::dnnl::stream _engine_stream ; 75}; 76 77class matrix_layout_t { 78public : 79enum class attribute { 80type , 81order , 82rows , 83cols , 84ld , 85batch_count , 86strided_batch_offset 87 }; 88 89matrix_layout_t (library_data_t type , std::uint64_t rows , std::uint64_t cols , 90 std::int64_t ld ) 91 :_type (type ),_rows (rows ),_cols (cols ),_ld (ld ) {} 92 93void set_attribute (attribute attr ,const void * mem ) { 94get_set_attr < true> (attr ,const_cast < void *> (mem )); 95 } 96void get_attribute (attribute attr ,void * mem ) { 97get_set_attr < false> (attr ,mem ); 98 } 99 100private : 101template < bool is_set > void get_set_attr (attribute attr ,void * mem ) { 102#define CASE (tag ) \ 103 case attribute::tag: \ 104 if constexpr (is_set) { \ 105 _##tag = *static_cast<decltype(_##tag) *>(mem); \ 106 } else { \ 107 *static_cast<decltype(_##tag) *>(mem) = _##tag; \ 108 } \ 109 break; 110switch (attr ) { 111CASE (type ) 112CASE (order ) 113CASE (rows ) 114CASE (cols ) 115CASE (ld ) 116CASE (batch_count ) 117CASE (strided_batch_offset ) 118 } 119#undef CASE 120 } 121 122library_data_t _type ; 123order_t _order = order_t::col ; 124 std::uint64_t _rows ; 125 std::uint64_t _cols ; 126 std::int64_t _ld ; 127 std::uint64_t _batch_count = 1 ; 128 std::uint64_t _strided_batch_offset = 0 ; 129 130friend sycl::event matmul (descriptor_ptr handle ,matmul_desc_ptr computeDesc , 131const void * alpha ,const void * a , 132matrix_layout_ptr a_desc ,const void * b , 133matrix_layout_ptr b_desc ,const void * beta , 134const void * c ,matrix_layout_ptr c_desc ,void * d , 135matrix_layout_ptr d_desc , 136 ::dpct::cs::queue_ptr q_ptr ); 137friend sycl::event 138matrix_transform (transform_desc_ptr transform_desc ,const void * alpha , 139const void * a ,matrix_layout_ptr a_desc ,const void * beta , 140const void * b ,matrix_layout_ptr b_desc ,void * c , 141matrix_layout_ptr c_desc , ::dpct::cs::queue_ptr q_ptr ); 142}; 143 144class matmul_desc_t { 145public : 146enum class attribute { 147compute_type , 148scale_type , 149bias_data_type , 150bias_pointer , 151pointer_mode , 152trans_a , 153trans_b , 154trans_c , 155epilogue , 156epilogue_aux_ld , 157epilogue_aux_pointer , 158epilogue_aux_data_type , 159a_scale_pointer , 160b_scale_pointer , 161d_scale_pointer , 162absmax_d_pointer , 163unsupport 164 }; 165 166matmul_desc_t (compute_type compute_type ,library_data_t scale_type ) 167 :_compute_type (compute_type ),_scale_type (scale_type ) {} 168 169void set_attribute (attribute attr ,const void * mem ) { 170if (attr != attribute::unsupport ) 171get_set_attr < true> (attr ,const_cast < void *> (mem )); 172 } 173void get_attribute (attribute attr ,void * mem ) { 174if (attr != attribute::unsupport ) 175get_set_attr < false> (attr ,mem ); 176 } 177 178private : 179template < bool is_set > void get_set_attr (attribute attr ,void * mem ) { 180#define CASE (tag ) \ 181 case attribute::tag: \ 182 if constexpr (is_set) { \ 183 _##tag = *static_cast<decltype(_##tag) *>(mem); \ 184 } else { \ 185 *static_cast<decltype(_##tag) *>(mem) = _##tag; \ 186 } \ 187 break; 188switch (attr ) { 189CASE (compute_type ) 190CASE (scale_type ) 191CASE (bias_data_type ) 192CASE (bias_pointer ) 193CASE (pointer_mode ) 194CASE (trans_a ) 195CASE (trans_b ) 196CASE (trans_c ) 197CASE (epilogue ) 198CASE (a_scale_pointer ) 199CASE (b_scale_pointer ) 200CASE (d_scale_pointer ) 201CASE (absmax_d_pointer ) 202CASE (epilogue_aux_ld ) 203CASE (epilogue_aux_pointer ) 204CASE (epilogue_aux_data_type ) 205default : 206break ; 207 } 208#undef CASE 209 } 210 211compute_type _compute_type ; 212library_data_t _scale_type ; 213library_data_t _bias_data_type = library_data_t::real_float ; 214pointer_mode_t _pointer_mode = pointer_mode_t::host ; 215 oneapi::mkl::transpose _trans_a = oneapi::mkl::transpose::nontrans ; 216 oneapi::mkl::transpose _trans_b = oneapi::mkl::transpose::nontrans ; 217 oneapi::mkl::transpose _trans_c = oneapi::mkl::transpose::nontrans ; 218epilogue_t _epilogue = epilogue_t::nop ; 219library_data_t _epilogue_aux_data_type = library_data_t::real_float ; 220size_t _epilogue_aux_ld = 0 ; 221void * _a_scale_pointer = nullptr ; 222void * _b_scale_pointer = nullptr ; 223void * _d_scale_pointer = nullptr ; 224void * _absmax_d_pointer = nullptr ; 225void * _bias_pointer = nullptr ; 226void * _epilogue_aux_pointer = nullptr ; 227 228friend sycl::event matmul (descriptor_ptr handle ,matmul_desc_ptr computeDesc , 229const void * alpha ,const void * a , 230matrix_layout_ptr a_desc ,const void * b , 231matrix_layout_ptr b_desc ,const void * beta , 232const void * c ,matrix_layout_ptr c_desc ,void * d , 233matrix_layout_ptr d_desc , 234 ::dpct::cs::queue_ptr q_ptr ); 235}; 236 237/// This function does the following operations: 238/// (1) D_temp = epilogue(alpha * scale_a * op_a(A) * scale_b * op_b(B) + beta * C) 239/// (2) Amax = absmax(D_temp) when matmul_desc_t::attribute::absmax_d_pointer is specified 240/// (3) D = scale_d * D_temp 241/// "op_a" is specified by the matmul_desc_t::attribute::trans_a 242/// (default is nontrans) 243/// "op_b" is specified by the matmul_desc_t::attribute::trans_b 244/// (default is nontrans) 245/// "scale_a" is specified by the matmul_desc_t::attribute::a_scale_pointer 246/// (default is nullptr, which behaves as 1.0) 247/// "scale_b" is specified by the matmul_desc_t::attribute::b_scale_pointer 248/// (default is nullptr, which behaves as 1.0) 249/// "scale_d" is specified by the matmul_desc_t::attribute::d_scale_pointer 250/// (default is nullptr, which behaves as 1.0) 251/// "alpha" can be a scalar value or a vector, which is specified by the 252/// matmul_desc_tattribute::::pointer_mode 253/// "epilogue" is specified by the matmul_desc_t::attribute::epilogue 254/// Currently, this function only supports the following type combinations: 255/// scale_type==int32 && a_type==int8 && b_type==int8 && c_type==int32; 256/// scale_type==float && a_type==int8 && b_type==int8 && c_type==int8; 257/// scale_type==float && a_type==int8 && b_type==int8 && c_type==int32; 258/// scale_type==float && a_type==float && b_type==float && c_type==float. 259/// Currently, this function only supports beta==0 or beta==1. 260/// Currently, this function only supports the relu, bias, gelu, gelu_bias, 261/// gelu_aux, gelu_aux_bias, dgelu and bgradb epilogue. 262/// NOTE: Non-col-major matrix will be converted to col-major matrix before. 263/// TODO: Impl row-major matmul without layout conversion. 264/// multiplication and converted back after multiplication. 265/// \param [in] handle A handle containing context info. 266/// \param [in] compute_desc Describe the computation. 267/// \param [in] alpha Scaling factor alpha. 268/// \param [in] a Input matrix A. 269/// \param [in] a_desc Describe the matrix A. 270/// \param [in] b Input matrix B. 271/// \param [in] b_desc Describe the matrix B. 272/// \param [in] beta Scaling factor beta. 273/// \param [in] c Input matrix C. 274/// \param [in] c_desc Describe the matrix C. 275/// \param [out] d Output matrix D. 276/// \param [in] d_desc Describe the matrix D. 277/// \param [in] q_ptr The queue where the routine should be executed. 278inline sycl::event matmul (descriptor_ptr handle ,matmul_desc_ptr compute_desc , 279const void * alpha ,const void * a , 280matrix_layout_ptr a_desc ,const void * b , 281matrix_layout_ptr b_desc ,const void * beta , 282const void * c ,matrix_layout_ptr c_desc ,void * d , 283matrix_layout_ptr d_desc , 284 ::dpct::cs::queue_ptr q_ptr ) { 285const size_t m = compute_desc -> _trans_a == oneapi::mkl::transpose::nontrans 286 ?a_desc -> _rows 287 :a_desc -> _cols ; 288const size_t n = d_desc -> _cols ; 289const size_t k = compute_desc -> _trans_b == oneapi::mkl::transpose::nontrans 290 ?b_desc -> _rows 291 :b_desc -> _cols ; 292const library_data_t a_type = a_desc -> _type ; 293const library_data_t b_type = b_desc -> _type ; 294const library_data_t c_type = c_desc -> _type ; 295const library_data_t d_type = d_desc -> _type ; 296const library_data_t scale_type = compute_desc -> _scale_type ; 297 298if (!q_ptr ) 299q_ptr = & ::dpct::cs::get_default_queue (); 300handle -> init (q_ptr ); 301bool vector_alpha = false; 302bool device_alpha = false; 303if (compute_desc -> _pointer_mode == pointer_mode_t::device_vector || 304compute_desc -> _pointer_mode == 305 pointer_mode_t::alpha_device_vector_beta_zero || 306compute_desc -> _pointer_mode == 307 pointer_mode_t::alpha_device_vector_beta_host ) { 308vector_alpha = true; 309device_alpha = true; 310 }else if (compute_desc -> _pointer_mode == pointer_mode_t::device ) { 311device_alpha = true; 312 } 313 314bool beta_is_zero = true; 315if (beta != nullptr ) { 316int beta_value = detail::type_dispatch < detail::get_beta_value_impl > ( 317compute_desc -> _scale_type ,beta ,q_ptr ); 318if (beta_value != 0 ) { 319beta_is_zero = false; 320if (beta_value != 1 ) 321throw std::runtime_error ( 322"dpct::blas_gemm::experimental::matmul() does " 323"not support non-zero and non-one beta currently." ); 324 } 325 } 326 327if (compute_desc -> _epilogue != epilogue_t::nop && 328compute_desc -> _epilogue != epilogue_t::relu && 329compute_desc -> _epilogue != epilogue_t::bias && 330compute_desc -> _epilogue != epilogue_t::gelu && 331compute_desc -> _epilogue != epilogue_t::gelu_bias && 332compute_desc -> _epilogue != epilogue_t::gelu_aux && 333compute_desc -> _epilogue != epilogue_t::gelu_aux_bias && 334compute_desc -> _epilogue != epilogue_t::dgelu && 335compute_desc -> _epilogue != epilogue_t::bgradb ) { 336throw std::runtime_error ( 337"dpct::blas_gemm::experimental::matmul() only " 338"supports relu, bias, gelu, gelu_bias, gelu_aux, " 339"gelu_aux_bias, dgelu and bgradb epilogue currently." ); 340 } 341 342if (!(compute_desc -> _scale_type == library_data_t::real_int32 && 343a_desc -> _type == library_data_t::real_int8 && 344b_desc -> _type == library_data_t::real_int8 && 345c_desc -> _type == library_data_t::real_int32 )&& 346 !(compute_desc -> _scale_type == library_data_t::real_float && 347a_desc -> _type == library_data_t::real_int8 && 348b_desc -> _type == library_data_t::real_int8 && 349c_desc -> _type == library_data_t::real_int8 )&& 350 !(compute_desc -> _scale_type == library_data_t::real_float && 351a_desc -> _type == library_data_t::real_int8 && 352b_desc -> _type == library_data_t::real_int8 && 353c_desc -> _type == library_data_t::real_int32 )&& 354 !(compute_desc -> _scale_type == library_data_t::real_float && 355a_desc -> _type == library_data_t::real_float && 356b_desc -> _type == library_data_t::real_float && 357c_desc -> _type == library_data_t::real_float )) { 358throw std::runtime_error ( 359"dpct::blas_gemm::experimental::matmul() only supports data type " 360"combinataions:\n scale_type==int32 && a_type==int8 && b_type==int8" 361"&& c_type==int32,\n scale_type==float && a_type==int8 && " 362"b_type==int8 && c_type==int8,\n scale_type==float && a_type==int8" 363"&& b_type==int8 && c_type==int32 or\n scale_type==float && " 364"a_type==float" 365"&& b_type==float && c_type==float." ); 366 } 367 368if (a_desc -> _batch_count != b_desc -> _batch_count || 369a_desc -> _batch_count != c_desc -> _batch_count || 370a_desc -> _batch_count != d_desc -> _batch_count ) { 371throw std::runtime_error ("dpct::blas_gemm::experimental::matmul() does not " 372"support different batch count." ); 373 } 374 375#ifdef DPCT_USM_LEVEL_NONE 376if (a_desc -> _batch_count != 1 ) { 377throw std::runtime_error ( 378"dpct::blas_gemm::experimental::matmul() only supports " 379"batch mode with USM enabled." ); 380 } 381#endif 382 383size_t a_elm_size = dpct::detail::library_data_size [static_cast < unsigned int > ( 384a_desc -> _type )] / 3858 ; 386size_t b_elm_size = dpct::detail::library_data_size [static_cast < unsigned int > ( 387b_desc -> _type )] / 3888 ; 389size_t c_elm_size = dpct::detail::library_data_size [static_cast < unsigned int > ( 390c_desc -> _type )] / 3918 ; 392size_t d_elm_size = dpct::detail::library_data_size [static_cast < unsigned int > ( 393d_desc -> _type )] / 3948 ; 395 396static const auto matmul_single = 397 [](descriptor_ptr handle ,matmul_desc_ptr compute_desc ,const void * alpha , 398const void * a ,matrix_layout_ptr a_desc ,const void * b , 399matrix_layout_ptr b_desc ,const void * beta ,const void * c , 400matrix_layout_ptr c_desc ,void * d ,matrix_layout_ptr d_desc , 401 ::dpct::cs::queue_ptr q_ptr ,const size_t m ,const size_t n , 402const size_t k ,const library_data_t a_type , 403const library_data_t b_type ,const library_data_t c_type , 404const library_data_t d_type ,const library_data_t scale_type , 405bool vector_alpha ,bool device_alpha ,bool beta_is_zero , 406size_t a_elm_size ,size_t b_elm_size ,size_t c_elm_size , 407size_t d_elm_size )-> sycl::event { 408// For non-col_major matrix, convert it to col_major. 409const void * new_a = a ; 410const void * new_b = b ; 411const void * new_c = c ; 412void * new_d = d ; 413bool new_b_allocated = false; 414bool new_c_allocated = false; 415bool new_d_allocated = false; 416size_t new_lda = a_desc -> _ld ,new_ldb = b_desc -> _ld ,new_ldc = c_desc -> _ld , 417new_ldd = d_desc -> _ld ; 418 std::vector < sycl::event > transform_events ; 419 420if (a_desc -> _order != order_t::col ) 421new_lda = a_desc -> _rows ; 422new_a = ::dpct::cs::malloc (a_elm_size * a_desc -> _cols * new_lda ,* q_ptr ); 423 sycl::event e_init ; 424if (a_desc -> _order != order_t::col ) 425e_init = detail::type_dispatch < detail::matrix_transform_impl > ( 426a_desc -> _type ,q_ptr ,a_desc -> _rows ,a_desc -> _cols ,a_desc -> _ld , 427a_desc -> _order , (const std::int8_t * )a ,new_lda , order_t::col , 428 (std::int8_t * )new_a , std::vector < sycl::event > {}); 429else 430e_init = ::dpct::cs::memcpy ( 431* q_ptr , (void * )new_a ,a ,a_elm_size * a_desc -> _cols * new_lda , 432 ::dpct::cs::memcpy_direction::device_to_device ); 433 434// alpha = alpha * scale_a * scale_b 435 sycl::event e_scale_new_a = detail::scale_new_a ( 436q_ptr ,m ,k , (void * )new_a ,a_type ,alpha ,scale_type ,vector_alpha , 437device_alpha ,compute_desc -> _a_scale_pointer , 438compute_desc -> _b_scale_pointer , {e_init }); 439 440transform_events .push_back (e_scale_new_a ); 441 442if (b_desc -> _order != order_t::col ) { 443new_ldb = b_desc -> _rows ; 444new_b = ::dpct::cs::malloc (b_elm_size * b_desc -> _cols * new_ldb ,* q_ptr ); 445new_b_allocated = true; 446 sycl::event e = detail::type_dispatch < detail::matrix_transform_impl > ( 447b_desc -> _type ,q_ptr ,b_desc -> _rows ,b_desc -> _cols ,b_desc -> _ld , 448b_desc -> _order ,b ,new_ldb , order_t::col ,const_cast < void *> (new_b ), 449 std::vector < sycl::event > {}); 450transform_events .push_back (e ); 451 } 452 453if (!beta_is_zero && c_desc -> _order != order_t::col ) { 454new_ldc = c_desc -> _rows ; 455new_c = ::dpct::cs::malloc (c_elm_size * c_desc -> _cols * new_ldc ,* q_ptr ); 456new_c_allocated = true; 457 sycl::event e = detail::type_dispatch < detail::matrix_transform_impl > ( 458c_desc -> _type ,q_ptr ,c_desc -> _rows ,c_desc -> _cols ,c_desc -> _ld , 459c_desc -> _order ,c ,new_ldc , order_t::col ,const_cast < void *> (new_c ), 460 std::vector < sycl::event > {}); 461transform_events .push_back (e ); 462 } 463 464if (d_desc -> _order != order_t::col ) { 465new_ldd = d_desc -> _rows ; 466new_d = ::dpct::cs::malloc (d_elm_size * d_desc -> _cols * new_ldd ,* q_ptr ); 467new_d_allocated = true; 468 } 469 470// start to call oneDNN matmul primitive 471// a,d are col_major, b is row_major 472const ::dnnl::memory::dim M = m ; 473const ::dnnl::memory::dim N = n ; 474const ::dnnl::memory::dim K = k ; 475 476 ::dnnl::memory::dims src_dims = {M ,K }; 477 ::dnnl::memory::dims weights_dims = {K ,N }; 478 ::dnnl::memory::dims bias_dims = {M ,N }; 479 ::dnnl::memory::dims dst_dims = {M ,N }; 480 481const ::dnnl::memory::dims src_strides = 482compute_desc -> _trans_a == oneapi::mkl::transpose::nontrans 483 ? ::dnnl::memory::dims {1 ,static_cast < long > (new_lda )} 484 : ::dnnl::memory::dims {static_cast < long > (new_lda ),1 }; 485const ::dnnl::memory::dims weights_strides = 486compute_desc -> _trans_b == oneapi::mkl::transpose::nontrans 487 ? ::dnnl::memory::dims {1 ,static_cast < long > (new_ldb )} 488 : ::dnnl::memory::dims {static_cast < long > (new_ldb ),1 }; 489const ::dnnl::memory::dims bias_strides = 490 ::dnnl::memory::dims {1 ,static_cast < long > (new_ldc )}; 491const ::dnnl::memory::dims dst_strides = 492 ::dnnl::memory::dims {1 ,static_cast < long > (new_ldd )}; 493 494auto src_md = ::dnnl::memory::desc ( 495src_dims , dpct::dnnl::memory_desc_ext::to_dnnl_data_type (a_type ), 496src_strides ); 497auto weights_md = ::dnnl::memory::desc ( 498weights_dims , dpct::dnnl::memory_desc_ext::to_dnnl_data_type (b_type ), 499weights_strides ); 500auto bias_md = ::dnnl::memory::desc ( 501bias_dims , dpct::dnnl::memory_desc_ext::to_dnnl_data_type (c_type ), 502bias_strides ); 503auto dst_md = ::dnnl::memory::desc ( 504dst_dims , dpct::dnnl::memory_desc_ext::to_dnnl_data_type (d_type ), 505dst_strides ); 506 507auto * src_mem = 508new ::dnnl::memory (src_md ,handle -> get_engine (),DNNL_MEMORY_NONE ); 509auto * weights_mem = 510new ::dnnl::memory (weights_md ,handle -> get_engine (),DNNL_MEMORY_NONE ); 511auto * bias_mem = 512new ::dnnl::memory (bias_md ,handle -> get_engine (),DNNL_MEMORY_NONE ); 513auto * dst_mem = 514new ::dnnl::memory (dst_md ,handle -> get_engine (),DNNL_MEMORY_NONE ); 515 516#ifdef DPCT_USM_LEVEL_NONE 517 detail::type_dispatch < detail::set_buffer_impl > (a_type ,src_mem ,new_a ); 518 detail::type_dispatch < detail::set_buffer_impl > (b_type ,weights_mem ,new_b ); 519if (!beta_is_zero ) 520 detail::type_dispatch < detail::set_buffer_impl > (c_type ,bias_mem ,new_c ); 521 detail::type_dispatch < detail::set_buffer_impl > (d_type ,dst_mem ,new_d ); 522#else 523src_mem -> set_data_handle (const_cast < void *> (new_a )); 524weights_mem -> set_data_handle (const_cast < void *> (new_b )); 525if (!beta_is_zero ) 526bias_mem -> set_data_handle (const_cast < void *> (new_c )); 527dst_mem -> set_data_handle (new_d ); 528#endif 529 530 std::unordered_map < int , ::dnnl::memory > matmul_args ; 531matmul_args .insert ({DNNL_ARG_SRC ,* src_mem }); 532matmul_args .insert ({DNNL_ARG_WEIGHTS ,* weights_mem }); 533matmul_args .insert ({DNNL_ARG_DST ,* dst_mem }); 534 ::dnnl::primitive_attr matmul_attr ; 535 536 ::dnnl::post_ops matmul_ops ; 537if (!beta_is_zero ) { 538matmul_ops .append_binary (::dnnl::algorithm::binary_add ,bias_md ); 539matmul_args .insert ({DNNL_ARG_ATTR_MULTIPLE_POST_OP (matmul_ops .len ()- 1 ) | 540DNNL_ARG_SRC_1 , 541* bias_mem }); 542 } 543 544 ::dnnl::memory * po_bias_mem = nullptr ; 545auto po_bias_md = 546 ::dnnl::memory::desc (::dnnl::memory::dims {M ,1 }, 547 dpct::dnnl::memory_desc_ext::to_dnnl_data_type ( 548compute_desc -> _bias_data_type ), 549 ::dnnl::memory::dims {1 ,M }); 550if (compute_desc -> _epilogue == epilogue_t::bias || 551compute_desc -> _epilogue == epilogue_t::gelu_bias || 552compute_desc -> _epilogue == epilogue_t::gelu_aux_bias ) { 553po_bias_mem = new ::dnnl::memory (po_bias_md ,handle -> get_engine (), 554DNNL_MEMORY_NONE ); 555#ifdef DPCT_USM_LEVEL_NONE 556 detail::type_dispatch < detail::set_buffer_impl > ( 557compute_desc -> _bias_data_type ,po_bias_mem , 558compute_desc -> _bias_pointer ); 559#else 560po_bias_mem -> set_data_handle (compute_desc -> _bias_pointer ); 561#endif 562 } 563 564 ::dnnl::memory * po_bias_bgradb_mem = nullptr ; 565auto po_bias_bgradb_md = ::dnnl::memory::desc ( 566compute_desc -> _trans_b == oneapi::mkl::transpose::nontrans 567 ? ::dnnl::memory::dims {N ,1 } 568 : ::dnnl::memory::dims {1 ,N }, 569 dpct::dnnl::memory_desc_ext::to_dnnl_data_type ( 570compute_desc -> _bias_data_type ), 571compute_desc -> _trans_b == oneapi::mkl::transpose::nontrans 572 ? ::dnnl::memory::dims {1 ,N } 573 : ::dnnl::memory::dims {N ,1 }); 574if (compute_desc -> _epilogue == epilogue_t::bgradb ) { 575po_bias_bgradb_mem = new ::dnnl::memory ( 576po_bias_bgradb_md ,handle -> get_engine (),DNNL_MEMORY_NONE ); 577#ifdef DPCT_USM_LEVEL_NONE 578 detail::type_dispatch < detail::set_buffer_impl > ( 579compute_desc -> _bias_data_type ,po_bias_bgradb_mem , 580compute_desc -> _bias_pointer ); 581#else 582po_bias_bgradb_mem -> set_data_handle (compute_desc -> _bias_pointer ); 583#endif 584 } 585 586 ::dnnl::memory * po_aux_mem = nullptr ; 587auto po_aux_md = ::dnnl::memory::desc ( 588 ::dnnl::memory::dims {M ,N }, 589 dpct::dnnl::memory_desc_ext::to_dnnl_data_type ( 590compute_desc -> _epilogue_aux_data_type ), 591 ::dnnl::memory::dims { 5921 ,static_cast < long > (compute_desc -> _epilogue_aux_ld )}); 593if (compute_desc -> _epilogue == epilogue_t::dgelu ) { 594po_aux_mem = 595new ::dnnl::memory (po_aux_md ,handle -> get_engine (),DNNL_MEMORY_NONE ); 596#ifdef DPCT_USM_LEVEL_NONE 597 detail::type_dispatch < detail::set_buffer_impl > ( 598compute_desc -> _epilogue_aux_data_type ,po_aux_mem , 599compute_desc -> _epilogue_aux_pointer ); 600#else 601po_aux_mem -> set_data_handle (compute_desc -> _epilogue_aux_pointer ); 602#endif 603 } 604 605switch (compute_desc -> _epilogue ) { 606case epilogue_t::relu : 607matmul_ops .append_eltwise (::dnnl::algorithm::eltwise_relu ,0.f ,0.f ); 608break ; 609case epilogue_t::bias : 610case epilogue_t::gelu_bias : { 611matmul_ops .append_binary (::dnnl::algorithm::binary_add ,po_bias_md ); 612matmul_args .insert ({DNNL_ARG_ATTR_MULTIPLE_POST_OP (matmul_ops .len ()- 1 ) | 613DNNL_ARG_SRC_1 , 614* po_bias_mem }); 615if (compute_desc -> _epilogue == epilogue_t::gelu_bias ) 616matmul_ops .append_eltwise (::dnnl::algorithm::eltwise_gelu_tanh ,0.f , 6170.f ); 618break ; 619 } 620case epilogue_t::gelu : 621matmul_ops .append_eltwise (::dnnl::algorithm::eltwise_gelu_tanh ,0.f ,0.f ); 622break ; 623default : 624break ; 625 } 626 627matmul_attr .set_post_ops (matmul_ops ); 628 629auto matmul_pd = ::dnnl::matmul::primitive_desc ( 630handle -> get_engine (),src_md ,weights_md ,dst_md ,matmul_attr ); 631auto matmul_prim = ::dnnl::matmul (matmul_pd ); 632 sycl::event matmul_prim_event = 633 ::dnnl::sycl_interop::execute (matmul_prim ,handle -> get_engine_stream (), 634matmul_args ,transform_events ); 635 636// post-op implemented by separate primitives 637 sycl::event post_op_prim_event ; 638if (compute_desc -> _epilogue == epilogue_t::gelu_aux || 639compute_desc -> _epilogue == epilogue_t::gelu_aux_bias ) { 640 sycl::event prev_event = matmul_prim_event ; 641if (compute_desc -> _epilogue == epilogue_t::gelu_aux_bias ) { 642auto po_bias_pd = ::dnnl::binary::primitive_desc ( 643handle -> get_engine (), ::dnnl::algorithm::binary_add ,dst_md , 644po_bias_md ,dst_md ); 645auto po_bias_prim = ::dnnl::binary (po_bias_pd ); 646 std::unordered_map < int , ::dnnl::memory > po_bias_args ; 647po_bias_args .insert ({DNNL_ARG_SRC_0 ,* dst_mem }); 648po_bias_args .insert ({DNNL_ARG_SRC_1 ,* po_bias_mem }); 649po_bias_args .insert ({DNNL_ARG_DST ,* dst_mem }); 650 sycl::event prev_event = ::dnnl::sycl_interop::execute ( 651po_bias_prim ,handle -> get_engine_stream (),po_bias_args , 652 {matmul_prim_event }); 653 } 654size_t size_of_element = 655 dpct::detail::library_data_size [static_cast < unsigned int > ( 656d_desc -> _type )] / 6578 ; 658 sycl::event copy_e = dpct::blas::matrix_mem_copy_async ( 659compute_desc -> _epilogue_aux_pointer ,new_d , 660compute_desc -> _epilogue_aux_ld ,d_desc -> _ld ,m ,n ,size_of_element , 661 ::dpct::cs::memcpy_direction::automatic ,* q_ptr , {prev_event }); 662 663auto gelu_pd = ::dnnl::eltwise_forward::primitive_desc ( 664handle -> get_engine (), ::dnnl::prop_kind::forward_training , 665 ::dnnl::algorithm::eltwise_gelu_tanh ,dst_md ,dst_md ); 666auto gelu_prim = ::dnnl::eltwise_forward (gelu_pd ); 667 std::unordered_map < int , ::dnnl::memory > gelu_args ; 668gelu_args .insert ({DNNL_ARG_SRC ,* dst_mem }); 669gelu_args .insert ({DNNL_ARG_DST ,* dst_mem }); 670post_op_prim_event = ::dnnl::sycl_interop::execute ( 671gelu_prim ,handle -> get_engine_stream (),gelu_args , {copy_e }); 672 }else if (compute_desc -> _epilogue == epilogue_t::dgelu ) { 673auto gelu_pd = ::dnnl::eltwise_forward::primitive_desc ( 674handle -> get_engine (), ::dnnl::prop_kind::forward_training , 675 ::dnnl::algorithm::eltwise_gelu_tanh ,po_aux_md ,po_aux_md ); 676auto dgelu_pd = ::dnnl::eltwise_backward::primitive_desc ( 677handle -> get_engine (), ::dnnl::algorithm::eltwise_gelu_tanh ,dst_md , 678dst_md ,po_aux_md ,gelu_pd ); 679auto dgelu_prim = ::dnnl::eltwise_backward (dgelu_pd ); 680 std::unordered_map < int , ::dnnl::memory > dgelu_args ; 681dgelu_args .insert ({DNNL_ARG_SRC ,* po_aux_mem }); 682dgelu_args .insert ({DNNL_ARG_DIFF_DST ,* dst_mem }); 683dgelu_args .insert ({DNNL_ARG_DIFF_SRC ,* dst_mem }); 684post_op_prim_event = 685 ::dnnl::sycl_interop::execute (dgelu_prim ,handle -> get_engine_stream (), 686dgelu_args , {matmul_prim_event }); 687 }else if (compute_desc -> _epilogue == epilogue_t::bgradb ) { 688auto reduction_pd = ::dnnl::reduction::primitive_desc ( 689handle -> get_engine (), ::dnnl::algorithm::reduction_sum ,weights_md , 690po_bias_bgradb_md ,0.f ,0.f ); 691auto reduction_prim = ::dnnl::reduction (reduction_pd ); 692 std::unordered_map < int , ::dnnl::memory > reduction_args ; 693reduction_args .insert ({DNNL_ARG_SRC ,* weights_mem }); 694reduction_args .insert ({DNNL_ARG_DST ,* po_bias_bgradb_mem }); 695post_op_prim_event = ::dnnl::sycl_interop::execute ( 696reduction_prim ,handle -> get_engine_stream (),reduction_args , 697 {matmul_prim_event }); 698 } 699 700#ifndef DPCT_USM_LEVEL_NONE 701// WA to avoid incorrect result on CPU with USM 702if (q_ptr -> get_device ().is_cpu ()) { 703matmul_prim_event .wait (); 704post_op_prim_event .wait (); 705 } 706#endif 707// end of calling oneDNN 708 709 sycl::event absmax_d_event ; 710if (auto absmax_ptr = compute_desc -> _absmax_d_pointer ) { 711absmax_d_event = detail::type_dispatch < detail::absmax_impl > ( 712d_desc -> _type ,absmax_ptr ,new_d ,new_ldd ,d_desc -> _rows , 713d_desc -> _cols ,q_ptr , 714 std::vector < sycl::event > {matmul_prim_event ,post_op_prim_event }); 715 } 716 717 sycl::event scale_d_event ; 718if (auto d_scale_ptr = compute_desc -> _d_scale_pointer ) { 719scale_d_event = detail::type_dispatch < detail::scale_d_impl > ( 720d_desc -> _type ,d_scale_ptr ,new_d ,new_ldd ,d_desc -> _rows , 721d_desc -> _cols ,q_ptr ,compute_desc -> _scale_type , 722 std::vector < sycl::event > {matmul_prim_event ,absmax_d_event , 723post_op_prim_event }); 724 } 725 726 sycl::event transform_d_event ; 727if (d_desc -> _order != order_t::col ) { 728transform_d_event = detail::type_dispatch < detail::matrix_transform_impl > ( 729d_desc -> _type ,q_ptr ,d_desc -> _rows ,d_desc -> _cols ,new_ldd , 730 order_t::col ,new_d ,d_desc -> _ld ,d_desc -> _order ,d , 731 std::vector < sycl::event > {matmul_prim_event ,absmax_d_event , 732post_op_prim_event }); 733 } 734 735 sycl::event free_event = q_ptr -> submit ([& ](sycl::handler & cgh ) { 736cgh .depends_on ({transform_d_event ,matmul_prim_event ,absmax_d_event , 737post_op_prim_event }); 738cgh .host_task ([= ] { 739delete src_mem ; 740delete weights_mem ; 741delete bias_mem ; 742delete dst_mem ; 743if (po_bias_mem ) 744delete po_bias_mem ; 745if (po_bias_bgradb_mem ) 746delete po_bias_bgradb_mem ; 747if (po_aux_mem ) 748delete po_aux_mem ; 749 ::dpct::cs::free ((void * )new_a ,* q_ptr ); 750if (new_b_allocated ) 751 ::dpct::cs::free ((void * )new_b ,* q_ptr ); 752if (new_c_allocated ) 753 ::dpct::cs::free ((void * )new_c ,* q_ptr ); 754if (new_d_allocated ) 755 ::dpct::cs::free ((void * )new_d ,* q_ptr ); 756 }); 757 }); 758return free_event ; 759 }; 760 761 std::vector < sycl::event > events ; 762const std::byte * offsetted_a = static_cast < const std::byte *> (a ); 763const std::byte * offsetted_b = static_cast < const std::byte *> (b ); 764const std::byte * offsetted_c = static_cast < const std::byte *> (c ); 765 std::byte * offsetted_d = static_cast < std::byte *> (d ); 766for (std::uint64_t i = 0 ;i < a_desc -> _batch_count ;i ++ ) { 767 sycl::event e = matmul_single ( 768handle ,compute_desc ,alpha ,offsetted_a ,a_desc ,offsetted_b ,b_desc , 769beta ,offsetted_c ,c_desc ,offsetted_d ,d_desc ,q_ptr ,m ,n ,k ,a_type , 770b_type ,c_type ,d_type ,scale_type ,vector_alpha ,device_alpha , 771beta_is_zero ,a_elm_size ,b_elm_size ,c_elm_size ,d_elm_size ); 772events .push_back (e ); 773 774offsetted_a += a_elm_size * a_desc -> _strided_batch_offset ; 775offsetted_b += b_elm_size * b_desc -> _strided_batch_offset ; 776offsetted_c += c_elm_size * c_desc -> _strided_batch_offset ; 777offsetted_d += d_elm_size * d_desc -> _strided_batch_offset ; 778 } 779 780return q_ptr -> single_task (events , [] {}); 781} 782 783class transform_desc_t { 784public : 785enum class attribute {scale_type ,pointer_mode ,trans_a ,trans_b }; 786 787transform_desc_t (library_data_t scale_type ) :_scale_type (scale_type ) {} 788void set_attribute (attribute attr ,const void * mem ) { 789get_set_attr < true> (attr ,const_cast < void *> (mem )); 790 } 791void get_attribute (attribute attr ,void * mem ) { 792get_set_attr < false> (attr ,mem ); 793 } 794 795private : 796template < bool is_set > void get_set_attr (attribute attr ,void * mem ) { 797#define CASE (tag ) \ 798 case attribute::tag: \ 799 if constexpr (is_set) { \ 800 _##tag = *static_cast<decltype(_##tag) *>(mem); \ 801 } else { \ 802 *static_cast<decltype(_##tag) *>(mem) = _##tag; \ 803 } \ 804 break; 805switch (attr ) { 806CASE (scale_type ) 807CASE (pointer_mode ) 808CASE (trans_a ) 809CASE (trans_b ) 810 } 811#undef CASE 812 } 813 814library_data_t _scale_type ; 815pointer_mode_t _pointer_mode = pointer_mode_t::host ; 816 oneapi::mkl::transpose _trans_a = oneapi::mkl::transpose::nontrans ; 817 oneapi::mkl::transpose _trans_b = oneapi::mkl::transpose::nontrans ; 818 819friend sycl::event 820matrix_transform (transform_desc_ptr transform_desc ,const void * alpha , 821const void * a ,matrix_layout_ptr a_desc ,const void * beta , 822const void * b ,matrix_layout_ptr b_desc ,void * c , 823matrix_layout_ptr c_desc , ::dpct::cs::queue_ptr q_ptr ); 824}; 825 826/// This function does operation: 827/// C = alpha*transformation(A) + beta*transformation(B). 828/// The "transformation" includes matrix transpose and layout conversion. 829/// Currently it only supports alpha==1 && beta==0. 830/// \param [in] transform_desc Describe the transformation. 831/// \param [in] alpha Scaling factor alpha. 832/// \param [in] a Input matrix A. 833/// \param [in] a_desc Describe the matrix A. 834/// \param [in] beta Scaling factor beta. 835/// \param [in] b Input matrix B. 836/// \param [in] b_desc Describe the matrix B. 837/// \param [out] c Output matrix C. 838/// \param [in] c_desc Describe the matrix C. 839/// \param [in] q_ptr The queue where the routine should be executed. 840inline sycl::event matrix_transform (transform_desc_ptr transform_desc , 841const void * alpha ,const void * a , 842matrix_layout_ptr a_desc ,const void * beta , 843const void * b ,matrix_layout_ptr b_desc , 844void * c ,matrix_layout_ptr c_desc , 845 ::dpct::cs::queue_ptr q_ptr ) { 846if (!q_ptr ) 847q_ptr = & ::dpct::cs::get_default_queue (); 848 849if (transform_desc -> _pointer_mode != pointer_mode_t::host ) { 850throw std::runtime_error ( 851"dpct::blas_gemm::experimental::matrix_transform() " 852"only supports pointer_mode_t::host as pointer_mode currently." ); 853 } 854if (transform_desc -> _scale_type != library_data_t::real_float ) { 855throw std::runtime_error ( 856"dpct::blas_gemm::experimental::matrix_transform() " 857"only supports library_data_t::real_float as scale_type currently." ); 858 } 859 860if (alpha != nullptr ) { 861if (1.0f != * reinterpret_cast < const float *> (alpha )) 862throw std::runtime_error ( 863"dpct::blas_gemm::experimental::matrix_transform() does not " 864"support non-one alpha currently." ); 865 } 866 867if (beta != nullptr ) { 868if (0.0f != * reinterpret_cast < const float *> (beta )) 869throw std::runtime_error ( 870"dpct::blas_gemm::experimental::matrix_transform() does not " 871"support non-zero beta currently." ); 872 } 873 874if (b != nullptr ) { 875throw std::runtime_error ( 876"dpct::blas_gemm::experimental::matrix_transform() does not " 877"support matrix B currently." ); 878 } 879 880if ((a_desc -> _type != library_data_t::real_int8 || 881c_desc -> _type != library_data_t::real_int8 )&& 882 (a_desc -> _type != library_data_t::real_int32 || 883c_desc -> _type != library_data_t::real_int32 )) { 884throw std::runtime_error ( 885"dpct::blas_gemm::experimental::matrix_transform() only supports " 886"combinations of data types: a_type==real_int8&&c_type==real_int8, " 887"a_type==real_int32&&c_type==real_int32." ); 888 } 889 890return detail::type_dispatch < detail::matrix_transform_impl > ( 891a_desc -> _type ,q_ptr ,a_desc -> _rows ,a_desc -> _cols ,a_desc -> _ld , 892a_desc -> _order ,a ,c_desc -> _ld ,c_desc -> _order ,c , 893 std::vector < sycl::event > {}); 894} 895}// namespace dpct::blas_gemm::experimental 896#endif // __DPCT_BLAS_GEMM_UTILS_HPP__