okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- dnnl_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 9#ifndef __DPCT_DNNL_UTILS_HPP__ 10#define __DPCT_DNNL_UTILS_HPP__ 11 12#include "compat_service.hpp" 13#include "lib_common_utils.hpp" 14 15#include <oneapi/dnnl/dnnl.hpp> 16#include <oneapi/dnnl/dnnl_sycl.hpp> 17 18#if defined(__has_include )&& __has_include (< oneapi /math /rng /device .hpp > ) 19#include <oneapi/math/rng/device.hpp> 20#elif defined(__has_include )&& __has_include (< oneapi /mkl /rng /device .hpp > ) 21#include <oneapi/mkl/rng/device.hpp> 22#elif defined(__has_include ) 23#error "SYCLomatic runtime requires oneMath/oneMKL support" 24#else 25#error "SYCLomatic runtime requires __has_include support" 26#endif 27 28#include <algorithm> 29#include <list> 30#include <unordered_map> 31 32#include "detail/dnnl_utils_detail.hpp" 33 34namespace dpct { 35namespace dnnl { 36/// Get concatenated library version as an integer. 37static inline size_t get_version () { 38const ::dnnl::version_t * ver = ::dnnl::version (); 39return ver -> major * 1000 + ver -> minor * 100 + ver -> patch ; 40} 41class engine_ext ; 42typedef oneapi::mkl::rng::philox4x32x10 rng_engine_t ; 43/// An enum class representing memory layout. Used by 44/// memory_desc_ext to create a memory with pre-defined layout. 45enum class memory_format_tag {nchw ,nhwc ,nchw_blocked }; 46 47/// An enum class representing RNN data memory layout. Used by 48/// memory_desc_ext to create a memory with pre-defined layout. 49enum class rnn_memory_format_tag {tnc ,ntc }; 50 51/// A class holding the description of an N-dimensions memory. 52class memory_desc_ext { 53 ::dnnl::memory::desc _desc ; 54public : 55/// Convert dpct::library_data_t to dnnl::memory::data_type. 56static ::dnnl::memory::data_type to_dnnl_data_type (dpct::library_data_t dt ); 57/// Convert dnnl::memory::data_type to dpct::library_data_t. 58static dpct::library_data_t 59to_dpct_library_data_t (::dnnl::memory::data_type dt ,unsigned block_size ); 60/// Convert dpct::dnnl::memory_format_tag to dnnl::memory::format_tag. 61static ::dnnl::memory::format_tag to_dnnl_format_tag (dpct::library_data_t dt , 62memory_format_tag tag ); 63memory_desc_ext ()= default ; 64memory_desc_ext (::dnnl::memory::desc & desc ) :_desc (desc ) {} 65memory_desc_ext (::dnnl::memory::desc && desc ) :_desc (std::move (desc )) {} 66/// Setting a 4D memory with given parameters. 67/// \param [in] tag Format tag. 68/// \param [in] dt Data type. 69/// \param [in] n Number of images. 70/// \param [in] c Number of channels. 71/// \param [in] h Height of images. 72/// \param [in] w Width of images. 73void set (memory_format_tag tag , dpct::library_data_t dt ,int n ,int c ,int h , 74int w ); 75/// Setting a 3D RNN data memory with given parameters. 76/// \param [in] tag RNN data format tag. 77/// \param [in] dt Data type. 78/// \param [in] t Number of sequence length. 79/// \param [in] n Number of batch. 80/// \param [in] c Height of input channel. 81void set (rnn_memory_format_tag tag , dpct::library_data_t dt ,int t ,int n ,int c ); 82/// Setting a 4D memory with given parameters. 83/// \param [in] dt Data type. 84/// \param [in] n Number of images. 85/// \param [in] c Number of channels. 86/// \param [in] h Height of images. 87/// \param [in] w Width of images. 88/// \param [in] n_stride Stride between two continuous images. 89/// \param [in] c_stride Stride between two continuous channels. 90/// \param [in] h_stride Stride between two continuous rows. 91/// \param [in] w_stride Stride between two continuous columns. 92void set (dpct::library_data_t dt ,int n ,int c ,int h ,int w ,int n_stride , 93int c_stride ,int h_stride ,int w_stride ); 94/// Setting a ND memory with given parameters. 95/// \param [in] dt Data type. 96/// \param [in] ndims Dimension of the memory. 97/// \param [in] dims Array of dimension ndims that contain the size of each 98/// memory dimension. \param [in] strides Array of dimension ndims that 99/// contain the stride of each memory dimension. 100void set (dpct::library_data_t dt ,int ndims ,const int dims [], 101const int strides []); 102/// Setting a ND memory with given parameters. 103/// \param [in] tag Format tag. 104/// \param [in] dt Data type. 105/// \param [in] ndims Dimension of the memory. 106/// \param [in] dims Array of dimension ndims that contain the size of each 107/// memory dimension. 108void set (memory_format_tag tag , dpct::library_data_t dt ,int ndims , 109const int dims []); 110/// Getting a ::dnnl::memory::desc from a memory_desc_ext. 111/// \returns The ::dnnl::memory::desc. 112const ::dnnl::memory::desc & get_desc ()const {return _desc ; } 113/// Setting holding desc with given dnnl memory descriptor. 114void set_desc (::dnnl::memory::desc desc ) {_desc = desc ; } 115/// Getting a size of a memory_desc_ext in bytes. 116/// \returns The size. 117size_t get_size ()const {return _desc .get_size (); } 118/// Getting parameters from a 4D memory. 119/// \param [out] dt Data type. 120/// \param [out] n Number of images. 121/// \param [out] c Number of channels. 122/// \param [out] h Height of images. 123/// \param [out] w Width of images. 124/// \param [out] n_stride Stride between two continuous images. 125/// \param [out] c_stride Stride between two continuous channels. 126/// \param [out] h_stride Stride between two continuous rows. 127/// \param [out] w_stride Stride between two continuous columns. 128void get (dpct::library_data_t * dt ,int * n ,int * c ,int * h ,int * w , 129int * n_stride ,int * c_stride ,int * h_stride ,int * w_stride )const ; 130/// Getting parameters from a 4D memory. 131/// \param [out] dt Data type. 132/// \param [out] tag Format tag. 133/// \param [out] n Number of images. 134/// \param [out] c Number of channels. 135/// \param [out] h Height of images. 136/// \param [out] w Width of images. 137void get (dpct::library_data_t * dt ,memory_format_tag * tag ,int * n ,int * c , 138int * h ,int * w )const ; 139/// Getting parameters from a 3D RNN data memory. 140/// \param [out] dt Data type. 141/// \param [out] tag RNN data format tag. 142/// \param [out] t Number of sequence length. 143/// \param [out] n Number of batch. 144/// \param [out] c Height of input channel. 145void get (dpct::library_data_t * dt ,rnn_memory_format_tag * tag ,int * t ,int * n , 146int * c )const ; 147/// Getting parameters from a ND memory. 148/// \param [in] requested_ndims Requested number of dimensions to get from a 149/// given memory descriptor. 150/// \param [out] dt Data type. 151/// \param [out] ndims Dimension of the memory. 152/// \param [out] dims Array of dimension requested_ndims that contain the 153/// size of each memory dimension. 154/// \param [out] strides Array of dimension requested_ndims that contain the 155/// stride of each memory dimension. 156void get (int requested_ndims , dpct::library_data_t * dt ,int * ndims , 157int dims [],int strides [])const ; 158/// Getting parameters from a ND memory. 159/// \param [in] requested_ndims Requested number of dimensions to get from a 160/// given memory descriptor. 161/// \param [out] dt Data type. 162/// \param [out] tag Format tag. 163/// \param [out] ndims Dimension of the memory. 164/// \param [out] dims Array of dimension requested_ndims that contain the 165/// size of each memory dimension. 166void get (int requested_ndims , dpct::library_data_t * dt , 167memory_format_tag * tag ,int * ndims ,int dims [])const ; 168/// Getting dims from a ND memory. 169/// \return The dims. 170 std::vector < int64_t > get_dims ()const {return _desc .get_dims (); } 171/// Getting strides from a ND memory. 172/// \return The strides. 173 std::vector < int64_t > get_strides ()const { 174return _desc .get_strides (); 175 } 176/// Getting element num from a ND memory. 177/// \return The element number. 178size_t get_element_num ()const { 179auto dims = _desc .get_dims (); 180if (dims .empty ()) { 181return 0 ; 182 } 183size_t result = 1 ; 184for (auto & dim :dims ) { 185result *=dim ; 186 } 187return result ; 188 } 189 190 operatorbool ()const { 191return bool (_desc ); 192 } 193 194memory_desc_ext & operator= (std::nullptr_t ) { 195_desc .reset (nullptr ); 196return * this ; 197 } 198}; 199 200/// A class holding description for an activation operation. 201class activation_desc { 202 ::dnnl::algorithm _alg ; 203float _alpha ; 204float _beta ; 205 206public : 207/// Setting an activation descriptor with given parameters. 208/// \param [in] alg Activation algorithm. 209/// \param [in] alpha Value of alpha parameter. 210void set (::dnnl::algorithm alg ,float alpha ) { 211_alg = alg ; 212if (alg == ::dnnl::algorithm::eltwise_clip ) { 213_alpha = 0 ; 214_beta = alpha ; 215 }else { 216_alpha = alpha ; 217 } 218 } 219/// Getting parameters form an activation descriptor. 220/// \param [out] alg Activation algorithm. 221/// \param [out] alpha Value of alpha parameter. 222void get (::dnnl::algorithm * alg ,float * alpha )const { 223* alg = _alg ; 224if (_alg == ::dnnl::algorithm::eltwise_clip ) { 225* alpha = _beta ; 226 }else { 227* alpha = _alpha ; 228 } 229 } 230/// Setting the alpha parameter of an activation descriptor. 231/// \param [in] alpha Value of alpha parameter. 232void set_alpha (float alpha ) {_alpha = alpha ; } 233/// Setting the beta parameter of an activation descriptor. 234/// \param [in] beta Value of beta parameter. 235void set_beta (float beta ) {_beta = beta ; } 236/// Setting the algorithm parameter of an activation descriptor. 237/// \param [in] alg Activation algorithm. 238void set_algorithm (::dnnl::algorithm alg ) {_alg = alg ; } 239/// Getting the alpha parameter from an activation descriptor. 240/// \param [out] alpha Value of alpha parameter. 241float get_alpha ()const {return _alpha ; } 242/// Getting the beta parameter from an activation descriptor. 243/// \param [out] beta Value of beta parameter. 244float get_beta ()const {return _beta ; } 245/// Getting the algorithm parameter from an activation descriptor. 246/// \param [out] alg Activation algorithm. 247 ::dnnl::algorithm get_algorithm ()const {return _alg ; } 248}; 249 250/// A class holding description for a local response normalization operation. 251class lrn_desc { 252unsigned int _local_size ; 253float _alpha ; 254float _beta ; 255float _k ; 256 257public : 258/// Setting a local response normalization descriptor with given parameters. 259/// \param [in] local_size Value of local_size parameter. 260/// \param [in] alpha Value of alpha parameter. 261/// \param [in] beta Value of beta parameter. 262/// \param [in] k Value of k parameter. 263void set (unsigned int local_size ,float alpha ,float beta ,float k ) { 264_local_size = local_size ; 265_alpha = alpha ; 266_beta = beta ; 267_k = k ; 268 } 269/// Getting parameters form a local response normalization descriptor. 270/// \param [out] local_size Value of local_size parameter. 271/// \param [out] alpha Value of alpha parameter. 272/// \param [out] beta Value of beta parameter. 273/// \param [out] k Value of k parameter. 274void get (unsigned int * local_size ,float * alpha ,float * beta , 275float * k )const { 276* local_size = _local_size ; 277* alpha = _alpha ; 278* beta = _beta ; 279* k = _k ; 280 } 281/// Setting the local size parameter of a local response normalization 282/// descriptor. 283/// \param [in] local_size Value of local_size parameter. 284void set_local_size (unsigned int local_size ) {_local_size = local_size ; } 285/// Setting the alpha parameter of a local response normalization descriptor. 286/// \param [in] alpha Value of alpha parameter. 287void set_alpha (float alpha ) {_alpha = alpha ; } 288/// Setting the beta parameter of a local response normalization descriptor. 289/// \param [in] beta Value of beta parameter. 290void set_beta (float beta ) {_beta = beta ; } 291/// Setting the k parameter of a local response normalization descriptor. 292/// \param [in] k Value of k parameter. 293void set_k (float k ) {_k = k ; } 294/// Getting the local size parameter from a local response normalization 295/// descriptor. 296/// \param [out] local_size Value of local_size parameter. 297unsigned int get_local_size ()const {return _local_size ; } 298/// Getting the alpha parameter from a local response normalization 299/// descriptor. 300/// \param [out] alpha Value of alpha parameter. 301float get_alpha ()const {return _alpha ; } 302/// Getting the beta parameter from a local response normalization descriptor. 303/// \param [out] beta Value of beta parameter. 304float get_beta ()const {return _beta ; } 305/// Getting the k parameter from a local response normalization descriptor. 306/// \param [out] k Value of k parameter. 307float get_k ()const {return _k ; } 308}; 309 310/// An enum class representing softmax algorithm. 311enum class softmax_algorithm {normal ,log }; 312/// An enum class representing softmax mode. 313enum class softmax_mode {instance ,channel }; 314 315/// A class holding description for a pooling operation. 316class pooling_desc { 317 ::dnnl::algorithm _alg ; 318 std::vector < int64_t > _stride ; 319 std::vector < int64_t > _kernel ; 320 std::vector < int64_t > _padding ; 321 322public : 323/// Setting a 2D pooling descriptor with given parameters. 324/// \param [in] alg Pooling algorithm. 325/// \param [in] kernel_h Value of height of kernel. 326/// \param [in] kernel_w Value of width of kernel. 327/// \param [in] padding_h Value of height of padding. 328/// \param [in] padding_w Value of width of padding. 329/// \param [in] stride_h Value of height of stride. 330/// \param [in] stride_w Value of width of stride. 331void set (::dnnl::algorithm alg ,int kernel_h ,int kernel_w ,int padding_h , 332int padding_w ,int stride_h ,int stride_w ) { 333_alg = alg ; 334_stride = {stride_h ,stride_w }; 335_kernel = {kernel_h ,kernel_w }; 336_padding = {padding_h ,padding_w }; 337 } 338/// Setting a ND pooling descriptor with given parameters. 339/// \param [in] alg Pooling algorithm. 340/// \param [in] ndims Dimension of the pooling operation. 341/// \param [in] kernel Array of dimension ndims containing the kernel size of 342/// each dimension. 343/// \param [in] padding Array of dimension ndims containing the padding size of 344/// each dimension. 345/// \param [in] stride Array of dimension ndims containing the stride size of 346/// each dimension. 347void set (::dnnl::algorithm alg ,int ndims ,int kernel [],int padding [], 348int stride []) { 349_alg = alg ; 350_stride = std::vector < int64_t > (stride ,stride + ndims ); 351_kernel = std::vector < int64_t > (kernel ,kernel + ndims ); 352_padding = std::vector < int64_t > (padding ,padding + ndims ); 353 } 354/// Getting parameters from a 2D pooling descriptor. 355/// \param [out] alg Pooling algorithm. 356/// \param [out] kernel_h Value of height of kernel. 357/// \param [out] kernel_w Value of width of kernel. 358/// \param [out] padding_h Value of height of padding. 359/// \param [out] padding_w Value of width of padding. 360/// \param [out] stride_h Value of height of stride. 361/// \param [out] stride_w Value of width of stride. 362void get (::dnnl::algorithm * alg ,int * kernel_h ,int * kernel_w ,int * padding_h , 363int * padding_w ,int * stride_h ,int * stride_w )const { 364* alg = _alg ; 365* kernel_h = _kernel [0 ]; 366* kernel_w = _kernel [1 ]; 367* padding_h = _padding [0 ]; 368* padding_w = _padding [1 ]; 369* stride_h = _stride [0 ]; 370* stride_w = _stride [1 ]; 371 } 372/// Getting parameters from a ND pooling descriptor. 373/// \param [in] requested_ndims Requested number of dimensions to get from a 374/// given pooling descriptor. 375/// \param [out] alg Pooling algorithm. 376/// \param [out] ndims Dimension of the pooling operation. 377/// \param [out] kernel Array of dimension ndims containing the kernel size of 378/// each dimension. 379/// \param [out] padding Array of dimension ndims containing the padding size 380/// of each dimension. 381/// \param [out] stride Array of dimension ndims containing the stride size of 382/// each dimension. 383void get (int requested_ndims , ::dnnl::algorithm * alg ,int * ndims , 384int kernel [],int padding [],int stride [])const { 385* alg = _alg ; 386* ndims = _stride .size (); 387for (int i = 0 ;i < requested_ndims ;i ++ ) { 388kernel [i ]= _kernel [i ]; 389padding [i ]= _padding [i ]; 390stride [i ]= _stride [i ]; 391 } 392 } 393/// Setting the algorithm parameter of a pooling descriptor. 394/// \param [in] alg Pooling algorithm. 395void set_algorithm (::dnnl::algorithm alg ) {_alg = alg ; } 396/// Setting the stride parameter of a pooling descriptor. 397/// \param [in] stride Array of dimension ndims containing the stride size of 398/// each dimension. 399void set_stride (const std::vector < int64_t > & stride ) {_stride = stride ; } 400/// Setting the kernel parameter of a pooling descriptor. 401/// \param [in] kernel Array of dimension ndims containing the kernel size of 402/// each dimension. 403void set_kernel (const std::vector < int64_t > & kernel ) {_kernel = kernel ; } 404/// Setting the padding parameter of a pooling descriptor. 405/// \param [in] padding Array of dimension ndims containing the padding size 406/// of each dimension. 407void set_padding (const std::vector < int64_t > & padding ) {_padding = padding ; } 408 409/// Getting the algorithm parameter from a pooling descriptor. 410/// \param [out] alg Pooling algorithm. 411 ::dnnl::algorithm get_algorithm ()const {return _alg ; } 412/// Getting the stride parameter from a pooling descriptor. 413/// \returns Array of dimension ndims containing the stride size of each 414/// dimension. 415const std::vector < int64_t > & get_stride ()const {return _stride ; } 416/// Getting the kernel parameter from a pooling descriptor. 417/// \returns Array of dimension ndims containing the kernel size of each 418/// dimension. 419const std::vector < int64_t > & get_kernel ()const {return _kernel ; } 420/// Getting the padding parameter from a pooling descriptor. 421/// \returns Array of dimension ndims containing the padding size of each 422/// dimension. 423const std::vector < int64_t > & get_padding ()const {return _padding ; } 424/// Getting the output dimensions of a memory after 2D pooling has been 425/// applied. 426/// \param [in] desc Input memory descriptor. 427/// \param [out] out_n Number of images. 428/// \param [out] out_c Number of channels. 429/// \param [out] out_h Height of images. 430/// \param [out] out_w Width of images. 431void get_forward_output_dim (const memory_desc_ext & desc ,int * out_n , 432int * out_c ,int * out_h ,int * out_w )const { 433auto dims = desc .get_dims (); 434* out_n = dims [0 ]; 435* out_c = dims [1 ]; 436* out_h = 1 + (dims [2 ]+ 2 * _padding [0 ]- _kernel [0 ]) /_stride [0 ]; 437* out_w = 1 + (dims [3 ]+ 2 * _padding [1 ]- _kernel [1 ]) /_stride [1 ]; 438 } 439/// Getting the output dimensions of a memory after ND pooling has been 440/// applied. 441/// \param [in] desc Input memory descriptor. 442/// \param [out] ndims Dimension of the memory. 443/// \param [out] out_dims Array of dimension requested_ndims that contain 444/// the size of each memory dimension. 445void get_forward_output_dim (const memory_desc_ext & desc ,int ndims , 446int out_dims [])const { 447assert (ndims >=4 && "ndims is at least 4." ); 448auto dims = desc .get_dims (); 449out_dims [0 ]= dims [0 ]; 450out_dims [1 ]= dims [1 ]; 451for (int i = 2 ;i < ndims ;i ++ ) { 452out_dims [i ]= 4531 + (dims [i ]+ 2 * _padding [i - 2 ]- _kernel [i - 2 ]) /_stride [i - 2 ]; 454 } 455 } 456}; 457 458/// An enum class representing reduction operations. 459enum class reduction_op { 460max , 461min , 462sum , 463mul , 464mean , 465amax , 466mul_no_zeros , 467norm1 , 468norm2 469}; 470 471/// An enum class representing batch normalization mode. 472enum class batch_normalization_mode {per_activation ,spatial }; 473 474/// An enum class representing batch normalization operations. 475enum class batch_normalization_ops {none ,activation ,add_activation }; 476 477/// An enum class representing binary operations. 478enum class binary_op {add ,sub ,mul ,div ,min ,max ,sqrt ,neg }; 479 480/// An struct representing convolution algorithm infomation. 481struct convolution_algorithm_info { 482 ::dnnl::algorithm algo = ::dnnl::algorithm::convolution_auto ; 483int status = 0 ; 484}; 485 486/// A class holding description for a convolution operation. 487class convolution_desc { 488 std::vector < int64_t > _strides ; 489 std::vector < int64_t > _dilates ; 490 std::vector < int64_t > _paddings ; 491int _group_count = 1 ; 492 ::dnnl::fpmath_mode _math_mode = ::dnnl::fpmath_mode::strict ; 493public : 494/// Setting a group count to be used in the convolution. 495/// \param [in] group_count Value of group count. 496void set_group_count (int group_count ) {_group_count = group_count ; } 497/// Getting a group count specified in the given convolution descriptor. 498/// \returns Value of group count. 499int get_group_count () {return _group_count ; } 500/// Setting floating point math mode to be used in the convolution. 501/// \param [in] math_mode Value of math_mode. 502void set_math_mode (::dnnl::fpmath_mode math_mode ) {_math_mode = math_mode ; } 503/// Getting floating point math mode specified in the given convolution descriptor. 504/// \returns Value of math mode. 505 ::dnnl::fpmath_mode get_math_mode () {return _math_mode ; } 506/// Setting a 2D convolution descriptor with given parameters. 507/// \param [in] padding_h Value of height of padding. 508/// \param [in] padding_w Value of width of padding. 509/// \param [in] stride_h Value of height of stride. 510/// \param [in] stride_w Value of width of stride. 511/// \param [in] dilate_h Value of height of dilate. 512/// \param [in] dilate_w Value of width of dilate. 513void set (int padding_h ,int padding_w ,int stride_h ,int stride_w , 514int dilate_h ,int dilate_w ) { 515_strides = {stride_h ,stride_w }; 516_dilates = {dilate_h - 1 ,dilate_w - 1 }; 517_paddings = {padding_h ,padding_w }; 518 } 519/// Setting a ND convolution descriptor with given parameters. 520/// \param [in] ndims Dimension of the convolution operation. 521/// \param [in] paddings Array of dimension ndims containing the padding size of 522/// each dimension. 523/// \param [in] strides Array of dimension ndims containing the stride size of 524/// each dimension. 525/// \param [in] dilates Array of dimension ndims containing the kernel size of 526/// each dimension. 527void set (int ndims ,int paddings [],int strides [],int dilates []) { 528_strides = std::vector < int64_t > (strides ,strides + ndims ); 529_paddings = std::vector < int64_t > (paddings ,paddings + ndims ); 530_dilates = std::vector < int64_t > (dilates ,dilates + ndims ); 531for (auto & dilate :_dilates ) { 532dilate -- ; 533 } 534 } 535/// Getting parameters from a 2D convolution descriptor. 536/// \param [out] padding_h Value of height of padding. 537/// \param [out] padding_w Value of width of padding. 538/// \param [out] stride_h Value of height of stride. 539/// \param [out] stride_w Value of width of stride. 540/// \param [out] dilate_h Value of height of dilate. 541/// \param [out] dilate_w Value of width of dilate. 542void get (int * padding_h ,int * padding_w ,int * stride_h ,int * stride_w , 543int * dilate_h ,int * dilate_w )const { 544* dilate_h = _dilates [0 ]; 545* dilate_w = _dilates [1 ]; 546* padding_h = _paddings [0 ]; 547* padding_w = _paddings [1 ]; 548* stride_h = _strides [0 ]; 549* stride_w = _strides [1 ]; 550 } 551/// Getting parameters from a ND convolution descriptor. 552/// \param [in] requested_ndims Requested number of dimensions to get from a 553/// given convolution descriptor. 554/// \param [out] ndims Dimension of the pooling operation. 555/// \param [out] paddings Array of dimension ndims containing the padding size 556/// of each dimension. 557/// \param [out] strides Array of dimension ndims containing the stride size of 558/// each dimension. 559/// \param [out] dilates Array of dimension ndims containing the dilate size of 560/// each dimension. 561void get (int requested_ndims ,int * ndims ,int paddings [],int strides [], 562int dilates [])const { 563* ndims = _strides .size (); 564for (int i = 0 ;i < requested_ndims ;i ++ ) { 565dilates [i ]= _dilates [i ]; 566paddings [i ]= _paddings [i ]; 567strides [i ]= _strides [i ]; 568 } 569 } 570/// Getting the stride parameter from a convolution descriptor. 571/// \returns Array of dimension ndims containing the stride size of each 572/// dimension. 573const std::vector < int64_t > & get_stride ()const {return _strides ; } 574/// Getting the kernel parameter from a convolution descriptor. 575/// \returns Array of dimension ndims containing the dilate size of each 576/// dimension. 577const std::vector < int64_t > & get_dilate ()const {return _dilates ; } 578/// Getting the padding parameter from a convolution descriptor. 579/// \returns Array of dimension ndims containing the padding size of each 580/// dimension. 581const std::vector < int64_t > & get_padding ()const {return _paddings ; } 582/// Getting the output dimensions of a memory after 2D convolution has been 583/// applied. 584/// \param [in] desc Input memory descriptor. 585/// \param [in] weight_desc Input weight memory descriptor. 586/// \param [out] out_n Number of images. 587/// \param [out] out_c Number of channels. 588/// \param [out] out_h Height of images. 589/// \param [out] out_w Width of images. 590void get_forward_output_dim (const memory_desc_ext & desc , 591const memory_desc_ext & weight_desc ,int * out_n , 592int * out_c ,int * out_h ,int * out_w )const { 593auto dims = desc .get_dims (); 594auto weight_dims = weight_desc .get_dims (); 595* out_n = dims [0 ]; 596* out_c = weight_dims [0 ]; 597* out_h = 1 + (dims [2 ]+ 2 * _paddings [0 ]- 598 (1 + ((_dilates [0 ]+ 1 )* (weight_dims [2 ]- 1 )))) / 599_strides [0 ]; 600* out_w = 1 + (dims [3 ]+ 2 * _paddings [1 ]- 601 (1 + ((_dilates [1 ]+ 1 )* (weight_dims [3 ]- 1 )))) / 602_strides [1 ]; 603 } 604/// Getting the output dimensions of a memory after ND convolution has been 605/// applied. 606/// \param [in] desc Input memory descriptor. 607/// \param [in] weight_desc Input weight memory descriptor. 608/// \param [out] ndims Dimension of the memory. 609/// \param [out] out_dims Array of dimension requested_ndims that contain 610/// the size of each memory dimension. 611void get_forward_output_dim (const memory_desc_ext & desc , 612const memory_desc_ext & weight_desc ,int ndims , 613int out_dims [])const { 614assert (ndims >=4 && "ndims is at least 4." ); 615auto dims = desc .get_dims (); 616auto weight_dims = weight_desc .get_dims (); 617out_dims [0 ]= dims [0 ]; 618out_dims [1 ]= weight_dims [1 ]; 619for (int i = 2 ;i < ndims ;i ++ ) { 620out_dims [i ]= 1 + (dims [i ]+ 2 * _paddings [i - 2 ]- 621 (1 + ((_dilates [i - 2 ]+ 1 )* (weight_dims [i ]- 1 )))) / 622_strides [i - 2 ]; 623 } 624 } 625 626convolution_desc & operator= (std::nullptr_t ) { 627return * this = convolution_desc (); 628 } 629 630 operatorbool ()const { 631return !(_strides .size ()== 0 632&& _dilates .size ()== 0 633&& _paddings .size ()== 0 ); 634 } 635}; 636 637/// An enum class representing rnn mode. 638enum class rnn_mode {vanilla_relu ,vanilla_tanh ,lstm ,gru }; 639 640/// An enum class representing rnn bias mode. 641enum class rnn_bias_mode {none ,single }; 642 643/// An enum class representing rnn direction. 644enum class rnn_direction {unidirectional ,bidirectional }; 645 646/// A class holding description for a RNN operation. 647class rnn_desc { 648rnn_mode _mode ; 649rnn_bias_mode _bias_mode ; 650rnn_direction _direction ; 651 dpct::library_data_t _dt ; 652int _input_size ; 653int _hidden_size ; 654int _projection_size ; 655int _layer_size ; 656 657public : 658void set (rnn_mode mode ,rnn_bias_mode bias_mode ,rnn_direction direction , 659 dpct::library_data_t dt ,int input_size ,int hidden_size , 660int projection_size ,int layer_size ) { 661_mode = mode ; 662_bias_mode = bias_mode ; 663_direction = direction ; 664_input_size = input_size ; 665_hidden_size = hidden_size ; 666_projection_size = projection_size ; 667_layer_size = layer_size ; 668_dt = dt ; 669 } 670void get (rnn_mode * mode ,rnn_bias_mode * bias_mode ,rnn_direction * direction , 671 dpct::library_data_t * dt ,int * input_size ,int * hidden_size , 672int * projection_size ,int * layer_size )const { 673* mode = _mode ; 674* bias_mode = _bias_mode ; 675* direction = _direction ; 676* input_size = _input_size ; 677* hidden_size = _hidden_size ; 678* projection_size = _projection_size ; 679* layer_size = _layer_size ; 680* dt = _dt ; 681 } 682}; 683 684/// A class holding description for a Dropout operation. 685class dropout_desc { 686struct dropout_desc_imp { 687float _p = 0.5f ; 688unsigned long long _seed = 1 ; 689void * _state = nullptr ; 690 std::vector < std::uint8_t > _host_state ; 691rng_engine_t _rng_engine ; 692dropout_desc_imp () :_rng_engine (::dpct::cs::get_default_queue (),1 ) {} 693 }; 694 std::shared_ptr < dropout_desc_imp > _imp ; 695 696void generate (sycl::queue * q , std::int64_t required_state_size , 697 std::int64_t num ,void * buffer ) { 698#ifndef __INTEL_MKL__ 699throw std::runtime_error ("The oneAPI Math Kernel Library (oneMKL) " 700"Interfaces Project does not support this API." ); 701#else 702 sycl::event e_gen = oneapi::mkl::rng::generate ( 703 oneapi::mkl::rng::bernoulli < std::int32_t > (1.f - _imp -> _p ), 704_imp -> _rng_engine ,num , (std::int32_t * )buffer ); 705 sycl::event e_save = q -> submit ([& ](sycl::handler & cgh ) { 706cgh .depends_on (e_gen ); 707cgh .host_task ([= ] { 708 oneapi::mkl::rng::save_state (_imp -> _rng_engine , 709_imp -> _host_state .data ()); 710 }); 711 }); 712q -> memcpy (_imp -> _state ,_imp -> _host_state .data (),required_state_size , 713e_save ); 714#endif 715 } 716public : 717 operatorbool ()const { 718return bool (_imp ); 719 } 720dropout_desc & operator= (std::nullptr_t ) { 721_imp .reset (); 722return * this ; 723 } 724/// Initializing a dropout descriptor. 725void init (){ 726_imp = std::make_shared < dropout_desc_imp > (); 727 } 728/// Setting a dropout descriptor with given parameters. 729/// \param [in] engine Engine of the dropout operation. 730/// \param [in] p Probability of value set to zero. 731/// \param [in] state Memory that store random generator state. 732/// \param [in] state_size Required size to store random generator state. 733/// \param [in] seed Seed to initialize conditions of the generator state. 734void set (engine_ext & engine ,float p ,void * state ,size_t state_size , 735unsigned long long seed ); 736/// Getting parameters from a dropout descriptor. 737/// \param [in] engine Engine of the dropout operation. 738/// \param [in] p Probability of value set to zero. 739/// \param [in] state Memory that store random generator state. 740/// \param [in] seed Seed to initialize conditions of the generator state. 741void get (float * p ,void ** states ,unsigned long long * seed )const noexcept { 742* seed = _imp -> _seed ; 743* states = _imp -> _state ; 744* p = _imp -> _p ; 745 } 746/// Getting the probability of value set to zero. 747/// \returns Probability. 748float get_probability ()const noexcept {return _imp -> _p ; } 749/// Restoreing a dropout descriptor from stored state. 750/// \param [in] engine Engine of the dropout operation. 751/// \param [in] p Probability of value set to zero. 752/// \param [in] state Memory that store random generator state. 753/// \param [in] state_size Required size to store random generator state. 754/// \param [in] seed Seed to initialize conditions of the generator state. 755void restore (engine_ext & engine ,float p ,void * state ,size_t state_size , 756unsigned long long seed ); 757friend class engine_ext ; 758}; 759 760/// A class holding the oneDNN engine. 761class engine_ext { 762struct output_argument_info { 763float _alpha ; 764float _beta ; 765int _name ; 766memory_desc_ext _desc ; 767void * _data ; 768output_argument_info (float alpha ,float beta ,int name , 769memory_desc_ext desc ,void * data ) 770 :_alpha (alpha ),_beta (beta ),_name (name ),_desc (desc ),_data (data ) {} 771output_argument_info (float alpha ,float beta ,memory_desc_ext desc , 772void * data ) 773 :_alpha (alpha ),_beta (beta ),_name (0 ),_desc (desc ),_data (data ) {} 774 }; 775struct buffer_info { 776size_t capacity = 0 ; 777uint8_t * buffer = nullptr ; 778size_t usage = 0 ; 779 sycl::queue q ; 780 sycl::event deps ; 781size_t primitive_depth = 0 ; 782 }; 783struct internal_resource { 784 std::int64_t random_engine_state_size = -1 ; 785buffer_info binfo ; 786 }; 787 std::shared_ptr < ::dnnl::engine > _eng = nullptr ; 788 std::shared_ptr < ::dnnl::stream > _s = nullptr ; 789 sycl::queue * _q = nullptr ; 790unsigned int _engine_id = 0 ; 791static thread_localunsigned int _engine_count ; 792static thread_local std::map < void * , ::dnnl::memory > _workspace_map ; 793static thread_local std::map < sycl::queue * , 794 std::shared_ptr < internal_resource >> 795_internal_resource_cache ; 796static thread_local detail::primitive_cache _primitive_cache ; 797 ::dnnl::memory & get_workspace (void * key ) {return _workspace_map [key ]; } 798void insert_workspace (void * key , ::dnnl::memory workspace ) { 799_workspace_map [key ]= workspace ; 800 } 801const ::dnnl::stream & get_stream ()const {return * _s ; } 802const ::dnnl::engine & get_engine ()const {return * _eng ; } 803 804void * allocate (const memory_desc_ext & desc ,int count = 1 ); 805void * allocate (size_t size ); 806 std::shared_ptr < internal_resource > get_internal_resource (sycl::queue * q ){ 807auto it = _internal_resource_cache .find (_q ); 808if (it == _internal_resource_cache .end ()) { 809return _internal_resource_cache [_q ]= std::make_shared < internal_resource > (); 810 } 811return it -> second ; 812 } 813void enter_primitive (size_t request_buffer_size = 0 ) { 814auto & info = get_internal_resource (_q )-> binfo ; 815if (info .primitive_depth == 0 ) { 816info .usage = 0 ; 817if (request_buffer_size > info .capacity ) { 818if (info .buffer && (info .capacity != 0 )) { 819auto ainfo = info ; 820ainfo .q .submit ([= ](sycl::handler & cgh ) { 821cgh .depends_on (ainfo .deps ); 822cgh .host_task ([= ] { sycl::free (ainfo .buffer ,ainfo .q ); }); 823 }); 824 } 825size_t new_buffer_capacity = 826 (std::max )(request_buffer_size ,info .capacity * 2 ); 827info .capacity = new_buffer_capacity ; 828info .buffer = (uint8_t * )sycl::malloc_device (new_buffer_capacity ,* _q ); 829info .q = * _q ; 830info .deps = sycl::event (); 831 } 832 } 833info .primitive_depth ++ ; 834 } 835 sycl::event exit_primitive (const sycl::event & e ) { 836auto & info = get_internal_resource (_q )-> binfo ; 837info .primitive_depth -- ; 838if ((info .primitive_depth == 0 )&& info .usage ) { 839info .deps = e ; 840 } 841return e ; 842 } 843 ::dnnl::memory::desc 844compress_spatial_dimensions_to_channel (const ::dnnl::memory::desc & desc ); 845 ::dnnl::memory::desc 846get_bn_scale_bias_mean_var_desc (const ::dnnl::memory::desc & desc , 847batch_normalization_mode mode ); 848 sycl::event batch_normalization_backward_internal ( 849batch_normalization_mode mode ,float epsilon ,float alpha_data , 850const memory_desc_ext & src_desc ,void * src , 851const memory_desc_ext & diff_dst_desc ,void * diff_dst ,float beta_data , 852const memory_desc_ext & diff_src_desc ,void * diff_src ,float alpha_param , 853const memory_desc_ext & diff_scale_bias_desc ,void * scale ,void * bias , 854float beta_param ,void * diff_scale ,void * diff_bias , 855const memory_desc_ext & mean_var_desc ,void * saved_mean ,void * saved_var ); 856 sycl::event batch_normalization_forward_internal ( 857bool is_infer ,batch_normalization_mode mode ,float epsilon ,float factor , 858float alpha ,const memory_desc_ext & src_desc ,void * src ,float beta , 859const memory_desc_ext & dst_desc ,void * dst , 860const memory_desc_ext & scale_bias_desc ,void * scale ,void * bias , 861const memory_desc_ext & mean_var_desc ,void * saved_mean ,void * saved_var , 862void * running_mean ,void * running_var ); 863 ::dnnl::memory::desc 864transfer_memory_desc_to_channel_major_format (const ::dnnl::memory::desc & desc ); 865 ::dnnl::memory::desc 866bn_reorder_memory_to_channel_major_format ( 867bool is_input , ::dnnl::memory::desc & desc ,void * src ,void ** cache ); 868 ::dnnl::memory::desc 869transfer_memory_desc_to_format_tag_any (const ::dnnl::memory::desc & desc ){ 870return ::dnnl::memory::desc (desc .get_dims (),desc .get_data_type (), 871 ::dnnl::memory::format_tag::any ); 872 } 873void allocate_and_reorder_memory_to_optimal (::dnnl::memory::desc & from_desc , 874void *& from , 875 ::dnnl::memory::desc & to_desc , 876void *& to ) { 877if (from_desc != to_desc ) { 878to = allocate (to_desc ); 879async_reorder (1.f ,from_desc ,from ,0.f ,to_desc ,to ); 880 } 881 } 882template < typename primitive_type ,typename ...args_type > 883 std::pair < detail::primitive_cache_key_type , detail::primitive_and_args > 884create_primitive_args_or_get (args_type && ...args ); 885template < typename primitive_type > 886typename primitive_type::primitive_desc 887get_primitive_desc (::dnnl::primitive * p ); 888template < typename primitive_type ,typename ...args_type > 889typename primitive_type::primitive_desc 890create_primitive_desc (args_type && ...args ); 891template < typename T > 892void generate_cache_key (std::string & key_buffer ,const T & arg ); 893template < typename T ,typename ...args_type > 894void generate_cache_key (std::string & key_buffer ,const T & first_arg , 895const args_type & ...args ); 896void insert_arg (std::unordered_map < int , ::dnnl::memory > * args ,int name , 897const ::dnnl::memory::desc & desc ,void * data ) { 898auto it = args -> find (name ); 899if (it != args -> end ()) { 900it -> second .set_data_handle (data ); 901 }else { 902args -> insert ({name , ::dnnl::memory (desc ,* _eng ,data )}); 903 } 904 } 905void insert_arg (std::unordered_map < int , ::dnnl::memory > * args ,int name , 906const ::dnnl::memory & mem ) { 907 (* args )[name ]= mem ; 908 } 909 sycl::event execute_rnn_forward_primitive ( 910rnn_mode mode , ::dnnl::prop_kind kind , ::dnnl::rnn_direction direction , 911rnn_bias_mode bias_mode , ::dnnl::memory::data_type dt , 912 ::dnnl::memory::format_tag tag ,int seq_length ,int batch_size ,int src_c , 913int dst_c ,int layer_size ,int direction_num ,int hidden_size , 914int gate_num ,int projection_size , std::vector < void *> & data , 915 std::vector < int > & offset ,int iter_num ,size_t * weight_size = nullptr , 916size_t * workspace_size = nullptr ,size_t * scratchpad_size = nullptr ); 917 918 sycl::event rnn_forward_internal ( 919const rnn_desc & desc , ::dnnl::prop_kind kind , 920const memory_desc_ext & src_desc ,void * src , 921const memory_desc_ext & dst_desc ,void * dst , 922const memory_desc_ext & iter_desc ,void * src_iter ,void * dst_iter , 923const memory_desc_ext & iter_c_desc ,void * src_iter_c ,void * dst_iter_c , 924size_t weight_size ,void * weight ,size_t workspace_size ,void * workspace , 925size_t scratchpad_size ,void * scratchpad ,bool is_get_execution_args , 926size_t * weight_size_query ,size_t * workspace_size_query , 927size_t * scratchpad_size_query ); 928 929 sycl::event execute_rnn_backward_primitive ( 930rnn_mode mode , ::dnnl::rnn_direction direction ,rnn_bias_mode bias_mode , 931 ::dnnl::memory::data_type dt , ::dnnl::memory::format_tag tag , 932int seq_length ,int batch_size ,int src_c ,int dst_c ,int layer_size , 933int direction_num ,int hidden_size ,int gate_num ,int projection_size , 934 std::vector < void *> & data , std::vector < int > & offset ,int iter_num ); 935bool 936scale_parameter_preprocess (const std::vector < output_argument_info > & args ); 937template < typename primitive_type > 938 sycl::event 939execute_primitive (const std::pair < detail::primitive_cache_key_type , 940 detail::primitive_and_args > & primitive , 941const std::vector < output_argument_info > & extra_args = {}); 942template < typename T > 943sycl::event fill_with_type (sycl::queue * q , void * src , const void * value , 944size_t size_with_byte ) { 945return q -> fill < T > ( static_cast < T *> ( src ), * static_cast < const T *> ( value ), 946size_with_byte / sizeof ( T )); 947} 948template < typename T > 949void transform_no_zero_with_type (sycl::queue * q , void * src , void * dst , 950size_t num ) { 951q -> submit ([ & ](sycl::handler & cgh ) { 952cgh . parallel_for < ::dpct::cs::kernel_name < class zero_to_one, T >> ( 953sycl:: range < 1 > ( num ), [ = ](sycl::id < 1 > idx ) { 954T * src_ptr = static_cast < T *> ( src ) + idx [ 0 ]; 955T * dst_ptr = static_cast < T *> ( dst ) + idx [ 0 ]; 956if ( * src_ptr ) { 957* dst_ptr = * src_ptr ; 958} else { 959* dst_ptr = 1 ; 960} 961}); 962}); 963} 964void transform_no_zero ( const memory_desc_ext & desc , void * src , void * dst ); 965::dnnl::memory::desc get_group_weight_desc (int group_count , 966const memory_desc_ext & weight_desc ); 967void get_rnn_configuration ( const ::dnnl::memory::desc & desc , 968rnn_direction direction , rnn_mode mode , 969dpct::library_data_t dt , int hidden_size , 970::dnnl::memory::data_type * dnnl_dt , 971::dnnl::memory::format_tag * tag , 972int * projection_size , int * output_size , 973int * seq_length , int * batch_size , 974int * direction_num , int * gate_num ); 975public : 976engine_ext () {} 977engine_ext (std::nullptr_t) { 978_eng = nullptr ; 979_s = nullptr ; 980_q = nullptr ; 981} 982operator bool() const { 983return bool( _eng ) && bool( _s ) && bool( _q ); 984} 985engine_ext & operator = (std::nullptr_t) { 986_eng = nullptr ; 987_s = nullptr ; 988_q = nullptr ; 989return * this ; 990} 991/// Creating oneDNN engine. 992void create_engine () { 993#if USE_DPCT_HELPER 994_q = & ::dpct::cs:: get_current_device (). default_queue (); 995#else 996_q = ::dpct::cs:: get_current_device (). default_queue (); 997#endif 998_eng = std:: make_shared < ::dnnl::engine > (::dnnl::sycl_interop:: make_engine ( 999::dpct::cs:: get_current_device (), 1000::dpct::cs:: get_current_device (). get_context ())); 1001_s = std:: make_shared < ::dnnl::stream > ( 1002::dnnl::sycl_interop:: make_stream ( * _eng , * _q )); 1003_engine_id = _engine_count ++ ; 1004} 1005/// Setting the user's SYCL queue for an oneDNN engine. 1006/// \param [in] q Pointer to the SYCL queue. 1007void set_queue (sycl::queue * q ) { 1008if (! q ) { 1009throw std:: runtime_error ( "set_queue: pointer must not be nullptr." ); 1010} 1011if (! _eng ) { 1012throw std:: runtime_error ( "set_queue: current engine is invalid." ); 1013} 1014if ( q -> get_context () != ::dnnl::sycl_interop:: get_context ( * _eng )) { 1015throw std:: runtime_error ( 1016"set_queue: queue is mismatch with current engine context." ); 1017} 1018_q = q ; 1019_s = std:: make_shared < ::dnnl::stream > ( 1020::dnnl::sycl_interop:: make_stream ( * _eng , * _q )); 1021} 1022/// Retrieving the user's SYCL queue set in the oneDNN engine. 1023/// \returns Pointer to the SYCL queue. 1024sycl::queue * get_queue () const { return _q ; } 1025/// Setting all elements of a memory to a given value. 1026/// \param [in] src_desc Source memory descriptor. 1027/// \param [in] src Pointer to source data. 1028/// \param [in] valuePtr Pointer to a single value. 1029void fill ( const memory_desc_ext & src_desc , void * src , 1030const void * valuePtr ); 1031/// Coping the scaled data from a memory to another memory with a different 1032/// description. 1033/// \param [in] alpha Value to scaling factors used to scale the computed 1034/// value. 1035/// \param [in] src_desc Source memory descriptor. 1036/// \param [in] src Pointer to source data. 1037/// \param [in] beta Value to scaling factors used to scale the prior value 1038/// in the destination memory. 1039/// \param [in] dst_desc Destination memory descriptor. 1040/// \param [out] dst Pointer to destination data. 1041void reorder (float alpha , const memory_desc_ext & src_desc , void * src , 1042float beta , const memory_desc_ext & dst_desc , void * dst ); 1043/// Scaling all the elements of a memory by a given factor. 1044/// \param [in] alpha Value to scaling factors. 1045/// \param [in] src_desc Source memory descriptor. 1046/// \param [out] src Pointer to source data. 1047void scale (float alpha , const memory_desc_ext & src_desc , void * src ); 1048/// Adding the scaled values of a memory to another memory. 1049/// \param [in] alpha Value to scaling factors used to scale the computed 1050/// value. 1051/// \param [in] src_desc Source memory descriptor. 1052/// \param [in] src Pointer to source data. 1053/// \param [in] beta Value to scaling factors used to scale the prior value 1054/// in the destination memory. 1055/// \param [in] dst_desc Destination memory descriptor. 1056/// \param [out] dst Pointer to destination data. 1057void sum (float alpha , const memory_desc_ext & src_desc , void * src , 1058float beta , const memory_desc_ext & dst_desc , void * dst ); 1059/// Computing a specified activation function value. 1060/// \param [in] desc Activation descriptor. 1061/// \param [in] alpha Value to scaling factors used to scale the computed 1062/// value. 1063/// \param [in] src_desc Source memory descriptor. 1064/// \param [in] src Pointer to source data. 1065/// \param [in] beta Value to scaling factors used to scale the prior value 1066/// in the destination memory. 1067/// \param [in] dst_desc Destination memory descriptor. 1068/// \param [out] dst Pointer to destination data. 1069void activation_forward (activation_desc & desc , float alpha , 1070const memory_desc_ext & src_desc , void * src , 1071float beta , const memory_desc_ext & dst_desc , 1072void * dst ); 1073/// Computing the gradient of a specified activation function. 1074/// \param [in] desc Activation descriptor. 1075/// \param [in] alpha Value to scaling factors used to scale the computed 1076/// value. 1077/// \param [in] dst_desc Destination memory descriptor. 1078/// \param [in] dst Pointer to destination data. 1079/// \param [in] diff_dst_desc Differential destination memory descriptor. 1080/// \param [in] diff_dst Pointer to differential destination data. 1081/// \param [in] src_desc Source memory descriptor. 1082/// \param [in] src Pointer to source data. 1083/// \param [in] beta Value to scaling factors used to scale the prior value 1084/// in the differential destination memory. 1085/// \param [in] diff_src_desc Differential source memory descriptor. 1086/// \param [out] diff_src Pointer to differential source data. 1087void 1088activation_backward (activation_desc & desc , float alpha , 1089const memory_desc_ext & dst_desc , void * dst , 1090const memory_desc_ext & diff_dst_desc , void * diff_dst , 1091const memory_desc_ext & src_desc , void * src , float beta , 1092const memory_desc_ext & diff_src_desc , void * diff_src ); 1093/// Computing a specified pooling function value. 1094/// \param [in] desc Pooling descriptor. 1095/// \param [in] alpha Value to scaling factors used to scale the computed 1096/// value. 1097/// \param [in] src_desc Source memory descriptor. 1098/// \param [in] src Pointer to source data. 1099/// \param [in] beta Value to scaling factors used to scale the prior value 1100/// in the destination memory. 1101/// \param [in] dst_desc Destination memory descriptor. 1102/// \param [out] dst Pointer to destination data. 1103/// \param [out] workspace Pointer to workspace generated from forward propagation. 1104void pooling_forward (pooling_desc & desc , float alpha , 1105const memory_desc_ext & src_desc , void * src , 1106float beta , const memory_desc_ext & dst_desc , 1107void * dst , ::dnnl::memory * workspace = nullptr ); 1108/// Computing the gradient of a specified pooling function. 1109/// \param [in] desc Activation descriptor. 1110/// \param [in] alpha Value to scaling factors used to scale the computed 1111/// value. 1112/// \param [in] dst_desc Destination memory descriptor. 1113/// \param [in] dst Pointer to destination data. 1114/// \param [in] diff_dst_desc Differential destination memory descriptor. 1115/// \param [in] diff_dst Pointer to differential destination data. 1116/// \param [in] src_desc Source memory descriptor. 1117/// \param [in] src Pointer to source data. 1118/// \param [in] beta Value to scaling factors used to scale the prior value 1119/// in the differential destination memory. 1120/// \param [in] diff_src_desc Differential source memory descriptor. 1121/// \param [out] diff_src Pointer to differential 1122/// source data. 1123/// \param [in] workspace Pointer to workspace used for backward 1124/// propagation. 1125void pooling_backward (pooling_desc & desc , float alpha , 1126const memory_desc_ext & dst_desc , void * dst , 1127const memory_desc_ext & diff_dst_desc , 1128void * diff_dst , const memory_desc_ext & src_desc , 1129void * src , float beta , 1130const memory_desc_ext & diff_src_desc , 1131void * diff_src , 1132::dnnl::memory * workspace = nullptr ); 1133/// Computing a specified softmax function value. 1134/// \param [in] alg Softmax algorithm. 1135/// \param [in] mode Softmax mode. 1136/// \param [in] alpha Value to scaling factors used to scale the computed 1137/// value. 1138/// \param [in] src_desc Source memory descriptor. 1139/// \param [in] src Pointer to source data. 1140/// \param [in] beta Value to scaling factors used to scale the prior value 1141/// in the destination memory. 1142/// \param [in] dst_desc Destination memory descriptor. 1143/// \param [out] dst Pointer to destination data. 1144void softmax_forward (softmax_algorithm alg , softmax_mode mode , 1145float alpha , const memory_desc_ext & src_desc , 1146void * src , float beta , 1147const memory_desc_ext & dst_desc , void * dst ); 1148/// Computing the gradient of a specified softmax function. 1149/// \param [in] alg Softmax algorithm. 1150/// \param [in] mode Softmax mode. 1151/// \param [in] alpha Value to scaling factors used to scale the computed 1152/// value. 1153/// \param [in] dst_desc Destination memory descriptor. 1154/// \param [in] dst Pointer to destination data. 1155/// \param [in] diff_dst_desc Differential destination memory descriptor. 1156/// \param [in] diff_dst Pointer to differential destination data. 1157/// \param [in] beta Value to scaling factors used to scale the prior value 1158/// in the differential destination memory. 1159/// \param [in] diff_src_desc Differential source memory descriptor. 1160/// \param [out] diff_src Pointer to differential source data. 1161void softmax_backward (softmax_algorithm alg , softmax_mode mode , 1162float alpha , const memory_desc_ext & dst_desc , 1163void * dst , const memory_desc_ext & diff_dst_desc , 1164void * diff_dst , float beta , 1165const memory_desc_ext & diff_src_desc , 1166void * diff_src ); 1167/// Computing a specified local response normalization function value. 1168/// \param [in] desc Local response normalization descriptor. 1169/// \param [in] alpha Value to scaling factors used to scale the computed 1170/// value. 1171/// \param [in] src_desc Source memory descriptor. 1172/// \param [in] src Pointer to source data. 1173/// \param [in] beta Value to scaling factors used to scale the prior value 1174/// in the destination memory. 1175/// \param [in] dst_desc Destination memory descriptor. 1176/// \param [out] dst Pointer to destination data. 1177/// \param [out] workspace Pointer to workspace generated from forward 1178/// propagation. 1179void lrn_forward (lrn_desc & desc , float alpha , 1180const memory_desc_ext & src_desc , void * src , 1181float beta , const memory_desc_ext & dst_desc , 1182void * dst , ::dnnl::memory * workspace = nullptr ); 1183/// Computing the gradient of a specified local response normalization 1184/// function. 1185/// \param [in] desc Local response normalization descriptor. 1186/// \param [in] alpha Value to scaling factors used to scale the computed value. 1187/// \param [in] dst_desc Destination memory descriptor. 1188/// \param [in] dst Pointer to destination data. 1189/// \param [in] diff_dst_desc Differential destination memory descriptor. 1190/// \param [in] diff_dst Pointer to differential destination data. 1191/// \param [in] src_desc Source memory descriptor. 1192/// \param [in] src Pointer to source data. 1193/// \param [in] beta Value to scaling factors used to scale the prior value 1194/// in the differential destination memory. 1195/// \param [in] diff_src_desc Differential source memory descriptor. 1196/// \param [out] diff_src Pointer to differential source data. 1197/// \param [in] workspace Pointer to workspace used for backward propagation. 1198void lrn_backward (lrn_desc & desc , float alpha , 1199const memory_desc_ext & dst_desc , void * dst , 1200const memory_desc_ext & diff_dst_desc , void * diff_dst , 1201const memory_desc_ext & src_desc , void * src , 1202float beta , const memory_desc_ext & diff_src_desc , 1203void * diff_src , ::dnnl::memory * workspace = nullptr ); 1204/// Setting all elements of a memory to a given value asynchronously. 1205/// \param [in] src_desc Source memory descriptor. 1206/// \param [in] src Pointer to source data. 1207/// \param [in] valuePtr Pointer to a single value. 1208/// \returns An event representing the fill operations. 1209sycl::event async_fill ( const memory_desc_ext & src_desc , void * src , 1210const void * valuePtr ); 1211/// Coping the scaled data from a memory to another memory with a different 1212/// description asynchronously. 1213/// \param [in] alpha Value to scaling factors used to scale the computed 1214/// value. 1215/// \param [in] src_desc Source memory descriptor. 1216/// \param [in] src Pointer to source data. 1217/// \param [in] beta Value to scaling factors used to scale the prior value 1218/// in the destination memory. 1219/// \param [in] dst_desc Destination memory descriptor. 1220/// \param [out] dst Pointer to destination data. 1221/// \returns An event representing the reorder operations. 1222sycl::event async_reorder (float alpha , const memory_desc_ext & src_desc , void * src , 1223float beta , const memory_desc_ext & dst_desc , void * dst ); 1224/// Scaling all the elements of a memory by a given factor asynchronously. 1225/// \param [in] alpha Value to scaling factors. 1226/// \param [in] src_desc Source memory descriptor. 1227/// \param [out] src Pointer to source data. 1228/// \returns An event representing the scale operations. 1229sycl::event async_scale (float alpha , const memory_desc_ext & src_desc , void * src ); 1230/// Adding the scaled values of a memory to another memory asynchronously. 1231/// \param [in] alpha Value to scaling factors used to scale the computed 1232/// value. 1233/// \param [in] src_desc Source memory descriptor. 1234/// \param [in] src Pointer to source data. 1235/// \param [in] beta Value to scaling factors used to scale the prior value 1236/// in the destination memory. 1237/// \param [in] dst_desc Destination memory descriptor. 1238/// \param [out] dst Pointer to destination data. 1239/// \returns An event representing the sum operations. 1240sycl::event async_sum (float alpha , const memory_desc_ext & src_desc , void * src , 1241float beta , const memory_desc_ext & dst_desc , void * dst ); 1242 1243/// Perform specified binary operation asynchronously. 1244/// \param [in] op Specified binary operation. 1245/// \param [in] alpha_0 Value to scaling factors used to scale the src_0 1246/// value. 1247/// \param [in] src_desc_0 Source 0 memory descriptor. 1248/// \param [in] src_0 Pointer to source 0 data. 1249/// \param [in] alpha_1 Value to scaling factors used to scale the src_1 1250/// value. 1251/// \param [in] src_desc_1 Source 1 memory descriptor. 1252/// \param [in] src_1 Pointer to source 1 data. 1253/// \param [in] beta Value to scaling factors used to scale the prior value 1254/// in the destination memory. 1255/// \param [in] dst_desc Destination memory descriptor. 1256/// \param [out] dst Pointer to destination data. 1257/// \returns An event representing the binary operations. 1258sycl::event async_binary (binary_op op , float alpha_0 , 1259const memory_desc_ext & src_desc_0 , void * src_0 , 1260float alpha_1 , const memory_desc_ext & src_desc_1 , 1261void * src_1 , float beta , const memory_desc_ext & dst_desc , 1262void * dst ); 1263 1264/// Perform specified binary operation asynchronously. 1265/// \param [in] op Specified reduction operation. 1266/// \param [in] alpha Value to scaling factors used to scale the data 1267/// value. 1268/// \param [in] src_desc Source memory descriptor. 1269/// \param [in] src Pointer to source data. 1270/// \param [in] beta Value to scaling factors used to scale the prior value 1271/// in the destination memory. 1272/// \param [in] dst_desc Destination memory descriptor. 1273/// \param [out] dst Pointer to destination data. 1274/// \returns An event representing the reduction operations. 1275sycl::event async_reduction (reduction_op op , float alpha , 1276const memory_desc_ext & src_desc , void * src , float beta , 1277const memory_desc_ext & dst_desc , void * dst ); 1278/// Computing a specified activation function value asynchronously. 1279/// \param [in] desc Activation descriptor. 1280/// \param [in] alpha Value to scaling factors used to scale the computed 1281/// value. 1282/// \param [in] src_desc Source memory descriptor. 1283/// \param [in] src Pointer to source data. 1284/// \param [in] beta Value to scaling factors used to scale the prior value 1285/// in the destination memory. 1286/// \param [in] dst_desc Destination memory descriptor. 1287/// \param [out] dst Pointer to destination data. 1288/// \returns An event representing the activation forward operations. 1289sycl::event async_activation_forward (activation_desc & desc , float alpha , 1290const memory_desc_ext & src_desc , void * src , 1291float beta , const memory_desc_ext & dst_desc , 1292void * dst ); 1293/// Computing the gradient of a specified activation function asynchronously. 1294/// \param [in] desc Activation descriptor. 1295/// \param [in] alpha Value to scaling factors used to scale the computed 1296/// value. 1297/// \param [in] dst_desc Destination memory descriptor. 1298/// \param [in] dst Pointer to destination data. 1299/// \param [in] diff_dst_desc Differential destination memory descriptor. 1300/// \param [in] diff_dst Pointer to differential destination data. 1301/// \param [in] src_desc Source memory descriptor. 1302/// \param [in] src Pointer to source data. 1303/// \param [in] beta Value to scaling factors used to scale the prior value 1304/// in the differential destination memory. 1305/// \param [in] diff_src_desc Differential source memory descriptor. 1306/// \param [out] diff_src Pointer to differential source data. 1307/// \returns An event representing the activation backward operations. 1308sycl::event 1309async_activation_backward (activation_desc & desc , float alpha , 1310const memory_desc_ext & dst_desc , void * dst , 1311const memory_desc_ext & diff_dst_desc , void * diff_dst , 1312const memory_desc_ext & src_desc , void * src , float beta , 1313const memory_desc_ext & diff_src_desc , void * diff_src ); 1314/// Computing a specified pooling function value asynchronously. 1315/// \param [in] desc Pooling descriptor. 1316/// \param [in] alpha Value to scaling factors used to scale the computed 1317/// value. 1318/// \param [in] src_desc Source memory descriptor. 1319/// \param [in] src Pointer to source data. 1320/// \param [in] beta Value to scaling factors used to scale the prior value 1321/// in the destination memory. 1322/// \param [in] dst_desc Destination memory descriptor. 1323/// \param [out] dst Pointer to destination data. 1324/// \param [out] workspace Pointer to workspace generated from forward propagation. 1325/// \returns An event representing the pooling forward operations. 1326sycl::event async_pooling_forward (pooling_desc & desc , float alpha , 1327const memory_desc_ext & src_desc , void * src , 1328float beta , const memory_desc_ext & dst_desc , 1329void * dst , ::dnnl::memory * workspace = nullptr ); 1330/// Computing the gradient of a specified pooling function asynchronously. 1331/// \param [in] desc Activation descriptor. 1332/// \param [in] alpha Value to scaling factors used to scale the computed 1333/// value. 1334/// \param [in] dst_desc Destination memory descriptor. 1335/// \param [in] dst Pointer to destination data. 1336/// \param [in] diff_dst_desc Differential destination memory descriptor. 1337/// \param [in] diff_dst Pointer to differential destination data. 1338/// \param [in] src_desc Source memory descriptor. 1339/// \param [in] src Pointer to source data. 1340/// \param [in] beta Value to scaling factors used to scale the prior value 1341/// in the differential destination memory. 1342/// \param [in] diff_src_desc Differential source memory descriptor. 1343/// \param [out] diff_src Pointer to differential 1344/// source data. 1345/// \param [in] workspace Pointer to workspace used for backward 1346/// propagation. 1347/// \returns An event representing the pooling backward operations. 1348sycl::event async_pooling_backward (pooling_desc & desc , float alpha , 1349const memory_desc_ext & dst_desc , void * dst , 1350const memory_desc_ext & diff_dst_desc , 1351void * diff_dst , const memory_desc_ext & src_desc , 1352void * src , float beta , 1353const memory_desc_ext & diff_src_desc , 1354void * diff_src , 1355::dnnl::memory * workspace = nullptr ); 1356/// Computing a specified softmax function value asynchronously. 1357/// \param [in] alg Softmax algorithm. 1358/// \param [in] mode Softmax mode. 1359/// \param [in] alpha Value to scaling factors used to scale the computed 1360/// value. 1361/// \param [in] src_desc Source memory descriptor. 1362/// \param [in] src Pointer to source data. 1363/// \param [in] beta Value to scaling factors used to scale the prior value 1364/// in the destination memory. 1365/// \param [in] dst_desc Destination memory descriptor. 1366/// \param [out] dst Pointer to destination data. 1367/// \returns An event representing the softmax forward operations. 1368sycl::event async_softmax_forward (softmax_algorithm alg , softmax_mode mode , 1369float alpha , const memory_desc_ext & src_desc , 1370void * src , float beta , 1371const memory_desc_ext & dst_desc , void * dst ); 1372/// Computing the gradient of a specified softmax function asynchronously. 1373/// \param [in] alg Softmax algorithm. 1374/// \param [in] mode Softmax mode. 1375/// \param [in] alpha Value to scaling factors used to scale the computed 1376/// value. 1377/// \param [in] dst_desc Destination memory descriptor. 1378/// \param [in] dst Pointer to destination data. 1379/// \param [in] diff_dst_desc Differential destination memory descriptor. 1380/// \param [in] diff_dst Pointer to differential destination data. 1381/// \param [in] beta Value to scaling factors used to scale the prior value 1382/// in the differential destination memory. 1383/// \param [in] diff_src_desc Differential source memory descriptor. 1384/// \param [out] diff_src Pointer to differential source data. 1385/// \returns An event representing the softmax backward operations. 1386sycl::event async_softmax_backward (softmax_algorithm alg , softmax_mode mode , 1387float alpha , const memory_desc_ext & dst_desc , 1388void * dst , const memory_desc_ext & diff_dst_desc , 1389void * diff_dst , float beta , 1390const memory_desc_ext & diff_src_desc , 1391void * diff_src ); 1392/// Computing a specified local response normalization function value 1393/// asynchronously. 1394/// \param [in] desc Local response normalization descriptor. 1395/// \param [in] alpha Value to scaling factors used to scale the computed 1396/// value. 1397/// \param [in] src_desc Source memory descriptor. 1398/// \param [in] src Pointer to source data. 1399/// \param [in] beta Value to scaling factors used to scale the prior value 1400/// in the destination memory. 1401/// \param [in] dst_desc Destination memory descriptor. 1402/// \param [out] dst Pointer to destination data. 1403/// \param [out] workspace Pointer to workspace generated from forward 1404/// propagation. 1405/// \returns An event representing the lrn forward operations. 1406sycl::event async_lrn_forward (lrn_desc & desc , float alpha , 1407const memory_desc_ext & src_desc , void * src , 1408float beta , const memory_desc_ext & dst_desc , 1409void * dst , ::dnnl::memory * workspace = nullptr ); 1410/// Computing the gradient of a specified local response normalization 1411/// function asynchronously. 1412/// \param [in] desc Local response normalization descriptor. 1413/// \param [in] alpha Value to scaling factors used to scale the computed value. 1414/// \param [in] dst_desc Destination memory descriptor. 1415/// \param [in] dst Pointer to destination data. 1416/// \param [in] diff_dst_desc Differential destination memory descriptor. 1417/// \param [in] diff_dst Pointer to differential destination data. 1418/// \param [in] src_desc Source memory descriptor. 1419/// \param [in] src Pointer to source data. 1420/// \param [in] beta Value to scaling factors used to scale the prior value 1421/// in the differential destination memory. 1422/// \param [in] diff_src_desc Differential source memory descriptor. 1423/// \param [out] diff_src Pointer to differential source data. 1424/// \param [in] workspace Pointer to workspace used for backward propagation. 1425/// \returns An event representing the lrn backward operations. 1426sycl::event async_lrn_backward (lrn_desc & desc , float alpha , 1427const memory_desc_ext & dst_desc , void * dst , 1428const memory_desc_ext & diff_dst_desc , void * diff_dst , 1429const memory_desc_ext & src_desc , void * src , 1430float beta , const memory_desc_ext & diff_src_desc , 1431void * diff_src , ::dnnl::memory * workspace = nullptr ); 1432 1433/// Derives a memory descriptor for the batch normalization scale, bias, mean, 1434/// variance from the source memory descriptor and batch normalization mode. 1435/// \param [out] desc Derived memory descriptor. 1436/// \param [in] src_desc Source memory descriptor. 1437/// \param [in] mode Batch normalization mode. 1438static void derive_batch_normalization_memory_desc (memory_desc_ext & desc , 1439const memory_desc_ext & src_desc , 1440batch_normalization_mode mode ); 1441 1442/// Derives a memory descriptor for the batch normalization scale, bias, mean, 1443/// variance from the source memory descriptor and batch normalization mode. 1444/// \param [out] scale_bias_desc Derived scale and bias memory descriptor. 1445/// \param [out] mean_var_desc Derived mean and var memory descriptor. 1446/// \param [in] src_desc Source memory descriptor. 1447/// \param [in] mode Batch normalization mode. 1448static void derive_batch_normalization_memory_desc (memory_desc_ext & scale_bias_desc , 1449memory_desc_ext & mean_var_desc , 1450const memory_desc_ext & src_desc , 1451batch_normalization_mode mode ); 1452 1453/// Get the size of workspace that needed by batch normalization. The data stored 1454/// in workspace must be preserved between forward and backward. 1455/// \param [in] ops Batch normalization operation mode. This mode can set to 1456/// perform only batch normalization, or batch normalization followed by 1457/// activation, or batch normalization followed by element-wise addition and 1458/// activation. 1459/// \param [in] src_desc Source memory descriptor. 1460/// \returns Size of workspace. 1461size_t get_batch_normalization_workspace_size ( 1462batch_normalization_ops ops , const memory_desc_ext & src_desc ); 1463 1464/// Computing a specified batch normalization inference stage function value 1465/// asynchronously. 1466/// \param [in] mode Batch normalization mode. 1467/// \param [in] epsilon Epsilon value used in computation. 1468/// \param [in] alpha Value to scaling factors used to scale the computed 1469/// value. 1470/// \param [in] src_desc Source memory descriptor. 1471/// \param [in] src Pointer to source data. 1472/// \param [in] beta Value to scaling factors used to scale the prior value 1473/// in the destination memory. 1474/// \param [in] dst_desc Destination memory descriptor. 1475/// \param [out] dst Pointer to destination data. 1476/// \param [in] scale_bias_mean_var_desc Scale, bias, mean, variance memory 1477/// descriptor. 1478/// \param [in] scale Pointer to scale data. 1479/// \param [in] bias Pointer to bias data. 1480/// \param [in] mean Pointer to mean data. 1481/// \param [in] var Pointer to variance data. 1482/// \returns An event representing the batch normalization forward operations. 1483sycl::event async_batch_normalization_forward_inference ( 1484batch_normalization_mode mode , float epsilon , float alpha , 1485const memory_desc_ext & src_desc , void * src , float beta , 1486const memory_desc_ext & dst_desc , void * dst , 1487const memory_desc_ext & scale_bias_mean_var_desc , void * scale , void * bias , 1488void * mean , void * var ); 1489 1490/// Computing a specified batch normalization inference stage function value 1491/// asynchronously. 1492/// \param [in] mode Batch normalization mode. 1493/// \param [in] ops Batch normalization operation mode. This mode can set to 1494/// perform only batch normalization, or batch normalization followed by 1495/// activation, or batch normalization followed by element-wise addition and 1496/// activation. 1497/// \param [in] adesc Activation operation descriptor. 1498/// \param [in] epsilon Epsilon value used in computation. 1499/// \param [in] alpha Value to scaling factors used to scale the computed 1500/// value. 1501/// \param [in] src_desc Source memory descriptor. 1502/// \param [in] src Pointer to source data. 1503/// \param [in] beta Value to scaling factors used to scale the prior value 1504/// in the destination memory. 1505/// \param [in] dst_desc Destination memory descriptor. 1506/// \param [out] dst Pointer to destination data. 1507/// \param [in] summand_desc Summand memory descriptor. 1508/// \param [in] summand Pointer to summand data. 1509/// \param [in] scale_bias_desc Scale, bias memory descriptor. 1510/// \param [in] scale Pointer to scale data. 1511/// \param [in] bias Pointer to bias data. 1512/// \param [in] mean_var_desc Mean, variance memory descriptor. 1513/// \param [in] mean Pointer to mean data. 1514/// \param [in] var Pointer to variance data. 1515/// \returns An event representing the batch normalization forward operations. 1516sycl::event async_batch_normalization_forward_inference ( 1517batch_normalization_mode mode , batch_normalization_ops ops , 1518activation_desc & adesc , float epsilon , float alpha , 1519const memory_desc_ext & src_desc , void * src , float beta , 1520const memory_desc_ext & dst_desc , void * dst , 1521const memory_desc_ext & summand_desc , void * summand , 1522const memory_desc_ext & scale_bias_desc , void * scale , void * bias , 1523const memory_desc_ext & mean_var_desc , void * mean , void * var ); 1524 1525/// Computing a specified batch normalization training stage function value 1526/// asynchronously. 1527/// \param [in] mode Batch normalization mode. 1528/// \param [in] epsilon Epsilon value used in computation. 1529/// \param [in] factor Factor value used in running mean and variance 1530/// computation. 1531/// \param [in] alpha Value to scaling factors used to scale the computed 1532/// value. 1533/// \param [in] src_desc Source memory descriptor. 1534/// \param [in] src Pointer to source data. 1535/// \param [in] beta Value to scaling factors used to scale the prior value 1536/// in the destination memory. 1537/// \param [in] dst_desc Destination memory descriptor. 1538/// \param [out] dst Pointer to destination data. 1539/// \param [in] scale_bias_mean_var_desc Scale, bias, mean, variance memory 1540/// descriptor. 1541/// \param [in] scale Pointer to scale data. 1542/// \param [in] bias Pointer to bias data. 1543/// \param [out] running_mean Pointer to running mean data. 1544/// \param [out] running_var Pointer to running variance data. 1545/// \param [out] saved_mean Pointer to optional cache to save mean data. 1546/// \param [out] saved_var Pointer to optional cache to save variance data. 1547/// \returns An event representing the batch normalization forward operations. 1548sycl::event async_batch_normalization_forward_training ( 1549batch_normalization_mode mode , float epsilon , float factor , float alpha , 1550const memory_desc_ext & src_desc , void * src , float beta , 1551const memory_desc_ext & dst_desc , void * dst , 1552const memory_desc_ext & scale_bias_mean_var_desc , void * scale , void * bias , 1553void * running_mean , void * running_var , void * saved_mean , void * saved_var ); 1554 1555/// Computing a specified batch normalization training stage function value 1556/// asynchronously. 1557/// \param [in] mode Batch normalization mode. 1558/// \param [in] ops Batch normalization operation mode. This mode can set to 1559/// perform only batch normalization, or batch normalization followed by 1560/// activation, or batch normalization followed by element-wise addition and 1561/// activation. 1562/// \param [in] adesc Activation operation descriptor. 1563/// \param [in] epsilon Epsilon value used in computation. 1564/// \param [in] factor Factor value used in running mean and variance 1565/// computation. 1566/// \param [in] alpha Value to scaling factors used to scale the computed 1567/// value. 1568/// \param [in] src_desc Source memory descriptor. 1569/// \param [in] src Pointer to source data. 1570/// \param [in] beta Value to scaling factors used to scale the prior value 1571/// in the destination memory. 1572/// \param [in] dst_desc Destination memory descriptor. 1573/// \param [out] dst Pointer to destination data. 1574/// \param [in] summand_desc Summand memory descriptor. 1575/// \param [in] summand Pointer to summand data. 1576/// \param [in] scale_bias_mean_var_desc Scale, bias, mean, variance memory 1577/// descriptor. 1578/// \param [in] scale Pointer to scale data. 1579/// \param [in] bias Pointer to bias data. 1580/// \param [out] running_mean Pointer to running mean data. 1581/// \param [out] running_var Pointer to running variance data. 1582/// \param [out] saved_mean Pointer to optional cache to save mean data. 1583/// \param [out] saved_var Pointer to optional cache to save variance data. 1584/// \param [in] workspace_size Size of workspace. 1585/// \param [out] workspace Pointer to workspace generated from forward 1586/// propagation. 1587/// \returns An event representing the batch normalization forward operations. 1588sycl::event async_batch_normalization_forward_training ( 1589batch_normalization_mode mode , batch_normalization_ops ops , 1590activation_desc & adesc , float epsilon , float factor , float alpha , 1591const memory_desc_ext & src_desc , void * src , float beta , 1592const memory_desc_ext & dst_desc , void * dst , 1593const memory_desc_ext & summand_desc , void * summand , 1594const memory_desc_ext & scale_bias_mean_var_desc , void * scale , void * bias , 1595void * running_mean , void * running_var , void * saved_mean , void * saved_var , 1596size_t workspace_size , void * workspace ); 1597 1598/// Computing a specified batch normalization training stage function value 1599/// asynchronously. 1600/// \param [in] mode Batch normalization mode. 1601/// \param [in] ops Batch normalization operation mode. This mode can set to 1602/// perform only batch normalization, or batch normalization followed by 1603/// activation, or batch normalization followed by element-wise addition and 1604/// activation. 1605/// \param [in] adesc Activation operation descriptor. 1606/// \param [in] epsilon Epsilon value used in computation. 1607/// \param [in] factor Factor value used in running mean and variance 1608/// computation. 1609/// \param [in] alpha Value to scaling factors used to scale the computed 1610/// value. 1611/// \param [in] src_desc Source memory descriptor. 1612/// \param [in] src Pointer to source data. 1613/// \param [in] beta Value to scaling factors used to scale the prior value 1614/// in the destination memory. 1615/// \param [in] dst_desc Destination memory descriptor. 1616/// \param [out] dst Pointer to destination data. 1617/// \param [in] summand_desc Summand memory descriptor. 1618/// \param [in] summand Pointer to summand data. 1619/// \param [in] scale_bias_desc Scale, bias memory descriptor. 1620/// \param [in] scale Pointer to scale data. 1621/// \param [in] bias Pointer to bias data. 1622/// \param [in] mean_var_desc Mean, variance memory descriptor. 1623/// \param [out] running_mean Pointer to running mean data. 1624/// \param [out] running_var Pointer to running variance data. 1625/// \param [out] saved_mean Pointer to optional cache to save mean data. 1626/// \param [out] saved_var Pointer to optional cache to save variance data. 1627/// \param [in] workspace_size Size of workspace. 1628/// \param [out] workspace Pointer to workspace generated from forward 1629/// propagation. 1630/// \returns An event representing the batch normalization forward operations. 1631sycl::event async_batch_normalization_forward_training ( 1632batch_normalization_mode mode , batch_normalization_ops ops , 1633activation_desc & adesc , float epsilon , float factor , float alpha , 1634const memory_desc_ext & src_desc , void * src , float beta , 1635const memory_desc_ext & dst_desc , void * dst , 1636const memory_desc_ext & summand_desc , void * summand , 1637const memory_desc_ext & scale_bias_desc , void * scale , void * bias , 1638const memory_desc_ext & mean_var_desc , void * running_mean , void * running_var , 1639void * saved_mean , void * saved_var , size_t workspace_size , void * workspace ); 1640 1641/// Computing the gradient of a specified batch normalization function asynchronously. 1642/// \param [in] mode Batch normalization mode. 1643/// \param [in] epsilon Epsilon value used in computation. 1644/// \param [in] alpha_data Value to scaling factors used to scale the computed 1645/// data value. 1646/// \param [in] src_desc Source memory descriptor. 1647/// \param [in] src Pointer to source data. 1648/// \param [in] diff_dst_desc Differential destination memory descriptor. 1649/// \param [in] diff_dst Pointer to differential destination data. 1650/// \param [in] beta_data Value to scaling factors used to scale the prior value 1651/// in the data memory. 1652/// \param [in] diff_src_desc Differential source memory descriptor. 1653/// \param [out] diff_src Pointer to differential source data. 1654/// \param [in] alpha_param Value to scaling factors used to scale the computed 1655/// parameter value. 1656/// \param [in] diff_scale_bias_mean_var_desc Differential scale, bias, mean, 1657/// variance memory descriptor. 1658/// \param [in] scale Pointer to scale data. 1659/// \param [in] beta_param Value to scaling factors used to scale the prior value 1660/// in the parameter memory. 1661/// \param [in] diff_scale Pointer to differential scale data. 1662/// \param [in] diff_bias Pointer to differential bias data. 1663/// \param [in] saved_mean Pointer to optional cache saved mean data in forward. 1664/// \param [in] saved_var Pointer to optional cache saved variance data in forward. 1665/// \returns An event representing the batch normalization backward operations. 1666sycl::event async_batch_normalization_backward ( 1667batch_normalization_mode mode , float epsilon , float alpha_data , 1668const memory_desc_ext & src_desc , void * src , 1669const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta_data , 1670const memory_desc_ext & diff_src_desc , void * diff_src , float alpha_param , 1671const memory_desc_ext & diff_scale_bias_mean_var_desc , void * scale , 1672float beta_param , void * diff_scale , void * diff_bias , void * saved_mean , 1673void * saved_var ); 1674 1675/// Computing the gradient of a specified batch normalization function 1676/// asynchronously. 1677/// \param [in] mode Batch normalization mode. 1678/// \param [in] ops Batch normalization operation mode. This mode can set to 1679/// perform only batch normalization, or batch normalization followed by 1680/// activation, or batch normalization followed by element-wise addition and 1681/// activation. 1682/// \param [in] adesc Activation operation descriptor. 1683/// \param [in] epsilon Epsilon value used in computation. 1684/// \param [in] alpha_data Value to scaling factors used to scale the computed 1685/// data value. 1686/// \param [in] src_desc Source memory descriptor. 1687/// \param [in] src Pointer to source data. 1688/// \param [in] dst_desc Destination memory descriptor. 1689/// \param [in] dst Pointer to destination data. 1690/// \param [in] diff_dst_desc Differential destination memory descriptor. 1691/// \param [in] diff_dst Pointer to differential destination data. 1692/// \param [in] beta_data Value to scaling factors used to scale the prior value 1693/// in the data memory. 1694/// \param [in] diff_src_desc Differential source memory descriptor. 1695/// \param [out] diff_src Pointer to differential source data. 1696/// \param [in] diff_summand_desc Differential summand memory descriptor. 1697/// \param [out] diff_summand Pointer to differential summand data. 1698/// \param [in] alpha_param Value to scaling factors used to scale the computed 1699/// parameter value. 1700/// \param [in] diff_scale_bias_mean_var_desc Differential scale, bias, mean, 1701/// variance memory descriptor. 1702/// \param [in] scale Pointer to scale data. 1703/// \param [in] bias Pointer to bias data. 1704/// \param [in] beta_param Value to scaling factors used to scale the prior value 1705/// in the parameter memory. 1706/// \param [out] diff_scale Pointer to differential scale data. 1707/// \param [out] diff_bias Pointer to differential bias data. 1708/// \param [in] saved_mean Pointer to optional cache saved mean data in forward. 1709/// \param [in] saved_var Pointer to optional cache saved variance data in forward. 1710/// \param [in] workspace_size Size of workspace. 1711/// \param [in] workspace Pointer to workspace used for backward propagation. 1712/// \returns An event representing the batch normalization backward operations. 1713sycl::event async_batch_normalization_backward ( 1714batch_normalization_mode mode , batch_normalization_ops ops , 1715activation_desc & adesc , float epsilon , float alpha_data , 1716const memory_desc_ext & src_desc , void * src , 1717const memory_desc_ext & dst_desc , void * dst , 1718const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta_data , 1719const memory_desc_ext & diff_src_desc , void * diff_src , 1720const memory_desc_ext & diff_summand_desc , void * diff_summand , 1721float alpha_param , const memory_desc_ext & diff_scale_bias_mean_var_desc , 1722void * scale , void * bias , float beta_param , void * diff_scale , 1723void * diff_bias , void * saved_mean , void * saved_var , 1724size_t workspace_size , void * workspace ); 1725 1726/// Computing the gradient of a specified batch normalization function 1727/// asynchronously. 1728/// \param [in] mode Batch normalization mode. 1729/// \param [in] ops Batch normalization operation mode. This mode can set to 1730/// perform only batch normalization, or batch normalization followed by 1731/// activation, or batch normalization followed by element-wise addition and 1732/// activation. 1733/// \param [in] adesc Activation operation descriptor. 1734/// \param [in] epsilon Epsilon value used in computation. 1735/// \param [in] alpha_data Value to scaling factors used to scale the computed 1736/// data value. 1737/// \param [in] src_desc Source memory descriptor. 1738/// \param [in] src Pointer to source data. 1739/// \param [in] dst_desc Destination memory descriptor. 1740/// \param [in] dst Pointer to destination data. 1741/// \param [in] diff_dst_desc Differential destination memory descriptor. 1742/// \param [in] diff_dst Pointer to differential destination data. 1743/// \param [in] beta_data Value to scaling factors used to scale the prior value 1744/// in the data memory. 1745/// \param [in] diff_src_desc Differential source memory descriptor. 1746/// \param [out] diff_src Pointer to differential source data. 1747/// \param [in] diff_summand_desc Differential summand memory descriptor. 1748/// \param [out] diff_summand Pointer to differential summand data. 1749/// \param [in] alpha_param Value to scaling factors used to scale the computed 1750/// parameter value. 1751/// \param [in] diff_scale_bias_desc Differential scale, bias memory descriptor. 1752/// \param [in] scale Pointer to scale data. 1753/// \param [in] bias Pointer to bias data. 1754/// \param [in] beta_param Value to scaling factors used to scale the prior value 1755/// in the parameter memory. 1756/// \param [out] diff_scale Pointer to differential scale data. 1757/// \param [out] diff_bias Pointer to differential bias data. 1758/// \param [in] mean_var_desc Differential mean, variance memory descriptor. 1759/// \param [in] saved_mean Pointer to optional cache saved mean data in forward. 1760/// \param [in] saved_var Pointer to optional cache saved variance data in forward. 1761/// \param [in] workspace_size Size of workspace. 1762/// \param [in] workspace Pointer to workspace used for backward propagation. 1763/// \returns An event representing the batch normalization backward operations. 1764sycl::event async_batch_normalization_backward ( 1765batch_normalization_mode mode , batch_normalization_ops ops , 1766activation_desc & adesc , float epsilon , float alpha_data , 1767const memory_desc_ext & src_desc , void * src , const memory_desc_ext & dst_desc , 1768void * dst , const memory_desc_ext & diff_dst_desc , void * diff_dst , 1769float beta_data , const memory_desc_ext & diff_src_desc , void * diff_src , 1770const memory_desc_ext & diff_summand_desc , void * diff_summand , 1771float alpha_param , const memory_desc_ext & diff_scale_bias_desc , void * scale , 1772void * bias , float beta_param , void * diff_scale , void * diff_bias , 1773const memory_desc_ext & mean_var_desc , void * saved_mean , void * saved_var , 1774size_t workspace_size , void * workspace ); 1775 1776/// Computing a specified convolution function value asynchronously. 1777/// \param [in] desc Convolution descriptor. 1778/// \param [in] alg Convolution algorithm. 1779/// \param [in] alpha Value to scaling factors used to scale the computed 1780/// value. 1781/// \param [in] src_desc Source memory descriptor. 1782/// \param [in] src Pointer to source data. 1783/// \param [in] weight_desc Weight memory descriptor. 1784/// \param [in] weight Pointer to weight data. 1785/// \param [in] beta Value to scaling factors used to scale the prior value 1786/// in the destination memory. 1787/// \param [in] dst_desc Destination memory descriptor. 1788/// \param [out] dst Pointer to destination data. 1789/// \returns An event representing the convolution forward operations. 1790sycl::event async_convolution_forward (convolution_desc & desc , ::dnnl::algorithm alg , 1791float alpha , const memory_desc_ext & src_desc , 1792void * src , const memory_desc_ext & weight_desc , 1793void * weight , float beta , 1794const memory_desc_ext & dst_desc , void * dst ); 1795 1796/// Computing a specified convolution function value asynchronously. 1797/// \param [in] desc Convolution descriptor. 1798/// \param [in] alg Convolution algorithm. 1799/// \param [in] adesc Activation operation descriptor. 1800/// \param [in] alpha_0 Value to scaling factors used to scale the data 1801/// value. 1802/// \param [in] src_desc Source memory descriptor. 1803/// \param [in] src Pointer to source data. 1804/// \param [in] weight_desc Weight memory descriptor. 1805/// \param [in] weight Pointer to weight data. 1806/// \param [in] alpha_1 Value to scaling factors used to scale the summand 1807/// value. 1808/// \param [in] summand_desc Summand memory descriptor. 1809/// \param [in] summand Pointer to summand data. 1810/// \param [in] bias_desc Bias memory descriptor. 1811/// \param [in] bias Pointer to bias data. 1812/// \param [in] dst_desc Destination memory descriptor. 1813/// \param [out] dst Pointer to destination data. 1814/// \returns An event representing the convolution forward operations. 1815sycl::event async_convolution_forward ( 1816convolution_desc & desc , ::dnnl::algorithm alg , activation_desc & adesc , 1817float alpha_0 , const memory_desc_ext & src_desc , void * src , 1818const memory_desc_ext & weight_desc , void * weight , float alpha_1 , 1819const memory_desc_ext & summand_desc , void * summand , 1820const memory_desc_ext & bias_desc , void * bias , 1821const memory_desc_ext & dst_desc , void * dst ); 1822 1823/// Computing the data gradient of a specified convolution function asynchronously. 1824/// \param [in] desc Convolution descriptor. 1825/// \param [in] alg Convolution algorithm. 1826/// \param [in] alpha Value to scaling factors used to scale the computed 1827/// value. 1828/// \param [in] weight_desc Weight memory descriptor. 1829/// \param [in] weight Pointer to weight data. 1830/// \param [in] diff_dst_desc Differential destination memory descriptor. 1831/// \param [in] diff_dst Pointer to differential destination data. 1832/// \param [in] beta Value to scaling factors used to scale the prior value 1833/// in the destination memory. 1834/// \param [in] diff_src_desc Differential source memory descriptor. 1835/// \param [out] diff_src Pointer to differential source data. 1836/// \returns An event representing the convolution backward data operations. 1837sycl::event async_convolution_backward_data ( 1838convolution_desc & desc , ::dnnl::algorithm alg , float alpha , 1839const memory_desc_ext & weight_desc , void * weight , 1840const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta , 1841const memory_desc_ext & diff_src_desc , void * diff_src ); 1842 1843/// Computing the weight gradient of a specified convolution function 1844/// asynchronously. 1845/// \param [in] desc Convolution descriptor. 1846/// \param [in] alg Convolution algorithm. 1847/// \param [in] alpha Value to scaling factors used to scale the computed 1848/// value. 1849/// \param [in] src_desc Source memory descriptor. 1850/// \param [in] src Pointer to source data. 1851/// \param [in] diff_dst_desc Differential destination memory descriptor. 1852/// \param [in] diff_dst Pointer to differential destination data. 1853/// \param [in] beta Value to scaling factors used to scale the prior value 1854/// in the destination memory. 1855/// \param [in] diff_weight_desc Differential weight memory descriptor. 1856/// \param [out] diff_weight Pointer to differential weight data. 1857/// \returns An event representing the convolution backward weight operations. 1858sycl::event async_convolution_backward_weight ( 1859convolution_desc & desc , ::dnnl::algorithm alg , float alpha , 1860const memory_desc_ext & src_desc , void * src , 1861const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta , 1862const memory_desc_ext & diff_weight_desc , void * diff_weight ); 1863 1864/// Computing the bias gradient of a specified convolution function 1865/// asynchronously. 1866/// \param [in] alpha Value to scaling factors used to scale the computed 1867/// value. 1868/// \param [in] diff_dst_desc Differential destination memory descriptor. 1869/// \param [in] diff_dst Pointer to differential destination data. 1870/// \param [in] beta Value to scaling factors used to scale the prior value 1871/// in the destination memory. 1872/// \param [in] diff_bias_desc Differential bias memory descriptor. 1873/// \param [out] diff_bias Pointer to differential bias data. 1874/// \returns An event representing the convolution backward bias operations. 1875sycl::event async_convolution_backward_bias (float alpha , 1876const memory_desc_ext & diff_dst_desc , 1877void * diff_dst , float beta , 1878const memory_desc_ext & diff_bias_desc , 1879void * diff_bias ); 1880 1881/// Getting the required weight space size for specified rnn operation. 1882/// \param [in] desc RNN descriptor. 1883/// \param [out] weight_space_size Size of required weight space. 1884void rnn_get_weight_space_size ( const rnn_desc & desc , 1885size_t * weight_space_size ); 1886 1887/// Getting the required scratchpad size and workspace size for specified rnn operation. 1888/// \param [in] desc RNN descriptor. 1889/// \param [in] kind Propagation kind. 1890/// \param [in] src_desc Source memory descriptor. 1891/// \param [out] scratchpad_size Size of required scratchpad. 1892/// \param [out] workspace_size Size of required workspace. 1893void rnn_get_scratchpad_workspace_size ( const rnn_desc & desc , ::dnnl::prop_kind kind , 1894const memory_desc_ext & src_desc , 1895size_t * scratchpad_size , size_t * workspace_size ); 1896 1897/// Computing a specified rnn function value asynchronously. 1898/// \param [in] desc RNN descriptor. 1899/// \param [in] kind Propagation kind. 1900/// \param [in] src_desc Source memory descriptor. 1901/// \param [in] src Pointer to source data. 1902/// \param [in] dst_desc Destination memory descriptor. 1903/// \param [out] dst Pointer to destination data. 1904/// \param [in] iter_desc Recurrent hidden state data memory descriptor. 1905/// \param [in] src_iter Pointer to input recurrent hidden state data. 1906/// \param [in] dst_iter Pointer to output recurrent hidden state data. 1907/// \param [in] iter_c_desc Recurrent cell state data memory descriptor. 1908/// \param [in] src_c_iter Pointer to input recurrent cell state data. 1909/// \param [in] dst_c_iter Pointer to output recurrent cell state data. 1910/// \param [in] weight_size Size of weight memory. 1911/// \param [in] weight Pointer to weight data. 1912/// \param [in] scratchpad_size Size of scratchpad memory. 1913/// \param [in] scratchpad Pointer to scratchpad data. 1914/// \param [in] workspace_size Size of workspace memory. 1915/// \param [in] workspace Pointer to workspace data. 1916/// \returns An event representing the status of rnn forward operations. 1917sycl::event async_rnn_forward ( const rnn_desc & desc , ::dnnl::prop_kind kind , 1918const memory_desc_ext & src_desc , void * src , 1919const memory_desc_ext & dst_desc , void * dst , 1920const memory_desc_ext & iter_desc , void * src_iter , 1921void * dst_iter , 1922const memory_desc_ext & iter_c_desc , 1923void * src_iter_c , void * dst_iter_c , 1924size_t weight_size , void * weight , 1925size_t scratchpad_size , void * scratchpad , 1926size_t workspace_size , void * workspace ); 1927 1928/// Computing the data and weight gradient of a specified rnn function 1929/// asynchronously. 1930/// \param [in] desc RNN descriptor. 1931/// \param [in] dst_desc Destination memory descriptor. 1932/// \param [in] dst Pointer to destination data. 1933/// \param [in] diff_dst Pointer to differential destination data. 1934/// \param [in] src_desc Source memory descriptor. 1935/// \param [in] src Pointer to source data. 1936/// \param [out] diff_src Pointer to differential source data. 1937/// \param [in] iter_desc Recurrent hidden state data memory descriptor. 1938/// \param [in] src_iter Pointer to input recurrent hidden state data. 1939/// \param [in] diff_dst_iter Pointer to differential output recurrent hidden state data. 1940/// \param [out] diff_src_iter Pointer to differential input recurrent hidden state data. 1941/// \param [in] iter_c_desc Recurrent cell state data memory descriptor. 1942/// \param [in] src_c_iter Pointer to input recurrent cell state data. 1943/// \param [in] diff_dst_c_iter Pointer to differential output recurrent cell state data. 1944/// \param [out] diff_src_c_iter Pointer to differential input recurrent cell state data. 1945/// \param [in] weight_size Size of weight memory. 1946/// \param [in] weight Pointer to weight data. 1947/// \param [out] diff_weight Pointer to differential weight data. 1948/// \param [in] scratchpad_size Size of scratchpad memory. 1949/// \param [in] scratchpad Pointer to scratchpad data. 1950/// \param [in] workspace_size Size of workspace memory. 1951/// \param [in] workspace Pointer to workspace data. 1952/// \returns An event representing the status of rnn backward operations. 1953sycl::event async_rnn_backward ( 1954const rnn_desc & desc , const memory_desc_ext & dst_desc , void * dst , 1955void * diff_dst , const memory_desc_ext & src_desc , void * src , 1956void * diff_src , const memory_desc_ext & iter_desc , void * src_iter , 1957void * diff_dst_iter , void * diff_src_iter , 1958const memory_desc_ext & iter_c_desc , void * src_iter_c , 1959void * diff_dst_iter_c , void * diff_src_iter_c , size_t weight_size , 1960void * weight , void * diff_weight , size_t scratchpad_size , void * scratchpad , 1961size_t workspace_size , void * workspace ); 1962 1963/// Getting the required state size for specified dropout operation. 1964/// \param [in] src_desc Source memory descriptor. 1965/// \returns Required size of state. 1966size_t get_dropout_state_size (); 1967 1968/// Getting the required workspace size for dropout operation. 1969/// \param [in] src_desc Source memory descriptor. 1970/// \returns Required size of workspace. 1971static size_t get_dropout_workspace_size ( const memory_desc_ext & src_desc ); 1972 1973/// Computing a specified dropout function value asynchronously. 1974/// \param [in] desc Dropout descriptor. 1975/// \param [in] src_desc Source memory descriptor. 1976/// \param [in] src Pointer to source data. 1977/// \param [in] dst_desc Destination memory descriptor. 1978/// \param [out] dst Pointer to destination data. 1979/// \param [in] workspace Pointer to workspace data. 1980/// \param [in] workspace_size Size of workspace memory. 1981/// \returns An event representing the dropout forward operations. 1982sycl::event async_dropout_forward (dropout_desc & desc , 1983const memory_desc_ext & src_desc , void * src , 1984const memory_desc_ext & dst_desc , void * dst , 1985void * workspace , size_t workspace_size ); 1986 1987/// Computing the gradient of a specified dropout function asynchronously. 1988/// \param [in] desc Dropout descriptor. 1989/// \param [in] diff_dst_desc Differential destination memory descriptor. 1990/// \param [in] diff_dst Pointer to differential destination data. 1991/// \param [in] diff_src_desc Differential source memory descriptor. 1992/// \param [out] diff_src Pointer to differential source data. 1993/// \param [in] workspace Pointer to workspace data. 1994/// \param [in] workspace_size Size of workspace memory. 1995/// \returns An event representing the dropout backward operations. 1996sycl::event async_dropout_backward (dropout_desc & desc , 1997const memory_desc_ext & diff_dst_desc , 1998void * diff_dst , 1999const memory_desc_ext & diff_src_desc , 2000void * diff_src , void * workspace , 2001size_t workspace_size ); 2002}; 2003 2004inline thread_local unsigned int engine_ext:: _engine_count ; 2005inline thread_local detail::primitive_cache engine_ext:: _primitive_cache ; 2006inline thread_local std::map < void * , ::dnnl::memory > engine_ext:: _workspace_map ; 2007inline thread_local std::map < sycl::queue * , 2008std::shared_ptr < engine_ext::internal_resource >> 2009engine_ext:: _internal_resource_cache ; 2010 2011inline 2012void dropout_desc:: restore (engine_ext & engine , float p , void * state , 2013size_t state_size , unsigned long long seed ) { 2014#ifndef __INTEL_MKL__ 2015throw std:: runtime_error ( "The oneAPI Math Kernel Library (oneMKL) " 2016"Interfaces Project does not support this API." ); 2017#else 2018if ( state ) { 2019std::int64_t required_state_size = engine . get_dropout_state_size (); 2020if ( state_size < required_state_size ) { 2021throw std:: runtime_error ( "restore: state_size less than required state size." ); 2022} 2023sycl::queue * q = engine . get_queue (); 2024_imp -> _p = p ; 2025_imp -> _seed = seed ; 2026_imp -> _state = state ; 2027_imp -> _host_state = std:: vector < std::uint8_t > ( required_state_size ); 2028q -> memcpy ( _imp -> _host_state . data (), _imp -> _state , required_state_size ). wait (); 2029_imp -> _rng_engine = 2030oneapi::mkl::rng:: load_state < rng_engine_t > ( 2031* q , _imp -> _host_state . data ()); 2032} 2033#endif 2034} 2035 2036inline 2037void dropout_desc:: set (engine_ext & engine , float p , void * state , 2038size_t state_size , unsigned long long seed ) { 2039#ifndef __INTEL_MKL__ 2040throw std:: runtime_error ( "The oneAPI Math Kernel Library (oneMKL) " 2041"Interfaces Project does not support this API." ); 2042#else 2043_imp -> _p = p ; 2044if ( state ) { 2045std::int64_t required_state_size = engine . get_dropout_state_size (); 2046if ( state_size < required_state_size ) { 2047throw std:: runtime_error ( "set: no sufficient memory to save states." ); 2048} 2049sycl::queue * q = engine . get_queue (); 2050_imp -> _seed = seed ; 2051_imp -> _state = state ; 2052_imp -> _host_state = std:: vector < std::uint8_t > ( required_state_size ); 2053_imp -> _rng_engine = rng_engine_t ( * q , seed ); 2054oneapi::mkl::rng:: save_state ( _imp -> _rng_engine , _imp -> _host_state . data ()); 2055q -> memcpy ( _imp -> _state , _imp -> _host_state . data (), required_state_size ). wait (); 2056} 2057#endif 2058} 2059 2060inline 2061::dnnl::memory::data_type 2062memory_desc_ext:: to_dnnl_data_type (dpct::library_data_t dt ) { 2063using dnnl_dt = ::dnnl::memory::data_type; 2064switch ( dt ) { 2065case dpct::library_data_t:: real_half : 2066return dnnl_dt:: f16 ; 2067case dpct::library_data_t:: real_bfloat16 : 2068return dnnl_dt:: bf16 ; 2069case dpct::library_data_t:: real_float : 2070return dnnl_dt:: f32 ; 2071case dpct::library_data_t:: real_double : 2072return dnnl_dt:: f64 ; 2073case dpct::library_data_t:: real_int32 : 2074return dnnl_dt:: s32 ; 2075case dpct::library_data_t:: real_int8 : 2076return dnnl_dt:: s8 ; 2077case dpct::library_data_t:: real_uint8 : 2078return dnnl_dt:: u8 ; 2079case dpct::library_data_t:: real_int8_4 : 2080return dnnl_dt:: s8 ; 2081case dpct::library_data_t:: real_int8_32 : 2082return dnnl_dt:: s8 ; 2083case dpct::library_data_t:: real_uint8_4 : 2084return dnnl_dt:: u8 ; 2085default : 2086throw std:: runtime_error ( "to_dnnl_data_type: unsupported data type." ); 2087} 2088} 2089 2090inline 2091dpct::library_data_t 2092memory_desc_ext:: to_dpct_library_data_t (::dnnl::memory::data_type dt , 2093unsigned block_size ) { 2094using dpct_dt = dpct::library_data_t; 2095using dnnl_dt = ::dnnl::memory::data_type; 2096switch ( dt ) { 2097case dnnl_dt:: f16 : 2098return dpct_dt:: real_half ; 2099case dnnl_dt:: bf16 : 2100return dpct_dt:: real_bfloat16 ; 2101case dnnl_dt:: f32 : 2102return dpct_dt:: real_float ; 2103case dnnl_dt:: f64 : 2104return dpct_dt:: real_double ; 2105case dnnl_dt:: s32 : 2106return dpct_dt:: real_int32 ; 2107case dnnl_dt:: s8 : 2108if ( block_size == 4 ) { 2109return dpct_dt:: real_int8_4 ; 2110} else if ( block_size == 32 ) { 2111return dpct_dt:: real_int8_32 ; 2112} else { 2113return dpct_dt:: real_int8 ; 2114} 2115case dnnl_dt:: u8 : 2116if ( block_size == 4 ) { 2117return dpct_dt:: real_uint8_4 ; 2118} else { 2119return dpct_dt:: real_uint8 ; 2120} 2121default : 2122throw std:: runtime_error ( "to_dpct_library_data_t: unsupported data type " 2123"dnnl::memory::data_type::undef." ); 2124} 2125} 2126 2127inline 2128::dnnl::memory::format_tag 2129memory_desc_ext:: to_dnnl_format_tag (dpct::library_data_t dt , 2130memory_format_tag tag ) { 2131using dpct_dt = dpct::library_data_t; 2132using dpct_tag = memory_format_tag; 2133using dnnl_tag = ::dnnl::memory::format_tag; 2134switch ( tag ) { 2135case dpct_tag:: nchw : 2136return dnnl_tag:: nchw ; 2137case dpct_tag:: nhwc : 2138return dnnl_tag:: nhwc ; 2139default : 2140if ( dt == dpct_dt:: real_int8_32 ) { 2141return dnnl_tag:: nChw32c ; 2142} else { 2143return dnnl_tag:: nChw4c ; 2144} 2145} 2146} 2147 2148inline 2149void memory_desc_ext:: set (memory_format_tag tag , dpct::library_data_t dt , int n , 2150int c , int h , int w ) { 2151_desc = ::dnnl::memory:: desc ({ n , c , h , w }, to_dnnl_data_type ( dt ), 2152to_dnnl_format_tag ( dt , tag )); 2153} 2154 2155inline 2156void memory_desc_ext:: set (dpct::library_data_t dt , int n , int c , int h , int w , 2157int n_stride , int c_stride , int h_stride , 2158int w_stride ) { 2159_desc = ::dnnl::memory:: desc ({ n , c , h , w }, to_dnnl_data_type ( dt ), 2160{ n_stride , c_stride , h_stride , w_stride }); 2161} 2162 2163inline 2164void memory_desc_ext:: set (dpct::library_data_t dt , int ndims , const int dims [], 2165const int strides []) { 2166_desc = ::dnnl::memory:: desc ({ dims , dims + ndims }, to_dnnl_data_type ( dt ), 2167{ strides , strides + ndims }); 2168} 2169 2170inline 2171void memory_desc_ext:: set (memory_format_tag tag , dpct::library_data_t dt , 2172int ndims , const int dims []) { 2173_desc = ::dnnl::memory:: desc ({ dims , dims + ndims }, to_dnnl_data_type ( dt ), 2174to_dnnl_format_tag ( dt , tag )); 2175} 2176 2177inline 2178void memory_desc_ext:: set (rnn_memory_format_tag tag , dpct::library_data_t dt , 2179int t , int n , int c ) { 2180if ( tag == rnn_memory_format_tag:: tnc ) { 2181_desc = ::dnnl::memory:: desc ({ t , n , c }, to_dnnl_data_type ( dt ), 2182::dnnl::memory::format_tag:: tnc ); 2183} else if ( tag == rnn_memory_format_tag:: ntc ) { 2184_desc = ::dnnl::memory:: desc ({ t , n , c }, to_dnnl_data_type ( dt ), 2185::dnnl::memory::format_tag:: ntc ); 2186} else { 2187throw std:: runtime_error ( "set: unsupported memory format tag." ); 2188} 2189} 2190 2191inline 2192void memory_desc_ext:: get (dpct::library_data_t * dt , int * n , int * c , int * h , 2193int * w , int * n_stride , int * c_stride , int * h_stride , 2194int * w_stride ) const { 2195unsigned block_size = 1 ; 2196auto dims = _desc . get_dims (); 2197auto inner_blks = _desc . get_inner_blks (); 2198auto strides = _desc . get_strides (); 2199if (! inner_blks . empty ()) { 2200block_size = inner_blks [ 0 ]; 2201} 2202 2203* dt = to_dpct_library_data_t ( _desc . get_data_type (), block_size ); 2204* n = dims [ 0 ]; 2205* c = dims [ 1 ]; 2206* h = dims [ 2 ]; 2207* w = dims [ 3 ]; 2208* n_stride = strides [ 0 ] / block_size ; 2209* c_stride = strides [ 1 ] / block_size ; 2210* h_stride = strides [ 2 ] / block_size ; 2211* w_stride = strides [ 3 ] / block_size ; 2212} 2213 2214inline 2215void memory_desc_ext:: get (dpct::library_data_t * dt , memory_format_tag * tag , 2216int * n , int * c , int * h , int * w ) const { 2217unsigned block_size = 1 ; 2218* tag = memory_format_tag:: nchw ; 2219auto dims = _desc . get_dims (); 2220auto strides = _desc . get_strides (); 2221auto inner_blks = _desc . get_inner_blks (); 2222if (! inner_blks . empty ()) { 2223block_size = inner_blks [ 0 ]; 2224* tag = memory_format_tag:: nchw_blocked ; 2225} 2226if ( strides [ 1 ] == 1 && dims [ 1 ] != 1 ) { 2227* tag = memory_format_tag:: nhwc ; 2228} 2229* dt = to_dpct_library_data_t ( _desc . get_data_type (), block_size ); 2230* n = dims [ 0 ]; 2231* c = dims [ 1 ]; 2232* h = dims [ 2 ]; 2233* w = dims [ 3 ]; 2234} 2235 2236inline 2237void memory_desc_ext:: get (dpct::library_data_t * dt , rnn_memory_format_tag * tag , 2238int * t , int * n , int * c ) const { 2239auto dims = _desc . get_dims (); 2240auto strides = _desc . get_strides (); 2241 2242if ( strides [ 0 ] >= strides [ 1 ]) { 2243* tag = rnn_memory_format_tag:: tnc ; 2244} else { 2245* tag = rnn_memory_format_tag:: ntc ; 2246} 2247 2248* dt = to_dpct_library_data_t ( _desc . get_data_type (), 1 ); 2249* t = dims [ 0 ]; 2250* n = dims [ 1 ]; 2251* c = dims [ 2 ]; 2252} 2253 2254inline 2255void memory_desc_ext:: get (int requested_ndims , dpct::library_data_t * dt , 2256int * ndims , int dims [], int strides []) const { 2257unsigned block_size = 1 ; 2258auto inner_blks = _desc . get_inner_blks (); 2259auto adims = _desc . get_dims (); 2260auto astrides = _desc . get_strides (); 2261if (! inner_blks . empty ()) { 2262block_size = inner_blks [ 0 ]; 2263} 2264* dt = to_dpct_library_data_t ( _desc . get_data_type (), block_size ); 2265* ndims = _desc . get_ndims (); 2266for (int index = 0 ; index < requested_ndims ; index ++ ) { 2267dims [ index ] = adims [ index ]; 2268strides [ index ] = 2269astrides [ index ] / block_size ; 2270} 2271} 2272 2273inline 2274void memory_desc_ext:: get (int requested_ndims , dpct::library_data_t * dt , 2275memory_format_tag * tag , int * ndims , 2276int dims []) const { 2277unsigned block_size = 1 ; 2278* tag = memory_format_tag:: nchw ; 2279auto inner_blks = _desc . get_inner_blks (); 2280auto adims = _desc . get_dims (); 2281auto astrides = _desc . get_strides (); 2282if (! inner_blks . empty ()) { 2283block_size = inner_blks [ 0 ]; 2284* tag = memory_format_tag:: nchw_blocked ; 2285} 2286if ( astrides [ 1 ] == 1 && 2287adims [ 1 ] != 1 ) { 2288* tag = memory_format_tag:: nhwc ; 2289} 2290* dt = to_dpct_library_data_t ( _desc . get_data_type (), block_size ); 2291* ndims = _desc . get_ndims (); 2292for (int index = 0 ; index < requested_ndims ; index ++ ) { 2293dims [ index ] = adims [ index ]; 2294} 2295} 2296 2297inline 2298void engine_ext:: get_rnn_configuration ( const ::dnnl::memory::desc & desc , 2299rnn_direction direction , rnn_mode mode , 2300dpct::library_data_t dt , int hidden_size , 2301::dnnl::memory::data_type * dnnl_dt , 2302::dnnl::memory::format_tag * tag , 2303int * projection_size , int * output_size , 2304int * seq_length , int * batch_size , 2305int * direction_num , int * gate_num ) { 2306if (! desc . is_zero ()) { 2307auto dims = desc . get_dims (); 2308auto strides = desc . get_strides (); 2309if ( strides [ 0 ] >= strides [ 1 ]) { 2310* tag = ::dnnl::memory::format_tag:: tnc ; 2311* seq_length = dims [ 0 ]; 2312* batch_size = dims [ 1 ]; 2313} else { 2314* tag = ::dnnl::memory::format_tag:: ntc ; 2315* seq_length = dims [ 1 ]; 2316* batch_size = dims [ 0 ]; 2317} 2318} 2319if ( direction == rnn_direction:: bidirectional ) { 2320* direction_num = 2 ; 2321} else { 2322* direction_num = 1 ; 2323} 2324if ( mode == rnn_mode:: lstm ) { 2325* gate_num = 4 ; 2326} else if ( mode == rnn_mode:: gru ) { 2327* gate_num = 3 ; 2328} else { 2329* gate_num = 1 ; 2330} 2331if ( * projection_size != hidden_size ) { 2332* output_size = * projection_size ; 2333} else { 2334* projection_size = 0 ; 2335* output_size = hidden_size ; 2336} 2337* dnnl_dt = memory_desc_ext:: to_dnnl_data_type ( dt ); 2338} 2339 2340inline 2341void * engine_ext:: allocate ( const memory_desc_ext & data_desc , int count ) { 2342return allocate ( data_desc . get_size () * count ); 2343} 2344 2345inline 2346void * engine_ext:: allocate (size_t size ) { 2347auto & Info = get_internal_resource ( _q ) -> binfo ; 2348uint8_t * result = Info . buffer + Info . usage ; 2349Info . usage += size ; 2350return result ; 2351} 2352 2353inline 2354void engine_ext:: transform_no_zero ( const memory_desc_ext & desc , void * src , void * dst ) { 2355::dnnl::memory::data_type dt = desc . get_desc (). get_data_type (); 2356size_t element_num = desc . get_element_num (); 2357switch ( dt ) { 2358case ::dnnl::memory::data_type:: f32 : 2359transform_no_zero_with_type < float > ( _q , src , dst , element_num ); 2360break ; 2361case ::dnnl::memory::data_type:: f16 : 2362transform_no_zero_with_type < sycl::half > ( _q , src , dst , element_num ); 2363break ; 2364case ::dnnl::memory::data_type:: s32 : 2365transform_no_zero_with_type < int32_t > ( _q , src , dst , element_num ); 2366break ; 2367case ::dnnl::memory::data_type:: s8 : 2368transform_no_zero_with_type < int8_t > ( _q , src , dst , element_num ); 2369break ; 2370case ::dnnl::memory::data_type:: u8 : 2371transform_no_zero_with_type < uint8_t > ( _q , src , dst , element_num ); 2372break ; 2373default : 2374throw std:: runtime_error ( "transform_no_zero: unsupported data type." ); 2375} 2376} 2377 2378inline 2379::dnnl::memory::desc 2380engine_ext:: get_group_weight_desc (int group_count , 2381const memory_desc_ext & weight_desc ) { 2382if ( group_count == 1 ) { 2383return weight_desc . get_desc (); 2384} 2385auto help_weight_desc = weight_desc . get_desc (); 2386int ndims = help_weight_desc . get_ndims (); 2387if (! help_weight_desc . get_inner_blks (). empty ()) { 2388throw std:: runtime_error ( "get_group_weight_desc: group convolution with " 2389"blocked weight memory unimplemented." ); 2390} 2391std::vector < int64_t > new_size ; 2392auto old_size = weight_desc . get_dims (); 2393new_size . push_back ( group_count ); 2394new_size . push_back ( old_size [ 0 ] / group_count ); 2395for (int index = 1 ; index < old_size . size (); index ++ ) { 2396new_size . push_back ( old_size [ index ]); 2397} 2398std::vector < int64_t > strides = help_weight_desc . get_strides (); 2399::dnnl::memory::format_tag tag ; 2400bool is_nhwc = ( strides [ 1 ] == 1 && old_size [ 1 ] != 1 ); 2401 2402if ( ndims == 4 ) { 2403if ( is_nhwc ) { 2404tag = ::dnnl::memory::format_tag:: gohwi ; 2405} else { 2406tag = ::dnnl::memory::format_tag:: goihw ; 2407} 2408} else if ( ndims == 5 ) { 2409if ( is_nhwc ) { 2410tag = ::dnnl::memory::format_tag:: godhwi ; 2411} else { 2412tag = ::dnnl::memory::format_tag:: goidhw ; 2413} 2414} 2415 2416help_weight_desc = 2417::dnnl::memory:: desc ( new_size , weight_desc . get_desc (). get_data_type (), tag ); 2418return help_weight_desc ; 2419} 2420 2421inline 2422::dnnl::memory::desc engine_ext:: compress_spatial_dimensions_to_channel ( 2423const ::dnnl::memory::desc & desc ) { 2424int ndims = desc . get_ndims (); 2425auto dims = desc . get_dims (); 2426auto inner_blks = desc . get_inner_blks (); 2427assert ( ndims >= 4 && "ndims is at least 4." ); 2428std::vector < int64_t > compressed_dims (ndims); 2429compressed_dims [ 0 ] = dims [ 0 ]; 2430compressed_dims [ 1 ] = dims [ 1 ]; 2431for (int index = 2 ; index < ndims ; index ++ ) { 2432compressed_dims [ 1 ] = compressed_dims [ 1 ] * dims [ index ]; 2433compressed_dims [ index ] = 1 ; 2434} 2435if (! inner_blks . empty () && inner_blks [ 0 ] == 4 ) { 2436return ::dnnl::memory:: desc ( compressed_dims , desc . get_data_type (), 2437::dnnl::memory::format_tag:: nChw4c ); 2438} else if (! inner_blks . empty () && inner_blks [ 0 ] == 32 ) { 2439return ::dnnl::memory:: desc ( compressed_dims , desc . get_data_type (), 2440::dnnl::memory::format_tag:: nChw32c ); 2441} 2442std::vector < int64_t > strides ( ndims , 1 ); 2443strides [ 0 ] = compressed_dims [ 1 ]; 2444 2445return ::dnnl::memory:: desc ( compressed_dims , desc . get_data_type (), strides ); 2446} 2447 2448inline 2449::dnnl::memory::desc 2450engine_ext:: get_bn_scale_bias_mean_var_desc ( const ::dnnl::memory::desc & desc , 2451batch_normalization_mode mode ) { 2452int ndims = desc . get_ndims (); 2453auto dims = desc . get_dims (); 2454assert ( ndims >= 4 && "ndims is at least 4." ); 2455int channel_num = 1 ; 2456if ( mode == batch_normalization_mode:: spatial ) { 2457channel_num = dims [ 1 ]; 2458} else { 2459for (int index = 1 ; index < ndims ; index ++ ) { 2460channel_num = channel_num * dims [ index ]; 2461} 2462} 2463return ::dnnl::memory:: desc ({ channel_num }, desc . get_data_type (), 2464::dnnl::memory::format_tag:: a ); 2465} 2466 2467inline 2468::dnnl::memory::desc engine_ext:: transfer_memory_desc_to_channel_major_format ( 2469const ::dnnl::memory::desc & desc ) { 2470if (! desc . get_inner_blks (). empty ()) { 2471return desc ; 2472} 2473int ndims = desc . get_ndims (); 2474auto dims = desc . get_dims (); 2475if ( ndims == 4 ) { 2476return ::dnnl::memory:: desc ( dims , desc . get_data_type (), 2477::dnnl::memory::format_tag:: nchw ); 2478} 2479return ::dnnl::memory:: desc ( dims , desc . get_data_type (), 2480::dnnl::memory::format_tag:: ncdhw ); 2481} 2482 2483/// If the alpha = 0 and beta = 1, then the destination (dst = alpha * out + 2484/// beta * prior_dst) have no change. In this case this function returns true 2485/// means the operation can exit directly. 2486inline 2487bool engine_ext:: scale_parameter_preprocess ( 2488const std::vector < output_argument_info > & args ) { 2489bool direct_exit = true; 2490for (auto & arg : args ) { 2491if ( arg . _alpha == 0.f ) { 2492if ( arg . _beta != 1.f ) { 2493async_scale ( arg . _beta , arg . _desc , arg . _data ); 2494} 2495} else { 2496direct_exit = false; 2497} 2498} 2499return direct_exit ; 2500} 2501 2502inline 2503void engine_ext:: derive_batch_normalization_memory_desc ( 2504memory_desc_ext & scale_bias_desc , memory_desc_ext & mean_var_desc , 2505const memory_desc_ext & src_desc , batch_normalization_mode mode ) { 2506derive_batch_normalization_memory_desc ( scale_bias_desc , src_desc , mode ); 2507derive_batch_normalization_memory_desc ( mean_var_desc , src_desc , mode ); 2508} 2509 2510inline 2511void engine_ext:: derive_batch_normalization_memory_desc ( 2512memory_desc_ext & desc , const memory_desc_ext & src_desc , 2513batch_normalization_mode mode ) { 2514int src_ndims = src_desc . get_desc (). get_ndims (); 2515auto inner_blks = src_desc . get_desc (). get_inner_blks (); 2516if ( src_desc . get_desc (). get_ndims () != 4 || 2517src_desc . get_desc (). get_ndims () != 5 ) { 2518throw std:: runtime_error ( "derive_batch_normalization_memory_desc: only 4d " 2519"and 5d memory descriptor supported." ); 2520} 2521std::vector < int64_t > dims = src_desc . get_dims (); 2522dims [ 0 ] = 1 ; 2523if ( mode == batch_normalization_mode:: spatial ) { 2524dims [ 2 ] = 1 ; 2525dims [ 3 ] = 1 ; 2526if ( src_ndims == 5 ) { 2527dims [ 4 ] = 1 ; 2528} 2529} 2530auto data_type = src_desc . get_desc (). get_data_type (); 2531if ( data_type == ::dnnl::memory::data_type:: f16 ) { 2532data_type = ::dnnl::memory::data_type:: f32 ; 2533} 2534if (! inner_blks . empty () && inner_blks [ 0 ] == 4 ) { 2535desc . set_desc (::dnnl::memory:: desc ( dims , data_type , 2536::dnnl::memory::format_tag:: nChw4c )); 2537} else if (! inner_blks . empty () && inner_blks [ 0 ] == 32 ) { 2538desc . set_desc (::dnnl::memory:: desc ( dims , data_type , 2539::dnnl::memory::format_tag:: nChw32c )); 2540} else { 2541if ( src_ndims == 4 ) { 2542desc . set_desc (::dnnl::memory:: desc ( dims , data_type , 2543::dnnl::memory::format_tag:: nchw )); 2544} else { 2545desc . set_desc (::dnnl::memory:: desc ( dims , data_type , 2546::dnnl::memory::format_tag:: ncdhw )); 2547} 2548} 2549} 2550 2551template < typename primitive_type > 2552sycl::event engine_ext:: execute_primitive ( 2553const std::pair < detail::primitive_cache_key_type, detail::primitive_and_args > 2554& primitive , 2555const std::vector < output_argument_info > & output_args ) { 2556std::vector < void *> caches ; 2557int output_arg_num = output_args . size (); 2558for (int i = 0 ; i < output_arg_num ; i ++ ) { 2559if ( output_args [ i ]. _beta != 0.f ) { 2560auto cache = allocate ( output_args [ i ]. _desc ); 2561caches . push_back ( cache ); 2562( * primitive . second . args )[ output_args [ i ]. _name ]. set_data_handle ( cache ); 2563} 2564} 2565 2566auto e = ::dnnl::sycl_interop:: execute ( 2567* ( static_cast < primitive_type *> ( primitive . second . primitive )), * _s , 2568* primitive . second . args ); 2569_primitive_cache . put ( 2570primitive . first , primitive . second . primitive , primitive . second . args , 2571[](::dnnl::primitive * p ) { delete static_cast < primitive_type *> ( p ); }, e , 2572_q ); 2573int cache_index = 0 ; 2574for (int i = 0 ; i < output_arg_num ; i ++ ) { 2575if ( output_args [ i ]. _beta != 0.f ) { 2576e = async_sum ( output_args [ i ]. _alpha , output_args [ i ]. _desc , 2577caches [ cache_index ++ ], output_args [ i ]. _beta , 2578output_args [ i ]. _desc , output_args [ i ]. _data ); 2579} else { 2580if ( output_args [ i ]. _alpha != 1.f ) { 2581e = async_scale ( output_args [ i ]. _alpha , output_args [ i ]. _desc , 2582output_args [ i ]. _data ); 2583} 2584} 2585} 2586return e ; 2587} 2588 2589inline 2590::dnnl::memory::desc engine_ext:: bn_reorder_memory_to_channel_major_format ( 2591bool is_input , ::dnnl::memory::desc & desc , void * src , void ** cache ) { 2592::dnnl::memory::desc result ; 2593result = transfer_memory_desc_to_channel_major_format ( desc ); 2594if (( result != desc ) || ! src ) { 2595* cache = allocate ( desc ); 2596if ( is_input && src ) { 2597async_reorder ( 1.f , desc , src , 0.f , result , * cache ); 2598} 2599} 2600return result ; 2601} 2602 2603inline 2604sycl::event engine_ext:: batch_normalization_backward_internal ( 2605batch_normalization_mode mode , float epsilon , float alpha_data , 2606const memory_desc_ext & src_desc , void * src , 2607const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta_data , 2608const memory_desc_ext & diff_src_desc , void * diff_src , float alpha_param , 2609const memory_desc_ext & diff_scale_bias_desc , void * scale , void * bias , 2610float beta_param , void * diff_scale , void * diff_bias , 2611const memory_desc_ext & mean_var_desc , void * saved_mean , void * saved_var ) { 2612if ( scale_parameter_preprocess ( 2613{{ alpha_data , beta_data , diff_src_desc , diff_src }, 2614{ alpha_param , beta_param , diff_scale_bias_desc , diff_scale }, 2615{ alpha_param , beta_param , diff_scale_bias_desc , diff_bias }})) { 2616return sycl:: event (); 2617} 2618 2619void * reordered_src = nullptr , * reordered_diff_dst = nullptr , 2620* reordered_diff_src = nullptr , * reordered_scale = nullptr , 2621* reordered_bias = nullptr , * reordered_diff_scale = nullptr , 2622* reordered_diff_bias = nullptr , * reordered_saved_mean = nullptr , 2623* reordered_saved_var = nullptr ; 2624 2625::dnnl::memory::desc help_src_desc = src_desc . get_desc (); 2626::dnnl::memory::desc help_diff_dst_desc = diff_dst_desc . get_desc (); 2627::dnnl::memory::desc help_diff_src_desc = diff_src_desc . get_desc (); 2628::dnnl::memory::desc help_diff_scale_bias_desc = 2629diff_scale_bias_desc . get_desc (); 2630::dnnl::memory::desc help_mean_var_desc = mean_var_desc . get_desc (); 2631::dnnl::memory::desc actual_diff_src_desc = help_diff_src_desc ; 2632::dnnl::memory::desc actual_diff_scale_bias_desc = help_diff_scale_bias_desc ; 2633enter_primitive ( 2634help_diff_scale_bias_desc . get_size () * 14 + help_src_desc . get_size () * 2 + 2635help_diff_dst_desc . get_size () * 7 + help_diff_src_desc . get_size () * 5 + 2636help_mean_var_desc . get_size () * 13 ); 2637if ( mode == batch_normalization_mode:: per_activation ) { 2638help_src_desc = bn_reorder_memory_to_channel_major_format (true, help_src_desc , src , 2639& reordered_src ); 2640help_diff_dst_desc = bn_reorder_memory_to_channel_major_format ( 2641true, help_diff_dst_desc , diff_dst , & reordered_diff_dst ); 2642help_diff_src_desc = bn_reorder_memory_to_channel_major_format ( 2643false, help_diff_src_desc , diff_src , & reordered_diff_src ); 2644actual_diff_src_desc = help_diff_src_desc ; 2645help_diff_scale_bias_desc = bn_reorder_memory_to_channel_major_format ( 2646true, help_diff_scale_bias_desc , scale , & reordered_scale ); 2647actual_diff_scale_bias_desc = help_diff_scale_bias_desc ; 2648if ( bias ) { 2649bn_reorder_memory_to_channel_major_format (true, help_diff_scale_bias_desc , bias , 2650& reordered_bias ); 2651} 2652bn_reorder_memory_to_channel_major_format (false, help_diff_scale_bias_desc , 2653diff_scale , & reordered_diff_scale ); 2654bn_reorder_memory_to_channel_major_format (false, help_diff_scale_bias_desc , 2655diff_bias , & reordered_diff_bias ); 2656 2657help_mean_var_desc = bn_reorder_memory_to_channel_major_format ( 2658true, help_mean_var_desc , saved_mean , & reordered_saved_mean ); 2659bn_reorder_memory_to_channel_major_format (true, help_mean_var_desc , saved_var , 2660& reordered_saved_var ); 2661help_src_desc = compress_spatial_dimensions_to_channel ( help_src_desc ); 2662help_diff_src_desc = 2663compress_spatial_dimensions_to_channel ( help_diff_src_desc ); 2664help_diff_dst_desc = 2665compress_spatial_dimensions_to_channel ( help_diff_dst_desc ); 2666} else { 2667if (( help_src_desc != help_diff_dst_desc ) || 2668( help_src_desc != help_diff_src_desc ) || 2669( help_diff_dst_desc != help_diff_src_desc )) { 2670help_src_desc = bn_reorder_memory_to_channel_major_format ( 2671true, help_src_desc , src , & reordered_src ); 2672help_diff_dst_desc = bn_reorder_memory_to_channel_major_format ( 2673true, help_diff_dst_desc , diff_dst , & reordered_diff_dst ); 2674help_diff_src_desc = bn_reorder_memory_to_channel_major_format ( 2675false, help_diff_src_desc , diff_src , & reordered_diff_src ); 2676actual_diff_src_desc = help_diff_src_desc ; 2677} 2678} 2679 2680help_diff_scale_bias_desc = 2681get_bn_scale_bias_mean_var_desc ( help_diff_scale_bias_desc , mode ); 2682help_mean_var_desc = 2683get_bn_scale_bias_mean_var_desc ( help_mean_var_desc , mode ); 2684 2685auto forward_primitive = 2686create_primitive_desc < ::dnnl::batch_normalization_forward > ( 2687::dnnl::prop_kind:: forward_training , help_src_desc , 2688help_diff_dst_desc , epsilon , 2689::dnnl::normalization_flags:: use_scale | 2690::dnnl::normalization_flags:: use_shift ); 2691auto primitive_args = 2692create_primitive_args_or_get < ::dnnl::batch_normalization_backward > ( 2693::dnnl::prop_kind:: backward , help_diff_src_desc , help_diff_dst_desc , 2694help_src_desc , epsilon , 2695::dnnl::normalization_flags:: use_scale | 2696::dnnl::normalization_flags:: use_shift , forward_primitive ); 2697 2698void * dst_cache = nullptr ; 2699if (! saved_mean && ! saved_var ) { 2700dst_cache = allocate ( diff_dst_desc ); 2701if (! reordered_saved_mean ) { 2702reordered_saved_mean = allocate ( mean_var_desc ); 2703} 2704if (! reordered_saved_var ) { 2705reordered_saved_var = allocate ( mean_var_desc ); 2706} 2707if (! bias ) { 2708_q -> fill ( reordered_bias , 0 , diff_scale_bias_desc . get_size ()); 2709} 2710 2711batch_normalization_forward_internal ( 2712true, mode , epsilon , 0.f , 1.f , src_desc , src , 0.f , diff_dst_desc , 2713dst_cache , diff_scale_bias_desc , scale , bias ? bias : reordered_bias , 2714mean_var_desc , reordered_saved_mean , reordered_saved_var , nullptr , 2715nullptr ); 2716} 2717 2718insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , help_src_desc , 2719reordered_src ? reordered_src : src ); 2720insert_arg ( primitive_args . second . args , DNNL_ARG_SCALE , 2721help_diff_scale_bias_desc , 2722reordered_scale ? reordered_scale : scale ); 2723insert_arg ( primitive_args . second . args , DNNL_ARG_MEAN , help_mean_var_desc , 2724reordered_saved_mean ? reordered_saved_mean : saved_mean ); 2725insert_arg ( primitive_args . second . args , DNNL_ARG_VARIANCE , help_mean_var_desc , 2726reordered_saved_var ? reordered_saved_var : saved_var ); 2727insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , help_diff_src_desc , 2728reordered_diff_dst ? reordered_diff_dst : diff_dst ); 2729insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SRC , help_diff_src_desc , 2730reordered_diff_src ? reordered_diff_src : diff_src ); 2731insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SCALE , 2732help_diff_scale_bias_desc , 2733reordered_diff_scale ? reordered_diff_scale : diff_scale ); 2734insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SHIFT , 2735help_diff_scale_bias_desc , 2736reordered_diff_bias ? reordered_diff_bias : diff_bias ); 2737 2738sycl::event e = execute_primitive < ::dnnl::batch_normalization_backward > ( 2739primitive_args , 2740{{ alpha_data , beta_data , DNNL_ARG_DIFF_SRC , help_diff_src_desc , 2741reordered_diff_src ? reordered_diff_src : diff_src }, 2742{ alpha_param , beta_param , DNNL_ARG_DIFF_SCALE , help_diff_scale_bias_desc , 2743reordered_diff_scale ? reordered_diff_scale : diff_scale }, 2744{ alpha_param , beta_param , DNNL_ARG_DIFF_SHIFT , help_diff_scale_bias_desc , 2745reordered_diff_bias ? reordered_diff_bias : diff_bias }}); 2746if ( actual_diff_src_desc != diff_src_desc . get_desc () && reordered_diff_src ) { 2747e = async_reorder ( 1.f , actual_diff_src_desc , reordered_diff_src , 0.f , 2748diff_src_desc , diff_src ); 2749} 2750if ( actual_diff_scale_bias_desc != diff_scale_bias_desc . get_desc () && 2751reordered_diff_scale && reordered_diff_bias ) { 2752async_reorder ( 1.f , actual_diff_scale_bias_desc , reordered_diff_scale , 0.f , 2753diff_scale_bias_desc , diff_scale ); 2754e = async_reorder ( 1.f , actual_diff_scale_bias_desc , reordered_diff_bias , 0.f , 2755diff_scale_bias_desc , diff_bias ); 2756} 2757return exit_primitive ( e ); 2758} 2759 2760inline 2761sycl::event engine_ext:: batch_normalization_forward_internal ( 2762bool is_infer , batch_normalization_mode mode , float epsilon , float factor , 2763float alpha , const memory_desc_ext & src_desc , void * src , float beta , 2764const memory_desc_ext & dst_desc , void * dst , 2765const memory_desc_ext & scale_bias_desc , void * scale , void * bias , 2766const memory_desc_ext & mean_var_desc , void * saved_mean , void * saved_var , 2767void * running_mean , void * running_var ) { 2768if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 2769return sycl:: event (); 2770} 2771enter_primitive ( src_desc . get_size () + 5 * dst_desc . get_size () + 2772scale_bias_desc . get_size () * 2 + 2773mean_var_desc . get_size () * 9 ); 2774void * reordered_src = nullptr , * reordered_dst = nullptr , 2775* reordered_scale = nullptr , * reordered_bias = nullptr , 2776* reordered_saved_mean = nullptr , * reordered_saved_var = nullptr ; 2777::dnnl::memory::desc help_src_desc = src_desc . get_desc (); 2778::dnnl::memory::desc help_dst_desc = dst_desc . get_desc (); 2779::dnnl::memory::desc help_scale_bias_desc = scale_bias_desc . get_desc (); 2780::dnnl::memory::desc help_mean_var_desc = mean_var_desc . get_desc (); 2781::dnnl::memory::desc actual_dst_desc = help_dst_desc ; 2782::dnnl::memory::desc actual_mean_var_desc = help_mean_var_desc ; 2783 2784if ( mode == batch_normalization_mode:: per_activation ) { 2785help_src_desc = bn_reorder_memory_to_channel_major_format (true, help_src_desc , src , 2786& reordered_src ); 2787help_dst_desc = bn_reorder_memory_to_channel_major_format ( 2788false, help_dst_desc , dst , & reordered_dst ); 2789actual_dst_desc = help_dst_desc ; 2790help_scale_bias_desc = bn_reorder_memory_to_channel_major_format ( 2791true, help_scale_bias_desc , scale , & reordered_scale ); 2792bn_reorder_memory_to_channel_major_format (true, help_scale_bias_desc , bias , 2793& reordered_bias ); 2794help_mean_var_desc = bn_reorder_memory_to_channel_major_format ( 2795is_infer , help_mean_var_desc , saved_mean , 2796& reordered_saved_mean ); 2797actual_mean_var_desc = help_mean_var_desc ; 2798bn_reorder_memory_to_channel_major_format ( is_infer , 2799help_mean_var_desc , saved_var , 2800& reordered_saved_var ); 2801help_src_desc = compress_spatial_dimensions_to_channel ( help_src_desc ); 2802help_dst_desc = compress_spatial_dimensions_to_channel ( help_dst_desc ); 2803} else { 2804if ( help_src_desc != help_dst_desc ) { 2805help_src_desc = bn_reorder_memory_to_channel_major_format ( 2806true, help_src_desc , src , & reordered_src ); 2807help_dst_desc = bn_reorder_memory_to_channel_major_format ( 2808false, help_dst_desc , dst , & reordered_dst ); 2809actual_dst_desc = help_dst_desc ; 2810} 2811} 2812help_scale_bias_desc = 2813get_bn_scale_bias_mean_var_desc ( help_scale_bias_desc , mode ); 2814help_mean_var_desc = 2815get_bn_scale_bias_mean_var_desc ( help_mean_var_desc , mode ); 2816 2817::dnnl::prop_kind kind ; 2818::dnnl::normalization_flags flag = ::dnnl::normalization_flags:: use_scale | 2819::dnnl::normalization_flags:: use_shift ; 2820if ( is_infer ) { 2821kind = ::dnnl::prop_kind:: forward_inference ; 2822flag = ::dnnl::normalization_flags:: use_global_stats | flag ; 2823} else { 2824kind = ::dnnl::prop_kind:: forward_training ; 2825} 2826auto primitive_args = 2827create_primitive_args_or_get < ::dnnl::batch_normalization_forward > ( 2828kind , help_src_desc , help_dst_desc , epsilon , flag ); 2829 2830insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , help_src_desc , 2831reordered_src ? reordered_src : src ); 2832insert_arg ( primitive_args . second . args , DNNL_ARG_SCALE , help_scale_bias_desc , 2833reordered_scale ? reordered_scale : scale ); 2834insert_arg ( primitive_args . second . args , DNNL_ARG_SHIFT , help_scale_bias_desc , 2835reordered_bias ? reordered_bias : bias ); 2836insert_arg ( primitive_args . second . args , DNNL_ARG_MEAN , help_mean_var_desc , 2837reordered_saved_mean ? reordered_saved_mean 2838: saved_mean ); 2839insert_arg ( primitive_args . second . args , DNNL_ARG_VARIANCE , help_mean_var_desc , 2840reordered_saved_var ? reordered_saved_var 2841: saved_var ); 2842insert_arg ( primitive_args . second . args , DNNL_ARG_DST , help_dst_desc , 2843reordered_dst ? reordered_dst : dst ); 2844sycl::event e = execute_primitive < ::dnnl::batch_normalization_forward > ( primitive_args , 2845{{ alpha , beta , DNNL_ARG_DST , help_dst_desc , 2846reordered_dst ? reordered_dst : dst }}); 2847 2848if (! is_infer && running_var ) { 2849auto src_ndim = src_desc . get_desc (). get_ndims (); 2850auto src_dims = src_desc . get_dims (); 2851int element_num = src_dims [ 0 ]; 2852if ( mode == batch_normalization_mode:: spatial ) { 2853for (int index = 2 ; index < src_ndim ; index ++ ) { 2854element_num *= src_dims [ index ]; 2855} 2856} 2857float unbias_factor = element_num / ( element_num - 1.f ); 2858async_scale ( 1.f - factor , mean_var_desc , running_var ); 2859e = async_sum ( factor * unbias_factor , mean_var_desc , 2860reordered_saved_var ? reordered_saved_var : saved_var , 28611.f , mean_var_desc , running_var ); 2862} 2863if (! is_infer && running_mean ) { 2864e = async_sum ( factor , mean_var_desc , 2865reordered_saved_mean ? reordered_saved_mean : saved_mean , 2866( 1.f - factor ), mean_var_desc , running_mean ); 2867} 2868if ( reordered_dst && ( actual_dst_desc != dst_desc . get_desc ())) { 2869e = async_reorder ( 1.f , actual_dst_desc , reordered_dst , 0.f , dst_desc , dst ); 2870} 2871if (! is_infer && reordered_saved_mean && reordered_saved_var && saved_mean && 2872saved_var && ( actual_mean_var_desc != mean_var_desc . get_desc ())) { 2873e = async_reorder ( 1.f , actual_mean_var_desc , reordered_saved_mean , 0.f , 2874mean_var_desc , saved_mean ); 2875e = async_reorder ( 1.f , actual_mean_var_desc , reordered_saved_var , 0.f , 2876mean_var_desc , saved_var ); 2877} 2878return exit_primitive ( e ); 2879} 2880 2881inline 2882sycl::event engine_ext:: rnn_forward_internal ( 2883const rnn_desc & desc , ::dnnl::prop_kind kind , 2884const memory_desc_ext & src_desc , void * src , const memory_desc_ext & dst_desc , 2885void * dst , const memory_desc_ext & iter_desc , void * src_iter , void * dst_iter , 2886const memory_desc_ext & iter_c_desc , void * src_iter_c , void * dst_iter_c , 2887size_t weight_size , void * weight , size_t workspace_size , void * workspace , 2888size_t scratchpad_size , void * scratchpad , bool is_get_execution_args , 2889size_t * weight_size_query , size_t * workspace_size_query , 2890size_t * scratchpad_size_query ) { 2891::dnnl::memory::data_type src_dt ; 2892::dnnl::memory::format_tag src_format_tag ; 2893rnn_mode mode ; 2894rnn_bias_mode bias_mode ; 2895rnn_direction direction ; 2896dpct::library_data_t dt ; 2897int direction_num = 1 , input_size = 0 , hidden_size = 0 , projection_size = 0 , 2898layer_size = 0 , gate_num = 1 , output_size = 0 , data_type_size = 0 , 2899seq_length = 1 , batch_size = 1 ; 2900std::vector < void *> data = { src , dst , src_iter , dst_iter , 2901src_iter_c , dst_iter_c , weight , workspace , 2902scratchpad }; 2903std::vector < int > offset ( 6 , 0 ); 2904void * input_layer_cache = nullptr , * hidden_layer_cache = nullptr ; 2905sycl::event e ; 2906enter_primitive ( src_desc . get_size () * 2 ); 2907desc . get ( & mode , & bias_mode , & direction , & dt , & input_size , & hidden_size , 2908& projection_size , & layer_size ); 2909 2910get_rnn_configuration ( src_desc . get_desc (), direction , mode , dt , hidden_size , 2911& src_dt , & src_format_tag , & projection_size , 2912& output_size , & seq_length , & batch_size , & direction_num , 2913& gate_num ); 2914 2915if ( direction == rnn_direction:: bidirectional ) { 2916// Here to combine the oneDNN bidirectional_sum and 2917// bidirectional_concat config, so call execute_rnn_forward_primitive 2918// twice. 2919if ( layer_size > 1 ) { 2920if (! is_get_execution_args ) { 2921input_layer_cache = allocate ( src_desc ); 2922hidden_layer_cache = allocate ( src_desc ); 2923_q -> memcpy ( input_layer_cache , src , src_desc . get_size ()); 2924} 2925data [ 0 ] = input_layer_cache ; 2926data [ 1 ] = hidden_layer_cache ; 2927e = execute_rnn_forward_primitive ( 2928mode , kind , ::dnnl::rnn_direction:: bidirectional_sum , bias_mode , 2929src_dt , src_format_tag , seq_length , batch_size , output_size , 2930output_size , 1 , direction_num , hidden_size , gate_num , projection_size , 2931data , offset , layer_size - 1 , weight_size_query , workspace_size_query , 2932scratchpad_size_query ); 2933data [ 0 ] = 2934(( layer_size - 1 ) % 2 == 0 ) ? input_layer_cache : hidden_layer_cache ; 2935data [ 1 ] = dst ; 2936} 2937e = execute_rnn_forward_primitive ( 2938mode , kind , ::dnnl::rnn_direction:: bidirectional_concat , bias_mode , 2939src_dt , src_format_tag , seq_length , batch_size , output_size , 29402 * output_size , 1 , direction_num , hidden_size , gate_num , 2941projection_size , data , offset , 1 , weight_size_query , 2942workspace_size_query , scratchpad_size_query ); 2943} else { 2944e = execute_rnn_forward_primitive ( 2945mode , kind , ::dnnl::rnn_direction:: unidirectional_left2right , bias_mode , 2946src_dt , src_format_tag , seq_length , batch_size , output_size , 2947output_size , layer_size , direction_num , hidden_size , gate_num , 2948projection_size , data , offset , 1 , weight_size_query , 2949workspace_size_query , scratchpad_size_query ); 2950} 2951 2952return exit_primitive ( e ); 2953} 2954 2955inline 2956sycl::event engine_ext:: execute_rnn_forward_primitive ( 2957rnn_mode mode , ::dnnl::prop_kind kind , ::dnnl::rnn_direction direction , 2958rnn_bias_mode bias_mode , ::dnnl::memory::data_type dt , 2959::dnnl::memory::format_tag tag , int seq_length , int batch_size , int src_c , 2960int dst_c , int layer_size , int direction_num , int hidden_size , int gate_num , 2961int projection_size , std::vector < void *> & data , std::vector < int > & offset , 2962int iter_num , size_t * weight_size , size_t * workspace_size , 2963size_t * scratchpad_size ) { 2964 2965sycl::event e ; 2966::dnnl::primitive * p = nullptr ; 2967std::unordered_map < int, ::dnnl::memory > * args = nullptr ; 2968detail::primitive_cache_key_type key ; 2969std::unordered_map < int, ::dnnl::memory > * execution_args ; 2970::dnnl::memory::desc bias_desc ( 2971{ layer_size , direction_num , gate_num , hidden_size }, dt , 2972::dnnl::memory::format_tag:: ldgo ); 2973::dnnl::memory::desc weight_layer_desc ( 2974{ layer_size , direction_num , 2975projection_size ? projection_size : hidden_size , gate_num , hidden_size }, 2976dt , ::dnnl::memory::format_tag:: ldigo ); 2977::dnnl::memory::desc weight_iter_desc ( 2978{ layer_size , direction_num , 2979projection_size ? projection_size : hidden_size , gate_num , hidden_size }, 2980dt , ::dnnl::memory::format_tag:: ldigo ); 2981::dnnl::memory::desc projection_desc ; 2982if ( projection_size ) { 2983projection_desc = ::dnnl::memory:: desc ( 2984{ layer_size , direction_num , hidden_size , projection_size }, dt , 2985::dnnl::memory::format_tag:: ldio ); 2986} 2987 2988if ( weight_size ) { 2989* weight_size += 2990( weight_layer_desc . get_size () + weight_iter_desc . get_size () + 2991projection_desc . get_size () + bias_desc . get_size ()) * 2992iter_num ; 2993return e ; 2994} 2995 2996::dnnl::memory::desc src_desc ({ seq_length , batch_size , src_c }, dt , tag ); 2997::dnnl::memory::desc dst_desc ({ seq_length , batch_size , dst_c }, dt , tag ); 2998::dnnl::memory::desc iter_desc ( 2999{ layer_size , direction_num , batch_size , 3000projection_size ? projection_size : hidden_size }, 3001dt , ::dnnl::memory::format_tag:: ldnc ); 3002::dnnl::memory::desc iter_c_desc ( 3003{ layer_size , direction_num , batch_size , hidden_size }, dt , 3004::dnnl::memory::format_tag:: ldnc ); 3005 3006::dnnl::memory::desc workspace_desc ; 3007::dnnl::memory::desc scratchpad_desc ; 3008::dnnl::primitive_attr attr ; 3009attr . set_scratchpad_mode (::dnnl::scratchpad_mode:: user ); 3010 3011if ( mode == rnn_mode:: vanilla_relu || mode == rnn_mode:: vanilla_tanh ) { 3012auto primitive = create_primitive_args_or_get < ::dnnl::vanilla_rnn_forward > ( 3013kind , 3014mode == rnn_mode:: vanilla_relu ? ::dnnl::algorithm:: eltwise_relu 3015: ::dnnl::algorithm:: eltwise_tanh , 3016direction , src_desc , iter_desc , weight_layer_desc , weight_iter_desc , 3017bias_desc , dst_desc , iter_desc , attr ); 3018 3019auto pd = get_primitive_desc < ::dnnl::vanilla_rnn_forward > ( 3020primitive . second . primitive ); 3021 3022workspace_desc = pd . workspace_desc (); 3023scratchpad_desc = pd . scratchpad_desc (); 3024if ( workspace_size && scratchpad_size ) { 3025* workspace_size += workspace_desc . get_size () * iter_num ; 3026* scratchpad_size = scratchpad_desc . get_size () > * scratchpad_size 3027? scratchpad_desc . get_size () 3028: * scratchpad_size ; 3029} else { 3030key = primitive . first ; 3031p = primitive . second . primitive ; 3032args = primitive . second . args ; 3033} 3034} else if ( mode == rnn_mode:: gru ) { 3035auto primitive = create_primitive_args_or_get < ::dnnl::gru_forward > ( 3036kind , direction , src_desc , iter_desc , weight_layer_desc , 3037weight_iter_desc , bias_desc , dst_desc , iter_desc , attr ); 3038 3039auto pd = 3040get_primitive_desc < ::dnnl::gru_forward > ( primitive . second . primitive ); 3041 3042workspace_desc = pd . workspace_desc (); 3043scratchpad_desc = pd . scratchpad_desc (); 3044if ( workspace_size && scratchpad_size ) { 3045* workspace_size += workspace_desc . get_size () * iter_num ; 3046* scratchpad_size = scratchpad_desc . get_size () > * scratchpad_size 3047? scratchpad_desc . get_size () 3048: * scratchpad_size ; 3049} else { 3050key = primitive . first ; 3051p = primitive . second . primitive ; 3052args = primitive . second . args ; 3053} 3054} else if ( mode == rnn_mode:: lstm ) { 3055auto primitive = create_primitive_args_or_get < ::dnnl::lstm_forward > ( 3056kind , direction , src_desc , iter_desc , iter_c_desc , weight_layer_desc , 3057weight_iter_desc , ::dnnl::memory:: desc (), projection_desc , bias_desc , 3058dst_desc , iter_desc , iter_c_desc , attr ); 3059 3060auto pd = 3061get_primitive_desc < ::dnnl::lstm_forward > ( primitive . second . primitive ); 3062 3063workspace_desc = pd . workspace_desc (); 3064scratchpad_desc = pd . scratchpad_desc (); 3065if ( workspace_size && scratchpad_size ) { 3066* workspace_size += workspace_desc . get_size () * iter_num ; 3067* scratchpad_size = scratchpad_desc . get_size () > * scratchpad_size 3068? scratchpad_desc . get_size () 3069: * scratchpad_size ; 3070} else { 3071key = primitive . first ; 3072p = primitive . second . primitive ; 3073args = primitive . second . args ; 3074} 3075} 3076 3077for (int i = 0 ; i < iter_num ; i ++ ) { 3078void * in_cache = data [ 0 ], * out_cache = data [ 1 ], * dst_iter_c_cache = nullptr , 3079* dst_iter_cache = ((uint8_t * )( data [ 3 ]) + offset [ 1 ]); 3080if ( mode == rnn_mode:: lstm ) { 3081dst_iter_c_cache = (uint8_t * )( data [ 4 ]) + offset [ 2 ]; 3082} 3083if (! workspace_size ) { 3084insert_arg ( args , DNNL_ARG_SRC_LAYER , src_desc , data [ 0 ]); 3085insert_arg ( args , DNNL_ARG_DST_LAYER , dst_desc , data [ 1 ]); 3086insert_arg ( args , DNNL_ARG_SCRATCHPAD , scratchpad_desc , data [ 8 ]); 3087auto insert_rnn_arg = [ & ](int arg_name , ::dnnl::memory::desc & d , void * data , 3088int & offset ) { 3089insert_arg ( args , arg_name , d , (uint8_t * ) data + offset ); 3090offset += d . get_size (); 3091}; 3092insert_rnn_arg ( DNNL_ARG_SRC_ITER , iter_desc , data [ 2 ], offset [ 0 ]); 3093insert_rnn_arg ( DNNL_ARG_DST_ITER , iter_desc , data [ 3 ], offset [ 1 ]); 3094 3095if ( mode == rnn_mode:: lstm ) { 3096insert_rnn_arg ( DNNL_ARG_SRC_ITER_C , iter_c_desc , data [ 4 ], offset [ 2 ]); 3097insert_rnn_arg ( DNNL_ARG_DST_ITER_C , iter_c_desc , data [ 5 ], offset [ 3 ]); 3098} 3099insert_rnn_arg ( DNNL_ARG_WEIGHTS_LAYER , weight_layer_desc , data [ 6 ], 3100offset [ 4 ]); 3101insert_rnn_arg ( DNNL_ARG_WEIGHTS_ITER , weight_iter_desc , data [ 6 ], offset [ 4 ]); 3102if ( projection_size ) { 3103insert_rnn_arg ( DNNL_ARG_WEIGHTS_PROJECTION , projection_desc , data [ 6 ], 3104offset [ 4 ]); 3105} 3106if ( bias_mode == rnn_bias_mode:: none ) { 3107_q -> memset ((uint8_t * )( data [ 6 ]) + offset [ 4 ], 0 , bias_desc . get_size ()); 3108} 3109insert_rnn_arg ( DNNL_ARG_BIAS , bias_desc , data [ 6 ], offset [ 4 ]); 3110if ( kind == ::dnnl::prop_kind:: forward_training ) { 3111insert_rnn_arg ( DNNL_ARG_WORKSPACE , workspace_desc , data [ 7 ], offset [ 5 ]); 3112} 3113if ( mode == rnn_mode:: vanilla_relu || mode == rnn_mode:: vanilla_tanh ) { 3114execute_primitive < ::dnnl::vanilla_rnn_forward > ( 3115{ key , { static_cast < ::dnnl::vanilla_rnn_forward *> ( p ), args }}); 3116} else if ( mode == rnn_mode:: gru ) { 3117execute_primitive < ::dnnl::gru_forward > ( 3118{ key , { static_cast < ::dnnl::gru_forward *> ( p ), args }}); 3119} else if ( mode == rnn_mode:: lstm ) { 3120execute_primitive < ::dnnl::lstm_forward > ( 3121{ key , { static_cast < ::dnnl::lstm_forward *> ( p ), args }}); 3122} 3123if ( i != iter_num - 1 ) { 3124std:: swap ( data [ 0 ], data [ 1 ]); 3125} 3126} 3127if ( kind == ::dnnl::prop_kind:: forward_training ) { 3128if ( workspace_size ) { 3129* workspace_size += 3130( src_desc . get_size () + dst_desc . get_size () + iter_desc . get_size ()); 3131if ( mode == rnn_mode:: lstm ) { 3132* workspace_size += iter_c_desc . get_size (); 3133} 3134} else { 3135_q -> memcpy ((uint8_t * )( data [ 7 ]) + offset [ 5 ], in_cache , 3136src_desc . get_size ()); 3137offset [ 5 ] += src_desc . get_size (); 3138_q -> memcpy ((uint8_t * )( data [ 7 ]) + offset [ 5 ], out_cache , 3139dst_desc . get_size ()); 3140offset [ 5 ] += dst_desc . get_size (); 3141_q -> memcpy ((uint8_t * )( data [ 7 ]) + offset [ 5 ], dst_iter_cache , 3142iter_desc . get_size ()); 3143offset [ 5 ] += iter_desc . get_size (); 3144if ( mode == rnn_mode:: lstm ) { 3145_q -> memcpy ((uint8_t * )( data [ 7 ]) + offset [ 5 ], dst_iter_c_cache , 3146iter_c_desc . get_size ()); 3147offset [ 5 ] += iter_c_desc . get_size (); 3148} 3149} 3150} 3151} 3152return e ; 3153} 3154 3155inline 3156sycl::event engine_ext:: execute_rnn_backward_primitive ( 3157rnn_mode mode , ::dnnl::rnn_direction direction , rnn_bias_mode bias_mode , 3158::dnnl::memory::data_type dt , ::dnnl::memory::format_tag tag , 3159int seq_length , int batch_size , int src_c , int dst_c , int layer_size , 3160int direction_num , int hidden_size , int gate_num , int projection_size , 3161std::vector < void *> & data , std::vector < int > & offset , int iter_num ) { 3162 3163sycl::event e ; 3164::dnnl::primitive * p = nullptr ; 3165std::unordered_map < int, ::dnnl::memory > * args = nullptr ; 3166detail::primitive_cache_key_type key ; 3167::dnnl::prop_kind fkind = ::dnnl::prop_kind:: forward_training ; 3168::dnnl::prop_kind bkind = ::dnnl::prop_kind:: backward ; 3169::dnnl::memory::desc bias_desc ( 3170{ layer_size , direction_num , gate_num , hidden_size }, dt , 3171::dnnl::memory::format_tag:: ldgo ); 3172::dnnl::memory::desc weight_layer_desc ( 3173{ layer_size , direction_num , 3174projection_size ? projection_size : hidden_size , gate_num , hidden_size }, 3175dt , ::dnnl::memory::format_tag:: ldigo ); 3176::dnnl::memory::desc weight_iter_desc ( 3177{ layer_size , direction_num , 3178projection_size ? projection_size : hidden_size , gate_num , hidden_size }, 3179dt , ::dnnl::memory::format_tag:: ldigo ); 3180::dnnl::memory::desc diff_weight_layer_desc ( 3181{ layer_size , direction_num , 3182projection_size ? projection_size : hidden_size , gate_num , hidden_size }, 3183dt , ::dnnl::memory::format_tag:: ldgoi ); 3184::dnnl::memory::desc diff_weight_iter_desc ( 3185{ layer_size , direction_num , 3186projection_size ? projection_size : hidden_size , gate_num , hidden_size }, 3187dt , ::dnnl::memory::format_tag:: ldgoi ); 3188::dnnl::memory::desc projection_desc , diff_projection_desc ; 3189if ( projection_size ) { 3190projection_desc = ::dnnl::memory:: desc ( 3191{ layer_size , direction_num , hidden_size , projection_size }, dt , 3192::dnnl::memory::format_tag:: ldio ); 3193diff_projection_desc = ::dnnl::memory:: desc ( 3194{ layer_size , direction_num , hidden_size , projection_size }, dt , 3195::dnnl::memory::format_tag:: ldoi ); 3196} 3197 3198::dnnl::memory::desc src_desc ({ seq_length , batch_size , src_c }, dt , tag ); 3199::dnnl::memory::desc dst_desc ({ seq_length , batch_size , dst_c }, dt , tag ); 3200::dnnl::memory::desc iter_desc ( 3201{ layer_size , direction_num , batch_size , 3202projection_size ? projection_size : hidden_size }, 3203dt , ::dnnl::memory::format_tag:: ldnc ); 3204::dnnl::memory::desc iter_c_desc ( 3205{ layer_size , direction_num , batch_size , hidden_size }, dt , 3206::dnnl::memory::format_tag:: ldnc ); 3207 3208::dnnl::memory::desc workspace_desc ; 3209::dnnl::memory::desc scratchpad_desc ; 3210::dnnl::primitive_attr attr ; 3211attr . set_scratchpad_mode (::dnnl::scratchpad_mode:: user ); 3212 3213if ( mode == rnn_mode:: vanilla_relu || mode == rnn_mode:: vanilla_tanh ) { 3214auto fpd = create_primitive_desc < ::dnnl::vanilla_rnn_forward > ( 3215fkind , 3216mode == rnn_mode:: vanilla_relu ? ::dnnl::algorithm:: eltwise_relu 3217: ::dnnl::algorithm:: eltwise_tanh , 3218direction , src_desc , iter_desc , weight_layer_desc , weight_iter_desc , 3219bias_desc , dst_desc , iter_desc , attr ); 3220auto primitive = create_primitive_args_or_get < ::dnnl::vanilla_rnn_backward > ( 3221bkind , 3222mode == rnn_mode:: vanilla_relu ? ::dnnl::algorithm:: eltwise_relu 3223: ::dnnl::algorithm:: eltwise_tanh , 3224direction , src_desc , iter_desc , diff_weight_layer_desc , 3225diff_weight_iter_desc , bias_desc , dst_desc , iter_desc , src_desc , 3226iter_desc , weight_layer_desc , weight_iter_desc , bias_desc , dst_desc , 3227iter_desc , fpd , attr ); 3228auto pd = get_primitive_desc < ::dnnl::vanilla_rnn_backward > ( 3229primitive . second . primitive ); 3230workspace_desc = pd . workspace_desc (); 3231scratchpad_desc = pd . scratchpad_desc (); 3232key = primitive . first ; 3233p = primitive . second . primitive ; 3234args = primitive . second . args ; 3235} else if ( mode == rnn_mode:: gru ) { 3236auto fpd = create_primitive_desc < ::dnnl::gru_forward > ( 3237fkind , direction , src_desc , iter_desc , weight_layer_desc , 3238weight_iter_desc , bias_desc , dst_desc , iter_desc , attr ); 3239auto primitive = create_primitive_args_or_get < ::dnnl::gru_backward > ( 3240bkind , direction , src_desc , iter_desc , diff_weight_layer_desc , 3241diff_weight_iter_desc , bias_desc , dst_desc , iter_desc , src_desc , 3242iter_desc , weight_layer_desc , weight_iter_desc , bias_desc , dst_desc , 3243iter_desc , fpd , attr ); 3244auto pd = 3245get_primitive_desc < ::dnnl::gru_backward > ( primitive . second . primitive ); 3246workspace_desc = pd . workspace_desc (); 3247scratchpad_desc = pd . scratchpad_desc (); 3248key = primitive . first ; 3249p = primitive . second . primitive ; 3250args = primitive . second . args ; 3251} else if ( mode == rnn_mode:: lstm ) { 3252auto fpd = create_primitive_desc < ::dnnl::lstm_forward > ( 3253fkind , direction , src_desc , iter_desc , iter_c_desc , weight_layer_desc , 3254weight_iter_desc , ::dnnl::memory:: desc (), projection_desc , bias_desc , 3255dst_desc , iter_desc , iter_c_desc , attr ); 3256auto primitive = create_primitive_args_or_get < ::dnnl::lstm_backward > ( 3257bkind , direction , src_desc , iter_desc , iter_c_desc , 3258diff_weight_layer_desc , diff_weight_iter_desc , ::dnnl::memory:: desc (), 3259diff_projection_desc , bias_desc , dst_desc , iter_desc , iter_c_desc , 3260src_desc , iter_desc , iter_c_desc , weight_layer_desc , weight_iter_desc , 3261::dnnl::memory:: desc (), projection_desc , bias_desc , dst_desc , iter_desc , 3262iter_c_desc , fpd , attr ); 3263auto pd = 3264get_primitive_desc < ::dnnl::lstm_backward > ( primitive . second . primitive ); 3265workspace_desc = pd . workspace_desc (); 3266scratchpad_desc = pd . scratchpad_desc (); 3267key = primitive . first ; 3268p = primitive . second . primitive ; 3269args = primitive . second . args ; 3270} 3271 3272for (int i = 0 ; i < iter_num ; i ++ ) { 3273insert_arg ( args , DNNL_ARG_DIFF_SRC_LAYER , src_desc , data [ 8 ]); 3274insert_arg ( args , DNNL_ARG_DIFF_DST_LAYER , dst_desc , data [ 9 ]); 3275insert_arg ( args , DNNL_ARG_SCRATCHPAD , scratchpad_desc , data [ 15 ]); 3276auto insert_rnn_arg = [ & ](int arg_name , ::dnnl::memory::desc & d , void * data , 3277int & offset ) { 3278offset += d . get_size (); 3279insert_arg ( args , arg_name , d , (uint8_t * ) data - offset ); 3280}; 3281if ( mode == rnn_mode:: lstm ) { 3282insert_rnn_arg ( DNNL_ARG_DST_ITER_C , iter_c_desc , data [ 7 ], offset [ 0 ]); 3283insert_rnn_arg ( DNNL_ARG_SRC_ITER_C , iter_c_desc , data [ 4 ], offset [ 2 ]); 3284} 3285insert_rnn_arg ( DNNL_ARG_DST_ITER , iter_desc , data [ 7 ], offset [ 0 ]); 3286insert_rnn_arg ( DNNL_ARG_DST_LAYER , dst_desc , data [ 7 ], offset [ 0 ]); 3287insert_rnn_arg ( DNNL_ARG_SRC_LAYER , src_desc , data [ 7 ], offset [ 0 ]); 3288insert_rnn_arg ( DNNL_ARG_WORKSPACE , workspace_desc , data [ 7 ], offset [ 0 ]); 3289insert_rnn_arg ( DNNL_ARG_SRC_ITER , iter_desc , data [ 2 ], offset [ 1 ]); 3290insert_rnn_arg ( DNNL_ARG_BIAS , bias_desc , data [ 6 ], offset [ 3 ]); 3291if ( projection_size ) { 3292insert_rnn_arg ( DNNL_ARG_WEIGHTS_PROJECTION , diff_projection_desc , data [ 6 ], 3293offset [ 3 ]); 3294} 3295insert_rnn_arg ( DNNL_ARG_WEIGHTS_ITER , diff_weight_iter_desc , data [ 6 ], 3296offset [ 3 ]); 3297insert_rnn_arg ( DNNL_ARG_WEIGHTS_LAYER , diff_weight_layer_desc , data [ 6 ], 3298offset [ 3 ]); 3299insert_rnn_arg ( DNNL_ARG_DIFF_SRC_ITER , iter_desc , data [ 10 ], offset [ 4 ]); 3300insert_rnn_arg ( DNNL_ARG_DIFF_DST_ITER , iter_desc , data [ 11 ], offset [ 5 ]); 3301if ( mode == rnn_mode:: lstm ) { 3302insert_rnn_arg ( DNNL_ARG_DIFF_SRC_ITER_C , iter_c_desc , data [ 12 ], offset [ 6 ]); 3303insert_rnn_arg ( DNNL_ARG_DIFF_DST_ITER_C , iter_c_desc , data [ 13 ], offset [ 7 ]); 3304} 3305insert_rnn_arg ( DNNL_ARG_DIFF_BIAS , bias_desc , data [ 14 ], offset [ 8 ]); 3306if ( bias_mode == rnn_bias_mode:: none ) { 3307_q -> memset ((uint8_t * )( data [ 14 ]) - offset [ 8 ], 0 , bias_desc . get_size ()); 3308} 3309if ( projection_size ) { 3310insert_rnn_arg ( DNNL_ARG_DIFF_WEIGHTS_PROJECTION , projection_desc , data [ 14 ], 3311offset [ 8 ]); 3312} 3313insert_rnn_arg ( DNNL_ARG_DIFF_WEIGHTS_ITER , weight_iter_desc , data [ 14 ], 3314offset [ 8 ]); 3315insert_rnn_arg ( DNNL_ARG_DIFF_WEIGHTS_LAYER , weight_layer_desc , data [ 14 ], 3316offset [ 8 ]); 3317if ( mode == rnn_mode:: vanilla_relu || mode == rnn_mode:: vanilla_tanh ) { 3318e = execute_primitive < ::dnnl::vanilla_rnn_backward > ( 3319{ key , { static_cast < ::dnnl::vanilla_rnn_backward *> ( p ), args }}); 3320} else if ( mode == rnn_mode:: gru ) { 3321e = execute_primitive < ::dnnl::gru_backward > ( 3322{ key , { static_cast < ::dnnl::gru_backward *> ( p ), args }}); 3323} else if ( mode == rnn_mode:: lstm ) { 3324e = execute_primitive < ::dnnl::lstm_backward > ( 3325{ key , { static_cast < ::dnnl::lstm_backward *> ( p ), args }}); 3326} 3327if ( i != iter_num - 1 ) { 3328std:: swap ( data [ 8 ], data [ 9 ]); 3329} 3330} 3331return e ; 3332} 3333 3334#define EMPTY_CACHE_KEY ( type ) \ 3335template <> \ 3336inline void engine_ext::generate_cache_key<type>(std::string & key_buffer, \ 3337const type &arg) {} 3338 3339EMPTY_CACHE_KEY (::dnnl:: engine ) 3340EMPTY_CACHE_KEY (::dnnl::convolution_forward:: primitive_desc ) 3341EMPTY_CACHE_KEY (::dnnl::eltwise_forward:: primitive_desc ) 3342EMPTY_CACHE_KEY (::dnnl::softmax_forward:: primitive_desc ) 3343EMPTY_CACHE_KEY (::dnnl::pooling_forward:: primitive_desc ) 3344EMPTY_CACHE_KEY (::dnnl::lrn_forward:: primitive_desc ) 3345EMPTY_CACHE_KEY (::dnnl::batch_normalization_forward:: primitive_desc ) 3346EMPTY_CACHE_KEY (::dnnl::vanilla_rnn_forward:: primitive_desc ) 3347EMPTY_CACHE_KEY (::dnnl::lstm_forward:: primitive_desc ) 3348EMPTY_CACHE_KEY (::dnnl::gru_forward:: primitive_desc ) 3349#undef EMPTY_CACHE_KEY 3350 3351template <> 3352inline void engine_ext:: generate_cache_key < std::vector < float >> ( 3353std::string & key_buffer , const std::vector < float > & vec ) { 3354key_buffer . append ((char * ) vec . data (), vec . size () * sizeof (float)); 3355} 3356 3357template <> 3358inline void engine_ext:: generate_cache_key < ::dnnl::primitive_attr > ( 3359std::string & key_buffer , const ::dnnl::primitive_attr & attr ) { 3360if (! attr ) { 3361return ; 3362} 3363auto math_mode = (uint8_t) attr . get_fpmath_mode (); 3364key_buffer . append ((char * ) & math_mode , sizeof (uint8_t)); 3365} 3366 3367template <> 3368inline void engine_ext:: generate_cache_key < ::dnnl::memory::dims > ( 3369std::string & key_buffer , const ::dnnl::memory::dims & dims ) { 3370key_buffer . append ((char * ) dims . data (), dims . size () * sizeof (int64_t)); 3371} 3372 3373template <> 3374inline void engine_ext:: generate_cache_key < ::dnnl::memory::desc > ( 3375std::string & key_buffer , const ::dnnl::memory::desc & desc ) { 3376uint8_t params [ 3 ] = {(uint8_t) desc . get_format_kind (), 3377(uint8_t) desc . get_ndims (), 3378(uint8_t) desc . get_data_type ()}; 3379generate_cache_key ( key_buffer , desc . get_inner_blks ()); 3380generate_cache_key ( key_buffer , desc . get_dims ()); 3381generate_cache_key ( key_buffer , desc . get_strides ()); 3382} 3383 3384template < typename T > 3385void engine_ext:: generate_cache_key (std::string & key_buffer , const T & arg ) { 3386key_buffer . append ((char * ) & arg , sizeof ( T )); 3387} 3388 3389template < typename T, typename ... args_type > 3390void engine_ext:: generate_cache_key (std::string & key_buffer , const T & first_arg , 3391const args_type & ... args ) { 3392generate_cache_key ( key_buffer , first_arg ); 3393generate_cache_key ( key_buffer , args ...); 3394} 3395 3396template < typename primitive_type, typename ... args_type > 3397std::pair < detail::primitive_cache_key_type, detail::primitive_and_args > 3398engine_ext:: create_primitive_args_or_get (args_type && ... args ) { 3399std::string buffer ; 3400buffer . reserve ( 512 ); 3401generate_cache_key ( buffer , std:: forward < args_type > ( args )...); 3402buffer . append (std:: to_string ( _engine_id )); 3403auto value = _primitive_cache . get ( buffer ); 3404primitive_type * p = nullptr ; 3405std::unordered_map < int, ::dnnl::memory > * a = nullptr ; 3406if ( value ) { 3407p = (primitive_type * ) value -> _primitive ; 3408a = value -> _args ; 3409} else { 3410p = new primitive_type( create_primitive_desc < primitive_type > ( 3411std:: forward < args_type > ( args )...)); 3412a = new std::unordered_map < int, ::dnnl::memory > (); 3413} 3414return { buffer , { p , a }}; 3415} 3416 3417template < typename primitive_type > 3418typename primitive_type::primitive_desc 3419engine_ext:: get_primitive_desc (::dnnl::primitive * p ) { 3420return typename primitive_type:: primitive_desc ( 3421const_cast < dnnl_primitive_desc_t > ( p -> get_primitive_desc ())); 3422} 3423 3424template < typename primitive_type, typename ... args_type > 3425typename primitive_type::primitive_desc 3426engine_ext:: create_primitive_desc (args_type && ... args ) { 3427return typename primitive_type:: primitive_desc ( 3428* _eng , std:: forward < args_type > ( args )...); 3429} 3430 3431inline 3432void engine_ext:: fill ( const memory_desc_ext & src_desc , void * src , 3433const void * valuePtr ) { 3434async_fill ( src_desc , src , valuePtr ). wait (); 3435} 3436 3437inline 3438void engine_ext:: reorder (float alpha , const memory_desc_ext & src_desc , 3439void * src , float beta , const memory_desc_ext & dst_desc , 3440void * dst ) { 3441async_reorder ( alpha , src_desc , src , beta , dst_desc , dst ). wait (); 3442} 3443 3444inline 3445void engine_ext:: scale (float alpha , const memory_desc_ext & src_desc , 3446void * src ) { 3447async_scale ( alpha , src_desc , src ). wait (); 3448} 3449inline 3450void engine_ext:: sum (float alpha , const memory_desc_ext & src_desc , void * src , 3451float beta , const memory_desc_ext & dst_desc , void * dst ) { 3452async_sum ( alpha , src_desc , src , beta , dst_desc , dst ). wait (); 3453} 3454inline 3455void engine_ext:: activation_forward (activation_desc & desc , float alpha , 3456const memory_desc_ext & src_desc , void * src , 3457float beta , const memory_desc_ext & dst_desc , 3458void * dst ) { 3459async_activation_forward ( desc , alpha , src_desc , src , beta , dst_desc , dst ) 3460. wait (); 3461} 3462inline 3463void engine_ext:: activation_backward ( 3464activation_desc & desc , float alpha , const memory_desc_ext & dst_desc , 3465void * dst , const memory_desc_ext & diff_dst_desc , void * diff_dst , 3466const memory_desc_ext & src_desc , void * src , float beta , 3467const memory_desc_ext & diff_src_desc , void * diff_src ) { 3468async_activation_backward ( desc , alpha , dst_desc , dst , diff_dst_desc , diff_dst , 3469src_desc , src , beta , diff_src_desc , diff_src ) 3470. wait (); 3471} 3472inline 3473void engine_ext:: pooling_forward (pooling_desc & desc , float alpha , 3474const memory_desc_ext & src_desc , void * src , 3475float beta , const memory_desc_ext & dst_desc , 3476void * dst , 3477::dnnl::memory * workspace ) { 3478async_pooling_forward ( desc , alpha , src_desc , src , beta , dst_desc , dst , 3479workspace ). wait (); 3480} 3481 3482inline 3483void engine_ext:: pooling_backward ( 3484pooling_desc & desc , float alpha , const memory_desc_ext & dst_desc , void * dst , 3485const memory_desc_ext & diff_dst_desc , void * diff_dst , 3486const memory_desc_ext & src_desc , void * src , float beta , 3487const memory_desc_ext & diff_src_desc , void * diff_src , 3488::dnnl::memory * workspace ) { 3489async_pooling_backward ( desc , alpha , dst_desc , dst , diff_dst_desc , diff_dst , 3490src_desc , src , beta , diff_src_desc , diff_src , 3491workspace ) 3492. wait (); 3493} 3494 3495inline 3496void engine_ext:: softmax_forward (softmax_algorithm alg , softmax_mode mode , 3497float alpha , const memory_desc_ext & src_desc , 3498void * src , float beta , 3499const memory_desc_ext & dst_desc , void * dst ) { 3500async_softmax_forward ( alg , mode , alpha , src_desc , src , beta , dst_desc , dst ) 3501. wait (); 3502} 3503 3504inline 3505void engine_ext:: softmax_backward (softmax_algorithm alg , softmax_mode mode , 3506float alpha , const memory_desc_ext & dst_desc , 3507void * dst , 3508const memory_desc_ext & diff_dst_desc , 3509void * diff_dst , float beta , 3510const memory_desc_ext & diff_src_desc , 3511void * diff_src ) { 3512async_softmax_backward ( alg , mode , alpha , dst_desc , dst , diff_dst_desc , 3513diff_dst , beta , diff_src_desc , diff_src ) 3514. wait (); 3515} 3516 3517inline 3518void engine_ext:: lrn_forward (lrn_desc & desc , float alpha , 3519const memory_desc_ext & src_desc , void * src , 3520float beta , const memory_desc_ext & dst_desc , 3521void * dst , ::dnnl::memory * workspace ) { 3522async_lrn_forward ( desc , alpha , src_desc , src , beta , dst_desc , dst , workspace ) 3523. wait (); 3524} 3525 3526inline 3527void engine_ext:: lrn_backward (lrn_desc & desc , float alpha , 3528const memory_desc_ext & dst_desc , void * dst , 3529const memory_desc_ext & diff_dst_desc , 3530void * diff_dst , const memory_desc_ext & src_desc , 3531void * src , float beta , 3532const memory_desc_ext & diff_src_desc , 3533void * diff_src , 3534::dnnl::memory * workspace ) { 3535async_lrn_backward ( desc , alpha , dst_desc , dst , diff_dst_desc , diff_dst , 3536src_desc , src , beta , diff_src_desc , diff_src , workspace ) 3537. wait (); 3538} 3539 3540inline 3541sycl::event engine_ext:: async_fill ( const memory_desc_ext & src_desc , void * src , 3542const void * valuePtr ) { 3543::dnnl::memory::data_type dt = src_desc . get_desc (). get_data_type (); 3544unsigned mem_size = src_desc . get_size (); 3545switch ( dt ) { 3546case ::dnnl::memory::data_type:: f32 : 3547return fill_with_type < float > ( _q , src , valuePtr , mem_size ); 3548case ::dnnl::memory::data_type:: f16 : 3549return fill_with_type < sycl::half > ( _q , src , valuePtr , mem_size ); 3550case ::dnnl::memory::data_type:: s32 : 3551return fill_with_type < int32_t > ( _q , src , valuePtr , mem_size ); 3552case ::dnnl::memory::data_type:: s8 : 3553return fill_with_type < int8_t > ( _q , src , valuePtr , mem_size ); 3554case ::dnnl::memory::data_type:: u8 : 3555return fill_with_type < uint8_t > ( _q , src , valuePtr , mem_size ); 3556default : 3557throw std:: runtime_error ( "async_fill: unsupported data type." ); 3558} 3559} 3560 3561inline 3562sycl::event engine_ext:: async_reorder (float alpha , const memory_desc_ext & src_desc , 3563void * src , float beta , 3564const memory_desc_ext & dst_desc , void * dst ) { 3565if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 3566return sycl:: event (); 3567} 3568enter_primitive ( 2 * dst_desc . get_size ()); 3569 3570auto primitive_args = create_primitive_args_or_get < ::dnnl::reorder > ( 3571src_desc . get_desc (), * _eng , dst_desc . get_desc ()); 3572 3573insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3574src ); 3575insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3576dst ); 3577 3578return exit_primitive ( execute_primitive < ::dnnl::reorder > ( 3579primitive_args , {{ alpha , beta , DNNL_ARG_DST , dst_desc , dst }})); 3580} 3581 3582inline 3583sycl::event engine_ext:: async_scale (float alpha , const memory_desc_ext & src_desc , 3584void * src ) { 3585if ( alpha == 1.f ) { 3586return sycl:: event (); 3587} 3588size_t cache_size = src_desc . get_size (); 3589enter_primitive ( cache_size ); 3590void * src_cache = allocate ( cache_size ); 3591_q -> memcpy ( src_cache , src , cache_size ); 3592auto primitive_args = create_primitive_args_or_get < ::dnnl::eltwise_forward > ( 3593::dnnl::prop_kind:: forward_inference , ::dnnl::algorithm:: eltwise_linear , 3594src_desc . get_desc (), src_desc . get_desc (), alpha , 0.f ); 3595 3596insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3597src_cache ); 3598insert_arg ( primitive_args . second . args , DNNL_ARG_DST , src_desc . get_desc (), 3599src ); 3600 3601return exit_primitive ( 3602execute_primitive < ::dnnl::eltwise_forward > ( primitive_args )); 3603} 3604 3605inline sycl::event 3606engine_ext:: async_sum (float alpha , const memory_desc_ext & src_desc , void * src , 3607float beta , const memory_desc_ext & dst_desc , void * dst ) { 3608if ( alpha == 0.f && beta == 1.f ) { 3609return sycl:: event (); 3610} 3611size_t cache_size = dst_desc . get_size (); 3612enter_primitive ( cache_size ); 3613void * dst_cache = allocate ( dst_desc ); 3614_q -> memcpy ( dst_cache , dst , cache_size ); 3615 3616auto primitive_args = create_primitive_args_or_get < ::dnnl::sum > ( 3617std::vector < float > { alpha , beta }, 3618std::vector < ::dnnl::memory::desc > { src_desc . get_desc (), 3619dst_desc . get_desc ()}); 3620insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3621dst ); 3622insert_arg ( primitive_args . second . args , DNNL_ARG_MULTIPLE_SRC , 3623src_desc . get_desc (), src ); 3624insert_arg ( primitive_args . second . args , DNNL_ARG_MULTIPLE_SRC + 1 , 3625dst_desc . get_desc (), dst_cache ); 3626 3627return exit_primitive ( execute_primitive < ::dnnl::sum > ( primitive_args )); 3628} 3629 3630inline 3631sycl::event engine_ext:: async_binary (binary_op op , float alpha_0 , 3632const memory_desc_ext & src_desc_0 , void * src_0 , 3633float alpha_1 , const memory_desc_ext & src_desc_1 , 3634void * src_1 , float beta , 3635const memory_desc_ext & dst_desc , void * dst ) { 3636::dnnl::algorithm onednn_algorithm ; 3637switch ( op ) { 3638case binary_op:: max : 3639onednn_algorithm = ::dnnl::algorithm:: binary_max ; 3640break ; 3641case binary_op:: min : 3642onednn_algorithm = ::dnnl::algorithm:: binary_min ; 3643break ; 3644case binary_op:: add : 3645onednn_algorithm = ::dnnl::algorithm:: binary_add ; 3646break ; 3647case binary_op:: sub : 3648onednn_algorithm = ::dnnl::algorithm:: binary_sub ; 3649break ; 3650case binary_op:: mul : 3651onednn_algorithm = ::dnnl::algorithm:: binary_mul ; 3652break ; 3653case binary_op:: div : 3654onednn_algorithm = ::dnnl::algorithm:: binary_div ; 3655break ; 3656case binary_op:: sqrt : 3657onednn_algorithm = ::dnnl::algorithm:: eltwise_sqrt ; 3658break ; 3659case binary_op:: neg : 3660onednn_algorithm = ::dnnl::algorithm:: eltwise_linear ; 3661break ; 3662} 3663size_t src0_cache_size = src_desc_0 . get_size (); 3664size_t src1_cache_size = src_desc_1 . get_size (); 3665size_t dst_cache_size = dst_desc . get_size (); 3666enter_primitive ( 2 * src0_cache_size + 2 * src1_cache_size + 36675 * dst_cache_size ); 3668if ( onednn_algorithm == ::dnnl::algorithm:: eltwise_sqrt || 3669onednn_algorithm == ::dnnl::algorithm:: eltwise_linear ) { 3670void * src_cache = nullptr , * dst_cache = nullptr ; 3671src_cache = allocate ( src0_cache_size ); 3672dst_cache = allocate ( dst_cache_size ); 3673_q -> memcpy ( src_cache , src_0 , src0_cache_size ); 3674_q -> memcpy ( dst_cache , dst , dst_cache_size ); 3675async_scale ( alpha_0 , src_desc_0 , src_cache ); 3676async_scale ( beta , dst_desc , dst_cache ); 3677 3678// Let the output = 1 - input to simulate the behavior of neg. 3679auto primitive_args = create_primitive_args_or_get < ::dnnl::eltwise_forward > ( 3680::dnnl::prop_kind:: forward_inference , onednn_algorithm , 3681src_desc_0 . get_desc (), dst_desc . get_desc (), -1.f , 1.f ); 3682 3683insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc_0 . get_desc (), 3684src_cache ); 3685insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3686dst ); 3687 3688execute_primitive < ::dnnl::eltwise_forward > ( 3689primitive_args , {{ 1.f , 0.f , DNNL_ARG_DST , dst_desc , dst }}); 3690return exit_primitive ( 3691async_sum ( 1.f , dst_desc , dst_cache , 1.f , dst_desc , dst )); 3692} 3693 3694void * src_0_cache = nullptr , * src_1_cache = nullptr , * dst_cache = nullptr ; 3695 3696src_0_cache = allocate ( src0_cache_size ); 3697src_1_cache = allocate ( src1_cache_size ); 3698dst_cache = allocate ( dst_cache_size ); 3699 3700_q -> memcpy ( src_0_cache , src_0 , src0_cache_size ); 3701_q -> memcpy ( src_1_cache , src_1 , src1_cache_size ); 3702_q -> memcpy ( dst_cache , dst , dst_cache_size ); 3703 3704async_scale ( alpha_0 , src_desc_0 , src_0_cache ); 3705async_scale ( alpha_1 , src_desc_1 , src_1_cache ); 3706async_scale ( beta , dst_desc , dst_cache ); 3707 3708auto primitive_args = create_primitive_args_or_get < ::dnnl::binary > ( 3709onednn_algorithm , src_desc_0 . get_desc (), src_desc_1 . get_desc (), 3710dst_desc . get_desc ()); 3711 3712insert_arg ( primitive_args . second . args , DNNL_ARG_SRC_0 , src_desc_0 . get_desc (), 3713src_0_cache ); 3714insert_arg ( primitive_args . second . args , DNNL_ARG_SRC_1 , src_desc_1 . get_desc (), 3715src_1_cache ); 3716insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3717dst ); 3718 3719execute_primitive < ::dnnl::binary > ( primitive_args , 3720{{ 1.f , 0.f , DNNL_ARG_DST , dst_desc , dst }}); 3721return exit_primitive ( 3722async_sum ( 1.f , dst_desc , dst_cache , 1.f , dst_desc , dst )); 3723} 3724 3725inline 3726sycl::event engine_ext:: async_reduction (reduction_op op , float alpha , 3727const memory_desc_ext & src_desc , void * src , 3728float beta , const memory_desc_ext & dst_desc , 3729void * dst ) { 3730if ( alpha == 0.f && beta == 1.f ) { 3731return sycl:: event (); 3732} 3733size_t src_cache_size = src_desc . get_size (); 3734size_t dst_cache_size = dst_desc . get_size (); 3735enter_primitive ( 3 * src_cache_size + 2 * dst_cache_size ); 3736float p = 2.f ; 3737::dnnl::algorithm onednn_algorithm ; 3738void * cache = nullptr ; 3739switch ( op ) { 3740case reduction_op:: amax : 3741cache = allocate ( src_cache_size ); 3742activation_desc adesc ; 3743adesc . set_algorithm (::dnnl::algorithm:: eltwise_abs ); 3744async_activation_forward ( adesc , 1.f , src_desc , src , 0.f , src_desc , cache ); 3745onednn_algorithm = ::dnnl::algorithm:: reduction_max ; 3746src = cache ; 3747break ; 3748case reduction_op:: max : 3749onednn_algorithm = ::dnnl::algorithm:: reduction_max ; 3750break ; 3751case reduction_op:: min : 3752onednn_algorithm = ::dnnl::algorithm:: reduction_min ; 3753break ; 3754case reduction_op:: sum : 3755onednn_algorithm = ::dnnl::algorithm:: reduction_sum ; 3756break ; 3757case reduction_op:: mean : 3758onednn_algorithm = ::dnnl::algorithm:: reduction_mean ; 3759break ; 3760case reduction_op:: mul : 3761onednn_algorithm = ::dnnl::algorithm:: reduction_mul ; 3762break ; 3763case reduction_op:: mul_no_zeros : 3764cache = allocate ( src_cache_size ); 3765transform_no_zero ( src_desc , src , cache ); 3766onednn_algorithm = ::dnnl::algorithm:: reduction_mul ; 3767src = cache ; 3768break ; 3769case reduction_op:: norm1 : 3770p = 1.f ; 3771onednn_algorithm = ::dnnl::algorithm:: reduction_norm_lp_power_p_sum ; 3772break ; 3773case reduction_op:: norm2 : 3774onednn_algorithm = ::dnnl::algorithm:: reduction_norm_lp_sum ; 3775break ; 3776} 3777auto primitive_args = create_primitive_args_or_get < ::dnnl::reduction > ( 3778onednn_algorithm , src_desc . get_desc (), dst_desc . get_desc (), p , 0.f ); 3779 3780insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3781src ); 3782insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3783dst ); 3784 3785return exit_primitive ( execute_primitive < ::dnnl::reduction > ( 3786primitive_args , {{ alpha , beta , DNNL_ARG_DST , dst_desc , dst }})); 3787} 3788 3789inline 3790sycl::event engine_ext:: async_activation_forward (activation_desc & desc , float alpha , 3791const memory_desc_ext & src_desc , 3792void * src , float beta , 3793const memory_desc_ext & dst_desc , 3794void * dst ) { 3795if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 3796return sycl:: event (); 3797} 3798enter_primitive ( 2 * dst_desc . get_size ()); 3799auto primitive_args = create_primitive_args_or_get < ::dnnl::eltwise_forward > ( 3800::dnnl::prop_kind:: forward , desc . get_algorithm (), src_desc . get_desc (), 3801dst_desc . get_desc (), desc . get_alpha (), desc . get_beta ()); 3802 3803insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3804src ); 3805insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3806dst ); 3807 3808return exit_primitive ( execute_primitive < ::dnnl::eltwise_forward > ( 3809primitive_args , {{ alpha , beta , DNNL_ARG_DST , dst_desc , dst }})); 3810} 3811 3812inline 3813sycl::event engine_ext:: async_activation_backward ( 3814activation_desc & desc , float alpha , const memory_desc_ext & dst_desc , 3815void * dst , const memory_desc_ext & diff_dst_desc , void * diff_dst , 3816const memory_desc_ext & src_desc , void * src , float beta , 3817const memory_desc_ext & diff_src_desc , void * diff_src ) { 3818 3819if ( scale_parameter_preprocess ({{ alpha , beta , diff_src_desc , diff_src }})) { 3820return sycl:: event (); 3821} 3822enter_primitive ( 2 * diff_src_desc . get_size ()); 3823::dnnl::memory::desc data_desc = dst_desc . get_desc (); 3824auto alg = desc . get_algorithm (); 3825if (( alg == ::dnnl::algorithm:: eltwise_clip ) || 3826( alg == ::dnnl::algorithm:: eltwise_linear ) || 3827( alg == ::dnnl::algorithm:: eltwise_swish )) { 3828data_desc = src_desc . get_desc (); 3829} 3830auto primitive_args = create_primitive_args_or_get < ::dnnl::eltwise_backward > ( 3831alg , diff_src_desc . get_desc (), diff_dst_desc . get_desc (), data_desc , 3832desc . get_alpha (), desc . get_beta (), 3833create_primitive_desc < ::dnnl::eltwise_forward > ( 3834::dnnl::prop_kind:: forward , alg , src_desc . get_desc (), 3835dst_desc . get_desc (), desc . get_alpha (), desc . get_beta ())); 3836 3837insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3838dst ); 3839insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3840src ); 3841insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , 3842diff_dst_desc . get_desc (), diff_dst ); 3843insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SRC , 3844diff_src_desc . get_desc (), diff_src ); 3845 3846return exit_primitive ( execute_primitive < ::dnnl::eltwise_backward > ( 3847primitive_args , 3848{{ alpha , beta , DNNL_ARG_DIFF_SRC , diff_src_desc , diff_src }})); 3849} 3850 3851inline 3852sycl::event engine_ext:: async_pooling_forward (pooling_desc & desc , float alpha , 3853const memory_desc_ext & src_desc , 3854void * src , float beta , 3855const memory_desc_ext & dst_desc , 3856void * dst , ::dnnl::memory * workspace ) { 3857if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 3858return sycl:: event (); 3859} 3860enter_primitive ( 2 * dst_desc . get_size ()); 3861int pooling_dim = desc . get_stride (). size (); 3862std::vector < int64_t > dilation ( pooling_dim , 0 ); 3863auto primitive_args = 3864create_primitive_args_or_get < ::dnnl::pooling_forward > ( 3865::dnnl::prop_kind:: forward_training , desc . get_algorithm (), 3866src_desc . get_desc (), dst_desc . get_desc (), desc . get_stride (), 3867desc . get_kernel (), dilation , desc . get_padding (), desc . get_padding ()); 3868auto pd = get_primitive_desc < ::dnnl::pooling_forward > ( 3869primitive_args . second . primitive ); 3870::dnnl::memory ws_mem ( pd . workspace_desc (), * _eng ); 3871if ( workspace ) { 3872* workspace = ws_mem ; 3873} else { 3874insert_workspace ( src , ws_mem ); 3875} 3876insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3877src ); 3878insert_arg ( primitive_args . second . args , DNNL_ARG_WORKSPACE , ws_mem ); 3879insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3880dst ); 3881 3882return exit_primitive ( execute_primitive < ::dnnl::pooling_forward > ( 3883primitive_args , {{ alpha , beta , DNNL_ARG_DST , dst_desc , dst }})); 3884} 3885 3886inline 3887sycl::event engine_ext:: async_pooling_backward ( 3888pooling_desc & desc , float alpha , const memory_desc_ext & dst_desc , void * dst , 3889const memory_desc_ext & diff_dst_desc , void * diff_dst , 3890const memory_desc_ext & src_desc , void * src , float beta , 3891const memory_desc_ext & diff_src_desc , void * diff_src , 3892::dnnl::memory * workspace ) { 3893if ( scale_parameter_preprocess ({{ alpha , beta , diff_src_desc , diff_src }})) { 3894return sycl:: event (); 3895} 3896enter_primitive ( 2 * diff_src_desc . get_size ()); 3897int pooling_dim = desc . get_stride (). size (); 3898std::vector < int64_t > dilation ( pooling_dim , 0 ); 3899auto primitive_args = create_primitive_args_or_get < ::dnnl::pooling_backward > ( 3900desc . get_algorithm (), diff_src_desc . get_desc (), diff_dst_desc . get_desc (), 3901desc . get_stride (), desc . get_kernel (), dilation , desc . get_padding (), 3902desc . get_padding (), 3903create_primitive_desc < ::dnnl::pooling_forward > ( 3904::dnnl::prop_kind:: forward_training , desc . get_algorithm (), 3905src_desc . get_desc (), dst_desc . get_desc (), desc . get_stride (), 3906desc . get_kernel (), dilation , desc . get_padding (), desc . get_padding ())); 3907 3908insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 3909dst ); 3910insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 3911src ); 3912insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , 3913diff_dst_desc . get_desc (), diff_dst ); 3914insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SRC , 3915diff_src_desc . get_desc (), diff_src ); 3916 3917if ( workspace ) { 3918insert_arg ( primitive_args . second . args , DNNL_ARG_WORKSPACE , * workspace ); 3919} else { 3920insert_arg ( primitive_args . second . args , DNNL_ARG_WORKSPACE , 3921get_workspace ( src )); 3922} 3923 3924return exit_primitive ( execute_primitive < ::dnnl::pooling_backward > ( 3925primitive_args , 3926{{ alpha , beta , DNNL_ARG_DIFF_SRC , diff_src_desc , diff_src }})); 3927} 3928 3929inline 3930sycl::event engine_ext:: async_softmax_forward (softmax_algorithm alg , 3931softmax_mode mode , float alpha , 3932const memory_desc_ext & src_desc , 3933void * src , float beta , 3934const memory_desc_ext & dst_desc , 3935void * dst ) { 3936if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 3937return sycl:: event (); 3938} 3939 3940::dnnl::memory::desc help_src_desc = src_desc . get_desc (); 3941::dnnl::memory::desc help_dst_desc = dst_desc . get_desc (); 3942if ( mode == softmax_mode:: instance ) { 3943help_src_desc = compress_spatial_dimensions_to_channel ( help_src_desc ); 3944help_dst_desc = compress_spatial_dimensions_to_channel ( help_dst_desc ); 3945} 3946enter_primitive ( 2 * help_dst_desc . get_size ()); 3947 3948::dnnl::algorithm softmax_alg = ::dnnl::algorithm:: softmax_accurate ; 3949if ( alg == softmax_algorithm:: log ) { 3950softmax_alg = ::dnnl::algorithm:: softmax_log ; 3951} 3952auto primitive_args = create_primitive_args_or_get < ::dnnl::softmax_forward > ( 3953::dnnl::prop_kind:: forward , softmax_alg , help_src_desc , 3954help_dst_desc , 1 ); 3955 3956insert_arg ( primitive_args . second . args , DNNL_ARG_DST , help_dst_desc , dst ); 3957insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , help_src_desc , src ); 3958 3959return exit_primitive ( execute_primitive < ::dnnl::softmax_forward > ( 3960primitive_args , 3961{{ alpha , beta , DNNL_ARG_DST , memory_desc_ext ( help_dst_desc ), dst }})); 3962} 3963 3964inline 3965sycl::event engine_ext:: async_softmax_backward ( 3966softmax_algorithm alg , softmax_mode mode , float alpha , 3967const memory_desc_ext & dst_desc , void * dst , 3968const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta , 3969const memory_desc_ext & diff_src_desc , void * diff_src ) { 3970if ( scale_parameter_preprocess ({{ alpha , beta , diff_src_desc , diff_src }})) { 3971return sycl:: event (); 3972} 3973::dnnl::memory::desc help_diff_src_desc = diff_src_desc . get_desc (); 3974::dnnl::memory::desc help_dst_desc = dst_desc . get_desc (); 3975::dnnl::memory::desc help_diff_dst_desc = diff_dst_desc . get_desc (); 3976if ( mode == softmax_mode:: instance ) { 3977help_diff_src_desc = 3978compress_spatial_dimensions_to_channel ( help_diff_src_desc ); 3979help_dst_desc = compress_spatial_dimensions_to_channel ( help_dst_desc ); 3980help_diff_dst_desc = 3981compress_spatial_dimensions_to_channel ( help_diff_dst_desc ); 3982} 3983enter_primitive ( 2 * help_diff_src_desc . get_size ()); 3984 3985::dnnl::algorithm softmax_alg = ::dnnl::algorithm:: softmax_accurate ; 3986if ( alg == softmax_algorithm:: log ) { 3987softmax_alg = ::dnnl::algorithm:: softmax_log ; 3988} 3989 3990auto primitive_args = create_primitive_args_or_get < ::dnnl::softmax_backward > ( 3991softmax_alg , help_diff_src_desc , help_diff_dst_desc , help_dst_desc , 1 , 3992create_primitive_desc < ::dnnl::softmax_forward > ( 3993::dnnl::prop_kind:: forward , softmax_alg , help_diff_src_desc , 3994help_dst_desc , 1 )); 3995insert_arg ( primitive_args . second . args , DNNL_ARG_DST , help_dst_desc , dst ); 3996insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , help_diff_dst_desc , 3997diff_dst ); 3998insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SRC , help_diff_src_desc , 3999diff_src ); 4000 4001return exit_primitive ( execute_primitive < ::dnnl::softmax_backward > ( 4002primitive_args , {{ alpha , beta , DNNL_ARG_DIFF_SRC , 4003memory_desc_ext ( help_diff_src_desc ), diff_src }})); 4004} 4005 4006inline 4007sycl::event engine_ext:: async_lrn_forward (lrn_desc & desc , float alpha , 4008const memory_desc_ext & src_desc , void * src , 4009float beta , const memory_desc_ext & dst_desc , 4010void * dst , ::dnnl::memory * workspace ) { 4011 4012if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 4013return sycl:: event (); 4014} 4015enter_primitive ( 2 * dst_desc . get_size ()); 4016auto primitive_args = create_primitive_args_or_get < ::dnnl::lrn_forward > ( 4017::dnnl::prop_kind:: forward_training , 4018::dnnl::algorithm:: lrn_across_channels , src_desc . get_desc (), 4019dst_desc . get_desc (), desc . get_local_size (), desc . get_alpha (), 4020desc . get_beta (), desc . get_k ()); 4021auto pd = 4022get_primitive_desc < ::dnnl::lrn_forward > ( primitive_args . second . primitive ); 4023::dnnl::memory ws_mem ( pd . workspace_desc (), * _eng ); 4024if ( workspace ) { 4025* workspace = ws_mem ; 4026} else { 4027insert_workspace ( src , ws_mem ); 4028} 4029 4030insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 4031src ); 4032insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 4033dst ); 4034insert_arg ( primitive_args . second . args , DNNL_ARG_WORKSPACE , ws_mem ); 4035 4036return exit_primitive ( execute_primitive < ::dnnl::lrn_forward > ( 4037primitive_args , {{ alpha , beta , DNNL_ARG_DST , dst_desc , dst }})); 4038} 4039 4040inline 4041sycl::event 4042engine_ext:: async_lrn_backward (lrn_desc & desc , float alpha , 4043const memory_desc_ext & dst_desc , void * dst , 4044const memory_desc_ext & diff_dst_desc , void * diff_dst , 4045const memory_desc_ext & src_desc , void * src , float beta , 4046const memory_desc_ext & diff_src_desc , void * diff_src , 4047::dnnl::memory * workspace ) { 4048 4049if ( scale_parameter_preprocess ({{ alpha , beta , diff_src_desc , diff_src }})) { 4050return sycl:: event (); 4051} 4052enter_primitive ( 2 * diff_src_desc . get_size ()); 4053auto primitive_args = create_primitive_args_or_get < ::dnnl::lrn_backward > ( 4054::dnnl::algorithm:: lrn_across_channels , diff_src_desc . get_desc (), 4055diff_dst_desc . get_desc (), src_desc . get_desc (), desc . get_local_size (), 4056desc . get_alpha (), desc . get_beta (), desc . get_k (), 4057create_primitive_desc < ::dnnl::lrn_forward > ( 4058::dnnl::prop_kind:: forward_training , 4059::dnnl::algorithm:: lrn_across_channels , src_desc . get_desc (), 4060dst_desc . get_desc (), desc . get_local_size (), desc . get_alpha (), 4061desc . get_beta (), desc . get_k ())); 4062 4063insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , src_desc . get_desc (), 4064src ); 4065insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 4066dst ); 4067insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , 4068diff_dst_desc . get_desc (), diff_dst ); 4069insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SRC , 4070diff_src_desc . get_desc (), diff_src ); 4071 4072if ( workspace ) { 4073insert_arg ( primitive_args . second . args , DNNL_ARG_WORKSPACE , * workspace ); 4074} else { 4075insert_arg ( primitive_args . second . args , DNNL_ARG_WORKSPACE , 4076get_workspace ( src )); 4077} 4078 4079return exit_primitive ( execute_primitive < ::dnnl::lrn_backward > ( 4080primitive_args , 4081{{ alpha , beta , DNNL_ARG_DIFF_SRC , diff_src_desc , diff_src }})); 4082} 4083 4084inline 4085size_t engine_ext:: get_batch_normalization_workspace_size ( 4086batch_normalization_ops ops , const memory_desc_ext & src_desc ) { 4087if ( ops == batch_normalization_ops:: none ) { 4088return 0 ; 4089} 4090return src_desc . get_size (); 4091} 4092 4093inline 4094sycl::event engine_ext:: async_batch_normalization_forward_inference ( 4095batch_normalization_mode mode , float epsilon , float alpha , 4096const memory_desc_ext & src_desc , void * src , float beta , 4097const memory_desc_ext & dst_desc , void * dst , 4098const memory_desc_ext & scale_bias_mean_var_desc , void * scale , void * bias , 4099void * mean , void * var ) { 4100 4101return batch_normalization_forward_internal ( 4102true, mode , epsilon , 0.f , alpha , src_desc , src , beta , dst_desc , dst , 4103scale_bias_mean_var_desc , scale , bias , scale_bias_mean_var_desc , mean , 4104var , nullptr , nullptr ); 4105} 4106 4107inline 4108sycl::event engine_ext:: async_batch_normalization_forward_inference ( 4109batch_normalization_mode mode , batch_normalization_ops ops , 4110activation_desc & adesc , float epsilon , float alpha , 4111const memory_desc_ext & src_desc , void * src , float beta , 4112const memory_desc_ext & dst_desc , void * dst , 4113const memory_desc_ext & summand_desc , void * summand , 4114const memory_desc_ext & scale_bias_desc , void * scale , void * bias , 4115const memory_desc_ext & mean_var_desc , void * mean , void * var ) { 4116 4117bool has_post_op = ( ops != batch_normalization_ops:: none ); 4118sycl::event e ; 4119enter_primitive ( src_desc . get_size () + dst_desc . get_size () * 4 + 4120scale_bias_desc . get_size () * 2 + 4121mean_var_desc . get_size () * 5 ); 4122if ( has_post_op ) { 4123void * dst_cache = allocate ( dst_desc ); 4124batch_normalization_forward_internal ( 4125true, mode , epsilon , 0.f , 1.f , src_desc , src , 0.f , dst_desc , dst_cache , 4126scale_bias_desc , scale , bias , mean_var_desc , mean , var , nullptr , 4127nullptr ); 4128 4129if ( ops == batch_normalization_ops:: add_activation ) { 4130async_sum ( 1.f , summand_desc , summand , 1.f , dst_desc , dst_cache ); 4131} 4132async_activation_forward ( adesc , 1.f , dst_desc , dst_cache , 0.f , dst_desc , 4133dst_cache ); 4134return exit_primitive ( 4135async_sum ( alpha , dst_desc , dst_cache , beta , dst_desc , dst )); 4136} 4137return exit_primitive ( batch_normalization_forward_internal ( 4138true, mode , epsilon , 0.f , alpha , src_desc , src , beta , dst_desc , dst , 4139scale_bias_desc , scale , bias , mean_var_desc , mean , var , nullptr , 4140nullptr )); 4141} 4142 4143inline 4144sycl::event engine_ext:: async_batch_normalization_forward_training ( 4145batch_normalization_mode mode , float epsilon , float factor , float alpha , 4146const memory_desc_ext & src_desc , void * src , float beta , 4147const memory_desc_ext & dst_desc , void * dst , 4148const memory_desc_ext & scale_bias_mean_var_desc , void * scale , void * bias , 4149void * running_mean , void * running_var , void * saved_mean , void * saved_var ) { 4150return batch_normalization_forward_internal ( 4151false, mode , epsilon , factor , alpha , src_desc , src , beta , dst_desc , dst , 4152scale_bias_mean_var_desc , scale , bias , scale_bias_mean_var_desc , 4153saved_mean , saved_var , running_mean , running_var ); 4154} 4155 4156inline 4157sycl::event engine_ext:: async_batch_normalization_forward_training ( 4158batch_normalization_mode mode , batch_normalization_ops ops , 4159activation_desc & adesc , float epsilon , float factor , float alpha , 4160const memory_desc_ext & src_desc , void * src , float beta , 4161const memory_desc_ext & dst_desc , void * dst , 4162const memory_desc_ext & summand_desc , void * summand , 4163const memory_desc_ext & scale_bias_desc , void * scale , void * bias , 4164const memory_desc_ext & mean_var_desc , void * running_mean , void * running_var , 4165void * saved_mean , void * saved_var , size_t workspace_size , 4166void * workspace ) { 4167enter_primitive ( src_desc . get_size () + dst_desc . get_size () * 3 + 4168mean_var_desc . get_size () * 5 + 4169scale_bias_desc . get_size () * 2 ); 4170bool has_post_op = ( ops != batch_normalization_ops:: none ); 4171sycl::event e ; 4172if ( has_post_op ) { 4173if ( workspace_size < dst_desc . get_desc (). get_size ()) { 4174throw std:: runtime_error ( "async_batch_normalization_forward_training_ex: " 4175"no sufficient workspace." ); 4176} 4177batch_normalization_forward_internal ( 4178false, mode , epsilon , factor , 1.f , src_desc , src , 0.f , dst_desc , 4179workspace , scale_bias_desc , scale , bias , mean_var_desc , 4180saved_mean , saved_var , running_mean , running_var ); 4181if ( ops == batch_normalization_ops:: add_activation ) { 4182async_sum ( 1.f , summand_desc , summand , 1.f , dst_desc , 4183workspace ); 4184} 4185return exit_primitive ( async_activation_forward ( 4186adesc , alpha , dst_desc , workspace , beta , dst_desc , dst )); 4187} 4188return exit_primitive ( batch_normalization_forward_internal ( 4189false, mode , epsilon , factor , alpha , src_desc , src , beta , dst_desc , dst , 4190scale_bias_desc , scale , bias , mean_var_desc , saved_mean , saved_var , 4191running_mean , running_var )); 4192} 4193 4194inline 4195sycl::event engine_ext:: async_batch_normalization_forward_training ( 4196batch_normalization_mode mode , batch_normalization_ops ops , 4197activation_desc & adesc , float epsilon , float factor , float alpha , 4198const memory_desc_ext & src_desc , void * src , float beta , 4199const memory_desc_ext & dst_desc , void * dst , 4200const memory_desc_ext & summand_desc , void * summand , 4201const memory_desc_ext & scale_bias_mean_var_desc , void * scale , void * bias , 4202void * running_mean , void * running_var , void * saved_mean , void * saved_var , 4203size_t workspace_size , void * workspace ) { 4204return async_batch_normalization_forward_training ( 4205mode , ops , adesc , epsilon , factor , alpha , src_desc , src , beta , dst_desc , 4206dst , summand_desc , summand , scale_bias_mean_var_desc , scale , bias , 4207scale_bias_mean_var_desc , running_mean , running_var , saved_mean , 4208saved_var , workspace_size , workspace ); 4209} 4210 4211inline 4212sycl::event engine_ext:: async_batch_normalization_backward ( 4213batch_normalization_mode mode , float epsilon , float alpha_data , 4214const memory_desc_ext & src_desc , void * src , 4215const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta_data , 4216const memory_desc_ext & diff_src_desc , void * diff_src , float alpha_param , 4217const memory_desc_ext & diff_scale_bias_mean_var_desc , void * scale , 4218float beta_param , void * diff_scale , void * diff_bias , void * saved_mean , 4219void * saved_var ) { 4220 4221return batch_normalization_backward_internal ( 4222mode , epsilon , alpha_data , src_desc , src , diff_dst_desc , diff_dst , 4223beta_data , diff_src_desc , diff_src , alpha_param , 4224diff_scale_bias_mean_var_desc , scale , nullptr , beta_param , diff_scale , 4225diff_bias , diff_scale_bias_mean_var_desc , saved_mean , saved_var ); 4226} 4227 4228inline 4229sycl::event engine_ext:: async_batch_normalization_backward ( 4230batch_normalization_mode mode , batch_normalization_ops ops , 4231activation_desc & adesc , float epsilon , float alpha_data , 4232const memory_desc_ext & src_desc , void * src , const memory_desc_ext & dst_desc , 4233void * dst , const memory_desc_ext & diff_dst_desc , void * diff_dst , 4234float beta_data , const memory_desc_ext & diff_src_desc , void * diff_src , 4235const memory_desc_ext & diff_summand_desc , void * diff_summand , 4236float alpha_param , const memory_desc_ext & diff_scale_bias_desc , void * scale , 4237void * bias , float beta_param , void * diff_scale , void * diff_bias , 4238const memory_desc_ext & mean_var_desc , void * saved_mean , void * saved_var , 4239size_t workspace_size , void * workspace ) { 4240std::vector < void *> caches ; 4241::dnnl::memory::desc real_diff_dst_desc = diff_dst_desc . get_desc (); 4242void * real_diff_dst = diff_dst ; 4243 4244if ( ops != batch_normalization_ops:: none && 4245workspace_size < dst_desc . get_desc (). get_size ()) { 4246throw std:: runtime_error ( "async_batch_normalization_backward_ex: " 4247"no sufficient workspace." ); 4248} 4249enter_primitive ( diff_scale_bias_desc . get_size () * 8 + 4250src_desc . get_size () * 3 + diff_dst_desc . get_size () * 5 + 4251diff_src_desc . get_size () + mean_var_desc . get_size () * 9 + 4252diff_summand_desc . get_size ()); 4253if ( ops == batch_normalization_ops:: add_activation ) { 4254void * diff_summand_cache = allocate ( diff_summand_desc ); 4255async_activation_backward ( adesc , 1.f , dst_desc , dst , diff_dst_desc , diff_dst , 4256dst_desc , workspace , 0.f , 4257diff_summand_desc , diff_summand_cache ); 4258async_sum ( alpha_data , diff_summand_desc , diff_summand_cache , beta_data , 4259diff_summand_desc , diff_summand ); 4260real_diff_dst_desc = diff_summand_desc . get_desc (); 4261real_diff_dst = diff_summand_cache ; 4262} else if ( ops == batch_normalization_ops:: activation ) { 4263void * diff_dst_cache = allocate ( diff_dst_desc ); 4264async_activation_backward ( adesc , 1.f , dst_desc , dst , diff_dst_desc , 4265diff_dst , dst_desc , workspace , 42660.f , diff_dst_desc , diff_dst_cache ); 4267real_diff_dst = diff_dst_cache ; 4268} 4269 4270return exit_primitive ( batch_normalization_backward_internal ( 4271mode , epsilon , alpha_data , src_desc , src , real_diff_dst_desc , 4272real_diff_dst , beta_data , diff_src_desc , diff_src , alpha_param , 4273diff_scale_bias_desc , scale , bias , beta_param , diff_scale , diff_bias , 4274mean_var_desc , saved_mean , saved_var )); 4275} 4276 4277inline 4278sycl::event engine_ext:: async_batch_normalization_backward ( 4279batch_normalization_mode mode , batch_normalization_ops ops , 4280activation_desc & adesc , float epsilon , float alpha_data , 4281const memory_desc_ext & src_desc , void * src , const memory_desc_ext & dst_desc , 4282void * dst , const memory_desc_ext & diff_dst_desc , void * diff_dst , 4283float beta_data , const memory_desc_ext & diff_src_desc , void * diff_src , 4284const memory_desc_ext & diff_summand_desc , void * diff_summand , 4285float alpha_param , const memory_desc_ext & diff_scale_bias_mean_var_desc , 4286void * scale , void * bias , float beta_param , void * diff_scale , 4287void * diff_bias , void * saved_mean , void * saved_var , 4288size_t workspace_size , void * workspace ) { 4289 4290return async_batch_normalization_backward ( 4291mode , ops , adesc , epsilon , alpha_data , src_desc , src , dst_desc , dst , 4292diff_dst_desc , diff_dst , beta_data , diff_src_desc , diff_src , 4293diff_summand_desc , diff_summand , alpha_param , 4294diff_scale_bias_mean_var_desc , scale , bias , beta_param , diff_scale , 4295diff_bias , diff_scale_bias_mean_var_desc , saved_mean , saved_var , 4296workspace_size , workspace ); 4297} 4298 4299inline 4300sycl::event 4301engine_ext:: async_convolution_forward (convolution_desc & desc , ::dnnl::algorithm alg , 4302float alpha , const memory_desc_ext & src_desc , 4303void * src , const memory_desc_ext & weight_desc , 4304void * weight , float beta , 4305const memory_desc_ext & dst_desc , void * dst ) { 4306if ( scale_parameter_preprocess ({{ alpha , beta , dst_desc , dst }})) { 4307return sycl:: event (); 4308} 4309auto help_weight_desc = 4310get_group_weight_desc ( desc . get_group_count (), weight_desc ); 4311 4312::dnnl::primitive_attr attr ; 4313attr . set_fpmath_mode ( desc . get_math_mode ()); 4314 4315auto origin_src_md = src_desc . get_desc (); 4316auto origin_dst_md = dst_desc . get_desc (); 4317auto origin_weight_md = help_weight_desc ; 4318auto src_md = transfer_memory_desc_to_format_tag_any ( origin_src_md ); 4319auto dst_md = transfer_memory_desc_to_format_tag_any ( origin_dst_md ); 4320auto weight_md = transfer_memory_desc_to_format_tag_any ( origin_weight_md ); 4321 4322auto primitive_args = 4323create_primitive_args_or_get < ::dnnl::convolution_forward > ( 4324::dnnl::prop_kind:: forward_training , alg , src_md , weight_md , dst_md , 4325desc . get_stride (), desc . get_dilate (), desc . get_padding (), 4326desc . get_padding (), attr ); 4327 4328auto pd = get_primitive_desc < ::dnnl::convolution_forward > ( 4329primitive_args . second . primitive ); 4330auto optimal_src_md = pd . src_desc (); 4331auto optimal_dst_md = pd . dst_desc (); 4332auto optimal_weight_md = pd . weights_desc (); 4333 4334enter_primitive ( 4335optimal_src_md . get_size () * 3 + optimal_dst_md . get_size () * 5 + 4336optimal_weight_md . get_size () * 3 + origin_dst_md . get_size () * 2 ); 4337 4338void * optimal_src = src , * optimal_dst = dst , * optimal_weight = weight ; 4339allocate_and_reorder_memory_to_optimal ( origin_src_md , src , optimal_src_md , 4340optimal_src ); 4341allocate_and_reorder_memory_to_optimal ( origin_weight_md , weight , 4342optimal_weight_md , optimal_weight ); 4343 4344if ( beta == 0.f ) { 4345if ( origin_dst_md != optimal_dst_md ) { 4346optimal_dst = allocate ( optimal_dst_md ); 4347} 4348} else { 4349allocate_and_reorder_memory_to_optimal ( origin_dst_md , dst , optimal_dst_md , 4350optimal_dst ); 4351} 4352 4353insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , optimal_src_md , 4354optimal_src ); 4355insert_arg ( primitive_args . second . args , DNNL_ARG_WEIGHTS , optimal_weight_md , 4356optimal_weight ); 4357insert_arg ( primitive_args . second . args , DNNL_ARG_DST , optimal_dst_md , 4358optimal_dst ); 4359 4360auto e = execute_primitive < ::dnnl::convolution_forward > ( 4361primitive_args , 4362{{ alpha , beta , DNNL_ARG_DST , optimal_dst_md , optimal_dst }}); 4363 4364if ( origin_dst_md != optimal_dst_md ) { 4365e = async_reorder ( 1.f , optimal_dst_md , optimal_dst , 0.f , origin_dst_md , 4366dst ); 4367} 4368return exit_primitive ( e ); 4369} 4370 4371inline 4372sycl::event engine_ext:: async_convolution_forward ( 4373convolution_desc & desc , ::dnnl::algorithm alg , activation_desc & adesc , 4374float alpha_0 , const memory_desc_ext & src_desc , void * src , 4375const memory_desc_ext & weight_desc , void * weight , float alpha_1 , 4376const memory_desc_ext & summand_desc , void * summand , 4377const memory_desc_ext & bias_desc , void * bias , 4378const memory_desc_ext & dst_desc , void * dst ) { 4379 4380int channel_num = bias_desc . get_element_num (); 4381auto help_weight_desc = 4382get_group_weight_desc ( desc . get_group_count (), weight_desc ); 4383::dnnl::memory::desc help_bias_desc = {{ channel_num }, 4384bias_desc . get_desc (). get_data_type (), 4385::dnnl::memory::format_tag:: a }; 4386auto origin_weight_md = help_weight_desc ; 4387auto origin_bias_md = help_bias_desc ; 4388auto origin_src_md = src_desc . get_desc (); 4389auto origin_dst_md = dst_desc . get_desc (); 4390auto src_md = transfer_memory_desc_to_format_tag_any ( origin_src_md ); 4391auto dst_md = transfer_memory_desc_to_format_tag_any ( origin_dst_md ); 4392auto weight_md = transfer_memory_desc_to_format_tag_any ( origin_weight_md ); 4393auto bias_md = transfer_memory_desc_to_format_tag_any ( origin_bias_md ); 4394 4395::dnnl::primitive_attr attr ; 4396attr . set_fpmath_mode ( desc . get_math_mode ()); 4397 4398auto primitive_args = 4399create_primitive_args_or_get < ::dnnl::convolution_forward > ( 4400::dnnl::prop_kind:: forward_training , alg , src_md , weight_md , bias_md , 4401dst_md , desc . get_stride (), desc . get_dilate (), desc . get_padding (), 4402desc . get_padding (), attr ); 4403 4404auto pd = get_primitive_desc < ::dnnl::convolution_forward > ( 4405primitive_args . second . primitive ); 4406auto optimal_src_md = pd . src_desc (); 4407auto optimal_dst_md = pd . dst_desc (); 4408auto optimal_weight_md = pd . weights_desc (); 4409auto optimal_bias_md = pd . bias_desc (); 4410 4411enter_primitive ( optimal_src_md . get_size () + 3 * optimal_weight_md . get_size () + 4412optimal_bias_md . get_size () + 7 * optimal_dst_md . get_size () + 4413summand_desc . get_size ()); 4414 4415void * optimal_src = src , * optimal_dst = dst , * optimal_weight = weight , 4416* optimal_bias = bias ; 4417allocate_and_reorder_memory_to_optimal ( origin_src_md , src , optimal_src_md , 4418optimal_src ); 4419allocate_and_reorder_memory_to_optimal ( origin_weight_md , weight , 4420optimal_weight_md , optimal_weight ); 4421allocate_and_reorder_memory_to_optimal ( origin_bias_md , bias , optimal_bias_md , 4422optimal_bias ); 4423if ( origin_dst_md != optimal_dst_md ) { 4424optimal_dst = allocate ( optimal_dst_md ); 4425} 4426 4427insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , optimal_src_md , 4428optimal_src ); 4429insert_arg ( primitive_args . second . args , DNNL_ARG_BIAS , optimal_bias_md , 4430optimal_bias ); 4431insert_arg ( primitive_args . second . args , DNNL_ARG_DST , optimal_dst_md , 4432optimal_dst ); 4433 4434void * cache = nullptr ; 4435if ( alpha_0 != 1.f ) { 4436cache = allocate ( optimal_weight_md ); 4437_q -> memcpy ( cache , optimal_weight , optimal_weight_md . get_size ()); 4438async_scale ( alpha_0 , optimal_weight_md , cache ); 4439insert_arg ( primitive_args . second . args , DNNL_ARG_WEIGHTS , optimal_weight_md , 4440cache ); 4441execute_primitive < ::dnnl::convolution_forward > ( 4442primitive_args , 4443{{ 1.f , 0.f , DNNL_ARG_DST , optimal_dst_md , optimal_dst }}); 4444} else { 4445insert_arg ( primitive_args . second . args , DNNL_ARG_WEIGHTS , optimal_weight_md , 4446optimal_weight ); 4447execute_primitive < ::dnnl::convolution_forward > ( 4448primitive_args , 4449{{ 1.f , 0.f , DNNL_ARG_DST , optimal_dst_md , optimal_dst }}); 4450} 4451if ( origin_dst_md != optimal_dst_md ) { 4452async_reorder ( 1.f , optimal_dst_md , optimal_dst , 0.f , origin_dst_md , dst ); 4453} 4454async_sum ( alpha_1 , summand_desc , summand , 1.f , dst_desc , dst ); 4455return exit_primitive ( 4456async_activation_forward ( adesc , 1.f , dst_desc , dst , 0.f , dst_desc , dst )); 4457} 4458 4459inline 4460sycl::event engine_ext:: async_convolution_backward_data ( 4461convolution_desc & desc , ::dnnl::algorithm alg , float alpha , 4462const memory_desc_ext & weight_desc , void * weight , 4463const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta , 4464const memory_desc_ext & diff_src_desc , void * diff_src ) { 4465 4466if ( scale_parameter_preprocess ({{ alpha , beta , diff_dst_desc , diff_dst }})) { 4467return sycl:: event (); 4468} 4469 4470auto help_weight_desc = 4471get_group_weight_desc ( desc . get_group_count (), weight_desc ); 4472 4473auto origin_weight_md = help_weight_desc ; 4474auto origin_diff_src_md = diff_src_desc . get_desc (); 4475auto origin_diff_dst_md = diff_dst_desc . get_desc (); 4476auto diff_src_md = transfer_memory_desc_to_format_tag_any ( origin_diff_src_md ); 4477auto diff_dst_md = transfer_memory_desc_to_format_tag_any ( origin_diff_dst_md ); 4478auto weight_md = transfer_memory_desc_to_format_tag_any ( origin_weight_md ); 4479 4480::dnnl::primitive_attr attr ; 4481attr . set_fpmath_mode ( desc . get_math_mode ()); 4482 4483auto forward_primitive = create_primitive_desc < ::dnnl::convolution_forward > ( 4484::dnnl::prop_kind:: forward_training , ::dnnl::algorithm:: convolution_auto , 4485diff_src_md , weight_md , diff_dst_md , desc . get_stride (), desc . get_dilate (), 4486desc . get_padding (), desc . get_padding (), attr ); 4487 4488auto primitive_args = 4489create_primitive_args_or_get < ::dnnl::convolution_backward_data > ( 4490::dnnl::algorithm:: convolution_auto , diff_src_md , weight_md , 4491diff_dst_md , desc . get_stride (), desc . get_dilate (), desc . get_padding (), 4492desc . get_padding (), forward_primitive , attr ); 4493 4494auto pd = get_primitive_desc < ::dnnl::convolution_backward_data > ( 4495primitive_args . second . primitive ); 4496auto optimal_diff_src_md = pd . diff_src_desc (); 4497auto optimal_diff_dst_md = pd . diff_dst_desc (); 4498auto optimal_weight_md = pd . weights_desc (); 4499 4500enter_primitive ( 5 * optimal_diff_src_md . get_size () + 4501optimal_diff_dst_md . get_size () + 4502optimal_weight_md . get_size ()); 4503 4504void * optimal_diff_src = diff_src , * optimal_diff_dst = diff_dst , 4505* optimal_weight = weight ; 4506allocate_and_reorder_memory_to_optimal ( origin_diff_dst_md , diff_dst , 4507optimal_diff_dst_md , optimal_diff_dst ); 4508allocate_and_reorder_memory_to_optimal ( origin_weight_md , weight , 4509optimal_weight_md , optimal_weight ); 4510if ( beta == 0.f ) { 4511if ( origin_diff_src_md != optimal_diff_src_md ) { 4512optimal_diff_src = allocate ( optimal_diff_src_md ); 4513} 4514} else { 4515allocate_and_reorder_memory_to_optimal ( 4516origin_diff_src_md , diff_src , optimal_diff_src_md , optimal_diff_src ); 4517} 4518 4519insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , optimal_diff_dst_md , 4520optimal_diff_dst ); 4521insert_arg ( primitive_args . second . args , DNNL_ARG_WEIGHTS , optimal_weight_md , 4522optimal_weight ); 4523insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_SRC , optimal_diff_src_md , 4524optimal_diff_src ); 4525 4526auto e = execute_primitive < ::dnnl::convolution_backward_data > ( 4527primitive_args , 4528{{ alpha , beta , DNNL_ARG_DIFF_SRC , optimal_diff_src_md , optimal_diff_src }}); 4529 4530if ( origin_diff_src_md != optimal_diff_src_md ) { 4531e = async_reorder ( 1.f , optimal_diff_src_md , optimal_diff_src , 0.f , 4532origin_diff_src_md , diff_src ); 4533} 4534return exit_primitive ( e ); 4535} 4536 4537inline 4538sycl::event engine_ext:: async_convolution_backward_weight ( 4539convolution_desc & desc , ::dnnl::algorithm alg , float alpha , 4540const memory_desc_ext & src_desc , void * src , 4541const memory_desc_ext & diff_dst_desc , void * diff_dst , float beta , 4542const memory_desc_ext & diff_weight_desc , void * diff_weight ) { 4543 4544if ( scale_parameter_preprocess ( 4545{{ alpha , beta , diff_weight_desc , diff_weight }})) { 4546return sycl:: event (); 4547} 4548 4549auto help_diff_weight_desc = 4550get_group_weight_desc ( desc . get_group_count (), diff_weight_desc ); 4551 4552::dnnl::primitive_attr attr ; 4553attr . set_fpmath_mode ( desc . get_math_mode ()); 4554 4555auto origin_diff_weight_md = help_diff_weight_desc ; 4556auto origin_src_md = src_desc . get_desc (); 4557auto origin_diff_dst_md = diff_dst_desc . get_desc (); 4558auto src_md = transfer_memory_desc_to_format_tag_any ( origin_src_md ); 4559auto diff_dst_md = transfer_memory_desc_to_format_tag_any ( origin_diff_dst_md ); 4560auto diff_weight_md = 4561transfer_memory_desc_to_format_tag_any ( origin_diff_weight_md ); 4562 4563auto forward_primitive = create_primitive_desc < ::dnnl::convolution_forward > ( 4564::dnnl::prop_kind:: forward_training , ::dnnl::algorithm:: convolution_auto , 4565src_md , diff_weight_md , diff_dst_md , desc . get_stride (), desc . get_dilate (), 4566desc . get_padding (), desc . get_padding (), attr ); 4567 4568auto primitive_args = 4569create_primitive_args_or_get < ::dnnl::convolution_backward_weights > ( 4570::dnnl::algorithm:: convolution_auto , src_md , diff_weight_md , 4571diff_dst_md , desc . get_stride (), desc . get_dilate (), desc . get_padding (), 4572desc . get_padding (), forward_primitive , attr ); 4573 4574auto pd = get_primitive_desc < ::dnnl::convolution_backward_weights > ( 4575primitive_args . second . primitive ); 4576auto optimal_src_md = pd . src_desc (); 4577auto optimal_diff_dst_md = pd . diff_dst_desc (); 4578auto optimal_diff_weight_md = pd . diff_weights_desc (); 4579 4580enter_primitive ( optimal_diff_weight_md . get_size () * 5 + 4581optimal_diff_dst_md . get_size () + optimal_src_md . get_size ()); 4582 4583void * optimal_src = src , * optimal_diff_dst = diff_dst , 4584* optimal_diff_weight = diff_weight ; 4585allocate_and_reorder_memory_to_optimal ( origin_diff_dst_md , diff_dst , 4586optimal_diff_dst_md , optimal_diff_dst ); 4587allocate_and_reorder_memory_to_optimal ( origin_src_md , src , optimal_src_md , 4588optimal_src ); 4589if ( beta == 0.f ) { 4590if ( origin_diff_weight_md != optimal_diff_weight_md ) { 4591optimal_diff_weight = allocate ( optimal_diff_weight_md ); 4592} 4593} else { 4594allocate_and_reorder_memory_to_optimal ( origin_diff_weight_md , diff_weight , 4595optimal_diff_weight_md , 4596optimal_diff_weight ); 4597} 4598 4599insert_arg ( primitive_args . second . args , DNNL_ARG_SRC , optimal_src_md , 4600optimal_src ); 4601insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_DST , optimal_diff_dst_md , 4602optimal_diff_dst ); 4603insert_arg ( primitive_args . second . args , DNNL_ARG_DIFF_WEIGHTS , 4604optimal_diff_weight_md , optimal_diff_weight ); 4605 4606auto e = execute_primitive < ::dnnl::convolution_backward_weights > ( 4607primitive_args , {{ alpha , beta , DNNL_ARG_DIFF_WEIGHTS , 4608optimal_diff_weight_md , optimal_diff_weight }}); 4609 4610if ( origin_diff_weight_md != optimal_diff_weight_md ) { 4611e = async_reorder ( 1.f , optimal_diff_weight_md , optimal_diff_weight , 0.f , 4612origin_diff_weight_md , diff_weight ); 4613} 4614return exit_primitive ( e ); 4615} 4616 4617inline 4618sycl::event engine_ext:: async_convolution_backward_bias ( 4619float alpha , const memory_desc_ext & diff_dst_desc , void * diff_dst , 4620float beta , const memory_desc_ext & diff_bias_desc , void * diff_bias ) { 4621return async_reduction (reduction_op:: sum , alpha , diff_dst_desc , diff_dst , beta , 4622diff_bias_desc , diff_bias ); 4623} 4624 4625inline 4626void engine_ext:: rnn_get_weight_space_size ( const rnn_desc & desc , 4627size_t * weight_space_size ) { 4628* weight_space_size = 0 ; 4629rnn_forward_internal ( desc , ::dnnl::prop_kind:: forward_inference , 4630memory_desc_ext (), nullptr , memory_desc_ext (), nullptr , 4631memory_desc_ext (), nullptr , nullptr , memory_desc_ext (), 4632nullptr , nullptr , 0 , nullptr , 0 , nullptr , 0 , nullptr , true, 4633weight_space_size , nullptr , nullptr ); 4634return ; 4635} 4636 4637inline 4638void engine_ext:: rnn_get_scratchpad_workspace_size ( 4639const rnn_desc & desc , ::dnnl::prop_kind kind , 4640const memory_desc_ext & src_desc , size_t * scratchpad_size , 4641size_t * workspace_size ) { 4642* workspace_size = 0 ; 4643* scratchpad_size = 0 ; 4644rnn_forward_internal ( desc , kind , src_desc , nullptr , memory_desc_ext (), 4645nullptr , memory_desc_ext (), nullptr , nullptr , 4646memory_desc_ext (), nullptr , nullptr , 0 , nullptr , 0 , 4647nullptr , 0 , nullptr , true, nullptr , workspace_size , 4648scratchpad_size ); 4649return ; 4650} 4651 4652inline 4653sycl::event engine_ext:: async_rnn_forward ( 4654const rnn_desc & desc , ::dnnl::prop_kind kind , 4655const memory_desc_ext & src_desc , void * src , const memory_desc_ext & dst_desc , 4656void * dst , const memory_desc_ext & iter_desc , void * src_iter , void * dst_iter , 4657const memory_desc_ext & iter_c_desc , void * src_iter_c , void * dst_iter_c , 4658size_t weight_size , void * weight , size_t scratchpad_size , void * scratchpad , 4659size_t workspace_size , void * workspace ) { 4660 4661return rnn_forward_internal ( 4662desc , kind , src_desc , src , dst_desc , dst , iter_desc , src_iter , dst_iter , 4663iter_c_desc , src_iter_c , dst_iter_c , weight_size , weight , workspace_size , 4664workspace , scratchpad_size , scratchpad , false, nullptr , nullptr , 4665nullptr ); 4666} 4667 4668inline 4669sycl::event engine_ext:: async_rnn_backward ( 4670const rnn_desc & desc , const memory_desc_ext & dst_desc , void * dst , 4671void * diff_dst , const memory_desc_ext & src_desc , void * src , void * diff_src , 4672const memory_desc_ext & iter_desc , void * src_iter , void * diff_dst_iter , 4673void * diff_src_iter , const memory_desc_ext & iter_c_desc , void * src_iter_c , 4674void * diff_dst_iter_c , void * diff_src_iter_c , size_t weight_size , 4675void * weight , void * diff_weight , size_t scratchpad_size , void * scratchpad , 4676size_t workspace_size , void * workspace ) { 4677::dnnl::memory::data_type src_dt ; 4678::dnnl::memory::format_tag src_format_tag ; 4679rnn_mode mode ; 4680rnn_memory_format_tag format_tag ; 4681rnn_bias_mode bias_mode ; 4682rnn_direction direction ; 4683dpct::library_data_t dt ; 4684int direction_num = 1 , input_size = 0 , hidden_size = 0 , projection_size = 0 , 4685layer_size = 0 , gate_num = 1 , output_size = 0 , data_type_size = 0 , 4686seq_length = 1 , batch_size = 1 ; 4687void * last_layer_cache = nullptr ; 4688void * hidden_layer_cache = nullptr ; 4689sycl::event e ; 4690enter_primitive ( src_desc . get_size () * 2 ); 4691std::vector < int > offset ( 9 , 0 ); 4692std::vector < void *> data = { 4693src , 4694dst , 4695(uint8_t * ) src_iter + iter_desc . get_size (), 4696nullptr , 4697(uint8_t * ) src_iter_c + iter_c_desc . get_size (), 4698nullptr , 4699(uint8_t * ) weight + weight_size , 4700(uint8_t * ) workspace + workspace_size , 4701diff_src , 4702diff_dst , 4703(uint8_t * ) diff_src_iter + iter_desc . get_size (), 4704(uint8_t * ) diff_dst_iter + iter_desc . get_size (), 4705(uint8_t * ) diff_src_iter_c + iter_c_desc . get_size (), 4706(uint8_t * ) diff_dst_iter_c + iter_c_desc . get_size (), 4707(uint8_t * ) diff_weight + weight_size , 4708scratchpad }; 4709 4710desc . get ( & mode , & bias_mode , & direction , & dt , & input_size , & hidden_size , 4711& projection_size , & layer_size ); 4712 4713get_rnn_configuration ( src_desc . get_desc (), direction , mode , dt , hidden_size , 4714& src_dt , & src_format_tag , & projection_size , 4715& output_size , & seq_length , & batch_size , & direction_num , 4716& gate_num ); 4717 4718if ( direction == rnn_direction:: bidirectional ) { 4719if ( layer_size > 1 ) { 4720last_layer_cache = allocate ( src_desc ); 4721hidden_layer_cache = allocate ( src_desc ); 4722data [ 8 ] = last_layer_cache ; 4723} 4724e = execute_rnn_backward_primitive ( 4725mode , ::dnnl::rnn_direction:: bidirectional_concat , bias_mode , src_dt , 4726src_format_tag , seq_length , batch_size , output_size , 2 * output_size , 1 , 4727direction_num , hidden_size , gate_num , projection_size , data , offset , 1 ); 4728if ( layer_size > 1 ) { 4729data [ 8 ] = hidden_layer_cache ; 4730data [ 9 ] = last_layer_cache ; 4731e = execute_rnn_backward_primitive ( 4732mode , ::dnnl::rnn_direction:: bidirectional_sum , bias_mode , src_dt , 4733src_format_tag , seq_length , batch_size , output_size , output_size , 1 , 4734direction_num , hidden_size , gate_num , projection_size , data , offset , 4735layer_size - 1 ); 4736_q -> memcpy ( diff_src , 4737(( layer_size - 1 ) % 2 == 0 ) ? last_layer_cache 4738: hidden_layer_cache , 4739src_desc . get_size ()); 4740} 4741} else { 4742e = execute_rnn_backward_primitive ( 4743mode , ::dnnl::rnn_direction:: unidirectional_left2right , bias_mode , 4744src_dt , src_format_tag , seq_length , batch_size , output_size , 4745output_size , layer_size , direction_num , hidden_size , gate_num , 4746projection_size , data , offset , 1 ); 4747} 4748 4749return exit_primitive ( e ); 4750} 4751 4752inline 4753size_t engine_ext:: get_dropout_state_size (){ 4754#ifndef __INTEL_MKL__ 4755throw std:: runtime_error ( "The oneAPI Math Kernel Library (oneMKL) " 4756"Interfaces Project does not support this API." ); 4757#else 4758auto r = get_internal_resource ( _q ); 4759if ( r -> random_engine_state_size == -1 ){ 4760auto rand_engine = rng_engine_t ( * _q , 0 ); 4761r -> random_engine_state_size = 4762oneapi::mkl::rng:: get_state_size ( rand_engine ); 4763} 4764return r -> random_engine_state_size ; 4765#endif 4766} 4767 4768inline size_t 4769engine_ext:: get_dropout_workspace_size ( const memory_desc_ext & src_desc ) { 4770return src_desc . get_size (); 4771} 4772 4773inline 4774sycl::event engine_ext:: async_dropout_forward (dropout_desc & desc , 4775const memory_desc_ext & src_desc , 4776void * src , 4777const memory_desc_ext & dst_desc , 4778void * dst , void * workspace , 4779size_t workspace_size ) { 4780if ( workspace_size < src_desc . get_size ()) { 4781throw std:: runtime_error ( "async_dropout_forward: no sufficient workspace." ); 4782} 4783enter_primitive ( src_desc . get_size () * 2 + dst_desc . get_size () * 2 ); 4784float p = desc . get_probability (); 4785if ( p == 1.f ) { 4786return _q -> memset ( dst , 0 , dst_desc . get_size ()); 4787} else if ( p == 0.f ) { 4788return async_reorder ( 1.f , src_desc , src , 0.f , dst_desc , dst ); 4789} 4790 4791float scale_factor = 1.f / ( 1.f - p ); 4792void * cache = workspace ; 4793 4794memory_desc_ext rng_data_desc ( 4795::dnnl::memory:: desc ( src_desc . get_dims (), ::dnnl::memory::data_type:: s32 , 4796src_desc . get_strides ())); 4797if ( src_desc . get_desc (). get_data_type () != ::dnnl::memory::data_type:: s32 ) { 4798cache = allocate ( rng_data_desc ); 4799} 4800 4801desc . generate ( _q , get_dropout_state_size (), rng_data_desc . get_element_num (), 4802(std::int32_t * ) cache ); 4803 4804if ( cache == workspace ) { 4805async_scale ( scale_factor , src_desc , workspace ); 4806} else { 4807async_reorder ( scale_factor , rng_data_desc , cache , 0.f , src_desc , workspace ); 4808} 4809 4810auto primitive_args = create_primitive_args_or_get < ::dnnl::binary > ( 4811::dnnl::algorithm:: binary_mul , src_desc . get_desc (), src_desc . get_desc (), 4812dst_desc . get_desc ()); 4813 4814insert_arg ( primitive_args . second . args , DNNL_ARG_SRC_0 , src_desc . get_desc (), 4815src ); 4816insert_arg ( primitive_args . second . args , DNNL_ARG_SRC_1 , src_desc . get_desc (), 4817workspace ); 4818insert_arg ( primitive_args . second . args , DNNL_ARG_DST , dst_desc . get_desc (), 4819dst ); 4820 4821return exit_primitive ( execute_primitive < ::dnnl::binary > ( primitive_args )); 4822} 4823 4824inline 4825sycl::event engine_ext:: async_dropout_backward ( 4826dropout_desc & desc , const memory_desc_ext & diff_dst_desc , 4827void * diff_dst , const memory_desc_ext & diff_src_desc , void * diff_src , 4828void * workspace , size_t workspace_size ) { 4829enter_primitive ( 2 * diff_src_desc . get_size ()); 4830float p = desc . get_probability (); 4831if ( p == 1.f ) { 4832return _q -> memset ( diff_src , 0 , diff_src_desc . get_size ()); 4833} else if ( p == 0.f ) { 4834return async_reorder ( 1.f , diff_dst_desc , diff_dst , 0.f , diff_src_desc , 4835diff_src ); 4836} 4837 4838auto primitive_args = create_primitive_args_or_get < ::dnnl::binary > ( 4839::dnnl::algorithm:: binary_mul , diff_dst_desc . get_desc (), 4840diff_dst_desc . get_desc (), diff_src_desc . get_desc ()); 4841 4842insert_arg ( primitive_args . second . args , DNNL_ARG_SRC_0 , 4843diff_dst_desc . get_desc (), diff_dst ); 4844insert_arg ( primitive_args . second . args , DNNL_ARG_SRC_1 , 4845diff_dst_desc . get_desc (), workspace ); 4846insert_arg ( primitive_args . second . args , DNNL_ARG_DST , diff_src_desc . get_desc (), 4847diff_src ); 4848 4849return exit_primitive ( execute_primitive < ::dnnl::binary > ( primitive_args )); 4850} 4851} // namespace dnnl 4852} // namespace dpct 4853 4854#endif // __DPCT_DNNL_UTILS_HPP__