ggml-sycl.cpp 207 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include <algorithm>
  12. #include <assert.h>
  13. #include <atomic>
  14. #include <cinttypes>
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <cstdlib>
  18. #include <float.h>
  19. #include <limits>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <vector>
  23. #include <cmath>
  24. #include <iostream>
  25. #include <fstream>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <regex>
  29. #include <sycl/sycl.hpp>
  30. #include <sycl/half_type.hpp>
  31. #include "ggml-sycl.h"
  32. #include "ggml.h"
  33. #include "ggml-backend-impl.h"
  34. #include "ggml-sycl/backend.hpp"
  35. #include "ggml-sycl/presets.hpp"
  36. #include "ggml-sycl/gemm.hpp"
  37. bool ggml_sycl_loaded(void);
  38. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  39. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  40. void ggml_sycl_set_main_device(int main_device);
  41. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  42. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  43. bool ggml_backend_is_sycl(ggml_backend_t backend);
  44. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  45. static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer);
  46. static inline int get_sycl_env(const char *env_name, int default_val);
  47. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  48. const void *ptr_src, size_t size) {
  49. char *host_buf = (char *)malloc(size);
  50. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  51. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  52. free(host_buf);
  53. }
  54. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  55. typedef void (*ggml_sycl_func_t)(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  56. typedef void (*ggml_sycl_op_mul_mat_t)(
  57. ggml_backend_sycl_context & ctx,
  58. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  59. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  60. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  61. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  62. const queue_ptr &stream);
  63. typedef void (*ggml_sycl_op_flatten_t)(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  64. const ggml_tensor *src1,
  65. ggml_tensor *dst, const float *src0_dd,
  66. const float *src1_dd, float *dst_dd,
  67. const queue_ptr &main_stream);
  68. static __dpct_inline__ float op_repeat(const float a, const float b) {
  69. return b;
  70. GGML_UNUSED(a);
  71. }
  72. static __dpct_inline__ float op_add(const float a, const float b) {
  73. return a + b;
  74. }
  75. static __dpct_inline__ float op_mul(const float a, const float b) {
  76. return a * b;
  77. }
  78. static __dpct_inline__ float op_div(const float a, const float b) {
  79. return a / b;
  80. }
  81. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  82. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  83. int ne0, int ne1, int ne2, int ne3,
  84. int ne10, int ne11, int ne12, int ne13,
  85. /*int s0, */ int s1, int s2, int s3,
  86. /*int s10,*/ int s11, int s12, int s13,
  87. const sycl::nd_item<3> &item_ct1) {
  88. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  89. item_ct1.get_local_id(2);
  90. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  91. item_ct1.get_local_id(1));
  92. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  93. item_ct1.get_local_id(0)) /
  94. ne3;
  95. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  96. item_ct1.get_local_id(0)) %
  97. ne3;
  98. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  99. return;
  100. }
  101. const int i11 = i1 % ne11;
  102. const int i12 = i2 % ne12;
  103. const int i13 = i3 % ne13;
  104. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  105. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  106. const size_t i_dst = i_src0;
  107. const src0_t * src0_row = src0 + i_src0;
  108. const src1_t * src1_row = src1 + i_src1;
  109. dst_t * dst_row = dst + i_dst;
  110. for (int i0 = i0s; i0 < ne0;
  111. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  112. const int i10 = i0 % ne10;
  113. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  114. }
  115. }
  116. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  117. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  118. int ne0, int ne1, int ne2, int ne3,
  119. int ne10, int ne11, int ne12, int ne13,
  120. /*int s0, */ int s1, int s2, int s3,
  121. /*int s10,*/ int s11, int s12, int s13,
  122. const sycl::nd_item<3> &item_ct1) {
  123. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  124. item_ct1.get_local_id(2);
  125. const int i3 = i/(ne2*ne1*ne0);
  126. const int i2 = (i/(ne1*ne0)) % ne2;
  127. const int i1 = (i/ne0) % ne1;
  128. const int i0 = i % ne0;
  129. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  130. return;
  131. }
  132. const int i11 = i1 % ne11;
  133. const int i12 = i2 % ne12;
  134. const int i13 = i3 % ne13;
  135. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  136. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  137. const size_t i_dst = i_src0;
  138. const src0_t * src0_row = src0 + i_src0;
  139. const src1_t * src1_row = src1 + i_src1;
  140. dst_t * dst_row = dst + i_dst;
  141. const int i10 = i0 % ne10;
  142. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  143. }
  144. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  145. const int ne10, const int ne11, const int ne12,
  146. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  147. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  148. item_ct1.get_local_id(2);
  149. if (i >= ne) {
  150. return;
  151. }
  152. int src1_idx = i - offset;
  153. int oz = src1_idx / nb2;
  154. int oy = (src1_idx - (oz * nb2)) / nb1;
  155. int ox = src1_idx % nb1;
  156. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  157. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  158. } else {
  159. dst[i] = x[i];
  160. }
  161. }
  162. static void gelu_f32(const float * x, float * dst, const int k,
  163. const sycl::nd_item<3> &item_ct1) {
  164. const float GELU_COEF_A = 0.044715f;
  165. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  166. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  167. item_ct1.get_local_id(2);
  168. if (i >= k) {
  169. return;
  170. }
  171. float xi = x[i];
  172. dst[i] = 0.5f * xi *
  173. (1.0f +
  174. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  175. }
  176. static void silu_f32(const float * x, float * dst, const int k,
  177. const sycl::nd_item<3> &item_ct1) {
  178. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  179. item_ct1.get_local_id(2);
  180. if (i >= k) {
  181. return;
  182. }
  183. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  184. }
  185. static void gelu_quick_f32(const float *x, float *dst, int k,
  186. const sycl::nd_item<3> &item_ct1) {
  187. const float GELU_QUICK_COEF = -1.702f;
  188. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  189. item_ct1.get_local_id(2);
  190. if (i >= k) {
  191. return;
  192. }
  193. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  194. }
  195. static void tanh_f32(const float *x, float *dst, int k,
  196. const sycl::nd_item<3> &item_ct1) {
  197. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  198. item_ct1.get_local_id(2);
  199. if (i >= k) {
  200. return;
  201. }
  202. dst[i] = sycl::tanh((float)(x[i]));
  203. }
  204. static void relu_f32(const float * x, float * dst, const int k,
  205. const sycl::nd_item<3> &item_ct1) {
  206. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  207. item_ct1.get_local_id(2);
  208. if (i >= k) {
  209. return;
  210. }
  211. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  212. }
  213. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  214. const sycl::nd_item<3> &item_ct1) {
  215. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  216. item_ct1.get_local_id(2);
  217. if (i >= k) {
  218. return;
  219. }
  220. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  221. }
  222. static void hardswish_f32(const float * x, float * dst, const int k,
  223. const sycl::nd_item<3> &item_ct1) {
  224. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  225. item_ct1.get_local_id(2);
  226. if (i >= k) {
  227. return;
  228. }
  229. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  230. }
  231. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  232. const sycl::nd_item<3> &item_ct1) {
  233. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  234. item_ct1.get_local_id(2);
  235. if (i >= k) {
  236. return;
  237. }
  238. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  239. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  240. }
  241. static void sqr_f32(const float * x, float * dst, const int k,
  242. const sycl::nd_item<3> &item_ct1) {
  243. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  244. item_ct1.get_local_id(2);
  245. if (i >= k) {
  246. return;
  247. }
  248. dst[i] = x[i] * x[i];
  249. }
  250. static void upscale_f32(const float *x, float *dst, const int nb00, const int nb01,
  251. const int nb02, const int nb03, const int ne10, const int ne11,
  252. const int ne12, const int ne13, const float sf0, const float sf1,
  253. const float sf2, const float sf3, const sycl::nd_item<1> &item_ct1) {
  254. int index = item_ct1.get_local_id(0) +
  255. item_ct1.get_group(0) * item_ct1.get_local_range(0);
  256. if (index >= ne10 * ne11 * ne12 * ne13) {
  257. return;
  258. }
  259. // operation
  260. int i10 = index % ne10;
  261. int i11 = (index / ne10) % ne11;
  262. int i12 = (index / (ne10 * ne11)) % ne12;
  263. int i13 = (index / (ne10 * ne11 * ne12)) % ne13;
  264. int i00 = i10 / sf0;
  265. int i01 = i11 / sf1;
  266. int i02 = i12 / sf2;
  267. int i03 = i13 / sf3;
  268. dst[index] = *(float *)((char *)x + i03 * nb03 + i02 * nb02 + i01 * nb01 + i00 * nb00);
  269. }
  270. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  271. const sycl::nd_item<3> &item_ct1) {
  272. int nidx = item_ct1.get_local_id(2) +
  273. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  274. if (nidx >= ne0) {
  275. return;
  276. }
  277. // operation
  278. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  279. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  280. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  281. item_ct1.get_group(0) < ne02) {
  282. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  283. item_ct1.get_group(0) * ne00 * ne01;
  284. dst[offset_dst] = x[offset_src];
  285. } else {
  286. dst[offset_dst] = 0.0f;
  287. }
  288. }
  289. template<int QUANT_BLOCK_TILE>
  290. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  291. const sycl::nd_item<3> &item_ct1) {
  292. const int ix = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  293. item_ct1.get_local_id(2)) * QUANT_BLOCK_TILE;
  294. if (ix >= kx_padded) {
  295. return;
  296. }
  297. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  298. item_ct1.get_local_id(1);
  299. const int i_padded = iy*kx_padded + ix;
  300. block_q8_1 * y = (block_q8_1 *) vy;
  301. const int ib = i_padded / QK8_1; // block index
  302. const int iqs = i_padded % QK8_1; // quant index
  303. typedef sycl::vec<float, QUANT_BLOCK_TILE> TC;
  304. typedef sycl::vec<int8_t, QUANT_BLOCK_TILE> TQ;
  305. TC zeros;
  306. TQ qzeros;
  307. #pragma unroll
  308. for (int i = 0; i < QUANT_BLOCK_TILE; i++)
  309. {
  310. zeros[i] = 0.f;
  311. qzeros[i] = 0;
  312. }
  313. const TC xi = ix < kx ? *(TC *)&x[iy * kx + ix] : zeros;
  314. float sum = xi[0];
  315. float amax = sycl::fabs(xi[0]);
  316. #pragma unroll
  317. for (int i = 1; i < QUANT_BLOCK_TILE; i++)
  318. {
  319. sum += xi[i];
  320. amax = sycl::fmax(sycl::fabs(xi[i]), amax);
  321. }
  322. sum = warp_reduce_sum(sum, item_ct1);
  323. amax = warp_reduce_max(amax, item_ct1);
  324. const float d = amax / 127;
  325. TQ q = qzeros;
  326. if (amax != 0.0f)
  327. {
  328. #pragma unroll
  329. for (int i = 0; i < QUANT_BLOCK_TILE; i++) {
  330. q[i] = sycl::round(xi[i] / d);
  331. }
  332. }
  333. *(TQ *)&y[ib].qs[iqs] = q;
  334. if (iqs > 0) {
  335. return;
  336. }
  337. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  338. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  339. }
  340. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  341. static void k_get_rows(
  342. const void * src0, const int32_t * src1, dst_t * dst,
  343. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  344. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  345. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  346. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  347. size_t s10, size_t s11, size_t s12,
  348. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  349. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  350. item_ct1.get_local_id(2)) *
  351. 2;
  352. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  353. item_ct1.get_local_id(1);
  354. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  355. item_ct1.get_local_id(0)) /
  356. ne12;
  357. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  358. item_ct1.get_local_id(0)) %
  359. ne12;
  360. if (i00 >= ne00) {
  361. return;
  362. }
  363. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  364. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  365. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  366. const int ib = i00/qk; // block index
  367. const int iqs = (i00%qk)/qr; // quant index
  368. const int iybs = i00 - i00%qk; // dst block start index
  369. const int y_offset = qr == 1 ? 1 : qk/2;
  370. // dequantize
  371. dfloat2 v;
  372. dequantize_kernel(src0_row, ib, iqs, v);
  373. dst_row[iybs + iqs + 0] = v.x();
  374. dst_row[iybs + iqs + y_offset] = v.y();
  375. }
  376. template<typename src0_t, typename dst_t>
  377. static void k_get_rows_float(
  378. const src0_t * src0, const int32_t * src1, dst_t * dst,
  379. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  380. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  381. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  382. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  383. size_t s10, size_t s11, size_t s12,
  384. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  385. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  386. item_ct1.get_local_id(2);
  387. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  388. item_ct1.get_local_id(1);
  389. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  390. item_ct1.get_local_id(0)) /
  391. ne12;
  392. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  393. item_ct1.get_local_id(0)) %
  394. ne12;
  395. if (i00 >= ne00) {
  396. return;
  397. }
  398. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  399. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  400. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  401. dst_row[i00] = src0_row[i00];
  402. }
  403. static void mul_mat_p021_f16_f32(
  404. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  405. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  406. const sycl::nd_item<3> &item_ct1) {
  407. const sycl::half *x = (const sycl::half *)vx;
  408. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  409. item_ct1.get_local_id(1);
  410. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  411. item_ct1.get_local_id(0);
  412. const int channel_x = channel / (nchannels_y / nchannels_x);
  413. const int nrows_y = ncols_x;
  414. const int nrows_dst = nrows_x;
  415. const int row_dst = row_x;
  416. float tmp = 0.0f;
  417. for (int col_x0 = 0; col_x0 < ncols_x;
  418. col_x0 += item_ct1.get_local_range(2)) {
  419. const int col_x = col_x0 + item_ct1.get_local_id(2);
  420. if (col_x >= ncols_x) {
  421. break;
  422. }
  423. // x is transposed and permuted
  424. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  425. const float xi =
  426. sycl::vec<sycl::half, 1>(x[ix])
  427. .convert<float, sycl::rounding_mode::automatic>()[0];
  428. const int row_y = col_x;
  429. // y is not transposed but permuted
  430. const int iy = channel*nrows_y + row_y;
  431. tmp += xi * y[iy];
  432. }
  433. // dst is not transposed and not permuted
  434. const int idst = channel*nrows_dst + row_dst;
  435. // sum up partial sums and write back result
  436. #pragma unroll
  437. for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
  438. tmp +=
  439. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  440. }
  441. if (item_ct1.get_local_id(2) == 0) {
  442. dst[idst] = tmp;
  443. }
  444. }
  445. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  446. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  447. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  448. const sycl::nd_item<3> &item_ct1) {
  449. const sycl::half *x = (const sycl::half *)vx;
  450. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  451. item_ct1.get_local_id(1);
  452. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  453. item_ct1.get_local_id(0);
  454. const int channel_x = channel / channel_x_divisor;
  455. const int nrows_y = ncols_x;
  456. const int nrows_dst = nrows_x;
  457. const int row_dst = row_x;
  458. const int idst = channel*nrows_dst + row_dst;
  459. float tmp = 0.0f;
  460. for (int col_x0 = 0; col_x0 < ncols_x;
  461. col_x0 += item_ct1.get_local_range(2)) {
  462. const int col_x = col_x0 + item_ct1.get_local_id(2);
  463. if (col_x >= ncols_x) {
  464. break;
  465. }
  466. const int row_y = col_x;
  467. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  468. const int iy = channel*nrows_y + row_y;
  469. const float xi =
  470. sycl::vec<sycl::half, 1>(x[ix])
  471. .convert<float, sycl::rounding_mode::automatic>()[0];
  472. tmp += xi * y[iy];
  473. }
  474. // sum up partial sums and write back result
  475. #pragma unroll
  476. for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
  477. tmp +=
  478. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  479. }
  480. if (item_ct1.get_local_id(2) == 0) {
  481. dst[idst] = tmp;
  482. }
  483. }
  484. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  485. const float * xi = (const float *) cxi;
  486. float * dsti = (float *) cdsti;
  487. *dsti = *xi;
  488. }
  489. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  490. const float * xi = (const float *) cxi;
  491. sycl::half *dsti = (sycl::half *)cdsti;
  492. *dsti = sycl::vec<float, 1>(*xi)
  493. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  494. }
  495. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  496. const sycl::half *xi = (const sycl::half *)cxi;
  497. sycl::half *dsti = (sycl::half *)cdsti;
  498. *dsti = *xi;
  499. }
  500. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  501. const sycl::half *xi = (const sycl::half *)cxi;
  502. float * dsti = (float *) cdsti;
  503. *dsti = *xi;
  504. }
  505. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  506. const int16_t *xi = (const int16_t *)cxi;
  507. int16_t *dsti = (int16_t *)cdsti;
  508. *dsti = *xi;
  509. }
  510. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  511. const int32_t *xi = (const int32_t *)cxi;
  512. int32_t *dsti = (int32_t *)cdsti;
  513. *dsti = *xi;
  514. }
  515. template <cpy_kernel_t cpy_1>
  516. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  517. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  518. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  519. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  520. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  521. item_ct1.get_local_id(2);
  522. if (i >= ne) {
  523. return;
  524. }
  525. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  526. // then combine those indices with the corresponding byte offsets to get the total offsets
  527. const int i03 = i/(ne00 * ne01 * ne02);
  528. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  529. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  530. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  531. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  532. const int i13 = i/(ne10 * ne11 * ne12);
  533. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  534. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  535. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  536. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  537. cpy_1(cx + x_offset, cdst + dst_offset);
  538. }
  539. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  540. const float * xi = (const float *) cxi;
  541. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  542. float amax = 0.0f; // absolute max
  543. for (int j = 0; j < QK8_0; j++) {
  544. const float v = xi[j];
  545. amax = sycl::fmax(amax, sycl::fabs((float)v));
  546. }
  547. const float d = amax / ((1 << 7) - 1);
  548. const float id = d ? 1.0f/d : 0.0f;
  549. dsti->d = d;
  550. for (int j = 0; j < QK8_0; ++j) {
  551. const float x0 = xi[j]*id;
  552. dsti->qs[j] = sycl::round((float)x0);
  553. }
  554. }
  555. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  556. const float * xi = (const float *) cxi;
  557. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  558. float amax = 0.0f;
  559. float vmax = 0.0f;
  560. for (int j = 0; j < QK4_0; ++j) {
  561. const float v = xi[j];
  562. if (amax < sycl::fabs((float)v)) {
  563. amax = sycl::fabs((float)v);
  564. vmax = v;
  565. }
  566. }
  567. const float d = vmax / -8;
  568. const float id = d ? 1.0f/d : 0.0f;
  569. dsti->d = d;
  570. for (int j = 0; j < QK4_0/2; ++j) {
  571. const float x0 = xi[0 + j]*id;
  572. const float x1 = xi[QK4_0/2 + j]*id;
  573. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  574. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  575. dsti->qs[j] = xi0;
  576. dsti->qs[j] |= xi1 << 4;
  577. }
  578. }
  579. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  580. const float * xi = (const float *) cxi;
  581. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  582. float vmin = FLT_MAX;
  583. float vmax = -FLT_MAX;
  584. for (int j = 0; j < QK4_1; ++j) {
  585. const float v = xi[j];
  586. if (v < vmin) vmin = v;
  587. if (v > vmax) vmax = v;
  588. }
  589. const float d = (vmax - vmin) / ((1 << 4) - 1);
  590. const float id = d ? 1.0f/d : 0.0f;
  591. dsti->dm.x() = d;
  592. dsti->dm.y() = vmin;
  593. for (int j = 0; j < QK4_1/2; ++j) {
  594. const float x0 = (xi[0 + j] - vmin)*id;
  595. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  596. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  597. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  598. dsti->qs[j] = xi0;
  599. dsti->qs[j] |= xi1 << 4;
  600. }
  601. }
  602. template <cpy_kernel_t cpy_blck, int qk>
  603. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  604. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  605. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  606. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  607. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  608. item_ct1.get_local_id(2)) *
  609. qk;
  610. if (i >= ne) {
  611. return;
  612. }
  613. const int i03 = i/(ne00 * ne01 * ne02);
  614. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  615. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  616. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  617. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  618. const int i13 = i/(ne10 * ne11 * ne12);
  619. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  620. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  621. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  622. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  623. cpy_blck(cx + x_offset, cdst + dst_offset);
  624. }
  625. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  626. const sycl::nd_item<3> &item_ct1) {
  627. const int row = item_ct1.get_group(1);
  628. const int col = item_ct1.get_local_id(2);
  629. float sum = 0.0f;
  630. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  631. sum += x[row * ncols + i];
  632. }
  633. sum = warp_reduce_sum(sum, item_ct1);
  634. if (col == 0) {
  635. dst[row] = sum;
  636. }
  637. }
  638. template<typename T>
  639. static inline void ggml_sycl_swap(T & a, T & b) {
  640. T tmp = a;
  641. a = b;
  642. b = tmp;
  643. }
  644. template <ggml_sort_order order>
  645. __dpct_inline__ static void
  646. k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
  647. const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
  648. // bitonic sort
  649. int col = item_ct1.get_local_id(2);
  650. int row = item_ct1.get_group(1);
  651. if (col >= ncols_pad) {
  652. return;
  653. }
  654. const float * x_row = x + row * ncols;
  655. auto dst_row = (int *)dpct_local;
  656. // initialize indices
  657. dst_row[col] = col;
  658. item_ct1.barrier(sycl::access::fence_space::local_space);
  659. for (int k = 2; k <= ncols_pad; k *= 2) {
  660. for (int j = k / 2; j > 0; j /= 2) {
  661. int ixj = col ^ j;
  662. if (ixj > col) {
  663. if ((col & k) == 0) {
  664. if (dst_row[col] >= ncols ||
  665. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  666. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  667. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  668. ) {
  669. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  670. }
  671. } else {
  672. if (dst_row[ixj] >= ncols ||
  673. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  674. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  675. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  676. ) {
  677. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  678. }
  679. }
  680. }
  681. /*
  682. DPCT1118:1: SYCL group functions and algorithms must be encountered
  683. in converged control flow. You may need to adjust the code.
  684. */
  685. item_ct1.barrier(sycl::access::fence_space::local_space);
  686. }
  687. }
  688. // copy the result to dst without the padding
  689. if (col < ncols) {
  690. dst[row * ncols + col] = dst_row[col];
  691. }
  692. }
  693. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  694. const sycl::nd_item<3> &item_ct1) {
  695. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  696. item_ct1.get_local_id(1);
  697. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  698. item_ct1.get_local_id(2);
  699. if (col >= ncols) {
  700. return;
  701. }
  702. const int i = row*ncols + col;
  703. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  704. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  705. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  706. }
  707. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  708. const sycl::nd_item<3> &item_ct1) {
  709. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  710. item_ct1.get_local_id(2);
  711. if (i >= k) {
  712. return;
  713. }
  714. dst[i] = scale * x[i];
  715. }
  716. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  717. const sycl::nd_item<3> &item_ct1) {
  718. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  719. item_ct1.get_local_id(2);
  720. if (i >= k) {
  721. return;
  722. }
  723. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  724. }
  725. template <typename Ti, typename To>
  726. static void pool2d_nchw_kernel(
  727. const int ih, const int iw, const int oh, const int ow,
  728. const int kh, const int kw, const int sh, const int sw,
  729. const int ph, const int pw, const int parallel_elements,
  730. const Ti* src, To* dst, const enum ggml_op_pool op,
  731. const sycl::nd_item<3> &item_ct1) {
  732. int idx = item_ct1.get_local_id(2) +
  733. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  734. if (idx >= parallel_elements) {
  735. return;
  736. }
  737. const int I_HW = ih * iw;
  738. const int O_HW = oh * ow;
  739. const int nc = idx / O_HW;
  740. const int cur_oh = idx % O_HW / ow;
  741. const int cur_ow = idx % O_HW % ow;
  742. const Ti* i_ptr = src + nc * I_HW;
  743. To* o_ptr = dst + nc * O_HW;
  744. const int start_h = cur_oh * sh - ph;
  745. const int bh = sycl::max(0, start_h);
  746. const int eh = sycl::min(ih, start_h + kh);
  747. const int start_w = cur_ow * sw - pw;
  748. const int bw = sycl::max(0, start_w);
  749. const int ew = sycl::min(iw, start_w + kw);
  750. To res = 0;
  751. switch (op) {
  752. case GGML_OP_POOL_AVG: res = 0; break;
  753. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  754. }
  755. for (int i = bh; i < eh; i += 1) {
  756. for (int j = bw; j < ew; j += 1) {
  757. #if DPCT_COMPATIBILITY_TEMP >= 350
  758. /*
  759. DPCT1098:106: The '*' expression is used instead of the __ldg
  760. call. These two expressions do not provide the exact same
  761. functionality. Check the generated code for potential precision
  762. and/or performance issues.
  763. */
  764. Ti cur = *(i_ptr + i * iw + j);
  765. #else
  766. Ti cur = i_ptr[i * iw + j];
  767. #endif
  768. switch (op) {
  769. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  770. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  771. }
  772. }
  773. }
  774. o_ptr[cur_oh * ow + cur_ow] = res;
  775. }
  776. template <int qk, int qr, dequantize_kernel_t dq>
  777. static void get_rows_sycl(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  778. ggml_tensor *dst, const void *src0_dd,
  779. const int32_t *src1_dd, float *dst_dd,
  780. queue_ptr stream) {
  781. GGML_TENSOR_BINARY_OP_LOCALS
  782. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  783. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  784. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  785. // strides in elements
  786. //const size_t s0 = nb0 / ggml_element_size(dst);
  787. const size_t s1 = nb1 / ggml_element_size(dst);
  788. const size_t s2 = nb2 / ggml_element_size(dst);
  789. const size_t s3 = nb3 / ggml_element_size(dst);
  790. const size_t s10 = nb10 / ggml_element_size(src1);
  791. const size_t s11 = nb11 / ggml_element_size(src1);
  792. const size_t s12 = nb12 / ggml_element_size(src1);
  793. //const size_t s13 = nb13 / ggml_element_size(src1);
  794. GGML_ASSERT(ne00 % 2 == 0);
  795. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  796. [=](sycl::nd_item<3> item_ct1) {
  797. k_get_rows<qk, qr, dq>(
  798. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  799. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  800. });
  801. (void) dst;
  802. }
  803. template <typename src0_t>
  804. static void get_rows_sycl_float(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  805. const ggml_tensor *src1, ggml_tensor *dst,
  806. const src0_t *src0_dd, const int32_t *src1_dd,
  807. float *dst_dd, queue_ptr stream) {
  808. GGML_TENSOR_BINARY_OP_LOCALS
  809. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  810. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  811. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  812. // strides in elements
  813. //const size_t s0 = nb0 / ggml_element_size(dst);
  814. const size_t s1 = nb1 / ggml_element_size(dst);
  815. const size_t s2 = nb2 / ggml_element_size(dst);
  816. const size_t s3 = nb3 / ggml_element_size(dst);
  817. const size_t s10 = nb10 / ggml_element_size(src1);
  818. const size_t s11 = nb11 / ggml_element_size(src1);
  819. const size_t s12 = nb12 / ggml_element_size(src1);
  820. //const size_t s13 = nb13 / ggml_element_size(src1);
  821. {
  822. dpct::has_capability_or_fail(stream->get_device(),
  823. {sycl::aspect::fp16});
  824. stream->parallel_for(
  825. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  826. [=](sycl::nd_item<3> item_ct1) {
  827. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  828. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  829. });
  830. }
  831. (void) dst;
  832. }
  833. template<float (*bin_op)(const float, const float)>
  834. struct bin_bcast_sycl {
  835. template <typename src0_t, typename src1_t, typename dst_t>
  836. void operator()(ggml_backend_sycl_context & ctx,
  837. const struct ggml_tensor *src0,
  838. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  839. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  840. queue_ptr stream) {
  841. GGML_TENSOR_BINARY_OP_LOCALS
  842. int nr0 = ne10/ne0;
  843. int nr1 = ne11/ne1;
  844. int nr2 = ne12/ne2;
  845. int nr3 = ne13/ne3;
  846. int nr[4] = { nr0, nr1, nr2, nr3 };
  847. // collapse dimensions until first broadcast dimension
  848. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  849. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  850. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  851. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  852. auto collapse = [](int64_t cne[]) {
  853. cne[0] *= cne[1];
  854. cne[1] = cne[2];
  855. cne[2] = cne[3];
  856. cne[3] = 1;
  857. };
  858. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  859. cnb[1] *= cne[1];
  860. cnb[2] *= cne[2];
  861. cnb[3] *= cne[3];
  862. };
  863. for (int i = 0; i < 4; i++) {
  864. if (nr[i] != 1) {
  865. break;
  866. }
  867. if (i > 0) {
  868. collapse_nb(cnb0, cne0);
  869. collapse_nb(cnb1, cne1);
  870. collapse(cne0);
  871. collapse(cne1);
  872. }
  873. }
  874. {
  875. int64_t ne0 = cne0[0];
  876. int64_t ne1 = cne0[1];
  877. int64_t ne2 = cne0[2];
  878. int64_t ne3 = cne0[3];
  879. int64_t ne10 = cne1[0];
  880. int64_t ne11 = cne1[1];
  881. int64_t ne12 = cne1[2];
  882. int64_t ne13 = cne1[3];
  883. size_t nb0 = cnb0[0];
  884. size_t nb1 = cnb0[1];
  885. size_t nb2 = cnb0[2];
  886. size_t nb3 = cnb0[3];
  887. size_t nb10 = cnb1[0];
  888. size_t nb11 = cnb1[1];
  889. size_t nb12 = cnb1[2];
  890. size_t nb13 = cnb1[3];
  891. size_t s0 = nb0 / sizeof(dst_t);
  892. size_t s1 = nb1 / sizeof(dst_t);
  893. size_t s2 = nb2 / sizeof(dst_t);
  894. size_t s3 = nb3 / sizeof(dst_t);
  895. size_t s10 = nb10 / sizeof(src1_t);
  896. size_t s11 = nb11 / sizeof(src1_t);
  897. size_t s12 = nb12 / sizeof(src1_t);
  898. size_t s13 = nb13 / sizeof(src1_t);
  899. GGML_ASSERT(s0 == 1);
  900. GGML_ASSERT(s10 == 1);
  901. const int block_size = 128;
  902. int64_t hne0 = std::max(ne0/2LL, 1LL);
  903. sycl::range<3> block_dims(1, 1, 1);
  904. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  905. block_dims[1] = std::min<unsigned int>(
  906. ne1, block_size / (unsigned int)block_dims[2]);
  907. block_dims[0] = std::min(
  908. std::min<unsigned int>(
  909. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  910. (unsigned int)block_dims[1]),
  911. 64U);
  912. sycl::range<3> block_nums(
  913. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  914. (ne1 + block_dims[1] - 1) / block_dims[1],
  915. (hne0 + block_dims[2] - 1) / block_dims[2]);
  916. if (block_nums[0] > 65535) {
  917. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  918. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  919. {
  920. dpct::has_capability_or_fail(stream->get_device(),
  921. {sycl::aspect::fp16});
  922. stream->parallel_for(
  923. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  924. sycl::range<3>(1, 1, block_size),
  925. sycl::range<3>(1, 1, block_size)),
  926. [=](sycl::nd_item<3> item_ct1) {
  927. k_bin_bcast_unravel<bin_op>(
  928. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  929. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  930. s13, item_ct1);
  931. });
  932. }
  933. } else {
  934. /*
  935. DPCT1049:16: The work-group size passed to the SYCL kernel may
  936. exceed the limit. To get the device limit, query
  937. info::device::max_work_group_size. Adjust the work-group size if
  938. needed.
  939. */
  940. dpct::has_capability_or_fail(stream->get_device(),
  941. {sycl::aspect::fp16});
  942. stream->parallel_for(
  943. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  944. [=](sycl::nd_item<3> item_ct1) {
  945. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  946. ne2, ne3, ne10, ne11, ne12, ne13,
  947. s1, s2, s3, s11, s12, s13,
  948. item_ct1);
  949. });
  950. }
  951. }
  952. }
  953. };
  954. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  955. const int n_elements, const int ne10, const int ne11,
  956. const int ne12, const int nb1, const int nb2,
  957. const int offset, queue_ptr stream) {
  958. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  959. stream->parallel_for(
  960. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  961. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  962. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  963. [=](sycl::nd_item<3> item_ct1) {
  964. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  965. item_ct1);
  966. });
  967. }
  968. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  969. queue_ptr stream) {
  970. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  971. stream->parallel_for(
  972. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  973. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  974. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  975. [=](sycl::nd_item<3> item_ct1) {
  976. gelu_f32(x, dst, k, item_ct1);
  977. });
  978. }
  979. static void silu_f32_sycl(const float *x, float *dst, const int k,
  980. queue_ptr stream) {
  981. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  982. stream->parallel_for(
  983. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  984. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  985. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  986. [=](sycl::nd_item<3> item_ct1) {
  987. silu_f32(x, dst, k, item_ct1);
  988. });
  989. }
  990. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  991. queue_ptr stream) {
  992. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  993. stream->parallel_for(
  994. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  995. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  996. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  997. [=](sycl::nd_item<3> item_ct1) {
  998. gelu_quick_f32(x, dst, k, item_ct1);
  999. });
  1000. }
  1001. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  1002. queue_ptr stream) {
  1003. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  1004. stream->parallel_for(
  1005. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1006. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  1007. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  1008. [=](sycl::nd_item<3> item_ct1) {
  1009. tanh_f32(x, dst, k, item_ct1);
  1010. });
  1011. }
  1012. static void relu_f32_sycl(const float *x, float *dst, const int k,
  1013. queue_ptr stream) {
  1014. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  1015. stream->parallel_for(
  1016. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1017. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  1018. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  1019. [=](sycl::nd_item<3> item_ct1) {
  1020. relu_f32(x, dst, k, item_ct1);
  1021. });
  1022. }
  1023. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  1024. queue_ptr stream) {
  1025. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  1026. stream->parallel_for(
  1027. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1028. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  1029. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  1030. [=](sycl::nd_item<3> item_ct1) {
  1031. hardsigmoid_f32(x, dst, k, item_ct1);
  1032. });
  1033. }
  1034. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  1035. queue_ptr stream) {
  1036. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  1037. stream->parallel_for(
  1038. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1039. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  1040. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  1041. [=](sycl::nd_item<3> item_ct1) {
  1042. hardswish_f32(x, dst, k, item_ct1);
  1043. });
  1044. }
  1045. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  1046. const float negative_slope,
  1047. queue_ptr stream) {
  1048. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  1049. stream->parallel_for(
  1050. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1051. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  1052. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  1053. [=](sycl::nd_item<3> item_ct1) {
  1054. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  1055. });
  1056. }
  1057. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  1058. queue_ptr stream) {
  1059. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  1060. stream->parallel_for(
  1061. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1062. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  1063. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  1064. [=](sycl::nd_item<3> item_ct1) {
  1065. sqr_f32(x, dst, k, item_ct1);
  1066. });
  1067. }
  1068. static void upscale_f32_sycl(const float *x, float *dst, const int nb00, const int nb01,
  1069. const int nb02, const int nb03, const int ne10, const int ne11,
  1070. const int ne12, const int ne13, const float sf0, const float sf1,
  1071. const float sf2, const float sf3, queue_ptr stream) {
  1072. int dst_size = ne10 * ne11 * ne12 * ne13;
  1073. int num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  1074. sycl::range<1> gridDim(num_blocks * SYCL_UPSCALE_BLOCK_SIZE);
  1075. stream->parallel_for(
  1076. sycl::nd_range<1>(gridDim, sycl::range<1>(SYCL_UPSCALE_BLOCK_SIZE)),
  1077. [=](sycl::nd_item<1> item_ct1) {
  1078. upscale_f32(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3, item_ct1);
  1079. });
  1080. }
  1081. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  1082. const int ne01, const int ne02, const int ne0,
  1083. const int ne1, const int ne2, queue_ptr stream) {
  1084. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  1085. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  1086. stream->parallel_for(
  1087. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  1088. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  1089. [=](sycl::nd_item<3> item_ct1) {
  1090. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  1091. });
  1092. }
  1093. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  1094. const int ky, const int kx_padded,
  1095. queue_ptr stream) {
  1096. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  1097. const sycl::range<3> num_blocks(1, ky, block_num_x);
  1098. int constexpr QUANT_BLOCK_TILE = QK8_1 / WARP_SIZE;
  1099. static_assert(QK8_1 % WARP_SIZE == 0);
  1100. const sycl::range<3> block_size(1, 1, SYCL_QUANTIZE_BLOCK_SIZE / QUANT_BLOCK_TILE);
  1101. {
  1102. dpct::has_capability_or_fail(stream->get_device(),
  1103. {sycl::aspect::fp16});
  1104. stream->parallel_for(
  1105. sycl::nd_range<3>(num_blocks * block_size, block_size),
  1106. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  1107. quantize_q8_1<QUANT_BLOCK_TILE>(x, vy, kx, kx_padded, item_ct1);
  1108. });
  1109. }
  1110. }
  1111. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  1112. float *dst, const int ncols_x,
  1113. const int nrows_x,
  1114. const int nchannels_x,
  1115. const int nchannels_y,
  1116. queue_ptr stream) {
  1117. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  1118. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  1119. {
  1120. dpct::has_capability_or_fail(stream->get_device(),
  1121. {sycl::aspect::fp16});
  1122. stream->parallel_for(
  1123. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1124. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  1125. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  1126. nchannels_y, item_ct1);
  1127. });
  1128. }
  1129. }
  1130. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  1131. const void *vx, const float *y, float *dst, const int ncols_x,
  1132. const int nrows_x, const int row_stride_x, const int nchannels_x,
  1133. const int nchannels_y, const int channel_stride_x, queue_ptr stream) {
  1134. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  1135. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  1136. {
  1137. dpct::has_capability_or_fail(stream->get_device(),
  1138. {sycl::aspect::fp16});
  1139. stream->parallel_for(
  1140. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1141. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  1142. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  1143. row_stride_x, channel_stride_x,
  1144. nchannels_y / nchannels_x, item_ct1);
  1145. });
  1146. }
  1147. }
  1148. static void
  1149. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  1150. const int ne01, const int ne02, const int nb00,
  1151. const int nb01, const int nb02, const int nb03,
  1152. const int ne10, const int ne11, const int ne12,
  1153. const int nb10, const int nb11, const int nb12,
  1154. const int nb13, queue_ptr stream) {
  1155. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  1156. {
  1157. dpct::has_capability_or_fail(stream->get_device(),
  1158. {sycl::aspect::fp16});
  1159. stream->parallel_for(
  1160. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1161. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  1162. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  1163. [=](sycl::nd_item<3> item_ct1) {
  1164. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  1165. nb01, nb02, nb03, ne10, ne11, ne12,
  1166. nb10, nb11, nb12, nb13, item_ct1);
  1167. });
  1168. }
  1169. }
  1170. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  1171. const int ne00, const int ne01,
  1172. const int ne02, const int nb00,
  1173. const int nb01, const int nb02,
  1174. const int nb03, const int ne10,
  1175. const int ne11, const int ne12,
  1176. const int nb10, const int nb11,
  1177. const int nb12, const int nb13,
  1178. queue_ptr stream) {
  1179. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  1180. {
  1181. dpct::has_capability_or_fail(stream->get_device(),
  1182. {sycl::aspect::fp16});
  1183. stream->parallel_for(
  1184. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1185. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  1186. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  1187. [=](sycl::nd_item<3> item_ct1) {
  1188. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1189. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1190. item_ct1);
  1191. });
  1192. }
  1193. }
  1194. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  1195. const int ne00, const int ne01,
  1196. const int ne02, const int nb00,
  1197. const int nb01, const int nb02,
  1198. const int nb03, const int ne10,
  1199. const int ne11, const int ne12,
  1200. const int nb10, const int nb11,
  1201. const int nb12, const int nb13,
  1202. queue_ptr stream) {
  1203. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  1204. {
  1205. dpct::has_capability_or_fail(stream->get_device(),
  1206. {sycl::aspect::fp16});
  1207. stream->parallel_for(
  1208. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1209. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  1210. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  1211. [=](sycl::nd_item<3> item_ct1) {
  1212. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1213. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1214. item_ct1);
  1215. });
  1216. }
  1217. }
  1218. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  1219. const int ne00, const int ne01,
  1220. const int ne02, const int nb00,
  1221. const int nb01, const int nb02,
  1222. const int nb03, const int ne10,
  1223. const int ne11, const int ne12,
  1224. const int nb10, const int nb11,
  1225. const int nb12, const int nb13,
  1226. queue_ptr stream) {
  1227. GGML_ASSERT(ne % QK8_0 == 0);
  1228. const int num_blocks = ne / QK8_0;
  1229. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  1230. sycl::range<3>(1, 1, 1)),
  1231. [=](sycl::nd_item<3> item_ct1) {
  1232. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  1233. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1234. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1235. item_ct1);
  1236. });
  1237. }
  1238. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  1239. const int ne00, const int ne01,
  1240. const int ne02, const int nb00,
  1241. const int nb01, const int nb02,
  1242. const int nb03, const int ne10,
  1243. const int ne11, const int ne12,
  1244. const int nb10, const int nb11,
  1245. const int nb12, const int nb13,
  1246. queue_ptr stream) {
  1247. GGML_ASSERT(ne % QK4_0 == 0);
  1248. const int num_blocks = ne / QK4_0;
  1249. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  1250. sycl::range<3>(1, 1, 1)),
  1251. [=](sycl::nd_item<3> item_ct1) {
  1252. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  1253. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1254. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1255. item_ct1);
  1256. });
  1257. }
  1258. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  1259. const int ne00, const int ne01,
  1260. const int ne02, const int nb00,
  1261. const int nb01, const int nb02,
  1262. const int nb03, const int ne10,
  1263. const int ne11, const int ne12,
  1264. const int nb10, const int nb11,
  1265. const int nb12, const int nb13,
  1266. queue_ptr stream) {
  1267. GGML_ASSERT(ne % QK4_1 == 0);
  1268. const int num_blocks = ne / QK4_1;
  1269. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  1270. sycl::range<3>(1, 1, 1)),
  1271. [=](sycl::nd_item<3> item_ct1) {
  1272. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  1273. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1274. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1275. item_ct1);
  1276. });
  1277. }
  1278. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  1279. const int ne00, const int ne01,
  1280. const int ne02, const int nb00,
  1281. const int nb01, const int nb02,
  1282. const int nb03, const int ne10,
  1283. const int ne11, const int ne12,
  1284. const int nb10, const int nb11,
  1285. const int nb12, const int nb13,
  1286. queue_ptr stream) {
  1287. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  1288. {
  1289. dpct::has_capability_or_fail(stream->get_device(),
  1290. {sycl::aspect::fp16});
  1291. stream->parallel_for(
  1292. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1293. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  1294. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  1295. [=](sycl::nd_item<3> item_ct1) {
  1296. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1297. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1298. item_ct1);
  1299. });
  1300. }
  1301. }
  1302. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  1303. const int ne00, const int ne01,
  1304. const int ne02, const int nb00,
  1305. const int nb01, const int nb02,
  1306. const int nb03, const int ne10,
  1307. const int ne11, const int ne12,
  1308. const int nb10, const int nb11,
  1309. const int nb12, const int nb13,
  1310. queue_ptr stream) {
  1311. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  1312. {
  1313. // dpct::has_capability_or_fail(stream->get_device(),
  1314. // {sycl::aspect::fp16});
  1315. stream->parallel_for(
  1316. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1317. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  1318. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  1319. [=](sycl::nd_item<3> item_ct1) {
  1320. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1321. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1322. item_ct1);
  1323. });
  1324. }
  1325. }
  1326. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  1327. const int ne00, const int ne01,
  1328. const int ne02, const int nb00,
  1329. const int nb01, const int nb02,
  1330. const int nb03, const int ne10,
  1331. const int ne11, const int ne12,
  1332. const int nb10, const int nb11,
  1333. const int nb12, const int nb13,
  1334. queue_ptr stream) {
  1335. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  1336. {
  1337. // dpct::has_capability_or_fail(stream->get_device(),
  1338. // {sycl::aspect::fp16});
  1339. stream->parallel_for(
  1340. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1341. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  1342. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  1343. [=](sycl::nd_item<3> item_ct1) {
  1344. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  1345. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  1346. item_ct1);
  1347. });
  1348. }
  1349. }
  1350. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  1351. const int k, queue_ptr stream) {
  1352. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  1353. stream->parallel_for(
  1354. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1355. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  1356. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  1357. [=](sycl::nd_item<3> item_ct1) {
  1358. scale_f32(x, dst, scale, k, item_ct1);
  1359. });
  1360. }
  1361. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  1362. const float max, const int k,
  1363. queue_ptr stream) {
  1364. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  1365. stream->parallel_for(
  1366. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1367. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  1368. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  1369. [=](sycl::nd_item<3> item_ct1) {
  1370. clamp_f32(x, dst, min, max, k, item_ct1);
  1371. });
  1372. }
  1373. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  1374. const int nrows, queue_ptr stream) {
  1375. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  1376. const sycl::range<3> block_nums(1, nrows, 1);
  1377. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1378. [=](sycl::nd_item<3> item_ct1)
  1379. [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  1380. k_sum_rows_f32(x, dst, ncols, item_ct1);
  1381. });
  1382. }
  1383. static int next_power_of_2(int x) {
  1384. int n = 1;
  1385. while (n < x) {
  1386. n *= 2;
  1387. }
  1388. return n;
  1389. }
  1390. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  1391. const int nrows, ggml_sort_order order,
  1392. queue_ptr stream) {
  1393. // bitonic sort requires ncols to be power of 2
  1394. const int ncols_pad = next_power_of_2(ncols);
  1395. const sycl::range<3> block_dims(1, 1, ncols_pad);
  1396. const sycl::range<3> block_nums(1, nrows, 1);
  1397. const size_t shared_mem = ncols_pad * sizeof(int);
  1398. if (order == GGML_SORT_ORDER_ASC) {
  1399. stream->submit([&](sycl::handler &cgh) {
  1400. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  1401. sycl::range<1>(shared_mem), cgh);
  1402. cgh.parallel_for(
  1403. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1404. [=](sycl::nd_item<3> item_ct1) {
  1405. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
  1406. x, dst, ncols, ncols_pad, item_ct1,
  1407. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  1408. .get());
  1409. });
  1410. });
  1411. } else if (order == GGML_SORT_ORDER_DESC) {
  1412. stream->submit([&](sycl::handler &cgh) {
  1413. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  1414. sycl::range<1>(shared_mem), cgh);
  1415. cgh.parallel_for(
  1416. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1417. [=](sycl::nd_item<3> item_ct1) {
  1418. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
  1419. x, dst, ncols, ncols_pad, item_ct1,
  1420. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  1421. .get());
  1422. });
  1423. });
  1424. } else {
  1425. GGML_ABORT("fatal error");
  1426. }
  1427. }
  1428. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  1429. const int ncols_x, const int nrows_x,
  1430. const int rows_per_channel, const int n_past,
  1431. queue_ptr stream) {
  1432. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  1433. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  1434. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  1435. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1436. [=](sycl::nd_item<3> item_ct1) {
  1437. diag_mask_inf_f32(x, dst, ncols_x,
  1438. rows_per_channel, n_past,
  1439. item_ct1);
  1440. });
  1441. }
  1442. static bool g_sycl_loaded = false;
  1443. bool ggml_sycl_loaded(void) {
  1444. return g_sycl_loaded;
  1445. }
  1446. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  1447. dpct::device_info prop;
  1448. SYCL_CHECK(CHECK_TRY_ERROR(
  1449. dpct::get_device_info(prop, device)));
  1450. std::string version;
  1451. version += std::to_string(prop.get_major_version());
  1452. version += ".";
  1453. version += std::to_string(prop.get_minor_version());
  1454. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  1455. std::string name = std::string(prop.get_name());
  1456. name = std::regex_replace(name, std::regex("\\(R\\)"), "");
  1457. name = std::regex_replace(name, std::regex("\\(TM\\)"), "");
  1458. auto global_mem_size = prop.get_global_mem_size()/1000000;
  1459. fprintf(stderr, "|%2d|%19s|%39s|%7s|%7d|%8d|%5d|%6luM|%21s|\n", id, device_type.c_str(),
  1460. name.c_str(), version.c_str(), prop.get_max_compute_units(),
  1461. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  1462. global_mem_size, device.get_info<sycl::info::device::driver_version>().c_str());
  1463. }
  1464. void ggml_backend_sycl_print_sycl_devices() {
  1465. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  1466. int device_count = dpct::dev_mgr::instance().device_count();
  1467. std::map<std::string, size_t> DeviceNums;
  1468. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  1469. fprintf(stderr, "| | | | |Max | |Max |Global | |\n");
  1470. fprintf(stderr, "| | | | |compute|Max work|sub |mem | |\n");
  1471. fprintf(stderr, "|ID| Device Type| Name|Version|units |group |group|size | Driver version|\n");
  1472. fprintf(stderr, "|--|-------------------|---------------------------------------|-------|-------|--------|-----|-------|---------------------|\n");
  1473. for (int id = 0; id < device_count; ++id) {
  1474. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  1475. sycl::backend backend = device.get_backend();
  1476. std::string backend_type = get_device_backend_and_type(device);
  1477. int type_id=DeviceNums[backend_type]++;
  1478. std::stringstream device_type;
  1479. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  1480. print_device_detail(id, device, device_type.str());
  1481. }
  1482. }
  1483. static inline int get_sycl_env(const char *env_name, int default_val) {
  1484. char *user_device_string = getenv(env_name);
  1485. int user_number = default_val;
  1486. unsigned n;
  1487. if (user_device_string != NULL &&
  1488. sscanf(user_device_string, " %u", &n) == 1) {
  1489. user_number = (int)n;
  1490. } else {
  1491. user_number = default_val;
  1492. }
  1493. return user_number;
  1494. }
  1495. static void ggml_check_sycl() try {
  1496. static bool initialized = false;
  1497. if (!initialized) {
  1498. fprintf(stderr, "[SYCL] call ggml_check_sycl\n");
  1499. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  1500. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  1501. #if defined(GGML_SYCL_F16)
  1502. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  1503. #else
  1504. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  1505. #endif
  1506. /* NOT REMOVE, keep it for next optimize for XMX.
  1507. #if defined(SYCL_USE_XMX)
  1508. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  1509. #else
  1510. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  1511. #endif
  1512. */
  1513. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  1514. dpct::dev_mgr::instance().device_count()) != 0) {
  1515. initialized = true;
  1516. g_sycl_loaded = false;
  1517. return;
  1518. }
  1519. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  1520. ggml_backend_sycl_print_sycl_devices();
  1521. initialized = true;
  1522. g_sycl_loaded = true;
  1523. }
  1524. }
  1525. catch (sycl::exception const &exc) {
  1526. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  1527. << ", line:" << __LINE__ << std::endl;
  1528. std::exit(1);
  1529. }
  1530. static ggml_sycl_device_info ggml_sycl_init() {
  1531. ggml_sycl_device_info info = {};
  1532. info.device_count = dpct::dev_mgr::instance().device_count();
  1533. if (info.device_count == 0) {
  1534. fprintf(stderr, "%s: failed to initialize " GGML_SYCL_NAME ": %s\n", __func__);
  1535. return info;
  1536. }
  1537. GGML_ASSERT(info.device_count <= GGML_SYCL_MAX_DEVICES);
  1538. int64_t total_vram = 0;
  1539. #if defined(GGML_SYCL_FORCE_MMQ)
  1540. fprintf(stderr, "%s: GGML_SYCL_FORCE_MMQ: yes\n", __func__);
  1541. #else
  1542. fprintf(stderr, "%s: GGML_SYCL_FORCE_MMQ: no\n", __func__);
  1543. #endif
  1544. #if defined(SYCL_USE_XMX)
  1545. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  1546. #else
  1547. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  1548. #endif
  1549. fprintf(stderr, "%s: found %d " GGML_SYCL_NAME " devices:\n", __func__, info.device_count);
  1550. for (int i = 0; i < info.device_count; ++i) {
  1551. info.devices[i].vmm = 0;
  1552. dpct::device_info prop;
  1553. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  1554. prop, dpct::dev_mgr::instance().get_device(i))));
  1555. info.default_tensor_split[i] = total_vram;
  1556. total_vram += prop.get_global_mem_size();
  1557. info.devices[i].cc =
  1558. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  1559. info.max_work_group_sizes[i] = prop.get_max_work_group_size();
  1560. }
  1561. for (int id = 0; id < info.device_count; ++id) {
  1562. info.default_tensor_split[id] /= total_vram;
  1563. }
  1564. return info;
  1565. }
  1566. const ggml_sycl_device_info & ggml_sycl_info() {
  1567. static ggml_sycl_device_info info = ggml_sycl_init();
  1568. return info;
  1569. }
  1570. /*
  1571. device_index: device index from 0 to n (continue numbers).
  1572. It is used for device select/set in SYCL backend internal data structure.
  1573. */
  1574. inline void check_allow_gpu_index(const int device_index) {
  1575. if (device_index >= ggml_sycl_info().device_count) {
  1576. char error_buf[256];
  1577. snprintf(
  1578. error_buf,
  1579. sizeof(error_buf),
  1580. "%s error: device_index:%d is out of range: [0-%d]",
  1581. __func__,
  1582. device_index,
  1583. ggml_sycl_info().device_count - 1);
  1584. fprintf(stderr, "%s\n", error_buf);
  1585. assert(false);
  1586. }
  1587. }
  1588. // buffer pool for sycl (legacy)
  1589. struct ggml_sycl_pool_leg : public ggml_sycl_pool {
  1590. static const int MAX_SYCL_BUFFERS = 256;
  1591. int device;
  1592. queue_ptr qptr;
  1593. struct ggml_sycl_buffer {
  1594. void * ptr = nullptr;
  1595. size_t size = 0;
  1596. };
  1597. ggml_sycl_buffer buffer_pool[MAX_SYCL_BUFFERS] = {};
  1598. size_t pool_size = 0;
  1599. explicit ggml_sycl_pool_leg(queue_ptr qptr_, int device_) :
  1600. qptr(qptr_),
  1601. device(device_) {
  1602. }
  1603. ~ggml_sycl_pool_leg() {
  1604. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  1605. ggml_sycl_buffer & b = buffer_pool[i];
  1606. if (b.ptr != nullptr) {
  1607. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(b.ptr, *qptr)));
  1608. pool_size -= b.size;
  1609. }
  1610. }
  1611. GGML_ASSERT(pool_size == 0);
  1612. }
  1613. void * alloc(size_t size, size_t * actual_size) override {
  1614. #ifdef DEBUG_sycl_MALLOC
  1615. int nnz = 0;
  1616. size_t max_size = 0;
  1617. #endif
  1618. size_t best_diff = 1ull << 36;
  1619. int ibest = -1;
  1620. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  1621. ggml_sycl_buffer& b = buffer_pool[i];
  1622. if (b.ptr != nullptr) {
  1623. #ifdef DEBUG_sycl_MALLOC
  1624. ++nnz;
  1625. if (b.size > max_size) max_size = b.size;
  1626. #endif
  1627. if (b.size >= size) {
  1628. size_t diff = b.size - size;
  1629. if (diff < best_diff) {
  1630. best_diff = diff;
  1631. ibest = i;
  1632. if (!best_diff) {
  1633. void * ptr = b.ptr;
  1634. *actual_size = b.size;
  1635. b.ptr = nullptr;
  1636. b.size = 0;
  1637. return ptr;
  1638. }
  1639. }
  1640. }
  1641. }
  1642. }
  1643. if (ibest >= 0) {
  1644. ggml_sycl_buffer& b = buffer_pool[ibest];
  1645. void * ptr = b.ptr;
  1646. *actual_size = b.size;
  1647. b.ptr = nullptr;
  1648. b.size = 0;
  1649. return ptr;
  1650. }
  1651. void * ptr;
  1652. size_t look_ahead_size = (size_t) (1.05 * size);
  1653. SYCL_CHECK(
  1654. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  1655. look_ahead_size, *qptr)));
  1656. if (!ptr) {
  1657. fprintf(stderr, "%s: can't malloc %lu Bytes memory on device", __func__, look_ahead_size);
  1658. return nullptr;
  1659. }
  1660. *actual_size = look_ahead_size;
  1661. pool_size += look_ahead_size;
  1662. #ifdef DEBUG_SYCL_MALLOC
  1663. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  1664. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  1665. #endif
  1666. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  1667. return ptr;
  1668. }
  1669. void free(void * ptr, size_t size) override {
  1670. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  1671. ggml_sycl_buffer& b = buffer_pool[i];
  1672. if (b.ptr == nullptr) {
  1673. b.ptr = ptr;
  1674. b.size = size;
  1675. return;
  1676. }
  1677. }
  1678. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_sycl_BUFFERS\n");
  1679. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr)));
  1680. pool_size -= size;
  1681. }
  1682. };
  1683. std::unique_ptr<ggml_sycl_pool> ggml_backend_sycl_context::new_pool_for_device(queue_ptr qptr, int device) {
  1684. // TBD: NO VMM support
  1685. // if (ggml_sycl_info().devices[device].vmm) {
  1686. // return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_vmm(device));
  1687. // }
  1688. return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_leg(qptr, device));
  1689. }
  1690. // TBD pool with virtual memory management
  1691. // struct ggml_sycl_pool_vmm : public ggml_sycl_pool
  1692. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  1693. const struct ggml_tensor *src,
  1694. int64_t i3, int64_t i2,
  1695. int64_t i1_low, int64_t i1_high,
  1696. queue_ptr stream) try {
  1697. dpct::memcpy_direction kind;
  1698. char * src_ptr;
  1699. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  1700. kind = dpct::host_to_device;
  1701. src_ptr = (char *) src->data;
  1702. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  1703. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  1704. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  1705. kind = dpct::device_to_device;
  1706. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  1707. int id;
  1708. SYCL_CHECK(CHECK_TRY_ERROR(
  1709. id = get_current_device_id()));
  1710. // GGML_SYCL_DEBUG("current device index %d\n", id);
  1711. src_ptr = (char *) extra->data_device[id];
  1712. } else {
  1713. // GGML_SYCL_DEBUG("GGML_ABORT("fatal error")\n");
  1714. GGML_ABORT("fatal error");
  1715. }
  1716. char * dst_ptr = (char *) dst;
  1717. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  1718. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  1719. const enum ggml_type type = src->type;
  1720. const int64_t ts = ggml_type_size(type);
  1721. const int64_t bs = ggml_blck_size(type);
  1722. int64_t i1_diff = i1_high - i1_low;
  1723. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  1724. if (nb0 == ts && nb1 == ts*ne0/bs) {
  1725. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  1726. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  1727. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  1728. kind, *stream));
  1729. } else if (nb0 == ts) {
  1730. return CHECK_TRY_ERROR(
  1731. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  1732. ts * ne0 / bs, i1_diff, kind, *stream));
  1733. } else {
  1734. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  1735. const void * rx = (const void *) ((const char *) x + i1*nb1);
  1736. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  1737. // pretend the row is a matrix with cols=1
  1738. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  1739. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  1740. /*
  1741. DPCT1001:85: The statement could not be removed.
  1742. */
  1743. /*
  1744. DPCT1000:86: Error handling if-stmt was detected but could not be
  1745. rewritten.
  1746. */
  1747. if (r != 0) return r;
  1748. }
  1749. return 0;
  1750. }
  1751. }
  1752. catch (sycl::exception const &exc) {
  1753. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  1754. << ", line:" << __LINE__ << std::endl;
  1755. std::exit(1);
  1756. }
  1757. static void ggml_sycl_op_get_rows(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1758. const ggml_tensor *src1, ggml_tensor *dst,
  1759. const float *src0_d, const float *src1_d,
  1760. float *dst_d, const queue_ptr &stream) {
  1761. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  1762. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  1763. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  1764. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  1765. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  1766. const int32_t * src1_i32 = (const int32_t *) src1_d;
  1767. switch (src0->type) {
  1768. case GGML_TYPE_F16:
  1769. get_rows_sycl_float(ctx, src0, src1, dst, (const sycl::half *)src0_d,
  1770. src1_i32, dst_d, stream);
  1771. break;
  1772. case GGML_TYPE_F32:
  1773. get_rows_sycl_float(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  1774. break;
  1775. case GGML_TYPE_Q4_0:
  1776. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  1777. break;
  1778. case GGML_TYPE_Q4_1:
  1779. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  1780. break;
  1781. case GGML_TYPE_Q5_0:
  1782. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  1783. break;
  1784. case GGML_TYPE_Q5_1:
  1785. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  1786. break;
  1787. case GGML_TYPE_Q8_0:
  1788. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  1789. break;
  1790. default:
  1791. // TODO: k-quants
  1792. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  1793. GGML_ABORT("fatal error");
  1794. break;
  1795. }
  1796. }
  1797. template <class op>
  1798. inline void ggml_sycl_op_bin_bcast(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1799. const ggml_tensor *src1, ggml_tensor *dst,
  1800. const float *src0_dd, const float *src1_dd,
  1801. float *dst_dd,
  1802. const queue_ptr &main_stream) {
  1803. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  1804. op()(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  1805. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  1806. op()(ctx, src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  1807. (sycl::half *)dst_dd, main_stream);
  1808. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  1809. op()(ctx, src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  1810. main_stream);
  1811. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  1812. op()(ctx, src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  1813. main_stream);
  1814. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  1815. op()(ctx, src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  1816. main_stream);
  1817. } else {
  1818. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  1819. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  1820. GGML_ABORT("fatal error");
  1821. }
  1822. }
  1823. static void ggml_sycl_op_repeat(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1824. const ggml_tensor *src1, ggml_tensor *dst,
  1825. const float *src0_d, const float *src1_d,
  1826. float *dst_d,
  1827. const queue_ptr &main_stream) {
  1828. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(ctx, dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  1829. (void) src1;
  1830. (void) src1_d;
  1831. }
  1832. inline void ggml_sycl_op_add(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1833. ggml_tensor *dst, const float *src0_dd,
  1834. const float *src1_dd, float *dst_dd,
  1835. const queue_ptr &main_stream) {
  1836. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  1837. }
  1838. inline void ggml_sycl_op_acc(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1839. ggml_tensor *dst, const float *src0_dd,
  1840. const float *src1_dd, float *dst_dd,
  1841. const queue_ptr &main_stream) {
  1842. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1843. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  1844. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1845. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  1846. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  1847. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  1848. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  1849. int offset = dst->op_params[3] / 4; // offset in bytes
  1850. acc_f32_sycl(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  1851. (void) dst;
  1852. }
  1853. inline void ggml_sycl_op_mul(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1854. ggml_tensor *dst, const float *src0_dd,
  1855. const float *src1_dd, float *dst_dd,
  1856. const queue_ptr &main_stream) {
  1857. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  1858. }
  1859. inline void ggml_sycl_op_div(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1860. ggml_tensor *dst, const float *src0_dd,
  1861. const float *src1_dd, float *dst_dd,
  1862. const queue_ptr &main_stream) {
  1863. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  1864. }
  1865. inline void ggml_sycl_op_gelu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1866. ggml_tensor *dst, const float *src0_dd,
  1867. const float *src1_dd, float *dst_dd,
  1868. const queue_ptr &main_stream) {
  1869. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1870. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1871. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1872. (void) src1;
  1873. (void) dst;
  1874. (void) src1_dd;
  1875. }
  1876. inline void ggml_sycl_op_silu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1877. ggml_tensor *dst, const float *src0_dd,
  1878. const float *src1_dd, float *dst_dd,
  1879. const queue_ptr &main_stream) {
  1880. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1881. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1882. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1883. (void) src1;
  1884. (void) dst;
  1885. (void) src1_dd;
  1886. }
  1887. inline void ggml_sycl_op_gelu_quick(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1888. const ggml_tensor *src1, ggml_tensor *dst,
  1889. const float *src0_dd, const float *src1_dd,
  1890. float *dst_dd,
  1891. const queue_ptr &main_stream) {
  1892. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1893. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1894. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1895. (void) src1;
  1896. (void) dst;
  1897. (void) src1_dd;
  1898. }
  1899. inline void ggml_sycl_op_tanh(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1900. ggml_tensor *dst, const float *src0_dd,
  1901. const float *src1_dd, float *dst_dd,
  1902. const queue_ptr &main_stream) {
  1903. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1904. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1905. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1906. (void) src1;
  1907. (void) dst;
  1908. (void) src1_dd;
  1909. }
  1910. inline void ggml_sycl_op_relu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1911. ggml_tensor *dst, const float *src0_dd,
  1912. const float *src1_dd, float *dst_dd,
  1913. const queue_ptr &main_stream) {
  1914. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1915. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1916. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1917. (void) src1;
  1918. (void) dst;
  1919. (void) src1_dd;
  1920. }
  1921. static void ggml_sycl_op_hardsigmoid(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1922. const ggml_tensor *src1, ggml_tensor *dst,
  1923. const float *src0_dd, const float *src1_dd,
  1924. float *dst_dd,
  1925. const queue_ptr &main_stream) {
  1926. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1927. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1928. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1929. (void) src1;
  1930. (void) dst;
  1931. (void) src1_dd;
  1932. }
  1933. static void ggml_sycl_op_hardswish(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1934. const ggml_tensor *src1, ggml_tensor *dst,
  1935. const float *src0_dd, const float *src1_dd,
  1936. float *dst_dd, const queue_ptr &main_stream) {
  1937. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1938. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1939. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1940. (void) src1;
  1941. (void) dst;
  1942. (void) src1_dd;
  1943. }
  1944. inline void ggml_sycl_op_leaky_relu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1945. const ggml_tensor *src1, ggml_tensor *dst,
  1946. const float *src0_dd, const float *src1_dd,
  1947. float *dst_dd,
  1948. const queue_ptr &main_stream) {
  1949. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1950. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1951. float negative_slope;
  1952. memcpy(&negative_slope, dst->op_params, sizeof(float));
  1953. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  1954. (void) src1;
  1955. (void) dst;
  1956. (void) src1_dd;
  1957. }
  1958. inline void ggml_sycl_op_sqr(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1959. ggml_tensor *dst, const float *src0_dd,
  1960. const float *src1_dd, float *dst_dd,
  1961. const queue_ptr &main_stream) {
  1962. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1963. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  1964. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  1965. (void) src1;
  1966. (void) dst;
  1967. (void) src1_dd;
  1968. }
  1969. inline void ggml_sycl_op_upscale(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1970. const ggml_tensor *src1, ggml_tensor *dst,
  1971. const float *src0_dd, const float *src1_dd,
  1972. float *dst_dd,
  1973. const queue_ptr &main_stream) {
  1974. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1975. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  1976. const float sf0 = (float)dst->ne[0]/src0->ne[0];
  1977. const float sf1 = (float)dst->ne[1]/src0->ne[1];
  1978. const float sf2 = (float)dst->ne[2]/src0->ne[2];
  1979. const float sf3 = (float)dst->ne[3]/src0->ne[3];
  1980. upscale_f32_sycl(src0_dd, dst_dd, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  1981. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3,
  1982. main_stream);
  1983. (void) src1;
  1984. (void) dst;
  1985. (void) src1_dd;
  1986. }
  1987. inline void ggml_sycl_op_pad(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1988. ggml_tensor *dst, const float *src0_dd,
  1989. const float *src1_dd, float *dst_dd,
  1990. const queue_ptr &main_stream) {
  1991. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  1992. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  1993. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  1994. pad_f32_sycl(src0_dd, dst_dd,
  1995. src0->ne[0], src0->ne[1], src0->ne[2],
  1996. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  1997. (void) src1;
  1998. (void) dst;
  1999. (void) src1_dd;
  2000. }
  2001. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  2002. int64_t min_compute_capability = INT_MAX;
  2003. int64_t max_compute_capability = INT_MIN;
  2004. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2005. if (tensor_split[i] < (i + 1 < ggml_sycl_info().device_count ? tensor_split[i + 1] : 1.0f)) {
  2006. if (min_compute_capability > ggml_sycl_info().devices[i].cc) {
  2007. min_compute_capability = ggml_sycl_info().devices[i].cc;
  2008. }
  2009. if (max_compute_capability < ggml_sycl_info().devices[i].cc) {
  2010. max_compute_capability = ggml_sycl_info().devices[i].cc;
  2011. }
  2012. }
  2013. }
  2014. switch(type) {
  2015. case GGML_TYPE_Q4_0:
  2016. case GGML_TYPE_Q4_1:
  2017. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  2018. case GGML_TYPE_Q5_0:
  2019. case GGML_TYPE_Q5_1:
  2020. case GGML_TYPE_Q8_0:
  2021. return 64;
  2022. case GGML_TYPE_F16:
  2023. case GGML_TYPE_F32:
  2024. return 1;
  2025. case GGML_TYPE_Q2_K:
  2026. case GGML_TYPE_Q3_K:
  2027. case GGML_TYPE_Q4_K:
  2028. case GGML_TYPE_Q5_K:
  2029. case GGML_TYPE_IQ2_XXS:
  2030. case GGML_TYPE_IQ2_XS:
  2031. case GGML_TYPE_IQ2_S:
  2032. case GGML_TYPE_IQ1_S:
  2033. case GGML_TYPE_IQ1_M:
  2034. case GGML_TYPE_IQ3_XXS:
  2035. case GGML_TYPE_IQ4_XS:
  2036. case GGML_TYPE_IQ4_NL:
  2037. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  2038. case GGML_TYPE_IQ3_S:
  2039. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  2040. case GGML_TYPE_Q6_K:
  2041. return 64;
  2042. default:
  2043. GGML_ABORT("fatal error");
  2044. }
  2045. }
  2046. inline void ggml_sycl_op_mul_mat_sycl(
  2047. ggml_backend_sycl_context & ctx,
  2048. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2049. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2050. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2051. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2052. const queue_ptr &stream) try {
  2053. GGML_ASSERT(src0_dd_i != nullptr);
  2054. GGML_ASSERT(src1_ddf_i != nullptr);
  2055. GGML_ASSERT(dst_dd_i != nullptr);
  2056. const int64_t ne00 = src0->ne[0];
  2057. const int64_t ne10 = src1->ne[0];
  2058. const int64_t ne0 = dst->ne[0];
  2059. const int64_t row_diff = row_high - row_low;
  2060. int id;
  2061. SYCL_CHECK(
  2062. CHECK_TRY_ERROR(id = get_current_device_id()));
  2063. // the main device has a larger memory buffer to hold the results from all GPUs
  2064. // ldc == nrows of the matrix that cuBLAS writes into
  2065. int ldc = id == ctx.device ? ne0 : row_diff;
  2066. #ifdef GGML_SYCL_F16
  2067. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  2068. #else
  2069. bool use_fp16 = false;
  2070. #endif
  2071. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  2072. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  2073. dst->op_params[0] == GGML_PREC_DEFAULT) {
  2074. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  2075. ggml_sycl_pool_alloc<sycl::half> src0_as_f16(ctx.pool());
  2076. if (src0->type != GGML_TYPE_F16) {
  2077. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  2078. GGML_ASSERT(to_fp16_sycl != nullptr);
  2079. size_t ne = row_diff*ne00;
  2080. src0_as_f16.alloc(ne);
  2081. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  2082. }
  2083. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  2084. ? (const sycl::half *)src0_dd_i
  2085. : src0_as_f16.get();
  2086. ggml_sycl_pool_alloc<sycl::half> src1_as_f16(ctx.pool());
  2087. if (src1->type != GGML_TYPE_F16) {
  2088. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  2089. GGML_ASSERT(to_fp16_sycl != nullptr);
  2090. size_t ne = src1_ncols*ne10;
  2091. src1_as_f16.alloc(ne);
  2092. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  2093. }
  2094. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  2095. ? (const sycl::half *)src1->data + src1_padded_row_size
  2096. : src1_as_f16.get();
  2097. ggml_sycl_pool_alloc<sycl::half> dst_f16(ctx.pool(), row_diff * src1_ncols);
  2098. const sycl::half alpha_f16 = 1.0f;
  2099. const sycl::half beta_f16 = 0.0f;
  2100. #if !GGML_SYCL_DNNL
  2101. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  2102. *stream, oneapi::mkl::transpose::trans,
  2103. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  2104. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  2105. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  2106. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  2107. dpct::library_data_t::real_half)));
  2108. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  2109. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  2110. #else
  2111. auto dnnl_stream = ctx.stream_dnnl(stream);
  2112. DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ptr, DnnlGemmWrapper::to_dt<sycl::half>(),
  2113. src0_ptr, DnnlGemmWrapper::to_dt<sycl::half>(), dst_f16.get(), DnnlGemmWrapper::to_dt<sycl::half>());
  2114. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  2115. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff* src1_ncols, stream);
  2116. #endif
  2117. }
  2118. else {
  2119. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  2120. ggml_sycl_pool_alloc<float> src0_ddq_as_f32(ctx.pool());
  2121. ggml_sycl_pool_alloc<float> src1_ddq_as_f32(ctx.pool());
  2122. if (src0->type != GGML_TYPE_F32) {
  2123. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  2124. GGML_ASSERT(to_fp32_sycl != nullptr);
  2125. src0_ddq_as_f32.alloc(row_diff*ne00);
  2126. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  2127. }
  2128. if (src1->type != GGML_TYPE_F32) {
  2129. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  2130. GGML_ASSERT(to_fp32_sycl != nullptr);
  2131. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  2132. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  2133. }
  2134. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  2135. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  2136. const float alpha = 1.0f;
  2137. const float beta = 0.0f;
  2138. #if !GGML_SYCL_DNNL
  2139. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  2140. *stream, oneapi::mkl::transpose::trans,
  2141. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  2142. dpct::get_value(&alpha, *stream), src0_ddf_i, ne00,
  2143. src1_ddf1_i, ne10, dpct::get_value(&beta, *stream),
  2144. dst_dd_i, ldc)));
  2145. #else
  2146. auto dnnl_stream = ctx.stream_dnnl(stream);
  2147. DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ddf1_i, DnnlGemmWrapper::to_dt<float>(),
  2148. src0_ddf_i, DnnlGemmWrapper::to_dt<float>(), dst_dd_i, DnnlGemmWrapper::to_dt<float>());
  2149. #endif
  2150. }
  2151. (void) dst;
  2152. (void) src1_ddq_i;
  2153. (void) src1_padded_row_size;
  2154. }
  2155. catch (sycl::exception const &exc) {
  2156. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2157. << ", line:" << __LINE__ << std::endl;
  2158. std::exit(1);
  2159. }
  2160. static void ggml_sycl_op_pool2d(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2161. const ggml_tensor *src1, ggml_tensor *dst,
  2162. const float *src0_dd, const float *src1_dd,
  2163. float *dst_dd, const queue_ptr &main_stream) {
  2164. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2165. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2166. const int32_t * opts = (const int32_t *)dst->op_params;
  2167. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  2168. const int k0 = opts[1];
  2169. const int k1 = opts[2];
  2170. const int s0 = opts[3];
  2171. const int s1 = opts[4];
  2172. const int p0 = opts[5];
  2173. const int p1 = opts[6];
  2174. const int64_t IH = src0->ne[1];
  2175. const int64_t IW = src0->ne[0];
  2176. const int64_t N = dst->ne[3];
  2177. const int64_t OC = dst->ne[2];
  2178. const int64_t OH = dst->ne[1];
  2179. const int64_t OW = dst->ne[0];
  2180. const int parallel_elements = N * OC * OH * OW;
  2181. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  2182. sycl::range<3> block_nums(1, 1, num_blocks);
  2183. main_stream->parallel_for(
  2184. sycl::nd_range<3>(block_nums *
  2185. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  2186. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  2187. [=](sycl::nd_item<3> item_ct1) {
  2188. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  2189. parallel_elements, src0_dd, dst_dd, op,
  2190. item_ct1);
  2191. });
  2192. (void) src1;
  2193. (void) src1_dd;
  2194. }
  2195. inline void ggml_sycl_op_sum_rows(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2196. const ggml_tensor *src1, ggml_tensor *dst,
  2197. const float *src0_dd, const float *src1_dd,
  2198. float *dst_dd,
  2199. const queue_ptr &main_stream) {
  2200. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2201. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2202. const int64_t ncols = src0->ne[0];
  2203. const int64_t nrows = ggml_nrows(src0);
  2204. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  2205. (void) src1;
  2206. (void) dst;
  2207. (void) src1_dd;
  2208. }
  2209. inline void ggml_sycl_op_argsort(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2210. const ggml_tensor *src1, ggml_tensor *dst,
  2211. const float *src0_dd, const float *src1_dd,
  2212. float *dst_dd,
  2213. const queue_ptr &main_stream) {
  2214. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2215. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  2216. const int64_t ncols = src0->ne[0];
  2217. const int64_t nrows = ggml_nrows(src0);
  2218. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  2219. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  2220. (void) src1;
  2221. (void) dst;
  2222. (void) src1_dd;
  2223. }
  2224. inline void ggml_sycl_op_diag_mask_inf(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2225. const ggml_tensor *src1,
  2226. ggml_tensor *dst, const float *src0_dd,
  2227. const float *src1_dd, float *dst_dd,
  2228. const queue_ptr &main_stream) {
  2229. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2230. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2231. const int64_t ne00 = src0->ne[0];
  2232. const int64_t ne01 = src0->ne[1];
  2233. const int nrows0 = ggml_nrows(src0);
  2234. const int n_past = ((int32_t *) dst->op_params)[0];
  2235. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  2236. (void) src1;
  2237. (void) dst;
  2238. (void) src1_dd;
  2239. }
  2240. inline void ggml_sycl_op_scale(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2241. ggml_tensor *dst, const float *src0_dd,
  2242. const float *src1_dd, float *dst_dd,
  2243. const queue_ptr &main_stream) {
  2244. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2245. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2246. float scale;
  2247. memcpy(&scale, dst->op_params, sizeof(float));
  2248. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  2249. /*
  2250. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  2251. error codes. The call was replaced with 0. You need to rewrite this code.
  2252. */
  2253. SYCL_CHECK(0);
  2254. (void) src1;
  2255. (void) dst;
  2256. (void) src1_dd;
  2257. }
  2258. inline void ggml_sycl_op_clamp(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2259. ggml_tensor *dst, const float *src0_dd,
  2260. const float *src1_dd, float *dst_dd,
  2261. const queue_ptr &main_stream) {
  2262. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2263. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2264. float min;
  2265. float max;
  2266. memcpy(&min, dst->op_params, sizeof(float));
  2267. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  2268. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  2269. /*
  2270. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  2271. error codes. The call was replaced with 0. You need to rewrite this code.
  2272. */
  2273. SYCL_CHECK(0);
  2274. (void) src1;
  2275. (void) dst;
  2276. (void) src1_dd;
  2277. }
  2278. static void ggml_sycl_op_flatten(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2279. const ggml_tensor *src1, ggml_tensor *dst,
  2280. const ggml_sycl_op_flatten_t op) try {
  2281. const int64_t nrows0 = ggml_nrows(src0);
  2282. const bool use_src1 = src1 != nullptr;
  2283. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  2284. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2285. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2286. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2287. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  2288. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  2289. // dd = data device
  2290. float * src0_ddf = (float *) src0->data;
  2291. float * src1_ddf = use_src1 ? (float *) src1->data : nullptr;
  2292. float * dst_ddf = (float *) dst->data;
  2293. ggml_sycl_pool_alloc<float> src0_f(ctx.pool());
  2294. ggml_sycl_pool_alloc<float> src1_f(ctx.pool());
  2295. ggml_sycl_pool_alloc<float> dst_f(ctx.pool());
  2296. ggml_sycl_set_device(ctx.device);
  2297. queue_ptr main_stream = ctx.stream();
  2298. // GGML_SYCL_DEBUG("ctx.device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  2299. // ctx.device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  2300. // do the computation
  2301. op(ctx, src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  2302. // print_ggml_tensor("tensor", dst);
  2303. }
  2304. catch (sycl::exception const &exc) {
  2305. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2306. << ", line:" << __LINE__ << std::endl;
  2307. std::exit(1);
  2308. }
  2309. static void ggml_sycl_set_peer_access(const int n_tokens, int main_device) {
  2310. static bool peer_access_enabled = false;
  2311. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  2312. if (peer_access_enabled == enable_peer_access) {
  2313. return;
  2314. }
  2315. #ifdef NDEBUG
  2316. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2317. SYCL_CHECK(ggml_sycl_set_device(i));
  2318. }
  2319. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2320. SYCL_CHECK(ggml_sycl_set_device(i));
  2321. for (int id_other = 0; id_other < ggml_sycl_info().device_count; ++id_other) {
  2322. if (i == id_other) {
  2323. continue;
  2324. }
  2325. if (i != main_device && id_other != main_device) {
  2326. continue;
  2327. }
  2328. // int can_access_peer;
  2329. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  2330. // if (can_access_peer) {
  2331. // if (enable_peer_access) {
  2332. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  2333. // } else {
  2334. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  2335. // }
  2336. // }
  2337. }
  2338. }
  2339. #endif // NDEBUG
  2340. peer_access_enabled = enable_peer_access;
  2341. }
  2342. struct ggml_backend_sycl_split_buffer_type_context {
  2343. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  2344. };
  2345. static void ggml_sycl_op_mul_mat(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2346. const ggml_tensor *src1, ggml_tensor *dst,
  2347. ggml_sycl_op_mul_mat_t op,
  2348. const bool convert_src1_to_q8_1) try {
  2349. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  2350. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  2351. const int64_t nrows1 = ggml_nrows(src1);
  2352. GGML_ASSERT(ne03 == ne13);
  2353. const int64_t ne0 = dst->ne[0];
  2354. const int64_t ne1 = dst->ne[1];
  2355. const int nb2 = dst->nb[2];
  2356. const int nb3 = dst->nb[3];
  2357. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2358. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2359. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  2360. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  2361. const int64_t i02_divisor = ne12 / ne02;
  2362. const size_t src0_ts = ggml_type_size(src0->type);
  2363. const size_t src0_bs = ggml_blck_size(src0->type);
  2364. const size_t q8_1_ts = sizeof(block_q8_1);
  2365. const size_t q8_1_bs = QK8_1;
  2366. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2367. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  2368. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  2369. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  2370. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  2371. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  2372. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  2373. GGML_ASSERT(!(split && ne02 > 1));
  2374. GGML_ASSERT(!(split && ne03 > 1));
  2375. GGML_ASSERT(!(split && ne02 < ne12));
  2376. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  2377. if (split) {
  2378. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  2379. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  2380. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  2381. tensor_split = buft_ctx->tensor_split;
  2382. }
  2383. struct dev_data {
  2384. ggml_sycl_pool_alloc<char> src0_dd_alloc;
  2385. ggml_sycl_pool_alloc<float> src1_ddf_alloc;
  2386. ggml_sycl_pool_alloc<char> src1_ddq_alloc;
  2387. ggml_sycl_pool_alloc<float> dst_dd_alloc;
  2388. char *src0_dd = nullptr;
  2389. float *src1_ddf = nullptr; // float
  2390. char *src1_ddq = nullptr; // q8_1
  2391. float *dst_dd = nullptr;
  2392. int64_t row_low;
  2393. int64_t row_high;
  2394. };
  2395. dev_data dev[GGML_SYCL_MAX_DEVICES];
  2396. int used_devices = 0;
  2397. queue_ptr main_stream = ctx.stream();
  2398. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2399. // by default, use all rows
  2400. dev[i].row_low = 0;
  2401. dev[i].row_high = ne01;
  2402. // for multi GPU, get the row boundaries from tensor split
  2403. // and round to mul_mat_q tile sizes
  2404. if (split) {
  2405. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  2406. if (i != 0) {
  2407. dev[i].row_low = ne01*tensor_split[i];
  2408. if (dev[i].row_low < ne01) {
  2409. dev[i].row_low -= dev[i].row_low % rounding;
  2410. }
  2411. }
  2412. if (i != ggml_sycl_info().device_count - 1) {
  2413. dev[i].row_high = ne01*tensor_split[i + 1];
  2414. if (dev[i].row_high < ne01) {
  2415. dev[i].row_high -= dev[i].row_high % rounding;
  2416. }
  2417. }
  2418. }
  2419. }
  2420. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2421. if ((!split && i != ctx.device) || dev[i].row_low == dev[i].row_high) {
  2422. continue;
  2423. }
  2424. used_devices++;
  2425. const bool src1_on_device = i == ctx.device;
  2426. const bool dst_on_device = i == ctx.device;
  2427. ggml_sycl_set_device(i);
  2428. queue_ptr stream = ctx.stream(i, 0);
  2429. if (src0_is_contiguous) {
  2430. dev[i].src0_dd = (char *) src0->data;
  2431. } else {
  2432. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ctx.pool(i), ggml_nbytes(src0));
  2433. }
  2434. if (src1_on_device && src1_is_contiguous) {
  2435. dev[i].src1_ddf = (float *) src1->data;
  2436. } else {
  2437. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ctx.pool(i), ggml_nelements(src1));
  2438. }
  2439. if (convert_src1_to_q8_1) {
  2440. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(ctx.pool(i), nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  2441. if (src1_on_device && src1_is_contiguous) {
  2442. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  2443. /*
  2444. DPCT1010:90: SYCL uses exceptions to report errors and does not
  2445. use the error codes. The call was replaced with 0. You need to
  2446. rewrite this code.
  2447. */
  2448. SYCL_CHECK(0);
  2449. }
  2450. }
  2451. if (dst_on_device) {
  2452. dev[i].dst_dd = (float *) dst->data;
  2453. } else {
  2454. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  2455. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(ctx.pool(i), size_dst_ddf);
  2456. }
  2457. }
  2458. // if multiple devices are used they need to wait for the main device
  2459. // here an event is recorded that signals that the main device has finished calculating the input data
  2460. if (split && used_devices > 1) {
  2461. ggml_sycl_set_device(ctx.device);
  2462. /*
  2463. DPCT1024:91: The original code returned the error code that was further
  2464. consumed by the program logic. This original code was replaced with 0.
  2465. You may need to rewrite the program logic consuming the error code.
  2466. */
  2467. SYCL_CHECK(CHECK_TRY_ERROR(
  2468. *src0_extra->events[ctx.device][0] =
  2469. ctx.stream()->ext_oneapi_submit_barrier()));
  2470. }
  2471. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  2472. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  2473. const int64_t is = split ? (src1_col_0/src1_col_stride) % GGML_SYCL_MAX_STREAMS : 0;
  2474. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  2475. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2476. if ((!split && i != ctx.device) || dev[i].row_low == dev[i].row_high) {
  2477. continue;
  2478. }
  2479. const bool src1_on_device = i == ctx.device;
  2480. const bool dst_on_device = i == ctx.device;
  2481. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  2482. ggml_sycl_set_device(i);
  2483. queue_ptr stream = ctx.stream(i, is);
  2484. // wait for main GPU data if necessary
  2485. if (split && (i != ctx.device || is != 0)) {
  2486. /*
  2487. DPCT1009:163: SYCL uses exceptions to report errors and does not
  2488. use the error codes. The original code was commented out and a
  2489. warning string was inserted. You need to rewrite this code.
  2490. */
  2491. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  2492. {*src0_extra->events[ctx.device][0]})));
  2493. }
  2494. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  2495. const int64_t i03 = i0 / ne12;
  2496. const int64_t i02 = i0 % ne12;
  2497. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  2498. // for split tensors the data begins at i0 == i0_offset_low
  2499. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  2500. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  2501. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  2502. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  2503. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  2504. // in that case an offset on dst_ddf_i is needed
  2505. if (i == ctx.device) {
  2506. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  2507. }
  2508. // copy src0, src1 to device if necessary
  2509. if (src1_is_contiguous) {
  2510. if (i != ctx.device) {
  2511. if (convert_src1_to_q8_1) {
  2512. char * src1_ddq_i_source = dev[ctx.device].src1_ddq + src1_ddq_i_offset;
  2513. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  2514. src1_ddq_i, src1_ddq_i_source,
  2515. src1_ncols * src1_padded_col_size * q8_1_ts /
  2516. q8_1_bs).wait()));
  2517. } else {
  2518. float * src1_ddf_i_source = (float *) src1_extra->data_device[ctx.device];
  2519. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  2520. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  2521. src1_ddf_i, src1_ddf_i_source,
  2522. src1_ncols * ne10 * sizeof(float))));
  2523. }
  2524. }
  2525. } else if (src1_on_device && !src1_is_contiguous) {
  2526. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  2527. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  2528. } else {
  2529. GGML_ABORT("fatal error");
  2530. }
  2531. if (convert_src1_to_q8_1 && !src1_is_contiguous) {
  2532. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  2533. /*
  2534. DPCT1010:92: SYCL uses exceptions to report errors and does
  2535. not use the error codes. The call was replaced with 0. You
  2536. need to rewrite this code.
  2537. */
  2538. SYCL_CHECK(0);
  2539. }
  2540. if (src1_col_0 == 0 && !src0_is_contiguous && i02 % i02_divisor == 0) {
  2541. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  2542. }
  2543. if (src1->type == GGML_TYPE_F16) {
  2544. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  2545. }
  2546. // do the computation
  2547. SYCL_CHECK(CHECK_TRY_ERROR(op(ctx, src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  2548. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
  2549. /*
  2550. DPCT1010:93: SYCL uses exceptions to report errors and does not
  2551. use the error codes. The call was replaced with 0. You need to
  2552. rewrite this code.
  2553. */
  2554. SYCL_CHECK(0);
  2555. // copy dst to host or other device if necessary
  2556. if (!dst_on_device) {
  2557. void * dst_off_device = dst->data;
  2558. if (split) {
  2559. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  2560. // dst is NOT transposed.
  2561. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  2562. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  2563. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  2564. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  2565. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  2566. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  2567. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  2568. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  2569. row_diff * sizeof(float), row_diff * sizeof(float),
  2570. src1_ncols, dpct::device_to_device, *stream)));
  2571. } else {
  2572. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  2573. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  2574. dhf_dst_i += src1_col_0*ne0;
  2575. SYCL_CHECK(CHECK_TRY_ERROR(
  2576. stream->memcpy(dhf_dst_i, dst_dd_i,
  2577. src1_ncols * ne0 * sizeof(float)).wait()));
  2578. }
  2579. }
  2580. // add event for the main device to wait on until other device is done
  2581. if (split && (i != ctx.device || is != 0)) {
  2582. /*
  2583. DPCT1024:94: The original code returned the error code that
  2584. was further consumed by the program logic. This original
  2585. code was replaced with 0. You may need to rewrite the
  2586. program logic consuming the error code.
  2587. */
  2588. SYCL_CHECK(CHECK_TRY_ERROR(
  2589. *src0_extra->events[i][is] =
  2590. stream->ext_oneapi_submit_barrier()));
  2591. }
  2592. }
  2593. }
  2594. }
  2595. // main device waits for all other devices to be finished
  2596. if (split && ggml_sycl_info().device_count > 1) {
  2597. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  2598. is_max = is_max <= GGML_SYCL_MAX_STREAMS ? is_max : GGML_SYCL_MAX_STREAMS;
  2599. ggml_sycl_set_device(ctx.device);
  2600. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2601. if (dev[i].row_low == dev[i].row_high) {
  2602. continue;
  2603. }
  2604. for (int64_t is = 0; is < is_max; ++is) {
  2605. SYCL_CHECK(CHECK_TRY_ERROR(
  2606. ctx.stream()->ext_oneapi_submit_barrier(
  2607. {*src0_extra->events[i][is]})));
  2608. }
  2609. }
  2610. }
  2611. }
  2612. catch (sycl::exception const &exc) {
  2613. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2614. << ", line:" << __LINE__ << std::endl;
  2615. std::exit(1);
  2616. }
  2617. static void ggml_sycl_repeat(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2618. GGML_SYCL_DEBUG("call %s\n", __func__);
  2619. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_repeat);
  2620. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2621. }
  2622. static void ggml_sycl_get_rows(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2623. GGML_SYCL_DEBUG("call %s\n", __func__);
  2624. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_get_rows);
  2625. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2626. }
  2627. static void ggml_sycl_add(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2628. GGML_SYCL_DEBUG("call %s\n", __func__);
  2629. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_add);
  2630. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2631. }
  2632. static void ggml_sycl_acc(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2633. GGML_SYCL_DEBUG("call %s\n", __func__);
  2634. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_acc);
  2635. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2636. }
  2637. static void ggml_sycl_mul(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2638. GGML_SYCL_DEBUG("call %s\n", __func__);
  2639. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_mul);
  2640. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2641. }
  2642. static void ggml_sycl_div(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2643. GGML_SYCL_DEBUG("call %s\n", __func__);
  2644. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_div);
  2645. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2646. }
  2647. static void ggml_sycl_gelu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2648. GGML_SYCL_DEBUG("call %s\n", __func__);
  2649. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_gelu);
  2650. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2651. }
  2652. static void ggml_sycl_silu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2653. GGML_SYCL_DEBUG("call %s\n", __func__);
  2654. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_silu);
  2655. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2656. }
  2657. static void ggml_sycl_gelu_quick(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2658. GGML_SYCL_DEBUG("call %s\n", __func__);
  2659. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_gelu_quick);
  2660. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2661. }
  2662. static void ggml_sycl_tanh(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2663. GGML_SYCL_DEBUG("call %s\n", __func__);
  2664. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_tanh);
  2665. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2666. }
  2667. static void ggml_sycl_relu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2668. GGML_SYCL_DEBUG("call %s\n", __func__);
  2669. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_relu);
  2670. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2671. }
  2672. static void ggml_sycl_hardsigmoid(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2673. GGML_SYCL_DEBUG("call %s\n", __func__);
  2674. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_hardsigmoid);
  2675. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2676. }
  2677. static void ggml_sycl_hardswish(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2678. GGML_SYCL_DEBUG("call %s\n", __func__);
  2679. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_hardswish);
  2680. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2681. }
  2682. static void ggml_sycl_leaky_relu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2683. GGML_SYCL_DEBUG("call %s\n", __func__);
  2684. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_leaky_relu);
  2685. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2686. }
  2687. static void ggml_sycl_sqr(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2688. GGML_SYCL_DEBUG("call %s\n", __func__);
  2689. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_sqr);
  2690. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2691. }
  2692. static void ggml_sycl_norm(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2693. GGML_SYCL_DEBUG("call %s\n", __func__);
  2694. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_norm);
  2695. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2696. }
  2697. static void ggml_sycl_group_norm(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2698. GGML_SYCL_DEBUG("call %s\n", __func__);
  2699. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_group_norm);
  2700. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2701. }
  2702. static void ggml_sycl_upscale(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2703. GGML_SYCL_DEBUG("call %s\n", __func__);
  2704. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_upscale);
  2705. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2706. }
  2707. static void ggml_sycl_pad(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2708. GGML_SYCL_DEBUG("call %s\n", __func__);
  2709. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_pad);
  2710. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2711. }
  2712. static void ggml_sycl_rms_norm(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2713. GGML_SYCL_DEBUG("call %s\n", __func__);
  2714. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_rms_norm);
  2715. GGML_SYCL_DEBUG("call %s done\n", __func__);
  2716. }
  2717. static void ggml_sycl_mul_mat_vec_p021(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2718. const ggml_tensor *src1,
  2719. ggml_tensor *dst) try {
  2720. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  2721. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2722. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  2723. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  2724. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2725. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2726. const int64_t ne00 = src0->ne[0];
  2727. const int64_t ne01 = src0->ne[1];
  2728. const int64_t ne02 = src0->ne[2];
  2729. const int64_t ne12 = src1->ne[2];
  2730. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  2731. queue_ptr main_stream = ctx.stream();
  2732. void * src0_ddq = src0->data;
  2733. float * src1_ddf = (float *) src1->data;
  2734. float * dst_ddf = (float *) dst->data;
  2735. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  2736. }
  2737. catch (sycl::exception const &exc) {
  2738. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2739. << ", line:" << __LINE__ << std::endl;
  2740. std::exit(1);
  2741. }
  2742. static void ggml_sycl_mul_mat_vec_nc(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2743. const ggml_tensor *src1,
  2744. ggml_tensor *dst) try {
  2745. GGML_ASSERT(!ggml_is_transposed(src0));
  2746. GGML_ASSERT(!ggml_is_transposed(src1));
  2747. GGML_ASSERT(!ggml_is_permuted(src0));
  2748. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2749. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2750. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2751. const int64_t ne00 = src0->ne[0];
  2752. const int64_t ne01 = src0->ne[1];
  2753. const int64_t ne02 = src0->ne[2];
  2754. const int64_t nb01 = src0->nb[1];
  2755. const int64_t nb02 = src0->nb[2];
  2756. const int64_t ne12 = src1->ne[2];
  2757. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  2758. queue_ptr main_stream = ctx.stream();
  2759. void * src0_ddq = src0->data;
  2760. float * src1_ddf = (float *) src1->data;
  2761. float * dst_ddf = (float *) dst->data;
  2762. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  2763. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  2764. ggml_mul_mat_vec_nc_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, ne12, channel_stride_x, main_stream);
  2765. }
  2766. catch (sycl::exception const &exc) {
  2767. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2768. << ", line:" << __LINE__ << std::endl;
  2769. std::exit(1);
  2770. }
  2771. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  2772. const sycl::half *src1_as_f16, char *dst,
  2773. const void **ptrs_src, void **ptrs_dst,
  2774. int64_t ne12, int64_t ne13, int64_t ne23,
  2775. size_t nb02, size_t nb03, size_t nb12,
  2776. size_t nb13, size_t nbd2, size_t nbd3,
  2777. int64_t r2, int64_t r3,
  2778. const sycl::nd_item<3> &item_ct1) {
  2779. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  2780. item_ct1.get_local_id(2);
  2781. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  2782. item_ct1.get_local_id(1);
  2783. if (i13 >= ne13 || i12 >= ne12) {
  2784. return;
  2785. }
  2786. int64_t i03 = i13 / r3;
  2787. int64_t i02 = i12 / r2;
  2788. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  2789. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  2790. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  2791. }
  2792. static void ggml_sycl_mul_mat_batched_sycl(ggml_backend_sycl_context & ctx,
  2793. const ggml_tensor *src0,
  2794. const ggml_tensor *src1,
  2795. ggml_tensor *dst) try {
  2796. GGML_ASSERT(!ggml_is_transposed(src0));
  2797. GGML_ASSERT(!ggml_is_transposed(src1));
  2798. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2799. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2800. GGML_TENSOR_BINARY_OP_LOCALS
  2801. const int64_t ne_dst = ggml_nelements(dst);
  2802. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  2803. queue_ptr main_stream = ctx.stream();;
  2804. void * src0_ddq = src0->data;
  2805. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  2806. float * src1_ddf = (float *) src1->data;
  2807. float * dst_ddf = (float *) dst->data;
  2808. // convert src1 to fp16
  2809. ggml_sycl_pool_alloc<sycl::half> src1_f16_alloc(ctx.pool());
  2810. if (src1->type != GGML_TYPE_F16) {
  2811. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  2812. const int64_t ne_src1 = ggml_nelements(src1);
  2813. src1_f16_alloc.alloc(ne_src1);
  2814. GGML_ASSERT(to_fp16_sycl != nullptr);
  2815. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  2816. }
  2817. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  2818. : src1_f16_alloc.get();
  2819. char * dst_t;
  2820. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  2821. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  2822. // dst strides
  2823. size_t nbd2 = dst->nb[2];
  2824. size_t nbd3 = dst->nb[3];
  2825. const float alpha_f32 = 1.0f;
  2826. const float beta_f32 = 0.0f;
  2827. const void * alpha = &alpha_f32;
  2828. const void * beta = &beta_f32;
  2829. dst_t = (char *) dst_ddf;
  2830. GGML_ASSERT(ne12 % ne02 == 0);
  2831. GGML_ASSERT(ne13 % ne03 == 0);
  2832. // broadcast factors
  2833. const int64_t r2 = ne12/ne02;
  2834. const int64_t r3 = ne13/ne03;
  2835. if (r2 == 1 && r3 == 1 && ggml_is_contiguous_2(src0) && ggml_is_contiguous_2(src1)) {
  2836. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  2837. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  2838. *main_stream, oneapi::mkl::transpose::trans,
  2839. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  2840. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  2841. nb01 / nb00, nb02 / nb00,
  2842. (const char *)src1_f16, dpct::library_data_t::real_half,
  2843. nb11 / nb10, nb12 / nb10, beta,
  2844. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  2845. ne12 * ne13, cu_compute_type)));
  2846. } else {
  2847. const int ne23 = ne12*ne13;
  2848. ggml_sycl_pool_alloc<const void *> ptrs_src(ctx.pool(), 2*ne23);
  2849. ggml_sycl_pool_alloc< void *> ptrs_dst(ctx.pool(), 1*ne23);
  2850. sycl::range<3> block_dims(1, ne12, ne13);
  2851. /*
  2852. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  2853. the limit. To get the device limit, query
  2854. info::device::max_work_group_size. Adjust the work-group size if needed.
  2855. */
  2856. {
  2857. dpct::has_capability_or_fail(main_stream->get_device(),
  2858. {sycl::aspect::fp16});
  2859. main_stream->submit([&](sycl::handler &cgh) {
  2860. const void **ptrs_src_get = ptrs_src.get();
  2861. void **ptrs_dst_get = ptrs_dst.get();
  2862. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  2863. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  2864. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  2865. [=](sycl::nd_item<3> item_ct1) {
  2866. k_compute_batched_ptrs(
  2867. src0_as_f16, src1_f16,
  2868. dst_t, ptrs_src_get,
  2869. ptrs_dst_get, ne12, ne13, ne23,
  2870. nb02, nb03, nb12_scaled, nb13_scaled,
  2871. nbd2, nbd3, r2, r3, item_ct1);
  2872. });
  2873. });
  2874. }
  2875. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  2876. *main_stream, oneapi::mkl::transpose::trans,
  2877. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  2878. (const void **)(ptrs_src.get() + 0 * ne23),
  2879. dpct::library_data_t::real_half, nb01 / nb00,
  2880. (const void **)(ptrs_src.get() + 1 * ne23),
  2881. dpct::library_data_t::real_half, nb11 / nb10, beta,
  2882. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  2883. cu_compute_type)));
  2884. }
  2885. }
  2886. catch (sycl::exception const &exc) {
  2887. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2888. << ", line:" << __LINE__ << std::endl;
  2889. std::exit(1);
  2890. }
  2891. inline bool ggml_sycl_supports_mmq(enum ggml_type type) {
  2892. // TODO: accuracy issues in MMQ
  2893. return false;
  2894. }
  2895. bool ggml_sycl_supports_dmmv(enum ggml_type type) {
  2896. switch (type) {
  2897. case GGML_TYPE_Q4_0:
  2898. case GGML_TYPE_Q4_1:
  2899. case GGML_TYPE_Q5_0:
  2900. case GGML_TYPE_Q5_1:
  2901. case GGML_TYPE_Q8_0:
  2902. case GGML_TYPE_Q2_K:
  2903. case GGML_TYPE_Q3_K:
  2904. case GGML_TYPE_Q4_K:
  2905. case GGML_TYPE_Q5_K:
  2906. case GGML_TYPE_Q6_K:
  2907. case GGML_TYPE_F16:
  2908. return true;
  2909. default:
  2910. return false;
  2911. }
  2912. }
  2913. static void ggml_sycl_mul_mat(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  2914. const bool split = ggml_backend_buffer_is_sycl_split(src0->buffer);
  2915. int64_t min_compute_capability = INT_MAX;
  2916. if (split) {
  2917. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  2918. auto & tensor_split = buft_ctx->tensor_split;
  2919. for (int id = 0; id < ggml_sycl_info().device_count; ++id) {
  2920. // skip devices that are not going to do any work:
  2921. if (tensor_split[id] >= (id + 1 < ggml_sycl_info().device_count ? tensor_split[id + 1] : 1.0f)) {
  2922. continue;
  2923. }
  2924. if (min_compute_capability > ggml_sycl_info().devices[id].cc) {
  2925. min_compute_capability = ggml_sycl_info().devices[id].cc;
  2926. }
  2927. }
  2928. } else {
  2929. min_compute_capability = ggml_sycl_info().devices[ctx.device].cc;
  2930. }
  2931. // check data types and tensor shapes for custom matrix multiplication kernels:
  2932. bool use_dequantize_mul_mat_vec = ggml_sycl_supports_dmmv(src0->type)
  2933. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  2934. && src0->ne[0] % GGML_SYCL_DMMV_X == 0 && src1->ne[1] == 1;
  2935. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  2936. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  2937. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE
  2938. && (ctx.stream()->get_backend() == sycl::backend::ext_oneapi_cuda || src1->ne[1] > MMVQ_MIN_BATCH_SIZE);
  2939. bool use_mul_mat_q = ggml_sycl_supports_mmq(src0->type)
  2940. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  2941. // mmvq and mmq need the __dp4a instruction which is available for gen12+
  2942. // Workaround in https://github.com/ggerganov/llama.cpp/commit/95f84d5ce8b449a9b16009434aca800df504a02e
  2943. use_mul_mat_q = use_mul_mat_q && (src0->type != GGML_TYPE_IQ2_XXS);
  2944. #ifdef SYCL_USE_XMX
  2945. use_mul_mat_q = use_mul_mat_q && (src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  2946. #endif // SYCL_USE_XMX
  2947. // mmvq path is faster in the CUDA backend.
  2948. if (ctx.stream()->get_backend() == sycl::backend::ext_oneapi_cuda)
  2949. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  2950. if (!split && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  2951. // KQ single-batch
  2952. ggml_sycl_mul_mat_vec_p021(ctx, src0, src1, dst);
  2953. } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  2954. // KQV single-batch
  2955. ggml_sycl_mul_mat_vec_nc(ctx, src0, src1, dst);
  2956. } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  2957. // KQ + KQV multi-batch
  2958. ggml_sycl_mul_mat_batched_sycl(ctx, src0, src1, dst);
  2959. } else if (use_dequantize_mul_mat_vec) {
  2960. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  2961. } else if (use_mul_mat_vec_q) {
  2962. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  2963. } else if (use_mul_mat_q) {
  2964. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  2965. } else {
  2966. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  2967. }
  2968. }
  2969. struct mmid_row_mapping {
  2970. int32_t i1;
  2971. int32_t i2;
  2972. };
  2973. __dpct_inline__ static void k_copy_src1_to_contiguous(
  2974. const char *__restrict__ src1_original, char *__restrict__ src1_contiguous,
  2975. int *__restrict__ cur_src1_row, mmid_row_mapping *__restrict__ row_mapping,
  2976. const char *__restrict ids, int64_t i02, size_t ids_nb1, size_t ids_nb0,
  2977. int64_t ne11, int64_t ne10, size_t nb11, size_t nb12,
  2978. const sycl::nd_item<3> &item_ct1, int &src1_row) {
  2979. int32_t iid1 = item_ct1.get_group(2);
  2980. int32_t id = item_ct1.get_group(1);
  2981. const int32_t row_id_i = *(const int32_t *) (ids + iid1*ids_nb1 + id*ids_nb0);
  2982. if (row_id_i != i02) {
  2983. return;
  2984. }
  2985. const int64_t i11 = id % ne11;
  2986. const int64_t i12 = iid1;
  2987. if (item_ct1.get_local_id(2) == 0) {
  2988. src1_row =
  2989. dpct::atomic_fetch_add<sycl::access::address_space::generic_space>(
  2990. cur_src1_row, 1);
  2991. row_mapping[src1_row] = {id, iid1};
  2992. }
  2993. /*
  2994. DPCT1065:194: Consider replacing sycl::nd_item::barrier() with
  2995. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  2996. performance if there is no access to global memory.
  2997. */
  2998. item_ct1.barrier();
  2999. const float * src1_row_original = (const float *)(src1_original + i11*nb11 + i12*nb12);
  3000. float * src1_row_contiguous = (float *)(src1_contiguous + src1_row*nb11);
  3001. #pragma unroll
  3002. for (int i = item_ct1.get_local_id(2); i < ne10;
  3003. i += item_ct1.get_local_range(2)) {
  3004. src1_row_contiguous[i] = src1_row_original[i];
  3005. }
  3006. }
  3007. __dpct_inline__ static void k_copy_dst_from_contiguous(
  3008. char *__restrict__ dst_original, const char *__restrict__ dst_contiguous,
  3009. const mmid_row_mapping *__restrict__ row_mapping, int64_t ne0, size_t nb1,
  3010. size_t nb2, const sycl::nd_item<3> &item_ct1) {
  3011. int32_t i = item_ct1.get_group(2);
  3012. const int32_t i1 = row_mapping[i].i1;
  3013. const int32_t i2 = row_mapping[i].i2;
  3014. const float * dst_row_contiguous = (const float *)(dst_contiguous + i*nb1);
  3015. float * dst_row_original = (float *)(dst_original + i1*nb1 + i2*nb2);
  3016. #pragma unroll
  3017. for (int j = item_ct1.get_local_id(2); j < ne0;
  3018. j += item_ct1.get_local_range(2)) {
  3019. dst_row_original[j] = dst_row_contiguous[j];
  3020. }
  3021. }
  3022. static void ggml_sycl_mul_mat_id(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  3023. const ggml_tensor *src1,
  3024. ggml_tensor *dst) try {
  3025. GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer) && "mul_mat_id does not support split buffers");
  3026. const ggml_tensor *ids = dst->src[2];
  3027. GGML_TENSOR_BINARY_OP_LOCALS
  3028. const queue_ptr stream = ctx.stream();
  3029. const int64_t n_as = ne02;
  3030. const int64_t n_ids = ids->ne[0];
  3031. std::vector<char> ids_host(ggml_nbytes(ids));
  3032. const char * ids_dev = (const char *) ids->data;
  3033. SYCL_CHECK(CHECK_TRY_ERROR(
  3034. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  3035. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  3036. ggml_tensor src0_row = *src0;
  3037. ggml_tensor src1_row = *src1;
  3038. ggml_tensor dst_row = *dst;
  3039. char *src0_original = (char *)src0->data;
  3040. char *src1_original = (char *)src1->data;
  3041. char *dst_original = (char *)dst->data;
  3042. src0_row.ne[2] = 1;
  3043. src0_row.ne[3] = 1;
  3044. src0_row.nb[3] = nb02;
  3045. src1_row.ne[1] = 1;
  3046. src1_row.ne[2] = 1;
  3047. src1_row.ne[3] = 1;
  3048. src1_row.nb[2] = nb11;
  3049. src1_row.nb[3] = nb11;
  3050. dst_row.ne[1] = 1;
  3051. dst_row.ne[2] = 1;
  3052. dst_row.ne[3] = 1;
  3053. dst_row.nb[2] = nb1;
  3054. dst_row.nb[3] = nb1;
  3055. if (ne12 == 1) {
  3056. for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
  3057. for (int64_t id = 0; id < n_ids; id++) {
  3058. const int32_t i02 = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
  3059. GGML_ASSERT(i02 >= 0 && i02 < n_as);
  3060. const int64_t i11 = id % ne11;
  3061. const int64_t i12 = iid1;
  3062. const int64_t i1 = id;
  3063. const int64_t i2 = i12;
  3064. src0_row.data = src0_original + i02*nb02;
  3065. src1_row.data = src1_original + + i11*nb11 + i12*nb12;
  3066. dst_row.data = dst_original + i1*nb1 + i2*nb2;
  3067. ggml_sycl_mul_mat(ctx, &src0_row, &src1_row, &dst_row);
  3068. }
  3069. }
  3070. } else {
  3071. ggml_sycl_pool_alloc<char> src1_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(src1));
  3072. ggml_sycl_pool_alloc<char> dst_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(dst));
  3073. src1_row.data = src1_contiguous.get();
  3074. dst_row.data = dst_contiguous.get();
  3075. for (int64_t i02 = 0; i02 < n_as; i02++) {
  3076. int64_t num_src1_rows = 0;
  3077. for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
  3078. for (int64_t id = 0; id < n_ids; id++) {
  3079. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
  3080. GGML_ASSERT(row_id_i >= 0 && row_id_i < n_as);
  3081. if (row_id_i != i02) {
  3082. continue;
  3083. }
  3084. num_src1_rows++;
  3085. }
  3086. }
  3087. if (num_src1_rows == 0) {
  3088. continue;
  3089. }
  3090. ggml_sycl_pool_alloc<int> dev_cur_src1_row(ctx.pool(), 1);
  3091. ggml_sycl_pool_alloc<mmid_row_mapping> dev_row_mapping(ctx.pool(), num_src1_rows);
  3092. SYCL_CHECK(CHECK_TRY_ERROR(
  3093. stream->memset(dev_cur_src1_row.get(), 0, sizeof(int))));
  3094. {
  3095. sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne10, 768u));
  3096. sycl::range<3> grid_dims(1, n_ids, ids->ne[1]);
  3097. stream->submit([&](sycl::handler &cgh) {
  3098. sycl::local_accessor<int, 0> src1_row_acc(cgh);
  3099. char *__restrict src1_contiguous_get =
  3100. src1_contiguous.get();
  3101. int *__restrict dev_cur_src1_row_get =
  3102. dev_cur_src1_row.get();
  3103. mmid_row_mapping *__restrict dev_row_mapping_get =
  3104. dev_row_mapping.get();
  3105. size_t ids_nb_ct6 = ids->nb[1];
  3106. size_t ids_nb_ct7 = ids->nb[0];
  3107. cgh.parallel_for(
  3108. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  3109. [=](sycl::nd_item<3> item_ct1) {
  3110. k_copy_src1_to_contiguous(
  3111. src1_original, src1_contiguous_get,
  3112. dev_cur_src1_row_get,
  3113. dev_row_mapping_get, ids_dev, i02,
  3114. ids_nb_ct6, ids_nb_ct7, ne11, ne10, nb11, nb12,
  3115. item_ct1, src1_row_acc);
  3116. });
  3117. });
  3118. }
  3119. src0_row.data = src0_original + i02*nb02;
  3120. GGML_ASSERT(nb11 == sizeof(float)*ne10);
  3121. GGML_ASSERT(nb1 == sizeof(float)*ne0);
  3122. src1_row.ne[1] = num_src1_rows;
  3123. src1_row.nb[1] = nb11;
  3124. src1_row.nb[2] = num_src1_rows*nb11;
  3125. src1_row.nb[3] = num_src1_rows*nb11;
  3126. dst_row.ne[1] = num_src1_rows;
  3127. dst_row.nb[1] = nb1;
  3128. dst_row.nb[2] = num_src1_rows*nb1;
  3129. dst_row.nb[3] = num_src1_rows*nb1;
  3130. ggml_sycl_mul_mat(ctx, &src0_row, &src1_row, &dst_row);
  3131. {
  3132. sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne0, 768u));
  3133. sycl::range<3> grid_dims(1, 1, num_src1_rows);
  3134. stream->submit([&](sycl::handler &cgh) {
  3135. const char *__restrict dst_contiguous_get =
  3136. dst_contiguous.get();
  3137. const mmid_row_mapping *__restrict dev_row_mapping_get =
  3138. dev_row_mapping.get();
  3139. cgh.parallel_for(
  3140. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  3141. [=](sycl::nd_item<3> item_ct1) {
  3142. k_copy_dst_from_contiguous(dst_original,
  3143. dst_contiguous_get,
  3144. dev_row_mapping_get,
  3145. ne0, nb1, nb2, item_ct1);
  3146. });
  3147. });
  3148. }
  3149. }
  3150. }
  3151. }
  3152. catch (sycl::exception const &exc) {
  3153. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3154. << ", line:" << __LINE__ << std::endl;
  3155. std::exit(1);
  3156. }
  3157. static void ggml_sycl_scale(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3158. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_scale);
  3159. }
  3160. static void ggml_sycl_clamp(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3161. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_clamp);
  3162. }
  3163. static void ggml_sycl_cpy(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  3164. ggml_tensor *dst) try {
  3165. const int64_t ne = ggml_nelements(src0);
  3166. GGML_ASSERT(ne == ggml_nelements(src1));
  3167. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  3168. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  3169. GGML_TENSOR_BINARY_OP_LOCALS01;
  3170. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  3171. queue_ptr main_stream = ctx.stream();
  3172. char * src0_ddc = (char *) src0->data;
  3173. char * src1_ddc = (char *) src1->data;
  3174. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  3175. ggml_cpy_f32_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3176. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  3177. ggml_cpy_f32_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3178. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  3179. ggml_cpy_f32_q8_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3180. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  3181. ggml_cpy_f32_q4_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3182. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  3183. ggml_cpy_f32_q4_1_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3184. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  3185. ggml_cpy_f16_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3186. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  3187. ggml_cpy_f16_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3188. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  3189. ggml_cpy_i16_i16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3190. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  3191. ggml_cpy_i32_i32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3192. } else {
  3193. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  3194. ggml_type_name(src0->type), ggml_type_name(src1->type));
  3195. GGML_ABORT("fatal error");
  3196. }
  3197. (void) dst;
  3198. }
  3199. catch (sycl::exception const &exc) {
  3200. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3201. << ", line:" << __LINE__ << std::endl;
  3202. std::exit(1);
  3203. }
  3204. static void ggml_sycl_dup(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3205. // TODO: why do we pass dst as src1 here?
  3206. ggml_sycl_cpy(ctx, src0, dst, nullptr);
  3207. (void) src1;
  3208. }
  3209. static void ggml_sycl_diag_mask_inf(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3210. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  3211. }
  3212. static void ggml_sycl_soft_max(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3213. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_soft_max);
  3214. }
  3215. static void ggml_sycl_rope(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3216. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  3217. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_rope);
  3218. }
  3219. static void ggml_sycl_pool2d(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3220. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_pool2d);
  3221. }
  3222. static void ggml_sycl_im2col(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3223. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_im2col);
  3224. }
  3225. static void ggml_sycl_sum_rows(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3226. GGML_ASSERT(ggml_is_contiguous(src0));
  3227. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_sum_rows);
  3228. }
  3229. static void ggml_sycl_argsort(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3230. GGML_ASSERT(ggml_is_contiguous(src0));
  3231. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_argsort);
  3232. }
  3233. static void ggml_sycl_nop(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3234. (void) src0;
  3235. (void) src1;
  3236. (void) dst;
  3237. }
  3238. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  3239. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  3240. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  3241. }
  3242. void ggml_sycl_set_main_device(const int main_device) try {
  3243. if (dpct::get_current_device_id() == main_device) return;
  3244. check_allow_gpu_index(main_device);
  3245. dpct::select_device(main_device);
  3246. if (g_ggml_sycl_debug) {
  3247. dpct::device_info prop;
  3248. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  3249. prop, dpct::dev_mgr::instance().get_device(main_device))));
  3250. fprintf(stderr, "Using device %d (%s) as main device\n",
  3251. main_device, prop.get_name());
  3252. }
  3253. }
  3254. catch (sycl::exception const &exc) {
  3255. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3256. << ", line:" << __LINE__ << std::endl;
  3257. std::exit(1);
  3258. }
  3259. bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tensor * tensor) {
  3260. if (!g_sycl_loaded) return false;
  3261. ggml_sycl_func_t func;
  3262. switch (tensor->op) {
  3263. case GGML_OP_CONV_TRANSPOSE_1D:
  3264. func = ggml_sycl_op_conv_transpose_1d;
  3265. break;
  3266. case GGML_OP_REPEAT:
  3267. func = ggml_sycl_repeat;
  3268. break;
  3269. case GGML_OP_GET_ROWS:
  3270. func = ggml_sycl_get_rows;
  3271. break;
  3272. case GGML_OP_DUP:
  3273. func = ggml_sycl_dup;
  3274. break;
  3275. case GGML_OP_ADD:
  3276. func = ggml_sycl_add;
  3277. break;
  3278. case GGML_OP_ACC:
  3279. func = ggml_sycl_acc;
  3280. break;
  3281. case GGML_OP_MUL:
  3282. func = ggml_sycl_mul;
  3283. break;
  3284. case GGML_OP_DIV:
  3285. func = ggml_sycl_div;
  3286. break;
  3287. case GGML_OP_UNARY:
  3288. switch (ggml_get_unary_op(tensor)) {
  3289. case GGML_UNARY_OP_GELU:
  3290. func = ggml_sycl_gelu;
  3291. break;
  3292. case GGML_UNARY_OP_SILU:
  3293. func = ggml_sycl_silu;
  3294. break;
  3295. case GGML_UNARY_OP_GELU_QUICK:
  3296. func = ggml_sycl_gelu_quick;
  3297. break;
  3298. case GGML_UNARY_OP_TANH:
  3299. func = ggml_sycl_tanh;
  3300. break;
  3301. case GGML_UNARY_OP_RELU:
  3302. func = ggml_sycl_relu;
  3303. break;
  3304. case GGML_UNARY_OP_HARDSIGMOID:
  3305. func = ggml_sycl_hardsigmoid;
  3306. break;
  3307. case GGML_UNARY_OP_HARDSWISH:
  3308. func = ggml_sycl_hardswish;
  3309. break;
  3310. default:
  3311. return false;
  3312. }
  3313. break;
  3314. case GGML_OP_NORM:
  3315. func = ggml_sycl_norm;
  3316. break;
  3317. case GGML_OP_GROUP_NORM:
  3318. func = ggml_sycl_group_norm;
  3319. break;
  3320. case GGML_OP_CONCAT:
  3321. func = ggml_sycl_op_concat;
  3322. break;
  3323. case GGML_OP_UPSCALE:
  3324. func = ggml_sycl_upscale;
  3325. break;
  3326. case GGML_OP_PAD:
  3327. func = ggml_sycl_pad;
  3328. break;
  3329. case GGML_OP_LEAKY_RELU:
  3330. func = ggml_sycl_leaky_relu;
  3331. break;
  3332. case GGML_OP_RMS_NORM:
  3333. func = ggml_sycl_rms_norm;
  3334. break;
  3335. case GGML_OP_MUL_MAT:
  3336. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  3337. return false;
  3338. }
  3339. func = ggml_sycl_mul_mat;
  3340. break;
  3341. case GGML_OP_MUL_MAT_ID:
  3342. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  3343. return false;
  3344. }
  3345. func = ggml_sycl_mul_mat_id;
  3346. break;
  3347. case GGML_OP_SCALE:
  3348. func = ggml_sycl_scale;
  3349. break;
  3350. case GGML_OP_SQR:
  3351. func = ggml_sycl_sqr;
  3352. break;
  3353. case GGML_OP_CLAMP:
  3354. func = ggml_sycl_clamp;
  3355. break;
  3356. case GGML_OP_CPY:
  3357. func = ggml_sycl_cpy;
  3358. break;
  3359. case GGML_OP_CONT:
  3360. func = ggml_sycl_dup;
  3361. break;
  3362. case GGML_OP_NONE:
  3363. case GGML_OP_RESHAPE:
  3364. case GGML_OP_VIEW:
  3365. case GGML_OP_PERMUTE:
  3366. case GGML_OP_TRANSPOSE:
  3367. func = ggml_sycl_nop;
  3368. break;
  3369. case GGML_OP_DIAG_MASK_INF:
  3370. func = ggml_sycl_diag_mask_inf;
  3371. break;
  3372. case GGML_OP_SOFT_MAX:
  3373. func = ggml_sycl_soft_max;
  3374. break;
  3375. case GGML_OP_ROPE:
  3376. func = ggml_sycl_rope;
  3377. break;
  3378. case GGML_OP_IM2COL:
  3379. func = ggml_sycl_im2col;
  3380. break;
  3381. case GGML_OP_POOL_2D:
  3382. func = ggml_sycl_pool2d;
  3383. break;
  3384. case GGML_OP_SUM_ROWS:
  3385. func = ggml_sycl_sum_rows;
  3386. break;
  3387. case GGML_OP_ARGSORT:
  3388. func = ggml_sycl_argsort;
  3389. break;
  3390. case GGML_OP_TIMESTEP_EMBEDDING:
  3391. func = ggml_sycl_op_timestep_embedding;
  3392. break;
  3393. default:
  3394. return false;
  3395. }
  3396. if (tensor->src[0] != nullptr && ggml_backend_buffer_is_sycl_split(tensor->src[0]->buffer)) {
  3397. ggml_sycl_set_peer_access(tensor->src[1]->ne[1], ctx.device);
  3398. }
  3399. func(ctx, tensor->src[0], tensor->src[1], tensor);
  3400. return true;
  3401. }
  3402. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  3403. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_gpu_list\n");
  3404. for(int i=0;i<max_len;i++) id_list[i] = -1;
  3405. for (int i=0;i< ggml_sycl_info().device_count;i++){
  3406. if (i>=max_len) break;
  3407. id_list[i] = i;
  3408. }
  3409. return;
  3410. }
  3411. catch (sycl::exception const &exc) {
  3412. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3413. << ", line:" << __LINE__ << std::endl;
  3414. std::exit(1);
  3415. }
  3416. int ggml_sycl_get_device_count() try {
  3417. int device_count;
  3418. if (CHECK_TRY_ERROR(device_count =
  3419. dpct::dev_mgr::instance().device_count()) != 0) {
  3420. return 0;
  3421. }
  3422. return device_count;
  3423. }
  3424. catch (sycl::exception const &exc) {
  3425. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3426. << ", line:" << __LINE__ << std::endl;
  3427. std::exit(1);
  3428. }
  3429. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  3430. size_t description_size) try {
  3431. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_device_description\n");
  3432. dpct::device_info prop;
  3433. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  3434. prop, dpct::dev_mgr::instance().get_device(device))));
  3435. snprintf(description, description_size, "%s", prop.get_name());
  3436. }
  3437. catch (sycl::exception const &exc) {
  3438. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3439. << ", line:" << __LINE__ << std::endl;
  3440. std::exit(1);
  3441. }
  3442. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  3443. size_t *total) try {
  3444. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  3445. ggml_sycl_set_device(device);
  3446. /*
  3447. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  3448. error codes. The original code was commented out and a warning string was
  3449. inserted. You need to rewrite this code.
  3450. */
  3451. /*
  3452. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  3453. device information which may not be supported by all compilers or runtimes.
  3454. You may need to adjust the code.
  3455. */
  3456. SYCL_CHECK(CHECK_TRY_ERROR(
  3457. dpct::dev_mgr::instance().get_device(device).get_memory_info(*free, *total)));
  3458. }
  3459. catch (sycl::exception const &exc) {
  3460. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3461. << ", line:" << __LINE__ << std::endl;
  3462. std::exit(1);
  3463. }
  3464. ////////////////////////////////////////////////////////////////////////////////
  3465. // backend interface
  3466. #define UNUSED GGML_UNUSED
  3467. // sycl buffer
  3468. struct ggml_backend_sycl_buffer_context {
  3469. int device;
  3470. void * dev_ptr = nullptr;
  3471. queue_ptr stream;
  3472. std::string name;
  3473. ggml_backend_sycl_buffer_context(int device, void * dev_ptr, queue_ptr stream) :
  3474. device(device), dev_ptr(dev_ptr), stream(stream) {
  3475. check_allow_gpu_index(device);
  3476. name = (GGML_SYCL_NAME + std::to_string(device));
  3477. }
  3478. ~ggml_backend_sycl_buffer_context() {
  3479. if (dev_ptr != nullptr) {
  3480. ggml_sycl_set_device(device);
  3481. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(dev_ptr, *stream)));
  3482. }
  3483. }
  3484. };
  3485. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  3486. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  3487. return ctx->name.c_str();
  3488. }
  3489. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  3490. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  3491. }
  3492. static void
  3493. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  3494. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  3495. ggml_sycl_set_device(ctx->device);
  3496. delete ctx;
  3497. }
  3498. catch (sycl::exception const &exc) {
  3499. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3500. << ", line:" << __LINE__ << std::endl;
  3501. std::exit(1);
  3502. }
  3503. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  3504. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  3505. return ctx->dev_ptr;
  3506. }
  3507. GGML_CALL static void
  3508. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  3509. ggml_tensor *tensor) try {
  3510. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  3511. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  3512. assert(tensor->view_src->buffer->buft == buffer->buft);
  3513. tensor->backend = tensor->view_src->backend;
  3514. tensor->extra = tensor->view_src->extra;
  3515. return;
  3516. }
  3517. if (ggml_is_quantized(tensor->type)) {
  3518. // initialize padding to 0 to avoid possible NaN values
  3519. size_t original_size = ggml_nbytes(tensor);
  3520. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  3521. if (padded_size > original_size && tensor->view_src == nullptr) {
  3522. SYCL_CHECK(CHECK_TRY_ERROR(ctx->stream->memset(
  3523. (char *)tensor->data + original_size, 0,
  3524. padded_size - original_size).wait()));
  3525. }
  3526. }
  3527. }
  3528. catch (sycl::exception const &exc) {
  3529. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3530. << ", line:" << __LINE__ << std::endl;
  3531. std::exit(1);
  3532. }
  3533. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  3534. ggml_tensor *tensor,
  3535. const void *data, size_t offset,
  3536. size_t size) try {
  3537. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  3538. ggml_sycl_set_device(ctx->device);
  3539. auto stream = &(dpct::dev_mgr::instance().get_device(ctx->device).default_queue());
  3540. SYCL_CHECK(
  3541. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  3542. char* host_buf = (char*)malloc(size);
  3543. memcpy(host_buf, data, size);
  3544. SYCL_CHECK(
  3545. CHECK_TRY_ERROR((*stream).memcpy((char *)tensor->data + offset, host_buf, size)
  3546. .wait()));
  3547. free(host_buf);
  3548. }
  3549. catch (sycl::exception const &exc) {
  3550. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3551. << ", line:" << __LINE__ << std::endl;
  3552. std::exit(1);
  3553. }
  3554. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  3555. const ggml_tensor *tensor,
  3556. void *data, size_t offset,
  3557. size_t size) try {
  3558. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  3559. ggml_sycl_set_device(ctx->device);
  3560. auto stream = dpct::dev_mgr::instance().get_device(ctx->device).default_queue();
  3561. SYCL_CHECK(CHECK_TRY_ERROR(
  3562. stream.memcpy(data, (const char *)tensor->data + offset, size)
  3563. .wait()));
  3564. }
  3565. catch (sycl::exception const &exc) {
  3566. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3567. << ", line:" << __LINE__ << std::endl;
  3568. std::exit(1);
  3569. }
  3570. GGML_CALL static bool
  3571. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  3572. const ggml_tensor *src,
  3573. ggml_tensor *dst) try {
  3574. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  3575. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  3576. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)dst->buffer->context;
  3577. ggml_sycl_set_device(src_ctx->device);
  3578. /*
  3579. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  3580. error codes. The original code was commented out and a warning string
  3581. was inserted. You need to rewrite this code.
  3582. */
  3583. SYCL_CHECK(CHECK_TRY_ERROR(
  3584. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  3585. ggml_sycl_set_device(dst_ctx->device);
  3586. /*
  3587. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  3588. error codes. The original code was commented out and a warning string
  3589. was inserted. You need to rewrite this code.
  3590. */
  3591. SYCL_CHECK(CHECK_TRY_ERROR(
  3592. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  3593. /*
  3594. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  3595. error codes. The original code was commented out and a warning string
  3596. was inserted. You need to rewrite this code.
  3597. */
  3598. queue_ptr stream_dst = dst_ctx->stream;
  3599. queue_ptr stream_src = src_ctx->stream;
  3600. size_t size = ggml_nbytes(src);
  3601. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  3602. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  3603. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  3604. #if 0
  3605. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  3606. (char *)dst->data, (const char *)src->data, size).wait()));
  3607. /*
  3608. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  3609. error codes. The original code was commented out and a warning string
  3610. was inserted. You need to rewrite this code.
  3611. */
  3612. SYCL_CHECK(CHECK_TRY_ERROR(
  3613. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  3614. #endif
  3615. return true;
  3616. }
  3617. return false;
  3618. }
  3619. catch (sycl::exception const &exc) {
  3620. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3621. << ", line:" << __LINE__ << std::endl;
  3622. std::exit(1);
  3623. }
  3624. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  3625. uint8_t value) try {
  3626. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  3627. ggml_sycl_set_device(ctx->device);
  3628. queue_ptr stream = ctx->stream;
  3629. SYCL_CHECK(
  3630. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  3631. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  3632. .memset(ctx->dev_ptr, value, buffer->size)
  3633. .wait()));
  3634. }
  3635. catch (sycl::exception const &exc) {
  3636. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3637. << ", line:" << __LINE__ << std::endl;
  3638. std::exit(1);
  3639. }
  3640. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  3641. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  3642. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  3643. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  3644. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  3645. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  3646. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  3647. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  3648. /* .clear = */ ggml_backend_sycl_buffer_clear,
  3649. /* .reset = */ NULL,
  3650. };
  3651. // sycl buffer type
  3652. struct ggml_backend_sycl_buffer_type_context {
  3653. int device;
  3654. std::string name;
  3655. // each buffer type has its own stream
  3656. queue_ptr stream = nullptr;
  3657. };
  3658. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  3659. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  3660. return ctx->name.c_str();
  3661. }
  3662. GGML_CALL static ggml_backend_buffer_t
  3663. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  3664. size_t size) try {
  3665. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  3666. ggml_sycl_set_device(buft_ctx->device);
  3667. const queue_ptr stream = buft_ctx->stream;
  3668. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  3669. void * dev_ptr;
  3670. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  3671. size, *stream)));
  3672. if (!dev_ptr) {
  3673. fprintf(stderr, "%s: can't malloc %lu Bytes memory on device", __func__, size);
  3674. return nullptr;
  3675. }
  3676. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr, buft_ctx->stream);
  3677. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  3678. }
  3679. catch (sycl::exception const &exc) {
  3680. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3681. << ", line:" << __LINE__ << std::endl;
  3682. std::exit(1);
  3683. }
  3684. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  3685. return 128;
  3686. UNUSED(buft);
  3687. }
  3688. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  3689. return dpct::get_current_device().get_max_mem_alloc_size();
  3690. UNUSED(buft);
  3691. }
  3692. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  3693. size_t size = ggml_nbytes(tensor);
  3694. int64_t ne0 = tensor->ne[0];
  3695. if (ggml_is_quantized(tensor->type)) {
  3696. if (ne0 % MATRIX_ROW_PADDING != 0) {
  3697. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  3698. }
  3699. }
  3700. return size;
  3701. UNUSED(buft);
  3702. }
  3703. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  3704. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  3705. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  3706. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  3707. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  3708. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  3709. /* .is_host = */ nullptr,
  3710. };
  3711. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
  3712. static std::mutex mutex;
  3713. std::lock_guard<std::mutex> lock(mutex);
  3714. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  3715. if (device>=ggml_sycl_info().device_count or device<0) {
  3716. printf("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
  3717. device, ggml_sycl_info().device_count-1);
  3718. GGML_ASSERT(device<ggml_sycl_info().device_count);
  3719. }
  3720. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  3721. static bool ggml_backend_sycl_buffer_type_initialized = false;
  3722. if (!ggml_backend_sycl_buffer_type_initialized) {
  3723. for (int i = 0; i < ggml_sycl_info().device_count; i++) {
  3724. auto & device_i = dpct::dev_mgr::instance().get_device(i);
  3725. queue_ptr stream = &(device_i.default_queue());
  3726. ggml_backend_sycl_buffer_types[i] = {
  3727. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  3728. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(i), stream},
  3729. };
  3730. }
  3731. ggml_backend_sycl_buffer_type_initialized = true;
  3732. }
  3733. return &ggml_backend_sycl_buffer_types[device];
  3734. }
  3735. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(ggml_backend_sycl_context * ctx) {
  3736. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  3737. int device = ctx->device;
  3738. if (device>=ggml_sycl_info().device_count or device<0) {
  3739. printf("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
  3740. device, ggml_sycl_info().device_count-1);
  3741. GGML_ASSERT(device<ggml_sycl_info().device_count);
  3742. }
  3743. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  3744. static bool ggml_backend_sycl_buffer_type_initialized = false;
  3745. if (!ggml_backend_sycl_buffer_type_initialized) {
  3746. for (int i = 0; i < ggml_sycl_info().device_count; i++) {
  3747. ggml_backend_sycl_buffer_types[i] = {
  3748. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  3749. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(i), ctx->stream(i, 0)},
  3750. };
  3751. }
  3752. ggml_backend_sycl_buffer_type_initialized = true;
  3753. }
  3754. return &ggml_backend_sycl_buffer_types[device];
  3755. }
  3756. // sycl split buffer type
  3757. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split, int id) {
  3758. const int64_t nrows = ggml_nrows(tensor);
  3759. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  3760. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  3761. *row_low -= *row_low % rounding;
  3762. if (id == ggml_sycl_info().device_count - 1) {
  3763. *row_high = nrows;
  3764. } else {
  3765. *row_high = nrows*tensor_split[id + 1];
  3766. *row_high -= *row_high % rounding;
  3767. }
  3768. }
  3769. struct ggml_backend_sycl_split_buffer_context {
  3770. ~ggml_backend_sycl_split_buffer_context() try {
  3771. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  3772. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3773. for (int64_t is = 0; is < GGML_SYCL_MAX_STREAMS; ++is) {
  3774. if (extra->events[i][is] != nullptr) {
  3775. /*
  3776. DPCT1009:206: SYCL uses exceptions to report errors and
  3777. does not use the error codes. The original code was
  3778. commented out and a warning string was inserted. You
  3779. need to rewrite this code.
  3780. */
  3781. SYCL_CHECK(CHECK_TRY_ERROR(
  3782. dpct::destroy_event(extra->events[i][is])));
  3783. }
  3784. }
  3785. if (extra->data_device[i] != nullptr) {
  3786. /*
  3787. DPCT1009:207: SYCL uses exceptions to report errors and does
  3788. not use the error codes. The original code was commented out
  3789. and a warning string was inserted. You need to rewrite this
  3790. code.
  3791. */
  3792. ggml_sycl_set_device(i);
  3793. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  3794. extra->data_device[i], *(streams[i]))));
  3795. }
  3796. }
  3797. delete extra;
  3798. }
  3799. }
  3800. catch (sycl::exception const &exc) {
  3801. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3802. << ", line:" << __LINE__ << std::endl;
  3803. std::exit(1);
  3804. }
  3805. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  3806. std::vector<queue_ptr> streams;
  3807. };
  3808. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  3809. return GGML_SYCL_NAME "_Split";
  3810. UNUSED(buffer);
  3811. }
  3812. static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  3813. return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  3814. }
  3815. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  3816. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  3817. delete ctx;
  3818. }
  3819. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  3820. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  3821. return (void *)0x1000;
  3822. UNUSED(buffer);
  3823. }
  3824. GGML_CALL static void
  3825. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  3826. ggml_tensor *tensor) try {
  3827. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  3828. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  3829. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  3830. const int64_t ne0 = tensor->ne[0];
  3831. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  3832. ctx->tensor_extras.push_back(extra);
  3833. ctx->streams.push_back(&(dpct::get_current_device().default_queue()));
  3834. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3835. int64_t row_low, row_high;
  3836. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  3837. int64_t nrows_split = row_high - row_low;
  3838. if (nrows_split == 0) {
  3839. continue;
  3840. }
  3841. size_t size = ggml_nbytes_split(tensor, nrows_split);
  3842. const size_t original_size = size;
  3843. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  3844. if (ne0 % MATRIX_ROW_PADDING != 0) {
  3845. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  3846. }
  3847. // FIXME: do not crash if cudaMalloc fails
  3848. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  3849. ggml_sycl_set_device(i);
  3850. const queue_ptr stream = ctx->streams[i];
  3851. char * buf;
  3852. /*
  3853. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  3854. error codes. The original code was commented out and a warning string
  3855. was inserted. You need to rewrite this code.
  3856. */
  3857. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  3858. size, *stream)));
  3859. if (!buf) {
  3860. char err_buf[1024];
  3861. snprintf(err_buf, 1023, "%s: can't malloc %lu Bytes memory on device", __func__, size);
  3862. throw std::runtime_error(err_buf);
  3863. }
  3864. // set padding to 0 to avoid possible NaN values
  3865. if (size > original_size) {
  3866. /*
  3867. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  3868. the error codes. The original code was commented out and a warning
  3869. string was inserted. You need to rewrite this code.
  3870. */
  3871. SYCL_CHECK(CHECK_TRY_ERROR(
  3872. (*stream)
  3873. .memset(buf + original_size, 0, size - original_size)
  3874. .wait()));
  3875. }
  3876. extra->data_device[i] = buf;
  3877. for (int64_t is = 0; is < GGML_SYCL_MAX_STREAMS; ++is) {
  3878. /*
  3879. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  3880. the error codes. The original code was commented out and a warning
  3881. string was inserted. You need to rewrite this code.
  3882. */
  3883. SYCL_CHECK(
  3884. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  3885. }
  3886. }
  3887. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  3888. tensor->extra = extra;
  3889. }
  3890. catch (sycl::exception const &exc) {
  3891. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3892. << ", line:" << __LINE__ << std::endl;
  3893. std::exit(1);
  3894. }
  3895. GGML_CALL static void
  3896. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  3897. ggml_tensor *tensor, const void *data,
  3898. size_t offset, size_t size) try {
  3899. // split tensors must always be set in their entirety at once
  3900. GGML_ASSERT(offset == 0);
  3901. GGML_ASSERT(size == ggml_nbytes(tensor));
  3902. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  3903. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  3904. const int64_t ne0 = tensor->ne[0];
  3905. const size_t nb1 = tensor->nb[1];
  3906. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  3907. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3908. int64_t row_low, row_high;
  3909. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  3910. int64_t nrows_split = row_high - row_low;
  3911. if (nrows_split == 0) {
  3912. continue;
  3913. }
  3914. const size_t offset_split = row_low*nb1;
  3915. size_t size = ggml_nbytes_split(tensor, nrows_split);
  3916. const size_t original_size = size;
  3917. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  3918. if (ne0 % MATRIX_ROW_PADDING != 0) {
  3919. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  3920. }
  3921. const char * buf_host = (const char *)data + offset_split;
  3922. /*
  3923. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  3924. error codes. The original code was commented out and a warning string
  3925. was inserted. You need to rewrite this code.
  3926. */
  3927. ggml_sycl_set_device(i);
  3928. const queue_ptr stream = ctx->streams[i];
  3929. SYCL_CHECK(CHECK_TRY_ERROR(
  3930. (*stream)
  3931. .memcpy(extra->data_device[i], buf_host, original_size)
  3932. .wait()));
  3933. }
  3934. }
  3935. catch (sycl::exception const &exc) {
  3936. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3937. << ", line:" << __LINE__ << std::endl;
  3938. std::exit(1);
  3939. }
  3940. GGML_CALL static void
  3941. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  3942. const ggml_tensor *tensor, void *data,
  3943. size_t offset, size_t size) try {
  3944. // split tensors must always be set in their entirety at once
  3945. GGML_ASSERT(offset == 0);
  3946. GGML_ASSERT(size == ggml_nbytes(tensor));
  3947. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  3948. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  3949. const int64_t ne0 = tensor->ne[0];
  3950. const size_t nb1 = tensor->nb[1];
  3951. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  3952. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3953. int64_t row_low, row_high;
  3954. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  3955. int64_t nrows_split = row_high - row_low;
  3956. if (nrows_split == 0) {
  3957. continue;
  3958. }
  3959. const size_t offset_split = row_low*nb1;
  3960. size_t size = ggml_nbytes_split(tensor, nrows_split);
  3961. const size_t original_size = size;
  3962. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  3963. if (ne0 % MATRIX_ROW_PADDING != 0) {
  3964. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  3965. }
  3966. char * buf_host = (char *)data + offset_split;
  3967. /*
  3968. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  3969. error codes. The original code was commented out and a warning string
  3970. was inserted. You need to rewrite this code.
  3971. */
  3972. ggml_sycl_set_device(i);
  3973. const queue_ptr stream = ctx->streams[i];
  3974. SYCL_CHECK(CHECK_TRY_ERROR(
  3975. (*stream)
  3976. .memcpy(buf_host, extra->data_device[i], original_size)
  3977. .wait()));
  3978. }
  3979. }
  3980. catch (sycl::exception const &exc) {
  3981. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3982. << ", line:" << __LINE__ << std::endl;
  3983. std::exit(1);
  3984. }
  3985. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  3986. UNUSED(buffer);
  3987. UNUSED(value);
  3988. }
  3989. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  3990. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  3991. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  3992. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  3993. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  3994. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  3995. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  3996. /* .cpy_tensor = */ NULL,
  3997. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  3998. /* .reset = */ NULL,
  3999. };
  4000. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  4001. return GGML_SYCL_NAME "_Split";
  4002. UNUSED(buft);
  4003. }
  4004. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  4005. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  4006. // instead, we allocate them for each tensor separately in init_tensor
  4007. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  4008. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  4009. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  4010. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  4011. }
  4012. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  4013. return 128;
  4014. UNUSED(buft);
  4015. }
  4016. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  4017. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  4018. size_t total_size = 0;
  4019. const int64_t ne0 = tensor->ne[0];
  4020. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  4021. int64_t row_low, row_high;
  4022. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  4023. int64_t nrows_split = row_high - row_low;
  4024. if (nrows_split == 0) {
  4025. continue;
  4026. }
  4027. total_size += ggml_nbytes_split(tensor, nrows_split);
  4028. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  4029. if (ne0 % MATRIX_ROW_PADDING != 0) {
  4030. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  4031. }
  4032. }
  4033. return total_size;
  4034. }
  4035. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  4036. return false;
  4037. UNUSED(buft);
  4038. }
  4039. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  4040. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  4041. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  4042. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  4043. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  4044. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  4045. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  4046. };
  4047. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  4048. static std::mutex mutex;
  4049. std::lock_guard<std::mutex> lock(mutex);
  4050. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  4051. ggml_check_sycl();
  4052. // FIXME: this is not thread safe
  4053. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  4054. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  4055. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  4056. if (all_zero) {
  4057. tensor_split_arr = ggml_sycl_info().default_tensor_split;
  4058. } else {
  4059. float split_sum = 0.0f;
  4060. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  4061. tensor_split_arr[i] = split_sum;
  4062. split_sum += tensor_split[i];
  4063. }
  4064. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  4065. tensor_split_arr[i] /= split_sum;
  4066. }
  4067. }
  4068. auto it = buft_map.find(tensor_split_arr);
  4069. if (it != buft_map.end()) {
  4070. return &it->second;
  4071. }
  4072. struct ggml_backend_buffer_type buft {
  4073. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  4074. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  4075. };
  4076. auto result = buft_map.emplace(tensor_split_arr, buft);
  4077. return &result.first->second;
  4078. }
  4079. // host buffer type
  4080. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  4081. return GGML_SYCL_NAME "_Host";
  4082. UNUSED(buft);
  4083. }
  4084. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  4085. return GGML_SYCL_NAME "_Host";
  4086. UNUSED(buffer);
  4087. }
  4088. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  4089. ggml_sycl_host_free(buffer->context);
  4090. }
  4091. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  4092. void * ptr = ggml_sycl_host_malloc(size);
  4093. if (ptr == nullptr) {
  4094. // fallback to cpu buffer
  4095. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  4096. }
  4097. // FIXME: this is a hack to avoid having to implement a new buffer type
  4098. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  4099. buffer->buft = buft;
  4100. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  4101. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  4102. return buffer;
  4103. }
  4104. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  4105. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  4106. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  4107. /* .iface = */ {
  4108. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  4109. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  4110. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  4111. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  4112. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  4113. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  4114. },
  4115. /* .context = */ nullptr,
  4116. };
  4117. return &ggml_backend_sycl_buffer_type_host;
  4118. }
  4119. // backend
  4120. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  4121. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4122. return sycl_ctx->name.c_str();
  4123. }
  4124. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  4125. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4126. delete sycl_ctx;
  4127. delete backend;
  4128. }
  4129. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  4130. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4131. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  4132. }
  4133. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  4134. ggml_tensor *tensor,
  4135. const void *data, size_t offset,
  4136. size_t size) try {
  4137. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4138. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  4139. GGML_ASSERT(buf->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  4140. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4141. SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
  4142. (char *)tensor->data + offset, data, size).wait()));
  4143. }
  4144. catch (sycl::exception const &exc) {
  4145. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4146. << ", line:" << __LINE__ << std::endl;
  4147. std::exit(1);
  4148. }
  4149. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  4150. const ggml_tensor *tensor,
  4151. void *data, size_t offset,
  4152. size_t size) try {
  4153. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4154. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  4155. GGML_ASSERT(buf->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  4156. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4157. SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
  4158. data, (const char *)tensor->data + offset, size).wait()));
  4159. }
  4160. catch (sycl::exception const &exc) {
  4161. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4162. << ", line:" << __LINE__ << std::endl;
  4163. std::exit(1);
  4164. }
  4165. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  4166. const ggml_tensor *src,
  4167. ggml_tensor *dst) try {
  4168. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4169. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  4170. /*
  4171. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  4172. error codes. The original code was commented out and a warning string
  4173. was inserted. You need to rewrite this code.
  4174. */
  4175. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4176. SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
  4177. dst->data, src->data, ggml_nbytes(dst)).wait()));
  4178. return true;
  4179. }
  4180. return false;
  4181. }
  4182. catch (sycl::exception const &exc) {
  4183. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4184. << ", line:" << __LINE__ << std::endl;
  4185. std::exit(1);
  4186. }
  4187. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  4188. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4189. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4190. SYCL_CHECK(CHECK_TRY_ERROR((stream)->wait()));
  4191. UNUSED(backend);
  4192. }
  4193. catch (sycl::exception const &exc) {
  4194. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4195. << ", line:" << __LINE__ << std::endl;
  4196. std::exit(1);
  4197. }
  4198. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  4199. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4200. ggml_sycl_set_main_device(sycl_ctx->device);
  4201. for (int i = 0; i < cgraph->n_nodes; i++) {
  4202. ggml_tensor * node = cgraph->nodes[i];
  4203. if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  4204. continue;
  4205. }
  4206. #ifndef NDEBUG
  4207. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  4208. for (int j = 0; j < GGML_MAX_SRC; j++) {
  4209. if (node->src[j] != nullptr) {
  4210. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  4211. }
  4212. }
  4213. #endif
  4214. bool ok = ggml_sycl_compute_forward(*sycl_ctx, node);
  4215. if (!ok) {
  4216. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  4217. }
  4218. GGML_ASSERT(ok);
  4219. }
  4220. return GGML_STATUS_SUCCESS;
  4221. }
  4222. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  4223. switch (op->op) {
  4224. case GGML_OP_CONV_TRANSPOSE_1D:
  4225. {
  4226. ggml_type src0_type = op->src[0]->type;
  4227. ggml_type src1_type = op->src[1]->type;
  4228. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4229. return true;
  4230. }
  4231. return false;
  4232. } break;
  4233. case GGML_OP_UNARY:
  4234. switch (ggml_get_unary_op(op)) {
  4235. case GGML_UNARY_OP_GELU:
  4236. case GGML_UNARY_OP_SILU:
  4237. case GGML_UNARY_OP_RELU:
  4238. case GGML_UNARY_OP_HARDSIGMOID:
  4239. case GGML_UNARY_OP_HARDSWISH:
  4240. case GGML_UNARY_OP_GELU_QUICK:
  4241. case GGML_UNARY_OP_TANH:
  4242. return ggml_is_contiguous(op->src[0]);
  4243. default:
  4244. return false;
  4245. }
  4246. break;
  4247. case GGML_OP_MUL_MAT:
  4248. case GGML_OP_MUL_MAT_ID:
  4249. {
  4250. struct ggml_tensor * a;
  4251. struct ggml_tensor * b;
  4252. if (op->op == GGML_OP_MUL_MAT) {
  4253. a = op->src[0];
  4254. b = op->src[1];
  4255. } else {
  4256. a = op->src[2];
  4257. b = op->src[1];
  4258. }
  4259. if (a->ne[3] != b->ne[3]) {
  4260. return false;
  4261. }
  4262. ggml_type a_type = a->type;
  4263. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
  4264. a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
  4265. a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
  4266. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
  4267. ) {
  4268. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  4269. return false;
  4270. }
  4271. }
  4272. ggml_type src0_type = op->src[0]->type;
  4273. if (src0_type == GGML_TYPE_BF16) {
  4274. return false;
  4275. }
  4276. return true;
  4277. } break;
  4278. case GGML_OP_GET_ROWS:
  4279. {
  4280. switch (op->src[0]->type) {
  4281. case GGML_TYPE_F16:
  4282. case GGML_TYPE_F32:
  4283. case GGML_TYPE_Q4_0:
  4284. case GGML_TYPE_Q4_1:
  4285. case GGML_TYPE_Q5_0:
  4286. case GGML_TYPE_Q5_1:
  4287. case GGML_TYPE_Q8_0:
  4288. return true;
  4289. default:
  4290. return false;
  4291. }
  4292. } break;
  4293. case GGML_OP_CPY:
  4294. {
  4295. ggml_type src0_type = op->src[0]->type;
  4296. ggml_type src1_type = op->src[1]->type;
  4297. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4298. return true;
  4299. }
  4300. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  4301. return true;
  4302. }
  4303. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  4304. return true;
  4305. }
  4306. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  4307. return true;
  4308. }
  4309. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  4310. return true;
  4311. }
  4312. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4313. return true;
  4314. }
  4315. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4316. return true;
  4317. }
  4318. return false;
  4319. } break;
  4320. case GGML_OP_CONCAT:
  4321. {
  4322. ggml_type src0_type = op->src[0]->type;
  4323. int dim = op->op_params[0];
  4324. return ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16 && dim == 2;
  4325. } break;
  4326. case GGML_OP_DUP:
  4327. case GGML_OP_NONE:
  4328. case GGML_OP_RESHAPE:
  4329. case GGML_OP_REPEAT:
  4330. case GGML_OP_VIEW:
  4331. case GGML_OP_PERMUTE:
  4332. case GGML_OP_TRANSPOSE:
  4333. case GGML_OP_NORM:
  4334. case GGML_OP_ADD:
  4335. case GGML_OP_MUL:
  4336. case GGML_OP_DIV:
  4337. case GGML_OP_RMS_NORM:
  4338. case GGML_OP_SCALE:
  4339. case GGML_OP_SQR:
  4340. case GGML_OP_CLAMP:
  4341. case GGML_OP_CONT:
  4342. case GGML_OP_DIAG_MASK_INF:
  4343. case GGML_OP_SOFT_MAX:
  4344. return true;
  4345. case GGML_OP_ROPE:
  4346. return ggml_is_contiguous(op->src[0]);
  4347. case GGML_OP_IM2COL:
  4348. case GGML_OP_POOL_2D:
  4349. case GGML_OP_SUM_ROWS:
  4350. case GGML_OP_ARGSORT:
  4351. case GGML_OP_ACC:
  4352. case GGML_OP_GROUP_NORM:
  4353. case GGML_OP_UPSCALE:
  4354. case GGML_OP_PAD:
  4355. case GGML_OP_LEAKY_RELU:
  4356. case GGML_OP_TIMESTEP_EMBEDDING:
  4357. return true;
  4358. default:
  4359. return false;
  4360. }
  4361. UNUSED(backend);
  4362. }
  4363. GGML_CALL static bool ggml_backend_sycl_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  4364. const int min_batch_size = 32;
  4365. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS && op->op != GGML_OP_MUL_MAT_ID;
  4366. GGML_UNUSED(backend);
  4367. }
  4368. GGML_CALL static bool ggml_backend_sycl_supports_buft(ggml_backend_t backend, ggml_backend_buffer_type_t buft) {
  4369. if (buft->iface.get_name != ggml_backend_sycl_buffer_type_name) {
  4370. return false;
  4371. }
  4372. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  4373. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4374. return buft_ctx->device == sycl_ctx->device;
  4375. }
  4376. static ggml_backend_i ggml_backend_sycl_interface = {
  4377. /* .get_name = */ ggml_backend_sycl_name,
  4378. /* .free = */ ggml_backend_sycl_free,
  4379. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  4380. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  4381. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  4382. /* .cpy_tensor_async = */ NULL, //ggml_backend_sycl_cpy_tensor_async, // TODO: update for the new interface
  4383. /* .synchronize = */ ggml_backend_sycl_synchronize,
  4384. /* .graph_plan_create = */ NULL,
  4385. /* .graph_plan_free = */ NULL,
  4386. /* .graph_plan_update = */ NULL,
  4387. /* .graph_plan_compute = */ NULL,
  4388. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  4389. /* .supports_op = */ ggml_backend_sycl_supports_op,
  4390. /* .supports_buft = */ ggml_backend_sycl_supports_buft,
  4391. /* .offload_op = */ ggml_backend_sycl_offload_op,
  4392. /* .event_new = */ NULL,
  4393. /* .event_free = */ NULL,
  4394. /* .event_record = */ NULL,
  4395. /* .event_wait = */ NULL,
  4396. /* .event_synchronize = */ NULL,
  4397. };
  4398. static ggml_guid_t ggml_backend_sycl_guid() {
  4399. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  4400. return &guid;
  4401. }
  4402. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  4403. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  4404. ggml_check_sycl();
  4405. check_allow_gpu_index(device);
  4406. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context(device);
  4407. if (ctx == nullptr) {
  4408. fprintf(stderr, "%s: error: failed to allocate context\n", __func__);
  4409. return nullptr;
  4410. };
  4411. ggml_backend_t sycl_backend = new ggml_backend {
  4412. /* .guid = */ ggml_backend_sycl_guid(),
  4413. /* .interface = */ ggml_backend_sycl_interface,
  4414. /* .context = */ ctx
  4415. };
  4416. return sycl_backend;
  4417. }
  4418. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  4419. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  4420. }
  4421. GGML_CALL int ggml_backend_sycl_get_device_count() {
  4422. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  4423. return ggml_sycl_info().device_count;
  4424. }
  4425. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  4426. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  4427. return sycl_backend;
  4428. UNUSED(params);
  4429. }
  4430. extern "C" int ggml_backend_sycl_reg_devices();
  4431. int ggml_backend_sycl_reg_devices() {
  4432. assert(ggml_sycl_info().device_count>0);
  4433. for (int i = 0; i < ggml_sycl_info().device_count; i++) {
  4434. char name[128];
  4435. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, i);
  4436. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  4437. }
  4438. return ggml_sycl_info().device_count;
  4439. }